Simple Abstract Messenger

If you have any questions or need assistance, please feel free to use our contact form to connect with an expert on our team.

An effective implementation of blockchain interoperability is sending cross-chain messages. Traditionally, without UIP, this would be a challenging task. However, UIP makes this an easy and safe process. This guide walks you through building a simple dApp that utilises UIP to send cross-chain text messages with EVM smart contracts.

For this guide, we won't need to worry about fees as we will deploy two smart contracts on the testnet — one on the Ethereum Sepolia testnet and the other on the Polygon Amoy network.

Prerequisites

Before you begin, you'll need to enure you're familiar with the concepts and have the tools we'll be using for this guide. If you're unsure about anything, we recommend reading more via the provided links.

We will be using the following native tokens:

Click on the respective token to be taken to a public faucet and complete the steps to acquire the necessary funds in your wallet.

Environment Setup

To begin with, let's setup the working environment. We'll initialize the Hardhat project and configure the environment file with the necessary information.

1

Clone the example project from GitHub.

git clone https://github.com/Entangle-Protocol/UIP-examples.git
2

Install the dependencies.

yarn install
3

Set up your environment variables in the hardhat.config.ts file, including your mnemonic phrase.

ethereum_sepolia: {
    url: "https://ethereum-sepolia-rpc.publicnode.com",
    accounts: {
        mnemonic: "",
    },
    chainId: 11155111
}

Deploying Contracts

Once the environment has been successfully setup we can begin deploying contracts. Before beginning, double check to make sure your environment variables are all correct.

Hardhat makes it easy to deploy contracts, simply run the commands below.

1

Deploy the Ethereum Sepolia contract.

npx hardhat run scripts/deployMessenger.ts --network ethereum_sepolia
2

Deploy the Polygon Amoy contract.

npx hardhat run scripts/deployMessenger.ts --network polygon_amoy

Example Deployment Log

After deploying the contract using the above command, you will see this log as a result, notifying you of the successful contract deployment.

Note down or save the "MessengerProtocol" address as it will be required in order to send further messages.

Using network: polygon_amoy

ChainID polygon_amoy : 80002
With RPC: https://rpc.ankr.com/polygon_amoy

Signer: 0xB3B029c49EA026BaCc0901a071Cf8Fd0D5Bde9AF
Signer native balance: 0.795489155215821829

Deployment address loaded from
        ./addresses/polygon_amoy/EndPoint.json

Deploying MessengerProtocol

        With args:
            0xB3B029c49EA026BaCc0901a071Cf8Fd0D5Bde9AF,0x410dEc5b10a4B1A2eF25De1Fb798339126b7d84b

MessengerProtocol deployed to: 0x14e6578B4a9Ef9D1A2BD24fBbbbf9Ef4A0459EA8
MessengerProtocol implementation deployed to: 0xf69bC40C6C4d25Df0882a45399f5a623Aa7b9783

Data successfully saved to ./addresses/polygon_amoy/MessengerProtocol.json

Sending and Receiving Messages

In this section, we'll take a look at sending and receiving messages.

Sending Messages

The Chain ID specifies that the message is being sent to the Polygon Amoy chain. For a complete list of chain IDs, you can refer to ChainList. Make sure to check the "Include Testnets" box to show testnet chains.

To send a message from the source chain (Ethereum Sepolia) to the destination chain (Polygon Amoy) copy the below command, update the address of the deployed contract on Polygon Amoy and run it.

npx hardhat sendMessage --destchainid 80002 --destaddress <address of deployed contract on polygon amoy> --message "hello" --network ethereum_sepolia

Example Transaction Log
Using network: polygon_amoy
signer: [object Object]
Deployment address loaded from
        ./addresses/polygon_amoy/MessengerProtocol.json

===============================================
ContractTransactionReceipt {
  provider: HardhatEthersProvider {
    _hardhatProvider: LazyInitializationProviderAdapter {
      _providerFactory: [AsyncFunction (anonymous)],
      _emitter: [EventEmitter],
      _initializingPromise: [Promise],
      provider: [BackwardsCompatibilityProviderAdapter]
    },
    _networkName: 'polygon_amoy',
    _blockListeners: [],
    _transactionHashListeners: Map(0) {},
    _eventListeners: [],
    _isHardhatNetworkCached: false,
    _transactionHashPollingTimeout: undefined
  },
  to: <address of deployed contract on polygon amoy>,
  from: <your sender wallet address>,
  contractAddress: null,
  hash: '0x183c0b7d69511c556e171347fe28c1b96b5980623d1959d7845a395617c94401',
  index: 1,
  blockHash: '0xccf0a95c60edc064e3499b80562719b1f3606041fbcd4005783b6e587bc0f0cf',
  blockNumber: 14901297,
  logsBloom: '0x0000400000000000001000000400000000000000000000000000000000000000000000000000000000000010000000000010a000000000000000000000000000000000000000000000000000010000800020000000000000100100000020000000000200000000000000000000000000040000000000000080000000000000000000000000000000000000000000000000000000000000000000000001000000200000000000000000000000000000000000000000040000000000000000004000400000000000000001000000000020000002000000800000108000000000000000001000000000000000000000000000000000400000000000000000100000',
  gasUsed: 56902n,
  blobGasUsed: undefined,
  cumulativeGasUsed: 211811n,
  gasPrice: 50284800008n,
  blobGasPrice: undefined,
  type: 2,
  status: 1,
  root: undefined
}
===============================================
Operation proposed to chain 11155111 to address 0x1091029fCF0fa1d67037c6D84206c796A967cF7F with message: "hello"

Receiving Messages

To read the last message on the destination chain (in this example it's Polygon Amoy) copy the below command, update your sender wallet address and run it.

npx hardhat getLastMessage --sender <your sender wallet address> --network polygon_amoy

Expected Result
Last message from address <your sender wallet address> is: "hello"

Conclusion

In this guide we created a dApp utilising EVM contracts to facilitate in cross-chain text messaging between the Ethereum Sepolia and Polygon Amoy chains.

Last updated

Was this helpful?