Cryo Explorer Ethereum Mainnet

Address Contract Partially Verified

Address 0x774A82AfAdb5A63d92B980D179D8B0f2E6d577A0
Balance 5.7486 ETH
Nonce 1
Code Size 17576 bytes
Indexed Transactions 0
External Etherscan · Sourcify

Contract Bytecode

17576 bytes
0x60806040526004361061036f5760003560e01c80639b1209b5116101c6578063e1b97139116100f7578063f3fef3a311610095578063f8a918e21161006f578063f8a918e214610a8a578063f9f2935614610a9f578063fa09e63014610ab4578063ffa1ad7414610ad457600080fd5b8063f3fef3a314610a34578063f7888aec14610a54578063f7c649dd14610a7457600080fd5b8063e6d486ac116100d1578063e6d486ac14610987578063eae3ac8f146109a7578063ecf74777146109c7578063f2fde38b14610a1457600080fd5b8063e1b9713914610929578063e30c397814610949578063e6a6e7a21461096757600080fd5b8063c822adda11610164578063d305536f1161013e578063d305536f146108a9578063d9ece259146108c9578063da3e3397146108e9578063da9d3be71461090957600080fd5b8063c822adda146107da578063cd99aad814610873578063cdaf200f1461089357600080fd5b8063b61d27f6116101a0578063b61d27f614610772578063b6703fcd14610785578063ba751323146107a5578063c4e2c1e6146107ba57600080fd5b80639b1209b514610709578063aca8f6f71461071e578063b381cf401461073e57600080fd5b806368dbd7a9116102a057806381842c2c1161023e5780638e88eccc116102185780638e88eccc1461068457806390adc83c146106a45780639169d833146106b95780639ac2a011146106d957600080fd5b806381842c2c146106155780638b2662061461064d5780638da5cb5b1461066657600080fd5b8063715018a61161027a578063715018a6146105a957806378c94986146105be57806379ba5097146105de5780637cc34bb4146105f357600080fd5b806368dbd7a9146105545780636ed625ab1461056957806370431d291461058957600080fd5b80631f5a0bbe1161030d57806334b10a6d116102e757806334b10a6d146104cf57806354621b42146104ef5780635c6b7a1d1461050f57806362cef7911461053457600080fd5b80631f5a0bbe1461046f578063247884291461048f5780633419ba23146104af57600080fd5b8063087ed83711610349578063087ed837146103d85780630c35b12d146103fa5780631761b7ce1461041a5780631b51d9b51461044f57600080fd5b8063031609401461037b578063077d97d7146103a357806307b46054146103b857600080fd5b3661037657005b600080fd5b34801561038757600080fd5b50610390610b08565b6040519081526020015b60405180910390f35b6103b66103b1366004613a0c565b610b7b565b005b3480156103c457600080fd5b506103b66103d3366004613abb565b610cf1565b3480156103e457600080fd5b506103ed610de3565b60405161039a9190613af0565b34801561040657600080fd5b50610390610415366004613abb565b610e46565b34801561042657600080fd5b5061043a610435366004613b3d565b611042565b6040805192835260208301919091520161039a565b34801561045b57600080fd5b5061043a61046a366004613b3d565b6110ec565b34801561047b57600080fd5b506103b661048a366004613b56565b6111e2565b34801561049b57600080fd5b506103b66104aa366004613b56565b61124d565b3480156104bb57600080fd5b506103b66104ca366004613b73565b6112b5565b3480156104db57600080fd5b506103b66104ea366004613b3d565b6112cd565b3480156104fb57600080fd5b5061039061050a366004613b3d565b61139d565b34801561051b57600080fd5b50610524611434565b604051901515815260200161039a565b34801561054057600080fd5b5061052461054f366004613b3d565b61143d565b34801561056057600080fd5b506103906114b0565b34801561057557600080fd5b50610390610584366004613bac565b6114f2565b34801561059557600080fd5b506103906105a4366004613bac565b611611565b3480156105b557600080fd5b506103b6611b4d565b3480156105ca57600080fd5b506103906105d9366004613bac565b611b61565b3480156105ea57600080fd5b506103b6611d03565b3480156105ff57600080fd5b50610608611d7d565b60405161039a9190613bce565b34801561062157600080fd5b50600654610635906001600160a01b031681565b6040516001600160a01b03909116815260200161039a565b34801561065957600080fd5b50600a5460095411610524565b34801561067257600080fd5b506000546001600160a01b0316610635565b34801561069057600080fd5b506103b661069f366004613b3d565b611fba565b3480156106b057600080fd5b506103906120cb565b3480156106c557600080fd5b506103b66106d4366004613b3d565b6120e1565b3480156106e557600080fd5b506105246106f4366004613b56565b60026020526000908152604090205460ff1681565b34801561071557600080fd5b506103906121ab565b34801561072a57600080fd5b50610390610739366004613bac565b6121c7565b34801561074a57600080fd5b506106357f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc281565b6103b6610780366004613c83565b61225a565b34801561079157600080fd5b506103b66107a0366004613b73565b6122e7565b3480156107b157600080fd5b5061039061230c565b3480156107c657600080fd5b506103b66107d5366004613d0c565b61234d565b3480156107e657600080fd5b506108386107f5366004613b3d565b6008602052600090815260409020805460018201546002830154600384015460048501546005860154600687015460079097015495969495939492939192909188565b604080519889526020890197909752958701949094526060860192909252608085015260a084015260c083015260e08201526101000161039a565b34801561087f57600080fd5b5061039061088e366004613e05565b612360565b34801561089f57600080fd5b50610390600a5481565b3480156108b557600080fd5b506103b66108c4366004613b56565b6124d3565b3480156108d557600080fd5b5061043a6108e4366004613bac565b6124fd565b3480156108f557600080fd5b506103b6610904366004613e9f565b612838565b34801561091557600080fd5b506103b6610924366004613bac565b61284b565b34801561093557600080fd5b506103b6610944366004613b56565b6128d1565b34801561095557600080fd5b506001546001600160a01b0316610635565b34801561097357600080fd5b50610390610982366004613b3d565b61292f565b34801561099357600080fd5b506103906109a2366004613bac565b6129fb565b3480156109b357600080fd5b506103906109c2366004613bac565b612b7d565b3480156109d357600080fd5b506003546004546005546109ef926001600160a01b0316919083565b604080516001600160a01b03909416845260208401929092529082015260600161039a565b348015610a2057600080fd5b506103b6610a2f366004613b56565b612d0d565b348015610a4057600080fd5b506103b6610a4f366004613ee0565b612d7e565b348015610a6057600080fd5b50610390610a6f366004613b73565b612d91565b348015610a8057600080fd5b5061039060095481565b348015610a9657600080fd5b50610524612dff565b348015610aab57600080fd5b50610524612e6e565b348015610ac057600080fd5b506103b6610acf366004613b56565b612e8c565b348015610ae057600080fd5b5060408051808201825260058152640312e302e360dc1b6020820152905161039a9190613f38565b6006546040805163069b411760e41b815290516000926001600160a01b0316916369b411709160048083019260209291908290030181865afa158015610b52573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610b769190613f6b565b905090565b610b83612efc565b8483148015610b9157508481145b610be25760405162461bcd60e51b815260206004820152601960248201527f417267756d656e7473206c656e677468206d69736d617463680000000000000060448201526064015b60405180910390fd5b60005b85811015610ce857600080888884818110610c0257610c02613f84565b9050602002016020810190610c179190613b56565b6001600160a01b0316878785818110610c3257610c32613f84565b90506020020135868686818110610c4b57610c4b613f84565b9050602002810190610c5d9190613f9a565b604051610c6b929190613fe1565b60006040518083038185875af1925050503d8060008114610ca8576040519150601f19603f3d011682016040523d82523d6000602084013e610cad565b606091505b5091509150818190610cd25760405162461bcd60e51b8152600401610bd99190613f38565b5050508080610ce090614007565b915050610be5565b50505050505050565b610cf9612efc565b604080516060810182526001600160a01b0385168082526020808301869052918301849052600380546001600160a01b03191682179055600485815560058590558351631608dcf760e21b81529351919363582373dc9381830193909290918290030181865afa158015610d71573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610d95919061402b565b600680546001600160a01b0319166001600160a01b03929092169190911790556040517f4f69b8ffd85964c34f21936e1baae46e323dde164a4537df2c6c4d662b00a66790600090a1505050565b6040805160018082528183019092526060916000919060208083019080368337019050509050610e11611d7d565b610100015181600081518110610e2957610e29613f84565b6001600160a01b0390921660209283029190910190910152919050565b600033610e5b6000546001600160a01b031690565b6001600160a01b03161480610e7f57503360009081526002602052604090205460ff165b610e9b5760405162461bcd60e51b8152600401610bd990614048565b6003546001600160a01b0316610ec35760405162461bcd60e51b8152600401610bd99061408e565b82600003610ed35750600061103b565b6000610edd611d7d565b90506000856001600160a01b031663df6854846040518163ffffffff1660e01b8152600401602060405180830381865afa158015610f1f573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610f439190613f6b565b825160208401516040516314904a5b60e21b8152600481019190915260248101839052604481018890529192506001600160a01b031690635241296c9060640160408051808303816000875af1158015610fa1573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610fc591906140aa565b93505083831015610ffd5760405162461bcd60e51b815260206004820152600260248201526114cd60f21b6044820152606401610bd9565b604080518681526020810185905233917f1124384a24d472f729e164fc8001d6f07e10ecee1ffc889daaba0b49b644f263910160405180910390a250505b9392505050565b600080336110586000546001600160a01b031690565b6001600160a01b0316148061107c57503360009081526002602052604090205460ff165b6110985760405162461bcd60e51b8152600401610bd990614048565b6003546001600160a01b03166110c05760405162461bcd60e51b8152600401610bd99061408e565b600a805490819060006110d283614007565b91905055506110e181856124fd565b909590945092505050565b600080336111026000546001600160a01b031690565b6001600160a01b0316148061112657503360009081526002602052604090205460ff165b6111425760405162461bcd60e51b8152600401610bd990614048565b6003546001600160a01b031661116a5760405162461bcd60e51b8152600401610bd99061408e565b611172612e6e565b156111a8576000806111846000611042565b909250905061119382856140ce565b935061119f81846140ce565b9250505061116a565b828110156111dd5760405162461bcd60e51b815260206004820152600260248201526115cd60f21b6044820152606401610bd9565b915091565b6111ea612efc565b604051600181526001600160a01b038216907f9fdbc2d48b8a0db2f62663bf9312ad02f5b1f6414ad600b55a247d09aeec3ea29060200160405180910390a26001600160a01b03166000908152600260205260409020805460ff19166001179055565b611255612efc565b604051600081526001600160a01b038216907f9fdbc2d48b8a0db2f62663bf9312ad02f5b1f6414ad600b55a247d09aeec3ea29060200160405180910390a26001600160a01b03166000908152600260205260409020805460ff19169055565b6112bd612efc565b6112c982826000612f56565b5050565b336112e06000546001600160a01b031690565b6001600160a01b0316148061130457503360009081526002602052604090205460ff165b6113205760405162461bcd60e51b8152600401610bd990614048565b604051632e1a7d4d60e01b8152600481018290527f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc26001600160a01b031690632e1a7d4d90602401600060405180830381600087803b15801561138257600080fd5b505af1158015611396573d6000803e3d6000fd5b5050505050565b6000336113b26000546001600160a01b031690565b6001600160a01b031614806113d657503360009081526002602052604090205460ff165b6113f25760405162461bcd60e51b8152600401610bd990614048565b6003546001600160a01b031661141a5760405162461bcd60e51b8152600401610bd99061408e565b61142c670de0b6b3a7640000836114f2565b90505b919050565b6000610b76600a545b600090815260086020908152604091829020825161010081018452815481526001820154928101929092526002810154928201929092526003820154606082015260048201546080820152600582015460a0820152600682015460c0820181905260079092015460e09091015242101590565b600a546000905b6009548110156114ee576000818152600860205260409020546114da90836140ce565b9150806114e681614007565b9150506114b7565b5090565b6000336115076000546001600160a01b031690565b6001600160a01b0316148061152b57503360009081526002602052604090205460ff165b6115475760405162461bcd60e51b8152600401610bd990614048565b6003546001600160a01b031661156f5760405162461bcd60e51b8152600401610bd99061408e565b670de0b6b3a76400008311156115ac5760405162461bcd60e51b8152602060048201526002602482015261503160f01b6044820152606401610bd9565b82156115d55760006115c56115bf6120cb565b8561304d565b90506115d2816000611611565b50505b6115de826110ec565b6040519092507fcb98953bd1dbc99409d236f79303561f0568a868c3a01b79116dd95a8374f2999150600090a192915050565b6000336116266000546001600160a01b031690565b6001600160a01b0316148061164a57503360009081526002602052604090205460ff165b6116665760405162461bcd60e51b8152600401610bd990614048565b6003546001600160a01b031661168e5760405162461bcd60e51b8152600401610bd99061408e565b8260000361169e57506000611b47565b60006116a8611d7d565b60098054919250819060006116bc83614007565b9190505550600082600001516001600160a01b031663344259e46040518060e00160405280866020015181526020018981526020016000815260200186600001516001600160a01b031663d2ec5fca88602001516040518263ffffffff1660e01b815260040161172e91815260200190565b602060405180830381865afa15801561174b573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061176f9190613f6b565b815260006020808301829052604080840183905260609384019290925281516001600160e01b031960e087901b168152845160048201529084015160248201529083015160448201529082015160648201526080820151608482015260a082015160a482015260c09091015160c482015260e401610120604051808303816000875af1158015611803573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061182791906140e6565b9050600061183982610100015161306c565b905060008260600151836040015161185191906140ce565b905060008360c001518460a0015161186991906140ce565b905060008460e00151905060008560800151905060008860c001516001600160a01b0316633ba0b9a96040518163ffffffff1660e01b8152600401602060405180830381865afa1580156118c1573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906118e59190613f6b565b905060006118f385856130f5565b9050600082670de0b6b3a764000061190b8488614168565b611915919061417f565b61191f919061419e565b8b5160208d0151604051631a4b33b960e11b815260048101919091529192506000916f4b3b4ca85a86c47a098a224000000000916001600160a01b031690633496677290602401602060405180830381865afa158015611983573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906119a79190613f6b565b6119ba9068056bc75e2d63100000614168565b856119c585896130f5565b6119cf919061417f565b6119d9919061417f565b6119e3919061419e565b9050806119f0848a6140ce565b6119fa91906140ce565b9c508d8d1015611a315760405162461bcd60e51b8152602060048201526002602482015261573160f01b6044820152606401610bd9565b50505050506040518061010001604052808b8152602001898152602001848152602001838152602001828152602001866080015181526020018560000151815260200186610100015181525060086000888152602001908152602001600020600082015181600001556020820151816001015560408201518160020155606082015181600301556080820151816004015560a0820151816005015560c0820151816006015560e08201518160070155905050336001600160a01b03167f62956436287ff13618051d39dd523f9252585e7f4ea5bb0a0b97dc44074b79bc88610100015188604051611b379291906001600160a01b03929092168252602082015260400190565b60405180910390a2505050505050505b92915050565b611b55612efc565b611b5f600061310b565b565b600033611b766000546001600160a01b031690565b6001600160a01b03161480611b9a57503360009081526002602052604090205460ff165b611bb65760405162461bcd60e51b8152600401610bd990614048565b6003546001600160a01b0316611bde5760405162461bcd60e51b8152600401610bd99061408e565b6000611be8611d7d565b805160208201516040516305b0c4b360e21b815260048101919091526024810187905260006044820181905260648201529192506001600160a01b0316906316c312cc906084016020604051808303816000875af1158015611c4e573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611c729190613f6b565b915082821015611ca95760405162461bcd60e51b8152602060048201526002602482015261413160f01b6044820152606401610bd9565b610100810151604080516001600160a01b03909216825260208201869052810183905233907fdcbc1c05240f31ff3ad067ef1ee35ce4997762752e3a095284754544f4c709d7906060015b60405180910390a25092915050565b60015433906001600160a01b03168114611d715760405162461bcd60e51b815260206004820152602960248201527f4f776e61626c6532537465703a2063616c6c6572206973206e6f7420746865206044820152683732bb9037bbb732b960b91b6064820152608401610bd9565b611d7a8161310b565b50565b60408051610120810182526000606082018190526080820181905260a0820181905260c0820181905260e08201819052610100820152600354600480546001600160a01b0390921680845260208401839052600554848601529351633407b0a360e01b81529081018290529192918290633407b0a390602401602060405180830381865afa158015611e13573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611e379190613f6b565b6060840152604051630898394560e41b8152600481018290526001600160a01b03831690638983945090602401602060405180830381865afa158015611e81573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611ea5919061402b565b6001600160a01b0390811660808501526060840151604051633dab407760e01b815260048101849052602481019190915290831690633dab4077906044016040805180830381865afa158015611eff573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611f2391906141c0565b6001600160a01b0390811660c086015290811660a085015260405163470646f960e11b81526004810183905290831690638e0c8df2906024016040805180830381865afa158015611f78573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611f9c91906141c0565b6001600160a01b0390811660e0860152166101008401525090919050565b33611fcd6000546001600160a01b031690565b6001600160a01b03161480611ff157503360009081526002602052604090205460ff165b61200d5760405162461bcd60e51b8152600401610bd990614048565b6003546001600160a01b03166120355760405162461bcd60e51b8152600401610bd99061408e565b600654604051630facca9760e21b8152600481018390526001600160a01b0390911690633eb32a5c90602401600060405180830381600087803b15801561207b57600080fd5b505af115801561208f573d6000803e3d6000fd5b50506040518381523392507f237bec3768bb0119dc6be68107f35ce9e4af60f186251c8d6813334e0cf168da915060200160405180910390a250565b6000610b766120d8611d7d565b60800151613124565b336120f46000546001600160a01b031690565b6001600160a01b0316148061211857503360009081526002602052604090205460ff165b6121345760405162461bcd60e51b8152600401610bd990614048565b7f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc26001600160a01b031663d0e30db0826040518263ffffffff1660e01b81526004016000604051808303818588803b15801561218f57600080fd5b505af11580156121a3573d6000803e3d6000fd5b505050505050565b60006121b56114b0565b6121bd6120cb565b610b7691906140ce565b6000336121dc6000546001600160a01b031690565b6001600160a01b0316148061220057503360009081526002602052604090205460ff165b61221c5760405162461bcd60e51b8152600401610bd990614048565b6003546001600160a01b03166122445760405162461bcd60e51b8152600401610bd99061408e565b61103b61224f611d7d565b60a001518484610e46565b612262612efc565b600080856001600160a01b0316858585604051612280929190613fe1565b60006040518083038185875af1925050503d80600081146122bd576040519150601f19603f3d011682016040523d82523d6000602084013e6122c2565b606091505b5091509150818190610ce85760405162461bcd60e51b8152600401610bd99190613f38565b6122ef612efc565b60006122fa83613124565b905061230783828461314a565b505050565b600a546000905b6009548110156114ee5760008181526008602052604090206001015461233990836140ce565b91508061234581614007565b915050612313565b612355612efc565b61230783838361314a565b6000336123756000546001600160a01b031690565b6001600160a01b0316148061239957503360009081526002602052604090205460ff165b6123b55760405162461bcd60e51b8152600401610bd990614048565b6003546001600160a01b03166123dd5760405162461bcd60e51b8152600401610bd99061408e565b60006123e7611d7d565b60e00151905060006123f7611d7d565b61010001519050600061240983613124565b9050600061241683613124565b905061242187613302565b600061242c85613124565b6124369084614168565b90508161244285613124565b61244c9190614168565b9550868610156124835760405162461bcd60e51b8152602060048201526002602482015261533160f01b6044820152606401610bd9565b61248d8187613393565b604080518281526020810188905233917fb851b91d0f335badac9719481388a2032f36714848770f4836ec35700ec2c1ba910160405180910390a2505050505092915050565b6124db612efc565b600780546001600160a01b0319166001600160a01b0392909216919091179055565b600080336125136000546001600160a01b031690565b6001600160a01b0316148061253757503360009081526002602052604090205460ff165b6125535760405162461bcd60e51b8152600401610bd990614048565b6003546001600160a01b031661257b5760405162461bcd60e51b8152600401610bd99061408e565b6125848461143d565b6125b55760405162461bcd60e51b81526020600482015260026024820152612b9960f11b6044820152606401610bd9565b6000848152600860209081526040808320815161010081018352815481526001820154938101939093526002810154918301919091526003810154606083015260048101546080830152600581015460a0830152600681015460c08301526007015460e0820181905290919061262a9061306c565b60208101519091506001600160a01b031661264d57600080935093505050612831565b6000612657611d7d565b610100810151909150600061266b82613124565b905061267a8560e00151611fba565b600061268e866060015187608001516130f5565b905061269b8160006129fb565b5060008460c001516001600160a01b0316633ba0b9a96040518163ffffffff1660e01b8152600401602060405180830381865afa1580156126e0573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906127049190613f6b565b670de0b6b3a764000083896080015161271d9190614168565b612727919061417f565b612731919061419e565b90506000612743828960a001516130f5565b9050612750816000612b7d565b508288606001511180156127675750612767612dff565b1561278d5761278b8660a00151848a606001516127849190614168565b6000610e46565b505b875199508361279b86613124565b6127a59190614168565b98508a8910156127dc5760405162461bcd60e51b8152602060048201526002602482015261573360f01b6044820152606401610bd9565b604080516001600160a01b0387168152602081018b90529081018b905233907ff341246adaac6f497bc2a656f546ab9e182111d630394f0c57c710a59a2cb5679060600160405180910390a250505050505050505b9250929050565b612840612efc565b612307838383612f56565b3361285e6000546001600160a01b031690565b6001600160a01b0316148061288257503360009081526002602052604090205460ff165b61289e5760405162461bcd60e51b8152600401610bd990614048565b6003546001600160a01b03166128c65760405162461bcd60e51b8152600401610bd99061408e565b600991909155600a55565b6128d9612efc565b6001600160a01b038116611d715760405162461bcd60e51b815260206004820152601860248201527f4954424f776e61626c653a207a65726f206164647265737300000000000000006044820152606401610bd9565b6000336129446000546001600160a01b031690565b6001600160a01b0316148061296857503360009081526002602052604090205460ff165b6129845760405162461bcd60e51b8152600401610bd990614048565b6003546001600160a01b03166129ac5760405162461bcd60e51b8152600401610bd99061408e565b6129ca6129c46129ba611d7d565b6101000151613124565b83611b61565b6040519091507fb36627d41b80e3867adc36deaed5334f9ea56f559579be82ffe726ad28f21f9b90600090a1919050565b600033612a106000546001600160a01b031690565b6001600160a01b03161480612a3457503360009081526002602052604090205460ff165b612a505760405162461bcd60e51b8152600401610bd990614048565b6003546001600160a01b0316612a785760405162461bcd60e51b8152600401610bd99061408e565b82600003612a8857506000611b47565b6000612a92611d7d565b80516020820151604051630411fb2760e31b81526004810191909152602481018790529192506001600160a01b03169063208fd938906044016020604051808303816000875af1158015612aea573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612b0e9190613f6b565b915082821015612b455760405162461bcd60e51b8152602060048201526002602482015261299960f11b6044820152606401610bd9565b604080518581526020810184905233917fb27cad604d1f2c15b71c3739896a23659e07c5103c9007951982faa88ace68a99101611cf4565b600033612b926000546001600160a01b031690565b6001600160a01b03161480612bb657503360009081526002602052604090205460ff165b612bd25760405162461bcd60e51b8152600401610bd990614048565b6003546001600160a01b0316612bfa5760405162461bcd60e51b8152600401610bd99061408e565b82600003612c0a57506000611b47565b6000612c14611d7d565b805160208201516060830151604051631a295cb960e01b815260048101929092526024820152604481018790529192506001600160a01b031690631a295cb9906064016080604051808303816000875af1158015612c76573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612c9a91906141ef565b50919350505082821015612cd55760405162461bcd60e51b8152602060048201526002602482015261533360f01b6044820152606401610bd9565b604080518581526020810184905233917fe780ff52e28831c2e9216084a47ac9ac6ec5baa73d6db0a9f726cf3010fe01469101611cf4565b612d15612efc565b600180546001600160a01b0383166001600160a01b03199091168117909155612d466000546001600160a01b031690565b6001600160a01b03167f38d16b8cac22d99fc7c124b9cd0de2d3fa1faef420bfe791d8c362d765e2270060405160405180910390a350565b612d86612efc565b6112c982823361314a565b6040516370a0823160e01b81526001600160a01b038281166004830152600091908416906370a0823190602401602060405180830381865afa158015612ddb573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061103b9190613f6b565b6000612e09611d7d565b60a001516001600160a01b0316632f13b60c6040518163ffffffff1660e01b8152600401602060405180830381865afa158015612e4a573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610b769190614225565b6000612e7d600a546009541190565b8015610b765750610b76611434565b612e94612efc565b6000612e9f82613124565b90506112c982823361314a565b600080546001600160a01b038381166001600160a01b0319831681178455604051919092169283917f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e09190a35050565b6000546001600160a01b03163314611b5f5760405162461bcd60e51b815260206004820181905260248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e65726044820152606401610bd9565b8015612fee57604051636eb1769f60e11b81523060048201526001600160a01b03838116602483015282919085169063dd62ed3e90604401602060405180830381865afa158015612fab573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612fcf9190613f6b565b10612fd957505050565b612fee6001600160a01b03841683600061357f565b6130026001600160a01b038416838361357f565b604080516001600160a01b038481168252602082018490528516917feded619173dbb378903f97d44ecec898a1c4876f445ae551e063113aef58b471910160405180910390a2505050565b6000670de0b6b3a7640000613062838561417f565b61103b919061419e565b6040805160608082018352600080835260208301528183015260065491516398efa65360e01b81526004810184905290916001600160a01b0316906398efa65390602401600060405180830381865afa1580156130cd573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f1916820160405261142c9190810190614247565b6000818310613104578161103b565b5090919050565b600180546001600160a01b0319169055611d7a81612eac565b60006001600160a01b038216156131435761313e826136c7565b61142c565b4792915050565b6001600160a01b0381166131925760405162461bcd60e51b815260206004820152600f60248201526e496e76616c6964206164647265737360881b6044820152606401610bd9565b600061319d84613124565b9050828110156131e45760405162461bcd60e51b8152602060048201526012602482015271496e73756666696369656e742066756e647360701b6044820152606401610bd9565b6001600160a01b03841661329b576000826001600160a01b03168460405160006040518083038185875af1925050503d806000811461323f576040519150601f19603f3d011682016040523d82523d6000602084013e613244565b606091505b50509050806132955760405162461bcd60e51b815260206004820152601760248201527f4e6174697665207472616e73666572206661696c65642e0000000000000000006044820152606401610bd9565b506132af565b6132af6001600160a01b0385168385613732565b836001600160a01b0316826001600160a01b03167f9207361cc2a04b9c7a06691df1eb87c6a63957ae88bf01d0d18c81e3d1272099856040516132f491815260200190565b60405180910390a350505050565b60075460405160009182916001600160a01b03909116908290613326908690614366565b60006040518083038185875af1925050503d8060008114613363576040519150601f19603f3d011682016040523d82523d6000602084013e613368565b606091505b509150915081819061338d5760405162461bcd60e51b8152600401610bd99190613f38565b50505050565b600061339d611d7d565b905060008160a001516001600160a01b0316633ba0b9a96040518163ffffffff1660e01b8152600401602060405180830381865afa1580156133e3573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906134079190613f6b565b905060008261010001516001600160a01b031663313ce5676040518163ffffffff1660e01b8152600401602060405180830381865afa15801561344e573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906134729190613f6b565b61347d90600a614466565b613487908661417f565b8360e001516001600160a01b031663313ce5676040518163ffffffff1660e01b8152600401602060405180830381865afa1580156134c9573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906134ed9190613f6b565b6134f890600a614466565b61350a86670de0b6b3a764000061417f565b613514919061417f565b61351e919061419e565b90506000612710846040015184613535919061417f565b61353f919061419e565b90508161354c8285614168565b11156121a35760405162461bcd60e51b8152602060048201526002602482015261229960f11b6044820152606401610bd9565b8015806135f95750604051636eb1769f60e11b81523060048201526001600160a01b03838116602483015284169063dd62ed3e90604401602060405180830381865afa1580156135d3573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906135f79190613f6b565b155b6136645760405162461bcd60e51b815260206004820152603660248201527f5361666545524332303a20617070726f76652066726f6d206e6f6e2d7a65726f60448201527520746f206e6f6e2d7a65726f20616c6c6f77616e636560501b6064820152608401610bd9565b6040516001600160a01b03831660248201526044810182905261230790849063095ea7b360e01b906064015b60408051601f198184030181529190526020810180516001600160e01b03166001600160e01b031990931692909217909152613762565b6040516370a0823160e01b81523060048201526000906001600160a01b038316906370a0823190602401602060405180830381865afa15801561370e573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061142c9190613f6b565b6040516001600160a01b03831660248201526044810182905261230790849063a9059cbb60e01b90606401613690565b60006137b7826040518060400160405280602081526020017f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c6564815250856001600160a01b03166138379092919063ffffffff16565b90508051600014806137d85750808060200190518101906137d89190614225565b6123075760405162461bcd60e51b815260206004820152602a60248201527f5361666545524332303a204552433230206f7065726174696f6e20646964206e6044820152691bdd081cdd58d8d9595960b21b6064820152608401610bd9565b6060613846848460008561384e565b949350505050565b6060824710156138af5760405162461bcd60e51b815260206004820152602660248201527f416464726573733a20696e73756666696369656e742062616c616e636520666f6044820152651c8818d85b1b60d21b6064820152608401610bd9565b600080866001600160a01b031685876040516138cb9190614366565b60006040518083038185875af1925050503d8060008114613908576040519150601f19603f3d011682016040523d82523d6000602084013e61390d565b606091505b509150915061391e87838387613929565b979650505050505050565b60608315613998578251600003613991576001600160a01b0385163b6139915760405162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e74726163740000006044820152606401610bd9565b5081613846565b61384683838151156139ad5781518083602001fd5b8060405162461bcd60e51b8152600401610bd99190613f38565b60008083601f8401126139d957600080fd5b50813567ffffffffffffffff8111156139f157600080fd5b6020830191508360208260051b850101111561283157600080fd5b60008060008060008060608789031215613a2557600080fd5b863567ffffffffffffffff80821115613a3d57600080fd5b613a498a838b016139c7565b90985096506020890135915080821115613a6257600080fd5b613a6e8a838b016139c7565b90965094506040890135915080821115613a8757600080fd5b50613a9489828a016139c7565b979a9699509497509295939492505050565b6001600160a01b0381168114611d7a57600080fd5b600080600060608486031215613ad057600080fd5b8335613adb81613aa6565b95602085013595506040909401359392505050565b6020808252825182820181905260009190848201906040850190845b81811015613b315783516001600160a01b031683529284019291840191600101613b0c565b50909695505050505050565b600060208284031215613b4f57600080fd5b5035919050565b600060208284031215613b6857600080fd5b813561103b81613aa6565b60008060408385031215613b8657600080fd5b8235613b9181613aa6565b91506020830135613ba181613aa6565b809150509250929050565b60008060408385031215613bbf57600080fd5b50508035926020909101359150565b81516001600160a01b03168152602080830151908201526040808301519082015260608083015190820152608080830151610120830191613c19908401826001600160a01b03169052565b5060a0830151613c3460a08401826001600160a01b03169052565b5060c0830151613c4f60c08401826001600160a01b03169052565b5060e0830151613c6a60e08401826001600160a01b03169052565b50610100928301516001600160a01b0316919092015290565b60008060008060608587031215613c9957600080fd5b8435613ca481613aa6565b935060208501359250604085013567ffffffffffffffff80821115613cc857600080fd5b818701915087601f830112613cdc57600080fd5b813581811115613ceb57600080fd5b886020828501011115613cfd57600080fd5b95989497505060200194505050565b600080600060608486031215613d2157600080fd5b8335613d2c81613aa6565b9250602084013591506040840135613d4381613aa6565b809150509250925092565b634e487b7160e01b600052604160045260246000fd5b604051610120810167ffffffffffffffff81118282101715613d8857613d88613d4e565b60405290565b6040516060810167ffffffffffffffff81118282101715613d8857613d88613d4e565b6040805190810167ffffffffffffffff81118282101715613d8857613d88613d4e565b604051601f8201601f1916810167ffffffffffffffff81118282101715613dfd57613dfd613d4e565b604052919050565b60008060408385031215613e1857600080fd5b823567ffffffffffffffff80821115613e3057600080fd5b818501915085601f830112613e4457600080fd5b8135602082821115613e5857613e58613d4e565b613e6a601f8301601f19168201613dd4565b92508183528781838601011115613e8057600080fd5b8181850182850137600091830181019190915290969401359450505050565b600080600060608486031215613eb457600080fd5b8335613ebf81613aa6565b92506020840135613ecf81613aa6565b929592945050506040919091013590565b60008060408385031215613ef357600080fd5b8235613efe81613aa6565b946020939093013593505050565b60005b83811015613f27578181015183820152602001613f0f565b8381111561338d5750506000910152565b6020815260008251806020840152613f57816040850160208701613f0c565b601f01601f19169190910160400192915050565b600060208284031215613f7d57600080fd5b5051919050565b634e487b7160e01b600052603260045260246000fd5b6000808335601e19843603018112613fb157600080fd5b83018035915067ffffffffffffffff821115613fcc57600080fd5b60200191503681900382131561283157600080fd5b8183823760009101908152919050565b634e487b7160e01b600052601160045260246000fd5b60006001820161401957614019613ff1565b5060010190565b805161142f81613aa6565b60006020828403121561403d57600080fd5b815161103b81613aa6565b60208082526026908201527f45786563757461626c653a2063616c6c6572206973206e6f742074686520657860408201526532b1baba37b960d11b606082015260800190565b602080825260029082015261413360f01b604082015260600190565b600080604083850312156140bd57600080fd5b505080516020909101519092909150565b600082198211156140e1576140e1613ff1565b500190565b600061012082840312156140f957600080fd5b614101613d64565b8251815261411160208401614020565b602082015260408301516040820152606083015160608201526080830151608082015260a083015160a082015260c083015160c082015260e083015160e08201526101008084015181830152508091505092915050565b60008282101561417a5761417a613ff1565b500390565b600081600019048311821515161561419957614199613ff1565b500290565b6000826141bb57634e487b7160e01b600052601260045260246000fd5b500490565b600080604083850312156141d357600080fd5b82516141de81613aa6565b6020840151909250613ba181613aa6565b6000806000806080858703121561420557600080fd5b505082516020840151604085015160609095015191969095509092509050565b60006020828403121561423757600080fd5b8151801515811461103b57600080fd5b6000602080838503121561425a57600080fd5b825167ffffffffffffffff8082111561427257600080fd5b908401906060828703121561428657600080fd5b61428e613d8e565b825181528383015161429f81613aa6565b81850152604083810151838111156142b657600080fd5b80850194505087601f8501126142cb57600080fd5b8351838111156142dd576142dd613d4e565b6142eb868260051b01613dd4565b818152868101945060069190911b85018601908982111561430b57600080fd5b948601945b818610156143545782868b0312156143285760008081fd5b614330613db1565b865161433b81613aa6565b8152868801518882015285529482019493860193614310565b91830191909152509695505050505050565b60008251614378818460208701613f0c565b9190910192915050565b600181815b808511156143bd5781600019048211156143a3576143a3613ff1565b808516156143b057918102915b93841c9390800290614387565b509250929050565b6000826143d457506001611b47565b816143e157506000611b47565b81600181146143f757600281146144015761441d565b6001915050611b47565b60ff84111561441257614412613ff1565b50506001821b611b47565b5060208310610133831016604e8410600b8410161715614440575081810a611b47565b61444a8383614382565b806000190482111561445e5761445e613ff1565b029392505050565b600061103b83836143c556fea2646970667358221220efdd2b45fc9d135e4b44e9647e827a08182387dd3b6e05e0c6d9924c04ebfeb764736f6c634300080f0033

Verified Source Code Partial Match

Compiler: v0.8.15+commit.e14f2714 EVM: london Optimization: Yes (200 runs)
ITBContract.sol 93 lines
/* SPDX-License-Identifier: UNLICENSED */
pragma solidity ^0.8.0;

import './utils/Withdrawable.sol';
import './utils/IWETH.sol';


/// @title ITBContract contract that implements common owner only functions accross all strategies
/// @author IntoTheBlock Corp
/// @dev Abstract
abstract contract ITBContract is Withdrawable {
    using SafeERC20 for IERC20;
    event ApproveToken(address indexed token, address guy, uint256 wad);
    address payable immutable public WNATIVE;

    uint constant ONE = 1e18;

    /// @param _executors Executor addresses
    constructor(address[] memory _executors, address payable _wnative) Executable(_executors) {
        WNATIVE = _wnative;
    }

    function _percentageAmount(uint _amount, uint _percentage) internal pure returns (uint) {
        return _amount * _percentage / ONE;
    }

    /// @notice Set allowance for a given token, amount and spender
    /// @param _token Token to spend
    /// @param _guy Spender
    /// @param _wad Max amount to spend
    function _approveToken(address _token, address _guy, uint256 _wad) internal {
        if (_wad != 0) {
            if (IERC20(_token).allowance(address(this), _guy) >= _wad)
                return;
            IERC20(_token).safeApprove(_guy, 0);
        }
        IERC20(_token).safeApprove(_guy, _wad);
        emit ApproveToken(_token, _guy, _wad);
    }

    /// @notice Check current allowance and, if necessary, set it to a new amount for a given token, amount and spender
    /// @param _token Token to spend
    /// @param _guy Spender
    /// @param _amount New max amount to spend
    function _checkAllowanceAndApprove(address _token, address _guy, uint256 _amount) internal {
        if (IERC20(_token).allowance(address(this), _guy) < _amount)
            _approveToken(_token, _guy, type(uint256).max);
    }

    /// @notice Only owner. Set allowance for a given token, amount and spender
    /// @param _token Token to spend
    /// @param _guy Spender
    /// @param _wad Max amount to spend
    function approveToken(address _token, address _guy, uint256 _wad) external onlyOwner {
        _approveToken(_token, _guy, _wad);
    }

    /// @notice Only owner. Revoke allowance for a given token and spender
    /// @param _token Token to spend
    /// @param _guy Spender
    function revokeToken(address _token, address _guy) external onlyOwner {
        _approveToken(_token, _guy, 0);
    }

    /// @notice Only owner. Execute an arbitrary call
    /// @param _to Target address
    /// @param _value Value (i. e. msg.value)
    /// @param _data Invocation data
    function execute(address _to, uint256 _value, bytes calldata _data) external payable onlyOwner {
        (bool success, bytes memory returnData) = _to.call{ value: _value }(_data);
        require(success, string(returnData));
    }

    /// @notice Only owner. Execute multiple arbitrary calls in order
    /// @param _tos Target address for each call
    /// @param _values Value for each call (i. e. msg.value)
    /// @param _datas Invocation data for each call
    function batchExecute(address[] calldata _tos, uint256[] calldata _values, bytes[] calldata _datas) external payable onlyOwner {
        require(_tos.length == _values.length && _tos.length == _datas.length, "Arguments length mismatch");
        for (uint256 i = 0; i < _tos.length; i++) {
            (bool success, bytes memory returnData) = _tos[i].call{ value: _values[i] }(_datas[i]);
            require(success, string(returnData));
        }
    }

    function wrapNative(uint256 _amount) public onlyExecutor {
        IWETH(WNATIVE).deposit{ value: _amount }();
    }

    function unwrapNative(uint256 _amount) public onlyExecutor {
        IWETH(WNATIVE).withdraw(_amount);
    }
}
Executable.sol 40 lines
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/access/Ownable2Step.sol";

/// @title Base contract that implements executor related functions
/// @author IntoTheBlock Corp
/// @dev Abstract
abstract contract Executable is Ownable2Step {
    mapping(address => bool) public executors;

    event ExecutorUpdated(address indexed executor, bool enabled);

    /// @param _executors Initial whitelisted executor addresses
    constructor(address[] memory _executors) {
        for (uint256 i = 0; i < _executors.length; i++) {
            addExecutor(_executors[i]);
        }
    }

    /// @notice Revert if call is not being made from the owner or an executor
    modifier onlyExecutor() {
        require(owner() == msg.sender || executors[msg.sender], "Executable: caller is not the executor");
        _;
    }

    /// @notice Only owner. Add an executor
    /// @param _executor New executor address
    function addExecutor(address _executor) public onlyOwner {
        emit ExecutorUpdated(_executor, true);
        executors[_executor] = true;
    }

    /// @notice Only owner. Remove an executor
    /// @param _executor Executor address to remove
    function removeExecutor(address _executor) external onlyOwner {
        emit ExecutorUpdated(_executor, false);
        executors[_executor] = false;
    }
}
IWETH.sol 25 lines
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

interface IWETH {
    function name() external view returns (string memory);
    function symbol() external view returns (string memory);
    function decimals() external view returns (uint8);

    event Approval(address indexed src, address indexed guy, uint256 wad);
    event Transfer(address indexed src, address indexed dst, uint256 wad);
    event Deposit(address indexed dst, uint256 wad);
    event Withdrawal(address indexed src, uint256 wad);

    function balanceOf(address) external view returns (uint256);
    function allowance(address, address) external view returns (uint256);

    fallback() external payable;
    receive() external payable;
    function deposit() external payable;
    function withdraw(uint256 wad) external;
    function totalSupply() external view returns (uint256);
    function approve(address guy, uint256 wad) external returns (bool);
    function transfer(address dst, uint256 wad) external returns (bool);
    function transferFrom(address src, address dst, uint256 wad) external returns (bool);
}
Ownable2StepWithShortcut.sol 11 lines
/* SPDX-License-Identifier: UNLICENSED */
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/access/Ownable2Step.sol";

abstract contract Ownable2StepWithShortcut is Ownable2Step {
    function transferOwnership1Step(address newOwner) public onlyOwner {
        require(newOwner != address(0), "ITBOwnable: zero address");
        _transferOwnership(newOwner);
    }
}
Withdrawable.sol 92 lines
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

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

/**
    Ensures that any contract that inherits from this contract is able to
    withdraw funds that are accidentally received or stuck.
 */

/// @title Base contract that implements withdrawal related functions
/// @author IntoTheBlock Corp
/// @dev Abstract
abstract contract Withdrawable is Executable {
    using SafeERC20 for IERC20;
    address constant ETHER = address(0);

    event LogWithdraw(
        address indexed _to,
        address indexed _asset_address,
        uint256 amount
    );

    receive() external payable {}

    /// @notice ERC20 or ETH balance of this contract given a token address
    /// @param _asset_address Token address or address(0) for ETH
    /// @return Balance    
    function _balance(address _asset_address) internal view returns (uint256) {
        return _asset_address == ETHER ? address(this).balance : _erc20Balance(_asset_address);
    }

    function _erc20Balance(address _asset_address) internal view returns (uint256) {
        return IERC20(_asset_address).balanceOf(address(this));
    }
    
    /// @notice ERC20 balance of given account
    /// @param _asset_address Token address 
    /// @param _account Account address 
    /// @return Balance  
    function balanceOf(address _asset_address, address _account) public view returns (uint256) {
        return IERC20(_asset_address).balanceOf(_account);
    }

    /// @notice Send the given amount of the given token or ETH to the given receiver
    /// @param _asset_address Token address or address(0) for ETH
    /// @param _amount Amount to send
    /// @param _to Receiver address
    function _withdraw_to(address _asset_address, uint256 _amount, address payable _to) internal {
        require(_to != address(0), 'Invalid address');
        uint256 balance = _balance(_asset_address);
        require(balance >= _amount, 'Insufficient funds');
        if (_asset_address == ETHER) {
            (bool success, ) = _to.call{value: _amount}(''); /* carry gas over so it works with contracts with custom fallback, we dont care about reentrancy on onlyOwner */
            require(success, 'Native transfer failed.');
        } else
            IERC20(_asset_address).safeTransfer(_to, _amount);
        emit LogWithdraw(_to, _asset_address, _amount);
    }

    /// @notice Only owner. Send the given amount of the given token or ETH to the caller
    /// @param _asset_address Token address or address(0) for ETH
    /// @param _amount Amount to send
    function withdraw(address _asset_address, uint256 _amount) external onlyOwner {
        _withdraw_to(_asset_address, _amount, payable(msg.sender));
    }

    /// @notice Only owner. Send the given amount of the given token or ETH to the given receiver
    /// @param _asset_address Token address or address(0) for ETH
    /// @param _amount Amount to send
    /// @param _to Receiver address
    function withdrawTo(address _asset_address, uint256 _amount, address payable _to) external onlyOwner {
        _withdraw_to(_asset_address, _amount, _to);
    }

    /// @notice Only owner. Send its entire balance of the given token or ETH to the caller
    /// @param _asset_address Token address or address(0) for ETH
    function withdrawAll(address _asset_address) external onlyOwner {
        uint256 balance = _balance(_asset_address);
        _withdraw_to(_asset_address, balance, payable(msg.sender));
    }

    /// @notice Only owner. Send its entire balance of the given token or ETH to the given receiver
    /// @param _asset_address Token address or address(0) for ETH
    /// @param _to Receiver address
    function withdrawAllTo(address _asset_address, address payable _to) external onlyOwner {
        uint256 balance = _balance(_asset_address);
        _withdraw_to(_asset_address, balance, _to);
    }
}
Ownable.sol 83 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (access/Ownable.sol)

pragma solidity ^0.8.0;

import "../utils/Context.sol";

/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * By default, the owner account will be the one that deploys the contract. This
 * can later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract Ownable is Context {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    constructor() {
        _transferOwnership(_msgSender());
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if the sender is not the owner.
     */
    function _checkOwner() internal view virtual {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby disabling any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}
Ownable2Step.sol 57 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.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() public virtual {
        address sender = _msgSender();
        require(pendingOwner() == sender, "Ownable2Step: caller is not the new owner");
        _transferOwnership(sender);
    }
}
IERC20.sol 78 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/IERC20.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);

    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `from` to `to` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address from, address to, uint256 amount) external returns (bool);
}
IERC20Permit.sol 90 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.4) (token/ERC20/extensions/IERC20Permit.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in
 * https://eips.ethereum.org/EIPS/eip-2612[EIP-2612].
 *
 * Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by
 * presenting a message signed by the account. By not relying on {IERC20-approve}, the token holder account doesn't
 * need to send a transaction, and thus is not required to hold Ether at all.
 *
 * ==== Security Considerations
 *
 * There are two important considerations concerning the use of `permit`. The first is that a valid permit signature
 * expresses an allowance, and it should not be assumed to convey additional meaning. In particular, it should not be
 * considered as an intention to spend the allowance in any specific way. The second is that because permits have
 * built-in replay protection and can be submitted by anyone, they can be frontrun. A protocol that uses permits should
 * take this into consideration and allow a `permit` call to fail. Combining these two aspects, a pattern that may be
 * generally recommended is:
 *
 * ```solidity
 * function doThingWithPermit(..., uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) public {
 *     try token.permit(msg.sender, address(this), value, deadline, v, r, s) {} catch {}
 *     doThing(..., value);
 * }
 *
 * function doThing(..., uint256 value) public {
 *     token.safeTransferFrom(msg.sender, address(this), value);
 *     ...
 * }
 * ```
 *
 * Observe that: 1) `msg.sender` is used as the owner, leaving no ambiguity as to the signer intent, and 2) the use of
 * `try/catch` allows the permit to fail and makes the code tolerant to frontrunning. (See also
 * {SafeERC20-safeTransferFrom}).
 *
 * Additionally, note that smart contract wallets (such as Argent or Safe) are not able to produce permit signatures, so
 * contracts should have entry points that don't rely on permit.
 */
interface IERC20Permit {
    /**
     * @dev Sets `value` as the allowance of `spender` over ``owner``'s tokens,
     * given ``owner``'s signed approval.
     *
     * IMPORTANT: The same issues {IERC20-approve} has related to transaction
     * ordering also apply here.
     *
     * Emits an {Approval} event.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     * - `deadline` must be a timestamp in the future.
     * - `v`, `r` and `s` must be a valid `secp256k1` signature from `owner`
     * over the EIP712-formatted function arguments.
     * - the signature must use ``owner``'s current nonce (see {nonces}).
     *
     * For more information on the signature format, see the
     * https://eips.ethereum.org/EIPS/eip-2612#specification[relevant EIP
     * section].
     *
     * CAUTION: See Security Considerations above.
     */
    function permit(
        address owner,
        address spender,
        uint256 value,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) external;

    /**
     * @dev Returns the current nonce for `owner`. This value must be
     * included whenever a signature is generated for {permit}.
     *
     * Every successful call to {permit} increases ``owner``'s nonce by one. This
     * prevents a signature from being used multiple times.
     */
    function nonces(address owner) external view returns (uint256);

    /**
     * @dev Returns the domain separator used in the encoding of the signature for {permit}, as defined by {EIP712}.
     */
    // solhint-disable-next-line func-name-mixedcase
    function DOMAIN_SEPARATOR() external view returns (bytes32);
}
SafeERC20.sol 143 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.3) (token/ERC20/utils/SafeERC20.sol)

pragma solidity ^0.8.0;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

pragma solidity ^0.8.1;

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

        return account.code.length > 0;
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

pragma solidity ^0.8.0;

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

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

    function _contextSuffixLength() internal view virtual returns (uint256) {
        return 0;
    }
}
Math.sol 339 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/math/Math.sol)

pragma solidity ^0.8.0;

/**
 * @dev Standard math utilities missing in the Solidity language.
 */
library Math {
    enum Rounding {
        Down, // Toward negative infinity
        Up, // Toward infinity
        Zero // Toward zero
    }

    /**
     * @dev Returns the largest of two numbers.
     */
    function max(uint256 a, uint256 b) internal pure returns (uint256) {
        return a > b ? a : b;
    }

    /**
     * @dev Returns the smallest of two numbers.
     */
    function min(uint256 a, uint256 b) internal pure returns (uint256) {
        return a < b ? a : b;
    }

    /**
     * @dev Returns the average of two numbers. The result is rounded towards
     * zero.
     */
    function average(uint256 a, uint256 b) internal pure returns (uint256) {
        // (a + b) / 2 can overflow.
        return (a & b) + (a ^ b) / 2;
    }

    /**
     * @dev Returns the ceiling of the division of two numbers.
     *
     * This differs from standard division with `/` in that it rounds up instead
     * of rounding down.
     */
    function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
        // (a + b - 1) / b can overflow on addition, so we distribute.
        return a == 0 ? 0 : (a - 1) / b + 1;
    }

    /**
     * @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
     * @dev Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv)
     * with further edits by Uniswap Labs also under MIT license.
     */
    function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
        unchecked {
            // 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use
            // 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(x, y, not(0))
                prod0 := mul(x, y)
                prod1 := sub(sub(mm, prod0), lt(mm, prod0))
            }

            // Handle non-overflow cases, 256 by 256 division.
            if (prod1 == 0) {
                // Solidity will revert if denominator == 0, unlike the div opcode on its own.
                // The surrounding unchecked block does not change this fact.
                // See https://docs.soliditylang.org/en/latest/control-structures.html#checked-or-unchecked-arithmetic.
                return prod0 / denominator;
            }

            // Make sure the result is less than 2^256. Also prevents denominator == 0.
            require(denominator > prod1, "Math: mulDiv overflow");

            ///////////////////////////////////////////////
            // 512 by 256 division.
            ///////////////////////////////////////////////

            // Make division exact by subtracting the remainder from [prod1 prod0].
            uint256 remainder;
            assembly {
                // Compute remainder using mulmod.
                remainder := mulmod(x, y, denominator)

                // Subtract 256 bit number from 512 bit number.
                prod1 := sub(prod1, gt(remainder, prod0))
                prod0 := sub(prod0, remainder)
            }

            // Factor powers of two out of denominator and compute largest power of two divisor of denominator. Always >= 1.
            // See https://cs.stackexchange.com/q/138556/92363.

            // Does not overflow because the denominator cannot be zero at this stage in the function.
            uint256 twos = denominator & (~denominator + 1);
            assembly {
                // Divide denominator by twos.
                denominator := div(denominator, twos)

                // Divide [prod1 prod0] by twos.
                prod0 := div(prod0, twos)

                // Flip twos such that it is 2^256 / twos. If twos is zero, then it becomes one.
                twos := add(div(sub(0, twos), twos), 1)
            }

            // Shift in bits from prod1 into prod0.
            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 for
            // four bits. That is, denominator * inv = 1 mod 2^4.
            uint256 inverse = (3 * denominator) ^ 2;

            // Use the 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.
            inverse *= 2 - denominator * inverse; // inverse mod 2^8
            inverse *= 2 - denominator * inverse; // inverse mod 2^16
            inverse *= 2 - denominator * inverse; // inverse mod 2^32
            inverse *= 2 - denominator * inverse; // inverse mod 2^64
            inverse *= 2 - denominator * inverse; // inverse mod 2^128
            inverse *= 2 - denominator * inverse; // 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 preconditions 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 * inverse;
            return result;
        }
    }

    /**
     * @notice Calculates x * y / denominator with full precision, following the selected rounding direction.
     */
    function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
        uint256 result = mulDiv(x, y, denominator);
        if (rounding == Rounding.Up && mulmod(x, y, denominator) > 0) {
            result += 1;
        }
        return result;
    }

    /**
     * @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded down.
     *
     * Inspired by Henry S. Warren, Jr.'s "Hacker's Delight" (Chapter 11).
     */
    function sqrt(uint256 a) internal pure returns (uint256) {
        if (a == 0) {
            return 0;
        }

        // For our first guess, we get the biggest power of 2 which is smaller than the square root of the target.
        //
        // We know that the "msb" (most significant bit) of our target number `a` is a power of 2 such that we have
        // `msb(a) <= a < 2*msb(a)`. This value can be written `msb(a)=2**k` with `k=log2(a)`.
        //
        // This can be rewritten `2**log2(a) <= a < 2**(log2(a) + 1)`
        // → `sqrt(2**k) <= sqrt(a) < sqrt(2**(k+1))`
        // → `2**(k/2) <= sqrt(a) < 2**((k+1)/2) <= 2**(k/2 + 1)`
        //
        // Consequently, `2**(log2(a) / 2)` is a good first approximation of `sqrt(a)` with at least 1 correct bit.
        uint256 result = 1 << (log2(a) >> 1);

        // At this point `result` is an estimation with one bit of precision. We know the true value is a uint128,
        // since it is the square root of a uint256. Newton's method converges quadratically (precision doubles at
        // every iteration). We thus need at most 7 iteration to turn our partial result with one bit of precision
        // into the expected uint128 result.
        unchecked {
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            result = (result + a / result) >> 1;
            return min(result, a / result);
        }
    }

    /**
     * @notice Calculates sqrt(a), following the selected rounding direction.
     */
    function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = sqrt(a);
            return result + (rounding == Rounding.Up && result * result < a ? 1 : 0);
        }
    }

    /**
     * @dev Return the log in base 2, rounded down, of a positive value.
     * Returns 0 if given 0.
     */
    function log2(uint256 value) internal pure returns (uint256) {
        uint256 result = 0;
        unchecked {
            if (value >> 128 > 0) {
                value >>= 128;
                result += 128;
            }
            if (value >> 64 > 0) {
                value >>= 64;
                result += 64;
            }
            if (value >> 32 > 0) {
                value >>= 32;
                result += 32;
            }
            if (value >> 16 > 0) {
                value >>= 16;
                result += 16;
            }
            if (value >> 8 > 0) {
                value >>= 8;
                result += 8;
            }
            if (value >> 4 > 0) {
                value >>= 4;
                result += 4;
            }
            if (value >> 2 > 0) {
                value >>= 2;
                result += 2;
            }
            if (value >> 1 > 0) {
                result += 1;
            }
        }
        return result;
    }

    /**
     * @dev Return the log in base 2, following the selected rounding direction, of a positive value.
     * Returns 0 if given 0.
     */
    function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = log2(value);
            return result + (rounding == Rounding.Up && 1 << result < value ? 1 : 0);
        }
    }

    /**
     * @dev Return the log in base 10, rounded down, of a positive value.
     * Returns 0 if given 0.
     */
    function log10(uint256 value) internal pure returns (uint256) {
        uint256 result = 0;
        unchecked {
            if (value >= 10 ** 64) {
                value /= 10 ** 64;
                result += 64;
            }
            if (value >= 10 ** 32) {
                value /= 10 ** 32;
                result += 32;
            }
            if (value >= 10 ** 16) {
                value /= 10 ** 16;
                result += 16;
            }
            if (value >= 10 ** 8) {
                value /= 10 ** 8;
                result += 8;
            }
            if (value >= 10 ** 4) {
                value /= 10 ** 4;
                result += 4;
            }
            if (value >= 10 ** 2) {
                value /= 10 ** 2;
                result += 2;
            }
            if (value >= 10 ** 1) {
                result += 1;
            }
        }
        return result;
    }

    /**
     * @dev Return the log in base 10, following the selected rounding direction, of a positive value.
     * Returns 0 if given 0.
     */
    function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = log10(value);
            return result + (rounding == Rounding.Up && 10 ** result < value ? 1 : 0);
        }
    }

    /**
     * @dev Return the log in base 256, rounded down, of a positive value.
     * Returns 0 if given 0.
     *
     * Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string.
     */
    function log256(uint256 value) internal pure returns (uint256) {
        uint256 result = 0;
        unchecked {
            if (value >> 128 > 0) {
                value >>= 128;
                result += 16;
            }
            if (value >> 64 > 0) {
                value >>= 64;
                result += 8;
            }
            if (value >> 32 > 0) {
                value >>= 32;
                result += 4;
            }
            if (value >> 16 > 0) {
                value >>= 16;
                result += 2;
            }
            if (value >> 8 > 0) {
                result += 1;
            }
        }
        return result;
    }

    /**
     * @dev Return the log in base 256, following the selected rounding direction, of a positive value.
     * Returns 0 if given 0.
     */
    function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
        unchecked {
            uint256 result = log256(value);
            return result + (rounding == Rounding.Up && 1 << (result << 3) < value ? 1 : 0);
        }
    }
}
BitMaps.sol 51 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/structs/BitMaps.sol)
pragma solidity ^0.8.0;

/**
 * @dev Library for managing uint256 to bool mapping in a compact and efficient way, providing the keys are sequential.
 * Largely inspired by Uniswap's https://github.com/Uniswap/merkle-distributor/blob/master/contracts/MerkleDistributor.sol[merkle-distributor].
 */
library BitMaps {
    struct BitMap {
        mapping(uint256 => uint256) _data;
    }

    /**
     * @dev Returns whether the bit at `index` is set.
     */
    function get(BitMap storage bitmap, uint256 index) internal view returns (bool) {
        uint256 bucket = index >> 8;
        uint256 mask = 1 << (index & 0xff);
        return bitmap._data[bucket] & mask != 0;
    }

    /**
     * @dev Sets the bit at `index` to the boolean `value`.
     */
    function setTo(BitMap storage bitmap, uint256 index, bool value) internal {
        if (value) {
            set(bitmap, index);
        } else {
            unset(bitmap, index);
        }
    }

    /**
     * @dev Sets the bit at `index`.
     */
    function set(BitMap storage bitmap, uint256 index) internal {
        uint256 bucket = index >> 8;
        uint256 mask = 1 << (index & 0xff);
        bitmap._data[bucket] |= mask;
    }

    /**
     * @dev Unsets the bit at `index`.
     */
    function unset(BitMap storage bitmap, uint256 index) internal {
        uint256 bucket = index >> 8;
        uint256 mask = 1 << (index & 0xff);
        bitmap._data[bucket] &= ~mask;
    }
}
PositionManager.sol 349 lines
/* SPDX-License-Identifier: UNLICENSED */
pragma solidity ^0.8.0;

import '@itb/quant-common/contracts/solidity8/ITBContract.sol';
import '@openzeppelin/contracts/utils/math/Math.sol';
import './interfaces/IVault.sol';
import './interfaces/IWithdrawal.sol';
import './interfaces/IAsset.sol';

contract PositionManager is ITBContract {
    struct PositionConfig {
        address vault;
        IVault.Id id;
        uint deviation_limit_bps;
    }

    struct QueuedWithdrawal {
        uint lpt_burnt;
        uint total_amount_out;
        uint ra_amount;
        uint ct_amount;
        uint ds_amount;
        uint pa_amount;
        uint claimable_at;
        bytes32 withdrawalId;
    }

    struct VaultConfig {
        IVault vault;
        IVault.Id id;
        uint deviation_limit_bps;
        uint ds_id;
        address lv;
        address ct;
        address ds;
        address pa;
        address ra;
    }

    event Deposit(address indexed caller, address underlying, uint underlying_change, uint lpt_change);
    event SwapPaToRa(address indexed caller, uint amount, uint ra_out);
    event SwapCtDsToRa(address indexed caller, uint amount, uint ra_out);
    event SwapDsPaToRa(address indexed caller, uint amount, uint ra_out);
    event RedeemExpiredCt(address indexed caller, uint amount, uint ra_out);
    event StartWithdrawal(address indexed caller, address ra, uint withdrawal_index);
    event ClaimWithdrawal(address indexed caller, bytes32 withdrawal_id);
    event Withdraw(address indexed caller, address underlying, uint underlying_change, uint lpt_change);
    event Assemble();
    event Disassemble();
    event UpdatePositionConfig();

    PositionConfig public positionConfig;
    IWithdrawal public withdrawalContract;
    address router;

    mapping(uint => QueuedWithdrawal) public withdrawalQueue;
    uint public cumulativeWithdrawalsQueued;
    uint public indexNextWithdrawal;

    constructor(
        address[] memory _executors,
        address payable _wnative,
        address _vault,
        IVault.Id _id,
        uint _deviation_limit_bps,
        address _router
    ) ITBContract(_executors, _wnative) {
        updatePositionConfig(_vault, _id, _deviation_limit_bps);
        update1InchRouter(_router);
    }

    modifier hasConfig() {
        require(positionConfig.vault != address(0), 'A3'); // position_config is missing
        _;
    }

    function VERSION() external pure returns (string memory) {
        return '1.0.0';
    }

    function updatePositionConfig(address _vault, IVault.Id _id, uint _deviation_limit_bps) public onlyOwner {
        positionConfig = PositionConfig(_vault, _id, _deviation_limit_bps);
        withdrawalContract = IWithdrawal(IVault(_vault).getWithdrawalContract());
        emit UpdatePositionConfig();
    }

    function update1InchRouter(address _router) public onlyOwner {
        router = address(_router);
    }

    function vaultConfig() public view returns (VaultConfig memory vault_config) {
        IVault vault = IVault(positionConfig.vault);
        IVault.Id id = positionConfig.id;
        vault_config.vault = vault;
        vault_config.id = id;
        vault_config.deviation_limit_bps = positionConfig.deviation_limit_bps;
        vault_config.ds_id = vault.lastDsId(id);
        vault_config.lv = vault.lvAsset(id);
        (vault_config.ct, vault_config.ds) = vault.swapAsset(id, vault_config.ds_id);
        (vault_config.ra, vault_config.pa) = vault.underlyingAsset(id);
    }

    /*
     * Core Functions
     */

    function _getWithdrawal(bytes32 _withdrawal_id) internal view returns (IWithdrawal.WithdrawalInfo memory) {
        return withdrawalContract.getWithdrawal(_withdrawal_id);
    }

    function _swap1Inch(bytes memory _swap_data) internal {
        (bool success, bytes memory returnData) = router.call{value: 0}(_swap_data);
        require(success, string(returnData));
    }

    function _validateRaPaExchangeRate(uint _pa_in, uint _ra_out) internal view {
        VaultConfig memory c = vaultConfig();

        uint initial_exchange_rate = IAsset(c.ct).exchangeRate();
        uint executed_price_1e18 = _ra_out * 1e18 * 10**IAsset(c.pa).decimals() / (_pa_in * 10**IAsset(c.ra).decimals());

        uint margin = initial_exchange_rate * c.deviation_limit_bps / 10000;
        require(initial_exchange_rate - margin <= executed_price_1e18, 'E2');
    }

    function depositLv(uint _amount, uint _min_lpt_out) public onlyExecutor hasConfig returns (uint lpt_amount) {
        VaultConfig memory c = vaultConfig();
        lpt_amount = c.vault.depositLv(c.id, _amount, 0, 0);
        require(lpt_amount >= _min_lpt_out, 'A1');
        emit Deposit(msg.sender, c.ra, _amount, lpt_amount);
    }

    function swapPaToRa(bytes memory _swap_data, uint _min_ra_out) external onlyExecutor hasConfig returns (uint ra_out) {
        address pa = vaultConfig().pa;
        address ra = vaultConfig().ra;
        uint pa_before = _balance(pa);
        uint ra_before = _balance(ra);
        _swap1Inch(_swap_data);
        uint pa_in = pa_before - _balance(pa);
        ra_out = _balance(ra) - ra_before;
        require(ra_out >= _min_ra_out, 'S1');
        _validateRaPaExchangeRate(pa_in, ra_out);
        emit SwapPaToRa(msg.sender, pa_in, ra_out);
    }

    function swapCtDsToRa(uint _amount, uint _min_ra_out) public onlyExecutor hasConfig returns (uint ra_out) {
        if (_amount == 0)
            return 0;
        VaultConfig memory c = vaultConfig();
        ra_out = c.vault.returnRaWithCtDs(c.id, _amount);
        require(ra_out >= _min_ra_out, 'S2');
        emit SwapCtDsToRa(msg.sender, _amount, ra_out);
    }

    function swapDsPaToRa(uint _amount, uint _min_ra_out) public onlyExecutor hasConfig returns (uint ra_out) {
        if (_amount == 0)
            return 0;
        VaultConfig memory c = vaultConfig();
        (ra_out, , , ) = c.vault.redeemRaWithDsPa(c.id, c.ds_id, _amount);
        require(ra_out >= _min_ra_out, 'S3');
        emit SwapDsPaToRa(msg.sender, _amount, ra_out);
    }

    function redeemExpiredCt(address _ct, uint _amount, uint _min_ra_out) public onlyExecutor hasConfig returns (uint ra_out) {
        if (_amount == 0)
            return 0;
        VaultConfig memory c = vaultConfig();
        uint ds_id = IAsset(_ct).dsId();
        (, ra_out) = c.vault.redeemWithExpiredCt(c.id, ds_id, _amount);
        require(ra_out >= _min_ra_out, 'S4');
        emit RedeemExpiredCt(msg.sender, _amount, ra_out);
    }

    function startWithdrawal(uint _lpt_amount, uint _amount_out_min) public onlyExecutor hasConfig returns (uint amount_out) {
        if (_lpt_amount == 0) return 0;

        VaultConfig memory c = vaultConfig();
        uint withdrawal_index = cumulativeWithdrawalsQueued;
        cumulativeWithdrawalsQueued++;

        IVault.RedeemEarlyResult memory redeem_result = c.vault.redeemEarlyLv(IVault.RedeemEarlyParams(c.id, _lpt_amount, 0, c.vault.expiry(c.id), 0, 0, 0));
        IWithdrawal.WithdrawalInfo memory w = _getWithdrawal(redeem_result.withdrawalId);

        uint ra_received = redeem_result.raReceivedFromAmm + redeem_result.raIdleReceived;
        uint ct_received = redeem_result.ctReceivedFromAmm + redeem_result.ctReceivedFromVault;
        uint ds_received = redeem_result.dsReceived;
        
        {
            uint pa_received = redeem_result.paReceived;
            uint exchange_rate = IAsset(c.ds).exchangeRate();

            // At completeWithdrawal CT/DS are redeemed for RA at 1:1 ratio
            uint ra_from_ds_ct = Math.min(ct_received, ds_received);

            // At completeWithdrawal PA/DS are redeemed for RA based on exchange rate
            uint remaining_ds_as_pa_amount = (ds_received - ra_from_ds_ct) * ONE / exchange_rate;
            uint ra_from_ds_pa = Math.min(remaining_ds_as_pa_amount, pa_received) * exchange_rate * (1e20 - c.vault.baseRedemptionFee(c.id)) / 1e38;

            amount_out = ra_received + ra_from_ds_ct + ra_from_ds_pa;
            require(amount_out >= _amount_out_min, 'W1');
        }

        withdrawalQueue[withdrawal_index] = QueuedWithdrawal({
            lpt_burnt: _lpt_amount,
            total_amount_out: amount_out,
            ra_amount: ra_received,
            ct_amount: ct_received,
            ds_amount: ds_received,
            pa_amount: redeem_result.paReceived,
            claimable_at: w.claimableAt,
            withdrawalId: redeem_result.withdrawalId
        });

        emit StartWithdrawal(msg.sender, c.ra, withdrawal_index);
    }

    function claimWithdrawal(bytes32 _withdrawal_id) public onlyExecutor hasConfig {
        withdrawalContract.claimToSelf(_withdrawal_id);
        emit ClaimWithdrawal(msg.sender, _withdrawal_id);
    }

    function completeWithdrawal(uint _withdrawal_index, uint _min_ra_out) public onlyExecutor hasConfig returns (uint lpt_burnt, uint ra_out) {
        require(withdrawalIsReady(_withdrawal_index), 'W2');

        QueuedWithdrawal memory w = withdrawalQueue[_withdrawal_index];
        IWithdrawal.WithdrawalInfo memory w_info = _getWithdrawal(w.withdrawalId);
        if (w_info.owner == address(0)) /* already claimed */
            return (0, 0);
        
        VaultConfig memory c = vaultConfig();
        address ra = c.ra;
        uint ra_before = _balance(ra);

        claimWithdrawal(w.withdrawalId);
        
        uint ct_ds_amount = Math.min(w.ct_amount, w.ds_amount);
        swapCtDsToRa(ct_ds_amount, 0);

        uint remaining_ds_as_pa_amount = (w.ds_amount - ct_ds_amount) * ONE / IAsset(c.ds).exchangeRate();
        uint ds_pa_amount = Math.min(remaining_ds_as_pa_amount, w.pa_amount);
        swapDsPaToRa(ds_pa_amount, 0);

        if (w.ct_amount > ct_ds_amount && isCtExpired())
            redeemExpiredCt(c.ct, w.ct_amount - ct_ds_amount, 0);

        lpt_burnt = w.lpt_burnt;
        ra_out = _balance(ra) - ra_before;
        require(ra_out >= _min_ra_out, 'W3');

        emit Withdraw(msg.sender, ra, ra_out, lpt_burnt);
    }

    function completeNextWithdrawal(uint _min_ra_out) public onlyExecutor hasConfig returns (uint lpt_burnt, uint ra_out) {
        uint i = indexNextWithdrawal;
        indexNextWithdrawal++;
        (lpt_burnt, ra_out) = completeWithdrawal(i, _min_ra_out);
    }

    function completeNextWithdrawals(uint _min_ra_out) public onlyExecutor hasConfig returns (uint total_lpt_burnt, uint total_ra_out) {
        while (canCompleteWithdrawals()) {
            (uint lpt_burnt, uint ra_out) = completeNextWithdrawal(0);
            total_lpt_burnt += lpt_burnt;
            total_ra_out += ra_out;
        }
        require(total_ra_out >= _min_ra_out, 'W4');
    }

    /// @dev Should not be used under normal circumstances
    function overrideWithdrawalIndexes(uint _cumulativeWithdrawalsQueued, uint _indexNextWithdrawal) external onlyExecutor hasConfig {
        cumulativeWithdrawalsQueued = _cumulativeWithdrawalsQueued;
        indexNextWithdrawal = _indexNextWithdrawal;
    }

    function assemble(uint _min_lpt_out) external onlyExecutor hasConfig returns (uint lpt_amount) {
        lpt_amount = depositLv(_balance(vaultConfig().ra), _min_lpt_out);
        emit Assemble();
    }

    function disassemble(uint _percentage, uint _min_ra_out) public onlyExecutor hasConfig returns (uint ra_out) {
        require(_percentage <= ONE, 'P1');

        if (_percentage > 0) {
            uint lpt_amount = _percentageAmount(getLPTBalance(), _percentage);
            startWithdrawal(lpt_amount, 0);
        }
        (, ra_out) = completeNextWithdrawals(_min_ra_out);

        emit Disassemble();
    }

    function fullDisassemble(uint _min_ra_out) external onlyExecutor hasConfig returns (uint) {
        return disassemble(ONE, _min_ra_out);
    }

    function redeemExpiredCtByConfig(uint _amount, uint _min_ra_out) public onlyExecutor hasConfig returns (uint ra_out) {
        return redeemExpiredCt(vaultConfig().ct, _amount, _min_ra_out);
    }

    /*
     * View Functions
     */

    function getLPTBalance() public view returns (uint) {
        return _balance(vaultConfig().lv);
    }

    function lptPendingOfWithdraw() public view returns (uint amount_pending) {
        for (uint i = indexNextWithdrawal; i < cumulativeWithdrawalsQueued; i++) amount_pending += withdrawalQueue[i].lpt_burnt;
    }

    function underlyingPendingOfWithdraw() public view returns (uint amount_pending) {
        for (uint i = indexNextWithdrawal; i < cumulativeWithdrawalsQueued; i++) amount_pending += withdrawalQueue[i].total_amount_out;
    }

    function getTotalLPT() public view returns (uint) {
        return getLPTBalance() + lptPendingOfWithdraw();
    }

    function getWithdrawalDelay() public view returns (uint) {
        return withdrawalContract.DELAY();
    }

    function haveWithdrawalsQueued() public view returns (bool) {
        return cumulativeWithdrawalsQueued > indexNextWithdrawal;
    }

    function withdrawalIsReady(uint _withdrawal_index) public view returns (bool) {
        QueuedWithdrawal memory w = withdrawalQueue[_withdrawal_index];
        return block.timestamp >= w.claimable_at;
    }

    function nextWithdrawalIsReady() public view returns (bool) {
        return withdrawalIsReady(indexNextWithdrawal);
    }

    function canCompleteWithdrawals() public view returns (bool) {
        return haveWithdrawalsQueued() && nextWithdrawalIsReady();
    }

    function isCtExpired() public view returns (bool) {
        return IAsset(vaultConfig().ct).isExpired();
    }

    function getPositionAssets() public view returns (address[] memory) {
        address[] memory assets = new address[](1);
        assets[0] = vaultConfig().ra;
        return assets;
    }
}
PositionManagerOwnable2StepWithShortcut.sol 9 lines
/* SPDX-License-Identifier: UNLICENSED */
pragma solidity ^0.8.0;

import './PositionManager.sol';
import '@itb/quant-common/contracts/solidity8/utils/Ownable2StepWithShortcut.sol';

contract PositionManagerOwnable2StepWithShortcut is PositionManager, Ownable2StepWithShortcut {
    constructor(address[] memory _executors, address payable _wnative, address _vault, IVault.Id _id, uint _deviation_limit_bps, address _router) PositionManager(_executors, _wnative, _vault, _id, _deviation_limit_bps, _router) {}
}
IAsset.sol 12 lines
/* SPDX-License-Identifier: UNLICENSED */
pragma solidity ^0.8.0;

interface IAsset {
    function dsId() external view returns (uint256);

    function isExpired() external view returns (bool);

    function exchangeRate() external view returns (uint256);

    function decimals() external view returns (uint256);
}
IVault.sol 76 lines
/* SPDX-License-Identifier: UNLICENSED */
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/utils/structs/BitMaps.sol";

interface IVault {
    type Id is bytes32;

    function lvAsset(Id id) external view returns (address lv);

    /**
     * @notice Deposit a wrapped asset into a given vault
     * @param id The Module id that is used to reference both psm and lv of a given pair
     * @param amount The amount of the redemption asset(ra) deposited
     */
    function depositLv(Id id, uint256 amount, uint256 raTolerance, uint256 ctTolerance) external returns (uint256 received);

    /**
     * @notice Redeem lv before expiry
     * @param id The Module id that is used to reference both psm and lv of a given pair
     * @param amount The amount of the asset to be redeemed
     * @param amountOutMin The minimum amount of the asset to be received
     */

    struct RedeemEarlyParams {
        Id id;
        uint256 amount;
        uint256 amountOutMin;
        uint256 ammDeadline;
        uint256 ctAmountOutMin;
        uint256 dsAmountOutMin;
        uint256 paAmountOutMin;
    }

    struct RedeemEarlyResult {
        Id id;
        address receiver;
        uint256 raReceivedFromAmm;
        uint256 raIdleReceived;
        uint256 paReceived;
        uint256 ctReceivedFromAmm;
        uint256 ctReceivedFromVault;
        uint256 dsReceived;
        bytes32 withdrawalId;
    }

    function redeemEarlyLv(RedeemEarlyParams memory redeemParams) external returns (RedeemEarlyResult memory result);

    /**
     * Returns the amount of AMM LP tokens that the vault holds
     * @param id The Module id that is used to reference both psm and lv of a given pair
     */
    function vaultLp(Id id) external view returns (uint256);

    function valueLocked(Id id, bool ra) external view returns (uint256);
    
    function expiry(Id id) external view returns (uint256);

    function baseRedemptionFee(Id id) external view returns (uint256);

    function returnRaWithCtDs(Id id, uint256 amount) external returns (uint256 ra);

    function lastDsId(Id id) external view returns (uint256 dsId);

    function redeemWithExpiredCt(Id id, uint256 dsId, uint256 amount) external returns (uint256 accruedPa, uint256 accruedRa);

    function receiveLeftoverFunds(Id id, uint256 amount) external;

    function underlyingAsset(Id id) external view returns (address ra, address pa);

    function getWithdrawalContract() external view returns (address withdrawalContract);

    function swapAsset(Id id, uint256 dsId) external view returns (address ct, address ds);

    function redeemRaWithDsPa(Id id, uint256 dsId, uint256 amount) external returns (uint256 received, uint256 _exchangeRate, uint256 fee, uint256 dsUsed);
}
IWithdrawal.sol 21 lines
/* SPDX-License-Identifier: UNLICENSED */
pragma solidity ^0.8.0;

interface IWithdrawal {
    struct Tokens {
        address token;
        uint256 amount;
    }

    struct WithdrawalInfo {
        uint256 claimableAt;
        address owner;
        Tokens[] tokens;
    }

    function DELAY() external view returns (uint256);

    function claimToSelf(bytes32 withdrawalId) external;

    function getWithdrawal(bytes32 withdrawalId) external view returns (WithdrawalInfo memory);
}

Read Contract

VERSION 0xffa1ad74 → string
WNATIVE 0xb381cf40 → address
balanceOf 0xf7888aec → uint256
canCompleteWithdrawals 0xf9f29356 → bool
cumulativeWithdrawalsQueued 0xf7c649dd → uint256
executors 0x9ac2a011 → bool
getLPTBalance 0x90adc83c → uint256
getPositionAssets 0x087ed837 → address[]
getTotalLPT 0x9b1209b5 → uint256
getWithdrawalDelay 0x03160940 → uint256
haveWithdrawalsQueued 0x8b266206 → bool
indexNextWithdrawal 0xcdaf200f → uint256
isCtExpired 0xf8a918e2 → bool
lptPendingOfWithdraw 0x68dbd7a9 → uint256
nextWithdrawalIsReady 0x5c6b7a1d → bool
owner 0x8da5cb5b → address
pendingOwner 0xe30c3978 → address
positionConfig 0xecf74777 → address, bytes32, uint256
underlyingPendingOfWithdraw 0xba751323 → uint256
vaultConfig 0x7cc34bb4 → tuple
withdrawalContract 0x81842c2c → address
withdrawalIsReady 0x62cef791 → bool
withdrawalQueue 0xc822adda → uint256, uint256, uint256, uint256, uint256, uint256, uint256, bytes32

Write Contract 33 functions

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

acceptOwnership 0x79ba5097
No parameters
addExecutor 0x1f5a0bbe
address _executor
approveToken 0xda3e3397
address _token
address _guy
uint256 _wad
assemble 0xe6a6e7a2
uint256 _min_lpt_out
returns: uint256
batchExecute 0x077d97d7
address[] _tos
uint256[] _values
bytes[] _datas
claimWithdrawal 0x8e88eccc
bytes32 _withdrawal_id
completeNextWithdrawal 0x1761b7ce
uint256 _min_ra_out
returns: uint256, uint256
completeNextWithdrawals 0x1b51d9b5
uint256 _min_ra_out
returns: uint256, uint256
completeWithdrawal 0xd9ece259
uint256 _withdrawal_index
uint256 _min_ra_out
returns: uint256, uint256
depositLv 0x78c94986
uint256 _amount
uint256 _min_lpt_out
returns: uint256
disassemble 0x6ed625ab
uint256 _percentage
uint256 _min_ra_out
returns: uint256
execute 0xb61d27f6
address _to
uint256 _value
bytes _data
fullDisassemble 0x54621b42
uint256 _min_ra_out
returns: uint256
overrideWithdrawalIndexes 0xda9d3be7
uint256 _cumulativeWithdrawalsQueued
uint256 _indexNextWithdrawal
redeemExpiredCt 0x0c35b12d
address _ct
uint256 _amount
uint256 _min_ra_out
returns: uint256
redeemExpiredCtByConfig 0xaca8f6f7
uint256 _amount
uint256 _min_ra_out
returns: uint256
removeExecutor 0x24788429
address _executor
renounceOwnership 0x715018a6
No parameters
revokeToken 0x3419ba23
address _token
address _guy
startWithdrawal 0x70431d29
uint256 _lpt_amount
uint256 _amount_out_min
returns: uint256
swapCtDsToRa 0xe6d486ac
uint256 _amount
uint256 _min_ra_out
returns: uint256
swapDsPaToRa 0xeae3ac8f
uint256 _amount
uint256 _min_ra_out
returns: uint256
swapPaToRa 0xcd99aad8
bytes _swap_data
uint256 _min_ra_out
returns: uint256
transferOwnership 0xf2fde38b
address newOwner
transferOwnership1Step 0xe1b97139
address newOwner
unwrapNative 0x34b10a6d
uint256 _amount
update1InchRouter 0xd305536f
address _router
updatePositionConfig 0x07b46054
address _vault
bytes32 _id
uint256 _deviation_limit_bps
withdraw 0xf3fef3a3
address _asset_address
uint256 _amount
withdrawAll 0xfa09e630
address _asset_address
withdrawAllTo 0xb6703fcd
address _asset_address
address _to
withdrawTo 0xc4e2c1e6
address _asset_address
uint256 _amount
address _to
wrapNative 0x9169d833
uint256 _amount

Token Balances (1)

View Transfers →
WETH 0

Recent Transactions

No transactions found for this address