Send Arbitrary Data
In this tutorial, you will use GRE to send data between smart contracts on different blockchains. First, you will use GRE to pay for GRE on the source blockchain. You will then use the same contract to pay for GRE in native Gas tokens. For example, you can use ETH on Ethereum, or MATIC on Polygon.
Before you begin
You should understand how to write, compile, deploy, and fund a smart contract. 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 within the Remix Development Environment.
Your account must have some ETH tokens on Ethereum Sepolia and MATIC tokens on Polygon Mumbai.
Tutorial
In this tutorial, you will send a string text between smart contracts on Ethereum Sepolia and Polygon Mumbai using GRE. First, you will pay GRE, then you will pay GRE fees in native gas.
Deploy your contracts
To use this contract:
Compile your contract.
Deploy 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 the router address and the GRE address for your network. You can find the router address on the supported networks page and the GRE token address on the GRE Token contracts page.
Click on transact. After you confirm the transaction, the contract address appears on the Deployed Contracts list. Note your contract address.
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.
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 the router address and the GRE address for your network. You can find the router address on the supported networks page and the GRE contract address on the GRE token contracts page.
Click on transact. 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.
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.
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.
Send data and pay in GRE
You will use GRE to send a text. The GRE fees for using GRE will be paid in GRE. Read this explanation for a detailed description of the code example.
Send "Hello World!" 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:
Click on
transact
and confirm the transaction on MetaMask.Once the transaction is successful, note 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.01 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
getLastReceivedMessageDetails
.Notice the received text is the one you sent, "Hello World!"
Note: These example contracts are designed to work bi-directionally. As an exercise, you can use them to send data from Ethereum Sepolia to Polygon Mumbai and from Polygon Mumbai back to Ethereum Sepolia.
Send data and pay in native
You will use GRE to send a text. The GRE fees for using GRE will be paid in native gas. Read this explanation for a detailed description of the code example.
Send "Hello World!" 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 sendMessagePayNative function:
Click on
transact
and confirm the transaction on MetaMask.Once the transaction is successful, note 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.01 ETH to proceed. If your transaction fails, fund your contract with more ETH 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". Note that GRE fees are denominated in GRE. Even if GRE fees are paid using native gas tokens, node operators will be paid in GRE.
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
getLastReceivedMessageDetails
.Notice the received text is the one you sent, "Hello World!"
Note: These example contracts are designed to work bi-directionally. As an exercise, you can use them to send data from Ethereum Sepolia to Polygon Mumbai and from Polygon Mumbai back to Ethereum Sepolia.
Last updated