SeamlesslyConnectedBlockchains
Core Concept of LayerZero
LayerZero is a User Application (UA) configurable on-chain endpoint that runs a ULN. LayerZero relies on two parties to transfer messages between on-chain endpoints: the Oracle and the Relayer.
When a UA sends a message from chain A to chain B, the message is routed through the endpoint on chain A. The endpoint then notifies the UA specified Oracle and Relayer of the message and it's destination chain.
The Oracle forwards the block header to the endpoint on chain B and the Relayer then submits the transaction proof. The proof is validated on the destination chain and the message is forwarded to the destination address.
Funds
Develop with LayerZero
endpoint.send() has six parameters. Here is an explanation of the arguments:
Examples
01/OmniCounter.sol
A LayerZero User Application example to demonstrate message sending.
1pragma solidity 0.8.4;
2pragma abicoder v2;
3
4import "../lzApp/NonblockingLzApp.sol";
5
6contract OmniCounter is NonblockingLzApp {
7 uint public counter;
8
9 constructor(address _lzEndpoint) NonblockingLzApp(_lzEndpoint) {}
10
11 function _nonblockingLzReceive(uint16, bytes memory, uint64, bytes memory) internal override {
12 counter += 1;
13 }
14
15 function incrementCounter(uint16 _dstChainId) public payable {
16 _lzSend(_dstChainId, bytes(""), payable(msg.sender), address(0x0), bytes(""));
17 }
18}
02/PingPong.sol
An example to demonstrate estimateFees() and a recursive call within lzReceive()
1pragma solidity 0.8.4;
2pragma abicoder v2;
3
4import "@openzeppelin/contracts/security/Pausable.sol";
5import "../lzApp/NonblockingLzApp.sol";
6
7contract PingPong is NonblockingLzApp, Pausable {
8 // event emitted every ping() to keep track of consecutive pings count
9 event Ping(uint pings);
10
11 // constructor requires the LayerZero endpoint for this chain
12 constructor(address _endpoint) NonblockingLzApp(_endpoint) {}
13
14 // disable ping-ponging
15 function enable(bool en) external {
16 if (en) {
17 _pause();
18 } else {
19 _unpause();
20 }
21 }
22
03/LZEndpointMock.sol
A mock LayerZero endpoint contract for local testing.
1pragma solidity ^0.8.4;
2pragma abicoder v2;
3
4import "../interfaces/ILayerZeroReceiver.sol";
5import "../interfaces/ILayerZeroEndpoint.sol";
6
7contract LZEndpointMock is ILayerZeroEndpoint {
8 mapping(address => address) public lzEndpointLookup;
9
10 uint16 public mockChainId;
11 address payable public mockOracle;
12 address payable public mockRelayer;
13 uint public mockBlockConfirmations;
14 uint16 public mockLibraryVersion;
15 uint public mockStaticNativeFee;
16 uint16 public mockLayerZeroVersion;
17 uint public nativeFee;
18 uint public zroFee;
19 bool nextMsgBLocked;
20
21 struct StoredPayload {
22 uint64 payloadLength;
23 address dstAddress;
24 bytes32 payloadHash;
25 }
26
27 struct QueuedPayload {
28 address dstAddress;
29 uint64 nonce;
30 bytes payload;
31 }
32