// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import "https://github.com/LayerZero-Labs/solidity-examples/blob/main/contracts/token/oft/v2/BaseOFTV2.sol"; import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; /* Chain Information: LayerZero Optimism Goerli chainId: 10132 endpoint: 0xae92d5aD7583AD66E49A0c67BAd18F6ba52dDDc1 LayerZero Goerli chainId: 10121 endpoint: 0xbfD2135BFfbb0B5378b56643c2Df8a87552Bfa23 [ { "refundAddress": "0x9Eb404FA6a9ad4bF45f20be8f07a16C491629486", "zroPaymentAddress": "0x0", "adapterParams": ""0x0002000000000000000000000000000000000000000000000000000000000003d0900000000000000000000000000000000000000000000000000000000000000000c5e31322429453e6d196c34fb16b88edc2a58082 } ] {0x9Eb404FA6a9ad4bF45f20be8f07a16C491629486,0x0,0x0002000000000000000000000000000000000000000000000000000000000003d0900000000000000000000000000000000000000000000000000000000000000000c5e31322429453e6d196c34fb16b88edc2a58082} Note: If providedGasLimit >= minGasLimit, Fail: "LZApp: gas limit is too low", where providedGasLimit is _getGasLimit(_adapterParams) and minGasLimit is minDstGasLimit */ contract ProxyOFTV2 is BaseOFTV2 { using SafeERC20 for IERC20; IERC20 internal immutable innerToken; uint internal immutable ld2sdRate; // total amount is transferred from this chain to other chains, ensuring the total is less than uint64.max in sd uint public outboundAmount; constructor(address _token, uint8 _sharedDecimals, address _lzEndpoint) BaseOFTV2(_sharedDecimals, _lzEndpoint) { innerToken = IERC20(_token); (bool success, bytes memory data) = _token.staticcall( abi.encodeWithSignature("decimals()") ); require(success, "ProxyOFT: failed to get token decimals"); uint8 decimals = abi.decode(data, (uint8)); require(_sharedDecimals <= decimals, "ProxyOFT: sharedDecimals must be <= decimals"); ld2sdRate = 10 ** (decimals - _sharedDecimals); } /************************************************************************ * public functions ************************************************************************/ function circulatingSupply() public view virtual override returns (uint) { return innerToken.totalSupply() - outboundAmount; } function token() public view virtual override returns (address) { return address(innerToken); } /************************************************************************ * internal functions ************************************************************************/ function _debitFrom(address _from, uint16, bytes32, uint _amount) internal virtual override returns (uint) { require(_from == _msgSender(), "ProxyOFT: owner is not send caller"); _amount = _transferFrom(_from, address(this), _amount); // _amount still may have dust if the token has transfer fee, then give the dust back to the sender (uint amount, uint dust) = _removeDust(_amount); if (dust > 0) innerToken.safeTransfer(_from, dust); // check total outbound amount outboundAmount += amount; uint cap = _sd2ld(type(uint64).max); require(cap >= outboundAmount, "ProxyOFT: outboundAmount overflow"); return amount; } function _creditTo(uint16, address _toAddress, uint _amount) internal virtual override returns (uint) { outboundAmount -= _amount; // tokens are already in this contract, so no need to transfer if (_toAddress == address(this)) { return _amount; } return _transferFrom(address(this), _toAddress, _amount); } function _transferFrom(address _from, address _to, uint _amount) internal virtual override returns (uint) { uint before = innerToken.balanceOf(_to); if (_from == address(this)) { innerToken.safeTransfer(_to, _amount); } else { innerToken.safeTransferFrom(_from, _to, _amount); } return innerToken.balanceOf(_to) - before; } function _ld2sdRate() internal view virtual override returns (uint) { return ld2sdRate; } }
0.4.18