|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity ^0.8.22; |
| 3 | + |
| 4 | +import {ILayerZeroEndpoint} from "@layerzerolabs/oft-evm/contracts/interfaces/ILayerZeroEndpoint.sol"; |
| 5 | +import {ILayerZeroReceiver} from "@layerzerolabs/oft-evm/contracts/interfaces/ILayerZeroReceiver.sol"; |
| 6 | + |
| 7 | +contract LZEndpointMock is ILayerZeroEndpoint { |
| 8 | + uint16 public chainId; |
| 9 | + mapping(uint16 => address) public destinations; |
| 10 | + |
| 11 | + constructor(uint16 _chainId) { |
| 12 | + chainId = _chainId; |
| 13 | + } |
| 14 | + |
| 15 | + function setDestLzEndpoint(address destLzEndpoint, uint16 destChainId) external { |
| 16 | + destinations[destChainId] = destLzEndpoint; |
| 17 | + } |
| 18 | + |
| 19 | + function estimateFees( |
| 20 | + uint16 _dstChainId, |
| 21 | + address _userApplication, |
| 22 | + bytes calldata _payload, |
| 23 | + bool _payInZRO, |
| 24 | + bytes calldata _adapterParam |
| 25 | + ) external view override returns (uint256 nativeFee, uint256 zroFee) { |
| 26 | + nativeFee = 0.01 ether; |
| 27 | + zroFee = 0; |
| 28 | + } |
| 29 | + |
| 30 | + function send( |
| 31 | + uint16 _dstChainId, |
| 32 | + bytes calldata _destination, |
| 33 | + bytes calldata _payload, |
| 34 | + address payable _refundAddress, |
| 35 | + address _zroPaymentAddress, |
| 36 | + bytes calldata _adapterParams |
| 37 | + ) external payable override returns (bytes memory) { |
| 38 | + address destEndpoint = destinations[_dstChainId]; |
| 39 | + require(destEndpoint != address(0), "Destination not set"); |
| 40 | + |
| 41 | + address dstAddress; |
| 42 | + assembly { |
| 43 | + dstAddress := mload(add(_destination, 20)) |
| 44 | + } |
| 45 | + |
| 46 | + LZEndpointMock(destEndpoint).receivePayload( |
| 47 | + chainId, |
| 48 | + abi.encodePacked(msg.sender), |
| 49 | + dstAddress, |
| 50 | + 0, // nonce doesn't matter for testing |
| 51 | + 100000, // gas limit, doesn't matter for testing |
| 52 | + _payload |
| 53 | + ); |
| 54 | + |
| 55 | + return ""; |
| 56 | + } |
| 57 | + |
| 58 | + function receivePayload( |
| 59 | + uint16 _srcChainId, |
| 60 | + bytes memory _srcAddress, |
| 61 | + address _dstAddress, |
| 62 | + uint64 _nonce, |
| 63 | + uint256 _gasLimit, |
| 64 | + bytes memory _payload |
| 65 | + ) external { |
| 66 | + address srcAddress; |
| 67 | + assembly { |
| 68 | + srcAddress := mload(add(_srcAddress, 20)) |
| 69 | + } |
| 70 | + |
| 71 | + ILayerZeroReceiver(_dstAddress).lzReceive( |
| 72 | + _srcChainId, |
| 73 | + _srcAddress, |
| 74 | + _nonce, |
| 75 | + _payload |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + // Implement the remaining interfaces with minimal functionality for testing |
| 80 | + function getChainId() external view override returns (uint16) { |
| 81 | + return chainId; |
| 82 | + } |
| 83 | + |
| 84 | + function getConfig( |
| 85 | + uint16, /* _version */ |
| 86 | + uint16, /* _chainId */ |
| 87 | + address, /* _userApplication */ |
| 88 | + uint256 /* _configType */ |
| 89 | + ) external pure override returns (bytes memory) { |
| 90 | + return ""; |
| 91 | + } |
| 92 | + |
| 93 | + function getInboundNonce(uint16, bytes calldata) external pure override returns (uint64) { |
| 94 | + return 0; |
| 95 | + } |
| 96 | + |
| 97 | + function getOutboundNonce(uint16, address) external pure override returns (uint64) { |
| 98 | + return 0; |
| 99 | + } |
| 100 | + |
| 101 | + function hasStoredPayload(uint16, bytes calldata) external pure override returns (bool) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + function isReceivingPayload() external pure override returns (bool) { |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + function isSendingPayload() external pure override returns (bool) { |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + function receivePayloadAndRetryAffected( |
| 114 | + uint16, bytes calldata, address, uint64, uint256, bytes calldata, bytes calldata |
| 115 | + ) external pure override {} |
| 116 | + |
| 117 | + function retryPayload( |
| 118 | + uint16, bytes calldata, bytes calldata |
| 119 | + ) external pure override {} |
| 120 | + |
| 121 | + function setConfig( |
| 122 | + uint16, uint16, address, uint256, bytes calldata |
| 123 | + ) external pure override {} |
| 124 | + |
| 125 | + function setSendVersion(uint16) external pure override {} |
| 126 | + |
| 127 | + function setReceiveVersion(uint16) external pure override {} |
| 128 | +} |
0 commit comments