1

About Code: https://github.com/substrate-developer-hub/substrate-parachain-template/blob/main/runtime/src/lib.rs#L130-L145

pub struct WeightToFee;
impl WeightToFeePolynomial for WeightToFee {
    type Balance = Balance;
    fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
        // in Rococo, extrinsic base weight (smallest non-zero weight) is mapped to 1 MILLIUNIT:
        // in our template, we map to 1/10 of that, or 1/10 MILLIUNIT
        let p = MILLIUNIT / 10;
        let q = 100 * Balance::from(ExtrinsicBaseWeight::get());
        smallvec![WeightToFeeCoefficient {
            degree: 1,
            negative: false,
            coeff_frac: Perbill::from_rational(p % q, q),
            coeff_integer: p / q,
        }]
    }
}

I don't really understand what Perbill:: from_Rational (P % q, q) means. Has anyone tried to plot this polynomial in rectangular coordinates?

1 Answer 1

3

It just approximates p/q (in this line, (p % q) / q) as a PerBill: https://github.com/paritytech/substrate/blob/2195448d01fc935912d8cf543be6d609f5899652/primitives/arithmetic/src/per_things.rs#L265L283


EDIT: I see the question is more about the polynomial than the from_rational function.

The polynomial is just of the form a*x^n + b*x^(n-1) + ... where x is the independent variable, {a, b, ...} are coefficients and the exponent of each term is its degree. You see that the polynomial actually returns a vec of WeightToFeeCoefficient structs. The degree tells you the exponent, negative tells you if it's positive or negative, and coeff_integer + coeff_frac tells you the constant coefficient on the term.

Since there is only one term in this polynomial, and Weight is the independent variable, it would be the function:

weight fee: Balance = (p / q + (p % q) / q) * Weight;

This isn't the final fee, as the length fee will also be added and then the weight fee will be adjusted based on the NextFeeMultiplier.

3
  • If p is 10000000 and q is 1250000000, then the corresponding polynomial is Perbill(80000000)*x. So how much balance does 1 weight correspond to?
    – 99kies
    Apr 27, 2022 at 11:42
  • Edited the post to address Apr 27, 2022 at 14:21
  • You mention (p % q) / q approximates p/q but I'm having a hard time wrapping my head around it. Can you explain it a bit more? Why (p % q) / q ? Jun 20, 2023 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.