Skip to content

moayaan1911/layerzero-example

Repository files navigation

🚀 LayerZero Example

Welcome to this awesome LayerZero cross-chain token transfer project! 🌌 This repo demonstrates how to use LayerZero to move MyOFT tokens between Holesky and Base Sepolia testnets. Let’s dive in! 😎


📍 Contract Addresses & Deployer

  • Holesky Contract: 0x4461b34d90aDDc7C42Ba2436fEA7f69c9355D62C 🏰
  • Base Sepolia Contract: 0x4883ea19C731Eb9Da7A6E1Cf23AA8B7A6f6313c3 🌉
  • Deployer Wallet: 0x39d2770AbcC456f6C6be820705eD966592E0ad96 👑

📜 Files & Their Purpose

Here’s what each file does in this stellar repo! ✨

  • MyOFT.sol 🛠️
    The smart contract! An ERC20 token with LayerZero’s OFT extension for cross-chain magic.

  • deploy.js 🚀
    Deploys MyOFT to both Holesky and Base Sepolia. Sets up the initial 1000 MOT supply for the deployer.

  • test-deploy.js 🧪
    A test deployment script to preview MyOFT locally before hitting the testnets.

  • setPeers.js 🤝
    Links the Holesky and Base Sepolia contracts as trusted peers so they can talk to each other.

  • sendTokens.js 📤
    The star of the show! Sends 10 MOT from Holesky to Base Sepolia (or vice versa) based on the network you run it on.

  • checkBalance.js 👀
    Checks your MOT balance on either Holesky or Base Sepolia—super handy for verification!

  • checkPeer.js 🔍
    Verifies that the Base Sepolia peer is set correctly on Holesky.

  • checkPeerBase.js 🔎
    Verifies that the Holesky peer is set correctly on Base Sepolia.

  • transferTokens.js 🎁
    Transfers MOT to other wallets on the same chain—great for sharing the love!


🛠️ Commands

Here are all the commands you can run with both npm and yarn, plus what they do! 🎯

NPM Commands

  • npm run deploy-holesky
    Deploys MyOFT to Holesky. 🚀
  • npm run deploy-baseSepolia
    Deploys MyOFT to Base Sepolia. 🌉
  • npm run set-peers-holesky
    Sets peers from Holesky’s perspective. 🤝
  • npm run set-peers-baseSepolia
    Sets peers from Base Sepolia’s perspective. 🤝
  • npm run send-holesky
    Sends 10 MOT from Holesky to Base Sepolia. 📤
  • npm run send-baseSepolia
    Sends 10 MOT from Base Sepolia to Holesky. 📤
  • npm run check-holesky
    Checks MOT balance on Holesky. 👀
  • npm run check-baseSepolia
    Checks MOT balance on Base Sepolia. 👀
  • npm run check-peer-holesky
    Checks Base Sepolia peer on Holesky. 🔍
  • npm run check-peer-baseSepolia
    Checks Holesky peer on Base Sepolia. 🔎
  • npm run transfer-holesky
    Transfers MOT to another wallet on Holesky. 🎁
  • npm run transfer-baseSepolia
    Transfers MOT to another wallet on Base Sepolia. 🎁

Yarn Commands

  • yarn hardhat run deploy.js --network holesky
    Deploys MyOFT to Holesky. 🚀
  • yarn hardhat run deploy.js --network baseSepolia
    Deploys MyOFT to Base Sepolia. 🌉
  • yarn hardhat run setPeers.js --network holesky
    Sets peers from Holesky’s perspective. 🤝
  • yarn hardhat run setPeers.js --network baseSepolia
    Sets peers from Base Sepolia’s perspective. 🤝
  • yarn hardhat run sendTokens.js --network holesky
    Sends 10 MOT from Holesky to Base Sepolia. 📤
  • yarn hardhat run sendTokens.js --network baseSepolia
    Sends 10 MOT from Base Sepolia to Holesky. 📤
  • yarn hardhat run checkBalance.js --network holesky
    Checks MOT balance on Holesky. 👀
  • yarn hardhat run checkBalance.js --network baseSepolia
    Checks MOT balance on Base Sepolia. 👀
  • yarn hardhat run checkPeer.js --network holesky
    Checks Base Sepolia peer on Holesky. 🔍
  • yarn hardhat run checkPeerBase.js --network baseSepolia
    Checks Holesky peer on Base Sepolia. 🔎
  • yarn hardhat run transferTokens.js --network holesky
    Transfers MOT to another wallet on Holesky. 🎁
  • yarn hardhat run transferTokens.js --network baseSepolia
    Transfers MOT to another wallet on Base Sepolia. 🎁

🌠 Flow Explanation

Here’s how this LayerZero magic works, step-by-step! 🌟 Check out the LayerZero Docs for more details!

  • Deployment 🚀

    • deploy.js deploys MyOFT to Holesky and Base Sepolia with 1000 MOT each for the deployer.
  • Peer Setup 🤝

    • setPeers.js links the contracts as trusted peers: Holesky (EID 40161) trusts Base Sepolia (0x4883ea19...), and Base Sepolia (EID 40245) trusts Holesky (0x4461b34d...).
  • Transfer Initiation 📤

    • sendTokens.js burns 10 MOT on the source chain (e.g., Holesky) and sends a message via LayerZero’s endpoint (0x6edce654...) to the destination chain (e.g., Base Sepolia).
  • LayerZero Magic

    • The endpoint relays the message cross-chain. On the destination chain, MyOFT mints 10 MOT to the recipient (your wallet in this case).
  • Verification 👀

    • checkBalance.js confirms the burn (source -10 MOT) and mint (destination +10 MOT) after a short delay (1-5 minutes on testnet).
  • Project Logic 🧠

    • Built on LayerZero’s Omnichain Fungible Token (OFT) standard.
    • MyOFT.sol extends ERC20 with cross-chain capabilities.
    • Anyone with MOT and ETH can send tokens—no owner restriction!
    • Peers must be set once by the deployer, then transfers are open to all.

🎉 About This Project

  • Purpose: A simple demo of LayerZero’s cross-chain power! 🌍
  • Tech: Hardhat, ethers.js, Solidity, and LayerZero’s OFT framework.
  • Chains: Holesky (EID 40161) and Base Sepolia (EID 40245).
  • Token: MyOFT—a custom ERC20 that zips across chains! ⚡
  • Next Steps: Build a DApp frontend, distribute MOT, and let the world play! 🚀

🖥️ How to Run Locally

Ready to blast off with this LayerZero example on your own machine? 🚀 Here’s a step-by-step guide to get you up and running locally! Follow these steps, and you’ll be transferring MOT tokens in no time! 🌌

  • Step 1: Clone the Repo 📥
    Grab the code from GitHub and dive in!

    git clone https://github.com/moayaan1911/layerzero-example.git
    cd layerzero-example
  • Step 2: Install Dependencies 🛠️
    Set up all the goodies with Yarn (or NPM if you prefer)!

    yarn install
    # OR
    npm install
  • Step 3: Configure Environment 🔑
    Create a .env file with your private key and RPCs (get Holesky/Base Sepolia ETH from faucets)!

    echo "PRIVATE_KEY=your_private_key_here" >> .env
    echo "HOLESKY_RPC_URL=https://rpc.holesky.ethpandaops.io" >> .env
    echo "BASE_SEPOLIA_RPC_URL=https://sepolia.base.org" >> .env

    Faucets: Holesky 💧 | Base Sepolia 💧

  • Step 4: Compile Contracts ⚙️
    Turn that Solidity into bytecode magic!

    yarn hardhat compile
    # OR
    npm run compile
  • Step 5: Deploy Contracts 🚀
    Deploy MyOFT to both testnets (make sure you have test ETH)!

    yarn hardhat run deploy.js --network holesky
    yarn hardhat run deploy.js --network baseSepolia
    # OR
    npm run deploy-holesky
    npm run deploy-baseSepolia
  • Step 6: Set Peers 🤝
    Link the contracts so they trust each other across chains!

    yarn hardhat run setPeers.js --network holesky
    yarn hardhat run setPeers.js --network baseSepolia
    # OR
    npm run set-peers-holesky
    npm run set-peers-baseSepolia
  • Step 7: Send Tokens 📤
    Transfer 10 MOT between chains—watch the cross-chain magic happen!

    yarn hardhat run sendTokens.js --network holesky       # Holesky -> Base Sepolia
    yarn hardhat run sendTokens.js --network baseSepolia  # Base Sepolia -> Holesky
    # OR
    npm run send-holesky
    npm run send-baseSepolia
  • Step 8: Check Balances 👀
    Verify your MOT balances after transfers (wait 1-5 mins for LayerZero to process)!

    yarn hardhat run checkBalance.js --network holesky
    yarn hardhat run checkBalance.js --network baseSepolia
    # OR
    npm run check-holesky
    npm run check-baseSepolia
  • Bonus: Troubleshoot 🕵️‍♂️
    If something’s off, check peers or add --show-stack-traces for more info!

    yarn hardhat run checkPeer.js --network holesky
    yarn hardhat run checkPeerBase.js --network baseSepolia

That’s it! You’re now a LayerZero pro running this locally! 🎉 Have fun swapping tokens across chains! 🌍



👨‍💻 About the Developer

Mohammad Ayaan Siddiqui

Assalamualaikum guys! 🙌 This is Mohammad Ayaan Siddiqui (♦moayaan.eth♦). I’m a Full Stack Blockchain Developer with over 2 years of experience rocking the Web3 world! 🚀 I’ve worn many hats:

  • Research Intern at a Hong Kong-based firm 🇭🇰
  • Technical Co-Founder at a Netherlands-based firm 🇳🇱
  • Full Stack Intern at a Singapore-based crypto hardware wallet firm 🇸🇬
  • Currently a Blockchain Developer on a US-based Bitcoin DeFi project 🇺🇸
  • Currently pursuing an online MBA in Blockchain Management from University of Studies Guglielmo Marconi, Italy 🇮🇹

Let’s connect and build something epic! Find me at moayaan.com 🌐

If you liked this project, please donate to Gaza 🇵🇸 UNRWA Donation Link

Happy coding, fam! 😎✨

About

LayerZero cross-chain token transfer project

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published