Skip to content

Commit 9c8e3d0

Browse files
committed
fix: xcm precompile feedback
1 parent 11e4b55 commit 9c8e3d0

File tree

10 files changed

+98
-100
lines changed

10 files changed

+98
-100
lines changed

develop/smart-contracts/precompiles/xcm-precompile.md

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,59 +23,62 @@ The interface defines a `Weight` struct that represents the computational cost o
2323

2424
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`.
2525

26-
## XCM Caller Contract
26+
## Interact with the XCM Precompile
2727

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:
2929

30-
```solidity title="XcmCaller.sol"
31-
--8<-- "code/develop/smart-contracts/precompiles/xcm-precompile/XcmCaller.sol"
32-
```
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
3333

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.
34+
![](/images/develop/smart-contracts/precompiles/xcm-precompile/xcm-precompile-01.webp)
3535

36-
### Weight a Message
36+
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
3739

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.
40+
![](/images/develop/smart-contracts/precompiles/xcm-precompile/xcm-precompile-02.webp)
3941

40-
To test this functionality in Remix:
42+
Once connected, you can directly interact with the XCM precompile's functions (`execute`, `send`, and `weighMessage`) through the Remix interface.
4143

42-
1. Copy and paste the `IXCM` interface and the `XCMCaller` contract into a Solidity file
43-
2. Compile the contract
44+
![](/images/develop/smart-contracts/precompiles/xcm-precompile/xcm-precompile-03.webp)
4445

45-
![](/images/develop/smart-contracts/precompiles/xcm-precompile/xcm-precompile-01.webp)
46+
### Weight a Message
4647

47-
3. Deploy the `XcmCaller` contract
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.
4849

49-
![](/images/develop/smart-contracts/precompiles/xcm-precompile/xcm-precompile-02.webp)
50+
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:
5051

51-
4. Call `callWeighMessage` with a SCALE encoded XCM message. For example, for testing, you can use the following encoded XCM message:
52+
```text title="encoded-xcm-message-example"
53+
0x050c000401000003008c86471301000003008c8647000d010101000000010100368e8759910dab756d344995f1d3c79374ca8f70066d3a709e48029f6bf0ee7e
54+
```
5255

53-
```text title="encoded-xcm-message-example"
54-
0x050c000401000003008c86471301000003008c8647000d010101000000010100368e8759910dab756d344995f1d3c79374ca8f70066d3a709e48029f6bf0ee7e
55-
```
56+
![](/images/develop/smart-contracts/precompiles/xcm-precompile/xcm-precompile-04.webp)
5657

57-
This encoded message represents a sequence of XCM instructions:
58+
This encoded message represents a sequence of XCM instructions:
5859

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.
6263

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
6465

6566
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:
6667

67-
![](/images/develop/smart-contracts/precompiles/xcm-precompile/xcm-precompile-03.webp)
68+
![](/images/develop/smart-contracts/precompiles/xcm-precompile/xcm-precompile-05.webp)
69+
70+
!!!note
71+
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.
6872

6973
### Execute a Message
7074

7175
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.
7276

7377
To execute a message:
7478

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:
7982

8083
- `message`: The encoded XCM message bytes.
8184
- `weight`: The `Weight` struct returned from `callWeighMessage`.
@@ -84,11 +87,11 @@ To execute a message:
8487

8588
5. Click on the **Transact** button to execute the xcm message:
8689

87-
![](/images/develop/smart-contracts/precompiles/xcm-precompile/xcm-precompile-04.webp)
90+
![](/images/develop/smart-contracts/precompiles/xcm-precompile/xcm-precompile-06.webp)
8891

8992
If successful, you will see the following output in the Remix terminal:
9093

91-
![](/images/develop/smart-contracts/precompiles/xcm-precompile/xcm-precompile-05.webp)
94+
![](/images/develop/smart-contracts/precompiles/xcm-precompile/xcm-precompile-07.webp)
9295

9396
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.
9497

@@ -106,8 +109,12 @@ The destination parameter must be encoded according to XCM's location format, sp
106109

107110
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.
108111

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+
109118
## Conclusion
110119

111120
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. -->
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## Interact with the XCM Precompile
2+
3+
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.
-15.2 KB
Loading
-26.8 KB
Loading
-16.4 KB
Loading
-14.8 KB
Loading
4.02 KB
Loading
171 KB
Loading
177 KB
Loading

0 commit comments

Comments
 (0)