Address Contract Verified
Address
0x3b29c19ff2fcEa0Ff98D0ef5B184354D74eA74b0
Balance
0 ETH
Nonce
2
Code Size
24571 bytes
Creator
0xa51949Be...3A51 at tx 0xf76853e8...d9269e
Indexed Transactions
0
Contract Bytecode
24571 bytes
0x6080604052600436106102285760003560e01c806382e0e3161161012a57806382e0e316146105dd57806384861e93146105fd5780638c0d0c291461066c5780638c1ad610146106815780638da5cb5b1461069757806394bfdb6e146106ac578063ac9650d8146106cc578063b2b88d45146106ec578063b58481761461070c578063b91c9f4014610721578063babcc53914610755578063cf26856414610795578063d0b06f5d146107b5578063d4b83992146107d9578063d8899850146107f7578063dcc48cd414610817578063e30c397814610837578063eef44eb11461084c578063f24d1ca31461086c578063f2fde38b146108a0578063f384bd05146108c0578063fa461e33146108f4578063fb30088414610914578063fcd4ce3314610a1557600080fd5b80630a184d841461022d578063115042d31461028457806313217f90146102b2578063150b7a02146102c757806316f0115b146103005780631709a61b1461032057806320d0dc3e146103545780632a5aa292146103745780633328cfda1461039657806333465c5f146104015780634437152a146104215780635474b451146104415780635656112a14610487578063578bcc20146104b45780636f05e1ae146104d45780636f307dc3146104f4578063713fc5ad14610528578063715018a61461054857806374d7c62b1461055d5780637588141f1461057357806376f6c33f1461059357806379ba5097146105a857806380790424146105bd575b600080fd5b34801561023957600080fd5b5061026e610248366004614c38565b60096020908152600092835260408084209091529082529020546001600160a01b031681565b60405161027b9190614c64565b60405180910390f35b34801561029057600080fd5b506102a461029f366004614c78565b610a35565b60405190815260200161027b565b3480156102be57600080fd5b506102a4610a7f565b3480156102d357600080fd5b506102e76102e2366004614cd9565b610ad6565b6040516001600160e01b0319909116815260200161027b565b34801561030c57600080fd5b5060015461026e906001600160a01b031681565b34801561032c57600080fd5b5061026e7f000000000000000000000000aeb1d03929bf87f69888f381e73fbf75753d75af81565b34801561036057600080fd5b506102a461036f366004614d63565b610b6d565b34801561038057600080fd5b5061039461038f366004614c78565b610bd3565b005b3480156103a257600080fd5b506103dc6103b1366004614d7f565b600a6020526000908152604090205464ffffffffff811690600160281b90046001600160d81b031682565b6040805164ffffffffff90931683526001600160d81b0390911660208301520161027b565b34801561040d57600080fd5b506102a461041c366004614c78565b610c1b565b34801561042d57600080fd5b5061039461043c366004614d7f565b610c36565b34801561044d57600080fd5b506103dc61045c366004614d7f565b60056020526000908152604090205464ffffffffff811690600160281b90046001600160d81b031682565b34801561049357600080fd5b506104a76104a2366004614dae565b610c7e565b60405161027b9190614e19565b3480156104c057600080fd5b506103946104cf366004614e70565b611228565b3480156104e057600080fd5b506103946104ef366004614ef5565b61127e565b34801561050057600080fd5b5061026e7f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc281565b34801561053457600080fd5b506102a461054336600461519a565b61135f565b34801561055457600080fd5b50610394611632565b34801561056957600080fd5b506102a460005481565b34801561057f57600080fd5b506102a461058e366004615212565b611646565b34801561059f57600080fd5b506102a4611677565b3480156105b457600080fd5b50610394611787565b3480156105c957600080fd5b506103946105d8366004614d7f565b61180a565b3480156105e957600080fd5b506103946105f8366004614d7f565b611856565b34801561060957600080fd5b50610645610618366004614c78565b6008602052600090815260409020546001600160601b03811690600160601b90046001600160a01b031682565b604080516001600160601b0390931683526001600160a01b0390911660208301520161027b565b34801561067857600080fd5b506102a4611900565b34801561068d57600080fd5b506102a460065481565b3480156106a357600080fd5b5061026e61194e565b3480156106b857600080fd5b506103946106c73660046152f1565b61195d565b6106df6106da366004614ef5565b611a54565b60405161027b91906153b2565b3480156106f857600080fd5b506102a4610707366004615426565b611ad0565b34801561071857600080fd5b506102a4611c15565b34801561072d57600080fd5b5061026e7f000000000000000000000000320aaab3038bc08317f5a4be19ea1d9608551d7981565b34801561076157600080fd5b50610785610770366004614d7f565b600b6020526000908152604090205460ff1681565b604051901515815260200161027b565b3480156107a157600080fd5b506103946107b036600461546f565b611c2d565b3480156107c157600080fd5b50600254600160b81b900465ffffffffffff166102a4565b3480156107e557600080fd5b506002546001600160801b03166102a4565b34801561080357600080fd5b506103946108123660046154a4565b611d18565b34801561082357600080fd5b506102a4610832366004615505565b611f74565b34801561084357600080fd5b5061026e6120a5565b34801561085857600080fd5b50610394610867366004615562565b6120b4565b34801561087857600080fd5b506102a47f000000000000000000000000000000000000000000000000000000000002a30081565b3480156108ac57600080fd5b506103946108bb366004614d7f565b6120c9565b3480156108cc57600080fd5b506102a47f00000000000000000000000000000000000000000000000006f05b59d3b2000081565b34801561090057600080fd5b5061039461090f3660046155b5565b61212f565b34801561092057600080fd5b506109c961092f366004615607565b604080516080810182526000808252602082018190529181018290526060810191909152506001600160a01b039182166000908152600d602090815260408083209390941682529182528290208251608081018452905461ffff80821683526201000082041692820192909252600160201b820464ffffffffff1692810192909252600160481b90046001600160b81b0316606082015290565b60405161027b9190815161ffff90811682526020808401519091169082015260408083015164ffffffffff16908201526060918201516001600160b81b03169181019190915260800190565b348015610a2157600080fd5b50610394610a30366004614d7f565b612261565b60025460009042600160b81b90910465ffffffffffff1603610a6e57600254610a689083906001600160801b0316613014565b92915050565b610a6882610a7a610a7f565b613014565b60025460009042600160b81b90910465ffffffffffff1603610aab57506002546001600160801b031690565b6000610ab561304d565b600254909250610ad0915082906001600160801b03166130a2565b91505090565b600080610ae583850185615640565b6040805180820190915233815260208101879052909150610b0687826130ba565b60408201516020015115610b3257610b2d87836000015133856040015186606001516131c1565b610b59565b602082015115610b5957610b598782600001518460000151856020015186606001516132b1565b50630a85bd0160e11b979650505050505050565b600080610b8261058e36859003850185615212565b90506000610b8f82610c1b565b905080600003610bb25760405163010ab10b60e51b815260040160405180910390fd5b610bcb8282610bc636889003880188615212565b6134f8565b949350505050565b610bdb613543565b610be4816135a2565b6040518181527fdde96abba1706431e0b3c4faf7eca6e86409897f1ed29de345a816031a8417db906020015b60405180910390a150565b6000908152600860205260409020546001600160601b031690565b610c3e613543565b610c4781613620565b6040516001600160a01b038216907f4349687687e69ab0479266619127f66a1d429752cd8accfef157633fa10875d290600090a250565b610cd96040518060e0016040528060006001600160a01b031681526020016000815260200160006001600160a01b0316815260200160008152602001600081526020016000815260200160006001600160a01b031681525090565b6000610ce3611677565b6001600160a01b0386166000908152600d6020908152604082209293509091908290610d1190880188614d7f565b6001600160a01b03166001600160a01b031681526020019081526020016000209050856001600160a01b031660096000876000016020810190610d549190614d7f565b6001600160a01b039081168252602080830193909352604091820160009081208a85013582529093529120541614610d9f5760405163a7a74c7b60e01b815260040160405180910390fd5b6000610dc3610db16020880188614d7f565b6001610dbc8861573c565b600061135f565b8254909150610de090610dda9061ffff168361575e565b84613014565b8254600160481b90046001600160b81b031611610e1057604051636ef5bcdd60e11b815260040160405180910390fd5b81547f000000000000000000000000000000000000000000000000000000000002a30090610e4c90600160201b900464ffffffffff1642615775565b1015610e6b5760405163186f265d60e21b815260040160405180910390fd5b815464ffffffffff4216600160201b0264ffffffffff60201b198216811784558391600091610ea29161ffff918216911617615788565b91906101000a81548161ffff021916908361ffff16021790555081600001600281819054906101000a900461ffff16610eda906157a6565b91906101000a81548161ffff021916908361ffff1602179055508560200135866000016020810190610f0c9190614d7f565b6001600160a01b0316886001600160a01b03167f367be65505e5aff90e7e646744b5f75280ec6d79a0e78690f6020b875a03bc1260405160405180910390a460096000610f5c6020890189614d7f565b6001600160a01b0316815260208082019290925260409081016000908120898401358252909252812080546001600160a01b031916905583670de0b6b3a7640000610fc77f00000000000000000000000000000000000000000000000000000000000000038561575e565b610fd1919061575e565b610fdb91906157dd565b90506000600a81610fef60208b018b614d7f565b6001600160a01b03168152602080820192909252604090810160002081518083019092525464ffffffffff81168252600160281b90046001600160d81b031691810182905291501580159061105057508181602001516001600160d81b0316115b156110db57805160009061106b9064ffffffffff1642615775565b90506201518081111561107e5750620151805b60006110ca83602001516001600160d81b031683620151806706f05b59d3b200006110a991906157dd565b6110b3919061575e565b6110c590670de0b6b3a7640000615775565b6136d1565b9050808410156110d8578093505b50505b60405180604001604052804264ffffffffff168152602001836001600160d81b0316815250600a60008a60000160208101906111179190614d7f565b6001600160a01b03908116825260208083019390935260409182016000208451948401516001600160d81b0316600160281b0264ffffffffff90951694909417909355805160e081018252928c1683528a820180359284019290925261121b929190820190611186908c614d7f565b6001600160a01b031681526020017f00000000000000000000000000000000000000000000000009b6e64a8ec6000081526020017f000000000000000000000000000000000000000000000000000000000001518081526020018481526020017f000000000000000000000000320aaab3038bc08317f5a4be19ea1d9608551d796001600160a01b03168152509650866136e6565b5050505050509392505050565b6001600160a01b038381166000908152600d6020908152604080832093861683529290522054600160481b90046001600160b81b03166112788484338480871161127257866137fd565b856137fd565b50505050565b611286613543565b60005b8181101561135a576000600b60008585858181106112a9576112a96157f1565b90506020020160208101906112be9190614d7f565b6001600160a01b031681526020810191909152604001600020805460ff19169115159190911790558282828181106112f8576112f86157f1565b905060200201602081019061130d9190614d7f565b6001600160a01b03167f4196a45b9de3a3d829f8037f12d13f69efcc4dd50e3e25ea1bc556d3b50cbfeb600060405161134a911515815260200190565b60405180910390a2600101611289565b505050565b81518051602080830151805190820120604093840151935160009485946001946113c7947f395537e76dcb230984fc10f328a95df716a3205146e2e10a6b1405dc0a670593949293909290910193845260208401929092526040830152606082015260800190565b60408051601f198184030181529082905280516020918201207b0ca2ba3432b932bab69029b4b3b732b21026b2b9b9b0b3b29d05199960211b91830191909152603c820152605c0160408051808303601f1901815282825280516020918201208882015180518184015191850151600087529386018086529290925260ff909116928401929092526060830191909152608082015260a0016020604051602081039080840390855afa158015611481573d6000803e3d6000fd5b5050506020604051035190507f000000000000000000000000aeb1d03929bf87f69888f381e73fbf75753d75af6001600160a01b0316816001600160a01b0316146114df57604051630e2899d360e41b815260040160405180910390fd5b60007f52f9f8258fb6ca6994c194731e388edeba2a2fd3efc2636c57beeb070fdf8a328662093a808960405160200161151b9493929190615807565b60408051601f198184030181529190528051602090910120855151909150811461155857604051638671e7a360e01b815260040160405180910390fd5b84516040015142108061157d5750845160400151429061157b906104b090615850565b105b1561159b576040516356339a3f60e11b815260040160405180910390fd5b6000808660000151602001518060200190518101906115ba9190615863565b915091507f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc26001600160a01b0316826001600160a01b0316146116105760405163b192d61160e01b815260040160405180910390fd5b8561161b5780611625565b6116258982613885565b9998505050505050505050565b61163a613543565b61164460006139c6565b565b6000816040516020016116599190614e19565b60408051601f19818403018152919052805160209091012092915050565b60025460009042600160b81b90910465ffffffffffff16036116a357506002546001600160801b031690565b6000806116ae61304d565b60025491935091506116ca9082906001600160801b03166130a2565b92506116d583612a26565b600280546001600160801b039290921660016cffffffffffff0000000000000160801b031990921691909117600160b81b4265ffffffffffff16021760016cffffffffffff0000000000000160801b0316600160801b66ffffffffffffff8516026001600160e81b031617600160e81b62ffffff8416021790556040518381527fe71dc78e32c761439c1a81c1a2955431ba841ff2619db4d158f7356d656e65ae9060200160405180910390a1505090565b33806117916120a5565b6001600160a01b0316146117fe5760405162461bcd60e51b815260206004820152602960248201527f4f776e61626c6532537465703a2063616c6c6572206973206e6f7420746865206044820152683732bb9037bbb732b960b91b60648201526084015b60405180910390fd5b611807816139c6565b50565b611812613543565b6001600160a01b0381166000818152600c6020526040808220829055517f28e9477c5e3da52a98610cb094e45e9a1ba1969c9206e11b9360f182ab894f8c9190a250565b6001600160a01b0381166000908152600c6020526040812054908190036118905760405163b9ec721160e01b815260040160405180910390fd5b426118bb7f000000000000000000000000000000000000000000000000000000000006978083615850565b11156118da57604051631ddf5ac960e01b815260040160405180910390fd5b6001600160a01b0382166000908152600c60205260408120556118fc826139df565b5050565b60025460009042600160b81b90910465ffffffffffff1603611938576002805461193391600160e81b909104900b613a3c565b905090565b600061194261304d565b915050610ad081613a3c565b6003546001600160a01b031690565b6000611967611677565b905060008060005b85811015611a4a57806000036119c657868682818110611991576119916157f1565b6119a79260206040909202019081019150614d7f565b91506119bf8260026119b88861573c565b600161135f565b9250611a1f565b8686828181106119d8576119d86157f1565b6119ee9260206040909202019081019150614d7f565b6001600160a01b0316826001600160a01b031614611a1f576040516312397d9960e01b815260040160405180910390fd5b611a4288888884818110611a3557611a356157f1565b9050604002018587613a92565b60010161196f565b5050505050505050565b60608115610a685750604051818152602001600582901b808483378101805b825185018035602082018337600080823584305af4611a96573d6000803e3d6000fd5b508083526020830192503d81523d6000602083013e3d01603f0167ffffffffffffffe016818310611a735760408051919052949350505050565b600154600090608083013515159082908190611b52906001600160a01b0316337f0000000000000000000000000000000000000000000000000000000000000000883560208a0135611b2860608c0160408d01614d7f565b8b60a0013533604051602001611b3e9190614c64565b604051602081830303815290604052613d29565b915091508215611bbd57611bbd33611b706080880160608901614d7f565b612710611b8160808a01358661575e565b611b8b91906157dd565b6001600160a01b037f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc216929190613e8a565b6001600160a01b038781166000908152600d60209081526040808320938a1683529290522054600160481b90046001600160b81b0316611c078888338480881161127257876137fd565b5090925050505b9392505050565b611c2a620151806706f05b59d3b200006157dd565b81565b60005b8181101561135a57611c6933848484818110611c4e57611c4e6157f1565b905060400201803603810190611c649190615891565b6130ba565b828282818110611c7b57611c7b6157f1565b611c919260206040909202019081019150614d7f565b6001600160a01b03166323b872dd3330868686818110611cb357611cb36157f1565b905060400201602001356040518463ffffffff1660e01b8152600401611cdb939291906158e8565b600060405180830381600087803b158015611cf557600080fd5b505af1158015611d09573d6000803e3d6000fd5b50505050806001019050611c30565b60008080611d33611d2e36899003890189615212565b613f18565b9194509250905085811115611d6557604051630f11a33160e41b815260048101829052602481018790526044016117f5565b600083815260086020526040812055600d6000611d8560208a018a614d7f565b6001600160a01b03166001600160a01b031681526020019081526020016000206000886040016020810190611dba9190614d7f565b6001600160a01b03168152602081019190915260400160002054600160201b900464ffffffffff168203611e6e576000600d81611dfa60208b018b614d7f565b6001600160a01b03166001600160a01b031681526020019081526020016000206000896040016020810190611e2f9190614d7f565b6001600160a01b031681526020810191909152604001600020805464ffffffffff92909216600160201b0264ffffffffff60201b199092169190911790555b611e95333083611e8460e08c0160c08d01614d7f565b6001600160a01b0316929190613e8a565b611ebc81611ea660208a018a614d7f565b611eb660608b0160408c01614d7f565b87613f67565b611ecc6060880160408901614d7f565b6001600160a01b03166342842e0e30878a602001356040518463ffffffff1660e01b8152600401611eff939291906158e8565b600060405180830381600087803b158015611f1957600080fd5b505af1158015611f2d573d6000803e3d6000fd5b50505050827fc87036081503cc1fd53dc456ee0c40aef140882f77b06b4b4b554fee2b60816a82604051611f6391815260200190565b60405180910390a250505050505050565b6001546000906080840135151590611fef906001600160a01b031682611f9a5787611f9c565b305b7f00000000000000000000000000000000000000000000000000000000000000001587356020890135611fd560608b0160408c01614d7f565b8a60a00135338d8c604051602001611b3e9392919061599f565b509150801561209c57600061271061200b60808701358561575e565b61201591906157dd565b905061205b61202a6080870160608801614d7f565b6001600160a01b037f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2169083614111565b61209a876120698386615775565b6001600160a01b037f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2169190614111565b505b50949350505050565b6004546001600160a01b031690565b611278338486856120c48661573c565b6132b1565b6120d1613543565b600480546001600160a01b0319166001600160a01b0383169081179091556120f761194e565b6001600160a01b03167f38d16b8cac22d99fc7c124b9cd0de2d3fa1faef420bfe791d8c362d765e2270060405160405180910390a350565b6001546001600160a01b031633146121785760405162461bcd60e51b815260206004820152600c60248201526b3bb937b7339031b0b63632b960a11b60448201526064016117f5565b60008060008613156121ae57507f00000000000000000000000000000000000000000000000000000000000000009050846121e1565b600085136121bb57600080fd5b50507f000000000000000000000000000000000000000000000000000000000000000015835b81156122325760006121f584860186614d7f565b905061222c6001600160a01b037f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc216823385613e8a565b50612259565b6000808061224286880188615a4e565b92509250925061225583833387856132b1565b5050505b505050505050565b612269613543565b6001600160a01b0381166000818152600c6020526040808220429055517f3e29f5fe90abf7abcb8b95c060dcd18f16a85b2b2cacee9438e46d7f505027ac9190a250565b6000611c0e60026122ca6122c586606087901b6157dd565b612419565b6122d49190615aa5565b6126fb565b60405163a167129560e01b81526000908190731f98431c8ad98523631ae4a59f267346ea31f9849063a16712959061231990899089908990600401615adf565b6020604051808303816000875af1158015612338573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061235c9190615b07565b60405163f637731d60e01b81529091506001600160a01b0382169063f637731d9061238b908690600401614c64565b600060405180830381600087803b1580156123a557600080fd5b505af11580156123b9573d6000803e3d6000fd5b509298975050505050505050565b600380546001600160a01b038381166001600160a01b0319831681179093556040519116919082907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e090600090a35050565b60006401000276a36001600160a01b03831610801590612455575073fffd8963efd1fc6a506488495d951d5263988d266001600160a01b038316105b6124855760405162461bcd60e51b81526020600482015260016024820152602960f91b60448201526064016117f5565b600160201b600160c01b03602083901b166001600160801b03811160071b81811c6001600160401b03811160061b90811c63ffffffff811160051b90811c61ffff811160041b90811c60ff8111600390811b91821c600f811160021b90811c918211600190811b92831c9790881196179094179092171790911717176080811061251757607f810383901c9150612521565b80607f0383901b91505b908002607f81811c60ff83811c9190911c800280831c81831c1c800280841c81841c1c800280851c81851c1c800280861c81861c1c800280871c81871c1c800280881c81881c1c800280891c81891c1c8002808a1c818a1c1c8002808b1c818b1c1c8002808c1c818c1c1c8002808d1c818d1c1c8002808e1c9c81901c9c909c1c80029c8d901c9e9d607f198f0160401b60c09190911c6001603f1b161760c19b909b1c6001603e1b169a909a1760c29990991c6001603d1b169890981760c39790971c6001603c1b169690961760c49590951c6001603b1b169490941760c59390931c6001603a1b169290921760c69190911c600160391b161760c79190911c600160381b161760c89190911c600160371b161760c99190911c600160361b161760ca9190911c600160351b161760cb9190911c600160341b161760cc9190911c600160331b161760cd9190911c600160321b1617693627a301d71055774c8581026f028f6481ab7f045a5af012a19d003aa9198101608090811d906fdb2df09e81959a81455e260799a0632f8301901d600281810b9083900b146126ef57886001600160a01b03166126d4826126fb565b6001600160a01b031611156126e95781611625565b80611625565b50979650505050505050565b60008060008360020b12612712578260020b61271a565b8260020b6000035b9050620d89e88111156127535760405162461bcd60e51b81526020600482015260016024820152601560fa1b60448201526064016117f5565b60008160011660000361276a57600160801b61277c565b6ffffcb933bd6fad37aa2d162d1a5940015b6001600160881b0316905060028216156127a6576ffff97272373d413259a46990580e213a0260801c5b60048216156127c5576ffff2e50f5f656932ef12357cf3c7fdcc0260801c5b60088216156127e4576fffe5caca7e10e4e61c3624eaa0941cd00260801c5b6010821615612803576fffcb9843d60f6159c9db58835c9266440260801c5b6020821615612822576fff973b41fa98c081472e6896dfb254c00260801c5b6040821615612841576fff2ea16466c96a3843ec78b326b528610260801c5b6080821615612860576ffe5dee046a99a2a811c461f1969c30530260801c5b610100821615612880576ffcbe86c7900a88aedcffc83b479aa3a40260801c5b6102008216156128a0576ff987a7253ac413176f2b074cf7815e540260801c5b6104008216156128c0576ff3392b0822b70005940c7a398e4b70f30260801c5b6108008216156128e0576fe7159475a2c29b7443b29c7fa6e889d90260801c5b611000821615612900576fd097f3bdfd2022b8845ad8f792aa58250260801c5b612000821615612920576fa9f746462d870fdf8a65dc1f90e061e50260801c5b614000821615612940576f70d869a156d2a1b890bb3df62baf32f70260801c5b618000821615612960576f31be135f97d08fd981231505542fcfa60260801c5b62010000821615612981576f09aa508b5b7a84e1c677de54f3e99bc90260801c5b620200008216156129a1576e5d6af8dedb81196699c329225ee6040260801c5b620400008216156129c0576d2216e584f5fa1ea926041bedfe980260801c5b620800008216156129dd576b048a170391f7dc42444e8fa20260801c5b60008460020b13156129fe5780600019816129fa576129fa6157c7565b0490505b600160201b810615612a11576001612a14565b60005b60ff16602082901c0192505050919050565b6000600160801b8210612a3857600080fd5b5090565b60408051600180825281830190925260009182919060208083019080368337019050509050600081600081518110612a7657612a766157f1565b63ffffffff9092166020928302919091019091015260405163883bdbfd60e01b81526000906001600160a01b0385169063883bdbfd90612aba908590600401615b24565b600060405180830381865afa158015612ad7573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052612aff9190810190615c05565b50905080600081518110612b1557612b156157f1565b602002602001015192505050919050565b600080826001600160a01b0316633850c7bd6040518163ffffffff1660e01b815260040160e060405180830381865afa158015612b67573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612b8b9190615ce7565b509398975050505050505050565b6000816001600160a01b0316630dfe16816040518163ffffffff1660e01b8152600401602060405180830381865afa158015612bd9573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612bfd9190615b07565b6001600160a01b0316836001600160a01b0316630dfe16816040518163ffffffff1660e01b8152600401602060405180830381865afa158015612c44573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612c689190615b07565b6001600160a01b0316148015611c0e5750816001600160a01b031663d21220a76040518163ffffffff1660e01b8152600401602060405180830381865afa158015612cb7573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612cdb9190615b07565b6001600160a01b0316836001600160a01b031663d21220a76040518163ffffffff1660e01b8152600401602060405180830381865afa158015612d22573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612d469190615b07565b6001600160a01b0316149392505050565b6000808290506000612e8d826001600160a01b0316630dfe16816040518163ffffffff1660e01b8152600401602060405180830381865afa158015612da0573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612dc49190615b07565b836001600160a01b031663d21220a76040518163ffffffff1660e01b8152600401602060405180830381865afa158015612e02573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612e269190615b07565b846001600160a01b031663ddca3f436040518163ffffffff1660e01b8152600401602060405180830381865afa158015612e64573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612e889190615d80565b612eca565b9050612ead731f98431c8ad98523631ae4a59f267346ea31f98482612f35565b6001600160a01b0316846001600160a01b03161492505050919050565b6040805160608101825260008082526020820181905291810191909152826001600160a01b0316846001600160a01b03161115612f05579192915b50604080516060810182526001600160a01b03948516815292909316602083015262ffffff169181019190915290565b600081602001516001600160a01b031682600001516001600160a01b031610612f5d57600080fd5b82826000015183602001518460400151604051602001612f7f93929190615adf565b60408051601f19818403018152908290528051602091820120612ff5939290917fe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b5491016001600160f81b0319815260609390931b6001600160601b03191660018401526015830191909152603582015260550190565b60408051601f1981840301815291905280516020909101209392505050565b6000806130417f00000000000000000000000000000000000000000000000006f05b59d3b200008561575e565b9050610bcb83826157dd565b6001546000908190613067906001600160a01b0316612a3c565b60025490925061309c90600160801b810460060b90849061309790600160b81b900465ffffffffffff1642615775565b61418f565b90509091565b6000611c0e826110c56130b486613a3c565b85614224565b80516001600160a01b03166000908152600b602052604090205460ff166130f4576040516368f7a67560e11b815260040160405180910390fd5b80516001600160a01b039081166000908152600960209081526040808320828601518452825280832080546001600160a01b0319168786169081179091558352600d825280832085519094168352929052908120805490919061315a9061ffff166157a6565b91906101000a81548161ffff021916908361ffff160217905550806020015181600001516001600160a01b0316836001600160a01b03167fa32435755c235de2976ed44a75a2f85cb01faf0c894f639fe0c32bb9455fea8f60405160405180910390a45050565b608082015160015490151590600090613235906001600160a01b0316836131e857876131ea565b305b7f0000000000000000000000000000000000000000000000000000000000000000158760000151886020015189604001518a60a001518e8d8c604051602001611b3e93929190615da5565b50905081156132a8576000612710856080015183613253919061575e565b61325d91906157dd565b606086015190915061329a906001600160a01b037f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2169083614111565b611a4a876120698385615775565b50505050505050565b6001600160a01b0384166000908152600b602052604090205460ff166132ea576040516368f7a67560e11b815260040160405180910390fd5b60006132f4611677565b6001600160a01b038781166000908152600d60209081526040808320938a168352929052908120549192509061333b908590600160481b90046001600160b81b0316615850565b9050600061334d87600286600161135f565b6001600160a01b03808a166000908152600d60209081526040808320938c16835292905290812054919250906133929061338c90849061ffff1661575e565b85613014565b90508083106133be5760405163099df59560e01b815260048101849052602481018290526044016117f5565b600160b81b83106133e2576040516347798fbb60e11b815260040160405180910390fd5b6001600160a01b038981166000908152600d602090815260408083208c851684529091529081902080546001600160481b0316600160481b6001600160b81b03881602179055516340c10f1960e01b81527f000000000000000000000000320aaab3038bc08317f5a4be19ea1d9608551d79909116906340c10f199061346e908a908a90600401615e4c565b600060405180830381600087803b15801561348857600080fd5b505af115801561349c573d6000803e3d6000fd5b50505050876001600160a01b0316896001600160a01b03167f215ddfcab0d76106c31bf9b5a07ea2e7f29d6aedc851283dfa6cfaf0f57b4a76886040516134e591815260200190565b60405180910390a3505050505050505050565b600080613506858585614342565b600086815260086020526040902054909150600160601b90046001600160a01b03163303610bcb5761353a81600754614367565b95945050505050565b3361354c61194e565b6001600160a01b0316146116445760405162461bcd60e51b815260206004820181905260248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e657260448201526064016117f5565b6224ea008110156135c65760405163014889ef60e61b815260040160405180910390fd5b6301e133808111156135eb576040516339fff14360e11b815260040160405180910390fd5b60008190556040518181527f3d2aa45fe0e6a1adc880e2168a84dafb756666d9a5929072ec63804bc379d2c790602001610c10565b6001546001600160a01b0316801580159061364257506136408183612b99565b155b1561366057604051635750bb2f60e01b815260040160405180910390fd5b61366982612d57565b61368657604051638bd5971160e01b815260040160405180910390fd5b600180546001600160a01b0319166001600160a01b0384169081179091556040517f67816c9262630d6052ccaada1732fda377aa9e9abb3bc91cfd887a016a18a43a90600090a25050565b6000611c0e8383670de0b6b3a764000061437c565b60006136f182611646565b90506136fc81610c1b565b1561371a57604051632da2f89160e01b815260040160405180910390fd5b613766816040805180820182526001600160601b03428116825233602080840191825260009586526008905292909320905191516001600160a01b0316600160601b0291909216179055565b81604001516001600160a01b03168260200151827f42d62a8e3e20f2d287ae6847f14da1a409484c016cee5cbc797f0ca5cd8024938560000151866060015187608001518860a001518960c001516040516137f09594939291906001600160a01b039586168152602081019490945260408401929092526060830152909116608082015260a00190565b60405180910390a4919050565b6138098585848461439b565b604051632770a7eb60e21b81526001600160a01b037f000000000000000000000000320aaab3038bc08317f5a4be19ea1d9608551d791690639dc29fac906138579086908590600401615e4c565b600060405180830381600087803b15801561387157600080fd5b505af1158015612255573d6000803e3d6000fd5b6001600160a01b038216600090815260056020908152604080832081518083019092525464ffffffffff81168252600160281b90046001600160d81b031691810182905290158015906138e457508281602001516001600160d81b0316105b1561396b5780516000906138ff9064ffffffffff1642615775565b90506202a30081111561391257506202a3005b600061395a83602001516001600160d81b0316670de0b6b3a764000084620151806706f05b59d3b2000061394691906157dd565b613950919061575e565b6110c59190615850565b905080851115613968578094505b50505b505060408051808201825264ffffffffff42811682526001600160d81b0380851660208085019182526001600160a01b03881660009081526005909152949094209251935116600160281b0292169190911790558092915050565b600480546001600160a01b0319169055611807816123c7565b6001600160a01b0381166000818152600b6020908152604091829020805460ff1916600190811790915591519182527f4196a45b9de3a3d829f8037f12d13f69efcc4dd50e3e25ea1bc556d3b50cbfeb910160405180910390a250565b6000610a6882670de0b6b3a76400007f000000000000000000000000320aaab3038bc08317f5a4be19ea1d9608551d797f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc261442d565b3360096000613aa46020870187614d7f565b6001600160a01b039081168252602080830193909352604091820160009081208885013582529093529120541614613aef576040516311970ee160e31b815260040160405180910390fd5b60096000613b006020860186614d7f565b6001600160a01b03168152602080820192909252604090810160009081208684018035835290845282822080546001600160a01b0319169055338252600d9093529081209091600191908390613b569088614d7f565b6001600160a01b031681526020808201929092526040908101600090812054338252600d845291812061ffff90921693909303935083929091613b9b90880188614d7f565b6001600160a01b0316815260208082019290925260409081016000908120805461ffff191661ffff9590951694909417909355338352600d82528220908290613be690880188614d7f565b6001600160a01b031681526020810191909152604001600090812054600160481b90046001600160b81b03169150613c2561338c61ffff85168761575e565b90508115801590613c365750808210155b15613c5e5760405163099df59560e01b815260048101839052602481018290526044016117f5565b613c6b6020870187614d7f565b6001600160a01b03166342842e0e308989602001356040518463ffffffff1660e01b8152600401613c9e939291906158e8565b600060405180830381600087803b158015613cb857600080fd5b505af1158015613ccc573d6000803e3d6000fd5b5050506020870180359150613ce19088614d7f565b6001600160a01b0316336001600160a01b03167f367be65505e5aff90e7e646744b5f75280ec6d79a0e78690f6020b875a03bc1260405160405180910390a450505050505050565b60008083421115613d56576040516359e3d5cb60e11b8152600481018590524260248201526044016117f5565b6000808b6001600160a01b031663128acb088c8c613d738d61451d565b6001600160a01b038c1615613d88578b613dc1565b8e613db157613dac600173fffd8963efd1fc6a506488495d951d5263988d26615e65565b613dc1565b613dc16401000276a36001615e8c565b8a6040518663ffffffff1660e01b8152600401613de2959493929190615eac565b60408051808303816000875af1158015613e00573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190613e249190615ee7565b915091508915613e4157613e3781615f0b565b9350819250613e50565b613e4a82615f0b565b93508092505b87841015613e7b5760405163e20a4c5d60e01b815260048101859052602481018990526044016117f5565b50509850989650505050505050565b60006040516323b872dd60e01b6000528460045283602452826044526020600060646000808a5af13d15601f3d1160016000511416171691506000606052806040525080613f115760405162461bcd60e51b81526020600482015260146024820152731514905394d1915497d19493d357d1905253115160621b60448201526064016117f5565b5050505050565b6000806000613f2684611646565b9250613f3183610c1b565b915081600003613f545760405163010ab10b60e51b815260040160405180910390fd5b613f5f8383866134f8565b929491935050565b6001600160a01b038381166000908152600d602090815260408083209386168352929052208054600290613fa49062010000900461ffff16615788565b82546101009290920a61ffff8181021990931691831602179091556001600160a01b038481166000908152600d602090815260408083209387168352929052908120549091169080821561401d5782614002866001610dbc8861573c565b61400c919061575e565b915061401a82610a7a611677565b90505b6001600160a01b038681166000908152600d60209081526040808320938916835292905290812054600160481b90046001600160b81b0316908082841161406657838303614069565b60005b9150818a1161407957600061407d565b818a035b90506000811561409b576140948284868d8d61452f565b90506140ad565b6140a88a8a30878f6137fd565b508983035b861580156140ba57508015155b80156140f357506001600160a01b038a81166000908152600d60209081526040808320938d168352929052205462010000900461ffff16155b15614104576141048a8a838461439b565b5050505050505050505050565b600060405163a9059cbb60e01b6000528360045282602452602060006044600080895af13d15601f3d11600160005114161716915060006060528060405250806112785760405162461bcd60e51b815260206004820152600f60248201526e1514905394d1915497d19052531151608a1b60448201526064016117f5565b60008160060b6000036141c95760405162461bcd60e51b8152602060048201526002602482015261042560f41b60448201526064016117f5565b838303600683810b9082900b816141e2576141e26157c7565b05915060008160060b12801561420f57508260060b8160060b81614208576142086157c7565b0760060b15155b1561421c57600019909101905b509392505050565b600254600090819061424590600160b81b900465ffffffffffff1642615775565b90506000614255826000546146ca565b905060008560000361428857507f00000000000000000000000000000000000000000000000029a2241af62c000061432e565b61429285876146ca565b90507f00000000000000000000000000000000000000000000000029a2241af62c00008111156142e357507f00000000000000000000000000000000000000000000000029a2241af62c000061432e565b7f00000000000000000000000000000000000000000000000006f05b59d3b2000081101561432e57507f00000000000000000000000000000000000000000000000006f05b59d3b200005b61433881836146df565b9695505050505050565b6000610bcb8260a0015184426143589190615775565b84608001518560600151614710565b6000611c0e8383670de0b6b3a7640000614763565b82820281151584158583048514171661439457600080fd5b0492915050565b6143a58183615775565b6001600160a01b038581166000818152600d602090815260408083209489168084529482529182902080546001600160b81b0396909616600160481b026001600160481b039096169590951790945551848152919290917f6bf816470c006730e7f4a7fd0cef6041a868c06000a0bf3960688d4323b38330910160405180910390a350505050565b600080614439866126fb565b90506001600160801b036001600160a01b038216116144a8576001600160a01b038082168002908481169086161061448857614483600160c01b876001600160801b031683614791565b6144a0565b6144a081876001600160801b0316600160c01b614791565b92505061209c565b60006144c26001600160a01b03831680600160401b614791565b9050836001600160a01b0316856001600160a01b0316106144fa576144f5600160801b876001600160801b031683614791565b614512565b61451281876001600160801b0316600160801b614791565b979650505050505050565b6000600160ff1b8210612a3857600080fd5b60008061271061455f7f00000000000000000000000000000000000000000000000000000000000003e88961575e565b61456991906157dd565b9050808703600061457a8883615850565b604051632770a7eb60e21b81529091506001600160a01b037f000000000000000000000000320aaab3038bc08317f5a4be19ea1d9608551d791690639dc29fac906145cb9030908790600401615e4c565b600060405180830381600087803b1580156145e557600080fd5b505af11580156145f9573d6000803e3d6000fd5b50505050868111156146ab576146128686308a8b6137fd565b60405163a9059cbb60e01b81526001600160a01b037f000000000000000000000000320aaab3038bc08317f5a4be19ea1d9608551d79169063a9059cbb906146629089908b860390600401615e4c565b6020604051808303816000875af1158015614681573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906146a59190615f27565b506146be565b6146b88686308a856137fd565b80870393505b50505095945050505050565b6000611c0e83670de0b6b3a76400008461437c565b6000611c0e670de0b6b3a7640000836146f78661483f565b6147019190615f44565b61470b9190615f74565b6149f8565b60008061471d85856146ca565b9050600061473384670de0b6b3a7640000615775565b9050600061474182846146df565b9050600061474f828a61575e565b9050611625670de0b6b3a7640000826157dd565b82820281151584158583048514171661477b57600080fd5b6001826001830304018115150290509392505050565b60008080600019858709858702925082811083820303915050806000036147ca57600084116147bf57600080fd5b508290049050611c0e565b8084116147d657600080fd5b60008486880960026001871981018816978890046003810283188082028403028082028403028082028403028082028403028082028403029081029092039091026000889003889004909101858311909403939093029303949094049190911702949350505050565b60008082136148605760405162461bcd60e51b81526004016117f590615fa2565b6000606061486d84614ba1565b03609f8181039490941b90931c6c465772b2bbbb5f824b15207a3081018102606090811d6d0388eaa27412d5aca026815d636e018202811d6d0df99ac502031bf953eff472fdcc018202811d6d13cdffb29d51d99322bdff5f2211018202811d6d0a0f742023def783a307a986912e018202811d6d01920d8043ca89b5239253284e42018202811d6c0b7a86d7375468fac667a0a527016c29508e458543d8aa4df2abee7883018302821d6d0139601a2efabe717e604cbb4894018302821d6d02247f7a7b6594320649aa03aba1018302821d6c8c3f38e95a6b1ff2ab1c3b343619018302821d6d02384773bdf1ac5676facced60901901830290911d6cb9a025d814b29c212b8b1a07cd190190910260016c0504a838426634cdd8738f543560611b03190105711340daa0d5f769dba1915cef59f0815a5506027d0267a36c0c95b3975ab3ee5b203a7614a3f75373f047d803ae7b6687f2b393909302929092017d57115e47018c7177eebf7cd370a3356a1b7863008a5ae8028c72b88642840160ae1d92915050565b6000680248ce36a70cb26b3e198213614a1357506000919050565b680755bf798b4a1bf1e58212614a5a5760405162461bcd60e51b815260206004820152600c60248201526b4558505f4f564552464c4f5760a01b60448201526064016117f5565b6503782dace9d9604e83901b059150600060606bb17217f7d1cf79abc9e3b39884821b056001605f1b01901d6bb17217f7d1cf79abc9e3b39881029093036c240c330e9fb2d9cbaf0fd5aafb1981018102606090811d6d0277594991cfc85f6e2461837cd9018202811d6d1a521255e34f6a5061b25ef1c9c319018202811d6db1bbb201f443cf962f1a1d3db4a5018202811d6e02c72388d9f74f51a9331fed693f1419018202811d6e05180bb14799ab47a8a8cb2a527d57016d02d16720577bd19bf614176fe9ea6c10fe68e7fd37d0007b713f765084018402831d9081019084016d01d3967ed30fc4f89c02bab5708119010290911d6e0587f503bb6ea29d25fcb740196450019091026d360d7aeea093263ecc6e0ecb291760621b010574029d9dc38563c32e5c2f6dc192ee70ef65f9978af30260c3939093039290921c92915050565b6000808211614bc25760405162461bcd60e51b81526004016117f590615fa2565b5060016001600160801b03821160071b82811c6001600160401b031060061b1782811c63ffffffff1060051b1782811c61ffff1060041b1782811c60ff10600390811b90911783811c600f1060021b1783811c909110821b1791821c111790565b6001600160a01b038116811461180757600080fd5b60008060408385031215614c4b57600080fd5b8235614c5681614c23565b946020939093013593505050565b6001600160a01b0391909116815260200190565b600060208284031215614c8a57600080fd5b5035919050565b60008083601f840112614ca357600080fd5b5081356001600160401b03811115614cba57600080fd5b602083019150836020828501011115614cd257600080fd5b9250929050565b600080600080600060808688031215614cf157600080fd5b8535614cfc81614c23565b94506020860135614d0c81614c23565b93506040860135925060608601356001600160401b03811115614d2e57600080fd5b614d3a88828901614c91565b969995985093965092949392505050565b600060e08284031215614d5d57600080fd5b50919050565b600060e08284031215614d7557600080fd5b611c0e8383614d4b565b600060208284031215614d9157600080fd5b8135611c0e81614c23565b600060808284031215614d5d57600080fd5b60008060008385036080811215614dc457600080fd5b8435614dcf81614c23565b93506040601f1982011215614de357600080fd5b5060208401915060608401356001600160401b03811115614e0357600080fd5b614e0f86828701614d9c565b9150509250925092565b81516001600160a01b03908116825260208084015190830152604080840151821690830152606080840151908301526080808401519083015260a0838101519083015260c092830151169181019190915260e00190565b600080600060608486031215614e8557600080fd5b8335614e9081614c23565b92506020840135614ea081614c23565b929592945050506040919091013590565b60008083601f840112614ec357600080fd5b5081356001600160401b03811115614eda57600080fd5b6020830191508360208260051b8501011115614cd257600080fd5b60008060208385031215614f0857600080fd5b82356001600160401b03811115614f1e57600080fd5b614f2a85828601614eb1565b90969095509350505050565b634e487b7160e01b600052604160045260246000fd5b604080519081016001600160401b0381118282101715614f6e57614f6e614f36565b60405290565b604051608081016001600160401b0381118282101715614f6e57614f6e614f36565b60405160c081016001600160401b0381118282101715614f6e57614f6e614f36565b604051601f8201601f191681016001600160401b0381118282101715614fe057614fe0614f36565b604052919050565b600082601f830112614ff957600080fd5b81356001600160401b0381111561501257615012614f36565b615025601f8201601f1916602001614fb8565b81815284602083860101111561503a57600080fd5b816020850160208301376000918101602001919091529392505050565b60ff8116811461180757600080fd5b60006060828403121561507857600080fd5b604051606081018181106001600160401b038211171561509a5761509a614f36565b60405290508082356150ab81615057565b8082525060208301356020820152604083013560408201525092915050565b6000608082840312156150dc57600080fd5b6150e4614f4c565b905081356001600160401b03808211156150fd57600080fd5b908301906080828603121561511157600080fd5b615119614f74565b8235815260208301358281111561512f57600080fd5b61513b87828601614fe8565b6020830152506040830135604082015260608301358281111561515d57600080fd5b61516987828601614fe8565b60608301525083525061518190508360208401615066565b602082015292915050565b801515811461180757600080fd5b600080600080608085870312156151b057600080fd5b84356151bb81614c23565b93506020850135600481106151cf57600080fd5b925060408501356001600160401b038111156151ea57600080fd5b6151f6878288016150ca565b92505060608501356152078161518c565b939692955090935050565b600060e0828403121561522457600080fd5b60405160e081018181106001600160401b038211171561524657615246614f36565b604052823561525481614c23565b815260208381013590820152604083013561526e81614c23565b80604083015250606083013560608201526080830135608082015260a083013560a082015260c08301356152a181614c23565b60c08201529392505050565b60008083601f8401126152bf57600080fd5b5081356001600160401b038111156152d657600080fd5b6020830191508360208260061b8501011115614cd257600080fd5b6000806000806060858703121561530757600080fd5b843561531281614c23565b935060208501356001600160401b038082111561532e57600080fd5b61533a888389016152ad565b9095509350604087013591508082111561535357600080fd5b5061536087828801614d9c565b91505092959194509250565b6000815180845260005b8181101561539257602081850181015186830182015201615376565b506000602082860101526020601f19601f83011685010191505092915050565b6000602080830181845280855180835260408601915060408160051b870101925083870160005b8281101561540757603f198886030184526153f585835161536c565b945092850192908501906001016153d9565b5092979650505050505050565b600060c08284031215614d5d57600080fd5b6000806000610100848603121561543c57600080fd5b833561544781614c23565b9250602084013561545781614c23565b91506154668560408601615414565b90509250925092565b6000806020838503121561548257600080fd5b82356001600160401b0381111561549857600080fd5b614f2a858286016152ad565b60008060008061014085870312156154bb57600080fd5b6154c58686614d4b565b935060e085013592506101008501356154dd81614c23565b91506101208501356001600160401b038111156154f957600080fd5b61536087828801614d9c565b600080600080610120858703121561551c57600080fd5b843561552781614c23565b9350602085013561553781614c23565b92506155468660408701615414565b91506101008501356001600160401b038111156154f957600080fd5b6000806000806080858703121561557857600080fd5b843561558381614c23565b9350602085013561559381614c23565b92506040850135915060608501356001600160401b038111156154f957600080fd5b600080600080606085870312156155cb57600080fd5b843593506020850135925060408501356001600160401b038111156155ef57600080fd5b6155fb87828801614c91565b95989497509550505050565b6000806040838503121561561a57600080fd5b823561562581614c23565b9150602083013561563581614c23565b809150509250929050565b60006020828403121561565257600080fd5b81356001600160401b038082111561566957600080fd5b9083019081850361012081121561567f57600080fd5b615687614f74565b833561569281614c23565b81526020848101359082015260c0603f19830112156156b057600080fd5b6156b8614f96565b9150604084013582526060840135602083015260808401356156d981614c23565b604083015260a08401356156ec81614c23565b606083015260c0840135608083015260e084013560a08301526040810191909152610100830135908282111561572157600080fd5b61572d878386016150ca565b60608201529695505050505050565b6000610a6836836150ca565b634e487b7160e01b600052601160045260246000fd5b8082028115828204841417610a6857610a68615748565b81810381811115610a6857610a68615748565b600061ffff82168061579c5761579c615748565b6000190192915050565b600061ffff8083168181036157bd576157bd615748565b6001019392505050565b634e487b7160e01b600052601260045260246000fd5b6000826157ec576157ec6157c7565b500490565b634e487b7160e01b600052603260045260246000fd5b848152608081016004851061582c57634e487b7160e01b600052602160045260246000fd5b602082019490945260408101929092526001600160a01b0316606090910152919050565b80820180821115610a6857610a68615748565b6000806040838503121561587657600080fd5b825161588181614c23565b6020939093015192949293505050565b6000604082840312156158a357600080fd5b604051604081018181106001600160401b03821117156158c5576158c5614f36565b60405282356158d381614c23565b81526020928301359281019290925250919050565b6001600160a01b039384168152919092166020820152604081019190915260600190565b6000808335601e1984360301811261592357600080fd5b83016020810192503590506001600160401b0381111561594257600080fd5b803603821315614cd257600080fd5b81835281816020850137506000828201602090810191909152601f909101601f19169091010190565b803561598581615057565b60ff16825260208181013590830152604090810135910152565b6001600160a01b03848116825283166020820152606060408201526000823536849003607e190181126159d157600080fd5b608060608401528301803560e08401526159ee602082018261590c565b6080610100860152615a0561016086018284615951565b9150506040820135610120850152615a20606083018361590c565b85830360df19016101408701529250615a3a828483615951565b9250505061353a608084016020860161597a565b600080600060608486031215615a6357600080fd5b8335615a6e81614c23565b92506020840135615a7e81614c23565b915060408401356001600160401b03811115615a9957600080fd5b614e0f868287016150ca565b60008160020b8360020b80615abc57615abc6157c7565b627fffff19821460001982141615615ad657615ad6615748565b90059392505050565b6001600160a01b03938416815291909216602082015262ffffff909116604082015260600190565b600060208284031215615b1957600080fd5b8151611c0e81614c23565b6020808252825182820181905260009190848201906040850190845b81811015615b6257835163ffffffff1683529284019291840191600101615b40565b50909695505050505050565b60006001600160401b03821115615b8757615b87614f36565b5060051b60200190565b600082601f830112615ba257600080fd5b81516020615bb7615bb283615b6e565b614fb8565b82815260059290921b84018101918181019086841115615bd657600080fd5b8286015b84811015615bfa578051615bed81614c23565b8352918301918301615bda565b509695505050505050565b60008060408385031215615c1857600080fd5b82516001600160401b0380821115615c2f57600080fd5b818501915085601f830112615c4357600080fd5b81516020615c53615bb283615b6e565b82815260059290921b84018101918181019089841115615c7257600080fd5b948201945b83861015615ca05785518060060b8114615c915760008081fd5b82529482019490820190615c77565b91880151919650909350505080821115615cb957600080fd5b50615cc685828601615b91565b9150509250929050565b805161ffff81168114615ce257600080fd5b919050565b600080600080600080600060e0888a031215615d0257600080fd5b8751615d0d81614c23565b8097505060208801518060020b8114615d2557600080fd5b9550615d3360408901615cd0565b9450615d4160608901615cd0565b9350615d4f60808901615cd0565b925060a0880151615d5f81615057565b60c0890151909250615d708161518c565b8091505092959891949750929550565b600060208284031215615d9257600080fd5b815162ffffff81168114611c0e57600080fd5b600060018060a01b03808616835280851660208401525060606040830152825160806060840152805160e084015260208101516080610100850152615dee61016085018261536c565b604083015161012086015260609092015184830360df1901610140860152919050615e19818361536c565b915050602084015160ff8151166080850152602081015160a0850152604081015160c08501525080915050949350505050565b6001600160a01b03929092168252602082015260400190565b6001600160a01b03828116828216039080821115615e8557615e85615748565b5092915050565b6001600160a01b03818116838216019080821115615e8557615e85615748565b6001600160a01b0386811682528515156020830152604082018590528316606082015260a0608082018190526000906145129083018461536c565b60008060408385031215615efa57600080fd5b505080516020909101519092909150565b6000600160ff1b8201615f2057615f20615748565b5060000390565b600060208284031215615f3957600080fd5b8151611c0e8161518c565b80820260008212600160ff1b84141615615f6057615f60615748565b8181058314821517610a6857610a68615748565b600082615f8357615f836157c7565b600160ff1b821460001984141615615f9d57615f9d615748565b500590565b60208082526009908201526815539111519253915160ba1b60408201526060019056fea2646970667358221220cf1e7367895e3d245f9e47ffa8db0c5b480a9cd6632426bf75f10fbb3e2d067c64736f6c63430008110033
Verified Source Code Full Match
Compiler: v0.8.17+commit.8df45f5f
EVM: london
Optimization: Yes (20 runs)
Context.sol 24 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)
pragma solidity ^0.8.0;
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}
IUniswapV3PoolOwnerActions.sol 23 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Permissioned pool actions
/// @notice Contains pool methods that may only be called by the factory owner
interface IUniswapV3PoolOwnerActions {
/// @notice Set the denominator of the protocol's % share of the fees
/// @param feeProtocol0 new protocol fee for token0 of the pool
/// @param feeProtocol1 new protocol fee for token1 of the pool
function setFeeProtocol(uint8 feeProtocol0, uint8 feeProtocol1) external;
/// @notice Collect the protocol fee accrued to the pool
/// @param recipient The address to which collected protocol fees should be sent
/// @param amount0Requested The maximum amount of token0 to send, can be 0 to collect fees in only token1
/// @param amount1Requested The maximum amount of token1 to send, can be 0 to collect fees in only token0
/// @return amount0 The protocol fee collected in token0
/// @return amount1 The protocol fee collected in token1
function collectProtocol(
address recipient,
uint128 amount0Requested,
uint128 amount1Requested
) external returns (uint128 amount0, uint128 amount1);
}
ERC20.sol 206 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
/// @notice Modern and gas efficient ERC20 + EIP-2612 implementation.
/// @author Solmate (https://github.com/Rari-Capital/solmate/blob/main/src/tokens/ERC20.sol)
/// @author Modified from Uniswap (https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2ERC20.sol)
/// @dev Do not manually set balances without updating totalSupply, as the sum of all user balances must not exceed it.
abstract contract ERC20 {
/*//////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
event Transfer(address indexed from, address indexed to, uint256 amount);
event Approval(address indexed owner, address indexed spender, uint256 amount);
/*//////////////////////////////////////////////////////////////
METADATA STORAGE
//////////////////////////////////////////////////////////////*/
string public name;
string public symbol;
uint8 public immutable decimals;
/*//////////////////////////////////////////////////////////////
ERC20 STORAGE
//////////////////////////////////////////////////////////////*/
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
/*//////////////////////////////////////////////////////////////
EIP-2612 STORAGE
//////////////////////////////////////////////////////////////*/
uint256 internal immutable INITIAL_CHAIN_ID;
bytes32 internal immutable INITIAL_DOMAIN_SEPARATOR;
mapping(address => uint256) public nonces;
/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
constructor(
string memory _name,
string memory _symbol,
uint8 _decimals
) {
name = _name;
symbol = _symbol;
decimals = _decimals;
INITIAL_CHAIN_ID = block.chainid;
INITIAL_DOMAIN_SEPARATOR = computeDomainSeparator();
}
/*//////////////////////////////////////////////////////////////
ERC20 LOGIC
//////////////////////////////////////////////////////////////*/
function approve(address spender, uint256 amount) public virtual returns (bool) {
allowance[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
return true;
}
function transfer(address to, uint256 amount) public virtual returns (bool) {
balanceOf[msg.sender] -= amount;
// Cannot overflow because the sum of all user
// balances can't exceed the max uint256 value.
unchecked {
balanceOf[to] += amount;
}
emit Transfer(msg.sender, to, amount);
return true;
}
function transferFrom(
address from,
address to,
uint256 amount
) public virtual returns (bool) {
uint256 allowed = allowance[from][msg.sender]; // Saves gas for limited approvals.
if (allowed != type(uint256).max) allowance[from][msg.sender] = allowed - amount;
balanceOf[from] -= amount;
// Cannot overflow because the sum of all user
// balances can't exceed the max uint256 value.
unchecked {
balanceOf[to] += amount;
}
emit Transfer(from, to, amount);
return true;
}
/*//////////////////////////////////////////////////////////////
EIP-2612 LOGIC
//////////////////////////////////////////////////////////////*/
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public virtual {
require(deadline >= block.timestamp, "PERMIT_DEADLINE_EXPIRED");
// Unchecked because the only math done is incrementing
// the owner's nonce which cannot realistically overflow.
unchecked {
address recoveredAddress = ecrecover(
keccak256(
abi.encodePacked(
"\x19\x01",
DOMAIN_SEPARATOR(),
keccak256(
abi.encode(
keccak256(
"Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"
),
owner,
spender,
value,
nonces[owner]++,
deadline
)
)
)
),
v,
r,
s
);
require(recoveredAddress != address(0) && recoveredAddress == owner, "INVALID_SIGNER");
allowance[recoveredAddress][spender] = value;
}
emit Approval(owner, spender, value);
}
function DOMAIN_SEPARATOR() public view virtual returns (bytes32) {
return block.chainid == INITIAL_CHAIN_ID ? INITIAL_DOMAIN_SEPARATOR : computeDomainSeparator();
}
function computeDomainSeparator() internal view virtual returns (bytes32) {
return
keccak256(
abi.encode(
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
keccak256(bytes(name)),
keccak256("1"),
block.chainid,
address(this)
)
);
}
/*//////////////////////////////////////////////////////////////
INTERNAL MINT/BURN LOGIC
//////////////////////////////////////////////////////////////*/
function _mint(address to, uint256 amount) internal virtual {
totalSupply += amount;
// Cannot overflow because the sum of all user
// balances can't exceed the max uint256 value.
unchecked {
balanceOf[to] += amount;
}
emit Transfer(address(0), to, amount);
}
function _burn(address from, uint256 amount) internal virtual {
balanceOf[from] -= amount;
// Cannot underflow because a user's balance
// will never be larger than the total supply.
unchecked {
totalSupply -= amount;
}
emit Transfer(from, address(0), amount);
}
}
IUniswapV3PoolActions.sol 103 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Permissionless pool actions
/// @notice Contains pool methods that can be called by anyone
interface IUniswapV3PoolActions {
/// @notice Sets the initial price for the pool
/// @dev Price is represented as a sqrt(amountToken1/amountToken0) Q64.96 value
/// @param sqrtPriceX96 the initial sqrt price of the pool as a Q64.96
function initialize(uint160 sqrtPriceX96) external;
/// @notice Adds liquidity for the given recipient/tickLower/tickUpper position
/// @dev The caller of this method receives a callback in the form of IUniswapV3MintCallback#uniswapV3MintCallback
/// in which they must pay any token0 or token1 owed for the liquidity. The amount of token0/token1 due depends
/// on tickLower, tickUpper, the amount of liquidity, and the current price.
/// @param recipient The address for which the liquidity will be created
/// @param tickLower The lower tick of the position in which to add liquidity
/// @param tickUpper The upper tick of the position in which to add liquidity
/// @param amount The amount of liquidity to mint
/// @param data Any data that should be passed through to the callback
/// @return amount0 The amount of token0 that was paid to mint the given amount of liquidity. Matches the value in the callback
/// @return amount1 The amount of token1 that was paid to mint the given amount of liquidity. Matches the value in the callback
function mint(
address recipient,
int24 tickLower,
int24 tickUpper,
uint128 amount,
bytes calldata data
) external returns (uint256 amount0, uint256 amount1);
/// @notice Collects tokens owed to a position
/// @dev Does not recompute fees earned, which must be done either via mint or burn of any amount of liquidity.
/// Collect must be called by the position owner. To withdraw only token0 or only token1, amount0Requested or
/// amount1Requested may be set to zero. To withdraw all tokens owed, caller may pass any value greater than the
/// actual tokens owed, e.g. type(uint128).max. Tokens owed may be from accumulated swap fees or burned liquidity.
/// @param recipient The address which should receive the fees collected
/// @param tickLower The lower tick of the position for which to collect fees
/// @param tickUpper The upper tick of the position for which to collect fees
/// @param amount0Requested How much token0 should be withdrawn from the fees owed
/// @param amount1Requested How much token1 should be withdrawn from the fees owed
/// @return amount0 The amount of fees collected in token0
/// @return amount1 The amount of fees collected in token1
function collect(
address recipient,
int24 tickLower,
int24 tickUpper,
uint128 amount0Requested,
uint128 amount1Requested
) external returns (uint128 amount0, uint128 amount1);
/// @notice Burn liquidity from the sender and account tokens owed for the liquidity to the position
/// @dev Can be used to trigger a recalculation of fees owed to a position by calling with an amount of 0
/// @dev Fees must be collected separately via a call to #collect
/// @param tickLower The lower tick of the position for which to burn liquidity
/// @param tickUpper The upper tick of the position for which to burn liquidity
/// @param amount How much liquidity to burn
/// @return amount0 The amount of token0 sent to the recipient
/// @return amount1 The amount of token1 sent to the recipient
function burn(
int24 tickLower,
int24 tickUpper,
uint128 amount
) external returns (uint256 amount0, uint256 amount1);
/// @notice Swap token0 for token1, or token1 for token0
/// @dev The caller of this method receives a callback in the form of IUniswapV3SwapCallback#uniswapV3SwapCallback
/// @param recipient The address to receive the output of the swap
/// @param zeroForOne The direction of the swap, true for token0 to token1, false for token1 to token0
/// @param amountSpecified The amount of the swap, which implicitly configures the swap as exact input (positive), or exact output (negative)
/// @param sqrtPriceLimitX96 The Q64.96 sqrt price limit. If zero for one, the price cannot be less than this
/// value after the swap. If one for zero, the price cannot be greater than this value after the swap
/// @param data Any data to be passed through to the callback
/// @return amount0 The delta of the balance of token0 of the pool, exact when negative, minimum when positive
/// @return amount1 The delta of the balance of token1 of the pool, exact when negative, minimum when positive
function swap(
address recipient,
bool zeroForOne,
int256 amountSpecified,
uint160 sqrtPriceLimitX96,
bytes calldata data
) external returns (int256 amount0, int256 amount1);
/// @notice Receive token0 and/or token1 and pay it back, plus a fee, in the callback
/// @dev The caller of this method receives a callback in the form of IUniswapV3FlashCallback#uniswapV3FlashCallback
/// @dev Can be used to donate underlying tokens pro-rata to currently in-range liquidity providers by calling
/// with 0 amount{0,1} and sending the donation amount(s) from the callback
/// @param recipient The address which will receive the token0 and token1 amounts
/// @param amount0 The amount of token0 to send
/// @param amount1 The amount of token1 to send
/// @param data Any data to be passed through to the callback
function flash(
address recipient,
uint256 amount0,
uint256 amount1,
bytes calldata data
) external;
/// @notice Increase the maximum number of price and liquidity observations that this pool will store
/// @dev This method is no-op if the pool already has an observationCardinalityNext greater than or equal to
/// the input observationCardinalityNext.
/// @param observationCardinalityNext The desired minimum number of observations for the pool to store
function increaseObservationCardinalityNext(uint16 observationCardinalityNext) external;
}
IUniswapV3PoolState.sol 116 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Pool state that can change
/// @notice These methods compose the pool's state, and can change with any frequency including multiple times
/// per transaction
interface IUniswapV3PoolState {
/// @notice The 0th storage slot in the pool stores many values, and is exposed as a single method to save gas
/// when accessed externally.
/// @return sqrtPriceX96 The current price of the pool as a sqrt(token1/token0) Q64.96 value
/// tick The current tick of the pool, i.e. according to the last tick transition that was run.
/// This value may not always be equal to SqrtTickMath.getTickAtSqrtRatio(sqrtPriceX96) if the price is on a tick
/// boundary.
/// observationIndex The index of the last oracle observation that was written,
/// observationCardinality The current maximum number of observations stored in the pool,
/// observationCardinalityNext The next maximum number of observations, to be updated when the observation.
/// feeProtocol The protocol fee for both tokens of the pool.
/// Encoded as two 4 bit values, where the protocol fee of token1 is shifted 4 bits and the protocol fee of token0
/// is the lower 4 bits. Used as the denominator of a fraction of the swap fee, e.g. 4 means 1/4th of the swap fee.
/// unlocked Whether the pool is currently locked to reentrancy
function slot0()
external
view
returns (
uint160 sqrtPriceX96,
int24 tick,
uint16 observationIndex,
uint16 observationCardinality,
uint16 observationCardinalityNext,
uint8 feeProtocol,
bool unlocked
);
/// @notice The fee growth as a Q128.128 fees of token0 collected per unit of liquidity for the entire life of the pool
/// @dev This value can overflow the uint256
function feeGrowthGlobal0X128() external view returns (uint256);
/// @notice The fee growth as a Q128.128 fees of token1 collected per unit of liquidity for the entire life of the pool
/// @dev This value can overflow the uint256
function feeGrowthGlobal1X128() external view returns (uint256);
/// @notice The amounts of token0 and token1 that are owed to the protocol
/// @dev Protocol fees will never exceed uint128 max in either token
function protocolFees() external view returns (uint128 token0, uint128 token1);
/// @notice The currently in range liquidity available to the pool
/// @dev This value has no relationship to the total liquidity across all ticks
function liquidity() external view returns (uint128);
/// @notice Look up information about a specific tick in the pool
/// @param tick The tick to look up
/// @return liquidityGross the total amount of position liquidity that uses the pool either as tick lower or
/// tick upper,
/// liquidityNet how much liquidity changes when the pool price crosses the tick,
/// feeGrowthOutside0X128 the fee growth on the other side of the tick from the current tick in token0,
/// feeGrowthOutside1X128 the fee growth on the other side of the tick from the current tick in token1,
/// tickCumulativeOutside the cumulative tick value on the other side of the tick from the current tick
/// secondsPerLiquidityOutsideX128 the seconds spent per liquidity on the other side of the tick from the current tick,
/// secondsOutside the seconds spent on the other side of the tick from the current tick,
/// initialized Set to true if the tick is initialized, i.e. liquidityGross is greater than 0, otherwise equal to false.
/// Outside values can only be used if the tick is initialized, i.e. if liquidityGross is greater than 0.
/// In addition, these values are only relative and must be used only in comparison to previous snapshots for
/// a specific position.
function ticks(int24 tick)
external
view
returns (
uint128 liquidityGross,
int128 liquidityNet,
uint256 feeGrowthOutside0X128,
uint256 feeGrowthOutside1X128,
int56 tickCumulativeOutside,
uint160 secondsPerLiquidityOutsideX128,
uint32 secondsOutside,
bool initialized
);
/// @notice Returns 256 packed tick initialized boolean values. See TickBitmap for more information
function tickBitmap(int16 wordPosition) external view returns (uint256);
/// @notice Returns the information about a position by the position's key
/// @param key The position's key is a hash of a preimage composed by the owner, tickLower and tickUpper
/// @return _liquidity The amount of liquidity in the position,
/// Returns feeGrowthInside0LastX128 fee growth of token0 inside the tick range as of the last mint/burn/poke,
/// Returns feeGrowthInside1LastX128 fee growth of token1 inside the tick range as of the last mint/burn/poke,
/// Returns tokensOwed0 the computed amount of token0 owed to the position as of the last mint/burn/poke,
/// Returns tokensOwed1 the computed amount of token1 owed to the position as of the last mint/burn/poke
function positions(bytes32 key)
external
view
returns (
uint128 _liquidity,
uint256 feeGrowthInside0LastX128,
uint256 feeGrowthInside1LastX128,
uint128 tokensOwed0,
uint128 tokensOwed1
);
/// @notice Returns data about a specific observation index
/// @param index The element of the observations array to fetch
/// @dev You most likely want to use #observe() instead of this method to get an observation as of some amount of time
/// ago, rather than at a specific index in the array.
/// @return blockTimestamp The timestamp of the observation,
/// Returns tickCumulative the tick multiplied by seconds elapsed for the life of the pool as of the observation timestamp,
/// Returns secondsPerLiquidityCumulativeX128 the seconds per in range liquidity for the life of the pool as of the observation timestamp,
/// Returns initialized whether the observation has been initialized and the values are safe to use
function observations(uint256 index)
external
view
returns (
uint32 blockTimestamp,
int56 tickCumulative,
uint160 secondsPerLiquidityCumulativeX128,
bool initialized
);
}
INFTEDA.sol 66 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
import {ERC721} from "solmate/tokens/ERC721.sol";
import {ERC20} from "solmate/tokens/ERC20.sol";
interface INFTEDA {
/// @notice struct containing all auction info
/// @dev this struct is never stored, only a hash of it
struct Auction {
// the nft owner
address nftOwner;
// the nft token id
uint256 auctionAssetID;
// the nft contract address
ERC721 auctionAssetContract;
// How much the auction price will decay in each period
// expressed as percent scaled by 1e18, i.e. 1e18 = 100%
uint256 perPeriodDecayPercentWad;
// the number of seconds in the period over which perPeriodDecay occurs
uint256 secondsInPeriod;
// the auction start price
uint256 startPrice;
// the payment asset and quote asset for startPrice
ERC20 paymentAsset;
}
/// @notice emitted when an auction is started
/// @param auctionID the id of the auction that was started
/// @param auctionAssetID the token id of the ERC721 asset being auctioned
/// @param auctionAssetContract the contract address of the ERC721 asset being auctioned
/// @param nftOwner the owner of the ERC721 asset being auctioned
/// @param perPeriodDecayPercentWad How much the auction price will decay in each period
/// @param secondsInPeriod the number of seconds in the period over which perPeriodDecay occurs
/// @param startPrice the starting price of the auction
/// @param paymentAsset the payment asset and quote asset for startPrice
event StartAuction(
uint256 indexed auctionID,
uint256 indexed auctionAssetID,
ERC721 indexed auctionAssetContract,
address nftOwner,
uint256 perPeriodDecayPercentWad,
uint256 secondsInPeriod,
uint256 startPrice,
ERC20 paymentAsset
);
/// @param auctionID the id of the auction that has ended
/// @param price the price that the purchaser paid to receive the ERC721 asset being auctioned
event EndAuction(uint256 indexed auctionID, uint256 price);
/// @notice Returns the current price of the passed auction, reverts if no such auction exists
/// @param auction The auction for which the caller wants to know the current price
/// @return price the current amount required to purchase the NFT being sold in this auction
function auctionCurrentPrice(Auction calldata auction) external view returns (uint256);
/// @notice Returns a uint256 used to identify the auction
/// @dev Derived from the auction. Identitical auctions cannot exist simultaneously
/// @param auction The auction to get an ID for
/// @return id the id of this auction
function auctionID(Auction memory auction) external pure returns (uint256);
/// @notice Returns the time at which startAuction was most recently successfully called for the given auction id
/// @param id The id of the auction
function auctionStartTime(uint256 id) external view returns (uint256);
}
PaprController.sol 659 lines
//SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.8.17;
import {ERC20} from "solmate/tokens/ERC20.sol";
import {ERC721, ERC721TokenReceiver} from "solmate/tokens/ERC721.sol";
import {SafeTransferLib} from "solmate/utils/SafeTransferLib.sol";
import {FixedPointMathLib} from "solmate/utils/FixedPointMathLib.sol";
import {INFTEDA, NFTEDAStarterIncentive} from "./NFTEDA/extensions/NFTEDAStarterIncentive.sol";
import {Ownable2Step} from "openzeppelin-contracts/access/Ownable2Step.sol";
import {Multicallable} from "solady/utils/Multicallable.sol";
import {PaprToken} from "./PaprToken.sol";
import {UniswapOracleFundingRateController} from "./UniswapOracleFundingRateController.sol";
import {ReservoirOracleUnderwriter} from "./ReservoirOracleUnderwriter.sol";
import {IPaprController} from "./interfaces/IPaprController.sol";
import {UniswapHelpers} from "./libraries/UniswapHelpers.sol";
contract PaprController is
IPaprController,
UniswapOracleFundingRateController,
ERC721TokenReceiver,
Multicallable,
Ownable2Step,
ReservoirOracleUnderwriter,
NFTEDAStarterIncentive
{
using SafeTransferLib for ERC20;
/// @notice boolean indicating whether token0 in pool is the underlying token
bool internal immutable _token0IsUnderlying;
/// @inheritdoc IPaprController
uint256 public immutable override maxLTV;
/// @inheritdoc IPaprController
uint256 public immutable override liquidationAuctionMinSpacing = 2 days;
/// @notice amount the price of an auction decreases by per auctionDecayPeriod, expressed as a decimal scaled by 1e18
uint256 internal immutable _perPeriodAuctionDecayWAD = 0.7e18;
/// @notice amount of time that perPeriodAuctionDecayWAD is applied to, expressed in seconds
uint256 internal immutable _auctionDecayPeriod = 1 days;
/// @notice the multiplier for the starting price of an auction, applied to the current price of the collateral in papr tokens
uint256 internal immutable _auctionStartPriceMultiplier = 3;
/// @notice the minimum time between when proposeAllowedCollateral and acceptProposedCollateral can be called for a certain asset
uint256 internal immutable _newCollateralProposalPeriod = 5 days;
/// @notice fee paid by the vault owner when their vault is liquidated if there was excess debt credited to their vault, in bips
uint256 internal immutable _liquidationPenaltyBips = 1000;
/// @inheritdoc IPaprController
mapping(ERC721 => mapping(uint256 => address)) public override collateralOwner;
/// @inheritdoc IPaprController
mapping(ERC721 => CachedPrice) public lastAuctionStartPrice;
/// @inheritdoc IPaprController
mapping(ERC721 => bool) public override isAllowed;
/// @notice returns the timestamp at which an asset was proposed to be added as allowed collateral
mapping(ERC721 => uint256) internal _proposedTimestamp;
/// @dev account => asset => vaultInfo
mapping(address => mapping(ERC721 => IPaprController.VaultInfo)) private _vaultInfo;
/// @dev does not validate args
/// e.g. does not check whether underlying or oracleSigner are address(0)
constructor(
string memory name,
string memory symbol,
uint256 _maxLTV,
ERC20 underlying,
address oracleSigner,
ERC721[] memory startingCollateral
)
NFTEDAStarterIncentive(1e17)
UniswapOracleFundingRateController(underlying, new PaprToken(name, symbol))
ReservoirOracleUnderwriter(oracleSigner, address(underlying))
{
maxLTV = _maxLTV;
_token0IsUnderlying = address(underlying) < address(papr);
uint256 underlyingONE = 10 ** underlying.decimals();
uint160 initSqrtRatio;
// initialize the pool at 1:1
if (_token0IsUnderlying) {
initSqrtRatio = UniswapHelpers.oneToOneSqrtRatio(underlyingONE, 1e18);
} else {
initSqrtRatio = UniswapHelpers.oneToOneSqrtRatio(1e18, underlyingONE);
}
address _pool = UniswapHelpers.deployAndInitPool(address(underlying), address(papr), 10000, initSqrtRatio);
_init(underlyingONE, _pool);
for (uint256 i = 0; i < startingCollateral.length; i++) {
_allowCollateral(startingCollateral[i]);
}
}
/// @inheritdoc IPaprController
function addCollateral(IPaprController.Collateral[] calldata collateralArr) external override {
for (uint256 i = 0; i < collateralArr.length;) {
_addCollateralToVault(msg.sender, collateralArr[i]);
collateralArr[i].addr.transferFrom(msg.sender, address(this), collateralArr[i].id);
unchecked {
++i;
}
}
}
/// @inheritdoc IPaprController
function removeCollateral(
address sendTo,
IPaprController.Collateral[] calldata collateralArr,
ReservoirOracleUnderwriter.OracleInfo calldata oracleInfo
) external override {
uint256 cachedTarget = updateTarget();
uint256 oraclePrice;
ERC721 collateralAddr;
for (uint256 i = 0; i < collateralArr.length;) {
if (i == 0) {
collateralAddr = collateralArr[i].addr;
oraclePrice = underwritePriceForCollateral(
collateralAddr, ReservoirOracleUnderwriter.PriceKind.LOWER, oracleInfo, true
);
} else {
if (collateralAddr != collateralArr[i].addr) {
revert CollateralAddressesDoNotMatch();
}
}
_removeCollateral(sendTo, collateralArr[i], oraclePrice, cachedTarget);
unchecked {
++i;
}
}
}
/// @inheritdoc IPaprController
function increaseDebt(
address mintTo,
ERC721 asset,
uint256 amount,
ReservoirOracleUnderwriter.OracleInfo calldata oracleInfo
) external override {
_increaseDebt({account: msg.sender, asset: asset, mintTo: mintTo, amount: amount, oracleInfo: oracleInfo});
}
/// @inheritdoc IPaprController
function reduceDebt(address account, ERC721 asset, uint256 amount) external override {
uint256 debt = _vaultInfo[account][asset].debt;
_reduceDebt({
account: account,
asset: asset,
burnFrom: msg.sender,
accountDebt: debt,
amountToReduce: amount > debt ? debt : amount
});
}
/// @notice Handler for safeTransferFrom of an NFT
/// @dev Should be preferred to `addCollateral` if only one NFT is being added
/// to avoid approval call and save gas
/// @param from the current owner of the nft
/// @param _id the id of the NFT
/// @param data encoded IPaprController.OnERC721ReceivedArgs
/// @return selector indicating succesful receiving of the NFT
function onERC721Received(address, address from, uint256 _id, bytes calldata data)
external
override
returns (bytes4)
{
IPaprController.OnERC721ReceivedArgs memory request = abi.decode(data, (IPaprController.OnERC721ReceivedArgs));
IPaprController.Collateral memory collateral = IPaprController.Collateral(ERC721(msg.sender), _id);
_addCollateralToVault(from, collateral);
if (request.swapParams.minOut > 0) {
_increaseDebtAndSell(from, request.proceedsTo, ERC721(msg.sender), request.swapParams, request.oracleInfo);
} else if (request.debt > 0) {
_increaseDebt(from, collateral.addr, request.proceedsTo, request.debt, request.oracleInfo);
}
return ERC721TokenReceiver.onERC721Received.selector;
}
/// CONVENIENCE SWAP FUNCTIONS ///
/// @inheritdoc IPaprController
function increaseDebtAndSell(
address proceedsTo,
ERC721 collateralAsset,
IPaprController.SwapParams calldata params,
ReservoirOracleUnderwriter.OracleInfo calldata oracleInfo
) external override returns (uint256 amountOut) {
bool hasFee = params.swapFeeBips != 0;
(amountOut,) = UniswapHelpers.swap(
pool,
hasFee ? address(this) : proceedsTo,
!_token0IsUnderlying,
params.amount,
params.minOut,
params.sqrtPriceLimitX96,
params.deadline,
abi.encode(msg.sender, collateralAsset, oracleInfo)
);
if (hasFee) {
uint256 fee = amountOut * params.swapFeeBips / 1e4;
underlying.safeTransfer(params.swapFeeTo, fee);
underlying.safeTransfer(proceedsTo, amountOut - fee);
}
}
/// @inheritdoc IPaprController
function buyAndReduceDebt(address account, ERC721 collateralAsset, IPaprController.SwapParams calldata params)
external
override
returns (uint256)
{
bool hasFee = params.swapFeeBips != 0;
(uint256 amountOut, uint256 amountIn) = UniswapHelpers.swap(
pool,
msg.sender,
_token0IsUnderlying,
params.amount,
params.minOut,
params.sqrtPriceLimitX96,
params.deadline,
abi.encode(msg.sender)
);
if (hasFee) {
underlying.safeTransferFrom(msg.sender, params.swapFeeTo, amountIn * params.swapFeeBips / 1e4);
}
uint256 debt = _vaultInfo[account][collateralAsset].debt;
_reduceDebt({
account: account,
asset: collateralAsset,
burnFrom: msg.sender,
accountDebt: debt,
amountToReduce: amountOut > debt ? debt : amountOut
});
return amountOut;
}
function uniswapV3SwapCallback(int256 amount0Delta, int256 amount1Delta, bytes calldata _data) external {
if (msg.sender != address(pool)) {
revert("wrong caller");
}
bool isUnderlyingIn;
uint256 amountToPay;
if (amount0Delta > 0) {
amountToPay = uint256(amount0Delta);
isUnderlyingIn = _token0IsUnderlying;
} else {
require(amount1Delta > 0); // swaps entirely within 0-liquidity regions are not supported
amountToPay = uint256(amount1Delta);
isUnderlyingIn = !(_token0IsUnderlying);
}
if (isUnderlyingIn) {
address payer = abi.decode(_data, (address));
underlying.safeTransferFrom(payer, msg.sender, amountToPay);
} else {
(address account, ERC721 asset, ReservoirOracleUnderwriter.OracleInfo memory oracleInfo) =
abi.decode(_data, (address, ERC721, ReservoirOracleUnderwriter.OracleInfo));
_increaseDebt(account, asset, msg.sender, amountToPay, oracleInfo);
}
}
/// LIQUIDATION AUCTION FUNCTIONS ///
/// @inheritdoc IPaprController
function purchaseLiquidationAuctionNFT(
Auction calldata auction,
uint256 maxPrice,
address sendTo,
ReservoirOracleUnderwriter.OracleInfo calldata oracleInfo
) external override {
// This function is almost the same as NFTEDA._purchaseNFT
// however we inline so we can receive payment first
// and execute logic before sending NFT
uint256 id;
uint256 startTime;
uint256 price;
(id, startTime, price) = _checkAuctionAndReturnDetails(auction);
if (price > maxPrice) {
revert MaxPriceTooLow(price, maxPrice);
}
_clearAuctionState(id);
if (startTime == _vaultInfo[auction.nftOwner][auction.auctionAssetContract].latestAuctionStartTime) {
_vaultInfo[auction.nftOwner][auction.auctionAssetContract].latestAuctionStartTime = 0;
}
auction.paymentAsset.safeTransferFrom(msg.sender, address(this), price);
_auctionPurchaseUpdateVault(price, auction.nftOwner, auction.auctionAssetContract, oracleInfo);
auction.auctionAssetContract.safeTransferFrom(address(this), sendTo, auction.auctionAssetID);
emit EndAuction(id, price);
}
/// @inheritdoc IPaprController
function startLiquidationAuction(
address account,
IPaprController.Collateral calldata collateral,
ReservoirOracleUnderwriter.OracleInfo calldata oracleInfo
) external override returns (INFTEDA.Auction memory auction) {
uint256 cachedTarget = updateTarget();
IPaprController.VaultInfo storage info = _vaultInfo[account][collateral.addr];
// check collateral belongs to account
if (collateralOwner[collateral.addr][collateral.id] != account) {
revert IPaprController.InvalidCollateralAccountPair();
}
uint256 oraclePrice =
underwritePriceForCollateral(collateral.addr, ReservoirOracleUnderwriter.PriceKind.TWAP, oracleInfo, false);
if (!(info.debt > _maxDebt(oraclePrice * info.count, cachedTarget))) {
revert IPaprController.NotLiquidatable();
}
if (block.timestamp - info.latestAuctionStartTime < liquidationAuctionMinSpacing) {
revert IPaprController.MinAuctionSpacing();
}
info.latestAuctionStartTime = uint40(block.timestamp);
--info.count;
++info.auctionCount;
emit RemoveCollateral(account, collateral.addr, collateral.id);
delete collateralOwner[collateral.addr][collateral.id];
// start price is frozen price * auctionStartPriceMultiplier,
// converted to papr value at the current contract price
uint256 price = (oraclePrice * _auctionStartPriceMultiplier) * FixedPointMathLib.WAD / cachedTarget;
// we guard auction price decay incase of oracle attack
CachedPrice memory cached = lastAuctionStartPrice[collateral.addr];
if (cached.price != 0 && cached.price > price) {
uint256 timeElapsed = block.timestamp - cached.timestamp;
if (timeElapsed > 1 days) {
timeElapsed = 1 days;
}
uint256 min = FixedPointMathLib.mulWadDown(
cached.price, FixedPointMathLib.WAD - (MAX_PER_SECOND_PRICE_CHANGE * timeElapsed)
);
if (price < min) {
price = min;
}
}
lastAuctionStartPrice[collateral.addr] =
CachedPrice({timestamp: uint40(block.timestamp), price: uint216(price)});
_startAuction(
auction = Auction({
nftOwner: account,
auctionAssetID: collateral.id,
auctionAssetContract: collateral.addr,
perPeriodDecayPercentWad: _perPeriodAuctionDecayWAD,
secondsInPeriod: _auctionDecayPeriod,
startPrice: price,
paymentAsset: papr
})
);
}
function acceptProposedCollateral(ERC721 asset) external {
uint256 proposedTime = _proposedTimestamp[asset];
if (proposedTime == 0) revert AssetNotProposed();
if (proposedTime + _newCollateralProposalPeriod > block.timestamp) revert ProposalPeriodNotComplete();
delete _proposedTimestamp[asset];
_allowCollateral(asset);
}
/// OWNER FUNCTIONS ///
/// @inheritdoc IPaprController
function setPool(address _pool) external override onlyOwner {
_setPool(_pool);
emit UpdatePool(_pool);
}
/// @inheritdoc IPaprController
function setFundingPeriod(uint256 _fundingPeriod) external override onlyOwner {
_setFundingPeriod(_fundingPeriod);
emit UpdateFundingPeriod(_fundingPeriod);
}
function proposeAllowedCollateral(ERC721 asset) external onlyOwner {
_proposedTimestamp[asset] = block.timestamp;
emit ProposeAllowedCollateral(asset);
}
function cancelProposedCollateral(ERC721 asset) external onlyOwner {
delete _proposedTimestamp[asset];
emit CancelProposedAllowedCollateral(asset);
}
function removeAllowedCollateral(ERC721[] calldata assets) external onlyOwner {
for (uint256 i = 0; i < assets.length;) {
isAllowed[assets[i]] = false;
emit AllowCollateral(assets[i], false);
unchecked {
++i;
}
}
}
/// VIEW FUNCTIONS ///
/// @inheritdoc IPaprController
function maxDebt(uint256 totalCollateraValue) external view override returns (uint256) {
if (_lastUpdated == block.timestamp) {
return _maxDebt(totalCollateraValue, _target);
}
return _maxDebt(totalCollateraValue, newTarget());
}
/// @inheritdoc IPaprController
function vaultInfo(address account, ERC721 asset)
external
view
override
returns (IPaprController.VaultInfo memory)
{
return _vaultInfo[account][asset];
}
/// INTERNAL NON-VIEW ///
function _allowCollateral(ERC721 asset) internal {
isAllowed[asset] = true;
emit AllowCollateral(asset, true);
}
function _addCollateralToVault(address account, IPaprController.Collateral memory collateral) internal {
if (!isAllowed[collateral.addr]) {
revert IPaprController.InvalidCollateral();
}
collateralOwner[collateral.addr][collateral.id] = account;
++_vaultInfo[account][collateral.addr].count;
emit AddCollateral(account, collateral.addr, collateral.id);
}
function _removeCollateral(
address sendTo,
IPaprController.Collateral calldata collateral,
uint256 oraclePrice,
uint256 cachedTarget
) internal {
if (collateralOwner[collateral.addr][collateral.id] != msg.sender) {
revert IPaprController.OnlyCollateralOwner();
}
delete collateralOwner[collateral.addr][collateral.id];
uint16 newCount;
unchecked {
newCount = _vaultInfo[msg.sender][collateral.addr].count - 1;
_vaultInfo[msg.sender][collateral.addr].count = newCount;
}
uint256 debt = _vaultInfo[msg.sender][collateral.addr].debt;
uint256 max = _maxDebt(oraclePrice * newCount, cachedTarget);
if (debt != 0 && !(debt < max)) {
revert IPaprController.ExceedsMaxDebt(debt, max);
}
collateral.addr.safeTransferFrom(address(this), sendTo, collateral.id);
emit RemoveCollateral(msg.sender, collateral.addr, collateral.id);
}
function _increaseDebt(
address account,
ERC721 asset,
address mintTo,
uint256 amount,
ReservoirOracleUnderwriter.OracleInfo memory oracleInfo
) internal {
if (!isAllowed[asset]) {
revert IPaprController.InvalidCollateral();
}
uint256 cachedTarget = updateTarget();
uint256 newDebt = _vaultInfo[account][asset].debt + amount;
uint256 oraclePrice =
underwritePriceForCollateral(asset, ReservoirOracleUnderwriter.PriceKind.LOWER, oracleInfo, true);
uint256 max = _maxDebt(_vaultInfo[account][asset].count * oraclePrice, cachedTarget);
if (!(newDebt < max)) revert IPaprController.ExceedsMaxDebt(newDebt, max);
if (newDebt >= 1 << 184) revert IPaprController.DebtAmountExceedsUint184();
_vaultInfo[account][asset].debt = uint184(newDebt);
PaprToken(address(papr)).mint(mintTo, amount);
emit IncreaseDebt(account, asset, amount);
}
function _reduceDebt(address account, ERC721 asset, address burnFrom, uint256 accountDebt, uint256 amountToReduce)
internal
{
_reduceDebtWithoutBurn(account, asset, accountDebt, amountToReduce);
PaprToken(address(papr)).burn(burnFrom, amountToReduce);
}
function _reduceDebtWithoutBurn(address account, ERC721 asset, uint256 accountDebt, uint256 amountToReduce)
internal
{
_vaultInfo[account][asset].debt = uint184(accountDebt - amountToReduce);
emit ReduceDebt(account, asset, amountToReduce);
}
/// same as increaseDebtAndSell but takes args in memory
/// to work with onERC721Received
function _increaseDebtAndSell(
address account,
address proceedsTo,
ERC721 collateralAsset,
IPaprController.SwapParams memory params,
ReservoirOracleUnderwriter.OracleInfo memory oracleInfo
) internal {
bool hasFee = params.swapFeeBips != 0;
(uint256 amountOut,) = UniswapHelpers.swap(
pool,
hasFee ? address(this) : proceedsTo,
!_token0IsUnderlying,
params.amount,
params.minOut,
params.sqrtPriceLimitX96,
params.deadline,
abi.encode(account, collateralAsset, oracleInfo)
);
if (hasFee) {
uint256 fee = amountOut * params.swapFeeBips / 1e4;
underlying.safeTransfer(params.swapFeeTo, fee);
underlying.safeTransfer(proceedsTo, amountOut - fee);
}
}
function _auctionPurchaseUpdateVault(
uint256 price,
address account,
ERC721 asset,
ReservoirOracleUnderwriter.OracleInfo calldata oracleInfo
) internal {
--_vaultInfo[account][asset].auctionCount;
uint256 count = _vaultInfo[account][asset].count;
uint256 collateralValueCached;
uint256 maxDebtCached;
if (count != 0) {
collateralValueCached = underwritePriceForCollateral(
asset, ReservoirOracleUnderwriter.PriceKind.TWAP, oracleInfo, false
) * count;
maxDebtCached = _maxDebt(collateralValueCached, updateTarget());
}
uint256 debtCached = _vaultInfo[account][asset].debt;
/// anything above what is needed to bring this vault under maxDebt is considered excess
uint256 neededToSaveVault;
uint256 excess;
unchecked {
neededToSaveVault = maxDebtCached > debtCached ? 0 : debtCached - maxDebtCached;
excess = price > neededToSaveVault ? price - neededToSaveVault : 0;
}
uint256 remaining;
if (excess > 0) {
remaining = _handleExcess(excess, neededToSaveVault, debtCached, account, asset);
} else {
_reduceDebt(account, asset, address(this), debtCached, price);
// no excess, so price <= neededToSaveVault, meaning debtCached >= price
unchecked {
remaining = debtCached - price;
}
}
if (count == 0 && remaining != 0 && _vaultInfo[account][asset].auctionCount == 0) {
/// there will be debt left with no NFTs, set it to 0
_reduceDebtWithoutBurn(account, asset, remaining, remaining);
}
}
function _handleExcess(uint256 excess, uint256 neededToSaveVault, uint256 debtCached, address account, ERC721 asset)
internal
returns (uint256 remaining)
{
uint256 fee = excess * _liquidationPenaltyBips / 1e4;
uint256 credit;
// excess is a % of fee and so is <= fee
unchecked {
credit = excess - fee;
}
uint256 totalOwed = credit + neededToSaveVault;
PaprToken(address(papr)).burn(address(this), fee);
if (totalOwed > debtCached) {
// we owe them more papr than they have in debt
// so we pay down debt and send them the rest
_reduceDebt(account, asset, address(this), debtCached, debtCached);
// totalOwed > debtCached
unchecked {
papr.transfer(account, totalOwed - debtCached);
}
} else {
// reduce vault debt
_reduceDebt(account, asset, address(this), debtCached, totalOwed);
// debtCached >= totalOwed
unchecked {
remaining = debtCached - totalOwed;
}
}
}
/// INTERNAL VIEW ///
function _maxDebt(uint256 totalCollateraValue, uint256 cachedTarget) internal view returns (uint256) {
uint256 maxLoanUnderlying = totalCollateraValue * maxLTV;
return maxLoanUnderlying / cachedTarget;
}
}
ReservoirOracleUnderwriter.sol 161 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
import {ERC721} from "solmate/tokens/ERC721.sol";
import {FixedPointMathLib} from "solmate/utils/FixedPointMathLib.sol";
import {ReservoirOracle} from "@reservoir/ReservoirOracle.sol";
contract ReservoirOracleUnderwriter {
/// @notice The kind of floor price to use from the oracle
/// @dev SPOT is the floor price at the time of the oracle message
/// @dev TWAP is the average weighted floor price over the last TWAP_SECONDS
/// @dev LOWER is the minimum of SPOT and TWAP
/// @dev UPPER is the maximum of SPOT and TWAP
/// @dev see https://docs.reservoir.tools/reference/getoraclecollectionstopbidv2 for more details
enum PriceKind {
SPOT,
TWAP,
LOWER,
UPPER
}
/// @notice The signature of a message from our oracle signer
struct Sig {
uint8 v;
bytes32 r;
bytes32 s;
}
/// @notice The message and signature from our oracle signer
struct OracleInfo {
ReservoirOracle.Message message;
Sig sig;
}
/// @notice Describes a cached oracle price for a given NFT collection
/// @dev used to constrain how quickly price can grow and guard against oracle attacks
struct CachedPrice {
// the timestamp the price was cached
uint40 timestamp;
// the oracle price of the NFT collection
uint216 price;
}
error IncorrectOracleSigner();
error WrongIdentifierFromOracleMessage();
error WrongCurrencyFromOracleMessage();
error OracleMessageTimestampInvalid();
/// @notice the amount of time to use for the TWAP
uint256 constant TWAP_SECONDS = 7 days;
/// @notice the maximum time a given signed oracle message is valid for
uint256 constant VALID_FOR = 20 minutes;
/// @dev constant values used in checking signatures
bytes32 constant MESSAGE_SIG_HASH = keccak256("Message(bytes32 id,bytes payload,uint256 timestamp)");
bytes32 constant TOP_BID_SIG_HASH =
keccak256("ContractWideCollectionTopBidPrice(uint8 kind,uint256 twapSeconds,address contract)");
/// @notice The max per second price appreciation allowed for any collateral asset
/// @dev used to guard against oracle attacks
uint256 public constant MAX_PER_SECOND_PRICE_CHANGE = 0.5e18 / uint256(1 days);
/// @notice the signing address the contract expects from the oracle message
address public immutable oracleSigner;
/// @notice address of the currency we are receiving oracle prices in
address internal immutable _quoteCurrency;
/// @notice returns the cached timestamp and price for asset
mapping(ERC721 => CachedPrice) public cachedPriceForAsset;
constructor(address _oracleSigner, address _quoteCurrency_) {
oracleSigner = _oracleSigner;
_quoteCurrency = _quoteCurrency_;
}
/// @notice returns the price of an asset from a signed oracle message
/// @dev reverts if the signer of the oracle message is incorrect
/// @dev reverts if the oracle message was signed longer than VALID_FOR ago
/// @dev reverts if the oracle message is for the wrong ERC721 asset, wrong price kind, or wrong quote currency
/// @param asset the address of the ERC721 asset to underwrite the price for
/// @param priceKind the kind of price the function expects the oracle message to contain
/// @param oracleInfo the message and signature from our oracle signer
/// @param guard whether to use a guard to constrain price appreciation
/// @return oraclePrice the price of the asset, expressed in _quoteCurrency units. Price is max allowed price given
/// MAX_PER_SECOND_PRICE_CHANGE if guard = true and oracleInfo price > max
function underwritePriceForCollateral(ERC721 asset, PriceKind priceKind, OracleInfo memory oracleInfo, bool guard)
public
returns (uint256)
{
address signerAddress = ecrecover(
keccak256(
abi.encodePacked(
"\x19Ethereum Signed Message:\n32",
// EIP-712 structured-data hash
keccak256(
abi.encode(
MESSAGE_SIG_HASH,
oracleInfo.message.id,
keccak256(oracleInfo.message.payload),
oracleInfo.message.timestamp
)
)
)
),
oracleInfo.sig.v,
oracleInfo.sig.r,
oracleInfo.sig.s
);
if (signerAddress != oracleSigner) {
revert IncorrectOracleSigner();
}
bytes32 expectedId = keccak256(abi.encode(TOP_BID_SIG_HASH, priceKind, TWAP_SECONDS, asset));
if (oracleInfo.message.id != expectedId) {
revert WrongIdentifierFromOracleMessage();
}
if (
oracleInfo.message.timestamp > block.timestamp || oracleInfo.message.timestamp + VALID_FOR < block.timestamp
) {
revert OracleMessageTimestampInvalid();
}
(address oracleQuoteCurrency, uint256 oraclePrice) = abi.decode(oracleInfo.message.payload, (address, uint256));
if (oracleQuoteCurrency != _quoteCurrency) {
revert WrongCurrencyFromOracleMessage();
}
return guard ? _cacheAndReturnPriceOrMaxPrice(asset, oraclePrice) : oraclePrice;
}
/// @notice caches and returns the minimum of the passed price and the max price as well as the timestamp
/// @dev max price computed by MAX_PER_SECOND_PRICE_CHANGE * time elapsed since the cache was last updated
/// @dev time elapsed maxes at 2 days such that price can never grow by more than 100% between two successive
/// increase debt events for the same asset
function _cacheAndReturnPriceOrMaxPrice(ERC721 asset, uint256 price) internal returns (uint256) {
CachedPrice memory cached = cachedPriceForAsset[asset];
if (cached.price != 0 && cached.price < price) {
uint256 timeElapsed = block.timestamp - cached.timestamp;
if (timeElapsed > 2 days) {
timeElapsed = 2 days;
}
uint256 max = FixedPointMathLib.mulWadDown(
cached.price, (MAX_PER_SECOND_PRICE_CHANGE * timeElapsed) + FixedPointMathLib.WAD
);
if (price > max) {
price = max;
}
}
// We are OK with not checking for price overflow when casting to uint216
// as we do not consider values greater than this to be a practical possibility
cachedPriceForAsset[asset] = CachedPrice({timestamp: uint40(block.timestamp), price: uint216(price)});
return price;
}
}
IUniswapOracleFundingRateController.sol 19 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
import {IFundingRateController} from "./IFundingRateController.sol";
interface IUniswapOracleFundingRateController is IFundingRateController {
/// @notice emitted when pool is set
/// @param pool the new pool value
event SetPool(address indexed pool);
/// @notice emitted if _setPool is called with a pool
/// that's tokens do not match pool()
error PoolTokensDoNotMatch();
error InvalidUniswapV3Pool();
/// @notice The address of the Uniswap pool used for mark()
/// @return pool address of the pool
function pool() external returns (address);
}
Ownable.sol 83 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol)
pragma solidity ^0.8.0;
import "../utils/Context.sol";
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* By default, the owner account will be the one that deploys the contract. This
* can later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
constructor() {
_transferOwnership(_msgSender());
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/
function _checkOwner() internal view virtual {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions anymore. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby removing any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
IUniswapV3Pool.sol 24 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
import './pool/IUniswapV3PoolImmutables.sol';
import './pool/IUniswapV3PoolState.sol';
import './pool/IUniswapV3PoolDerivedState.sol';
import './pool/IUniswapV3PoolActions.sol';
import './pool/IUniswapV3PoolOwnerActions.sol';
import './pool/IUniswapV3PoolEvents.sol';
/// @title The interface for a Uniswap V3 Pool
/// @notice A Uniswap pool facilitates swapping and automated market making between any two assets that strictly conform
/// to the ERC20 specification
/// @dev The pool interface is broken up into many smaller pieces
interface IUniswapV3Pool is
IUniswapV3PoolImmutables,
IUniswapV3PoolState,
IUniswapV3PoolDerivedState,
IUniswapV3PoolActions,
IUniswapV3PoolOwnerActions,
IUniswapV3PoolEvents
{
}
UniswapOracleFundingRateController.sol 174 lines
//SPDX-License-Identifier: BUSL-1.1
pragma solidity >=0.8.0;
import {ERC20} from "solmate/tokens/ERC20.sol";
import {FixedPointMathLib} from "solmate/utils/FixedPointMathLib.sol";
import {SafeCastLib} from "solmate/utils/SafeCastLib.sol";
import {OracleLibrary} from "./libraries/OracleLibrary.sol";
import {UniswapHelpers} from "./libraries/UniswapHelpers.sol";
import {
IUniswapOracleFundingRateController,
IFundingRateController
} from "./interfaces/IUniswapOracleFundingRateController.sol";
contract UniswapOracleFundingRateController is IUniswapOracleFundingRateController {
/// @inheritdoc IFundingRateController
ERC20 public immutable underlying;
/// @inheritdoc IFundingRateController
ERC20 public immutable papr;
/// @inheritdoc IFundingRateController
uint256 public fundingPeriod;
/// @inheritdoc IUniswapOracleFundingRateController
address public pool;
/// @dev the max value of target / mark, used as a guard in _multiplier
uint256 internal immutable _targetMarkRatioMax = 3e18;
/// @dev the min value of target / mark, used as a guard in _multiplier
uint256 internal immutable _targetMarkRatioMin = 0.5e18;
// single slot, write together
uint128 internal _target;
int56 internal _lastCumulativeTick;
uint48 internal _lastUpdated;
int24 internal _lastTwapTick;
constructor(ERC20 _underlying, ERC20 _papr) {
underlying = _underlying;
papr = _papr;
_setFundingPeriod(90 days);
}
/// @inheritdoc IFundingRateController
function updateTarget() public override returns (uint256 nTarget) {
if (_lastUpdated == block.timestamp) {
return _target;
}
(int56 latestCumulativeTick, int24 latestTwapTick) = _latestTwapTickAndTickCumulative();
nTarget = _newTarget(latestTwapTick, _target);
_target = SafeCastLib.safeCastTo128(nTarget);
// will not overflow for 8000 years
_lastUpdated = uint48(block.timestamp);
_lastCumulativeTick = latestCumulativeTick;
_lastTwapTick = latestTwapTick;
emit UpdateTarget(nTarget);
}
/// @inheritdoc IFundingRateController
function newTarget() public view override returns (uint256) {
if (_lastUpdated == block.timestamp) {
return _target;
}
(, int24 latestTwapTick) = _latestTwapTickAndTickCumulative();
return _newTarget(latestTwapTick, _target);
}
/// @inheritdoc IFundingRateController
function mark() public view returns (uint256) {
if (_lastUpdated == block.timestamp) {
return _mark(_lastTwapTick);
}
(, int24 latestTwapTick) = _latestTwapTickAndTickCumulative();
return _mark(latestTwapTick);
}
/// @inheritdoc IFundingRateController
function lastUpdated() external view override returns (uint256) {
return _lastUpdated;
}
/// @inheritdoc IFundingRateController
function target() external view override returns (uint256) {
return _target;
}
/// @notice initializes the controller, setting pool and target
/// @dev assumes pool is initialized, does not check that pool tokens
/// match papr and underlying
/// @param _target_ the start value of target
/// @param _pool the pool address to use
function _init(uint256 _target_, address _pool) internal {
if (_lastUpdated != 0) revert AlreadyInitialized();
_setPool(_pool);
_lastUpdated = uint48(block.timestamp);
_target = SafeCastLib.safeCastTo128(_target_);
_lastCumulativeTick = OracleLibrary.latestCumulativeTick(pool);
_lastTwapTick = UniswapHelpers.poolCurrentTick(pool);
emit UpdateTarget(_target_);
}
/// @notice Updates `pool`
/// @dev reverts if new pool does not have same token0 and token1 as `pool`
/// @dev if pool = address(0), does NOT check that tokens match papr and underlying
function _setPool(address _pool) internal {
address currentPool = pool;
if (currentPool != address(0) && !UniswapHelpers.poolsHaveSameTokens(currentPool, _pool)) {
revert PoolTokensDoNotMatch();
}
if (!UniswapHelpers.isUniswapPool(_pool)) revert InvalidUniswapV3Pool();
pool = _pool;
emit SetPool(_pool);
}
/// @notice Updates fundingPeriod
/// @dev reverts if period is longer than 90 days or less than 7
function _setFundingPeriod(uint256 _fundingPeriod) internal {
if (_fundingPeriod < 28 days) revert FundingPeriodTooShort();
if (_fundingPeriod > 365 days) revert FundingPeriodTooLong();
fundingPeriod = _fundingPeriod;
emit SetFundingPeriod(_fundingPeriod);
}
/// @dev internal function to allow optimized SLOADs
function _newTarget(int24 latestTwapTick, uint256 cachedTarget) internal view returns (uint256) {
return FixedPointMathLib.mulWadDown(cachedTarget, _multiplier(_mark(latestTwapTick), cachedTarget));
}
/// @dev internal function to allow optimized SLOADs
function _mark(int24 twapTick) internal view returns (uint256) {
return OracleLibrary.getQuoteAtTick(twapTick, 1e18, address(papr), address(underlying));
}
/// @dev reverts if block.timestamp - _lastUpdated == 0
function _latestTwapTickAndTickCumulative() internal view returns (int56 tickCumulative, int24 twapTick) {
tickCumulative = OracleLibrary.latestCumulativeTick(pool);
twapTick = OracleLibrary.timeWeightedAverageTick(
_lastCumulativeTick, tickCumulative, int56(uint56(block.timestamp - _lastUpdated))
);
}
/// @notice The multiplier to apply to target() to get newTarget()
/// @dev Computes the funding rate for the time since _lastUpdated
/// 1 = 1e18, i.e.
/// > 1e18 means positive funding rate
/// < 1e18 means negative funding rate
/// sub 1e18 to get percent change
/// @return multiplier used to obtain newTarget()
function _multiplier(uint256 _mark_, uint256 cachedTarget) internal view returns (uint256) {
uint256 period = block.timestamp - _lastUpdated;
uint256 periodRatio = FixedPointMathLib.divWadDown(period, fundingPeriod);
uint256 targetMarkRatio;
if (_mark_ == 0) {
targetMarkRatio = _targetMarkRatioMax;
} else {
targetMarkRatio = FixedPointMathLib.divWadDown(cachedTarget, _mark_);
if (targetMarkRatio > _targetMarkRatioMax) {
targetMarkRatio = _targetMarkRatioMax;
} else if (targetMarkRatio < _targetMarkRatioMin) {
targetMarkRatio = _targetMarkRatioMin;
}
}
// safe to cast because targetMarkRatio > 0
return uint256(FixedPointMathLib.powWad(int256(targetMarkRatio), int256(periodRatio)));
}
}
FullMath.sol 128 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
/// @title Contains 512-bit math functions
/// @notice Facilitates multiplication and division that can have overflow of an intermediate value without any loss of precision
/// @dev Handles "phantom overflow" i.e., allows multiplication and division where an intermediate value overflows 256 bits
library FullMath {
/// @notice Calculates floor(a×b÷denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
/// @param a The multiplicand
/// @param b The multiplier
/// @param denominator The divisor
/// @return result The 256-bit result
/// @dev Credit to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldiv
function mulDiv(
uint256 a,
uint256 b,
uint256 denominator
) internal pure returns (uint256 result) {
unchecked {
// 512-bit multiply [prod1 prod0] = a * b
// Compute the product mod 2**256 and mod 2**256 - 1
// then use the Chinese Remainder Theorem to reconstruct
// the 512 bit result. The result is stored in two 256
// variables such that product = prod1 * 2**256 + prod0
uint256 prod0; // Least significant 256 bits of the product
uint256 prod1; // Most significant 256 bits of the product
assembly {
let mm := mulmod(a, b, not(0))
prod0 := mul(a, b)
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
}
// Handle non-overflow cases, 256 by 256 division
if (prod1 == 0) {
require(denominator > 0);
assembly {
result := div(prod0, denominator)
}
return result;
}
// Make sure the result is less than 2**256.
// Also prevents denominator == 0
require(denominator > prod1);
///////////////////////////////////////////////
// 512 by 256 division.
///////////////////////////////////////////////
// Make division exact by subtracting the remainder from [prod1 prod0]
// Compute remainder using mulmod
uint256 remainder;
assembly {
remainder := mulmod(a, b, denominator)
}
// Subtract 256 bit number from 512 bit number
assembly {
prod1 := sub(prod1, gt(remainder, prod0))
prod0 := sub(prod0, remainder)
}
// Factor powers of two out of denominator
// Compute largest power of two divisor of denominator.
// Always >= 1.
uint256 twos = (type(uint256).max - denominator + 1) & denominator;
// Divide denominator by power of two
assembly {
denominator := div(denominator, twos)
}
// Divide [prod1 prod0] by the factors of two
assembly {
prod0 := div(prod0, twos)
}
// Shift in bits from prod1 into prod0. For this we need
// to flip `twos` such that it is 2**256 / twos.
// If twos is zero, then it becomes one
assembly {
twos := add(div(sub(0, twos), twos), 1)
}
prod0 |= prod1 * twos;
// Invert denominator mod 2**256
// Now that denominator is an odd number, it has an inverse
// modulo 2**256 such that denominator * inv = 1 mod 2**256.
// Compute the inverse by starting with a seed that is correct
// correct for four bits. That is, denominator * inv = 1 mod 2**4
uint256 inv = (3 * denominator) ^ 2;
// Now use Newton-Raphson iteration to improve the precision.
// Thanks to Hensel's lifting lemma, this also works in modular
// arithmetic, doubling the correct bits in each step.
inv *= 2 - denominator * inv; // inverse mod 2**8
inv *= 2 - denominator * inv; // inverse mod 2**16
inv *= 2 - denominator * inv; // inverse mod 2**32
inv *= 2 - denominator * inv; // inverse mod 2**64
inv *= 2 - denominator * inv; // inverse mod 2**128
inv *= 2 - denominator * inv; // inverse mod 2**256
// Because the division is now exact we can divide by multiplying
// with the modular inverse of denominator. This will give us the
// correct result modulo 2**256. Since the precoditions guarantee
// that the outcome is less than 2**256, this is the final result.
// We don't need to compute the high bits of the result and prod1
// is no longer required.
result = prod0 * inv;
return result;
}
}
/// @notice Calculates ceil(a×b÷denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
/// @param a The multiplicand
/// @param b The multiplier
/// @param denominator The divisor
/// @return result The 256-bit result
function mulDivRoundingUp(
uint256 a,
uint256 b,
uint256 denominator
) internal pure returns (uint256 result) {
result = mulDiv(a, b, denominator);
unchecked {
if (mulmod(a, b, denominator) > 0) {
require(result < type(uint256).max);
result++;
}
}
}
}
IUniswapV3PoolImmutables.sol 35 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Pool state that never changes
/// @notice These parameters are fixed for a pool forever, i.e., the methods will always return the same values
interface IUniswapV3PoolImmutables {
/// @notice The contract that deployed the pool, which must adhere to the IUniswapV3Factory interface
/// @return The contract address
function factory() external view returns (address);
/// @notice The first of the two tokens of the pool, sorted by address
/// @return The token contract address
function token0() external view returns (address);
/// @notice The second of the two tokens of the pool, sorted by address
/// @return The token contract address
function token1() external view returns (address);
/// @notice The pool's fee in hundredths of a bip, i.e. 1e-6
/// @return The fee
function fee() external view returns (uint24);
/// @notice The pool tick spacing
/// @dev Ticks can only be used at multiples of this value, minimum of 1 and always positive
/// e.g.: a tickSpacing of 3 means ticks can be initialized every 3rd tick, i.e., ..., -6, -3, 0, 3, 6, ...
/// This value is an int24 to avoid casting even though it is always positive.
/// @return The tick spacing
function tickSpacing() external view returns (int24);
/// @notice The maximum amount of position liquidity that can use any tick in the range
/// @dev This parameter is enforced per tick to prevent liquidity from overflowing a uint128 at any point, and
/// also prevents out-of-range liquidity from being used to prevent adding in-range liquidity to a pool
/// @return The max amount of liquidity per tick
function maxLiquidityPerTick() external view returns (uint128);
}
OracleLibrary.sol 62 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.8.0;
import {IUniswapV3Pool} from "v3-core/contracts/interfaces/IUniswapV3Pool.sol";
import {TickMath} from "fullrange/libraries/TickMath.sol";
import {FullMath} from "fullrange/libraries/FullMath.sol";
library OracleLibrary {
/// from https://github.com/Uniswap/v3-periphery/blob/main/contracts/libraries/OracleLibrary.sol#L49
function getQuoteAtTick(int24 tick, uint128 baseAmount, address baseToken, address quoteToken)
internal
pure
returns (uint256 quoteAmount)
{
unchecked {
uint160 sqrtRatioX96 = TickMath.getSqrtRatioAtTick(tick);
// Calculate quoteAmount with better precision if it doesn't overflow when multiplied by itself
if (sqrtRatioX96 <= type(uint128).max) {
uint256 ratioX192 = uint256(sqrtRatioX96) * sqrtRatioX96;
quoteAmount = baseToken < quoteToken
? FullMath.mulDiv(ratioX192, baseAmount, 1 << 192)
: FullMath.mulDiv(1 << 192, baseAmount, ratioX192);
} else {
uint256 ratioX128 = FullMath.mulDiv(sqrtRatioX96, sqrtRatioX96, 1 << 64);
quoteAmount = baseToken < quoteToken
? FullMath.mulDiv(ratioX128, baseAmount, 1 << 128)
: FullMath.mulDiv(1 << 128, baseAmount, ratioX128);
}
}
}
// adapted from https://github.com/Uniswap/v3-periphery/blob/main/contracts/libraries/OracleLibrary.sol#L30-L40
function timeWeightedAverageTick(int56 startTick, int56 endTick, int56 twapDuration)
internal
pure
returns (int24 twat)
{
require(twapDuration != 0, "BP");
unchecked {
int56 delta = endTick - startTick;
twat = int24(delta / twapDuration);
// Always round to negative infinity
if (delta < 0 && (delta % (twapDuration) != 0)) {
twat--;
}
twat;
}
}
// adapted from https://github.com/Uniswap/v3-periphery/blob/main/contracts/libraries/OracleLibrary.sol#L21-L28
function latestCumulativeTick(address pool) internal view returns (int56) {
uint32[] memory secondAgos = new uint32[](1);
secondAgos[0] = 0;
(int56[] memory tickCumulatives,) = IUniswapV3Pool(pool).observe(secondAgos);
return tickCumulatives[0];
}
}
Multicallable.sol 63 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/// @notice Contract that enables a single call to call multiple methods on itself.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/Multicallable.sol)
/// @author Modified from Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/Multicallable.sol)
/// @dev WARNING!
/// Multicallable is NOT SAFE for use in contracts with checks / requires on `msg.value`
/// (e.g. in NFT minting / auction contracts) without a suitable nonce mechanism.
/// It WILL open up your contract to double-spend vulnerabilities / exploits.
/// See: (https://www.paradigm.xyz/2021/08/two-rights-might-make-a-wrong/)
abstract contract Multicallable {
/// @dev Apply `DELEGATECALL` with the current contract to each calldata in `data`,
/// and store the `abi.encode` formatted results of each `DELEGATECALL` into `results`.
/// If any of the `DELEGATECALL`s reverts, the entire transaction is reverted,
/// and the error is bubbled up.
function multicall(bytes[] calldata data) public payable returns (bytes[] memory results) {
assembly {
if data.length {
results := mload(0x40) // Point `results` to start of free memory.
mstore(results, data.length) // Store `data.length` into `results`.
results := add(results, 0x20)
// `shl` 5 is equivalent to multiplying by 0x20.
let end := shl(5, data.length)
// Copy the offsets from calldata into memory.
calldatacopy(results, data.offset, end)
// Pointer to the top of the memory (i.e. start of the free memory).
let memPtr := add(results, end)
end := add(results, end)
for {} 1 {} {
// The offset of the current bytes in the calldata.
let o := add(data.offset, mload(results))
// Copy the current bytes from calldata to the memory.
calldatacopy(
memPtr,
add(o, 0x20), // The offset of the current bytes' bytes.
calldataload(o) // The length of the current bytes.
)
if iszero(delegatecall(gas(), address(), memPtr, calldataload(o), 0x00, 0x00)) {
// Bubble up the revert if the delegatecall reverts.
returndatacopy(0x00, 0x00, returndatasize())
revert(0x00, returndatasize())
}
// Append the current `memPtr` into `results`.
mstore(results, memPtr)
results := add(results, 0x20)
// Append the `returndatasize()`, and the return data.
mstore(memPtr, returndatasize())
returndatacopy(add(memPtr, 0x20), 0x00, returndatasize())
// Advance the `memPtr` by `returndatasize() + 0x20`,
// rounded up to the next multiple of 32.
memPtr := and(add(add(memPtr, returndatasize()), 0x3f), 0xffffffffffffffe0)
if iszero(lt(results, end)) { break }
}
// Restore `results` and allocate memory for it.
results := mload(0x40)
mstore(0x40, memPtr)
}
}
}
}
ERC721.sol 231 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
/// @notice Modern, minimalist, and gas efficient ERC-721 implementation.
/// @author Solmate (https://github.com/Rari-Capital/solmate/blob/main/src/tokens/ERC721.sol)
abstract contract ERC721 {
/*//////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
event Transfer(address indexed from, address indexed to, uint256 indexed id);
event Approval(address indexed owner, address indexed spender, uint256 indexed id);
event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
/*//////////////////////////////////////////////////////////////
METADATA STORAGE/LOGIC
//////////////////////////////////////////////////////////////*/
string public name;
string public symbol;
function tokenURI(uint256 id) public view virtual returns (string memory);
/*//////////////////////////////////////////////////////////////
ERC721 BALANCE/OWNER STORAGE
//////////////////////////////////////////////////////////////*/
mapping(uint256 => address) internal _ownerOf;
mapping(address => uint256) internal _balanceOf;
function ownerOf(uint256 id) public view virtual returns (address owner) {
require((owner = _ownerOf[id]) != address(0), "NOT_MINTED");
}
function balanceOf(address owner) public view virtual returns (uint256) {
require(owner != address(0), "ZERO_ADDRESS");
return _balanceOf[owner];
}
/*//////////////////////////////////////////////////////////////
ERC721 APPROVAL STORAGE
//////////////////////////////////////////////////////////////*/
mapping(uint256 => address) public getApproved;
mapping(address => mapping(address => bool)) public isApprovedForAll;
/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
constructor(string memory _name, string memory _symbol) {
name = _name;
symbol = _symbol;
}
/*//////////////////////////////////////////////////////////////
ERC721 LOGIC
//////////////////////////////////////////////////////////////*/
function approve(address spender, uint256 id) public virtual {
address owner = _ownerOf[id];
require(msg.sender == owner || isApprovedForAll[owner][msg.sender], "NOT_AUTHORIZED");
getApproved[id] = spender;
emit Approval(owner, spender, id);
}
function setApprovalForAll(address operator, bool approved) public virtual {
isApprovedForAll[msg.sender][operator] = approved;
emit ApprovalForAll(msg.sender, operator, approved);
}
function transferFrom(
address from,
address to,
uint256 id
) public virtual {
require(from == _ownerOf[id], "WRONG_FROM");
require(to != address(0), "INVALID_RECIPIENT");
require(
msg.sender == from || isApprovedForAll[from][msg.sender] || msg.sender == getApproved[id],
"NOT_AUTHORIZED"
);
// Underflow of the sender's balance is impossible because we check for
// ownership above and the recipient's balance can't realistically overflow.
unchecked {
_balanceOf[from]--;
_balanceOf[to]++;
}
_ownerOf[id] = to;
delete getApproved[id];
emit Transfer(from, to, id);
}
function safeTransferFrom(
address from,
address to,
uint256 id
) public virtual {
transferFrom(from, to, id);
if (to.code.length != 0)
require(
ERC721TokenReceiver(to).onERC721Received(msg.sender, from, id, "") ==
ERC721TokenReceiver.onERC721Received.selector,
"UNSAFE_RECIPIENT"
);
}
function safeTransferFrom(
address from,
address to,
uint256 id,
bytes calldata data
) public virtual {
transferFrom(from, to, id);
if (to.code.length != 0)
require(
ERC721TokenReceiver(to).onERC721Received(msg.sender, from, id, data) ==
ERC721TokenReceiver.onERC721Received.selector,
"UNSAFE_RECIPIENT"
);
}
/*//////////////////////////////////////////////////////////////
ERC165 LOGIC
//////////////////////////////////////////////////////////////*/
function supportsInterface(bytes4 interfaceId) public view virtual returns (bool) {
return
interfaceId == 0x01ffc9a7 || // ERC165 Interface ID for ERC165
interfaceId == 0x80ac58cd || // ERC165 Interface ID for ERC721
interfaceId == 0x5b5e139f; // ERC165 Interface ID for ERC721Metadata
}
/*//////////////////////////////////////////////////////////////
INTERNAL MINT/BURN LOGIC
//////////////////////////////////////////////////////////////*/
function _mint(address to, uint256 id) internal virtual {
require(to != address(0), "INVALID_RECIPIENT");
require(_ownerOf[id] == address(0), "ALREADY_MINTED");
// Counter overflow is incredibly unrealistic.
unchecked {
_balanceOf[to]++;
}
_ownerOf[id] = to;
emit Transfer(address(0), to, id);
}
function _burn(uint256 id) internal virtual {
address owner = _ownerOf[id];
require(owner != address(0), "NOT_MINTED");
// Ownership check above ensures no underflow.
unchecked {
_balanceOf[owner]--;
}
delete _ownerOf[id];
delete getApproved[id];
emit Transfer(owner, address(0), id);
}
/*//////////////////////////////////////////////////////////////
INTERNAL SAFE MINT LOGIC
//////////////////////////////////////////////////////////////*/
function _safeMint(address to, uint256 id) internal virtual {
_mint(to, id);
if (to.code.length != 0)
require(
ERC721TokenReceiver(to).onERC721Received(msg.sender, address(0), id, "") ==
ERC721TokenReceiver.onERC721Received.selector,
"UNSAFE_RECIPIENT"
);
}
function _safeMint(
address to,
uint256 id,
bytes memory data
) internal virtual {
_mint(to, id);
if (to.code.length != 0)
require(
ERC721TokenReceiver(to).onERC721Received(msg.sender, address(0), id, data) ==
ERC721TokenReceiver.onERC721Received.selector,
"UNSAFE_RECIPIENT"
);
}
}
/// @notice A generic interface for a contract which properly accepts ERC721 tokens.
/// @author Solmate (https://github.com/Rari-Capital/solmate/blob/main/src/tokens/ERC721.sol)
abstract contract ERC721TokenReceiver {
function onERC721Received(
address,
address,
uint256,
bytes calldata
) external virtual returns (bytes4) {
return ERC721TokenReceiver.onERC721Received.selector;
}
}
SafeTransferLib.sol 129 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
import {ERC20} from "../tokens/ERC20.sol";
/// @notice Safe ETH and ERC20 transfer library that gracefully handles missing return values.
/// @author Solmate (https://github.com/Rari-Capital/solmate/blob/main/src/utils/SafeTransferLib.sol)
/// @dev Caution! This library won't check that a token has code, responsibility is delegated to the caller.
library SafeTransferLib {
/*//////////////////////////////////////////////////////////////
ETH OPERATIONS
//////////////////////////////////////////////////////////////*/
function safeTransferETH(address to, uint256 amount) internal {
bool success;
assembly {
// Transfer the ETH and store if it succeeded or not.
success := call(gas(), to, amount, 0, 0, 0, 0)
}
require(success, "ETH_TRANSFER_FAILED");
}
/*//////////////////////////////////////////////////////////////
ERC20 OPERATIONS
//////////////////////////////////////////////////////////////*/
function safeTransferFrom(
ERC20 token,
address from,
address to,
uint256 amount
) internal {
bool success;
assembly {
// We'll write our calldata to this slot below, but restore it later.
let memPointer := mload(0x40)
// Write the abi-encoded calldata into memory, beginning with the function selector.
mstore(0, 0x23b872dd00000000000000000000000000000000000000000000000000000000)
mstore(4, from) // Append the "from" argument.
mstore(36, to) // Append the "to" argument.
mstore(68, amount) // Append the "amount" argument.
success := and(
// Set success to whether the call reverted, if not we check it either
// returned exactly 1 (can't just be non-zero data), or had no return data.
or(and(eq(mload(0), 1), gt(returndatasize(), 31)), iszero(returndatasize())),
// We use 100 because that's the total length of our calldata (4 + 32 * 3)
// Counterintuitively, this call() must be positioned after the or() in the
// surrounding and() because and() evaluates its arguments from right to left.
call(gas(), token, 0, 0, 100, 0, 32)
)
mstore(0x60, 0) // Restore the zero slot to zero.
mstore(0x40, memPointer) // Restore the memPointer.
}
require(success, "TRANSFER_FROM_FAILED");
}
function safeTransfer(
ERC20 token,
address to,
uint256 amount
) internal {
bool success;
assembly {
// We'll write our calldata to this slot below, but restore it later.
let memPointer := mload(0x40)
// Write the abi-encoded calldata into memory, beginning with the function selector.
mstore(0, 0xa9059cbb00000000000000000000000000000000000000000000000000000000)
mstore(4, to) // Append the "to" argument.
mstore(36, amount) // Append the "amount" argument.
success := and(
// Set success to whether the call reverted, if not we check it either
// returned exactly 1 (can't just be non-zero data), or had no return data.
or(and(eq(mload(0), 1), gt(returndatasize(), 31)), iszero(returndatasize())),
// We use 68 because that's the total length of our calldata (4 + 32 * 2)
// Counterintuitively, this call() must be positioned after the or() in the
// surrounding and() because and() evaluates its arguments from right to left.
call(gas(), token, 0, 0, 68, 0, 32)
)
mstore(0x60, 0) // Restore the zero slot to zero.
mstore(0x40, memPointer) // Restore the memPointer.
}
require(success, "TRANSFER_FAILED");
}
function safeApprove(
ERC20 token,
address to,
uint256 amount
) internal {
bool success;
assembly {
// We'll write our calldata to this slot below, but restore it later.
let memPointer := mload(0x40)
// Write the abi-encoded calldata into memory, beginning with the function selector.
mstore(0, 0x095ea7b300000000000000000000000000000000000000000000000000000000)
mstore(4, to) // Append the "to" argument.
mstore(36, amount) // Append the "amount" argument.
success := and(
// Set success to whether the call reverted, if not we check it either
// returned exactly 1 (can't just be non-zero data), or had no return data.
or(and(eq(mload(0), 1), gt(returndatasize(), 31)), iszero(returndatasize())),
// We use 68 because that's the total length of our calldata (4 + 32 * 2)
// Counterintuitively, this call() must be positioned after the or() in the
// surrounding and() because and() evaluates its arguments from right to left.
call(gas(), token, 0, 0, 68, 0, 32)
)
mstore(0x60, 0) // Restore the zero slot to zero.
mstore(0x40, memPointer) // Restore the memPointer.
}
require(success, "APPROVE_FAILED");
}
}
IUniswapV3PoolEvents.sol 121 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Events emitted by a pool
/// @notice Contains all events emitted by the pool
interface IUniswapV3PoolEvents {
/// @notice Emitted exactly once by a pool when #initialize is first called on the pool
/// @dev Mint/Burn/Swap cannot be emitted by the pool before Initialize
/// @param sqrtPriceX96 The initial sqrt price of the pool, as a Q64.96
/// @param tick The initial tick of the pool, i.e. log base 1.0001 of the starting price of the pool
event Initialize(uint160 sqrtPriceX96, int24 tick);
/// @notice Emitted when liquidity is minted for a given position
/// @param sender The address that minted the liquidity
/// @param owner The owner of the position and recipient of any minted liquidity
/// @param tickLower The lower tick of the position
/// @param tickUpper The upper tick of the position
/// @param amount The amount of liquidity minted to the position range
/// @param amount0 How much token0 was required for the minted liquidity
/// @param amount1 How much token1 was required for the minted liquidity
event Mint(
address sender,
address indexed owner,
int24 indexed tickLower,
int24 indexed tickUpper,
uint128 amount,
uint256 amount0,
uint256 amount1
);
/// @notice Emitted when fees are collected by the owner of a position
/// @dev Collect events may be emitted with zero amount0 and amount1 when the caller chooses not to collect fees
/// @param owner The owner of the position for which fees are collected
/// @param tickLower The lower tick of the position
/// @param tickUpper The upper tick of the position
/// @param amount0 The amount of token0 fees collected
/// @param amount1 The amount of token1 fees collected
event Collect(
address indexed owner,
address recipient,
int24 indexed tickLower,
int24 indexed tickUpper,
uint128 amount0,
uint128 amount1
);
/// @notice Emitted when a position's liquidity is removed
/// @dev Does not withdraw any fees earned by the liquidity position, which must be withdrawn via #collect
/// @param owner The owner of the position for which liquidity is removed
/// @param tickLower The lower tick of the position
/// @param tickUpper The upper tick of the position
/// @param amount The amount of liquidity to remove
/// @param amount0 The amount of token0 withdrawn
/// @param amount1 The amount of token1 withdrawn
event Burn(
address indexed owner,
int24 indexed tickLower,
int24 indexed tickUpper,
uint128 amount,
uint256 amount0,
uint256 amount1
);
/// @notice Emitted by the pool for any swaps between token0 and token1
/// @param sender The address that initiated the swap call, and that received the callback
/// @param recipient The address that received the output of the swap
/// @param amount0 The delta of the token0 balance of the pool
/// @param amount1 The delta of the token1 balance of the pool
/// @param sqrtPriceX96 The sqrt(price) of the pool after the swap, as a Q64.96
/// @param liquidity The liquidity of the pool after the swap
/// @param tick The log base 1.0001 of price of the pool after the swap
event Swap(
address indexed sender,
address indexed recipient,
int256 amount0,
int256 amount1,
uint160 sqrtPriceX96,
uint128 liquidity,
int24 tick
);
/// @notice Emitted by the pool for any flashes of token0/token1
/// @param sender The address that initiated the swap call, and that received the callback
/// @param recipient The address that received the tokens from flash
/// @param amount0 The amount of token0 that was flashed
/// @param amount1 The amount of token1 that was flashed
/// @param paid0 The amount of token0 paid for the flash, which can exceed the amount0 plus the fee
/// @param paid1 The amount of token1 paid for the flash, which can exceed the amount1 plus the fee
event Flash(
address indexed sender,
address indexed recipient,
uint256 amount0,
uint256 amount1,
uint256 paid0,
uint256 paid1
);
/// @notice Emitted by the pool for increases to the number of observations that can be stored
/// @dev observationCardinalityNext is not the observation cardinality until an observation is written at the index
/// just before a mint/swap/burn.
/// @param observationCardinalityNextOld The previous value of the next observation cardinality
/// @param observationCardinalityNextNew The updated value of the next observation cardinality
event IncreaseObservationCardinalityNext(
uint16 observationCardinalityNextOld,
uint16 observationCardinalityNextNew
);
/// @notice Emitted when the protocol fee is changed by the pool
/// @param feeProtocol0Old The previous value of the token0 protocol fee
/// @param feeProtocol1Old The previous value of the token1 protocol fee
/// @param feeProtocol0New The updated value of the token0 protocol fee
/// @param feeProtocol1New The updated value of the token1 protocol fee
event SetFeeProtocol(uint8 feeProtocol0Old, uint8 feeProtocol1Old, uint8 feeProtocol0New, uint8 feeProtocol1New);
/// @notice Emitted when the collected protocol fees are withdrawn by the factory owner
/// @param sender The address that collects the protocol fees
/// @param recipient The address that receives the collected protocol fees
/// @param amount0 The amount of token0 protocol fees that is withdrawn
/// @param amount0 The amount of token1 protocol fees that is withdrawn
event CollectProtocol(address indexed sender, address indexed recipient, uint128 amount0, uint128 amount1);
}
NFTEDAStarterIncentive.sol 78 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
import {FixedPointMathLib} from "solmate/utils/FixedPointMathLib.sol";
import {INFTEDA} from "../interfaces/INFTEDA.sol";
import {NFTEDA} from "../NFTEDA.sol";
contract NFTEDAStarterIncentive is NFTEDA {
struct AuctionState {
/// @dev the time the auction started
uint96 startTime;
/// @dev who called to start the auction
address starter;
}
/// @notice emitted when auction creator discount is set
/// @param discount the new auction creator discount
/// expressed as a percent scaled by 1e18
/// i.e. 1e18 = 100%
event SetAuctionCreatorDiscount(uint256 discount);
/// @notice The percent discount the creator of an auction should
/// receive, compared to the current price
/// 1e18 = 100%
uint256 public auctionCreatorDiscountPercentWad;
uint256 internal _pricePercentAfterDiscount;
/// @notice returns the auction state for a given auctionID
/// @dev auctionID => AuctionState
mapping(uint256 => AuctionState) public auctionState;
constructor(uint256 _auctionCreatorDiscountPercentWad) {
_setCreatorDiscount(_auctionCreatorDiscountPercentWad);
}
/// @inheritdoc INFTEDA
function auctionStartTime(uint256 id) public view virtual override returns (uint256) {
return auctionState[id].startTime;
}
/// @inheritdoc NFTEDA
function _setAuctionStartTime(uint256 id) internal virtual override {
auctionState[id] = AuctionState({startTime: uint96(block.timestamp), starter: msg.sender});
}
/// @inheritdoc NFTEDA
function _clearAuctionState(uint256 id) internal virtual override {
delete auctionState[id];
}
/// @inheritdoc NFTEDA
function _auctionCurrentPrice(uint256 id, uint256 startTime, INFTEDA.Auction memory auction)
internal
view
virtual
override
returns (uint256)
{
uint256 price = super._auctionCurrentPrice(id, startTime, auction);
if (msg.sender == auctionState[id].starter) {
price = FixedPointMathLib.mulWadUp(price, _pricePercentAfterDiscount);
}
return price;
}
/// @notice sets the discount offered to auction creators
/// @param _auctionCreatorDiscountPercentWad the percent off the spot auction price the creator will be offered
/// scaled by 1e18, i.e. 1e18 = 1 = 100%
function _setCreatorDiscount(uint256 _auctionCreatorDiscountPercentWad) internal virtual {
auctionCreatorDiscountPercentWad = _auctionCreatorDiscountPercentWad;
_pricePercentAfterDiscount = FixedPointMathLib.WAD - _auctionCreatorDiscountPercentWad;
emit SetAuctionCreatorDiscount(_auctionCreatorDiscountPercentWad);
}
}
ReservoirOracle.sol 105 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
// Inspired by https://github.com/ZeframLou/trustus
abstract contract ReservoirOracle {
// --- Structs ---
struct Message {
bytes32 id;
bytes payload;
// The UNIX timestamp when the message was signed by the oracle
uint256 timestamp;
// ECDSA signature or EIP-2098 compact signature
bytes signature;
}
// --- Errors ---
error InvalidMessage();
// --- Fields ---
address immutable RESERVOIR_ORACLE_ADDRESS;
// --- Constructor ---
constructor(address reservoirOracleAddress) {
RESERVOIR_ORACLE_ADDRESS = reservoirOracleAddress;
}
// --- Internal methods ---
function _verifyMessage(
bytes32 id,
uint256 validFor,
Message memory message
) internal view virtual returns (bool success) {
// Ensure the message matches the requested id
if (id != message.id) {
return false;
}
// Ensure the message timestamp is valid
if (
message.timestamp > block.timestamp ||
message.timestamp + validFor < block.timestamp
) {
return false;
}
bytes32 r;
bytes32 s;
uint8 v;
// Extract the individual signature fields from the signature
bytes memory signature = message.signature;
if (signature.length == 64) {
// EIP-2098 compact signature
bytes32 vs;
assembly {
r := mload(add(signature, 0x20))
vs := mload(add(signature, 0x40))
s := and(
vs,
0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
)
v := add(shr(255, vs), 27)
}
} else if (signature.length == 65) {
// ECDSA signature
assembly {
r := mload(add(signature, 0x20))
s := mload(add(signature, 0x40))
v := byte(0, mload(add(signature, 0x60)))
}
} else {
return false;
}
address signerAddress = ecrecover(
keccak256(
abi.encodePacked(
"\x19Ethereum Signed Message:\n32",
// EIP-712 structured-data hash
keccak256(
abi.encode(
keccak256(
"Message(bytes32 id,bytes payload,uint256 timestamp)"
),
message.id,
keccak256(message.payload),
message.timestamp
)
)
)
),
v,
r,
s
);
// Ensure the signer matches the designated oracle address
return signerAddress == RESERVOIR_ORACLE_ADDRESS;
}
}
IUniswapV3PoolDerivedState.sol 40 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Pool state that is not stored
/// @notice Contains view functions to provide information about the pool that is computed rather than stored on the
/// blockchain. The functions here may have variable gas costs.
interface IUniswapV3PoolDerivedState {
/// @notice Returns the cumulative tick and liquidity as of each timestamp `secondsAgo` from the current block timestamp
/// @dev To get a time weighted average tick or liquidity-in-range, you must call this with two values, one representing
/// the beginning of the period and another for the end of the period. E.g., to get the last hour time-weighted average tick,
/// you must call it with secondsAgos = [3600, 0].
/// @dev The time weighted average tick represents the geometric time weighted average price of the pool, in
/// log base sqrt(1.0001) of token1 / token0. The TickMath library can be used to go from a tick value to a ratio.
/// @param secondsAgos From how long ago each cumulative tick and liquidity value should be returned
/// @return tickCumulatives Cumulative tick values as of each `secondsAgos` from the current block timestamp
/// @return secondsPerLiquidityCumulativeX128s Cumulative seconds per liquidity-in-range value as of each `secondsAgos` from the current block
/// timestamp
function observe(uint32[] calldata secondsAgos)
external
view
returns (int56[] memory tickCumulatives, uint160[] memory secondsPerLiquidityCumulativeX128s);
/// @notice Returns a snapshot of the tick cumulative, seconds per liquidity and seconds inside a tick range
/// @dev Snapshots must only be compared to other snapshots, taken over a period for which a position existed.
/// I.e., snapshots cannot be compared if a position is not held for the entire period between when the first
/// snapshot is taken and the second snapshot is taken.
/// @param tickLower The lower tick of the range
/// @param tickUpper The upper tick of the range
/// @return tickCumulativeInside The snapshot of the tick accumulator for the range
/// @return secondsPerLiquidityInsideX128 The snapshot of seconds per liquidity for the range
/// @return secondsInside The snapshot of seconds per liquidity for the range
function snapshotCumulativesInside(int24 tickLower, int24 tickUpper)
external
view
returns (
int56 tickCumulativeInside,
uint160 secondsPerLiquidityInsideX128,
uint32 secondsInside
);
}
UniswapHelpers.sol 121 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.8.0;
import {IUniswapV3Pool} from "v3-core/contracts/interfaces/IUniswapV3Pool.sol";
import {IUniswapV3Factory} from "v3-core/contracts/interfaces/IUniswapV3Factory.sol";
import {TickMath} from "fullrange/libraries/TickMath.sol";
import {FullMath} from "fullrange/libraries/FullMath.sol";
import {SafeCast} from "v3-core/contracts/libraries/SafeCast.sol";
import {PoolAddress} from "./PoolAddress.sol";
library UniswapHelpers {
using SafeCast for uint256;
/// @param minOut The minimum out amount the user wanted
/// @param actualOut The actual out amount the user received
error TooLittleOut(uint256 minOut, uint256 actualOut);
/// @param deadline The minimum out amount the user wanted
/// @param currentTimestamp The actual out amount the user received
error PassedDeadline(uint256 deadline, uint256 currentTimestamp);
IUniswapV3Factory constant FACTORY = IUniswapV3Factory(0x1F98431c8aD98523631AE4a59f267346ea31F984);
/// @notice executes a swap on the Uniswap
/// @param pool The pool to swap on
/// @param recipient The address to send the output to
/// @param zeroForOne Whether to swap token0 for token1 or vice versa
/// @param amountSpecified The amount of token0 or token1 to swap
/// @param minOut The minimum amount of token0 or token1 to receive
/// @param sqrtPriceLimitX96 The price limit for the swap
/// @param deadline timestamp after which the swap should revert
/// @param data Any data to pass to the uniswap callback handler
/// @return amountOut The amount of token0 or token1 received
/// @return amountIn The amount of token0 or token1 sent
function swap(
address pool,
address recipient,
bool zeroForOne,
uint256 amountSpecified,
uint256 minOut,
uint160 sqrtPriceLimitX96,
uint256 deadline,
bytes memory data
) internal returns (uint256 amountOut, uint256 amountIn) {
if (block.timestamp > deadline) revert PassedDeadline(deadline, block.timestamp);
(int256 amount0, int256 amount1) = IUniswapV3Pool(pool).swap(
recipient,
zeroForOne,
amountSpecified.toInt256(),
sqrtPriceLimitX96 == 0
? (zeroForOne ? TickMath.MIN_SQRT_RATIO + 1 : TickMath.MAX_SQRT_RATIO - 1)
: sqrtPriceLimitX96,
data
);
if (zeroForOne) {
amountOut = uint256(-amount1);
amountIn = uint256(amount0);
} else {
amountOut = uint256(-amount0);
amountIn = uint256(amount1);
}
if (amountOut < minOut) {
revert TooLittleOut(amountOut, minOut);
}
}
/// @notice initializes a UniswapV3 pool with the given sqrt ratio
/// @param tokenA the first token in the pool
/// @param tokenB the second token in the pool
/// @param feeTier the fee tier of the pool
/// @param sqrtRatio the sqrt ratio to initialize the pool with
/// @return pool the address of the newly created pool
function deployAndInitPool(address tokenA, address tokenB, uint24 feeTier, uint160 sqrtRatio)
internal
returns (address)
{
IUniswapV3Pool pool = IUniswapV3Pool(FACTORY.createPool(tokenA, tokenB, feeTier));
pool.initialize(sqrtRatio);
return address(pool);
}
/// @notice returns the current price tick of a UniswapV3 pool
/// @param pool the address of the pool
/// @return tick the current price tick of the pool
function poolCurrentTick(address pool) internal view returns (int24) {
(, int24 tick,,,,,) = IUniswapV3Pool(pool).slot0();
return tick;
}
/// @notice returns whether or not two pools have the same tokens
/// @param pool1 the first pool
/// @param pool2 the second pool
/// @return same whether or not the two pools have the same tokens
function poolsHaveSameTokens(address pool1, address pool2) internal view returns (bool) {
return IUniswapV3Pool(pool1).token0() == IUniswapV3Pool(pool2).token0()
&& IUniswapV3Pool(pool1).token1() == IUniswapV3Pool(pool2).token1();
}
/// @notice returns whether or not a pool is a UniswapV3 pool
/// @param pool the address of the pool
/// @return isUniswapPool whether or not the pool is a UniswapV3 pool
function isUniswapPool(address pool) internal view returns (bool) {
IUniswapV3Pool p = IUniswapV3Pool(pool);
PoolAddress.PoolKey memory k = PoolAddress.getPoolKey(p.token0(), p.token1(), p.fee());
return pool == PoolAddress.computeAddress(address(FACTORY), k);
}
/// @notice returns the sqrt ratio at which token0 and token1 are trading at 1:1
/// @param token0ONE 10 ** token0.decimals()
/// @param token1ONE 10 ** token1.decimals()
/// @return sqrtRatio at which token0 and token1 are trading at 1:1
function oneToOneSqrtRatio(uint256 token0ONE, uint256 token1ONE) internal pure returns (uint160) {
return TickMath.getSqrtRatioAtTick(TickMath.getTickAtSqrtRatio(uint160((token1ONE << 96) / token0ONE)) / 2);
}
}
Ownable2Step.sol 57 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (access/Ownable2Step.sol)
pragma solidity ^0.8.0;
import "./Ownable.sol";
/**
* @dev Contract module which provides access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* By default, the owner account will be the one that deploys the contract. This
* can later be changed with {transferOwnership} and {acceptOwnership}.
*
* This module is used through inheritance. It will make available all functions
* from parent (Ownable).
*/
abstract contract Ownable2Step is Ownable {
address private _pendingOwner;
event OwnershipTransferStarted(address indexed previousOwner, address indexed newOwner);
/**
* @dev Returns the address of the pending owner.
*/
function pendingOwner() public view virtual returns (address) {
return _pendingOwner;
}
/**
* @dev Starts the ownership transfer of the contract to a new account. Replaces the pending transfer if there is one.
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual override onlyOwner {
_pendingOwner = newOwner;
emit OwnershipTransferStarted(owner(), newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`) and deletes any pending owner.
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual override {
delete _pendingOwner;
super._transferOwnership(newOwner);
}
/**
* @dev The new owner accepts the ownership transfer.
*/
function acceptOwnership() external {
address sender = _msgSender();
require(pendingOwner() == sender, "Ownable2Step: caller is not the new owner");
_transferOwnership(sender);
}
}
FixedPointMathLib.sol 388 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
/// @notice Arithmetic library with operations for fixed-point numbers.
/// @author Solmate (https://github.com/Rari-Capital/solmate/blob/main/src/utils/FixedPointMathLib.sol)
library FixedPointMathLib {
/*//////////////////////////////////////////////////////////////
SIMPLIFIED FIXED POINT OPERATIONS
//////////////////////////////////////////////////////////////*/
uint256 internal constant WAD = 1e18; // The scalar of ETH and most ERC20s.
function mulWadDown(uint256 x, uint256 y) internal pure returns (uint256) {
return mulDivDown(x, y, WAD); // Equivalent to (x * y) / WAD rounded down.
}
function mulWadUp(uint256 x, uint256 y) internal pure returns (uint256) {
return mulDivUp(x, y, WAD); // Equivalent to (x * y) / WAD rounded up.
}
function divWadDown(uint256 x, uint256 y) internal pure returns (uint256) {
return mulDivDown(x, WAD, y); // Equivalent to (x * WAD) / y rounded down.
}
function divWadUp(uint256 x, uint256 y) internal pure returns (uint256) {
return mulDivUp(x, WAD, y); // Equivalent to (x * WAD) / y rounded up.
}
function powWad(int256 x, int256 y) internal pure returns (int256) {
// Equivalent to x to the power of y because x ** y = (e ** ln(x)) ** y = e ** (ln(x) * y)
return expWad((lnWad(x) * y) / int256(WAD)); // Using ln(x) means x must be greater than 0.
}
function expWad(int256 x) internal pure returns (int256 r) {
unchecked {
// When the result is < 0.5 we return zero. This happens when
// x <= floor(log(0.5e18) * 1e18) ~ -42e18
if (x <= -42139678854452767551) return 0;
// When the result is > (2**255 - 1) / 1e18 we can not represent it as an
// int. This happens when x >= floor(log((2**255 - 1) / 1e18) * 1e18) ~ 135.
if (x >= 135305999368893231589) revert("EXP_OVERFLOW");
// x is now in the range (-42, 136) * 1e18. Convert to (-42, 136) * 2**96
// for more intermediate precision and a binary basis. This base conversion
// is a multiplication by 1e18 / 2**96 = 5**18 / 2**78.
x = (x << 78) / 5**18;
// Reduce range of x to (-½ ln 2, ½ ln 2) * 2**96 by factoring out powers
// of two such that exp(x) = exp(x') * 2**k, where k is an integer.
// Solving this gives k = round(x / log(2)) and x' = x - k * log(2).
int256 k = ((x << 96) / 54916777467707473351141471128 + 2**95) >> 96;
x = x - k * 54916777467707473351141471128;
// k is in the range [-61, 195].
// Evaluate using a (6, 7)-term rational approximation.
// p is made monic, we'll multiply by a scale factor later.
int256 y = x + 1346386616545796478920950773328;
y = ((y * x) >> 96) + 57155421227552351082224309758442;
int256 p = y + x - 94201549194550492254356042504812;
p = ((p * y) >> 96) + 28719021644029726153956944680412240;
p = p * x + (4385272521454847904659076985693276 << 96);
// We leave p in 2**192 basis so we don't need to scale it back up for the division.
int256 q = x - 2855989394907223263936484059900;
q = ((q * x) >> 96) + 50020603652535783019961831881945;
q = ((q * x) >> 96) - 533845033583426703283633433725380;
q = ((q * x) >> 96) + 3604857256930695427073651918091429;
q = ((q * x) >> 96) - 14423608567350463180887372962807573;
q = ((q * x) >> 96) + 26449188498355588339934803723976023;
assembly {
// Div in assembly because solidity adds a zero check despite the unchecked.
// The q polynomial won't have zeros in the domain as all its roots are complex.
// No scaling is necessary because p is already 2**96 too large.
r := sdiv(p, q)
}
// r should be in the range (0.09, 0.25) * 2**96.
// We now need to multiply r by:
// * the scale factor s = ~6.031367120.
// * the 2**k factor from the range reduction.
// * the 1e18 / 2**96 factor for base conversion.
// We do this all at once, with an intermediate result in 2**213
// basis, so the final right shift is always by a positive amount.
r = int256((uint256(r) * 3822833074963236453042738258902158003155416615667) >> uint256(195 - k));
}
}
function lnWad(int256 x) internal pure returns (int256 r) {
unchecked {
require(x > 0, "UNDEFINED");
// We want to convert x from 10**18 fixed point to 2**96 fixed point.
// We do this by multiplying by 2**96 / 10**18. But since
// ln(x * C) = ln(x) + ln(C), we can simply do nothing here
// and add ln(2**96 / 10**18) at the end.
// Reduce range of x to (1, 2) * 2**96
// ln(2^k * x) = k * ln(2) + ln(x)
int256 k = int256(log2(uint256(x))) - 96;
x <<= uint256(159 - k);
x = int256(uint256(x) >> 159);
// Evaluate using a (8, 8)-term rational approximation.
// p is made monic, we will multiply by a scale factor later.
int256 p = x + 3273285459638523848632254066296;
p = ((p * x) >> 96) + 24828157081833163892658089445524;
p = ((p * x) >> 96) + 43456485725739037958740375743393;
p = ((p * x) >> 96) - 11111509109440967052023855526967;
p = ((p * x) >> 96) - 45023709667254063763336534515857;
p = ((p * x) >> 96) - 14706773417378608786704636184526;
p = p * x - (795164235651350426258249787498 << 96);
// We leave p in 2**192 basis so we don't need to scale it back up for the division.
// q is monic by convention.
int256 q = x + 5573035233440673466300451813936;
q = ((q * x) >> 96) + 71694874799317883764090561454958;
q = ((q * x) >> 96) + 283447036172924575727196451306956;
q = ((q * x) >> 96) + 401686690394027663651624208769553;
q = ((q * x) >> 96) + 204048457590392012362485061816622;
q = ((q * x) >> 96) + 31853899698501571402653359427138;
q = ((q * x) >> 96) + 909429971244387300277376558375;
assembly {
// Div in assembly because solidity adds a zero check despite the unchecked.
// The q polynomial is known not to have zeros in the domain.
// No scaling required because p is already 2**96 too large.
r := sdiv(p, q)
}
// r is in the range (0, 0.125) * 2**96
// Finalization, we need to:
// * multiply by the scale factor s = 5.549…
// * add ln(2**96 / 10**18)
// * add k * ln(2)
// * multiply by 10**18 / 2**96 = 5**18 >> 78
// mul s * 5e18 * 2**96, base is now 5**18 * 2**192
r *= 1677202110996718588342820967067443963516166;
// add ln(2) * k * 5e18 * 2**192
r += 16597577552685614221487285958193947469193820559219878177908093499208371 * k;
// add ln(2**96 / 10**18) * 5e18 * 2**192
r += 600920179829731861736702779321621459595472258049074101567377883020018308;
// base conversion: mul 2**18 / 2**192
r >>= 174;
}
}
/*//////////////////////////////////////////////////////////////
LOW LEVEL FIXED POINT OPERATIONS
//////////////////////////////////////////////////////////////*/
function mulDivDown(
uint256 x,
uint256 y,
uint256 denominator
) internal pure returns (uint256 z) {
assembly {
// Store x * y in z for now.
z := mul(x, y)
// Equivalent to require(denominator != 0 && (x == 0 || (x * y) / x == y))
if iszero(and(iszero(iszero(denominator)), or(iszero(x), eq(div(z, x), y)))) {
revert(0, 0)
}
// Divide z by the denominator.
z := div(z, denominator)
}
}
function mulDivUp(
uint256 x,
uint256 y,
uint256 denominator
) internal pure returns (uint256 z) {
assembly {
// Store x * y in z for now.
z := mul(x, y)
// Equivalent to require(denominator != 0 && (x == 0 || (x * y) / x == y))
if iszero(and(iszero(iszero(denominator)), or(iszero(x), eq(div(z, x), y)))) {
revert(0, 0)
}
// First, divide z - 1 by the denominator and add 1.
// We allow z - 1 to underflow if z is 0, because we multiply the
// end result by 0 if z is zero, ensuring we return 0 if z is zero.
z := mul(iszero(iszero(z)), add(div(sub(z, 1), denominator), 1))
}
}
function rpow(
uint256 x,
uint256 n,
uint256 scalar
) internal pure returns (uint256 z) {
assembly {
switch x
case 0 {
switch n
case 0 {
// 0 ** 0 = 1
z := scalar
}
default {
// 0 ** n = 0
z := 0
}
}
default {
switch mod(n, 2)
case 0 {
// If n is even, store scalar in z for now.
z := scalar
}
default {
// If n is odd, store x in z for now.
z := x
}
// Shifting right by 1 is like dividing by 2.
let half := shr(1, scalar)
for {
// Shift n right by 1 before looping to halve it.
n := shr(1, n)
} n {
// Shift n right by 1 each iteration to halve it.
n := shr(1, n)
} {
// Revert immediately if x ** 2 would overflow.
// Equivalent to iszero(eq(div(xx, x), x)) here.
if shr(128, x) {
revert(0, 0)
}
// Store x squared.
let xx := mul(x, x)
// Round to the nearest number.
let xxRound := add(xx, half)
// Revert if xx + half overflowed.
if lt(xxRound, xx) {
revert(0, 0)
}
// Set x to scaled xxRound.
x := div(xxRound, scalar)
// If n is even:
if mod(n, 2) {
// Compute z * x.
let zx := mul(z, x)
// If z * x overflowed:
if iszero(eq(div(zx, x), z)) {
// Revert if x is non-zero.
if iszero(iszero(x)) {
revert(0, 0)
}
}
// Round to the nearest number.
let zxRound := add(zx, half)
// Revert if zx + half overflowed.
if lt(zxRound, zx) {
revert(0, 0)
}
// Return properly scaled zxRound.
z := div(zxRound, scalar)
}
}
}
}
}
/*//////////////////////////////////////////////////////////////
GENERAL NUMBER UTILITIES
//////////////////////////////////////////////////////////////*/
function sqrt(uint256 x) internal pure returns (uint256 z) {
assembly {
let y := x // We start y at x, which will help us make our initial estimate.
z := 181 // The "correct" value is 1, but this saves a multiplication later.
// This segment is to get a reasonable initial estimate for the Babylonian method. With a bad
// start, the correct # of bits increases ~linearly each iteration instead of ~quadratically.
// We check y >= 2^(k + 8) but shift right by k bits
// each branch to ensure that if x >= 256, then y >= 256.
if iszero(lt(y, 0x10000000000000000000000000000000000)) {
y := shr(128, y)
z := shl(64, z)
}
if iszero(lt(y, 0x1000000000000000000)) {
y := shr(64, y)
z := shl(32, z)
}
if iszero(lt(y, 0x10000000000)) {
y := shr(32, y)
z := shl(16, z)
}
if iszero(lt(y, 0x1000000)) {
y := shr(16, y)
z := shl(8, z)
}
// Goal was to get z*z*y within a small factor of x. More iterations could
// get y in a tighter range. Currently, we will have y in [256, 256*2^16).
// We ensured y >= 256 so that the relative difference between y and y+1 is small.
// That's not possible if x < 256 but we can just verify those cases exhaustively.
// Now, z*z*y <= x < z*z*(y+1), and y <= 2^(16+8), and either y >= 256, or x < 256.
// Correctness can be checked exhaustively for x < 256, so we assume y >= 256.
// Then z*sqrt(y) is within sqrt(257)/sqrt(256) of sqrt(x), or about 20bps.
// For s in the range [1/256, 256], the estimate f(s) = (181/1024) * (s+1) is in the range
// (1/2.84 * sqrt(s), 2.84 * sqrt(s)), with largest error when s = 1 and when s = 256 or 1/256.
// Since y is in [256, 256*2^16), let a = y/65536, so that a is in [1/256, 256). Then we can estimate
// sqrt(y) using sqrt(65536) * 181/1024 * (a + 1) = 181/4 * (y + 65536)/65536 = 181 * (y + 65536)/2^18.
// There is no overflow risk here since y < 2^136 after the first branch above.
z := shr(18, mul(z, add(y, 65536))) // A mul() is saved from starting z at 181.
// Given the worst case multiplicative error of 2.84 above, 7 iterations should be enough.
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
z := shr(1, add(z, div(x, z)))
// If x+1 is a perfect square, the Babylonian method cycles between
// floor(sqrt(x)) and ceil(sqrt(x)). This statement ensures we return floor.
// See: https://en.wikipedia.org/wiki/Integer_square_root#Using_only_integer_division
// Since the ceil is rare, we save gas on the assignment and repeat division in the rare case.
// If you don't care whether the floor or ceil square root is returned, you can remove this statement.
z := sub(z, lt(div(x, z), z))
}
}
function log2(uint256 x) internal pure returns (uint256 r) {
require(x > 0, "UNDEFINED");
assembly {
r := shl(7, lt(0xffffffffffffffffffffffffffffffff, x))
r := or(r, shl(6, lt(0xffffffffffffffff, shr(r, x))))
r := or(r, shl(5, lt(0xffffffff, shr(r, x))))
r := or(r, shl(4, lt(0xffff, shr(r, x))))
r := or(r, shl(3, lt(0xff, shr(r, x))))
r := or(r, shl(2, lt(0xf, shr(r, x))))
r := or(r, shl(1, lt(0x3, shr(r, x))))
r := or(r, lt(0x1, shr(r, x)))
}
}
function unsafeMod(uint256 x, uint256 y) internal pure returns (uint256 z) {
assembly {
// z will equal 0 if y is 0, unlike in Solidity where it will revert.
z := mod(x, y)
}
}
function unsafeDiv(uint256 x, uint256 y) internal pure returns (uint256 z) {
assembly {
// z will equal 0 if y is 0, unlike in Solidity where it will revert.
z := div(x, y)
}
}
/// @dev Will return 0 instead of reverting if y is zero.
function unsafeDivUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
assembly {
// Add 1 to x * y if x % y > 0.
z := add(gt(mod(x, y), 0), div(x, y))
}
}
}
SafeCastLib.sol 60 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
/// @notice Safe unsigned integer casting library that reverts on overflow.
/// @author Solmate (https://github.com/Rari-Capital/solmate/blob/main/src/utils/SafeCastLib.sol)
library SafeCastLib {
function safeCastTo248(uint256 x) internal pure returns (uint248 y) {
require(x < 1 << 248);
y = uint248(x);
}
function safeCastTo224(uint256 x) internal pure returns (uint224 y) {
require(x < 1 << 224);
y = uint224(x);
}
function safeCastTo192(uint256 x) internal pure returns (uint192 y) {
require(x < 1 << 192);
y = uint192(x);
}
function safeCastTo160(uint256 x) internal pure returns (uint160 y) {
require(x < 1 << 160);
y = uint160(x);
}
function safeCastTo128(uint256 x) internal pure returns (uint128 y) {
require(x < 1 << 128);
y = uint128(x);
}
function safeCastTo96(uint256 x) internal pure returns (uint96 y) {
require(x < 1 << 96);
y = uint96(x);
}
function safeCastTo64(uint256 x) internal pure returns (uint64 y) {
require(x < 1 << 64);
y = uint64(x);
}
function safeCastTo32(uint256 x) internal pure returns (uint32 y) {
require(x < 1 << 32);
y = uint32(x);
}
function safeCastTo8(uint256 x) internal pure returns (uint8 y) {
require(x < 1 << 8);
y = uint8(x);
}
}
IUniswapV3Factory.sol 78 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title The interface for the Uniswap V3 Factory
/// @notice The Uniswap V3 Factory facilitates creation of Uniswap V3 pools and control over the protocol fees
interface IUniswapV3Factory {
/// @notice Emitted when the owner of the factory is changed
/// @param oldOwner The owner before the owner was changed
/// @param newOwner The owner after the owner was changed
event OwnerChanged(address indexed oldOwner, address indexed newOwner);
/// @notice Emitted when a pool is created
/// @param token0 The first token of the pool by address sort order
/// @param token1 The second token of the pool by address sort order
/// @param fee The fee collected upon every swap in the pool, denominated in hundredths of a bip
/// @param tickSpacing The minimum number of ticks between initialized ticks
/// @param pool The address of the created pool
event PoolCreated(
address indexed token0,
address indexed token1,
uint24 indexed fee,
int24 tickSpacing,
address pool
);
/// @notice Emitted when a new fee amount is enabled for pool creation via the factory
/// @param fee The enabled fee, denominated in hundredths of a bip
/// @param tickSpacing The minimum number of ticks between initialized ticks for pools created with the given fee
event FeeAmountEnabled(uint24 indexed fee, int24 indexed tickSpacing);
/// @notice Returns the current owner of the factory
/// @dev Can be changed by the current owner via setOwner
/// @return The address of the factory owner
function owner() external view returns (address);
/// @notice Returns the tick spacing for a given fee amount, if enabled, or 0 if not enabled
/// @dev A fee amount can never be removed, so this value should be hard coded or cached in the calling context
/// @param fee The enabled fee, denominated in hundredths of a bip. Returns 0 in case of unenabled fee
/// @return The tick spacing
function feeAmountTickSpacing(uint24 fee) external view returns (int24);
/// @notice Returns the pool address for a given pair of tokens and a fee, or address 0 if it does not exist
/// @dev tokenA and tokenB may be passed in either token0/token1 or token1/token0 order
/// @param tokenA The contract address of either token0 or token1
/// @param tokenB The contract address of the other token
/// @param fee The fee collected upon every swap in the pool, denominated in hundredths of a bip
/// @return pool The pool address
function getPool(
address tokenA,
address tokenB,
uint24 fee
) external view returns (address pool);
/// @notice Creates a pool for the given two tokens and fee
/// @param tokenA One of the two tokens in the desired pool
/// @param tokenB The other of the two tokens in the desired pool
/// @param fee The desired fee for the pool
/// @dev tokenA and tokenB may be passed in either order: token0/token1 or token1/token0. tickSpacing is retrieved
/// from the fee. The call will revert if the pool already exists, the fee is invalid, or the token arguments
/// are invalid.
/// @return pool The address of the newly created pool
function createPool(
address tokenA,
address tokenB,
uint24 fee
) external returns (address pool);
/// @notice Updates the owner of the factory
/// @dev Must be called by the current owner
/// @param _owner The new owner of the factory
function setOwner(address _owner) external;
/// @notice Enables a fee amount with the given tickSpacing
/// @dev Fee amounts may never be removed once enabled
/// @param fee The fee amount to enable, denominated in hundredths of a bip (i.e. 1e-6)
/// @param tickSpacing The spacing between ticks to be enforced for all pools created with the given fee amount
function enableFeeAmount(uint24 fee, int24 tickSpacing) external;
}
IFundingRateController.sol 58 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
import {ERC20} from "solmate/tokens/ERC20.sol";
interface IFundingRateController {
/// @notice emitted when target is updated
/// @param newTarget the new target value
event UpdateTarget(uint256 newTarget);
event SetFundingPeriod(uint256 fundingPeriod);
error AlreadyInitialized();
error FundingPeriodTooShort();
error FundingPeriodTooLong();
/// @notice Updates target and returns new target
/// @dev if block.timestamp == lastUpdated() then just returns target()
/// @return Target the new target value
function updateTarget() external returns (uint256);
/// @notice The timestamp at which target was last updated
/// @return lastUpdated the timestamp (in seconds) at which target was last updated
function lastUpdated() external view returns (uint256);
/// @notice The target value of one whole unit of papr in underlying units.
/// @dev Target represents the 0% funding rate value. If mark() is equal to this
/// value, then funding rates are 0 and newTarget() will equal target().
/// @return target The value of one whole unit of papr in underlying units.
/// Example: if papr has 18 decimals and underlying 6 decimals, then
/// target = 1e6 means 1e18 papr is worth 1e6 underlying, according to target
function target() external view returns (uint256);
/// @notice The value of new value of target() if updateTarget() were called right now
/// @dev If mark() > target(), newTarget() will be less than target(), positive funding/negative interest
/// @dev If mark() < target(), newTarget() will be greater than target(), negative funding/positive interest
/// @return newTarget The up to date target value for this block
function newTarget() external view returns (uint256);
/// @notice The market value of a whole unit of papr in underlying units
/// @return mark market papr price, quoted in underlying
function mark() external view returns (uint256);
/// @notice The papr token, the value of which is intended to
/// reflect in-kind funding payments via target() changing in value
/// @return papr the ERC20 token (address)
function papr() external view returns (ERC20);
/// @notice The underlying token that is used to quote the value of papr
/// @return underlying the ERC20 token (address)
function underlying() external view returns (ERC20);
/// @notice The period over which funding is paid
/// @dev a shorter funding period means volatility has a greater impact
/// on funding => target, longer period means the inverse
/// @return fundingPeriod in seconds over which funding is paid
function fundingPeriod() external view returns (uint256);
}
NFTEDA.sol 134 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
import {ERC721} from "solmate/tokens/ERC721.sol";
import {ERC20} from "solmate/tokens/ERC20.sol";
import {SafeTransferLib} from "solmate/utils/SafeTransferLib.sol";
import {INFTEDA} from "./interfaces/INFTEDA.sol";
import {EDAPrice} from "./libraries/EDAPrice.sol";
abstract contract NFTEDA is INFTEDA {
using SafeTransferLib for ERC20;
error AuctionExists();
error InvalidAuction();
/// @param received The amount of payment received
/// @param expected The expected payment amount
error InsufficientPayment(uint256 received, uint256 expected);
/// @param currentPrice The current auction price
/// @param maxPrice The passed max price the purchaser is willing to pay
error MaxPriceTooLow(uint256 currentPrice, uint256 maxPrice);
/// @inheritdoc INFTEDA
function auctionCurrentPrice(INFTEDA.Auction calldata auction) public view virtual returns (uint256) {
uint256 id = auctionID(auction);
uint256 startTime = auctionStartTime(id);
if (startTime == 0) {
revert InvalidAuction();
}
return _auctionCurrentPrice(id, startTime, auction);
}
/// @inheritdoc INFTEDA
function auctionID(INFTEDA.Auction memory auction) public pure virtual returns (uint256) {
return uint256(keccak256(abi.encode(auction)));
}
/// @inheritdoc INFTEDA
function auctionStartTime(uint256 id) public view virtual returns (uint256);
/// @notice Creates an auction defined by the passed `auction`
/// @dev assumes the nft being sold is already controlled by the auction contract
/// @dev does no validation the auction, aside that it does not exist.
/// @dev if paymentAsset = address(0), purchase will not revert
/// @param auction The defintion of the auction
/// @return id the id of the auction
function _startAuction(INFTEDA.Auction memory auction) internal virtual returns (uint256 id) {
id = auctionID(auction);
if (auctionStartTime(id) != 0) {
revert AuctionExists();
}
_setAuctionStartTime(id);
emit StartAuction(
id,
auction.auctionAssetID,
auction.auctionAssetContract,
auction.nftOwner,
auction.perPeriodDecayPercentWad,
auction.secondsInPeriod,
auction.startPrice,
auction.paymentAsset
);
}
/// @notice purchases the NFT being sold in `auction`, reverts if current auction price exceed maxPrice
/// @param auction The auction selling the NFT
/// @param maxPrice The maximum the caller is willing to pay
function _purchaseNFT(INFTEDA.Auction memory auction, uint256 maxPrice, address sendTo)
internal
virtual
returns (uint256 startTime, uint256 price)
{
uint256 id;
(id, startTime, price) = _checkAuctionAndReturnDetails(auction);
if (price > maxPrice) {
revert MaxPriceTooLow(price, maxPrice);
}
_clearAuctionState(id);
auction.auctionAssetContract.safeTransferFrom(address(this), sendTo, auction.auctionAssetID);
auction.paymentAsset.safeTransferFrom(msg.sender, address(this), price);
emit EndAuction(id, price);
}
function _checkAuctionAndReturnDetails(INFTEDA.Auction memory auction)
internal
view
returns (uint256 id, uint256 startTime, uint256 price)
{
id = auctionID(auction);
startTime = auctionStartTime(id);
if (startTime == 0) {
revert InvalidAuction();
}
price = _auctionCurrentPrice(id, startTime, auction);
}
/// @notice Sets the time at which the auction was started
/// @dev abstracted to a function to allow developer some freedom with how to store auction state
/// @param id The id of the auction
function _setAuctionStartTime(uint256 id) internal virtual;
/// @notice Clears all stored state for the auction
/// @dev abstracted to a function to allow developer some freedom with how to store auction state
/// @param id The id of the auction
function _clearAuctionState(uint256 id) internal virtual;
/// @notice Returns the current price of the passed auction, reverts if no such auction exists
/// @dev startTime is passed, optimized for cases where the auctionId has already been computed
/// @dev and startTime looked it up
/// @param id The ID of the auction
/// @param startTime The start time of the auction
/// @param auction The auction for which the caller wants to know the current price
/// @return price the current amount required to purchase the NFT being sold in this auction
function _auctionCurrentPrice(uint256 id, uint256 startTime, INFTEDA.Auction memory auction)
internal
view
virtual
returns (uint256)
{
return EDAPrice.currentPrice(
auction.startPrice, block.timestamp - startTime, auction.secondsInPeriod, auction.perPeriodDecayPercentWad
);
}
}
EDAPrice.sol 33 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
import {FixedPointMathLib} from "solmate/utils/FixedPointMathLib.sol";
library EDAPrice {
/// @notice returns the current price of an exponential price decay auction defined by the passed params
/// @dev reverts if perPeriodDecayPercentWad >= 1e18
/// @dev reverts if uint256 secondsInPeriod = 0
/// @dev reverts if startPrice * multiplier overflows
/// @dev reverts if lnWad(percentWadRemainingPerPeriod) * ratio) overflows
/// @param startPrice the starting price of the auction
/// @param secondsElapsed the seconds elapsed since auction start
/// @param secondsInPeriod the seconds over which the price should decay perPeriodDecayPercentWad
/// @param perPeriodDecayPercentWad the percent the price should decay during secondsInPeriod, 100% = 1e18
/// @return price the current auction price
function currentPrice(
uint256 startPrice,
uint256 secondsElapsed,
uint256 secondsInPeriod,
uint256 perPeriodDecayPercentWad
) internal pure returns (uint256) {
uint256 ratio = FixedPointMathLib.divWadDown(secondsElapsed, secondsInPeriod);
uint256 percentWadRemainingPerPeriod = FixedPointMathLib.WAD - perPeriodDecayPercentWad;
// percentWadRemainingPerPeriod can be safely cast because < 1e18
// ratio can be safely cast because will not overflow unless ratio > int256.max,
// which would require secondsElapsed > int256.max, i.e. > 5.78e76 or 1.8e69 years
int256 multiplier = FixedPointMathLib.powWad(int256(percentWadRemainingPerPeriod), int256(ratio));
// casting to uint256 is safe because percentWadRemainingPerPeriod is non negative
uint256 price = startPrice * uint256(multiplier);
return (price / FixedPointMathLib.WAD);
}
}
PaprToken.sol 31 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.17;
import {ERC20} from "solmate/tokens/ERC20.sol";
contract PaprToken is ERC20 {
error ControllerOnly();
address immutable controller;
modifier onlyController() {
if (msg.sender != controller) {
revert ControllerOnly();
}
_;
}
constructor(string memory name, string memory symbol)
ERC20(string.concat("papr ", name), string.concat("papr", symbol), 18)
{
controller = msg.sender;
}
function mint(address to, uint256 amount) external onlyController {
_mint(to, amount);
}
function burn(address account, uint256 amount) external onlyController {
_burn(account, amount);
}
}
IPaprController.sol 257 lines
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
import {ReservoirOracleUnderwriter} from "../ReservoirOracleUnderwriter.sol";
import {ERC721} from "solmate/tokens/ERC721.sol";
import {ERC20} from "solmate/tokens/ERC20.sol";
import {INFTEDA} from "../NFTEDA/extensions/NFTEDAStarterIncentive.sol";
interface IPaprController {
/// @notice collateral for a vault
struct Collateral {
/// @dev address of the collateral, cast to ERC721
ERC721 addr;
/// @dev tokenId of the collateral
uint256 id;
}
/// @notice vault information for a vault
struct VaultInfo {
/// @dev number of collateral tokens in the vault
uint16 count;
/// @dev number of auctions on going for this vault
uint16 auctionCount;
/// @dev start time of last auction the vault underwent, 0 if no auction has been started
uint40 latestAuctionStartTime;
/// @dev debt of the vault, expressed in papr token units
uint184 debt;
}
/// @notice parameters describing a swap
/// @dev increaseDebtAndSell has the input token as papr and output token as the underlying
/// @dev buyAndReduceDebt has the input token as the underlying and output token as papr
struct SwapParams {
/// @dev amount of input token to swap
uint256 amount;
/// @dev minimum amount of output token to be received
uint256 minOut;
/// @dev sqrt price limit for the swap
uint160 sqrtPriceLimitX96;
/// @dev optional address to receive swap fees
address swapFeeTo;
/// @dev optional swap fee in bips
uint256 swapFeeBips;
/// @dev timestamp after which the swap should not be executed
uint256 deadline;
}
/// @notice parameters to be encoded in safeTransferFrom collateral addition
struct OnERC721ReceivedArgs {
/// @dev address to send proceeds to if minting debt or swapping
address proceedsTo;
/// @dev debt is ignored in favor of `swapParams.amount` of minOut > 0
uint256 debt;
/// @dev optional swapParams
SwapParams swapParams;
/// @dev oracle information associated with collateral being sent
ReservoirOracleUnderwriter.OracleInfo oracleInfo;
}
/// @notice parameters to change what collateral addresses can be used for a vault
struct CollateralAllowedConfig {
ERC721 collateral;
bool allowed;
}
/// @notice emitted when an address increases the debt balance of their vault
/// @param account address increasing their debt
/// @param collateralAddress address of the collateral token
/// @param amount amount of debt added
/// @dev vaults are uniquely identified by the address of the vault owner and the address of the collateral token used in the vault
event IncreaseDebt(address indexed account, ERC721 indexed collateralAddress, uint256 amount);
/// @notice emitted when a user adds collateral to their vault
/// @param account address adding collateral
/// @param collateralAddress contract address of the ERC721 collateral added
/// @param tokenId token id of the ERC721 collateral added
event AddCollateral(address indexed account, ERC721 indexed collateralAddress, uint256 indexed tokenId);
/// @notice emitted when a user removes collateral from their vault
/// @param account address removing collateral
/// @param collateralAddress contract address of the ERC721 collateral removed
/// @param tokenId token id of the ERC721 collateral removed
event RemoveCollateral(address indexed account, ERC721 indexed collateralAddress, uint256 indexed tokenId);
/// @notice emitted when a user reduces the debt balance of their vault
/// @param account address reducing their debt
/// @param collateralAddress address of the collateral token
/// @param amount amount of debt removed
event ReduceDebt(address indexed account, ERC721 indexed collateralAddress, uint256 amount);
/// @notice emitted when the owner sets whether a token address is allowed to serve as collateral for a vault
/// @param collateral address of the collateral token
/// @param isAllowed whether the collateral is allowed
event AllowCollateral(ERC721 indexed collateral, bool isAllowed);
/// @notice emitted when the owner sets a new funding period for the controller
/// @param newPeriod new funding period that was set, in seconds
event UpdateFundingPeriod(uint256 newPeriod);
/// @notice emitted when the owner sets a new Uniswap V3 pool for the controller
/// @param newPool address of the new Uniswap V3 pool that was set
event UpdatePool(address indexed newPool);
/// @param vaultDebt how much debt the vault has
/// @param maxDebt the max debt the vault is allowed to have
error ExceedsMaxDebt(uint256 vaultDebt, uint256 maxDebt);
/// @param asset proposed to be added as allowed collateral
event ProposeAllowedCollateral(ERC721 indexed asset);
/// @param asset the asset that was proposed to be added to allowed collateral and is now being cancelled
event CancelProposedAllowedCollateral(ERC721 indexed asset);
error InvalidCollateral();
error MinAuctionSpacing();
error NotLiquidatable();
error InvalidCollateralAccountPair();
error AccountHasNoDebt();
error OnlyCollateralOwner();
error DebtAmountExceedsUint184();
error CollateralAddressesDoNotMatch();
error LiquidationsLocked();
error ProposalPeriodNotComplete();
error AssetNotProposed();
/// @notice adds collateral to msg.senders vault for collateral.addr
/// @dev use safeTransferFrom to save gas if only sending one NFT
/// @param collateral collateral to add
function addCollateral(IPaprController.Collateral[] calldata collateral) external;
/// @notice removes collateral from msg.senders vault
/// @dev all collateral must be from same contract address
/// @dev oracleInfo price must be type LOWER
/// @param sendTo address to send the collateral to when removed
/// @param collateralArr array of IPaprController.Collateral to be removed
/// @param oracleInfo oracle information for the collateral being removed
function removeCollateral(
address sendTo,
IPaprController.Collateral[] calldata collateralArr,
ReservoirOracleUnderwriter.OracleInfo calldata oracleInfo
) external;
/// @notice increases debt balance of the vault uniquely identified by msg.sender and the collateral address
/// @dev oracleInfo price must be type LOWER
/// @param mintTo address to mint the debt to
/// @param asset address of the collateral token used to mint the debt
/// @param amount amount of debt to mint
/// @param oracleInfo oracle information for the collateral being used to mint debt
function increaseDebt(
address mintTo,
ERC721 asset,
uint256 amount,
ReservoirOracleUnderwriter.OracleInfo calldata oracleInfo
) external;
/// @notice removes and burns debt from the vault uniquely identified by account and the collateral address
/// @param account address reducing their debt
/// @param asset address of the collateral token the user would like to remove debt from
/// @param amount amount of debt to remove
function reduceDebt(address account, ERC721 asset, uint256 amount) external;
/// @notice mints debt and swaps the debt for the controller's underlying token on Uniswap
/// @dev oracleInfo price must be type LOWER
/// @param proceedsTo address to send the proceeds to
/// @param collateralAsset address of the collateral token used to mint the debt
/// @param params parameters for the swap
/// @param oracleInfo oracle information for the collateral being used to mint debt
/// @return amount amount of underlying token received by the user
function increaseDebtAndSell(
address proceedsTo,
ERC721 collateralAsset,
IPaprController.SwapParams calldata params,
ReservoirOracleUnderwriter.OracleInfo calldata oracleInfo
) external returns (uint256);
/// @notice removes debt from a vault and burns it by swapping the controller's underlying token for Papr tokens using the Uniswap V3 pool
/// @param account address reducing their debt
/// @param collateralAsset address of the collateral token the user would like to remove debt from
/// @param params parameters for the swap
/// @return amount amount of debt received from the swap and burned
function buyAndReduceDebt(address account, ERC721 collateralAsset, IPaprController.SwapParams calldata params)
external
returns (uint256);
/// @notice purchases a liquidation auction with the controller's papr token
/// @dev oracleInfo price must be type TWAP
/// @param auction auction to purchase
/// @param maxPrice maximum price to pay for the auction
/// @param sendTo address to send the collateral to if auction is won
function purchaseLiquidationAuctionNFT(
INFTEDA.Auction calldata auction,
uint256 maxPrice,
address sendTo,
ReservoirOracleUnderwriter.OracleInfo calldata oracleInfo
) external;
/// @notice starts a liquidation auction for a vault if it is liquidatable
/// @dev oracleInfo price must be type TWAP
/// @param account address of the user who's vault to liquidate
/// @param collateral collateral to liquidate
/// @param oracleInfo oracle information for the collateral being liquidated
/// @return auction auction that was started
function startLiquidationAuction(
address account,
IPaprController.Collateral calldata collateral,
ReservoirOracleUnderwriter.OracleInfo calldata oracleInfo
) external returns (INFTEDA.Auction memory auction);
/// @notice sets the Uniswap V3 pool that is used to determine mark
/// @dev owner function
/// @param _pool address of the Uniswap V3 pool
function setPool(address _pool) external;
/// @notice sets the funding period for interest payments
/// @param _fundingPeriod new funding period in seconds
function setFundingPeriod(uint256 _fundingPeriod) external;
/// @notice returns who owns a collateral token in a vault
/// @param collateral address of the collateral
/// @param tokenId tokenId of the collateral
function collateralOwner(ERC721 collateral, uint256 tokenId) external view returns (address);
/// @notice returns the last auction timestamp and price for a given asset
/// @param asset address of the ERC721 token
function lastAuctionStartPrice(ERC721 asset) external view returns (uint40, uint216);
/// @notice returns whether a token address is allowed to serve as collateral for a vault
/// @param collateral address of the collateral token
function isAllowed(ERC721 collateral) external view returns (bool);
/// @notice maximum LTV a vault can have, expressed as a decimal scaled by 1e18
function maxLTV() external view returns (uint256);
/// @notice minimum time that must pass before consecutive collateral is liquidated from the same vault
function liquidationAuctionMinSpacing() external view returns (uint256);
/// @notice returns the maximum debt that can be minted for a given collateral value
/// @param totalCollateraValue total value of the collateral
/// @return maxDebt maximum debt that can be minted, expressed in terms of the papr token
function maxDebt(uint256 totalCollateraValue) external view returns (uint256);
/// @notice returns information about a vault
/// @param account address of the vault owner
/// @param asset address of the collateral token associated with the vault
/// @return vaultInfo VaultInfo struct representing information about a vault
function vaultInfo(address account, ERC721 asset) external view returns (IPaprController.VaultInfo memory);
}
TickMath.sol 216 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.8.0;
/// @title Math library for computing sqrt prices from ticks and vice versa
/// @notice Computes sqrt price for ticks of size 1.0001, i.e. sqrt(1.0001^tick) as fixed point Q64.96 numbers. Supports
/// prices between 2**-128 and 2**128
library TickMath {
/// @dev The minimum tick that may be passed to #getSqrtRatioAtTick computed from log base 1.0001 of 2**-128
int24 internal constant MIN_TICK = -887272;
/// @dev The maximum tick that may be passed to #getSqrtRatioAtTick computed from log base 1.0001 of 2**128
int24 internal constant MAX_TICK = -MIN_TICK;
/// @dev The minimum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MIN_TICK)
uint160 internal constant MIN_SQRT_RATIO = 4295128739;
/// @dev The maximum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MAX_TICK)
uint160 internal constant MAX_SQRT_RATIO = 1461446703485210103287273052203988822378723970342;
/// @notice Calculates sqrt(1.0001^tick) * 2^96
/// @dev Throws if |tick| > max tick
/// @param tick The input tick for the above formula
/// @return sqrtPriceX96 A Fixed point Q64.96 number representing the sqrt of the ratio of the two assets (token1/token0)
/// at the given tick
function getSqrtRatioAtTick(int24 tick) internal pure returns (uint160 sqrtPriceX96) {
unchecked {
uint256 absTick = tick < 0 ? uint256(-int256(tick)) : uint256(int256(tick));
require(absTick <= uint256(int256(MAX_TICK)), "T");
uint256 ratio = absTick & 0x1 != 0
? 0xfffcb933bd6fad37aa2d162d1a594001
: 0x100000000000000000000000000000000;
if (absTick & 0x2 != 0) ratio = (ratio * 0xfff97272373d413259a46990580e213a) >> 128;
if (absTick & 0x4 != 0) ratio = (ratio * 0xfff2e50f5f656932ef12357cf3c7fdcc) >> 128;
if (absTick & 0x8 != 0) ratio = (ratio * 0xffe5caca7e10e4e61c3624eaa0941cd0) >> 128;
if (absTick & 0x10 != 0) ratio = (ratio * 0xffcb9843d60f6159c9db58835c926644) >> 128;
if (absTick & 0x20 != 0) ratio = (ratio * 0xff973b41fa98c081472e6896dfb254c0) >> 128;
if (absTick & 0x40 != 0) ratio = (ratio * 0xff2ea16466c96a3843ec78b326b52861) >> 128;
if (absTick & 0x80 != 0) ratio = (ratio * 0xfe5dee046a99a2a811c461f1969c3053) >> 128;
if (absTick & 0x100 != 0) ratio = (ratio * 0xfcbe86c7900a88aedcffc83b479aa3a4) >> 128;
if (absTick & 0x200 != 0) ratio = (ratio * 0xf987a7253ac413176f2b074cf7815e54) >> 128;
if (absTick & 0x400 != 0) ratio = (ratio * 0xf3392b0822b70005940c7a398e4b70f3) >> 128;
if (absTick & 0x800 != 0) ratio = (ratio * 0xe7159475a2c29b7443b29c7fa6e889d9) >> 128;
if (absTick & 0x1000 != 0) ratio = (ratio * 0xd097f3bdfd2022b8845ad8f792aa5825) >> 128;
if (absTick & 0x2000 != 0) ratio = (ratio * 0xa9f746462d870fdf8a65dc1f90e061e5) >> 128;
if (absTick & 0x4000 != 0) ratio = (ratio * 0x70d869a156d2a1b890bb3df62baf32f7) >> 128;
if (absTick & 0x8000 != 0) ratio = (ratio * 0x31be135f97d08fd981231505542fcfa6) >> 128;
if (absTick & 0x10000 != 0) ratio = (ratio * 0x9aa508b5b7a84e1c677de54f3e99bc9) >> 128;
if (absTick & 0x20000 != 0) ratio = (ratio * 0x5d6af8dedb81196699c329225ee604) >> 128;
if (absTick & 0x40000 != 0) ratio = (ratio * 0x2216e584f5fa1ea926041bedfe98) >> 128;
if (absTick & 0x80000 != 0) ratio = (ratio * 0x48a170391f7dc42444e8fa2) >> 128;
if (tick > 0) ratio = type(uint256).max / ratio;
// this divides by 1<<32 rounding up to go from a Q128.128 to a Q128.96.
// we then downcast because we know the result always fits within 160 bits due to our tick input constraint
// we round up in the division so getTickAtSqrtRatio of the output price is always consistent
sqrtPriceX96 = uint160((ratio >> 32) + (ratio % (1 << 32) == 0 ? 0 : 1));
}
}
/// @notice Calculates the greatest tick value such that getRatioAtTick(tick) <= ratio
/// @dev Throws in case sqrtPriceX96 < MIN_SQRT_RATIO, as MIN_SQRT_RATIO is the lowest value getRatioAtTick may
/// ever return.
/// @param sqrtPriceX96 The sqrt ratio for which to compute the tick as a Q64.96
/// @return tick The greatest tick for which the ratio is less than or equal to the input ratio
function getTickAtSqrtRatio(uint160 sqrtPriceX96) internal pure returns (int24 tick) {
unchecked {
// second inequality must be < because the price can never reach the price at the max tick
require(sqrtPriceX96 >= MIN_SQRT_RATIO && sqrtPriceX96 < MAX_SQRT_RATIO, "R");
uint256 ratio = uint256(sqrtPriceX96) << 32;
uint256 r = ratio;
uint256 msb = 0;
assembly {
let f := shl(7, gt(r, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF))
msb := or(msb, f)
r := shr(f, r)
}
assembly {
let f := shl(6, gt(r, 0xFFFFFFFFFFFFFFFF))
msb := or(msb, f)
r := shr(f, r)
}
assembly {
let f := shl(5, gt(r, 0xFFFFFFFF))
msb := or(msb, f)
r := shr(f, r)
}
assembly {
let f := shl(4, gt(r, 0xFFFF))
msb := or(msb, f)
r := shr(f, r)
}
assembly {
let f := shl(3, gt(r, 0xFF))
msb := or(msb, f)
r := shr(f, r)
}
assembly {
let f := shl(2, gt(r, 0xF))
msb := or(msb, f)
r := shr(f, r)
}
assembly {
let f := shl(1, gt(r, 0x3))
msb := or(msb, f)
r := shr(f, r)
}
assembly {
let f := gt(r, 0x1)
msb := or(msb, f)
}
if (msb >= 128) r = ratio >> (msb - 127);
else r = ratio << (127 - msb);
int256 log_2 = (int256(msb) - 128) << 64;
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(63, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(62, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(61, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(60, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(59, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(58, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(57, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(56, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(55, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(54, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(53, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(52, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(51, f))
r := shr(f, r)
}
assembly {
r := shr(127, mul(r, r))
let f := shr(128, r)
log_2 := or(log_2, shl(50, f))
}
int256 log_sqrt10001 = log_2 * 255738958999603826347141; // 128.128 number
int24 tickLow = int24((log_sqrt10001 - 3402992956809132418596140100660247210) >> 128);
int24 tickHi = int24((log_sqrt10001 + 291339464771989622907027621153398088495) >> 128);
tick = tickLow == tickHi ? tickLow : getSqrtRatioAtTick(tickHi) <= sqrtPriceX96 ? tickHi : tickLow;
}
}
function getTicks(int24 tickSpacing) internal pure returns (int24 minTick, int24 maxTick) {
minTick = (MIN_TICK / tickSpacing) * tickSpacing;
maxTick = (MAX_TICK / tickSpacing) * tickSpacing;
}
}
SafeCast.sol 28 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Safe casting methods
/// @notice Contains methods for safely casting between types
library SafeCast {
/// @notice Cast a uint256 to a uint160, revert on overflow
/// @param y The uint256 to be downcasted
/// @return z The downcasted integer, now type uint160
function toUint160(uint256 y) internal pure returns (uint160 z) {
require((z = uint160(y)) == y);
}
/// @notice Cast a int256 to a int128, revert on overflow or underflow
/// @param y The int256 to be downcasted
/// @return z The downcasted integer, now type int128
function toInt128(int256 y) internal pure returns (int128 z) {
require((z = int128(y)) == y);
}
/// @notice Cast a uint256 to a int256, revert on overflow
/// @param y The uint256 to be casted
/// @return z The casted integer, now type int256
function toInt256(uint256 y) internal pure returns (int256 z) {
require(y < 2**255);
z = int256(y);
}
}
PoolAddress.sol 48 lines
// SPDX-License-Identifier: GPL-2.0-or-later
// from https://github.com/Uniswap/v3-periphery/blob/main/contracts/libraries/PoolAddress.sol
// with updates for solc 8
pragma solidity >=0.8.0;
/// @title Provides functions for deriving a pool address from the factory, tokens, and the fee
library PoolAddress {
bytes32 internal constant POOL_INIT_CODE_HASH = 0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54;
/// @notice The identifying key of the pool
struct PoolKey {
address token0;
address token1;
uint24 fee;
}
/// @notice Returns PoolKey: the ordered tokens with the matched fee levels
/// @param tokenA The first token of a pool, unsorted
/// @param tokenB The second token of a pool, unsorted
/// @param fee The fee level of the pool
/// @return Poolkey The pool details with ordered token0 and token1 assignments
function getPoolKey(address tokenA, address tokenB, uint24 fee) internal pure returns (PoolKey memory) {
if (tokenA > tokenB) (tokenA, tokenB) = (tokenB, tokenA);
return PoolKey({token0: tokenA, token1: tokenB, fee: fee});
}
/// @notice Deterministically computes the pool address given the factory and PoolKey
/// @param factory The Uniswap V3 factory contract address
/// @param key The PoolKey
/// @return pool The contract address of the V3 pool
function computeAddress(address factory, PoolKey memory key) internal pure returns (address pool) {
require(key.token0 < key.token1);
pool = address(
uint160(
uint256(
keccak256(
abi.encodePacked(
hex"ff",
factory,
keccak256(abi.encode(key.token0, key.token1, key.fee)),
POOL_INIT_CODE_HASH
)
)
)
)
);
}
}
Read Contract
MAX_PER_SECOND_PRICE_CHANGE 0xb5848176 → uint256
auctionCreatorDiscountPercentWad 0x8c1ad610 → uint256
auctionCurrentPrice 0x2e832a16 → uint256
auctionID 0x49095493 → uint256
auctionStartTime 0x33465c5f → uint256
auctionState 0x84861e93 → uint96, address
cachedPriceForAsset 0x5474b451 → uint40, uint216
collateralOwner 0x0a184d84 → address
fundingPeriod 0x74d7c62b → uint256
isAllowed 0xbabcc539 → bool
lastAuctionStartPrice 0x3328cfda → uint40, uint216
lastUpdated 0xd0b06f5d → uint256
liquidationAuctionMinSpacing 0xf24d1ca3 → uint256
mark 0x8c0d0c29 → uint256
maxDebt 0x115042d3 → uint256
maxLTV 0xf384bd05 → uint256
newTarget 0x13217f90 → uint256
oracleSigner 0x1709a61b → address
owner 0x8da5cb5b → address
papr 0xb91c9f40 → address
pendingOwner 0xe30c3978 → address
pool 0x16f0115b → address
target 0xd4b83992 → uint256
underlying 0x6f307dc3 → address
vaultInfo 0xfb300884 → tuple
Write Contract 22 functions
These functions modify contract state and require a wallet transaction to execute.
acceptOwnership 0x79ba5097
No parameters
acceptProposedCollateral 0x82e0e316
address asset
addCollateral 0x665a890d
tuple[] collateralArr
buyAndReduceDebt 0xf3127d73
address account
address collateralAsset
tuple params
returns: uint256
cancelProposedCollateral 0x80790424
address asset
increaseDebt 0xc14e1147
address mintTo
address asset
uint256 amount
tuple oracleInfo
increaseDebtAndSell 0xbd65e88f
address proceedsTo
address collateralAsset
tuple params
tuple oracleInfo
returns: uint256
multicall 0xac9650d8
bytes[] data
returns: bytes[]
onERC721Received 0x150b7a02
address
address from
uint256 _id
bytes data
returns: bytes4
proposeAllowedCollateral 0xfcd4ce33
address asset
purchaseLiquidationAuctionNFT 0x4c14d099
tuple auction
uint256 maxPrice
address sendTo
tuple oracleInfo
reduceDebt 0x578bcc20
address account
address asset
uint256 amount
removeAllowedCollateral 0x6f05e1ae
address[] assets
removeCollateral 0xca077dc3
address sendTo
tuple[] collateralArr
tuple oracleInfo
renounceOwnership 0x715018a6
No parameters
setFundingPeriod 0x2a5aa292
uint256 _fundingPeriod
setPool 0x4437152a
address _pool
startLiquidationAuction 0x55bd2926
address account
tuple collateral
tuple oracleInfo
returns: tuple
transferOwnership 0xf2fde38b
address newOwner
underwritePriceForCollateral 0xaaa2e2e5
address asset
uint8 priceKind
tuple oracleInfo
bool guard
returns: uint256
uniswapV3SwapCallback 0xfa461e33
int256 amount0Delta
int256 amount1Delta
bytes _data
updateTarget 0x76f6c33f
No parameters
returns: uint256
Recent Transactions
No transactions found for this address