рдореБрдЦреНрдп рд╕рд╛рдордЧреНрд░реА рдкрд░ рдЬрд╛рдПрдВ

3 min read

рдЕрд╡рд▓реЛрдХрдитАЛ

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.
рдФрд░ рднреА рд╡реАрдбрд┐рдпреЛ рджреЗрдЦрдиреЗ рдХреЗ рд▓рд┐рдП рд╣рдорд╛рд░реЗ рдпреВрдЯреНрдпреВрдм рдЪреИрдирд▓ рдХреЛ рд╕рдмреНрд╕рдХреНрд░рд╛рдЗрдм рдХрд░реЗрдВ!

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тАЛ

рд╣рдо рдХреЙрд▓ рдХрд░реЗрдВрдЧреЗ 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.

рд╣рдореЗрдВ рдЖрдкрдХреА рдкреНрд░рддрд┐рдХреНрд░рд┐рдпрд╛ рдмрд╣реБрдд рдкрд╕рдВрдж рд╣реИ!

рдЕрдЧрд░ рдЖрдкрдХреЗ рдкрд╛рд╕ рдХреЛрдИ рд╕реБрдЭрд╛рд╡ рдпрд╛ рдирдП рд╡рд┐рд╖рдпреЛрдВ рдХреЗ рд▓рд┐рдП рдХреЛрдИ рдЕрдиреБрд░реЛрдз рд╣реЛ рддреЛ рд╣рдореЗрдВ рдЬрд░реВрд░ рдмрддрд╛рдПрдВ ред рд╣рдореЗрдВ рдЖрдкрд╕реЗ рд╕реБрдирдирд╛ рдЕрдЪреНрдЫрд╛ рд▓рдЧреЗрдЧрд╛ред

рдЗрд╕ рдЧрд╛рдЗрдб рдХреЛ рд╕рд╛рдЭрд╛ рдХрд░реЗрдВ