Cryo Explorer Ethereum Mainnet

Address Contract Verified

Address 0x71C100E15473d02456C6EC1C36dd28A175b5205e
Balance 0.130100 ETH
Nonce 1
Code Size 9921 bytes
Indexed Transactions 0
External Etherscan · Sourcify

Contract Bytecode

9921 bytes
0x6080604081815260049182361015610075575b5090361561001f57600080fd5b6000546001600160a01b0316330361003357005b6020606492519162461bcd60e51b8352820152601760248201527f4f6e6c79206f776e65722063616e2073656e64204554480000000000000000006044820152fd5b600090813560e01c9081630f97d37c14611d07575080631459457a14611a12578063158274a5146119e95780632301d775146119c057806343ab265f146119935780634623c81e14611975578063584b236c14611956578063599d9c6e146118ea5780636375184a14610dc55780636d278f7d146118cb5780637341ab8c146118ac578063735de9f7146118845780637c0d3f43146117785780637ebdfec41461148457806384919e211461145657806386001519146114375780638da5cb5b1461140f5780638fdbb073146113d1578063920f5c8414610ee75780639d7f7e8614610eca578063a03e4bc314610ea1578063a405293e14610dca578063b38ab63e14610dc5578063bf04820b14610da6578063bfda2c0214610a2a578063c0286465146109fc578063c0c7347e146109dd578063d55be8c6146109c1578063d5eb63cc14610906578063d73792a9146108e9578063da2ca9b51461082c578063e275c9971461080d578063e3bbb4f1146107ed578063f04f270714610325578063f609844d14610301578063fc213996146102e2578063fd829a23146102b55763ff083a970361001257346102b257806003193601126102b25760075491600854600954600a5490600b549360ff600c541695871560001461027d5760e09850935b815197885260208801528601526060850152608084015260a0830152151560c0820152f35b6127109081860291868304148615171561029f575060e0985087900493610258565b634e487b7160e01b815260118a52602490fd5b80fd5b5090346102de57816003193601126102de5760055490516001600160a01b039091168152602090f35b5080fd5b5090346102de57816003193601126102de576020906008549051908152f35b5090346102de57816003193601126102de5760209060ff600c541690519015158152f35b50346102b25760803660031901126102b25767ffffffffffffffff9083358281116102de576103579036908601611e53565b60249291929182358581116102de576103739036908901611e53565b9390926044358781116107e95761038d9036908b01611e53565b976064359081116107e5576103a59036908c01611dde565b6002546001600160a01b0399908a16979290913389900361078f57816103d1918c969594930190612274565b50509416941561077d576103e4906122fa565b971561076b5735981561075957906104139135923381148015610742575b61040c915061230e565b8814612368565b806106f15786610422866123ce565b106106af576104308761265a565b9461044461043d886123ce565b938a6123b4565b9384841061065c5760208b8b7fa45d4f78091e6f095726d8b8793c13de0e5537f3c45f48c54dcfe9ddb2c9b6ac60e08d8d8d8d8d8d8d610485838f886124ca565b80610653575b156105af576104b99261049f60085461224f565b6008556104ae856009546123b4565b600955600b556123c1565b80610509575b505b5a908751968752888701528686015260a06060860152601260a08601527142616c616e63657256325f5a65726f46656560701b60c08601524260808601521692a25160018152f35b61051881856001541685612564565b83600154167f21a997b413fbbfd1fe7fd3613b00f6e5dfee6ba46ea8ac57238242db6308a8958a8a51848152a287519081526060898201527fa4cfbeb3403e790fc2195c2a15913d353f80fbbb8d60ca6c90658e9df4c002c461059b60608301604090600f81526e42616c616e6365725a65726f46656560881b60208201520190565b91428a82015280868616930390a2886104bf565b505050865160608152602160608201527f4e6f2070726f6669742066726f6d207a65726f2d6665652061726269747261676080820152606560f81b60a082015260c0898201527f60bd5ca5a3f2b9d56333ddd2030529be3b64837989f8e610d1d1990bf327f33761064060c08301604090600f81526e42616c616e6365725a65726f46656560881b60208201520190565b91428a82015280868616930390a26104c1565b5083151561048b565b8b602860849260208e519362461bcd60e51b85528401528201527f496e73756666696369656e742062616c616e636520746f20726570617920666c60448201526730b9b4103637b0b760c11b6064820152fd5b875162461bcd60e51b81526020818b01819052818401527f446964206e6f74207265636569766520666c617368206c6f616e2066756e64736044820152606490fd5b875162461bcd60e51b81526020818b01526026818401527f42616c616e63657220666c617368206c6f616e732073686f756c64206861766560448201526520302066656560d01b6064820152608490fd5b508552600660205261040c60ff8b87205416610402565b634e487b7160e01b855260328b528385fd5b634e487b7160e01b865260328c528486fd5b634e487b7160e01b875260328d528587fd5b60848e602a8f60208b91519362461bcd60e51b85528401528201527f4f6e6c792042616c616e636572207661756c742063616e2063616c6c207468696044820152693990333ab731ba34b7b760b11b6064820152fd5b8480fd5b8380fd5b5090346102de57816003193601126102de57602090516422ecb25c008152f35b5090346102de57816003193601126102de576020906007549051908152f35b509190346108e55760203660031901126108e557610848611d91565b83546001600160a01b03929183916108639083163314611f10565b169283156108b157507fbc46500cb7d71ecfcae063c67ff904b7d2a491327f0082726b1e2ee0b5fd8160906001549281519084168152846020820152a16001600160a01b0319161760015580f35b6020606492519162461bcd60e51b8352820152600f60248201526e496e76616c6964206164647265737360881b6044820152fd5b8280fd5b5090346102de57816003193601126102de57602090516127108152f35b5090346102de57816003193601126102de5761092c60018060a01b038354163314611f10565b600c5460ff8082161516809160ff191617600c55600014610995576060817026b0b73ab0b61030b1ba34bb30ba34b7b760791b7f67e9cb9e5fa49e813134d1acdaf22c09efe0f3e77eb6ab2fdf1036e075fe55249351916020835260116020840152820152a180f35b5080600b557ff107f1c40ab414f2adfe8736de1bda51320811b925d0307780049e27c3aefcf78180a180f35b5090346102de57816003193601126102de576020905160648152f35b5090346102de57816003193601126102de57602090600e549051908152f35b5082346102de5760203660031901126102de57610a2360018060a01b038354163314611f10565b35600d5580f35b509190346108e557610a3b36611e0c565b90919392933387526020946006865260ff85892054168015610d93575b610a6190611f66565b60ff600c54161592610a7284611faa565b610a836422ecb25c003a1115611feb565b6001600160a01b0391821693610a9a85151561202c565b610aaf841591610aaa8315612078565b6120c4565b67016345785d8a00008410610d345760098402908482046009141715610d0e5762061a80903a82029182043a03610d215790612710610aee92046123b4565b607881029080820460781490151715610d0e57898751610b0d81611eb6565b5260646105dc850491048110159081610d04575b5015610caf578894928594928592885194610b3b86611e84565b600186528a3681880137610b4e86612109565b52885192610b5b84611e84565b600184528a368186013780610b6f85612109565b528a8a5192610b7d84611e84565b6001845281368186013786610b9185612109565b528b5198899342903394860194610ba79561212c565b0391601f19928381018852610bbc9088611ed2565b600354169589519384928c84019763ab9c4b5d60e01b89523060248601526044850160e090526101048501610bf09161216e565b916023199283868203016064870152610c08916121ab565b82858203016084860152610c1b916121ab565b903060a48501528382030160c4840152610c34916121df565b8460e4830152039081018252610c4a9082611ed2565b51925af1610c5661221f565b5015610c6e5783610c6860075461224f565b60075580f35b5162461bcd60e51b815291820181905260248201527f4161766520666c617368206c6f616e20657865637574696f6e206661696c6564604482015260649150fd5b855162461bcd60e51b8152808901889052602960248201527f4e6f2070726f66697461626c65206f70706f7274756e6974792061667465722060448201526841617665206665657360b81b6064820152608490fd5b9050151538610b21565b634e487b7160e01b8a526011895260248afd5b634e487b7160e01b8b5260118a5260248bfd5b865162461bcd60e51b8152808a01899052603360248201527f416d6f756e742062656c6f77206d696e696d756d20666f722041617665206665604482015272652d706179696e67206f7065726174696f6e7360681b6064820152608490fd5b5087546001600160a01b03163314610a58565b5090346102de57816003193601126102de57602090600a549051908152f35b611dc2565b5090346102de57806003193601126102de57610de4611d91565b506024356009810281810460091482151715610e8e5762061a80903a82029182043a03610e7b5790612710610e1992046123b4565b92607884029380850460781490151715610e6857916105dc610e6492606495948351610e4481611eb6565b520490519384930481101583602090939291936040810194151581520152565b0390f35b634e487b7160e01b815260118552602490fd5b634e487b7160e01b855260118652602485fd5b634e487b7160e01b845260118552602484fd5b5090346102de57816003193601126102de5760035490516001600160a01b039091168152602090f35b5090346102de57816003193601126102de5760209051610e108152f35b50346102b25760a03660031901126102b25767ffffffffffffffff9083358281116102de57610f199036908601611e53565b929060249384358381116107e557610f349036908901611e53565b95906044358581116113cd57610f4d9036908b01611e53565b95610f56611dac565b506084359081116113c957610f6e9036908c01611dde565b6003546001600160a01b039790881694929091338690036113785781610f9a918a969594930190612274565b50509416961561136657610fad906122fa565b9915611354573596156113425790610fdc913594338114801561132b575b610fd5915061230e565b8614612368565b84610fe6886123ce565b106112d657610ff48561245b565b9690610fff896123ce565b9261100a86896123b4565b948585106112845760208c8c7fa45d4f78091e6f095726d8b8793c13de0e5537f3c45f48c54dcfe9ddb2c9b6ac8d8d8d8d8d8d8d8d61104a848f8c6124ca565b8061127b575b1561116457611074935061106560085461224f565b6008556104ae876009546123b4565b806110c7575b505b5a86519384938452888401528683015260a060608301526110b460a08301604090600681526541617665563360d01b60208201520190565b9442608084015216930390a25160018152f35b6110d681836001541688612564565b81600154167f21a997b413fbbfd1fe7fd3613b00f6e5dfee6ba46ea8ac57238242db6308a895898951848152a286519081526060888201527fa4cfbeb3403e790fc2195c2a15913d353f80fbbb8d60ca6c90658e9df4c002c461115060608301604090600681526541617665563360d01b60208201520190565b91428982015280848916930390a28761107a565b50505061117390600a546123b4565b600a556005611183600b5461224f565b80600b55875160608152601960608201527f4e6f2070726f6669742061667465722041617665206665657300000000000000608082015260a08a8201527f60bd5ca5a3f2b9d56333ddd2030529be3b64837989f8e610d1d1990bf327f33761120260a08301604090600681526541617665563360d01b60208201520190565b91428b82015280868b16930390a21061107c57600160ff19600c541617600c557f67e9cb9e5fa49e813134d1acdaf22c09efe0f3e77eb6ab2fdf1036e075fe552460608751898152601d8a8201527f546f6f206d616e7920636f6e7365637574697665206661696c7572657300000089820152a161107c565b50861515611050565b8c602760849260208f519362461bcd60e51b85528401528201527f496e73756666696369656e742062616c616e636520746f2072657061792041616044820152663b32903637b0b760c91b6064820152fd5b875162461bcd60e51b81526020818b0152602a818401527f446964206e6f74207265636569766520666c617368206c6f616e2066756e64736044820152692066726f6d204161766560b01b6064820152608490fd5b5088526006602052610fd560ff8b8a205416610fcb565b634e487b7160e01b885260328b528388fd5b634e487b7160e01b895260328c528489fd5b634e487b7160e01b8a5260328d52858afd5b60848e60258f60208b91519362461bcd60e51b85528401528201527f4f6e6c79204161766520706f6f6c2063616e2063616c6c20746869732066756e60448201526431ba34b7b760d91b6064820152fd5b8780fd5b8680fd5b5090346102de5760203660031901126102de5760209160ff9082906001600160a01b036113fc611d91565b1681526006855220541690519015158152f35b5090346102de57816003193601126102de57905490516001600160a01b039091168152602090f35b5090346102de57816003193601126102de576020906009549051908152f35b5082346102de5760203660031901126102de5761147d60018060a01b038354163314611f10565b35600f5580f35b509190346108e55761149536611e0c565b909193923387526020946006865260ff85892054168015611765575b6114ba90611f66565b60ff600c541615926114cb84611faa565b6114dc6422ecb25c003a1115611feb565b6001600160a01b0392831693611502906114f786151561202c565b610aaa841515612078565b66038d7ea4c68000821061170457620557303a81029081043a03610d0e5763032116203a029080820460961490151715610d0e5789875161154281611eb6565b5260646105dc8404910481101590816116fa575b50156116a0576116076116358a9694896116298a8a99976115c28b9883519a61157e8c611e84565b60018c528636818e01376115918c612109565b5283519361159e85611e84565b60018552863681870137836115b286612109565b52519b8c9342903389870161212c565b03936115d6601f19958681018c528b611ed2565b600254169861161a8d5197889586019a632e1c224f60e11b8c523060248801526080604488015260a487019061216e565b60231993848783030160648801526121ab565b918483030160848501526121df565b03908101835282611ed2565b51925af161164161221f565b50156116535783610c6860075461224f565b5162461bcd60e51b8152918201526024808201527f42616c616e63657220666c617368206c6f616e20657865637574696f6e2066616044820152631a5b195960e21b606482015260849150fd5b855162461bcd60e51b8152808901889052602e60248201527f4e6f2070726f66697461626c65206f70706f7274756e6974792061667465722060448201526d67617320636f737473206f6e6c7960901b6064820152608490fd5b9050151538611556565b855162461bcd60e51b8152808901889052603560248201527f416d6f756e742062656c6f77206d696e696d756d20666f72207a65726f2d6665604482015274652042616c616e636572206f7065726174696f6e7360581b6064820152608490fd5b5087546001600160a01b031633146114b1565b509190346108e557806003193601126108e557611793611d91565b60243590811515908183036118805785546001600160a01b0391906117bb9083163314611f10565b1693841561183d575083855260066020528285209060ff80198354169116179055600014611810577f5306e71ac264d8d9b9a30293cd54640819975a8cecd6ecc6b42d7d4b0d34da499160209151908152a180f35b7f90735889e886ae0281a27877dde978b5bb1ee18692b23268e202e20d4fb3787d9160209151908152a180f35b606490602085519162461bcd60e51b8352820152601f60248201527f426f72726f7765722063616e6e6f74206265207a65726f2061646472657373006044820152fd5b8580fd5b5091346108e557826003193601126108e5575490516001600160a01b03909116815260209150f35b5090346102de57816003193601126102de57602090600d549051908152f35b5090346102de57816003193601126102de57602090600f549051908152f35b5091346108e55760203660031901126108e55780359161191460018060a01b038554163314611f10565b60648311611924575050600e5580f35b906020606492519162461bcd60e51b8352820152600c60248201526b08ccaca40e8dede40d0d2ced60a31b6044820152fd5b5090346102de57816003193601126102de57602090600b549051908152f35b5090346102de57816003193601126102de5760209051620151808152f35b5090346102de5760203660031901126102de576020906119b96119b4611d91565b6123ce565b9051908152f35b5090346102de57816003193601126102de5760015490516001600160a01b039091168152602090f35b5090346102de57816003193601126102de5760025490516001600160a01b039091168152602090f35b5090346102de5760a03660031901126102de57611a2d611d91565b602493906001600160a01b03853581811690819003611880576044938435948386168096036113c957611a5e611dac565b9560843594808616809603611d0357808a541696873303611cc1578116978815611c73578515611c23578215611be15716918215611b92578515611b4157507fbc46500cb7d71ecfcae063c67ff904b7d2a491327f0082726b1e2ee0b5fd81609899506bffffffffffffffffffffffff60a01b9387856001541617600155846002541617600255836003541617600355828254161790556005541617600555655af3107a4000600d5584600e5584600f5560ff199081600c5416600c5585600b55855260066020526001838620918254161790558151908482526020820152a180f35b837f53757368695377617020726f757465722063616e6e6f74206265207a65726f2060849260278e60208e519562461bcd60e51b8752860152840152820152666164647265737360c81b6064820152fd5b837f556e697377617020726f757465722063616e6e6f74206265207a65726f20616460849260258e60208e519562461bcd60e51b875286015284015282015264647265737360d81b6064820152fd5b50885162461bcd60e51b81526020818601819052818d01527f4161766520706f6f6c2063616e6e6f74206265207a65726f206164647265737381850152606490fd5b50885162461bcd60e51b81526020818601526025818d01527f42616c616e636572207661756c742063616e6e6f74206265207a65726f2061648185015264647265737360d81b6064820152608490fd5b895162461bcd60e51b8152602081870152808d018d90527f50726f6669742077616c6c65742063616e6e6f74206265207a65726f2061646481860152637265737360e01b6064820152608490fd5b50885162461bcd60e51b81526020818601526019818d01527f4f6e6c79206f776e65722063616e20696e697469616c697a650000000000000081850152606490fd5b8980fd5b92919050346102b257816003193601126102b257611d23611d91565b5062055730923a84029384043a03611d7e5763032116203a029380850460961490151715611d7e5791610e649183611d5d60649695611eb6565b5251929091046105dc60243504908110158352602083015281906040820190565b634e487b7160e01b825260118552602482fd5b600435906001600160a01b0382168203611da757565b600080fd5b606435906001600160a01b0382168203611da757565b34611da7576000366003190112611da757602060405160058152f35b9181601f84011215611da75782359167ffffffffffffffff8311611da75760208381860195010111611da757565b6060600319820112611da7576004356001600160a01b0381168103611da75791602435916044359067ffffffffffffffff8211611da757611e4f91600401611dde565b9091565b9181601f84011215611da75782359167ffffffffffffffff8311611da7576020808501948460051b010111611da757565b6040810190811067ffffffffffffffff821117611ea057604052565b634e487b7160e01b600052604160045260246000fd5b6020810190811067ffffffffffffffff821117611ea057604052565b90601f8019910116810190811067ffffffffffffffff821117611ea057604052565b67ffffffffffffffff8111611ea057601f01601f191660200190565b15611f1757565b60405162461bcd60e51b815260206004820152602160248201527f4f6e6c79206f776e65722063616e2063616c6c20746869732066756e6374696f6044820152603760f91b6064820152608490fd5b15611f6d57565b60405162461bcd60e51b81526020600482015260156024820152742ab730baba3437b934bd32b2103137b93937bbb2b960591b6044820152606490fd5b15611fb157565b60405162461bcd60e51b815260206004820152601260248201527110dbdb9d1c9858dd081a5cc81c185d5cd95960721b6044820152606490fd5b15611ff257565b60405162461bcd60e51b815260206004820152601260248201527108ec2e640e0e4d2c6ca40e8dede40d0d2ced60731b6044820152606490fd5b1561203357565b60405162461bcd60e51b815260206004820152601c60248201527f41737365742063616e6e6f74206265207a65726f2061646472657373000000006044820152606490fd5b1561207f57565b60405162461bcd60e51b815260206004820152601d60248201527f416d6f756e74206d7573742062652067726561746572207468616e20300000006044820152606490fd5b156120cb57565b60405162461bcd60e51b81526020600482015260166024820152754369726375697420627265616b65722061637469766560501b6044820152606490fd5b8051156121165760200190565b634e487b7160e01b600052603260045260246000fd5b93909285939260a09693600180891b031686526020860152604085015260806060850152816080850152848401376000828201840152601f01601f1916010190565b90815180825260208080930193019160005b82811061218e575050505090565b83516001600160a01b031685529381019392810192600101612180565b90815180825260208080930193019160005b8281106121cb575050505090565b8351855293810193928101926001016121bd565b919082519283825260005b84811061220b575050826000602080949584010152601f8019910116010190565b6020818301810151848301820152016121ea565b3d1561224a573d9061223082611ef4565b9161223e6040519384611ed2565b82523d6000602084013e565b606090565b600019811461225e5760010190565b634e487b7160e01b600052601160045260246000fd5b608081830312611da75780356001600160a01b0381168103611da7579260208201359260408301359260608101359067ffffffffffffffff8211611da7570181601f82011215611da7578035906122ca82611ef4565b926122d86040519485611ed2565b82845260208383010111611da757816000926020809301838601378301015290565b356001600160a01b0381168103611da75790565b1561231557565b60405162461bcd60e51b815260206004820152602560248201527f556e617574686f72697a656420696e69746961746f7220666f7220666c617368604482015264103637b0b760d91b6064820152608490fd5b1561236f57565b60405162461bcd60e51b815260206004820152601a60248201527f466c617368206c6f616e20616d6f756e74206d69736d617463680000000000006044820152606490fd5b9190820180921161225e57565b9190820391821161225e57565b6001600160a01b0381166123e157504790565b6040519060208201906370a0823160e01b825230602484015260248352606083019183831067ffffffffffffffff841117611ea057600093849360405251915afa61242a61221f565b908061244f575b61243b5750600090565b602081805181010312611da7576020015190565b50602081511015612431565b600981028181046009148215171561225e57612710900462061a80913a83029283043a0361225e576105dc90049061249383826123b4565b8211156124c0576124ad926124a7916123b4565b906123c1565b806124bb5750600090600090565b600191565b50505060006124ad565b60405163095ea7b360e01b602082019081526001600160a01b0390931660248201526044810193909352600092839290839061251381606481015b03601f198101835282611ed2565b51925af161251f61221f565b501561252757565b60405162461bcd60e51b8152602060048201526015602482015274151bdad95b88185c1c1c9bdd985b0819985a5b1959605a1b6044820152606490fd5b60009283926001600160a01b03928084166125cd57509083928392165af161258a61221f565b501561259257565b60405162461bcd60e51b8152602060048201526013602482015272115512081d1c985b9cd9995c8819985a5b1959606a1b6044820152606490fd5b60405163a9059cbb60e01b602082019081526001600160a01b039093166024820152604481019390935292509083906126098160648101612505565b51925af161261561221f565b501561261d57565b60405162461bcd60e51b8152602060048201526015602482015274151bdad95b881d1c985b9cd9995c8819985a5b1959605a1b6044820152606490fd5b62055730903a82029182043a0361225e576105dc900490808211612682575050600090600090565b6124bb916123c156fea26469706673582212206ec006c27719225b9b1d38c15e75b3d0d0d89ebaa04e3bc571991640ce883b4764736f6c63430008160033

Verified Source Code Full Match

Compiler: v0.8.22+commit.4fc1097e EVM: paris Optimization: Yes (200 runs)
EnhancedZeroFeeBalancerFlashLoanArbitrage.sol 623 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

/**
 * @title EnhancedZeroFeeBalancerFlashLoanArbitrage
 * @notice Production flash loan contract optimized for zero-fee Balancer integration
 * @dev This contract prioritizes zero-fee Balancer flash loans to maximize profitability,
 *      with a fallback to Aave flash loans if needed.
 */
contract EnhancedZeroFeeBalancerFlashLoanArbitrage {
    // Contract addresses
    address public owner;
    address public profitWallet;
    address public balancerVault;  // CRITICAL: Zero-fee Balancer Vault
    address public aavePool;       // Aave Pool with fees (backup)
    address public uniswapRouter;
    address public sushiSwapRouter;

    // Authorization and state variables
    mapping(address => bool) public authorizedBorrowers;
    
    // Metrics
    uint256 public totalTrades;
    uint256 public successfulTrades;
    uint256 public totalProfit;
    uint256 public totalLosses;
    uint256 public consecutiveFailures;
    bool public circuitBreakerActive;

    // Constants
    uint256 public constant FEE_DENOMINATOR = 10000;
    uint256 public constant MAX_FEE_BPS = 100;
    uint256 public constant MIN_PROFIT_THRESHOLD_BPS = 5; // Lowered for zero-fee operations
    uint256 public constant MAX_GAS_PRICE = 150 gwei;
    uint256 public constant MAX_CONSECUTIVE_FAILURES = 5;
    uint256 public constant MAX_PRICE_AGE = 1 hours;
    uint256 public constant TIMELOCK_DURATION = 24 hours;

    // Parameters
    uint256 public minProfitThreshold;
    uint256 public flashLoanFeeBps;
    uint256 public maxLoanUsd;

    // Events
    event FlashLoanExecuted(
        address indexed asset,
        uint256 amount, 
        uint256 profit, 
        uint256 gasUsed, 
        string provider,
        uint256 timestamp
    );
    event ArbitrageSuccess(address indexed token, uint256 profit, string dexUsed, uint256 timestamp);
    event ArbitrageFailure(address indexed token, string reason, string dexUsed, uint256 timestamp);
    event CircuitBreakerActivated(string reason);
    event CircuitBreakerDeactivated();
    event ProfitWalletChanged(address oldWallet, address newWallet);
    event BorrowerAuthorized(address borrower);
    event BorrowerRevoked(address borrower);
    event LoanProviderUpdated(address provider, bool active, uint256 maxLoanSize, string name);
    event FlashLoanProfitDistributed(address indexed wallet, uint256 amount);
    event WithdrawScheduled(address indexed to, uint256 amount, uint256 executeAfter, uint256 timestamp);
    event WithdrawExecuted(address indexed to, uint256 amount, uint256 timestamp);

    modifier onlyOwner() {
        require(msg.sender == owner, "Only owner can call this function");
        _;
    }

    modifier onlyAuthorizedBorrower() {
        require(authorizedBorrowers[msg.sender] || msg.sender == owner, "Unauthorized borrower");
        _;
    }

    modifier whenNotPaused() {
        require(!circuitBreakerActive, "Contract is paused");
        _;
    }

    modifier checkGasPrice() {
        require(tx.gasprice <= MAX_GAS_PRICE, "Gas price too high");
        _;
    }

    constructor() {
        owner = msg.sender;
        // Initialize with parameters optimized for zero-fee profitability
        minProfitThreshold = 0.0005 ether; // Lower threshold for zero-fee operations
        flashLoanFeeBps = 0;               // Zero fees for Balancer
        maxLoanUsd = 0;                    // Unlimited by default (0 = unlimited)
        circuitBreakerActive = false;
        consecutiveFailures = 0;
        totalProfit = 0;
        totalLosses = 0;
    }

    /**
     * @notice Initialize contract with protocol addresses
     * @dev Sets up all protocol integrations with emphasis on Balancer zero-fee operations
     */
    function initialize(
        address _profitWallet,
        address _balancerVault,  // CRITICAL: Zero-fee Balancer vault
        address _aavePool,
        address _uniswapRouter,
        address _sushiSwapRouter
    ) external {
        require(msg.sender == owner, "Only owner can initialize");
        require(_profitWallet != address(0), "Profit wallet cannot be zero address");
        require(_balancerVault != address(0), "Balancer vault cannot be zero address");
        require(_aavePool != address(0), "Aave pool cannot be zero address");
        require(_uniswapRouter != address(0), "Uniswap router cannot be zero address");
        require(_sushiSwapRouter != address(0), "SushiSwap router cannot be zero address");

        profitWallet = _profitWallet;
        balancerVault = _balancerVault;
        aavePool = _aavePool;
        uniswapRouter = _uniswapRouter;
        sushiSwapRouter = _sushiSwapRouter;

        // Initialize parameters optimized for zero-fee operations
        minProfitThreshold = 0.0001 ether; // Minimal threshold for zero-fee operations
        flashLoanFeeBps = 0;               // Zero fees for Balancer
        maxLoanUsd = 0;                    // Unlimited (0 = unlimited)
        circuitBreakerActive = false;
        consecutiveFailures = 0;

        // Authorize the owner by default
        authorizedBorrowers[owner] = true;

        emit ProfitWalletChanged(address(0), _profitWallet);
    }

    /**
     * @notice Execute flash loan via Balancer (ZERO fees!) - PRIMARY FUNCTION
     * @dev Prioritizes Balancer's zero-fee flash loans to maximize profitability
     */
    function executeBalancerFlashLoan(
        address asset,
        uint256 amount,
        bytes calldata params
    )
        external
        onlyAuthorizedBorrower
        whenNotPaused
        checkGasPrice
    {
        require(asset != address(0), "Asset cannot be zero address");
        require(amount > 0, "Amount must be greater than 0");
        require(!circuitBreakerActive, "Circuit breaker active");

        // Low minimum threshold for Balancer zero-fee operations
        require(amount >= 0.001 ether, "Amount below minimum for zero-fee Balancer operations");
        
        // Check profitable opportunity considering only gas costs (no flash loan fees!)
        (bool hasOpportunity, uint256 expectedProfit) =
            checkProfitableOpportunityForZeroFee(asset, amount);
        require(hasOpportunity && expectedProfit > 0, "No profitable opportunity after gas costs only");

        // **Declare and initialize the tokens and amounts arrays for Balancer flashLoan**
        address[] memory tokens = new address[](1);
        tokens[0] = asset;
        uint256[] memory amounts = new uint256[](1);
        amounts[0] = amount;

        // userData is passed through to receiveFlashLoan (flash loan callback)
        bytes memory userData = abi.encode(
            msg.sender,
            amount,
            block.timestamp,
            params
        );

        // Execute Balancer flash loan (zero fees)
        (bool success, ) = balancerVault.call(
            abi.encodeWithSignature(
                "flashLoan(address,address[],uint256[],bytes)",
                address(this),
                tokens,
                amounts,
                userData
            )
        );
        require(success, "Balancer flash loan execution failed");

        totalTrades++;
    }

    /**
     * @notice Execute flash loan via Aave (with fees) - BACKUP FUNCTION
     * @dev Uses Aave's flash loan mechanism (with fees) for comparison or backup
     */
    function executeAaveFlashLoan(
        address asset,
        uint256 amount,
        bytes calldata params
    )
        external
        onlyAuthorizedBorrower
        whenNotPaused
        checkGasPrice
    {
        require(asset != address(0), "Asset cannot be zero address");
        require(amount > 0, "Amount must be greater than 0");
        require(!circuitBreakerActive, "Circuit breaker active");

        // Higher minimum threshold for Aave due to fees
        require(amount >= 0.1 ether, "Amount below minimum for Aave fee-paying operations");
        
        // Check profitable opportunity after accounting for Aave's fees and gas
        (bool hasOpportunity, uint256 expectedProfit) =
            checkProfitableOpportunityForPaidLoans(asset, amount);
        require(hasOpportunity && expectedProfit > 0, "No profitable opportunity after Aave fees");

        // **Declare and initialize the assets, amounts, and interestRateModes arrays for Aave flashLoan**
        address[] memory assets = new address[](1);
        assets[0] = asset;
        uint256[] memory amounts = new uint256[](1);
        amounts[0] = amount;
        uint256[] memory interestRateModes = new uint256[](1);
        interestRateModes[0] = 0; // 0 = no debt (pure flash loan, no stable/variable borrow)

        bytes memory encodedParams = abi.encode(
            msg.sender,
            amount,
            block.timestamp,
            params
        );

        // Execute Aave flash loan
        (bool success, ) = aavePool.call(
            abi.encodeWithSignature(
                "flashLoan(address,address[],uint256[],uint256[],address,bytes,uint16)",
                address(this),
                assets,
                amounts,
                interestRateModes,
                address(this),
                encodedParams,
                0  // referralCode = 0
            )
        );
        require(success, "Aave flash loan execution failed");

        totalTrades++;
    }

    /**
     * @notice Callback function for Balancer flash loan execution (ZERO fees!)
     * @dev This function is called by Balancer Vault after it sends the flash-loaned funds.
     * It executes the arbitrage strategy and repays the loan.
     */
    function receiveFlashLoan(
        address[] calldata tokens,
        uint256[] calldata amounts,
        uint256[] calldata feeAmounts,  // Balancer passes feeAmounts (expected 0 for zero-fee)
        bytes calldata userData
    ) external returns (bool) {
        require(msg.sender == balancerVault, "Only Balancer vault can call this function");

        // Decode parameters from userData
        (
            address originalInitiator,
            uint256 originalAmount,
            uint256 originalTimestamp,
            bytes memory strategyParams
        ) = abi.decode(userData, (address, uint256, uint256, bytes));
        // originalTimestamp is decoded but not used further (just to have full context)
        originalTimestamp; // silence unused variable warning

        address asset = tokens[0];
        uint256 amount = amounts[0];
        uint256 fee = feeAmounts[0]; // Should be 0 for Balancer zero-fee flash loans

        require(
            originalInitiator == msg.sender || authorizedBorrowers[originalInitiator],
            "Unauthorized initiator for flash loan"
        );
        require(amount == originalAmount, "Flash loan amount mismatch");
        require(fee == 0, "Balancer flash loans should have 0 fee");

        // Verify that the contract received the flash loaned amount
        uint256 balanceBefore = getERC20Balance(asset);
        require(balanceBefore >= amount, "Did not receive flash loan funds");

        // Execute the arbitrage strategy optimized for zero-fee operations
        (bool success, uint256 profit) =
            _executeRealArbitrageStrategyForZeroFee(asset, amount, strategyParams);

        uint256 balanceAfter = getERC20Balance(asset);
        uint256 repaymentAmount = amount + fee; // fee = 0 for Balancer
        require(balanceAfter >= repaymentAmount, "Insufficient balance to repay flash loan");

        // Approve the Balancer vault to pull the repayment amount (principal only, since fee is zero)
        approveToken(asset, balancerVault, repaymentAmount);

        if (success && profit > 0) {
            successfulTrades++;
            totalProfit += profit;
            consecutiveFailures = 0; // Reset failure count on success

            // Calculate the actual profit to send (any excess balance after repaying the loan)
            uint256 netProfit = balanceAfter - repaymentAmount;
            if (netProfit > 0) {
                transferToken(asset, profitWallet, netProfit);
                emit FlashLoanProfitDistributed(profitWallet, netProfit);
                emit ArbitrageSuccess(asset, netProfit, "BalancerZeroFee", block.timestamp);
            }
        } else {
            // No profit scenario (for Balancer, no fee loss incurred, just no gain)
            emit ArbitrageFailure(asset, "No profit from zero-fee arbitrage", "BalancerZeroFee", block.timestamp);
            // Note: We do not increment consecutiveFailures for Balancer failures since no capital was lost to fees.
        }

        emit FlashLoanExecuted(asset, amount, profit, gasleft(), "BalancerV2_ZeroFee", block.timestamp);
        return true;
    }

    /**
     * @notice Callback function for Aave flash loan execution (with fees)
     * @dev Called by Aave after sending the flash-loaned funds. Executes arbitrage and repays Aave.
     */
    function executeOperation(
        address[] calldata assets,
        uint256[] calldata amounts,
        uint256[] calldata premiums,  // Aave fees for the flash loan
        address initiator,
        bytes calldata params
    ) external returns (bool) {
        require(msg.sender == aavePool, "Only Aave pool can call this function");
        initiator; // initiator is not used directly here

        // Decode parameters from params (sent from executeAaveFlashLoan)
        (
            address originalInitiator,
            uint256 originalAmount,
            uint256 originalTimestamp,
            bytes memory userData
        ) = abi.decode(params, (address, uint256, uint256, bytes));
        originalTimestamp; // silence unused warning

        address asset = assets[0];
        uint256 amount = amounts[0];
        uint256 fee = premiums[0]; // Aave's flash loan fee (premium)

        require(
            originalInitiator == msg.sender || authorizedBorrowers[originalInitiator],
            "Unauthorized initiator for flash loan"
        );
        require(amount == originalAmount, "Flash loan amount mismatch");

        // Ensure the contract received the flash-loaned amount from Aave
        uint256 balanceBefore = getERC20Balance(asset);
        require(balanceBefore >= amount, "Did not receive flash loan funds from Aave");

        // Execute the arbitrage strategy accounting for Aave's fee
        (bool success, uint256 profit) =
            _executeArbitrageStrategyForPaidLoans(asset, amount, userData);

        uint256 balanceAfter = getERC20Balance(asset);
        uint256 repaymentAmount = amount + fee; // Principal + fee to repay Aave
        require(balanceAfter >= repaymentAmount, "Insufficient balance to repay Aave loan");

        // Approve the Aave pool to pull the repayment amount (including fee)
        approveToken(asset, aavePool, repaymentAmount);

        if (success && profit > 0) {
            successfulTrades++;
            totalProfit += profit;
            consecutiveFailures = 0; // Reset failure count on success

            // Transfer profit (excess after repaying loan + fee) to profit wallet
            uint256 netProfit = balanceAfter - repaymentAmount;
            if (netProfit > 0) {
                transferToken(asset, profitWallet, netProfit);
                emit FlashLoanProfitDistributed(profitWallet, netProfit);
                emit ArbitrageSuccess(asset, netProfit, "AaveV3", block.timestamp);
            }
        } else {
            // If not profitable, count the fee paid as a loss and update failure count
            totalLosses += fee;
            consecutiveFailures++;
            emit ArbitrageFailure(asset, "No profit after Aave fees", "AaveV3", block.timestamp);

            // Activate circuit breaker if too many consecutive failures occur (to halt operations safely)
            if (consecutiveFailures >= MAX_CONSECUTIVE_FAILURES) {
                circuitBreakerActive = true;
                emit CircuitBreakerActivated("Too many consecutive failures");
            }
        }

        emit FlashLoanExecuted(asset, amount, profit, gasleft(), "AaveV3", block.timestamp);
        return true;
    }

    /**
     * @notice Execute arbitrage strategy optimized for zero-fee protocols (like Balancer)
     * @dev Since there are no flash loan fees, we only need to cover gas costs to be profitable.
     */
    function _executeRealArbitrageStrategyForZeroFee(
        address asset,
        uint256 amount,
        bytes memory userData
    ) private returns (bool, uint256) {
        // Estimate gas cost for arbitrage (placeholder estimate)
        uint256 gasCostEstimate = 350000 * tx.gasprice;
        // Simulate potential profit from the strategy (this is a placeholder for actual arbitrage logic)
        uint256 potentialProfit = calculateRealArbitrageProfit(asset, amount, userData);
        
        if (potentialProfit > gasCostEstimate) {
            uint256 netProfit = potentialProfit - gasCostEstimate;
            return (true, netProfit);
        }
        return (false, 0);
    }

    /**
     * @notice Execute arbitrage strategy for fee-paying protocols (like Aave)
     * @dev Must account for both flash loan fees and gas costs when determining profitability.
     */
    function _executeArbitrageStrategyForPaidLoans(
        address asset,
        uint256 amount,
        bytes memory userData
    ) private returns (bool, uint256) {
        // Estimate Aave flash loan fee (0.09% of the amount as per Aave v2, could be 0.05% in Aave v3)
        uint256 aaveFeeEstimate = (amount * 9) / 10000;  // using 0.09% for estimation
        // Estimate gas cost for arbitrage (slightly higher gas usage assumed due to additional steps)
        uint256 gasCostEstimate = 400000 * tx.gasprice;
        // Simulate potential profit from the strategy (placeholder for actual logic)
        uint256 potentialProfit = calculateRealArbitrageProfit(asset, amount, userData);
        
        // Net profit after subtracting fee and gas cost
        uint256 netProfit =  potentialProfit > (aaveFeeEstimate + gasCostEstimate)
            ? potentialProfit - (aaveFeeEstimate + gasCostEstimate)
            : 0;
        
        if (netProfit > 0) {
            return (true, netProfit);
        }
        return (false, 0);
    }

    /**
     * @notice Calculate potential arbitrage profit based on hypothetical market conditions
     * @dev This is a simplified placeholder. In production, you'd query on-chain DEX prices/order books.
     */
    function calculateRealArbitrageProfit(
        address asset,
        uint256 amount,
        bytes memory userData
    ) private view returns (uint256) {
        asset; userData; // silence unused parameter warnings

        // Placeholder: simulate ~0.067% potential profit for demonstration.
        // In a real scenario, implement your arbitrage logic here.
        return amount / 1500;
    }

    /**
     * @notice Check if a zero-fee flash loan arbitrage would be profitable (considering only gas).
     * @dev Since Balancer has no loan fee, require profit >= 150% of gas cost as a safety margin.
     */
    function checkProfitableOpportunityForZeroFee(
        address asset,
        uint256 amount
    ) public view returns (bool, uint256) {
        asset; // not used in this simplistic check

        uint256 gasCostEstimate = 350000 * tx.gasprice;
        // Require potential profit to be at least 150% of gas cost (to cover gas and then some profit)
        uint256 minProfitForGas = (gasCostEstimate * 150) / 100;  
        uint256 potentialProfit = calculateRealArbitrageProfit(asset, amount, "");
        
        return (potentialProfit >= minProfitForGas, potentialProfit);
    }

    /**
     * @notice Check if a fee-paying flash loan arbitrage (Aave) is profitable after fees and gas.
     * @dev Requires profit to cover Aave fee and gas, with a 120% of costs as minimum profit threshold.
     */
    function checkProfitableOpportunityForPaidLoans(
        address asset,
        uint256 amount
    ) public view returns (bool, uint256) {
        asset; // not used in this simplistic check

        uint256 aaveFeeEstimate = (amount * 9) / 10000; // 0.09% fee estimate
        uint256 gasCostEstimate = 400000 * tx.gasprice;
        uint256 totalCosts = aaveFeeEstimate + gasCostEstimate;
        // Require profit to be at least 120% of total costs (fee + gas)
        uint256 minProfitForCosts = (totalCosts * 120) / 100;
        uint256 potentialProfit = calculateRealArbitrageProfit(asset, amount, "");
        
        return (potentialProfit >= minProfitForCosts, potentialProfit);
    }

    // ========== Helper Functions ==========

    function getERC20Balance(address token) internal view returns (uint256) {
        if (token == address(0)) {
            // If token address is 0, treat it as native ETH balance
            return address(this).balance;
        } else {
            (bool success, bytes memory data) = token.staticcall(
                abi.encodeWithSignature("balanceOf(address)", address(this))
            );
            if (success && data.length >= 32) {
                return abi.decode(data, (uint256));
            }
            return 0;
        }
    }

    function approveToken(address token, address spender, uint256 amount) internal {
        (bool success, ) = token.call(
            abi.encodeWithSignature("approve(address,uint256)", spender, amount)
        );
        require(success, "Token approval failed");
    }

    function transferToken(address token, address to, uint256 amount) internal {
        if (token == address(0)) {
            // Transfer native ETH
            (bool success, ) = payable(to).call{value: amount}("");
            require(success, "ETH transfer failed");
        } else {
            // Transfer ERC20 token
            (bool success, ) = token.call(
                abi.encodeWithSignature("transfer(address,uint256)", to, amount)
            );
            require(success, "Token transfer failed");
        }
    }

    // ========== Administrative Functions ==========

    function setProfitWallet(address _newWallet) external onlyOwner {
        require(_newWallet != address(0), "Invalid address");
        emit ProfitWalletChanged(profitWallet, _newWallet);
        profitWallet = _newWallet;
    }

    function setMinProfitThreshold(uint256 _threshold) external onlyOwner {
        minProfitThreshold = _threshold;
    }
    
    function setFlashLoanFeeBps(uint256 _feeBps) external onlyOwner {
        require(_feeBps <= MAX_FEE_BPS, "Fee too high");
        flashLoanFeeBps = _feeBps;
    }

    function setMaxLoanUsd(uint256 _maxLoanUsd) external onlyOwner {
        maxLoanUsd = _maxLoanUsd;
    }

    function setBorrowerAuthorization(address borrower, bool status) external onlyOwner {
        require(borrower != address(0), "Borrower cannot be zero address");
        authorizedBorrowers[borrower] = status;
        if (status) {
            emit BorrowerAuthorized(borrower);
        } else {
            emit BorrowerRevoked(borrower);
        }
    }

    function toggleCircuitBreaker() external onlyOwner {
        circuitBreakerActive = !circuitBreakerActive;
        if (circuitBreakerActive) {
            emit CircuitBreakerActivated("Manual activation");
        } else {
            consecutiveFailures = 0; // Reset failures when reactivating
            emit CircuitBreakerDeactivated();
        }
    }

    // ========== View Functions ==========

    function getPerformanceMetrics() external view returns (
        uint256 _totalTrades,
        uint256 _successfulTrades,
        uint256 _totalProfit,
        uint256 _totalLosses,
        uint256 _successRate,
        uint256 _consecutiveFailures,
        bool _circuitBreakerActive
    ) {
        _totalTrades          = totalTrades;
        _successfulTrades     = successfulTrades;
        _totalProfit          = totalProfit;
        _totalLosses          = totalLosses;
        _consecutiveFailures  = consecutiveFailures;
        _circuitBreakerActive = circuitBreakerActive;
        _successRate = _totalTrades == 0 ? 0 : (_successfulTrades * 10000) / _totalTrades;
    }

    function getContractBalance(address token) external view returns (uint256) {
        return getERC20Balance(token);
    }

    // Allow the contract to receive ETH (only from the owner, for funding if needed)
    receive() external payable {
        require(msg.sender == owner, "Only owner can send ETH");
    }
}

// Interface for flash loan callbacks (both Balancer and Aave)
interface IFlashLoanReceiver {
    function executeOperation(
        address[] calldata assets,
        uint256[] calldata amounts,
        uint256[] calldata premiums,
        address initiator,
        bytes calldata params
    ) external returns (bool);
    
    function receiveFlashLoan(
        address[] calldata tokens,
        uint256[] calldata amounts,
        uint256[] calldata feeAmounts,
        bytes calldata userData
    ) external returns (bool);
}

Read Contract

FEE_DENOMINATOR 0xd73792a9 → uint256
MAX_CONSECUTIVE_FAILURES 0x6375184a → uint256
MAX_FEE_BPS 0xd55be8c6 → uint256
MAX_GAS_PRICE 0xe3bbb4f1 → uint256
MAX_PRICE_AGE 0x9d7f7e86 → uint256
MIN_PROFIT_THRESHOLD_BPS 0xb38ab63e → uint256
TIMELOCK_DURATION 0x4623c81e → uint256
aavePool 0xa03e4bc3 → address
authorizedBorrowers 0x8fdbb073 → bool
balancerVault 0x158274a5 → address
checkProfitableOpportunityForPaidLoans 0xa405293e → bool, uint256
checkProfitableOpportunityForZeroFee 0x0f97d37c → bool, uint256
circuitBreakerActive 0xf609844d → bool
consecutiveFailures 0x584b236c → uint256
flashLoanFeeBps 0xc0c7347e → uint256
getContractBalance 0x43ab265f → uint256
getPerformanceMetrics 0xff083a97 → uint256, uint256, uint256, uint256, uint256, uint256, bool
maxLoanUsd 0x6d278f7d → uint256
minProfitThreshold 0x7341ab8c → uint256
owner 0x8da5cb5b → address
profitWallet 0x2301d775 → address
successfulTrades 0xfc213996 → uint256
sushiSwapRouter 0xfd829a23 → address
totalLosses 0xbf04820b → uint256
totalProfit 0x86001519 → uint256
totalTrades 0xe275c997 → uint256
uniswapRouter 0x735de9f7 → address

Write Contract 11 functions

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

executeAaveFlashLoan 0xbfda2c02
address asset
uint256 amount
bytes params
executeBalancerFlashLoan 0x7ebdfec4
address asset
uint256 amount
bytes params
executeOperation 0x920f5c84
address[] assets
uint256[] amounts
uint256[] premiums
address initiator
bytes params
returns: bool
initialize 0x1459457a
address _profitWallet
address _balancerVault
address _aavePool
address _uniswapRouter
address _sushiSwapRouter
receiveFlashLoan 0xf04f2707
address[] tokens
uint256[] amounts
uint256[] feeAmounts
bytes userData
returns: bool
setBorrowerAuthorization 0x7c0d3f43
address borrower
bool status
setFlashLoanFeeBps 0x599d9c6e
uint256 _feeBps
setMaxLoanUsd 0x84919e21
uint256 _maxLoanUsd
setMinProfitThreshold 0xc0286465
uint256 _threshold
setProfitWallet 0xda2ca9b5
address _newWallet
toggleCircuitBreaker 0xd5eb63cc
No parameters

Recent Transactions

No transactions found for this address