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
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.
description: Learn how to use the XCM precompile to send cross-chain messages, execute XCM instructions, and estimate costs from your smart contracts
12471
+
description: Learn how to use the XCM precompile to send cross-chain messages, execute XCM instructions, and estimate costs from your smart contracts.
12472
+
categories: Smart Contracts
12472
12473
---
12473
12474
12474
12475
# XCM Precompile
12475
12476
12476
12477
## Introduction
12477
12478
12478
-
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.
12479
+
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.
12479
12480
12480
-
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}.
12481
+
Located at the fixed address `0x00000000000000000000000000000000000a0000`, the XCM precompile offers three primary functions:
12482
+
12483
+
- **`execute`**: for local XCM execution
12484
+
-**`send`**: for cross-chain message transmission
12485
+
- **`weighMessage`**: for cost estimation
12486
+
12487
+
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}.
12481
12488
12482
12489
## Precompile Interface
12483
12490
12484
-
The XCM precompile implements the `IXcm` interface, which defines the structure for interacting with XCM functionality:
12491
+
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:
12485
12492
12486
12493
```solidity title="IXcm.sol"
12487
12494
// SPDX-License-Identifier: MIT
@@ -12527,23 +12534,23 @@ interface IXcm {
12527
12534
}
12528
12535
```
12529
12536
12530
-
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.
12537
+
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.
12531
12538
12532
12539
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`.
12533
12540
12534
12541
## Interact with the XCM Precompile
12535
12542
12536
12543
To interact with the XCM precompile, you can use the precompile interface directly in Remix IDE:
12537
12544
12538
-
1. Create a new file called `IXcm.sol` in Remix
12539
-
2. Copy and paste the `IXcm` interface code into the file
12540
-
3. Compile the interface
12545
+
1. Create a new file called `IXcm.sol` in Remix.
12546
+
2. Copy and paste the `IXcm` interface code into the file.
12547
+
3. Compile the interface by selecting the button or using **Ctrl +S** keys:
@@ -12553,7 +12560,7 @@ Once connected, you can directly interact with the XCM precompile's functions (`
12553
12560
12554
12561
### Weight a Message
12555
12562
12556
-
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.
12563
+
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.
12557
12564
12558
12565
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:
12559
12566
@@ -12580,46 +12587,46 @@ The function returns a `Weight` struct containing `refTime` and `proofSize` valu
12580
12587
12581
12588
### Execute a Message
12582
12589
12583
-
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.
12590
+
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.
12584
12591
12585
-
To execute a message:
12592
+
Follow these steps to execute a message:
12586
12593
12587
-
1. Call `callWeighMessage` with your XCM message to get the required weight
12588
-
2. Use the returned weight values when calling `callXcmExecute`
12594
+
1. Call `callWeighMessage` with your XCM message to get the required weight.
12595
+
2. Use the returned weight values when calling `callXcmExecute`.
12589
12596
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:
12590
12597
12591
12598
- `message`: The encoded XCM message bytes.
12592
12599
- `weight`: The `Weight` struct returned from `callWeighMessage`.
12593
12600
12594
-
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}.
12601
+
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.
12595
12602
12596
12603
5. Click on the **Transact** button to execute the xcm message:
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.
12605
12612
12606
12613
### Send a Message
12607
12614
12608
-
The `send` function transmits an XCM message to a destination chain. This is the core functionality for cross-chain communication.
12615
+
The `send` function is responsible for transmitting an XCM message to a destination chain, enabling essential cross-chain communication.
12609
12616
12610
12617
To send a message:
12611
12618
12612
-
1. Prepare your destination location encoded in XCM format
12613
-
2. Prepare your XCM message (similar to the execute example)
12614
-
3. Call `callXcmSend` with both parameters
12619
+
1. Prepare your destination location encoded in XCM format.
12620
+
2. Prepare your XCM message (similar to the execute example).
12621
+
3. Call `callXcmSend` with both parameters.
12615
12622
12616
12623
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.
12617
12624
12618
-
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.
12625
+
Unlike `execute`, the `send` function doesn't require a weight parameter since the destination chain will handle execution costs according to its fee structure.
12619
12626
12620
12627
## Cross Contract Calls
12621
12628
12622
-
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.
12629
+
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.
12623
12630
12624
12631
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