Remote Execution via XCM¶
The Cross-Consensus Message (XCM) format defines how messages can be sent between interoperable blockchains. This format opens the door to sending an XCM message that executes an arbitrary set of bytes in a Moonbeam-based network, the relay chain, or other parachains in the Polkadot/Kusama ecosystems.
Remote execution via XCM opens a new set of possibilities for cross-chain interactions, from chains executing actions on other chains to users performing remote actions without switching chains.
This page covers the fundamentals of XCM remote execution. If you want to learn how to perform remote execution via XCM, please refer to the Remote Execution via the Substrate API or the Remote Execution via the Ethereum API guides.
Generally speaking, all transactions have an origin, which is where a call comes from. Ethereum transactions have only one origin type, the
msg.sender, which is the account that initiated the transaction.
Substrate-based transactions are more complex, as they can have different origins with different privilege levels. This is similar to having an EVM smart contract call with a specific
require statement in which the call must come from an allowed address. In contrast, these privilege levels are programmed in the Substrate-based runtime itself.
Origins are super important across different components of the Substrate runtime and, hence, the Moonbeam runtime. For example, they define the authority level they inherit in the on-chain governance implementation.
During the execution of an XCM message, the origin defines the context in which the XCM is being executed. By default, the XCM is executed by the source chain's Sovereign account in the destination chain. This Polkadot-specific property of having remote origins that are calculated when executing XCM is known as Computed Origins (formerly known as Multilocation Derivative Accounts).
Depending on the destination chain's configuration, including the
DescendOrigin XCM instruction can mutate the origin from which the XCM message is executed. This property is significant for remote XCM execution, as the action being executed considers the context of the newly mutated origin and not the source chain's Sovereign account.
XCM Instructions for Remote Execution¶
The core XCM instructions required to perform remote execution on Moonbeam (as an example) via XCM are the following:
DescendOrigin- (optional) gets executed in Moonbeam. Mutates the origin to create a new Computed Origin that represents a keyless account controlled via XCM by the sender in the source chain
WithdrawAsset- gets executed in Moonbeam. Takes funds from the Computed Origin
BuyExecution- gets executed in Moonbeam. Uses the funds taken by the previous XCM instruction to pay for the XCM execution, including the remote call
Transact- gets executed in Moonbeam. Executes the arbitrary bytes provided in the XCM instruction
The XCM instructions detailed above can be complemented by other XCM instructions to handle certain scenarios, like failure on execution, more correctly. One example is the inclusion of
General Remote Execution via XCM Flow¶
A user initiates a transaction in the source chain through a pallet that builds the XCM with at least the required XCM instructions for remote execution. The transaction is executed in the source chain, which sends an XCM message with the given instructions to the destination chain.
The XCM message arrives at the destination chain, which executes it. It is executed with the source chain's Sovereign account as a Computed Origin by default. One example that uses this type of origin is when chains open or accept an HRMP channel on the relay chain.
If the XCM message included a
DescendOrigin instruction, the destination chain may mutate the origin to calculate a new Computed Origin (as is the case with Moonbeam-based networks).
WithdrawAsset takes funds from the Computed Origin (either a Sovereign account or mutated), which are then used to pay for the XCM execution through the
BuyExecution XCM instruction. Note that on both instructions, you need to specify which asset you want to use. In addition, you must include the bytes to be executed in the amount of execution to buy.
Transact executes an arbitrary set of bytes that correspond to a pallet and function in the destination chain. You have to specify the type of origin to use (typically
SovereignAccount) and the weight required to execute the bytes (similar to gas in the Ethereum realm).
| Created: October 28, 2023