Cryo Explorer Ethereum Mainnet

Address Contract Verified

Address 0x800589bDafE1Fd3C47d545b7012B471e7DFbD0a7
Balance 0 ETH
Nonce 1
Code Size 15732 bytes
Indexed Transactions 0
External Etherscan · Sourcify

Contract Bytecode

15732 bytes
0x60806040526004361061025c5760003560e01c8063715018a611610144578063c93d732d116100b6578063e88943e21161007a578063e88943e214610736578063ea15afc314610756578063ecefc70514610776578063f2fde38b1461078c578063f640d508146107ac578063f8e86ece146107cc5761025c565b8063c93d732d14610675578063d08508bf14610688578063d6821ed8146106a8578063e5932c40146106dd578063e772ff37146106fd5761025c565b8063a734f06e11610108578063a734f06e1461059f578063a7cd52cb146105bf578063a85f3298146105ff578063ab3046951461061f578063b4c76fe014610635578063c415b95c146106555761025c565b8063715018a6146105205780638c821e90146105355780638da5cb5b1461054b57806390f3f20814610569578063a42dce801461057f5761025c565b806352b721f8116101dd5780635b769f3c116101a15780635b769f3c1461045f5780635c3d258f1461047f5780635da93d7e1461049f5780636076a0b4146104bf57806363124e6f146104ed578063672383c4146105005761025c565b806352b721f8146103c9578063538ba4f9146103df57806353fd7bf1146103ff578063562305731461041f5780635b5a66a71461043f5761025c565b80633aecd0e3116102245780633aecd0e31461031b5780633fc8cef314610349578063433b3c05146103695780634399fa5614610389578063457bfa2f146103a95761025c565b8063027c946e1461026b578063228cb733146102a857806323a9495e146102c857806331428a87146102e8578063375560a514610308575b3332141561026957600080fd5b005b34801561027757600080fd5b5060105461028b906001600160a01b031681565b6040516001600160a01b0390911681526020015b60405180910390f35b3480156102b457600080fd5b50600d5461028b906001600160a01b031681565b3480156102d457600080fd5b506102696102e3366004613331565b6107ec565b3480156102f457600080fd5b50600b5461028b906001600160a01b031681565b61026961031636600461334a565b610830565b34801561032757600080fd5b5061033b61033636600461339a565b610ad1565b60405190815260200161029f565b34801561035557600080fd5b5060065461028b906001600160a01b031681565b34801561037557600080fd5b5060075461028b906001600160a01b031681565b34801561039557600080fd5b50600c5461028b906001600160a01b031681565b3480156103b557600080fd5b5060115461028b906001600160a01b031681565b3480156103d557600080fd5b5061033b600f5481565b3480156103eb57600080fd5b5060025461028b906001600160a01b031681565b34801561040b57600080fd5b5061026961041a36600461339a565b610aec565b34801561042b57600080fd5b5061026961043a3660046133b7565b610b42565b34801561044b57600080fd5b5061026961045a36600461339a565b610bb8565b34801561046b57600080fd5b5061026961047a36600461339a565b610c0e565b34801561048b57600080fd5b5061026961049a366004613331565b610c64565b3480156104ab57600080fd5b506102696104ba36600461339a565b610ca1565b3480156104cb57600080fd5b506104df6104da36600461342b565b610cca565b60405161029f929190613471565b6102696104fb3660046134bf565b610d73565b34801561050c57600080fd5b5061028b61051b366004613331565b611033565b34801561052c57600080fd5b5061026961105d565b34801561054157600080fd5b5061033b60045481565b34801561055757600080fd5b506000546001600160a01b031661028b565b34801561057557600080fd5b5061033b60055481565b34801561058b57600080fd5b5061026961059a36600461339a565b611071565b3480156105ab57600080fd5b5060015461028b906001600160a01b031681565b3480156105cb57600080fd5b506105ef6105da36600461339a565b600a6020526000908152604090205460ff1681565b604051901515815260200161029f565b34801561060b57600080fd5b5061026961061a366004613331565b6110c7565b34801561062b57600080fd5b5061033b60035481565b34801561064157600080fd5b5061026961065036600461339a565b611104565b34801561066157600080fd5b50600e5461028b906001600160a01b031681565b61033b6106833660046134fa565b61115a565b34801561069457600080fd5b506102696106a33660046133b7565b611664565b3480156106b457600080fd5b506106c86106c3366004613535565b6116de565b6040805192835260208301919091520161029f565b3480156106e957600080fd5b506102696106f836600461339a565b61170e565b34801561070957600080fd5b506105ef61071836600461339a565b6001600160a01b03166000908152600a602052604090205460ff1690565b34801561074257600080fd5b5061026961075136600461339a565b611764565b34801561076257600080fd5b506102696107713660046135fe565b6117ba565b34801561078257600080fd5b5061033b61271081565b34801561079857600080fd5b506102696107a736600461339a565b611878565b3480156107b857600080fd5b506102696107c7366004613691565b6118ee565b3480156107d857600080fd5b506102696107e736600461339a565b611910565b6107f461193c565b60058190556040518181527f11e8ee12d79dc7314b845f4e82465af5bd3d2214081526061af36de8364eaa2e906020015b60405180910390a150565b610838611996565b60008160a00135116108655760405162461bcd60e51b815260040161085c906136d3565b60405180910390fd5b610876610100820160e08301613717565b6001600160401b03164614156108ce5760405162461bcd60e51b815260206004820152601d60248201527f43616e6e6f742062726964676520746f2073616d65206e6574776f726b000000604482015260640161085c565b6108f6333060a08401356108e5602086018661339a565b6001600160a01b03169291906119f0565b6000610927610908602084018461339a565b60a084013561091d608086016060870161339a565b8560800135611ae2565b9050600061094961093b602085018561339a565b6001600160a01b0316611d58565b905080156109d6576010546001600160a01b0316632972351183610973606087016040880161339a565b610981610140880188613740565b6040518563ffffffff1660e01b815260040161099f939291906137b6565b6000604051808303818588803b1580156109b857600080fd5b505af11580156109cc573d6000803e3d6000fd5b5050505050610ab3565b601054610a05906001600160a01b031660006109f5602087018761339a565b6001600160a01b03169190611d91565b601054610a23906001600160a01b0316836109f5602087018761339a565b6010546001600160a01b031663f9c028ec610a41602086018661339a565b610a51606087016040880161339a565b85610a60610140890189613740565b6040518663ffffffff1660e01b8152600401610a809594939291906137db565b600060405180830381600087803b158015610a9a57600080fd5b505af1158015610aae573d6000803e3d6000fd5b505050505b610ac283600084336001611ee8565b5050610ace6001600955565b50565b6000610ae66001600160a01b03831630611f9c565b92915050565b610af461193c565b600c80546001600160a01b0319166001600160a01b0383169081179091556040519081527f35e63b7fefc2b1fb2ed78adeed6688b187514d96c2d7d56222e52f3e41c2fb2f90602001610825565b610b4a61193c565b60005b81811015610bb357600a6000848484818110610b6b57610b6b61380f565b9050602002016020810190610b80919061339a565b6001600160a01b031681526020810191909152604001600020805460ff1916905580610bab8161383b565b915050610b4d565b505050565b610bc061193c565b601180546001600160a01b0319166001600160a01b0383169081179091556040519081527fb3bfb91ec7a3f9a14278772a47b34685afb9cdd7ef683fab8a790ccdd667264790602001610825565b610c1661193c565b600680546001600160a01b0319166001600160a01b0383169081179091556040519081527f41408be49f75701fe4bb8484ce88d68f1d82e03cb4eb44263b6682ce2dbd32f090602001610825565b610c6c61193c565b600f8190556040518181527f8f817f25bf47abe304b519d80d1a633064758c57d280731736fe82ad20816c5590602001610825565b610ca961193c565b6001600160a01b03166000908152600a60205260409020805460ff19169055565b600b5460405163181da82d60e21b81526001600160a01b03868116600483015285811660248301526044820185905260648201849052600092606092911690636076a0b49060840160006040518083038186803b158015610d2a57600080fd5b505afa158015610d3e573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052610d669190810190613856565b9097909650945050505050565b610d7b611996565b60008160c0013511610d9f5760405162461bcd60e51b815260040161085c906136d3565b806101000135461415610df45760405162461bcd60e51b815260206004820152601d60248201527f43616e6e6f742062726964676520746f2073616d65206e6574776f726b000000604482015260640161085c565b610e0b333060c08401356108e5602086018661339a565b600080610e3d610e1e602085018561339a565b60c0850135610e3360a087016080880161339a565b8660a00135611ae2565b90506000610e51604085016020860161339a565b9050610e63604085016020860161339a565b6001600160a01b0316610e79602086018661339a565b6001600160a01b03161415610e9057819250610ec7565b6000610ea06101a0860186613740565b90501115610eb957610eb2848361203d565b9250610ec7565b610ec384836120a9565b5092505b6000610edc61093b604087016020880161339a565b90508015610f69576010546001600160a01b0316632972351185610f066080890160608a0161339a565b610f146101808a018a613740565b6040518563ffffffff1660e01b8152600401610f32939291906137b6565b6000604051808303818588803b158015610f4b57600080fd5b505af1158015610f5f573d6000803e3d6000fd5b5050505050611016565b601054610f83906001600160a01b038481169116866122ba565b6010546001600160a01b031663f9c028ec610fa4604088016020890161339a565b610fb46080890160608a0161339a565b87610fc36101808b018b613740565b6040518663ffffffff1660e01b8152600401610fe39594939291906137db565b600060405180830381600087803b158015610ffd57600080fd5b505af1158015611011573d6000803e3d6000fd5b505050505b61102585600086336001612395565b50505050610ace6001600955565b6008818154811061104357600080fd5b6000918252602090912001546001600160a01b0316905081565b61106561193c565b61106f6000612413565b565b61107961193c565b600e80546001600160a01b0319166001600160a01b0383169081179091556040519081527f12e1d17016b94668449f97876f4a8d5cc2c19f314db337418894734037cc19d490602001610825565b6110cf61193c565b60048190556040518181527f70f24e12a9db25e0d80cbcde19ffef47d6a7c52c1089db4c71e53ce1856577fc90602001610825565b61110c61193c565b600780546001600160a01b0319166001600160a01b0383169081179091556040519081527f17780f3919f73af11f29e4157534858a06c91294d64b679fe4e49340122cd32290602001610825565b6000611164611996565b8160800135826060013510156111da5760405162461bcd60e51b815260206004820152603560248201527f657870656374656452657475726e206d75737420626520657175616c206f72206044820152743630b933b2b9103a3430b71036b4b72932ba3ab93760591b606482015260840161085c565b6111ea604083016020840161339a565b6001600160a01b0316611200602084018461339a565b6001600160a01b031614156112275760405162461bcd60e51b815260040161085c906138f2565b60008060005b61123b61010086018661393a565b90508110156112b957600061125461010087018761393a565b838181106112645761126461380f565b9050602002013511156112a75761127f61010086018661393a565b8281811061128f5761128f61380f565b90506020020135836112a19190613983565b92508091505b806112b18161383b565b91505061122d565b5081611318576112cf61093b602086018661339a565b1561130b5760405133903480156108fc02916000818181858888f19350505050158015611300573d6000803e3d6000fd5b503492505050611655565b5050506040810135611655565b61132f333060408701356108e5602089018961339a565b6000611360611341602087018761339a565b604087013561135660c0890160a08a0161339a565b8860c00135611ae2565b90506113cc61137361010087018761393a565b808060200260200160405190810160405280939291908181526020018383602002808284376000920191909152508592508791508690506113b760208b018b61339a565b6113c760408c0160208d0161339a565b612463565b935083156115e65784608001358410156114345760405162461bcd60e51b8152602060048201526024808201527f5377697463683a2052657475726e20616d6f756e7420776173206e6f7420656e6044820152630deeaced60e31b606482015260840161085c565b846060013584111561155657611479611454610100870160e0880161339a565b60608701356114696040890160208a0161339a565b6001600160a01b03169190612693565b50600d546114a8906001600160a01b031661149860608801358761399b565b6114696040890160208a0161339a565b50600c546001600160a01b031663391fdc24336114cc610100890160e08a0161339a565b6114d960208a018a61339a565b6114e960408b0160208c0161339a565b60408b013560608c01356114fd818d61399b565b6040518863ffffffff1660e01b815260040161151f97969594939291906139b2565b600060405180830381600087803b15801561153957600080fd5b505af115801561154d573d6000803e3d6000fd5b50505050611651565b61157b61156a610100870160e0880161339a565b856114696040890160208a0161339a565b50600c546001600160a01b031663391fdc243361159f610100890160e08a0161339a565b6115ac60208a018a61339a565b6115bc60408b0160208c0161339a565b8a604001358a60006040518863ffffffff1660e01b815260040161151f97969594939291906139b2565b604085013561160b306115fc602089018961339a565b6001600160a01b031690611f9c565b111561162d57611627336040870135611469602089018961339a565b50611651565b61164f33611642306115fc60208a018a61339a565b611469602089018961339a565b505b5050505b61165f6001600955565b919050565b61166c61193c565b60005b81811015610bb3576001600a600085858581811061168f5761168f61380f565b90506020020160208101906116a4919061339a565b6001600160a01b031681526020810191909152604001600020805460ff1916911515919091179055806116d68161383b565b91505061166f565b6000806127106116ee86856139f6565b6116f89190613a2b565b9150611704828661399b565b9050935093915050565b61171661193c565b600d80546001600160a01b0319166001600160a01b0383169081179091556040519081527f53a596d7be747a5a4f4d39a6a36476d2eed407c93f6f2ba8a96c8b971240d5cd90602001610825565b61176c61193c565b601080546001600160a01b0319166001600160a01b0383169081179091556040519081527fcf00a58401c372fa20037b5f8d3e6ee5ec5ac7c72541f3a06bee9564a361893d90602001610825565b6117c261193c565b8051600360008282546117d59190613983565b90915550600090505b81518110156118485760088282815181106117fb576117fb61380f565b60209081029190910181015182546001810184556000938452919092200180546001600160a01b0319166001600160a01b03909216919091179055806118408161383b565b9150506117de565b507fbbc63a7f378af7e269ef19f3fe0d08f044c91ee72930c0d045a58be58580f3d3816040516108259190613a3f565b61188061193c565b6001600160a01b0381166118e55760405162461bcd60e51b815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201526564647265737360d01b606482015260840161085c565b610ace81612413565b6118f661193c565b61190a6001600160a01b0384168284612693565b50505050565b61191861193c565b6001600160a01b03166000908152600a60205260409020805460ff19166001179055565b6000546001600160a01b0316331461106f5760405162461bcd60e51b815260206004820181905260248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e6572604482015260640161085c565b600260095414156119e95760405162461bcd60e51b815260206004820152601f60248201527f5265656e7472616e637947756172643a207265656e7472616e742063616c6c00604482015260640161085c565b6002600955565b806119fa5761190a565b611a0384611d58565b15611acd576001600160a01b03831633148015611a205750803410155b611a805760405162461bcd60e51b815260206004820152602b60248201527f57726f6e6720757365616765206f66204554482e756e6976657273616c54726160448201526a6e7366657246726f6d282960a81b606482015260840161085c565b6001600160a01b0382163014611ac8576040516001600160a01b0383169082156108fc029083906000818181858888f19350505050158015611ac6573d6000803e3d6000fd5b505b61190a565b61190a6001600160a01b03851684848461270e565b828115611d5057600e54604051637b8c4cdf60e01b81526001600160a01b0385811660048301526000921690637b8c4cdf9060240160206040518083038186803b158015611b2f57600080fd5b505afa158015611b43573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611b679190613a8c565b905080611b735750600f545b6000611b81612710806139f6565b82611b8c88876139f6565b611b9691906139f6565b611ba09190613a2b565b9050600081612710611bb289886139f6565b611bbc9190613a2b565b611bc6919061399b565b9050611bda886001600160a01b0316611d58565b15611c6d57600e546001600160a01b031663eedd56e1611bfa8484613983565b6040516001600160e01b031960e084901b1681526001600160a01b03808d16600483015260248201869052604482018790528a1660648201526084016000604051808303818588803b158015611c4f57600080fd5b505af1158015611c63573d6000803e3d6000fd5b5050505050611d29565b600e54611c88906001600160a01b038a811691166000611d91565b600e54611cb3906001600160a01b0316611ca28484613983565b6001600160a01b038b169190611d91565b600e5460405163eedd56e160e01b81526001600160a01b038a81166004830152602482018490526044820185905288811660648301529091169063eedd56e190608401600060405180830381600087803b158015611d1057600080fd5b505af1158015611d24573d6000803e3d6000fd5b505050505b612710611d3688876139f6565b611d409190613a2b565b611d4a908861399b565b93505050505b949350505050565b60006001600160a01b0382161580610ae657506001600160a01b03821673eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee1492915050565b801580611e1a5750604051636eb1769f60e11b81523060048201526001600160a01b03838116602483015284169063dd62ed3e9060440160206040518083038186803b158015611de057600080fd5b505afa158015611df4573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611e189190613a8c565b155b611e855760405162461bcd60e51b815260206004820152603660248201527f5361666545524332303a20617070726f76652066726f6d206e6f6e2d7a65726f60448201527520746f206e6f6e2d7a65726f20616c6c6f77616e636560501b606482015260840161085c565b6040516001600160a01b038316602482015260448101829052610bb390849063095ea7b360e01b906064015b60408051601f198184030181529190526020810180516001600160e01b03166001600160e01b031990931692909217909152612746565b600c546001600160a01b0316630aea28c76101008701358661012089013586611f1460208c018c61339a565b611f2160208d018d61339a565b611f3160408e0160208f0161339a565b8d60a001358c8f60c001358c6040518c63ffffffff1660e01b8152600401611f639b9a99989796959493929190613aa5565b600060405180830381600087803b158015611f7d57600080fd5b505af1158015611f91573d6000803e3d6000fd5b505050505050505050565b6000611fa783611d58565b15611fbd57506001600160a01b03811631610ae6565b6040516370a0823160e01b81526001600160a01b0383811660048301528416906370a082319060240160206040518083038186803b158015611ffe57600080fd5b505afa158015612012573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906120369190613a8c565b9392505050565b600061203661204f602085018561339a565b61205f604086016020870161339a565b843061206f6101a0890189613740565b8080601f01602080910402602001604051908101604052809392919081815260200183838082843760009201919091525061281b92505050565b60008080805b6120bd61012087018761393a565b905081101561213b5760006120d661012088018861393a565b838181106120e6576120e661380f565b9050602002013511156121295761210161012087018761393a565b828181106121115761211161380f565b90506020020135836121239190613983565b92508091505b806121338161383b565b9150506120af565b506000821161218c5760405162461bcd60e51b815260206004820152601a60248201527f696e76616c696420646973747269627574696f6e20706172616d000000000000604482015260640161085c565b6121e161219d61012087018761393a565b808060200260200160405190810160405280939291908181526020018383602002808284376000920191909152508892508691508590506113b760208b018b61339a565b92506000831161222a5760405162461bcd60e51b81526020600482015260146024820152730a6eec2e040ccc2d2d8cac840cce4deda40c8caf60631b604482015260640161085c565b600c546001600160a01b031663391fdc24333061224a60208a018a61339a565b61225a60408b0160208c0161339a565b898960006040518863ffffffff1660e01b815260040161228097969594939291906139b2565b600060405180830381600087803b15801561229a57600080fd5b505af11580156122ae573d6000803e3d6000fd5b50505050509250929050565b6122c383611d58565b610bb357806122e157610bb36001600160a01b038416836000611d91565b604051636eb1769f60e11b81523060048201526001600160a01b0383811660248301526000919085169063dd62ed3e9060440160206040518083038186803b15801561232c57600080fd5b505afa158015612340573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906123649190613a8c565b90508015612381576123816001600160a01b038516846000611d91565b61190a6001600160a01b0385168484611d91565b600c546001600160a01b0316630aea28c761014087013586610160890135866123c160208c018c61339a565b6123d160408d0160208e0161339a565b6123e160608e0160408f0161339a565b8d60c001358c8f60e001358c6040518c63ffffffff1660e01b8152600401611f639b9a99989796959493929190613aa5565b600080546001600160a01b038381166001600160a01b0319831681178455604051919092169283917f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e09190a35050565b600060045460035461247591906139f6565b875111156124f55760405162461bcd60e51b815260206004820152604160248201527f5377697463683a20446973747269627574696f6e2061727261792073686f756c60448201527f64206e6f742065786365656420666163746f726965732061727261792073697a6064820152606560f81b608482015260a40161085c565b856000805b8951811015612686578981815181106125155761251561380f565b60200260200101516000141561252a57612674565b6000888b838151811061253f5761253f61380f565b60200260200101518b61255291906139f6565b61255c9190613a2b565b9050878214156125695750825b612573818561399b565b9350600454826125839190613b2e565b6125ce576125c787878360086004548761259d9190613a2b565b815481106125ad576125ad61380f565b6000918252602090912001546001600160a01b031661291c565b9250612666565b6004546125db9083613b2e565b60011415612623576125c78787836008600454876125f99190613a2b565b815481106126095761260961380f565b6000918252602090912001546001600160a01b0316612933565b6126638787836008600454876126399190613a2b565b815481106126495761264961380f565b6000918252602090912001546001600160a01b0316612950565b92505b6126708386613983565b9450505b8061267e8161383b565b9150506124fa565b5050509695505050505050565b6000816126a257506001612036565b6126ab84611d58565b156126f0576040516001600160a01b0384169083156108fc029084906000818181858888f193505050501580156126e6573d6000803e3d6000fd5b5060019050612036565b6127046001600160a01b038516848461296d565b5060019392505050565b6040516001600160a01b038085166024830152831660448201526064810182905261190a9085906323b872dd60e01b90608401611eb1565b600061279b826040518060400160405280602081526020017f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c6564815250856001600160a01b031661299d9092919063ffffffff16565b90508051600014806127bc5750808060200190518101906127bc9190613b42565b610bb35760405162461bcd60e51b815260206004820152602a60248201527f5361666545524332303a204552433230206f7065726174696f6e20646964206e6044820152691bdd081cdd58d8d9595960b21b606482015260840161085c565b6000846001600160a01b0316866001600160a01b0316141561284f5760405162461bcd60e51b815260040161085c906138f2565b6000828060200190518101906128659190613b90565b90506128728786836129ac565b6128856001600160a01b03871630611f9c565b915081156128a3576128a16001600160a01b0387168584612693565b505b600c54604051630e47f70960e21b81526001600160a01b039091169063391fdc24906128e090339088908c908c908c908a906000906004016139b2565b600060405180830381600087803b1580156128fa57600080fd5b505af115801561290e573d6000803e3d6000fd5b505050505095945050505050565b600061292a85858585612b0e565b95945050505050565b60065460009061292a9086906001600160a01b0316868686612f7a565b60075460009061292a9086906001600160a01b0316868686612f7a565b6040516001600160a01b038316602482015260448101829052610bb390849063a9059cbb60e01b90606401611eb1565b6060611d508484600085612f9d565b80602001516129ba81613078565b81516129c581613078565b60006129d9866001600160a01b0316611d58565b15612a355784471015612a2e5760405162461bcd60e51b815260206004820152601b60248201527f4554482062616c616e636520697320696e73756666696369656e740000000000604482015260640161085c565b5083612a4f565b6020840151612a4f906001600160a01b03881690876122ba565b600084600001516001600160a01b0316828660400151604051612a729190613c76565b60006040518083038185875af1925050503d8060008114612aaf576040519150601f19603f3d011682016040523d82523d6000602084013e612ab4565b606091505b5050905080612b055760405162461bcd60e51b815260206004820152601f60248201527f4465782041676772656761746f7220657865637574696f6e206661696c656400604482015260640161085c565b50505050505050565b6000612b22856001600160a01b0316611d58565b15612b9157600660009054906101000a90046001600160a01b03166001600160a01b031663d0e30db0846040518263ffffffff1660e01b81526004016000604051808303818588803b158015612b7757600080fd5b505af1158015612b8b573d6000803e3d6000fd5b50505050505b6000612ba5866001600160a01b0316611d58565b612baf5785612bbc565b6006546001600160a01b03165b90506000612bd2866001600160a01b0316611d58565b612bdc5785612be9565b6006546001600160a01b03165b60405163e6a4390560e01b81526001600160a01b038481166004830152808316602483015291925060009186169063e6a439059060440160206040518083038186803b158015612c3857600080fd5b505afa158015612c4c573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612c709190613c92565b9050600080612c8a6001600160a01b03841686868b6130e0565b919750925090508115612cef57826001600160a01b031663fff6cae96040518163ffffffff1660e01b8152600401600060405180830381600087803b158015612cd257600080fd5b505af1158015612ce6573d6000803e3d6000fd5b50505050612d63565b8015612d635760405163bc25cf7760e01b81527346fd07da395799f113a7584563b8cb886f33c2bc60048201526001600160a01b0384169063bc25cf7790602401600060405180830381600087803b158015612d4a57600080fd5b505af1158015612d5e573d6000803e3d6000fd5b505050505b612d776001600160a01b038616848a612693565b50836001600160a01b0316856001600160a01b03161015612e0c5760405163022c0d9f60e01b8152600060048201819052602482018890523060448301526080606483015260848201526001600160a01b0384169063022c0d9f9060a401600060405180830381600087803b158015612def57600080fd5b505af1158015612e03573d6000803e3d6000fd5b50505050612e82565b60405163022c0d9f60e01b8152600481018790526000602482018190523060448301526080606483015260848201526001600160a01b0384169063022c0d9f9060a401600060405180830381600087803b158015612e6957600080fd5b505af1158015612e7d573d6000803e3d6000fd5b505050505b612e94896001600160a01b0316611d58565b15612f6d576006546040516370a0823160e01b81523060048201526001600160a01b0390911690632e1a7d4d9082906370a082319060240160206040518083038186803b158015612ee457600080fd5b505afa158015612ef8573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612f1c9190613a8c565b6040518263ffffffff1660e01b8152600401612f3a91815260200190565b600060405180830381600087803b158015612f5457600080fd5b505af1158015612f68573d6000803e3d6000fd5b505050505b5050505050949350505050565b6000612f938585612f8d89898888612b0e565b85612b0e565b9695505050505050565b606082471015612ffe5760405162461bcd60e51b815260206004820152602660248201527f416464726573733a20696e73756666696369656e742062616c616e636520666f6044820152651c8818d85b1b60d21b606482015260840161085c565b600080866001600160a01b0316858760405161301a9190613c76565b60006040518083038185875af1925050503d8060008114613057576040519150601f19603f3d011682016040523d82523d6000602084013e61305c565b606091505b509150915061306d8783838761327b565b979650505050505050565b6001600160a01b0381166000908152600a602052604090205460ff16610ace5760405162461bcd60e51b815260206004820152601b60248201527f41646472657373206973206e6f7420696e20616c6c6f776c6973740000000000604482015260640161085c565b60008080806130f86001600160a01b03881689611f9c565b9050600061310f6001600160a01b0388168a611f9c565b90506000808a6001600160a01b0316630902f1ac6040518163ffffffff1660e01b815260040160606040518083038186803b15801561314d57600080fd5b505afa158015613161573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906131859190613cc6565b5091509150886001600160a01b03168a6001600160a01b031611156131a657905b816001600160701b03168410806131c55750806001600160701b031683105b9550851580156131ef5750816001600160701b03168411806131ef5750806001600160701b031683115b945060006131ff896103e56139f6565b9050600061321685846001600160701b03166132f1565b61322090836139f6565b905060008261323888876001600160701b03166132f1565b613244906103e86139f6565b61324e9190613983565b90508015613265576132608183613a2b565b613268565b60005b9950505050505050509450945094915050565b606083156132e75782516132e0576001600160a01b0385163b6132e05760405162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e7472616374000000604482015260640161085c565b5081611d50565b611d508383613307565b60008183106133005781612036565b5090919050565b8151156133175781518083602001fd5b8060405162461bcd60e51b815260040161085c9190613d0b565b60006020828403121561334357600080fd5b5035919050565b60006020828403121561335c57600080fd5b81356001600160401b0381111561337257600080fd5b8201610160818503121561203657600080fd5b6001600160a01b0381168114610ace57600080fd5b6000602082840312156133ac57600080fd5b813561203681613385565b600080602083850312156133ca57600080fd5b82356001600160401b03808211156133e157600080fd5b818501915085601f8301126133f557600080fd5b81358181111561340457600080fd5b8660208260051b850101111561341957600080fd5b60209290920196919550909350505050565b6000806000806080858703121561344157600080fd5b843561344c81613385565b9350602085013561345c81613385565b93969395505050506040820135916060013590565b6000604082018483526020604081850152818551808452606086019150828701935060005b818110156134b257845183529383019391830191600101613496565b5090979650505050505050565b6000602082840312156134d157600080fd5b81356001600160401b038111156134e757600080fd5b82016101c0818503121561203657600080fd5b60006020828403121561350c57600080fd5b81356001600160401b0381111561352257600080fd5b8201610120818503121561203657600080fd5b60008060006060848603121561354a57600080fd5b83359250602084013561355c81613385565b929592945050506040919091013590565b634e487b7160e01b600052604160045260246000fd5b604051606081016001600160401b03811182821017156135a5576135a561356d565b60405290565b604051601f8201601f191681016001600160401b03811182821017156135d3576135d361356d565b604052919050565b60006001600160401b038211156135f4576135f461356d565b5060051b60200190565b6000602080838503121561361157600080fd5b82356001600160401b0381111561362757600080fd5b8301601f8101851361363857600080fd5b803561364b613646826135db565b6135ab565b81815260059190911b8201830190838101908783111561366a57600080fd5b928401925b8284101561306d57833561368281613385565b8252928401929084019061366f565b6000806000606084860312156136a657600080fd5b83356136b181613385565b92506020840135915060408401356136c881613385565b809150509250925092565b60208082526024908201527f54686520616d6f756e74206d7573742062652067726561746572207468616e206040820152637a65726f60e01b606082015260800190565b60006020828403121561372957600080fd5b81356001600160401b038116811461203657600080fd5b6000808335601e1984360301811261375757600080fd5b8301803591506001600160401b0382111561377157600080fd5b60200191503681900382131561378657600080fd5b9250929050565b81835281816020850137506000828201602090810191909152601f909101601f19169091010190565b6001600160a01b038416815260406020820181905260009061292a908301848661378d565b6001600160a01b038681168252851660208201526040810184905260806060820181905260009061306d908301848661378d565b634e487b7160e01b600052603260045260246000fd5b634e487b7160e01b600052601160045260246000fd5b600060001982141561384f5761384f613825565b5060010190565b6000806040838503121561386957600080fd5b825191506020808401516001600160401b0381111561388757600080fd5b8401601f8101861361389857600080fd5b80516138a6613646826135db565b81815260059190911b820183019083810190888311156138c557600080fd5b928401925b828410156138e3578351825292840192908401906138ca565b80955050505050509250929050565b60208082526028908201527f69742773206e6f7420616c6c6f77656420746f2073776170207769746820736160408201526736b2903a37b5b2b760c11b606082015260800190565b6000808335601e1984360301811261395157600080fd5b8301803591506001600160401b0382111561396b57600080fd5b6020019150600581901b360382131561378657600080fd5b6000821982111561399657613996613825565b500190565b6000828210156139ad576139ad613825565b500390565b6001600160a01b03978816815295871660208701529386166040860152919094166060840152608083019390935260a082019290925260c081019190915260e00190565b6000816000190483118215151615613a1057613a10613825565b500290565b634e487b7160e01b600052601260045260246000fd5b600082613a3a57613a3a613a15565b500490565b6020808252825182820181905260009190848201906040850190845b81811015613a805783516001600160a01b031683529284019291840191600101613a5b565b50909695505050505050565b600060208284031215613a9e57600080fd5b5051919050565b8b8152602081018b9052604081018a90526001600160a01b038981166060830152888116608083015287811660a0830152861660c082015260e0810185905261010081018490526101208101839052610160810160048310613b1757634e487b7160e01b600052602160045260246000fd5b826101408301529c9b505050505050505050505050565b600082613b3d57613b3d613a15565b500690565b600060208284031215613b5457600080fd5b8151801515811461203657600080fd5b60005b83811015613b7f578181015183820152602001613b67565b8381111561190a5750506000910152565b60006020808385031215613ba357600080fd5b82516001600160401b0380821115613bba57600080fd5b9084019060608287031215613bce57600080fd5b613bd6613583565b8251613be181613385565b815282840151613bf081613385565b81850152604083015182811115613c0657600080fd5b80840193505086601f840112613c1b57600080fd5b825182811115613c2d57613c2d61356d565b613c3f601f8201601f191686016135ab565b92508083528785828601011115613c5557600080fd5b613c6481868501878701613b64565b50604081019190915295945050505050565b60008251613c88818460208701613b64565b9190910192915050565b600060208284031215613ca457600080fd5b815161203681613385565b80516001600160701b038116811461165f57600080fd5b600080600060608486031215613cdb57600080fd5b613ce484613caf565b9250613cf260208501613caf565b9150604084015163ffffffff811681146136c857600080fd5b6020815260008251806020840152613d2a816040850160208701613b64565b601f01601f1916919091016040019291505056fea2646970667358221220f4baf152af8d10a145ffb4913f3a0ec94d226100a0a50fc8062d7ef98a247c9464736f6c63430008090033

Verified Source Code Full Match

Compiler: v0.8.9+commit.e5eed63a EVM: london Optimization: Yes (200 runs)
SwitchOrbiter.sol 293 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.9;

import { IOrbiterRouter } from "../interfaces/IOrbiterRouter.sol";
import "../lib/DataTypes.sol";
import "../dexs/SwitchV2.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";

contract SwitchOrbiter is SwitchV2 {
    using UniversalERC20 for IERC20;
    using SafeERC20 for IERC20;
    address public orbiterRouter;
    address public nativeWrap;

    struct TransferArgsOrbiter {
        address fromToken;
        address destToken;
        address orbiterMaker;
        address partner;
        uint256 partnerFeeRate;
        uint256 amount;
        uint256 estimatedDstTokenAmount;
        uint64 dstChainId;
        bytes32 id;
        bytes32 bridge;
        bytes orbiterData;
    }

    struct SwapArgsOrbiter {
        address fromToken;
        address bridgeToken;
        address destToken;
        address orbiterMaker;
        address partner;
        uint256 partnerFeeRate;
        uint256 amount;
        uint256 estimatedDstTokenAmount;
        uint256 dstChainId;
        uint256[] srcDistribution;
        bytes32 id;
        bytes32 bridge;
        bytes orbiterData;
        bytes aggregatorInfo;
    }

    event OrbiterRouterSet(address orbiterRouter);
    event NativeWrapSet(address _nativeWrap);

    constructor(
        address _weth,
        address _otherToken,
        uint256[] memory _pathCountAndSplit,
        address[] memory _factories,
        address _switchViewAddress,
        address _switchEventAddress,
        address _orbiterRouter,
        address _feeCollector
    ) SwitchV2(
        _weth,
        _otherToken,
        _pathCountAndSplit[0],
        _pathCountAndSplit[1],
        _factories,
        _switchViewAddress,
        _switchEventAddress,
        _feeCollector
    )
        public
    {
        orbiterRouter = _orbiterRouter;
        nativeWrap = _weth;
    }

    function setOrbiterRouter(address _orbiterRouter) external onlyOwner {
        orbiterRouter = _orbiterRouter;
        emit OrbiterRouterSet(_orbiterRouter);
    }

    function setNativeWrap(address _newNativeWrap) external onlyOwner {
        nativeWrap = _newNativeWrap;
        emit NativeWrapSet(nativeWrap);
    }

    function transferByOrbiter(
        TransferArgsOrbiter 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();
        if (isNative) {
            IOrbiterRouter(orbiterRouter).transfer{ value: amountAfterFee }(
                transferArgs.orbiterMaker,
                transferArgs.orbiterData
            );
        } else {
            // Give Orbiter bridge approval
            IERC20(transferArgs.fromToken).safeApprove(orbiterRouter, 0);
            IERC20(transferArgs.fromToken).safeApprove(orbiterRouter, amountAfterFee);

            IOrbiterRouter(orbiterRouter).transferToken(
                IERC20(transferArgs.fromToken),
                transferArgs.orbiterMaker,
                amountAfterFee,
                transferArgs.orbiterData
            );
        }

        _emitCrossChainTransferRequest(
            transferArgs,
            bytes32(0),
            amountAfterFee,
            msg.sender,
            DataTypes.SwapStatus.Succeeded
        );
    }

    function swapByOrbiter(
        SwapArgsOrbiter 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;
        if (swapArgs.fromToken == swapArgs.bridgeToken) {
            returnAmount = amountAfterFee;
        } else {
            if (swapArgs.aggregatorInfo.length > 0) {
                returnAmount = _swapThruAggregator(swapArgs, amountAfterFee);
            } else {
                (returnAmount, ) = _swapBeforeOrbiter(swapArgs, amountAfterFee);
            }
        }

        bool isNativeBridgeToken = IERC20(swapArgs.bridgeToken).isETH();

        if (isNativeBridgeToken) {
            IOrbiterRouter(orbiterRouter).transfer{ value: returnAmount }(
                swapArgs.orbiterMaker,
                swapArgs.orbiterData
            );
        } else {
            IERC20(bridgeToken).universalApprove(orbiterRouter, returnAmount);
            IOrbiterRouter(orbiterRouter).transferToken(
                IERC20(swapArgs.bridgeToken),
                swapArgs.orbiterMaker,
                returnAmount,
                swapArgs.orbiterData
            );
        }

        _emitCrossChainSwapRequest(
            swapArgs,
            bytes32(0),
            returnAmount,
            msg.sender,
            DataTypes.SwapStatus.Succeeded
        );
    }

    function _swapBeforeOrbiter(
        SwapArgsOrbiter 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(
        SwapArgsOrbiter 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(
        TransferArgsOrbiter 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(
        SwapArgsOrbiter 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
        );
    }
}
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);
    }
}
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;
    }
}
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);
}
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);
}
SafeERC20.sol 143 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.3) (token/ERC20/utils/SafeERC20.sol)

pragma solidity ^0.8.0;

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

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

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

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

    /**
     * @dev Deprecated. This function has issues similar to the ones found in
     * {IERC20-approve}, and its usage is discouraged.
     *
     * Whenever possible, use {safeIncreaseAllowance} and
     * {safeDecreaseAllowance} instead.
     */
    function safeApprove(IERC20 token, address spender, uint256 value) internal {
        // safeApprove should only be called when setting an initial allowance,
        // or when resetting it to zero. To increase and decrease it, use
        // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
        require(
            (value == 0) || (token.allowance(address(this), spender) == 0),
            "SafeERC20: approve from non-zero to non-zero allowance"
        );
        _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
    }

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

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

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

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

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

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

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

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

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

pragma solidity ^0.8.1;

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

        return account.code.length > 0;
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

pragma solidity ^0.8.0;

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

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

    function _contextSuffixLength() internal view virtual returns (uint256) {
        return 0;
    }
}
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");
    }

}
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
        );
}

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;
}

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();
}
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
        );
    }

}
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);
}

IOrbiterRouter.sol 19 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.9;

import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

// https://arbiscan.io/address/0x6a065083886ec63d274b8e1fe19ae2ddf498bfdd#code
interface IOrbiterRouter {
    function transferToken(
        IERC20 token,
        address to,
        uint value,
        bytes calldata data
    ) external payable;

    function transfer(
        address to,
        bytes calldata data
    ) external payable;
}
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;
}

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;
}
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);
}
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;
    }
}
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;
    }
}
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);
    }
}
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;
    }
}



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));
    // }
}

Read Contract

ETH_ADDRESS 0xa734f06e → address
FEE_BASE 0xecefc705 → uint256
ZERO_ADDRESS 0x538ba4f9 → address
addressInAllowlist 0xe772ff37 → bool
allowlist 0xa7cd52cb → bool
defaultSwingCut 0x52b721f8 → uint256
dexCount 0xab304695 → uint256
factories 0x672383c4 → address
feeCollector 0xc415b95c → address
getExpectedReturn 0x6076a0b4 → uint256, uint256[]
getFeeInfo 0xd6821ed8 → uint256, uint256
getTokenBalance 0x3aecd0e3 → uint256
nativeWrap 0x457bfa2f → address
orbiterRouter 0x027c946e → 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 21 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
setOrbiterRouter 0xe88943e2
address _orbiterRouter
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
swapByOrbiter 0xc90f3ae7
tuple swapArgs
transferByOrbiter 0xf0525878
tuple transferArgs
transferOwnership 0xf2fde38b
address newOwner
transferToken 0xf640d508
address token
uint256 amount
address recipient

Recent Transactions

No transactions found for this address