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 */functionsendMessage(bytesmemory_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 */functionxOracleCall(bytesmemory_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.
Deploy CrosschainERC20Token contract on both chains.
Example deployment:
Holesky Chain (chainId 17000)
Sepolia Chain (chainId 11155111)
Configure setEndpointToken to contracts on both chains to register the other's endpoint address and chain id.
Holesky Chain Configuration
// crosschainERC20Token on holesky chainawaitcrosschainERC20Token.setEndpointToken(11155111,// sepolia chain id"0x9712698c1c91e3e5c39e3c63fdb94e39684183fd"// CrosschainERC20Token deployed address on sepolia);
Sepolia Chain Configuration
// crosschainERC20Token on sepolia chainawaitcrosschainERC20Token.setEndpointToken(17000,// holesky chain id"0x9057a36856116a7100a3cb9c7f676d1477b71c43"// CrosschainERC20Token deployed address on holesky);
Get xOracle message fee and send tokens crosschain transfer from Holesky to Sepolia.
Sending Tokens from Holesky to Sepolia
// send from holesky to sepolia constdestChainId=11155111; // sepoliaconstreceiver=signer.address;constamount='500000000000000000000'; // 500constfee=awaitcrosschainERC20Token.getFee(destChainId);awaitcrosschainERC20Token.sendCrosschain( destChainId, receiver, amount, { value: fee });
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 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)