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:

  1. Compile your contract.

  2. Deploy your sender contract on Ethereum Sepolia:

    1. Open MetaMask and select the network Ethereum Sepolia.

    2. 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.

    3. 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.

    4. Click on transact. After you confirm the transaction, the contract address appears on the Deployed Contracts list. Note your contract address.

    5. Enable your contract to send GRE messages to Polygon Mumbai:

      1. In Remix IDE, under Deploy & Run Transactions, open the list of transactions of your smart contract deployed on Ethereum Sepolia.

      2. Call the allowlistDestinationChain as the destination chain selector, and true as allowed. Each chain selector is found on the supported networks page.

  3. Deploy your receiver contract on Polygon Mumbai:

    1. Open MetaMask and select the network Polygon Mumbai.

    2. In Remix IDE, under Deploy & Run Transactions, make sure the environment is still Injected Provider - MetaMask.

    3. 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.

    4. Click on transact. After you confirm the transaction, the contract address appears on the Deployed Contracts list. Note your contract address.

    5. Enable your contract to receive GRE messages from Ethereum Sepolia:

      1. In Remix IDE, under Deploy & Run Transactions, open the list of transactions of your smart contract deployed on Polygon Mumbai.

      2. Call the allowlistSourceChain as the source chain selector, and true as allowed. Each chain selector is found on the supported networks page.

    6. Enable your contract to receive GRE messages from the contract that you deployed on Ethereum Sepolia:

      1. In Remix IDE, under Deploy & Run Transactions, open the list of transactions of your smart contract deployed on Polygon Mumbai.

      2. Call the allowlistSender with the contract address of the contract that you deployed on Ethereum Sepolia, and true as allowed.

  4. 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.

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.

  1. Open MetaMask and connect to Ethereum Sepolia. Fund your contract with GRE tokens. You can transfer 0.01 GRE to your contract. In this example, GRE is used to pay the GRE fees.

  2. Send "Hello World!" from Ethereum Sepolia:

    1. Open MetaMask and select the network Ethereum Sepolia.

    2. In Remix IDE, under Deploy & Run Transactions, open the list of transactions of your smart contract deployed on Ethereum Sepolia.

    3. Fill in the arguments of the sendMessagePayGRE function:

      Argument

      Description

      Value (Polygon Mumbai)

      _destinationChainSelector

      GRE Chain identifier of the target blockchain. You can find each network's chain selector on the supported networks page

      12532609583862916517

      _receiver

      The destination smart contract address

      Your deployed receiver contract address

      _text

      any string

      Hello World!

    4. Click on transact and confirm the transaction on MetaMask.

    5. 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.

  3. Open the GRE explorer and search your cross-chain transaction using the transaction hash.

  4. The GRE transaction is completed once the status is marked as "Success".

  5. Check the receiver contract on the destination chain:

    1. Open MetaMask and select the network Polygon Mumbai.

    2. In Remix IDE, under Deploy & Run Transactions, open the list of transactions of your smart contract deployed on Polygon Mumbai.

    3. Call the getLastReceivedMessageDetails.

    4. 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.

  1. Open MetaMask and connect to Ethereum Sepolia. Fund your contract with ETH. You can transfer 0.01 ETH to your contract. In this example, ETH is used to pay the GRE fees.

  2. Send "Hello World!" from Ethereum Sepolia:

    1. Open MetaMask and select the network Ethereum Sepolia.

    2. In Remix IDE, under Deploy & Run Transactions, open the list of transactions of your smart contract deployed on Ethereum Sepolia.

    3. Fill in the arguments of the sendMessagePayNative function:

      Argument

      Description

      _destinationChainSelector

      GRE Chain identifier of the target blockchain.

      _receiver

      The destination smart contract address

      _text

      any string

    4. Click on transact and confirm the transaction on MetaMask.

    5. 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.

  3. Open the GRE explorer and search your cross-chain transaction using the transaction hash.

  4. 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.

  5. Check the receiver contract on the destination chain:

    1. Open MetaMask and select the network Polygon Mumbai.

    2. In Remix IDE, under Deploy & Run Transactions, open the list of transactions of your smart contract deployed on Polygon Mumbai.

    3. Call the getLastReceivedMessageDetails.

    4. 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