XCM Precompile¶
Introduction¶
The XCM (Cross-Consensus Message) precompile enables Polkadot Hub developers to access XCM functionality directly from their smart contracts using a Solidity interface.
Located at the fixed address 0x00000000000000000000000000000000000a0000
, the XCM precompile offers three primary functions:
execute
: for local XCM executionsend
: for cross-chain message transmissionweighMessage
: for cost estimation
This guide demonstrates how to interact with the XCM precompile through Solidity smart contracts using Remix IDE.
Note
The XCM precompile provides the barebones XCM functionality. While it provides a lot of flexibility, it doesn't provide abstractions to hide away XCM details. These have to be built on top.
Precompile Interface¶
The XCM precompile implements the IXcm
interface, which defines the structure for interacting with XCM functionality. The source code for the interface is as follows:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
/// @dev The on-chain address of the XCM (Cross-Consensus Messaging) precompile.
address constant XCM_PRECOMPILE_ADDRESS = address(0xA0000);
/// @title XCM Precompile Interface
/// @notice A low-level interface for interacting with `pallet_xcm`.
/// It forwards calls directly to the corresponding dispatchable functions,
/// providing access to XCM execution and message passing.
/// @dev Documentation:
/// @dev - XCM: https://docs.polkadot.com/develop/interoperability
/// @dev - SCALE codec: https://docs.polkadot.com/polkadot-protocol/parachain-basics/data-encoding
/// @dev - Weights: https://docs.polkadot.com/polkadot-protocol/parachain-basics/blocks-transactions-fees/fees/#transactions-weights-and-fees
interface IXcm {
/// @notice Weight v2 used for measurement for an XCM execution
struct Weight {
/// @custom:property The computational time used to execute some logic based on reference hardware.
uint64 refTime;
/// @custom:property The size of the proof needed to execute some logic.
uint64 proofSize;
}
/// @notice Executes an XCM message locally on the current chain with the caller's origin.
/// @dev Internally calls `pallet_xcm::execute`.
/// @param message A SCALE-encoded Versioned XCM message.
/// @param weight The maximum allowed `Weight` for execution.
/// @dev Call @custom:function weighMessage(message) to ensure sufficient weight allocation.
function execute(bytes calldata message, Weight calldata weight) external;
/// @notice Sends an XCM message to another parachain or consensus system.
/// @dev Internally calls `pallet_xcm::send`.
/// @param destination SCALE-encoded destination MultiLocation.
/// @param message SCALE-encoded Versioned XCM message.
function send(bytes calldata destination, bytes calldata message) external;
/// @notice Estimates the `Weight` required to execute a given XCM message.
/// @param message SCALE-encoded Versioned XCM message to analyze.
/// @return weight Struct containing estimated `refTime` and `proofSize`.
function weighMessage(bytes calldata message) external view returns (Weight memory weight);
}
The interface defines a Weight
struct that represents the computational cost of XCM operations. Weight has two components:
refTime
: computational time on reference hardwareproofSize
: the size of the proof required for execution
All XCM messages must be encoded using the SCALE codec, Polkadot's standard serialization format.
For further information, check the precompiles/IXCM.sol
file present in pallet-xcm
.
Interact with the XCM Precompile¶
To interact with the XCM precompile, you can use the precompile interface directly in Remix IDE:
- Create a new file called
IXcm.sol
in Remix. - Copy and paste the
IXcm
interface code into the file. -
Compile the interface by selecting the button or using Ctrl +S keys:
-
In the Deploy & Run Transactions tab, select the
IXcm
interface from the contract dropdown. - Enter the precompile address
0x00000000000000000000000000000000000a0000
in the At Address input field. -
Select the At Address button to connect to the precompile.
-
Once connected, you can use the Remix interface to interact with the XCM precompile's
execute
,send
, andweighMessage
functions.
The main entrypoint of the precompile is the execute
function. However, it's necessary to first call weighMessage
to fill in the required parameters.
Weigh a Message¶
The weighMessage
function estimates the computational cost required to execute an XCM message. This estimate is crucial for understanding the resources needed before actually executing or sending a message.
To test this functionality in Remix, you can call callWeighMessage
with a SCALE-encoded XCM message. For example, for testing, you can use the following encoded XCM message:
0x050c000401000003008c86471301000003008c8647000d010101000000010100368e8759910dab756d344995f1d3c79374ca8f70066d3a709e48029f6bf0ee7e
This encoded message represents a sequence of XCM instructions:
- Withdraw Asset: This instruction removes assets from the local chain's sovereign account or the caller's account, making them available for use in subsequent XCM instructions.
- Buy Execution: This instruction purchases execution time on the destination chain using the withdrawn assets, ensuring the message can be processed.
- Deposit Asset: This instruction deposits the remaining assets into a specified account on the destination chain after execution costs have been deducted.
This encoded message is provided as an example. You can craft your own XCM message tailored to your specific use case as needed.
The function returns a Weight
struct containing refTime
and proofSize
values, which indicate the estimated computational cost of executing this message. If successful, after calling the callWeighMessage
function, you should see the refTime
and proofSize
of the message:
Note
You can find many more examples of XCMs in this gist, which connects to the Polkadot Hub TestNet.
Execute a Message¶
The execute
function runs an XCM message locally using the caller's origin.
This function is the main entrypoint to cross-chain interactions.
Follow these steps to execute a message:
- Call
weighMessage
with your message to get the required weight. -
Pass the same message bytes and the weight obtained from the previous step to
execute
. For example, using the same message from the weighing example, you would callexecute
with:message
: The encoded XCM message bytes.weight
: TheWeight
struct returned fromweighMessage
.
You can use the papi console to examine the complete extrinsic structure for this operation.
-
On Remix, click on the Transact button to execute the XCM message:
If successful, you will see the following output in the Remix terminal:
Additionally, you can verify that the execution of this specific message was successful by checking that the beneficiary account associated with the XCM message has received the funds accordingly.
Send a Message¶
While most cross-chain operations can be performed via execute
, send
is sometimes necessary, for example, when opening HRMP channels.
To send a message:
- Prepare your destination location encoded in XCM format.
- Prepare your XCM message (similar to the execute example).
- Call
send
with both parameters.
The destination parameter must be encoded according to XCM's location format, specifying the target parachain or consensus system. The message parameter contains the XCM instructions to be executed on the destination chain.
Unlike execute
, the send
function doesn't require a weight parameter since the destination chain will handle execution costs according to its fee structure.
Cross Contract Calls¶
Beyond direct interaction and wrapper contracts, you can integrate XCM functionality directly into your existing smart contracts by inheriting from or importing the IXcm
interface. This approach enables you to embed cross-chain capabilities into your application logic seamlessly.
Whether you're building DeFi protocols, governance systems, or any application requiring cross-chain coordination, you can incorporate XCM calls directly within your contract's functions.
Conclusion¶
The XCM precompile provides a simple yet powerful interface for cross-chain interactions within the Polkadot ecosystem and beyond. By building and executing XCM programs, developers can build cross-chain applications that leverage the full potential of Polkadot's interoperability features.
Next steps¶
Head to the Polkadot Hub TestNet and start playing around with the precompile using Hardhat or Foundry.
You can use PAPI to build XCM programs and test them with Chopsticks.
| Created: July 28, 2025