Cryo Explorer Ethereum Mainnet

Address Contract Partially Verified

Address 0xd2667072a2a30e8C21fa276B474FD047ab5FF0F7
Balance 1.3667 ETH
Nonce 1
Code Size 6819 bytes
Indexed Transactions 0
External Etherscan · Sourcify

Contract Bytecode

6819 bytes
0x6080604052600436106101235760003560e01c806379ba5097116100a0578063b8606eef11610064578063b8606eef14610337578063c42069ec1461034d578063e30c39781461036d578063e3d90acf1461038d578063e679b6f5146103a057600080fd5b806379ba50971461028a5780637f6497831461029f578063871bb2c9146102bf5780638bf6e374146102df5780638da5cb5b146102ff57600080fd5b806358d18b3f116100e757806358d18b3f146101f757806359856af01461021757806368443f2e14610237578063685c587a1461025757806369fe0e2d1461026a57600080fd5b806313bfbc4d1461012f5780631640ff3e146101625780632c0f0e87146101775780633af32abf14610197578063548db174146101d757600080fd5b3661012a57005b600080fd5b34801561013b57600080fd5b5061014f61014a36600461156c565b6103c0565b6040519081526020015b60405180910390f35b6101756101703660046115f3565b6103eb565b005b34801561018357600080fd5b5061014f61019236600461156c565b61046f565b3480156101a357600080fd5b506101c76101b236600461164d565b60046020526000908152604090205460ff1681565b6040519015158152602001610159565b3480156101e357600080fd5b506101756101f23660046116ad565b610488565b34801561020357600080fd5b506101756102123660046116ef565b6104f2565b34801561022357600080fd5b506101756102323660046116ef565b61053d565b34801561024357600080fd5b50610175610252366004611778565b6105de565b6101756102653660046117cc565b610659565b34801561027657600080fd5b5061017561028536600461183b565b6107bf565b34801561029657600080fd5b506101756107f5565b3480156102ab57600080fd5b506101756102ba3660046116ad565b610834565b3480156102cb57600080fd5b506101756102da366004611854565b61089a565b3480156102eb57600080fd5b506101756102fa366004611887565b6109b5565b34801561030b57600080fd5b5060005461031f906001600160a01b031681565b6040516001600160a01b039091168152602001610159565b34801561034357600080fd5b5061014f60035481565b34801561035957600080fd5b5061017561036836600461164d565b610b2d565b34801561037957600080fd5b5060015461031f906001600160a01b031681565b61017561039b3660046116ef565b610b60565b3480156103ac57600080fd5b506101756103bb36600461164d565b610bec565b60006103ce82612710611936565b6103da84612710611949565b6103e49190611968565b9392505050565b60025460020361040e5760405163729668f560e11b815260040160405180910390fd5b60028055600354849081101561044e576003546040516306a215ad60e21b8152610445918391600401918252602082015260400190565b60405180910390fd5b61046361045b34876103c0565b858585610c87565b50506001600255505050565b600061271061047e8382611936565b6103da9085611949565b6000546001600160a01b031633146104b2576040516282b42960e81b815260040160405180910390fd5b6104ee828280806020026020016040519081016040528093929190818152602001838360200280828437600092019190915250610d5892505050565b5050565b84600354811015610524576003546040516306a215ad60e21b8152610445918391600401918252602082015260400190565b61053388888888888888610b60565b5050505050505050565b6002546002036105605760405163729668f560e11b815260040160405180910390fd5b600280556003548590811015610597576003546040516306a215ad60e21b8152610445918391600401918252602082015260400190565b476105a48933308b610e20565b6105b2898988888888610e8b565b6105ce336105c96105c3844761198a565b8a6103c0565b610f6b565b5050600160025550505050505050565b6000546001600160a01b03163314610608576040516282b42960e81b815260040160405180910390fd5b60005b82811015610653576106438484838181106106285761062861199d565b905060200201602081019061063d919061164d565b8361089a565b61064c816119b3565b905061060b565b50505050565b60025460020361067c5760405163729668f560e11b815260040160405180910390fd5b6002805560035484908110156106b3576003546040516306a215ad60e21b8152610445918391600401918252602082015260400190565b6040516370a0823160e01b81523060048201526000906001600160a01b038816906370a0823190602401602060405180830381865afa1580156106fa573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061071e91906119cc565b905061072c34868686610c87565b6040516370a0823160e01b81523060048201526107b190889033906107ac9085906001600160a01b038516906370a0823190602401602060405180830381865afa15801561077e573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906107a291906119cc565b6105c3919061198a565b610fe3565b505060016002555050505050565b6000546001600160a01b031633146107e9576040516282b42960e81b815260040160405180910390fd5b6107f281611013565b50565b6001546001600160a01b0316331461081f576040516282b42960e81b815260040160405180910390fd5b610829600061104e565b61083233611098565b565b6000546001600160a01b0316331461085e576040516282b42960e81b815260040160405180910390fd5b6104ee82828080602002602001604051908101604052809392919081815260200183836020028082843760009201919091525061110792505050565b6000546001600160a01b031633146108c4576040516282b42960e81b815260040160405180910390fd5b6001600160a01b0381166108eb5760405163d92e233d60e01b815260040160405180910390fd5b6040516370a0823160e01b81523060048201526000906001600160a01b038416906370a0823190602401602060405180830381865afa158015610932573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061095691906119cc565b9050816001600160a01b0316836001600160a01b03167ff247aed45b8afd5ae54bd3f6f884eb78df14bc00208ca905e6ae335ffe678d8c8360405161099d91815260200190565b60405180910390a36109b0838383610fe3565b505050565b6002546002036109d85760405163729668f560e11b815260040160405180910390fd5b600280556003548590811015610a0f576003546040516306a215ad60e21b8152610445918391600401918252602082015260400190565b6040516370a0823160e01b81523060048201526000906001600160a01b038916906370a0823190602401602060405180830381865afa158015610a56573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610a7a91906119cc565b9050610a888a33308c610e20565b610a968a8a88888888610e8b565b6040516370a0823160e01b8152306004820152610b1c90899033906107ac9085906001600160a01b038516906370a0823190602401602060405180830381865afa158015610ae8573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610b0c91906119cc565b610b16919061198a565b8b6103c0565b505060016002555050505050505050565b6000546001600160a01b03163314610b57576040516282b42960e81b815260040160405180910390fd5b6107f28161104e565b600254600203610b835760405163729668f560e11b815260040160405180910390fd5b600280556003548590811015610bba576003546040516306a215ad60e21b8152610445918391600401918252602082015260400190565b610bcf883330610bca8b8b61046f565b610e20565b610bdd888887878787610e8b565b50506001600255505050505050565b6000546001600160a01b03163314610c16576040516282b42960e81b815260040160405180910390fd5b6001600160a01b038116610c3d5760405163d92e233d60e01b815260040160405180910390fd5b60405147808252906001600160a01b038316907f9f15f0df128ea65ddf3aaa8d614e5f49a3c1e64d78f072b28aabdc36c9d27bf29060200160405180910390a26104ee8282610f6b565b6001600160a01b03831660009081526004602052604090205460ff16610ccb576040516340d53e3360e11b81526001600160a01b0384166004820152602401610445565b600080846001600160a01b0316868585604051610ce99291906119e5565b60006040518083038185875af1925050503d8060008114610d26576040519150601f19603f3d011682016040523d82523d6000602084013e610d2b565b606091505b509150915081610d50578060405163729a0b5b60e01b81526004016104459190611a45565b505050505050565b60005b81518110156104ee576000828281518110610d7857610d7861199d565b60200260200101519050600060046000858581518110610d9a57610d9a61199d565b60200260200101516001600160a01b03166001600160a01b0316815260200190815260200160002060006101000a81548160ff021916908315150217905550806001600160a01b03167ffb8b462bd4e3db069eb434596fcfb88044cfe688f7329d1c4bdfbb113257b1ac60405160405180910390a250610e19816119b3565b9050610d5b565b6040516001600160a01b03808516602483015283166044820152606481018290526106539085906323b872dd60e01b906084015b60408051601f198184030181529190526020810180516001600160e01b03166001600160e01b0319909316929092179091526111cf565b6001600160a01b03841660009081526004602052604090205460ff16610ecf576040516340d53e3360e11b81526001600160a01b0385166004820152602401610445565b610eda8684876112a4565b600080856001600160a01b0316348585604051610ef89291906119e5565b60006040518083038185875af1925050503d8060008114610f35576040519150601f19603f3d011682016040523d82523d6000602084013e610f3a565b606091505b509150915081610f5f578060405163729a0b5b60e01b81526004016104459190611a45565b610533888660006112a4565b600080836001600160a01b03168360405160006040518083038185875af1925050503d8060008114610fb9576040519150601f19603f3d011682016040523d82523d6000602084013e610fbe565b606091505b50915091508161065357806040516311c9b8f560e01b81526004016104459190611a45565b6040516001600160a01b0383166024820152604481018290526109b090849063a9059cbb60e01b90606401610e54565b60038190556040518181527f20461e09b8e557b77e107939f9ce6544698123aad0fc964ac5cc59b7df2e608f9060200160405180910390a150565b600180546001600160a01b0319166001600160a01b0383169081179091556040517f68f49b346b94582a8b5f9d10e3fe3365318fe8f191ff8dce7c59c6cad06b02f590600090a250565b6001600160a01b0381166110bf5760405163d92e233d60e01b815260040160405180910390fd5b600080546001600160a01b0319166001600160a01b038316908117825560405190917f50146d0e3c60aa1d17a70635b05494f864e86144a2201275021014fbf08bafe291a250565b60005b81518110156104ee5760008282815181106111275761112761199d565b602002602001015190506001600460008585815181106111495761114961199d565b60200260200101516001600160a01b03166001600160a01b0316815260200190815260200160002060006101000a81548160ff021916908315150217905550806001600160a01b03167f6e52dd2f27ea2ff599396dc0473c7f182d40ad3a9efe860f37f0c3fce64a851460405160405180910390a2506111c8816119b3565b905061110a565b6000611224826040518060400160405280602081526020017f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c6564815250856001600160a01b03166113339092919063ffffffff16565b90508051600014806112455750808060200190518101906112459190611a58565b6109b05760405162461bcd60e51b815260206004820152602a60248201527f5361666545524332303a204552433230206f7065726174696f6e20646964206e6044820152691bdd081cdd58d8d9595960b21b6064820152608401610445565b604080516001600160a01b038416602482015260448082018490528251808303909101815260649091019091526020810180516001600160e01b031663095ea7b360e01b1790526112f5848261134a565b610653576040516001600160a01b03841660248201526000604482015261132990859063095ea7b360e01b90606401610e54565b61065384826111cf565b606061134284846000856113f3565b949350505050565b6000806000846001600160a01b0316846040516113679190611a7a565b6000604051808303816000865af19150503d80600081146113a4576040519150601f19603f3d011682016040523d82523d6000602084013e6113a9565b606091505b50915091508180156113d35750805115806113d35750808060200190518101906113d39190611a58565b80156113e857506001600160a01b0385163b15155b925050505b92915050565b6060824710156114545760405162461bcd60e51b815260206004820152602660248201527f416464726573733a20696e73756666696369656e742062616c616e636520666f6044820152651c8818d85b1b60d21b6064820152608401610445565b600080866001600160a01b031685876040516114709190611a7a565b60006040518083038185875af1925050503d80600081146114ad576040519150601f19603f3d011682016040523d82523d6000602084013e6114b2565b606091505b50915091506114c3878383876114ce565b979650505050505050565b6060831561153d578251600003611536576001600160a01b0385163b6115365760405162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e74726163740000006044820152606401610445565b5081611342565b61134283838151156115525781518083602001fd5b8060405162461bcd60e51b81526004016104459190611a45565b6000806040838503121561157f57600080fd5b50508035926020909101359150565b80356001600160a01b03811681146115a557600080fd5b919050565b60008083601f8401126115bc57600080fd5b50813567ffffffffffffffff8111156115d457600080fd5b6020830191508360208285010111156115ec57600080fd5b9250929050565b6000806000806060858703121561160957600080fd5b843593506116196020860161158e565b9250604085013567ffffffffffffffff81111561163557600080fd5b611641878288016115aa565b95989497509550505050565b60006020828403121561165f57600080fd5b6103e48261158e565b60008083601f84011261167a57600080fd5b50813567ffffffffffffffff81111561169257600080fd5b6020830191508360208260051b85010111156115ec57600080fd5b600080602083850312156116c057600080fd5b823567ffffffffffffffff8111156116d757600080fd5b6116e385828601611668565b90969095509350505050565b600080600080600080600060c0888a03121561170a57600080fd5b6117138861158e565b9650602088013595506040880135945061172f6060890161158e565b935061173d6080890161158e565b925060a088013567ffffffffffffffff81111561175957600080fd5b6117658a828b016115aa565b989b979a50959850939692959293505050565b60008060006040848603121561178d57600080fd5b833567ffffffffffffffff8111156117a457600080fd5b6117b086828701611668565b90945092506117c390506020850161158e565b90509250925092565b6000806000806000608086880312156117e457600080fd5b6117ed8661158e565b9450602086013593506118026040870161158e565b9250606086013567ffffffffffffffff81111561181e57600080fd5b61182a888289016115aa565b969995985093965092949392505050565b60006020828403121561184d57600080fd5b5035919050565b6000806040838503121561186757600080fd5b6118708361158e565b915061187e6020840161158e565b90509250929050565b60008060008060008060008060e0898b0312156118a357600080fd5b6118ac8961158e565b9750602089013596506118c160408a0161158e565b9550606089013594506118d660808a0161158e565b93506118e460a08a0161158e565b925060c089013567ffffffffffffffff81111561190057600080fd5b61190c8b828c016115aa565b999c989b5096995094979396929594505050565b634e487b7160e01b600052601160045260246000fd5b808201808211156113ed576113ed611920565b600081600019048311821515161561196357611963611920565b500290565b60008261198557634e487b7160e01b600052601260045260246000fd5b500490565b818103818111156113ed576113ed611920565b634e487b7160e01b600052603260045260246000fd5b6000600182016119c5576119c5611920565b5060010190565b6000602082840312156119de57600080fd5b5051919050565b8183823760009101908152919050565b60005b83811015611a105781810151838201526020016119f8565b50506000910152565b60008151808452611a318160208601602086016119f5565b601f01601f19169290920160200192915050565b6020815260006103e46020830184611a19565b600060208284031215611a6a57600080fd5b815180151581146103e457600080fd5b60008251611a8c8184602087016119f5565b919091019291505056fea164736f6c6343000810000a

Verified Source Code Partial Match

Compiler: v0.8.16+commit.07a7930e EVM: london Optimization: Yes (200 runs)
Ownable.sol 43 lines
// SPDX-License-Identifier: MIT

pragma solidity 0.8.16;

abstract contract Ownable {

    error Unauthorized();
    error ZeroAddress();

    event OwnerSet(address indexed newOwner_);
    event PendingOwnerSet(address indexed pendingOwner_);

    address public owner;
    address public pendingOwner;

    modifier onlyOwner() {
        if (msg.sender != owner) revert Unauthorized();

        _;
    }

    function setPendingOwner(address pendingOwner_) external onlyOwner {
        _setPendingOwner(pendingOwner_);
    }

    function acceptOwnership() external {
        if (msg.sender != pendingOwner) revert Unauthorized();

        _setPendingOwner(address(0));
        _setOwner(msg.sender);
    }

    function _setOwner(address owner_) internal {
        if (owner_ == address(0)) revert ZeroAddress();

        emit OwnerSet(owner = owner_);
    }

    function _setPendingOwner(address pendingOwner_) internal {
        emit PendingOwnerSet(pendingOwner = pendingOwner_);
    }

}
SwapFeeRouter.sol 276 lines
// SPDX-License-Identifier: MIT

pragma solidity 0.8.16;

import { IERC20, SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

import { Ownable } from "./Ownable.sol";

// NOTE: There is no non-arbitrary upper-limit for the `feeBasisPoints`, and setting it above 10_000 just pauses the swap functions.

contract SwapFeeRouter is Ownable {

    error ETHTransferFailed(bytes errorData);
    error FeeBasisPointsNotRespected(uint256 expectedFeeBasisPoints_, uint256 actualFeeBasisPoints_);
    error ContractNotWhitelisted(address callee);
    error RenterAttempted();
    error SwapCallFailed(bytes errorData);

    event ContractAddedToWhitelist(address indexed contract_);
    event ContractRemovedFromWhitelist(address indexed contract_);
    event ETHPulled(address indexed destination_, uint256 amount_);
    event FeeSet(uint256 feeBasisPoints_);
    event TokensPulled(address indexed token_, address indexed destination_, uint256 amount_);

    uint256 internal _locked = 1;

    uint256 public feeBasisPoints;  // 1 = 0.01%, 100 = 1%, 10_000 = 100%

    mapping(address => bool) public isWhitelisted;

    constructor(address owner_, uint256 feeBasisPoints_, address[] memory whitelist_) {
        _setOwner(owner_);
        _setFees(feeBasisPoints_);
        _addToWhitelist(whitelist_);
    }

    modifier noRenter() {
        if (_locked == 2) revert RenterAttempted();

        _locked = 2;

        _;

        _locked = 1;
    }

    modifier feeBasisPointsRespected(uint256 feeBasisPoints_) {
        // Revert if the expected fee is less than the current fee.
        if (feeBasisPoints_ < feeBasisPoints) revert FeeBasisPointsNotRespected(feeBasisPoints_, feeBasisPoints);

        _;
    }

    function swapWithFeesOnInput(
        address inAsset_,
        uint256 swapAmount_,
        uint256 feeBasisPoints_,
        address swapContract_,
        address tokenPuller_,
        bytes calldata swapCallData_
    ) public payable noRenter feeBasisPointsRespected(feeBasisPoints_) {
        // Pull funds plus fees from caller.
        // NOTE: Assuming `swapCallData_` is correct, fees will remain in this contract.
        // NOTE: Worst case, assuming `swapCallData_` is incorrect/malicious, this contract loses nothing, but gains nothing.
        SafeERC20.safeTransferFrom(IERC20(inAsset_), msg.sender, address(this), getAmountWithFees(swapAmount_, feeBasisPoints_));

        // Perform the swap (set allowance, swap, unset allowance).
        // NOTE: This assume that the `swapCallData_` instructs the swapContract to send outAsset to correct destination.
        _performSwap(inAsset_, swapAmount_, swapContract_, tokenPuller_, swapCallData_);
    }

    function swapWithFeesOnOutput(
        address inAsset_,
        uint256 swapAmount_,
        address outAsset_,
        uint256 feeBasisPoints_,
        address swapContract_,
        address tokenPuller_,
        bytes calldata swapCallData_
    ) external noRenter feeBasisPointsRespected(feeBasisPoints_) {
        // Track this contract's starting outAsset balance to determine its increase later.
        uint256 startingOutAssetBalance = IERC20(outAsset_).balanceOf(address(this));

        // Pull funds from caller.
        SafeERC20.safeTransferFrom(IERC20(inAsset_), msg.sender, address(this), swapAmount_);

        // Perform the swap (set allowance, swap, unset allowance).
        // NOTE: This assume that the `swapCallData_` instructs the swapContract to send outAsset to this contract.
        _performSwap(inAsset_, swapAmount_, swapContract_, tokenPuller_, swapCallData_);

        // Send the amount of outAsset the swap produced, minus fees, to the destination.
        SafeERC20.safeTransfer(
            IERC20(outAsset_),
            msg.sender,
            getAmountWithoutFees(
                IERC20(outAsset_).balanceOf(address(this)) - startingOutAssetBalance,
                feeBasisPoints_
            )
        );
    }

    function swapFromEthWithFeesOnInput(
        uint256 feeBasisPoints_,
        address swapContract_,
        bytes calldata swapCallData_
    ) external payable noRenter feeBasisPointsRespected(feeBasisPoints_) {
        // Perform the swap (attaching ETH minus fees to call).
        // NOTE: This assume that the `swapCallData_` instructs the swapContract to send outAsset to correct destination.
        _performSwap(getAmountWithoutFees(msg.value, feeBasisPoints_), swapContract_, swapCallData_);
    }

    function swapFromEthWithFeesOnOutput(
        address outAsset_,
        uint256 feeBasisPoints_,
        address swapContract_,
        bytes calldata swapCallData_
    ) external payable noRenter feeBasisPointsRespected(feeBasisPoints_) {
        // Track this contract's starting outAsset balance to determine its increase later.
        uint256 startingOutAssetBalance = IERC20(outAsset_).balanceOf(address(this));

        // Perform the swap (attaching ETH to call).
        // NOTE: This assume that the `swapCallData_` instructs the swapContract to send outAsset to this contract.
        _performSwap(msg.value, swapContract_, swapCallData_);

        // Send the amount of outAsset the swap produced, minus fees, to the destination.
        SafeERC20.safeTransfer(
            IERC20(outAsset_),
            msg.sender,
            getAmountWithoutFees(
                IERC20(outAsset_).balanceOf(address(this)) - startingOutAssetBalance,
                feeBasisPoints_
            )
        );
    }

    function swapToEthWithFeesOnInput(
        address inAsset_,
        uint256 swapAmount_,
        uint256 feeBasisPoints_,
        address swapContract_,
        address tokenPuller_,
        bytes calldata swapCallData_
    ) external feeBasisPointsRespected(feeBasisPoints_) {
        // NOTE: Ths is functionally the same as `swapWithFeesOnInput` since the output is irrelevant.
        // NOTE: No `noRenter` needed since `swapWithFeesOnInput` will check that.
        swapWithFeesOnInput(inAsset_, swapAmount_, feeBasisPoints_, swapContract_, tokenPuller_, swapCallData_);
    }

    function swapToEthWithFeesOnOutput(
        address inAsset_,
        uint256 swapAmount_,
        uint256 feeBasisPoints_,
        address swapContract_,
        address tokenPuller_,
        bytes calldata swapCallData_
    ) external noRenter feeBasisPointsRespected(feeBasisPoints_) {
        // Track this contract's starting ETH balance to determine its increase later.
        uint256 startingETHBalance = address(this).balance;

        // Pull funds from caller.
        SafeERC20.safeTransferFrom(IERC20(inAsset_), msg.sender, address(this), swapAmount_);

        // Perform the swap (set allowance, swap, unset allowance).
        // NOTE: This assume that the `swapCallData_` instructs the swapContract to send ETH to this contract.
        _performSwap(inAsset_, swapAmount_, swapContract_, tokenPuller_, swapCallData_);

        // Send the amount of ETH the swap produced, minus fees, to the destination, and revert if it fails.
        _transferETH(
            msg.sender,
            getAmountWithoutFees(
                address(this).balance - startingETHBalance,
                feeBasisPoints_
            )
        );
    }

    function addToWhitelist(address[] calldata whitelist_) external onlyOwner {
        _addToWhitelist(whitelist_);
    }

    function removeFromWhitelist(address[] calldata whitelist_) external onlyOwner {
        _removeFromWhitelist(whitelist_);
    }

    function setFee(uint256 feeBasisPoints_) external onlyOwner {
        _setFees(feeBasisPoints_);
    }

    function pullToken(address token_, address destination_) public onlyOwner {
        if (destination_ == address(0)) revert ZeroAddress();

        uint256 amount = IERC20(token_).balanceOf(address(this));

        emit TokensPulled(token_, destination_, amount);

        SafeERC20.safeTransfer(IERC20(token_), destination_, amount);
    }

    function pullTokens(address[] calldata tokens_, address destination_) external onlyOwner {
        for (uint256 i; i < tokens_.length; ++i) {
            pullToken(tokens_[i], destination_);
        }
    }

    function pullETH(address destination_) external onlyOwner {
        if (destination_ == address(0)) revert ZeroAddress();

        uint256 amount = address(this).balance;

        emit ETHPulled(destination_, amount);

        _transferETH(destination_, amount);
    }

    function getAmountWithFees(uint256 amountWithoutFees_, uint256 feeBasisPoints_) public pure returns (uint256 amountWithFees_) {
        amountWithFees_ = (amountWithoutFees_ * (10_000 + feeBasisPoints_)) / 10_000;
    }

    function getAmountWithoutFees(uint256 amountWithFees_, uint256 feeBasisPoints_) public pure returns (uint256 amountWithoutFees_) {
        amountWithoutFees_ = (10_000 * amountWithFees_) / (10_000 + feeBasisPoints_);
    }

    function _addToWhitelist(address[] memory whitelist_) internal {
        for (uint256 i; i < whitelist_.length; ++i) {
            address account = whitelist_[i];
            isWhitelisted[whitelist_[i]] = true;
            emit ContractAddedToWhitelist(account);
        }
    }

    function _performSwap(address inAsset_, uint256 swapAmount_, address swapContract_, address tokenPuller_, bytes calldata swapCallData_) internal {
        // Prevent calling contracts that are not whitelisted.
        if (!isWhitelisted[swapContract_]) revert ContractNotWhitelisted(swapContract_);

        // Approve the contract that will pull inAsset.
        SafeERC20.forceApprove(IERC20(inAsset_), tokenPuller_, swapAmount_);

        // Call the swap contract as defined by `swapCallData_`, and revert if it fails.
        ( bool success, bytes memory errorData ) = swapContract_.call{ value: msg.value }(swapCallData_);
        if (!success) revert SwapCallFailed(errorData);

        // Un-approve the contract that pulled inAsset.
        // NOTE: This is important to prevent exploits that rely on allowances to arbitrary swapContracts to be non-zero after swap calls.
        SafeERC20.forceApprove(IERC20(inAsset_), tokenPuller_, 0);
    }

    function _performSwap(uint256 swapAmount_, address swapContract_, bytes calldata swapCallData_) internal {
        // Prevent calling contracts that are not whitelisted.
        if (!isWhitelisted[swapContract_]) revert ContractNotWhitelisted(swapContract_);

        // Call the swap contract as defined by `swapCallData_`, and revert if it fails.
        ( bool success, bytes memory errorData ) = swapContract_.call{ value: swapAmount_ }(swapCallData_);
        if (!success) revert SwapCallFailed(errorData);
    }

    function _removeFromWhitelist(address[] memory whitelist_) internal {
        for (uint256 i; i < whitelist_.length; ++i) {
            address account = whitelist_[i];
            isWhitelisted[whitelist_[i]] = false;
            emit ContractRemovedFromWhitelist(account);
        }
    }

    function _setFees(uint256 feeBasisPoints_) internal {
        emit FeeSet(feeBasisPoints = feeBasisPoints_);
    }

    function _transferETH(address destination_, uint256 amount_) internal {
        // NOTE: callers of this function are validating `destination_` to not be zero.
        ( bool success, bytes memory errorData ) = destination_.call{ value: amount_ }("");
        if (!success) revert ETHTransferFailed(errorData);
    }

    receive() external payable {}

}
Address.sol 244 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/Address.sol)

pragma solidity ^0.8.1;

/**
 * @dev Collection of functions related to the address type
 */
library Address {
    /**
     * @dev Returns true if `account` is a contract.
     *
     * [IMPORTANT]
     * ====
     * It is unsafe to assume that an address for which this function returns
     * false is an externally-owned account (EOA) and not a contract.
     *
     * Among others, `isContract` will return false for the following
     * types of addresses:
     *
     *  - an externally-owned account
     *  - a contract in construction
     *  - an address where a contract will be created
     *  - an address where a contract lived, but was destroyed
     *
     * Furthermore, `isContract` will also return true if the target contract within
     * the same transaction is already scheduled for destruction by `SELFDESTRUCT`,
     * which only has an effect at the end of a transaction.
     * ====
     *
     * [IMPORTANT]
     * ====
     * You shouldn't rely on `isContract` to protect against flash loan attacks!
     *
     * Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
     * like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
     * constructor.
     * ====
     */
    function isContract(address account) internal view returns (bool) {
        // This method relies on extcodesize/address.code.length, which returns 0
        // for contracts in construction, since the code is only stored at the end
        // of the constructor execution.

        return account.code.length > 0;
    }

    /**
     * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
     * `recipient`, forwarding all available gas and reverting on errors.
     *
     * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
     * of certain opcodes, possibly making contracts go over the 2300 gas limit
     * imposed by `transfer`, making them unable to receive funds via
     * `transfer`. {sendValue} removes this limitation.
     *
     * https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/[Learn more].
     *
     * IMPORTANT: because control is transferred to `recipient`, care must be
     * taken to not create reentrancy vulnerabilities. Consider using
     * {ReentrancyGuard} or the
     * https://solidity.readthedocs.io/en/v0.8.0/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
     */
    function sendValue(address payable recipient, uint256 amount) internal {
        require(address(this).balance >= amount, "Address: insufficient balance");

        (bool success, ) = recipient.call{value: amount}("");
        require(success, "Address: unable to send value, recipient may have reverted");
    }

    /**
     * @dev Performs a Solidity function call using a low level `call`. A
     * plain `call` is an unsafe replacement for a function call: use this
     * function instead.
     *
     * If `target` reverts with a revert reason, it is bubbled up by this
     * function (like regular Solidity function calls).
     *
     * Returns the raw returned data. To convert to the expected return value,
     * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
     *
     * Requirements:
     *
     * - `target` must be a contract.
     * - calling `target` with `data` must not revert.
     *
     * _Available since v3.1._
     */
    function functionCall(address target, bytes memory data) internal returns (bytes memory) {
        return functionCallWithValue(target, data, 0, "Address: low-level call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
     * `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal returns (bytes memory) {
        return functionCallWithValue(target, data, 0, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but also transferring `value` wei to `target`.
     *
     * Requirements:
     *
     * - the calling contract must have an ETH balance of at least `value`.
     * - the called Solidity function must be `payable`.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
        return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
    }

    /**
     * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
     * with `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(
        address target,
        bytes memory data,
        uint256 value,
        string memory errorMessage
    ) internal returns (bytes memory) {
        require(address(this).balance >= value, "Address: insufficient balance for call");
        (bool success, bytes memory returndata) = target.call{value: value}(data);
        return verifyCallResultFromTarget(target, success, returndata, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
        return functionStaticCall(target, data, "Address: low-level static call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal view returns (bytes memory) {
        (bool success, bytes memory returndata) = target.staticcall(data);
        return verifyCallResultFromTarget(target, success, returndata, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but performing a delegate call.
     *
     * _Available since v3.4._
     */
    function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
        return functionDelegateCall(target, data, "Address: low-level delegate call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
     * but performing a delegate call.
     *
     * _Available since v3.4._
     */
    function functionDelegateCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal returns (bytes memory) {
        (bool success, bytes memory returndata) = target.delegatecall(data);
        return verifyCallResultFromTarget(target, success, returndata, errorMessage);
    }

    /**
     * @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling
     * the revert reason or using the provided one) in case of unsuccessful call or if target was not a contract.
     *
     * _Available since v4.8._
     */
    function verifyCallResultFromTarget(
        address target,
        bool success,
        bytes memory returndata,
        string memory errorMessage
    ) internal view returns (bytes memory) {
        if (success) {
            if (returndata.length == 0) {
                // only check isContract if the call was successful and the return data is empty
                // otherwise we already know that it was a contract
                require(isContract(target), "Address: call to non-contract");
            }
            return returndata;
        } else {
            _revert(returndata, errorMessage);
        }
    }

    /**
     * @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the
     * revert reason or using the provided one.
     *
     * _Available since v4.3._
     */
    function verifyCallResult(
        bool success,
        bytes memory returndata,
        string memory errorMessage
    ) internal pure returns (bytes memory) {
        if (success) {
            return returndata;
        } else {
            _revert(returndata, errorMessage);
        }
    }

    function _revert(bytes memory returndata, string memory errorMessage) private pure {
        // Look for revert reason and bubble it up if present
        if (returndata.length > 0) {
            // The easiest way to bubble the revert reason is using memory via assembly
            /// @solidity memory-safe-assembly
            assembly {
                let returndata_size := mload(returndata)
                revert(add(32, returndata), returndata_size)
            }
        } else {
            revert(errorMessage);
        }
    }
}
IERC20.sol 78 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/IERC20.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);

    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `from` to `to` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address from, address to, uint256 amount) external returns (bool);
}
SafeERC20.sol 143 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.3) (token/ERC20/utils/SafeERC20.sol)

pragma solidity ^0.8.0;

import "../IERC20.sol";
import "../extensions/IERC20Permit.sol";
import "../../../utils/Address.sol";

/**
 * @title SafeERC20
 * @dev Wrappers around ERC20 operations that throw on failure (when the token
 * contract returns false). Tokens that return no value (and instead revert or
 * throw on failure) are also supported, non-reverting calls are assumed to be
 * successful.
 * To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
 * which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
 */
library SafeERC20 {
    using Address for address;

    /**
     * @dev Transfer `value` amount of `token` from the calling contract to `to`. If `token` returns no value,
     * non-reverting calls are assumed to be successful.
     */
    function safeTransfer(IERC20 token, address to, uint256 value) internal {
        _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
    }

    /**
     * @dev Transfer `value` amount of `token` from `from` to `to`, spending the approval given by `from` to the
     * calling contract. If `token` returns no value, non-reverting calls are assumed to be successful.
     */
    function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
        _callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
    }

    /**
     * @dev Deprecated. This function has issues similar to the ones found in
     * {IERC20-approve}, and its usage is discouraged.
     *
     * Whenever possible, use {safeIncreaseAllowance} and
     * {safeDecreaseAllowance} instead.
     */
    function safeApprove(IERC20 token, address spender, uint256 value) internal {
        // safeApprove should only be called when setting an initial allowance,
        // or when resetting it to zero. To increase and decrease it, use
        // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
        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));
    }

    /**
     * @dev Increase the calling contract's allowance toward `spender` by `value`. If `token` returns no value,
     * non-reverting calls are assumed to be successful.
     */
    function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
        uint256 oldAllowance = token.allowance(address(this), spender);
        _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance + value));
    }

    /**
     * @dev Decrease the calling contract's allowance toward `spender` by `value`. If `token` returns no value,
     * non-reverting calls are assumed to be successful.
     */
    function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
        unchecked {
            uint256 oldAllowance = token.allowance(address(this), spender);
            require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
            _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance - value));
        }
    }

    /**
     * @dev Set the calling contract's allowance toward `spender` to `value`. If `token` returns no value,
     * non-reverting calls are assumed to be successful. Meant to be used with tokens that require the approval
     * to be set to zero before setting it to a non-zero value, such as USDT.
     */
    function forceApprove(IERC20 token, address spender, uint256 value) internal {
        bytes memory approvalCall = abi.encodeWithSelector(token.approve.selector, spender, value);

        if (!_callOptionalReturnBool(token, approvalCall)) {
            _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, 0));
            _callOptionalReturn(token, approvalCall);
        }
    }

    /**
     * @dev Use a ERC-2612 signature to set the `owner` approval toward `spender` on `token`.
     * Revert on invalid signature.
     */
    function safePermit(
        IERC20Permit token,
        address owner,
        address spender,
        uint256 value,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) internal {
        uint256 nonceBefore = token.nonces(owner);
        token.permit(owner, spender, value, deadline, v, r, s);
        uint256 nonceAfter = token.nonces(owner);
        require(nonceAfter == nonceBefore + 1, "SafeERC20: permit did not succeed");
    }

    /**
     * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
     * on the return value: the return value is optional (but if data is returned, it must not be false).
     * @param token The token targeted by the call.
     * @param data The call data (encoded using abi.encode or one of its variants).
     */
    function _callOptionalReturn(IERC20 token, bytes memory data) private {
        // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
        // we're implementing it ourselves. We use {Address-functionCall} to perform this call, which verifies that
        // the target address contains contract code and also asserts for success in the low-level call.

        bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
        require(returndata.length == 0 || abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
    }

    /**
     * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
     * on the return value: the return value is optional (but if data is returned, it must not be false).
     * @param token The token targeted by the call.
     * @param data The call data (encoded using abi.encode or one of its variants).
     *
     * This is a variant of {_callOptionalReturn} that silents catches all reverts and returns a bool instead.
     */
    function _callOptionalReturnBool(IERC20 token, bytes memory data) private returns (bool) {
        // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
        // we're implementing it ourselves. We cannot use {Address-functionCall} here since this should return false
        // and not revert is the subcall reverts.

        (bool success, bytes memory returndata) = address(token).call(data);
        return
            success && (returndata.length == 0 || abi.decode(returndata, (bool))) && Address.isContract(address(token));
    }
}
IERC20Permit.sol 60 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/extensions/IERC20Permit.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in
 * https://eips.ethereum.org/EIPS/eip-2612[EIP-2612].
 *
 * Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by
 * presenting a message signed by the account. By not relying on {IERC20-approve}, the token holder account doesn't
 * need to send a transaction, and thus is not required to hold Ether at all.
 */
interface IERC20Permit {
    /**
     * @dev Sets `value` as the allowance of `spender` over ``owner``'s tokens,
     * given ``owner``'s signed approval.
     *
     * IMPORTANT: The same issues {IERC20-approve} has related to transaction
     * ordering also apply here.
     *
     * Emits an {Approval} event.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     * - `deadline` must be a timestamp in the future.
     * - `v`, `r` and `s` must be a valid `secp256k1` signature from `owner`
     * over the EIP712-formatted function arguments.
     * - the signature must use ``owner``'s current nonce (see {nonces}).
     *
     * For more information on the signature format, see the
     * https://eips.ethereum.org/EIPS/eip-2612#specification[relevant EIP
     * section].
     */
    function permit(
        address owner,
        address spender,
        uint256 value,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) external;

    /**
     * @dev Returns the current 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 Returns the domain separator used in the encoding of the signature for {permit}, as defined by {EIP712}.
     */
    // solhint-disable-next-line func-name-mixedcase
    function DOMAIN_SEPARATOR() external view returns (bytes32);
}

Read Contract

feeBasisPoints 0xb8606eef → uint256
getAmountWithFees 0x2c0f0e87 → uint256
getAmountWithoutFees 0x13bfbc4d → uint256
isWhitelisted 0x3af32abf → bool
owner 0x8da5cb5b → address
pendingOwner 0xe30c3978 → address

Write Contract 14 functions

These functions modify contract state and require a wallet transaction to execute.

acceptOwnership 0x79ba5097
No parameters
addToWhitelist 0x7f649783
address[] whitelist_
pullETH 0xe679b6f5
address destination_
pullToken 0x871bb2c9
address token_
address destination_
pullTokens 0x68443f2e
address[] tokens_
address destination_
removeFromWhitelist 0x548db174
address[] whitelist_
setFee 0x69fe0e2d
uint256 feeBasisPoints_
setPendingOwner 0xc42069ec
address pendingOwner_
swapFromEthWithFeesOnInput 0x1640ff3e
uint256 feeBasisPoints_
address swapContract_
bytes swapCallData_
swapFromEthWithFeesOnOutput 0x685c587a
address outAsset_
uint256 feeBasisPoints_
address swapContract_
bytes swapCallData_
swapToEthWithFeesOnInput 0x58d18b3f
address inAsset_
uint256 swapAmount_
uint256 feeBasisPoints_
address swapContract_
address tokenPuller_
bytes swapCallData_
swapToEthWithFeesOnOutput 0x59856af0
address inAsset_
uint256 swapAmount_
uint256 feeBasisPoints_
address swapContract_
address tokenPuller_
bytes swapCallData_
swapWithFeesOnInput 0xe3d90acf
address inAsset_
uint256 swapAmount_
uint256 feeBasisPoints_
address swapContract_
address tokenPuller_
bytes swapCallData_
swapWithFeesOnOutput 0x8bf6e374
address inAsset_
uint256 swapAmount_
address outAsset_
uint256 feeBasisPoints_
address swapContract_
address tokenPuller_
bytes swapCallData_

Recent Transactions

No transactions found for this address