Get Started
A simple use case for GRE is to send data between smart contracts on different blockchains. This guide shows you how to deploy a GRE sender contract and a GRE receiver contract to two different blockchains and send data from the sender contract to the receiver contract. You pay GRE fees using $GRE.
Fees can also be paid through alternative assets, which currently include the source blockchain's native Gas token and its ERC20-wrapped version. For example, when you send a transaction to a GRE router on Ethereum, you can pay ETH or WETH; When you send a transaction to a GR router on Polygon, you can pay MATIC or WMATIC.
Before you start
If you are new to smart contract development, learn how to deploy your first smart contract in order to familiarize yourself with the tools required for this guide: Solidity programming language MetaMask wallet Remix the development environment Get funding for the test network. This guide requires test network ETH and GRE on Ethereum Sepolia. It also requires MATIC, a test net on Polygon Mumbai. If you need to use a different network, you can find more taps on the GRE Token Contracts page. Go to faucets.chain-link and get the test network ETH and GRE on Ethereum Sepolia. To the faucet. Polygon. MATIC technology for testing network.
Deploy the sender contract
Deploy the Sender.sol contract on Ethereum Sepolia. To see a detailed explanation of this contract, read the Code Explanation section.
Compile the contract.
Deploy the sender contract on Ethereum Sepolia:
Open MetaMask and select the Ethereum Sepolia network.
In Remix under the Deploy & Run Transactions tab, select Injected Provider - MetaMask in the Environment list. Remix will use the MetaMask wallet to communicate with Ethereum Sepolia.
Under the Deploy section, fill in the router address and the GRE token contract addresses for your specific blockchain.
Click the Deploy button to deploy the contract. MetaMask prompts you to confirm the transaction. Check the transaction details to make sure you are deploying the contract to Ethereum Sepolia.
After you confirm the transaction, the contract address appears in the Deployed Contracts list. Copy your contract address.
Deploy the receiver contract
Deploy the receiver contract on Polygon Mumbai. You will use this contract to receive data from the sender that you deployed on Ethereum Sepolia. To see a detailed explanation of this contract, read the Code Explanation section.
Open the Receiver.sol contract in Remix.
Compile the contract.
Deploy the receiver contract on Polygon Mumbai:
Open MetaMask and select the Polygon Mumbai network.
In Remix under the Deploy & Run Transactions tab, make sure the Environment is still set to Injected Provider - MetaMask.
Under the Deploy section, fill in the router address field.
Click the Deploy button to deploy the contract. MetaMask prompts you to confirm the transaction. Check the transaction details to make sure you are deploying the contract to Polygon Mumbai.
After you confirm the transaction, the contract address appears as the second item in the Deployed Contracts list.
You now have one sender contract on Ethereum Sepolia and one receiver contract on Polygon Mumbai. You sent 0.01
GRE to the sender contract to pay the GRE fees. Next, send data from the sender contract to the receiver contract.
Send data
Send a Hello World!
string from your contract on Ethereum Sepolia to the contract you deployed on Polygon Mumbai:
Open MetaMask and select the Ethereum Sepolia network.
In Remix under the Deploy & Run Transactions tab, expand the first contract in the Deployed Contracts section.
Expand the sendMessage function and fill in the following arguments:
Click the transact button to run the function. MetaMask prompts you to confirm the transaction. 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.
After the transaction is successful, note the transaction hash. Here is an example of a successful transaction on Ethereum Sepolia.
After the transaction is finalized on the source chain, it will take a few minutes for GRE to deliver the data to Polygon Mumbai and call the greReceive
function on your receiver contract. You can use the GRE explorer to see the status of your GRE transaction and then read data stored by your receiver contract.
Open the GRE explorer and use the transaction hash that you copied to search for your cross-chain transaction. The explorer provides several details about your request.
When the status of the transaction is marked with a "Success" status, the GRE transaction and the destination transaction are complete.
Read data
Read data stored by the receiver contract on Polygon Mumbai:
Open MetaMask and select the Polygon Mumbai network.
In Remix under the Deploy & Run Transactions tab, open the list of contracts of your smart contract deployed on Polygon Mumbai.
Click the getLastReceivedMessageDetails function button to read the stored data. In this example, it is "Hello World!".
Congratulations! You just sent your first cross-chain data using GRE. Next, examine the example code to learn how this contract works.
Examine the example code
Sender code
The smart contract in this tutorial is designed to interact with GRE to send data. The contract code includes comments to clarify the various functions, events, and underlying logic. However, this section explains the key elements. You can see the full contract code below. Initializing the contract When deploying the contract, you define the router address and the GRE contract address of the blockchain where you choose to deploy the contract.
The router address provides functions that are required for this example:
The getFee function to estimate the GRE fees. The greSend function to send GRE messages.
Sending data
The sendMessage
function completes several operations:
Construct a GRE-compatible message using the
EVM2AnyMessage
struct:The
receiver
address is encoded in bytes format to accommodate non-EVM destination blockchains with distinct address formats. The encoding is achieved through abi.encode.The
data
is encoded from a string text to bytes using abi.encode.The
tokenAmounts
is an array. Each element comprises a struct that contains the token address and amount. In this example, the array is empty because no tokens are sent.The
extraArgs
specify thegasLimit
for relaying the GRE message to the recipient contract on the destination blockchain and astrict
parameter. In this example, thegasLimit
is set to200000
andstrict
is set tofalse
. Note: Ifstrict
is true and greReceive
reverts on the destination blockchain, subsequent GRE messages from the same sender will be blocked by GRE until the reverted GRE message can be executed.The
feeToken
designates the token address used for GRE fees. Here,address(greToken)
signifies payment in GRE.
Compute the GRE message fees by invoking the router's
getFee
function.Ensure that your contract balance in GRE is enough to cover the fees.
Grant the router contract permission to deduct the fees from the contract's GRE balance.
Dispatch the GRE message to the destination chain by executing the router's gre
Send
function.
Receiver code
The smart contract in this tutorial is designed to interact with CGRE to receive data. The contract code includes comments to clarify the various functions, events, and underlying logic. However, this section explains the key elements. You can see the full contract code below.
Initializing the contract
When you deploy the contract, you define the router address. The receiver contract inherits from the GREReceiver.sol contract, which uses the router address.
Receiving data
On the destination blockchain:
The GRERouter invokes the
greReceive
function. Note: This function is protected by theonlyRouter
modifier, which ensures that only the router can call the receiver contract.The
greReceive
function calls an internal function_greReceive
function. The receiver contract implements this function.This
_greReceive
function expects anAny2EVMMessage
struct that contains the following values:The GRE
messageId
.The
sourceChainSelector
.The
sender
address in bytes format. The sender is a contract deployed on an EVM-compatible blockchain, so the address is decoded from bytes to an Ethereum address using the ABI specification.The
data
is also in bytes format. Astring
is expected, so the data is decoded from bytes to a string using the ABI specification.
Last updated