Skip to main content

Quadratic weighting struct

A tool for weighting more complex transactions.

Goal#

Calculate transaction weights for transactions with 3 or more parameters.

Use cases#

Calculate correct weight based on data within a function, required to calculate transaction fees.

Overview#

This is a more complex way scale to weight transactions. It computes weight according to the following formula:

ax^2 + by + c

Where a, b, and c are fields in the struct, and x and y are transaction parameters. Have a look at the [examples][#examples] section to see it implemented and used in context.

Steps#

1. Write the Quadratic struct#

Write a weighting struct that weighs transactions where the first parameter is a boolean value.

pub struct Quadratic(u32, u32, u32);
impl WeighData<(&u32, &u32)> for Quadratic {
fn weigh_data(&self, (x, y): (&u32, &u32)) -> Weight {
let ax2 = x.saturating_mul(*x).saturating_mul(self.0);
let by = y.saturating_mul(self.1);
let c = self.2;
ax2.saturating_add(by).saturating_add(c).into()
}
}

2. Classify dispatch calls#

Since this implementation of WeighData requires a DispatchClass, use default to classify all calls as normal.

// Implement ClassifyDispatch.
impl<T> ClassifyDispatch<T> for Quadratic {
fn classify_dispatch(&self, _: T) -> DispatchClass {
// Classify all calls as Normal (which is the default)
Default::default()
}
}

3. Implement PaysFee#

Last, specify how PaysFee is used for the custom WeighData struct.

// Implement PaysFee.
impl<T> PaysFee<T> for Quadratic {
fn pays_fee(&self, _: T) -> Pays {
Pays::Yes
}
}

Examples#

  • Feeless transaction pallet
  • pallet-weights

Related material#

How-to guides#

Knowledgebase#

Other#

Was this guide useful?