Address Contract Verified
Address
0x2d036AFA7Df77Ba8375E1A544a6315A8fC89E9dE
Balance
0.000000000 ETH
Nonce
1
Code Size
23683 bytes
Creator
0xedB6E780...3c8C at tx 0x4a7a3e10...5b69c4
Indexed Transactions
0
Contract Bytecode
23683 bytes
0x60806040526004361015610018575b361561001657005b005b60003560e01c806304ca1339146144515780630d34219c1461442557806310e1c1661461440757806313b64e24146143555780631e999814146143375780631f32eb77146141db57806324f1c5c114614143578063277327a514613fca5780633410fe6e14613fad5780633aad938d14613f905780633e99c1e414613c97578063457a8924146137b0578063472d35b91461372b5780634d06734514613708578063514fcac7146134cc578063558e44d3146134af57806359c69313146134915780635b54918214613468578063634eef471461337657806366961d4414613358578063679230f21461333c57806367bf6ba6146133135780636859ebf01461324857806370cec1ac1461317e578063715018a614613137578063727d2114146130e05780637af187881461308a57806381bbc0d314612fed578063830562bc14612fc457806386ea511e14612f9357806387d11e6114612dc65780638da5cb5b14612d9d5780638dfb8ccd14612d565780638f9057df146127fd578063975b8662146127df5780639c302fdb1461226e5780639f92b7151461220c578063a1a65f9c146121d2578063a674537f146121b4578063a85c38ef146120de578063a8b37cfe1461170d578063b4824034146116ef578063b7bac4cf1461163e578063cb25c17d14611612578063d09ef24114611427578063d0fb0203146113fe578063d23a0bd01461117d578063d79e85671461102c578063e2cfcfee14610f79578063ea7bcef414610bc6578063f1a82c7e14610ba8578063f2fde38b14610b31578063f6252ff214610ae8578063fab960ed146102ab5763fd31f89a0361000e57346102a65760203660031901126102a657600435600052600a6020526020604060002054604051908152f35b600080fd5b60e03660031901126102a65760043560028110156102a65760243560028110156102a6576044356001600160a01b0381168082036102a65760c4359360a435936064359391906084356001600160401b0388116102a657366023890112156102a657876004013561031b81614588565b986103296040519a8b61454e565b818a5236602483830101116102a65781600092602460209301838d01378a0101528415610ab357833b15610a7d57600254604051630736b32b60e31b81526004810187905290602090829060249082906001600160a01b03165afa908115610a7157600091610a42575b50610a0a576103a0615312565b85156109c557801561098a576103b7871515614d44565b600f548711610956576103ca8487615aa1565b906000968160001904831161091257670de0b6b3a76400006103ec8385614bd4565b0496600d5488106108db57600e5488116108a45761040986614500565b851596871561085d57883410610820578961043561042c9c5b6004549d8e6147f5565b60045542614fb6565b9561043f89614500565b891561081757868d8a8d5b896040519561045887614517565b8487528d60208801338152604089019561047181614500565b8652606089019161048181614500565b8252608089018c815260a08a0193845260c08a0194855260e08a019586526101008a018b81526101208b018c81526101408c019a8b526101608c018d8152998d5260076020526040909c209a518b55915160018b0180546001600160a01b0319166001600160a01b03929092169190911781559651979a979196909290919061050982614500565b61051282614500565b8254905161051f81614500565b61052881614500565b61ffff60a01b1990911660a09290921b60ff60a01b169190911760a89190911b60ff60a81b16179055516002880180546001600160a01b0319166001600160a01b039290921691909117905551600387015551600480870191909155905160058601559051600685015590516007840155905160088301559151600990910191811015610803576105c89288928a9260ff80198354169116179055615284565b808a52600a6020528a600260408c2054604051906105e582614533565b81528c604060208301918083528184019560018752878252600b6020528282209082526020522091518255516001820155019051151560ff80198354169116179055808a52600a60205260408a205415156000146107f057808a52600b60205260408a20818b52600a60205260408b20548b526020528a600160408c2001555b808a52600a6020528a60408b2055808a52600860205260408a20548b8b52600c60205260408b20558952600860205260408920805490600182018092116107dc5755604051946106b487614500565b8686526106c081614500565b602086015260408501526060840152608083015260a08201528360c082015261010060e082015286519081610100820152855b8281106107c5575090867ffefbf00d957f3d8c906d65eaa25d86c7eca54738c3902132da94c7a83e7a913c8388610120856107449897010152610120813395601f80199101168101030190a3614500565b806107bc575b61075e575b60208360018055604051908152f35b818061076b819334614bc7565b335af1610776614c52565b501561078357818061074f565b60405162461bcd60e51b8152602060048201526011602482015270115512081c995d1d5c9b8819985a5b1959607a1b6044820152606490fd5b5080341161074a565b80602080928b0101516101208285010152016106f3565b634e487b7160e01b8b52601160045260248bfd5b808a5260096020528a60408b2055610665565b634e487b7160e01b8c52602160045260248cfd5b868d8a8461044a565b60405162461bcd60e51b8152602060048201526015602482015274125b9cdd59999a58da595b9d08115512081cd95b9d605a1b6044820152606490fd5b8961043561042c9c61089f6040516323b872dd60e01b60208201523360248201523060448201528760648201526064815261089960848261454e565b86615ae4565b610422565b60405162461bcd60e51b815260206004820152600f60248201526e4f7264657220746f6f206c6172676560881b6044820152606490fd5b60405162461bcd60e51b815260206004820152600f60248201526e13dc99195c881d1bdbc81cdb585b1b608a1b6044820152606490fd5b606460405162461bcd60e51b815260206004820152602060248201527f50726963652063616c63756c6174696f6e20776f756c64206f766572666c6f776044820152fd5b60405162461bcd60e51b815260206004820152600c60248201526b54544c20746f6f206c6f6e6760a01b6044820152606490fd5b60405162461bcd60e51b8152602060048201526013602482015272496e76616c69642070726963652072616e676560681b6044820152606490fd5b60405162461bcd60e51b815260206004820152601d60248201527f416d6f756e74206d7573742062652067726561746572207468616e20300000006044820152606490fd5b60405162461bcd60e51b815260206004820152601060248201526f151bdad95b881a5cc8189b1bd8dad95960821b6044820152606490fd5b610a64915060203d602011610a6a575b610a5c818361454e565b810190614954565b89610393565b503d610a52565b6040513d6000823e3d90fd5b60405162461bcd60e51b815260206004820152600e60248201526d139bdd08184818dbdb9d1c9858dd60921b6044820152606490fd5b60405162461bcd60e51b815260206004820152600d60248201526c24b73b30b634b2103a37b5b2b760991b6044820152606490fd5b346102a65760203660031901126102a6576020610b296004358060005260078352610b2460018060a01b03600160406000200154161515614acb565b614ff5565b604051908152f35b346102a65760203660031901126102a657610b4a614474565b610b5261525b565b6001600160a01b03168015610b9257600080546001600160a01b03198116831782556001600160a01b031690600080516020615c2e8339815191529080a3005b631e4fbdf760e01b600052600060045260246000fd5b346102a65760003660031901126102a6576020600e54604051908152f35b346102a65760203660031901126102a6576004356001600160401b0381116102a657366023820112156102a657806004013590610c0282614571565b91610c10604051938461454e565b8083526024602084019160051b830101913683116102a657602401905b828210610f695783610c3d615312565b805115610f2c576064815111610edb5760005b8151811015610ed557610c638183614892565b5190816000526007602052610c8960018060a01b03600160406000200154161515614acb565b8160005260076020526040600020916009830160ff8154166004811015610ebf57610cb49015614b4c565b610cbd82614cc2565b15610e865781610d31600195610d3693600360ff198254161790558681019081548860ff8260a01c16610cef81614500565b14610e22575b50815460ff8160a01c16610d0881614500565b15610d61575b506002888060a01b039101541690549060ff808360a81c169260a01c1690615284565b615440565b7f1ad308dc7017610c82d08084545f7176df5e2f08f078c3c8f8926cd7e5555514600080a201610c50565b8860ff8260a81c16610d7281614500565b03610ddd576005820154600683015460009181811115610dd657610d969250614bc7565b80610da2575b50610d0e565b6000808083610dcf95610db88396471015614c07565b8e8060a01b03165af1610dc9614c52565b50614c82565b8980610d9c565b5050610d96565b670de0b6b3a7640000610e1560038401546007850154600091818111600014610e1b57610e0a9250614bc7565b600485015490614bd4565b04610d96565b5050610e0a565b6003820154600783015460009181811115610e7e57610e419250614bc7565b905b81610e4f575b50610cf5565b610e7791610e698b8060a01b0360028601541680926153f7565b918b8060a01b0316906152d8565b8980610e49565b505090610e43565b60405162461bcd60e51b815260206004820152601160248201527013dc99195c881b9bdd08195e1c1a5c9959607a1b6044820152606490fd5b634e487b7160e01b600052602160045260246000fd5b60018055005b60405162461bcd60e51b815260206004820152602360248201527f546f6f206d616e79206f726465727320746f20636c65616e207570206174206f6044820152626e636560e81b6064820152608490fd5b60405162461bcd60e51b815260206004820152601560248201527404e6f206f726465727320746f20636c65616e20757605c1b6044820152606490fd5b8135815260209182019101610c2d565b346102a65760203660031901126102a657610f92614474565b610f9a61525b565b6001600160a01b03168015610fed57601280546001600160a01b0319811683179091556001600160a01b03167f3ddadec435df2f077c5597f4952343aa7ef9a4db1af45c9e6f92ff7f6e1c67c1600080a3005b60405162461bcd60e51b8152602060048201526017602482015276496e76616c696420666163746f7279206164647265737360481b6044820152606490fd5b346102a65760403660031901126102a6576004356001600160a01b038116908190036102a65760243561105d61525b565b611068821515614900565b4715611143578061113e5750475b4781116110fe57600080808084865af161108e614c52565b50156110c15760207f20f907b58305c7b76035bc03b26f32b1c4f6560f96be6f3bb54c5c848a2d4ddd91604051908152a2005b60405162461bcd60e51b8152602060048201526015602482015274115512081dda5d1a191c985dd85b0819985a5b1959605a1b6044820152606490fd5b60405162461bcd60e51b8152602060048201526018602482015277496e73756666696369656e74204554482062616c616e636560401b6044820152606490fd5b611076565b60405162461bcd60e51b81526020600482015260126024820152714e6f2045544820746f20776974686472617760701b6044820152606490fd5b346102a65760203660031901126102a6576004356001600160401b0381116102a6576111ad9036906004016144d0565b6111b561525b565b80156113b957600a811161137f576001600160401b03811161136957600160401b8111611369576014548160145580821061130a575b508160146000526020600020600a83049060005b8281106112ca5750600a8202840380611285575b5050505060405190806020830160208452526040820192906000905b80821061125e577f8b5911bc3a69df070f5a8ecf2460704729bd313de1b0aba9a8527ff6f0a24a1e84860385a1005b90919384359062ffffff82168092036102a65760208160019382935201950192019061122f565b9260009360005b8181106112a157505050015582808080611213565b90919460206112c06001926112b589614fcf565b908560030290614fdf565b960192910161128c565b6000805b600a81106112e35750828201556001016111ff565b949060206113016001926112f685614fcf565b908960030290614fdf565b920195016112ce565b60146000526020600020600a60098181860104830193010401906003600a8406028061134c575b505b81811061134057506111eb565b60008155600101611333565b6000198201908154906000199060200360031b1c16905584611331565b634e487b7160e01b600052604160045260246000fd5b60405162461bcd60e51b8152602060048201526012602482015271546f6f206d616e792066656520746965727360701b6044820152606490fd5b60405162461bcd60e51b815260206004820152601f60248201527f4665652074696572732061727261792063616e6e6f7420626520656d707479006044820152606490fd5b346102a65760003660031901126102a6576003546040516001600160a01b039091168152602090f35b346102a65760203660031901126102a657600435600061016060405161144c81614517565b8281528260208201528260408201528260608201528260808201528260a08201528260c08201528260e082015282610100820152826101208201528261014082015201528060005260076020526114b460018060a01b03600160406000200154161515614acb565b6000526007602052610180604060002061161060405180808080956114d882614517565b8054825260018101546001600160a01b0381166020840190815290604084019060ff9060a081901c821661150b81614500565b835260a81c16606085019061151f81614500565b815260018060a01b03600285015416906080860191825260038501549260a0870193845260048601549460c0880195865261159a61016060ff600960058b01549a60e08d019b8c5261010060068201549d019c8d5261012060078201549e019d8e5261014060088201549f019e8f520154169c019b8c614fc3565b6040519c518d52516001600160a01b031660208d0152516115ba81614500565b60408c0152516115c981614500565b60608b0152516001600160a01b031660808a01525160a08901525160c08801525160e08701525161010086015251610120850152516101408401525161016083019061450a565bf35b346102a65760203660031901126102a65760043560005260096020526020604060002054604051908152f35b346102a65760203660031901126102a6576004356001600160a01b038116908190036102a65761166c61525b565b80156116ab57601380546001600160a01b031916821790557f0b91e9e48ef4e7faf637ee996fa9a770073f525e31b6f7f7e91be3775a602456600080a2005b60405162461bcd60e51b815260206004820152601c60248201527b496e76616c69642070726963652068656c706572206164647265737360201b6044820152606490fd5b346102a65760003660031901126102a6576020600554604051908152f35b346102a65760803660031901126102a6576004356001600160401b0381116102a65761173d9036906004016144d0565b6024356001600160401b0381116102a65761175c9036906004016144d0565b92906044356001600160401b0381116102a65761177d9036906004016144d0565b9290946064359361178c615312565b6002546040516314c530d560e31b815233600482015290602090829060249082906001600160a01b03165afa8015610a71576117d0916000916120bf575b5061496c565b8515612083578186148061207a575b6117e8906149fd565b60005b8681106117f85760018055005b61180e611806828588614a54565b351515614eb8565b61181981838a614a54565b35156120265761182a818886614a54565b3590611837818588614a54565b359161184482858c614a54565b3590806000526007602052604060002060405161186081614517565b8154815260018201546001600160a01b038116602083015260408201906119129060ff9060a081901c821661189481614500565b845260a81c169260608101936118a981614500565b845260028501546001600160a01b03166080820152600385015460a0820152600485015460c08201908152600586015460e083015260068601546101008301526007860154610120830152600886015461014083015260099095015460ff169061016001614fc3565b6001825161191f81614500565b61192881614500565b14611fea575b505161193981614500565b61194281614500565b15611f82575b50806000526007602052604060002060ff6009820154166004811015610ebf576119729015614b4c565b61198461197e83614cc2565b15614b8b565b600281019260018060a01b03845416916119a66119a18385614dd6565b615a39565b6002546040516316f4f72360e11b815290602090829060049082906001600160a01b03165afa908115610a7157600091611f64575b506001600160a01b0316908115611f205760206024926040519384809263bbe4f6db60e01b82528860048301525afa918215610a7157600092611ef0575b506001600160a01b038216958615611e9d57600182019081549560ff8760a01c169a611a448c614500565b8b1580988199611e83575b5015611e5757611a5f8786615553565b905b81811115611e4f5750965b611a77881515614f0f565b611a94670de0b6b3a7640000611a8d898b614bd4565b049c614500565b15611c9c57508054611aaf906001600160a01b0316876153f7565b93883b156102a6578f94898c96604051809e8192633015be1d60e21b8352856004840192611adc93615705565b0381895a94600095f19b8c15610a7157611b149c611c8b575b5082546001600160a01b03169b611b229061271090611b1a908f614a96565b84614bd4565b048092614bc7565b9b81611be4575b50509054825460019b600080516020615bce8339815191529860409890979096909590945092611b65926001600160a01b0390811691166152d8565b60078201611b74868254614fb6565b90555460ff8160a01c16611b8781614500565b159081611bcb575b50611bb3575b5050611ba281838761571b565b82519182526020820152a3016117eb565b6006611bc29101918254614fb6565b90558f80611b95565b8b915060a81c60ff16611bdd81614500565b1438611b8f565b81611c029160018060a09c9a98969b99979c1b0360035416906152d8565b87546003546001600160a01b03918216989116803b156102a657611c4198600080946040519b8c9586948593632a2fc2f760e01b855260048501615b8d565b03925af18015610a715760019b600080516020615bce83398151915298604098611b6593611c7a575b50939597509b8294969850611b29565b6000611c859161454e565b38611c6a565b6000611c969161454e565b38611af5565b611caa8188969495966153f7565b906020604051809263095ea7b360e01b825281600081611cce888b60048401614f4f565b03925af18015610a7157611e33575b50883b156102a6578f906000908c611d09604051948593849363b23d349b60e01b855260048501615705565b0381838d5af18015610a7157611e22575b50546001600160a01b0316612710611d3a611d3483614a96565b8c614bd4565b0491611d46838c614bc7565b9280611d90575b50505097600080516020615bce833981519152949260409492611d8b600080808060019f60018060a01b038754165af1611d85614c52565b50614f6a565b611b65565b60018060a09d9598969997949d1b0360035416803b156102a657600092611dce926040519e8f9485938492629a7d7960e81b84528460048501615b8d565b03925af1958615610a7157611d8b600080808060019f9a60409b600080516020615bce8339815191529d611e12575b50979f50505050508295975081949650611d4d565b82611e1c9161454e565b38611dfd565b6000611e2d9161454e565b38611d1a565b611e4a9060203d8111610a6a57610a5c818361454e565b611cdd565b905096611a6c565b6003850154600786015460009181811115611e7c57611e769250614bc7565b90611a61565b5050611e76565b6001915060a81c60ff16611e9681614500565b1438611a4f565b60405162461bcd60e51b815260206004820152602560248201527f4d61726b6574206d616b657220706f6f6c206e6f7420666f756e6420666f72206044820152643a37b5b2b760d91b6064820152608490fd5b611f1291925060203d8111611f19575b611f0a818361454e565b8101906145a3565b908f611a19565b503d611f00565b60405162461bcd60e51b815260206004820152601c60248201527b13585c9ad95d081b585ad95c88199858dd1bdc9e481b9bdd081cd95d60221b6044820152606490fd5b611f7c915060203d8111611f1957611f0a818361454e565b8f6119db565b518203611f8f578b611948565b60405162461bcd60e51b815260206004820152602d60248201527f457865637574696f6e207072696365206e6f74206d617463686564207769746860448201526c1037b93232b910383934b1b29760991b6064820152608490fd5b51611ff481614500565b611ffd81614500565b6120145761200e82518511156159db565b8d61192e565b612021825185101561597e565b61200e565b60405162461bcd60e51b815260206004820152602660248201527f457865637574696f6e207072696365206d75737420626520677265617465722060448201526507468616e20360d41b6064820152608490fd5b508581146117df565b60405162461bcd60e51b8152602060048201526014602482015273139bc81bdc99195c9cc81d1bc8199d5b199a5b1b60621b6044820152606490fd5b6120d8915060203d602011610a6a57610a5c818361454e565b896117ca565b346102a65760203660031901126102a6576004356000526007602052610180604060002061161081549160018101549060ff8260a01c1660ff8360a81c1660018060a01b03600284015416600384015460048501549060058601549260068701549460078801549660ff600960088b01549a015416996040519c8d5260018060a01b031660208d015261217081614500565b60408c015261217e81614500565b60608b015260808a015260a089015260c088015260e087015261010086015261012085015261014084015261016083019061450a565b346102a65760003660031901126102a6576020600d54604051908152f35b346102a65760203660031901126102a6576001600160a01b036121f3614474565b1660005260066020526020604060002054604051908152f35b346102a65760203660031901126102a6577f72a51ea60242ee3def7dbd2c9b40d17fa8a3f853c78d14f0f9641a507af6e3c8604060043561224b61525b565b6122596101f4821115614cfe565b600554908060055582519182526020820152a1005b346102a65760603660031901126102a657604435602435600435612290615312565b6000818152600760205260409020600101546122b6906001600160a01b03161515614acb565b8060005260076020526122da60018060a01b03600160406000200154163314614b0e565b6122e5821515614eb8565b80600052600760205260406000209060ff6009830154166004811015610ebf5761230f9015614b4c565b6004600092600181019485549060ff8260a01c169561232d87614500565b8615809381946127c5575b501561279b575061234c8484015484615553565b905b818111156127935750945b8515159061236682614f0f565b60025460405163745e9e9f60e01b81529560209187919082906001600160a01b03165afa948515610a7157600095612772575b506001600160a01b038516948515612732576000936123b9600093614500565b156125ad5750505050600160ff865460a81c166123d581614500565b0361258f576005810154600682015480821115612586576123f591614bc7565b915b60028201805460405163815a1a0d60e01b81526001600160a01b03909116600482015260248101859052604481019890985260016064890152906020908890608490829087905af1968715610a715760009761254d575b50805461248991906124739061246d906001600160a01b03168a615693565b89614bc7565b905487546001600160a01b0390811691166152d8565b851561253d57670de0b6b3a76400008202828104670de0b6b3a7640000148315171561252757610ed5966124bc91614be7565b945b600782016124cd868254614fb6565b90555460ff8160a01c166124e081614500565b15908161250d575b506124f5575b505061571b565b60066125049101918254614fb6565b905584806124ee565b6001915060a81c60ff1661252081614500565b14876124e8565b634e487b7160e01b600052601160045260246000fd5b610ed595506004810154946124be565b90966020823d60201161257e575b816125686020938361454e565b8101031261257b5750519561248961244e565b80fd5b3d915061255b565b505060006123f5565b670de0b6b3a76400006125a6600483015486614bd4565b04916123f7565b90919294986125d2996125f26020600288019460018060a01b038654169d8e8d6153f7565b9d8e918860405180968195829463095ea7b360e01b845260048401614f4f565b03925af180156127275790602093929161270c575b5060018060a01b03845416916040519c8d9363815a1a0d60e01b85526004850152602484015260448301528460648301528185608482800301925af19889156127015782996126c9575b5054612690919081908190819061267b90612675906001600160a01b03168e61561b565b8d614bc7565b8b546001600160a01b03165af1611d85614c52565b1561253d57670de0b6b3a7640000860295808704670de0b6b3a76400001490151715612527576126c384610ed597614be7565b946124be565b9098506020813d6020116126f9575b816126e56020938361454e565b810103126126f557519781612651565b5080fd5b3d91506126d8565b6040513d84823e3d90fd5b61272290843d8611610a6a57610a5c818361454e565b612607565b6040513d87823e3d90fd5b60405162461bcd60e51b815260206004820152601860248201527711985b1b189858dad15e1958dd5d1bdc881b9bdd081cd95d60421b6044820152606490fd5b61278c91955060203d602011611f1957611f0a818361454e565b9389612399565b905094612359565b6003840154600785015490818111156127be576127b89250614bc7565b9061234e565b50506127b8565b6001915060a81c60ff166127d881614500565b148a612338565b346102a65760003660031901126102a6576020601054604051908152f35b346102a65760203660031901126102a6576004356001600160401b0381116102a65761282d9036906004016144a0565b612835615312565b6002546040516314c530d560e31b815233600482015290602090829060249082906001600160a01b03165afa8015610a715761287891600091612d37575061496c565b6128838115156149b3565b60005b8181106128935760018055005b6128a7366128a2838587614a44565b614a64565b90815160005260076020526040600020602083019081516000526007602052604060002060ff6009830154166004811015610ebf571580612d1f575b6128ec90615334565b6128f68551614cc2565b1580612d0e575b15612ccf5760019061294b828401549360ff8560a01c1661291d81614500565b1580612caf575b61292d90615378565b6002848060a01b0391015416838060a01b03600284015416146153ba565b0154906001600160a01b0380821690831614612c5e5760a81c60ff1661297081614500565b159081612c46575b5015612c0e578251600052600760205260406000209080516000526007602052604060002090600483019283549060048401918254809110612bc957600382015460078301908154600091818111600014612bc1576129d79250614bc7565b955b60038101549260078201938454600091818111600014612bba576129fd9250614bc7565b60408c015198808a11612bb2575b50808911612ba8575b50612ac791612ac16001612aae8b670de0b6b3a7640000612a43612aa897612a3d841515614f0f565b83614bd4565b0494612a9660008080806002860195612a80612a688d8c8060a01b038a54169061561b565b612a7a8c8060a01b038a54168b615693565b9d614bc7565b908a808060a01b03910154165af1611d85614c52565b838060a01b03905416958680926153f7565b946153f7565b97019660018060a01b0388541692614bc7565b916152d8565b612ad2868254614fb6565b9055612adf858254614fb6565b90558454825490818111612b17575b50505090612b088392612b1195946001985191549161571b565b5191549161571b565b01612886565b60008093670de0b6b3a7640000612b3e612b38849685969c9b9a999c614bc7565b89614bd4565b91549104906001600160a01b03165af1612b56614c52565b5015612b6757909192888080612aee565b60405162461bcd60e51b8152602060048201526019602482015278115512081cd85d9a5b99dcc81c995d1d5c9b8819985a5b1959603a1b6044820152606490fd5b9750612ac7612a14565b98508e612a0b565b50506129fd565b5050956129d9565b60405162461bcd60e51b815260206004820152601f60248201527f5072696365206d69736d6174636820666f72206c696d6974206f7264657273006044820152606490fd5b60405162461bcd60e51b815260206004820152601060248201526f4e6f74206c696d6974206f726465727360801b6044820152606490fd5b60ff915060a81c16612c5781614500565b1586612978565b60405162461bcd60e51b815260206004820152602360248201527f53656c6c657220616e642062757965722063616e6e6f74206265207468652073604482015262616d6560e81b6064820152608490fd5b5061292d8460ff8186015460a01c16612cc781614500565b149050612924565b60405162461bcd60e51b81526020600482015260176024820152760c481bd9881d1a19481bdc99195c9cc8195e1c1a5c9959604a1b6044820152606490fd5b50612d198351614cc2565b156128fd565b5060ff6009820154166004811015610ebf57156128e3565b612d50915060203d602011610a6a57610a5c818361454e565b846117ca565b346102a65760203660031901126102a6576004356014548110156102a65762ffffff60209160146000526003600a84600020818404015492060260031b1c16604051908152f35b346102a65760003660031901126102a6576000546040516001600160a01b039091168152602090f35b346102a65760403660031901126102a6576004356001600160401b0381116102a657612df69036906004016144d0565b90612dff61448a565b91612e0861525b565b6001600160a01b03831690612e1e821515614900565b8015612f585760148111612f075760005b818110612e3857005b612e43818386614a54565b356001600160a01b03811691908290036102a657612e628215156148bc565b6040516370a0823160e01b8152306004820152602081602481865afa928315610a71578591600094612ece575b5083600194612ea2575b50505001612e2f565b602081612ebf600080516020615c0e833981519152938c866152d8565b604051908152a3838780612e99565b915091926020823d8211612eff575b81612eea6020938361454e565b8101031261257b575051919084906001612e8f565b3d9150612edd565b60405162461bcd60e51b815260206004820152602360248201527f546f6f206d616e7920746f6b656e7320746f207769746864726177206174206f6044820152626e636560e81b6064820152608490fd5b60405162461bcd60e51b8152602060048201526013602482015272139bc81d1bdad95b9cc81cdc1958da599a5959606a1b6044820152606490fd5b346102a65760403660031901126102a6576020612fba612fb1614474565b60243590614dd6565b6040519015158152f35b346102a65760003660031901126102a6576002546040516001600160a01b039091168152602090f35b346102a65760203660031901126102a65760043561300961525b565b612710811161304b5760407f1a0e49c8d547d5d440b1e87ef8c3f4dcedad3129fe3308e075c0d72395c3451191601154908060115582519182526020820152a1005b60405162461bcd60e51b8152602060048201526017602482015276088eae6e840e8ded8cae4c2dcc6ca40e8dede40d0d2ced604b1b6044820152606490fd5b346102a65760403660031901126102a657600435600052600b60205260406000206024356000526020526060604060002080549060ff600260018301549201541690604051928352602083015215156040820152f35b346102a65760203660031901126102a6577f5829309fa85e85a75b744b2b1ae6b2913b3d2a9c94df75a5b61de31f962256be602060043561311f61525b565b61312a811515614d44565b80600f55604051908152a1005b346102a65760003660031901126102a65761315061525b565b600080546001600160a01b0319811682556001600160a01b0316600080516020615c2e8339815191528280a3005b346102a65760203660031901126102a657613197614474565b61319f61525b565b6001600160a01b03166131b38115156148bc565b80600052600660205260406000205480156131f8576040600080516020615bae83398151915291836000526006602052600082812055815190815260006020820152a2005b60405162461bcd60e51b815260206004820152602260248201527f4e6f207370656369666963206665652073657420666f72207468697320746f6b60448201526132b760f11b6064820152608490fd5b346102a65760403660031901126102a657613261614474565b6024359061326d61525b565b6001600160a01b0316906132828215156148bc565b603281106132d5576040816132aa61012c600080516020615bae833981519152941115614cfe565b83600052600660205281600020549084600052600660205280836000205582519182526020820152a2005b60405162461bcd60e51b81526020600482015260166024820152754665652070657263656e7461676520746f6f206c6f7760501b6044820152606490fd5b346102a65760003660031901126102a6576013546040516001600160a01b039091168152602090f35b346102a65760003660031901126102a657602060405160328152f35b346102a65760003660031901126102a6576020600f54604051908152f35b346102a65760203660031901126102a6576004356001600160a01b038116908190036102a6576133a461525b565b801561342757600280546001600160a01b0319168217905560405163d0fb020360e01b815290602090829060049082905afa908115610a7157600091613408575b50600380546001600160a01b0319166001600160a01b0392909216919091179055005b613421915060203d602011611f1957611f0a818361454e565b816133e5565b60405162461bcd60e51b8152602060048201526019602482015278496e76616c6964206772616469656e7420726567697374727960381b6044820152606490fd5b346102a65760003660031901126102a6576012546040516001600160a01b039091168152602090f35b346102a65760203660031901126102a6576020612fba600435614cc2565b346102a65760003660031901126102a65760206040516101f48152f35b346102a65760203660031901126102a6576004356134e8615312565b60008181526007602052604090206001015461350e906001600160a01b03161515614acb565b80600052600760205261353260018060a01b03600160406000200154163314614b0e565b80600052600760205260406000206009810160ff8154166004811015610ebf576135fb928492613565610d319315614b4c565b61357161197e85614cc2565b600260ff198254161790556001810190815460ff8160a01c1661359381614500565b6136a257508154600160ff8260a81c166135ac81614500565b0361365f576005820154600683015480821115613656576135cc91614bc7565b80613626575b50505b60020154905460ff60a882901c81169260a09290921c16906001600160a01b0316615284565b7f61b9399f2f0f32ca39ce8d7be32caed5ec22fe07a6daba3a467ed479ec606582600080a260018055005b600080808361364f9561363c8396471015614c07565b6001600160a01b03165af1610dc9614c52565b86806135d2565b505060006135cc565b600382015460078301548082111561368d57610e0a61368791670de0b6b3a764000093614bc7565b046135cc565b5050670de0b6b3a76400006136876000610e0a565b60038201546007830154808211156136fe576136bd91614bc7565b905b816136cc575b50506135d5565b6136f7916136e760018060a01b0360028601541680926153f7565b916001600160a01b0316906152d8565b86806136c5565b50506000906136bf565b346102a65760203660031901126102a6576020610b29613726614474565b614a96565b346102a65760203660031901126102a6576004356001600160a01b038116908190036102a65761375961525b565b801561377557600380546001600160a01b031916919091179055005b60405162461bcd60e51b815260206004820152601360248201527224b73b30b634b2103332b29036b0b730b3b2b960691b6044820152606490fd5b346102a65760403660031901126102a6576004356001600160401b0381116102a6576137e09036906004016144a0565b906024356001600160401b0381116102a6576138009036906004016144d0565b909261380a615312565b6002546040516314c530d560e31b815233600482015290602090829060249082906001600160a01b03165afa8015610a715761384d91600091613c78575061496c565b6138588115156149b3565b6138638282146149fd565b60005b8181106138735760018055005b61387e818386614a44565b9061389661388d828689614a54565b35923690614a64565b918251600052600760205260406000209260208101938451600052600760205260406000209060ff6009820154166004811015610ebf571580613c60575b6138dd90615334565b6138e78351614cc2565b1580613c4f575b15613c195760ff8161393d60018094015491838360a01c1661390f81614500565b1580613bfa575b61391f90615378565b6002858060a01b0391015416848060a01b03600287015416146153ba565b60a81c1661394a81614500565b14908115613bdc575b5015613ba35780516000526007602052604060002093805160005260076020526040600020916139926119a18560018060a01b0360028a015416614dd6565b6001860192600160ff855460a81c166139aa81614500565b14613b8e575b6001810196600160ff895460a81c166139c881614500565b14613b79575b6139d88682615553565b9460038301549260078101938454600091818111600014613b71576139fd9250614bc7565b995b604086015197808911613b69575b508a8811613b5e575b60019a509360028998979692948894613adc868c98613aa9613b279f60008080613b219f613a94613a8d613a7c670de0b6b3a7640000613a628699613a5c8e1515614f0f565b8d614bd4565b9a909f0180549f909a049e6001600160a01b03168f61561b565b9960018060a01b038a541690615693565b988d614bc7565b90546001600160a01b03165af1611d85614c52565b546001600160a01b031690612ac1613acb83613ac5818d6153f7565b936153f7565b85546001600160a01b031692614bc7565b60078201613aeb878254614fb6565b90555460ff8160a01c16613afe81614500565b159081613b45575b50613b2d575b5050613b19838254614fb6565b90555161571b565b5161571b565b01613866565b6006613b3c9101918254614fb6565b90558f80613b0c565b8e915060a81c60ff16613b5781614500565b1438613b06565b929996508992613a16565b97508f613a0d565b5050996139ff565b613b89600483015487101561597e565b6139ce565b613b9e60048801548611156159db565b6139b0565b60405162461bcd60e51b81526020600482015260116024820152704e6f74206d61726b6574206f726465727360781b6044820152606490fd5b600180925060ff91015460a81c16613bf381614500565b1489613953565b5061391f85858189015460a01c16613c1181614500565b149050613916565b60405162461bcd60e51b815260206004820152600e60248201526d13dc99195c9cc8195e1c1a5c995960921b6044820152606490fd5b50613c5a8651614cc2565b156138ee565b5060ff6009830154166004811015610ebf57156138d4565b613c91915060203d602011610a6a57610a5c818361454e565b866117ca565b346102a65760203660031901126102a657613cb0614474565b6002546040516303e21fa960e61b815290602090829060049082906001600160a01b03165afa908115610a7157600091613f71575b506001600160a01b03168015613f3b5760405163c45a015560e01b815291602083600481855afa918215610a7157600493600093613f19575b50602090604051948580926315ab88c960e31b82525afa928315610a7157600093613ef7575b5060405163e6a4390560e01b81526001600160a01b039182166004820181905293821660248201529160209183916044918391165afa908115610a7157600091613ed8575b506001600160a01b0316908115613e9d57604051630240bc6b60e21b815290606082600481865afa908115610a71576000928392613e39575b50602060049460405195868092630dfe168160e01b82525afa8015610a7157604094600091613e1a575b506001600160a01b031603613e1557905b82516001600160701b03928316815291166020820152f35b613dfd565b613e33915060203d602011611f1957611f0a818361454e565b85613dec565b929391506060833d606011613e95575b81613e566060938361454e565b81010312613e9157613e6783614940565b936040613e7660208601614940565b94015163ffffffff81160361257b5750909291906020613dc2565b8380fd5b3d9150613e49565b60405162461bcd60e51b815260206004820152601360248201527214185a5c88191bd95cc81b9bdd08195e1a5cdd606a1b6044820152606490fd5b613ef1915060203d602011611f1957611f0a818361454e565b82613d89565b6020919350613f1290823d8411611f1957611f0a818361454e565b9290613d44565b6020919350613f3490823d8411611f1957611f0a818361454e565b9290613d1e565b60405162461bcd60e51b815260206004820152600e60248201526d149bdd5d195c881b9bdd081cd95d60921b6044820152606490fd5b613f8a915060203d602011611f1957611f0a818361454e565b82613ce5565b346102a65760003660031901126102a657602060405161012c8152f35b346102a65760003660031901126102a65760206040516127108152f35b346102a65760603660031901126102a657613fe3614474565b613feb61448a565b9060443590613ff861525b565b6001600160a01b03169061400d8215156148bc565b6001600160a01b03831692614023841515614900565b6040516370a0823160e01b815230600482015291602083602481875afa928315610a715760009361410f575b5082156140d257806140cc5750815b821161408a5781614081600080516020615c0e83398151915293602093866152d8565b604051908152a3005b60405162461bcd60e51b815260206004820152601a602482015279496e73756666696369656e7420746f6b656e2062616c616e636560301b6044820152606490fd5b9161405e565b60405162461bcd60e51b81526020600482015260156024820152744e6f20746f6b656e7320746f20776974686472617760581b6044820152606490fd5b90926020823d60201161413b575b8161412a6020938361454e565b8101031261257b575051918561404f565b3d915061411d565b346102a65760203660031901126102a65760043561415f61525b565b61271081116141a15760407f9af3456a3485c9955fbe2f71c7de1d0f89f93ff6ae53e43ec73f541db505abdc91601054908060105582519182526020820152a1005b60405162461bcd60e51b8152602060048201526012602482015271088caecd2c2e8d2dedc40e8dede40d0d2ced60731b6044820152606490fd5b346102a65760603660031901126102a6576141f4614474565b60243560028110156102a6576044359060028210156102a65761421692615284565b61421f81614804565b9061422982614571565b614236604051918261454e565b82815261424283614571565b6020820190601f1901368237826000526009602052604060002054906000915b8015158061432e575b156142e95780600052600760205260ff600960406000200154166004811015610ebf5715806142d9575b6142bd575b84600052600b602052604060002090600052602052600160406000200154614262565b9180836142cd6142d39387614892565b526147f5565b9161429a565b506142e381614cc2565b15614295565b81846040519182916020830190602084525180915260408301919060005b818110614315575050500390f35b8251845285945060209384019390920191600101614307565b5085831061426b565b346102a65760003660031901126102a6576020601154604051908152f35b346102a65760403660031901126102a65760043560243561437461525b565b808210156143b657816040917f34a3b94861a601870265c6c4d74c28d1a09f6c803ce42b95e749f46127c8f17093600d5580600e5582519182526020820152a1005b60405162461bcd60e51b815260206004820152602360248201527f4d696e2073697a65206d757374206265206c657373207468616e206d61782073604482015262697a6560e81b6064820152608490fd5b346102a65760203660031901126102a6576020610b29600435614804565b346102a65760203660031901126102a65760043560005260086020526020604060002054604051908152f35b346102a65760203660031901126102a6576020610b2961446f614474565b6145db565b600435906001600160a01b03821682036102a657565b602435906001600160a01b03821682036102a657565b9181601f840112156102a6578235916001600160401b0383116102a657602080850194606085020101116102a657565b9181601f840112156102a6578235916001600160401b0383116102a6576020808501948460051b0101116102a657565b60021115610ebf57565b906004821015610ebf5752565b61018081019081106001600160401b0382111761136957604052565b606081019081106001600160401b0382111761136957604052565b601f909101601f19168101906001600160401b0382119082101761136957604052565b6001600160401b0381116113695760051b60200190565b6001600160401b03811161136957601f01601f191660200190565b908160209103126102a657516001600160a01b03811681036102a65790565b908160209103126102a6575160ff811681036102a65790565b6013546001600160a01b03169081614651575b6145f891506150b1565b80156146015790565b60405162461bcd60e51b815260206004820152602260248201527f4e6f206c697175696469747920617661696c61626c6520696e205632206f7220604482015261563360f01b6064820152608490fd5b6002546040516303e21fa960e61b815290602090829060049082906001600160a01b03165afa908115610a71576000916147d6575b506001600160a01b0316918261469d575b506145ee565b60405163313ce56760e01b815291906001600160a01b038216602084600481845afa938415610a71576000946147a1575b5060206004939495604051948580926315ab88c960e31b82525afa908115610a7157602093600092614781575b506064919260ff604051978895869463bbbb384960e01b8652600486015260018060a01b031660248501521660448301525afa918215610a715760009261474d575b50816147495781614697565b5090565b90916020823d602011614779575b816147686020938361454e565b8101031261257b575051903861473d565b3d915061475b565b6064925061479b90853d8711611f1957611f0a818361454e565b916146fb565b60049394506147c7602091823d84116147cf575b6147bf818361454e565b8101906145c2565b9493506146ce565b503d6147b5565b6147ef915060203d602011611f1957611f0a818361454e565b38614686565b60001981146125275760010190565b600090806000526009602052604060002054805b61482157505090565b80600052600760205260ff600960406000200154166004811015610ebf571580614882575b614872575b81600052600b60205260406000209060005260205260016040600020015490819091614818565b9161487c906147f5565b9161484b565b5061488c81614cc2565b15614846565b80518210156148a65760209160051b010190565b634e487b7160e01b600052603260045260246000fd5b156148c357565b60405162461bcd60e51b8152602060048201526015602482015274496e76616c696420746f6b656e206164647265737360581b6044820152606490fd5b1561490757565b60405162461bcd60e51b8152602060048201526011602482015270125b9d985b1a59081c9958da5c1a595b9d607a1b6044820152606490fd5b51906001600160701b03821682036102a657565b908160209103126102a6575180151581036102a65790565b1561497357565b60405162461bcd60e51b815260206004820152601860248201527710d85b1b195c881a5cc81b9bdd08185d5d1a1bdc9a5e995960421b6044820152606490fd5b156149ba57565b60405162461bcd60e51b815260206004820152601b60248201527a139bc81bdc99195c881b585d18da195cc81d1bc8199d5b199a5b1b602a1b6044820152606490fd5b15614a0457565b60405162461bcd60e51b815260206004820152601860248201527709ad2e6dac2e8c6d0cac840c2e4e4c2f2e640d8cadccee8d60431b6044820152606490fd5b91908110156148a6576060020190565b91908110156148a65760051b0190565b91908260609103126102a657604051614a7c81614533565b604080829480358452602081013560208501520135910152565b6001600160a01b031660008181526006602052604090205415614ac457600052600660205260406000205490565b5060055490565b15614ad257565b60405162461bcd60e51b815260206004820152601460248201527313dc99195c88191bd95cc81b9bdd08195e1a5cdd60621b6044820152606490fd5b15614b1557565b60405162461bcd60e51b815260206004820152600f60248201526e2737ba1037b93232b91037bbb732b960891b6044820152606490fd5b15614b5357565b60405162461bcd60e51b815260206004820152601060248201526f4f72646572206e6f742061637469766560801b6044820152606490fd5b15614b9257565b60405162461bcd60e51b815260206004820152600d60248201526c13dc99195c88195e1c1a5c9959609a1b6044820152606490fd5b9190820391821161252757565b8181029291811591840414171561252757565b8115614bf1570490565b634e487b7160e01b600052601260045260246000fd5b15614c0e57565b60405162461bcd60e51b815260206004820152601c60248201527b125b9cdd59999a58da595b9d08115512081a5b8818dbdb9d1c9858dd60221b6044820152606490fd5b3d15614c7d573d90614c6382614588565b91614c71604051938461454e565b82523d6000602084013e565b606090565b15614c8957565b60405162461bcd60e51b8152602060048201526011602482015270115512081c99599d5b990819985a5b1959607a1b6044820152606490fd5b600081815260076020526040902060010154614ce8906001600160a01b03161515614acb565b6000526007602052600860406000200154421190565b15614d0557565b60405162461bcd60e51b815260206004820152601760248201527608ccaca40e0cae4c6cadce8c2ceca40e8dede40d0d2ced604b1b6044820152606490fd5b15614d4b57565b60405162461bcd60e51b815260206004820152601a602482015279054544c206d7573742062652067726561746572207468616e20360341b6044820152606490fd5b15614d9457565b60405162461bcd60e51b815260206004820152601a602482015279507269636520646966666572656e636520746f6f206c6172676560301b6044820152606490fd5b614ddf906145db565b908115614eb157818114614eaa578180821115614e5157614dff91614bc7565b90614e2b7e068db8bac710cb295e9e1b089a027525460aa64c2f837b4a2339c0ebedfa43831115614d8d565b612710820291808304612710149015171561252757614e4991614be7565b601054101590565b90614e5b91614bc7565b90614e877e068db8bac710cb295e9e1b089a027525460aa64c2f837b4a2339c0ebedfa43831115614d8d565b612710820291808304612710149015171561252757614ea591614be7565b614e49565b5050600190565b5050600090565b15614ebf57565b60405162461bcd60e51b815260206004820152602260248201527f46696c6c20616d6f756e74206d7573742062652067726561746572207468616e604482015261020360f41b6064820152608490fd5b15614f1657565b60405162461bcd60e51b8152602060048201526011602482015270139bc8185b5bdd5b9d081d1bc8199a5b1b607a1b6044820152606490fd5b6001600160a01b039091168152602081019190915260400190565b15614f7157565b60405162461bcd60e51b815260206004820152601d60248201527f455448207472616e7366657220746f2073656c6c6572206661696c65640000006044820152606490fd5b9190820180921161252757565b6004821015610ebf5752565b3562ffffff811681036102a65790565b9062ffffff809160031b9316831b921b19161790565b60005260076020526040600020600181015460ff8160a01c1661501781614500565b159081615062575b50156150455760066005820154910154808211600014614eb15761504291614bc7565b90565b60076003820154910154808211600014614eb15761504291614bc7565b6001915060a81c60ff1661507581614500565b143861501f565b60ff6011199116019060ff821161252757565b60ff166012039060ff821161252757565b60ff16604d811161252757600a0a90565b60408051630fa6707960e21b81526001600160a01b0390921660048301819052919081602481305afa80600092839261521f575b506150f257505050600090565b8115808015615217575b61520e5760206004946040519586809263313ce56760e01b82525afa938415610a71576000946151ed575b5060ff84169360128503615163575090919250670de0b6b3a76400008302928304670de0b6b3a76400001417156125275761504291614be7565b565b9193601211156151b457509061518361517e6151899361508f565b6150a0565b90614be7565b90670de0b6b3a7640000820291808304670de0b6b3a764000014901517156125275761504291614be7565b919261518361517e6151c59361507c565b90670de0b6b3a76400008302928304670de0b6b3a76400001417156125275761504291614be7565b61520791945060203d6020116147cf576147bf818361454e565b9238615127565b50505050600090565b5081156150fc565b929091506040833d604011615253575b8161523c6040938361454e565b8101031261257b57506020825192015190386150e5565b3d915061522f565b6000546001600160a01b0316330361526f57565b63118cdaa760e01b6000523360045260246000fd5b60405160609190911b6001600160601b031916602082019081529290916152aa81614500565b60f81b60348301526152bb81614500565b60f81b6035820152601681526152d260368261454e565b51902090565b61530d61516193926152ff60405194859263a9059cbb60e01b602085015260248401614f4f565b03601f19810184528361454e565b615ae4565b600260015414615323576002600155565b633ee5aeb560e01b60005260046000fd5b1561533b57565b60405162461bcd60e51b81526020600482015260156024820152744f7264657273206d7573742062652061637469766560581b6044820152606490fd5b1561537f57565b60405162461bcd60e51b8152602060048201526013602482015272496e76616c6964206f7264657220747970657360681b6044820152606490fd5b156153c157565b60405162461bcd60e51b815260206004820152600e60248201526d0a8ded6cadc40dad2e6dac2e8c6d60931b6044820152606490fd5b9061540190615b3f565b9060ff8216601281036154145750905090565b6012111561542b5761518361517e6150429361508f565b61543a61517e6150429361507c565b90614bd4565b80600052600b602052604060002082600052602052604060002060ff600282015416156155195780541561550057600181015482600052600b602052604060002082546000526020526001604060002001555b600181018054909190156154e957549082600052600b602052604060002090546000526020526040600020555b600052600b60205260406000209060005260205260006002604082208281558260018201550155565b90505481600052600a6020526040600020556154c0565b6001810154826000526009602052604060002055615493565b60405162461bcd60e51b81526020600482015260126024820152714f72646572206e6f7420696e20717565756560701b6044820152606490fd5b6001810154600160ff8260a81c1661556a81614500565b1490816155ea575b50156155cb57600660058201549101546000918181116000146155c3576151899250614bc7565b670de0b6b3a7640000820291808304670de0b6b3a764000014901517156125275761504291614be7565b505090615599565b905060076003820154910154808211600014614eb15761504291614bc7565b60ff915060a01c166155fb81614500565b1538615572565b9081526001600160a01b03909116602082015260400190565b6156289061543a83614a96565b61271060009104918261563a57505090565b6003546001600160a01b031690813b1561568f5761567284928492604051948580948193637b904e7560e01b83528360048401615602565b03925af180156127015761568557505090565b816147499161454e565b8280fd5b6156a09061543a83614a96565b6127106000910491826156b257505090565b6003546156ce9084906001600160a01b039081169084166152d8565b6003546001600160a01b031690813b1561568f576156728392839260405194858094819362262d0160e91b83528a60048401615602565b6040919493926060820195825260208201520152565b918260005260076020526040600020918360018401805460ff8160a01c1661574281614500565b159081615964575b501561589457600585019485549560068201968754600081831160001461588e57506157768183614bc7565b905b81159283159384615860575b508110801590615859575b1561580b575050600080516020615bee8339815191529793836157fd9693610d319360096157ef9701600160ff19825416179055615802575b5060020154905460ff60a882901c81169260a09290921c16906001600160a01b0316615284565b549260405193849384615705565b0390a2565b548655386157c8565b604080519889526020890192909252908701525050505060608201929092527f686bccf6e038d1f0f07e859bb6b9eada3eab6ae5537f18f14d450031da6f01659250905080608081016157fd565b508261578f565b9750925061271082029682880461271014171561252757615882838c98614be7565b60115410159238615784565b90615778565b600385019485549560078201968754600081831160001461595e57506158ba8183614bc7565b905b81159283159384615930575b5081148015615859571561580b575050600080516020615bee8339815191529793836157fd9693610d319360096157ef9701600160ff19825416179055615802575060020154905460ff60a882901c81169260a09290921c16906001600160a01b0316615284565b9750925061271082029682880461271014171561252757615952838c98614be7565b601154101592386158c8565b906158bc565b6001915060a81c60ff1661597781614500565b143861574a565b1561598557565b60405162461bcd60e51b815260206004820152602860248201527f457865637574696f6e2070726963652062656c6f772073656c6c65722773206d604482015267696e20707269636560c01b6064820152608490fd5b156159e257565b60405162461bcd60e51b815260206004820152602960248201527f457865637574696f6e20707269636520657863656564732062757965722773206044820152686d617820707269636560b81b6064820152608490fd5b15615a4057565b60405162461bcd60e51b815260206004820152603360248201527f457865637574696f6e20707269636520646576696174657320746f6f206d7563604482015272682066726f6d206d61726b657420707269636560681b6064820152608490fd5b90615aab90615b3f565b9060ff821660128103615abe5750905090565b60121115615ad55761543a61517e6150429361508f565b61518361517e6150429361507c565b906000602091828151910182855af115610a71576000513d615b3657506001600160a01b0381163b155b615b155750565b635274afe760e01b60009081526001600160a01b0391909116600452602490fd5b60011415615b0e565b60405163313ce56760e01b815290602090829060049082906001600160a01b03165afa908115610a7157600091615b74575090565b615042915060203d6020116147cf576147bf818361454e565b9081526001600160a01b039182166020820152911660408201526060019056fee2f9e9065995f6d98a1dc7a86e975c10b2848f8af0a077c8d675b71d486dbd11e332d84b9d7b6ed7b61613e3346be661c47cd1bcb330e8cdb0887aa2d3425e6ea4360fd9d66ebd27e7e5e650a0dbc45ff3fe0e9560cf7b07b3c748dc95489f52da0612d7ca9ff90ca7143a6021ba8938994f8d045b2834ae585fd07b27ea697c8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0a264697066735822122036af7e26ffd76815a77b0bef2300b91c76d5054482b1c9e1e3f77de111cf31af64736f6c634300081a0033
Verified Source Code Full Match
Compiler: v0.8.26+commit.8a97fa7a
EVM: paris
Optimization: Yes (1 runs)
Ownable.sol 100 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)
pragma solidity ^0.8.20;
import {Context} from "../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.
*
* The initial owner is set to the address provided by the deployer. 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;
/**
* @dev The caller account is not authorized to perform an operation.
*/
error OwnableUnauthorizedAccount(address account);
/**
* @dev The owner is not a valid owner account. (eg. `address(0)`)
*/
error OwnableInvalidOwner(address owner);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the address provided by the deployer as the initial owner.
*/
constructor(address initialOwner) {
if (initialOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(initialOwner);
}
/**
* @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 {
if (owner() != _msgSender()) {
revert OwnableUnauthorizedAccount(_msgSender());
}
}
/**
* @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 {
if (newOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_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);
}
}
IERC1363.sol 86 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (interfaces/IERC1363.sol)
pragma solidity ^0.8.20;
import {IERC20} from "./IERC20.sol";
import {IERC165} from "./IERC165.sol";
/**
* @title IERC1363
* @dev Interface of the ERC-1363 standard as defined in the https://eips.ethereum.org/EIPS/eip-1363[ERC-1363].
*
* Defines an extension interface for ERC-20 tokens that supports executing code on a recipient contract
* after `transfer` or `transferFrom`, or code on a spender contract after `approve`, in a single transaction.
*/
interface IERC1363 is IERC20, IERC165 {
/*
* Note: the ERC-165 identifier for this interface is 0xb0202a11.
* 0xb0202a11 ===
* bytes4(keccak256('transferAndCall(address,uint256)')) ^
* bytes4(keccak256('transferAndCall(address,uint256,bytes)')) ^
* bytes4(keccak256('transferFromAndCall(address,address,uint256)')) ^
* bytes4(keccak256('transferFromAndCall(address,address,uint256,bytes)')) ^
* bytes4(keccak256('approveAndCall(address,uint256)')) ^
* bytes4(keccak256('approveAndCall(address,uint256,bytes)'))
*/
/**
* @dev Moves a `value` amount of tokens from the caller's account to `to`
* and then calls {IERC1363Receiver-onTransferReceived} on `to`.
* @param to The address which you want to transfer to.
* @param value The amount of tokens to be transferred.
* @return A boolean value indicating whether the operation succeeded unless throwing.
*/
function transferAndCall(address to, uint256 value) external returns (bool);
/**
* @dev Moves a `value` amount of tokens from the caller's account to `to`
* and then calls {IERC1363Receiver-onTransferReceived} on `to`.
* @param to The address which you want to transfer to.
* @param value The amount of tokens to be transferred.
* @param data Additional data with no specified format, sent in call to `to`.
* @return A boolean value indicating whether the operation succeeded unless throwing.
*/
function transferAndCall(address to, uint256 value, bytes calldata data) external returns (bool);
/**
* @dev Moves a `value` amount of tokens from `from` to `to` using the allowance mechanism
* and then calls {IERC1363Receiver-onTransferReceived} on `to`.
* @param from The address which you want to send tokens from.
* @param to The address which you want to transfer to.
* @param value The amount of tokens to be transferred.
* @return A boolean value indicating whether the operation succeeded unless throwing.
*/
function transferFromAndCall(address from, address to, uint256 value) external returns (bool);
/**
* @dev Moves a `value` amount of tokens from `from` to `to` using the allowance mechanism
* and then calls {IERC1363Receiver-onTransferReceived} on `to`.
* @param from The address which you want to send tokens from.
* @param to The address which you want to transfer to.
* @param value The amount of tokens to be transferred.
* @param data Additional data with no specified format, sent in call to `to`.
* @return A boolean value indicating whether the operation succeeded unless throwing.
*/
function transferFromAndCall(address from, address to, uint256 value, bytes calldata data) external returns (bool);
/**
* @dev Sets a `value` amount of tokens as the allowance of `spender` over the
* caller's tokens and then calls {IERC1363Spender-onApprovalReceived} on `spender`.
* @param spender The address which will spend the funds.
* @param value The amount of tokens to be spent.
* @return A boolean value indicating whether the operation succeeded unless throwing.
*/
function approveAndCall(address spender, uint256 value) external returns (bool);
/**
* @dev Sets a `value` amount of tokens as the allowance of `spender` over the
* caller's tokens and then calls {IERC1363Spender-onApprovalReceived} on `spender`.
* @param spender The address which will spend the funds.
* @param value The amount of tokens to be spent.
* @param data Additional data with no specified format, sent in call to `spender`.
* @return A boolean value indicating whether the operation succeeded unless throwing.
*/
function approveAndCall(address spender, uint256 value, bytes calldata data) external returns (bool);
}
IERC165.sol 6 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (interfaces/IERC165.sol)
pragma solidity ^0.8.20;
import {IERC165} from "../utils/introspection/IERC165.sol";
IERC20.sol 6 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (interfaces/IERC20.sol)
pragma solidity ^0.8.20;
import {IERC20} from "../token/ERC20/IERC20.sol";
IERC20Metadata.sol 26 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (token/ERC20/extensions/IERC20Metadata.sol)
pragma solidity ^0.8.20;
import {IERC20} from "../IERC20.sol";
/**
* @dev Interface for the optional metadata functions from the ERC-20 standard.
*/
interface IERC20Metadata is IERC20 {
/**
* @dev Returns the name of the token.
*/
function name() external view returns (string memory);
/**
* @dev Returns the symbol of the token.
*/
function symbol() external view returns (string memory);
/**
* @dev Returns the decimals places of the token.
*/
function decimals() external view returns (uint8);
}
IERC20.sol 79 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (token/ERC20/IERC20.sol)
pragma solidity ^0.8.20;
/**
* @dev Interface of the ERC-20 standard as defined in the ERC.
*/
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 value of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the value of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves a `value` amount of 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 value) 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 a `value` amount of tokens 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 value) external returns (bool);
/**
* @dev Moves a `value` amount of tokens from `from` to `to` using the
* allowance mechanism. `value` 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 value) external returns (bool);
}
SafeERC20.sol 212 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.3.0) (token/ERC20/utils/SafeERC20.sol)
pragma solidity ^0.8.20;
import {IERC20} from "../IERC20.sol";
import {IERC1363} from "../../../interfaces/IERC1363.sol";
/**
* @title SafeERC20
* @dev Wrappers around ERC-20 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 {
/**
* @dev An operation with an ERC-20 token failed.
*/
error SafeERC20FailedOperation(address token);
/**
* @dev Indicates a failed `decreaseAllowance` request.
*/
error SafeERC20FailedDecreaseAllowance(address spender, uint256 currentAllowance, uint256 requestedDecrease);
/**
* @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.encodeCall(token.transfer, (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.encodeCall(token.transferFrom, (from, to, value)));
}
/**
* @dev Variant of {safeTransfer} that returns a bool instead of reverting if the operation is not successful.
*/
function trySafeTransfer(IERC20 token, address to, uint256 value) internal returns (bool) {
return _callOptionalReturnBool(token, abi.encodeCall(token.transfer, (to, value)));
}
/**
* @dev Variant of {safeTransferFrom} that returns a bool instead of reverting if the operation is not successful.
*/
function trySafeTransferFrom(IERC20 token, address from, address to, uint256 value) internal returns (bool) {
return _callOptionalReturnBool(token, abi.encodeCall(token.transferFrom, (from, to, 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.
*
* IMPORTANT: If the token implements ERC-7674 (ERC-20 with temporary allowance), and if the "client"
* smart contract uses ERC-7674 to set temporary allowances, then the "client" smart contract should avoid using
* this function. Performing a {safeIncreaseAllowance} or {safeDecreaseAllowance} operation on a token contract
* that has a non-zero temporary allowance (for that particular owner-spender) will result in unexpected behavior.
*/
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 oldAllowance = token.allowance(address(this), spender);
forceApprove(token, spender, oldAllowance + value);
}
/**
* @dev Decrease the calling contract's allowance toward `spender` by `requestedDecrease`. If `token` returns no
* value, non-reverting calls are assumed to be successful.
*
* IMPORTANT: If the token implements ERC-7674 (ERC-20 with temporary allowance), and if the "client"
* smart contract uses ERC-7674 to set temporary allowances, then the "client" smart contract should avoid using
* this function. Performing a {safeIncreaseAllowance} or {safeDecreaseAllowance} operation on a token contract
* that has a non-zero temporary allowance (for that particular owner-spender) will result in unexpected behavior.
*/
function safeDecreaseAllowance(IERC20 token, address spender, uint256 requestedDecrease) internal {
unchecked {
uint256 currentAllowance = token.allowance(address(this), spender);
if (currentAllowance < requestedDecrease) {
revert SafeERC20FailedDecreaseAllowance(spender, currentAllowance, requestedDecrease);
}
forceApprove(token, spender, currentAllowance - requestedDecrease);
}
}
/**
* @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.
*
* NOTE: If the token implements ERC-7674, this function will not modify any temporary allowance. This function
* only sets the "standard" allowance. Any temporary allowance will remain active, in addition to the value being
* set here.
*/
function forceApprove(IERC20 token, address spender, uint256 value) internal {
bytes memory approvalCall = abi.encodeCall(token.approve, (spender, value));
if (!_callOptionalReturnBool(token, approvalCall)) {
_callOptionalReturn(token, abi.encodeCall(token.approve, (spender, 0)));
_callOptionalReturn(token, approvalCall);
}
}
/**
* @dev Performs an {ERC1363} transferAndCall, with a fallback to the simple {ERC20} transfer if the target has no
* code. This can be used to implement an {ERC721}-like safe transfer that rely on {ERC1363} checks when
* targeting contracts.
*
* Reverts if the returned value is other than `true`.
*/
function transferAndCallRelaxed(IERC1363 token, address to, uint256 value, bytes memory data) internal {
if (to.code.length == 0) {
safeTransfer(token, to, value);
} else if (!token.transferAndCall(to, value, data)) {
revert SafeERC20FailedOperation(address(token));
}
}
/**
* @dev Performs an {ERC1363} transferFromAndCall, with a fallback to the simple {ERC20} transferFrom if the target
* has no code. This can be used to implement an {ERC721}-like safe transfer that rely on {ERC1363} checks when
* targeting contracts.
*
* Reverts if the returned value is other than `true`.
*/
function transferFromAndCallRelaxed(
IERC1363 token,
address from,
address to,
uint256 value,
bytes memory data
) internal {
if (to.code.length == 0) {
safeTransferFrom(token, from, to, value);
} else if (!token.transferFromAndCall(from, to, value, data)) {
revert SafeERC20FailedOperation(address(token));
}
}
/**
* @dev Performs an {ERC1363} approveAndCall, with a fallback to the simple {ERC20} approve if the target has no
* code. This can be used to implement an {ERC721}-like safe transfer that rely on {ERC1363} checks when
* targeting contracts.
*
* NOTE: When the recipient address (`to`) has no code (i.e. is an EOA), this function behaves as {forceApprove}.
* Opposedly, when the recipient address (`to`) has code, this function only attempts to call {ERC1363-approveAndCall}
* once without retrying, and relies on the returned value to be true.
*
* Reverts if the returned value is other than `true`.
*/
function approveAndCallRelaxed(IERC1363 token, address to, uint256 value, bytes memory data) internal {
if (to.code.length == 0) {
forceApprove(token, to, value);
} else if (!token.approveAndCall(to, value, data)) {
revert SafeERC20FailedOperation(address(token));
}
}
/**
* @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 {_callOptionalReturnBool} that reverts if call fails to meet the requirements.
*/
function _callOptionalReturn(IERC20 token, bytes memory data) private {
uint256 returnSize;
uint256 returnValue;
assembly ("memory-safe") {
let success := call(gas(), token, 0, add(data, 0x20), mload(data), 0, 0x20)
// bubble errors
if iszero(success) {
let ptr := mload(0x40)
returndatacopy(ptr, 0, returndatasize())
revert(ptr, returndatasize())
}
returnSize := returndatasize()
returnValue := mload(0)
}
if (returnSize == 0 ? address(token).code.length == 0 : returnValue != 1) {
revert SafeERC20FailedOperation(address(token));
}
}
/**
* @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 silently catches all reverts and returns a bool instead.
*/
function _callOptionalReturnBool(IERC20 token, bytes memory data) private returns (bool) {
bool success;
uint256 returnSize;
uint256 returnValue;
assembly ("memory-safe") {
success := call(gas(), token, 0, add(data, 0x20), mload(data), 0, 0x20)
returnSize := returndatasize()
returnValue := mload(0)
}
return success && (returnSize == 0 ? address(token).code.length > 0 : returnValue == 1);
}
}
Context.sol 28 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)
pragma solidity ^0.8.20;
/**
* @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;
}
}
Create2.sol 92 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/Create2.sol)
pragma solidity ^0.8.20;
import {Errors} from "./Errors.sol";
/**
* @dev Helper to make usage of the `CREATE2` EVM opcode easier and safer.
* `CREATE2` can be used to compute in advance the address where a smart
* contract will be deployed, which allows for interesting new mechanisms known
* as 'counterfactual interactions'.
*
* See the https://eips.ethereum.org/EIPS/eip-1014#motivation[EIP] for more
* information.
*/
library Create2 {
/**
* @dev There's no code to deploy.
*/
error Create2EmptyBytecode();
/**
* @dev Deploys a contract using `CREATE2`. The address where the contract
* will be deployed can be known in advance via {computeAddress}.
*
* The bytecode for a contract can be obtained from Solidity with
* `type(contractName).creationCode`.
*
* Requirements:
*
* - `bytecode` must not be empty.
* - `salt` must have not been used for `bytecode` already.
* - the factory must have a balance of at least `amount`.
* - if `amount` is non-zero, `bytecode` must have a `payable` constructor.
*/
function deploy(uint256 amount, bytes32 salt, bytes memory bytecode) internal returns (address addr) {
if (address(this).balance < amount) {
revert Errors.InsufficientBalance(address(this).balance, amount);
}
if (bytecode.length == 0) {
revert Create2EmptyBytecode();
}
assembly ("memory-safe") {
addr := create2(amount, add(bytecode, 0x20), mload(bytecode), salt)
// if no address was created, and returndata is not empty, bubble revert
if and(iszero(addr), not(iszero(returndatasize()))) {
let p := mload(0x40)
returndatacopy(p, 0, returndatasize())
revert(p, returndatasize())
}
}
if (addr == address(0)) {
revert Errors.FailedDeployment();
}
}
/**
* @dev Returns the address where a contract will be stored if deployed via {deploy}. Any change in the
* `bytecodeHash` or `salt` will result in a new destination address.
*/
function computeAddress(bytes32 salt, bytes32 bytecodeHash) internal view returns (address) {
return computeAddress(salt, bytecodeHash, address(this));
}
/**
* @dev Returns the address where a contract will be stored if deployed via {deploy} from a contract located at
* `deployer`. If `deployer` is this contract's address, returns the same value as {computeAddress}.
*/
function computeAddress(bytes32 salt, bytes32 bytecodeHash, address deployer) internal pure returns (address addr) {
assembly ("memory-safe") {
let ptr := mload(0x40) // Get free memory pointer
// | | ↓ ptr ... ↓ ptr + 0x0B (start) ... ↓ ptr + 0x20 ... ↓ ptr + 0x40 ... |
// |-------------------|---------------------------------------------------------------------------|
// | bytecodeHash | CCCCCCCCCCCCC...CC |
// | salt | BBBBBBBBBBBBB...BB |
// | deployer | 000000...0000AAAAAAAAAAAAAAAAAAA...AA |
// | 0xFF | FF |
// |-------------------|---------------------------------------------------------------------------|
// | memory | 000000...00FFAAAAAAAAAAAAAAAAAAA...AABBBBBBBBBBBBB...BBCCCCCCCCCCCCC...CC |
// | keccak(start, 85) | ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ |
mstore(add(ptr, 0x40), bytecodeHash)
mstore(add(ptr, 0x20), salt)
mstore(ptr, deployer) // Right-aligned with 12 preceding garbage bytes
let start := add(ptr, 0x0b) // The hashed data starts at the final garbage byte which we will set to 0xff
mstore8(start, 0xff)
addr := and(keccak256(start, 85), 0xffffffffffffffffffffffffffffffffffffffff)
}
}
}
Hashes.sol 31 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.3.0) (utils/cryptography/Hashes.sol)
pragma solidity ^0.8.20;
/**
* @dev Library of standard hash functions.
*
* _Available since v5.1._
*/
library Hashes {
/**
* @dev Commutative Keccak256 hash of a sorted pair of bytes32. Frequently used when working with merkle proofs.
*
* NOTE: Equivalent to the `standardNodeHash` in our https://github.com/OpenZeppelin/merkle-tree[JavaScript library].
*/
function commutativeKeccak256(bytes32 a, bytes32 b) internal pure returns (bytes32) {
return a < b ? efficientKeccak256(a, b) : efficientKeccak256(b, a);
}
/**
* @dev Implementation of keccak256(abi.encode(a, b)) that doesn't allocate or expand memory.
*/
function efficientKeccak256(bytes32 a, bytes32 b) internal pure returns (bytes32 value) {
assembly ("memory-safe") {
mstore(0x00, a)
mstore(0x20, b)
value := keccak256(0x00, 0x40)
}
}
}
MerkleProof.sol 514 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/cryptography/MerkleProof.sol)
// This file was procedurally generated from scripts/generate/templates/MerkleProof.js.
pragma solidity ^0.8.20;
import {Hashes} from "./Hashes.sol";
/**
* @dev These functions deal with verification of Merkle Tree proofs.
*
* The tree and the proofs can be generated using our
* https://github.com/OpenZeppelin/merkle-tree[JavaScript library].
* You will find a quickstart guide in the readme.
*
* WARNING: You should avoid using leaf values that are 64 bytes long prior to
* hashing, or use a hash function other than keccak256 for hashing leaves.
* This is because the concatenation of a sorted pair of internal nodes in
* the Merkle tree could be reinterpreted as a leaf value.
* OpenZeppelin's JavaScript library generates Merkle trees that are safe
* against this attack out of the box.
*
* IMPORTANT: Consider memory side-effects when using custom hashing functions
* that access memory in an unsafe way.
*
* NOTE: This library supports proof verification for merkle trees built using
* custom _commutative_ hashing functions (i.e. `H(a, b) == H(b, a)`). Proving
* leaf inclusion in trees built using non-commutative hashing functions requires
* additional logic that is not supported by this library.
*/
library MerkleProof {
/**
*@dev The multiproof provided is not valid.
*/
error MerkleProofInvalidMultiproof();
/**
* @dev Returns true if a `leaf` can be proved to be a part of a Merkle tree
* defined by `root`. For this, a `proof` must be provided, containing
* sibling hashes on the branch from the leaf to the root of the tree. Each
* pair of leaves and each pair of pre-images are assumed to be sorted.
*
* This version handles proofs in memory with the default hashing function.
*/
function verify(bytes32[] memory proof, bytes32 root, bytes32 leaf) internal pure returns (bool) {
return processProof(proof, leaf) == root;
}
/**
* @dev Returns the rebuilt hash obtained by traversing a Merkle tree up
* from `leaf` using `proof`. A `proof` is valid if and only if the rebuilt
* hash matches the root of the tree. When processing the proof, the pairs
* of leaves & pre-images are assumed to be sorted.
*
* This version handles proofs in memory with the default hashing function.
*/
function processProof(bytes32[] memory proof, bytes32 leaf) internal pure returns (bytes32) {
bytes32 computedHash = leaf;
for (uint256 i = 0; i < proof.length; i++) {
computedHash = Hashes.commutativeKeccak256(computedHash, proof[i]);
}
return computedHash;
}
/**
* @dev Returns true if a `leaf` can be proved to be a part of a Merkle tree
* defined by `root`. For this, a `proof` must be provided, containing
* sibling hashes on the branch from the leaf to the root of the tree. Each
* pair of leaves and each pair of pre-images are assumed to be sorted.
*
* This version handles proofs in memory with a custom hashing function.
*/
function verify(
bytes32[] memory proof,
bytes32 root,
bytes32 leaf,
function(bytes32, bytes32) view returns (bytes32) hasher
) internal view returns (bool) {
return processProof(proof, leaf, hasher) == root;
}
/**
* @dev Returns the rebuilt hash obtained by traversing a Merkle tree up
* from `leaf` using `proof`. A `proof` is valid if and only if the rebuilt
* hash matches the root of the tree. When processing the proof, the pairs
* of leaves & pre-images are assumed to be sorted.
*
* This version handles proofs in memory with a custom hashing function.
*/
function processProof(
bytes32[] memory proof,
bytes32 leaf,
function(bytes32, bytes32) view returns (bytes32) hasher
) internal view returns (bytes32) {
bytes32 computedHash = leaf;
for (uint256 i = 0; i < proof.length; i++) {
computedHash = hasher(computedHash, proof[i]);
}
return computedHash;
}
/**
* @dev Returns true if a `leaf` can be proved to be a part of a Merkle tree
* defined by `root`. For this, a `proof` must be provided, containing
* sibling hashes on the branch from the leaf to the root of the tree. Each
* pair of leaves and each pair of pre-images are assumed to be sorted.
*
* This version handles proofs in calldata with the default hashing function.
*/
function verifyCalldata(bytes32[] calldata proof, bytes32 root, bytes32 leaf) internal pure returns (bool) {
return processProofCalldata(proof, leaf) == root;
}
/**
* @dev Returns the rebuilt hash obtained by traversing a Merkle tree up
* from `leaf` using `proof`. A `proof` is valid if and only if the rebuilt
* hash matches the root of the tree. When processing the proof, the pairs
* of leaves & pre-images are assumed to be sorted.
*
* This version handles proofs in calldata with the default hashing function.
*/
function processProofCalldata(bytes32[] calldata proof, bytes32 leaf) internal pure returns (bytes32) {
bytes32 computedHash = leaf;
for (uint256 i = 0; i < proof.length; i++) {
computedHash = Hashes.commutativeKeccak256(computedHash, proof[i]);
}
return computedHash;
}
/**
* @dev Returns true if a `leaf` can be proved to be a part of a Merkle tree
* defined by `root`. For this, a `proof` must be provided, containing
* sibling hashes on the branch from the leaf to the root of the tree. Each
* pair of leaves and each pair of pre-images are assumed to be sorted.
*
* This version handles proofs in calldata with a custom hashing function.
*/
function verifyCalldata(
bytes32[] calldata proof,
bytes32 root,
bytes32 leaf,
function(bytes32, bytes32) view returns (bytes32) hasher
) internal view returns (bool) {
return processProofCalldata(proof, leaf, hasher) == root;
}
/**
* @dev Returns the rebuilt hash obtained by traversing a Merkle tree up
* from `leaf` using `proof`. A `proof` is valid if and only if the rebuilt
* hash matches the root of the tree. When processing the proof, the pairs
* of leaves & pre-images are assumed to be sorted.
*
* This version handles proofs in calldata with a custom hashing function.
*/
function processProofCalldata(
bytes32[] calldata proof,
bytes32 leaf,
function(bytes32, bytes32) view returns (bytes32) hasher
) internal view returns (bytes32) {
bytes32 computedHash = leaf;
for (uint256 i = 0; i < proof.length; i++) {
computedHash = hasher(computedHash, proof[i]);
}
return computedHash;
}
/**
* @dev Returns true if the `leaves` can be simultaneously proven to be a part of a Merkle tree defined by
* `root`, according to `proof` and `proofFlags` as described in {processMultiProof}.
*
* This version handles multiproofs in memory with the default hashing function.
*
* CAUTION: Not all Merkle trees admit multiproofs. See {processMultiProof} for details.
*
* NOTE: Consider the case where `root == proof[0] && leaves.length == 0` as it will return `true`.
* The `leaves` must be validated independently. See {processMultiProof}.
*/
function multiProofVerify(
bytes32[] memory proof,
bool[] memory proofFlags,
bytes32 root,
bytes32[] memory leaves
) internal pure returns (bool) {
return processMultiProof(proof, proofFlags, leaves) == root;
}
/**
* @dev Returns the root of a tree reconstructed from `leaves` and sibling nodes in `proof`. The reconstruction
* proceeds by incrementally reconstructing all inner nodes by combining a leaf/inner node with either another
* leaf/inner node or a proof sibling node, depending on whether each `proofFlags` item is true or false
* respectively.
*
* This version handles multiproofs in memory with the default hashing function.
*
* CAUTION: Not all Merkle trees admit multiproofs. To use multiproofs, it is sufficient to ensure that: 1) the tree
* is complete (but not necessarily perfect), 2) the leaves to be proven are in the opposite order they are in the
* tree (i.e., as seen from right to left starting at the deepest layer and continuing at the next layer).
*
* NOTE: The _empty set_ (i.e. the case where `proof.length == 1 && leaves.length == 0`) is considered a no-op,
* and therefore a valid multiproof (i.e. it returns `proof[0]`). Consider disallowing this case if you're not
* validating the leaves elsewhere.
*/
function processMultiProof(
bytes32[] memory proof,
bool[] memory proofFlags,
bytes32[] memory leaves
) internal pure returns (bytes32 merkleRoot) {
// This function rebuilds the root hash by traversing the tree up from the leaves. The root is rebuilt by
// consuming and producing values on a queue. The queue starts with the `leaves` array, then goes onto the
// `hashes` array. At the end of the process, the last hash in the `hashes` array should contain the root of
// the Merkle tree.
uint256 leavesLen = leaves.length;
uint256 proofFlagsLen = proofFlags.length;
// Check proof validity.
if (leavesLen + proof.length != proofFlagsLen + 1) {
revert MerkleProofInvalidMultiproof();
}
// The xxxPos values are "pointers" to the next value to consume in each array. All accesses are done using
// `xxx[xxxPos++]`, which return the current value and increment the pointer, thus mimicking a queue's "pop".
bytes32[] memory hashes = new bytes32[](proofFlagsLen);
uint256 leafPos = 0;
uint256 hashPos = 0;
uint256 proofPos = 0;
// At each step, we compute the next hash using two values:
// - a value from the "main queue". If not all leaves have been consumed, we get the next leaf, otherwise we
// get the next hash.
// - depending on the flag, either another value from the "main queue" (merging branches) or an element from the
// `proof` array.
for (uint256 i = 0; i < proofFlagsLen; i++) {
bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
bytes32 b = proofFlags[i]
? (leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++])
: proof[proofPos++];
hashes[i] = Hashes.commutativeKeccak256(a, b);
}
if (proofFlagsLen > 0) {
if (proofPos != proof.length) {
revert MerkleProofInvalidMultiproof();
}
unchecked {
return hashes[proofFlagsLen - 1];
}
} else if (leavesLen > 0) {
return leaves[0];
} else {
return proof[0];
}
}
/**
* @dev Returns true if the `leaves` can be simultaneously proven to be a part of a Merkle tree defined by
* `root`, according to `proof` and `proofFlags` as described in {processMultiProof}.
*
* This version handles multiproofs in memory with a custom hashing function.
*
* CAUTION: Not all Merkle trees admit multiproofs. See {processMultiProof} for details.
*
* NOTE: Consider the case where `root == proof[0] && leaves.length == 0` as it will return `true`.
* The `leaves` must be validated independently. See {processMultiProof}.
*/
function multiProofVerify(
bytes32[] memory proof,
bool[] memory proofFlags,
bytes32 root,
bytes32[] memory leaves,
function(bytes32, bytes32) view returns (bytes32) hasher
) internal view returns (bool) {
return processMultiProof(proof, proofFlags, leaves, hasher) == root;
}
/**
* @dev Returns the root of a tree reconstructed from `leaves` and sibling nodes in `proof`. The reconstruction
* proceeds by incrementally reconstructing all inner nodes by combining a leaf/inner node with either another
* leaf/inner node or a proof sibling node, depending on whether each `proofFlags` item is true or false
* respectively.
*
* This version handles multiproofs in memory with a custom hashing function.
*
* CAUTION: Not all Merkle trees admit multiproofs. To use multiproofs, it is sufficient to ensure that: 1) the tree
* is complete (but not necessarily perfect), 2) the leaves to be proven are in the opposite order they are in the
* tree (i.e., as seen from right to left starting at the deepest layer and continuing at the next layer).
*
* NOTE: The _empty set_ (i.e. the case where `proof.length == 1 && leaves.length == 0`) is considered a no-op,
* and therefore a valid multiproof (i.e. it returns `proof[0]`). Consider disallowing this case if you're not
* validating the leaves elsewhere.
*/
function processMultiProof(
bytes32[] memory proof,
bool[] memory proofFlags,
bytes32[] memory leaves,
function(bytes32, bytes32) view returns (bytes32) hasher
) internal view returns (bytes32 merkleRoot) {
// This function rebuilds the root hash by traversing the tree up from the leaves. The root is rebuilt by
// consuming and producing values on a queue. The queue starts with the `leaves` array, then goes onto the
// `hashes` array. At the end of the process, the last hash in the `hashes` array should contain the root of
// the Merkle tree.
uint256 leavesLen = leaves.length;
uint256 proofFlagsLen = proofFlags.length;
// Check proof validity.
if (leavesLen + proof.length != proofFlagsLen + 1) {
revert MerkleProofInvalidMultiproof();
}
// The xxxPos values are "pointers" to the next value to consume in each array. All accesses are done using
// `xxx[xxxPos++]`, which return the current value and increment the pointer, thus mimicking a queue's "pop".
bytes32[] memory hashes = new bytes32[](proofFlagsLen);
uint256 leafPos = 0;
uint256 hashPos = 0;
uint256 proofPos = 0;
// At each step, we compute the next hash using two values:
// - a value from the "main queue". If not all leaves have been consumed, we get the next leaf, otherwise we
// get the next hash.
// - depending on the flag, either another value from the "main queue" (merging branches) or an element from the
// `proof` array.
for (uint256 i = 0; i < proofFlagsLen; i++) {
bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
bytes32 b = proofFlags[i]
? (leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++])
: proof[proofPos++];
hashes[i] = hasher(a, b);
}
if (proofFlagsLen > 0) {
if (proofPos != proof.length) {
revert MerkleProofInvalidMultiproof();
}
unchecked {
return hashes[proofFlagsLen - 1];
}
} else if (leavesLen > 0) {
return leaves[0];
} else {
return proof[0];
}
}
/**
* @dev Returns true if the `leaves` can be simultaneously proven to be a part of a Merkle tree defined by
* `root`, according to `proof` and `proofFlags` as described in {processMultiProof}.
*
* This version handles multiproofs in calldata with the default hashing function.
*
* CAUTION: Not all Merkle trees admit multiproofs. See {processMultiProof} for details.
*
* NOTE: Consider the case where `root == proof[0] && leaves.length == 0` as it will return `true`.
* The `leaves` must be validated independently. See {processMultiProofCalldata}.
*/
function multiProofVerifyCalldata(
bytes32[] calldata proof,
bool[] calldata proofFlags,
bytes32 root,
bytes32[] memory leaves
) internal pure returns (bool) {
return processMultiProofCalldata(proof, proofFlags, leaves) == root;
}
/**
* @dev Returns the root of a tree reconstructed from `leaves` and sibling nodes in `proof`. The reconstruction
* proceeds by incrementally reconstructing all inner nodes by combining a leaf/inner node with either another
* leaf/inner node or a proof sibling node, depending on whether each `proofFlags` item is true or false
* respectively.
*
* This version handles multiproofs in calldata with the default hashing function.
*
* CAUTION: Not all Merkle trees admit multiproofs. To use multiproofs, it is sufficient to ensure that: 1) the tree
* is complete (but not necessarily perfect), 2) the leaves to be proven are in the opposite order they are in the
* tree (i.e., as seen from right to left starting at the deepest layer and continuing at the next layer).
*
* NOTE: The _empty set_ (i.e. the case where `proof.length == 1 && leaves.length == 0`) is considered a no-op,
* and therefore a valid multiproof (i.e. it returns `proof[0]`). Consider disallowing this case if you're not
* validating the leaves elsewhere.
*/
function processMultiProofCalldata(
bytes32[] calldata proof,
bool[] calldata proofFlags,
bytes32[] memory leaves
) internal pure returns (bytes32 merkleRoot) {
// This function rebuilds the root hash by traversing the tree up from the leaves. The root is rebuilt by
// consuming and producing values on a queue. The queue starts with the `leaves` array, then goes onto the
// `hashes` array. At the end of the process, the last hash in the `hashes` array should contain the root of
// the Merkle tree.
uint256 leavesLen = leaves.length;
uint256 proofFlagsLen = proofFlags.length;
// Check proof validity.
if (leavesLen + proof.length != proofFlagsLen + 1) {
revert MerkleProofInvalidMultiproof();
}
// The xxxPos values are "pointers" to the next value to consume in each array. All accesses are done using
// `xxx[xxxPos++]`, which return the current value and increment the pointer, thus mimicking a queue's "pop".
bytes32[] memory hashes = new bytes32[](proofFlagsLen);
uint256 leafPos = 0;
uint256 hashPos = 0;
uint256 proofPos = 0;
// At each step, we compute the next hash using two values:
// - a value from the "main queue". If not all leaves have been consumed, we get the next leaf, otherwise we
// get the next hash.
// - depending on the flag, either another value from the "main queue" (merging branches) or an element from the
// `proof` array.
for (uint256 i = 0; i < proofFlagsLen; i++) {
bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
bytes32 b = proofFlags[i]
? (leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++])
: proof[proofPos++];
hashes[i] = Hashes.commutativeKeccak256(a, b);
}
if (proofFlagsLen > 0) {
if (proofPos != proof.length) {
revert MerkleProofInvalidMultiproof();
}
unchecked {
return hashes[proofFlagsLen - 1];
}
} else if (leavesLen > 0) {
return leaves[0];
} else {
return proof[0];
}
}
/**
* @dev Returns true if the `leaves` can be simultaneously proven to be a part of a Merkle tree defined by
* `root`, according to `proof` and `proofFlags` as described in {processMultiProof}.
*
* This version handles multiproofs in calldata with a custom hashing function.
*
* CAUTION: Not all Merkle trees admit multiproofs. See {processMultiProof} for details.
*
* NOTE: Consider the case where `root == proof[0] && leaves.length == 0` as it will return `true`.
* The `leaves` must be validated independently. See {processMultiProofCalldata}.
*/
function multiProofVerifyCalldata(
bytes32[] calldata proof,
bool[] calldata proofFlags,
bytes32 root,
bytes32[] memory leaves,
function(bytes32, bytes32) view returns (bytes32) hasher
) internal view returns (bool) {
return processMultiProofCalldata(proof, proofFlags, leaves, hasher) == root;
}
/**
* @dev Returns the root of a tree reconstructed from `leaves` and sibling nodes in `proof`. The reconstruction
* proceeds by incrementally reconstructing all inner nodes by combining a leaf/inner node with either another
* leaf/inner node or a proof sibling node, depending on whether each `proofFlags` item is true or false
* respectively.
*
* This version handles multiproofs in calldata with a custom hashing function.
*
* CAUTION: Not all Merkle trees admit multiproofs. To use multiproofs, it is sufficient to ensure that: 1) the tree
* is complete (but not necessarily perfect), 2) the leaves to be proven are in the opposite order they are in the
* tree (i.e., as seen from right to left starting at the deepest layer and continuing at the next layer).
*
* NOTE: The _empty set_ (i.e. the case where `proof.length == 1 && leaves.length == 0`) is considered a no-op,
* and therefore a valid multiproof (i.e. it returns `proof[0]`). Consider disallowing this case if you're not
* validating the leaves elsewhere.
*/
function processMultiProofCalldata(
bytes32[] calldata proof,
bool[] calldata proofFlags,
bytes32[] memory leaves,
function(bytes32, bytes32) view returns (bytes32) hasher
) internal view returns (bytes32 merkleRoot) {
// This function rebuilds the root hash by traversing the tree up from the leaves. The root is rebuilt by
// consuming and producing values on a queue. The queue starts with the `leaves` array, then goes onto the
// `hashes` array. At the end of the process, the last hash in the `hashes` array should contain the root of
// the Merkle tree.
uint256 leavesLen = leaves.length;
uint256 proofFlagsLen = proofFlags.length;
// Check proof validity.
if (leavesLen + proof.length != proofFlagsLen + 1) {
revert MerkleProofInvalidMultiproof();
}
// The xxxPos values are "pointers" to the next value to consume in each array. All accesses are done using
// `xxx[xxxPos++]`, which return the current value and increment the pointer, thus mimicking a queue's "pop".
bytes32[] memory hashes = new bytes32[](proofFlagsLen);
uint256 leafPos = 0;
uint256 hashPos = 0;
uint256 proofPos = 0;
// At each step, we compute the next hash using two values:
// - a value from the "main queue". If not all leaves have been consumed, we get the next leaf, otherwise we
// get the next hash.
// - depending on the flag, either another value from the "main queue" (merging branches) or an element from the
// `proof` array.
for (uint256 i = 0; i < proofFlagsLen; i++) {
bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
bytes32 b = proofFlags[i]
? (leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++])
: proof[proofPos++];
hashes[i] = hasher(a, b);
}
if (proofFlagsLen > 0) {
if (proofPos != proof.length) {
revert MerkleProofInvalidMultiproof();
}
unchecked {
return hashes[proofFlagsLen - 1];
}
} else if (leavesLen > 0) {
return leaves[0];
} else {
return proof[0];
}
}
}
Errors.sol 34 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/Errors.sol)
pragma solidity ^0.8.20;
/**
* @dev Collection of common custom errors used in multiple contracts
*
* IMPORTANT: Backwards compatibility is not guaranteed in future versions of the library.
* It is recommended to avoid relying on the error API for critical functionality.
*
* _Available since v5.1._
*/
library Errors {
/**
* @dev The ETH balance of the account is not enough to perform the operation.
*/
error InsufficientBalance(uint256 balance, uint256 needed);
/**
* @dev A call to an address target failed. The target may have reverted.
*/
error FailedCall();
/**
* @dev The deployment failed.
*/
error FailedDeployment();
/**
* @dev A necessary precompile is missing.
*/
error MissingPrecompile(address);
}
IERC165.sol 25 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/introspection/IERC165.sol)
pragma solidity ^0.8.20;
/**
* @dev Interface of the ERC-165 standard, as defined in the
* https://eips.ethereum.org/EIPS/eip-165[ERC].
*
* Implementers can declare support of contract interfaces, which can then be
* queried by others ({ERC165Checker}).
*
* For an implementation, see {ERC165}.
*/
interface IERC165 {
/**
* @dev Returns true if this contract implements the interface defined by
* `interfaceId`. See the corresponding
* https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[ERC section]
* to learn more about how these ids are created.
*
* This function call must use less than 30 000 gas.
*/
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
ReentrancyGuard.sol 87 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/ReentrancyGuard.sol)
pragma solidity ^0.8.20;
/**
* @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 EIP-1153 (transient storage) is available on the chain you're deploying at,
* consider using {ReentrancyGuardTransient} instead.
*
* 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;
/**
* @dev Unauthorized reentrant call.
*/
error ReentrancyGuardReentrantCall();
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
if (_status == ENTERED) {
revert ReentrancyGuardReentrantCall();
}
// 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;
}
}
GradientMarketMakerFactory.sol 363 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {Create2} from "@openzeppelin/contracts/utils/Create2.sol";
import {IGradientRegistry} from "./interfaces/IGradientRegistry.sol";
import {GradientMarketMakerPoolV3} from "./GradientMarketMakerPoolV3.sol";
import {IEventAggregator} from "./interfaces/IEventAggregator.sol";
import {IGradientMarketMakerPoolV3} from "./interfaces/IGradientMarketMakerPoolV3.sol";
// Custom errors to save gas and reduce contract size
error InvalidRegistry();
error InvalidEventAggregator();
error InvalidTokenAddress();
error PoolAlreadyExists();
error TokenBlocked();
error EthAmountMismatch();
error TokenAmountZero();
/**
* @title GradientMarketMakerFactory
* @notice Factory contract for deploying individual token market maker pools
* @dev Similar to Uniswap V2 Factory pattern - one pool per token
*/
contract GradientMarketMakerFactory is Ownable {
using SafeERC20 for IERC20;
IGradientRegistry public immutable gradientRegistry;
IEventAggregator public eventAggregator;
// Mapping from token address to pool address
mapping(address => address) public getPool;
// Reverse mapping from pool address to token address
mapping(address => address) public getToken;
// Array of all pools
address[] public allPools;
// Events
event PoolCreated(
address indexed token,
address indexed pool,
uint256 poolIndex
);
event EventAggregatorUpdated(
address indexed oldEventAggregator,
address indexed newEventAggregator
);
constructor(
IGradientRegistry _gradientRegistry,
IEventAggregator _eventAggregator
) Ownable(msg.sender) {
if (address(_gradientRegistry) == address(0)) revert InvalidRegistry();
gradientRegistry = _gradientRegistry;
if (address(_eventAggregator) != address(0)) {
if (address(_eventAggregator).code.length == 0)
revert InvalidEventAggregator();
}
eventAggregator = _eventAggregator;
}
/**
* @notice Set the EventAggregator address
* @param _eventAggregator New EventAggregator address
*/
function setEventAggregator(
IEventAggregator _eventAggregator
) external onlyOwner {
if (
address(_eventAggregator) == address(0) ||
address(_eventAggregator).code.length == 0
) revert InvalidEventAggregator();
address oldEventAggregator = address(eventAggregator);
eventAggregator = _eventAggregator;
emit EventAggregatorUpdated(
oldEventAggregator,
address(_eventAggregator)
);
}
/**
* @notice Calculate the salt for CREATE2 deployment
* @param token Address of the token
* @return salt The calculated salt
*/
function _calculateSalt(
address token
) internal pure returns (bytes32 salt) {
return keccak256(abi.encodePacked(token));
}
/**
* @notice Get the bytecode for GradientMarketMakerPool with constructor arguments
* @param token Address of the token
* @return bytecode The complete bytecode for deployment
*/
function _getPoolBytecode(
address token
) internal view returns (bytes memory bytecode) {
bytecode = abi.encodePacked(
type(GradientMarketMakerPoolV3).creationCode,
abi.encode(IERC20(token), address(this))
);
}
/**
* @notice Predict the address where a pool will be deployed for a given token
* @param token Address of the token
* @return predictedAddress The predicted pool address
*/
function predictPoolAddress(
address token
) external view returns (address predictedAddress) {
bytes32 salt = _calculateSalt(token);
bytes memory bytecode = _getPoolBytecode(token);
predictedAddress = Create2.computeAddress(salt, keccak256(bytecode));
}
/**
* @notice Get the number of pools created
* @return Number of pools
*/
function allPoolsLength() external view returns (uint256) {
return allPools.length;
}
/**
* @notice Create a new market maker pool for a token using CREATE2
* @param token Address of the token
* @return pool Address of the created pool
*/
function createPool(address token) external returns (address pool) {
if (token == address(0)) revert InvalidTokenAddress();
if (token.code.length == 0) revert InvalidTokenAddress();
if (getPool[token] != address(0)) revert PoolAlreadyExists();
if (gradientRegistry.blockedTokens(token)) revert TokenBlocked();
// Calculate salt and bytecode for CREATE2
bytes32 salt = _calculateSalt(token);
bytes memory bytecode = _getPoolBytecode(token);
// Deploy pool using CREATE2
pool = Create2.deploy(0, salt, bytecode);
// Store pool address
getPool[token] = pool;
getToken[pool] = token;
allPools.push(pool);
try eventAggregator.emitPoolCreated(token, pool) {
// Success - EventAggregator call completed
} catch {
// EventAggregator call failed - continue execution
}
}
/**
* @notice Create a new market maker pool for a token with initial liquidity
* @param token Address of the token
* @param initialEthAmount Amount of ETH to add as initial liquidity
* @param initialTokenAmount Amount of tokens to add as initial liquidity
* @param minPrice Minimum price for liquidity range
* @param maxPrice Maximum price for liquidity range
* @return pool Address of the created pool
*/
function createPoolWithLiquidity(
address token,
uint256 initialEthAmount,
uint256 initialTokenAmount,
uint256 minPrice,
uint256 maxPrice
) external payable returns (address pool) {
if (token == address(0)) revert InvalidTokenAddress();
if (token.code.length == 0) revert InvalidTokenAddress();
if (getPool[token] != address(0)) revert PoolAlreadyExists();
if (gradientRegistry.blockedTokens(token)) revert TokenBlocked();
if (msg.value != initialEthAmount) revert EthAmountMismatch();
// Create the pool using CREATE2
bytes32 salt = _calculateSalt(token);
bytes memory bytecode = _getPoolBytecode(token);
pool = Create2.deploy(0, salt, bytecode);
// Store pool address
getPool[token] = pool;
getToken[pool] = token;
allPools.push(pool);
// Add initial liquidity for the specified user
if (initialEthAmount > 0) {
IGradientMarketMakerPoolV3(pool).addETHLiquidityForUser{
value: initialEthAmount
}(msg.sender, minPrice, maxPrice);
}
if (initialTokenAmount > 0) {
// Transfer tokens from caller to factory first
IERC20(token).safeTransferFrom(
msg.sender,
address(this),
initialTokenAmount
);
// Approve pool to spend tokens
IERC20(token).forceApprove(pool, initialTokenAmount);
// Add token liquidity for the specified user
IGradientMarketMakerPoolV3(pool).addTokenLiquidityForUser(
msg.sender,
initialTokenAmount,
minPrice,
maxPrice
);
// Reset allowance to zero to prevent future unexpected pulls
IERC20(token).forceApprove(pool, 0);
}
try eventAggregator.emitPoolCreated(token, pool) {
// Success - EventAggregator call completed
} catch {
// EventAggregator call failed - continue execution
}
}
/**
* @notice Check if a pool exists for a token
* @param token Address of the token
* @return exists True if pool exists
*/
function poolExists(address token) external view returns (bool exists) {
return getPool[token] != address(0);
}
/**
* @notice Get all deployed pools
* @return allPoolAddresses Array of all pool addresses
*/
function getAllPools()
external
view
returns (address[] memory allPoolAddresses)
{
return allPools;
}
/**
* @notice Check if a given address is a valid pool
* @param poolAddress Address to check
* @return isValid True if the address is a valid pool
*/
function isValidPool(
address poolAddress
) external view returns (bool isValid) {
return getToken[poolAddress] != address(0);
}
/**
* @notice Get the registry address
* @return registryAddress Address of the GradientRegistry
*/
function getRegistry() external view returns (address) {
return address(gradientRegistry);
}
/**
* @notice Get the event aggregator address
* @return eventAggregatorAddress Address of the EventAggregator
*/
function getEventAggregator() external view returns (address) {
return address(eventAggregator);
}
// =============================== EMERGENCY FUNCTIONS ===============================
/**
* @notice Emergency function to withdraw ETH from the contract
* @param recipient Address to receive the ETH
* @param amount Amount of ETH to withdraw (0 = withdraw all)
* @dev Only callable by contract owner in emergency situations
*/
function emergencyWithdrawETH(
address payable recipient,
uint256 amount
) external onlyOwner {
require(recipient != address(0), "Invalid recipient");
require(address(this).balance > 0, "No ETH to withdraw");
uint256 withdrawAmount = amount == 0 ? address(this).balance : amount;
require(
withdrawAmount <= address(this).balance,
"Insufficient ETH balance"
);
(bool success, ) = recipient.call{value: withdrawAmount}("");
require(success, "ETH withdrawal failed");
emit EmergencyWithdrawETH(recipient, withdrawAmount);
}
/**
* @notice Emergency function to withdraw ERC20 tokens from the contract
* @param token Address of the token to withdraw
* @param recipient Address to receive the tokens
* @param amount Amount of tokens to withdraw (0 = withdraw all)
* @dev Only callable by contract owner in emergency situations
*/
function emergencyWithdrawToken(
address token,
address recipient,
uint256 amount
) external onlyOwner {
require(token != address(0), "Invalid token address");
require(recipient != address(0), "Invalid recipient");
uint256 balance = IERC20(token).balanceOf(address(this));
require(balance > 0, "No tokens to withdraw");
uint256 withdrawAmount = amount == 0 ? balance : amount;
require(withdrawAmount <= balance, "Insufficient token balance");
IERC20(token).safeTransfer(recipient, withdrawAmount);
emit EmergencyWithdrawToken(token, recipient, withdrawAmount);
}
/**
* @notice Emergency function to withdraw multiple tokens at once
* @param tokens Array of token addresses to withdraw
* @param recipient Address to receive all tokens
* @dev Only callable by contract owner in emergency situations
* @dev More gas efficient than calling emergencyWithdrawToken multiple times
*/
function emergencyWithdrawMultipleTokens(
address[] calldata tokens,
address recipient
) external onlyOwner {
require(recipient != address(0), "Invalid recipient");
require(tokens.length > 0, "No tokens specified");
require(tokens.length <= 20, "Too many tokens to withdraw at once");
for (uint256 i = 0; i < tokens.length; i++) {
address token = tokens[i];
require(token != address(0), "Invalid token address");
uint256 balance = IERC20(token).balanceOf(address(this));
if (balance > 0) {
IERC20(token).safeTransfer(recipient, balance);
emit EmergencyWithdrawToken(token, recipient, balance);
}
}
}
// Events for emergency withdrawals
event EmergencyWithdrawETH(address indexed recipient, uint256 amount);
event EmergencyWithdrawToken(
address indexed token,
address indexed recipient,
uint256 amount
);
}
GradientMarketMakerPoolV3.sol 1984 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {MerkleProof} from "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol";
import {IGradientRegistry} from "./interfaces/IGradientRegistry.sol";
import {IGradientMarketMakerFactory} from "./interfaces/IGradientMarketMakerFactory.sol";
import {IUniswapV2Pair} from "./interfaces/IUniswapV2Pair.sol";
import {IUniswapV2Router02} from "./interfaces/IUniswapV2Router.sol";
import {IUniswapV2Factory} from "./interfaces/IUniswapV2Factory.sol";
import {IUniswapV3PriceHelper} from "./interfaces/IUniswapV3PriceHelper.sol";
import {IEventAggregator} from "./interfaces/IEventAggregator.sol";
import {IGradientMarketMakerPoolV3} from "./interfaces/IGradientMarketMakerPoolV3.sol";
// Custom errors
error InvalidTokenAddress();
error TokenBlocked();
error OnlyRewardDistributor();
error OnlyOrderbook();
error OnlyFactory();
error OnlyOwner();
error UnsupportedTokenDecimals();
error AmountZero();
error InsufficientShares();
error InsufficientPoolBalance();
error InsufficientWithdrawal();
error ETHTransferFailed();
error ETHTransferToOrderbookFailed();
error VersionAlreadyProcessed();
error VersionNotAvailable();
error InvalidMerkleProof();
error NoMerkleRootForUpdates();
error InvalidSharesPercentage();
error NoLiquidity();
error NoLiquidityToWithdraw();
error NoSharesToBurn();
error InsufficientSharesToBurn();
error NoRewards();
error NoETHLiquidityOrRewards();
error NoTokenLiquidityOrRewards();
error NoTokenProviderRewards();
error InvalidRecipient();
error InsufficientETHBalance();
error InsufficientTokenBalance();
error ETHWithdrawalFailed();
error TokenWithdrawalFailed();
error RouterNotSet();
error PairDoesNotExist();
error OverflowInETHRewardCalculation();
error OverflowInTokenProviderRewardCalculation();
error OverflowInTokenRewardCalculation();
error ETHAmountMismatch();
error InsufficientTokenLiquidity();
error InsufficientETHLiquidity();
error ETHAmountBelowMinimum();
error TokenAmountBelowMinimum();
error NoETHSent();
error NoLiquidityOrRewards();
error InvalidMinLiquidity();
error InvalidMinTokenLiquidity();
error InvalidPriceRange();
error PriceOutOfRange();
error InvalidPriceOrder();
error OverlappingPriceRange();
/**
* @title GradientMarketMakerPoolV3
* @notice Individual pool contract for a single token with price range functionality
* @dev Each token gets its own pool contract with concentrated liquidity price ranges
* @dev Users can specify min/max price ranges when adding liquidity
* @dev Enhanced version with better gas optimization and security
*/
contract GradientMarketMakerPoolV3 is ReentrancyGuard {
using SafeERC20 for IERC20;
// Price range struct for concentrated liquidity
struct PriceRange {
uint256 minPrice; // Minimum price (in wei per token)
uint256 maxPrice; // Maximum price (in wei per token)
bool isActive; // Whether this range is active
}
// Provider position with price range support
struct ProviderPosition {
uint256 position;
uint256 rewardDebt;
uint256 pendingRewards;
uint256 lastUpdateVersion;
}
// Pool metrics with price range support
struct PoolMetrics {
uint256 position; // Total ETH or tokens
uint256 totalLPShares; // Total LP shares
uint256 accRewardPerShare; // Accumulated rewards per share
uint256 rewardBalance; // Available reward balance
uint256 accountedPosition; // Accounted position for calculations
}
// Events
event LiquidityDeposited(
address indexed user,
address indexed token,
uint256 amount,
uint256 positionMinted,
bool isETH,
uint256 minPrice,
uint256 maxPrice
);
event LiquidityWithdrawn(
address indexed user,
address indexed token,
uint256 amount,
uint256 positionBurned,
bool isETH,
uint256 minPrice,
uint256 maxPrice
);
event PoolFeeDistributed(
address indexed from,
uint256 amount,
address indexed token,
bool isETH
);
event FeeClaimed(
address indexed user,
uint256 amount,
address indexed token,
bool isETH
);
event FeeRefunded(address indexed recipient, uint256 amount, bool isETH);
event PoolBalanceUpdated(
address indexed token,
uint256 newTotalETH,
uint256 newTotalTokens,
uint256 newETHLPShares,
uint256 newTokenLPShares
);
event PriceRangeUpdated(
address indexed user,
uint256 oldMinPrice,
uint256 oldMaxPrice,
uint256 newMinPrice,
uint256 newMaxPrice,
bool isETH
);
event MerkleRootUpdated(uint256 indexed version, bytes32 merkleRoot);
event UserPositionUpdated(
address indexed user,
uint256 indexed version,
uint256 newETHPositions,
uint256 newTokenPositions
);
event MinLiquidityUpdated(uint256 newMinLiquidity, bool isETH);
event EmergencyWithdraw(
address indexed token,
address indexed recipient,
uint256 amount,
bool isETH
);
// Position events
event PositionCreated(
uint256 indexed positionId,
address indexed owner,
uint256 amount,
uint256 minPrice,
uint256 maxPrice,
bool isETH
);
event PositionUpdated(
uint256 indexed positionId,
address indexed owner,
uint256 newAmount,
uint256 newMinPrice,
uint256 newMaxPrice
);
event PositionClosed(
uint256 indexed positionId,
address indexed owner,
uint256 amount,
uint256 rewards
);
// Immutable token address - this pool is dedicated to one token
IERC20 public immutable tokenContract;
IGradientMarketMakerFactory public immutable factoryContract;
// Pool state with price range support
uint256 public totalETH;
uint256 public totalTokens;
// Single position per user (like V2 but with price range)
mapping(address => ProviderPosition) public ethProviders;
mapping(address => ProviderPosition) public tokenProviders;
// User price ranges (shared between ETH and token positions)
mapping(address => PriceRange) public userPriceRanges;
// Reward tracking - separate ETH pools for each provider type (same as V2)
uint256 public accRewardPerShare; // For ETH providers (ETH rewards)
uint256 public accTokenRewardPerShare; // For token providers (token rewards)
uint256 public rewardBalance; // ETH rewards for ETH providers
uint256 public tokenProviderRewardBalance; // ETH rewards for token providers
uint256 public constant SCALE = 1e18;
// Maximum supported token decimals to prevent overflow
uint8 public constant MAX_TOKEN_DECIMALS = 24;
uint8 public tokenDecimals;
// Configurable minimum liquidity requirements
uint256 public minLiquidity;
uint256 public minTokenLiquidity;
// Track totals for this specific token pool
uint256 public totalEthAdded;
uint256 public totalEthRemoved;
uint256 public totalTokensAdded;
uint256 public totalTokensRemoved; // Total tokens removed from this pool
uint256 public totalTokenRewardsDistributed; // Total token rewards distributed
// Uniswap pair address
address public uniswapPair;
// Uniswap V3 Price Helper for accessing V3 pools
IUniswapV3PriceHelper public priceHelper;
// Merkle root for LP share updates
bytes32 public merkleRoot;
uint256 public currentVersion;
mapping(uint256 => bytes32) public versionMerkleRoots;
// Position limits
// Events are inherited from interface
modifier isNotBlocked() {
if (getRegistry().blockedTokens(address(tokenContract)))
revert TokenBlocked();
_;
}
modifier onlyRewardDistributor() {
if (!getRegistry().isRewardDistributor(msg.sender))
revert OnlyRewardDistributor();
_;
}
modifier onlyOrderbook() {
if (msg.sender != getRegistry().orderbook()) revert OnlyOrderbook();
_;
}
modifier onlyFactory() {
if (msg.sender != address(factoryContract)) revert OnlyFactory();
_;
}
modifier onlyOwner() {
if (msg.sender != factoryContract.owner()) revert OnlyOwner();
_;
}
constructor(IERC20 _token, address _factory) {
if (address(_token) == address(0)) revert InvalidTokenAddress();
if (_factory == address(0)) revert InvalidRecipient();
tokenContract = _token;
factoryContract = IGradientMarketMakerFactory(_factory);
tokenDecimals = IERC20Metadata(address(_token)).decimals();
// Validate token decimals to prevent overflow
if (tokenDecimals > MAX_TOKEN_DECIMALS) {
revert UnsupportedTokenDecimals();
}
minLiquidity = 1000000000000; // 0.000001 ETH minimum (default)
minTokenLiquidity = 2 * (10 ** tokenDecimals); // Set minimum token liquidity to 2 tokens
}
/**
* @notice Receive ETH for reward distribution
*/
receive() external payable {}
/**
* @notice Get the current owner (factory owner)
* @return The current owner of the factory
*/
function owner() public view returns (address) {
return factoryContract.owner();
}
// =============================== INTERNAL FUNCTIONS ===============================
/**
* @notice Get the registry address from the factory
* @return registryAddress Address of the GradientRegistry
*/
function getRegistry() public view returns (IGradientRegistry) {
return IGradientRegistry(factoryContract.getRegistry());
}
/**
* @notice Get the event aggregator address from the factory
* @return eventAggregatorAddress Address of the EventAggregator
*/
function getEventAggregator() public view returns (IEventAggregator) {
return IEventAggregator(factoryContract.getEventAggregator());
}
/**
* @notice Validate price range parameters
* @param minPrice Minimum price
* @param maxPrice Maximum price
*/
function _validatePriceRange(
uint256 minPrice,
uint256 maxPrice
) internal pure {
if (minPrice == 0 || maxPrice == 0) revert InvalidPriceRange();
if (minPrice >= maxPrice) revert InvalidPriceOrder();
}
/**
* @notice Check if current price is within a given range
* @param price Current price to check
* @param minPrice Minimum price
* @param maxPrice Maximum price
* @return isWithinRange True if price is within range
*/
function _isPriceWithinRange(
uint256 price,
uint256 minPrice,
uint256 maxPrice
) internal pure returns (bool isWithinRange) {
return price >= minPrice && price <= maxPrice;
}
/**
* @notice Normalize token amount to 18 decimals for consistent calculations
* @param amount Amount in token decimals
* @return uint256 Amount normalized to 18 decimals
*/
function _normalizeTo18Decimals(
uint256 amount
) internal view returns (uint256) {
if (tokenDecimals == 18) {
return amount;
} else if (tokenDecimals < 18) {
return amount * (10 ** (18 - tokenDecimals));
} else {
return amount / (10 ** (tokenDecimals - 18));
}
}
/**
* @notice Denormalize from 18 decimals to token decimals
* @param amount Amount in 18 decimals
* @return uint256 Amount in token decimals
*/
function _denormalizeFrom18Decimals(
uint256 amount
) internal view returns (uint256) {
if (tokenDecimals == 18) {
return amount;
} else if (tokenDecimals < 18) {
return amount / (10 ** (18 - tokenDecimals));
} else {
return amount * (10 ** (tokenDecimals - 18));
}
}
/**
* @notice Update merkle root after trade execution
* @param newMerkleRoot New merkle root to set
*/
function _updateMerkleRootAfterTrade(bytes32 newMerkleRoot) internal {
if (newMerkleRoot != bytes32(0)) {
currentVersion++;
merkleRoot = newMerkleRoot;
versionMerkleRoots[currentVersion] = newMerkleRoot;
try
getEventAggregator().emitMerkleRootUpdated(
currentVersion,
newMerkleRoot
)
{
// Success - EventAggregator call completed
} catch {
// EventAggregator call failed - continue execution
}
emit MerkleRootUpdated(currentVersion, newMerkleRoot);
}
}
/**
* @notice Verify merkle proof for user position update
* @param user User address
* @param proof Merkle proof
* @param newETHPosition New ETH position value
* @param newTokenPosition New token position value
* @param ethRewardsToAdd ETH rewards to add
* @param tokenRewardsToAdd Token rewards to add
* @return isValid Whether the proof is valid
*/
function _verifyMerkleProof(
address user,
bytes32[] calldata proof,
uint256 newETHPosition,
uint256 newTokenPosition,
uint256 ethRewardsToAdd,
uint256 tokenRewardsToAdd
) internal view returns (bool isValid) {
bytes32 leaf = keccak256(
abi.encodePacked(
user,
newETHPosition,
newTokenPosition,
ethRewardsToAdd,
tokenRewardsToAdd
)
);
// Special handling for single provider case
if (proof.length == 0) {
// For single provider, verify that the leaf equals the merkle root
return leaf == merkleRoot;
}
// Use standard merkle proof verification
return MerkleProof.verify(proof, merkleRoot, leaf);
}
/**
* @notice Check if user needs position update for either asset type
* @param user User address to check
* @return needsUpdate True if user needs position update
*/
function _needsPositionUpdate(address user) internal view returns (bool) {
// Check ETH position
if (
ethProviders[user].position > 0 &&
ethProviders[user].lastUpdateVersion < currentVersion
) {
return true;
}
if (
ethProviders[user].pendingRewards > 0 &&
ethProviders[user].lastUpdateVersion < currentVersion
) {
return true;
}
// Check token position
if (
tokenProviders[user].position > 0 &&
tokenProviders[user].lastUpdateVersion < currentVersion
) {
return true;
}
if (
tokenProviders[user].pendingRewards > 0 &&
tokenProviders[user].lastUpdateVersion < currentVersion
) {
return true;
}
return false;
}
// =============================== PUBLIC FUNCTIONS ===============================
/**
* @notice Add ETH liquidity with price range and optional position update
* @param minPrice Minimum price for this liquidity position
* @param maxPrice Maximum price for this liquidity position
* @param proof Merkle proof for position update (required if user has pending updates)
* @param newETHPosition New ETH position value (required if proof provided)
* @param newTokenPosition New token position value (required if proof provided)
* @param ethRewardsToAdd ETH rewards to add
* @param tokenRewardsToAdd Token rewards to add
*/
function addETHLiquidity(
uint256 minPrice,
uint256 maxPrice,
bytes32[] calldata proof,
uint256 newETHPosition,
uint256 newTokenPosition,
uint256 ethRewardsToAdd,
uint256 tokenRewardsToAdd
) external payable isNotBlocked nonReentrant {
if (msg.value < minLiquidity) revert ETHAmountBelowMinimum();
_validatePriceRange(minPrice, maxPrice);
// Check if user needs position update - only if they have existing liquidity
if (_needsPositionUpdate(msg.sender)) {
// Proof is required - validate parameters
if (merkleRoot == bytes32(0)) revert NoMerkleRootForUpdates();
// Verify merkle proof
if (
!_verifyMerkleProof(
msg.sender,
proof,
newETHPosition,
newTokenPosition,
ethRewardsToAdd,
tokenRewardsToAdd
)
) revert InvalidMerkleProof();
// Update user position first
_updateUserPosition(
msg.sender,
newETHPosition,
newTokenPosition,
ethRewardsToAdd,
tokenRewardsToAdd
);
}
// Then add ETH liquidity with price range
_addETHLiquidity(msg.sender, msg.value, minPrice, maxPrice);
}
/**
* @notice Add token liquidity with price range and optional position update
* @param tokenAmount Amount of tokens to deposit
* @param minPrice Minimum price for this liquidity position
* @param maxPrice Maximum price for this liquidity position
* @param proof Merkle proof for position update (required if user has pending updates)
* @param newETHPosition New ETH position value (required if proof provided)
* @param newTokenPosition New token position value (required if proof provided)
* @param ethRewardsToAdd ETH rewards to add
* @param tokenRewardsToAdd Token rewards to add
*/
function addTokenLiquidity(
uint256 tokenAmount,
uint256 minPrice,
uint256 maxPrice,
bytes32[] calldata proof,
uint256 newETHPosition,
uint256 newTokenPosition,
uint256 ethRewardsToAdd,
uint256 tokenRewardsToAdd
) external isNotBlocked nonReentrant {
if (tokenAmount < minTokenLiquidity) revert TokenAmountBelowMinimum();
_validatePriceRange(minPrice, maxPrice);
// Check if user needs position update - only if they have existing liquidity
if (_needsPositionUpdate(msg.sender)) {
// Proof is required - validate parameters
if (merkleRoot == bytes32(0)) revert NoMerkleRootForUpdates();
// Verify merkle proof
if (
!_verifyMerkleProof(
msg.sender,
proof,
newETHPosition,
newTokenPosition,
ethRewardsToAdd,
tokenRewardsToAdd
)
) revert InvalidMerkleProof();
// Update user position first
_updateUserPosition(
msg.sender,
newETHPosition,
newTokenPosition,
ethRewardsToAdd,
tokenRewardsToAdd
);
}
// Then add token liquidity with price range
_addTokenLiquidity(msg.sender, tokenAmount, minPrice, maxPrice);
}
/**
* @notice Add both ETH and token liquidity with price range and optional position update
* @param tokenAmount Amount of tokens to deposit
* @param minPrice Minimum price for liquidity position (applies to both ETH and token positions)
* @param maxPrice Maximum price for liquidity position (applies to both ETH and token positions)
* @param proof Merkle proof for position update (required if user has pending updates)
* @param newETHPosition New ETH position value (required if proof provided)
* @param newTokenPosition New token position value (required if proof provided)
* @param ethRewardsToAdd ETH rewards to add
* @param tokenRewardsToAdd Token rewards to add
*/
function addLiquidity(
uint256 tokenAmount,
uint256 minPrice,
uint256 maxPrice,
bytes32[] calldata proof,
uint256 newETHPosition,
uint256 newTokenPosition,
uint256 ethRewardsToAdd,
uint256 tokenRewardsToAdd
) external payable isNotBlocked nonReentrant {
if (msg.value < minLiquidity) revert ETHAmountBelowMinimum();
if (tokenAmount < minTokenLiquidity) revert TokenAmountBelowMinimum();
_validatePriceRange(minPrice, maxPrice);
// Check if user needs position update for either asset type - only if they have existing liquidity
bool needsUpdate = _needsPositionUpdate(msg.sender);
if (needsUpdate) {
// Proof is required - validate parameters
if (merkleRoot == bytes32(0)) revert NoMerkleRootForUpdates();
// Verify merkle proof
if (
!_verifyMerkleProof(
msg.sender,
proof,
newETHPosition,
newTokenPosition,
ethRewardsToAdd,
tokenRewardsToAdd
)
) revert InvalidMerkleProof();
// Update user position first
_updateUserPosition(
msg.sender,
newETHPosition,
newTokenPosition,
ethRewardsToAdd,
tokenRewardsToAdd
);
}
// Then add liquidity with price ranges
_addETHLiquidity(msg.sender, msg.value, minPrice, maxPrice);
_addTokenLiquidity(msg.sender, tokenAmount, minPrice, maxPrice);
}
/**
* @notice Add ETH liquidity to the pool with price range (internal function)
* @param user User address to add liquidity for
* @param ethAmount Amount of ETH to deposit
* @param minPrice Minimum price for this liquidity position
* @param maxPrice Maximum price for this liquidity position
*/
function _addETHLiquidity(
address user,
uint256 ethAmount,
uint256 minPrice,
uint256 maxPrice
) internal {
// Initialize Uniswap pair if not set
if (uniswapPair == address(0)) {
uniswapPair = getPairAddress();
}
if (uniswapPair == address(0)) revert PairDoesNotExist();
// Calculate pending rewards for existing position
if (ethProviders[user].position > 0) {
uint256 pendingReward = (ethProviders[user].position *
accRewardPerShare) /
SCALE -
ethProviders[user].rewardDebt;
ethProviders[user].pendingRewards += pendingReward;
}
// Add to existing position or create new one
if (ethProviders[user].position > 0) {
// User already has ETH position, add to it
ethProviders[user].position += ethAmount;
} else {
// Create new ETH position
ethProviders[user] = ProviderPosition({
position: ethAmount,
rewardDebt: 0,
pendingRewards: 0,
lastUpdateVersion: currentVersion
});
}
// Set or update user price range (allow updates when inactive)
if (!userPriceRanges[user].isActive) {
userPriceRanges[user] = PriceRange({
minPrice: minPrice,
maxPrice: maxPrice,
isActive: true
});
}
// Update reward debt for the position
ethProviders[user].rewardDebt =
(ethProviders[user].position * accRewardPerShare) /
SCALE;
totalETH += ethAmount;
totalEthAdded += ethAmount;
uint256 eventMinPrice = userPriceRanges[user].isActive
? userPriceRanges[user].minPrice
: minPrice;
uint256 eventMaxPrice = userPriceRanges[user].isActive
? userPriceRanges[user].maxPrice
: maxPrice;
try
getEventAggregator().emitLiquidityEvent(
user,
address(tokenContract),
0, // ETH_ADD
ethAmount,
eventMinPrice,
eventMaxPrice
)
{
// Success - EventAggregator call completed
} catch {
// EventAggregator call failed - continue execution
}
// Emit local event
emit LiquidityDeposited(
user,
address(tokenContract),
ethAmount,
ethAmount,
true,
eventMinPrice,
eventMaxPrice
);
}
/**
* @notice Add token liquidity to the pool with price range (internal function)
* @param user User address to add liquidity for
* @param tokenAmount Amount of tokens to deposit
* @param minPrice Minimum price for this liquidity position
* @param maxPrice Maximum price for this liquidity position
*/
function _addTokenLiquidity(
address user,
uint256 tokenAmount,
uint256 minPrice,
uint256 maxPrice
) internal {
// Initialize Uniswap pair if not set
if (uniswapPair == address(0)) {
uniswapPair = getPairAddress();
}
if (uniswapPair == address(0)) revert PairDoesNotExist();
// Record balance before transfer to handle fee-on-transfer tokens
uint256 balanceBefore = tokenContract.balanceOf(address(this));
// Transfer tokens from user
tokenContract.safeTransferFrom(msg.sender, address(this), tokenAmount);
// Calculate actual received amount (handles fee-on-transfer and rebasing tokens)
uint256 actualReceived = tokenContract.balanceOf(address(this)) -
balanceBefore;
// Calculate pending rewards for existing position
if (tokenProviders[user].position > 0) {
uint256 pendingReward = (tokenProviders[user].position *
accTokenRewardPerShare) /
SCALE -
tokenProviders[user].rewardDebt;
tokenProviders[user].pendingRewards += pendingReward;
}
// Add to existing position or create new one
if (tokenProviders[user].position > 0) {
// User already has token position, add to it
tokenProviders[user].position += actualReceived;
} else {
// Create new token position
tokenProviders[user] = ProviderPosition({
position: actualReceived,
rewardDebt: 0,
pendingRewards: 0,
lastUpdateVersion: currentVersion
});
}
// Set or update user price range
if (!userPriceRanges[user].isActive) {
userPriceRanges[user] = PriceRange({
minPrice: minPrice,
maxPrice: maxPrice,
isActive: true
});
}
// Update reward debt for the position
tokenProviders[user].rewardDebt =
(tokenProviders[user].position * accTokenRewardPerShare) /
SCALE;
totalTokens += actualReceived;
totalTokensAdded += actualReceived;
uint256 eventMinPrice = userPriceRanges[user].isActive
? userPriceRanges[user].minPrice
: minPrice;
uint256 eventMaxPrice = userPriceRanges[user].isActive
? userPriceRanges[user].maxPrice
: maxPrice;
try
getEventAggregator().emitLiquidityEvent(
user,
address(tokenContract),
2, // TOKEN_ADD
actualReceived,
eventMinPrice,
eventMaxPrice
)
{
// Success - EventAggregator call completed
} catch {
// EventAggregator call failed - continue execution
}
// Emit local event
emit LiquidityDeposited(
user,
address(tokenContract),
actualReceived,
actualReceived,
false,
eventMinPrice,
eventMaxPrice
);
}
/**
* @notice Get the Uniswap pool/pair address for this token (checks V3 first, then V2)
* @return poolAddress Address of the Uniswap V3 pool or V2 pair (returns address(0) if neither exists)
* @dev Returns V3 pool address if available, otherwise V2 pair address
*/
function getPairAddress() public view returns (address poolAddress) {
address routerAddress = getRegistry().router();
if (routerAddress == address(0)) revert RouterNotSet();
IUniswapV2Router02 router = IUniswapV2Router02(routerAddress);
address weth = router.WETH();
// Try V3 first
if (address(priceHelper) != address(0)) {
address v3Pool = priceHelper.getV3PoolAddress(
address(tokenContract),
weth
);
if (v3Pool != address(0)) {
return v3Pool;
}
}
// Fall back to V2
address factoryAddress = router.factory();
IUniswapV2Factory uniswapFactory = IUniswapV2Factory(factoryAddress);
return uniswapFactory.getPair(address(tokenContract), weth);
}
/**
* @notice Get the reserves for this token pair
* @return reserveETH ETH reserve amount
* @return reserveToken Token reserve amount
*/
function getReserves()
public
view
returns (uint256 reserveETH, uint256 reserveToken)
{
address pairAddress = getPairAddress();
// For V3-only tokens, there's no V2 pair - reserves not available via V2
if (pairAddress == address(0)) {
// V3 tokens don't have V2 reserves - return zeros or revert based on your needs
// For now, returning zeros for V3 tokens
return (0, 0);
}
(uint112 reserve0, uint112 reserve1, ) = IUniswapV2Pair(pairAddress)
.getReserves();
address token0 = IUniswapV2Pair(pairAddress).token0();
(reserveETH, reserveToken) = token0 == address(tokenContract)
? (reserve1, reserve0)
: (reserve0, reserve1);
}
/**
* @notice Update both ETH and token provider positions using merkle proof
* @param user User address
* @param newETHPosition New ETH position value
* @param newTokenPosition New token position value
* @param ethRewardsToAdd Total ETH rewards (accumulated)
* @param tokenRewardsToAdd Total token rewards (accumulated)
*/
function _updateUserPosition(
address user,
uint256 newETHPosition,
uint256 newTokenPosition,
uint256 ethRewardsToAdd,
uint256 tokenRewardsToAdd
) internal {
// Update ETH position
ethProviders[user].position = newETHPosition;
ethProviders[user].pendingRewards = ethRewardsToAdd;
ethProviders[user].lastUpdateVersion = currentVersion;
ethProviders[user].rewardDebt =
(newETHPosition * accRewardPerShare) /
SCALE;
// Update token position
tokenProviders[user].position = newTokenPosition;
tokenProviders[user].pendingRewards = tokenRewardsToAdd;
tokenProviders[user].lastUpdateVersion = currentVersion;
uint256 normalizedPosition = _normalizeTo18Decimals(newTokenPosition);
tokenProviders[user].rewardDebt =
(normalizedPosition * accTokenRewardPerShare) /
SCALE;
// Emit event for position update
emit UserPositionUpdated(
user,
currentVersion,
newETHPosition,
newTokenPosition
);
}
function removeETHLiquidityWithProof(
uint256 shares,
uint256 minEthAmount,
bytes32[] calldata proof,
uint256 newETHPosition,
uint256 newTokenPosition,
uint256 ethRewardsToAdd,
uint256 tokenRewardsToAdd
) external {
if (shares <= 0 || shares > 10000) revert InvalidSharesPercentage();
if (totalETH == 0) revert NoLiquidity();
if (ethProviders[msg.sender].position == 0)
revert NoLiquidityToWithdraw();
bool needsUpdate = _needsPositionUpdate(msg.sender);
if (needsUpdate) {
if (merkleRoot == bytes32(0)) revert NoMerkleRootForUpdates();
if (
!_verifyMerkleProof(
msg.sender,
proof,
newETHPosition,
newTokenPosition,
ethRewardsToAdd,
tokenRewardsToAdd
)
) {
revert InvalidMerkleProof();
}
_updateUserPosition(
msg.sender,
newETHPosition,
newTokenPosition,
ethRewardsToAdd,
tokenRewardsToAdd
);
}
uint256 amountToWithdraw = _removeETHLiquidityInternal(
msg.sender,
shares,
minEthAmount
);
// Deactivate price range only if both positions are empty
if (
ethProviders[msg.sender].position == 0 &&
tokenProviders[msg.sender].position == 0
) {
userPriceRanges[msg.sender].isActive = false;
}
emit LiquidityWithdrawn(
msg.sender,
address(tokenContract),
amountToWithdraw,
amountToWithdraw,
true,
userPriceRanges[msg.sender].minPrice,
userPriceRanges[msg.sender].maxPrice
);
try
getEventAggregator().emitLiquidityEvent(
msg.sender,
address(tokenContract),
1, // ETH_REMOVE
amountToWithdraw,
userPriceRanges[msg.sender].minPrice,
userPriceRanges[msg.sender].maxPrice
)
{
// Success - EventAggregator call completed
} catch {
// EventAggregator call failed - continue execution
}
}
function removeTokenLiquidityWithProof(
uint256 shares,
uint256 minTokenAmount,
bytes32[] calldata proof,
uint256 newETHPosition,
uint256 newTokenPosition,
uint256 ethRewardsToAdd,
uint256 tokenRewardsToAdd
) external {
if (shares <= 0 || shares > 10000) revert InvalidSharesPercentage();
if (totalTokens == 0) revert NoLiquidity();
if (tokenProviders[msg.sender].position == 0)
revert NoLiquidityToWithdraw();
bool needsUpdate = _needsPositionUpdate(msg.sender);
if (needsUpdate) {
if (merkleRoot == bytes32(0)) revert NoMerkleRootForUpdates();
if (
!_verifyMerkleProof(
msg.sender,
proof,
newETHPosition,
newTokenPosition,
ethRewardsToAdd,
tokenRewardsToAdd
)
) {
revert InvalidMerkleProof();
}
_updateUserPosition(
msg.sender,
newETHPosition,
newTokenPosition,
ethRewardsToAdd,
tokenRewardsToAdd
);
}
uint256 amountToWithdraw = _removeTokenLiquidityInternal(
msg.sender,
shares,
minTokenAmount
);
// Deactivate price range only if both positions are empty
if (
ethProviders[msg.sender].position == 0 &&
tokenProviders[msg.sender].position == 0
) {
userPriceRanges[msg.sender].isActive = false;
}
emit LiquidityWithdrawn(
msg.sender,
address(tokenContract),
amountToWithdraw,
amountToWithdraw,
false,
userPriceRanges[msg.sender].minPrice,
userPriceRanges[msg.sender].maxPrice
);
try
getEventAggregator().emitLiquidityEvent(
msg.sender,
address(tokenContract),
3, // TOKEN_REMOVE
amountToWithdraw,
userPriceRanges[msg.sender].minPrice,
userPriceRanges[msg.sender].maxPrice
)
{
// Success - EventAggregator call completed
} catch {
// EventAggregator call failed - continue execution
}
}
function removeLiquidityWithProof(
uint256 ethShares,
uint256 tokenShares,
uint256 minEthAmount,
uint256 minTokenAmount,
bytes32[] calldata proof,
uint256 newETHPosition,
uint256 newTokenPosition,
uint256 ethRewardsToAdd,
uint256 tokenRewardsToAdd
) external {
if (ethShares <= 0 || ethShares > 10000)
revert InvalidSharesPercentage();
if (tokenShares <= 0 || tokenShares > 10000)
revert InvalidSharesPercentage();
if (totalETH == 0 && totalTokens == 0) revert NoLiquidity();
if (
ethProviders[msg.sender].position == 0 &&
tokenProviders[msg.sender].position == 0
) {
revert NoLiquidityToWithdraw();
}
bool needsUpdate = _needsPositionUpdate(msg.sender);
if (needsUpdate) {
if (merkleRoot == bytes32(0)) revert NoMerkleRootForUpdates();
if (
!_verifyMerkleProof(
msg.sender,
proof,
newETHPosition,
newTokenPosition,
ethRewardsToAdd,
tokenRewardsToAdd
)
) {
revert InvalidMerkleProof();
}
_updateUserPosition(
msg.sender,
newETHPosition,
newTokenPosition,
ethRewardsToAdd,
tokenRewardsToAdd
);
}
uint256 ethAmountToWithdraw = 0;
uint256 tokenAmountToWithdraw = 0;
if (ethProviders[msg.sender].position > 0) {
ethAmountToWithdraw = _removeETHLiquidityInternal(
msg.sender,
ethShares,
minEthAmount
);
}
if (tokenProviders[msg.sender].position > 0) {
tokenAmountToWithdraw = _removeTokenLiquidityInternal(
msg.sender,
tokenShares,
minTokenAmount
);
}
if (
ethAmountToWithdraw < minEthAmount &&
tokenAmountToWithdraw < minTokenAmount
) revert InsufficientWithdrawal();
// Deactivate price range only if both positions are empty
if (
ethProviders[msg.sender].position == 0 &&
tokenProviders[msg.sender].position == 0
) {
userPriceRanges[msg.sender].isActive = false;
}
// Emit separate events for each position
if (ethAmountToWithdraw > 0) {
emit LiquidityWithdrawn(
msg.sender,
address(tokenContract),
ethAmountToWithdraw,
ethAmountToWithdraw,
true,
userPriceRanges[msg.sender].minPrice,
userPriceRanges[msg.sender].maxPrice
);
try
getEventAggregator().emitLiquidityEvent(
msg.sender,
address(tokenContract),
1, // ETH_REMOVE
ethAmountToWithdraw,
userPriceRanges[msg.sender].minPrice,
userPriceRanges[msg.sender].maxPrice
)
{
// Success - EventAggregator call completed
} catch {
// EventAggregator call failed - continue execution
}
}
if (tokenAmountToWithdraw > 0) {
emit LiquidityWithdrawn(
msg.sender,
address(tokenContract),
tokenAmountToWithdraw,
tokenAmountToWithdraw,
false,
userPriceRanges[msg.sender].minPrice,
userPriceRanges[msg.sender].maxPrice
);
try
getEventAggregator().emitLiquidityEvent(
msg.sender,
address(tokenContract),
3, // TOKEN_REMOVE
tokenAmountToWithdraw,
userPriceRanges[msg.sender].minPrice,
userPriceRanges[msg.sender].maxPrice
)
{
// Success - EventAggregator call completed
} catch {
// EventAggregator call failed - continue execution
}
}
}
function executeBuyOrder(
uint256 ethAmount,
uint256 tokenAmount,
bytes32 newMerkleRoot
) external payable isNotBlocked onlyOrderbook {
if (ethAmount == 0) revert AmountZero();
if (tokenAmount == 0) revert AmountZero();
if (msg.value != ethAmount) revert ETHAmountMismatch();
if (totalTokens <= tokenAmount) revert InsufficientTokenLiquidity();
totalTokens -= tokenAmount;
totalTokensRemoved += tokenAmount;
totalETH += msg.value;
totalEthAdded += msg.value;
if (tokenAmount > 0) {
tokenContract.safeTransfer(msg.sender, tokenAmount);
}
_updateMerkleRootAfterTrade(newMerkleRoot);
emit PoolBalanceUpdated(
address(tokenContract),
totalETH,
totalTokens,
totalETH,
totalTokens
);
}
function executeSellOrder(
uint256 ethAmount,
uint256 tokenAmount,
bytes32 newMerkleRoot
) external isNotBlocked onlyOrderbook {
if (ethAmount == 0) revert AmountZero();
if (tokenAmount == 0) revert AmountZero();
if (totalETH <= ethAmount) revert InsufficientETHLiquidity();
tokenContract.safeTransferFrom(msg.sender, address(this), tokenAmount);
totalETH -= ethAmount;
totalEthRemoved += ethAmount;
totalTokens += tokenAmount;
totalTokensAdded += tokenAmount;
(bool success, ) = payable(msg.sender).call{value: ethAmount}("");
if (!success) revert ETHTransferToOrderbookFailed();
_updateMerkleRootAfterTrade(newMerkleRoot);
emit PoolBalanceUpdated(
address(tokenContract),
totalETH,
totalTokens,
totalETH,
totalTokens
);
}
/**
* @notice Distribute pool fee as ETH rewards to liquidity providers
* @dev Only callable by reward distributor
*/
function distributePoolFee()
external
payable
onlyRewardDistributor
nonReentrant
{
if (msg.value == 0) revert NoETHSent();
totalEthAdded += msg.value;
_updatePoolRewards(msg.value);
emit PoolFeeDistributed(
msg.sender,
msg.value,
address(tokenContract),
true
);
}
/**
* @notice Update pool rewards distribution
* @param ethAmount Amount of ETH to distribute as rewards
*/
function _updatePoolRewards(uint256 ethAmount) internal {
if (ethAmount == 0) revert AmountZero();
// Distribute ETH rewards to ETH providers only
if (totalETH > 0) {
uint256 newAccRewardPerShare = accRewardPerShare +
((ethAmount * SCALE) / totalETH);
if (newAccRewardPerShare < accRewardPerShare)
revert OverflowInETHRewardCalculation();
accRewardPerShare = newAccRewardPerShare;
} else {
// No liquidity exists - immediately refund the ETH
(bool success, ) = payable(msg.sender).call{value: ethAmount}("");
if (!success) revert ETHTransferFailed();
emit FeeRefunded(msg.sender, ethAmount, true);
}
// Track ETH rewards
rewardBalance += ethAmount;
}
/**
* @notice Update token pool rewards distribution
* @param tokenAmount Amount of tokens to distribute as rewards
*/
function _updateTokenRewards(uint256 tokenAmount) internal {
if (tokenAmount == 0) revert AmountZero();
// Distribute token rewards to token providers only
if (totalTokens > 0) {
// Normalize token amount to 18 decimals for consistent calculations
uint256 normalizedTokenAmount = _normalizeTo18Decimals(tokenAmount);
uint256 normalizedTotalTokens = _normalizeTo18Decimals(totalTokens);
uint256 newAccTokenRewardPerShare = accTokenRewardPerShare +
((normalizedTokenAmount * SCALE) / normalizedTotalTokens);
if (newAccTokenRewardPerShare < accTokenRewardPerShare)
revert OverflowInTokenRewardCalculation();
accTokenRewardPerShare = newAccTokenRewardPerShare;
// Track token rewards distributed
totalTokenRewardsDistributed += tokenAmount;
} else {
// No liquidity exists - immediately refund the tokens
tokenContract.safeTransfer(msg.sender, tokenAmount);
emit FeeRefunded(msg.sender, tokenAmount, false);
}
}
/**
* @notice Distribute token fee as rewards to liquidity providers
* @param tokenAmount Amount of tokens to distribute as rewards
* @dev Only callable by reward distributor
*/
function distributeTokenFee(
uint256 tokenAmount
) external onlyRewardDistributor nonReentrant {
if (tokenAmount == 0) revert AmountZero();
// Record balance before transfer to handle fee-on-transfer tokens
uint256 balanceBefore = tokenContract.balanceOf(address(this));
// Transfer tokens from orderbook to this contract
tokenContract.safeTransferFrom(msg.sender, address(this), tokenAmount);
// Calculate actual received amount (handles fee-on-transfer and rebasing tokens)
uint256 actualReceived = tokenContract.balanceOf(address(this)) -
balanceBefore;
totalTokensAdded += actualReceived;
_updateTokenRewards(actualReceived);
emit PoolFeeDistributed(
msg.sender,
actualReceived,
address(tokenContract),
false
);
}
/**
* @notice Internal function to remove ETH liquidity
* @param user User address
* @param shares Percentage of shares to remove (0-10000)
* @param minAmount Minimum amount to withdraw
* @return amountToWithdraw Amount actually withdrawn
*/
function _removeETHLiquidityInternal(
address user,
uint256 shares,
uint256 minAmount
) internal returns (uint256 amountToWithdraw) {
uint256 pendingReward = (ethProviders[user].position *
accRewardPerShare) /
SCALE -
ethProviders[user].rewardDebt;
ethProviders[user].pendingRewards += pendingReward;
amountToWithdraw = (ethProviders[user].position * shares) / 10000;
if (amountToWithdraw == 0) revert NoSharesToBurn();
if (amountToWithdraw > ethProviders[user].position) {
amountToWithdraw = ethProviders[user].position;
}
if (amountToWithdraw > totalETH) revert InsufficientPoolBalance();
ethProviders[user].position -= amountToWithdraw;
ethProviders[user].rewardDebt =
(ethProviders[user].position * accRewardPerShare) /
SCALE;
totalETH -= amountToWithdraw;
totalEthRemoved += amountToWithdraw;
if (amountToWithdraw < minAmount) revert InsufficientWithdrawal();
(bool success, ) = payable(user).call{value: amountToWithdraw}("");
if (!success) revert ETHTransferFailed();
return amountToWithdraw;
}
/**
* @notice Internal function to remove token liquidity
* @param user User address
* @param shares Percentage of shares to remove (0-10000)
* @param minAmount Minimum amount to withdraw
* @return amountToWithdraw Amount actually withdrawn
*/
function _removeTokenLiquidityInternal(
address user,
uint256 shares,
uint256 minAmount
) internal returns (uint256 amountToWithdraw) {
uint256 normalizedLpShares = _normalizeTo18Decimals(
tokenProviders[user].position
);
uint256 pendingReward = (normalizedLpShares * accTokenRewardPerShare) /
SCALE -
tokenProviders[user].rewardDebt;
tokenProviders[user].pendingRewards += pendingReward;
amountToWithdraw = (tokenProviders[user].position * shares) / 10000;
if (amountToWithdraw == 0) revert NoSharesToBurn();
if (amountToWithdraw > tokenProviders[user].position) {
amountToWithdraw = tokenProviders[user].position;
}
if (amountToWithdraw > totalTokens) revert InsufficientPoolBalance();
tokenProviders[user].position -= amountToWithdraw;
tokenProviders[user].rewardDebt =
(tokenProviders[user].position * accTokenRewardPerShare) /
...
// [truncated — 67629 bytes total]
GradientOrderbook.sol 1909 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {IGradientRegistry} from "./interfaces/IGradientRegistry.sol";
import {IGradientMarketMakerPoolV3} from "./interfaces/IGradientMarketMakerPoolV3.sol";
import {IGradientFeeManager} from "./interfaces/IGradientFeeManager.sol";
import {IUniswapV2Router02} from "./interfaces/IUniswapV2Router.sol";
import {IFallbackExecutor} from "./interfaces/IFallbackExecutor.sol";
import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import {IUniswapV2Factory} from "./interfaces/IUniswapV2Factory.sol";
import {IUniswapV2Pair} from "./interfaces/IUniswapV2Pair.sol";
import {IUniswapV3Factory} from "./interfaces/IUniswapV3Factory.sol";
import {IUniswapV3Pool} from "./interfaces/IUniswapV3Pool.sol";
import {IUniswapV3PriceHelper} from "./interfaces/IUniswapV3PriceHelper.sol";
import {GradientMarketMakerFactory} from "./GradientMarketMakerFactory.sol";
/**
* @title GradientOrderbook
* @author Gradient Protocol
* @notice A decentralized orderbook for trading ERC20 tokens against ETH
* @dev This contract implements a traditional orderbook with limit and market orders.
*/
contract GradientOrderbook is Ownable, ReentrancyGuard {
using SafeERC20 for IERC20;
/// @notice Registry contract for accessing other protocol contracts
IGradientRegistry public gradientRegistry;
/// @notice Fee manager contract for handling fee distribution
IGradientFeeManager public feeManager;
/// @notice Types of orders that can be placed
enum OrderType {
Buy,
Sell
}
/// @notice Types of order execution
enum OrderExecutionType {
Limit,
Market
}
/// @notice Possible states of an order
enum OrderStatus {
Active,
Filled,
Cancelled,
Expired
}
/// @notice Structure containing all information about an order
/// @dev All amounts use the decimal precision of their respective tokens
struct Order {
uint256 orderId; // Unique identifier for the order
address owner; // Address that created the order
OrderType orderType; // Whether this is a buy or sell order
OrderExecutionType executionType; // Whether this is a limit or market order
address token; // Token being traded
uint256 amount; // Total amount of tokens to trade
uint256 price; // For limit orders: exact price, For market orders: max price (buy) or min price (sell)
uint256 ethAmount; // Amount of ETH committed for buy orders
uint256 ethSpent; // Actual ETH spent so far (for buy market orders)
uint256 filledAmount; // Amount of tokens that have been filled
uint256 expirationTime; // Timestamp when the order expires
OrderStatus status; // Current status of the order
}
/// @notice Parameters for matching orders
struct OrderMatch {
uint256 buyOrderId; // ID of the buy order
uint256 sellOrderId; // ID of the sell order
uint256 fillAmount; // Amount of tokens to exchange
}
/// @notice Counter for generating unique order IDs
uint256 private _orderIdCounter;
/// @notice Default fee percentage charged on all trades (in basis points, 1 = 0.01%)
uint256 public defaultFeePercentage;
/// @notice Token-specific fee percentages (in basis points, 1 = 0.01%)
/// @dev Default is 100 basis points (1%) for all tokens
mapping(address => uint256) public tokenSpecificFeePercentage;
/// @notice Maximum fee percentage that can be set (in basis points)
uint256 public constant MAX_FEE_PERCENTAGE = 500; // 5%
/// @notice Minimum fee percentage that can be set (in basis points)
uint256 public constant MIN_FEE_PERCENTAGE = 50; // 0.5%
/// @notice Maximum token-specific fee percentage (in basis points)
uint256 public constant MAX_TOKEN_SPECIFIC_FEE_PERCENTAGE = 300; // 3%
/// @notice Mapping from order ID to Order struct
mapping(uint256 => Order) public orders;
/// @notice Mapping from token pair + order type + execution type hash to array of order IDs
/// @dev Key is keccak256(abi.encodePacked(token, orderType, executionType))
mapping(bytes32 => uint256) public totalOrderCount;
mapping(bytes32 => uint256) public headOrder;
mapping(bytes32 => uint256) public tailOrder;
struct LinkedOrder {
uint256 prev;
uint256 next;
bool exists;
}
mapping(bytes32 => mapping(uint256 => LinkedOrder)) public linkedOrders;
/// @notice Mapping from order ID to its position in the queue
/// @dev Used for efficient removal of orders from queues
mapping(uint256 => uint256) private orderQueuePositions;
/// @notice Divisor used for fee calculations (10000 = 100%)
uint256 public constant DIVISOR = 10000;
uint256 public minOrderSize;
uint256 public maxOrderSize;
uint256 public maxOrderTtl;
/// @notice Maximum allowed price deviation from market price (in basis points, 1 = 0.01%)
uint256 public maxPriceDeviation = 500; // 5% default
/// @notice Dust tolerance for automatic order fulfillment (in basis points, 1 = 0.01%)
uint256 public dustTolerance = 100; // 1% default
/// @notice Uniswap V3 Factory address for accessing V3 pools
address public uniswapV3Factory;
/// @notice Uniswap V3 Price Helper contract for price calculations
IUniswapV3PriceHelper public uniswapV3PriceHelper;
/// @notice Common fee tiers for Uniswap V3 (500 = 0.05%, 3000 = 0.3%, 10000 = 1%)
uint24[] public v3FeeTiers = [500, 3000, 10000];
/// @notice Emitted when a new order is created
event OrderCreated(
uint256 indexed orderId,
address indexed owner,
OrderType orderType,
OrderExecutionType executionType,
address token,
uint256 amount,
uint256 price,
uint256 expirationTime,
uint256 totalCost,
string objectId
);
/// @notice Emitted when an order is cancelled by its owner
event OrderCancelled(uint256 indexed orderId);
/// @notice Emitted when an order expires
event OrderExpired(uint256 indexed orderId);
/// @notice Emitted when an order is completely filled
event OrderFulfilled(
uint256 indexed orderId,
uint256 amount,
uint256 totalFilledAmount,
uint256 executionPrice
);
/// @notice Emitted when an order is partially filled
event OrderPartiallyFulfilled(
uint256 indexed orderId,
uint256 amount,
uint256 remaining,
uint256 totalFilledAmount,
uint256 executionPrice
);
/// @notice Emitted when default fee percentage is updated
event DefaultFeePercentageUpdated(
uint256 oldFeePercentage,
uint256 newFeePercentage
);
/// @notice Emitted when token-specific fee percentage is updated
event TokenSpecificFeePercentageUpdated(
address indexed token,
uint256 oldFeePercentage,
uint256 newFeePercentage
);
event OrderSizeLimitsUpdated(uint256 minSize, uint256 maxSize);
event MaxTTLUpdated(uint256 newMaxTTL);
event RateLimitUpdated(uint256 newInterval);
/// @notice Emitted when an order is fulfilled through matching
event OrderFulfilledByMatching(
uint256 indexed orderId,
uint256 indexed matchedOrderId,
uint256 amount,
uint256 price
);
/// @notice Emitted when an order is fulfilled through market maker
event OrderFulfilledByMarketMaker(
uint256 indexed orderId,
address indexed marketMakerPool,
uint256 amount,
uint256 price
);
/// @notice Emitted when fees are distributed to market maker pool
event FeeDistributedToPool(
address indexed marketMakerPool,
address indexed token,
uint256 amount,
uint256 totalFee
);
/// @notice Emitted when max price deviation is updated
event MaxPriceDeviationUpdated(uint256 oldDeviation, uint256 newDeviation);
/// @notice Emitted when dust tolerance is updated
event DustToleranceUpdated(uint256 oldTolerance, uint256 newTolerance);
/// @notice Emitted when Uniswap V3 Factory is updated
event UniswapV3FactoryUpdated(
address indexed oldFactory,
address indexed newFactory
);
/// @notice Emitted when Uniswap V3 Price Helper is updated
event UniswapV3PriceHelperUpdated(address indexed priceHelper);
/// @notice Emitted when V3 fee tiers are updated
event V3FeeTiersUpdated(uint24[] feeTiers);
// Modifiers
modifier onlyAuthorizedFulfiller() {
require(
gradientRegistry.isAuthorizedFulfiller(msg.sender),
"Caller is not authorized"
);
_;
}
modifier orderExists(uint256 orderId) {
require(orders[orderId].owner != address(0), "Order does not exist");
_;
}
modifier onlyOrderOwner(uint256 orderId) {
require(orders[orderId].owner == msg.sender, "Not order owner");
_;
}
modifier validToken(address token) {
require(token != address(0), "Invalid token");
require(token.code.length > 0, "Not a contract");
// Check if token is blocked
require(!gradientRegistry.blockedTokens(token), "Token is blocked");
_;
}
modifier validateMarketOrderPrice(uint256 orderId, uint256 executionPrice) {
Order memory order = orders[orderId];
if (order.executionType == OrderExecutionType.Market) {
if (order.orderType == OrderType.Buy) {
require(
executionPrice <= order.price,
"Execution price exceeds buyer's max price"
);
} else {
require(
executionPrice >= order.price,
"Execution price below seller's min price"
);
}
}
if (order.executionType == OrderExecutionType.Limit) {
require(
executionPrice == order.price,
"Execution price not matched with order price."
);
}
_;
}
constructor(IGradientRegistry _gradientRegistry) Ownable(msg.sender) {
gradientRegistry = _gradientRegistry;
// feeManager will be set via setGradientRegistry after deployment
defaultFeePercentage = 100; // 1% default fee for all trades
minOrderSize = 1000000000000; // 0.000001 ETH
maxOrderSize = 1000 ether;
maxOrderTtl = 30 days;
}
receive() external payable {}
fallback() external payable {}
/// @notice Internal function to calculate and collect ETH fees
/// @param amount Amount in ETH to calculate fee from
/// @param token Token address for potential token-specific fee
/// @return uint256 Fee amount collected
function _collectEthFee(
uint256 amount,
address token
) internal returns (uint256) {
// Use token-specific fee if set, otherwise use default fee
uint256 feePercentage = getCurrentFeePercentage(token);
uint256 feeAmount = (amount * feePercentage) / DIVISOR;
if (feeAmount > 0) {
// Transfer ETH to feeManager and track
feeManager.collectEthFee{value: feeAmount}(feeAmount, token);
}
return feeAmount;
}
/// @notice Internal function to calculate and collect token fees
/// @param amount Amount in tokens to calculate fee from
/// @param token Token address
/// @return uint256 Fee amount collected
function _collectTokenFee(
uint256 amount,
address token
) internal returns (uint256) {
// Use token-specific fee if set, otherwise use default fee
uint256 feePercentage = getCurrentFeePercentage(token);
uint256 feeAmount = (amount * feePercentage) / DIVISOR;
if (feeAmount > 0) {
// Transfer tokens to feeManager
IERC20(token).safeTransfer(address(feeManager), feeAmount);
// Track the fee in feeManager
feeManager.collectTokenFee(feeAmount, token);
}
return feeAmount;
}
/// @notice Internal function to distribute market maker fees according to new split logic
/// @param totalFee Total fee amount to distribute
/// @param token Token address for partner token check
/// @param marketMakerPool Market maker pool address for distribution
function _distributeMarketMakerTokenFees(
uint256 totalFee,
address token,
address marketMakerPool
) internal {
feeManager.distributeMarketMakerTokenFees(
totalFee,
token,
marketMakerPool
);
}
/// @notice Internal function to distribute market maker ETH fees according to new split logic
/// @param totalFee Total ETH fee amount to distribute
/// @param token Token address for partner token check
/// @param marketMakerPool Market maker pool address for distribution
function _distributeMarketMakerEthFees(
uint256 totalFee,
address token,
address marketMakerPool
) internal {
feeManager.distributeMarketMakerEthFees{value: totalFee}(
totalFee,
token,
marketMakerPool
);
}
/// @notice Adds an order to its appropriate queue
/// @param orderId The ID of the order to add
/// @param token The token address
/// @param orderType The type of order (Buy/Sell)
/// @param executionType The type of execution (Limit/Market)
function _addOrderToQueue(
uint256 orderId,
address token,
OrderType orderType,
OrderExecutionType executionType
) internal {
bytes32 queueKey = _getQueueKey(token, orderType, executionType);
linkedOrders[queueKey][orderId] = LinkedOrder({
prev: tailOrder[queueKey],
next: 0,
exists: true
});
if (tailOrder[queueKey] != 0) {
linkedOrders[queueKey][tailOrder[queueKey]].next = orderId;
} else {
headOrder[queueKey] = orderId;
}
tailOrder[queueKey] = orderId;
// Store the position of the order in the queue
orderQueuePositions[orderId] = totalOrderCount[queueKey];
totalOrderCount[queueKey] += 1;
}
function _removeOrderFromLinkedQueue(
bytes32 queueKey,
uint256 orderId
) internal {
LinkedOrder storage node = linkedOrders[queueKey][orderId];
require(node.exists, "Order not in queue");
if (node.prev != 0) {
linkedOrders[queueKey][node.prev].next = node.next;
} else {
headOrder[queueKey] = node.next;
}
if (node.next != 0) {
linkedOrders[queueKey][node.next].prev = node.prev;
} else {
tailOrder[queueKey] = node.prev;
}
delete linkedOrders[queueKey][orderId];
}
/// @notice Creates a new order in the orderbook
/// @param orderType Type of order (Buy/Sell)
/// @param executionType Type of execution (Limit/Market)
/// @param token Address of the token to trade
/// @param amount Amount of tokens to trade
/// @param price For limit orders: exact price, For market orders: max price (buy) or min price (sell)
/// @param ttl Time-to-live in seconds for the order
/// @dev For buy orders, requires ETH to be sent with the transaction
/// @dev For sell orders, requires token approval
/// @return uint256 ID of the created order
function createOrder(
OrderType orderType,
OrderExecutionType executionType,
address token,
uint256 amount,
uint256 price,
uint256 ttl,
string memory objectId
) external payable validToken(token) nonReentrant returns (uint256) {
require(amount > 0, "Amount must be greater than 0");
require(price > 0, "Invalid price range");
require(ttl > 0, "TTL must be greater than 0");
require(ttl <= maxOrderTtl, "TTL too long");
// Normalize token amount to 18 decimals for consistent price calculations
uint256 normalizedAmount = normalizeTo18Decimals(amount, token);
require(
normalizedAmount <= type(uint256).max / price,
"Price calculation would overflow"
);
uint256 totalCost = (normalizedAmount * price) / 1e18;
require(totalCost >= minOrderSize, "Order too small");
require(totalCost <= maxOrderSize, "Order too large");
if (orderType == OrderType.Buy) {
require(msg.value >= totalCost, "Insufficient ETH sent");
} else {
IERC20(token).safeTransferFrom(msg.sender, address(this), amount);
}
uint256 orderId = _orderIdCounter++;
uint256 expirationTime = block.timestamp + ttl;
orders[orderId] = Order({
orderId: orderId,
owner: msg.sender,
orderType: orderType,
executionType: executionType,
token: token,
amount: normalizedAmount, // Store normalized amount for calculations
price: price,
ethAmount: (orderType == OrderType.Buy) ? totalCost : 0,
ethSpent: 0,
filledAmount: 0,
expirationTime: expirationTime,
status: OrderStatus.Active
});
_addOrderToQueue(orderId, token, orderType, executionType);
emit OrderCreated(
orderId,
msg.sender,
orderType,
executionType,
token,
amount, // Emit original amount for transparency
price,
expirationTime,
totalCost,
objectId
);
if (orderType == OrderType.Buy && msg.value > totalCost) {
(bool success, ) = msg.sender.call{value: msg.value - totalCost}(
""
);
require(success, "ETH return failed");
}
return orderId;
}
/// @notice Cancels an active order
/// @param orderId ID of the order to cancel
/// @dev Only the order owner can cancel their order
/// @dev Refunds ETH for buy orders and tokens for sell orders
function cancelOrder(
uint256 orderId
) external nonReentrant orderExists(orderId) onlyOrderOwner(orderId) {
Order storage order = orders[orderId];
require(order.status == OrderStatus.Active, "Order not active");
require(!isOrderExpired(orderId), "Order expired");
order.status = OrderStatus.Cancelled;
if (order.orderType == OrderType.Buy) {
uint256 refundAmount;
if (order.executionType == OrderExecutionType.Market) {
refundAmount = order.ethAmount > order.ethSpent
? (order.ethAmount - order.ethSpent)
: 0;
} else {
uint256 remainingAmount = order.amount > order.filledAmount
? (order.amount - order.filledAmount)
: 0;
refundAmount = (remainingAmount * order.price) / 1e18;
}
if (refundAmount > 0) {
require(
address(this).balance >= refundAmount,
"Insufficient ETH in contract"
);
(bool success, ) = order.owner.call{value: refundAmount}("");
require(success, "ETH refund failed");
}
} else {
uint256 remainingAmount = order.amount > order.filledAmount
? (order.amount - order.filledAmount)
: 0;
if (remainingAmount > 0) {
uint256 actualRemainingAmount = denormalizeFrom18Decimals(
remainingAmount,
order.token
);
IERC20(order.token).safeTransfer(
order.owner,
actualRemainingAmount
);
}
}
bytes32 queueKey = _getQueueKey(
order.token,
order.orderType,
order.executionType
);
_removeOrderFromLinkedQueue(queueKey, orderId);
emit OrderCancelled(orderId);
}
/// @notice Marks multiple expired orders as expired and handles refunds
/// @param orderIds Array of IDs of expired orders to clean up
/// @dev Anyone can call this function for expired orders
/// @dev Refunds tokens for unfilled sell orders and ETH for unfilled buy orders
/// @dev More gas efficient than calling cleanupExpiredOrder multiple times
function cleanupExpiredOrders(
uint256[] memory orderIds
) external nonReentrant {
require(orderIds.length > 0, "No orders to clean up");
require(orderIds.length <= 100, "Too many orders to clean up at once");
for (uint256 i = 0; i < orderIds.length; i++) {
uint256 orderId = orderIds[i];
// Check if order exists
require(
orders[orderId].owner != address(0),
"Order does not exist"
);
Order storage order = orders[orderId];
require(order.status == OrderStatus.Active, "Order not active");
require(isOrderExpired(orderId), "Order not expired");
order.status = OrderStatus.Expired;
if (order.orderType == OrderType.Sell) {
uint256 remainingAmount = order.amount > order.filledAmount
? (order.amount - order.filledAmount)
: 0;
if (remainingAmount > 0) {
// Denormalize the remaining amount back to token decimals
uint256 actualRemainingAmount = denormalizeFrom18Decimals(
remainingAmount,
order.token
);
IERC20(order.token).safeTransfer(
order.owner,
actualRemainingAmount
);
}
}
if (order.orderType == OrderType.Buy) {
uint256 refundAmount;
if (order.executionType == OrderExecutionType.Market) {
refundAmount = order.ethAmount > order.ethSpent
? (order.ethAmount - order.ethSpent)
: 0;
} else {
uint256 remainingAmount = order.amount > order.filledAmount
? (order.amount - order.filledAmount)
: 0;
refundAmount = (remainingAmount * order.price) / 1e18;
}
if (refundAmount > 0) {
require(
address(this).balance >= refundAmount,
"Insufficient ETH in contract"
);
(bool success, ) = payable(order.owner).call{
value: refundAmount
}("");
require(success, "ETH refund failed");
}
}
bytes32 queueKey = _getQueueKey(
order.token,
order.orderType,
order.executionType
);
_removeOrderFromLinkedQueue(queueKey, orderId);
emit OrderExpired(orderId);
}
}
/// @notice Fulfills multiple matched limit orders
/// @param matches Array of OrderMatch structs containing match details
/// @dev Only whitelisted fulfillers can call this function
/// @dev All orders in matches must be limit orders
/// @dev This function matches buy and sell orders against each other
function fulfillLimitOrders(
OrderMatch[] calldata matches
) external nonReentrant onlyAuthorizedFulfiller {
require(matches.length > 0, "No order matches to fulfill");
for (uint256 i = 0; i < matches.length; i++) {
_fulfillLimitOrders(matches[i]);
}
}
/// @notice Fulfills multiple matched market orders through order matching
/// @param matches Array of OrderMatch structs containing match details
/// @param executionPrices Array of execution prices for each match
/// @dev Only whitelisted fulfillers can call this function
/// @dev All orders in matches must be market orders
/// @dev This function matches buy and sell orders against each other
function fulfillMarketOrders(
OrderMatch[] calldata matches,
uint256[] calldata executionPrices
) external nonReentrant onlyAuthorizedFulfiller {
require(matches.length > 0, "No order matches to fulfill");
require(
matches.length == executionPrices.length,
"Mismatched arrays length"
);
for (uint256 i = 0; i < matches.length; i++) {
_fulfillMarketOrders(matches[i], executionPrices[i]);
}
}
/// @notice Internal function to fulfill a matched pair of limit orders
/// @param _match OrderMatch struct containing the match details
/// @dev Handles the transfer of ETH and tokens between parties
/// @dev Allows partial fills of either order
function _fulfillLimitOrders(OrderMatch memory _match) internal {
Order storage buyOrder = orders[_match.buyOrderId];
Order storage sellOrder = orders[_match.sellOrderId];
// Validate orders
require(
buyOrder.status == OrderStatus.Active &&
sellOrder.status == OrderStatus.Active,
"Orders must be active"
);
require(
!isOrderExpired(_match.buyOrderId) &&
!isOrderExpired(_match.sellOrderId),
"1 of the orders expired"
);
require(
buyOrder.orderType == OrderType.Buy &&
sellOrder.orderType == OrderType.Sell,
"Invalid order types"
);
require(buyOrder.token == sellOrder.token, "Token mismatch");
require(
buyOrder.owner != sellOrder.owner,
"Seller and buyer cannot be the same"
);
require(
buyOrder.executionType == OrderExecutionType.Limit &&
sellOrder.executionType == OrderExecutionType.Limit,
"Not limit orders"
);
// Handle different fulfillment types
_fulfillLimitOrdersMatching(_match);
}
/// @notice Internal function to fulfill limit orders through matching
/// @param _match OrderMatch struct containing the match details
function _fulfillLimitOrdersMatching(OrderMatch memory _match) internal {
Order storage buyOrder = orders[_match.buyOrderId];
Order storage sellOrder = orders[_match.sellOrderId];
require(
buyOrder.price >= sellOrder.price,
"Price mismatch for limit orders"
);
uint256 buyRemaining = buyOrder.amount > buyOrder.filledAmount
? (buyOrder.amount - buyOrder.filledAmount)
: 0;
uint256 sellRemaining = sellOrder.amount > sellOrder.filledAmount
? (sellOrder.amount - sellOrder.filledAmount)
: 0;
uint256 actualFillAmount = _match.fillAmount;
if (actualFillAmount > buyRemaining) {
actualFillAmount = buyRemaining;
}
if (actualFillAmount > sellRemaining) {
actualFillAmount = sellRemaining;
}
require(actualFillAmount > 0, "No amount to fill");
uint256 tokenAmount = actualFillAmount;
uint256 paymentAmount = (actualFillAmount * sellOrder.price) / 1e18; // Use sell price for limit orders
// Calculate fees from receiving amounts
uint256 buyerFee = _collectEthFee(paymentAmount, sellOrder.token); // ETH fee from buyer's payment
uint256 sellerFee = _collectTokenFee(tokenAmount, sellOrder.token); // Token fee from seller's tokens
// Transfer ETH to seller (buyer pays ETH fee)
uint256 sellerPayment = paymentAmount - buyerFee;
(bool success, ) = sellOrder.owner.call{value: sellerPayment}("");
require(success, "ETH transfer to seller failed");
// Transfer tokens to buyer (seller pays token fee)
{
uint256 actualTokenAmount = denormalizeFrom18Decimals(
tokenAmount,
sellOrder.token
);
uint256 actualTokenFee = denormalizeFrom18Decimals(
sellerFee,
sellOrder.token
);
IERC20(sellOrder.token).safeTransfer(
buyOrder.owner,
actualTokenAmount - actualTokenFee
);
}
buyOrder.filledAmount += actualFillAmount;
sellOrder.filledAmount += actualFillAmount;
if (buyOrder.price > sellOrder.price) {
uint256 savedAmount = (actualFillAmount *
(buyOrder.price - sellOrder.price)) / 1e18;
(success, ) = buyOrder.owner.call{value: savedAmount}("");
require(success, "ETH savings return failed");
}
_updateOrderStatus(
_match.buyOrderId,
actualFillAmount,
sellOrder.price
);
_updateOrderStatus(
_match.sellOrderId,
actualFillAmount,
buyOrder.price
);
}
/// @notice Internal function to fulfill a matched pair of market orders
/// @param _match OrderMatch struct containing the match details
/// @param executionPrice The price at which the orders will be executed
/// @dev Handles the transfer of ETH and tokens between parties
/// @dev Allows partial fills of either order
function _fulfillMarketOrders(
OrderMatch memory _match,
uint256 executionPrice
) internal {
Order storage buyOrder = orders[_match.buyOrderId];
Order storage sellOrder = orders[_match.sellOrderId];
// Validate orders
require(
buyOrder.status == OrderStatus.Active &&
sellOrder.status == OrderStatus.Active,
"Orders must be active"
);
require(
!isOrderExpired(_match.buyOrderId) &&
!isOrderExpired(_match.sellOrderId),
"Orders expired"
);
require(
buyOrder.orderType == OrderType.Buy &&
sellOrder.orderType == OrderType.Sell,
"Invalid order types"
);
require(buyOrder.token == sellOrder.token, "Token mismatch");
require(
(buyOrder.executionType == OrderExecutionType.Market ||
sellOrder.executionType == OrderExecutionType.Market),
"Not market orders"
);
_fulfillMarketOrdersMatching(_match, executionPrice);
}
/// @notice Internal function to fulfill market orders through matching
/// @param _match OrderMatch struct containing the match details
/// @param executionPrice The price at which the orders will be executed
function _fulfillMarketOrdersMatching(
OrderMatch memory _match,
uint256 executionPrice
) internal {
Order storage buyOrder = orders[_match.buyOrderId];
Order storage sellOrder = orders[_match.sellOrderId];
// Validate execution price against market price
require(
validateExecutionPrice(buyOrder.token, executionPrice),
"Execution price deviates too much from market price"
);
if (buyOrder.executionType == OrderExecutionType.Market) {
require(
executionPrice <= buyOrder.price,
"Execution price exceeds buyer's max price"
);
}
if (sellOrder.executionType == OrderExecutionType.Market) {
require(
executionPrice >= sellOrder.price,
"Execution price below seller's min price"
);
}
uint256 buyRemaining = getBuyOrderRemainingAmount(
buyOrder,
executionPrice
);
uint256 sellRemaining = sellOrder.amount > sellOrder.filledAmount
? (sellOrder.amount - sellOrder.filledAmount)
: 0;
uint256 actualFillAmount = _match.fillAmount;
if (actualFillAmount > buyRemaining) {
actualFillAmount = buyRemaining;
}
if (actualFillAmount > sellRemaining) {
actualFillAmount = sellRemaining;
}
require(actualFillAmount > 0, "No amount to fill");
uint256 tokenAmount = actualFillAmount;
uint256 paymentAmount = (actualFillAmount * executionPrice) / 1e18;
// Calculate fees from receiving amounts
uint256 buyerFee = _collectEthFee(paymentAmount, sellOrder.token); // ETH fee from buyer's payment
uint256 sellerFee = _collectTokenFee(tokenAmount, sellOrder.token); // Token fee from seller's tokens
// Transfer ETH to seller (buyer pays ETH fee)
uint256 sellerPayment = paymentAmount - buyerFee;
(bool success, ) = sellOrder.owner.call{value: sellerPayment}("");
require(success, "ETH transfer to seller failed");
{
uint256 actualTokenAmount = denormalizeFrom18Decimals(
tokenAmount,
sellOrder.token
);
uint256 actualTokenFee = denormalizeFrom18Decimals(
sellerFee,
sellOrder.token
);
IERC20(sellOrder.token).safeTransfer(
buyOrder.owner,
actualTokenAmount - actualTokenFee
);
}
buyOrder.filledAmount += actualFillAmount;
// Track actual ETH spent for buy market orders
if (
buyOrder.orderType == OrderType.Buy &&
buyOrder.executionType == OrderExecutionType.Market
) {
buyOrder.ethSpent += paymentAmount;
}
sellOrder.filledAmount += actualFillAmount;
_updateOrderStatus(_match.buyOrderId, actualFillAmount, executionPrice);
_updateOrderStatus(
_match.sellOrderId,
actualFillAmount,
executionPrice
);
}
/// @notice Fulfills multiple orders through the market maker pool
/// @param orderIds Array of order IDs to fulfill
/// @param fillAmounts Array of fill amounts for each order
/// @param executionPrices Array of execution prices for each order
/// @param merkleRoot The merkle root to use for position updates
/// @dev Only whitelisted fulfillers can call this function
function fulfillOrdersWithMarketMaker(
uint256[] calldata orderIds,
uint256[] calldata fillAmounts,
uint256[] calldata executionPrices,
bytes32 merkleRoot
) external nonReentrant onlyAuthorizedFulfiller {
require(orderIds.length > 0, "No orders to fulfill");
require(
orderIds.length == fillAmounts.length &&
orderIds.length == executionPrices.length,
"Mismatched arrays length"
);
for (uint256 i = 0; i < orderIds.length; i++) {
require(fillAmounts[i] > 0, "Fill amount must be greater than 0");
require(
executionPrices[i] > 0,
"Execution price must be greater than 0"
);
_fulfillOrderWithMarketMaker(
orderIds[i],
fillAmounts[i],
executionPrices[i],
merkleRoot
);
}
}
/// @notice Internal function to fulfill a single order through the market maker pool
/// @param orderId ID of the order to fulfill
/// @param fillAmount Amount of tokens to fill
/// @param executionPrice The price at which the order will be executed
/// @param merkleRoot The merkle root to use for position updates
function _fulfillOrderWithMarketMaker(
uint256 orderId,
uint256 fillAmount,
uint256 executionPrice,
bytes32 merkleRoot
) internal validateMarketOrderPrice(orderId, executionPrice) {
Order storage order = orders[orderId];
// Validate order
require(order.status == OrderStatus.Active, "Order not active");
require(!isOrderExpired(orderId), "Order expired");
// Validate execution price against market price
require(
validateExecutionPrice(order.token, executionPrice),
"Execution price deviates too much from market price"
);
address marketMakerFactory = gradientRegistry.marketMakerFactory();
require(
marketMakerFactory != address(0),
"Market maker factory not set"
);
address marketMakerPool = GradientMarketMakerFactory(marketMakerFactory)
.getPool(order.token);
require(
marketMakerPool != address(0),
"Market maker pool not found for token"
);
// Calculate actual fill amount based on remaining amount
uint256 remainingAmount;
if (
order.orderType == OrderType.Buy &&
order.executionType == OrderExecutionType.Market
) {
remainingAmount = getBuyOrderRemainingAmount(order, executionPrice);
} else {
remainingAmount = order.amount > order.filledAmount
? (order.amount - order.filledAmount)
: 0;
}
uint256 actualFillAmount = fillAmount > remainingAmount
? remainingAmount
: fillAmount;
require(actualFillAmount > 0, "No amount to fill");
// Calculate payment amount and fees
uint256 paymentAmount = (actualFillAmount * executionPrice) / 1e18;
if (order.orderType == OrderType.Buy) {
// Buy order from order to get tokens from market maker pool
uint256 actualTokenAmount = denormalizeFrom18Decimals(
actualFillAmount,
order.token
);
// For buy orders: orderbook sends ETH to market maker, receives tokens
IGradientMarketMakerPoolV3(marketMakerPool).executeBuyOrder{
value: paymentAmount
}(paymentAmount, actualTokenAmount, merkleRoot);
// Calculate fee from received tokens and deduct from user
uint256 tokenFee = (actualTokenAmount *
(getCurrentFeePercentage(order.token))) / DIVISOR;
uint256 netTokenAmount = actualTokenAmount - tokenFee;
// Distribute fees using new market maker split logic (50% to market makers, 50% to teams)
if (tokenFee > 0) {
// Transfer fee tokens to feeManager
IERC20(order.token).safeTransfer(address(feeManager), tokenFee);
// Distribute according to the split
_distributeMarketMakerTokenFees(
tokenFee,
order.token,
marketMakerPool
);
}
IERC20(order.token).safeTransfer(order.owner, netTokenAmount);
} else {
// Denormalize the amount for token approval
uint256 actualTokenAmount = denormalizeFrom18Decimals(
actualFillAmount,
order.token
);
// For sell orders: orderbook sends full tokens to market maker, receives ETH
IERC20(order.token).approve(marketMakerPool, actualTokenAmount);
// Execute sell order - Orderbook sends tokens, receives ETH
IGradientMarketMakerPoolV3(marketMakerPool).executeSellOrder(
paymentAmount,
actualTokenAmount,
merkleRoot
);
// Calculate fee from received ETH and deduct from user
uint256 ethFee = (paymentAmount *
(getCurrentFeePercentage(order.token))) / DIVISOR;
uint256 netEthAmount = paymentAmount - ethFee;
// Distribute fees using new market maker split logic (50% to market makers, 50% to teams)
if (ethFee > 0) {
// Distribute according to the split
_distributeMarketMakerEthFees(
ethFee,
order.token,
marketMakerPool
);
}
// Transfer ETH to seller (minus fee)
(bool success, ) = order.owner.call{value: netEthAmount}("");
require(success, "ETH transfer to seller failed");
}
// Update order state
order.filledAmount += actualFillAmount;
// Track actual ETH spent for buy market orders
if (
order.orderType == OrderType.Buy &&
order.executionType == OrderExecutionType.Market
) {
order.ethSpent += paymentAmount;
}
// Update order status
_updateOrderStatus(orderId, actualFillAmount, executionPrice);
emit OrderFulfilledByMarketMaker(
orderId,
marketMakerPool,
actualFillAmount,
executionPrice
);
}
/// @notice Allows users to fulfill their own order via AMM
/// @param orderId ID of the order to fulfill
/// @param fillAmount Amount of tokens to fill
/// @param minAmountOut Minimum amount to receive (slippage protection)
/// @dev Only the order owner can call this function
/// @dev Uses FallbackExecutor to find the best DEX and execute the trade
function fulfillOwnOrderWithAMM(
uint256 orderId,
uint256 fillAmount,
uint256 minAmountOut
) external nonReentrant orderExists(orderId) onlyOrderOwner(orderId) {
require(fillAmount > 0, "Fill amount must be greater than 0");
Order storage order = orders[orderId];
require(order.status == OrderStatus.Active, "Order not active");
// Calculate actual fill amount based on remaining amount
uint256 remainingAmount;
if (
order.orderType == OrderType.Buy &&
order.executionType == OrderExecutionType.Market
) {
remainingAmount = getBuyOrderRemainingAmount(order, order.price);
} else {
remainingAmount = order.amount > order.filledAmount
? (order.amount - order.filledAmount)
: 0;
}
uint256 actualFillAmount = fillAmount > remainingAmount
? remainingAmount
: fillAmount;
require(actualFillAmount > 0, "No amount to fill");
// Get FallbackExecutor from registry
address fallbackExecutor = gradientRegistry.fallbackExecutor();
require(fallbackExecutor != address(0), "FallbackExecutor not set");
// For buy orders, calculate how much ETH to send based on order type
uint256 ethToSend;
uint256 effectiveExecutionPrice;
if (order.orderType == OrderType.Buy) {
if (order.executionType == OrderExecutionType.Market) {
// For market orders, send the remaining ETH (up to the fill amount)
uint256 ethRemaining = order.ethAmount > order.ethSpent
? (order.ethAmount - order.ethSpent)
: 0;
ethToSend = ethRemaining;
} else {
// For limit orders, calculate based on order price
ethToSend = (actualFillAmount * order.price) / 1e18;
}
// Execute the buy trade directly through FallbackExecutor with full ETH amount
uint256 tokensReceived = IFallbackExecutor(fallbackExecutor)
.executeTrade{value: ethToSend}(
order.token,
ethToSend,
minAmountOut,
true // isBuy = true
);
// Calculate fee from received tokens and deduct from user (buy order receives tokens)
uint256 tokenFee = _collectTokenFee(tokensReceived, order.token);
uint256 netTokenAmount = tokensReceived - tokenFee;
// Transfer tokens to the buyer (minus fee)
IERC20(order.token).safeTransfer(order.owner, netTokenAmount);
// Calculate effective execution price for buy orders
if (tokensReceived > 0) {
effectiveExecutionPrice = (ethToSend * 1e18) / tokensReceived;
} else {
effectiveExecutionPrice = order.price; // Fallback to order price
}
} else {
// Denormalize the amount for token approval and transfer
uint256 actualTokenAmount = denormalizeFrom18Decimals(
actualFillAmount,
order.token
);
// Approve tokens to FallbackExecutor (approve the full amount)
IERC20(order.token).approve(fallbackExecutor, actualTokenAmount);
// Execute the sell trade with full amount
uint256 ethReceived = IFallbackExecutor(fallbackExecutor)
.executeTrade(
order.token,
actualTokenAmount,
minAmountOut,
false // isBuy = false
);
// Calculate fee from received ETH and deduct from user (sell order receives ETH)
uint256 ethFee = _collectEthFee(ethReceived, order.token);
uint256 netEthAmount = ethReceived - ethFee;
(bool success, ) = order.owner.call{value: netEthAmount}("");
require(success, "ETH transfer to seller failed");
// Calculate effective execution price for sell orders
if (actualFillAmount > 0) {
effectiveExecutionPrice =
(ethReceived * 1e18) /
actualFillAmount;
} else {
effectiveExecutionPrice = order.price; // Fallback to order price
}
}
order.filledAmount += actualFillAmount;
// Track actual ETH spent for buy market orders
if (
order.orderType == OrderType.Buy &&
order.executionType == OrderExecutionType.Market
) {
order.ethSpent += ethToSend;
}
// Update order status
_updateOrderStatus(orderId, actualFillAmount, effectiveExecutionPrice);
}
/// @notice Internal function to update order status
/// @param orderId ID of the order to update
/// @param actualFillAmount Amount of tokens/ETH that was filled
/// @param executionPrice The price at which the order was executed
function _updateOrderStatus(
uint256 orderId,
uint256 actualFillAmount,
uint256 executionPrice
) internal {
Order storage order = orders[orderId];
// For buy market orders, check if all ETH is spent
if (
order.orderType == OrderType.Buy &&
order.executionType == OrderExecutionType.Market
) {
uint256 remainingEth = order.ethAmount > order.ethSpent
? (order.ethAmount - order.ethSpent)
: 0;
// Check if remaining ETH is below dust tolerance
bool isDustRemaining = remainingEth > 0 &&
(remainingEth * 10000) / order.ethAmount <= dustTolerance;
if (order.ethSpent >= order.ethAmount || isDustRemaining) {
order.status = OrderStatus.Filled;
// If dust remaining, mark it as spent to prevent refund issues
if (isDustRemaining) {
order.ethSpent = order.ethAmount;
}
bytes32 queueKey = _getQueueKey(
order.token,
order.orderType,
order.executionType
);
_removeOrderFromLinkedQueue(queueKey, orderId);
emit OrderFulfilled(
orderId,
actualFillAmount,
order.ethSpent,
executionPrice
);
} else {
emit OrderPartiallyFulfilled(
orderId,
actualFillAmount,
remainingEth,
order.ethSpent,
executionPrice
);
}
} else {
uint256 remainingAmount = order.amount > order.filledAmount
? (order.amount - order.filledAmount)
: 0;
// Check if remaining tokens are below dust tolerance
bool isDustRemaining = remainingAmount > 0 &&
(remainingAmount * 10000) / order.amount <= dustTolerance;
if (order.filledAmount == order.amount || isDustRemaining) {
order.status = OrderStatus.Filled;
// If dust remaining, mark it as filled to prevent refund issues
if (isDustRemaining) {
order.filledAmount = order.amount;
}
bytes32 queueKey = _getQueueKey(
order.token,
order.orderType,
order.executionType
);
_removeOrderFromLinkedQueue(queueKey, orderId);
emit OrderFulfilled(
orderId,
actualFillAmount,
order.filledAmount,
executionPrice
);
} else {
emit OrderPartially...
// [truncated — 72995 bytes total]
IEventAggregator.sol 62 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface IEventAggregator {
// Event types
function ETH_ADD() external view returns (uint8);
function ETH_REMOVE() external view returns (uint8);
function TOKEN_ADD() external view returns (uint8);
function TOKEN_REMOVE() external view returns (uint8);
function REWARDS_CLAIMED() external view returns (uint8);
function TRADE_EXECUTED() external view returns (uint8);
// Main functions
function emitLiquidityEvent(
address user,
address token,
uint8 eventType,
uint256 amount,
uint256 minPrice,
uint256 maxPrice
) external;
function emitETHRewardsClaimed(
address user,
address token,
uint256 amount
) external;
function emitTokenRewardsClaimed(
address user,
address token,
uint256 amount
) external;
function emitTradeExecuted(
address user,
uint8 tradeType,
uint256 ethAmount,
uint256 tokenAmount
) external;
function emitMerkleRootUpdated(
uint256 version,
bytes32 merkleRoot
) external;
function emitPoolCreated(address token, address pool) external;
// View functions
function getEventTypeName(
uint8 eventType
) external pure returns (string memory name);
function getTradeTypeName(
uint8 tradeType
) external pure returns (string memory name);
}
IFallbackExecutor.sol 57 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface IFallbackExecutor {
struct DEXConfig {
address router;
address factory;
bool isActive;
uint256 priority;
}
// Events
event DEXAdded(address indexed dex, address router, address factory);
event DEXRemoved(address indexed dex);
event TradeExecuted(
address indexed token,
address indexed dex,
uint256 amountIn,
uint256 amountOut,
bool isBuy
);
// View functions
function dexes(
address dex
)
external
view
returns (
address router,
address factory,
bool isActive,
uint256 priority
);
function gradientRegistry() external view returns (address);
function getActiveDEXes() external view returns (address[] memory);
function getDEXConfig(address dex) external view returns (DEXConfig memory);
// State changing functions
function addDEX(address dex, address router, uint256 priority) external;
function removeDEX(address dex) external;
function executeTrade(
address token,
uint256 amount,
uint256 minAmountOut,
bool isBuy
) external payable returns (uint256 amountOut);
function emergencyWithdraw(address[] calldata tokens) external;
function emergencyWithdrawETH() external;
}
IGradientFeeManager.sol 156 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
/**
* @title IGradientFeeManager
* @notice Interface for the GradientFeeManager contract
* @dev Handles partner fee distribution and platform fee management
*/
interface IGradientFeeManager {
// ========================== Events ==========================
/// @notice Emitted when ETH fees are withdrawn
event EthFeesWithdrawn(address indexed recipient, uint256 amount);
/// @notice Emitted when token fees are withdrawn
event TokenFeesWithdrawn(
address indexed token,
address indexed recipient,
uint256 amount
);
/// @notice Emitted when partner ETH fees are claimed
event PartnerEthFeesClaimed(
address indexed token,
address indexed partnerWallet,
uint256 amount
);
/// @notice Emitted when partner token fees are claimed
event PartnerTokenFeesClaimed(
address indexed token,
address indexed partnerWallet,
uint256 amount
);
/// @notice Emitted when fees are distributed to teams (market maker only)
event FeeDistributedToTeams(
address indexed token,
uint256 grayTeamFee,
uint256 partnerTeamFee,
uint256 totalTeamFee
);
/// @notice Emitted when fees are distributed to market maker pool
event FeeDistributedToPool(
address indexed marketMakerPool,
address indexed token,
uint256 amount,
uint256 totalFee
);
// ========================== Fee Distribution Functions ==========================
/// @notice Distributes market maker token fees according to partner split logic
/// @param totalFee Total fee amount to distribute
/// @param token Token address for partner token check
/// @param marketMakerPool Market maker pool address for distribution
function distributeMarketMakerTokenFees(
uint256 totalFee,
address token,
address marketMakerPool
) external;
/// @notice Distributes market maker ETH fees according to partner split logic
/// @param totalFee Total ETH fee amount to distribute
/// @param token Token address for partner token check
/// @param marketMakerPool Market maker pool address for distribution
function distributeMarketMakerEthFees(
uint256 totalFee,
address token,
address marketMakerPool
) external payable;
// ========================== Fee Collection Functions ==========================
/// @notice Collects ETH fees and updates totals
/// @param amount Amount in ETH to collect
/// @param token Token address for potential token-specific fee tracking
function collectEthFee(uint256 amount, address token) external payable;
/// @notice Collects token fees and updates totals
/// @param amount Amount in tokens to collect
/// @param token Token address
function collectTokenFee(uint256 amount, address token) external;
// ========================== Fee Withdrawal Functions ==========================
/// @notice Withdraws collected ETH fees to the specified address
/// @param recipient Address to receive the ETH fees
function withdrawEthFees(address payable recipient) external;
/// @notice Withdraws collected token fees to the specified address
/// @param token Address of the token to withdraw fees for
/// @param recipient Address to receive the token fees
function withdrawTokenFees(address token, address recipient) external;
/// @notice Claim partner ETH fees for a specific token
/// @param token Address of the partner token to claim fees for
function claimPartnerEthFees(address token) external;
/// @notice Claim partner token fees for a specific token
/// @param token Address of the partner token to claim fees for
function claimPartnerTokenFees(address token) external;
// ========================== View Functions ==========================
/// @notice Gets total ETH fees collected
/// @return uint256 Total ETH fees collected
function totalEthFeesCollected() external view returns (uint256);
/// @notice Gets total token fees collected for a specific token
/// @param token Token address
/// @return uint256 Total token fees collected
function totalTokenFeesCollected(
address token
) external view returns (uint256);
/// @notice Gets partner ETH fees collected for a specific token
/// @param token Token address
/// @return uint256 Partner ETH fees collected
function partnerEthFeesCollected(
address token
) external view returns (uint256);
/// @notice Gets partner token fees collected for a specific token
/// @param token Token address
/// @return uint256 Partner token fees collected
function partnerTokenFeesCollected(
address token
) external view returns (uint256);
/// @notice Gets platform ETH fees claimed
/// @return uint256 Platform ETH fees claimed
function platformEthFeesClaimed() external view returns (uint256);
/// @notice Gets platform token fees claimed for a specific token
/// @param token Token address
/// @return uint256 Platform token fees claimed
function platformTokenFeesClaimed(
address token
) external view returns (uint256);
/// @notice Gets partner ETH fees claimed for a specific token
/// @param token Token address
/// @return uint256 Partner ETH fees claimed
function partnerEthFeesClaimed(
address token
) external view returns (uint256);
/// @notice Gets partner token fees claimed for a specific token
/// @param token Token address
/// @return uint256 Partner token fees claimed
function partnerTokenFeesClaimed(
address token
) external view returns (uint256);
}
IGradientMarketMakerFactory.sol 56 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface IGradientMarketMakerFactory {
// Events
event PoolCreated(
address indexed token,
address indexed pool,
uint256 timestamp
);
// Pool management
function createPool(address token) external returns (address pool);
function createPoolWithLiquidity(
address token,
address initialLiquidityProvider,
uint256 initialEthAmount,
uint256 initialTokenAmount
) external payable returns (address pool);
function getPool(address token) external view returns (address pool);
function poolExists(address token) external view returns (bool);
function getAllPools() external view returns (address[] memory);
function getPoolCount() external view returns (uint256);
// Pool info
function pools(uint256 index) external view returns (address);
function poolCount() external view returns (uint256);
// Factory info
function owner() external view returns (address);
function getEventAggregator() external view returns (address);
function setEventAggregator(address _eventAggregator) external;
/**
* @notice Get the registry address
* @return registryAddress Address of the GradientRegistry
*/
function getRegistry() external view returns (address);
/**
* @notice Check if a given address is a valid pool
* @param poolAddress Address to check
* @return isValid True if the address is a valid pool
*/
function isValidPool(
address poolAddress
) external view returns (bool isValid);
}
IGradientMarketMakerPoolV3.sol 605 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
/**
* @title IGradientMarketMakerPoolV3
* @notice Interface for individual token market maker pool contracts with price range functionality
* @dev Each pool is dedicated to one token with concentrated liquidity price ranges
* @dev Users can specify min/max price ranges when adding liquidity
*/
interface IGradientMarketMakerPoolV3 {
// Price range struct for concentrated liquidity
struct PriceRange {
uint256 minPrice; // Minimum price (in wei per token)
uint256 maxPrice; // Maximum price (in wei per token)
bool isActive; // Whether this range is active
}
// Enhanced provider position with price range
struct ProviderPosition {
uint256 position; // ETH or token position
uint256 rewardDebt; // Reward debt for accurate calculations
uint256 pendingRewards; // Pending rewards to claim
uint256 lastUpdateVersion; // Last version when position was updated
uint256 lpShares; // LP shares for this provider
PriceRange priceRange; // Price range for this position
}
// Pool metrics with price range support
struct PoolMetrics {
uint256 totalPosition; // Total ETH or tokens
uint256 totalLPShares; // Total LP shares
uint256 accRewardPerShare; // Accumulated rewards per share
uint256 rewardBalance; // Available reward balance
uint256 accountedPosition; // Accounted position for calculations
uint256 currentPrice; // Current market price
}
// Events
event LiquidityDeposited(
address indexed user,
address indexed token,
uint256 amount,
uint256 lpSharesMinted,
bool isETH,
uint256 minPrice,
uint256 maxPrice
);
event LiquidityWithdrawn(
address indexed user,
address indexed token,
uint256 amount,
uint256 lpSharesBurned,
bool isETH,
uint256 minPrice,
uint256 maxPrice
);
event PoolFeeDistributed(
address indexed from,
uint256 amount,
address indexed token,
bool isETH
);
event FeeClaimed(
address indexed user,
uint256 amount,
address indexed token,
bool isETH
);
event PoolBalanceUpdated(
address indexed token,
uint256 newTotalETH,
uint256 newTotalTokens,
uint256 newETHLPShares,
uint256 newTokenLPShares
);
event PriceRangeUpdated(
address indexed user,
uint256 oldMinPrice,
uint256 oldMaxPrice,
uint256 newMinPrice,
uint256 newMaxPrice,
bool isETH
);
event MerkleRootUpdated(uint256 indexed version, bytes32 merkleRoot);
event UserPositionUpdated(
address indexed user,
uint256 indexed version,
uint256 newETHPosition,
uint256 newTokenPosition
);
event MinLiquidityUpdated(uint256 newMinLiquidity, bool isETH);
event EmergencyWithdraw(
address indexed token,
address indexed recipient,
uint256 amount,
bool isETH
);
// Enhanced liquidity management functions with price ranges
/**
* @notice Add ETH liquidity with price range and optional position update
* @param minPrice Minimum price for this liquidity position
* @param maxPrice Maximum price for this liquidity position
* @param proof Merkle proof for position update (required if user has pending updates)
* @param newETHPosition New ETH position (required if proof provided)
* @param newTokenPosition New token position (required if proof provided)
* @param ethRewardsToAdd Off-chain calculated ETH rewards to add
* @param tokenRewardsToAdd Off-chain calculated token rewards to add
*/
function addETHLiquidityWithPriceRange(
uint256 minPrice,
uint256 maxPrice,
bytes32[] calldata proof,
uint256 newETHPosition,
uint256 newTokenPosition,
uint256 ethRewardsToAdd,
uint256 tokenRewardsToAdd
) external payable;
/**
* @notice Add token liquidity with price range and optional position update
* @param tokenAmount Amount of tokens to deposit
* @param minPrice Minimum price for this liquidity position
* @param maxPrice Maximum price for this liquidity position
* @param proof Merkle proof for position update (required if user has pending updates)
* @param newETHPosition New ETH position (required if proof provided)
* @param newTokenPosition New token position (required if proof provided)
* @param ethRewardsToAdd Off-chain calculated ETH rewards to add
* @param tokenRewardsToAdd Off-chain calculated token rewards to add
*/
function addTokenLiquidityWithPriceRange(
uint256 tokenAmount,
uint256 minPrice,
uint256 maxPrice,
bytes32[] calldata proof,
uint256 newETHPosition,
uint256 newTokenPosition,
uint256 ethRewardsToAdd,
uint256 tokenRewardsToAdd
) external;
/**
* @notice Add both ETH and token liquidity with price ranges and optional position update
* @param tokenAmount Amount of tokens to deposit
* @param ethMinPrice Minimum price for ETH liquidity position
* @param ethMaxPrice Maximum price for ETH liquidity position
* @param tokenMinPrice Minimum price for token liquidity position
* @param tokenMaxPrice Maximum price for token liquidity position
* @param proof Merkle proof for position update (required if user has pending updates)
* @param newETHPosition New ETH position (required if proof provided)
* @param newTokenPosition New token position (required if proof provided)
* @param ethRewardsToAdd Off-chain calculated ETH rewards to add
* @param tokenRewardsToAdd Off-chain calculated token rewards to add
*/
function addLiquidityWithPriceRanges(
uint256 tokenAmount,
uint256 ethMinPrice,
uint256 ethMaxPrice,
uint256 tokenMinPrice,
uint256 tokenMaxPrice,
bytes32[] calldata proof,
uint256 newETHPosition,
uint256 newTokenPosition,
uint256 ethRewardsToAdd,
uint256 tokenRewardsToAdd
) external payable;
/**
* @notice Update price range for existing ETH liquidity position
* @param newMinPrice New minimum price
* @param newMaxPrice New maximum price
* @param proof Merkle proof for position update (required if user has pending updates)
* @param newETHPosition New ETH position (required if proof provided)
* @param newTokenPosition New token position (required if proof provided)
* @param ethRewardsToAdd Off-chain calculated ETH rewards to add
* @param tokenRewardsToAdd Off-chain calculated token rewards to add
*/
function updateETHPositionPriceRange(
uint256 newMinPrice,
uint256 newMaxPrice,
bytes32[] calldata proof,
uint256 newETHPosition,
uint256 newTokenPosition,
uint256 ethRewardsToAdd,
uint256 tokenRewardsToAdd
) external;
/**
* @notice Update price range for existing token liquidity position
* @param newMinPrice New minimum price
* @param newMaxPrice New maximum price
* @param proof Merkle proof for position update (required if user has pending updates)
* @param newETHPosition New ETH position (required if proof provided)
* @param newTokenPosition New token position (required if proof provided)
* @param ethRewardsToAdd Off-chain calculated ETH rewards to add
* @param tokenRewardsToAdd Off-chain calculated token rewards to add
*/
function updateTokenPositionPriceRange(
uint256 newMinPrice,
uint256 newMaxPrice,
bytes32[] calldata proof,
uint256 newETHPosition,
uint256 newTokenPosition,
uint256 ethRewardsToAdd,
uint256 tokenRewardsToAdd
) external;
// Standard liquidity functions (without price ranges for backward compatibility)
/**
* @notice Add ETH liquidity with optional position update (no price range)
* @param proof Merkle proof for position update (required if user has pending updates)
* @param newETHPosition New ETH position (required if proof provided)
* @param newTokenPosition New token position (required if proof provided)
* @param ethRewardsToAdd Off-chain calculated ETH rewards to add
* @param tokenRewardsToAdd Off-chain calculated token rewards to add
*/
function addETHLiquidityWithProof(
bytes32[] calldata proof,
uint256 newETHPosition,
uint256 newTokenPosition,
uint256 ethRewardsToAdd,
uint256 tokenRewardsToAdd
) external payable;
/**
* @notice Add token liquidity with optional position update (no price range)
* @param tokenAmount Amount of tokens to deposit
* @param proof Merkle proof for position update (required if user has pending updates)
* @param newETHPosition New ETH position (required if proof provided)
* @param newTokenPosition New token position (required if proof provided)
* @param ethRewardsToAdd Off-chain calculated ETH rewards to add
* @param tokenRewardsToAdd Off-chain calculated token rewards to add
*/
function addTokenLiquidityWithProof(
uint256 tokenAmount,
bytes32[] calldata proof,
uint256 newETHPosition,
uint256 newTokenPosition,
uint256 ethRewardsToAdd,
uint256 tokenRewardsToAdd
) external;
// Removal functions with price range support
/**
* @notice Remove ETH liquidity with optional position update
* @param shares Percentage of pool to withdraw (in basis points, 10000 = 100%)
* @param minEthAmount Minimum amount of ETH to receive
* @param proof Merkle proof for position update (required if user has pending updates)
* @param newETHPosition New ETH position (required if proof provided)
* @param newTokenPosition New token position (required if proof provided)
* @param ethRewardsToAdd Off-chain calculated ETH rewards to add
* @param tokenRewardsToAdd Off-chain calculated token rewards to add
*/
function removeETHLiquidityWithProof(
uint256 shares,
uint256 minEthAmount,
bytes32[] calldata proof,
uint256 newETHPosition,
uint256 newTokenPosition,
uint256 ethRewardsToAdd,
uint256 tokenRewardsToAdd
) external;
/**
* @notice Remove token liquidity with optional position update
* @param shares Percentage of pool to withdraw (in basis points, 10000 = 100%)
* @param minTokenAmount Minimum amount of tokens to receive
* @param proof Merkle proof for position update (required if user has pending updates)
* @param newETHPosition New ETH position (required if proof provided)
* @param newTokenPosition New token position (required if proof provided)
* @param ethRewardsToAdd Off-chain calculated ETH rewards to add
* @param tokenRewardsToAdd Off-chain calculated token rewards to add
*/
function removeTokenLiquidityWithProof(
uint256 shares,
uint256 minTokenAmount,
bytes32[] calldata proof,
uint256 newETHPosition,
uint256 newTokenPosition,
uint256 ethRewardsToAdd,
uint256 tokenRewardsToAdd
) external;
/**
* @notice Remove both ETH and token liquidity with merkle proof
* @param ethShares Percentage of ETH liquidity to remove (0-10000)
* @param tokenShares Percentage of token liquidity to remove (0-10000)
* @param minEthAmount Minimum ETH amount to receive
* @param minTokenAmount Minimum token amount to receive
* @param proof Merkle proof for position update
* @param newETHPosition New ETH position after update
* @param newTokenPosition New token position after update
* @param ethRewardsToAdd Total ETH rewards (accumulated)
* @param tokenRewardsToAdd Total token rewards (accumulated)
*/
function removeLiquidityWithProof(
uint256 ethShares,
uint256 tokenShares,
uint256 minEthAmount,
uint256 minTokenAmount,
bytes32[] calldata proof,
uint256 newETHPosition,
uint256 newTokenPosition,
uint256 ethRewardsToAdd,
uint256 tokenRewardsToAdd
) external;
// Order execution functions (same as V2)
/**
* @notice Execute buy order - Orderbook sends ETH, receives tokens
* @param ethAmount Amount of ETH sent by orderbook
* @param tokenAmount Amount of tokens to send to orderbook
* @param newMerkleRoot New merkle root to update after trade
*/
function executeBuyOrder(
uint256 ethAmount,
uint256 tokenAmount,
bytes32 newMerkleRoot
) external payable;
/**
* @notice Execute sell order - Orderbook sends tokens, receives ETH
* @param ethAmount Amount of ETH to send to orderbook
* @param tokenAmount Amount of tokens sent by orderbook
* @param newMerkleRoot New merkle root to update after trade
*/
function executeSellOrder(
uint256 ethAmount,
uint256 tokenAmount,
bytes32 newMerkleRoot
) external;
// Reward distribution functions (same as V2)
/**
* @notice Distributes ETH fees to ETH providers
*/
function distributePoolFee() external payable;
/**
* @notice Distributes token fees to token providers
* @param tokenAmount Amount of tokens to distribute as fees
*/
function distributeTokenFee(uint256 tokenAmount) external;
// Reward claiming functions (same as V2)
/**
* @notice Claim all rewards with optional position update
* @param proof Merkle proof for position update (required if user has pending updates)
* @param newETHPosition New ETH position (required if proof provided)
* @param newTokenPosition New token position (required if proof provided)
* @param ethRewardsToAdd Off-chain calculated ETH rewards to add
* @param tokenRewardsToAdd Off-chain calculated token rewards to add
*/
function claimRewardsWithProof(
bytes32[] calldata proof,
uint256 newETHPosition,
uint256 newTokenPosition,
uint256 ethRewardsToAdd,
uint256 tokenRewardsToAdd
) external;
/**
* @notice Claim only ETH rewards with optional position update
* @param proof Merkle proof for position update (required if user has pending updates)
* @param newETHPosition New ETH position (required if proof provided)
* @param newTokenPosition New token position (required if proof provided)
* @param ethRewardsToAdd Off-chain calculated ETH rewards to add
* @param tokenRewardsToAdd Off-chain calculated token rewards to add
*/
function claimETHRewardsWithProof(
bytes32[] calldata proof,
uint256 newETHPosition,
uint256 newTokenPosition,
uint256 ethRewardsToAdd,
uint256 tokenRewardsToAdd
) external;
/**
* @notice Claim only token rewards with optional position update
* @param proof Merkle proof for position update (required if user has pending updates)
* @param newETHPosition New ETH position (required if proof provided)
* @param newTokenPosition New token position (required if proof provided)
* @param ethRewardsToAdd Off-chain calculated ETH rewards to add
* @param tokenRewardsToAdd Off-chain calculated token rewards to add
*/
function claimTokenRewardsWithProof(
bytes32[] calldata proof,
uint256 newETHPosition,
uint256 newTokenPosition,
uint256 ethRewardsToAdd,
uint256 tokenRewardsToAdd
) external;
// Position update functions (same as V2)
/**
* @notice Update user position using merkle proof
* @param version Version of the merkle root
* @param proof Merkle proof for the user's new position
* @param newETHPosition New ETH position (actual ETH amount)
* @param newTokenPosition New token position (actual token amount)
* @param ethRewardsToAdd Off-chain calculated ETH rewards to add
* @param tokenRewardsToAdd Off-chain calculated token rewards to add
*/
function updateUserPosition(
uint256 version,
bytes32[] calldata proof,
uint256 newETHPosition,
uint256 newTokenPosition,
uint256 ethRewardsToAdd,
uint256 tokenRewardsToAdd
) external;
// Factory-only functions (same as V2)
/**
* @notice Add ETH liquidity for a specific user (factory only)
* @param user User address to add liquidity for
*/
function addETHLiquidityForUser(
address user,
uint256 minPrice,
uint256 maxPrice
) external payable;
/**
* @notice Add token liquidity for a specific user (factory only)
* @param user User address to add liquidity for
* @param tokenAmount Amount of tokens to deposit
*/
function addTokenLiquidityForUser(
address user,
uint256 tokenAmount,
uint256 minPrice,
uint256 maxPrice
) external;
// View functions with price range support
/**
* @notice Get comprehensive user position information including price ranges
* @param user Address of the user
* @return ethPosition User's ETH position
* @return tokenPosition User's token position
* @return ethLPShares User's ETH LP shares
* @return tokenLPShares User's token LP shares
* @return ethPendingRewards User's pending ETH rewards
* @return tokenPendingRewards User's pending token rewards
* @return lastUpdateVersion User's last update version
* @return ethPriceRange User's ETH price range
* @return tokenPriceRange User's token price range
*/
function getUserPositionWithPriceRanges(
address user
)
external
view
returns (
uint256 ethPosition,
uint256 tokenPosition,
uint256 ethLPShares,
uint256 tokenLPShares,
uint256 ethPendingRewards,
uint256 tokenPendingRewards,
uint256 lastUpdateVersion,
PriceRange memory ethPriceRange,
PriceRange memory tokenPriceRange
);
/**
* @notice Get user position information (backward compatibility)
* @param user Address of the user
* @return ethPosition User's ETH position
* @return tokenPosition User's token position
* @return ethLPShares User's ETH LP shares
* @return tokenLPShares User's token LP shares
* @return ethPendingRewards User's pending ETH rewards
* @return tokenPendingRewards User's pending token rewards
* @return lastUpdateVersion User's last update version
*/
function getUserPosition(
address user
)
external
view
returns (
uint256 ethPosition,
uint256 tokenPosition,
uint256 ethLPShares,
uint256 tokenLPShares,
uint256 ethPendingRewards,
uint256 tokenPendingRewards,
uint256 lastUpdateVersion
);
/**
* @notice Get pool metrics for ETH pool
* @return metrics ETH pool metrics
*/
function getETHPoolMetrics()
external
view
returns (PoolMetrics memory metrics);
/**
* @notice Get pool metrics for token pool
* @return metrics Token pool metrics
*/
function getTokenPoolMetrics()
external
view
returns (PoolMetrics memory metrics);
/**
* @notice Get current market price (ETH per token)
* @return price Current market price in wei per token
*/
function getCurrentPrice() external view returns (uint256 price);
/**
* @notice Check if a price is within a given range
* @param price Price to check
* @param minPrice Minimum price
* @param maxPrice Maximum price
* @return isWithinRange True if price is within range
*/
function isPriceWithinRange(
uint256 price,
uint256 minPrice,
uint256 maxPrice
) external view returns (bool isWithinRange);
/**
* @notice Get the Uniswap V2 pair address for this token
* @return pairAddress Address of the Uniswap V2 pair
*/
function getPairAddress() external view returns (address pairAddress);
/**
* @notice Get the reserves for this token pair
* @return reserveETH ETH reserve amount
* @return reserveToken Token reserve amount
*/
function getReserves()
external
view
returns (uint256 reserveETH, uint256 reserveToken);
// Owner functions (same as V2)
/**
* @notice Set minimum ETH liquidity requirement
* @param _minLiquidity New minimum ETH liquidity amount
*/
function setMinLiquidity(uint256 _minLiquidity) external;
/**
* @notice Set minimum token liquidity requirement
* @param _minTokenLiquidity New minimum token liquidity amount
*/
function setMinTokenLiquidity(uint256 _minTokenLiquidity) external;
// Emergency functions (same as V2)
/**
* @notice Emergency function to withdraw ETH from the contract
* @param recipient Address to receive the ETH
* @param amount Amount of ETH to withdraw (0 = withdraw all)
*/
function emergencyWithdrawETH(
address payable recipient,
uint256 amount
) external;
/**
* @notice Emergency function to withdraw any ERC20 token from the contract
* @param tokenAddress Address of the token to withdraw
* @param recipient Address to receive the tokens
* @param amount Amount of tokens to withdraw (0 = withdraw all)
*/
function emergencyWithdrawToken(
address tokenAddress,
address recipient,
uint256 amount
) external;
// Basic view functions (same as V2)
function token() external view returns (address);
function factory() external view returns (address);
function totalETH() external view returns (uint256);
function totalTokens() external view returns (uint256);
function merkleRoot() external view returns (bytes32);
function currentVersion() external view returns (uint256);
function minLiquidity() external view returns (uint256);
function minTokenLiquidity() external view returns (uint256);
}
IGradientRegistry.sol 138 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
/**
* @title IGradientRegistry
* @notice Interface for the GradientRegistry contract
*/
interface IGradientRegistry {
/**
* @notice Set all main contract addresses at once
* @param _marketMakerFactory Address of the MarketMakerFactory contract
* @param _gradientToken Address of the Gradient token contract
* @param _orderbook Address of the Orderbook contract
* @param _fallbackExecutor Address of the FallbackExecutor contract
* @param _router Address of the Uniswap V2 Router contract
* @param _feeManager Address of the FeeManager contract
*/
function setMainContracts(
address _marketMakerFactory,
address _gradientToken,
address _orderbook,
address _fallbackExecutor,
address _router,
address _feeManager
) external;
/**
* @notice Set an individual main contract address
* @param contractName Name of the contract to update
* @param newAddress New address for the contract
*/
function setContractAddress(
string calldata contractName,
address newAddress
) external;
/**
* @notice Set an additional contract address using a key
* @param key The key to identify the contract
* @param contractAddress The address of the contract
*/
function setAdditionalContract(
bytes32 key,
address contractAddress
) external;
/**
* @notice Set the block status of a token
* @param token The address of the token to set the block status of
* @param blocked Whether the token should be blocked
*/
function setTokenBlockStatus(address token, bool blocked) external;
/**
* @notice Set a reward distributor address
* @param rewardDistributor The address of the reward distributor to authorize
*/
function setRewardDistributor(address rewardDistributor) external;
/**
* @notice Authorize or deauthorize a fulfiller
* @param fulfiller The address of the fulfiller to authorize
* @param status The status of the fulfiller
*/
function authorizeFulfiller(address fulfiller, bool status) external;
/**
* @notice Check if an address is an authorized fulfiller
* @param fulfiller The address to check
* @return bool Whether the address is an authorized fulfiller
*/
function isAuthorizedFulfiller(
address fulfiller
) external view returns (bool);
/**
* @notice Get all main contract addresses
* @return _marketMakerFactory Address of the MarketMakerFactory contract
* @return _gradientToken Address of the Gradient token contract
* @return _orderbook Address of the Orderbook contract
* @return _fallbackExecutor Address of the FallbackExecutor contract
* @return _router Address of the Uniswap V2 Router contract
* @return _feeManager Address of the FeeManager contract
*/
function getAllMainContracts()
external
view
returns (
address _marketMakerFactory,
address _gradientToken,
address _orderbook,
address _fallbackExecutor,
address _router,
address _feeManager
);
// View functions for individual contract addresses
function marketMakerFactory() external view returns (address);
// Legacy function for backward compatibility (returns factory address)
function marketMakerPool() external view returns (address);
// New function to get pool for specific token
function getMarketMakerPool(address token) external view returns (address);
// New function to check if pool exists for token
function poolExists(address token) external view returns (bool);
function gradientToken() external view returns (address);
function orderbook() external view returns (address);
function fallbackExecutor() external view returns (address);
function router() external view returns (address);
function feeManager() external view returns (address);
// View functions for mappings
function blockedTokens(address token) external view returns (bool);
function isRewardDistributor(
address rewardDistributor
) external view returns (bool);
function authorizedFulfillers(
address fulfiller
) external view returns (bool);
// Partner token management functions (for market maker fee splits)
function setPartnerToken(address token, address partnerWallet) external;
function removePartnerToken(address token) external;
function checkIsPartnerToken(address token) external view returns (bool);
function getPartnerWallet(address token) external view returns (address);
}
IUniswapV2Factory.sol 33 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface IUniswapV2Factory {
event PairCreated(
address indexed token0,
address indexed token1,
address pair,
uint
);
function feeTo() external view returns (address);
function feeToSetter() external view returns (address);
function getPair(
address tokenA,
address tokenB
) external view returns (address pair);
function allPairs(uint) external view returns (address pair);
function allPairsLength() external view returns (uint);
function createPair(
address tokenA,
address tokenB
) external returns (address pair);
function setFeeTo(address) external;
function setFeeToSetter(address) external;
}
IUniswapV2Pair.sol 101 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface IUniswapV2Pair {
event Approval(address indexed owner, address indexed spender, uint value);
event Transfer(address indexed from, address indexed to, uint value);
function name() external pure returns (string memory);
function symbol() external pure returns (string memory);
function decimals() external pure returns (uint8);
function totalSupply() external view returns (uint);
function balanceOf(address owner) external view returns (uint);
function allowance(
address owner,
address spender
) external view returns (uint);
function approve(address spender, uint value) external returns (bool);
function transfer(address to, uint value) external returns (bool);
function transferFrom(
address from,
address to,
uint value
) external returns (bool);
function DOMAIN_SEPARATOR() external view returns (bytes32);
function PERMIT_TYPEHASH() external pure returns (bytes32);
function nonces(address owner) external view returns (uint);
function permit(
address owner,
address spender,
uint value,
uint deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
event Mint(address indexed sender, uint amount0, uint amount1);
event Burn(
address indexed sender,
uint amount0,
uint amount1,
address indexed to
);
event Swap(
address indexed sender,
uint amount0In,
uint amount1In,
uint amount0Out,
uint amount1Out,
address indexed to
);
event Sync(uint112 reserve0, uint112 reserve1);
function MINIMUM_LIQUIDITY() external pure returns (uint);
function factory() external view returns (address);
function token0() external view returns (address);
function token1() external view returns (address);
function getReserves()
external
view
returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
function price0CumulativeLast() external view returns (uint);
function price1CumulativeLast() external view returns (uint);
function kLast() external view returns (uint);
function mint(address to) external returns (uint liquidity);
function burn(address to) external returns (uint amount0, uint amount1);
function swap(
uint amount0Out,
uint amount1Out,
address to,
bytes calldata data
) external;
function skim(address to) external;
function sync() external;
function initialize(address, address) external;
}
IUniswapV2Router.sol 198 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface IUniswapV2Router01 {
function factory() external pure returns (address);
function WETH() external pure returns (address);
function addLiquidity(
address tokenA,
address tokenB,
uint amountADesired,
uint amountBDesired,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) external returns (uint amountA, uint amountB, uint liquidity);
function addLiquidityETH(
address token,
uint amountTokenDesired,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
)
external
payable
returns (uint amountToken, uint amountETH, uint liquidity);
function removeLiquidity(
address tokenA,
address tokenB,
uint liquidity,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) external returns (uint amountA, uint amountB);
function removeLiquidityETH(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external returns (uint amountToken, uint amountETH);
function removeLiquidityWithPermit(
address tokenA,
address tokenB,
uint liquidity,
uint amountAMin,
uint amountBMin,
address to,
uint deadline,
bool approveMax,
uint8 v,
bytes32 r,
bytes32 s
) external returns (uint amountA, uint amountB);
function removeLiquidityETHWithPermit(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline,
bool approveMax,
uint8 v,
bytes32 r,
bytes32 s
) external returns (uint amountToken, uint amountETH);
function swapExactTokensForTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
function swapTokensForExactTokens(
uint amountOut,
uint amountInMax,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
function swapExactETHForTokens(
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external payable returns (uint[] memory amounts);
function swapTokensForExactETH(
uint amountOut,
uint amountInMax,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
function swapExactTokensForETH(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
function swapETHForExactTokens(
uint amountOut,
address[] calldata path,
address to,
uint deadline
) external payable returns (uint[] memory amounts);
function quote(
uint amountA,
uint reserveA,
uint reserveB
) external pure returns (uint amountB);
function getAmountOut(
uint amountIn,
uint reserveIn,
uint reserveOut
) external pure returns (uint amountOut);
function getAmountIn(
uint amountOut,
uint reserveIn,
uint reserveOut
) external pure returns (uint amountIn);
function getAmountsOut(
uint amountIn,
address[] calldata path
) external view returns (uint[] memory amounts);
function getAmountsIn(
uint amountOut,
address[] calldata path
) external view returns (uint[] memory amounts);
}
interface IUniswapV2Router02 is IUniswapV2Router01 {
function removeLiquidityETHSupportingFeeOnTransferTokens(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external returns (uint amountETH);
function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline,
bool approveMax,
uint8 v,
bytes32 r,
bytes32 s
) external returns (uint amountETH);
function swapExactTokensForTokensSupportingFeeOnTransferTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external;
function swapExactETHForTokensSupportingFeeOnTransferTokens(
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external payable;
function swapExactTokensForETHSupportingFeeOnTransferTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external;
}
IUniswapV3Factory.sol 10 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface IUniswapV3Factory {
function getPool(
address tokenA,
address tokenB,
uint24 fee
) external view returns (address pool);
}
IUniswapV3Pool.sol 25 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface IUniswapV3Pool {
function token0() external view returns (address);
function token1() external view returns (address);
function fee() external view returns (uint24);
function slot0()
external
view
returns (
uint160 sqrtPriceX96,
int24 tick,
uint16 observationIndex,
uint16 observationCardinality,
uint16 observationCardinalityNext,
uint8 feeProtocol,
bool unlocked
);
function liquidity() external view returns (uint128);
}
IUniswapV3PriceHelper.sol 60 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
/**
* @title IUniswapV3PriceHelper
* @notice Interface for Uniswap V3 price helper contract
* @dev This helper reduces pool contract size by handling all V3-specific logic
*/
interface IUniswapV3PriceHelper {
/**
* @notice Get V3 pool address for a token by trying different fee tiers
* @param token Address of the token
* @param weth WETH address
* @return poolAddress Address of the V3 pool, or address(0) if not found
*/
function getV3PoolAddress(
address token,
address weth
) external view returns (address poolAddress);
/**
* @notice Get price from Uniswap V3 pool
* @param token Address of the token
* @param weth WETH address
* @param tokenDecimals Number of decimals for the token
* @return price Price in ETH per token (18 decimals), or 0 if pool doesn't exist
*/
function getPriceFromV3(
address token,
address weth,
uint8 tokenDecimals
) external view returns (uint256 price);
/**
* @notice Get current market price from Uniswap (checks V3 first, then V2)
* @param token Address of the token
* @param routerAddress Address of the Uniswap V2 Router (to get WETH and V2 factory)
* @param tokenDecimals Number of decimals for the token
* @return price Current market price in wei per token
*/
function getCurrentPrice(
address token,
address routerAddress,
uint8 tokenDecimals
) external view returns (uint256 price);
/**
* @notice Get amount out for a swap in Uniswap V3 (similar to V2's getAmountOut)
* @param amountIn Amount of input token
* @param tokenIn Address of the input token
* @param tokenOut Address of the output token
* @return amountOut Amount of output token that would be received
* @dev Returns 0 if pool doesn't exist or calculation fails
*/
function getAmountOut(
uint256 amountIn,
address tokenIn,
address tokenOut
) external view returns (uint256 amountOut);
}
Read Contract
DIVISOR 0x3410fe6e → uint256
MAX_FEE_PERCENTAGE 0x558e44d3 → uint256
MAX_TOKEN_SPECIFIC_FEE_PERCENTAGE 0x3aad938d → uint256
MIN_FEE_PERCENTAGE 0x679230f2 → uint256
defaultFeePercentage 0xb4824034 → uint256
dustTolerance 0x1e999814 → uint256
feeManager 0xd0fb0203 → address
getActiveOrders 0x1f32eb77 → uint256[]
getActiveOrdersCount 0x10e1c166 → uint256
getCurrentFeePercentage 0x4d067345 → uint256
getCurrentMarketPrice 0x04ca1339 → uint256
getOrder 0xd09ef241 → tuple
getRemainingAmount 0xf6252ff2 → uint256
getReserves 0x3e99c1e4 → uint256, uint256
gradientRegistry 0x830562bc → address
headOrder 0xcb25c17d → uint256
isOrderExpired 0x59c69313 → bool
linkedOrders 0x7af18788 → uint256, uint256, bool
maxOrderSize 0xf1a82c7e → uint256
maxOrderTtl 0x66961d44 → uint256
maxPriceDeviation 0x975b8662 → uint256
minOrderSize 0xa674537f → uint256
orders 0xa85c38ef → uint256, address, uint8, uint8, address, uint256, uint256, uint256, uint256, uint256, uint256, uint8
owner 0x8da5cb5b → address
tailOrder 0xfd31f89a → uint256
tokenSpecificFeePercentage 0xa1a65f9c → uint256
totalOrderCount 0x0d34219c → uint256
uniswapV3Factory 0x5b549182 → address
uniswapV3PriceHelper 0x67bf6ba6 → address
v3FeeTiers 0x8dfb8ccd → uint24
validateExecutionPrice 0x86ea511e → bool
Write Contract 24 functions
These functions modify contract state and require a wallet transaction to execute.
cancelOrder 0x514fcac7
uint256 orderId
cleanupExpiredOrders 0xea7bcef4
uint256[] orderIds
createOrder 0xfab960ed
uint8 orderType
uint8 executionType
address token
uint256 amount
uint256 price
uint256 ttl
string objectId
returns: uint256
emergencyWithdrawETH 0xd79e8567
address recipient
uint256 amount
emergencyWithdrawMultipleTokens 0x87d11e61
address[] tokens
address recipient
emergencyWithdrawToken 0x277327a5
address token
address recipient
uint256 amount
fulfillLimitOrders 0x9ac53cd1
tuple[] matches
fulfillMarketOrders 0xeca5d257
tuple[] matches
uint256[] executionPrices
fulfillOrdersWithMarketMaker 0xa8b37cfe
uint256[] orderIds
uint256[] fillAmounts
uint256[] executionPrices
bytes32 merkleRoot
fulfillOwnOrderWithAMM 0x9c302fdb
uint256 orderId
uint256 fillAmount
uint256 minAmountOut
removeTokenSpecificFeePercentage 0x70cec1ac
address token
renounceOwnership 0x715018a6
No parameters
setDefaultFeePercentage 0x9f92b715
uint256 newFeePercentage
setFeeManager 0x472d35b9
address _feeManager
setGradientRegistry 0x634eef47
address _gradientRegistry
setMaxOrderTtl 0x727d2114
uint256 _maxOrderTtl
setOrderSizeLimits 0x13b64e24
uint256 _minOrderSize
uint256 _maxOrderSize
setTokenSpecificFeePercentage 0x6859ebf0
address token
uint256 newFeePercentage
setUniswapV3Factory 0xe2cfcfee
address _uniswapV3Factory
setUniswapV3PriceHelper 0xb7bac4cf
address _uniswapV3PriceHelper
setV3FeeTiers 0xd23a0bd0
uint24[] _feeTiers
transferOwnership 0xf2fde38b
address newOwner
updateDustTolerance 0x81bbc0d3
uint256 newDustTolerance
updateMaxPriceDeviation 0x24f1c5c1
uint256 newDeviation
Recent Transactions
No transactions found for this address