Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
AurevoirXavier committed Jul 10, 2023
1 parent 4d5e871 commit 7687790
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ use dc_primitives::*;
use frame_support::{
sp_runtime::Perbill,
weights::{
constants::ExtrinsicBaseWeight, WeightToFeeCoefficient, WeightToFeeCoefficients,
WeightToFeePolynomial,
constants::ExtrinsicBaseWeight, Weight, WeightToFee as WeightToFeeT,
WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial,
},
};

Expand All @@ -64,7 +64,21 @@ macro_rules! fast_runtime_or_not {
/// - Setting it to `0` will essentially disable the weight fee.
/// - Setting it to `1` will cause the literal `#[weight = x]` values to be charged.
pub struct WeightToFee;
impl WeightToFeePolynomial for WeightToFee {
impl WeightToFeeT for WeightToFee {
type Balance = Balance;

fn weight_to_fee(weight: &Weight) -> Self::Balance {
let time_poly: FeePolynomial<Balance> = RefTimeToFee::polynomial().into();
let proof_poly: FeePolynomial<Balance> = ProofSizeToFee::polynomial().into();

// Take the maximum instead of the sum to charge by the more scarce resource.
time_poly.eval(weight.ref_time()).max(proof_poly.eval(weight.proof_size()))
}
}

/// Maps the reference time component of `Weight` to a fee.
pub struct RefTimeToFee;
impl WeightToFeePolynomial for RefTimeToFee {
type Balance = Balance;

fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
Expand All @@ -80,6 +94,25 @@ impl WeightToFeePolynomial for WeightToFee {
}
}

/// Maps the proof size component of `Weight` to a fee.
pub struct ProofSizeToFee;
impl WeightToFeePolynomial for ProofSizeToFee {
type Balance = Balance;

fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
// Map 1MB proof to 1 UNIT.
let p = UNIT;
let q = 10_000_000;

smallvec![WeightToFeeCoefficient {
degree: 1,
negative: false,
coeff_frac: Perbill::from_rational(p % q, q),
coeff_integer: p / q,
}]
}
}

pub struct DealWithFees<R>(sp_std::marker::PhantomData<R>);
impl<R> frame_support::traits::OnUnbalanced<pallet_balances::NegativeImbalance<R>>
for DealWithFees<R>
Expand Down

0 comments on commit 7687790

Please sign in to comment.