You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: develop/smart-contracts/precompiles/xcm-precompile.md
+29-22Lines changed: 29 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,41 +1,48 @@
1
1
---
2
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
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
4
5
---
5
6
6
7
# XCM Precompile
7
8
8
9
## Introduction
9
10
10
-
The XCM (Cross-Consensus Message) precompile enables Polkadot Hub developers to access XCM functionality directly from their smart contracts. This precompile enables contracts to send cross-chain messages, execute XCM instructions locally, and estimate execution costs—all through a standardized Solidity interface.
11
+
The XCM (Cross-Consensus Message) 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.
11
12
12
-
Located at the fixed address `0x00000000000000000000000000000000000a0000`, the XCM precompile offers three primary functions: `execute` for local XCM execution, `send` for cross-chain message transmission, and `weighMessage` for cost estimation. 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}.
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}.
13
20
14
21
## Precompile Interface
15
22
16
-
The XCM precompile implements the `IXcm` interface, which defines the structure for interacting with XCM functionality:
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:
The interface defines a `Weight` struct that represents the computational cost of XCM operations. Weight has two components: `refTime` (computational time on reference hardware) and `proofSize` (size of the proof needed for execution). 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.
29
+
The interface defines a `Weight` struct that represents the computational cost of XCM operations. Weight has two components: `refTime` (computational time on reference hardware) and `proofSize` (the size of the proof required for execution). 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.
23
30
24
31
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`.
25
32
26
33
## Interact with the XCM Precompile
27
34
28
35
To interact with the XCM precompile, you can use the precompile interface directly in Remix IDE:
29
36
30
-
1. Create a new file called `IXcm.sol` in Remix
31
-
2. Copy and paste the `IXcm` interface code into the file
32
-
3. Compile the interface
37
+
1. Create a new file called `IXcm.sol` in Remix.
38
+
2. Copy and paste the `IXcm` interface code into the file.
39
+
3. Compile the interface by selecting the button or using **Ctrl +S** keys:
@@ -45,7 +52,7 @@ Once connected, you can directly interact with the XCM precompile's functions (`
45
52
46
53
### Weight a Message
47
54
48
-
The `weighMessage` function estimates the computational cost required to execute an XCM message. This is crucial for understanding the resources needed before actually executing or sending a message.
55
+
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.
49
56
50
57
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:
51
58
@@ -72,18 +79,18 @@ The function returns a `Weight` struct containing `refTime` and `proofSize` valu
72
79
73
80
### Execute a Message
74
81
75
-
The `execute` function runs an XCM message locally using the caller's origin. This is useful for executing XCM instructions that don't require cross-chain communication.
82
+
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.
76
83
77
-
To execute a message:
84
+
Follow these steps to execute a message:
78
85
79
-
1. Call `callWeighMessage` with your XCM message to get the required weight
86
+
1. Call `callWeighMessage` with your XCM message to get the required weight.
80
87
2. Use the returned weight values when calling `callXcmExecute`
81
88
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:
82
89
83
90
-`message`: The encoded XCM message bytes.
84
91
-`weight`: The `Weight` struct returned from `callWeighMessage`.
85
92
86
-
You can examine the complete extrinsic structure for this operation [here](https://dev.papi.how/extrinsics#networkId=localhost&endpoint=wss%3A%2F%2Ftestnet-passet-hub.polkadot.io&data=0x1f03050c000401000003008c86471301000003008c8647000d010101000000010100368e8759910dab756d344995f1d3c79374ca8f70066d3a709e48029f6bf0ee7e0750c61e2901daad0600){target=\_blank}.
93
+
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.
87
94
88
95
5. Click on the **Transact** button to execute the xcm message:
89
96
@@ -97,21 +104,21 @@ Additionally, you can verify that the execution of this specific message was suc
97
104
98
105
### Send a Message
99
106
100
-
The `send` function transmits an XCM message to a destination chain. This is the core functionality for cross-chain communication.
107
+
The `send` function is responsible for transmitting an XCM message to a destination chain, enabling essential cross-chain communication.
101
108
102
109
To send a message:
103
110
104
-
1. Prepare your destination location encoded in XCM format
105
-
2. Prepare your XCM message (similar to the execute example)
106
-
3. Call `callXcmSend` with both parameters
111
+
1. Prepare your destination location encoded in XCM format.
112
+
2. Prepare your XCM message (similar to the execute example).
113
+
3. Call `callXcmSend` with both parameters.
107
114
108
115
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.
109
116
110
-
Unlike `execute`, the `send` function doesn't require a weight parameter since the destination chain will handle execution costs according to its own fee structure.
117
+
Unlike `execute`, the `send` function doesn't require a weight parameter since the destination chain will handle execution costs according to its fee structure.
111
118
112
119
## Cross Contract Calls
113
120
114
-
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 seamlessly embed cross-chain capabilities into your application logic.
121
+
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.
115
122
116
123
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.
0 commit comments