Skip to main content

🎥 What is LayerZero and How to Use it?

Created on
Updated on
Dec 17, 2024

3 min read

Overview

In this video, we cover how LayerZero works and what the LayerZero architecture looks like. We then demonstrate how a message can be sent across different blockchains using LayerZero.
Subscribe to our YouTube channel for more videos!

Blockchains are isolated in a way that something residing on one chain is limited to the scope of that chain, and with multi-chain becoming the new normal, the need for technologies that enable cross-chain communication is very high. LayerZero is one such technology. LayerZero is a protocol facilitating direct and trustless communication between different blockchain networks, aiming to streamline cross-chain interactions and transactions without intermediaries. Its architecture allows message passing across chains with a user-configurable level of trust.

Sending a Cross-chain Message

We will be using the LayerZero Solidity example repository and the OmniCounter smart contract. OmniCounter contract is a very basic smart contract that imports LayerZero contracts and has a function called incrementCounter, which, when called, increases the counter by one. We will deploy the contract on two chains (Polygon Mumbai testnet and Avalanche Fuji testnet). We will call the incrementCounter function on the Polygon Mumbai testnet, and using the LayerZero contracts, the contract will also be increased on the contract deployed on the Avalanche Fuji testnet.

Step 1: Clone the repo and install the dependencies

git clone https://github.com/LayerZero-Labs/solidity-examples.git
cd solidity-examples
npm install

Step 2: Change the Hardhat config file

Navigate to the hardhat.config.js file and replace the contents with the code in this file here.

Step 3: Add RPC URLs

Replace QuickNode_URL in the hardhat.config.js file with your QuickNode HTTP URLS for specific chains you want to use. For example, in this case, we are using Polygon Mumbai testnet and Avalanche Fuji testnet.

Step 4: Change the incrementCounter.js task

Navigate to the tasks directory, locate the incrementCounter.js file and replace the contents with the code in this file here.

Step 5: Deploy the contracts

Now, let's deploy the contract on both chains. We are deploying on Polygon Mumbai and Avalanche Fuji, but you can choose any chains; make sure to add the RPC URLs for those chains in the hardhat.config.js file as mentioned in Step 3.

npx hardhat --network mumbai deploy --tags OmniCounter
npx hardhat --network fuji deploy --tags OmniCounter

Step 6: Set the remote addresses

We will set the remote addresses for each contract as the address of the other contract. These will be the destination identifiers and will help our contracts to receive messages.

npx hardhat --network mumbai setTrustedRemote --target-network fuji --contract OmniCounter
npx hardhat --network fuji setTrustedRemote --target-network mumbai --contract OmniCounter

Step 7: Send a cross-chain message from Polygon Mumbai testnet to Avalanche Fuji testnet

We will call the incrementCounter function on the contract stored on Polygon Mumbai testnet and target network where the counter will be increased on the contract would be Avalanche Fuji testnet

npx hardhat --network mumbai incrementCounter --target-network fuji

Once the transaction is sent, we can run the following command in a separate Terminal window to check if the counter has increased or not.

npx hardhat --network fuji ocPoll

It may take some time for the message to get delivered on the target chain; you can check the status of the transaction on LayerZero scan for testnet and for mainnet.

We ❤️ Feedback!

Let us know if you have any feedback or requests for new topics. We'd love to hear from you.

Share this guide