Last active
October 23, 2023 15:10
-
-
Save ayo-klaytn/19746f5a3f4ca7323d758bf9ad31bc3a to your computer and use it in GitHub Desktop.
crossChainHelloWorld
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: Unlicensed | |
pragma solidity ^0.8.0; | |
// This line imports the NonblockingLzApp contract from LayerZero's solidity-examples Github repo. | |
import "https://github.com/LayerZero-Labs/solidity-examples/blob/main/contracts/lzApp/NonblockingLzApp.sol"; | |
// This contract is inheriting from the NonblockingLzApp contract. | |
contract CrossChainHelloWorld is NonblockingLzApp { | |
// A public string variable named "data" is declared. This will be the message sent to the destination. | |
string public data = "Nothing received yet"; | |
// A uint16 variable named "destChainId" is declared to hold the LayerZero Chain Id of the destination blockchain. | |
uint16 destChainId; | |
//This constructor initializes the contract with our source chain's _lzEndpoint. | |
constructor(address _lzEndpoint, address intialOwner) NonblockingLzApp(_lzEndpoint) Ownable(intialOwner) { | |
// Below is an "if statement" to simplify wiring our contract's together. | |
// In this case, we're auto-filling the dest chain Id based on the source endpoint. | |
// For example: if our source endpoint is Klaytn Baobab, then the destination is Polygon Mumbai. | |
// NOTE: This is to simplify our tutorial, and is not standard wiring practice in LayerZero contracts. | |
// Wiring 1: If Source == Klaytn Baobab, then Destination Chain = Polygon Mumbai | |
if (_lzEndpoint == 0x6aB5Ae6822647046626e83ee6dB8187151E1d5ab) destChainId = 10109; | |
// Wiring 2: If Source == Polygon Mumbai, then Destination Chain = Klaytn Baobab | |
if (_lzEndpoint == 0xf69186dfBa60DdB133E91E9A4B5673624293d8F8) destChainId = 10150; | |
} | |
// This function is called when data is received. It overrides the equivalent function in the parent contract. | |
function _nonblockingLzReceive(uint16, bytes memory, uint64, bytes memory _payload) internal override { | |
// The LayerZero _payload (message) is decoded as a string and stored in the "data" variable. | |
data = abi.decode(_payload, (string)); | |
} | |
// This function is called to send the data string to the destination. | |
// It's payable, so that we can use our native gas token to pay for gas fees. | |
function send(string memory _message) public payable { | |
// The message is encoded as bytes and stored in the "payload" variable. | |
bytes memory payload = abi.encode(_message); | |
// The data is sent using the parent contract's _lzSend function. | |
_lzSend(destChainId, payload, payable(msg.sender), address(0x0), bytes(""), msg.value); | |
} | |
// This function allows the contract owner to designate another contract address to trust. | |
// It can only be called by the owner due to the "onlyOwner" modifier. | |
// NOTE: In standard LayerZero contract's, this is done through SetTrustedRemote. | |
function trustAddress(address _otherContract) public onlyOwner { | |
trustedRemoteLookup[destChainId] = abi.encodePacked(_otherContract, address(this)); | |
} | |
// This function estimates the fees for a LayerZero operation. | |
// It calculates the fees required on the source chain, destination chain, and by the LayerZero protocol itself. | |
// @param dstChainId The LayerZero endpoint ID of the destination chain where the transaction is headed. | |
// @param adapterParams The LayerZero relayer parameters used in the transaction. | |
// Default Relayer Adapter Parameters = 0x00010000000000000000000000000000000000000000000000000000000000030d40 | |
// @param _message The message you plan to send across chains. | |
// @return nativeFee The estimated fee required denominated in the native chain's gas token. | |
function estimateFees(uint16 dstChainId, bytes calldata adapterParams, string memory _message) public view returns (uint nativeFee, uint zroFee) { | |
//Input the message you plan to send. | |
bytes memory payload = abi.encode(_message); | |
// Call the estimateFees function on the lzEndpoint contract. | |
// This function estimates the fees required on the source chain, the destination chain, and by the LayerZero protocol. | |
return lzEndpoint.estimateFees(dstChainId, address(this), payload, false, adapterParams); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment