1.首先我们在bsc testnet 和 rinkeby 上各发一份erc721合约 开放mint权限 实现一个nft跨链
function mint(uint256 tokenId,address receiver) external whenNotPaused {
require(!_exists(tokenId), 'Token does exist');
_safeMint(receiver, tokenId );
}
分别以 bsc_nft,rinkeby_nft 表示
2.分别在bsc_testnet 与 rinkeby 链上分别部署一份跨链合约
RinkebyToBsc
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.6;
import "./NonblockingReceiver.sol";
interface Nft {
function mint(uint256 tokenId,address receiver) external;
function burn(uint256 tokenId) external;
}
// chainId: 10001
// endpoint: 0x79a63d6d8BBD5c6dfc774dA79bCcD948EAcb53FA
// nft 0x72a8d73C145b4Aeb1D89530049edc2e66D34CeE3
//
contract RinkebyToBsc is NonblockingReceiver {
uint gasForDestinationLzReceive = 350000;
address public _nft;
uint16 public _chainId;
constructor(uint16 chainId, address nft,address _layerZeroEndpoint){
endpoint = ILayerZeroEndpoint(_layerZeroEndpoint);
_nft = nft;
_chainId = chainId;
}
function _LzReceive(uint16 _srcChainId, bytes memory _srcAddress, uint64 _nonce, bytes memory _payload) override internal {
_srcChainId;
_nonce;
_srcAddress;
// decode
(address toAddr, uint tokenId) = abi.decode(_payload, (address, uint));
Nft(_nft).mint(tokenId,toAddr);
}
function crossChain(uint256 tokenId) public payable {
//require(msg.sender == ownerOf(tokenId), "You must own the token to traverse");
require(trustedRemoteLookup[_chainId].length > 0, "This chain is currently unavailable for travel");
Nft(_nft).burn(tokenId);
bytes memory payload = abi.encode(msg.sender, tokenId);
uint16 version = 1;
bytes memory adapterParams = abi.encodePacked(version, gasForDestinationLzReceive);
// get the fees we need to pay to LayerZero + Relayer to cover message delivery
// you will be refunded for extra gas paid
(uint messageFee, ) = endpoint.estimateFees(_chainId, address(this), payload, false, adapterParams);
require(msg.value >= messageFee, "GG: msg.value not enough to cover messageFee. Send gas for message fees");
endpoint.send{value: msg.value}(
_chainId, // destination chainId
trustedRemoteLookup[_chainId], // destination address of nft contract
payload, // abi.encoded()'ed bytes
payable(msg.sender), // refund address
address(0x0), // 'zroPaymentAddress' unused for this
adapterParams // txParameters
);
}
}
BscToRinkey
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.6;
import "./NonblockingReceiver.sol";
interface Nft {
function mint(uint256 tokenId,address receiver) external;
function burn(uint256 tokenId) external;
}
// chainId: 10002
// endpoint: 0x6Fcb97553D41516Cb228ac03FdC8B9a0a9df04A1
// nft 0x0cE833406B536588eaB8cd6fA1be26Ef2FE43B8c
//0xC4AfE6Df930EF422f00A4270419Ce65FBeB9C640
contract BscToRinkey is NonblockingReceiver {
uint gasForDestinationLzReceive = 350000;
address public _nft;
uint16 public _chainId;
constructor(uint16 chainId, address nft,address _layerZeroEndpoint){ //在构造方法中 初始化号一些基本参数 内容可以在这里找到 https://layerzero.gitbook.io/docs/technical-reference/testnet/testnet-addresses
endpoint = ILayerZeroEndpoint(_layerZeroEndpoint);
_nft = nft;
_chainId = chainId;
}
function _LzReceive(uint16 _srcChainId, bytes memory _srcAddress, uint64 _nonce, bytes memory _payload) override internal { //接收到跨链请求后需要的操作 也就是给用户在当前链mint的一个当前链的nft ,这个函数由ILayerZeroEndpoint 调用传递跨链_payload到目标链
_srcChainId;
_nonce;
_srcAddress;
// decode
(address toAddr, uint tokenId) = abi.decode(_payload, (address, uint));
Nft(_nft).mint(tokenId,toAddr);
}
function crossChain(uint256 tokenId) public payable { //跨链
//require(msg.sender == ownerOf(tokenId), "You must own the token to traverse");
require(trustedRemoteLookup[_chainId].length > 0, "This chain is currently unavailable for travel");
Nft(_nft).burn(tokenId);
bytes memory payload = abi.encode(msg.sender, tokenId); // encode 参数
uint16 version = 1;
bytes memory adapterParams = abi.encodePacked(version, gasForDestinationLzReceive);
// get the fees we need to pay to LayerZero + Relayer to cover message delivery
// you will be refunded for extra gas paid
(uint messageFee, ) = endpoint.estimateFees(_chainId, address(this), payload, false, adapterParams);
require(msg.value >= messageFee, "GG: msg.value not enough to cover messageFee. Send gas for message fees"); //跨链费
endpoint.send{value: msg.value}( //ILayerZeroEndpoint 跨链并带上参数
_chainId, // destination chainId
trustedRemoteLookup[_chainId], // destination address of nft contract //你需要让layerzero跨链后传递消息的目标地址,这个是手动set进去的 以chainId为key的mapping 其实就是我们部署的RinkebyToBsc 地址;
payload, // abi.encoded()'ed bytes
payable(msg.sender), // refund address
address(0x0), // 'zroPaymentAddress' unused for this
adapterParams // txParameters
);
}
}
调用
function setTrustedRemote(uint16 _chainId, bytes calldata _trustedRemote) external {
trustedRemoteLookup[_chainId] = _trustedRemote;
}
set 目标链的目标地址
合约涉及的其他合约可以在官方文档找到 https://layerzero.gitbook.io/docs/guides/interfaces/evm-solidity-interfaces
整个过程还是比较简单 首先layerzero在各个链都有部署了endpoint,endpoint定义的跨链的参数等等, 在执行跨链时 endpoint 会调用当前链的轻节点合约会通知chainLink,和 relayer。chainLink去链下拿到交易在当前链发生的最终证明好像是一个区块的receipthash,relayer 去准备这笔交易的proof ,分别提交到目标链 。目标链的轻节点合约拿到chainLink与relayer 的东西来验证跨链交易 验证通过后 调用 endpoint去调用目标合约的LzReceive 方法 完成跨链。