This repository contains two Solidity smart contracts that demonstrate cross-chain communication using the LayerZero protocol. These contracts are designed to operate on different Ethereum test networks and allow for message passing and token bridging between chains.
The contracts leverage the LayerZero protocol to enable cross-chain communication. They demonstrate basic functionalities such as sending and receiving messages and transferring tokens across different chains.
This contract allows sending and receiving string messages between chains. It utilizes LayerZero's NonblockingLzApp
to ensure that cross-chain messages are received even if the transaction reverts.
This contract extends the ERC20
standard and the NonblockingLzApp
to facilitate token bridging between chains. It allows users to burn tokens on the source chain and mint them on the destination chain.
constructor(address _endpoint)
: Initializes the contract with the LayerZero endpoint address and sets the destination chain ID based on the endpoint.
_nonblockingLzReceive
: Overrides the parent function to update thedata
variable with the received message.send(string memory _message)
: Encodes and sends the provided message to the destination chain.trustAddress(address _otherContract)
: Sets the trusted remote contract address for cross-chain communication.
constructor(address _endpoint)
: Initializes the contract with the LayerZero endpoint address, sets the destination chain ID, and mints an initial supply of tokens to the deployer.
_nonblockingLzReceive
: Overrides the parent function to mint tokens to the specified account on the destination chain.bridge(uint _amount)
: Burns the specified amount of tokens on the source chain and sends a message to mint them on the destination chain.trustAddress(address _otherContract)
: Sets the trusted remote contract address for cross-chain communication.
-
Optimism (Chain ID: 10232)
- Endpoint:
0x55370E0fBB5f5b8dAeD978BA1c075a499eB107B8
- Endpoint:
-
Arbitrum (Chain ID: 10231)
- Endpoint:
0x6098e96a28E02f27B1e6BD381f870F1C8Bd169d3
- Endpoint:
- Deploy the Contract: Deploy the contract on the desired network with the appropriate endpoint.
- Send Message: Call the
send
function with the desired message. The message will be encoded and sent to the destination chain. - Receive Message: The
_nonblockingLzReceive
function on the destination chain will decode and store the received message.
- Deploy the Contract: Deploy the contract on both the source and destination chains with the appropriate endpoints.
- Bridge Tokens: Call the
bridge
function with the desired token amount. The tokens will be burned on the source chain and a message will be sent to mint them on the destination chain. - Mint Tokens: The
_nonblockingLzReceive
function on the destination chain will decode the message and mint the tokens to the specified address.
-
Clone the Repository:
git clone https://github.com/anurag-iitk/layerzero-cross-chain.git cd layerzero-cross-chain
-
Install Dependencies: Make sure you have Node.js installed. Run the following command to install the required dependencies:
npm install
-
Compile the Contracts: Use Hardhat to compile the contracts:
npx hardhat compile
-
Deploy the Contracts: You can deploy the contracts using the Hardhat deployment scripts. Configure the network settings in
hardhat.config.ts
and run:npx hardhat run ignition/modules/deploy.ts --network <network-name>