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.
function getFee(uint64 dstChainId) external view returns(uint256)
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.
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 CrosschainERC20Token contract on block explorer.
On the Write Contract tab, call the faucet function to receive faucet 1,000 tokens.
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.
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)