In fact, it is almost impossible to have an algorithm to calculate the XCM fees for all chains. This is because the calculation of XCM fees is related to the following points, and each chain may have different configurations.
- Weigher: Means of measuring the weight consumed by an XCM message locally.
It seems that most chains currently use
FixedWeightBounds, but the
UnitWeightCost used by each chain may be different.
- Trader: The means of purchasing weight credit for XCM execution.
Even on the same chain, different tokens may have different
Trader because their values are different, they need different
WeightToFee. For example, Acala does it this way.
However, I would like to write down the XCM fee calculation process at an abstract level here, so that we can do the calculation for different chains.
The calculation of XCM fees can be divided into two steps. The first step is to calculate the weight, and the second step is to calculate the fee with the weight.
For the weight, most chains that use
(Note that instructions used in
SetAppendix also count towards the instruction count)
WeightToFeeto calculate fee. There are two commonly used methods.
For @VitalyPeryatin's comment:
Where can I found what chains use FixedWeightBounds for the weight?
You could check the
impl xcm_executor::Config code in the repo of the chain. For example, Darwinia config
type Weigher = FixedWeightBounds.
What other types exists except FixedWeightBounds?
WeightInfoBounds, and Westend used it here. Note any struct which impl trait WeightBounds could be used here.
Where can I found Transact weight?
Transact is an instruction in XCM, if not used, the weight is 0, if used, it can be obtained from its parameters
require_weight_at_most. (Just a reminder: This weight is not the actual weight required to execute the
call, but an upper limit weight specified for
call execution when sending XCM.)