123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- pragma solidity 0.7.6;
- import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
- import "@openzeppelin/contracts/access/Ownable.sol";
- import "@layerzerolabs/contracts/contracts/interfaces/ILayerZeroEndpoint.sol";
- import "@layerzerolabs/contracts/contracts/interfaces/ILayerZeroReceiver.sol";
- import "@layerzerolabs/contracts/contracts/interfaces/ILayerZeroUserApplicationConfig.sol";
- contract OmnichainFungibleToken is ERC20, Ownable, ILayerZeroReceiver, ILayerZeroUserApplicationConfig {
-
-
- ILayerZeroEndpoint immutable public endpoint;
-
- mapping(uint16 => bytes) public dstContractLookup;
-
- bool public paused;
- bool public isMain;
- event Paused(bool isPaused);
- event SendToChain(uint16 dstChainId, bytes to, uint256 qty);
- event ReceiveFromChain(uint16 srcChainId, uint64 nonce, uint256 qty);
- constructor(
- string memory _name,
- string memory _symbol,
- address _endpoint,
- uint16 _mainChainId,
- uint256 initialSupplyOnMainEndpoint
- ) ERC20(_name, _symbol) {
- if (ILayerZeroEndpoint(_endpoint).getChainId() == _mainChainId) {
- _mint(msg.sender, initialSupplyOnMainEndpoint);
- isMain = true;
- }
-
- endpoint = ILayerZeroEndpoint(_endpoint);
- }
- function pauseSendTokens(bool _pause) external onlyOwner {
- paused = _pause;
- emit Paused(_pause);
- }
- function setDestination(uint16 _dstChainId, bytes calldata _destinationContractAddress) public onlyOwner {
- dstContractLookup[_dstChainId] = _destinationContractAddress;
- }
- function chainId() external view returns (uint16){
- return endpoint.getChainId();
- }
- function sendTokens(
- uint16 _dstChainId,
- bytes calldata _to,
- uint256 _qty,
- address zroPaymentAddress,
- bytes calldata adapterParam
- ) public payable {
- require(!paused, "OFT: sendTokens() is currently paused");
-
- if (isMain) {
-
- _transfer(msg.sender, address(this), _qty);
- } else {
- _burn(msg.sender, _qty);
- }
-
- bytes memory payload = abi.encode(_to, _qty);
-
- endpoint.send{value: msg.value}(
- _dstChainId,
- dstContractLookup[_dstChainId],
- payload,
- msg.sender,
- zroPaymentAddress,
- adapterParam
- );
- emit SendToChain(_dstChainId, _to, _qty);
- }
- function lzReceive(
- uint16 _srcChainId,
- bytes memory _fromAddress,
- uint64 nonce,
- bytes memory _payload
- ) external override {
- require(msg.sender == address(endpoint));
- require(
- _fromAddress.length == dstContractLookup[_srcChainId].length && keccak256(_fromAddress) == keccak256(dstContractLookup[_srcChainId]),
- "OFT: invalid source sending contract"
- );
-
- (bytes memory _to, uint256 _qty) = abi.decode(_payload, (bytes, uint256));
- address toAddress;
-
- assembly {
- toAddress := mload(add(_to, 20))
- }
-
- if (isMain) {
- _transfer(address(this), toAddress, _qty);
- } else {
- _mint(toAddress, _qty);
- }
- emit ReceiveFromChain(_srcChainId, nonce, _qty);
- }
- function estimateSendTokensFee(uint16 _dstChainId, bool _useZro, bytes calldata txParameters) external view returns (uint256 nativeFee, uint256 zroFee) {
- return endpoint.estimateFees(_dstChainId, address(this), bytes(""), _useZro, txParameters);
- }
-
-
- function setConfig(
- uint16 _version,
- uint16 _chainId,
- uint256 _configType,
- bytes calldata _config
- ) external override onlyOwner {
- endpoint.setConfig(_version, _chainId, _configType, _config);
- }
- function setSendVersion(uint16 version) external override onlyOwner {
- endpoint.setSendVersion(version);
- }
- function setReceiveVersion(uint16 version) external override onlyOwner {
- endpoint.setReceiveVersion(version);
- }
- function forceResumeReceive(uint16 _srcChainId, bytes calldata _srcAddress) external override onlyOwner {
- endpoint.forceResumeReceive(_srcChainId, _srcAddress);
- }
- function renounceOwnership() public override onlyOwner {}
- }
|