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
+39-32Lines changed: 39 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,59 +23,62 @@ The interface defines a `Weight` struct that represents the computational cost o
23
23
24
24
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
25
26
-
## XCM Caller Contract
26
+
## Interact with the XCM Precompile
27
27
28
-
The `XcmCaller` contract provides a convenient wrapper around the XCM precompile, handling the low-level interactions and providing a more user-friendly interface:
28
+
To interact with the XCM precompile, you can use the precompile interface directly in Remix IDE:
2. Copy and paste the `IXcm` interface code into the file
32
+
3. Compile the interface
33
33
34
-
The contract utilizes an immutable address that points to the XCM precompile and provides three wrapper functions corresponding to the precompile's interface methods. The `receive()` function enables the contract to accept tokens, which may be required for specific XCM operations.
4. In the Deploy & Run Transactions tab, select the `IXcm` interface from the contract dropdown
37
+
5. Enter the precompile address `0x00000000000000000000000000000000000a0000` in the **At Address** input field
38
+
6. Click "At Address" to connect to the precompile
37
39
38
-
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.
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.
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:
50
51
51
-
4. Call `callWeighMessage` with a SCALE encoded XCM message. For example, for testing, you can use the following encoded XCM message:
This encoded message represents a sequence of XCM instructions:
58
+
This encoded message represents a sequence of XCM instructions:
58
59
59
-
- **[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.
60
-
- **[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.
61
-
- **[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.
60
+
-**[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.
61
+
-**[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.
62
+
-**[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.
62
63
63
-
This encoded message is provided as an example. You can craft your own XCM message tailored to your specific use case as needed
64
+
This encoded message is provided as an example. You can craft your own XCM message tailored to your specific use case as needed
64
65
65
66
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:
For example, to interact with Polkadot Hub TestNet, you can check this [gist](https://gist.github.com/franciscoaguirre/a6dea0c55e81faba65bedf700033a1a2){target=\_blank}, which provides examples of how to craft XCM messages for different purposes.
68
72
69
73
### Execute a Message
70
74
71
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.
72
76
73
77
To execute a message:
74
78
75
-
1. First, ensure that your contract has enough funds to cover the execution costs of calling the XCM message. Check the [Test Token](/develop/smart-contracts/connect-to-polkadot#test-tokens){target=\_blank} section for more information
76
-
2. Call `callWeighMessage` with your XCM message to get the required weight
77
-
3. Use the returned weight values when calling `callXcmExecute`
78
-
4. 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:
79
+
1. Call `callWeighMessage` with your XCM message to get the required weight
80
+
2. Use the returned weight values when calling `callXcmExecute`
81
+
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:
79
82
80
83
-`message`: The encoded XCM message bytes.
81
84
-`weight`: The `Weight` struct returned from `callWeighMessage`.
@@ -84,11 +87,11 @@ To execute a message:
84
87
85
88
5. Click on the **Transact** button to execute the xcm message:
Also, you can verify that the execution of this specific message succeeded by checking that the beneficiary account of the xcm message has received the funds accordingly.
94
97
@@ -106,8 +109,12 @@ The destination parameter must be encoded according to XCM's location format, sp
106
109
107
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.
108
111
112
+
## Cross Contract Calls
113
+
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 allows you to embed cross-chain capabilities seamlessly into your application logic.
115
+
116
+
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.
117
+
109
118
## Conclusion
110
119
111
120
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.
112
-
113
-
<!-- The examples in this guide demonstrate the basic patterns for interacting with the XCM precompile through Remix IDE. For more comprehensive examples and testing scenarios, check out the [XCM Hardhat example](https://github.com/polkadot-developers/polkavm-hardhat-examples){target=\_blank} which provides additional tools and test cases for working with XCM precompiles in a development environment. -->
To interact with the XCM precompile using Remix IDE:
4
+
5
+
1.**Copy the IXcm Interface**: Grab the IXcm interface definition (provided above) and paste it into a new Solidity file named `IXcm.sol` in Remix.
6
+
2.**Set the Precompile Address**: In Remix, go to the "Deploy & Run Transactions" plugin, and in the "At Address" field, enter the precompile address: `0x00000000000000000000000000000000000a0000`.
7
+
3.**Interact with the Precompile**: Click the "At Address" button. Remix will now allow you to interact directly with the XCM precompile contract using the functions defined in the IXcm interface.
8
+
9
+
You can now call the `execute`, `send`, and `weighMessage` functions on the XCM precompile from Remix, using properly encoded parameters as described in this guide.
0 commit comments