ETH Price: $2,427.25 (+0.49%)
Gas: 3.5 Gwei

Transaction Decoder

Block:
14222685 at Feb-17-2022 08:54:01 AM +UTC
Transaction Fee:
0.00240482184105975 ETH $5.84
Gas Used:
79,793 Gas / 30.13825575 Gwei

Account State Difference:

  Address   Before After State Difference Code
0x2901823B...57899bbb0
(KSM Starter: Deployer)
1.83877428934452093 Eth
Nonce: 137
1.83636946750346118 Eth
Nonce: 138
0.00240482184105975
(Miner: 0x8f0...be7)
3,499.179498172724252826 Eth3,499.179617862224252826 Eth0.0001196895
0xdAC17F95...13D831ec7

Execution Trace

AnyswapV4Router.anySwapOutUnderlying( token=0x22648C12acD87912EA1710357B1302c6a4154Ebc, to=0x2901823BD9e15B1555b8dE1aC813d2b57899bbb0, amount=100000000, toChainID=56 )
  • AnyswapV5ERC20.STATICCALL( )
  • TetherToken.transferFrom( _from=0x2901823BD9e15B1555b8dE1aC813d2b57899bbb0, _to=0x22648C12acD87912EA1710357B1302c6a4154Ebc, _value=100000000 )
  • AnyswapV5ERC20.depositVault( amount=100000000, to=0x2901823BD9e15B1555b8dE1aC813d2b57899bbb0 ) => ( 100000000 )
  • AnyswapV5ERC20.burn( from=0x2901823BD9e15B1555b8dE1aC813d2b57899bbb0, amount=100000000 ) => ( True )
    File 1 of 3: AnyswapV4Router
    /**
     *Submitted for verification at FtmScan.com on 2021-05-31
    */
    
    /**
     *Submitted for verification at BscScan.com on 2021-04-15
    */
    
    /**
     *Submitted for verification at BscScan.com on 2021-04-08
    */
    
    /**
     *Submitted for verification at hecoinfo.com on 2021-04-08
    */
    
    // SPDX-License-Identifier: GPL-3.0-or-later
    
    pragma solidity >=0.8.0;
    
    interface ISushiswapV2Pair {
        function factory() external view returns (address);
        function token0() external view returns (address);
        function token1() external view returns (address);
        function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
        function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external;
    }
    
    // a library for performing overflow-safe math, courtesy of DappHub (https://github.com/dapphub/ds-math)
    
    library SafeMathSushiswap {
        function add(uint x, uint y) internal pure returns (uint z) {
            require((z = x + y) >= x, 'ds-math-add-overflow');
        }
    
        function sub(uint x, uint y) internal pure returns (uint z) {
            require((z = x - y) <= x, 'ds-math-sub-underflow');
        }
    
        function mul(uint x, uint y) internal pure returns (uint z) {
            require(y == 0 || (z = x * y) / y == x, 'ds-math-mul-overflow');
        }
    }
    
    library SushiswapV2Library {
        using SafeMathSushiswap for uint;
    
        // returns sorted token addresses, used to handle return values from pairs sorted in this order
        function sortTokens(address tokenA, address tokenB) internal pure returns (address token0, address token1) {
            require(tokenA != tokenB, 'SushiswapV2Library: IDENTICAL_ADDRESSES');
            (token0, token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
            require(token0 != address(0), 'SushiswapV2Library: ZERO_ADDRESS');
        }
    
        // calculates the CREATE2 address for a pair without making any external calls
        function pairFor(address factory, address tokenA, address tokenB) internal pure returns (address pair) {
            (address token0, address token1) = sortTokens(tokenA, tokenB);
            pair = address(uint160(uint256(keccak256(abi.encodePacked(
                    hex'ff',
                    factory,
                    keccak256(abi.encodePacked(token0, token1)),
                    hex'e18a34eb0e04b04f7a0ac29a6e80748dca96319b42c54d679cb821dca90c6303' // init code hash
                )))));
        }
    
        // fetches and sorts the reserves for a pair
        function getReserves(address factory, address tokenA, address tokenB) internal view returns (uint reserveA, uint reserveB) {
            (address token0,) = sortTokens(tokenA, tokenB);
            (uint reserve0, uint reserve1,) = ISushiswapV2Pair(pairFor(factory, tokenA, tokenB)).getReserves();
            (reserveA, reserveB) = tokenA == token0 ? (reserve0, reserve1) : (reserve1, reserve0);
        }
    
        // given some amount of an asset and pair reserves, returns an equivalent amount of the other asset
        function quote(uint amountA, uint reserveA, uint reserveB) internal pure returns (uint amountB) {
            require(amountA > 0, 'SushiswapV2Library: INSUFFICIENT_AMOUNT');
            require(reserveA > 0 && reserveB > 0, 'SushiswapV2Library: INSUFFICIENT_LIQUIDITY');
            amountB = amountA.mul(reserveB) / reserveA;
        }
    
        // given an input amount of an asset and pair reserves, returns the maximum output amount of the other asset
        function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) internal pure returns (uint amountOut) {
            require(amountIn > 0, 'SushiswapV2Library: INSUFFICIENT_INPUT_AMOUNT');
            require(reserveIn > 0 && reserveOut > 0, 'SushiswapV2Library: INSUFFICIENT_LIQUIDITY');
            uint amountInWithFee = amountIn.mul(997);
            uint numerator = amountInWithFee.mul(reserveOut);
            uint denominator = reserveIn.mul(1000).add(amountInWithFee);
            amountOut = numerator / denominator;
        }
    
        // given an output amount of an asset and pair reserves, returns a required input amount of the other asset
        function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) internal pure returns (uint amountIn) {
            require(amountOut > 0, 'SushiswapV2Library: INSUFFICIENT_OUTPUT_AMOUNT');
            require(reserveIn > 0 && reserveOut > 0, 'SushiswapV2Library: INSUFFICIENT_LIQUIDITY');
            uint numerator = reserveIn.mul(amountOut).mul(1000);
            uint denominator = reserveOut.sub(amountOut).mul(997);
            amountIn = (numerator / denominator).add(1);
        }
    
        // performs chained getAmountOut calculations on any number of pairs
        function getAmountsOut(address factory, uint amountIn, address[] memory path) internal view returns (uint[] memory amounts) {
            require(path.length >= 2, 'SushiswapV2Library: INVALID_PATH');
            amounts = new uint[](path.length);
            amounts[0] = amountIn;
            for (uint i; i < path.length - 1; i++) {
                (uint reserveIn, uint reserveOut) = getReserves(factory, path[i], path[i + 1]);
                amounts[i + 1] = getAmountOut(amounts[i], reserveIn, reserveOut);
            }
        }
    
        // performs chained getAmountIn calculations on any number of pairs
        function getAmountsIn(address factory, uint amountOut, address[] memory path) internal view returns (uint[] memory amounts) {
            require(path.length >= 2, 'SushiswapV2Library: INVALID_PATH');
            amounts = new uint[](path.length);
            amounts[amounts.length - 1] = amountOut;
            for (uint i = path.length - 1; i > 0; i--) {
                (uint reserveIn, uint reserveOut) = getReserves(factory, path[i - 1], path[i]);
                amounts[i - 1] = getAmountIn(amounts[i], reserveIn, reserveOut);
            }
        }
    }
    
    // helper methods for interacting with ERC20 tokens and sending NATIVE that do not consistently return true/false
    library TransferHelper {
        function safeApprove(address token, address to, uint value) internal {
            // bytes4(keccak256(bytes('approve(address,uint256)')));
            (bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x095ea7b3, to, value));
            require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: APPROVE_FAILED');
        }
    
        function safeTransfer(address token, address to, uint value) internal {
            // bytes4(keccak256(bytes('transfer(address,uint256)')));
            (bool success, bytes memory data) = token.call(abi.encodeWithSelector(0xa9059cbb, to, value));
            require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: TRANSFER_FAILED');
        }
    
        function safeTransferFrom(address token, address from, address to, uint value) internal {
            // bytes4(keccak256(bytes('transferFrom(address,address,uint256)')));
            (bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x23b872dd, from, to, value));
            require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: TRANSFER_FROM_FAILED');
        }
    
        function safeTransferNative(address to, uint value) internal {
            (bool success,) = to.call{value:value}(new bytes(0));
            require(success, 'TransferHelper: NATIVE_TRANSFER_FAILED');
        }
    }
    
    interface IwNATIVE {
        function deposit() external payable;
        function transfer(address to, uint value) external returns (bool);
        function withdraw(uint) external;
    }
    
    interface AnyswapV1ERC20 {
        function mint(address to, uint256 amount) external returns (bool);
        function burn(address from, uint256 amount) external returns (bool);
        function changeVault(address newVault) external returns (bool);
        function depositVault(uint amount, address to) external returns (uint);
        function withdrawVault(address from, uint amount, address to) external returns (uint);
        function underlying() external view returns (address);
    }
    
    /**
     * @dev Interface of the ERC20 standard as defined in the EIP.
     */
    interface IERC20 {
        function totalSupply() external view returns (uint256);
        function balanceOf(address account) external view returns (uint256);
        function transfer(address recipient, uint256 amount) external returns (bool);
        function allowance(address owner, address spender) external view returns (uint256);
        function approve(address spender, uint256 amount) external returns (bool);
        function permit(address target, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) external;
        function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
        function transferWithPermit(address target, address to, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) external returns (bool);
    
        event Transfer(address indexed from, address indexed to, uint256 value);
        event Approval(address indexed owner, address indexed spender, uint256 value);
    }
    
    contract AnyswapV4Router {
        using SafeMathSushiswap for uint;
    
        address public immutable factory;
        address public immutable wNATIVE;
    
        modifier ensure(uint deadline) {
            require(deadline >= block.timestamp, 'AnyswapV3Router: EXPIRED');
            _;
        }
    
        constructor(address _factory, address _wNATIVE, address _mpc) {
            _newMPC = _mpc;
            _newMPCEffectiveTime = block.timestamp;
            factory = _factory;
            wNATIVE = _wNATIVE;
        }
    
        receive() external payable {
            assert(msg.sender == wNATIVE); // only accept Native via fallback from the wNative contract
        }
    
        address private _oldMPC;
        address private _newMPC;
        uint256 private _newMPCEffectiveTime;
    
    
        event LogChangeMPC(address indexed oldMPC, address indexed newMPC, uint indexed effectiveTime, uint chainID);
        event LogChangeRouter(address indexed oldRouter, address indexed newRouter, uint chainID);
        event LogAnySwapIn(bytes32 indexed txhash, address indexed token, address indexed to, uint amount, uint fromChainID, uint toChainID);
        event LogAnySwapOut(address indexed token, address indexed from, address indexed to, uint amount, uint fromChainID, uint toChainID);
        event LogAnySwapTradeTokensForTokens(address[] path, address indexed from, address indexed to, uint amountIn, uint amountOutMin, uint fromChainID, uint toChainID);
        event LogAnySwapTradeTokensForNative(address[] path, address indexed from, address indexed to, uint amountIn, uint amountOutMin, uint fromChainID, uint toChainID);
    
        modifier onlyMPC() {
            require(msg.sender == mpc(), "AnyswapV3Router: FORBIDDEN");
            _;
        }
    
        function mpc() public view returns (address) {
            if (block.timestamp >= _newMPCEffectiveTime) {
                return _newMPC;
            }
            return _oldMPC;
        }
    
        function cID() public view returns (uint id) {
            assembly {id := chainid()}
        }
    
        function changeMPC(address newMPC) public onlyMPC returns (bool) {
            require(newMPC != address(0), "AnyswapV3Router: address(0x0)");
            _oldMPC = mpc();
            _newMPC = newMPC;
            _newMPCEffectiveTime = block.timestamp + 2*24*3600;
            emit LogChangeMPC(_oldMPC, _newMPC, _newMPCEffectiveTime, cID());
            return true;
        }
    
        function changeVault(address token, address newVault) public onlyMPC returns (bool) {
            require(newVault != address(0), "AnyswapV3Router: address(0x0)");
            return AnyswapV1ERC20(token).changeVault(newVault);
        }
    
        function _anySwapOut(address from, address token, address to, uint amount, uint toChainID) internal {
            AnyswapV1ERC20(token).burn(from, amount);
            emit LogAnySwapOut(token, from, to, amount, cID(), toChainID);
        }
    
        // Swaps `amount` `token` from this chain to `toChainID` chain with recipient `to`
        function anySwapOut(address token, address to, uint amount, uint toChainID) external {
            _anySwapOut(msg.sender, token, to, amount, toChainID);
        }
    
        // Swaps `amount` `token` from this chain to `toChainID` chain with recipient `to` by minting with `underlying`
        function anySwapOutUnderlying(address token, address to, uint amount, uint toChainID) external {
            TransferHelper.safeTransferFrom(AnyswapV1ERC20(token).underlying(), msg.sender, token, amount);
            AnyswapV1ERC20(token).depositVault(amount, msg.sender);
            _anySwapOut(msg.sender, token, to, amount, toChainID);
        }
    
        function anySwapOutUnderlyingWithPermit(
            address from,
            address token,
            address to,
            uint amount,
            uint deadline,
            uint8 v,
            bytes32 r,
            bytes32 s,
            uint toChainID
        ) external {
            address _underlying = AnyswapV1ERC20(token).underlying();
            IERC20(_underlying).permit(from, address(this), amount, deadline, v, r, s);
            TransferHelper.safeTransferFrom(_underlying, from, token, amount);
            AnyswapV1ERC20(token).depositVault(amount, from);
            _anySwapOut(from, token, to, amount, toChainID);
        }
    
        function anySwapOutUnderlyingWithTransferPermit(
            address from,
            address token,
            address to,
            uint amount,
            uint deadline,
            uint8 v,
            bytes32 r,
            bytes32 s,
            uint toChainID
        ) external {
            IERC20(AnyswapV1ERC20(token).underlying()).transferWithPermit(from, token, amount, deadline, v, r, s);
            AnyswapV1ERC20(token).depositVault(amount, from);
            _anySwapOut(from, token, to, amount, toChainID);
        }
    
        function anySwapOut(address[] calldata tokens, address[] calldata to, uint[] calldata amounts, uint[] calldata toChainIDs) external {
            for (uint i = 0; i < tokens.length; i++) {
                _anySwapOut(msg.sender, tokens[i], to[i], amounts[i], toChainIDs[i]);
            }
        }
    
        // swaps `amount` `token` in `fromChainID` to `to` on this chainID
        function _anySwapIn(bytes32 txs, address token, address to, uint amount, uint fromChainID) internal {
            AnyswapV1ERC20(token).mint(to, amount);
            emit LogAnySwapIn(txs, token, to, amount, fromChainID, cID());
        }
    
        // swaps `amount` `token` in `fromChainID` to `to` on this chainID
        // triggered by `anySwapOut`
        function anySwapIn(bytes32 txs, address token, address to, uint amount, uint fromChainID) external onlyMPC {
            _anySwapIn(txs, token, to, amount, fromChainID);
        }
    
        // swaps `amount` `token` in `fromChainID` to `to` on this chainID with `to` receiving `underlying`
        function anySwapInUnderlying(bytes32 txs, address token, address to, uint amount, uint fromChainID) external onlyMPC {
            _anySwapIn(txs, token, to, amount, fromChainID);
            AnyswapV1ERC20(token).withdrawVault(to, amount, to);
        }
    
        // swaps `amount` `token` in `fromChainID` to `to` on this chainID with `to` receiving `underlying` if possible
        function anySwapInAuto(bytes32 txs, address token, address to, uint amount, uint fromChainID) external onlyMPC {
            _anySwapIn(txs, token, to, amount, fromChainID);
            AnyswapV1ERC20 _anyToken = AnyswapV1ERC20(token);
            address _underlying = _anyToken.underlying();
            if (_underlying != address(0) && IERC20(_underlying).balanceOf(token) >= amount) {
                _anyToken.withdrawVault(to, amount, to);
            }
        }
    
        // extracts mpc fee from bridge fees
        function anySwapFeeTo(address token, uint amount) external onlyMPC {
            address _mpc = mpc();
            AnyswapV1ERC20(token).mint(_mpc, amount);
            AnyswapV1ERC20(token).withdrawVault(_mpc, amount, _mpc);
        }
    
        function anySwapIn(bytes32[] calldata txs, address[] calldata tokens, address[] calldata to, uint256[] calldata amounts, uint[] calldata fromChainIDs) external onlyMPC {
            for (uint i = 0; i < tokens.length; i++) {
                _anySwapIn(txs[i], tokens[i], to[i], amounts[i], fromChainIDs[i]);
            }
        }
    
        // **** SWAP ****
        // requires the initial amount to have already been sent to the first pair
        function _swap(uint[] memory amounts, address[] memory path, address _to) internal virtual {
            for (uint i; i < path.length - 1; i++) {
                (address input, address output) = (path[i], path[i + 1]);
                (address token0,) = SushiswapV2Library.sortTokens(input, output);
                uint amountOut = amounts[i + 1];
                (uint amount0Out, uint amount1Out) = input == token0 ? (uint(0), amountOut) : (amountOut, uint(0));
                address to = i < path.length - 2 ? SushiswapV2Library.pairFor(factory, output, path[i + 2]) : _to;
                ISushiswapV2Pair(SushiswapV2Library.pairFor(factory, input, output)).swap(
                    amount0Out, amount1Out, to, new bytes(0)
                );
            }
        }
    
        // sets up a cross-chain trade from this chain to `toChainID` for `path` trades to `to`
        function anySwapOutExactTokensForTokens(
            uint amountIn,
            uint amountOutMin,
            address[] calldata path,
            address to,
            uint deadline,
            uint toChainID
        ) external virtual ensure(deadline) {
            AnyswapV1ERC20(path[0]).burn(msg.sender, amountIn);
            emit LogAnySwapTradeTokensForTokens(path, msg.sender, to, amountIn, amountOutMin, cID(), toChainID);
        }
    
        // sets up a cross-chain trade from this chain to `toChainID` for `path` trades to `to`
        function anySwapOutExactTokensForTokensUnderlying(
            uint amountIn,
            uint amountOutMin,
            address[] calldata path,
            address to,
            uint deadline,
            uint toChainID
        ) external virtual ensure(deadline) {
            TransferHelper.safeTransferFrom(AnyswapV1ERC20(path[0]).underlying(), msg.sender, path[0], amountIn);
            AnyswapV1ERC20(path[0]).depositVault(amountIn, msg.sender);
            AnyswapV1ERC20(path[0]).burn(msg.sender, amountIn);
            emit LogAnySwapTradeTokensForTokens(path, msg.sender, to, amountIn, amountOutMin, cID(), toChainID);
        }
    
        // sets up a cross-chain trade from this chain to `toChainID` for `path` trades to `to`
        function anySwapOutExactTokensForTokensUnderlyingWithPermit(
            address from,
            uint amountIn,
            uint amountOutMin,
            address[] calldata path,
            address to,
            uint deadline,
            uint8 v,
            bytes32 r,
            bytes32 s,
            uint toChainID
        ) external virtual ensure(deadline) {
            address _underlying = AnyswapV1ERC20(path[0]).underlying();
            IERC20(_underlying).permit(from, address(this), amountIn, deadline, v, r, s);
            TransferHelper.safeTransferFrom(_underlying, from, path[0], amountIn);
            AnyswapV1ERC20(path[0]).depositVault(amountIn, from);
            AnyswapV1ERC20(path[0]).burn(from, amountIn);
            {
            address[] memory _path = path;
            address _from = from;
            address _to = to;
            uint _amountIn = amountIn;
            uint _amountOutMin = amountOutMin;
            uint _cID = cID();
            uint _toChainID = toChainID;
            emit LogAnySwapTradeTokensForTokens(_path, _from, _to, _amountIn, _amountOutMin, _cID, _toChainID);
            }
        }
    
        // sets up a cross-chain trade from this chain to `toChainID` for `path` trades to `to`
        function anySwapOutExactTokensForTokensUnderlyingWithTransferPermit(
            address from,
            uint amountIn,
            uint amountOutMin,
            address[] calldata path,
            address to,
            uint deadline,
            uint8 v,
            bytes32 r,
            bytes32 s,
            uint toChainID
        ) external virtual ensure(deadline) {
            IERC20(AnyswapV1ERC20(path[0]).underlying()).transferWithPermit(from, path[0], amountIn, deadline, v, r, s);
            AnyswapV1ERC20(path[0]).depositVault(amountIn, from);
            AnyswapV1ERC20(path[0]).burn(from, amountIn);
            emit LogAnySwapTradeTokensForTokens(path, from, to, amountIn, amountOutMin, cID(), toChainID);
        }
    
        // Swaps `amounts[path.length-1]` `path[path.length-1]` to `to` on this chain
        // Triggered by `anySwapOutExactTokensForTokens`
        function anySwapInExactTokensForTokens(
            bytes32 txs,
            uint amountIn,
            uint amountOutMin,
            address[] calldata path,
            address to,
            uint deadline,
            uint fromChainID
        ) external onlyMPC virtual ensure(deadline) returns (uint[] memory amounts) {
            amounts = SushiswapV2Library.getAmountsOut(factory, amountIn, path);
            require(amounts[amounts.length - 1] >= amountOutMin, 'SushiswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT');
            _anySwapIn(txs, path[0], SushiswapV2Library.pairFor(factory, path[0], path[1]), amounts[0], fromChainID);
            _swap(amounts, path, to);
        }
    
        // sets up a cross-chain trade from this chain to `toChainID` for `path` trades to `to`
        function anySwapOutExactTokensForNative(
            uint amountIn,
            uint amountOutMin,
            address[] calldata path,
            address to,
            uint deadline,
            uint toChainID
        ) external virtual ensure(deadline) {
            AnyswapV1ERC20(path[0]).burn(msg.sender, amountIn);
            emit LogAnySwapTradeTokensForNative(path, msg.sender, to, amountIn, amountOutMin, cID(), toChainID);
        }
    
        // sets up a cross-chain trade from this chain to `toChainID` for `path` trades to `to`
        function anySwapOutExactTokensForNativeUnderlying(
            uint amountIn,
            uint amountOutMin,
            address[] calldata path,
            address to,
            uint deadline,
            uint toChainID
        ) external virtual ensure(deadline) {
            TransferHelper.safeTransferFrom(AnyswapV1ERC20(path[0]).underlying(), msg.sender, path[0], amountIn);
            AnyswapV1ERC20(path[0]).depositVault(amountIn, msg.sender);
            AnyswapV1ERC20(path[0]).burn(msg.sender, amountIn);
            emit LogAnySwapTradeTokensForNative(path, msg.sender, to, amountIn, amountOutMin, cID(), toChainID);
        }
    
        // sets up a cross-chain trade from this chain to `toChainID` for `path` trades to `to`
        function anySwapOutExactTokensForNativeUnderlyingWithPermit(
            address from,
            uint amountIn,
            uint amountOutMin,
            address[] calldata path,
            address to,
            uint deadline,
            uint8 v,
            bytes32 r,
            bytes32 s,
            uint toChainID
        ) external virtual ensure(deadline) {
            address _underlying = AnyswapV1ERC20(path[0]).underlying();
            IERC20(_underlying).permit(from, address(this), amountIn, deadline, v, r, s);
            TransferHelper.safeTransferFrom(_underlying, from, path[0], amountIn);
            AnyswapV1ERC20(path[0]).depositVault(amountIn, from);
            AnyswapV1ERC20(path[0]).burn(from, amountIn);
            {
            address[] memory _path = path;
            address _from = from;
            address _to = to;
            uint _amountIn = amountIn;
            uint _amountOutMin = amountOutMin;
            uint _cID = cID();
            uint _toChainID = toChainID;
            emit LogAnySwapTradeTokensForNative(_path, _from, _to, _amountIn, _amountOutMin, _cID, _toChainID);
            }
        }
    
        // sets up a cross-chain trade from this chain to `toChainID` for `path` trades to `to`
        function anySwapOutExactTokensForNativeUnderlyingWithTransferPermit(
            address from,
            uint amountIn,
            uint amountOutMin,
            address[] calldata path,
            address to,
            uint deadline,
            uint8 v,
            bytes32 r,
            bytes32 s,
            uint toChainID
        ) external virtual ensure(deadline) {
            IERC20(AnyswapV1ERC20(path[0]).underlying()).transferWithPermit(from, path[0], amountIn, deadline, v, r, s);
            AnyswapV1ERC20(path[0]).depositVault(amountIn, from);
            AnyswapV1ERC20(path[0]).burn(from, amountIn);
            emit LogAnySwapTradeTokensForNative(path, from, to, amountIn, amountOutMin, cID(), toChainID);
        }
    
        // Swaps `amounts[path.length-1]` `path[path.length-1]` to `to` on this chain
        // Triggered by `anySwapOutExactTokensForNative`
        function anySwapInExactTokensForNative(
            bytes32 txs,
            uint amountIn,
            uint amountOutMin,
            address[] calldata path,
            address to,
            uint deadline,
            uint fromChainID
        ) external onlyMPC virtual ensure(deadline) returns (uint[] memory amounts) {
            require(path[path.length - 1] == wNATIVE, 'AnyswapV3Router: INVALID_PATH');
            amounts = SushiswapV2Library.getAmountsOut(factory, amountIn, path);
            require(amounts[amounts.length - 1] >= amountOutMin, 'AnyswapV3Router: INSUFFICIENT_OUTPUT_AMOUNT');
            _anySwapIn(txs, path[0],  SushiswapV2Library.pairFor(factory, path[0], path[1]), amounts[0], fromChainID);
            _swap(amounts, path, address(this));
            IwNATIVE(wNATIVE).withdraw(amounts[amounts.length - 1]);
            TransferHelper.safeTransferNative(to, amounts[amounts.length - 1]);
        }
    
        // **** LIBRARY FUNCTIONS ****
        function quote(uint amountA, uint reserveA, uint reserveB) public pure virtual returns (uint amountB) {
            return SushiswapV2Library.quote(amountA, reserveA, reserveB);
        }
    
        function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut)
            public
            pure
            virtual
            returns (uint amountOut)
        {
            return SushiswapV2Library.getAmountOut(amountIn, reserveIn, reserveOut);
        }
    
        function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut)
            public
            pure
            virtual
            returns (uint amountIn)
        {
            return SushiswapV2Library.getAmountIn(amountOut, reserveIn, reserveOut);
        }
    
        function getAmountsOut(uint amountIn, address[] memory path)
            public
            view
            virtual
            returns (uint[] memory amounts)
        {
            return SushiswapV2Library.getAmountsOut(factory, amountIn, path);
        }
    
        function getAmountsIn(uint amountOut, address[] memory path)
            public
            view
            virtual
            returns (uint[] memory amounts)
        {
            return SushiswapV2Library.getAmountsIn(factory, amountOut, path);
        }
    }

    File 2 of 3: AnyswapV5ERC20
    /**
     *Submitted for verification at BscScan.com on 2021-06-15
    */
    
    /**
     *Submitted for verification at BscScan.com on 2021-06-11
    */
    
    /**
     *Submitted for verification at polygonscan.com on 2021-06-11
    */
    
    /**
     *Submitted for verification at Etherscan.io on 2021-06-08
    */
    
    /**
     *Submitted for verification at Etherscan.io on 2021-06-07
    */
    
    // SPDX-License-Identifier: GPL-3.0-or-later
    
    pragma solidity 0.8.2;
    
    /**
     * @dev Interface of the ERC20 standard as defined in the EIP.
     */
    interface IERC20 {
        function totalSupply() external view returns (uint256);
        function decimals() external view returns (uint8);
        function balanceOf(address account) external view returns (uint256);
        function transfer(address recipient, uint256 amount) external returns (bool);
        function allowance(address owner, address spender) external view returns (uint256);
        function approve(address spender, uint256 amount) external returns (bool);
        function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
        function permit(address target, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) external;
        function transferWithPermit(address target, address to, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) external returns (bool);
        event Transfer(address indexed from, address indexed to, uint256 value);
        event Approval(address indexed owner, address indexed spender, uint256 value);
    }
    
    /**
     * @dev Interface of the ERC2612 standard as defined in the EIP.
     *
     * Adds the {permit} method, which can be used to change one's
     * {IERC20-allowance} without having to send a transaction, by signing a
     * message. This allows users to spend tokens without having to hold Ether.
     *
     * See https://eips.ethereum.org/EIPS/eip-2612.
     */
    interface IERC2612 {
    
        /**
         * @dev Returns the current ERC2612 nonce for `owner`. This value must be
         * included whenever a signature is generated for {permit}.
         *
         * Every successful call to {permit} increases ``owner``'s nonce by one. This
         * prevents a signature from being used multiple times.
         */
        function nonces(address owner) external view returns (uint256);
    }
    
    /// @dev Wrapped ERC-20 v10 (AnyswapV3ERC20) is an ERC-20 ERC-20 wrapper. You can `deposit` ERC-20 and obtain an AnyswapV3ERC20 balance which can then be operated as an ERC-20 token. You can
    /// `withdraw` ERC-20 from AnyswapV3ERC20, which will then burn AnyswapV3ERC20 token in your wallet. The amount of AnyswapV3ERC20 token in any wallet is always identical to the
    /// balance of ERC-20 deposited minus the ERC-20 withdrawn with that specific wallet.
    interface IAnyswapV3ERC20 is IERC20, IERC2612 {
    
        /// @dev Sets `value` as allowance of `spender` account over caller account's AnyswapV3ERC20 token,
        /// after which a call is executed to an ERC677-compliant contract with the `data` parameter.
        /// Emits {Approval} event.
        /// Returns boolean value indicating whether operation succeeded.
        /// For more information on approveAndCall format, see https://github.com/ethereum/EIPs/issues/677.
        function approveAndCall(address spender, uint256 value, bytes calldata data) external returns (bool);
    
        /// @dev Moves `value` AnyswapV3ERC20 token from caller's account to account (`to`),
        /// after which a call is executed to an ERC677-compliant contract with the `data` parameter.
        /// A transfer to `address(0)` triggers an ERC-20 withdraw matching the sent AnyswapV3ERC20 token in favor of caller.
        /// Emits {Transfer} event.
        /// Returns boolean value indicating whether operation succeeded.
        /// Requirements:
        ///   - caller account must have at least `value` AnyswapV3ERC20 token.
        /// For more information on transferAndCall format, see https://github.com/ethereum/EIPs/issues/677.
        function transferAndCall(address to, uint value, bytes calldata data) external returns (bool);
    }
    
    interface ITransferReceiver {
        function onTokenTransfer(address, uint, bytes calldata) external returns (bool);
    }
    
    interface IApprovalReceiver {
        function onTokenApproval(address, uint, bytes calldata) external returns (bool);
    }
    
    library Address {
        function isContract(address account) internal view returns (bool) {
            bytes32 codehash;
            bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470;
            // solhint-disable-next-line no-inline-assembly
            assembly { codehash := extcodehash(account) }
            return (codehash != 0x0 && codehash != accountHash);
        }
    }
    
    library SafeERC20 {
        using Address for address;
    
        function safeTransfer(IERC20 token, address to, uint value) internal {
            callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
        }
    
        function safeTransferFrom(IERC20 token, address from, address to, uint value) internal {
            callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
        }
    
        function safeApprove(IERC20 token, address spender, uint value) internal {
            require((value == 0) || (token.allowance(address(this), spender) == 0),
                "SafeERC20: approve from non-zero to non-zero allowance"
            );
            callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
        }
        function callOptionalReturn(IERC20 token, bytes memory data) private {
            require(address(token).isContract(), "SafeERC20: call to non-contract");
    
            // solhint-disable-next-line avoid-low-level-calls
            (bool success, bytes memory returndata) = address(token).call(data);
            require(success, "SafeERC20: low-level call failed");
    
            if (returndata.length > 0) { // Return data is optional
                // solhint-disable-next-line max-line-length
                require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
            }
        }
    }
    
    contract AnyswapV5ERC20 is IAnyswapV3ERC20 {
        using SafeERC20 for IERC20;
        string public name;
        string public symbol;
        uint8  public immutable override decimals;
    
        address public immutable underlying;
    
        bytes32 public constant PERMIT_TYPEHASH = keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
        bytes32 public constant TRANSFER_TYPEHASH = keccak256("Transfer(address owner,address to,uint256 value,uint256 nonce,uint256 deadline)");
        bytes32 public immutable DOMAIN_SEPARATOR;
    
        /// @dev Records amount of AnyswapV3ERC20 token owned by account.
        mapping (address => uint256) public override balanceOf;
        uint256 private _totalSupply;
    
        // init flag for setting immediate vault, needed for CREATE2 support
        bool private _init;
    
        // flag to enable/disable swapout vs vault.burn so multiple events are triggered
        bool private _vaultOnly;
    
        // configurable delay for timelock functions
        uint public delay = 2*24*3600;
    
    
        // set of minters, can be this bridge or other bridges
        mapping(address => bool) public isMinter;
        address[] public minters;
    
        // primary controller of the token contract
        address public vault;
    
        address public pendingMinter;
        uint public delayMinter;
    
        address public pendingVault;
        uint public delayVault;
    
        uint public pendingDelay;
        uint public delayDelay;
    
    
        modifier onlyAuth() {
            require(isMinter[msg.sender], "AnyswapV4ERC20: FORBIDDEN");
            _;
        }
    
        modifier onlyVault() {
            require(msg.sender == mpc(), "AnyswapV3ERC20: FORBIDDEN");
            _;
        }
    
        function owner() public view returns (address) {
            return mpc();
        }
    
        function mpc() public view returns (address) {
            if (block.timestamp >= delayVault) {
                return pendingVault;
            }
            return vault;
        }
    
        function setVaultOnly(bool enabled) external onlyVault {
            _vaultOnly = enabled;
        }
    
        function initVault(address _vault) external onlyVault {
            require(_init);
            vault = _vault;
            pendingVault = _vault;
            isMinter[_vault] = true;
            minters.push(_vault);
            delayVault = block.timestamp;
            _init = false;
        }
    
        function setMinter(address _auth) external onlyVault {
            pendingMinter = _auth;
            delayMinter = block.timestamp + delay;
        }
    
        function setVault(address _vault) external onlyVault {
            pendingVault = _vault;
            delayVault = block.timestamp + delay;
        }
    
        function applyVault() external onlyVault {
            require(block.timestamp >= delayVault);
            vault = pendingVault;
        }
    
        function applyMinter() external onlyVault {
            require(block.timestamp >= delayMinter);
            isMinter[pendingMinter] = true;
            minters.push(pendingMinter);
        }
    
        // No time delay revoke minter emergency function
        function revokeMinter(address _auth) external onlyVault {
            isMinter[_auth] = false;
        }
    
        function getAllMinters() external view returns (address[] memory) {
            return minters;
        }
    
    
        function changeVault(address newVault) external onlyVault returns (bool) {
            require(newVault != address(0), "AnyswapV3ERC20: address(0x0)");
            pendingVault = newVault;
            delayVault = block.timestamp + delay;
            emit LogChangeVault(vault, pendingVault, delayVault);
            return true;
        }
    
        function changeMPCOwner(address newVault) public onlyVault returns (bool) {
            require(newVault != address(0), "AnyswapV3ERC20: address(0x0)");
            pendingVault = newVault;
            delayVault = block.timestamp + delay;
            emit LogChangeMPCOwner(vault, pendingVault, delayVault);
            return true;
        }
    
        function mint(address to, uint256 amount) external onlyAuth returns (bool) {
            _mint(to, amount);
            return true;
        }
    
        function burn(address from, uint256 amount) external onlyAuth returns (bool) {
            require(from != address(0), "AnyswapV3ERC20: address(0x0)");
            _burn(from, amount);
            return true;
        }
    
        function Swapin(bytes32 txhash, address account, uint256 amount) public onlyAuth returns (bool) {
            _mint(account, amount);
            emit LogSwapin(txhash, account, amount);
            return true;
        }
    
        function Swapout(uint256 amount, address bindaddr) public returns (bool) {
            require(!_vaultOnly, "AnyswapV4ERC20: onlyAuth");
            require(bindaddr != address(0), "AnyswapV3ERC20: address(0x0)");
            _burn(msg.sender, amount);
            emit LogSwapout(msg.sender, bindaddr, amount);
            return true;
        }
    
        /// @dev Records current ERC2612 nonce for account. This value must be included whenever signature is generated for {permit}.
        /// Every successful call to {permit} increases account's nonce by one. This prevents signature from being used multiple times.
        mapping (address => uint256) public override nonces;
    
        /// @dev Records number of AnyswapV3ERC20 token that account (second) will be allowed to spend on behalf of another account (first) through {transferFrom}.
        mapping (address => mapping (address => uint256)) public override allowance;
    
        event LogChangeVault(address indexed oldVault, address indexed newVault, uint indexed effectiveTime);
        event LogChangeMPCOwner(address indexed oldOwner, address indexed newOwner, uint indexed effectiveHeight);
        event LogSwapin(bytes32 indexed txhash, address indexed account, uint amount);
        event LogSwapout(address indexed account, address indexed bindaddr, uint amount);
        event LogAddAuth(address indexed auth, uint timestamp);
    
        constructor(string memory _name, string memory _symbol, uint8 _decimals, address _underlying, address _vault) {
            name = _name;
            symbol = _symbol;
            decimals = _decimals;
            underlying = _underlying;
            if (_underlying != address(0x0)) {
                require(_decimals == IERC20(_underlying).decimals());
            }
    
            // Use init to allow for CREATE2 accross all chains
            _init = true;
    
            // Disable/Enable swapout for v1 tokens vs mint/burn for v3 tokens
            _vaultOnly = false;
    
            vault = _vault;
            pendingVault = _vault;
            delayVault = block.timestamp;
    
            uint256 chainId;
            assembly {chainId := chainid()}
            DOMAIN_SEPARATOR = keccak256(
                abi.encode(
                    keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
                    keccak256(bytes(name)),
                    keccak256(bytes("1")),
                    chainId,
                    address(this)));
        }
    
        /// @dev Returns the total supply of AnyswapV3ERC20 token as the ETH held in this contract.
        function totalSupply() external view override returns (uint256) {
            return _totalSupply;
        }
    
        function depositWithPermit(address target, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s, address to) external returns (uint) {
            IERC20(underlying).permit(target, address(this), value, deadline, v, r, s);
            IERC20(underlying).safeTransferFrom(target, address(this), value);
            return _deposit(value, to);
        }
    
        function depositWithTransferPermit(address target, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s, address to) external returns (uint) {
            IERC20(underlying).transferWithPermit(target, address(this), value, deadline, v, r, s);
            return _deposit(value, to);
        }
    
        function deposit() external returns (uint) {
            uint _amount = IERC20(underlying).balanceOf(msg.sender);
            IERC20(underlying).safeTransferFrom(msg.sender, address(this), _amount);
            return _deposit(_amount, msg.sender);
        }
    
        function deposit(uint amount) external returns (uint) {
            IERC20(underlying).safeTransferFrom(msg.sender, address(this), amount);
            return _deposit(amount, msg.sender);
        }
    
        function deposit(uint amount, address to) external returns (uint) {
            IERC20(underlying).safeTransferFrom(msg.sender, address(this), amount);
            return _deposit(amount, to);
        }
    
        function depositVault(uint amount, address to) external onlyVault returns (uint) {
            return _deposit(amount, to);
        }
    
        function _deposit(uint amount, address to) internal returns (uint) {
            require(underlying != address(0x0) && underlying != address(this));
            _mint(to, amount);
            return amount;
        }
    
        function withdraw() external returns (uint) {
            return _withdraw(msg.sender, balanceOf[msg.sender], msg.sender);
        }
    
        function withdraw(uint amount) external returns (uint) {
            return _withdraw(msg.sender, amount, msg.sender);
        }
    
        function withdraw(uint amount, address to) external returns (uint) {
            return _withdraw(msg.sender, amount, to);
        }
    
        function withdrawVault(address from, uint amount, address to) external onlyVault returns (uint) {
            return _withdraw(from, amount, to);
        }
    
        function _withdraw(address from, uint amount, address to) internal returns (uint) {
            _burn(from, amount);
            IERC20(underlying).safeTransfer(to, amount);
            return amount;
        }
    
        /** @dev Creates `amount` tokens and assigns them to `account`, increasing
         * the total supply.
         *
         * Emits a {Transfer} event with `from` set to the zero address.
         *
         * Requirements
         *
         * - `to` cannot be the zero address.
         */
        function _mint(address account, uint256 amount) internal {
            require(account != address(0), "ERC20: mint to the zero address");
    
            _totalSupply += amount;
            balanceOf[account] += amount;
            emit Transfer(address(0), account, amount);
        }
    
        /**
         * @dev Destroys `amount` tokens from `account`, reducing the
         * total supply.
         *
         * Emits a {Transfer} event with `to` set to the zero address.
         *
         * Requirements
         *
         * - `account` cannot be the zero address.
         * - `account` must have at least `amount` tokens.
         */
        function _burn(address account, uint256 amount) internal {
            require(account != address(0), "ERC20: burn from the zero address");
    
            balanceOf[account] -= amount;
            _totalSupply -= amount;
            emit Transfer(account, address(0), amount);
        }
    
        /// @dev Sets `value` as allowance of `spender` account over caller account's AnyswapV3ERC20 token.
        /// Emits {Approval} event.
        /// Returns boolean value indicating whether operation succeeded.
        function approve(address spender, uint256 value) external override returns (bool) {
            // _approve(msg.sender, spender, value);
            allowance[msg.sender][spender] = value;
            emit Approval(msg.sender, spender, value);
    
            return true;
        }
    
        /// @dev Sets `value` as allowance of `spender` account over caller account's AnyswapV3ERC20 token,
        /// after which a call is executed to an ERC677-compliant contract with the `data` parameter.
        /// Emits {Approval} event.
        /// Returns boolean value indicating whether operation succeeded.
        /// For more information on approveAndCall format, see https://github.com/ethereum/EIPs/issues/677.
        function approveAndCall(address spender, uint256 value, bytes calldata data) external override returns (bool) {
            // _approve(msg.sender, spender, value);
            allowance[msg.sender][spender] = value;
            emit Approval(msg.sender, spender, value);
    
            return IApprovalReceiver(spender).onTokenApproval(msg.sender, value, data);
        }
    
        /// @dev Sets `value` as allowance of `spender` account over `owner` account's AnyswapV3ERC20 token, given `owner` account's signed approval.
        /// Emits {Approval} event.
        /// Requirements:
        ///   - `deadline` must be timestamp in future.
        ///   - `v`, `r` and `s` must be valid `secp256k1` signature from `owner` account over EIP712-formatted function arguments.
        ///   - the signature must use `owner` account's current nonce (see {nonces}).
        ///   - the signer cannot be zero address and must be `owner` account.
        /// For more information on signature format, see https://eips.ethereum.org/EIPS/eip-2612#specification[relevant EIP section].
        /// AnyswapV3ERC20 token implementation adapted from https://github.com/albertocuestacanada/ERC20Permit/blob/master/contracts/ERC20Permit.sol.
        function permit(address target, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) external override {
            require(block.timestamp <= deadline, "AnyswapV3ERC20: Expired permit");
    
            bytes32 hashStruct = keccak256(
                abi.encode(
                    PERMIT_TYPEHASH,
                    target,
                    spender,
                    value,
                    nonces[target]++,
                    deadline));
    
            require(verifyEIP712(target, hashStruct, v, r, s) || verifyPersonalSign(target, hashStruct, v, r, s));
    
            // _approve(owner, spender, value);
            allowance[target][spender] = value;
            emit Approval(target, spender, value);
        }
    
        function transferWithPermit(address target, address to, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) external override returns (bool) {
            require(block.timestamp <= deadline, "AnyswapV3ERC20: Expired permit");
    
            bytes32 hashStruct = keccak256(
                abi.encode(
                    TRANSFER_TYPEHASH,
                    target,
                    to,
                    value,
                    nonces[target]++,
                    deadline));
    
            require(verifyEIP712(target, hashStruct, v, r, s) || verifyPersonalSign(target, hashStruct, v, r, s));
    
            require(to != address(0) || to != address(this));
    
            uint256 balance = balanceOf[target];
            require(balance >= value, "AnyswapV3ERC20: transfer amount exceeds balance");
    
            balanceOf[target] = balance - value;
            balanceOf[to] += value;
            emit Transfer(target, to, value);
    
            return true;
        }
    
        function verifyEIP712(address target, bytes32 hashStruct, uint8 v, bytes32 r, bytes32 s) internal view returns (bool) {
            bytes32 hash = keccak256(
                abi.encodePacked(
                    "\x19\x01",
                    DOMAIN_SEPARATOR,
                    hashStruct));
            address signer = ecrecover(hash, v, r, s);
            return (signer != address(0) && signer == target);
        }
    
        function verifyPersonalSign(address target, bytes32 hashStruct, uint8 v, bytes32 r, bytes32 s) internal view returns (bool) {
            bytes32 hash = prefixed(hashStruct);
            address signer = ecrecover(hash, v, r, s);
            return (signer != address(0) && signer == target);
        }
    
        // Builds a prefixed hash to mimic the behavior of eth_sign.
        function prefixed(bytes32 hash) internal view returns (bytes32) {
            return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", DOMAIN_SEPARATOR, hash));
        }
    
        /// @dev Moves `value` AnyswapV3ERC20 token from caller's account to account (`to`).
        /// A transfer to `address(0)` triggers an ETH withdraw matching the sent AnyswapV3ERC20 token in favor of caller.
        /// Emits {Transfer} event.
        /// Returns boolean value indicating whether operation succeeded.
        /// Requirements:
        ///   - caller account must have at least `value` AnyswapV3ERC20 token.
        function transfer(address to, uint256 value) external override returns (bool) {
            require(to != address(0) || to != address(this));
            uint256 balance = balanceOf[msg.sender];
            require(balance >= value, "AnyswapV3ERC20: transfer amount exceeds balance");
    
            balanceOf[msg.sender] = balance - value;
            balanceOf[to] += value;
            emit Transfer(msg.sender, to, value);
    
            return true;
        }
    
        /// @dev Moves `value` AnyswapV3ERC20 token from account (`from`) to account (`to`) using allowance mechanism.
        /// `value` is then deducted from caller account's allowance, unless set to `type(uint256).max`.
        /// A transfer to `address(0)` triggers an ETH withdraw matching the sent AnyswapV3ERC20 token in favor of caller.
        /// Emits {Approval} event to reflect reduced allowance `value` for caller account to spend from account (`from`),
        /// unless allowance is set to `type(uint256).max`
        /// Emits {Transfer} event.
        /// Returns boolean value indicating whether operation succeeded.
        /// Requirements:
        ///   - `from` account must have at least `value` balance of AnyswapV3ERC20 token.
        ///   - `from` account must have approved caller to spend at least `value` of AnyswapV3ERC20 token, unless `from` and caller are the same account.
        function transferFrom(address from, address to, uint256 value) external override returns (bool) {
            require(to != address(0) || to != address(this));
            if (from != msg.sender) {
                // _decreaseAllowance(from, msg.sender, value);
                uint256 allowed = allowance[from][msg.sender];
                if (allowed != type(uint256).max) {
                    require(allowed >= value, "AnyswapV3ERC20: request exceeds allowance");
                    uint256 reduced = allowed - value;
                    allowance[from][msg.sender] = reduced;
                    emit Approval(from, msg.sender, reduced);
                }
            }
    
            uint256 balance = balanceOf[from];
            require(balance >= value, "AnyswapV3ERC20: transfer amount exceeds balance");
    
            balanceOf[from] = balance - value;
            balanceOf[to] += value;
            emit Transfer(from, to, value);
    
            return true;
        }
    
        /// @dev Moves `value` AnyswapV3ERC20 token from caller's account to account (`to`),
        /// after which a call is executed to an ERC677-compliant contract with the `data` parameter.
        /// A transfer to `address(0)` triggers an ETH withdraw matching the sent AnyswapV3ERC20 token in favor of caller.
        /// Emits {Transfer} event.
        /// Returns boolean value indicating whether operation succeeded.
        /// Requirements:
        ///   - caller account must have at least `value` AnyswapV3ERC20 token.
        /// For more information on transferAndCall format, see https://github.com/ethereum/EIPs/issues/677.
        function transferAndCall(address to, uint value, bytes calldata data) external override returns (bool) {
            require(to != address(0) || to != address(this));
    
            uint256 balance = balanceOf[msg.sender];
            require(balance >= value, "AnyswapV3ERC20: transfer amount exceeds balance");
    
            balanceOf[msg.sender] = balance - value;
            balanceOf[to] += value;
            emit Transfer(msg.sender, to, value);
    
            return ITransferReceiver(to).onTokenTransfer(msg.sender, value, data);
        }
    }

    File 3 of 3: TetherToken
    pragma solidity ^0.4.17;
    
    /**
     * @title SafeMath
     * @dev Math operations with safety checks that throw on error
     */
    library SafeMath {
        function mul(uint256 a, uint256 b) internal pure returns (uint256) {
            if (a == 0) {
                return 0;
            }
            uint256 c = a * b;
            assert(c / a == b);
            return c;
        }
    
        function div(uint256 a, uint256 b) internal pure returns (uint256) {
            // assert(b > 0); // Solidity automatically throws when dividing by 0
            uint256 c = a / b;
            // assert(a == b * c + a % b); // There is no case in which this doesn't hold
            return c;
        }
    
        function sub(uint256 a, uint256 b) internal pure returns (uint256) {
            assert(b <= a);
            return a - b;
        }
    
        function add(uint256 a, uint256 b) internal pure returns (uint256) {
            uint256 c = a + b;
            assert(c >= a);
            return c;
        }
    }
    
    /**
     * @title Ownable
     * @dev The Ownable contract has an owner address, and provides basic authorization control
     * functions, this simplifies the implementation of "user permissions".
     */
    contract Ownable {
        address public owner;
    
        /**
          * @dev The Ownable constructor sets the original `owner` of the contract to the sender
          * account.
          */
        function Ownable() public {
            owner = msg.sender;
        }
    
        /**
          * @dev Throws if called by any account other than the owner.
          */
        modifier onlyOwner() {
            require(msg.sender == owner);
            _;
        }
    
        /**
        * @dev Allows the current owner to transfer control of the contract to a newOwner.
        * @param newOwner The address to transfer ownership to.
        */
        function transferOwnership(address newOwner) public onlyOwner {
            if (newOwner != address(0)) {
                owner = newOwner;
            }
        }
    
    }
    
    /**
     * @title ERC20Basic
     * @dev Simpler version of ERC20 interface
     * @dev see https://github.com/ethereum/EIPs/issues/20
     */
    contract ERC20Basic {
        uint public _totalSupply;
        function totalSupply() public constant returns (uint);
        function balanceOf(address who) public constant returns (uint);
        function transfer(address to, uint value) public;
        event Transfer(address indexed from, address indexed to, uint value);
    }
    
    /**
     * @title ERC20 interface
     * @dev see https://github.com/ethereum/EIPs/issues/20
     */
    contract ERC20 is ERC20Basic {
        function allowance(address owner, address spender) public constant returns (uint);
        function transferFrom(address from, address to, uint value) public;
        function approve(address spender, uint value) public;
        event Approval(address indexed owner, address indexed spender, uint value);
    }
    
    /**
     * @title Basic token
     * @dev Basic version of StandardToken, with no allowances.
     */
    contract BasicToken is Ownable, ERC20Basic {
        using SafeMath for uint;
    
        mapping(address => uint) public balances;
    
        // additional variables for use if transaction fees ever became necessary
        uint public basisPointsRate = 0;
        uint public maximumFee = 0;
    
        /**
        * @dev Fix for the ERC20 short address attack.
        */
        modifier onlyPayloadSize(uint size) {
            require(!(msg.data.length < size + 4));
            _;
        }
    
        /**
        * @dev transfer token for a specified address
        * @param _to The address to transfer to.
        * @param _value The amount to be transferred.
        */
        function transfer(address _to, uint _value) public onlyPayloadSize(2 * 32) {
            uint fee = (_value.mul(basisPointsRate)).div(10000);
            if (fee > maximumFee) {
                fee = maximumFee;
            }
            uint sendAmount = _value.sub(fee);
            balances[msg.sender] = balances[msg.sender].sub(_value);
            balances[_to] = balances[_to].add(sendAmount);
            if (fee > 0) {
                balances[owner] = balances[owner].add(fee);
                Transfer(msg.sender, owner, fee);
            }
            Transfer(msg.sender, _to, sendAmount);
        }
    
        /**
        * @dev Gets the balance of the specified address.
        * @param _owner The address to query the the balance of.
        * @return An uint representing the amount owned by the passed address.
        */
        function balanceOf(address _owner) public constant returns (uint balance) {
            return balances[_owner];
        }
    
    }
    
    /**
     * @title Standard ERC20 token
     *
     * @dev Implementation of the basic standard token.
     * @dev https://github.com/ethereum/EIPs/issues/20
     * @dev Based oncode by FirstBlood: https://github.com/Firstbloodio/token/blob/master/smart_contract/FirstBloodToken.sol
     */
    contract StandardToken is BasicToken, ERC20 {
    
        mapping (address => mapping (address => uint)) public allowed;
    
        uint public constant MAX_UINT = 2**256 - 1;
    
        /**
        * @dev Transfer tokens from one address to another
        * @param _from address The address which you want to send tokens from
        * @param _to address The address which you want to transfer to
        * @param _value uint the amount of tokens to be transferred
        */
        function transferFrom(address _from, address _to, uint _value) public onlyPayloadSize(3 * 32) {
            var _allowance = allowed[_from][msg.sender];
    
            // Check is not needed because sub(_allowance, _value) will already throw if this condition is not met
            // if (_value > _allowance) throw;
    
            uint fee = (_value.mul(basisPointsRate)).div(10000);
            if (fee > maximumFee) {
                fee = maximumFee;
            }
            if (_allowance < MAX_UINT) {
                allowed[_from][msg.sender] = _allowance.sub(_value);
            }
            uint sendAmount = _value.sub(fee);
            balances[_from] = balances[_from].sub(_value);
            balances[_to] = balances[_to].add(sendAmount);
            if (fee > 0) {
                balances[owner] = balances[owner].add(fee);
                Transfer(_from, owner, fee);
            }
            Transfer(_from, _to, sendAmount);
        }
    
        /**
        * @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.
        * @param _spender The address which will spend the funds.
        * @param _value The amount of tokens to be spent.
        */
        function approve(address _spender, uint _value) public onlyPayloadSize(2 * 32) {
    
            // To change the approve amount you first have to reduce the addresses`
            //  allowance to zero by calling `approve(_spender, 0)` if it is not
            //  already 0 to mitigate the race condition described here:
            //  https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
            require(!((_value != 0) && (allowed[msg.sender][_spender] != 0)));
    
            allowed[msg.sender][_spender] = _value;
            Approval(msg.sender, _spender, _value);
        }
    
        /**
        * @dev Function to check the amount of tokens than an owner allowed to a spender.
        * @param _owner address The address which owns the funds.
        * @param _spender address The address which will spend the funds.
        * @return A uint specifying the amount of tokens still available for the spender.
        */
        function allowance(address _owner, address _spender) public constant returns (uint remaining) {
            return allowed[_owner][_spender];
        }
    
    }
    
    
    /**
     * @title Pausable
     * @dev Base contract which allows children to implement an emergency stop mechanism.
     */
    contract Pausable is Ownable {
      event Pause();
      event Unpause();
    
      bool public paused = false;
    
    
      /**
       * @dev Modifier to make a function callable only when the contract is not paused.
       */
      modifier whenNotPaused() {
        require(!paused);
        _;
      }
    
      /**
       * @dev Modifier to make a function callable only when the contract is paused.
       */
      modifier whenPaused() {
        require(paused);
        _;
      }
    
      /**
       * @dev called by the owner to pause, triggers stopped state
       */
      function pause() onlyOwner whenNotPaused public {
        paused = true;
        Pause();
      }
    
      /**
       * @dev called by the owner to unpause, returns to normal state
       */
      function unpause() onlyOwner whenPaused public {
        paused = false;
        Unpause();
      }
    }
    
    contract BlackList is Ownable, BasicToken {
    
        /////// Getters to allow the same blacklist to be used also by other contracts (including upgraded Tether) ///////
        function getBlackListStatus(address _maker) external constant returns (bool) {
            return isBlackListed[_maker];
        }
    
        function getOwner() external constant returns (address) {
            return owner;
        }
    
        mapping (address => bool) public isBlackListed;
        
        function addBlackList (address _evilUser) public onlyOwner {
            isBlackListed[_evilUser] = true;
            AddedBlackList(_evilUser);
        }
    
        function removeBlackList (address _clearedUser) public onlyOwner {
            isBlackListed[_clearedUser] = false;
            RemovedBlackList(_clearedUser);
        }
    
        function destroyBlackFunds (address _blackListedUser) public onlyOwner {
            require(isBlackListed[_blackListedUser]);
            uint dirtyFunds = balanceOf(_blackListedUser);
            balances[_blackListedUser] = 0;
            _totalSupply -= dirtyFunds;
            DestroyedBlackFunds(_blackListedUser, dirtyFunds);
        }
    
        event DestroyedBlackFunds(address _blackListedUser, uint _balance);
    
        event AddedBlackList(address _user);
    
        event RemovedBlackList(address _user);
    
    }
    
    contract UpgradedStandardToken is StandardToken{
        // those methods are called by the legacy contract
        // and they must ensure msg.sender to be the contract address
        function transferByLegacy(address from, address to, uint value) public;
        function transferFromByLegacy(address sender, address from, address spender, uint value) public;
        function approveByLegacy(address from, address spender, uint value) public;
    }
    
    contract TetherToken is Pausable, StandardToken, BlackList {
    
        string public name;
        string public symbol;
        uint public decimals;
        address public upgradedAddress;
        bool public deprecated;
    
        //  The contract can be initialized with a number of tokens
        //  All the tokens are deposited to the owner address
        //
        // @param _balance Initial supply of the contract
        // @param _name Token Name
        // @param _symbol Token symbol
        // @param _decimals Token decimals
        function TetherToken(uint _initialSupply, string _name, string _symbol, uint _decimals) public {
            _totalSupply = _initialSupply;
            name = _name;
            symbol = _symbol;
            decimals = _decimals;
            balances[owner] = _initialSupply;
            deprecated = false;
        }
    
        // Forward ERC20 methods to upgraded contract if this one is deprecated
        function transfer(address _to, uint _value) public whenNotPaused {
            require(!isBlackListed[msg.sender]);
            if (deprecated) {
                return UpgradedStandardToken(upgradedAddress).transferByLegacy(msg.sender, _to, _value);
            } else {
                return super.transfer(_to, _value);
            }
        }
    
        // Forward ERC20 methods to upgraded contract if this one is deprecated
        function transferFrom(address _from, address _to, uint _value) public whenNotPaused {
            require(!isBlackListed[_from]);
            if (deprecated) {
                return UpgradedStandardToken(upgradedAddress).transferFromByLegacy(msg.sender, _from, _to, _value);
            } else {
                return super.transferFrom(_from, _to, _value);
            }
        }
    
        // Forward ERC20 methods to upgraded contract if this one is deprecated
        function balanceOf(address who) public constant returns (uint) {
            if (deprecated) {
                return UpgradedStandardToken(upgradedAddress).balanceOf(who);
            } else {
                return super.balanceOf(who);
            }
        }
    
        // Forward ERC20 methods to upgraded contract if this one is deprecated
        function approve(address _spender, uint _value) public onlyPayloadSize(2 * 32) {
            if (deprecated) {
                return UpgradedStandardToken(upgradedAddress).approveByLegacy(msg.sender, _spender, _value);
            } else {
                return super.approve(_spender, _value);
            }
        }
    
        // Forward ERC20 methods to upgraded contract if this one is deprecated
        function allowance(address _owner, address _spender) public constant returns (uint remaining) {
            if (deprecated) {
                return StandardToken(upgradedAddress).allowance(_owner, _spender);
            } else {
                return super.allowance(_owner, _spender);
            }
        }
    
        // deprecate current contract in favour of a new one
        function deprecate(address _upgradedAddress) public onlyOwner {
            deprecated = true;
            upgradedAddress = _upgradedAddress;
            Deprecate(_upgradedAddress);
        }
    
        // deprecate current contract if favour of a new one
        function totalSupply() public constant returns (uint) {
            if (deprecated) {
                return StandardToken(upgradedAddress).totalSupply();
            } else {
                return _totalSupply;
            }
        }
    
        // Issue a new amount of tokens
        // these tokens are deposited into the owner address
        //
        // @param _amount Number of tokens to be issued
        function issue(uint amount) public onlyOwner {
            require(_totalSupply + amount > _totalSupply);
            require(balances[owner] + amount > balances[owner]);
    
            balances[owner] += amount;
            _totalSupply += amount;
            Issue(amount);
        }
    
        // Redeem tokens.
        // These tokens are withdrawn from the owner address
        // if the balance must be enough to cover the redeem
        // or the call will fail.
        // @param _amount Number of tokens to be issued
        function redeem(uint amount) public onlyOwner {
            require(_totalSupply >= amount);
            require(balances[owner] >= amount);
    
            _totalSupply -= amount;
            balances[owner] -= amount;
            Redeem(amount);
        }
    
        function setParams(uint newBasisPoints, uint newMaxFee) public onlyOwner {
            // Ensure transparency by hardcoding limit beyond which fees can never be added
            require(newBasisPoints < 20);
            require(newMaxFee < 50);
    
            basisPointsRate = newBasisPoints;
            maximumFee = newMaxFee.mul(10**decimals);
    
            Params(basisPointsRate, maximumFee);
        }
    
        // Called when new token are issued
        event Issue(uint amount);
    
        // Called when tokens are redeemed
        event Redeem(uint amount);
    
        // Called when contract is deprecated
        event Deprecate(address newAddress);
    
        // Called if contract ever adds fees
        event Params(uint feeBasisPoints, uint maxFee);
    }