# How to calculate fee xcm message at source chain and destination chain?

I am looking for a way to charge for xcm message.

(I know this topic has been discussed a lot, but I still haven't found my own answer, and haven't written a script to test, so I want to open this discussion further ^.^). As far as I know, charging for xcm messages has two steps.

Step 1: Calculate the total weight for all instructions that xcm uses. Here is the formula I found:

``````weight = base_weight + (num_assets * weight_per_asset) + (asset_id.len() * weight_per_byte)
``````

Step 2: Calculate fee from the weight given in step 1. I found the following code:

``````pub fn estimate_fee_for_weight(weight: Weight) -> u128 {
let (_, units_per_second, units_per_mb) = TokensPerSecondPerMegabyte::get();

units_per_second * (weight.ref_time() as u128) / (WEIGHT_REF_TIME_PER_SECOND as u128)
units_per_mb * (weight.proof_size() as u128) / (WEIGHT_PROOF_SIZE_PER_MB as u128)
}
``````

1. How is the value of `unit_per_second` and `units_per_mb` calculated?
2. It seems that Weight is the common unit of all tokens (Dot, Kusama, Moonbeam, ...) so with different tokens, how to convert weight to a token like it? That is, how will `weight to dot` differ from `weight to KSM`? How to calculate?
3. How to determine the weight limit in xcm?
4. How can I find the base_weight of all instructions?

Thank you for your help. Some reference I found: https://github.com/paritytech/xcm-docs.git

1. How is the value of unit_per_second and units_per_mb calculated?

The functions you are using are a implementation for the XCM Docs example, in this case the `unit_per_second` and `units_per_mb` 1_000_000_000_000 and 1024 * 1024, and they are are specified in the code (see link):

``````pub TokensPerSecondPerMegabyte: (AssetId, u128, u128) =
(Concrete(TokenLocation::get()), 1_000_000_000_000, 1024 * 1024);
``````
1. It seems that Weight is the common unit of all tokens (Dot, Kusama, Moonbeam, ...) so with different tokens, how to convert weight to a token like it? That is, how will weight to dot differ from weight to KSM? How to calculate?

A configuration item in the Executor Config: `Trader`, is the responsible to convert the required weight units into fees

1. How to determine the weight limit in xcm?

By using `WeightLimit::Limited()`, you guarantee the message will error if it tries to use more weight than you expect, if you don't mind this, you can use `WeightLimit::Unlimited`.

1. How can I find the base_weight of all instructions?

For example Polkadot currently uses a fixed amount of weight for all XCM instructions, which is 1,000,000,000 weight units per instruction. Is defined in the config (See link to the code). In the XCM docs example you are looking this is a bit less, 1,000(See code).