Tutorial 1: Deploying a Cross-Chain Application Using LayerZero v2

A step-by-step guide on how to build & deploy a LayerZero cross-chain user application.

Deploying a Cross-Chain App with LayerZero v2

Check the detailed steps below for an in-depth guide:

Step 1. Prerequisites

  • Open your preferred toolset for smart contract development. For this tutorial, we're using the Remix IDE.
  • This tutorial uses MetaMask for wallets and mainnet Polygon and Optimism. Please adjust according to your setup.

Step 2. Set up Development Environment

  1. Clone the Blockdaemon oft-remix repository into Remix.
  2. In the Remix, select and load the OFT.sol contract.
  3. Configure Remix with a file remapping.
    1. Go to the Solidity Compiler section in the left sidebar within the Remix IDE.
    2. Enable Use Configuration File.
    3. Enter config.
  4. Compile the code and wait until the process is complete.

Step 3. Deployment Process

🚧

Note:

  • Ensure to select the OFT.sol contract on the compilation step.
  • Ensure that wallets linked to Remix IDE has access to mainnet funds.
  • Make sure the compiler settings are set according to the provided config.json.
  1. Choose Injected Provider - MetaMask in the environment field.
  2. Select MetaMask Polygon account.
  3. Expand the Deploy field to see all available fields.
    1. Enter \_name and \_symbol in each respective field. These can be any arbitrary names.
  4. Enter the Polygon endpoint address in the _lzEndpoint field. For a list of the endpoints, see here.
  5. Paste the public MetaMask address of the Polygon account into the _owner field.
  6. Click Transact in Remix IDE.
  7. Confirm this operation in your wallet.
  8. Wait for a successful deployment on Polygon.

Step 4. Indexing Contract Addresses

Once deployed, it is important to note that addresses uniquely identify contracts on the blockchain. These addresses are indexed in explorers such as Etherscan for mainnet. Indexing helps monitor transactions, contract interactions, and on-chain activity.

Step 5. Set up another smart contract

Now that the Polygon smart contract is deployed and indexed, the next step is to create another contract on another mainnet. For example, you can deploy contracts to the Optimism mainnet using the same process as Polygon, but remember to update the endpoint contract address for this new network.

Step 6. Configure Cross-Chain Communication

We have deployed two smart contracts and will now establish cross-chain communication.

  1. Get \_eid for Polygon:
    1. Get the Polygon's endpoint ID (30109). For a list of the endpoint ID, see here.
  2. Convert Polygon contract address to bytes32:
    1. Compile and deploy convert.sol in Remix IDE.
    2. Access the Deploy & run transactions tab.
    3. Choose Remix VM (Shanghai) as your environment.
    4. Under Deployed Contracts, enter the address of your deployed Polygon contract into the addressToBytes32 method.
    5. Click on call after entering the address on addressToBytes32 to execute the method.
    6. Copy the bytes32 value from the decoded output of the method execution.
  3. Return to OFT.sol File:
    1. Switch your deployment environment to MetaMask under the Deploy and run transactions tab.
    2. Load the Optimism contract by adding the address obtained during deployment in the At Address field under the Deploy and run transactions tab.
  4. Set the peer to Polygon:
    1. To establish cross-chain communication, call the setPeer function. This function requires two inputs:
      • _eid: The destination endpoint ID used to identify the target blockchain network. Replace the _eid for Polygon as outlined in the documentation
      • _peer : The address of the corresponding contract on the network. Replace the _peer with the bytes32 address of the Polygon contract that we converted before.
// @dev must-have configurations for standard OApps
function setPeer(uint32 _eid, bytes32 _peer) public virtual onlyOwner {
    peers[_eid] = _peer; // Array of peer addresses by destination.
    emit PeerSet(_eid, _peer); // Event emitted each time a peer is set.
}
  1. Click Transact
  2. Confirm the operation in your wallet.

👍

Info:

You need to configure Optimism as a peer using the Polygon contract. This follows the exact same process.

Step 7. Implement Cross-Chain Functionality

⬆️ 7.1. The Send Function

Send functionality allows contracts to send various data payloads, such as tokens and messages, to counterparts on different blockchains.

Key Parameters:

  • Destination chain ID.
  • Payload to be sent.
  • Customizable options for transaction execution, such as gas limits and callback functions.

⬇️ 7.2. The Receive Function

The receive function processes messages or tokens received from other blockchains, conforming to the protocol's messaging interface.

💰 7.3. Fee Estimation

Estimate the gas costs of sending and receiving messages between chains within the LayerZero protocol.

To do this, you can implement the quote() function into your OApp contracts. This function helps estimate the gas costs associated with sending messages across chains. The executor, DVN, and protocol fees are deducted from the value sent to the endpoint.

Step 8. Testing and Monitor

To check if our cross-chain setup works, we can do the following:

  • Use the Send function in our contract to send tokens from one blockchain to another.
  • Use tools like Etherscan to watch the transaction status on the source blockchain
  • Track the receipt events emitted by our contract, which confirm the successful debit and credit of tokens.

🚀

What's Next?

In this tutorial, we've covered deploying the LayerZero OApp with its default settings.

To take the next step, follow the tutorial where we'll guide you to configure the OApp to work with Blockdaemon-Animoca Oracle as your application's verifier.

👋 Need Help?

Contact us through email or our support page for any issues, bugs, or assistance you may need.