Skip to content

Commit 83c9681

Browse files
nhussein11dawnkelly09brunopgalvao
authored
[FEAT] - Add XCM precompile page (#794)
* fix: weigh messages and execute done * fix: xcm precompile feedback * fix: wording * fix: clean up * fix: clean up * Apply suggestions from code review Co-authored-by: Dawn Kelly <[email protected]> * Update develop/smart-contracts/precompiles/xcm-precompile.md Co-authored-by: Dawn Kelly <[email protected]> * fix: formatting * Update develop/smart-contracts/precompiles/xcm-precompile.md Co-authored-by: Bruno Galvao <[email protected]> * fix: xcm link added * edits to page, updates llms --------- Co-authored-by: Dawn Kelly <[email protected]> Co-authored-by: Bruno Galvao <[email protected]> Co-authored-by: DAWN KELLY <[email protected]>
1 parent f0adb2d commit 83c9681

File tree

10 files changed

+311
-1
lines changed

10 files changed

+311
-1
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
title: Precompiles
22
nav:
33
- index.md
4-
- 'Interact With Precompiles': interact-with-precompiles.md
4+
- 'Interact With Precompiles': interact-with-precompiles.md
5+
- 'XCM Precompile': xcm-precompile.md
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
---
2+
title: Interact with the XCM Precompile
3+
description: Learn how to use the XCM precompile to send cross-chain messages, execute XCM instructions, and estimate costs from your smart contracts.
4+
categories: Smart Contracts
5+
---
6+
7+
# XCM Precompile
8+
9+
## Introduction
10+
11+
The [XCM (Cross-Consensus Message)](/develop/interoperability/intro-to-xcm){target=\_blank} precompile enables Polkadot Hub developers to access XCM functionality directly from their smart contracts, sending cross-chain messages, executing XCM instructions locally, and estimating execution costs—all through a standardized Solidity interface.
12+
13+
Located at the fixed address `0x00000000000000000000000000000000000a0000`, the XCM precompile offers three primary functions:
14+
15+
- **`execute`**: for local XCM execution
16+
- **`send`**: for cross-chain message transmission
17+
- **`weighMessage`**: for cost estimation
18+
19+
This guide demonstrates how to interact with the XCM precompile through Solidity smart contracts using [Remix IDE](/develop/smart-contracts/dev-environments/remix.md){target=\_blank}.
20+
21+
## Precompile Interface
22+
23+
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:
24+
25+
```solidity title="IXcm.sol"
26+
--8<-- "https://raw.githubusercontent.com/paritytech/polkadot-sdk/cb629d46ebf00aa65624013a61f9c69ebf02b0b4/polkadot/xcm/pallet-xcm/src/precompiles/IXcm.sol"
27+
```
28+
29+
The interface defines a `Weight` struct that represents the computational cost of XCM operations. Weight has two components:
30+
31+
- **`refTime`**: computational time on reference hardware
32+
- **`proofSize`**: the size of the proof required for execution
33+
34+
All XCM messages must be encoded using the [SCALE codec](/polkadot-protocol/parachain-basics/data-encoding/#data-encoding){target=\_blank}, Polkadot's standard serialization format.
35+
36+
For further information, check the [`precompiles/IXCM.sol`](https://github.com/paritytech/polkadot-sdk/blob/cb629d46ebf00aa65624013a61f9c69ebf02b0b4/polkadot/xcm/pallet-xcm/src/precompiles/IXcm.sol){target=\_blank} file present in the `pallet-xcm`.
37+
38+
## Interact with the XCM Precompile
39+
40+
To interact with the XCM precompile, you can use the precompile interface directly in [Remix IDE](/develop/smart-contracts/dev-environments/remix.md){target=\_blank}:
41+
42+
1. Create a new file called `IXcm.sol` in Remix.
43+
2. Copy and paste the `IXcm` interface code into the file.
44+
3. Compile the interface by selecting the button or using **Ctrl +S** keys:
45+
46+
![](/images/develop/smart-contracts/precompiles/xcm-precompile/xcm-precompile-01.webp)
47+
48+
4. In the **Deploy & Run Transactions** tab, select the `IXcm` interface from the contract dropdown.
49+
5. Enter the precompile address `0x00000000000000000000000000000000000a0000` in the **At Address** input field.
50+
6. Select the **At Address** button to connect to the precompile.
51+
52+
![](/images/develop/smart-contracts/precompiles/xcm-precompile/xcm-precompile-02.webp)
53+
54+
7. Once connected, you can use the Remix interface to interact with the XCM precompile's `execute`, `send`, and `weighMessage` functions.
55+
56+
![](/images/develop/smart-contracts/precompiles/xcm-precompile/xcm-precompile-03.webp)
57+
58+
### Weigh a Message
59+
60+
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.
61+
62+
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:
63+
64+
```text title="encoded-xcm-message-example"
65+
0x050c000401000003008c86471301000003008c8647000d010101000000010100368e8759910dab756d344995f1d3c79374ca8f70066d3a709e48029f6bf0ee7e
66+
```
67+
68+
![](/images/develop/smart-contracts/precompiles/xcm-precompile/xcm-precompile-04.webp)
69+
70+
This encoded message represents a sequence of XCM instructions:
71+
72+
- **[Withdraw Asset](https://github.com/polkadot-fellows/xcm-format?tab=readme-ov-file#withdrawasset){target=\_blank}**: 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.
73+
- **[Buy Execution](https://github.com/polkadot-fellows/xcm-format?tab=readme-ov-file#buyexecution){target=\_blank}**: This instruction purchases execution time on the destination chain using the withdrawn assets, ensuring the message can be processed.
74+
- **[Deposit Asset](https://github.com/polkadot-fellows/xcm-format?tab=readme-ov-file#depositasset){target=\_blank}**: This instruction deposits the remaining assets into a specified account on the destination chain after execution costs have been deducted.
75+
76+
This encoded message is provided as an example. You can craft your own XCM message tailored to your specific use case as needed.
77+
78+
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:
79+
80+
![](/images/develop/smart-contracts/precompiles/xcm-precompile/xcm-precompile-05.webp)
81+
82+
!!!note
83+
To interact with Polkadot Hub TestNet, visit this [gist](https://gist.github.com/franciscoaguirre/a6dea0c55e81faba65bedf700033a1a2){target=\_blank}, which provides examples of how to craft XCM messages for different purposes.
84+
85+
### Execute a Message
86+
87+
The `execute` function runs an XCM message locally using the caller's origin. This function helps execute XCM instructions that don't require cross-chain communication.
88+
89+
Follow these steps to execute a message:
90+
91+
1. Call `callWeighMessage` with your XCM message to get the required weight.
92+
2. Use the returned weight values when calling `callXcmExecute`.
93+
3. Pass the same XCM message bytes and the weight obtained from the previous step. For example, using the same message from the weighing example, you would call `callXcmExecute` with:
94+
95+
- `message`: The encoded XCM message bytes.
96+
- `weight`: The `Weight` struct returned from `callWeighMessage`.
97+
98+
You can use the [papi console](https://dev.papi.how/extrinsics#networkId=localhost&endpoint=wss%3A%2F%2Ftestnet-passet-hub.polkadot.io&data=0x1f03050c000401000003008c86471301000003008c8647000d010101000000010100368e8759910dab756d344995f1d3c79374ca8f70066d3a709e48029f6bf0ee7e0750c61e2901daad0600){target=\_blank} to examine the complete extrinsic structure for this operation.
99+
100+
5. Click on the **Transact** button to execute the xcm message:
101+
102+
![](/images/develop/smart-contracts/precompiles/xcm-precompile/xcm-precompile-06.webp)
103+
104+
If successful, you will see the following output in the Remix terminal:
105+
106+
![](/images/develop/smart-contracts/precompiles/xcm-precompile/xcm-precompile-07.webp)
107+
108+
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.
109+
110+
### Send a Message
111+
112+
The `send` function is responsible for transmitting an XCM message to a destination chain, enabling essential cross-chain communication.
113+
114+
To send a message:
115+
116+
1. Prepare your destination location encoded in XCM format.
117+
2. Prepare your XCM message (similar to the execute example).
118+
3. Call `callXcmSend` with both parameters.
119+
120+
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.
121+
122+
Unlike `execute`, the `send` function doesn't require a weight parameter since the destination chain will handle execution costs according to its fee structure.
123+
124+
## Cross Contract Calls
125+
126+
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.
127+
128+
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.
129+
130+
## Conclusion
131+
132+
The XCM precompile provides a powerful interface for cross-chain interactions within the Polkadot ecosystem. By understanding how to properly encode messages, estimate weights, and execute or send XCM instructions, developers can build sophisticated cross-chain applications that leverage the full potential of Polkadot's interoperability features.
135 KB
Loading
157 KB
Loading
154 KB
Loading
158 KB
Loading
170 KB
Loading
171 KB
Loading
177 KB
Loading

0 commit comments

Comments
 (0)