Simple Abstract Messenger Example

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

Change your directory to the project folder.

cd UIP-examples/other/Messenger/EVM
3

Install the dependencies.

npm install
4

Set up your wallet details in hardhat.config.ts using either a private key or a mnemonic phrase (ensure spaces are included between words).

Since the same wallet can be used across multiple networks, there's no need to add separate wallets.

Private Key Example
ethereum_sepolia: {
    url: "https://ethereum-sepolia-rpc.publicnode.com",
    accounts: [
        "<private key>"
    ],
    chainId: 11155111
}
Mnemonic Phrase Example
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 Hardhat run command, a log will confirm the successful deployment.

Using network: mantle_sepolia

ChainID mantle_sepolia: 5003

Signer: 0xB3B029c49EA026BaCc0901a071Cf8Fd0D5Bde9AF
Signer native balance: 98.477296506288888787

Deploying MessengerProtocol
With args: 0xB3B029c49EA026BaCc0901a071Cf8Fd0D5Bde9AF,0x5C4eCE5c0D29f7D6c47abd2341421d9f9f7c4FFB

MessengerProtocol deployed to: 0xf66e086e0032D3ba517469E6217BeeF0aB44b728
MessengerProtocol implementation deployed to: 0x49b6B710b0F9ACC20225aE54D9E10f9B3DD76ed7


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

The MessengerProtocol file will also be generated, containing the addresses mentioned in the log. You can find it in the following directory:

EVM/addresses/ethereum_sepolia/MessengerProtocol.json

The deployment address is required for sending messages using Hardhat in the next section of this guide, so it's recommended to note or save the MessengerProtocol deployment address.

Sending and Receiving Messages

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

Sending Messages

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: <your sender wallet address>
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?