Sending cross-chain message
Introduce
To send a cross-chain message, you need to call the sendMessage function on the source chain contract. This function will transmit a payload to a specified endpoint on the destination chain. Once the message is received, the xOracleCall function on the destination chain contract will be executed as a callback to handle the payload.
Contract A (Source Chain): Sending a message to contract B (called endpoint) on the destination chain.
/**
* @dev Send message to destination chain
* @param _payload payload to calldata to endpoint
* @param _endpoint endpoint address on destination chain
* @param _dstChainId destination chain id
*/
function sendMessage(
bytes memory _payload,
address _endpoint,
uint64 _dstChainId
)Contract B (Destination Chain): Handling the fulfill message callback.
/**
* @dev Callback from xOracleMessage (sent from another chain)
* @param _payload payload
*/
function xOracleCall(bytes memory _payload)The send message fee is paid with a native token (e.g., ETH). You can obtain the fee amount using the getFee function.
These implementations facilitate communication between smart contracts across different blockchain networks, enabling cross-chain functionality through payload transmission and receipt handling.
Let's start
Example: Crosschain ERC20 Token
This example demonstrates how to implement a crosschain ERC20 token that can be transferred between blockchain networks with xOracle Message.
Import Necessary Contracts and Interfaces.
Implement
contractorand initialize. In the constructor, initialize the ERC20 token with name, symbol, xOracleMessage address, and initial supply.
Implement
sendCrosschainfunction to send tokens across chains.
Implement
getFeefunction to get the required fee from the xOracle message contract.
Implement
xOracleCallfunction to handle incoming messages from other chains.
Implement
setEndpointTokento register the endpoint address and destination chain id.
Full Code
Here is the full example code, including the necessary imports and functions.
For more details, you can refer to the full code on GitHub.
Deployment notes
Deploy
CrosschainERC20Tokencontract on both chains.
Example deployment:
Holesky Chain (chainId 17000)
Sepolia Chain (chainId 11155111)
Configure
setEndpointTokento contracts on both chains to register the other's endpoint address and chain id.
Holesky Chain Configuration
Sepolia Chain Configuration
Get xOracle message fee and send tokens crosschain transfer from Holesky to Sepolia.
Sending Tokens from Holesky to Sepolia
Playground
The contract CrosschainERC20Token.sol is deployed on testnet chains. You can interact with the CrosschainERC20Token contract on the block explorer, by following these steps:
Go to
CrosschainERC20Tokencontract on block explorer.On the
Write Contracttab, call thefaucetfunction to receive faucet 1,000 tokens.On the
Read Contracttab, call thegetFeefunction to get required xOracle message fee with the following parameter:_dstChainId: The destination chain ID (e.g., Sepolia chain ID 11155111). This will return the required xOracle message fee in wei.On the
Write Contracttab, call thesendCrosschainfunction with the following parameters:payableAmount (ether): The xOracle message fee converted to ether (wei divided by 10^18 e.g., 10000000000000000 wei / 10^18 = 0.01)_dstChainId: The destination chain ID (e.g., Sepolia chain ID 11155111)._receiver: The wallet address to receive the tokens on the destination chain._amount: The amount of tokens to transfer in wei. (e.g., 500 tokens * 10^18 = 500000000000000000000)
Playground deployed address
Last updated