Transfer Tokens and Data Across Chains
This tutorial extends the programmable token transfer example. It uses GRE to transfer tokens and arbitrary data between smart contracts on different blockchains, with a focus on defensive coding in the recipient's contract. If a specified error occurs during GRE message reception, the contract locks the token. Locking tokens allows the owner to recover and redirect them as needed. Defensive coding is crucial because it restores locked tokens and ensures the protection of users' assets.
Before you start
You should know how to write, compile, deploy, and fund smart contracts. If you need to brush up on the basics, read this tutorial, which will guide you through using the Solidity programming language, interacting with the MetaMask wallet, and working in a Remix development environment.
Your account must have some ETH and GRE tokens on Ethereum Sepolia and MATIC tokens on Polygon Mumbai. Learn how to get a test network GRE.
Check the "Supported Networks" page to confirm that your channel supports the token you want to transfer. In this example, you will be transferring tokens from Ethereum Sepolia to Polygon Mumbai, so check the list of supported tokens here.
Learn how to get a GRE test token. By following this guide, you should have a GRE-BnM token, and GRE-BnM should appear in the token list in MetaMask.
Find out how to fund your contract. This guide shows how to fund your contract in GRE, but you can use the same guide to fund your contract with any ERC20 tokens, as long as they appear in MetaMask's token list.
Follow the previous tutorial: Transferring Tokens and Data across chains to learn how to use GRE for programmable token transfer.
Tutorials
In this guide, you will initiate a transaction from a smart contract on Ethereum Sepolia, using GRE to send a string text and a GRE-BNM token to another smart contract on Polygon Mumbai. However, upon reaching the receiver contract, the processing logic will intentionally malfunction. This tutorial will demonstrate an elegant error handling method that allows contract owners to recover locked tokens.
Deploy your contracts
To use this contract:
Open the contract in Remix.
Compile your contract.
Deploy and fund your sender contract on Ethereum Sepolia:
Open MetaMask and select the network Ethereum Sepolia.
In Remix IDE, click on Deploy & Run Transactions and select Injected Provider - MetaMask from the environment list. Remix will then interact with your MetaMask wallet to communicate with Ethereum Sepolia.
Fill in your blockchain's router and GRE contract addresses. The router address can be found on the supported networks page and the GRE contract address on the GRE token contracts page.
Click the transact button. After you confirm the transaction, the contract address appears on the Deployed Contracts list. Note your contract address.
Open MetaMask and fund your contract with GRE-BnM tokens. You can transfer
0.002
GRE-BnM to your contract.Enable your contract to send GRE messages to Polygon Mumbai:
In Remix IDE, under Deploy & Run Transactions, open the list of transactions of your smart contract deployed on Ethereum Sepolia.
Call the
allowlistDestinationChain
with as the destination chain selector, andtrue
as allowed. Each chain selector is found on the supported networks page.
Deploy your receiver contract on Polygon Mumbai:
Open MetaMask and select the network Polygon Mumbai.
In Remix IDE, under Deploy & Run Transactions, make sure the environment is still Injected Provider - MetaMask.
Fill in your blockchain's router and GRE contract addresses. The router address can be found on the supported networks page and the GRE contract address on the GRE token contracts page.
Click the transact button. After you confirm the transaction, the contract address appears on the Deployed Contracts list. Note your contract address.
Enable your contract to receive GREmessages from Ethereum Sepolia:
In Remix IDE, under Deploy & Run Transactions, open the list of transactions of your smart contract deployed on Polygon Mumbai.
Call the
allowlistSourceChain
with as the source chain selector, andtrue
as allowed. Each chain selector is found on the supported networks page.
Enable your contract to receive GRE messages from the contract that you deployed on Ethereum Sepolia:
In Remix IDE, under Deploy & Run Transactions, open the list of transactions of your smart contract deployed on Polygon Mumbai.
Call the
allowlistSender
with the contract address of the contract that you deployed on Ethereum Sepolia, andtrue
as allowed.
Call the
setSimRevert
function, passingtrue
as a parameter, then wait for the transaction to confirm. Settings_simRevert
to true simulates a failure when processing the received message. Read the explanation section for more details.
At this point, you have one sender contract on Ethereum Sepolia and one receiver contract on Polygon Mumbai. As security measures, you enabled the sender contract to send GRE messages to Polygon Mumbai and the receiver contract to receive GRE messages from the sender and Ethereum Sepolia. The receiver contract cannot process the message, and therefore, instead of throwing an exception, it will lock the received tokens, enabling the owner to recover them.
Note: Another security measure enforces that only the router can call the
_greReceive
function. Read the explanation section for more details.
Deploy your contracts
To use this contract:
Open the contract in Remix.
Compile your contract.
Deploy and fund your sender contract on Ethereum Sepolia:
Open MetaMask and select the network Ethereum Sepolia.
In Remix IDE, click on Deploy & Run Transactions and select Injected Provider - MetaMask from the environment list. Remix will then interact with your MetaMask wallet to communicate with Ethereum Sepolia.
Fill in your blockchain's router and GRE contract addresses. The router address can be found on the supported networks page and the GRE contract address on the GRE token contracts page.
Click the transact button. After you confirm the transaction, the contract address appears on the Deployed Contracts list. Note your contract address.
Open MetaMask and fund your contract with GRE-BnM tokens. You can transfer
0.002
GRE-BnM to your contract.Enable your contract to send GRE messages to Polygon Mumbai:
In Remix IDE, under Deploy & Run Transactions, open the list of transactions of your smart contract deployed on Ethereum Sepolia.
Call the
allowlistDestinationChain
as the destination chain selector, andtrue
as allowed. Each chain selector is found on the supported networks page.
Deploy your receiver contract on Polygon Mumbai:
Open MetaMask and select the network Polygon Mumbai.
In Remix IDE, under Deploy & Run Transactions, make sure the environment is still Injected Provider - MetaMask.
Fill in your blockchain's router and GRE contract addresses. The router address can be found on the supported networks page and the GRE contract address on the GRE token contracts page.
Click the transact button. After you confirm the transaction, the contract address appears on the Deployed Contracts list. Note your contract address.
Enable your contract to receive GRE messages from Ethereum Sepolia:
In Remix IDE, under Deploy & Run Transactions, open the list of transactions of your smart contract deployed on Polygon Mumbai.
Call the
allowlistSourceChain
as the source chain selector, andtrue
as allowed. Each chain selector is found on the supported networks page.
Enable your contract to receive GRE messages from the contract that you deployed on Ethereum Sepolia:
In Remix IDE, under Deploy & Run Transactions, open the list of transactions of your smart contract deployed on Polygon Mumbai.
Call the
allowlistSender
with the contract address of the contract that you deployed on Ethereum Sepolia, andtrue
as allowed.
Call the
setSimRevert
function, passingtrue
as a parameter, then wait for the transaction to confirm. Settings_simRevert
to true simulates a failure when processing the received message. Read the explanation section for more details.
At this point, you have one sender contract on Ethereum Sepolia and one receiver contract on Polygon Mumbai. As security measures, you enabled the sender contract to send GRE messages to Polygon Mumbai and the receiver contract to receive GRE messages from the sender and Ethereum Sepolia. The receiver contract cannot process the message, and therefore, instead of throwing an exception, it will lock the received tokens, enabling the owner to recover them.
Note: Another security measure enforces that only the router can call the
_greReceive
function. Read the explanation section for more details.
Recover the locked tokens
You will transfer 0.001 GRE-BnM and a text. The GRE fees for using GRE will be paid in GRE.
Open MetaMask and connect to Ethereum Sepolia. Fund your contract with GRE tokens. You can transfer
0.5
GRE to your contract. In this example, GRE is used to pay the CGRE fees.Send a string data with tokens from Ethereum Sepolia:
Open MetaMask and select the network Ethereum Sepolia.
In Remix IDE, under Deploy & Run Transactions, open the list of transactions of your smart contract deployed on Ethereum Sepolia.
Fill in the arguments of the sendMessagePayGRE function:
ArgumentValue and Description_destinationChainSelector
GRE Chain identifier of the destination blockchain (Polygon Mumbai in this example). You can find each chain selector on the supported networks page.
_receiver
Your receiver contract address at Polygon Mumbai. The destination contract address.
_text
Hello World!
Anystring
_token
The CGRE-BnM contract address at the source chain (Ethereum Sepolia in this example). You can find all the addresses for each supported blockchain on the supported networks page.
_amount
1000000000000000
The token amount (0.001 GRE-BnM).Click on
transact
and confirm the transaction on MetaMask.After the transaction is successful, record the transaction hash. Here is an example of a transaction on Ethereum Sepolia.
Note: During gas price spikes, your transaction might fail, requiring more than 0.5 GRE to proceed. If your transaction fails, fund your contract with more GRE tokens and try again.
Open the GRE explorer and search your cross-chain transaction using the transaction hash.
The GRE transaction is completed once the status is marked as "Success".
Check the receiver contract on the destination chain:
Open MetaMask and select the network Polygon Mumbai.
In Remix IDE, under Deploy & Run Transactions, open the list of transactions of your smart contract deployed on Polygon Mumbai.
Call the
getFailedMessagesIds
function to list the failed messages.Notice the messageId
To recover the locked tokens, call the
retryFailedMessage
function:ArgumentDescriptionmessageId
The unique identifier of the failed message.
tokenReceiver
The address to which the tokens will be sent.
After confirming the transaction, you can open it in a block explorer. Notice that the locked funds were transferred to the
tokenReceiver
address.
Note: These example contracts are designed to work bi-directionally. As an exercise, you can use them to transfer tokens and data from Ethereum Sepolia to Polygon Mumbai and from Polygon Mumbai back to Ethereum Sepolia.
Explanation
The smart contract featured in this tutorial is designed to interact with GRE to transfer and receive tokens and data. The contract code is similar to the Transfer Tokens and Data Across Chains tutorial. Hence, you can refer to its code explanation. We will only explain the processing of received messages.
Receiving and processing messages
Upon receiving a message on the destination blockchain, the greReceive
function is called by the GRE router. This function serves as the entry point to the contract for processing incoming GRE messages, enforcing crucial security checks through the onlyRouter
, and onlyAllowlisted
modifiers.
Here's the step-by-step breakdown of the process:
Entrance through
greReceive
:The
greReceive
function is invoked with anAny2EVMMessage
struct containing the message to be processed.Security checks ensure the call is from the authorized router, an allowlisted source chain, and an allowlisted sender.
Processing Message:
greReceive
calls theprocessMessage
function, which is external to leverage Solidity's try/catch error handling mechanism. Note: TheonlySelf
modifier ensures that only the contract can call this function.Inside
processMessage
, a check is performed for a simulated revert condition using thes_simRevert
state variable. This simulation is toggled by thesetSimRevert
function, callable only by the contract owner.If
s_simRevert
is false,processMessage
calls the_greReceive
function for further message processing.
Message Processing in
_greReceive
:_greReceive
extracts and stores various information from the message, such as themessageId
, decodedsender
address, token amounts, and data.It then emits a
MessageReceived
event, signaling the successful processing of the message.
Error Handling:
If an error occurs during the processing (or a simulated revert is triggered), the catch block within
greReceive
is executed.The
messageId
of the failed message is added tos_failedMessages
, and the message content is stored ins_messageContents
.A
MessageFailed
event is emitted, which allows for later identification and reprocessing of failed messages.
Reprocessing of failed messages
The retryFailedMessage
function provides a mechanism to recover assets if a GRE message processing fails. It's specifically designed to handle scenarios where message data issues prevent entire processing yet allow for token recovery:
Initiation:
Only the contract owner can call this function, providing the
messageId
of the failed message and thetokenReceiver
address for token recovery.
Validation:
It checks if the message has failed using
s_failedMessages.get(messageId)
. If not, it reverts the transaction.
Status Update:
The error code for the message is updated to
RESOLVED
to prevent reentry and multiple retries.
Token Recovery:
Retrieves the failed message content using
s_messageContents[messageId]
.Transfers the locked tokens associated with the failed message to the specified
tokenReceiver
as an escape hatch without processing the entire message again.
Event Emission:
An event
MessageRecovered
is emitted to signal the successful recovery of the tokens.
This function showcases a graceful asset recovery solution, protecting user values even when message processing encounters issues.
Last updated