Transfer Tokens and Data Across Chains
Last updated
Last updated
In this tutorial, you will use GRE to transfer tokens and arbitrary data between smart contracts on different blockchains. First, you will pay GRE fees on the source blockchain using GRE. You will then pay GRE fees in native Gas tokens using the same contract. For example, you can use ETH on Ethereum, or MATIC on Polygon.
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.
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 GRE test tokens. By following this guide, you should have the 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: Transfer tokens between chains.
In this tutorial, you will use GRE to send string text and GRE-BNM tokens between smart contracts on Ethereum Sepolia and Polygon Mumbai. First, you will pay in GRE, then you will pay for GRE in native Gas.
To use this contract:
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.
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, and true
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, and true
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, and true
as allowed.
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.
Note: Another security measure enforces that only the router can call the _greReceive
function. Read the explanation section for more details.