Cryo Explorer Ethereum Mainnet

Address Contract Verified

Address 0x77a974e9f36F0B0C1B566f8f5798032C39fA8376
Balance 0 ETH
Nonce 1
Code Size 9252 bytes
Indexed Transactions 0
External Etherscan · Sourcify

Contract Bytecode

9252 bytes
0x6080604052600436106100a05760003560e01c8063715018a611610064578063715018a61461017757806378e3214f1461018c5780638da5cb5b146101ac578063c683630d146101ca578063e74b981b1461020a578063f2fde38b1461022a57600080fd5b806324a9d853146100ac57806333320de3146100d557806346904840146100f757806359e50fed1461012f57806367e5b01c1461015757600080fd5b366100a757005b600080fd5b3480156100b857600080fd5b506100c260015481565b6040519081526020015b60405180910390f35b3480156100e157600080fd5b506100f56100f0366004611e74565b61024a565b005b34801561010357600080fd5b50600254610117906001600160a01b031681565b6040516001600160a01b0390911681526020016100cc565b61014261013d366004611f38565b6102e3565b604080519283526020830191909152016100cc565b34801561016357600080fd5b506100f5610172366004611f7a565b6108bf565b34801561018357600080fd5b506100f56108cc565b34801561019857600080fd5b506100f56101a7366004611f93565b6108e0565b3480156101b857600080fd5b506000546001600160a01b0316610117565b3480156101d657600080fd5b506101fa6101e5366004611fbf565b60036020526000908152604090205460ff1681565b60405190151581526020016100cc565b34801561021657600080fd5b506100f5610225366004611fbf565b61090f565b34801561023657600080fd5b506100f5610245366004611fbf565b610939565b6102526109b2565b805182511461026057600080fd5b60005b82518110156102de5781818151811061027e5761027e611fdc565b60200260200101516003600085848151811061029c5761029c611fdc565b6020908102919091018101516001600160a01b03168252810191909152604001600020805460ff19169115159190911790556102d781612008565b9050610263565b505050565b60008060005a9050600360006102fc6020870187611fbf565b6001600160a01b0316815260208101919091526040016000205460ff166103645760405162461bcd60e51b81526020600482015260176024820152761059191c995cdcc81b9bdd081dda1a5d195b1a5cdd1959604a1b60448201526064015b60405180910390fd5b6103716020850185611fbf565b6001600160a01b031661038a6040860160208701611fbf565b6001600160a01b0316141580156103ba575060006103ae6040860160208701611fbf565b6001600160a01b031614155b1561043657600360006103d36040870160208801611fbf565b6001600160a01b0316815260208101919091526040016000205460ff166104365760405162461bcd60e51b81526020600482015260176024820152761059191c995cdcc81b9bdd081dda1a5d195b1a5cdd1959604a1b604482015260640161035b565b60006104456060860186612021565b61044e9061210d565b90506000816101000151116104a55760405162461bcd60e51b815260206004820152601960248201527f496e76616c6964206d696e2072657475726e20616d6f756e7400000000000000604482015260640161035b565b6104ae81610a0c565b6104c681600001518260e00151836101400151610ada565b60006104d88261012001516080610c9a565b905060008060006001600160a01b03168460c001516001600160a01b031614610505578360c00151610507565b335b90506105198461012001516040610c9a565b61062d5761053b84600001513386608001518760a001518860e0015188610ca6565b60e08501819052845161054f913390610e72565b60e0850152835160009061056290610f2d565b15801561057a575061057a8561012001516004610c9a565b156105925761058a853330610f4f565b60e087015290505b6105ad336105a660408c0160208d01611fbf565b8784610fef565b6106226105bd60208b018b611fbf565b6105ca60408c018c612242565b8080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152505089518a925061060b9150610f2d565b61061657600061061c565b8860e001515b86611190565b985092506107709050565b600061063c8560000151610f2d565b15801561065457506106548561012001516004610c9a565b1561066c57610664853330610f4f565b60e087015290505b600061067c866020015184611357565b90506106993361069260408d0160208e01611fbf565b8885610fef565b61070a6106a960208c018c611fbf565b6106b660408d018d612242565b8080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250508a518b92506106f79150610f2d565b610702576000610704565b345b30611190565b809a508195505050600061072e87602001513089608001518a60a001518e8b610ca6565b905061073f87602001513083610e72565b905061075187602001513086846113e9565b5080610761876020015185611357565b61076b9190612290565b985050505b61077b828886611495565b835161078690610f2d565b1580156107ac575060006107a060408a0160208b01611fbf565b6001600160a01b031614155b156107d7576107d76107c460408a0160208b01611fbf565b85516001600160a01b031690600061156d565b7f97907616a601015471c14cbf2d2636e639bd38defe58a67bd9681b990fe4563d61080560208a018a611fbf565b8551602080880151604080516001600160a01b03958616815233938101939093529284168284015283166060820152918416608083015260a0820185905260c082018a9052519081900360e00190a1600061086360808a018a612242565b905011156108b0577f095e66fa4dd6a6f7b43fb8444a7bd0edb870508c7abf639bc216efb0bcff977961089960808a018a612242565b6040516108a79291906122a3565b60405180910390a15b5a850395505050505050915091565b6108c76109b2565b600155565b6108d46109b2565b6108de60006116b5565b565b6108e86109b2565b6108f182610f2d565b15610904576109003382611705565b5050565b6109008233836117da565b6109176109b2565b600280546001600160a01b0319166001600160a01b0392909216919091179055565b6109416109b2565b6001600160a01b0381166109a65760405162461bcd60e51b815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201526564647265737360d01b606482015260840161035b565b6109af816116b5565b50565b6000546001600160a01b031633146108de5760405162461bcd60e51b815260206004820181905260248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e6572604482015260640161035b565b6000610a1b8260000151610f2d565b9050610a2d8261012001516002610c9a565b15610a865780610a3e576000610a44565b8160e001515b34116109005760405162461bcd60e51b8152602060048201526011602482015270496e76616c6964206d73672e76616c756560781b604482015260640161035b565b80610a92576000610a98565b8160e001515b34146109005760405162461bcd60e51b8152602060048201526011602482015270496e76616c6964206d73672e76616c756560781b604482015260640161035b565b805160e0036102de576000836001600160a01b031663d505accf60e01b83604051602001610b099291906122f6565b60408051601f1981840301815290829052610b2391612327565b6000604051808303816000865af19150503d8060008114610b60576040519150601f19603f3d011682016040523d82523d6000602084013e610b65565b606091505b5050905080610c9457604051636eb1769f60e11b815233600482015230602482015283906001600160a01b0386169063dd62ed3e90604401602060405180830381865afa158015610bba573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610bde9190612339565b1015610c2c5760405162461bcd60e51b815260206004820152601e60248201527f5065726d69742063616c6c206661696c65643a3a20616c6c6f77616e63650000604482015260640161035b565b7fbd347af04a90c556fe3142f82605fcfdd80924262f5e6d62efaa1bd0c4a577c3604051610c8b9060208082526019908201527f5065726d69742063616c6c206661696c65643a3a20656c736500000000000000604082015260600190565b60405180910390a15b50505050565b81811615155b92915050565b835182908015610e67576000610cbb89610f2d565b90506000610cd48a83610cce578a611357565b30611357565b905082875114610d175760405162461bcd60e51b815260206004820152600e60248201526d092dcecc2d8d2c840d8cadccee8d60931b604482015260640161035b565b60005b83811015610e3957600086610d4857888281518110610d3b57610d3b611fdc565b6020026020010151610d7a565b612710898381518110610d5d57610d5d611fdc565b602002602001015189610d709190612352565b610d7a9190612369565b9050610dad8c85610d8b578c610d8d565b305b8c8581518110610d9f57610d9f611fdc565b6020026020010151846113e9565b7fecf85deac0bc62ca12693bc31f36a5973308b6c1d3b3346530424fe78c5d48a46000828e8d8681518110610de457610de4611fdc565b6020026020010151604051610e209493929190931515845260208401929092526001600160a01b03908116604084015216606082015260800190565b60405180910390a150610e3281612008565b9050610d1a565b506000610e4b8b84610cce578b611357565b610e559083612290565b9050610e618188612290565b94505050505b509695505050505050565b806000610e7e85610f2d565b9050600061271060015485610e939190612352565b610e9d9190612369565b9050610ec28683610eae5786610eb0565b305b6002546001600160a01b0316846113e9565b610ecc8185612290565b6002546040805160018152602081018590526001600160a01b038a8116828401529092166060830152519194507fecf85deac0bc62ca12693bc31f36a5973308b6c1d3b3346530424fe78c5d48a4919081900360800190a150509392505050565b6001600160a01b031673eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee1490565b600080610f5f8560000151610f2d565b15610fa15760405162461bcd60e51b8152602060048201526012602482015271086d8c2d2da40e8ded6cadc40d2e6408aa8960731b604482015260640161035b565b6000610fb1866000015130611357565b9050610fc7866000015186868960e001516113e9565b6001925080610fda876000015130611357565b610fe49190612290565b915050935093915050565b816060015151826040015151146110485760405162461bcd60e51b815260206004820152601b60248201527f696e76616c696420737263526563656976657273206c656e6774680000000000604482015260640161035b565b801561109b5761105f826101200151610100610c9a565b801561107357506001600160a01b03831615155b1561109b5760e08201518251611096916001600160a01b039091169085906118fb565b610c94565b6000805b83604001515181101561113d57836060015181815181106110c2576110c2611fdc565b6020026020010151826110d5919061238b565b915061112d8460000151846110ea57876110ec565b305b8660400151848151811061110257611102611fdc565b60200260200101518760600151858151811061112057611120611fdc565b60200260200101516113e9565b61113681612008565b905061109f565b508260e001518111156111895760405162461bcd60e51b8152602060048201526014602482015273115e18d9595919590819195cd8cb985b5bdd5b9d60621b604482015260640161035b565b5050505050565b60008060006111a3866020015185611357565b905060006111b5876000015130611357565b905060006111c7886020015130611357565b905060008a6001600160a01b0316888b6040516111e49190612327565b60006040518083038185875af1925050503d8060008114611221576040519150601f19603f3d011682016040523d82523d6000602084013e611226565b606091505b50509050806112655760405162461bcd60e51b815260206004820152600b60248201526a10d85b1b0819985a5b195960aa1b604482015260640161035b565b506001600160a01b03861630146112a5576000816112878a6020015130611357565b6112919190612290565b90506112a389602001513089846113e9565b505b826112b4896020015188611357565b6112be9190612290565b93508760e0015194506112d78861012001516001610c9a565b80156112ff575087516112e990610f2d565b806112ff57506112ff8861012001516004610c9a565b1561134a576000611314896000015130611357565b9050828114611348576113278184612290565b955061134889600001513033898d60e001516113439190612290565b6113e9565b505b5050509550959350505050565b600061136283610f2d565b1561137857506001600160a01b03811631610ca0565b6040516370a0823160e01b81526001600160a01b0383811660048301528416906370a0823190602401602060405180830381865afa1580156113be573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906113e29190612339565b9050610ca0565b816001600160a01b0316836001600160a01b0316036114405760405162461bcd60e51b81526020600482015260136024820152721cd95b99195c88084f481c9958da5c1a595b9d606a1b604482015260640161035b565b8015610c945761144f84610f2d565b1561146e57306001600160a01b03841603611096576110968282611705565b306001600160a01b03841603611489576110968483836117da565b610c94848484846119a8565b6114a58161012001516001610c9a565b1561151857828161010001516114bb9190612352565b60e08201516114ca9084612352565b10156102de5760405162461bcd60e51b815260206004820152601b60248201527f52657475726e20616d6f756e74206973206e6f7420656e6f7567680000000000604482015260640161035b565b8061010001518210156102de5760405162461bcd60e51b815260206004820152601b60248201527f52657475726e20616d6f756e74206973206e6f7420656e6f7567680000000000604482015260640161035b565b8015806115e75750604051636eb1769f60e11b81523060048201526001600160a01b03838116602483015284169063dd62ed3e90604401602060405180830381865afa1580156115c1573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906115e59190612339565b155b6116525760405162461bcd60e51b815260206004820152603660248201527f5361666545524332303a20617070726f76652066726f6d206e6f6e2d7a65726f60448201527520746f206e6f6e2d7a65726f20616c6c6f77616e636560501b606482015260840161035b565b6040516001600160a01b0383166024820152604481018290526102de90849063095ea7b360e01b906064015b60408051601f198184030181529190526020810180516001600160e01b03166001600160e01b031990931692909217909152611ade565b600080546001600160a01b038381166001600160a01b0319831681178455604051919092169283917f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e09190a35050565b80600003611711575050565b604080516000808252602082019092526001600160a01b03841690839060405161173b9190612327565b60006040518083038185875af1925050503d8060008114611778576040519150601f19603f3d011682016040523d82523d6000602084013e61177d565b606091505b50509050806102de5760405162461bcd60e51b815260206004820152602360248201527f5472616e7366657248656c7065723a204554485f5452414e534645525f46414960448201526213115160ea1b606482015260840161035b565b806000036117e757505050565b604080516001600160a01b038481166024830152604480830185905283518084039091018152606490920183526020820180516001600160e01b031663a9059cbb60e01b17905291516000928392908716916118439190612327565b6000604051808303816000865af19150503d8060008114611880576040519150601f19603f3d011682016040523d82523d6000602084013e611885565b606091505b50915091508180156118af5750805115806118af5750808060200190518101906118af919061239e565b6111895760405162461bcd60e51b815260206004820152601f60248201527f5472616e7366657248656c7065723a205452414e534645525f4641494c454400604482015260640161035b565b604051636eb1769f60e11b81523060048201526001600160a01b0383811660248301526000919085169063dd62ed3e90604401602060405180830381865afa15801561194b573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061196f9190612339565b9050610c948463095ea7b360e01b85611988868661238b565b6040516001600160a01b039092166024830152604482015260640161167e565b8015610c9457604080516001600160a01b0385811660248301528481166044830152606480830185905283518084039091018152608490920183526020820180516001600160e01b03166323b872dd60e01b1790529151600092839290881691611a129190612327565b6000604051808303816000865af19150503d8060008114611a4f576040519150601f19603f3d011682016040523d82523d6000602084013e611a54565b606091505b5091509150818015611a7e575080511580611a7e575080806020019051810190611a7e919061239e565b611ad65760405162461bcd60e51b8152602060048201526024808201527f5472616e7366657248656c7065723a205452414e534645525f46524f4d5f46416044820152631253115160e21b606482015260840161035b565b505050505050565b6000611b33826040518060400160405280602081526020017f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c6564815250856001600160a01b0316611bb39092919063ffffffff16565b9050805160001480611b54575080806020019051810190611b54919061239e565b6102de5760405162461bcd60e51b815260206004820152602a60248201527f5361666545524332303a204552433230206f7065726174696f6e20646964206e6044820152691bdd081cdd58d8d9595960b21b606482015260840161035b565b6060611bc28484600085611bca565b949350505050565b606082471015611c2b5760405162461bcd60e51b815260206004820152602660248201527f416464726573733a20696e73756666696369656e742062616c616e636520666f6044820152651c8818d85b1b60d21b606482015260840161035b565b600080866001600160a01b03168587604051611c479190612327565b60006040518083038185875af1925050503d8060008114611c84576040519150601f19603f3d011682016040523d82523d6000602084013e611c89565b606091505b5091509150611c9a87838387611ca5565b979650505050505050565b60608315611d14578251600003611d0d576001600160a01b0385163b611d0d5760405162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e7472616374000000604482015260640161035b565b5081611bc2565b611bc28383815115611d295781518083602001fd5b8060405162461bcd60e51b815260040161035b91906123bb565b634e487b7160e01b600052604160045260246000fd5b604051610160810167ffffffffffffffff81118282101715611d7d57611d7d611d43565b60405290565b604051601f8201601f1916810167ffffffffffffffff81118282101715611dac57611dac611d43565b604052919050565b600067ffffffffffffffff821115611dce57611dce611d43565b5060051b60200190565b6001600160a01b03811681146109af57600080fd5b8035611df881611dd8565b919050565b600082601f830112611e0e57600080fd5b81356020611e23611e1e83611db4565b611d83565b82815260059290921b84018101918181019086841115611e4257600080fd5b8286015b84811015610e67578035611e5981611dd8565b8352918301918301611e46565b80151581146109af57600080fd5b60008060408385031215611e8757600080fd5b823567ffffffffffffffff80821115611e9f57600080fd5b611eab86838701611dfd565b9350602091508185013581811115611ec257600080fd5b85019050601f81018613611ed557600080fd5b8035611ee3611e1e82611db4565b81815260059190911b82018301908381019088831115611f0257600080fd5b928401925b82841015611f29578335611f1a81611e66565b82529284019290840190611f07565b80955050505050509250929050565b600060208284031215611f4a57600080fd5b813567ffffffffffffffff811115611f6157600080fd5b820160a08185031215611f7357600080fd5b9392505050565b600060208284031215611f8c57600080fd5b5035919050565b60008060408385031215611fa657600080fd5b8235611fb181611dd8565b946020939093013593505050565b600060208284031215611fd157600080fd5b8135611f7381611dd8565b634e487b7160e01b600052603260045260246000fd5b634e487b7160e01b600052601160045260246000fd5b60006001820161201a5761201a611ff2565b5060010190565b6000823561015e1983360301811261203857600080fd5b9190910192915050565b600082601f83011261205357600080fd5b81356020612063611e1e83611db4565b82815260059290921b8401810191818101908684111561208257600080fd5b8286015b84811015610e675780358352918301918301612086565b600082601f8301126120ae57600080fd5b813567ffffffffffffffff8111156120c8576120c8611d43565b6120db601f8201601f1916602001611d83565b8181528460208386010111156120f057600080fd5b816020850160208301376000918101602001919091529392505050565b6000610160823603121561212057600080fd5b612128611d59565b61213183611ded565b815261213f60208401611ded565b6020820152604083013567ffffffffffffffff8082111561215f57600080fd5b61216b36838701611dfd565b6040840152606085013591508082111561218457600080fd5b61219036838701612042565b606084015260808501359150808211156121a957600080fd5b6121b536838701611dfd565b608084015260a08501359150808211156121ce57600080fd5b6121da36838701612042565b60a08401526121eb60c08601611ded565b60c084015260e08581013590840152610100808601359084015261012080860135908401526101409150818501358181111561222657600080fd5b6122323682880161209d565b8385015250505080915050919050565b6000808335601e1984360301811261225957600080fd5b83018035915067ffffffffffffffff82111561227457600080fd5b60200191503681900382131561228957600080fd5b9250929050565b81810381811115610ca057610ca0611ff2565b60208152816020820152818360408301376000818301604090810191909152601f909201601f19160101919050565b60005b838110156122ed5781810151838201526020016122d5565b50506000910152565b6001600160e01b03198316815281516000906123198160048501602087016122d2565b919091016004019392505050565b600082516120388184602087016122d2565b60006020828403121561234b57600080fd5b5051919050565b8082028115828204841417610ca057610ca0611ff2565b60008261238657634e487b7160e01b600052601260045260246000fd5b500490565b80820180821115610ca057610ca0611ff2565b6000602082840312156123b057600080fd5b8151611f7381611e66565b60208152600082518060208401526123da8160408501602087016122d2565b601f01601f1916919091016040019291505056fea2646970667358221220dbb80eace97b70de4daf232752c96cd0ee502181307484fd163b5733dbd9cb8764736f6c63430008140033

Verified Source Code Full Match

Compiler: v0.8.20+commit.a1b79de6 EVM: paris Optimization: Yes (200 runs)
Ownable.sol 83 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (access/Ownable.sol)

pragma solidity ^0.8.0;

import "../utils/Context.sol";

/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * By default, the owner account will be the one that deploys the contract. This
 * can later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract Ownable is Context {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    constructor() {
        _transferOwnership(_msgSender());
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if the sender is not the owner.
     */
    function _checkOwner() internal view virtual {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby disabling any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}
draft-IERC20Permit.sol 8 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/extensions/draft-IERC20Permit.sol)

pragma solidity ^0.8.0;

// EIP-2612 is Final as of 2022-11-01. This file is deprecated.

import "./IERC20Permit.sol";
IERC20Permit.sol 90 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.4) (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.
 *
 * ==== Security Considerations
 *
 * There are two important considerations concerning the use of `permit`. The first is that a valid permit signature
 * expresses an allowance, and it should not be assumed to convey additional meaning. In particular, it should not be
 * considered as an intention to spend the allowance in any specific way. The second is that because permits have
 * built-in replay protection and can be submitted by anyone, they can be frontrun. A protocol that uses permits should
 * take this into consideration and allow a `permit` call to fail. Combining these two aspects, a pattern that may be
 * generally recommended is:
 *
 * ```solidity
 * function doThingWithPermit(..., uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) public {
 *     try token.permit(msg.sender, address(this), value, deadline, v, r, s) {} catch {}
 *     doThing(..., value);
 * }
 *
 * function doThing(..., uint256 value) public {
 *     token.safeTransferFrom(msg.sender, address(this), value);
 *     ...
 * }
 * ```
 *
 * Observe that: 1) `msg.sender` is used as the owner, leaving no ambiguity as to the signer intent, and 2) the use of
 * `try/catch` allows the permit to fail and makes the code tolerant to frontrunning. (See also
 * {SafeERC20-safeTransferFrom}).
 *
 * Additionally, note that smart contract wallets (such as Argent or Safe) are not able to produce permit signatures, so
 * contracts should have entry points that don't rely on permit.
 */
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].
     *
     * CAUTION: See Security Considerations above.
     */
    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);
        }
    }
}
Context.sol 28 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.4) (utils/Context.sol)

pragma solidity ^0.8.0;

/**
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }

    function _contextSuffixLength() internal view virtual returns (uint256) {
        return 0;
    }
}
Permitable.sol 32 lines
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import '@openzeppelin/contracts/token/ERC20/extensions/draft-IERC20Permit.sol';

contract Permitable {
  event Reverted(string reason);

  function _permit(
    IERC20 token,
    uint256 amount,
    bytes memory p
  ) internal {

    if (p.length == 32 * 7) {
      
      (bool success,) = address(token).call(
        abi.encodePacked(IERC20Permit.permit.selector, p)
      );

      if (!success) {
        if (token.allowance(msg.sender, address(this)) < amount) {
          revert("Permit call failed:: allowance");
        } else {
          emit Reverted("Permit call failed:: else");
        }
      }
    }
  }
}
SwapRouter.sol 544 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/utils/Context.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

import "./Permitable.sol";
import "./TransferHelper.sol";

contract SwapRouter is Permitable, Ownable {
    using SafeERC20 for IERC20;

    address private constant ETH_ADDRESS =
        address(0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE);

    uint256 private constant _PARTIAL_FILL = 0x01;
    uint256 private constant _REQUIRES_EXTRA_ETH = 0x02;
    uint256 private constant _SHOULD_CLAIM = 0x04;
    uint256 private constant _BURN_FROM_MSG_SENDER = 0x08;
    uint256 private constant _BURN_FROM_TX_ORIGIN = 0x10;
    uint256 private constant _SIMPLE_SWAP = 0x20;
    uint256 private constant _FEE_ON_DST = 0x40;
    uint256 private constant _FEE_IN_BPS = 0x80;
    uint256 private constant _APPROVE_FUND = 0x100;

    uint256 private constant BPS = 10000;

    uint256 public feeBps = 20;
    address public feeRecipient;

    mapping(address => bool) public isWhitelist;

    struct SwapDescriptionV2 {
        IERC20 srcToken;
        IERC20 dstToken;
        address[] srcReceivers; // transfer src token to these addresses, default
        uint256[] srcAmounts;
        address[] feeReceivers;
        uint256[] feeAmounts;
        address dstReceiver;
        uint256 amount;
        uint256 minReturnAmount;
        uint256 flags;
        bytes permit;
    }

    /// @dev  use for swapGeneric and swap to avoid stack too deep
    struct SwapExecutionParams {
        address callTarget; // call this address
        address approveTarget; // approve this address if _APPROVE_FUND set
        bytes targetData;
        SwapDescriptionV2 desc;
        bytes clientData;
    }

    struct SimpleSwapData {
        address[] firstPools;
        uint256[] firstSwapAmounts;
        bytes[] swapDatas;
        uint256 deadline;
        bytes destTokenFeeData;
    }

    event Swapped(
        address target,
        address sender,
        IERC20 srcToken,
        IERC20 dstToken,
        address dstReceiver,
        uint256 spentAmount,
        uint256 returnAmount
    );

    event ClientData(bytes clientData);

    event Fee(
        bool system,
        uint256 amount,
        address token,
        address recipient
    );

    constructor(address _feeRecipient) {
        feeRecipient = _feeRecipient;
    }

    receive() external payable {}

    function rescueFunds(address token, uint256 amount) external onlyOwner {
        if (_isETH(IERC20(token))) {
            TransferHelper.safeTransferETH(msg.sender, amount);
        } else {
            TransferHelper.safeTransfer(token, msg.sender, amount);
        }
    }

    function updateWhitelist(
        address[] memory addr,
        bool[] memory value
    ) external onlyOwner {
        require(addr.length == value.length);
        for (uint256 i; i < addr.length; ++i) {
            isWhitelist[addr[i]] = value[i];
        }
    }

    function setFeeProtocol(uint256 _feeBps) external onlyOwner {
        feeBps = _feeBps;
    }

    function setFeeRecipient(address _feeRecipient) external onlyOwner {
        feeRecipient = _feeRecipient;
    }

    function swapGeneric(
        SwapExecutionParams calldata execution
    ) external payable returns (uint256 returnAmount, uint256 gasUsed) {
        uint256 gasBefore = gasleft();
        require(isWhitelist[execution.callTarget], "Address not whitelisted");
        if (
            execution.approveTarget != execution.callTarget &&
            execution.approveTarget != address(0)
        ) {
            require(
                isWhitelist[execution.approveTarget],
                "Address not whitelisted"
            );
        }
        SwapDescriptionV2 memory desc = execution.desc;
        require(desc.minReturnAmount > 0, "Invalid min return amount");

        // if extra eth is needed, in case srcToken is ETH
        _collectExtraETHIfNeeded(desc);
        _permit(desc.srcToken, desc.amount, desc.permit);

        bool feeInBps = _flagsChecked(desc.flags, _FEE_IN_BPS);
        uint256 spentAmount;
        address dstReceiver = desc.dstReceiver == address(0)
            ? msg.sender
            : desc.dstReceiver;
        if (!_flagsChecked(desc.flags, _FEE_ON_DST)) {
            // fee on src token
            // take fee on srcToken

            // take fee and deduct total amount
            desc.amount = _takeFee(
                desc.srcToken,
                msg.sender,
                desc.feeReceivers,
                desc.feeAmounts,
                desc.amount,
                feeInBps
            );

            // take protocol fee and deduct total amount
            desc.amount = _takeFeeProtocol(
                desc.srcToken,
                msg.sender,
                desc.amount
            );

            bool collected;

            if (
                !_isETH(desc.srcToken) &&
                _flagsChecked(desc.flags, _SHOULD_CLAIM)
            ) {
                (collected, desc.amount) = _collectTokenIfNeeded(
                    desc,
                    msg.sender,
                    address(this)
                );
            }

            _transferFromOrApproveTarget(
                msg.sender,
                execution.approveTarget,
                desc,
                collected
            );
            // execute swap
            (spentAmount, returnAmount) = _executeSwap(
                execution.callTarget,
                execution.targetData,
                desc,
                _isETH(desc.srcToken) ? desc.amount : 0,
                dstReceiver
            );
        } else {
            bool collected;
            if (
                !_isETH(desc.srcToken) &&
                _flagsChecked(desc.flags, _SHOULD_CLAIM)
            ) {
                (collected, desc.amount) = _collectTokenIfNeeded(
                    desc,
                    msg.sender,
                    address(this)
                );
            }

            uint256 initialDstReceiverBalance = _getBalance(
                desc.dstToken,
                dstReceiver
            );
            _transferFromOrApproveTarget(
                msg.sender,
                execution.approveTarget,
                desc,
                collected
            );
            // fee on dst token
            // router get dst token first
            (spentAmount, returnAmount) = _executeSwap(
                execution.callTarget,
                execution.targetData,
                desc,
                _isETH(desc.srcToken) ? msg.value : 0,
                address(this)
            );
            {
                // then take fee on dst token
                uint256 leftAmount = _takeFee(
                    desc.dstToken,
                    address(this),
                    desc.feeReceivers,
                    desc.feeAmounts,
                    returnAmount,
                    feeInBps
                );

                // take protocol fee on dst token
                leftAmount = _takeFeeProtocol(
                    desc.dstToken,
                    address(this),
                    leftAmount
                );

                _doTransferERC20(
                    desc.dstToken,
                    address(this),
                    dstReceiver,
                    leftAmount
                );
            }

            returnAmount =
                _getBalance(desc.dstToken, dstReceiver) -
                initialDstReceiverBalance;
        }
        // check return amount
        _checkReturnAmount(spentAmount, returnAmount, desc);
        //revoke allowance
        if (!_isETH(desc.srcToken) && execution.approveTarget != address(0)) {
            desc.srcToken.safeApprove(execution.approveTarget, 0);
        }

        emit Swapped(
            execution.callTarget,
            msg.sender,
            desc.srcToken,
            desc.dstToken,
            dstReceiver,
            spentAmount,
            returnAmount
        );

        if (execution.clientData.length > 0) {
            emit ClientData(execution.clientData);
        }    

        unchecked {
            gasUsed = gasBefore - gasleft();
        }
    }

    function _doTransferERC20(
        IERC20 token,
        address from,
        address to,
        uint256 amount
    ) internal {
        require(from != to, "sender != recipient");
        if (amount > 0) {
            if (_isETH(token)) {
                if (from == address(this))
                    TransferHelper.safeTransferETH(to, amount);
            } else {
                if (from == address(this)) {
                    TransferHelper.safeTransfer(address(token), to, amount);
                } else {
                    TransferHelper.safeTransferFrom(
                        address(token),
                        from,
                        to,
                        amount
                    );
                }
            }
        }
    }

    function _getBalance(
        IERC20 token,
        address account
    ) internal view returns (uint256) {
        if (_isETH(token)) {
            return account.balance;
        } else {
            return token.balanceOf(account);
        }
    }

    function _isETH(IERC20 token) internal pure returns (bool) {
        return (address(token) == ETH_ADDRESS);
    }

    /// @dev this function calls to external contract to execute swap and also validate the returned amounts
    function _executeSwap(
        address callTarget,
        bytes memory targetData,
        SwapDescriptionV2 memory desc,
        uint256 value,
        address dstReceiver
    ) internal returns (uint256 spentAmount, uint256 returnAmount) {
        uint256 initialDstBalance = _getBalance(desc.dstToken, dstReceiver);
        uint256 routerInitialSrcBalance = _getBalance(
            desc.srcToken,
            address(this)
        );
        uint256 routerInitialDstBalance = _getBalance(
            desc.dstToken,
            address(this)
        );
        {
            // call to external contract
            (bool success, ) = callTarget.call{value: value}(targetData);
            require(success, "Call failed");
        }

        // if the `callTarget` returns amount to `msg.sender`, meaning this contract
        if (dstReceiver != address(this)) {
            uint256 stuckAmount = _getBalance(desc.dstToken, address(this)) -
                routerInitialDstBalance;
            _doTransferERC20(
                desc.dstToken,
                address(this),
                dstReceiver,
                stuckAmount
            );
        }

        // safe check here
        returnAmount =
            _getBalance(desc.dstToken, dstReceiver) -
            initialDstBalance;
        spentAmount = desc.amount;

        //should refund tokens router collected when partial fill
        if (
            _flagsChecked(desc.flags, _PARTIAL_FILL) &&
            (_isETH(desc.srcToken) || _flagsChecked(desc.flags, _SHOULD_CLAIM))
        ) {
            uint256 currBalance = _getBalance(desc.srcToken, address(this));
            if (currBalance != routerInitialSrcBalance) {
                spentAmount = routerInitialSrcBalance - currBalance;
                _doTransferERC20(
                    desc.srcToken,
                    address(this),
                    msg.sender,
                    desc.amount - spentAmount
                );
            }
        }
    }

    function _collectExtraETHIfNeeded(SwapDescriptionV2 memory desc) internal {
        bool srcETH = _isETH(desc.srcToken);
        if (_flagsChecked(desc.flags, _REQUIRES_EXTRA_ETH)) {
            require(
                msg.value > (srcETH ? desc.amount : 0),
                "Invalid msg.value"
            );
        } else {
            require(
                msg.value == (srcETH ? desc.amount : 0),
                "Invalid msg.value"
            );
        }
    }

    function _collectTokenIfNeeded(
        SwapDescriptionV2 memory desc,
        address from,
        address to
    ) internal returns (bool collected, uint256 amount) {
        require(!_isETH(desc.srcToken), "Claim token is ETH");
        uint256 initialRouterSrcBalance = _getBalance(
            desc.srcToken,
            address(this)
        );
        _doTransferERC20(desc.srcToken, from, to, desc.amount);
        collected = true;
        amount =
            _getBalance(desc.srcToken, address(this)) -
            initialRouterSrcBalance;
    }

    /// @dev transfer fund to `callTarget` or approve `approveTarget`
    function _transferFromOrApproveTarget(
        address from,
        address approveTarget,
        SwapDescriptionV2 memory desc,
        bool collected
    ) internal {
        // if token is collected
        require(
            desc.srcReceivers.length == desc.srcAmounts.length,
            "invalid srcReceivers length"
        );
        if (collected) {
            if (
                _flagsChecked(desc.flags, _APPROVE_FUND) &&
                approveTarget != address(0)
            ) {
                // approve to approveTarget since some systems use an allowance proxy contract
                desc.srcToken.safeIncreaseAllowance(approveTarget, desc.amount);
                return;
            }
        }
        uint256 total;
        for (uint256 i; i < desc.srcReceivers.length; ++i) {
            total += desc.srcAmounts[i];
            _doTransferERC20(
                desc.srcToken,
                collected ? address(this) : from,
                desc.srcReceivers[i],
                desc.srcAmounts[i]
            );
        }
        require(total <= desc.amount, "Exceeded desc.amount");
    }

    /// @dev token transferred from `from` to `feeData.recipients`
    function _takeFee(
        IERC20 token,
        address from,
        address[] memory recipients,
        uint256[] memory amounts,
        uint256 totalAmount,
        bool inBps
    ) internal returns (uint256 leftAmount) {
        leftAmount = totalAmount;
        uint256 recipientsLen = recipients.length;
        if (recipientsLen > 0) {
            bool isETH = _isETH(token);
            uint256 balanceBefore = _getBalance(
                token,
                isETH ? address(this) : from
            );
            require(amounts.length == recipientsLen, "Invalid length");
            for (uint256 i; i < recipientsLen; ++i) {

                uint256 amount = inBps
                    ? (totalAmount * amounts[i]) / BPS
                    : amounts[i];

                _doTransferERC20(
                    token,
                    isETH ? address(this) : from,
                    recipients[i],
                    amount
                );

                emit Fee(
                    false,
                    amount,
                    address(token),
                    recipients[i]
                );
            }

            uint256 totalFee = balanceBefore -
                _getBalance(token, isETH ? address(this) : from);
            leftAmount = totalAmount - totalFee;
        }
    }

    function _takeFeeProtocol(
        IERC20 token,
        address from,
        uint256 totalAmount
    ) internal returns (uint256 leftAmount) {
        leftAmount = totalAmount;

        bool isETH = _isETH(token);

        uint256 feeAmount = (totalAmount * feeBps) / BPS;

        _doTransferERC20(
            token,
            isETH ? address(this) : from,
            feeRecipient,
            feeAmount
        );

        leftAmount = totalAmount - feeAmount;

        emit Fee(
            true,
            feeAmount,
            address(token),
            feeRecipient
        );
    }

    function _checkReturnAmount(
        uint256 spentAmount,
        uint256 returnAmount,
        SwapDescriptionV2 memory desc
    ) internal pure {
        if (_flagsChecked(desc.flags, _PARTIAL_FILL)) {
            require(
                returnAmount * desc.amount >=
                    desc.minReturnAmount * spentAmount,
                "Return amount is not enough"
            );
        } else {
            require(
                returnAmount >= desc.minReturnAmount,
                "Return amount is not enough"
            );
        }
    }

    function _flagsChecked(
        uint256 number,
        uint256 flag
    ) internal pure returns (bool) {
        return number & flag != 0;
    }
}
TransferHelper.sol 45 lines
// SPDX-License-Identifier: GPL-3.0-or-later

pragma solidity >=0.5.16;

// helper methods for interacting with ERC20 tokens and sending ETH that do not consistently return true/false
library TransferHelper {
  function safeApprove(
    address token,
    address to,
    uint256 value
  ) internal {
    // bytes4(keccak256(bytes('approve(address,uint256)')));
    (bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x095ea7b3, to, value));
    require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: APPROVE_FAILED');
  }

  function safeTransfer(
    address token,
    address to,
    uint256 value
  ) internal {
    // bytes4(keccak256(bytes('transfer(address,uint256)')));
    if (value == 0) return;
    (bool success, bytes memory data) = token.call(abi.encodeWithSelector(0xa9059cbb, to, value));
    require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: TRANSFER_FAILED');
  }

  function safeTransferFrom(
    address token,
    address from,
    address to,
    uint256 value
  ) internal {
    // bytes4(keccak256(bytes('transferFrom(address,address,uint256)')));
    if (value == 0) return;
    (bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x23b872dd, from, to, value));
    require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: TRANSFER_FROM_FAILED');
  }

  function safeTransferETH(address to, uint256 value) internal {
    if (value == 0) return;
    (bool success, ) = to.call{value: value}(new bytes(0));
    require(success, 'TransferHelper: ETH_TRANSFER_FAILED');
  }
}

Read Contract

feeBps 0x24a9d853 → uint256
feeRecipient 0x46904840 → address
isWhitelist 0xc683630d → bool
owner 0x8da5cb5b → address

Write Contract 7 functions

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

renounceOwnership 0x715018a6
No parameters
rescueFunds 0x78e3214f
address token
uint256 amount
setFeeProtocol 0x67e5b01c
uint256 _feeBps
setFeeRecipient 0xe74b981b
address _feeRecipient
swapGeneric 0x546da4a1
tuple execution
returns: uint256, uint256
transferOwnership 0xf2fde38b
address newOwner
updateWhitelist 0x33320de3
address[] addr
bool[] value

Recent Transactions

No transactions found for this address