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.

  1. Import Necessary Contracts and Interfaces.

  1. Implement contractor and initialize. In the constructor, initialize the ERC20 token with name, symbol, xOracleMessage address, and initial supply.

  1. Implement sendCrosschain function to send tokens across chains.

  1. Implement getFee function to get the required fee from the xOracle message contract.

  1. Implement xOracleCall function to handle incoming messages from other chains.

  1. Implement setEndpointToken to 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

  1. Deploy CrosschainERC20Token contract on both chains.

Example deployment:

  • Holesky Chain (chainId 17000)

  • Sepolia Chain (chainId 11155111)

  1. Configure setEndpointToken to contracts on both chains to register the other's endpoint address and chain id.

Holesky Chain Configuration

Sepolia Chain Configuration

  1. 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:

  1. Go to CrosschainERC20Token contract on block explorer.

  2. On the Write Contract tab, call the faucet function to receive faucet 1,000 tokens.

  3. On the Read Contract tab, call the getFee function 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.

  4. On the Write Contract tab, call the sendCrosschain function 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