Skip to content

XCM Fees on Moonbeam

Introduction

XCM aims to be a language that communicates ideas between consensus systems. Sending an XCM message consists of a series of instructions that are executed in both the origin and the destination chains. The combination of XCM instructions results in actions such as token transfers. In order to process and execute each XCM instruction, there are typically associated fees that must be paid.

However, XCM is designed to be general, extensible, and efficient so that it remains valuable and future-proof throughout a growing ecosystem. As such, the generality applies to concepts including payments of fees for XCM execution. In Ethereum, fees are baked into the transaction protocol, whereas in the Polkadot ecosystem, each chain has the flexibility to define how XCM fees are handled.

This guide will cover aspects of fee payment, such as who is responsible for paying XCM execution fees, how it is paid for, and how the fees are calculated on Moonbeam.

Note

The following information is provided for general information purposes only. The weight and extrinsic base cost might have changed since the time of writing. Please ensure you check the actual values, and never use the following information for production apps.

Payment of Fees

Generally speaking, the fee payment process can be described as follows:

  1. Some assets need to be provided
  2. The exchange of assets for computing time (or weight) must be negotiated
  3. The XCM operations will be performed as instructed, with the provided weight limit or funds available for execution

Each chain can configure what happens with the XCM fees and in which tokens they can be paid (either the native reserve token or an external one). For example:

  • Polkadot and Kusama - the fees are paid in DOT or KSM (respectively) and given to the validator of the block
  • Moonbeam and Moonriver - the XCM execution fees can be paid in the reserve asset (GLMR or MOVR, respectively), but also in assets originated in other chains if they are registered as an XCM execution asset. When XCM execution (token transfers or remote execution) is paid in the native chain reserve asset (GLMR or MOVR), 80% is burned, while 20% is sent to the treasury. When XCM execution is paid in a foreign asset, the fee is sent to the treasury

Consider the following scenario: Alice has some DOT on Polkadot, and she wants to transfer it to Alith on Moonbeam. She sends an XCM message with a set of XCM instructions that will retrieve a given amount of DOT from her account on Polkadot and mint them as xcDOT into Alith's account. Part of the instructions are executed on Polkadot, and the other part is executed on Moonbeam.

How does Alice pay Moonbeam to execute these instructions and fulfill her request? Her request is fulfilled through a series of XCM instructions that are included in the XCM message, which enables her to buy execution time minus any related XCM execution fees. The execution time is used to issue and transfer xcDOT, a representation of DOT on Moonbeam. This means that when Alice sends some DOT to Alith's account on Moonbeam, she'll receive a 1:1 representation of her DOT as xcDOT minus any XCM execution fees. Note that in this scenario, XCM execution fees are paid in xcDOT and sent to the treasury.

The exact process for Alice's transfer is as follows:

  1. Assets are sent to an account on Polkadot that is owned by Moonbeam, known as the Sovereign account. After the assets are received, an XCM message is sent to Moonbeam
  2. The XCM message in Moonbeam will:
    1. Mint the corresponding asset representation
    2. Buy the corresponding execution time
    3. Use that execution time to deposit the representation (minus fees) to the destination account

XCM Instructions

An XCM message is comprised of a series of XCM instructions. As a result, different combinations of XCM instructions result in different actions. For example, to move DOT to Moonbeam, the following XCM instructions are used:

For example, to move DOT to Moonbeam, the following XCM instructions are used:

  1. TransferReserveAsset - gets executed in Polkadot. Assets are transferred from the origin account and deposited into Moonbeam's Sovereign account on Polkadot
  2. ReserveAssetDeposited - gets executed in Moonbeam. Mints the DOT representation on Moonbeam, called xcDOT
  3. ClearOrigin - gets executed in Moonbeam. Clears origin information, which was Polkadot's Sovereign account on Moonbeam
  4. BuyExecution - gets executed in Moonbeam. As such, execution fees are determined by Moonbeam. In this particular scenario, part of the minted xcDOT is used to pay for XCM execution
  5. DepositAsset - gets executed in Moonbeam. Ultimately, it sends assets to a destination account on Moonbeam

To check how the instructions for an XCM message are built to transfer self-reserve assets to a target chain, such as DOT to Moonbeam, you can refer to the X-Tokens Open Runtime Module Library repository (as an example). You'll want to take a look at the transfer_self_reserve_asset function. You'll notice it calls TransferReserveAsset and passes in assets, dest, and xcm as parameters. In particular, the xcm parameter includes the BuyExecution and DepositAsset instructions. If you then head over to the Polkadot GitHub repository, you can find the TransferReserveAsset instruction. The XCM message is constructed by combining the ReserveAssetDeposited and ClearOrigin instructions with the xcm parameter, which as mentioned includes the BuyExecution and DepositAsset instructions.

To move xcDOT from Moonbeam back to Polkadot, the instructions that are used are:

  1. WithdrawAsset - gets executed in Moonbeam. Takes the funds from the sender
  2. InitiateReserveWithdraw - gets executed in Moonbeam. Burns the funds while sending an XCM message to the destination chain to execute the remainder of the token transfer
  3. WithdrawAsset - gets executed in Polkadot. Takes the funds from Moonbeam's Sovereign account on Polkadot
  4. ClearOrigin - gets executed in Polkadot. Clears origin information, which was Moonbeam's Sovereign account on Moonbeam
  5. BuyExecution - gets executed in Polkadot. As such, Polkadot determines the execution fees. In this scenario, part of the DOTs being sent are used to pay for the execution of the XCM
  6. DepositAsset - gets executed in Polkadot. Ultimately, it sends assets to a destination account on Polkadot

To check how the instructions for an XCM message are built to transfer reserve assets to a target chain, such as xcDOT to Polkadot, you can refer to the X-Tokens Open Runtime Module Library repository. You'll want to take a look at the transfer_to_reserve function. You'll notice that it calls WithdrawAsset, then InitiateReserveWithdraw and passes in assets, dest, and xcm as parameters. In particular, the xcm parameter includes the BuyExecution and DepositAsset instructions. If you then head over to the Polkadot GitHub repository, you can find the InitiateReserveWithdraw instruction. The XCM message is constructed by combining the WithdrawAsset and ClearOrigin instructions with the xcm parameter, which as mentioned includes the BuyExecution and DepositAsset instructions.

Relay Chain XCM Fee Calculation

Substrate has introduced a weight system that determines how heavy or, in other words, how expensive from a computational cost perspective an extrinsic is. One unit of weight is defined as one picosecond of execution time. When it comes to paying fees, users will pay a transaction fee based on the weight of the call that is being made, in addition to factors such as network congestion.

The following sections will break down how to calculate XCM fees for Polkadot and Kusama. It's important to note that Kusama, in particular, uses benchmarked data to determine the total weight costs for XCM instructions and that some XCM instructions might include database reads and writes, which add weight to the call.

There are two databases available in Polkadot and Kusama: RocksDB (which is the default) and ParityDB, both of which have their own associated weight costs for each network.

Polkadot

The total weight costs on Polkadot take into consideration database reads and writes in addition to the weight required for a given instruction. Polkadot uses benchmarked weights for instructions and database read and write operations. The breakdown of weight costs for the database operations is as follows:

Database Read Write
RocksDB (default) 20,499,000 83,471,000
ParityDB 11,826,000 38,052,000

Now that you are aware of the weight costs for database reads and writes on Polkadot, you can calculate the weight cost for a given instruction using the base weight for instructions.

For example, the WithdrawAsset instruction has a base weight of 25,567,000, and performs one database read and one database write. Therefore, the total weight cost of the WithdrawAsset instruction is calculated as:

25567000 + 20499000 + 83471000 = 129537000

The BuyExecution instruction has a base weight of 3,153,000 and doesn't include any database reads or writes. Therefore, the total weight cost of the BuyExecution instruction is 3,153,000.

On Polkadot, the benchmarked base weights are broken up into two categories: fungible and generic. Fungible weights are for XCM instructions that involve moving assets, and generic weights are for everything else. You can view the current weights for fungible assets and generic assets directly in the Polkadot Runtime code.

With the instruction weight cost established, you can calculate the cost of each instruction in DOT.

In Polkadot, the ExtrinsicBaseWeight is set to 126,045,000 which is mapped to 1/10th of a cent. Where 1 cent is 10^10 / 100.

Therefore, to calculate the cost of executing an XCM instruction, you can use the following formula:

XCM-DOT-Cost = XCMInstrWeight * DOTWeightToFeeCoefficient

Where DOTWeightToFeeCoefficient is a constant (map to 1 cent), and can be calculated as:

DOTWeightToFeeCoefficient = 10^10 / ( 10 * 100 * DOTExtrinsicBaseWeight )

Using the actual values:

DOTWeightToFeeCoefficient = 10^10 / ( 10 * 100 * 126045000 )

As a result, DOTWeightToFeeCoefficient is equal to 0.0793367448 Planck-DOT. Now, you can begin to calculate the final fee in DOT, using DOTWeightToFeeCoefficient as a constant and TotalWeight as the variable:

XCM-Planck-DOT-Cost = TotalWeight * DOTWeightToFeeCoefficient
XCM-DOT-Cost = XCM-Planck-DOT-Cost / DOTDecimalConversion

Therefore, the actual calculation for the WithdrawAsset instruction is:

XCM-Planck-DOT-Cost = 129537000 * 0.0793367448 
XCM-DOT-Cost = 10277043.9112 / 10^10

The total cost for that particular instruction is 0.0010277044 DOT.

As an example, you can calculate the total cost of DOT for sending an XCM message that transfers xcDOT to DOT on Polkadot using the following weights and instruction costs:

Instruction Weight Cost
WithdrawAsset 129,537,000 0.0010277044 DOT
ClearOrigin 3,010,000 0.0000238803 DOT
BuyExecution 3,153,000 0.0000250149 DOT
DepositAsset 129,786,000 0.0010296799 DOT
TOTAL 265,486,000 0.0021062795 DOT

Kusama

The total weight costs on Kusama take into consideration database reads and writes in addition to the weight required for a given instruction. Database read and write operations have not been benchmarked, while instruction weights have been. The breakdown of weight costs for the database operations is as follows:

Database Read Write
RocksDB (default) 25,000,000 100,000,000
ParityDB 8,000,000 50,000,000

Now that you are aware of the weight costs for database reads and writes on Kusama, you can calculate the weight cost for a given instruction using the base weight for instructions.

For example, the WithdrawAsset instruction has a base weight of 24,720,000, and performs one database read and one database write. Therefore, the total weight cost of the WithdrawAsset instruction is calculated as:

24720000 + 25000000 + 100000000 = 149720000

The BuyExecution instruction has a base weight of 2,714,000 and doesn't include any database reads or writes. Therefore, the total weight cost of the BuyExecution instruction is 2,714,000.

On Kusama, the benchmarked base weights are broken up into two categories: fungible and generic. Fungible weights are for XCM instructions that involve moving assets, and generic weights are for everything else. You can view the current weights for fungible assets and generic assets directly in the Kusama Runtime code.

With the instruction weight cost established, you can calculate the cost of the instruction in KSM.

In Kusama, the ExtrinsicBaseWeight is set to 124,706,000 which is mapped to 1/10th of a cent. Where 1 cent is 10^12 / 30,000.

Therefore, to calculate the cost of executing an XCM instruction, you can use the following formula:

XCM-KSM-Cost = XCMInstrWeight * KSMWeightToFeeCoefficient

Where KSMWeightToFeeCoefficient is a constant (map to 1 cent), and can be calculated as:

KSMWeightToFeeCoefficient = 10^12 / ( 10 * 3000 * KSMExtrinsicBaseWeight )

Using the actual values:

KSMWeightToFeeCoefficient = 10^12 / ( 10 * 3000 * 124706000 )

As a result, KSMWeightToFeeCoefficient is equal to 0.267295345319 Planck-KSM. Now, you can begin to calculate the final fee in KSM, using KSMWeightToFeeCoefficient as a constant and TotalWeight (149,720,000) as the variable:

XCM-Planck-KSM-Cost = TotalWeight * KSMWeightToFeeCoefficient
XCM-KSM-Cost = XCM-Planck-KSM-Cost / KSMDecimalConversion

Therefore, the actual calculation for the WithdrawAsset instruction is:

XCM-Planck-KSM-Cost = 149720000 * 0.267295345319 
XCM-KSM-Cost = 40019459.1011 / 10^12

The total cost for that particular instruction is 0.000040019459 KSM.

As an example, you can calculate the total cost of KSM for sending an XCM message that transfers xcKSM to KSM on Kusama using the following weights and instruction costs:

Instruction Weight Cost
WithdrawAsset 149,720,000 0.000040019459 KSM
ClearOrigin 2,614,000 0.000000698710 KSM
BuyExecution 2,714,000 0.000000725440 KSM
DepositAsset 150,904,000 0.000040335936 KSM
TOTAL 305,952,000 0.000081779545 KSM

Moonbeam-based Networks XCM Fee Calculation

Substrate has introduced a weight system that determines how heavy or, in other words, how expensive an extrinsic is from a computational cost perspective. One unit of weight is defined as one picosecond of execution time. When it comes to paying fees, users will pay a transaction fee based on the weight of the call that is being made, and each parachain can decide how to convert from weight to fee, for example, accounting for additional costs for transaction size and storage costs.

For all Moonbeam-based networks, the generic XCM instructions are benchmarked, while the fungible XCM instructions still use a fixed amount of weight per instruction. Consequently, the total weight cost of the benchmarked XCM instructions considers the number of database reads and writes in addition to the weight required for a given instruction. The breakdown of weight cost for database operations is as follows:

Database Read Write
RocksDB (default) 25,000,000 100,000,000

Now that you know the weight costs for database reads and writes for Moonbase Alpha, you can calculate the weight cost for both fungible and generic XCM instructions using the base weight for instruction and the extra database reads and writes if applicable.

For example, the WithdrawAsset instruction is part of the fungible XCM instructions. Therefore, it is not benchmarked, and the total weight cost of the WithdrawAsset instruction is 200,000,000, except for when transferring local XC-20s. The total weight cost for the WithdrawAsset instruction for local XC-20s is based on a conversion of Ethereum gas to Substrate weight.

The BuyExecution instruction has a base weight of 181,080,000, and performs four database reads (assetManager pallet to get the unitsPerSecond). Therefore, the total weight cost of the BuyExecution instruction is calculated as follows:

181080000 + 4 * 25000000 = 281080000

You can find all the weight values for all the XCM instructions in the following table, which apply to all Moonbeam-based networks:

Benchmarked Instructions Non-Benchmarked Instructions
Generic XCM Instructions Fungible XCM Instructions

The following sections will break down how to calculate XCM fees for Moonbeam-based networks. There are two main scenarios:

  • Fees paid in the reserve token (native tokens like GLMR, MOVR, or DEV)
  • Fees paid in external assets (XC-20s)

Fee Calculation for Reserve Assets

For each XCM instruction, the weight units are converted to balance units as part of the fee calculation. The amount of Wei per weight unit for each of the Moonbeam-based networks is as follows:

Moonbeam Moonriver Moonbase Alpha
5,000,000 50,000 50,000

This means that on Moonbeam, for example, the formula to calculate the cost of one XCM instruction in the reserve asset is as follows:

XCM-Wei-Cost = XCMInstrWeight * WeiPerWeight
XCM-GLMR-Cost = XCM-Wei-Cost / 10^18

Therefore, the actual calculation for fungible instructions, for example, is:

XCM-Wei-Cost = 200000000 * 5000000
XCM-GLMR-Cost = 1000000000000000 / 10^18

The total cost is 0.001 GLMR for an XCM instruction on Moonbeam.

Fee Calculation for External Assets

Considering the scenario of Alice sending DOT to Alith's account on Moonbeam, the fees are taken from the amount of xcDOT Alith receives. To determine how much to charge, Moonbeam uses a concept called UnitsPerSecond, which refers to the units of tokens that the network charges per second of XCM execution time (considering decimals). This concept is used by Moonbeam (and maybe other parachains) to determine how much to charge for XCM execution using a different asset than its reserve.

Moreover, XCM execution on Moonbeam can be paid for by multiple assets (XC-20s) that originate in the chain where the asset is coming from. For example, at the time of writing, an XCM message sent from Kusama Asset Hub (formerly Statemine) can be paid in xcKSM, xcRMRK, or xcUSDT. As long as that asset has the UnitsPerSecond set in Moonbeam/Moonriver, it can be used to pay XCM execution for an XCM message coming from that specific chain.

To find out the UnitsPerSecond for a given asset, you can use the following script, which queries the assetManager.assetTypeUnitsPerSecond, and make sure to pass in the multilocation of the asset in question. If you're unsure of the multilocation, you can retrieve it using the assetManager.assetIdType query.

import { ApiPromise, WsProvider } from '@polkadot/api'; // Version 10.9.1

const providerWsURL = 'wss://wss.api.moonbeam.network';

const getUnitsPerSecond = async () => {
  const substrateProvider = new WsProvider(providerWsURL);
  const api = await ApiPromise.create({ provider: substrateProvider });

  const xcDOTAssetId = 42259045809535163221576417993425387648n;
  const assetType = (
    await api.query.assetManager.assetIdType(xcDOTAssetId)
  ).toJSON();

  const unitsPerSecond = await api.query.assetManager.assetTypeUnitsPerSecond(
    assetType
  );
  console.log(`The UnitsPerSecond for xcDOT is ${unitsPerSecond.toHuman()}`);
};

getUnitsPerSecond();

Once you run the script, you should see The UnitsPerSecond for xcDOT is 33,068,783,068 printed to your terminal.

Remember that one unit of weight is defined as one picosecond of execution time. Therefore, the formula to determine execution time is as follows:

ExecutionTime = Weight / Picosecond

To determine the total weight for Alice's transfer of DOT to Moonbeam, you'll need the weight for each of the four XCM instructions required for the transfer:

Instruction Weight
ReserveAssetDeposited 200,000,000
ClearOrigin 5,194,000
BuyExecution 281,080,000
DepositAsset 200,000,000
TOTAL 686,274,000

Note

For the BuyExecution instruction, the units of weight for the four database reads are accounted for in the above table.

With the total weight, you can calculate the execution time for Alice's transfer of DOT to Moonbeam using the following calculation:

ExecutionTime = 686274000 / 10^12

Which means that the four XCM instructions for the transfer cost 0.000686274 seconds of block execution time.

To calculate the total cost in xcDOT, you'll also need the number of decimals the asset in question uses, which for xcDOT is 10 decimals. You can determine the number of decimals for any asset by querying the asset metadata.

The block execution formula can then be used to determine how much Alice's transfer of DOT to Alith's account on Moonbeam costs. The formula for finding the total cost is as follows:

XCM-Cost = ( UnitsPerSecond / DecimalConversion ) * ExecutionTime

Then the calculation for the transfer is:

XCM-Cost = ( 33068783068 / 10^10 ) * 0.000686274

The total cost to transfer Alice's DOT to Alith's account for xcDOT is 0.0022694246 xcDOT.

Last update: April 22, 2024
| Created: July 25, 2022