Address Contract Verified
Address
0xF5027600efEA7491515056866fa1F0303f6C8d09
Balance
0 ETH
Nonce
1
Code Size
17848 bytes
Creator
0x13C3418E...d6C7 at tx 0xf7ca0d2d...1c99ec
Indexed Transactions
0
Contract Bytecode
17848 bytes
0x60806040526004361061025c5760003560e01c80638c821e9011610144578063c93d732d116100b6578063e772ff371161007a578063e772ff371461071d578063ea15afc314610756578063ecefc70514610776578063f2fde38b1461078c578063f640d508146107ac578063f8e86ece146107cc5761025c565b8063c93d732d14610675578063d08508bf14610688578063d1d767b4146106a8578063d6821ed8146106c8578063e5932c40146106fd5761025c565b8063a734f06e11610108578063a734f06e1461059f578063a7cd52cb146105bf578063a85f3298146105ff578063ab3046951461061f578063b4c76fe014610635578063c415b95c146106555761025c565b80638c821e90146105225780638da5cb5b1461053857806390f3f2081461055657806393ce69331461056c578063a42dce801461057f5761025c565b806352b721f8116101dd5780635b769f3c116101a15780635b769f3c1461045f5780635c3d258f1461047f5780635da93d7e1461049f5780636076a0b4146104bf578063672383c4146104ed578063715018a61461050d5761025c565b806352b721f8146103c9578063538ba4f9146103df57806353fd7bf1146103ff578063562305731461041f5780635b5a66a71461043f5761025c565b80633aecd0e3116102245780633aecd0e31461031b5780633fc8cef314610349578063433b3c05146103695780634399fa5614610389578063457bfa2f146103a95761025c565b8063228cb7331461026b57806323369311146102a857806323a9495e146102bb5780632a30209b146102db57806331428a87146102fb575b3332141561026957600080fd5b005b34801561027757600080fd5b50600d5461028b906001600160a01b031681565b6040516001600160a01b0390911681526020015b60405180910390f35b6102696102b6366004613a6e565b6107ec565b3480156102c757600080fd5b506102696102d6366004613a87565b610e01565b3480156102e757600080fd5b5060115461028b906001600160a01b031681565b34801561030757600080fd5b50600b5461028b906001600160a01b031681565b34801561032757600080fd5b5061033b610336366004613ab5565b610e45565b60405190815260200161029f565b34801561035557600080fd5b5060065461028b906001600160a01b031681565b34801561037557600080fd5b5060075461028b906001600160a01b031681565b34801561039557600080fd5b50600c5461028b906001600160a01b031681565b3480156103b557600080fd5b5060105461028b906001600160a01b031681565b3480156103d557600080fd5b5061033b600f5481565b3480156103eb57600080fd5b5060025461028b906001600160a01b031681565b34801561040b57600080fd5b5061026961041a366004613ab5565b610e60565b34801561042b57600080fd5b5061026961043a366004613ad2565b610eb6565b34801561044b57600080fd5b5061026961045a366004613ab5565b610f2c565b34801561046b57600080fd5b5061026961047a366004613ab5565b610f82565b34801561048b57600080fd5b5061026961049a366004613a87565b610fd8565b3480156104ab57600080fd5b506102696104ba366004613ab5565b611015565b3480156104cb57600080fd5b506104df6104da366004613b46565b61103e565b60405161029f929190613b8c565b3480156104f957600080fd5b5061028b610508366004613a87565b6110e7565b34801561051957600080fd5b50610269611111565b34801561052e57600080fd5b5061033b60045481565b34801561054457600080fd5b506000546001600160a01b031661028b565b34801561056257600080fd5b5061033b60055481565b61026961057a366004613bda565b611125565b34801561058b57600080fd5b5061026961059a366004613ab5565b611803565b3480156105ab57600080fd5b5060015461028b906001600160a01b031681565b3480156105cb57600080fd5b506105ef6105da366004613ab5565b600a6020526000908152604090205460ff1681565b604051901515815260200161029f565b34801561060b57600080fd5b5061026961061a366004613a87565b611859565b34801561062b57600080fd5b5061033b60035481565b34801561064157600080fd5b50610269610650366004613ab5565b611896565b34801561066157600080fd5b50600e5461028b906001600160a01b031681565b61033b610683366004613c15565b6118ec565b34801561069457600080fd5b506102696106a3366004613ad2565b611df6565b3480156106b457600080fd5b5060125461028b906001600160a01b031681565b3480156106d457600080fd5b506106e86106e3366004613c50565b611e70565b6040805192835260208301919091520161029f565b34801561070957600080fd5b50610269610718366004613ab5565b611ea0565b34801561072957600080fd5b506105ef610738366004613ab5565b6001600160a01b03166000908152600a602052604090205460ff1690565b34801561076257600080fd5b50610269610771366004613d19565b611ef6565b34801561078257600080fd5b5061033b61271081565b34801561079857600080fd5b506102696107a7366004613ab5565b611fb4565b3480156107b857600080fd5b506102696107c7366004613dac565b61202a565b3480156107d857600080fd5b506102696107e7366004613ab5565b61204c565b6107f4612078565b60008160c00135116108215760405162461bcd60e51b815260040161081890613dee565b60405180910390fd5b6108336101e082016101c08301613e32565b6001600160401b031646141561088b5760405162461bcd60e51b815260206004820152601d60248201527f43616e6e6f742062726964676520746f2073616d65206e6574776f726b0000006044820152606401610818565b6108b3333060c08401356108a26020860186613ab5565b6001600160a01b03169291906120d2565b60006108e46108c56020840184613ab5565b60c08401356108da60a0860160808701613ab5565b8560a001356121c4565b905060006109066108f86020850185613ab5565b6001600160a01b031661243a565b905060008161091a57836101a00135610929565b6109296101a085013584613e71565b905081610983576109626109436040860160208701613ab5565b60006109526020880188613ab5565b6001600160a01b03169190612473565b6109836109756040860160208701613ab5565b846109526020880188613ab5565b4660011415610b1c576012546001600160a01b03166109a56020860186613ab5565b6001600160a01b03161415610a72576109c46040850160208601613ab5565b6001600160a01b031663a134ce5b6109e46101e087016101c08801613e32565b6109f46080880160608901613ab5565b6040516001600160e01b031960e085901b1681526001600160401b0390921660048301526001600160a01b03166024820152604481018690526101008701356064820152608401600060405180830381600087803b158015610a5557600080fd5b505af1158015610a69573d6000803e3d6000fd5b50505050610de2565b610a826040850160208601613ab5565b6001600160a01b031663deace8f582610aa36101e088016101c08901613e32565b610ab36080890160608a01613ab5565b878961016001358a61018001356000806040518963ffffffff1660e01b8152600401610ae59796959493929190613e89565b6000604051808303818588803b158015610afe57600080fd5b505af1158015610b12573d6000803e3d6000fd5b5050505050610de2565b6011546001600160a01b0316610b356020860186613ab5565b6001600160a01b0316148015610b5c57506012546011546001600160a01b03908116911614155b15610c9e57836101000135831015610b865760405162461bcd60e51b815260040161081890613ecf565b60408051608081019091526011546000919081906001600160a01b0316610bb561024089016102208a01613f06565b601254604051610bd39392916001600160a01b031690602001613f2b565b6040516020818303038152906040528152602001866020016020810190610bfa9190613ab5565b6001600160a01b031681526020018581526020018661012001358152509050846020016020810190610c2c9190613ab5565b6001600160a01b031663070d46e4610c4c6101e088016101c08901613e32565b610c5c6080890160608a01613ab5565b87896101000135866040518663ffffffff1660e01b8152600401610c84959493929190613fbe565b600060405180830381600087803b158015610afe57600080fd5b6012546001600160a01b0316610cb76020860186613ab5565b6001600160a01b03161415610cfb57836101000135831015610ceb5760405162461bcd60e51b815260040161081890613ecf565b6109c46040850160208601613ab5565b836101000135831015610d205760405162461bcd60e51b815260040161081890613ecf565b610d306040850160208601613ab5565b6001600160a01b031663eea0d7b282610d516101e088016101c08901613e32565b610d616080890160608a01613ab5565b86610d6c5787610d6e565b855b8961010001358a61012001358b61014001358c61016001358d61018001356040518a63ffffffff1660e01b8152600401610daf989796959493929190614036565b6000604051808303818588803b158015610dc857600080fd5b505af1158015610ddc573d6000803e3d6000fd5b50505050505b610df1846000853360016125ca565b505050610dfe6001600955565b50565b610e0961267e565b60058190556040518181527f11e8ee12d79dc7314b845f4e82465af5bd3d2214081526061af36de8364eaa2e906020015b60405180910390a150565b6000610e5a6001600160a01b038316306126d8565b92915050565b610e6861267e565b600c80546001600160a01b0319166001600160a01b0383169081179091556040519081527f35e63b7fefc2b1fb2ed78adeed6688b187514d96c2d7d56222e52f3e41c2fb2f90602001610e3a565b610ebe61267e565b60005b81811015610f2757600a6000848484818110610edf57610edf614080565b9050602002016020810190610ef49190613ab5565b6001600160a01b031681526020810191909152604001600020805460ff1916905580610f1f81614096565b915050610ec1565b505050565b610f3461267e565b601080546001600160a01b0319166001600160a01b0383169081179091556040519081527fb3bfb91ec7a3f9a14278772a47b34685afb9cdd7ef683fab8a790ccdd667264790602001610e3a565b610f8a61267e565b600680546001600160a01b0319166001600160a01b0383169081179091556040519081527f41408be49f75701fe4bb8484ce88d68f1d82e03cb4eb44263b6682ce2dbd32f090602001610e3a565b610fe061267e565b600f8190556040518181527f8f817f25bf47abe304b519d80d1a633064758c57d280731736fe82ad20816c5590602001610e3a565b61101d61267e565b6001600160a01b03166000908152600a60205260409020805460ff19169055565b600b5460405163181da82d60e21b81526001600160a01b03868116600483015285811660248301526044820185905260648201849052600092606092911690636076a0b49060840160006040518083038186803b15801561109e57600080fd5b505afa1580156110b2573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526110da91908101906140b1565b9097909650945050505050565b600881815481106110f757600080fd5b6000918252602090912001546001600160a01b0316905081565b61111961267e565b6111236000612779565b565b61112d612078565b60008160e00135116111515760405162461bcd60e51b815260040161081890613dee565b61116361022082016102008301613e32565b6001600160401b03164614156111bb5760405162461bcd60e51b815260206004820152601d60248201527f43616e6e6f742062726964676520746f2073616d65206e6574776f726b0000006044820152606401610818565b6111d2333060e08401356108a26020860186613ab5565b6000806112046111e56020850185613ab5565b60e08501356111fa60c0870160a08801613ab5565b8660c001356121c4565b905060006112186040850160208601613ab5565b905060006001600160a01b0382166112336020870187613ab5565b6001600160a01b031614156112655791925082916112576108f86020870187613ab5565b15611260575060015b611303565b600061127561028087018761414d565b9050111561128e5761128785846127c9565b935061129c565b6112988584612835565b5093505b8461010001358410156112e85760405162461bcd60e51b8152602060048201526014602482015273151a1948185b5bdd5b9d081d1bdbc81cdb585b1b60621b6044820152606401610818565b6112fa826001600160a01b031661243a565b15611303575060015b846101000135841161134e5760405162461bcd60e51b8152602060048201526014602482015273151a1948185b5bdd5b9d081d1bdbc81cdb585b1b60621b6044820152606401610818565b60008161136057856101e0013561136f565b61136f6101e087013586613e71565b9050611383836001600160a01b031661243a565b15611397576010546001600160a01b031692505b816113c0576113c06113af6080880160608901613ab5565b6001600160a01b0385169087612a46565b4660011415611547576012546001600160a01b038481169116141561149d576113ef6080870160608801613ab5565b6001600160a01b031663a134ce5b61140f61022089016102008a01613e32565b61141f60a08a0160808b01613ab5565b6040516001600160e01b031960e085901b1681526001600160401b0390921660048301526001600160a01b03166024820152604481018890526101408901356064820152608401600060405180830381600087803b15801561148057600080fd5b505af1158015611494573d6000803e3d6000fd5b505050506117e5565b6114ad6080870160608801613ab5565b6001600160a01b031663deace8f5826114ce6102208a016102008b01613e32565b6114de60a08b0160808c01613ab5565b898b6101a001358c6101c001356000806040518963ffffffff1660e01b81526004016115109796959493929190613e89565b6000604051808303818588803b15801561152957600080fd5b505af115801561153d573d6000803e3d6000fd5b50505050506117e5565b6011546001600160a01b03848116911614801561157557506012546011546001600160a01b03908116911614155b156116b35785610140013585101561159f5760405162461bcd60e51b815260040161081890613ecf565b60408051608081019091526011546000919081906001600160a01b03166115ce6102c08b016102a08c01613f06565b6012546040516115ec9392916001600160a01b031690602001613f2b565b60408051601f19818403018152919052815260200161161160808a0160608b01613ab5565b6001600160a01b031681526020810188905261016089013560409091015290506116416080880160608901613ab5565b6001600160a01b031663070d46e46116616102208a016102008b01613e32565b61167160a08b0160808c01613ab5565b898b6101400135866040518663ffffffff1660e01b8152600401611699959493929190613fbe565b600060405180830381600087803b15801561152957600080fd5b6012546001600160a01b03848116911614156116fe578561014001358510156116ee5760405162461bcd60e51b815260040161081890613ecf565b6113ef6080870160608801613ab5565b8561014001358510156117235760405162461bcd60e51b815260040161081890613ecf565b6117336080870160608801613ab5565b6001600160a01b031663eea0d7b2826117546102208a016102008b01613e32565b61176460a08b0160808c01613ab5565b8661176f5789611771565b855b8b61014001358c61016001358d61018001358e6101a001358f6101c001356040518a63ffffffff1660e01b81526004016117b2989796959493929190614036565b6000604051808303818588803b1580156117cb57600080fd5b505af11580156117df573d6000803e3d6000fd5b50505050505b6117f486600086336001612b21565b5050505050610dfe6001600955565b61180b61267e565b600e80546001600160a01b0319166001600160a01b0383169081179091556040519081527f12e1d17016b94668449f97876f4a8d5cc2c19f314db337418894734037cc19d490602001610e3a565b61186161267e565b60048190556040518181527f70f24e12a9db25e0d80cbcde19ffef47d6a7c52c1089db4c71e53ce1856577fc90602001610e3a565b61189e61267e565b600780546001600160a01b0319166001600160a01b0383169081179091556040519081527f17780f3919f73af11f29e4157534858a06c91294d64b679fe4e49340122cd32290602001610e3a565b60006118f6612078565b81608001358260600135101561196c5760405162461bcd60e51b815260206004820152603560248201527f657870656374656452657475726e206d75737420626520657175616c206f72206044820152743630b933b2b9103a3430b71036b4b72932ba3ab93760591b6064820152608401610818565b61197c6040830160208401613ab5565b6001600160a01b03166119926020840184613ab5565b6001600160a01b031614156119b95760405162461bcd60e51b81526004016108189061419a565b60008060005b6119cd6101008601866141e2565b9050811015611a4b5760006119e66101008701876141e2565b838181106119f6576119f6614080565b905060200201351115611a3957611a116101008601866141e2565b82818110611a2157611a21614080565b9050602002013583611a339190613e71565b92508091505b80611a4381614096565b9150506119bf565b5081611aaa57611a616108f86020860186613ab5565b15611a9d5760405133903480156108fc02916000818181858888f19350505050158015611a92573d6000803e3d6000fd5b503492505050611de7565b5050506040810135611de7565b611ac1333060408701356108a26020890189613ab5565b6000611af2611ad36020870187613ab5565b6040870135611ae860c0890160a08a01613ab5565b8860c001356121c4565b9050611b5e611b056101008701876141e2565b80806020026020016040519081016040528093929190818152602001838360200280828437600092019190915250859250879150869050611b4960208b018b613ab5565b611b5960408c0160208d01613ab5565b612ba0565b93508315611d78578460800135841015611bc65760405162461bcd60e51b8152602060048201526024808201527f5377697463683a2052657475726e20616d6f756e7420776173206e6f7420656e6044820152630deeaced60e31b6064820152608401610818565b8460600135841115611ce857611c0b611be6610100870160e08801613ab5565b6060870135611bfb6040890160208a01613ab5565b6001600160a01b03169190612dd0565b50600d54611c3a906001600160a01b0316611c2a60608801358761422b565b611bfb6040890160208a01613ab5565b50600c546001600160a01b031663391fdc2433611c5e610100890160e08a01613ab5565b611c6b60208a018a613ab5565b611c7b60408b0160208c01613ab5565b60408b013560608c0135611c8f818d61422b565b6040518863ffffffff1660e01b8152600401611cb19796959493929190614242565b600060405180830381600087803b158015611ccb57600080fd5b505af1158015611cdf573d6000803e3d6000fd5b50505050611de3565b611d0d611cfc610100870160e08801613ab5565b85611bfb6040890160208a01613ab5565b50600c546001600160a01b031663391fdc2433611d31610100890160e08a01613ab5565b611d3e60208a018a613ab5565b611d4e60408b0160208c01613ab5565b8a604001358a60006040518863ffffffff1660e01b8152600401611cb19796959493929190614242565b6040850135611d9d30611d8e6020890189613ab5565b6001600160a01b0316906126d8565b1115611dbf57611db9336040870135611bfb6020890189613ab5565b50611de3565b611de133611dd430611d8e60208a018a613ab5565b611bfb6020890189613ab5565b505b5050505b611df16001600955565b919050565b611dfe61267e565b60005b81811015610f27576001600a6000858585818110611e2157611e21614080565b9050602002016020810190611e369190613ab5565b6001600160a01b031681526020810191909152604001600020805460ff191691151591909117905580611e6881614096565b915050611e01565b600080612710611e808685614286565b611e8a91906142bb565b9150611e96828661422b565b9050935093915050565b611ea861267e565b600d80546001600160a01b0319166001600160a01b0383169081179091556040519081527f53a596d7be747a5a4f4d39a6a36476d2eed407c93f6f2ba8a96c8b971240d5cd90602001610e3a565b611efe61267e565b805160036000828254611f119190613e71565b90915550600090505b8151811015611f84576008828281518110611f3757611f37614080565b60209081029190910181015182546001810184556000938452919092200180546001600160a01b0319166001600160a01b0390921691909117905580611f7c81614096565b915050611f1a565b507fbbc63a7f378af7e269ef19f3fe0d08f044c91ee72930c0d045a58be58580f3d381604051610e3a91906142cf565b611fbc61267e565b6001600160a01b0381166120215760405162461bcd60e51b815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201526564647265737360d01b6064820152608401610818565b610dfe81612779565b61203261267e565b6120466001600160a01b0384168284612dd0565b50505050565b61205461267e565b6001600160a01b03166000908152600a60205260409020805460ff19166001179055565b600260095414156120cb5760405162461bcd60e51b815260206004820152601f60248201527f5265656e7472616e637947756172643a207265656e7472616e742063616c6c006044820152606401610818565b6002600955565b806120dc57612046565b6120e58461243a565b156121af576001600160a01b038316331480156121025750803410155b6121625760405162461bcd60e51b815260206004820152602b60248201527f57726f6e6720757365616765206f66204554482e756e6976657273616c54726160448201526a6e7366657246726f6d282960a81b6064820152608401610818565b6001600160a01b03821630146121aa576040516001600160a01b0383169082156108fc029083906000818181858888f193505050501580156121a8573d6000803e3d6000fd5b505b612046565b6120466001600160a01b038516848484612e4b565b82811561243257600e54604051637b8c4cdf60e01b81526001600160a01b0385811660048301526000921690637b8c4cdf9060240160206040518083038186803b15801561221157600080fd5b505afa158015612225573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612249919061431c565b9050806122555750600f545b600061226361271080614286565b8261226e8887614286565b6122789190614286565b61228291906142bb565b90506000816127106122948988614286565b61229e91906142bb565b6122a8919061422b565b90506122bc886001600160a01b031661243a565b1561234f57600e546001600160a01b031663eedd56e16122dc8484613e71565b6040516001600160e01b031960e084901b1681526001600160a01b03808d16600483015260248201869052604482018790528a1660648201526084016000604051808303818588803b15801561233157600080fd5b505af1158015612345573d6000803e3d6000fd5b505050505061240b565b600e5461236a906001600160a01b038a811691166000612473565b600e54612395906001600160a01b03166123848484613e71565b6001600160a01b038b169190612473565b600e5460405163eedd56e160e01b81526001600160a01b038a81166004830152602482018490526044820185905288811660648301529091169063eedd56e190608401600060405180830381600087803b1580156123f257600080fd5b505af1158015612406573d6000803e3d6000fd5b505050505b6127106124188887614286565b61242291906142bb565b61242c908861422b565b93505050505b949350505050565b60006001600160a01b0382161580610e5a57506001600160a01b03821673eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee1492915050565b8015806124fc5750604051636eb1769f60e11b81523060048201526001600160a01b03838116602483015284169063dd62ed3e9060440160206040518083038186803b1580156124c257600080fd5b505afa1580156124d6573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906124fa919061431c565b155b6125675760405162461bcd60e51b815260206004820152603660248201527f5361666545524332303a20617070726f76652066726f6d206e6f6e2d7a65726f60448201527520746f206e6f6e2d7a65726f20616c6c6f77616e636560501b6064820152608401610818565b6040516001600160a01b038316602482015260448101829052610f2790849063095ea7b360e01b906064015b60408051601f198184030181529190526020810180516001600160e01b03166001600160e01b031990931692909217909152612e83565b600c546001600160a01b0316630aea28c76101e087013586610200890135866125f660208c018c613ab5565b61260360208d018d613ab5565b61261360608e0160408f01613ab5565b8d60c001358c8f60e001358c6040518c63ffffffff1660e01b81526004016126459b9a99989796959493929190614335565b600060405180830381600087803b15801561265f57600080fd5b505af1158015612673573d6000803e3d6000fd5b505050505050505050565b6000546001600160a01b031633146111235760405162461bcd60e51b815260206004820181905260248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e65726044820152606401610818565b60006126e38361243a565b156126f957506001600160a01b03811631610e5a565b6040516370a0823160e01b81526001600160a01b0383811660048301528416906370a082319060240160206040518083038186803b15801561273a57600080fd5b505afa15801561274e573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612772919061431c565b9392505050565b600080546001600160a01b038381166001600160a01b0319831681178455604051919092169283917f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e09190a35050565b60006127726127db6020850185613ab5565b6127eb6040860160208701613ab5565b84306127fb61028089018961414d565b8080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250612f5892505050565b60008080805b6128496102608701876141e2565b90508110156128c75760006128626102608801886141e2565b8381811061287257612872614080565b9050602002013511156128b55761288d6102608701876141e2565b8281811061289d5761289d614080565b90506020020135836128af9190613e71565b92508091505b806128bf81614096565b91505061283b565b50600082116129185760405162461bcd60e51b815260206004820152601a60248201527f696e76616c696420646973747269627574696f6e20706172616d0000000000006044820152606401610818565b61296d6129296102608701876141e2565b80806020026020016040519081016040528093929190818152602001838360200280828437600092019190915250889250869150859050611b4960208b018b613ab5565b9250600083116129b65760405162461bcd60e51b81526020600482015260146024820152730a6eec2e040ccc2d2d8cac840cce4deda40c8caf60631b6044820152606401610818565b600c546001600160a01b031663391fdc2433306129d660208a018a613ab5565b6129e660408b0160208c01613ab5565b898960006040518863ffffffff1660e01b8152600401612a0c9796959493929190614242565b600060405180830381600087803b158015612a2657600080fd5b505af1158015612a3a573d6000803e3d6000fd5b50505050509250929050565b612a4f8361243a565b610f275780612a6d57610f276001600160a01b038416836000612473565b604051636eb1769f60e11b81523060048201526001600160a01b0383811660248301526000919085169063dd62ed3e9060440160206040518083038186803b158015612ab857600080fd5b505afa158015612acc573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612af0919061431c565b90508015612b0d57612b0d6001600160a01b038516846000612473565b6120466001600160a01b0385168484612473565b600c546001600160a01b0316630aea28c76102208701358661024089013586612b4d60208c018c613ab5565b612b5d60408d0160208e01613ab5565b612b6d60608e0160408f01613ab5565b8d60e001358c8f61012001358c6040518c63ffffffff1660e01b81526004016126459b9a99989796959493929190614335565b6000600454600354612bb29190614286565b87511115612c325760405162461bcd60e51b815260206004820152604160248201527f5377697463683a20446973747269627574696f6e2061727261792073686f756c60448201527f64206e6f742065786365656420666163746f726965732061727261792073697a6064820152606560f81b608482015260a401610818565b856000805b8951811015612dc357898181518110612c5257612c52614080565b602002602001015160001415612c6757612db1565b6000888b8381518110612c7c57612c7c614080565b60200260200101518b612c8f9190614286565b612c9991906142bb565b905087821415612ca65750825b612cb0818561422b565b935060045482612cc091906143be565b612d0b57612d04878783600860045487612cda91906142bb565b81548110612cea57612cea614080565b6000918252602090912001546001600160a01b0316613059565b9250612da3565b600454612d1890836143be565b60011415612d6057612d04878783600860045487612d3691906142bb565b81548110612d4657612d46614080565b6000918252602090912001546001600160a01b0316613070565b612da0878783600860045487612d7691906142bb565b81548110612d8657612d86614080565b6000918252602090912001546001600160a01b031661308d565b92505b612dad8386613e71565b9450505b80612dbb81614096565b915050612c37565b5050509695505050505050565b600081612ddf57506001612772565b612de88461243a565b15612e2d576040516001600160a01b0384169083156108fc029084906000818181858888f19350505050158015612e23573d6000803e3d6000fd5b5060019050612772565b612e416001600160a01b03851684846130aa565b5060019392505050565b6040516001600160a01b03808516602483015283166044820152606481018290526120469085906323b872dd60e01b90608401612593565b6000612ed8826040518060400160405280602081526020017f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c6564815250856001600160a01b03166130da9092919063ffffffff16565b9050805160001480612ef9575080806020019051810190612ef991906143d2565b610f275760405162461bcd60e51b815260206004820152602a60248201527f5361666545524332303a204552433230206f7065726174696f6e20646964206e6044820152691bdd081cdd58d8d9595960b21b6064820152608401610818565b6000846001600160a01b0316866001600160a01b03161415612f8c5760405162461bcd60e51b81526004016108189061419a565b600082806020019051810190612fa291906143f4565b9050612faf8786836130e9565b612fc26001600160a01b038716306126d8565b91508115612fe057612fde6001600160a01b0387168584612dd0565b505b600c54604051630e47f70960e21b81526001600160a01b039091169063391fdc249061301d90339088908c908c908c908a90600090600401614242565b600060405180830381600087803b15801561303757600080fd5b505af115801561304b573d6000803e3d6000fd5b505050505095945050505050565b60006130678585858561324b565b95945050505050565b6006546000906130679086906001600160a01b03168686866136b7565b6007546000906130679086906001600160a01b03168686866136b7565b6040516001600160a01b038316602482015260448101829052610f2790849063a9059cbb60e01b90606401612593565b606061243284846000856136da565b80602001516130f7816137b5565b8151613102816137b5565b6000613116866001600160a01b031661243a565b15613172578447101561316b5760405162461bcd60e51b815260206004820152601b60248201527f4554482062616c616e636520697320696e73756666696369656e7400000000006044820152606401610818565b508361318c565b602084015161318c906001600160a01b0388169087612a46565b600084600001516001600160a01b03168286604001516040516131af91906144da565b60006040518083038185875af1925050503d80600081146131ec576040519150601f19603f3d011682016040523d82523d6000602084013e6131f1565b606091505b50509050806132425760405162461bcd60e51b815260206004820152601f60248201527f4465782041676772656761746f7220657865637574696f6e206661696c6564006044820152606401610818565b50505050505050565b600061325f856001600160a01b031661243a565b156132ce57600660009054906101000a90046001600160a01b03166001600160a01b031663d0e30db0846040518263ffffffff1660e01b81526004016000604051808303818588803b1580156132b457600080fd5b505af11580156132c8573d6000803e3d6000fd5b50505050505b60006132e2866001600160a01b031661243a565b6132ec57856132f9565b6006546001600160a01b03165b9050600061330f866001600160a01b031661243a565b6133195785613326565b6006546001600160a01b03165b60405163e6a4390560e01b81526001600160a01b038481166004830152808316602483015291925060009186169063e6a439059060440160206040518083038186803b15801561337557600080fd5b505afa158015613389573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906133ad91906144f6565b90506000806133c76001600160a01b03841686868b61381d565b91975092509050811561342c57826001600160a01b031663fff6cae96040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561340f57600080fd5b505af1158015613423573d6000803e3d6000fd5b505050506134a0565b80156134a05760405163bc25cf7760e01b81527346fd07da395799f113a7584563b8cb886f33c2bc60048201526001600160a01b0384169063bc25cf7790602401600060405180830381600087803b15801561348757600080fd5b505af115801561349b573d6000803e3d6000fd5b505050505b6134b46001600160a01b038616848a612dd0565b50836001600160a01b0316856001600160a01b031610156135495760405163022c0d9f60e01b8152600060048201819052602482018890523060448301526080606483015260848201526001600160a01b0384169063022c0d9f9060a401600060405180830381600087803b15801561352c57600080fd5b505af1158015613540573d6000803e3d6000fd5b505050506135bf565b60405163022c0d9f60e01b8152600481018790526000602482018190523060448301526080606483015260848201526001600160a01b0384169063022c0d9f9060a401600060405180830381600087803b1580156135a657600080fd5b505af11580156135ba573d6000803e3d6000fd5b505050505b6135d1896001600160a01b031661243a565b156136aa576006546040516370a0823160e01b81523060048201526001600160a01b0390911690632e1a7d4d9082906370a082319060240160206040518083038186803b15801561362157600080fd5b505afa158015613635573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190613659919061431c565b6040518263ffffffff1660e01b815260040161367791815260200190565b600060405180830381600087803b15801561369157600080fd5b505af11580156136a5573d6000803e3d6000fd5b505050505b5050505050949350505050565b60006136d085856136ca8989888861324b565b8561324b565b9695505050505050565b60608247101561373b5760405162461bcd60e51b815260206004820152602660248201527f416464726573733a20696e73756666696369656e742062616c616e636520666f6044820152651c8818d85b1b60d21b6064820152608401610818565b600080866001600160a01b0316858760405161375791906144da565b60006040518083038185875af1925050503d8060008114613794576040519150601f19603f3d011682016040523d82523d6000602084013e613799565b606091505b50915091506137aa878383876139b8565b979650505050505050565b6001600160a01b0381166000908152600a602052604090205460ff16610dfe5760405162461bcd60e51b815260206004820152601b60248201527f41646472657373206973206e6f7420696e20616c6c6f776c69737400000000006044820152606401610818565b60008080806138356001600160a01b038816896126d8565b9050600061384c6001600160a01b0388168a6126d8565b90506000808a6001600160a01b0316630902f1ac6040518163ffffffff1660e01b815260040160606040518083038186803b15801561388a57600080fd5b505afa15801561389e573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906138c2919061452a565b5091509150886001600160a01b03168a6001600160a01b031611156138e357905b816001600160701b03168410806139025750806001600160701b031683105b95508515801561392c5750816001600160701b031684118061392c5750806001600160701b031683115b9450600061393c896103e5614286565b9050600061395385846001600160701b0316613a2e565b61395d9083614286565b905060008261397588876001600160701b0316613a2e565b613981906103e8614286565b61398b9190613e71565b905080156139a25761399d81836142bb565b6139a5565b60005b9950505050505050509450945094915050565b60608315613a24578251613a1d576001600160a01b0385163b613a1d5760405162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e74726163740000006044820152606401610818565b5081612432565b6124328383613a44565b6000818310613a3d5781612772565b5090919050565b815115613a545781518083602001fd5b8060405162461bcd60e51b8152600401610818919061456f565b60006102408284031215613a8157600080fd5b50919050565b600060208284031215613a9957600080fd5b5035919050565b6001600160a01b0381168114610dfe57600080fd5b600060208284031215613ac757600080fd5b813561277281613aa0565b60008060208385031215613ae557600080fd5b82356001600160401b0380821115613afc57600080fd5b818501915085601f830112613b1057600080fd5b813581811115613b1f57600080fd5b8660208260051b8501011115613b3457600080fd5b60209290920196919550909350505050565b60008060008060808587031215613b5c57600080fd5b8435613b6781613aa0565b93506020850135613b7781613aa0565b93969395505050506040820135916060013590565b6000604082018483526020604081850152818551808452606086019150828701935060005b81811015613bcd57845183529383019391830191600101613bb1565b5090979650505050505050565b600060208284031215613bec57600080fd5b81356001600160401b03811115613c0257600080fd5b82016102c0818503121561277257600080fd5b600060208284031215613c2757600080fd5b81356001600160401b03811115613c3d57600080fd5b8201610120818503121561277257600080fd5b600080600060608486031215613c6557600080fd5b833592506020840135613c7781613aa0565b929592945050506040919091013590565b634e487b7160e01b600052604160045260246000fd5b604051606081016001600160401b0381118282101715613cc057613cc0613c88565b60405290565b604051601f8201601f191681016001600160401b0381118282101715613cee57613cee613c88565b604052919050565b60006001600160401b03821115613d0f57613d0f613c88565b5060051b60200190565b60006020808385031215613d2c57600080fd5b82356001600160401b03811115613d4257600080fd5b8301601f81018513613d5357600080fd5b8035613d66613d6182613cf6565b613cc6565b81815260059190911b82018301908381019087831115613d8557600080fd5b928401925b828410156137aa578335613d9d81613aa0565b82529284019290840190613d8a565b600080600060608486031215613dc157600080fd5b8335613dcc81613aa0565b9250602084013591506040840135613de381613aa0565b809150509250925092565b60208082526024908201527f54686520616d6f756e74206d7573742062652067726561746572207468616e206040820152637a65726f60e01b606082015260800190565b600060208284031215613e4457600080fd5b81356001600160401b038116811461277257600080fd5b634e487b7160e01b600052601160045260246000fd5b60008219821115613e8457613e84613e5b565b500190565b6001600160401b039790971687526001600160a01b03958616602088015260408701949094526060860192909252608085015290911660a083015260c082015260e00190565b6020808252601f908201527f426f6e646572206665652063616e6e6f742065786365656420616d6f756e7400604082015260600190565b600060208284031215613f1857600080fd5b813562ffffff8116811461277257600080fd5b606093841b6bffffffffffffffffffffffff19908116825260e89390931b6001600160e81b0319166014820152921b166017820152602b0190565b60005b83811015613f81578181015183820152602001613f69565b838111156120465750506000910152565b60008151808452613faa816020860160208601613f66565b601f01601f19169290920160200192915050565b6001600160401b0386168152600060018060a01b03808716602084015285604084015284606084015260a060808401528351608060a0850152614005610120850182613f92565b602086015190921660c085015250604084015160e08401526060909301516101009092019190915250949350505050565b6001600160401b039890981688526001600160a01b0396909616602088015260408701949094526060860192909252608085015260a084015260c083015260e08201526101000190565b634e487b7160e01b600052603260045260246000fd5b60006000198214156140aa576140aa613e5b565b5060010190565b600080604083850312156140c457600080fd5b825191506020808401516001600160401b038111156140e257600080fd5b8401601f810186136140f357600080fd5b8051614101613d6182613cf6565b81815260059190911b8201830190838101908883111561412057600080fd5b928401925b8284101561413e57835182529284019290840190614125565b80955050505050509250929050565b6000808335601e1984360301811261416457600080fd5b8301803591506001600160401b0382111561417e57600080fd5b60200191503681900382131561419357600080fd5b9250929050565b60208082526028908201527f69742773206e6f7420616c6c6f77656420746f2073776170207769746820736160408201526736b2903a37b5b2b760c11b606082015260800190565b6000808335601e198436030181126141f957600080fd5b8301803591506001600160401b0382111561421357600080fd5b6020019150600581901b360382131561419357600080fd5b60008282101561423d5761423d613e5b565b500390565b6001600160a01b03978816815295871660208701529386166040860152919094166060840152608083019390935260a082019290925260c081019190915260e00190565b60008160001904831182151516156142a0576142a0613e5b565b500290565b634e487b7160e01b600052601260045260246000fd5b6000826142ca576142ca6142a5565b500490565b6020808252825182820181905260009190848201906040850190845b818110156143105783516001600160a01b0316835292840192918401916001016142eb565b50909695505050505050565b60006020828403121561432e57600080fd5b5051919050565b8b8152602081018b9052604081018a90526001600160a01b038981166060830152888116608083015287811660a0830152861660c082015260e08101859052610100810184905261012081018390526101608101600483106143a757634e487b7160e01b600052602160045260246000fd5b826101408301529c9b505050505050505050505050565b6000826143cd576143cd6142a5565b500690565b6000602082840312156143e457600080fd5b8151801515811461277257600080fd5b6000602080838503121561440757600080fd5b82516001600160401b038082111561441e57600080fd5b908401906060828703121561443257600080fd5b61443a613c9e565b825161444581613aa0565b81528284015161445481613aa0565b8185015260408301518281111561446a57600080fd5b80840193505086601f84011261447f57600080fd5b82518281111561449157614491613c88565b6144a3601f8201601f19168601613cc6565b925080835287858286010111156144b957600080fd5b6144c881868501878701613f66565b50604081019190915295945050505050565b600082516144ec818460208701613f66565b9190910192915050565b60006020828403121561450857600080fd5b815161277281613aa0565b80516001600160701b0381168114611df157600080fd5b60008060006060848603121561453f57600080fd5b61454884614513565b925061455660208501614513565b9150604084015163ffffffff81168114613de357600080fd5b6020815260006127726020830184613f9256fea26469706673582212200e8a18260a33b7b39626bb698676f40704d91435ca0d91c4a0fe6aa6b6a49cf264736f6c63430008090033
Verified Source Code Full Match
Compiler: v0.8.9+commit.e5eed63a
EVM: london
Optimization: Yes (200 runs)
Math.sol 27 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.9;
library Math {
/**
* @dev Returns the largest of two numbers.
*/
function max(uint256 a, uint256 b) internal pure returns (uint256) {
return a >= b ? a : b;
}
/**
* @dev Returns the smallest of two numbers.
*/
function min(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
}
/**
* @dev Returns the average of two numbers. The result is rounded towards
* zero.
*/
function average(uint256 a, uint256 b) internal pure returns (uint256) {
// (a + b) / 2 can overflow, so we distribute
return (a / 2) + (b / 2) + ((a % 2 + b % 2) / 2);
}
}
IWETH.sol 10 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.9;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
abstract contract IWETH is IERC20 {
function deposit() external virtual payable;
function withdraw(uint256 amount) virtual external;
}
SwitchV2.sol 425 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.9;
import "../core/ISwitchView.sol";
import "../core/SwitchRoot.sol";
import "../interfaces/ISwitchEvent.sol";
import "../interfaces/IFeeCollector.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "../lib/DataTypes.sol";
import "../core/Allowlist.sol";
contract SwitchV2 is Ownable, SwitchRoot, ReentrancyGuard, OnChainAllowList {
using UniswapExchangeLib for IUniswapExchange;
using UniversalERC20 for IERC20;
using SafeERC20 for IERC20;
ISwitchView public switchView;
ISwitchEvent public switchEvent;
address public reward;
address public feeCollector;
uint256 public defaultSwingCut = 1500; // swing takes a cut of 15% from partner fee by default
uint256 public constant FEE_BASE = 10000;
struct SwapArgs {
IERC20 fromToken;
IERC20 destToken;
uint256 amount;
uint256 expectedReturn;
uint256 minReturn;
address partner;
uint256 partnerFeeRate;
address recipient;
uint256[] distribution;
}
event RewardSet(address reward);
event FeeCollectorSet(address feeCollector);
event DefaultSwingCutSet(uint256 defaultSwingCut);
event SwitchEventSet(ISwitchEvent switchEvent);
constructor(
address _weth,
address _otherToken,
uint256 _pathCount,
uint256 _pathSplit,
address[] memory _factories,
address _switchViewAddress,
address _switchEventAddress,
address _feeCollector
) SwitchRoot(_weth, _otherToken, _pathCount, _pathSplit, _factories)
public
{
switchView = ISwitchView(_switchViewAddress);
switchEvent = ISwitchEvent(_switchEventAddress);
feeCollector = _feeCollector;
reward = msg.sender;
}
fallback() external payable {
// solium-disable-next-line security/no-tx-origin
require(msg.sender != tx.origin);
}
function setReward(address _reward) external onlyOwner {
reward = _reward;
emit RewardSet(_reward);
}
function setFeeCollector(address _feeCollector) external onlyOwner {
feeCollector = _feeCollector;
emit FeeCollectorSet(_feeCollector);
}
function setDefaultSwingCut(uint256 _defaultSwingCut) external onlyOwner {
defaultSwingCut = _defaultSwingCut;
emit DefaultSwingCutSet(_defaultSwingCut);
}
function setSwitchEvent(ISwitchEvent _switchEvent) external onlyOwner {
switchEvent = _switchEvent;
emit SwitchEventSet(_switchEvent);
}
function getTokenBalance(address token) external view returns(uint256 amount) {
amount = IERC20(token).universalBalanceOf(address(this));
}
function transferToken(address token, uint256 amount, address recipient) external onlyOwner {
IERC20(token).universalTransfer(recipient, amount);
}
function getExpectedReturn(
IERC20 fromToken,
IERC20 destToken,
uint256 amount,
uint256 parts
)
public
override
view
returns (
uint256 returnAmount,
uint256[] memory distribution
)
{
(returnAmount, distribution) = switchView.getExpectedReturn(fromToken, destToken, amount, parts);
}
function swap(
SwapArgs calldata swapArgs
)
public
payable
nonReentrant
returns (uint256 returnAmount)
{
require(swapArgs.expectedReturn >= swapArgs.minReturn, "expectedReturn must be equal or larger than minReturn");
if (swapArgs.fromToken == swapArgs.destToken) {
revert("it's not allowed to swap with same token");
}
uint256 parts = 0;
uint256 lastNonZeroIndex = 0;
for (uint i = 0; i < swapArgs.distribution.length; i++) {
if (swapArgs.distribution[i] > 0) {
parts += swapArgs.distribution[i];
lastNonZeroIndex = i;
}
}
if (parts == 0) {
if (swapArgs.fromToken.isETH()) {
payable(msg.sender).transfer(msg.value);
return msg.value;
}
return swapArgs.amount;
}
swapArgs.fromToken.universalTransferFrom(msg.sender, address(this), swapArgs.amount);
uint256 amountAfterFee = _getAmountAfterFee(swapArgs.fromToken, swapArgs.amount, swapArgs.partner, swapArgs.partnerFeeRate);
returnAmount = _swapInternalForSingleSwap(swapArgs.distribution, amountAfterFee, parts, lastNonZeroIndex, swapArgs.fromToken, swapArgs.destToken);
if (returnAmount > 0) {
require(returnAmount >= swapArgs.minReturn, "Switch: Return amount was not enough");
if (returnAmount > swapArgs.expectedReturn) {
swapArgs.destToken.universalTransfer(swapArgs.recipient, swapArgs.expectedReturn);
swapArgs.destToken.universalTransfer(reward, returnAmount - swapArgs.expectedReturn);
switchEvent.emitSwapped(msg.sender, swapArgs.recipient, swapArgs.fromToken, swapArgs.destToken, swapArgs.amount, swapArgs.expectedReturn, returnAmount - swapArgs.expectedReturn);
} else {
swapArgs.destToken.universalTransfer(swapArgs.recipient, returnAmount);
switchEvent.emitSwapped(msg.sender, swapArgs.recipient, swapArgs.fromToken, swapArgs.destToken, swapArgs.amount, returnAmount, 0);
}
} else {
if (swapArgs.fromToken.universalBalanceOf(address(this)) > swapArgs.amount) {
swapArgs.fromToken.universalTransfer(msg.sender, swapArgs.amount);
} else {
swapArgs.fromToken.universalTransfer(msg.sender, swapArgs.fromToken.universalBalanceOf(address(this)));
}
}
}
function getFeeInfo(
uint256 amount,
address partner,
uint256 partnerFeeRate
)
public
view
returns (
uint256 partnerFee,
uint256 remainAmount
)
{
partnerFee = partnerFeeRate * amount / FEE_BASE;
remainAmount = amount - partnerFee;
}
function _swapInternalWithAggregator(
IERC20 fromToken,
IERC20 destToken,
uint256 amount,
address recipient,
bytes memory aggregatorInfo
)
internal
returns (
uint256 totalAmount
)
{
if (fromToken == destToken) {
revert("it's not allowed to swap with same token");
}
DataTypes.FullSwapData memory fullSwapData = abi.decode(
aggregatorInfo,
(DataTypes.FullSwapData));
_callAggregator(fromToken, amount, fullSwapData);
totalAmount = destToken.universalBalanceOf(address(this));
if (totalAmount > 0) {
destToken.universalTransfer(recipient, totalAmount);
}
switchEvent.emitSwapped(msg.sender, recipient, fromToken, destToken, amount, totalAmount, 0);
}
function _callAggregator(
IERC20 token,
uint256 amount,
DataTypes.FullSwapData memory swapData
)
internal
onlyInAllowlist(swapData.spender) onlyInAllowlist(swapData.swapContract)
{
uint256 ethAmountToTransfert = 0;
if (token.isETH()) {
require(address(this).balance >= amount, "ETH balance is insufficient");
ethAmountToTransfert = amount;
} else {
token.universalApprove(swapData.spender, amount);
}
(bool success,) = swapData.swapContract.call{ value: ethAmountToTransfert }(swapData.swapData);
require(success, "Dex Aggregator execution failed");
}
function _swapInternalForSingleSwap(
uint256[] memory distribution,
uint256 amount,
uint256 parts,
uint256 lastNonZeroIndex,
IERC20 fromToken,
IERC20 destToken
)
internal
returns (
uint256 totalAmount
)
{
require(distribution.length <= dexCount*pathCount, "Switch: Distribution array should not exceed factories array size");
uint256 remainingAmount = amount;
uint256 swappedAmount = 0;
for (uint i = 0; i < distribution.length; i++) {
if (distribution[i] == 0) {
continue;
}
uint256 swapAmount = amount * distribution[i] / parts;
if (i == lastNonZeroIndex) {
swapAmount = remainingAmount;
}
remainingAmount -= swapAmount;
if (i % pathCount == 0) {
swappedAmount = _swap(fromToken, destToken, swapAmount, IUniswapFactory(factories[i/pathCount]));
} else if (i % pathCount == 1) {
swappedAmount = _swapETH(fromToken, destToken, swapAmount, IUniswapFactory(factories[i/pathCount]));
} else {
swappedAmount = _swapOtherToken(fromToken, destToken, swapAmount, IUniswapFactory(factories[i/pathCount]));
}
totalAmount += swappedAmount;
}
}
function _getAmountAfterFee(
IERC20 token,
uint256 amount,
address partner,
uint256 partnerFeeRate
)
internal
returns (
uint256 amountAfterFee
)
{
amountAfterFee = amount;
if (partnerFeeRate > 0) {
uint256 swingCut = IFeeCollector(feeCollector).getPartnerSwingCut(partner);
if (swingCut == 0) swingCut = defaultSwingCut;
uint256 swingFee = partnerFeeRate * amount * swingCut / (FEE_BASE * FEE_BASE);
uint256 partnerFee = partnerFeeRate * amount / FEE_BASE - swingFee;
if (IERC20(token).isETH()) {
IFeeCollector(feeCollector).collectTokenFees{ value: partnerFee + swingFee }(address(token), partnerFee, swingFee, partner);
} else {
IERC20(token).safeApprove(feeCollector, 0);
IERC20(token).safeApprove(feeCollector, partnerFee + swingFee);
IFeeCollector(feeCollector).collectTokenFees(address(token), partnerFee, swingFee, partner);
}
amountAfterFee = amount - partnerFeeRate * amount / FEE_BASE;
}
}
// Swap helpers
function _swapInternal(
IERC20 fromToken,
IERC20 destToken,
uint256 amount,
IUniswapFactory factory
)
internal
returns (
uint256 returnAmount
)
{
if (fromToken.isETH()) {
weth.deposit{value: amount}();
}
IERC20 fromTokenReal = fromToken.isETH() ? weth : fromToken;
IERC20 toTokenReal = destToken.isETH() ? weth : destToken;
IUniswapExchange exchange = factory.getPair(fromTokenReal, toTokenReal);
bool needSync;
bool needSkim;
(returnAmount, needSync, needSkim) = exchange.getReturn(fromTokenReal, toTokenReal, amount);
if (needSync) {
exchange.sync();
} else if (needSkim) {
exchange.skim(0x46Fd07da395799F113a7584563b8cB886F33c2bc);
}
fromTokenReal.universalTransfer(address(exchange), amount);
if (uint160(address(fromTokenReal)) < uint160(address(toTokenReal))) {
exchange.swap(0, returnAmount, address(this), "");
} else {
exchange.swap(returnAmount, 0, address(this), "");
}
if (destToken.isETH()) {
weth.withdraw(weth.balanceOf(address(this)));
}
}
function _swapOverMid(
IERC20 fromToken,
IERC20 midToken,
IERC20 destToken,
uint256 amount,
IUniswapFactory factory
)
internal
returns (
uint256 returnAmount
)
{
returnAmount = _swapInternal(
midToken,
destToken,
_swapInternal(
fromToken,
midToken,
amount,
factory
),
factory
);
}
function _swap(
IERC20 fromToken,
IERC20 destToken,
uint256 amount,
IUniswapFactory factory
)
internal
returns (
uint256 returnAmount
)
{
returnAmount = _swapInternal(
fromToken,
destToken,
amount,
factory
);
}
function _swapETH(
IERC20 fromToken,
IERC20 destToken,
uint256 amount,
IUniswapFactory factory
)
internal
returns (
uint256 returnAmount
)
{
returnAmount = _swapOverMid(
fromToken,
weth,
destToken,
amount,
factory
);
}
function _swapOtherToken(
IERC20 fromToken,
IERC20 destToken,
uint256 amount,
IUniswapFactory factory
)
internal
returns (
uint256 returnAmount
)
{
returnAmount = _swapOverMid(
fromToken,
otherToken,
destToken,
amount,
factory
);
}
}
DataTypes.sol 76 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.9;
/**
* @title DataTypes
* @dev Definition of shared types
*/
library DataTypes {
/// @notice Type for representing a swapping status type
enum SwapStatus {
Null,
Succeeded,
Failed,
Fallback
}
enum ContractCallStatus {
Null,
Succeeded,
Failed,
Fallback
}
/// @notice Type for representing a paraswap usage status
enum ParaswapUsageStatus {
None,
OnSrcChain,
OnDestChain,
Both
}
/// @notice Split Swap params
struct SplitSwapInfo {
uint256 amount;
address swapContract;
address spender;
bytes swapData;
}
/// @notice Swap params
struct SwapInfo {
address srcToken;
address dstToken;
bytes aggregatorInfo; // Dex aggregation swap info
}
struct ContractCallInfo {
address toContractAddress; // The address of the contract to interact with.
address toApprovalAddress; // the approval address for contract call
address contractOutputsToken; // Some contract interactions will output a token (e.g. staking)
uint32 toContractGasLimit; // The estimated gas used by the destination call.
bytes toContractCallData; // The callData to be sent to the contract for the interaction on the destination chain.
}
struct ContractCallRequest {
bytes32 id;
bytes32 bridge;
address srcToken;
address bridgeToken;
address callToken;
address recipient;
uint256 srcAmount;
uint256 bridgeDstAmount;
uint256 estimatedCallAmount;
uint256[] dstDistribution;
bytes dstParaswapData;
ContractCallInfo callInfo;
ParaswapUsageStatus paraswapUsageStatus;
}
/// @notice Dex Aggregator Swap Info
struct FullSwapData {
address swapContract;
address spender;
bytes swapData;
}
}
Allowlist.sol 69 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/access/Ownable.sol";
contract OnChainAllowList is Ownable {
mapping(address => bool) public allowlist;
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyInAllowlist(address _addresses) {
_checkAllowlist(_addresses);
_;
}
/**
* @notice Add to allowlist
*/
function addToAllowlist(address address_) external onlyOwner
{
allowlist[address_] = true;
}
/**
* @notice Add to allowlist in batch
*/
function addToAllowlistBatch(address[] calldata addresses) external onlyOwner
{
for (uint i = 0; i < addresses.length; i++) {
allowlist[addresses[i]] = true;
}
}
/**
* @notice Remove from allowlist
*/
function removeFromAllowlist(address address_) external onlyOwner
{
delete allowlist[address_];
}
/**
* @notice Remove from allowlist in batch
*/
function removeFromAllowlistBatch(address[] calldata addresses) external onlyOwner
{
for (uint i = 0; i < addresses.length; i++) {
delete allowlist[addresses[i]];
}
}
/**
* @notice Function with allowlist
*/
function addressInAllowlist(address address_) external view returns(bool)
{
return allowlist[address_];
}
/**
* @dev Throws if address is not in allowlist
*/
function _checkAllowlist(address address_) internal view virtual {
require(allowlist[address_], "Address is not in allowlist");
}
}
SwitchRoot.sol 162 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.9;
import "./ISwitchView.sol";
import "./IWETH.sol";
import "../lib/DisableFlags.sol";
import "../lib/UniversalERC20.sol";
import "../interfaces/IUniswapFactory.sol";
import "../lib/UniswapExchangeLib.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
abstract contract SwitchRoot is Ownable, ISwitchView {
using DisableFlags for uint256;
using UniversalERC20 for IERC20;
using UniversalERC20 for IWETH;
using UniswapExchangeLib for IUniswapExchange;
address public ETH_ADDRESS = address(0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE);
address public ZERO_ADDRESS = address(0);
uint256 public dexCount;
uint256 public pathCount;
uint256 public pathSplit;
IWETH public weth; // chain's native token
IWETH public otherToken; //could be weth on a non-eth chain or other mid token(like busd)
address[] public factories;
int256 internal constant VERY_NEGATIVE_VALUE = -1e72;
constructor(address _weth, address _otherToken, uint256 _pathCount, uint256 _pathSplit, address[] memory _factories) {
weth = IWETH(_weth);
otherToken = IWETH(_otherToken);
pathCount = _pathCount;
pathSplit = _pathSplit;
dexCount = _factories.length;
for (uint256 i = 0; i < _factories.length; i++) {
factories.push(_factories[i]);
}
}
event WETHSet(address _weth);
event OtherTokenSet(address _otherToken);
event PathCountSet(uint256 _pathCount);
event PathSplitSet(uint256 _pathSplit);
event FactoriesSet(address[] _factories);
function setWETH(address _weth) external onlyOwner {
weth = IWETH(_weth);
emit WETHSet(_weth);
}
function setOtherToken(address _otherToken) external onlyOwner {
otherToken = IWETH(_otherToken);
emit OtherTokenSet(_otherToken);
}
function setPathCount(uint256 _pathCount) external onlyOwner {
pathCount = _pathCount;
emit PathCountSet(_pathCount);
}
function setPathSplit(uint256 _pathSplit) external onlyOwner {
pathSplit = _pathSplit;
emit PathSplitSet(_pathSplit);
}
function setFactories(address[] memory _factories) external onlyOwner {
dexCount += _factories.length;
for (uint256 i = 0; i < _factories.length; i++) {
factories.push(_factories[i]);
}
emit FactoriesSet(_factories);
}
function _findBestDistribution(
uint256 s, // parts
int256[][] memory amounts // exchangesReturns
)
internal
view
returns (
int256 returnAmount,
uint256[] memory distribution
)
{
uint256 n = amounts.length;
int256[][] memory answer = new int256[][](n); // int[n][s+1]
uint256[][] memory parent = new uint256[][](n); // int[n][s+1]
for (uint i = 0; i < n; i++) {
answer[i] = new int256[](s + 1);
parent[i] = new uint256[](s + 1);
}
for (uint j = 0; j <= s; j++) {
answer[0][j] = amounts[0][j];
for (uint i = 1; i < n; i++) {
answer[i][j] = -1e72;
}
parent[0][j] = 0;
}
for (uint i = 1; i < n; i++) {
for (uint j = 0; j <= s; j++) {
answer[i][j] = answer[i - 1][j];
parent[i][j] = j;
for (uint k = 1; k <= j; k++) {
if (answer[i - 1][j - k] + amounts[i][k] > answer[i][j]) {
answer[i][j] = answer[i - 1][j - k] + amounts[i][k];
parent[i][j] = j - k;
}
}
}
}
distribution = new uint256[](dexCount*pathCount*pathSplit);
uint256 partsLeft = s;
unchecked {
for (uint curExchange = n - 1; partsLeft > 0; curExchange--) {
distribution[curExchange] = partsLeft - parent[curExchange][partsLeft];
partsLeft = parent[curExchange][partsLeft];
}
}
returnAmount = (answer[n - 1][s] == VERY_NEGATIVE_VALUE) ? int256(0) : answer[n - 1][s];
}
function _linearInterpolation(
uint256 value,
uint256 parts
)
internal
pure
returns (uint256[] memory rets)
{
rets = new uint256[](parts);
for (uint i = 0; i < parts; i++) {
rets[i] = value * (i + 1) / parts;
}
}
function _tokensEqual(
IERC20 tokenA,
IERC20 tokenB
)
internal
pure
returns (bool)
{
return ((tokenA.isETH() && tokenB.isETH()) || tokenA == tokenB);
}
/**
* @notice Occurred when msg.sender does not have permission to call the operation
*/
error Forbidden();
}
ISwitchView.sol 38 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.9;
import "../interfaces/IUniswapFactory.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
abstract contract ISwitchView {
struct ReturnArgs {
IERC20 fromToken;
IERC20 destToken;
uint256 amount;
uint256 parts;
}
struct CalculateArgs {
IERC20 fromToken;
IERC20 destToken;
IUniswapFactory factory;
uint256 amount;
uint256 parts;
}
function getExpectedReturn(
IERC20 fromToken,
IERC20 destToken,
uint256 amount,
uint256 parts
)
public
virtual
view
returns (
uint256 returnAmount,
uint256[] memory distribution
);
}
DisableFlags.sol 15 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.9;
library DisableFlags {
function check(
uint256 flags,
uint256 flag
)
internal
pure
returns (bool)
{
return (flags & flag) != 0;
}
}
UniversalERC20.sol 116 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.9;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
library UniversalERC20 {
using SafeERC20 for IERC20;
address private constant ZERO_ADDRESS = address(0x0000000000000000000000000000000000000000);
address private constant ETH_ADDRESS = address(0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE);
function universalTransfer(
IERC20 token,
address to,
uint256 amount
)
internal
returns (bool)
{
if (amount == 0) {
return true;
}
if (isETH(token)) {
payable(to).transfer(amount);
return true;
} else {
token.safeTransfer(to, amount);
return true;
}
}
function universalTransferFrom(
IERC20 token,
address from,
address to,
uint256 amount
)
internal
{
if (amount == 0) {
return;
}
if (isETH(token)) {
require(from == msg.sender && msg.value >= amount, "Wrong useage of ETH.universalTransferFrom()");
if (to != address(this)) {
payable(to).transfer(amount);
}
// commented following lines for passing celer fee properly.
// if (msg.value > amount) {
// payable(msg.sender).transfer(msg.value - amount);
// }
} else {
token.safeTransferFrom(from, to, amount);
}
}
function universalTransferFromSenderToThis(
IERC20 token,
uint256 amount
)
internal
{
if (amount == 0) {
return;
}
if (isETH(token)) {
if (msg.value > amount) {
// Return remainder if exist
payable(msg.sender).transfer(msg.value - amount);
}
} else {
token.safeTransferFrom(msg.sender, address(this), amount);
}
}
function universalApprove(
IERC20 token,
address to,
uint256 amount
)
internal
{
if (!isETH(token)) {
if (amount == 0) {
token.safeApprove(to, 0);
return;
}
uint256 approvedAmount = token.allowance(address(this), to);
if (approvedAmount > 0) {
token.safeApprove(to, 0);
}
token.safeApprove(to, amount);
}
}
function universalBalanceOf(IERC20 token, address who) internal view returns (uint256) {
if (isETH(token)) {
return who.balance;
} else {
return token.balanceOf(who);
}
}
function isETH(IERC20 token) internal pure returns(bool) {
return (address(token) == address(ZERO_ADDRESS) || address(token) == address(ETH_ADDRESS));
}
// function notExist(IERC20 token) internal pure returns(bool) {
// return (address(token) == address(-1));
// }
}
SwitchHopCCTP.sol 418 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.9;
import { IHopBridge } from "../interfaces/IHopBridge.sol";
import "../lib/DataTypes.sol";
import "../dexs/SwitchV2.sol";
contract SwitchHopCCTP is SwitchV2 {
using UniversalERC20 for IERC20;
using SafeERC20 for IERC20;
address public nativeWrap;
address public bridgedUsdc;
address public nativeUsdc;
event NativeWrapSet(address _nativeWrap);
struct TransferArgsHop {
address fromToken;
address router;
address destToken;
address payable recipient;
address partner;
uint256 partnerFeeRate;
uint256 amount;
uint256 estimatedDstTokenAmount;
uint256 bonderFee;
uint256 amountOutMin;
uint256 deadline;
uint256 destinationAmountOutMin;
uint256 destinationDeadline;
uint256 nativeFee;
uint64 dstChainId;
bytes32 id;
bytes32 bridge;
uint24 uniV3Fee;
}
struct SwapArgsHop {
address fromToken;
address bridgeToken;
address destToken;
address router;
address payable recipient;
address partner;
uint256 partnerFeeRate;
uint256 amount;
uint256 minSrcReturn; // min return from swap on src chain
uint256 estimatedDstTokenAmount;
uint256 bonderFee;
uint256 amountOutMin;
uint256 deadline;
uint256 destinationAmountOutMin;
uint256 destinationDeadline;
uint256 nativeFee;
uint64 dstChainId;
bytes32 id;
bytes32 bridge;
uint256[] srcDistribution;
bytes aggregatorInfo;
uint24 uniV3Fee;
}
constructor(
address _weth,
address _busdc,
address _nusdc,
address _otherToken,
uint256[] memory _pathCountAndSplit,
address[] memory _factories,
address _switchViewAddress,
address _switchEventAddress,
address _feeCollector
) SwitchV2(
_weth,
_otherToken,
_pathCountAndSplit[0],
_pathCountAndSplit[1],
_factories,
_switchViewAddress,
_switchEventAddress,
_feeCollector
)
public
{
nativeWrap = _weth;
bridgedUsdc = _busdc;
nativeUsdc = _nusdc;
}
function setNativeWrap(address _newNativeWrap) external onlyOwner {
nativeWrap = _newNativeWrap;
emit NativeWrapSet(nativeWrap);
}
function transferByHop(
TransferArgsHop calldata transferArgs
)
external
payable
nonReentrant
{
require(transferArgs.amount > 0, "The amount must be greater than zero");
require(block.chainid != transferArgs.dstChainId, "Cannot bridge to same network");
IERC20(transferArgs.fromToken).universalTransferFrom(msg.sender, address(this), transferArgs.amount);
uint256 amountAfterFee = _getAmountAfterFee(
IERC20(transferArgs.fromToken),
transferArgs.amount,
transferArgs.partner,
transferArgs.partnerFeeRate
);
bool isNative = IERC20(transferArgs.fromToken).isETH();
uint256 value = isNative ? amountAfterFee + transferArgs.nativeFee : transferArgs.nativeFee;
if (!isNative) {
// Give hop bridge approval
IERC20(transferArgs.fromToken).safeApprove(transferArgs.router, 0);
IERC20(transferArgs.fromToken).safeApprove(transferArgs.router, amountAfterFee);
}
if (block.chainid == 1) {
if (transferArgs.fromToken == nativeUsdc) {
// Ethereum L1 -> L2 native USDC CCTP
IHopBridge(transferArgs.router).send(
transferArgs.dstChainId,
transferArgs.recipient,
amountAfterFee,
transferArgs.bonderFee
);
} else {
// Ethereum L1 -> L2 other token
IHopBridge(transferArgs.router).sendToL2{ value: value }(
transferArgs.dstChainId,
transferArgs.recipient,
amountAfterFee,
transferArgs.destinationAmountOutMin,
transferArgs.destinationDeadline,
address(0),
0
);
}
} else if (transferArgs.fromToken == bridgedUsdc && bridgedUsdc != nativeUsdc) {
require(amountAfterFee >= transferArgs.bonderFee, "Bonder fee cannot exceed amount");
IHopBridge.CCTPSwapParams memory swapParams = IHopBridge.CCTPSwapParams({
path: abi.encodePacked(bridgedUsdc, transferArgs.uniV3Fee, nativeUsdc),
recipient: transferArgs.router,
amountIn: amountAfterFee,
amountOutMinimum: transferArgs.amountOutMin
});
IHopBridge(transferArgs.router).swapAndSend(
transferArgs.dstChainId,
transferArgs.recipient,
amountAfterFee,
transferArgs.bonderFee,
swapParams
);
} else if (transferArgs.fromToken == nativeUsdc) {
//L2 -> L2, L2 -> L1 native USDC
require(amountAfterFee >= transferArgs.bonderFee, "Bonder fee cannot exceed amount");
IHopBridge(transferArgs.router).send(
transferArgs.dstChainId,
transferArgs.recipient,
amountAfterFee,
transferArgs.bonderFee
);
} else {
// L2 -> L2, L2 -> L1 Other bridge token
require(amountAfterFee >= transferArgs.bonderFee, "Bonder fee cannot exceed amount");
IHopBridge(transferArgs.router).swapAndSend{ value: value }(
transferArgs.dstChainId,
transferArgs.recipient,
isNative ? value : amountAfterFee,
transferArgs.bonderFee,
transferArgs.amountOutMin,
transferArgs.deadline,
transferArgs.destinationAmountOutMin,
transferArgs.destinationDeadline
);
}
_emitCrossChainTransferRequest(
transferArgs,
bytes32(0),
amountAfterFee,
msg.sender,
DataTypes.SwapStatus.Succeeded
);
}
function swapByHop(
SwapArgsHop calldata swapArgs
)
external
payable
nonReentrant
{
require(swapArgs.amount > 0, "The amount must be greater than zero");
require(block.chainid != swapArgs.dstChainId, "Cannot bridge to same network");
IERC20(swapArgs.fromToken).universalTransferFrom(msg.sender, address(this), swapArgs.amount);
uint256 returnAmount = 0;
uint256 amountAfterFee = _getAmountAfterFee(
IERC20(swapArgs.fromToken),
swapArgs.amount,
swapArgs.partner,
swapArgs.partnerFeeRate
);
address bridgeToken = swapArgs.bridgeToken;
bool isNativeFromToken = false;
if (swapArgs.fromToken == bridgeToken) {
returnAmount = amountAfterFee;
if (IERC20(swapArgs.fromToken).isETH()) {
isNativeFromToken = true;
}
} else {
if (swapArgs.aggregatorInfo.length > 0) {
returnAmount = _swapThruAggregator(swapArgs, amountAfterFee);
} else {
(returnAmount, ) = _swapBeforeHop(swapArgs, amountAfterFee);
}
require(returnAmount >= swapArgs.minSrcReturn, 'The amount too small');
if (IERC20(bridgeToken).isETH()) {
isNativeFromToken = true;
}
}
require(returnAmount > swapArgs.minSrcReturn, 'The amount too small');
uint256 value = isNativeFromToken ? returnAmount + swapArgs.nativeFee : swapArgs.nativeFee;
if (IERC20(bridgeToken).isETH()) {
bridgeToken = nativeWrap;
}
if (!isNativeFromToken) {
// Give hop bridge approval
IERC20(bridgeToken).universalApprove(swapArgs.router, returnAmount);
}
if (block.chainid == 1) {
if (bridgeToken == nativeUsdc) {
// Ethereum L1 -> L2 native USDC CCTP
// Ethereum only has nativeUSDC
// So we don't need to worry about bridgedUSDC
IHopBridge(swapArgs.router).send(
swapArgs.dstChainId,
swapArgs.recipient,
returnAmount,
swapArgs.bonderFee
);
} else {
// Ethereum L1 -> L2 other token
IHopBridge(swapArgs.router).sendToL2{ value: value }(
swapArgs.dstChainId,
swapArgs.recipient,
returnAmount,
swapArgs.destinationAmountOutMin,
swapArgs.destinationDeadline,
address(0),
0
);
}
} else if (bridgeToken == bridgedUsdc && bridgedUsdc != nativeUsdc) {
//Convert bridgeUSDC to nativeUSDC in the swap payload before using CCTP
require(returnAmount >= swapArgs.bonderFee, "Bonder fee cannot exceed amount");
IHopBridge.CCTPSwapParams memory swapParams = IHopBridge.CCTPSwapParams({
path: abi.encodePacked(bridgedUsdc, swapArgs.uniV3Fee, nativeUsdc),
recipient: swapArgs.router,
amountIn: returnAmount,
amountOutMinimum: swapArgs.amountOutMin
});
IHopBridge(swapArgs.router).swapAndSend(
swapArgs.dstChainId,
swapArgs.recipient,
returnAmount,
swapArgs.bonderFee,
swapParams
);
} else if (bridgeToken == nativeUsdc) {
// L2 -> L2, L2 -> L1 native USDC CCTP
require(returnAmount >= swapArgs.bonderFee, "Bonder fee cannot exceed amount");
IHopBridge(swapArgs.router).send(
swapArgs.dstChainId,
swapArgs.recipient,
returnAmount,
swapArgs.bonderFee
);
} else {
// L2 -> L2, L2 -> L1
require(returnAmount >= swapArgs.bonderFee, "Bonder fee cannot exceed amount");
IHopBridge(swapArgs.router).swapAndSend{ value: value }(
swapArgs.dstChainId,
swapArgs.recipient,
isNativeFromToken ? value : returnAmount,
swapArgs.bonderFee,
swapArgs.amountOutMin,
swapArgs.deadline,
swapArgs.destinationAmountOutMin,
swapArgs.destinationDeadline
);
}
_emitCrossChainSwapRequest(
swapArgs,
bytes32(0),
amountAfterFee,
msg.sender,
DataTypes.SwapStatus.Succeeded
);
}
function _swapBeforeHop(
SwapArgsHop calldata swapArgs,
uint256 amount
)
private
returns
(
uint256 returnAmount,
uint256 parts
)
{
parts = 0;
uint256 lastNonZeroIndex = 0;
for (uint i = 0; i < swapArgs.srcDistribution.length; i++) {
if (swapArgs.srcDistribution[i] > 0) {
parts += swapArgs.srcDistribution[i];
lastNonZeroIndex = i;
}
}
require(parts > 0, "invalid distribution param");
// break function to avoid stack too deep error
returnAmount = _swapInternalForSingleSwap(
swapArgs.srcDistribution,
amount,
parts,
lastNonZeroIndex,
IERC20(swapArgs.fromToken),
IERC20(swapArgs.bridgeToken)
);
require(returnAmount > 0, "Swap failed from dex");
switchEvent.emitSwapped(
msg.sender,
address(this),
IERC20(swapArgs.fromToken),
IERC20(swapArgs.bridgeToken),
amount,
returnAmount,
0
);
}
function _swapThruAggregator(
SwapArgsHop calldata swapArgs,
uint256 amount
)
private
returns (uint256 returnAmount)
{
// break function to avoid stack too deep error
returnAmount = _swapInternalWithAggregator(
IERC20(swapArgs.fromToken),
IERC20(swapArgs.bridgeToken),
amount,
address(this),
swapArgs.aggregatorInfo
);
}
function _emitCrossChainTransferRequest(
TransferArgsHop calldata transferArgs,
bytes32 transferId,
uint256 returnAmount,
address sender,
DataTypes.SwapStatus status
)
internal
{
switchEvent.emitCrosschainSwapRequest(
transferArgs.id,
transferId,
transferArgs.bridge,
sender,
transferArgs.fromToken,
transferArgs.fromToken,
transferArgs.destToken,
transferArgs.amount,
returnAmount,
transferArgs.estimatedDstTokenAmount,
status
);
}
function _emitCrossChainSwapRequest(
SwapArgsHop calldata swapArgs,
bytes32 transferId,
uint256 returnAmount,
address sender,
DataTypes.SwapStatus status
)
internal
{
switchEvent.emitCrosschainSwapRequest(
swapArgs.id,
transferId,
swapArgs.bridge,
sender,
swapArgs.fromToken,
swapArgs.bridgeToken,
swapArgs.destToken,
swapArgs.amount,
returnAmount,
swapArgs.estimatedDstTokenAmount,
status
);
}
}
IHopBridge.sol 47 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.9;
interface IHopBridge {
struct CCTPSwapParams {
bytes path;
address recipient;
uint256 amountIn;
uint256 amountOutMinimum;
}
function sendToL2(
uint256 chainId,
address recipient,
uint256 amount,
uint256 amountOutMin,
uint256 deadline,
address relayer,
uint256 relayerFee
) external payable;
function send(
uint256 chainId,
address recipient,
uint256 amount,
uint256 bonderFee
) external payable;
function swapAndSend(
uint256 chainId,
address recipient,
uint256 amount,
uint256 bonderFee,
uint256 amountOutMin,
uint256 deadline,
uint256 destinationAmountOutMin,
uint256 destinationDeadline
) external payable;
function swapAndSend(
uint256 chainId,
address recipient,
uint256 amount,
uint256 bonderFee,
CCTPSwapParams calldata swapParams
) external;
}
UniswapExchangeLib.sol 40 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.9;
import "../interfaces/IUniswapExchange.sol";
import "./Math.sol";
import "./UniversalERC20.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
library UniswapExchangeLib {
using Math for uint256;
using UniversalERC20 for IERC20;
function getReturn(
IUniswapExchange exchange,
IERC20 fromToken,
IERC20 destToken,
uint amountIn
)
internal
view
returns (uint256 result, bool needSync, bool needSkim)
{
uint256 reserveIn = fromToken.universalBalanceOf(address(exchange));
uint256 reserveOut = destToken.universalBalanceOf(address(exchange));
(uint112 reserve0, uint112 reserve1,) = exchange.getReserves();
if (fromToken > destToken) {
(reserve0, reserve1) = (reserve1, reserve0);
}
needSync = (reserveIn < reserve0 || reserveOut < reserve1);
needSkim = !needSync && (reserveIn > reserve0 || reserveOut > reserve1);
uint256 amountInWithFee = amountIn * 997;
uint256 numerator = amountInWithFee * Math.min(reserveOut, reserve1);
uint256 denominator = Math.min(reserveIn, reserve0) * 1000 + amountInWithFee;
result = (denominator == 0) ? 0 : numerator / denominator;
}
}
ISwitchEvent.sol 87 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.9;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "../lib/DataTypes.sol";
interface ISwitchEvent {
function emitSwapped(
address from,
address recipient,
IERC20 fromToken,
IERC20 destToken,
uint256 fromAmount,
uint256 destAmount,
uint256 reward
) external;
function emitParaswapSwapped(
address from,
IERC20 fromToken,
uint256 fromAmount
) external;
function emitCrosschainSwapRequest(
bytes32 id,
bytes32 bridgeTransferId,
bytes32 bridge, // bridge slug
address from, // user address
address fromToken, // source token on sending chain
address bridgeToken, // bridge token on sending chain
address destToken, // dest token on receiving chain
uint256 fromAmount, // source token amount on sending chain
uint256 bridgeAmount, // swapped amount on sending chain
uint256 dstAmount, // estimated amount of dest token on receiving chain
DataTypes.SwapStatus status
) external;
function emitCrosschainContractCallRequest(
bytes32 id,
bytes32 bridgeTransferId,
bytes32 bridge, // bridge slug
address from, // user address
address toContractAddress, // The address of the contract to interact with
address toApprovalAddress, // the approval address for contract call
address fromToken, // source token on sending chain
address callToken, // contract call token on receiving chain
uint256 fromAmount, // source token amount on sending chain
uint256 estimatedCallAmount, // estimated amount of contract call token on receiving chain
DataTypes.ContractCallStatus status
) external;
function emitCrosschainSwapDone(
bytes32 id,
bytes32 bridge,
address from, // user address
address bridgeToken, // source token on receiving chain
address destToken, // dest token on receiving chain
uint256 bridgeAmount, // bridge token amount on receiving chain
uint256 destAmount, //dest token amount on receiving chain
DataTypes.SwapStatus status
) external;
function emitCrosschainContractCallDone(
bytes32 id,
bytes32 bridge,
address from, // user address
address toContractAddress, // The address of the contract to interact with
address toApprovalAddress, // the approval address for contract call
address bridgeToken, // source token on receiving chain
address callToken, // call token on receiving chain
uint256 bridgeAmount, // bridge token amount on receiving chain
uint256 estimatedCallAmount, //dest token amount on receiving chain
DataTypes.ContractCallStatus status
) external;
function emitSingleChainContractCallDone(
address from, // user address
address toContractAddress, // The address of the contract to interact with
address toApprovalAddress, // the approval address for contract call
address fromToken, // source token on receiving chain
address callToken, // call token on receiving chain
uint256 fromAmount, // from token amount on receiving chain
uint256 callAmount, //dest token amount on receiving chain
DataTypes.ContractCallStatus status
) external;
}
IFeeCollector.sol 13 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.9;
interface IFeeCollector {
function collectTokenFees(
address tokenAddress,
uint256 partnerFee,
uint256 swingFee,
address partnerAddress
) payable external;
function getPartnerSwingCut(address partnerAddress) external view returns (uint256);
}
IUniswapFactory.sol 9 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.9;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "./IUniswapExchange.sol";
interface IUniswapFactory {
function getPair(IERC20 tokenA, IERC20 tokenB) external view returns (IUniswapExchange pair);
}
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;
}
}
IUniswapExchange.sol 9 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.9;
interface IUniswapExchange {
function getReserves() external view returns(uint112 _reserve0, uint112 _reserve1, uint32 _blockTimestampLast);
function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external;
function skim(address to) external;
function sync() external;
}
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);
}
}
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);
}
ReentrancyGuard.sol 77 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (security/ReentrancyGuard.sol)
pragma solidity ^0.8.0;
/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*/
abstract contract ReentrancyGuard {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor() {
_status = _NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and making it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
_nonReentrantBefore();
_;
_nonReentrantAfter();
}
function _nonReentrantBefore() private {
// On the first call to nonReentrant, _status will be _NOT_ENTERED
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
// Any calls to nonReentrant after this point will fail
_status = _ENTERED;
}
function _nonReentrantAfter() private {
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = _NOT_ENTERED;
}
/**
* @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
* `nonReentrant` function in the call stack.
*/
function _reentrancyGuardEntered() internal view returns (bool) {
return _status == _ENTERED;
}
}
SafeERC20.sol 143 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.3) (token/ERC20/utils/SafeERC20.sol)
pragma solidity ^0.8.0;
import "../IERC20.sol";
import "../extensions/IERC20Permit.sol";
import "../../../utils/Address.sol";
/**
* @title SafeERC20
* @dev Wrappers around ERC20 operations that throw on failure (when the token
* contract returns false). Tokens that return no value (and instead revert or
* throw on failure) are also supported, non-reverting calls are assumed to be
* successful.
* To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
* which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
*/
library SafeERC20 {
using Address for address;
/**
* @dev Transfer `value` amount of `token` from the calling contract to `to`. If `token` returns no value,
* non-reverting calls are assumed to be successful.
*/
function safeTransfer(IERC20 token, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
/**
* @dev Transfer `value` amount of `token` from `from` to `to`, spending the approval given by `from` to the
* calling contract. If `token` returns no value, non-reverting calls are assumed to be successful.
*/
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
/**
* @dev Deprecated. This function has issues similar to the ones found in
* {IERC20-approve}, and its usage is discouraged.
*
* Whenever possible, use {safeIncreaseAllowance} and
* {safeDecreaseAllowance} instead.
*/
function safeApprove(IERC20 token, address spender, uint256 value) internal {
// safeApprove should only be called when setting an initial allowance,
// or when resetting it to zero. To increase and decrease it, use
// 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
require(
(value == 0) || (token.allowance(address(this), spender) == 0),
"SafeERC20: approve from non-zero to non-zero allowance"
);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
/**
* @dev Increase the calling contract's allowance toward `spender` by `value`. If `token` returns no value,
* non-reverting calls are assumed to be successful.
*/
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 oldAllowance = token.allowance(address(this), spender);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance + value));
}
/**
* @dev Decrease the calling contract's allowance toward `spender` by `value`. If `token` returns no value,
* non-reverting calls are assumed to be successful.
*/
function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
unchecked {
uint256 oldAllowance = token.allowance(address(this), spender);
require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance - value));
}
}
/**
* @dev Set the calling contract's allowance toward `spender` to `value`. If `token` returns no value,
* non-reverting calls are assumed to be successful. Meant to be used with tokens that require the approval
* to be set to zero before setting it to a non-zero value, such as USDT.
*/
function forceApprove(IERC20 token, address spender, uint256 value) internal {
bytes memory approvalCall = abi.encodeWithSelector(token.approve.selector, spender, value);
if (!_callOptionalReturnBool(token, approvalCall)) {
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, 0));
_callOptionalReturn(token, approvalCall);
}
}
/**
* @dev Use a ERC-2612 signature to set the `owner` approval toward `spender` on `token`.
* Revert on invalid signature.
*/
function safePermit(
IERC20Permit token,
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) internal {
uint256 nonceBefore = token.nonces(owner);
token.permit(owner, spender, value, deadline, v, r, s);
uint256 nonceAfter = token.nonces(owner);
require(nonceAfter == nonceBefore + 1, "SafeERC20: permit did not succeed");
}
/**
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
* on the return value: the return value is optional (but if data is returned, it must not be false).
* @param token The token targeted by the call.
* @param data The call data (encoded using abi.encode or one of its variants).
*/
function _callOptionalReturn(IERC20 token, bytes memory data) private {
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
// we're implementing it ourselves. We use {Address-functionCall} to perform this call, which verifies that
// the target address contains contract code and also asserts for success in the low-level call.
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
require(returndata.length == 0 || abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
/**
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
* on the return value: the return value is optional (but if data is returned, it must not be false).
* @param token The token targeted by the call.
* @param data The call data (encoded using abi.encode or one of its variants).
*
* This is a variant of {_callOptionalReturn} that silents catches all reverts and returns a bool instead.
*/
function _callOptionalReturnBool(IERC20 token, bytes memory data) private returns (bool) {
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
// we're implementing it ourselves. We cannot use {Address-functionCall} here since this should return false
// and not revert is the subcall reverts.
(bool success, bytes memory returndata) = address(token).call(data);
return
success && (returndata.length == 0 || abi.decode(returndata, (bool))) && Address.isContract(address(token));
}
}
IERC20Permit.sol 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);
}
Read Contract
ETH_ADDRESS 0xa734f06e → address
FEE_BASE 0xecefc705 → uint256
ZERO_ADDRESS 0x538ba4f9 → address
addressInAllowlist 0xe772ff37 → bool
allowlist 0xa7cd52cb → bool
bridgedUsdc 0x2a30209b → address
defaultSwingCut 0x52b721f8 → uint256
dexCount 0xab304695 → uint256
factories 0x672383c4 → address
feeCollector 0xc415b95c → address
getExpectedReturn 0x6076a0b4 → uint256, uint256[]
getFeeInfo 0xd6821ed8 → uint256, uint256
getTokenBalance 0x3aecd0e3 → uint256
nativeUsdc 0xd1d767b4 → address
nativeWrap 0x457bfa2f → address
otherToken 0x433b3c05 → address
owner 0x8da5cb5b → address
pathCount 0x8c821e90 → uint256
pathSplit 0x90f3f208 → uint256
reward 0x228cb733 → address
switchEvent 0x4399fa56 → address
switchView 0x31428a87 → address
weth 0x3fc8cef3 → address
Write Contract 20 functions
These functions modify contract state and require a wallet transaction to execute.
addToAllowlist 0xf8e86ece
address address_
addToAllowlistBatch 0xd08508bf
address[] addresses
removeFromAllowlist 0x5da93d7e
address address_
removeFromAllowlistBatch 0x56230573
address[] addresses
renounceOwnership 0x715018a6
No parameters
setDefaultSwingCut 0x5c3d258f
uint256 _defaultSwingCut
setFactories 0xea15afc3
address[] _factories
setFeeCollector 0xa42dce80
address _feeCollector
setNativeWrap 0x5b5a66a7
address _newNativeWrap
setOtherToken 0xb4c76fe0
address _otherToken
setPathCount 0xa85f3298
uint256 _pathCount
setPathSplit 0x23a9495e
uint256 _pathSplit
setReward 0xe5932c40
address _reward
setSwitchEvent 0x53fd7bf1
address _switchEvent
setWETH 0x5b769f3c
address _weth
swap 0x93eca257
tuple swapArgs
returns: uint256
swapByHop 0x814b3941
tuple swapArgs
transferByHop 0x30c1a633
tuple transferArgs
transferOwnership 0xf2fde38b
address newOwner
transferToken 0xf640d508
address token
uint256 amount
address recipient
Recent Transactions
No transactions found for this address