Forkchoice Ethereum Mainnet

Address Contract Verified

Address 0x313e7Ef7d52f5C10aC04ebaa4d33CDc68634c212
Balance 0 ETH
Nonce 1
Code Size 9349 bytes
Indexed Transactions 0 (1 on-chain, 1.2% indexed)
External Etherscan · Sourcify

Contract Bytecode

9349 bytes
0x6080604052600436101561001a575b3615610018575f80fd5b005b5f3560e01c632bdb823c0361000e5760607ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc36011261025a5760043573ffffffffffffffffffffffffffffffffffffffff81169081810361025a576024359060443567ffffffffffffffff811161025a57806004019160807ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc833603011261025a57826100c6916107d1565b6100d260248201610649565b6064820135801515810361025a571561060e578235600b81101561025a576100fe60448694018561066a565b9390916001810361031b5750508260645f927fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0601f60405197889586947fe4da6f2e0000000000000000000000000000000000000000000000000000000086526040600487015281604487015286860137868582860101528a602485015201168101030181732f577a41bec1be1152aeea12e73b7391d15f655d5afa8015610310575f925f916102c3575b50911561026557610221915b8561025e5784905b604051926101cc606085610720565b602984527f416464726573733a206c6f772d6c6576656c2063616c6c20776974682076616c60208501527f7565206661696c6564000000000000000000000000000000000000000000000060408501526120db565b503590600b82101561025a5760207f1d8c50a59805451ff93bb2e438559a86b75386bcac2a591d3181d79e7e8346fd91604051908152a3005b5f80fd5b5f906101bd565b60646040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602060248201527f50656e646c65537761703a204b79626572207363616c696e67206661696c65646044820152fd5b9250503d805f843e6102d58184610720565b82019160408184031261025a576102eb81610827565b92602082015167ffffffffffffffff811161025a5761030a9201610855565b5f6101a9565b6040513d5f823e3d90fd5b92939192600281036105db5750508060041161025a577fffffffff00000000000000000000000000000000000000000000000000000000823516907f3b635ce40000000000000000000000000000000000000000000000000000000082036105ae578201917ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc81840301610140811261025a5760e01361025a57604051926103c2846106bb565b6103ce60048301610628565b84526020840190602483013582526103e860448401610628565b90604086019182526103fc60648501610628565b60608701908152608087016084860135815260a088019160a4870135835261042660c48801610628565b9360c08a0194855260e48801359067ffffffffffffffff821161025a576004610451928a01016112a7565b946101246104626101048a01610628565b9801359663ffffffff881680980361025a578d84519061048191610beb565b815161048c91610c2b565b84528d85519061049b91610beb565b81516104a691610c2b565b85528d81526040519a8b9a60208c01525173ffffffffffffffffffffffffffffffffffffffff1660248b01525160448a01525173ffffffffffffffffffffffffffffffffffffffff1660648901525173ffffffffffffffffffffffffffffffffffffffff1660848801525160a48701525160c48601525173ffffffffffffffffffffffffffffffffffffffff1660e485015261010484016101409052610164840161055091610c83565b9173ffffffffffffffffffffffffffffffffffffffff16610124840152610144830152037fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0810182526105a39082610720565b90610221915b6101b5565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52600160045260245ffd5b90939290600481036105f65750926105a991610221946119a4565b9093506005036105ae57846105a99161022194610cc6565b6105a9610621604461022194018561066a565b369161079b565b359073ffffffffffffffffffffffffffffffffffffffff8216820361025a57565b3573ffffffffffffffffffffffffffffffffffffffff8116810361025a5790565b9035907fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe18136030182121561025a570180359067ffffffffffffffff821161025a5760200191813603831361025a57565b60e0810190811067ffffffffffffffff8211176106d757604052565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b60a0810190811067ffffffffffffffff8211176106d757604052565b90601f7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0910116810190811067ffffffffffffffff8211176106d757604052565b67ffffffffffffffff81116106d757601f017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe01660200190565b9291926107a782610761565b916107b56040519384610720565b82948184528183011161025a578281602093845f960137010152565b73ffffffffffffffffffffffffffffffffffffffff168015610823578135600b81101561025a5760040361081457610812915061080c610abc565b9061089a565b565b61080c60206108129301610649565b5050565b5190811515820361025a57565b5f5b8381106108455750505f910152565b8181015183820152602001610836565b81601f8201121561025a57805161086b81610761565b926108796040519485610720565b8184526020828401011161025a576108979160208085019101610834565b90565b73ffffffffffffffffffffffffffffffffffffffff81169073ffffffffffffffffffffffffffffffffffffffff604051937fdd62ed3e0000000000000000000000000000000000000000000000000000000085523060048601521692836024820152602081604481865afa8015610310575f90610a6a575b7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff91501061093f57505050565b604051915f80602085017f095ea7b30000000000000000000000000000000000000000000000000000000081528660248701527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff6044870152604486526109a7606487610720565b85519082855af1906109b76120ac565b82610a30575b5081610a25575b50156109cf57505050565b610a2061081293604051907f095ea7b300000000000000000000000000000000000000000000000000000000602083015260248201525f604482015260448152610a1a606482610720565b8261232b565b61232b565b90503b15155f6109c4565b80519192508115918215610a48575b5050905f6109bd565b819250906020918101031261025a576020610a639101610827565b5f80610a3f565b506020813d602011610ab4575b81610a8460209383610720565b8101031261025a577fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff9051610912565b3d9150610a77565b60014614610bd357600a4614610bbb5760384614610ba35761a4b14614610b8b576121054614610b73576113884614610b735760846040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602360248201527f50656e646c65537761703a204f4b5820436861696e206e6f7420737570706f7260448201527f74656400000000000000000000000000000000000000000000000000000000006064820152fd5b7357df6092665eb6058de53939612413ff4b09114e90565b7370cbb871e8f30fc8ce23609e9e0ea87b6b222f5890565b732c34a2fb1d0b4f55de51e1d0bdefaddce6b7cdd690565b7368d6b739d2020067d1e2f713b999da97e4d5481290565b7340aa958dd87fc8305b97f2ba922cddca374bcd7f90565b81810292918115918404141715610bfe57565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b8115610c35570490565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601260045260245ffd5b519073ffffffffffffffffffffffffffffffffffffffff8216820361025a57565b907fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0601f602093610cbf81518092818752878088019101610834565b0116010190565b90916060928060041161025a57610d29907fffffffff000000000000000000000000000000000000000000000000000000008435169360047ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc369301910161079b565b927fe2c95c8200000000000000000000000000000000000000000000000000000000831461111857507fea76dddf0000000000000000000000000000000000000000000000000000000082146110a4577ff7a7005600000000000000000000000000000000000000000000000000000000821461101e577f07ed2379000000000000000000000000000000000000000000000000000000008214610e255760646040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601b60248201527f31696e63683a20756e737570706f727465642073656c6563746f7200000000006044820152fd5b8251830183810393610120851261025a5760e07fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0610e6560208401610c62565b96011261025a57604051610e78816106bb565b610e8460408301610c62565b8152610e9260608301610c62565b60208201908152610ea560808401610c62565b60408301908152610eb860a08501610c62565b916060840192835260c0850151936080810194855260e08601519560a082019687526101008101519760c083019889526101208201519067ffffffffffffffff821161025a57602001910160200190610f1091610855565b9780875190610f1e91610beb565b8651610f2991610c2b565b87528552604051998a9960208b015273ffffffffffffffffffffffffffffffffffffffff1660248a01525173ffffffffffffffffffffffffffffffffffffffff1660448901525173ffffffffffffffffffffffffffffffffffffffff1660648801525173ffffffffffffffffffffffffffffffffffffffff1660848701525173ffffffffffffffffffffffffffffffffffffffff1660a48601525160c48501525160e484015251610104830152610124820161012090526101448201610fee91610c83565b037fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0810182526108979082610720565b909160e08180518101031261025a5760208101519160408201519260608301519160808401519461106760a0860151946110628460e060c08a015199015199610beb565b610c2b565b92604051976020890152602488015260448701526064860152608485015260a484015260c483015260e482015260e4815261089761010482610720565b909160c08180518101031261025a5760208101516040820151926060830151936110e16080850151956110628460c060a089015198015198610beb565b92604051966020880152602487015260448601526064850152608484015260a483015260c482015260c4815261089760e482610720565b9060a084809495518101031261025a5760208301519161114e6040850151918501516110628460a0608089015198015197610beb565b92604051956020870152602486015260448501526064840152608483015260a482015260a4815261089760c482610720565b91908260a091031261025a5760405161119881610704565b6080808294803584526111ad60208201610628565b602085015260408101356040850152606081013560608501520135910152565b67ffffffffffffffff81116106d75760051b60200190565b9080601f8301121561025a5781356111fc816111cd565b9261120a6040519485610720565b81845260208085019260051b82010192831161025a57602001905b8282106112325750505090565b8135815260209182019101611225565b9080601f8301121561025a578135611259816111cd565b926112676040519485610720565b81845260208085019260051b82010192831161025a57602001905b82821061128f5750505090565b6020809161129c84610628565b815201910190611282565b9080601f8301121561025a578160206108979335910161079b565b81601f8201121561025a578035906112d9826111cd565b926112e76040519485610720565b82845260208085019360051b8301019181831161025a5760208101935b83851061131357505050505090565b843567ffffffffffffffff811161025a57820183603f8201121561025a57602081013561133f816111cd565b9161134d6040519384610720565b8183526020808085019360051b830101019086821161025a5760408101925b828410611386575050509082525060209485019401611304565b833567ffffffffffffffff811161025a5760209083010160a07fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0828b03011261025a57604051906113d682610704565b602081013567ffffffffffffffff811161025a578a60206113f992840101611242565b8252604081013567ffffffffffffffff811161025a578a602061141e92840101611242565b6020830152606081013567ffffffffffffffff811161025a578a6020611446928401016111e5565b6040830152608081013567ffffffffffffffff811161025a576020908201018a601f8201121561025a57803561147b816111cd565b916114896040519384610720565b81835260208084019260051b820101918d831161025a5760208201905b8382106114cd5750505050606083015260a00135608082015281526020938401930161136c565b813567ffffffffffffffff811161025a578f916114f16020938480948801016112a7565b8152019101906114a6565b9080601f8301121561025a57813591611514836111cd565b926115226040519485610720565b80845260208085019160051b8301019183831161025a5760208101915b83831061154e57505050505090565b823567ffffffffffffffff811161025a578201906101407fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0838803011261025a5760405190610140820182811067ffffffffffffffff8211176106d757604052602083013582526115c160408401610628565b60208301526115d260608401610628565b60408301526115e360808401610628565b606083015260a0830135608083015260c083013560a083015260e083013560c083015261010083013560e0830152610120830135801515810361025a576101008301526101408301359167ffffffffffffffff831161025a5761164e886020809695819601016112a7565b61012082015281520192019161153f565b90602080835192838152019201905f5b81811061167c5750505090565b825184526020938401939092019160010161166f565b90602080835192838152019201905f5b8181106116af5750505090565b825173ffffffffffffffffffffffffffffffffffffffff168452602093840193909201916001016116a2565b9080602083519182815201906020808260051b8501019401915f905b82821061170657505050505090565b9091929395947fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0878203018252845190815180825260208201906020808260051b8501019401925f5b828110611773575050505050602080600192960192019201909291959394956116f7565b90919293947fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe083829d9c9b9d030185528551906117e36117d16117bf845160a0855260a0850190611692565b60208501518482036020860152611692565b6040840151838203604085015261165f565b91606081015192828103606084015283519081815260208101906020808460051b8301019601925f915b81831061183b5750505050608091820151919092015250999a9899946020908101940192919060010161174f565b9091929396602080611877837fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe086600196030189528b51610c83565b990195019301919061180d565b9080602083519182815201916020808360051b8301019401925f915b8383106118af57505050505090565b9091929394602080611995837fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe086600196030187526101406101208b518051845273ffffffffffffffffffffffffffffffffffffffff86820151168685015273ffffffffffffffffffffffffffffffffffffffff604082015116604085015273ffffffffffffffffffffffffffffffffffffffff60608201511660608501526080810151608085015260a081015160a085015260c081015160c085015260e081015160e08501526101008101511515610100850152015191816101208201520190610c83565b970193019301919392906118a0565b906060928160041161025a577fffffffff000000000000000000000000000000000000000000000000000000008335169160048401947f0d5f0e3b0000000000000000000000000000000000000000000000000000000084145f14611acd575083019260807ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc828603011261025a5760648101359167ffffffffffffffff831161025a57611a74611a60610897966004611aa1968601016111e5565b926110628360446024840135930135610beb565b906040519687956020870152356024860152604485015260648401526080608484015260a483019061165f565b037fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe08101835282610720565b9193917f03b87e5f000000000000000000000000000000000000000000000000000000008403611cdb57508101926101407ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc838603011261025a57611b3460248301610628565b611b418560448501611180565b9460e484013567ffffffffffffffff811161025a57816004611b65928701016111e5565b9061010485013567ffffffffffffffff811161025a57816004611b8a928801016112c2565b916101248601359167ffffffffffffffff831161025a576108979873ffffffffffffffffffffffffffffffffffffffff611caa96611bd6611c65946004611c7998611aa19d01016114fc565b98611be86040850196828851916121ca565b9560608501611c03611bfb848351610beb565b835190610c2b565b9052526040519c8d9b60208d01523560248c01521660448a01526064890190608080918051845273ffffffffffffffffffffffffffffffffffffffff602082015116602085015260408101516040850152606081015160608501520151910152565b61014061010488015261016487019061165f565b907fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffdc868303016101248701526116db565b907fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffdc84830301610144850152611884565b7f08298b5a000000000000000000000000000000000000000000000000000000008403611dc8575081019060a07ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc828403011261025a57611d3e60648201610628565b60848201359067ffffffffffffffff821161025a5761089795611d95611d81611aa196600473ffffffffffffffffffffffffffffffffffffffff968801016111e5565b946110628360446024840135930135610beb565b9060405198899760208901523560248801526044870152606486015216608484015260a060a484015260c483019061165f565b9193917f9871efa4000000000000000000000000000000000000000000000000000000008403611e4f575083019260807ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc828603011261025a5760648101359167ffffffffffffffff831161025a57611a74611a60610897966004611aa1968601016111e5565b93907fb80c2f09000000000000000000000000000000000000000000000000000000008403612028578101906101207ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc828403011261025a57611eb58260248301611180565b9460c482013567ffffffffffffffff811161025a57836004611ed9928501016111e5565b9260e483013567ffffffffffffffff811161025a57816004611efd928601016112c2565b916101048401359367ffffffffffffffff851161025a5761089798611aa197611f34611fc6956004611ff799611fb39601016114fc565b97611f466040840191838351916121ca565b948301611f57611bfb848351610beb565b9052526040519a8b9960208b01523560248a01526044890190608080918051845273ffffffffffffffffffffffffffffffffffffffff602082015116602085015260408101516040850152606081015160608501520151910152565b61012060e488015261014487019061165f565b907fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffdc868303016101048701526116db565b907fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffdc84830301610124850152611884565b60846040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602660248201527f50656e646c65537761703a204f4b582073656c6563746f72206e6f742073757060448201527f706f7274656400000000000000000000000000000000000000000000000000006064820152fd5b3d156120d6573d906120bd82610761565b916120cb6040519384610720565b82523d5f602084013e565b606090565b90929192834710612105575f81610897956020839451920190855af16120ff6120ac565b9161225b565b60846040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602660248201527f416464726573733a20696e73756666696369656e742062616c616e636520666f60448201527f722063616c6c00000000000000000000000000000000000000000000000000006064820152fd5b805182101561219d5760209160051b010190565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52603260045260245ffd5b909291928151917fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe06122146121fe856111cd565b9461220c6040519687610720565b8086526111cd565b01366020850137825f5b82518110156122535780612242886110628761223c60019689612189565b51610beb565b61224c8288612189565b520161221e565b509450505050565b919290156122d6575081511561226f575090565b3b156122785790565b60646040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e74726163740000006044820152fd5b8251909150156122e95750805190602001fd5b612327906040519182917f08c379a0000000000000000000000000000000000000000000000000000000008352602060048401526024830190610c83565b0390fd5b9073ffffffffffffffffffffffffffffffffffffffff61239392165f8060405193612357604086610720565b602085527f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c6564602086015260208151910182855af16120ff6120ac565b805190811591821561242d575b5050156123a957565b60846040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602a60248201527f5361666545524332303a204552433230206f7065726174696f6e20646964206e60448201527f6f742073756363656564000000000000000000000000000000000000000000006064820152fd5b819250906020918101031261025a5760206124489101610827565b5f806123a056fea2646970667358221220c4157727239d43ff9a1221825a4b739b23022831330d47aee368f3474133328364736f6c634300081c0033

Verified Source Code Full Match

Compiler: v0.8.28+commit.7893614a EVM: shanghai Optimization: Yes (1000000 runs)
IERC20Metadata.sol 28 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/IERC20Metadata.sol)

pragma solidity ^0.8.0;

import "../IERC20.sol";

/**
 * @dev Interface for the optional metadata functions from the ERC20 standard.
 *
 * _Available since v4.1._
 */
interface IERC20Metadata is IERC20 {
    /**
     * @dev Returns the name of the token.
     */
    function name() external view returns (string memory);

    /**
     * @dev Returns the symbol of the token.
     */
    function symbol() external view returns (string memory);

    /**
     * @dev Returns the decimals places of the token.
     */
    function decimals() external view returns (uint8);
}
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);
}
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));
    }
}
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);
        }
    }
}
TokenHelper.sol 71 lines
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "../../interfaces/IWETH.sol";

abstract contract TokenHelper {
    using SafeERC20 for IERC20;

    address internal constant NATIVE = address(0);
    uint256 internal constant LOWER_BOUND_APPROVAL = type(uint96).max / 2; // some tokens use 96 bits for approval

    function _transferIn(address token, address from, uint256 amount) internal {
        if (token == NATIVE) require(msg.value == amount, "eth mismatch");
        else if (amount != 0) IERC20(token).safeTransferFrom(from, address(this), amount);
    }

    function _transferFrom(IERC20 token, address from, address to, uint256 amount) internal {
        if (amount != 0) token.safeTransferFrom(from, to, amount);
    }

    function _transferOut(address token, address to, uint256 amount) internal {
        if (amount == 0) return;
        if (token == NATIVE) {
            (bool success, ) = to.call{value: amount}("");
            require(success, "eth send failed");
        } else {
            IERC20(token).safeTransfer(to, amount);
        }
    }

    function _transferOut(address[] memory tokens, address to, uint256[] memory amounts) internal {
        uint256 numTokens = tokens.length;
        require(numTokens == amounts.length, "length mismatch");
        for (uint256 i = 0; i < numTokens; ) {
            _transferOut(tokens[i], to, amounts[i]);
            unchecked {
                i++;
            }
        }
    }

    function _selfBalance(address token) internal view returns (uint256) {
        return (token == NATIVE) ? address(this).balance : IERC20(token).balanceOf(address(this));
    }

    function _selfBalance(IERC20 token) internal view returns (uint256) {
        return token.balanceOf(address(this));
    }

    /// @notice Approves the stipulated contract to spend the given allowance in the given token
    /// @dev PLS PAY ATTENTION to tokens that requires the approval to be set to 0 before changing it
    function _safeApprove(address token, address to, uint256 value) internal {
        (bool success, bytes memory data) = token.call(abi.encodeWithSelector(IERC20.approve.selector, to, value));
        require(success && (data.length == 0 || abi.decode(data, (bool))), "Safe Approve");
    }

    function _safeApproveInf(address token, address to) internal {
        if (token == NATIVE) return;
        if (IERC20(token).allowance(address(this), to) < LOWER_BOUND_APPROVAL) {
            _safeApprove(token, to, 0);
            _safeApprove(token, to, type(uint256).max);
        }
    }

    function _wrap_unwrap_ETH(address tokenIn, address tokenOut, uint256 netTokenIn) internal {
        if (tokenIn == NATIVE) IWETH(tokenOut).deposit{value: netTokenIn}();
        else IWETH(tokenIn).withdraw(netTokenIn);
    }
}
IWETH.sol 34 lines
// SPDX-License-Identifier: GPL-3.0-or-later
/*
 * MIT License
 * ===========
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 */
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

interface IWETH is IERC20 {
    event Deposit(address indexed dst, uint256 wad);
    event Withdrawal(address indexed src, uint256 wad);

    function deposit() external payable;

    function withdraw(uint256 wad) external;
}
IPSwapAggregator.sol 37 lines
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.0;

struct SwapData {
    SwapType swapType;
    address extRouter;
    bytes extCalldata;
    bool needScale;
}

struct SwapDataExtra {
    address tokenIn;
    address tokenOut;
    uint256 minOut;
    SwapData swapData;
}

enum SwapType {
    NONE,
    KYBERSWAP,
    ODOS,
    // ETH_WETH not used in Aggregator
    ETH_WETH,
    OKX,
    ONE_INCH,
    RESERVE_1,
    RESERVE_2,
    RESERVE_3,
    RESERVE_4,
    RESERVE_5
}

interface IPSwapAggregator {
    event SwapSingle(SwapType indexed swapType, address indexed tokenIn, uint256 amountIn);

    function swap(address tokenIn, uint256 amountIn, SwapData calldata swapData) external payable;
}
OKXScaleHelper.sol 215 lines
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.17;

import "../../core/libraries/TokenHelper.sol";
import "./IPSwapAggregator.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

abstract contract OKXScaleHelper {
    function _okx_getTokenApprove() internal view returns (address) {
        if (block.chainid == 1) {
            return 0x40aA958dd87FC8305b97f2BA922CDdCa374bcD7f;
        }
        if (block.chainid == 10) {
            return 0x68D6B739D2020067D1e2F713b999dA97E4d54812;
        }
        if (block.chainid == 56) {
            return 0x2c34A2Fb1d0b4f55de51E1d0bDEfaDDce6b7cDD6;
        }
        if (block.chainid == 42161) {
            return 0x70cBb871E8f30Fc8Ce23609E9E0Ea87B6b222F58;
        }
        if (block.chainid == 8453) {
            return 0x57df6092665eb6058DE53939612413ff4B09114E;
        }
        if (block.chainid == 5000) {
            return 0x57df6092665eb6058DE53939612413ff4B09114E;
        }

        revert("PendleSwap: OKX Chain not supported");
    }

    function _okxScaling(
        bytes calldata rawCallData,
        uint256 actualAmount
    ) internal pure returns (bytes memory scaledCallData) {
        bytes4 selector = bytes4(rawCallData[:4]);
        bytes calldata dataToDecode = rawCallData[4:];

        if (selector == IOKXDexRouter.uniswapV3SwapTo.selector) {
            (uint256 receiver, uint256 amount, uint256 minReturn, uint256[] memory pools) = abi.decode(
                dataToDecode,
                (uint256, uint256, uint256, uint256[])
            );

            minReturn = (minReturn * actualAmount) / amount;
            amount = actualAmount;

            return abi.encodeWithSelector(selector, receiver, amount, minReturn, pools);
        } else if (selector == IOKXDexRouter.smartSwapTo.selector) {
            (
                uint256 orderId,
                address receiver,
                IOKXDexRouter.BaseRequest memory baseRequest,
                uint256[] memory batchesAmount,
                IOKXDexRouter.RouterPath[][] memory batches,
                IOKXDexRouter.PMMSwapRequest[] memory extraData
            ) = abi.decode(
                    dataToDecode,
                    (
                        uint256,
                        address,
                        IOKXDexRouter.BaseRequest,
                        uint256[],
                        IOKXDexRouter.RouterPath[][],
                        IOKXDexRouter.PMMSwapRequest[]
                    )
                );

            batchesAmount = _scaleArray(batchesAmount, actualAmount, baseRequest.fromTokenAmount);
            baseRequest.minReturnAmount = (baseRequest.minReturnAmount * actualAmount) / baseRequest.fromTokenAmount;
            baseRequest.fromTokenAmount = actualAmount;

            return abi.encodeWithSelector(selector, orderId, receiver, baseRequest, batchesAmount, batches, extraData);
        } else if (selector == IOKXDexRouter.unxswapTo.selector) {
            (uint256 srcToken, uint256 amount, uint256 minReturn, address receiver, bytes32[] memory pools) = abi
                .decode(dataToDecode, (uint256, uint256, uint256, address, bytes32[]));

            minReturn = (minReturn * actualAmount) / amount;
            amount = actualAmount;

            return abi.encodeWithSelector(selector, srcToken, amount, minReturn, receiver, pools);
        } else if (selector == IOKXDexRouter.unxswapByOrderId.selector) {
            (uint256 srcToken, uint256 amount, uint256 minReturn, bytes32[] memory pools) = abi.decode(
                dataToDecode,
                (uint256, uint256, uint256, bytes32[])
            );

            minReturn = (minReturn * actualAmount) / amount;
            amount = actualAmount;

            return abi.encodeWithSelector(selector, srcToken, amount, minReturn, pools);
        } else if (selector == IOKXDexRouter.smartSwapByOrderId.selector) {
            (
                uint256 orderId,
                IOKXDexRouter.BaseRequest memory baseRequest,
                uint256[] memory batchesAmount,
                IOKXDexRouter.RouterPath[][] memory batches,
                IOKXDexRouter.PMMSwapRequest[] memory extraData
            ) = abi.decode(
                    dataToDecode,
                    (
                        uint256,
                        IOKXDexRouter.BaseRequest,
                        uint256[],
                        IOKXDexRouter.RouterPath[][],
                        IOKXDexRouter.PMMSwapRequest[]
                    )
                );

            batchesAmount = _scaleArray(batchesAmount, actualAmount, baseRequest.fromTokenAmount);
            baseRequest.minReturnAmount = (baseRequest.minReturnAmount * actualAmount) / baseRequest.fromTokenAmount;
            baseRequest.fromTokenAmount = actualAmount;

            return abi.encodeWithSelector(selector, orderId, baseRequest, batchesAmount, batches, extraData);
        } else {
            revert("PendleSwap: OKX selector not supported");
        }
    }

    function _scaleArray(
        uint256[] memory arr,
        uint256 newAmount,
        uint256 oldAmount
    ) internal pure returns (uint256[] memory scaledArr) {
        scaledArr = new uint256[](arr.length);
        for (uint256 i = 0; i < arr.length; i++) {
            scaledArr[i] = (arr[i] * newAmount) / oldAmount;
        }
    }
}

interface IOKXDexRouter {
    struct BaseRequest {
        uint256 fromToken;
        address toToken;
        uint256 fromTokenAmount;
        uint256 minReturnAmount;
        uint256 deadLine;
    }

    struct RouterPath {
        address[] mixAdapters;
        address[] assetTo;
        uint256[] rawData;
        bytes[] extraData;
        uint256 fromToken;
    }

    struct PMMSwapRequest {
        uint256 pathIndex;
        address payer;
        address fromToken;
        address toToken;
        uint256 fromTokenAmountMax;
        uint256 toTokenAmountMax;
        uint256 salt;
        uint256 deadLine;
        bool isPushOrder;
        bytes extension;
    }

    // // address marketMaker;
    // // uint256 subIndex;
    // // bytes signature;
    // // uint256 source;  1byte type + 1byte bool(reverse) + 0...0 + 20 bytes address

    // function smartSwapByInvest(
    //     BaseRequest calldata baseRequest,
    //     uint256[] calldata batchesAmount,
    //     RouterPath[][] calldata batches,
    //     PMMSwapRequest[] calldata extraData,
    //     address to
    // ) external payable;

    function uniswapV3SwapTo(
        uint256 receiver,
        uint256 amount,
        uint256 minReturn,
        uint256[] calldata pools
    ) external payable returns (uint256 returnAmount);

    function smartSwapTo(
        uint256 orderId,
        address receiver,
        BaseRequest calldata baseRequest,
        uint256[] calldata batchesAmount,
        RouterPath[][] calldata batches,
        PMMSwapRequest[] calldata extraData
    ) external payable;

    function unxswapTo(
        uint256 srcToken,
        uint256 amount,
        uint256 minReturn,
        address receiver,
        // solhint-disable-next-line no-unused-vars
        bytes32[] calldata pools
    ) external payable returns (uint256 returnAmount);

    function unxswapByOrderId(
        uint256 srcToken,
        uint256 amount,
        uint256 minReturn,
        // solhint-disable-next-line no-unused-vars
        bytes32[] calldata pools
    ) external payable returns (uint256 returnAmount);

    function smartSwapByOrderId(
        uint256 orderId,
        BaseRequest calldata baseRequest,
        uint256[] calldata batchesAmount,
        RouterPath[][] calldata batches,
        PMMSwapRequest[] calldata extraData
    ) external payable returns (uint256 returnAmount);
}
OneInchScaleHelper.sol 120 lines
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.17;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

interface I1inchCommonType {
    type Address256 is uint256;
}

abstract contract OneInchScaleHelper is I1inchCommonType {
    function _rescaleMinAmount(
        uint256 minAmount,
        uint256 oldAmount,
        uint256 newAmount
    ) internal pure returns (uint256) {
        return (minAmount * newAmount) / oldAmount;
    }

    function _oneInchScaling(bytes calldata rawCallData, uint256 actualAmount) internal pure returns (bytes memory) {
        bytes4 selector = bytes4(rawCallData[:4]);
        bytes memory args = rawCallData[4:];

        if (selector == I1inchAggregationRouterV6.unoswapTo.selector) {
            (Address256 to, Address256 token, uint256 amount, uint256 minReturn, Address256 dex) = abi.decode(
                args,
                (Address256, Address256, uint256, uint256, Address256)
            );

            minReturn = (minReturn * actualAmount) / amount;
            amount = actualAmount;
            return abi.encodeWithSelector(selector, to, token, amount, minReturn, dex);
        }

        if (selector == I1inchAggregationRouterV6.unoswapTo2.selector) {
            (Address256 to, Address256 token, uint256 amount, uint256 minReturn, Address256 dex, Address256 dex2) = abi
                .decode(args, (Address256, Address256, uint256, uint256, Address256, Address256));

            minReturn = (minReturn * actualAmount) / amount;
            amount = actualAmount;

            return abi.encodeWithSelector(selector, to, token, amount, minReturn, dex, dex2);
        }

        if (selector == I1inchAggregationRouterV6.unoswapTo3.selector) {
            (
                Address256 to,
                Address256 token,
                uint256 amount,
                uint256 minReturn,
                Address256 dex,
                Address256 dex2,
                Address256 dex3
            ) = abi.decode(args, (Address256, Address256, uint256, uint256, Address256, Address256, Address256));

            minReturn = (minReturn * actualAmount) / amount;
            amount = actualAmount;

            return abi.encodeWithSelector(selector, to, token, amount, minReturn, dex, dex2, dex3);
        }

        if (selector == I1inchAggregationRouterV6.swap.selector) {
            (address executor, I1inchAggregationRouterV6.SwapDescription memory desc, bytes memory data) = abi.decode(
                args,
                (address, I1inchAggregationRouterV6.SwapDescription, bytes)
            );

            desc.minReturnAmount = (desc.minReturnAmount * actualAmount) / desc.amount;
            desc.amount = actualAmount;
            return abi.encodeWithSelector(selector, executor, desc, data);
        }

        revert("1inch: unsupported selector");
    }
}

interface I1inchAggregationRouterV6 is I1inchCommonType {
    struct SwapDescription {
        IERC20 srcToken;
        IERC20 dstToken;
        address payable srcReceiver;
        address payable dstReceiver;
        uint256 amount;
        uint256 minReturnAmount;
        uint256 flags;
    }

    // we ignore the non-to version since it's guarantee this contract won't receive tokens

    function unoswapTo(
        Address256 to,
        Address256 token,
        uint256 amount,
        uint256 minReturn,
        Address256 dex
    ) external returns (uint256 returnAmount);

    function unoswapTo2(
        Address256 to,
        Address256 token,
        uint256 amount,
        uint256 minReturn,
        Address256 dex,
        Address256 dex2
    ) external returns (uint256 returnAmount);

    function unoswapTo3(
        Address256 to,
        Address256 token,
        uint256 amount,
        uint256 minReturn,
        Address256 dex,
        Address256 dex2,
        Address256 dex3
    ) external returns (uint256 returnAmount);

    function swap(
        address executor,
        SwapDescription calldata desc,
        bytes calldata data
    ) external payable returns (uint256 returnAmount, uint256 spentAmount);
}
PendleSwap.sol 115 lines
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.17;

import "../../core/libraries/TokenHelper.sol";
import "./IPSwapAggregator.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "./OKXScaleHelper.sol";
import "./OneInchScaleHelper.sol";

contract PendleSwap is IPSwapAggregator, TokenHelper, OKXScaleHelper, OneInchScaleHelper {
    using Address for address;
    using SafeERC20 for IERC20;

    address private constant KYBER_SCALING_HELPER = 0x2f577A41BeC1BE1152AeEA12e73b7391d15f655D;

    function swap(address tokenIn, uint256 amountIn, SwapData calldata data) external payable {
        _approveForExtRouter(tokenIn, data);
        data.extRouter.functionCallWithValue(
            data.needScale ? _getScaledInputData(data.swapType, data.extCalldata, amountIn) : data.extCalldata,
            tokenIn == NATIVE ? amountIn : 0
        );

        emit SwapSingle(data.swapType, tokenIn, amountIn);
    }

    function _approveForExtRouter(address token, SwapData calldata data) internal {
        if (token == NATIVE) return;

        if (data.swapType == SwapType.OKX) {
            _safeApproveInfV2(IERC20(token), _okx_getTokenApprove());
        } else {
            _safeApproveInfV2(IERC20(token), data.extRouter);
        }
    }

    function _safeApproveInfV2(IERC20 token, address spender) internal {
        if (token.allowance(address(this), spender) < type(uint256).max) {
            token.forceApprove(spender, type(uint256).max);
        }
    }

    function _getScaledInputData(
        SwapType swapType,
        bytes calldata rawCallData,
        uint256 amountIn
    ) internal view returns (bytes memory scaledCallData) {
        if (swapType == SwapType.KYBERSWAP) {
            bool isSuccess;
            (isSuccess, scaledCallData) = IKyberScalingHelper(KYBER_SCALING_HELPER).getScaledInputData(
                rawCallData,
                amountIn
            );

            require(isSuccess, "PendleSwap: Kyber scaling failed");
        } else if (swapType == SwapType.ODOS) {
            scaledCallData = _odosScaling(rawCallData, amountIn);
        } else if (swapType == SwapType.OKX) {
            scaledCallData = _okxScaling(rawCallData, amountIn);
        } else if (swapType == SwapType.ONE_INCH) {
            scaledCallData = _oneInchScaling(rawCallData, amountIn);
        } else {
            assert(false);
        }
    }

    function _odosScaling(
        bytes calldata rawCallData,
        uint256 amountIn
    ) internal pure returns (bytes memory scaledCallData) {
        bytes4 selector = bytes4(rawCallData[:4]);
        bytes calldata dataToDecode = rawCallData[4:];

        assert(selector == IOdosRouterV2.swap.selector);
        (
            IOdosRouterV2.swapTokenInfo memory tokenInfo,
            bytes memory pathDefinition,
            address executor,
            uint32 referralCode
        ) = abi.decode(dataToDecode, (IOdosRouterV2.swapTokenInfo, bytes, address, uint32));

        tokenInfo.outputQuote = (tokenInfo.outputQuote * amountIn) / tokenInfo.inputAmount;
        tokenInfo.outputMin = (tokenInfo.outputMin * amountIn) / tokenInfo.inputAmount;
        tokenInfo.inputAmount = amountIn;

        return abi.encodeWithSelector(selector, tokenInfo, pathDefinition, executor, referralCode);
    }

    receive() external payable {}
}

interface IKyberScalingHelper {
    function getScaledInputData(
        bytes calldata inputData,
        uint256 newAmount
    ) external view returns (bool isSuccess, bytes memory data);
}

interface IOdosRouterV2 {
    struct swapTokenInfo {
        address inputToken;
        uint256 inputAmount;
        address inputReceiver;
        address outputToken;
        uint256 outputQuote;
        uint256 outputMin;
        address outputReceiver;
    }

    function swap(
        swapTokenInfo memory tokenInfo,
        bytes calldata pathDefinition,
        address executor,
        uint32 referralCode
    ) external payable returns (uint256 amountOut);
}

Write Contract 1 functions

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

swap 0xfd472065
address tokenIn
uint256 amountIn
tuple data

Token Balances (1)

View Transfers →
TokenBalancePriceValue
USDC 0.0001 $1.0000

Recent Transactions

This address has 1 on-chain transactions, but only 1.2% of the chain is indexed. Transactions will appear as indexing progresses. View on Etherscan →