Conditional weighting struct
There's always more than one way—but those can wait.
#
GoalCreate and use a custom weighting struct.
#
Use casesCalculate transaction fees by computing correct weights based on the data within a function.
#
OverviewSubstrate provides a mechanism known as transaction weighting to quantify the resources consumed while executing a transaction. This approach to a custom weight function establishes a weight value based on some condition. Once defined, it can be used directly in your pallet, written as such:
#[weight = Conditional(200)]
Here are the different traits we'll be implementing:
WeighData
: Weigh the data in a function.PaysFee
: Designate whether the dispatch pays a fee or not.ClassifyDispatch
: A way to tell the runtime about the type of dispatch being made.
#
StepsWeighData
struct#
1. Write the Write an implementation of WeighData
for Conditional
, where the first parameter is a boolean value:
#
2. Classify dispatch callsSince this implementation of WeighData
requires a DispatchClass
, use default
to classify all calls as normal:
PaysFee
#
3. Implement Last, specify how PaysFee
is used for the custom WeighData
struct:
#
Examples- pallet-weights
- pallet-feeless