账户
0x02...caa2
0x02...CaA2

0x02...CaA2

$500
此合同的源代码已经过验证!
合同元数据
编译器
0.6.12+commit.27d51765
语言
Solidity
合同源代码
文件 1 的 1:sonm_swap.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.6.12;

interface IERC20Old {
    function balanceOf(address account) external view returns (uint256);
    function transfer(address recipient, uint256 amount) external;
    function transferFrom(address sender, address recipient, uint256 amount) external;
}

interface IERC20New {
    function balanceOf(address account) external view returns (uint256);
    function transfer(address recipient, uint256 amount) external returns (bool);
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
}

contract TokenSwaper {
    using SafeMath for uint256;

    address public newTokenAddr = 0x46d0DAc0926fa16707042CAdC23F1EB4141fe86B;
    address public oldTokenAddr = 0x983F6d60db79ea8cA4eB9968C6aFf8cfA04B3c63;
    address public newApprover;
    address public oldApprover;
    address public owner;
    
    uint256 public toNewDeadline;
    uint256 public toOldDeadline;
    uint256 public toNewRate;

    constructor(
        address _newApprover,
        address _oldApprover,
        uint256 _toNewDeadline,
        uint256 _toOldDeadline,
        uint256 _toNewRate
    ) 
        public 
    {
        newApprover = _newApprover;
        oldApprover = _oldApprover;
        toNewDeadline = _toNewDeadline;
        toOldDeadline = _toOldDeadline;
        toNewRate = _toNewRate;
        owner = msg.sender;
    }

    modifier onlyOwner() {
        require(owner == msg.sender, "Ownable: caller is not the owner");
        _;
    }


    function SwapToNew(uint256 _amount) external {
        require(block.number <= toNewDeadline, "toNew: ended");
        IERC20Old(oldTokenAddr).transferFrom(msg.sender, address(this), _amount);
        uint256 newAmount = _amount.div(toNewRate);
        uint256 newBal = IERC20New(newTokenAddr).balanceOf(address(this));
        if(newBal >= newAmount){
            IERC20New(newTokenAddr).transfer(msg.sender, newAmount);
        }else{
            IERC20New(newTokenAddr).transferFrom(newApprover, msg.sender, newAmount);
        }
    }

    function SwapToOld(uint256 _amount) external {
        require(block.number <= toOldDeadline, "toOld: ended");
        IERC20New(newTokenAddr).transferFrom(msg.sender, address(this), _amount);
        uint256 oldAmount = _amount.mul(toNewRate);
        uint256 oldBal = IERC20Old(oldTokenAddr).balanceOf(address(this));
        if(oldBal >= oldAmount){
            IERC20Old(oldTokenAddr).transfer(msg.sender, oldAmount);
        }else{
            IERC20Old(oldTokenAddr).transferFrom(oldApprover, msg.sender, oldAmount);
        }
    }

    function withdraw(address _token, uint256 _amount) external onlyOwner {
        if(_token == oldTokenAddr){
            IERC20Old(_token).transfer(msg.sender, _amount);
        }else{
            IERC20New(_token).transfer(msg.sender, _amount);
        }
    }

    function setToOldDeadline(uint256 _toOldDeadline) external onlyOwner {
        toOldDeadline = _toOldDeadline;
    }

    function setToNewDeadline(uint256 _toNewDeadline) external onlyOwner {
        toNewDeadline = _toNewDeadline;
    }

    function setOldApprover(address _oldApprover) external onlyOwner {
        oldApprover = _oldApprover;
    }

    function setNewApprover(address _newApprover) external onlyOwner {
        newApprover = _newApprover;
    }
}

library SafeMath {
    function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        if (a == 0) {
            return 0;
        }

        uint256 c = a * b;
        require(c / a == b, "SafeMath: multiplication overflow");

        return c;
    }

    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        return div(a, b, "SafeMath: division by zero");
    }

    function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b > 0, errorMessage);
        uint256 c = a / b;
        return c;
    }

}
设置
{
  "compilationTarget": {
    "sonm_swap.sol": "TokenSwaper"
  },
  "evmVersion": "istanbul",
  "libraries": {},
  "metadata": {
    "bytecodeHash": "ipfs"
  },
  "optimizer": {
    "enabled": false,
    "runs": 200
  },
  "remappings": []
}
ABI
[{"inputs":[{"internalType":"address","name":"_newApprover","type":"address"},{"internalType":"address","name":"_oldApprover","type":"address"},{"internalType":"uint256","name":"_toNewDeadline","type":"uint256"},{"internalType":"uint256","name":"_toOldDeadline","type":"uint256"},{"internalType":"uint256","name":"_toNewRate","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"SwapToNew","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"SwapToOld","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"newApprover","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"newTokenAddr","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"oldApprover","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"oldTokenAddr","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_newApprover","type":"address"}],"name":"setNewApprover","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_oldApprover","type":"address"}],"name":"setOldApprover","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_toNewDeadline","type":"uint256"}],"name":"setToNewDeadline","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_toOldDeadline","type":"uint256"}],"name":"setToOldDeadline","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"toNewDeadline","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"toNewRate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"toOldDeadline","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_token","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"withdraw","outputs":[],"stateMutability":"nonpayable","type":"function"}]