This guide provides a comprehensive, step-by-step tutorial for integrating LayerZero V2 with the SEI blockchain. By the end of this guide, you will understand how to create, deploy, and manage omnichain tokens that can seamlessly move between SEI and any other LayerZero-supported blockchain.
LayerZero is an omnichain interoperability protocol that enables secure, permissionless communication between different blockchains. Think of it as a universal translator that allows blockchains to talk to each other.
Rename .env.example file to .env and update it with needed configurations:
.env
PRIVATE_KEY=your_private_key
At a minimum, you need to have the PRIVATE_KEY. RPC URLs are optional, but strongly recommended. If you don’t provide them, public RPCs will be used, but public RPCs can be unreliable or slow, leading to long waiting times for transactions to be confirmed or, at worst, cause your transactions to fail.
Update your hardhat.config.ts file to include the networks you want to deploy your contracts to:
hardhat.config.ts
networks: { // the network you are deploying to or are already on // Sei Mainnet (EID=30280) 'sei-mainnet': { eid: EndpointId.SEI_V2_MAINNET, url: process.env.RPC_URL_SEI || 'https://evm-rpc.sei-apis.com', accounts, }, // another network you want to connect to 'optimism-mainnet': { eid: EndpointId.OPTIMISM_V2_MAINNET, url: process.env.RPC_URL_OPTIMISM || 'https://mainnet.optimism.io', accounts, },}
Modify your layerzero.config.ts file to include the chains and channel security settings you want for each connection:
layerzero.config.ts
import { EndpointId } from '@layerzerolabs/lz-definitions';import type { OmniPointHardhat } from '@layerzerolabs/toolbox-hardhat';import { OAppEnforcedOption } from '@layerzerolabs/toolbox-hardhat';import { ExecutorOptionType } from '@layerzerolabs/lz-v2-utilities';import { TwoWayConfig, generateConnectionsConfig } from '@layerzerolabs/metadata-tools';const seiContract: OmniPointHardhat = { eid: EndpointId.SEI_V2_MAINNET, contractName: 'MyOFT'};const optimismContract: OmniPointHardhat = { eid: EndpointId.OPTIMISM_V2_MAINNET, contractName: 'MyOFT'};// To connect all the above chains to each other, we need the following pathways:// Optimism <-> sei// sei <-> Optimism// For this example's simplicity, we will use the same enforced options values for sending to all chains// To learn more, read https://docs.layerzero.network/v2/concepts/applications/oapp-standard#execution-options-and-enforced-settingsconst EVM_ENFORCED_OPTIONS: OAppEnforcedOption[] = [ { msgType: 1, optionType: ExecutorOptionType.LZ_RECEIVE, gas: 80000, value: 0 }];const pathways: TwoWayConfig[] = [ [ // 1) Chain B's contract (e.g. Optimism) optimismContract, // 2) Chain A's contract (e.g. sei) seiContract, // 3) Channel security settings: // • first array = "required" DVN names // • second array = "optional" DVN names array + threshold // • third value = threshold (i.e., number of optionalDVNs that must sign) // [ requiredDVN[], [ optionalDVN[], threshold ] ] [['LayerZero Labs' /* ← add more DVN names here */], []], // 4) Block confirmations: // [confirmations for Optimism → sei, confirmations for sei → Optimism] [1, 1], // 5) Enforced execution options: // [options for Optimism → sei, options for sei → Optimism] [EVM_ENFORCED_OPTIONS, EVM_ENFORCED_OPTIONS] ]];export default async function () { // Generate the connections config based on the pathways const connections = await generateConnectionsConfig(pathways); return { contracts: [{ contract: optimismContract }, { contract: seiContract }], connections };}
It is strongly recommended to review LayerZero’s Channel Security Model and understand the impact of each of these configuration settings.See Next Steps to review the available providers and security settings.
Now update your hardhat.config.ts to import the mint task:
hardhat.config.ts
// Get the environment configuration from .env file//// To make use of automatic environment setup:// - Duplicate .env.example file and name it .env// - Fill in the environment variablesimport 'dotenv/config';import 'hardhat-deploy';import 'hardhat-contract-sizer';import '@nomiclabs/hardhat-ethers';import '@layerzerolabs/toolbox-hardhat';import { HardhatUserConfig, HttpNetworkAccountsUserConfig } from 'hardhat/types';import { EndpointId } from '@layerzerolabs/lz-definitions';import './tasks/sendOFT';import './tasks/mint';// Set your preferred authentication method//// If you prefer using a mnemonic, set a MNEMONIC environment variable// to a valid mnemonicconst MNEMONIC = process.env.MNEMONIC;// If you prefer to be authenticated using a private key, set a PRIVATE_KEY environment variableconst PRIVATE_KEY = process.env.PRIVATE_KEY;const accounts: HttpNetworkAccountsUserConfig | undefined = MNEMONIC ? { mnemonic: MNEMONIC } : PRIVATE_KEY ? [PRIVATE_KEY] : undefined;if (accounts == null) { console.warn('Could not find MNEMONIC or PRIVATE_KEY environment variables. It will not be possible to execute transactions in your example.');}const config: HardhatUserConfig = { paths: { cache: 'cache/hardhat' }, solidity: { compilers: [ { version: '0.8.22', settings: { optimizer: { enabled: true, runs: 200 } } } ] }, networks: { // the network you are deploying to or are already on // Sei Mainnet (EID=30280) 'sei-mainnet': { eid: EndpointId.SEI_V2_MAINNET, url: process.env.RPC_URL_SEI || 'https://evm-rpc.sei-apis.com', accounts }, // another network you want to connect to 'optimism-mainnet': { eid: EndpointId.OPTIMISM_V2_MAINNET, url: process.env.RPC_URL_OPTIMISM || 'https://mainnet.optimism.io', accounts } }, namedAccounts: { deployer: { default: 0 // wallet address of index[0], of the mnemonic in .env } }};export default config;
Important: You must create and import the mint task before deployment, otherwise Hardhat will throw an error when trying to resolve the task dependencies during compilation.
The OFT contract handles all cross-chain logic automatically. When tokens are sent:
Source Chain: Burns tokens from sender’s balance
LayerZero Protocol: Verifies and relays the message
Destination Chain: Mints equivalent tokens to recipient
contracts/MyOFT.sol
// SPDX-License-Identifier: UNLICENSEDpragma solidity ^0.8.22;import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";import { OFT } from "@layerzerolabs/oft-evm/contracts/OFT.sol";contract MyOFT is OFT, Ownable { constructor(string memory name, string memory symbol, address endpoint, address owner) OFT(name, symbol, endpoint, owner) Ownable(owner) {} /** * @notice Mint new tokens (only owner) * @param to Address to receive the minted tokens * @param amount Amount of tokens to mint (with decimals) */ function mint(address to, uint256 amount) external onlyOwner { _mint(to, amount); }}
The OFT contract uses the ERC20 token standard. You may want to add a mint(...) function in the constructor(...) or contract body if this is your first time deploying an OFT. If you have an existing ERC20 token, you will want to use an OFT Adapter contract.You can read the general OFT Quickstart for a better understanding of how OFTs work and what contracts to use.
import { task } from 'hardhat/config';import { getNetworkNameForEid, types } from '@layerzerolabs/devtools-evm-hardhat';import { EndpointId } from '@layerzerolabs/lz-definitions';import { addressToBytes32 } from '@layerzerolabs/lz-v2-utilities';import { Options } from '@layerzerolabs/lz-v2-utilities';import { BigNumberish, BytesLike } from 'ethers';interface Args { amount: string; to: string; toEid: EndpointId;}interface SendParam { dstEid: EndpointId; // Destination endpoint ID, represented as a number. to: BytesLike; // Recipient address, represented as bytes. amountLD: BigNumberish; // Amount to send in local decimals. minAmountLD: BigNumberish; // Minimum amount to send in local decimals. extraOptions: BytesLike; // Additional options supplied by the caller to be used in the LayerZero message. composeMsg: BytesLike; // The composed message for the send() operation. oftCmd: BytesLike; // The OFT command to be executed, unused in default OFT implementations.}// send tokens from a contract on one network to anothertask('lz:oft:send', 'Sends tokens from either OFT or OFTAdapter') .addParam('to', 'contract address on network B', undefined, types.string) .addParam('toEid', 'destination endpoint ID', undefined, types.eid) .addParam('amount', 'amount to transfer in token decimals', undefined, types.string) .setAction(async (taskArgs: Args, { ethers, deployments }) => { const toAddress = taskArgs.to; const eidB = taskArgs.toEid; // Get the contract factories const oftDeployment = await deployments.get('MyOFT'); const [signer] = await ethers.getSigners(); // Create contract instances const oftContract = new ethers.Contract(oftDeployment.address, oftDeployment.abi, signer); const decimals = await oftContract.decimals(); const amount = ethers.utils.parseUnits(taskArgs.amount, decimals); let options = Options.newOptions().addExecutorLzReceiveOption(65000, 0).toBytes(); // Now you can interact with the correct contract const oft = oftContract; const sendParam: SendParam = { dstEid: eidB, to: addressToBytes32(toAddress), amountLD: amount, minAmountLD: amount, extraOptions: options, composeMsg: ethers.utils.arrayify('0x'), // Assuming no composed message oftCmd: ethers.utils.arrayify('0x') // Assuming no OFT command is needed }; // Get the quote for the send operation const feeQuote = await oft.quoteSend(sendParam, false); const nativeFee = feeQuote.nativeFee; console.log(`sending ${taskArgs.amount} token(s) to network ${getNetworkNameForEid(eidB)} (${eidB})`); const ERC20Factory = await ethers.getContractFactory('ERC20'); const innerTokenAddress = await oft.token(); // // If the token address !== address(this), then this is an OFT Adapter // if (innerTokenAddress !== oft.address) { // // If the contract is OFT Adapter, get decimals from the inner token // const innerToken = ERC20Factory.attach(innerTokenAddress); // // Approve the amount to be spent by the oft contract // await innerToken.approve(oftDeployment.address, amount); // } const r = await oft.send(sendParam, { nativeFee: nativeFee, lzTokenFee: 0 }, signer.address, { value: nativeFee }); console.log(`Send tx initiated. See: https://layerzeroscan.com/tx/${r.hash}`); });
Don’t forget to import the task in your hardhat.config.ts (if not already added):
// Add this import to hardhat.config.ts (should already be there from Step 3)import './tasks/sendOFT';
Execute the transfer:
# Send 100 tokens from SEI to Optimism (EID: 30111)npx hardhat lz:oft:send \ --to 0xRecipientAddress \ --toEid 30111 \ --amount 100 \ --network sei-mainnet
You’ve issued an omnichain token and bridged it from Sei Mainnet to Optimism. Customize supply logic, fees, or add more chains by applying changes to the core contract, redeploying, and repeating the wiring step.
Quote Send RevertsIf your quoteSend call reverts, it usually means that your LayerZero wiring hasn’t been fully configured or there’s no default pathway for the chains you’re trying to bridge. Here’s how to diagnose and fix it:1. Wiring Didn’t Succeed:Run the following to inspect your on‑chain wiring configuration:
See that your source configuration has a valid send library, DVN address, and target eid.2. No Default Pathway:LayerZero default settings should be considered placeholders. Sometimes the LayerZero defaults will contain a LzDeadDVN. Those entries indicate that a default pathway setting does not exist.
Check: You can see if your configuration contains a LzDeadDVN by viewing the Default Config Checker on LayerZero Scan.
Fix: Open your layerzero.config.ts and under the relevant pathways entry, add working DVN providers (in the [ requiredDVN[], [ optionalDVN[], threshold ] ] section).
Re-run your wiring command for the connections so that the wiring on both chains is live.