Cryo Explorer Ethereum Mainnet

Address Contract Verified

Address 0x33e4A7D15de9923C680542CB10d76ea4868123fc
Balance 0 ETH
Nonce 1
Code Size 9316 bytes
Indexed Transactions 0
External Etherscan · Sourcify

Contract Bytecode

9316 bytes
0x608060405234801561000f575f5ffd5b50600436106101e7575f3560e01c8063715018a611610109578063a9059cbb1161009e578063e68a49dd1161006e578063e68a49dd14610473578063e842a64b14610486578063ea8b63ed14610499578063f2fde38b146104ac575f5ffd5b8063a9059cbb14610402578063bb42847914610415578063d505accf14610428578063dd62ed3e1461043b575f5ffd5b80638da5cb5b116100d95780638da5cb5b146103cd57806395d89b41146103de5780639852099c146103e6578063a2fddee8146103ef575f5ffd5b8063715018a61461038457806379bc915e1461038e5780637ecebe001461039f57806384b0196e146103b2575f5ffd5b8063313ce5671161017f57806352000ec71161014f57806352000ec7146102fb57806355d5c37b14610322578063663ecad71461034957806370a082311461035c575f5ffd5b8063313ce567146102be5780633644e515146102cd5780634aae9fed146102d5578063517b7faf146102e8575f5ffd5b80631e31d053116101ba5780631e31d0531461027d5780631f7fa3091461028557806323b872dd146102985780632fd06421146102ab575f5ffd5b806306fdde03146101eb578063095ea7b31461020957806318160ddd1461022c5780631c39b6721461023e575b5f5ffd5b6101f36104bf565b60405161020091906120f9565b60405180910390f35b61021c61021736600461211f565b61054f565b6040519015158152602001610200565b6002545b604051908152602001610200565b6102657f0000000000000000000000000b010000b7624eb9b3dfbc279673c76e9d29d5f781565b6040516001600160a01b039091168152602001610200565b610265610568565b610230610293366004612149565b6105f5565b61021c6102a6366004612160565b6106f4565b6102306102b9366004612149565b610719565b60405160128152602001610200565b610230610937565b6102306102e3366004612149565b610940565b6102306102f6366004612149565b610a6e565b6102657f0000000000000000000000006590cbbccbe6b83ef3774ef1904d86a7b02c2fcc81565b6102657f0000000000000000000000001932e815254c53b3ecd81cecf252a5ac7f0e8bea81565b610230610357366004612149565b610aa3565b61023061036a36600461219e565b6001600160a01b03165f9081526020819052604090205490565b61038c610c52565b005b61023061039c366004612149565b90565b6102306103ad36600461219e565b610c65565b6103ba610c82565b60405161020097969594939291906121b9565b6008546001600160a01b0316610265565b6101f3610cc4565b61023060095481565b6102306103fd366004612149565b610cd3565b61021c61041036600461211f565b610cdf565b610230610423366004612149565b610cec565b61038c61043636600461224f565b610e76565b6102306104493660046122c0565b6001600160a01b039182165f90815260016020908152604080832093909416825291909152205490565b610230610481366004612149565b610fb1565b61038c61049436600461219e565b610fdc565b6102306104a7366004612149565b610ff0565b61038c6104ba36600461219e565b611027565b6060600380546104ce906122f7565b80601f01602080910402602001604051908101604052809291908181526020018280546104fa906122f7565b80156105455780601f1061051c57610100808354040283529160200191610545565b820191905f5260205f20905b81548152906001019060200180831161052857829003601f168201915b5050505050905090565b5f3361055c818585611061565b60019150505b92915050565b604051630250bfc560e61b81523060048201525f907f0000000000000000000000006590cbbccbe6b83ef3774ef1904d86a7b02c2fcc6001600160a01b03169063942ff14090602401602060405180830381865afa1580156105cc573d5f5f3e3d5ffd5b505050506040513d601f19601f820116820180604052508101906105f0919061232f565b905090565b5f815f0361061657604051635e9b968f60e01b815260040160405180910390fd5b6040516323b872dd60e01b8152336004820152306024820152604481018390527f0000000000000000000000006590cbbccbe6b83ef3774ef1904d86a7b02c2fcc6001600160a01b0316906323b872dd906064016020604051808303815f875af1158015610686573d5f5f3e3d5ffd5b505050506040513d601f19601f820116820180604052508101906106aa919061234a565b506106b53383611073565b604080518381526020810184905233917f045fd0ffbd0c96a2314cb03a74ef123292eef04f30d913967c4d40c3194fee3c910160405180910390a25090565b5f336107018582856110ab565b61070c858585611126565b60019150505b9392505050565b5f815f0361073a57604051635e9b968f60e01b815260040160405180910390fd5b6107443383611183565b6040516370a0823160e01b81523060048201525f907f0000000000000000000000006590cbbccbe6b83ef3774ef1904d86a7b02c2fcc6001600160a01b0316906370a0823190602401602060405180830381865afa1580156107a8573d5f5f3e3d5ffd5b505050506040513d601f19601f820116820180604052508101906107cc9190612369565b60405163a9059cbb60e01b8152336004820152602481018590529091507f0000000000000000000000006590cbbccbe6b83ef3774ef1904d86a7b02c2fcc6001600160a01b03169063a9059cbb906044016020604051808303815f875af1158015610839573d5f5f3e3d5ffd5b505050506040513d601f19601f8201168201806040525081019061085d919061234a565b506040516370a0823160e01b81523060048201525f907f0000000000000000000000006590cbbccbe6b83ef3774ef1904d86a7b02c2fcc6001600160a01b0316906370a0823190602401602060405180830381865afa1580156108c2573d5f5f3e3d5ffd5b505050506040513d601f19601f820116820180604052508101906108e69190612369565b90506108f28183612394565b604080518281526020810187905291945033917f22701aee943be44cab00c5cf0d098c738e946f3186d6aa7b3ed39362045b3bc5910160405180910390a25050919050565b5f6105f06111b7565b5f815f0361096157604051635e9b968f60e01b815260040160405180910390fd5b6109966001600160a01b037f0000000000000000000000000b010000b7624eb9b3dfbc279673c76e9d29d5f7163330856112e0565b60405163534a7e1d60e11b8152600481018390525f907f0000000000000000000000006590cbbccbe6b83ef3774ef1904d86a7b02c2fcc6001600160a01b03169063a694fc3a906024016020604051808303815f875af11580156109fc573d5f5f3e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610a209190612369565b9050610a2c3382611073565b604080518481526020810183905233917f217f0ba5e67b2e798d92e1cb3b8a0fd4ddbfca77aaa9687604246397a32e27f491015b60405180910390a292915050565b5f7f00000000000000000000000000000000000000000000000000000002540be400610a998361133a565b61056291906123bb565b5f815f03610ac457604051635e9b968f60e01b815260040160405180910390fd5b610ace3383611183565b604051626b2b5960e01b8152600481018390527f0000000000000000000000006590cbbccbe6b83ef3774ef1904d86a7b02c2fcc6001600160a01b031690626b2b59906024016020604051808303815f875af1158015610b30573d5f5f3e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610b549190612369565b505f610b88610b837f00000000000000000000000000000000000000000000000000000002540be400856123da565b6114f4565b60405163a9059cbb60e01b8152336004820152602481018290529091507f0000000000000000000000001932e815254c53b3ecd81cecf252a5ac7f0e8bea6001600160a01b03169063a9059cbb906044016020604051808303815f875af1158015610bf5573d5f5f3e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610c19919061234a565b50604080518481526020810183905233917fec4b60cad85a6117caff12c5fb728ba8e23b1b1ed324cef2c551a2a1b64464e89101610a60565b610c5a6116af565b610c635f6116dc565b565b6001600160a01b0381165f90815260076020526040812054610562565b5f6060805f5f5f6060610c9361172d565b610c9b61175a565b604080515f80825260208201909252600f60f81b9b939a50919850469750309650945092509050565b6060600480546104ce906122f7565b5f610562600183612394565b5f3361055c818585611126565b5f815f03610d0d57604051635e9b968f60e01b815260040160405180910390fd5b6040516323b872dd60e01b8152336004820152306024820152604481018390527f0000000000000000000000001932e815254c53b3ecd81cecf252a5ac7f0e8bea6001600160a01b0316906323b872dd906064016020604051808303815f875af1158015610d7d573d5f5f3e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610da1919061234a565b5060405163c782767b60e01b8152600481018390527f0000000000000000000000006590cbbccbe6b83ef3774ef1904d86a7b02c2fcc6001600160a01b03169063c782767b906024016020604051808303815f875af1158015610e06573d5f5f3e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610e2a9190612369565b9050610e363382611073565b604080518381526020810183905233917f8dd1c06b60f07559261248486cc77bb367a5878f563169dd3da5853bd6001c26910160405180910390a2919050565b83421115610e9f5760405163313c898160e11b8152600481018590526024015b60405180910390fd5b5f7f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9888888610eea8c6001600160a01b03165f90815260076020526040902080546001810190915590565b6040805160208101969096526001600160a01b0394851690860152929091166060840152608083015260a082015260c0810186905260e0016040516020818303038152906040528051906020012090505f610f4482611787565b90505f610f53828787876117b3565b9050896001600160a01b0316816001600160a01b031614610f9a576040516325c0072360e11b81526001600160a01b0380831660048301528b166024820152604401610e96565b610fa58a8a8a611061565b50505050505050505050565b5f7f00000000000000000000000000000000000000000000000000000002540be400610a99836117df565b610fe46116af565b610fed8161196c565b50565b5f8061101c7f00000000000000000000000000000000000000000000000000000002540be400846123da565b9050610712816114f4565b61102f6116af565b6001600160a01b03811661105857604051631e4fbdf760e01b81525f6004820152602401610e96565b610fed816116dc565b61106e8383836001611aba565b505050565b6001600160a01b03821661109c5760405163ec442f0560e01b81525f6004820152602401610e96565b6110a75f8383611b8c565b5050565b6001600160a01b038381165f908152600160209081526040808320938616835292905220545f198114611120578181101561111257604051637dc7a0d960e11b81526001600160a01b03841660048201526024810182905260448101839052606401610e96565b61112084848484035f611aba565b50505050565b6001600160a01b03831661114f57604051634b637e8f60e11b81525f6004820152602401610e96565b6001600160a01b0382166111785760405163ec442f0560e01b81525f6004820152602401610e96565b61106e838383611b8c565b6001600160a01b0382166111ac57604051634b637e8f60e11b81525f6004820152602401610e96565b6110a7825f83611b8c565b5f306001600160a01b037f00000000000000000000000033e4a7d15de9923c680542cb10d76ea4868123fc1614801561120f57507f000000000000000000000000000000000000000000000000000000000000000146145b1561123957507fd3ce86de1cd3291bdb04535632cfeea18bd5b4dc0ffc47f2575537b0b5431d8290565b6105f0604080517f8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f60208201527f19516b860ab25b04ecfeb9473c47b4bd4e06d3f424e9d2bcb9ffcc660c2fac0d918101919091527fc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc660608201524660808201523060a08201525f9060c00160405160208183030381529060405280519060200120905090565b604080516001600160a01b0385811660248301528416604482015260648082018490528251808303909101815260849091019091526020810180516001600160e01b03166323b872dd60e01b179052611120908590611cb2565b5f5f611345836117df565b90507f0000000000000000000000001932e815254c53b3ecd81cecf252a5ac7f0e8bea6001600160a01b03166318160ddd6040518163ffffffff1660e01b8152600401602060405180830381865afa1580156113a3573d5f5f3e3d5ffd5b505050506040513d601f19601f820116820180604052508101906113c79190612369565b5f036113d35792915050565b5f7f0000000000000000000000001932e815254c53b3ecd81cecf252a5ac7f0e8bea6001600160a01b03166318160ddd6040518163ffffffff1660e01b8152600401602060405180830381865afa158015611430573d5f5f3e3d5ffd5b505050506040513d601f19601f820116820180604052508101906114549190612369565b80611461576114616123a7565b7f0000000000000000000000001932e815254c53b3ecd81cecf252a5ac7f0e8bea6001600160a01b0316633a98ef396040518163ffffffff1660e01b8152600401602060405180830381865afa1580156114bd573d5f5f3e3d5ffd5b505050506040513d601f19601f820116820180604052508101906114e19190612369565b85091115610562576107126001826123f1565b5f7f0000000000000000000000001932e815254c53b3ecd81cecf252a5ac7f0e8bea6001600160a01b0316633a98ef396040518163ffffffff1660e01b8152600401602060405180830381865afa158015611551573d5f5f3e3d5ffd5b505050506040513d601f19601f820116820180604052508101906115759190612369565b5f036115a5576105627f00000000000000000000000000000000000000000000000000000002540be400836123bb565b7f0000000000000000000000001932e815254c53b3ecd81cecf252a5ac7f0e8bea6001600160a01b0316633a98ef396040518163ffffffff1660e01b8152600401602060405180830381865afa158015611601573d5f5f3e3d5ffd5b505050506040513d601f19601f820116820180604052508101906116259190612369565b7f0000000000000000000000001932e815254c53b3ecd81cecf252a5ac7f0e8bea6001600160a01b03166318160ddd6040518163ffffffff1660e01b8152600401602060405180830381865afa158015611681573d5f5f3e3d5ffd5b505050506040513d601f19601f820116820180604052508101906116a59190612369565b610a9990846123da565b6008546001600160a01b03163314610c635760405163118cdaa760e01b8152336004820152602401610e96565b600880546001600160a01b038381166001600160a01b0319831681179093556040519116919082907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0905f90a35050565b60606105f07f57726170706564205374616b6564204f626f6c000000000000000000000000136005611d13565b60606105f07f31000000000000000000000000000000000000000000000000000000000000016006611d13565b5f6105626117936111b7565b8360405161190160f01b8152600281019290925260228201526042902090565b5f5f5f5f6117c388888888611dbc565b9250925092506117d38282611e84565b50909695505050505050565b5f7f0000000000000000000000001932e815254c53b3ecd81cecf252a5ac7f0e8bea6001600160a01b03166318160ddd6040518163ffffffff1660e01b8152600401602060405180830381865afa15801561183c573d5f5f3e3d5ffd5b505050506040513d601f19601f820116820180604052508101906118609190612369565b5f0361189057610562827f00000000000000000000000000000000000000000000000000000002540be4006123da565b7f0000000000000000000000001932e815254c53b3ecd81cecf252a5ac7f0e8bea6001600160a01b03166318160ddd6040518163ffffffff1660e01b8152600401602060405180830381865afa1580156118ec573d5f5f3e3d5ffd5b505050506040513d601f19601f820116820180604052508101906119109190612369565b7f0000000000000000000000001932e815254c53b3ecd81cecf252a5ac7f0e8bea6001600160a01b0316633a98ef396040518163ffffffff1660e01b8152600401602060405180830381865afa158015611681573d5f5f3e3d5ffd5b806001600160a01b031661197e610568565b6001600160a01b03167ff25bb056171d38943fa57831cb70cbbdd82c34313ff8cfca06d41596856e33cf60405160405180910390a3604051630a79f5b160e41b81526001600160a01b0382811660048301527f0000000000000000000000001932e815254c53b3ecd81cecf252a5ac7f0e8bea169063a79f5b10906024016020604051808303815f875af1158015611a18573d5f5f3e3d5ffd5b505050506040513d601f19601f82011682018060405250810190611a3c9190612369565b6009819055604051630467a75960e21b815260048101919091527f0000000000000000000000006590cbbccbe6b83ef3774ef1904d86a7b02c2fcc6001600160a01b03169063119e9d64906024015f604051808303815f87803b158015611aa1575f5ffd5b505af1158015611ab3573d5f5f3e3d5ffd5b5050505050565b6001600160a01b038416611ae35760405163e602df0560e01b81525f6004820152602401610e96565b6001600160a01b038316611b0c57604051634a1406b160e11b81525f6004820152602401610e96565b6001600160a01b038085165f908152600160209081526040808320938716835292905220829055801561112057826001600160a01b0316846001600160a01b03167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92584604051611b7e91815260200190565b60405180910390a350505050565b6001600160a01b038316611bb6578060025f828254611bab91906123f1565b90915550611c269050565b6001600160a01b0383165f9081526020819052604090205481811015611c085760405163391434e360e21b81526001600160a01b03851660048201526024810182905260448101839052606401610e96565b6001600160a01b0384165f9081526020819052604090209082900390555b6001600160a01b038216611c4257600280548290039055611c60565b6001600160a01b0382165f9081526020819052604090208054820190555b816001600160a01b0316836001600160a01b03167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef83604051611ca591815260200190565b60405180910390a3505050565b5f611cc66001600160a01b03841683611f3c565b905080515f14158015611cea575080806020019051810190611ce8919061234a565b155b1561106e57604051635274afe760e01b81526001600160a01b0384166004820152602401610e96565b606060ff8314611d2d57611d2683611f49565b9050610562565b818054611d39906122f7565b80601f0160208091040260200160405190810160405280929190818152602001828054611d65906122f7565b8015611db05780601f10611d8757610100808354040283529160200191611db0565b820191905f5260205f20905b815481529060010190602001808311611d9357829003601f168201915b50505050509050610562565b5f80807f7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0841115611df557505f91506003905082611e7a565b604080515f808252602082018084528a905260ff891692820192909252606081018790526080810186905260019060a0016020604051602081039080840390855afa158015611e46573d5f5f3e3d5ffd5b5050604051601f1901519150506001600160a01b038116611e7157505f925060019150829050611e7a565b92505f91508190505b9450945094915050565b5f826003811115611e9757611e97612404565b03611ea0575050565b6001826003811115611eb457611eb4612404565b03611ed25760405163f645eedf60e01b815260040160405180910390fd5b6002826003811115611ee657611ee6612404565b03611f075760405163fce698f760e01b815260048101829052602401610e96565b6003826003811115611f1b57611f1b612404565b036110a7576040516335e2f38360e21b815260048101829052602401610e96565b606061071283835f611f86565b60605f611f558361201f565b6040805160208082528183019092529192505f91906020820181803683375050509182525060208101929092525090565b606081471015611fab5760405163cd78605960e01b8152306004820152602401610e96565b5f5f856001600160a01b03168486604051611fc69190612418565b5f6040518083038185875af1925050503d805f8114612000576040519150601f19603f3d011682016040523d82523d5f602084013e612005565b606091505b5091509150612015868383612046565b9695505050505050565b5f60ff8216601f81111561056257604051632cd44ac360e21b815260040160405180910390fd5b60608261205b57612056826120a2565b610712565b815115801561207257506001600160a01b0384163b155b1561209b57604051639996b31560e01b81526001600160a01b0385166004820152602401610e96565b5080610712565b8051156120b25780518082602001fd5b604051630a12f52160e11b815260040160405180910390fd5b5f81518084528060208401602086015e5f602082860101526020601f19601f83011685010191505092915050565b602081525f61071260208301846120cb565b6001600160a01b0381168114610fed575f5ffd5b5f5f60408385031215612130575f5ffd5b823561213b8161210b565b946020939093013593505050565b5f60208284031215612159575f5ffd5b5035919050565b5f5f5f60608486031215612172575f5ffd5b833561217d8161210b565b9250602084013561218d8161210b565b929592945050506040919091013590565b5f602082840312156121ae575f5ffd5b81356107128161210b565b60ff60f81b8816815260e060208201525f6121d760e08301896120cb565b82810360408401526121e981896120cb565b606084018890526001600160a01b038716608085015260a0840186905283810360c0850152845180825260208087019350909101905f5b8181101561223e578351835260209384019390920191600101612220565b50909b9a5050505050505050505050565b5f5f5f5f5f5f5f60e0888a031215612265575f5ffd5b87356122708161210b565b965060208801356122808161210b565b95506040880135945060608801359350608088013560ff811681146122a3575f5ffd5b9699959850939692959460a0840135945060c09093013592915050565b5f5f604083850312156122d1575f5ffd5b82356122dc8161210b565b915060208301356122ec8161210b565b809150509250929050565b600181811c9082168061230b57607f821691505b60208210810361232957634e487b7160e01b5f52602260045260245ffd5b50919050565b5f6020828403121561233f575f5ffd5b81516107128161210b565b5f6020828403121561235a575f5ffd5b81518015158114610712575f5ffd5b5f60208284031215612379575f5ffd5b5051919050565b634e487b7160e01b5f52601160045260245ffd5b8181038181111561056257610562612380565b634e487b7160e01b5f52601260045260245ffd5b5f826123d557634e487b7160e01b5f52601260045260245ffd5b500490565b808202811582820484141761056257610562612380565b8082018082111561056257610562612380565b634e487b7160e01b5f52602160045260245ffd5b5f82518060208501845e5f92019182525091905056fea2646970667358221220e3284bf5a4831c2e3e0aa5f093cb045d9bc45fedf9845a33ddbb8fa9d86d9fe264736f6c634300081c0033

Verified Source Code Full Match

Compiler: v0.8.28+commit.7893614a EVM: cancun Optimization: Yes (200 runs)
WrappedGovLst.sol 308 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import {ERC20Permit} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Permit.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {Staker} from "staker/Staker.sol";

import {FixedGovLst} from "./FixedGovLst.sol";
import {GovLst} from "./GovLst.sol";

/// @title WrappedGovLst
/// @author [ScopeLift](https://scopelift.co)
/// @notice A wrapper contract that provides a non-rebasing interface to liquid stake tokens. The wrapper accepts
/// stake tokens, `GovLST`, or `FixedGovLST` tokens. Wrapped tokens maintain 1:1 backing with `FixedGovLST`
/// shares, allowing holders to benefit from staking rewards without balance changes and avoid off-by-one rounding
/// issues when transferring tokens. The voting weight for all tokens held by a given wrapper deployment is assigned to
/// a single delegatee, which is controlled by the wrapper's owner.
contract WrappedGovLst is ERC20Permit, Ownable {
  using SafeERC20 for IERC20;

  /// @notice Emitted when a holder wraps `GovLst` tokens.
  event RebasingWrapped(address indexed holder, uint256 rebasingAmount, uint256 wrappedAmount);

  /// @notice Emitted when a holder wraps `FixedGovLst` tokens.
  event FixedWrapped(address indexed holder, uint256 fixedAmount, uint256 wrappedAmount);

  /// @notice Emitted when a holder wraps the underlying stake token.
  event UnderlyingWrapped(address indexed holder, uint256 underlyingAmount, uint256 wrappedAmount);

  /// @notice Emitted when a holder unwraps tokens into `GovLst` tokens.
  event RebasingUnwrapped(address indexed holder, uint256 lstAmount, uint256 wrappedAmount);

  /// @notice Emitted when a holder unwraps tokens into `FixedGovLst` tokens.
  event FixedUnwrapped(address indexed holder, uint256 lstAmount, uint256 wrappedAmount);

  /// @notice Emitted when the wrapper's owner updates the delegatee to which wrapped tokens voting weight is assigned.
  event DelegateeSet(address indexed oldDelegatee, address indexed newDelegatee);

  /// @notice Emitted when a holder tries to wrap or unwrap 0 liquid stake tokens.
  error WrappedGovLst__InvalidAmount();

  /// @notice The address of the `GovLst` contract which can be wrapped.
  GovLst public immutable LST;

  /// @notice The address of the `FixedGovLst` contract which backs the wrapped tokens 1:1 and can be wrapped.
  FixedGovLst public immutable FIXED_LST;

  /// @notice The address of the `IERC20` token used in the underlying staker.
  IERC20 public immutable STAKE_TOKEN;

  /// @notice Local copy of the LST's scale factor that is stored at deployment for use in wrapper calculations.
  uint256 internal immutable SHARE_SCALE_FACTOR;

  /// @notice The Staker deposit identifier which holds the wrapper's underlying tokens.
  Staker.DepositIdentifier public depositId;

  /// @param _name The name of the wrapper token.
  /// @param _symbol The symbol of the wrapper token.
  /// @param _lst The contract of the liquid stake token being wrapped.
  /// @param _delegatee The initial delegatee to whom the wrapper's voting weight will be delegated.
  /// @param _initialOwner The initial owner of the wrapper contract.
  /// @param _preFundWrapped The amount of `FixedGovLst` tokens to prefund the wrapper. If 0 some fixed tokens should be
  /// sent after deployment to the wrapper.
  constructor(
    string memory _name,
    string memory _symbol,
    GovLst _lst,
    address _delegatee,
    address _initialOwner,
    uint256 _preFundWrapped
  ) ERC20Permit(_name) ERC20(_name, _symbol) Ownable(_initialOwner) {
    LST = _lst;
    FIXED_LST = _lst.FIXED_LST();
    SHARE_SCALE_FACTOR = _lst.SHARE_SCALE_FACTOR();
    FIXED_LST.transferFrom(msg.sender, address(this), _preFundWrapped);
    STAKE_TOKEN = IERC20(address(LST.STAKE_TOKEN()));
    STAKE_TOKEN.approve(address(FIXED_LST), type(uint256).max);
    _setDelegatee(_delegatee);
  }

  /// @notice The address of the delegatee to which the wrapped token's voting weight is currently delegated.
  function delegatee() public view virtual returns (address) {
    return FIXED_LST.delegateeForHolder(address(this));
  }

  /// @notice Preview the amount of wrapped tokens that would be minted when wrapping `GovLST` tokens.
  /// @param _rebasingTokensToWrap The amount of `GovLST` tokens to wrap.
  /// @return The minimum amount of wrapped tokens that would be minted.
  /// @dev Returns the minimum amount that would be minted by `wrapRebasing`.
  function previewWrapRebasing(uint256 _rebasingTokensToWrap) public view virtual returns (uint256) {
    // Calculate the shares that will be transferred when converting to fixed
    return _calcSharesForStakeUp(_rebasingTokensToWrap) / SHARE_SCALE_FACTOR;
  }

  /// @notice Preview the amount of wrapped tokens that would be minted when wrapping underlying stake tokens.
  /// @param _stakeTokensToWrap The amount of underlying stake tokens to wrap.
  /// @return The minimum amount of wrapped tokens that would be minted.
  /// @dev Simulates the staking process to determine the resulting wrapped token amount.
  function previewWrapUnderlying(uint256 _stakeTokensToWrap) public view virtual returns (uint256) {
    return _calcSharesForStake(_stakeTokensToWrap) / SHARE_SCALE_FACTOR;
  }

  /// @notice Preview the amount of wrapped tokens that would be minted when wrapping fixed liquid staking tokens.
  /// @param _fixedTokensToWrap The amount of fixed liquid staking tokens to wrap.
  /// @return The minimum amount of wrapped tokens that would be minted.
  /// @dev Wrapped tokens maintain 1:1 backing with fixed liquid staking tokens.
  function previewWrapFixed(uint256 _fixedTokensToWrap) public view virtual returns (uint256) {
    return _fixedTokensToWrap;
  }

  /// @notice Preview the amount of rebasing liquid stake tokens that would be received when unwrapping.
  /// @param _wrappedAmount The amount of wrapped tokens to unwrap.
  /// @return The minimum amount of rebasing liquid stake tokens that would be received.
  /// @dev Converts wrapped tokens to shares, then to rebasing tokens. Rounds down to favor the protocol.
  function previewUnwrapToRebasing(uint256 _wrappedAmount) public view virtual returns (uint256) {
    uint256 _shares = _wrappedAmount * SHARE_SCALE_FACTOR;
    return _calcStakeForShares(_shares);
  }

  /// @notice Preview the amount of fixed liquid staking tokens that would be received when unwrapping.
  /// @param _wrappedAmount The amount of wrapped tokens to unwrap.
  /// @return The minimum amount of fixed liquid staking tokens that would be received.
  /// @dev Returns a conservative estimate (1 wei less) to account for potential rounding in `FixedGovLst` transfer.
  /// The actual amount received may be up to 1 wei more due to `FixedGovLst`'s internal rounding behavior.
  function previewUnwrapToFixed(uint256 _wrappedAmount) public view virtual returns (uint256) {
    // At worst 1 wei less than what has been requested will be returned.
    // The preview will return the minimum amount of assets returned.
    return _wrappedAmount - 1;
  }

  /// @notice Deposit liquid stake tokens and receive wrapped tokens in exchange.
  /// @param _lstAmountToWrap The quantity of liquid stake tokens to wrap.
  /// @return _wrappedAmount The quantity of wrapped tokens issued to the caller.
  /// @dev The caller must approve at least the amount of tokens to wrap on the lst contract before calling. Amount to
  /// wrap may not be zero.
  /// @dev When wrapping, `GovLst` tokens are first transferred to `WrappedGovLst`. The transfer can result in the
  /// `WrappedGovLst` balance increasing by at most 1 wei more than the transferred amount. A second transfer of
  /// `GovLst` tokens to the fixed alias address of the `WrappedGovLst` will happen when converting to fixed tokens. This
  /// transfer will use the initial wrapped amount rather than the balance increases from the first transfer, ensuring
  /// that at most 1 extra wei is sent rather than 2.
  function wrapRebasing(uint256 _lstAmountToWrap) external virtual returns (uint256 _wrappedAmount) {
    if (_lstAmountToWrap == 0) {
      revert WrappedGovLst__InvalidAmount();
    }

    LST.transferFrom(msg.sender, address(this), _lstAmountToWrap);
    _wrappedAmount = FIXED_LST.convertToFixed(_lstAmountToWrap);
    _mint(msg.sender, _wrappedAmount);

    emit RebasingWrapped(msg.sender, _lstAmountToWrap, _wrappedAmount);
  }

  /// @notice Deposit underlying stake tokens and receive wrapped tokens in exchange.
  /// @param _stakeTokensToWrap The quantity of underlying stake tokens to wrap.
  /// @return _wrappedAmount The quantity of wrapped tokens issued to the caller.
  /// @dev The caller must approve at least the amount of tokens to wrap on the stake token contract before calling.
  /// Amount to wrap may not be zero.
  function wrapUnderlying(uint256 _stakeTokensToWrap) public virtual returns (uint256) {
    if (_stakeTokensToWrap == 0) {
      revert WrappedGovLst__InvalidAmount();
    }

    STAKE_TOKEN.safeTransferFrom(msg.sender, address(this), _stakeTokensToWrap);

    uint256 _wrappedAmount = FIXED_LST.stake(_stakeTokensToWrap);
    _mint(msg.sender, _wrappedAmount);

    emit UnderlyingWrapped(msg.sender, _stakeTokensToWrap, _wrappedAmount);

    return _wrappedAmount;
  }

  /// @notice Deposit `FixedGovLST` tokens and receive wrapped tokens in exchange.
  /// @param _fixedTokensToWrap The quantity of `FixedGoLst` tokens to wrap.
  /// @return _wrappedAmount The quantity of wrapped tokens issued to the caller.
  /// @dev The caller must approve at least the amount of tokens to wrap on the `FixedGovLst` contract before calling.
  /// Amount to wrap may not be zero.
  /// @dev When transferring using the `FixedGovLst` at most the `WrappedGoLst` may receive 1 wei less
  /// than the tokens sent. This is due to the conversion of shares into tokens in the underlying
  /// `GovLst`. Shares will be rounded down to tokens and the smaller amount of tokens will be
  /// converted back into shares leading to an up to 1 wei difference between the sent amount and
  /// the received amount.
  function wrapFixed(uint256 _fixedTokensToWrap) external virtual returns (uint256) {
    if (_fixedTokensToWrap == 0) {
      revert WrappedGovLst__InvalidAmount();
    }

    FIXED_LST.transferFrom(msg.sender, address(this), _fixedTokensToWrap);

    _mint(msg.sender, _fixedTokensToWrap);

    emit FixedWrapped(msg.sender, _fixedTokensToWrap, _fixedTokensToWrap);

    return _fixedTokensToWrap;
  }

  /// @notice Burn wrapped tokens to receive liquid stake tokens in return.
  /// @param _wrappedAmount The quantity of wrapped tokens to burn.
  /// @return _lstAmountUnwrapped The quantity of liquid staked tokens received in exchange for the wrapped tokens.
  /// @dev The caller must approve at least the amount wrapped tokens on the wrapper token contract.
  /// @dev When unwrapping wrapped tokens to rebasing tokens, the shares are converted to tokens, resulting in an at
  /// most loss of 1 wei. Unwrapping requires two transfers by the `GovLst`, one transfer to the non-aliased
  /// `WrappedGovLst` and another transfer to the caller. Each transfer will send the amount and at most 1 extra wei. We
  /// avoid sending 2 extra wei to the caller by using the same token amount in the second transfer as the first.
  function unwrapToRebasing(uint256 _wrappedAmount) external virtual returns (uint256) {
    if (_wrappedAmount == 0) {
      revert WrappedGovLst__InvalidAmount();
    }

    _burn(msg.sender, _wrappedAmount);

    FIXED_LST.convertToRebasing(_wrappedAmount);
    uint256 _lstAmountUnwrapped = _calcStakeForShares(_wrappedAmount * SHARE_SCALE_FACTOR);

    LST.transfer(msg.sender, _lstAmountUnwrapped);

    emit RebasingUnwrapped(msg.sender, _wrappedAmount, _lstAmountUnwrapped);
    return _lstAmountUnwrapped;
  }

  /// @notice Burn wrapped tokens to receive `FixedGovLst` tokens.
  /// @param _wrappedAmount The quantity of wrapped tokens to burn.
  /// @return _fixedTokensUnwrapped The quantity of `FixedGovLst` tokens received.
  /// @dev Since wrapped tokens are 1:1 with `FixedGovLst` tokens, this is a simple transfer.
  /// @dev May transfer up to 1 extra wei due to FixedGovLst's internal rounding.
  /// @dev When transferring using the `FixedGovLst` at most the receiver may receive one wei less
  /// than the tokens sent. This is due to the conversion of shares into tokens in the underlying
  /// `GovLst`. Shares will be rounded down to tokens and the smaller amount of tokens will be
  /// converted back into shares leading to an up to 1 wei difference between the sent amount and
  /// the received amount.
  function unwrapToFixed(uint256 _wrappedAmount) external virtual returns (uint256 _fixedTokensUnwrapped) {
    if (_wrappedAmount == 0) {
      revert WrappedGovLst__InvalidAmount();
    }

    _burn(msg.sender, _wrappedAmount);

    uint256 _wrapperBalanceBefore = FIXED_LST.balanceOf(address(this));
    FIXED_LST.transfer(msg.sender, _wrappedAmount);
    uint256 _wrapperBalanceAfter = FIXED_LST.balanceOf(address(this));
    _fixedTokensUnwrapped = _wrapperBalanceBefore - _wrapperBalanceAfter;

    emit FixedUnwrapped(msg.sender, _fixedTokensUnwrapped, _wrappedAmount);

    return _fixedTokensUnwrapped;
  }

  /// @notice Method that can be called only by the owner to update the address to which all the wrapped token's voting
  /// weight will be delegated.
  function setDelegatee(address _newDelegatee) public virtual {
    _checkOwner();
    _setDelegatee(_newDelegatee);
  }

  /// @notice Calculate the number of shares that would be created from staking a given amount.
  /// @param _amount The amount of stake tokens to convert to shares.
  /// @return The number of shares that would be created.
  /// @dev Mirrors the share calculation logic from GovLst. Rounds down.
  function _calcSharesForStake(uint256 _amount) internal view virtual returns (uint256) {
    if (LST.totalSupply() == 0) {
      return SHARE_SCALE_FACTOR * _amount;
    }

    return (_amount * LST.totalShares()) / LST.totalSupply();
  }

  /// @notice Calculate the amount of stake tokens that correspond to a given number of shares.
  /// @param _shares The number of shares to convert to stake tokens.
  /// @return The amount of stake tokens that the shares represent.
  /// @dev Converts shares back to stake token amounts. Rounds down to favor the protocol.
  function _calcStakeForShares(uint256 _shares) internal view virtual returns (uint256) {
    if (LST.totalShares() == 0) {
      return _shares / SHARE_SCALE_FACTOR;
    }

    // Rounds down, favoring the protocol
    return (_shares * LST.totalSupply()) / LST.totalShares();
  }

  /// @notice Calculate the number of shares for a stake amount, rounding up.
  /// @param _amount The amount of stake tokens to convert to shares.
  /// @return The number of shares that would be created, rounded up.
  /// @dev Similar to _calcSharesForStake but rounds up if there's any remainder.
  /// This ensures no value is lost when converting rebasing tokens to fixed tokens.
  function _calcSharesForStakeUp(uint256 _amount) internal view virtual returns (uint256) {
    uint256 _result = _calcSharesForStake(_amount);
    if (LST.totalSupply() == 0) {
      return _result;
    }

    // Add 1 if there's any remainder from the division
    if (mulmod(_amount, LST.totalShares(), LST.totalSupply()) > 0) {
      _result += 1;
    }

    return _result;
  }

  /// @notice Internal method that sets the deposit identifier for the delegate specified on the LST.
  function _setDelegatee(address _newDelegatee) internal virtual {
    emit DelegateeSet(delegatee(), _newDelegatee);
    depositId = LST.fetchOrInitializeDepositForDelegatee(_newDelegatee);
    FIXED_LST.updateDeposit(depositId);
  }
}
ERC20Permit.sol 83 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/extensions/ERC20Permit.sol)

pragma solidity ^0.8.20;

import {IERC20Permit} from "./IERC20Permit.sol";
import {ERC20} from "../ERC20.sol";
import {ECDSA} from "../../../utils/cryptography/ECDSA.sol";
import {EIP712} from "../../../utils/cryptography/EIP712.sol";
import {Nonces} from "../../../utils/Nonces.sol";

/**
 * @dev Implementation 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.
 */
abstract contract ERC20Permit is ERC20, IERC20Permit, EIP712, Nonces {
    bytes32 private constant PERMIT_TYPEHASH =
        keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");

    /**
     * @dev Permit deadline has expired.
     */
    error ERC2612ExpiredSignature(uint256 deadline);

    /**
     * @dev Mismatched signature.
     */
    error ERC2612InvalidSigner(address signer, address owner);

    /**
     * @dev Initializes the {EIP712} domain separator using the `name` parameter, and setting `version` to `"1"`.
     *
     * It's a good idea to use the same `name` that is defined as the ERC20 token name.
     */
    constructor(string memory name) EIP712(name, "1") {}

    /**
     * @inheritdoc IERC20Permit
     */
    function permit(
        address owner,
        address spender,
        uint256 value,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) public virtual {
        if (block.timestamp > deadline) {
            revert ERC2612ExpiredSignature(deadline);
        }

        bytes32 structHash = keccak256(abi.encode(PERMIT_TYPEHASH, owner, spender, value, _useNonce(owner), deadline));

        bytes32 hash = _hashTypedDataV4(structHash);

        address signer = ECDSA.recover(hash, v, r, s);
        if (signer != owner) {
            revert ERC2612InvalidSigner(signer, owner);
        }

        _approve(owner, spender, value);
    }

    /**
     * @inheritdoc IERC20Permit
     */
    function nonces(address owner) public view virtual override(IERC20Permit, Nonces) returns (uint256) {
        return super.nonces(owner);
    }

    /**
     * @inheritdoc IERC20Permit
     */
    // solhint-disable-next-line func-name-mixedcase
    function DOMAIN_SEPARATOR() external view virtual returns (bytes32) {
        return _domainSeparatorV4();
    }
}
ERC20.sol 316 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/ERC20.sol)

pragma solidity ^0.8.20;

import {IERC20} from "./IERC20.sol";
import {IERC20Metadata} from "./extensions/IERC20Metadata.sol";
import {Context} from "../../utils/Context.sol";
import {IERC20Errors} from "../../interfaces/draft-IERC6093.sol";

/**
 * @dev Implementation of the {IERC20} interface.
 *
 * This implementation is agnostic to the way tokens are created. This means
 * that a supply mechanism has to be added in a derived contract using {_mint}.
 *
 * TIP: For a detailed writeup see our guide
 * https://forum.openzeppelin.com/t/how-to-implement-erc20-supply-mechanisms/226[How
 * to implement supply mechanisms].
 *
 * The default value of {decimals} is 18. To change this, you should override
 * this function so it returns a different value.
 *
 * We have followed general OpenZeppelin Contracts guidelines: functions revert
 * instead returning `false` on failure. This behavior is nonetheless
 * conventional and does not conflict with the expectations of ERC20
 * applications.
 *
 * Additionally, an {Approval} event is emitted on calls to {transferFrom}.
 * This allows applications to reconstruct the allowance for all accounts just
 * by listening to said events. Other implementations of the EIP may not emit
 * these events, as it isn't required by the specification.
 */
abstract contract ERC20 is Context, IERC20, IERC20Metadata, IERC20Errors {
    mapping(address account => uint256) private _balances;

    mapping(address account => mapping(address spender => uint256)) private _allowances;

    uint256 private _totalSupply;

    string private _name;
    string private _symbol;

    /**
     * @dev Sets the values for {name} and {symbol}.
     *
     * All two of these values are immutable: they can only be set once during
     * construction.
     */
    constructor(string memory name_, string memory symbol_) {
        _name = name_;
        _symbol = symbol_;
    }

    /**
     * @dev Returns the name of the token.
     */
    function name() public view virtual returns (string memory) {
        return _name;
    }

    /**
     * @dev Returns the symbol of the token, usually a shorter version of the
     * name.
     */
    function symbol() public view virtual returns (string memory) {
        return _symbol;
    }

    /**
     * @dev Returns the number of decimals used to get its user representation.
     * For example, if `decimals` equals `2`, a balance of `505` tokens should
     * be displayed to a user as `5.05` (`505 / 10 ** 2`).
     *
     * Tokens usually opt for a value of 18, imitating the relationship between
     * Ether and Wei. This is the default value returned by this function, unless
     * it's overridden.
     *
     * NOTE: This information is only used for _display_ purposes: it in
     * no way affects any of the arithmetic of the contract, including
     * {IERC20-balanceOf} and {IERC20-transfer}.
     */
    function decimals() public view virtual returns (uint8) {
        return 18;
    }

    /**
     * @dev See {IERC20-totalSupply}.
     */
    function totalSupply() public view virtual returns (uint256) {
        return _totalSupply;
    }

    /**
     * @dev See {IERC20-balanceOf}.
     */
    function balanceOf(address account) public view virtual returns (uint256) {
        return _balances[account];
    }

    /**
     * @dev See {IERC20-transfer}.
     *
     * Requirements:
     *
     * - `to` cannot be the zero address.
     * - the caller must have a balance of at least `value`.
     */
    function transfer(address to, uint256 value) public virtual returns (bool) {
        address owner = _msgSender();
        _transfer(owner, to, value);
        return true;
    }

    /**
     * @dev See {IERC20-allowance}.
     */
    function allowance(address owner, address spender) public view virtual returns (uint256) {
        return _allowances[owner][spender];
    }

    /**
     * @dev See {IERC20-approve}.
     *
     * NOTE: If `value` is the maximum `uint256`, the allowance is not updated on
     * `transferFrom`. This is semantically equivalent to an infinite approval.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     */
    function approve(address spender, uint256 value) public virtual returns (bool) {
        address owner = _msgSender();
        _approve(owner, spender, value);
        return true;
    }

    /**
     * @dev See {IERC20-transferFrom}.
     *
     * Emits an {Approval} event indicating the updated allowance. This is not
     * required by the EIP. See the note at the beginning of {ERC20}.
     *
     * NOTE: Does not update the allowance if the current allowance
     * is the maximum `uint256`.
     *
     * Requirements:
     *
     * - `from` and `to` cannot be the zero address.
     * - `from` must have a balance of at least `value`.
     * - the caller must have allowance for ``from``'s tokens of at least
     * `value`.
     */
    function transferFrom(address from, address to, uint256 value) public virtual returns (bool) {
        address spender = _msgSender();
        _spendAllowance(from, spender, value);
        _transfer(from, to, value);
        return true;
    }

    /**
     * @dev Moves a `value` amount of tokens from `from` to `to`.
     *
     * This internal function is equivalent to {transfer}, and can be used to
     * e.g. implement automatic token fees, slashing mechanisms, etc.
     *
     * Emits a {Transfer} event.
     *
     * NOTE: This function is not virtual, {_update} should be overridden instead.
     */
    function _transfer(address from, address to, uint256 value) internal {
        if (from == address(0)) {
            revert ERC20InvalidSender(address(0));
        }
        if (to == address(0)) {
            revert ERC20InvalidReceiver(address(0));
        }
        _update(from, to, value);
    }

    /**
     * @dev Transfers a `value` amount of tokens from `from` to `to`, or alternatively mints (or burns) if `from`
     * (or `to`) is the zero address. All customizations to transfers, mints, and burns should be done by overriding
     * this function.
     *
     * Emits a {Transfer} event.
     */
    function _update(address from, address to, uint256 value) internal virtual {
        if (from == address(0)) {
            // Overflow check required: The rest of the code assumes that totalSupply never overflows
            _totalSupply += value;
        } else {
            uint256 fromBalance = _balances[from];
            if (fromBalance < value) {
                revert ERC20InsufficientBalance(from, fromBalance, value);
            }
            unchecked {
                // Overflow not possible: value <= fromBalance <= totalSupply.
                _balances[from] = fromBalance - value;
            }
        }

        if (to == address(0)) {
            unchecked {
                // Overflow not possible: value <= totalSupply or value <= fromBalance <= totalSupply.
                _totalSupply -= value;
            }
        } else {
            unchecked {
                // Overflow not possible: balance + value is at most totalSupply, which we know fits into a uint256.
                _balances[to] += value;
            }
        }

        emit Transfer(from, to, value);
    }

    /**
     * @dev Creates a `value` amount of tokens and assigns them to `account`, by transferring it from address(0).
     * Relies on the `_update` mechanism
     *
     * Emits a {Transfer} event with `from` set to the zero address.
     *
     * NOTE: This function is not virtual, {_update} should be overridden instead.
     */
    function _mint(address account, uint256 value) internal {
        if (account == address(0)) {
            revert ERC20InvalidReceiver(address(0));
        }
        _update(address(0), account, value);
    }

    /**
     * @dev Destroys a `value` amount of tokens from `account`, lowering the total supply.
     * Relies on the `_update` mechanism.
     *
     * Emits a {Transfer} event with `to` set to the zero address.
     *
     * NOTE: This function is not virtual, {_update} should be overridden instead
     */
    function _burn(address account, uint256 value) internal {
        if (account == address(0)) {
            revert ERC20InvalidSender(address(0));
        }
        _update(account, address(0), value);
    }

    /**
     * @dev Sets `value` as the allowance of `spender` over the `owner` s tokens.
     *
     * This internal function is equivalent to `approve`, and can be used to
     * e.g. set automatic allowances for certain subsystems, etc.
     *
     * Emits an {Approval} event.
     *
     * Requirements:
     *
     * - `owner` cannot be the zero address.
     * - `spender` cannot be the zero address.
     *
     * Overrides to this logic should be done to the variant with an additional `bool emitEvent` argument.
     */
    function _approve(address owner, address spender, uint256 value) internal {
        _approve(owner, spender, value, true);
    }

    /**
     * @dev Variant of {_approve} with an optional flag to enable or disable the {Approval} event.
     *
     * By default (when calling {_approve}) the flag is set to true. On the other hand, approval changes made by
     * `_spendAllowance` during the `transferFrom` operation set the flag to false. This saves gas by not emitting any
     * `Approval` event during `transferFrom` operations.
     *
     * Anyone who wishes to continue emitting `Approval` events on the`transferFrom` operation can force the flag to
     * true using the following override:
     * ```
     * function _approve(address owner, address spender, uint256 value, bool) internal virtual override {
     *     super._approve(owner, spender, value, true);
     * }
     * ```
     *
     * Requirements are the same as {_approve}.
     */
    function _approve(address owner, address spender, uint256 value, bool emitEvent) internal virtual {
        if (owner == address(0)) {
            revert ERC20InvalidApprover(address(0));
        }
        if (spender == address(0)) {
            revert ERC20InvalidSpender(address(0));
        }
        _allowances[owner][spender] = value;
        if (emitEvent) {
            emit Approval(owner, spender, value);
        }
    }

    /**
     * @dev Updates `owner` s allowance for `spender` based on spent `value`.
     *
     * Does not update the allowance value in case of infinite allowance.
     * Revert if not enough allowance is available.
     *
     * Does not emit an {Approval} event.
     */
    function _spendAllowance(address owner, address spender, uint256 value) internal virtual {
        uint256 currentAllowance = allowance(owner, spender);
        if (currentAllowance != type(uint256).max) {
            if (currentAllowance < value) {
                revert ERC20InsufficientAllowance(spender, currentAllowance, value);
            }
            unchecked {
                _approve(owner, spender, currentAllowance - value, false);
            }
        }
    }
}
IERC20.sol 79 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/IERC20.sol)

pragma solidity ^0.8.20;

/**
 * @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 value of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

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

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

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

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

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

pragma solidity ^0.8.20;

import {IERC20} from "../IERC20.sol";
import {IERC20Permit} from "../extensions/IERC20Permit.sol";
import {Address} from "../../../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 An operation with an ERC20 token failed.
     */
    error SafeERC20FailedOperation(address token);

    /**
     * @dev Indicates a failed `decreaseAllowance` request.
     */
    error SafeERC20FailedDecreaseAllowance(address spender, uint256 currentAllowance, uint256 requestedDecrease);

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

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

    /**
     * @dev 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);
        forceApprove(token, spender, oldAllowance + value);
    }

    /**
     * @dev Decrease the calling contract's allowance toward `spender` by `requestedDecrease`. If `token` returns no
     * value, non-reverting calls are assumed to be successful.
     */
    function safeDecreaseAllowance(IERC20 token, address spender, uint256 requestedDecrease) internal {
        unchecked {
            uint256 currentAllowance = token.allowance(address(this), spender);
            if (currentAllowance < requestedDecrease) {
                revert SafeERC20FailedDecreaseAllowance(spender, currentAllowance, requestedDecrease);
            }
            forceApprove(token, spender, currentAllowance - requestedDecrease);
        }
    }

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

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

    /**
     * @dev 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);
        if (returndata.length != 0 && !abi.decode(returndata, (bool))) {
            revert SafeERC20FailedOperation(address(token));
        }
    }

    /**
     * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
     * on the return value: the return value is optional (but if data is returned, it must not be false).
     * @param token The token targeted by the call.
     * @param data The call data (encoded using abi.encode or one of its variants).
     *
     * This is a variant of {_callOptionalReturn} that 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(token).code.length > 0;
    }
}
Ownable.sol 100 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)

pragma solidity ^0.8.20;

import {Context} from "../utils/Context.sol";

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

    /**
     * @dev The caller account is not authorized to perform an operation.
     */
    error OwnableUnauthorizedAccount(address account);

    /**
     * @dev The owner is not a valid owner account. (eg. `address(0)`)
     */
    error OwnableInvalidOwner(address owner);

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

    /**
     * @dev Initializes the contract setting the address provided by the deployer as the initial owner.
     */
    constructor(address initialOwner) {
        if (initialOwner == address(0)) {
            revert OwnableInvalidOwner(address(0));
        }
        _transferOwnership(initialOwner);
    }

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

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

    /**
     * @dev Throws if the sender is not the owner.
     */
    function _checkOwner() internal view virtual {
        if (owner() != _msgSender()) {
            revert OwnableUnauthorizedAccount(_msgSender());
        }
    }

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

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        if (newOwner == address(0)) {
            revert OwnableInvalidOwner(address(0));
        }
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}
Staker.sol 878 lines
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.23;

import {DelegationSurrogate} from "./DelegationSurrogate.sol";
import {INotifiableRewardReceiver} from "./interfaces/INotifiableRewardReceiver.sol";
import {IEarningPowerCalculator} from "./interfaces/IEarningPowerCalculator.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {Multicall} from "@openzeppelin/contracts/utils/Multicall.sol";
import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol";

/// @title Staker
/// @author [ScopeLift](https://scopelift.co)
/// @notice This contract manages the distribution of rewards to stakers. Rewards are denominated
/// in an ERC20 token and sent to the contract by authorized reward notifiers. To stake means to
/// deposit a designated, delegable ERC20 governance token and leave it over a period of time.
/// The contract allows stakers to delegate the voting power of the tokens they stake to any
/// governance delegatee on a per deposit basis. The contract also allows stakers to designate the
/// claimer address that earns rewards for the associated deposit.
///
/// The staking mechanism of this contract is directly inspired by the Synthetix StakingRewards.sol
/// implementation. The core mechanic involves the streaming of rewards over a designated period
/// of time. Each staker earns rewards proportional to their share of the total stake, and each
/// staker earns only while their tokens are staked. Stakers may add or withdraw their stake at any
/// point. Claimers can claim the rewards they've earned at any point. When a new reward is
/// received, the reward duration restarts, and the rate at which rewards are streamed is updated
/// to include the newly received rewards along with any remaining rewards that have finished
/// streaming since the last time a reward was received.
///
/// The rate at which a depositor earns rewards is proportional to their earning power. Earning
/// power is based on the amount the depositor has staked and the activity of their delegatee.
/// The calculation of earning power is handled by a separate module called the earning power
/// calculator. This module is set by the owner, and can be updated by the owner. If the owner of
/// the Staker contract is a DAO, which is the expected common case, this means the DAO has
/// the ability to define and iterate on its own definition of active, aligned participation,
/// and to decide how to reward it.
abstract contract Staker is INotifiableRewardReceiver, Multicall {
  using SafeCast for uint256;

  /// @notice A unique identifier assigned to each deposit.
  type DepositIdentifier is uint256;

  /// @notice Emitted when stake is deposited by a depositor, either to a new deposit or one that
  /// already exists.
  event StakeDeposited(
    address owner,
    DepositIdentifier indexed depositId,
    uint256 amount,
    uint256 depositBalance,
    uint256 earningPower
  );

  /// @notice Emitted when a depositor withdraws some portion of stake from a given deposit.
  event StakeWithdrawn(
    address owner,
    DepositIdentifier indexed depositId,
    uint256 amount,
    uint256 depositBalance,
    uint256 earningPower
  );

  /// @notice Emitted when a deposit's delegatee is changed.
  event DelegateeAltered(
    DepositIdentifier indexed depositId,
    address oldDelegatee,
    address newDelegatee,
    uint256 earningPower
  );

  /// @notice Emitted when a deposit's claimer is changed.
  event ClaimerAltered(
    DepositIdentifier indexed depositId,
    address indexed oldClaimer,
    address indexed newClaimer,
    uint256 earningPower
  );

  /// @notice Emitted when a claimer claims their earned reward.
  event RewardClaimed(
    DepositIdentifier indexed depositId,
    address indexed claimer,
    uint256 amount,
    uint256 earningPower
  );

  /// @notice Emitted when this contract is notified of a new reward.
  event RewardNotified(uint256 amount, address notifier);

  /// @notice Emitted when the admin address is set.
  event AdminSet(address indexed oldAdmin, address indexed newAdmin);

  /// @notice Emitted when the earning power calculator address is set.
  event EarningPowerCalculatorSet(
    address indexed oldEarningPowerCalculator, address indexed newEarningPowerCalculator
  );

  /// @notice Emitted when the max bump tip is modified.
  event MaxBumpTipSet(uint256 oldMaxBumpTip, uint256 newMaxBumpTip);

  /// @notice Emitted when the claim fee parameters are modified.
  event ClaimFeeParametersSet(
    uint96 oldFeeAmount, uint96 newFeeAmount, address oldFeeCollector, address newFeeCollector
  );

  /// @notice Emitted when a reward notifier address is enabled or disabled.
  event RewardNotifierSet(address indexed account, bool isEnabled);

  /// @notice Emitted when a deposit's earning power is changed via bumping.
  event EarningPowerBumped(
    DepositIdentifier indexed depositId,
    uint256 oldEarningPower,
    uint256 newEarningPower,
    address bumper,
    address tipReceiver,
    uint256 tipAmount
  );

  /// @notice Thrown when an account attempts a call for which it lacks appropriate permission.
  /// @param reason Human readable code explaining why the call is unauthorized.
  /// @param caller The address that attempted the unauthorized call.
  error Staker__Unauthorized(bytes32 reason, address caller);

  /// @notice Thrown if the new rate after a reward notification would be zero.
  error Staker__InvalidRewardRate();

  /// @notice Thrown if the following invariant is broken after a new reward: the contract should
  /// always have a reward balance sufficient to distribute at the reward rate across the reward
  /// duration.
  error Staker__InsufficientRewardBalance();

  /// @notice Thrown if the unclaimed rewards are insufficient to cover a bumper's requested tip,
  /// or in the case of an earning power decrease the tip of a subsequent earning power increase.
  error Staker__InsufficientUnclaimedRewards();

  /// @notice Thrown if a caller attempts to specify address zero for certain designated addresses.
  error Staker__InvalidAddress();

  /// @notice Thrown if a bumper's requested tip is invalid.
  error Staker__InvalidTip();

  /// @notice Thrown if the claim fee parameters are outside permitted bounds.
  error Staker__InvalidClaimFeeParameters();

  /// @notice Thrown when an onBehalf method is called with a deadline that has expired.
  error Staker__ExpiredDeadline();

  /// @notice Thrown if a caller supplies an invalid signature to a method that requires one.
  error Staker__InvalidSignature();

  /// @notice Thrown if an earning power update is unqualified to be bumped.
  /// @param score The would-be new earning power which did not qualify.
  error Staker__Unqualified(uint256 score);

  /// @notice Metadata associated with a discrete staking deposit.
  /// @param balance The deposit's staked balance.
  /// @param owner The owner of this deposit.
  /// @param delegatee The governance delegate who receives the voting weight for this deposit.
  /// @param claimer The address which has the right to withdraw rewards earned by this
  /// deposit.
  /// @param earningPower The "power" this deposit has as it pertains to earning rewards, which
  /// accrue to this deposit at a rate proportional to its share of the total earning power of the
  /// system.
  /// @param rewardPerTokenCheckpoint Checkpoint of the reward per token accumulator for this
  /// deposit. It represents the value of the global accumulator at the last time a given deposit's
  /// rewards were calculated and stored. The difference between the global value and this value
  /// can be used to calculate the interim rewards earned by given deposit.
  /// @param scaledUnclaimedRewardCheckpoint Checkpoint of the unclaimed rewards earned by a given
  /// deposit with the scale factor included. This value is stored any time an action is taken that
  /// specifically impacts the rate at which rewards are earned by a given deposit. Total unclaimed
  /// rewards for a deposit are thus this value plus all rewards earned after this checkpoint was
  /// taken. This value is reset to zero when the deposit's rewards are claimed.
  struct Deposit {
    uint96 balance;
    address owner;
    uint96 earningPower;
    address delegatee;
    address claimer;
    uint256 rewardPerTokenCheckpoint;
    uint256 scaledUnclaimedRewardCheckpoint;
  }

  /// @notice Parameters associated with the fee assessed when rewards are claimed.
  /// @param feeAmount The absolute amount of the reward token that is taken as a fee when rewards
  /// are claimed for a given deposit.
  /// @param feeCollector The address to which reward token fees are sent.
  struct ClaimFeeParameters {
    uint96 feeAmount;
    address feeCollector;
  }

  /// @notice ERC20 token in which rewards are denominated and distributed.
  IERC20 public immutable REWARD_TOKEN;

  /// @notice Delegable governance token which users stake to earn rewards.
  IERC20 public immutable STAKE_TOKEN;

  /// @notice Length of time over which rewards sent to this contract are distributed to stakers.
  uint256 public constant REWARD_DURATION = 30 days;

  /// @notice Scale factor used in reward calculation math to reduce rounding errors caused by
  /// truncation during division.
  uint256 public constant SCALE_FACTOR = 1e36;

  /// @notice The maximum value to which the claim fee can be set.
  /// @dev For anything other than a zero value, this immutable parameter should be set in the
  /// constructor of a concrete implementation inheriting from Staker.
  uint256 public immutable MAX_CLAIM_FEE;

  /// @dev Unique identifier that will be used for the next deposit.
  DepositIdentifier private nextDepositId;

  /// @notice Permissioned actor that can enable/disable `rewardNotifier` addresses, set the max
  /// bump tip, set the claim fee parameters, and update the earning power calculator.
  address public admin;

  /// @notice Maximum tip a bumper can request.
  uint256 public maxBumpTip;

  /// @notice Global amount currently staked across all deposits.
  uint256 public totalStaked;

  /// @notice Global amount of earning power for all deposits.
  uint256 public totalEarningPower;

  /// @notice Contract that determines a deposit's earning power based on their delegatee.
  /// @dev An earning power calculator should take into account that a deposit's earning power is a
  /// uint96. There may be overflow issues within governance staker if this is not taken into
  /// account. Also, there should be some mechanism to prevent the deposit from frequently being
  /// bumpable: if earning power changes frequently, this will eat into a users unclaimed rewards.
  IEarningPowerCalculator public earningPowerCalculator;

  /// @notice Tracks the total staked by a depositor across all unique deposits.
  mapping(address depositor => uint256 amount) public depositorTotalStaked;

  /// @notice Tracks the total earning power by a depositor across all unique deposits.
  mapping(address depositor => uint256 earningPower) public depositorTotalEarningPower;

  /// @notice Stores the metadata associated with a given deposit.
  mapping(DepositIdentifier depositId => Deposit deposit) public deposits;

  /// @notice Time at which rewards distribution will complete if there are no new rewards.
  uint256 public rewardEndTime;

  /// @notice Last time at which the global rewards accumulator was updated.
  uint256 public lastCheckpointTime;

  /// @notice Global rate at which rewards are currently being distributed to stakers,
  /// denominated in scaled reward tokens per second, using the SCALE_FACTOR.
  uint256 public scaledRewardRate;

  /// @notice Checkpoint value of the global reward per token accumulator.
  uint256 public rewardPerTokenAccumulatedCheckpoint;

  /// @notice Maps addresses to whether they are authorized to call `notifyRewardAmount`.
  mapping(address rewardNotifier => bool) public isRewardNotifier;

  /// @notice Current configuration parameters for the fee assessed on claiming.
  ClaimFeeParameters public claimFeeParameters;

  /// @param _rewardToken ERC20 token in which rewards will be denominated.
  /// @param _stakeToken Delegable governance token which users will stake to earn rewards.
  /// @param _earningPowerCalculator The contract that will serve as the initial calculator of
  /// earning power for the staker system.
  /// @param _admin Address which will have permission to manage reward notifiers, claim fee
  /// parameters, the max bump tip, and the reward calculator.
  constructor(
    IERC20 _rewardToken,
    IERC20 _stakeToken,
    IEarningPowerCalculator _earningPowerCalculator,
    uint256 _maxBumpTip,
    address _admin
  ) {
    REWARD_TOKEN = _rewardToken;
    STAKE_TOKEN = _stakeToken;
    _setAdmin(_admin);
    _setMaxBumpTip(_maxBumpTip);
    _setEarningPowerCalculator(address(_earningPowerCalculator));
  }

  /// @notice Set the admin address.
  /// @param _newAdmin Address of the new admin.
  /// @dev Caller must be the current admin.
  function setAdmin(address _newAdmin) external virtual {
    _revertIfNotAdmin();
    _setAdmin(_newAdmin);
  }

  /// @notice Set the earning power calculator address.
  function setEarningPowerCalculator(address _newEarningPowerCalculator) external virtual {
    _revertIfNotAdmin();
    _setEarningPowerCalculator(_newEarningPowerCalculator);
  }

  /// @notice Set the max bump tip.
  /// @param _newMaxBumpTip Value of the new max bump tip.
  /// @dev Caller must be the current admin.
  function setMaxBumpTip(uint256 _newMaxBumpTip) external virtual {
    _revertIfNotAdmin();
    _setMaxBumpTip(_newMaxBumpTip);
  }

  /// @notice Enables or disables a reward notifier address.
  /// @param _rewardNotifier Address of the reward notifier.
  /// @param _isEnabled `true` to enable the `_rewardNotifier`, or `false` to disable.
  /// @dev Caller must be the current admin.
  function setRewardNotifier(address _rewardNotifier, bool _isEnabled) external virtual {
    _revertIfNotAdmin();
    isRewardNotifier[_rewardNotifier] = _isEnabled;
    emit RewardNotifierSet(_rewardNotifier, _isEnabled);
  }

  /// @notice Updates the parameters related to the claim fee.
  /// @param _params The new fee parameters.
  /// @dev Caller must be current admin.
  function setClaimFeeParameters(ClaimFeeParameters memory _params) external virtual {
    _revertIfNotAdmin();
    _setClaimFeeParameters(_params);
  }

  /// @notice A method to get the delegation surrogate contract for a given delegate.
  /// @param _delegatee The address to which the delegation surrogate is delegating voting power.
  /// @return The delegation surrogate.
  /// @dev A concrete implementation should return a delegate surrogate address for a given
  /// delegatee. In practice this may be as simple as returning an address stored in a mapping or
  /// computing its create2 address.
  function surrogates(address _delegatee) public view virtual returns (DelegationSurrogate);

  /// @notice Timestamp representing the last time at which rewards have been distributed, which is
  /// either the current timestamp (because rewards are still actively being streamed) or the time
  /// at which the reward duration ended (because all rewards to date have already been streamed).
  /// @return Timestamp representing the last time at which rewards have been distributed.
  function lastTimeRewardDistributed() public view virtual returns (uint256) {
    if (rewardEndTime <= block.timestamp) return rewardEndTime;
    else return block.timestamp;
  }

  /// @notice Live value of the global reward per token accumulator. It is the sum of the last
  /// checkpoint value with the live calculation of the value that has accumulated in the interim.
  /// This number should monotonically increase over time as more rewards are distributed.
  /// @return Live value of the global reward per token accumulator.
  function rewardPerTokenAccumulated() public view virtual returns (uint256) {
    if (totalEarningPower == 0) return rewardPerTokenAccumulatedCheckpoint;

    return rewardPerTokenAccumulatedCheckpoint
      + (scaledRewardRate * (lastTimeRewardDistributed() - lastCheckpointTime)) / totalEarningPower;
  }

  /// @notice Live value of the unclaimed rewards earned by a given deposit. It is the
  /// sum of the last checkpoint value of the unclaimed rewards with the live calculation of the
  /// rewards that have accumulated for this account in the interim. This value can only increase,
  /// until it is reset to zero once the unearned rewards are claimed.
  ///
  /// Note that the contract tracks the unclaimed rewards internally with the scale factor
  /// included, in order to avoid the accrual of precision losses as users takes actions that
  /// cause rewards to be checkpointed. This external helper method is useful for integrations, and
  /// returns the value after it has been scaled down to the reward token's raw decimal amount.
  /// @param _depositId Identifier of the deposit in question.
  /// @return Live value of the unclaimed rewards earned by a given deposit.
  function unclaimedReward(DepositIdentifier _depositId) external view virtual returns (uint256) {
    return _scaledUnclaimedReward(deposits[_depositId]) / SCALE_FACTOR;
  }

  /// @notice Stake tokens to a new deposit. The caller must pre-approve the staking contract to
  /// spend at least the would-be staked amount of the token.
  /// @param _amount The amount of the staking token to stake.
  /// @param _delegatee The address to assign the governance voting weight of the staked tokens.
  /// @return _depositId The unique identifier for this deposit.
  /// @dev The delegatee may not be the zero address. The deposit will be owned by the message
  /// sender, and the claimer will also be the message sender.
  function stake(uint256 _amount, address _delegatee)
    external
    virtual
    returns (DepositIdentifier _depositId)
  {
    _depositId = _stake(msg.sender, _amount, _delegatee, msg.sender);
  }

  /// @notice Method to stake tokens to a new deposit. The caller must pre-approve the staking
  /// contract to spend at least the would-be staked amount of the token.
  /// @param _amount Quantity of the staking token to stake.
  /// @param _delegatee Address to assign the governance voting weight of the staked tokens.
  /// @param _claimer Address that will have the right to claim rewards for this stake.
  /// @return _depositId Unique identifier for this deposit.
  /// @dev Neither the delegatee nor the claimer may be the zero address. The deposit will be
  /// owned by the message sender.
  function stake(uint256 _amount, address _delegatee, address _claimer)
    external
    virtual
    returns (DepositIdentifier _depositId)
  {
    _depositId = _stake(msg.sender, _amount, _delegatee, _claimer);
  }

  /// @notice Add more staking tokens to an existing deposit. A staker should call this method when
  /// they have an existing deposit, and wish to stake more while retaining the same delegatee and
  /// claimer.
  /// @param _depositId Unique identifier of the deposit to which stake will be added.
  /// @param _amount Quantity of stake to be added.
  /// @dev The message sender must be the owner of the deposit.
  function stakeMore(DepositIdentifier _depositId, uint256 _amount) external virtual {
    Deposit storage deposit = deposits[_depositId];
    _revertIfNotDepositOwner(deposit, msg.sender);
    _stakeMore(deposit, _depositId, _amount);
  }

  /// @notice For an existing deposit, change the address to which governance voting power is
  /// assigned.
  /// @param _depositId Unique identifier of the deposit which will have its delegatee altered.
  /// @param _newDelegatee Address of the new governance delegate.
  /// @dev The new delegatee may not be the zero address. The message sender must be the owner of
  /// the deposit.
  function alterDelegatee(DepositIdentifier _depositId, address _newDelegatee) external virtual {
    Deposit storage deposit = deposits[_depositId];
    _revertIfNotDepositOwner(deposit, msg.sender);
    _alterDelegatee(deposit, _depositId, _newDelegatee);
  }

  /// @notice For an existing deposit, change the claimer account which has the right to
  /// withdraw staking rewards.
  /// @param _depositId Unique identifier of the deposit which will have its claimer altered.
  /// @param _newClaimer Address of the new claimer.
  /// @dev The new claimer may not be the zero address. The message sender must be the owner of
  /// the deposit.
  function alterClaimer(DepositIdentifier _depositId, address _newClaimer) external virtual {
    Deposit storage deposit = deposits[_depositId];
    _revertIfNotDepositOwner(deposit, msg.sender);
    _alterClaimer(deposit, _depositId, _newClaimer);
  }

  /// @notice Withdraw staked tokens from an existing deposit.
  /// @param _depositId Unique identifier of the deposit from which stake will be withdrawn.
  /// @param _amount Quantity of staked token to withdraw.
  /// @dev The message sender must be the owner of the deposit. Stake is withdrawn to the message
  /// sender's account.
  function withdraw(DepositIdentifier _depositId, uint256 _amount) external virtual {
    Deposit storage deposit = deposits[_depositId];
    _revertIfNotDepositOwner(deposit, msg.sender);
    _withdraw(deposit, _depositId, _amount);
  }

  /// @notice Claim reward tokens earned by a given deposit. Message sender must be the claimer
  /// address of the deposit or the owner of the deposit. Tokens are sent to the caller.
  /// @param _depositId Identifier of the deposit from which accrued rewards will be claimed.
  /// @return Amount of reward tokens claimed, after the fee has been assessed.
  function claimReward(DepositIdentifier _depositId) external virtual returns (uint256) {
    Deposit storage deposit = deposits[_depositId];
    if (deposit.claimer != msg.sender && deposit.owner != msg.sender) {
      revert Staker__Unauthorized("not claimer or owner", msg.sender);
    }
    return _claimReward(_depositId, deposit, msg.sender);
  }

  /// @notice Called by an authorized rewards notifier to alert the staking contract that a new
  /// reward has been transferred to it. It is assumed that the reward has already been
  /// transferred to this staking contract before the rewards notifier calls this method.
  /// @param _amount Quantity of reward tokens the staking contract is being notified of.
  /// @dev It is critical that only well behaved contracts are approved by the admin to call this
  /// method, for two reasons.
  ///
  /// 1. A misbehaving contract could grief stakers by frequently notifying this contract of tiny
  ///    rewards, thereby continuously stretching out the time duration over which real rewards are
  ///    distributed. It is required that reward notifiers supply reasonable rewards at reasonable
  ///    intervals.
  //  2. A misbehaving contract could falsely notify this contract of rewards that were not actually
  ///    distributed, creating a shortfall for those claiming their rewards after others. It is
  ///    required that a notifier contract always transfers the `_amount` to this contract before
  ///    calling this method.
  function notifyRewardAmount(uint256 _amount) external virtual {
    if (!isRewardNotifier[msg.sender]) revert Staker__Unauthorized("not notifier", msg.sender);

    // We checkpoint the accumulator without updating the timestamp at which it was updated,
    // because that second operation will be done after updating the reward rate.
    rewardPerTokenAccumulatedCheckpoint = rewardPerTokenAccumulated();

    if (block.timestamp >= rewardEndTime) {
      scaledRewardRate = (_amount * SCALE_FACTOR) / REWARD_DURATION;
    } else {
      uint256 _remainingReward = scaledRewardRate * (rewardEndTime - block.timestamp);
      scaledRewardRate = (_remainingReward + _amount * SCALE_FACTOR) / REWARD_DURATION;
    }

    rewardEndTime = block.timestamp + REWARD_DURATION;
    lastCheckpointTime = block.timestamp;

    if ((scaledRewardRate / SCALE_FACTOR) == 0) revert Staker__InvalidRewardRate();

    // This check cannot _guarantee_ sufficient rewards have been transferred to the contract,
    // because it cannot isolate the unclaimed rewards owed to stakers left in the balance. While
    // this check is useful for preventing degenerate cases, it is not sufficient. Therefore, it is
    // critical that only safe reward notifier contracts are approved to call this method by the
    // admin.
    if (
      (scaledRewardRate * REWARD_DURATION) > (REWARD_TOKEN.balanceOf(address(this)) * SCALE_FACTOR)
    ) revert Staker__InsufficientRewardBalance();

    emit RewardNotified(_amount, msg.sender);
  }

  /// @notice A function that a bumper can call to update a deposit's earning power when a
  /// qualifying change in the earning power is returned by the earning power calculator. A
  /// deposit's earning power may change as determined by the algorithm of the current earning power
  /// calculator. In order to incentivize bumpers to trigger these updates a portion of deposit's
  /// unclaimed rewards are sent to the bumper.
  /// @param _depositId The identifier for the deposit that needs an updated earning power.
  /// @param _tipReceiver The receiver of the reward for updating a deposit's earning power.
  /// @param _requestedTip The amount of tip requested by the third-party.
  function bumpEarningPower(
    DepositIdentifier _depositId,
    address _tipReceiver,
    uint256 _requestedTip
  ) external virtual {
    if (_requestedTip > maxBumpTip) revert Staker__InvalidTip();

    Deposit storage deposit = deposits[_depositId];

    _checkpointGlobalReward();
    _checkpointReward(deposit);

    uint256 _unclaimedRewards = deposit.scaledUnclaimedRewardCheckpoint / SCALE_FACTOR;

    (uint256 _newEarningPower, bool _isQualifiedForBump) = earningPowerCalculator.getNewEarningPower(
      deposit.balance, deposit.owner, deposit.delegatee, deposit.earningPower
    );
    if (!_isQualifiedForBump || _newEarningPower == deposit.earningPower) {
      revert Staker__Unqualified(_newEarningPower);
    }

    if (_newEarningPower > deposit.earningPower && _unclaimedRewards < _requestedTip) {
      revert Staker__InsufficientUnclaimedRewards();
    }

    // Note: underflow causes a revert if the requested  tip is more than unclaimed rewards
    if (_newEarningPower < deposit.earningPower && (_unclaimedRewards - _requestedTip) < maxBumpTip)
    {
      revert Staker__InsufficientUnclaimedRewards();
    }

    emit EarningPowerBumped(
      _depositId, deposit.earningPower, _newEarningPower, msg.sender, _tipReceiver, _requestedTip
    );

    // Update global earning power & deposit earning power based on this bump
    totalEarningPower =
      _calculateTotalEarningPower(deposit.earningPower, _newEarningPower, totalEarningPower);
    depositorTotalEarningPower[deposit.owner] = _calculateTotalEarningPower(
      deposit.earningPower, _newEarningPower, depositorTotalEarningPower[deposit.owner]
    );
    deposit.earningPower = _newEarningPower.toUint96();

    // Send tip to the receiver
    SafeERC20.safeTransfer(REWARD_TOKEN, _tipReceiver, _requestedTip);
    deposit.scaledUnclaimedRewardCheckpoint =
      deposit.scaledUnclaimedRewardCheckpoint - (_requestedTip * SCALE_FACTOR);
  }

  /// @notice Live value of the unclaimed rewards earned by a given deposit with the
  /// scale factor included. Used internally for calculating reward checkpoints while minimizing
  /// precision loss.
  /// @return Live value of the unclaimed rewards earned by a given deposit with the
  /// scale factor included.
  /// @dev See documentation for the public, non-scaled `unclaimedReward` method for more details.
  function _scaledUnclaimedReward(Deposit storage deposit) internal view virtual returns (uint256) {
    return deposit.scaledUnclaimedRewardCheckpoint
      + (deposit.earningPower * (rewardPerTokenAccumulated() - deposit.rewardPerTokenCheckpoint));
  }

  /// @notice Internal method which finds the existing surrogate contract—or deploys a new one if
  /// none exists—for a given delegatee.
  /// @param _delegatee Account for which a surrogate is sought.
  /// @return _surrogate The address of the surrogate contract for the delegatee.
  /// @dev A concrete implementation would either deploy a new delegate surrogate or return an
  /// existing surrogate for a given delegatee address.
  function _fetchOrDeploySurrogate(address _delegatee)
    internal
    virtual
    returns (DelegationSurrogate _surrogate);

  /// @notice Internal convenience method which calls the `transferFrom` method on the stake token
  /// contract and reverts on failure.
  /// @param _from Source account from which stake token is to be transferred.
  /// @param _to Destination account of the stake token which is to be transferred.
  /// @param _value Quantity of stake token which is to be transferred.
  function _stakeTokenSafeTransferFrom(address _from, address _to, uint256 _value) internal virtual {
    SafeERC20.safeTransferFrom(STAKE_TOKEN, _from, _to, _value);
  }

  /// @notice Internal method which generates and returns a unique, previously unused deposit
  /// identifier.
  /// @return _depositId Previously unused deposit identifier.
  function _useDepositId() internal virtual returns (DepositIdentifier _depositId) {
    _depositId = nextDepositId;
    nextDepositId = DepositIdentifier.wrap(DepositIdentifier.unwrap(_depositId) + 1);
  }

  /// @notice Internal convenience methods which performs the staking operations.
  /// @dev This method must only be called after proper authorization has been completed.
  /// @dev See public stake methods for additional documentation.
  function _stake(address _depositor, uint256 _amount, address _delegatee, address _claimer)
    internal
    virtual
    returns (DepositIdentifier _depositId)
  {
    _revertIfAddressZero(_delegatee);
    _revertIfAddressZero(_claimer);

    _checkpointGlobalReward();

    DelegationSurrogate _surrogate = _fetchOrDeploySurrogate(_delegatee);
    _depositId = _useDepositId();

    uint256 _earningPower = earningPowerCalculator.getEarningPower(_amount, _depositor, _delegatee);

    totalStaked += _amount;
    totalEarningPower += _earningPower;
    depositorTotalStaked[_depositor] += _amount;
    depositorTotalEarningPower[_depositor] += _earningPower;
    deposits[_depositId] = Deposit({
      balance: _amount.toUint96(),
      owner: _depositor,
      delegatee: _delegatee,
      claimer: _claimer,
      earningPower: _earningPower.toUint96(),
      rewardPerTokenCheckpoint: rewardPerTokenAccumulatedCheckpoint,
      scaledUnclaimedRewardCheckpoint: 0
    });
    _stakeTokenSafeTransferFrom(_depositor, address(_surrogate), _amount);
    emit StakeDeposited(_depositor, _depositId, _amount, _amount, _earningPower);
    emit ClaimerAltered(_depositId, address(0), _claimer, _earningPower);
    emit DelegateeAltered(_depositId, address(0), _delegatee, _earningPower);
  }

  /// @notice Internal convenience method which adds more stake to an existing deposit.
  /// @dev This method must only be called after proper authorization has been completed.
  /// @dev See public stakeMore methods for additional documentation.
  function _stakeMore(Deposit storage deposit, DepositIdentifier _depositId, uint256 _amount)
    internal
    virtual
  {
    _checkpointGlobalReward();
    _checkpointReward(deposit);

    DelegationSurrogate _surrogate = surrogates(deposit.delegatee);

    uint256 _newBalance = deposit.balance + _amount;
    uint256 _newEarningPower =
      earningPowerCalculator.getEarningPower(_newBalance, deposit.owner, deposit.delegatee);

    totalEarningPower =
      _calculateTotalEarningPower(deposit.earningPower, _newEarningPower, totalEarningPower);
    totalStaked += _amount;
    depositorTotalStaked[deposit.owner] += _amount;
    depositorTotalEarningPower[deposit.owner] = _calculateTotalEarningPower(
      deposit.earningPower, _newEarningPower, depositorTotalEarningPower[deposit.owner]
    );
    deposit.earningPower = _newEarningPower.toUint96();
    deposit.balance = _newBalance.toUint96();
    _stakeTokenSafeTransferFrom(deposit.owner, address(_surrogate), _amount);
    emit StakeDeposited(deposit.owner, _depositId, _amount, _newBalance, _newEarningPower);
  }

  /// @notice Internal convenience method which alters the delegatee of an existing deposit.
  /// @dev This method must only be called after proper authorization has been completed.
  /// @dev See public alterDelegatee methods for additional documentation.
  function _alterDelegatee(
    Deposit storage deposit,
    DepositIdentifier _depositId,
    address _newDelegatee
  ) internal virtual {
    _revertIfAddressZero(_newDelegatee);
    _checkpointGlobalReward();
    _checkpointReward(deposit);

    DelegationSurrogate _oldSurrogate = surrogates(deposit.delegatee);
    uint256 _newEarningPower =
      earningPowerCalculator.getEarningPower(deposit.balance, deposit.owner, _newDelegatee);

    totalEarningPower =
      _calculateTotalEarningPower(deposit.earningPower, _newEarningPower, totalEarningPower);
    depositorTotalEarningPower[deposit.owner] = _calculateTotalEarningPower(
      deposit.earningPower, _newEarningPower, depositorTotalEarningPower[deposit.owner]
    );

    emit DelegateeAltered(_depositId, deposit.delegatee, _newDelegatee, _newEarningPower);
    deposit.delegatee = _newDelegatee;
    deposit.earningPower = _newEarningPower.toUint96();
    DelegationSurrogate _newSurrogate = _fetchOrDeploySurrogate(_newDelegatee);
    _stakeTokenSafeTransferFrom(address(_oldSurrogate), address(_newSurrogate), deposit.balance);
  }

  /// @notice Internal convenience method which alters the claimer of an existing deposit.
  /// @dev This method must only be called after proper authorization has been completed.
  /// @dev See public alterClaimer methods for additional documentation.
  function _alterClaimer(Deposit storage deposit, DepositIdentifier _depositId, address _newClaimer)
    internal
    virtual
  {
    _revertIfAddressZero(_newClaimer);
    _checkpointGlobalReward();
    _checkpointReward(deposit);

    // Updating the earning power here is not strictly necessary, but if the user is touching their
    // deposit anyway, it seems reasonable to make sure their earning power is up to date.
    uint256 _newEarningPower =
      earningPowerCalculator.getEarningPower(deposit.balance, deposit.owner, deposit.delegatee);
    totalEarningPower =
      _calculateTotalEarningPower(deposit.earningPower, _newEarningPower, totalEarningPower);
    depositorTotalEarningPower[deposit.owner] = _calculateTotalEarningPower(
      deposit.earningPower, _newEarningPower, depositorTotalEarningPower[deposit.owner]
    );

    deposit.earningPower = _newEarningPower.toUint96();

    emit ClaimerAltered(_depositId, deposit.claimer, _newClaimer, _newEarningPower);
    deposit.claimer = _newClaimer;
  }

  /// @notice Internal convenience method which withdraws the stake from an existing deposit.
  /// @dev This method must only be called after proper authorization has been completed.
  /// @dev See public withdraw methods for additional documentation.
  function _withdraw(Deposit storage deposit, DepositIdentifier _depositId, uint256 _amount)
    internal
    virtual
  {
    _checkpointGlobalReward();
    _checkpointReward(deposit);

    // overflow prevents withdrawing more than balance
    uint256 _newBalance = deposit.balance - _amount;
    uint256 _newEarningPower =
      earningPowerCalculator.getEarningPower(_newBalance, deposit.owner, deposit.delegatee);

    totalStaked -= _amount;
    totalEarningPower =
      _calculateTotalEarningPower(deposit.earningPower, _newEarningPower, totalEarningPower);
    depositorTotalStaked[deposit.owner] -= _amount;
    depositorTotalEarningPower[deposit.owner] = _calculateTotalEarningPower(
      deposit.earningPower, _newEarningPower, depositorTotalEarningPower[deposit.owner]
    );

    deposit.balance = _newBalance.toUint96();
    deposit.earningPower = _newEarningPower.toUint96();
    _stakeTokenSafeTransferFrom(address(surrogates(deposit.delegatee)), deposit.owner, _amount);
    emit StakeWithdrawn(deposit.owner, _depositId, _amount, _newBalance, _newEarningPower);
  }

  /// @notice Internal convenience method which claims earned rewards.
  /// @return Amount of reward tokens claimed, after the claim fee has been assessed.
  /// @dev This method must only be called after proper authorization has been completed.
  /// @dev See public claimReward methods for additional documentation.
  function _claimReward(DepositIdentifier _depositId, Deposit storage deposit, address _claimer)
    internal
    virtual
    returns (uint256)
  {
    _checkpointGlobalReward();
    _checkpointReward(deposit);

    uint256 _reward = deposit.scaledUnclaimedRewardCheckpoint / SCALE_FACTOR;
    // Intentionally reverts due to overflow if unclaimed rewards are less than fee.
    uint256 _payout = _reward - claimFeeParameters.feeAmount;
    if (_payout == 0) return 0;

    // retain sub-wei dust that would be left due to the precision loss
    deposit.scaledUnclaimedRewardCheckpoint =
      deposit.scaledUnclaimedRewardCheckpoint - (_reward * SCALE_FACTOR);

    uint256 _newEarningPower =
      earningPowerCalculator.getEarningPower(deposit.balance, deposit.owner, deposit.delegatee);

    emit RewardClaimed(_depositId, _claimer, _payout, _newEarningPower);

    totalEarningPower =
      _calculateTotalEarningPower(deposit.earningPower, _newEarningPower, totalEarningPower);
    depositorTotalEarningPower[deposit.owner] = _calculateTotalEarningPower(
      deposit.earningPower, _newEarningPower, depositorTotalEarningPower[deposit.owner]
    );
    deposit.earningPower = _newEarningPower.toUint96();

    SafeERC20.safeTransfer(REWARD_TOKEN, _claimer, _payout);
    if (claimFeeParameters.feeAmount > 0) {
      SafeERC20.safeTransfer(
        REWARD_TOKEN, claimFeeParameters.feeCollector, claimFeeParameters.feeAmount
      );
    }
    return _payout;
  }

  /// @notice Checkpoints the global reward per token accumulator.
  function _checkpointGlobalReward() internal virtual {
    rewardPerTokenAccumulatedCheckpoint = rewardPerTokenAccumulated();
    lastCheckpointTime = lastTimeRewardDistributed();
  }

  /// @notice Checkpoints the unclaimed rewards and reward per token accumulator of a given
  /// deposit.
  /// @param deposit The deposit for which the reward parameters will be checkpointed.
  /// @dev This is a sensitive internal helper method that must only be called after global rewards
  /// accumulator has been checkpointed. It assumes the global `rewardPerTokenCheckpoint` is up to
  /// date.
  function _checkpointReward(Deposit storage deposit) internal virtual {
    deposit.scaledUnclaimedRewardCheckpoint = _scaledUnclaimedReward(deposit);
    deposit.rewardPerTokenCheckpoint = rewardPerTokenAccumulatedCheckpoint;
  }

  /// @notice Internal helper method which calculates and returns an updated value for total
  /// earning power based on the old and new earning power of a deposit which is being changed.
  /// @param _depositOldEarningPower The earning power of the deposit before a change is applied.
  /// @param _depositNewEarningPower The earning power of the deposit after a change is applied.
  /// @return _newTotalEarningPower The new total earning power.
  function _calculateTotalEarningPower(
    uint256 _depositOldEarningPower,
    uint256 _depositNewEarningPower,
    uint256 _totalEarningPower
  ) internal pure returns (uint256 _newTotalEarningPower) {
    return _totalEarningPower + _depositNewEarningPower - _depositOldEarningPower;
  }

  /// @notice Internal helper method which sets the admin address.
  /// @param _newAdmin Address of the new admin.
  function _setAdmin(address _newAdmin) internal virtual {
    _revertIfAddressZero(_newAdmin);
    emit AdminSet(admin, _newAdmin);
    admin = _newAdmin;
  }

  /// @notice Internal helper method which sets the earning power calculator address.
  function _setEarningPowerCalculator(address _newEarningPowerCalculator) internal virtual {
    _revertIfAddressZero(_newEarningPowerCalculator);
    emit EarningPowerCalculatorSet(address(earningPowerCalculator), _newEarningPowerCalculator);
    earningPowerCalculator = IEarningPowerCalculator(_newEarningPowerCalculator);
  }

  /// @notice Internal helper method which sets the max bump tip.
  /// @param _newMaxTip Value of the new max bump tip.
  function _setMaxBumpTip(uint256 _newMaxTip) internal virtual {
    emit MaxBumpTipSet(maxBumpTip, _newMaxTip);
    maxBumpTip = _newMaxTip;
  }

  /// @notice Internal helper method which sets the claim fee parameters.
  /// @param _params The new fee parameters.
  function _setClaimFeeParameters(ClaimFeeParameters memory _params) internal virtual {
    if (
      _params.feeAmount > MAX_CLAIM_FEE
        || (_params.feeCollector == address(0) && _params.feeAmount > 0)
    ) revert Staker__InvalidClaimFeeParameters();

    emit ClaimFeeParametersSet(
      claimFeeParameters.feeAmount,
      _params.feeAmount,
      claimFeeParameters.feeCollector,
      _params.feeCollector
    );

    claimFeeParameters = _params;
  }

  /// @notice Internal helper method which reverts Staker__Unauthorized if the message
  /// sender is not the admin.
  function _revertIfNotAdmin() internal view virtual {
    if (msg.sender != admin) revert Staker__Unauthorized("not admin", msg.sender);
  }

  /// @notice Internal helper method which reverts Staker__Unauthorized if the alleged
  /// owner is not the true owner of the deposit.
  /// @param deposit Deposit to validate.
  /// @param _owner Alleged owner of deposit.
  function _revertIfNotDepositOwner(Deposit storage deposit, address _owner) internal view virtual {
    if (_owner != deposit.owner) revert Staker__Unauthorized("not owner", _owner);
  }

  /// @notice Internal helper method which reverts with Staker__InvalidAddress if the
  /// account in question is address zero.
  /// @param _account Account to verify.
  function _revertIfAddressZero(address _account) internal pure {
    if (_account == address(0)) revert Staker__InvalidAddress();
  }
}
FixedGovLst.sol 490 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import {GovLst} from "./GovLst.sol";
import {FixedLstAddressAlias} from "./FixedLstAddressAlias.sol";
import {Staker} from "staker/Staker.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {IERC20Permit} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Permit.sol";
import {IERC20Metadata} from "@openzeppelin/contracts/interfaces/IERC20Metadata.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {EIP712} from "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
import {Nonces} from "@openzeppelin/contracts/utils/Nonces.sol";
import {Multicall} from "@openzeppelin/contracts/utils/Multicall.sol";

/// @title FixedGovLst
/// @author [ScopeLift](https://scopelift.co)
/// @notice This contract creates a fixed balance counterpart LST to the rebasing LST implemented in `GovLst.sol`.
/// In most ways, it can be thought of as a peer to the rebasing LST with a different accounting system. Whereas the
/// rebasing LST token is 1:1 with the underlying governance token, this fixed LST has an exchange rate. Whereas the
/// total supply and holder balances of the rebasing LST token increase automatically as rewards are distributed, this
/// fixed LST token has balances that stay fixed when rewards are distributed. Instead, the exchange rate changes such
/// that the same number of fixed LST tokens are now worth more of the underlying governance tokens.
///
/// While peers in most respects, the fixed LST ultimately hooks into the rebasing LST's accounting system under
/// the hood. One practical effect of this is slightly higher gas costs for operations using the fixed LST. Another
/// effect is that governance tokens staked in the fixed LST show up in the total supply of the of rebasing LST, but
/// not vice versa. In other words, the total supply of the rebasing LST is the sum of tokens staked in the rebasing
/// LST _and_ the fixed LST. The total supply of the fixed LST is isolated to itself. Note that this is *not* true of
/// user balances. A holder's fixed LST balance and rebasing LST balance are independent. All of these accounting
/// properties are effectively the same as the wrapped version of the rebasing LST.
///
/// One very important way in which the fixed LST is different from a wrapped version of the rebasing LST is with
/// regards to delegation. Holders of a wrapped LST tokens are not able to specify their own delegatee, instead all
/// tokens are delegated to the default. Holders of the fixed LST do not have to make this tradeoff. They are able to
/// specify a delegate in the same way as holders of the rebasing LST.
contract FixedGovLst is IERC20, IERC20Metadata, IERC20Permit, Multicall, EIP712, Nonces {
  using FixedLstAddressAlias for address;
  using SafeERC20 for IERC20;

  /// @notice Emitted when a holder updates their deposit identifier, which determines the delegatee of their voting
  /// weight.
  /// @dev This event must be combined with the `DepositUpdated` event on the UniLst for an accurate picture all deposit
  /// ids for a given holder.
  /// @param holder The address of the account updating their deposit.
  /// @param oldDepositId The old deposit identifier that loses the holder's voting weight.
  /// @param newDepositId The new deposit identifier that will receive the holder's voting weight.
  event DepositUpdated(
    address indexed holder, Staker.DepositIdentifier oldDepositId, Staker.DepositIdentifier newDepositId
  );

  /// @notice Emitted when governance tokens are staked to receive fixed LST tokens.
  /// @param account The address of the account staking tokens.
  /// @param amount The number of governance tokens staked.
  event Staked(address indexed account, uint256 amount);

  /// @notice Emitted when rebasing LST tokens are converted to fixed LST tokens.
  /// @param account The address of the account converting their tokens.
  /// @param amount The number of rebasing LST tokens converted to fixed LST tokens.
  event Fixed(address indexed account, uint256 amount);

  /// @notice Emitted when fixed LST tokens are converted to rebasing LST tokens.
  /// @param account The address of the account converting their tokens.
  /// @param amount The number of rebasing LST tokens received.
  event Unfixed(address indexed account, uint256 amount);

  /// @notice Emitted when rebasing LST tokens mistakenly sent to a fixed holder alias address are rescued.
  /// @param account The address of the account rescuing their tokens.
  /// @param amount The number of rebasing LST tokens received from the rescue.
  event Rescued(address indexed account, uint256 amount);

  /// @notice Thrown when a holder attempts to transfer more tokens than they hold.
  error FixedGovLst__InsufficientBalance();

  /// @notice Thrown by signature-based "onBehalf" methods when a signature is past its expiry date.
  error FixedGovLst__SignatureExpired();

  /// @notice Thrown by signature-based "onBehalf" methods when a signature is invalid.
  error FixedGovLst__InvalidSignature();

  /// @notice The corresponding rebasing LST token for which this contract serves as a fixed balance counterpart.
  GovLst public immutable LST;

  /// @notice The underlying governance token which is staked.
  IERC20 public immutable STAKE_TOKEN;

  /// @notice The factor by which scales are multiplied in the underlying rebasing LST.
  uint256 public immutable SHARE_SCALE_FACTOR;

  /// @notice The ERC20 Metadata compliant name of the fixed LST token.
  string private NAME;

  /// @notice The ERC20 Metadata compliant symbol of the fixed LST token.
  string private SYMBOL;

  /// @notice The number of decimals for the fixed LST token.
  uint8 private constant DECIMALS = 18;

  /// @notice Type hash used when encoding data for `permit` calls.
  bytes32 public constant PERMIT_TYPEHASH =
    keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");

  /// @notice The number of rebasing LST shares a given fixed LST token holder controls via their fixed LST holdings.
  /// @dev The fixed LST `balanceOf` the holder is this number scaled down by the `SHARE_SCALE_FACTOR`
  mapping(address _holder => uint256 _balance) private shareBalances;

  /// @notice The total number of rebasing LST shares controlled across all fixed LST token holders.
  /// @dev The fixed LST `totalSupply` is this number scaled down by the `SHARE_SCALE_FACTOR`.
  uint256 private totalShares;

  /// @notice Mapping used to determine the amount of Fixed LST tokens the spender has been approved to transfer on
  /// the holder's behalf.
  mapping(address holder => mapping(address spender => uint256 amount)) public allowance;

  /// @param _name The name for the fixed balance liquid stake token.
  /// @param _symbol The symbol for the fixed balance liquid stake token.
  /// @param _lst The rebasing LST for which this contract will serve as the fixed balance counterpart.
  constructor(
    string memory _name,
    string memory _symbol,
    string memory _version,
    GovLst _lst,
    IERC20 _stakeToken,
    uint256 _shareScaleFactor
  ) EIP712(_name, _version) {
    NAME = _name;
    SYMBOL = _symbol;
    LST = _lst;
    SHARE_SCALE_FACTOR = _shareScaleFactor;
    STAKE_TOKEN = _stakeToken;
  }

  /// @notice The decimal precision with which the fixed LST token stores its balances.
  function decimals() external pure virtual returns (uint8) {
    return DECIMALS;
  }

  /// @inheritdoc IERC20Metadata
  function name() external view virtual returns (string memory) {
    return NAME;
  }

  /// @inheritdoc IERC20Metadata
  function symbol() external view virtual returns (string memory) {
    return SYMBOL;
  }

  /// @notice The EIP712 signing version of the contract.
  function version() external view virtual returns (string memory) {
    return _EIP712Version();
  }

  /// @notice The balance of the holder in fixed LST tokens. Unlike the rebasing LST, this balance is stable even as
  /// rewards accrue. As a result, a fixed LST token does not map 1:1 with the balance of the underlying staked tokens.
  /// Instead, the holder's fixed LST balance remains the same, but the number of stake tokens he would receive if he
  /// were to unstake increases.
  /// @param _holder The account whose balance is being queried.
  /// @return The balance of the holder in fixed tokens.
  function balanceOf(address _holder) public view virtual returns (uint256) {
    return _scaleDown(shareBalances[_holder]);
  }

  /// @notice The total number of fixed LST tokens in existence. As with a holder's balance, this number does not
  /// change when rewards are distributed.
  function totalSupply() public view virtual returns (uint256) {
    return _scaleDown(totalShares);
  }

  /// @notice Get the current nonce for an owner
  /// @dev This function explicitly overrides both Nonces and IERC20Permit to allow compatibility
  /// @param _owner The address of the owner
  /// @return The current nonce for the owner
  function nonces(address _owner) public view virtual override(Nonces, IERC20Permit) returns (uint256) {
    return Nonces.nonces(_owner);
  }

  /// @notice The domain separator used by this contract for all EIP712 signature based methods.
  function DOMAIN_SEPARATOR() external view virtual returns (bytes32) {
    return _domainSeparatorV4();
  }

  /// @notice The delegatee to whom the voting weight in the default deposit is delegated.
  /// @dev This method is a pass through to the method of the same name and signature on the Rebasing LST. It is
  /// provided here as a convenience for integrators.
  function defaultDelegatee() external view virtual returns (address) {
    return LST.defaultDelegatee();
  }

  /// @notice The stake deposit identifier associated with a given delegatee address.
  /// @param _delegatee The delegatee in question.
  /// @return The deposit identifier of the deposit in question.
  /// @dev This method is a pass through to the method of the same name and signature on the Rebasing LST. It is
  /// provided here as a convenience for integrators.
  function depositForDelegatee(address _delegatee) external view virtual returns (Staker.DepositIdentifier) {
    return LST.depositForDelegatee(_delegatee);
  }

  /// @notice The delegatee to whom a given holder's stake is currently delegated. This will be the delegatee to whom
  /// the user has chosen to assign their voting weight OR the default delegatee, if the user's deposit has been
  /// moved to the override state.
  /// @param _holder The holder in question.
  /// @return _delegatee The address to which this holder's voting weight is currently delegated.
  function delegateeForHolder(address _holder) public view virtual returns (address _delegatee) {
    return LST.delegateeForHolder(_holder.fixedAlias());
  }

  /// @notice The delegatee to whom a given holder's stake is currently delegated. This will be the delegatee to whom
  /// the user has chosen to assign their voting weight OR the default delegatee, if the user's deposit has been
  /// moved to the override state.
  /// @param _holder The holder in question.
  /// @return The address to which this holder's voting weight is currently delegated.
  /// @dev This method is included for partial compatibility with the `IVotes` interface. It returns the same data as
  /// the `delegateeForHolder` method.
  function delegates(address _holder) external view virtual returns (address) {
    return LST.delegateeForHolder(_holder.fixedAlias());
  }

  /// @notice Returns the deposit identifier managed by the LST for a given delegatee. If that deposit does not yet
  /// exist, it initializes it. A depositor can call this method if the deposit for their chosen delegatee has not been
  /// previously initialized.
  /// @param _delegatee The address of the delegatee.
  /// @return The deposit identifier of the existing, or newly created, stake deposit for this delegatee.
  /// @dev This method is a pass through to the method of the same name and signature on the Rebasing LST. It is
  /// provided here as a convenience for integrators.
  function fetchOrInitializeDepositForDelegatee(address _delegatee) external virtual returns (Staker.DepositIdentifier) {
    return LST.fetchOrInitializeDepositForDelegatee(_delegatee);
  }

  /// @notice Sets the delegatee which will receive the voting weight of the caller's tokens staked in the fixed LST
  /// by specifying the deposit identifier associated with that delegatee.
  /// @param _newDepositId The identifier of a deposit which must be one owned by the rebasing LST. Underlying tokens
  /// staked in the fixed LST will be moved into this deposit.
  function updateDeposit(Staker.DepositIdentifier _newDepositId) public virtual {
    _updateDeposit(msg.sender, _newDepositId);
  }

  /// @notice Stake tokens and receive fixed balance LST tokens directly.
  /// @param _stakeTokens The number of governance tokens that will be staked.
  /// @return _fixedTokens The number of fixed balance LST tokens received upon staking. These tokens are *not*
  /// exchanged 1:1 with the stake tokens.
  /// @dev The caller must approve the Fixed LST contract to transfer at least the number of stake tokens being
  /// staked before calling this method.
  function stake(uint256 _stakeTokens) public virtual returns (uint256) {
    return _stake(msg.sender, _stakeTokens);
  }

  /// @notice Convert existing rebasing LST tokens to fixed balance LST tokens.
  /// @param _lstTokens The number of rebasing LST tokens that will be converted to fixed balance LST tokens.
  /// @return _fixedTokens The number of fixed balance LST tokens received upon fixing. These tokens are *not*
  /// exchanged 1:1 with the stake tokens.
  function convertToFixed(uint256 _lstTokens) external virtual returns (uint256) {
    return _convertToFixed(msg.sender, _lstTokens);
  }

  /// @notice Move fixed LST tokens held by the caller to another account.
  /// @param _to The address that will receive the transferred tokens.
  /// @param _fixedTokens The number of tokens to send.
  /// @return Whether the transfer was successful or not.
  /// @dev This method will always return true. It reverts in conditions where the transfer was not successful.
  function transfer(address _to, uint256 _fixedTokens) external virtual returns (bool) {
    _transfer(msg.sender, _to, _fixedTokens);
    return true;
  }

  /// @notice Move fixed LST tokens from one account to another, where the sender has provided an allowance to move
  /// tokens to the caller.
  /// @param _from The address that will send the transferred tokens.
  /// @param _to The address that will receive the transferred tokens.
  /// @param _fixedTokens The number of tokens to transfer.
  /// @return Whether the transfer was successful or not.
  /// @dev This method will always return true. It reverts in conditions where the transfer was not successful.
  function transferFrom(address _from, address _to, uint256 _fixedTokens) external virtual returns (bool) {
    _checkAndUpdateAllowance(_from, _fixedTokens);
    _transfer(_from, _to, _fixedTokens);
    return true;
  }

  /// @notice Convert fixed LST tokens to rebasing LST tokens.
  /// @param _fixedTokens The number of fixed LST tokens to convert.
  /// @return _lstTokens The number of rebasing LST tokens received.
  function convertToRebasing(uint256 _fixedTokens) external virtual returns (uint256) {
    return _convertToRebasing(msg.sender, _fixedTokens);
  }

  /// @notice Unstake fixed LST tokens and receive underlying staked tokens back. If a withdrawal delay is being
  /// enforced by the rebasing LST, tokens will be moved into the withdrawal gate.
  /// @param _fixedTokens The number of fixed LST tokens to unstake.
  /// @return _stakeTokens The number of underlying governance tokens received in exchange.
  function unstake(uint256 _fixedTokens) external virtual returns (uint256 _stakeTokens) {
    return _unstake(msg.sender, _fixedTokens);
  }

  /// @notice Allow a depositor to change the address they are delegating their staked tokens.
  /// @param _delegatee The address where voting is delegated.
  /// @dev This operation can be completed in a more gas efficient manner by calling `updateDeposit` with the depositId
  /// of the user's chosen delegatee, assuming it has already been initialized. This method is included primarily for
  /// partial compatibility with the `IVotes` interface.
  function delegate(address _delegatee) public virtual {
    Staker.DepositIdentifier _depositId = LST.fetchOrInitializeDepositForDelegatee(_delegatee);
    updateDeposit(_depositId);
  }

  /// @notice Save rebasing LST tokens that were mistakenly sent to the fixed holder alias address. Each fixed LST
  /// holder has an alias in the rebasing LST contract that manages the fixed holder's position. This alias is purely
  /// an implementation detail of the system, and not meant to be interacted with my regular users in anyway. However,
  /// if the holder of the rebasing LST token mistakenly sends tokens to a fixed LST alias address, this method allows
  /// the receiver of those tokens to reclaim them as part of their balance here in the LST.
  /// @return _fixedTokens The number of fixed LST tokens rescued by reclaiming rebasing LST tokens sent the caller's
  /// alias address.
  function rescue() external virtual returns (uint256 _fixedTokens) {
    return _rescue(msg.sender);
  }

  /// @notice Grant an allowance to the spender to transfer up to a certain amount of fixed LST tokens on behalf of the
  /// message sender.
  /// @param _spender The address which is granted the allowance to transfer from the message sender.
  /// @param _amount The total amount of the message sender's fixed LST tokens that the spender will be permitted to
  /// transfer.
  function approve(address _spender, uint256 _amount) external virtual returns (bool) {
    allowance[msg.sender][_spender] = _amount;
    emit Approval(msg.sender, _spender, _amount);
    return true;
  }

  /// @notice Grant an allowance to the spender to transfer up to a certain amount of fixed LST tokens on behalf of a
  /// user who has signed a message testifying to their intent to grant this allowance.
  /// @param _owner The account which is granting the allowance.
  /// @param _spender The address which is granted the allowance to transfer from the holder.
  /// @param _value The total amount of fixed LST tokens the spender will be permitted to transfer from the holder.
  /// @param _deadline The timestamp after which the signature should expire.
  /// @param _v ECDSA signature component: Parity of the `y` coordinate of point `R`
  /// @param _r ECDSA signature component: x-coordinate of `R`
  /// @param _s ECDSA signature component: `s` value of the signature
  function permit(address _owner, address _spender, uint256 _value, uint256 _deadline, uint8 _v, bytes32 _r, bytes32 _s)
    external
    virtual
  {
    if (block.timestamp > _deadline) {
      revert FixedGovLst__SignatureExpired();
    }

    bytes32 _structHash;
    // Unchecked because the only math done is incrementing
    // the owner's nonce which cannot realistically overflow.
    unchecked {
      _structHash = keccak256(abi.encode(PERMIT_TYPEHASH, _owner, _spender, _value, _useNonce(_owner), _deadline));
    }

    bytes32 _hash = _hashTypedDataV4(_structHash);

    address _recoveredAddress = ecrecover(_hash, _v, _r, _s);

    if (_recoveredAddress == address(0) || _recoveredAddress != _owner) {
      revert FixedGovLst__InvalidSignature();
    }

    allowance[_recoveredAddress][_spender] = _value;

    emit Approval(_owner, _spender, _value);
  }

  /// @notice Internal convenience method which performs transfer operations.
  /// @dev This method must only be called after proper authorization has been completed.
  /// @dev See public transfer methods for additional documentation.
  function _transfer(address _from, address _to, uint256 _fixedTokens) internal virtual {
    if (balanceOf(_from) < _fixedTokens) {
      revert FixedGovLst__InsufficientBalance();
    }

    (uint256 _senderShares, uint256 _receiverShares) = LST.transferFixed(_from, _to, _scaleUp(_fixedTokens));
    shareBalances[_from] -= _senderShares;
    shareBalances[_to] += _receiverShares;

    emit IERC20.Transfer(_from, _to, _fixedTokens);
  }

  /// @notice Internal helper method for updating the deposit identifier associated with a holder's account.
  /// @dev The deposit identifier determines which delegatee receives the voting weight of the holder's staked tokens.
  /// @param _newDepositId The identifier of a deposit which must be one owned by the rebasing LST. Underlying tokens
  /// staked in the fixed LST will be moved into this deposit.
  function _updateDeposit(address _account, Staker.DepositIdentifier _newDepositId) internal virtual {
    Staker.DepositIdentifier _oldDepositId = LST.updateFixedDeposit(_account, _newDepositId);
    emit DepositUpdated(_account, _oldDepositId, _newDepositId);
  }

  /// @notice Internal convenience method which performs the stake operation.
  /// @param _account The account to perform the stake action.
  /// @param _stakeTokens The amount of governance tokens to stake.
  /// @return The number of fixed tokens after staking.
  function _stake(address _account, uint256 _stakeTokens) internal virtual returns (uint256) {
    // Send the stake tokens to the LST.
    STAKE_TOKEN.safeTransferFrom(_account, address(LST), _stakeTokens);
    uint256 _shares = LST.stakeAndConvertToFixed(_account, _stakeTokens);
    shareBalances[_account] += _shares;
    totalShares += _shares;
    uint256 _fixedTokens = _scaleDown(_shares);
    emit IERC20.Transfer(address(0), _account, _fixedTokens);
    emit Fixed(_account, _stakeTokens);
    return _fixedTokens;
  }

  /// @notice Internal convenience method which performs the unstake operation.
  /// @param _account The account to perform the unstake action.
  /// @param _amount The amount of fixed tokens to unstake.
  /// @return The number of governance tokens after unstaking.
  function _unstake(address _account, uint256 _amount) internal virtual returns (uint256) {
    uint256 _shares = _scaleUp(_amount);
    // revert on overflow prevents unfixing more than balance
    shareBalances[_account] -= _shares;
    totalShares -= _shares;
    emit IERC20.Transfer(_account, address(0), _amount);
    uint256 _stakeTokens = LST.convertToRebasingAndUnstake(_account, _shares);
    emit Unfixed(_account, _stakeTokens);
    return _stakeTokens;
  }

  /// @notice Internal convenience method which performs the convert to fixed tokens operation.
  /// @param _account The account to perform the conversion action.
  /// @param _lstTokens The amount of rebasing tokens to convert.
  /// @return The number of fixed tokens.
  function _convertToFixed(address _account, uint256 _lstTokens) internal virtual returns (uint256) {
    uint256 _shares = LST.convertToFixed(_account, _lstTokens);
    shareBalances[_account] += _shares;
    totalShares += _shares;
    uint256 _fixedTokens = _scaleDown(_shares);
    emit IERC20.Transfer(address(0), _account, _fixedTokens);
    emit Fixed(_account, _lstTokens);
    return _fixedTokens;
  }

  /// @notice Internal convenience method which performs the convert to rebasing tokens operation.
  /// @param _account The account to perform the conversion action.
  /// @param _fixedTokens The amount of rebasing tokens to convert.
  /// @return The number of rebasing tokens.
  function _convertToRebasing(address _account, uint256 _fixedTokens) internal virtual returns (uint256) {
    uint256 _shares = _scaleUp(_fixedTokens);
    // revert on overflow prevents unfixing more than balance
    shareBalances[_account] -= _shares;
    totalShares -= _shares;
    emit IERC20.Transfer(_account, address(0), _fixedTokens);
    uint256 _lstTokens = LST.convertToRebasing(_account, _shares);
    emit Unfixed(_account, _lstTokens);
    return _lstTokens;
  }

  /// @notice Internal convenience method which performs the rescue operation.
  /// @param _account The account to perform the rescue action.
  /// @return The number of fixed tokens rescued.
  function _rescue(address _account) internal virtual returns (uint256) {
    // Shares not accounted for inside this Fixed LST accounting system are the ones to rescue.
    uint256 _sharesToRescue = LST.sharesOf(_account.fixedAlias()) - shareBalances[_account];

    // We intentionally scale down then scale up. The method is not intended for reclaiming dust below
    // the precision of the Fixed LST, but only for tokens accidentally sent to the alias address inside
    // the Rebasing LST contract.
    uint256 _fixedTokens = _scaleDown(_sharesToRescue);
    _sharesToRescue = _scaleUp(_fixedTokens);

    shareBalances[_account] += _sharesToRescue;
    totalShares += _sharesToRescue;
    emit IERC20.Transfer(address(0), _account, _fixedTokens);
    uint256 _stakeTokens = LST.stakeForShares(_sharesToRescue);
    emit Rescued(_account, _stakeTokens);
    return _fixedTokens;
  }

  /// @notice Internal helper that updates the allowance of the from address for the message sender, and reverts if the
  /// message sender does not have sufficient allowance.
  /// @param _from The address for which the message sender's allowance should be checked & updated.
  /// @param _fixedTokens The amount of the allowance to check and decrement.
  function _checkAndUpdateAllowance(address _from, uint256 _fixedTokens) internal virtual {
    uint256 allowed = allowance[_from][msg.sender];
    if (allowed != type(uint256).max) {
      allowance[_from][msg.sender] = allowed - _fixedTokens;
    }
  }

  /// @notice Internal helper that converts fixed LST tokens up to rebasing LST shares.
  /// @param _fixedTokens The number of fixed LST tokens.
  /// @return _lstShares The number of LST shares.
  function _scaleUp(uint256 _fixedTokens) internal view virtual returns (uint256 _lstShares) {
    _lstShares = _fixedTokens * SHARE_SCALE_FACTOR;
  }

  /// @notice Internal helper that converts rebasing LST shares down to fixed LST tokens
  /// @param _lstShares The number of LST shares.
  /// @return _fixedTokens The number of fixed LST tokens.
  function _scaleDown(uint256 _lstShares) internal view virtual returns (uint256 _fixedTokens) {
    _fixedTokens = _lstShares / SHARE_SCALE_FACTOR;
  }
}
GovLst.sol 1532 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {IERC20Metadata} from "@openzeppelin/contracts/interfaces/IERC20Metadata.sol";
import {IERC20Permit} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Permit.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {EIP712} from "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
import {Nonces} from "@openzeppelin/contracts/utils/Nonces.sol";
import {Multicall} from "@openzeppelin/contracts/utils/Multicall.sol";
import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol";
import {Staker} from "staker/Staker.sol";
import {WithdrawGate} from "./WithdrawGate.sol";
import {FixedGovLst} from "./FixedGovLst.sol";
import {FixedLstAddressAlias} from "./FixedLstAddressAlias.sol";

/// @title GovLst
/// @author [ScopeLift](https://scopelift.co)
/// @notice A liquid staking token implemented on top of Staker. Users can deposit a governance token and receive
/// a liquid staked governance token in exchange. Holders can specify a delegatee to which staked tokens' voting weight
/// will be delegated. 1 staked token is equivalent to 1 underlying governance token. As rewards are distributed,
/// holders' balances automatically increase to reflect their share of the rewards earned. Reward balances are delegated
/// to a default delegatee set by the token owner. Holders can consolidate their voting weight back to their chosen
/// delegate. Holders who don't specify a custom delegatee also have their stake's voting weight assigned to the default
/// delegatee.
///
/// To enable delegation functionality, the LST must manage an individual stake deposit for each delegatee,
/// including one for the default delegatee. As tokens are staked, unstaked, or transferred, the LST must move tokens
/// between these deposits to reflect the changing state. Because a holder balance is a dynamic calculation based on
/// its share of the total staked supply, the balance is subject to truncation. Care must be taken to ensure all
/// deposits remain solvent. Where a deposit might be left short due to truncation, we aim to accumulate these
/// shortfalls in the default deposit, which can be subsidized to remain solvent.
/// @dev Not all tokens are compatible with GovLST.
abstract contract GovLst is IERC20, IERC20Metadata, IERC20Permit, Ownable, Multicall, EIP712, Nonces {
  using FixedLstAddressAlias for address;
  using SafeCast for uint256;
  using SafeERC20 for IERC20;

  /// @notice Emitted when the LST owner updates the payout amount required for the MEV reward game in
  /// `claimAndDistributeReward`.
  event PayoutAmountSet(uint256 oldPayoutAmount, uint256 newPayoutAmount);

  /// @notice Emitted when the LST owner updates the reward parameters.
  event RewardParametersSet(uint256 payoutAmount, uint256 feeBips, address feeCollector);

  /// @notice Emitted when the default delegatee is updated by the owner or guardian.
  event DefaultDelegateeSet(address oldDelegatee, address newDelegatee);

  /// @notice Emitted when the delegatee guardian is updated by the owner or guardian itself.
  event DelegateeGuardianSet(address oldDelegatee, address newDelegatee);

  /// @notice Emitted when the minimum qualifying earning power bips is set.
  event MinQualifyingEarningPowerBipsSet(
    uint256 _oldMinQualifyingEarningPowerBips, uint256 _newMinQualifyingEarningPowerBips
  );

  /// @notice Emitted when a stake deposit is initialized for a new delegatee.
  event DepositInitialized(address indexed delegatee, Staker.DepositIdentifier depositId);

  /// @notice Emitted when a user updates their stake deposit, moving their staked tokens accordingly.
  /// @dev This event must be combined with the `DepositUpdated` event on the FixedGovLst for an accurate picture all
  /// deposit ids for a given holder.
  event DepositUpdated(
    address indexed holder, Staker.DepositIdentifier oldDepositId, Staker.DepositIdentifier newDepositId
  );

  /// @notice Emitted when a user stakes tokens in exchange for liquid staked tokens.
  event Staked(address indexed account, uint256 amount);

  /// @notice Emitted when a user exchanges their liquid staked tokens for the underlying staked token.
  event Unstaked(address indexed account, uint256 amount);

  /// @notice Emitted when a deposit delegatee is overridden to the default delegatee.
  event OverrideEnacted(Staker.DepositIdentifier depositId);

  /// @notice Emitted when an overridden deposit delegatee is set back to the original delegatee.
  event OverrideRevoked(Staker.DepositIdentifier depositId);

  /// @notice Emitted when an overridden deposit is migrated to a new default delegatee.
  event OverrideMigrated(Staker.DepositIdentifier depositId, address oldDelegatee, address newDelegatee);

  ///@notice Emitted when a reward is distributed by an MEV searcher who claims the LST's stake rewards in exchange
  /// for providing the payout amount of the stake token to the LST.
  event RewardDistributed(
    address indexed claimer,
    address indexed recipient,
    uint256 rewardsClaimed,
    uint256 payoutAmount,
    uint256 feeAmount,
    address feeCollector
  );

  /// @notice Struct to encapsulate reward-related parameters.
  struct RewardParameters {
    /// @notice The amount of stake token that an MEV searcher must provide in order to earn the right to claim the
    /// stake rewards earned by the LST. Can be set by the LST owner.
    uint80 payoutAmount;
    /// @notice The amount of stake token issued to the fee collector, expressed in basis points.
    /// @dev Fee in basis points (1 bips = 0.01%)
    uint16 feeBips;
    /// @notice The address that receives the fees when rewards are distributed.
    address feeCollector;
  }

  /// @notice Emitted when a user stakes and attributes their staking action to a referrer address.
  event StakedWithAttribution(Staker.DepositIdentifier _depositId, uint256 _amount, address indexed _referrer);

  /// @notice Emitted when someone irrevocably adds stake tokens to a deposit without receiving liquid tokens.
  event DepositSubsidized(Staker.DepositIdentifier indexed depositId, uint256 amount);

  /// @notice Thrown when an operation to change the default delegatee or its guardian is attempted by an account that
  /// does not have permission to alter it.
  error GovLst__Unauthorized();

  /// @notice Thrown when an operation is not possible because the holder's balance is insufficient.
  error GovLst__InsufficientBalance();

  /// @notice Thrown when a caller (likely an MEV searcher) would receive an insufficient payout in
  /// `claimAndDistributeReward`.
  error GovLst__InsufficientRewards();

  /// @notice Thrown when the LST owner attempts to set invalid fee parameters.
  error GovLst__InvalidFeeParameters();

  /// @notice Thrown by signature-based "onBehalf" methods when a signature is invalid.
  error GovLst__InvalidSignature();

  /// @notice Thrown by signature-based "onBehalf" methods when a signature is past its expiry date.
  error GovLst__SignatureExpired();

  /// @notice Thrown when the fee bips exceed the maximum allowed value.
  error GovLst__FeeBipsExceedMaximum(uint16 feeBips, uint16 maxFeeBips);

  /// @notice Thrown when attempting to set the fee collector to the zero address.
  error GovLst__FeeCollectorCannotBeZeroAddress();

  /// @notice Thrown when attempting to improperly override a deposit's delegatee.
  error GovLst__InvalidOverride();

  /// @notice Thrown when attempting to update a parameter with an invalid value.
  error GovLst__InvalidParameter();

  /// @notice Thrown when a deposit does not have the required amount of earning power for a certain action to be taken.
  /// An example of this is an attempted override of a deposit that has an earning power above the minimum earning power
  /// threshold.
  error GovLst__EarningPowerNotQualified(uint256 earningPower, uint256 thresholdEarningPower);

  /// @notice Thrown when a holder tries to update their deposit to an invalid deposit.
  error GovLst__InvalidDeposit();

  /// @notice The Staker instance in which staked tokens will be deposited to earn rewards.
  Staker public immutable STAKER;

  /// @notice The governance token used by the staking system.
  /// @dev Tokens greater than 18 decimals have a higher potential for overflow issues. Tokens with transfer fees,
  /// ERC-777 logic, or unorthodox logic are likely incompatible with this system. Only compliant ERC20
  /// tokens should be used as the stake token.
  IERC20 public immutable STAKE_TOKEN;

  /// @notice The token distributed as rewards by the staking instance.
  /// @dev Tokens greater than 18 decimals have a higher potential for overflow issues. Tokens with transfer fees,
  /// ERC-777 logic or, unorthodox logic are likely incompatible with this system. Only compliant ERC20
  /// tokens should be used as the reward token.
  IERC20 public immutable REWARD_TOKEN;

  /// @notice A coupled contract used by the LST to enforce an optional delay when withdrawing staked tokens from the
  /// LST. Can be used to prevent users from frontrunning rewards by staking and withdrawing repeatedly at opportune
  /// times. Said strategy would likely be unprofitable due to gas fees, but we eliminate the possibility via a delay.
  WithdrawGate public immutable WITHDRAW_GATE;

  /// @notice A coupled ERC20 contract that represents a fixed balance version of the LST. Whereas this  LST has
  /// dynamic, rebasing balances, the Fixed LST is deployed alongside of it but has balances that remain fixed. To
  /// achieve this, the Fixed LST contract is privileged to make special calls on behalf if its holders, allowing the
  /// Fixed LST to use the same accounting system as this rebasing LST.
  FixedGovLst public immutable FIXED_LST;

  /// @notice The deposit identifier of the default deposit.
  Staker.DepositIdentifier public immutable DEFAULT_DEPOSIT_ID;

  /// @notice Scale factor applied to the stake token before converting it to shares, which are tracked internally and
  /// used to
  /// calculate holders' balances dynamically as rewards are accumulated.
  uint256 public constant SHARE_SCALE_FACTOR = 1e10;

  /// @notice Data structure for global totals for the LST.
  /// @param supply The total staked tokens in the whole system, which by definition also represents the total supply
  /// of the LST token itself.
  /// @param shares The total shares that have been issued to all token holders, representing their proportional claim
  /// on the total supply.
  /// @dev The data types chosen for each parameter are meant to enable the data to pack into a single slot, while
  /// ensuring that real values occurring in the system are safe from overflow.
  struct Totals {
    uint96 supply;
    uint160 shares;
  }

  /// @notice Data structure for data pertaining to a given LST holder.
  /// @param depositId The staking system deposit identifier corresponding to the holder's delegatee of choice.
  /// @param balanceCheckpoint The portion of the holder's balance that is currently delegated to the delegatee of
  /// their choosing. LST tokens are assigned to this delegatee when a user stakes or receives tokens via transfer.
  /// When rewards are distributed, they accrue to the default delegatee unless the holder chooses to consolidate them.
  /// Holders who leave their delegatee set to the default have a balance checkpoint of zero by definition.
  /// @param shares The number of shares held by this holder, used to calculate the holder's balance dynamically, based
  /// on their proportion of the total shares, and thus the total staked supply.
  /// @dev The data types chosen for each parameter are meant to enable the data to pack into a single slot while still
  /// being safe from overflow for real values that can occur in the system.
  struct HolderState {
    uint32 depositId;
    uint96 balanceCheckpoint;
    uint128 shares;
  }

  /// @notice Data structure for deploying the `GovLst`.
  /// @param _fixedLstName The name for the fixed liquid stake token.
  /// @param _fixedLstSymbol The symbol for the fixed liquid stake token.
  /// @param _rebasingLstName The name for the rebasing liquid stake token.
  /// @param _rebasingLstSymbol The symbol for the rebasing liquid stake token.
  /// @param _staker The staker deployment where tokens will be staked.
  /// @param _initialDefaultDelegatee The initial delegatee to which the default deposit will be delegated.
  /// @param _initialOwner The address of the initial LST owner.
  /// @param _initialPayoutAmount The initial amount that must be provided to win the MEV race and claim the LST's
  /// stake rewards.
  /// @param _stakeToBurn The stake amount to burn in order to avoid divide by 0 errors. A reasonable value for this
  /// would be 1e15.
  /// @param _minQualifyingEarningPowerBips The minimum qualifying earning power amount in BIPs (1/10,000) for a deposit
  /// to not be overridden.
  struct ConstructorParams {
    string fixedLstName;
    string fixedLstSymbol;
    string rebasingLstName;
    string rebasingLstSymbol;
    string version;
    Staker staker;
    address initialDefaultDelegatee;
    address initialOwner;
    uint80 initialPayoutAmount;
    address initialDelegateeGuardian;
    uint256 stakeToBurn;
    uint256 minQualifyingEarningPowerBips;
  }

  /// @notice Type hash used when encoding data for `permit` calls.
  bytes32 public constant PERMIT_TYPEHASH =
    keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");

  /// @notice The name of the LST token.
  string private NAME;

  /// @notice The symbol of the LST token.
  string private SYMBOL;

  /// @notice The denominator for a basis point which is 1/100 of a percentage point.
  uint16 public constant BIPS = 1e4;

  /// @notice Maximum allowable fee in basis points (20%).
  uint16 public constant MAX_FEE_BIPS = 2000;

  /// @notice Maximum BIPs value for minimum qualifying earning power BIPs.
  uint256 public immutable MINIMUM_QUALIFYING_EARNING_POWER_BIPS_CAP = 20_000;

  /// @notice The global total supply and total shares for the LST.
  Totals internal totals;

  /// @notice The delegatee to whom the voting weight in the default deposit is delegated. Can be set by the LST owner,
  /// or the delegatee guardian. Once the guardian sets it, only the guardian can change it moving forward. The owner
  /// is no longer able to update it.
  address public defaultDelegatee;

  /// @notice Address which has the right to update the default delegatee assigned to the default deposit. Once this
  /// address takes an action, it can no longer be changed or overridden by the LST owner.
  address public delegateeGuardian;

  /// @notice One way switch that flips to true when the delegatee guardian takes its first action. Once set to true,
  /// the default delegatee and the guardian address can only be changed by the guardian itself.
  bool public isGuardianControlled;

  /// @notice The minimum qualifying earning amount in bips (1/10,000).
  uint256 public minQualifyingEarningPowerBips;

  /// @notice Struct to store reward-related parameters.
  RewardParameters internal rewardParams;

  /// @notice Mapping of delegatee address to the delegate's GovLST-created Staker deposit identifier. The
  /// delegatee for a given deposit can not change. All LST holders who choose the same delegatee will have their
  /// tokens staked in the corresponding deposit. Each delegatee can only have a single deposit.
  mapping(address delegatee => Staker.DepositIdentifier depositId) internal storedDepositIdForDelegatee;

  /// @notice Mapping of holder addresses to the data pertaining to their holdings.
  mapping(address holder => HolderState state) private holderStates;

  /// @notice Mapping used to determine the amount of LST tokens the spender has been approved to transfer on the
  /// holder's behalf.
  mapping(address holder => mapping(address spender => uint256 amount)) public allowance;

  /// @notice A mapping used to determine if a deposit's delegatee has been overridden to the default delegatee.
  mapping(Staker.DepositIdentifier depositId => bool isOverridden) public isOverridden;

  constructor(ConstructorParams memory _params)
    Ownable(_params.initialOwner)
    EIP712(_params.rebasingLstName, _params.version)
  {
    STAKER = _params.staker;
    STAKE_TOKEN = IERC20(_params.staker.STAKE_TOKEN());
    REWARD_TOKEN = IERC20(_params.staker.REWARD_TOKEN());
    NAME = _EIP712Name();
    SYMBOL = _params.rebasingLstSymbol;

    _setDefaultDelegatee(_params.initialDefaultDelegatee);
    _setRewardParams(_params.initialPayoutAmount, 0, _params.initialOwner);
    _setDelegateeGuardian(_params.initialDelegateeGuardian);
    _setMinQualifyingEarningPowerBips(_params.minQualifyingEarningPowerBips);

    STAKE_TOKEN.approve(address(_params.staker), type(uint256).max);
    // Create initial deposit for default so other methods can assume it exists.
    DEFAULT_DEPOSIT_ID = STAKER.stake(0, _params.initialDefaultDelegatee);
    STAKE_TOKEN.safeTransferFrom(msg.sender, address(this), _params.stakeToBurn);
    _stake(address(this), _params.stakeToBurn);
    _emitStakedEvent(address(this), _params.stakeToBurn);
    _emitTransferEvent(address(0), address(this), _params.stakeToBurn);

    // Deploy the WithdrawGate
    WITHDRAW_GATE = new WithdrawGate(_params.initialOwner, address(this), address(STAKE_TOKEN), 0);
    FIXED_LST = _deployFixedGovLst(
      _params.fixedLstName, _params.fixedLstSymbol, _params.version, this, STAKE_TOKEN, SHARE_SCALE_FACTOR
    );
  }

  /// @notice The name of the liquid stake token.
  function name() external view virtual override returns (string memory) {
    return NAME;
  }

  /// @notice The symbol for the liquid stake token.
  function symbol() external view virtual override returns (string memory) {
    return SYMBOL;
  }

  /// @notice The decimal precision which the LST tokens stores its balances with.
  /// @dev Make sure this matches the Stake token's decimals as an incompatibility
  /// will create an unintuitive UX where 1 LST token does not equal 1 stake token.
  function decimals() external pure virtual override returns (uint8) {
    return 18;
  }

  /// @notice The EIP712 signing version of the contract.
  function version() external view virtual returns (string memory) {
    return _EIP712Version();
  }

  /// @notice The total amount of LST token supply, also equal to the total number of stake tokens in the system.
  function totalSupply() external view virtual returns (uint256) {
    return uint256(totals.supply);
  }

  /// @notice The total number of outstanding shares issued to LST token holders. Each shares represents a proportional
  /// claim on the LST's total supply. As rewards are distributed, each share becomes worth proportionally more.
  function totalShares() external view virtual returns (uint256) {
    return uint256(totals.shares);
  }

  /// @notice Returns the number of shares that are valued at a given amount of stake token. Note that shares have a
  /// scale factor of `SHARE_SCALE_FACTOR` applied to minimize precision loss due to truncation.
  /// @param _amount The quantity of stake token that will be converted to a number of shares.
  /// @return The quantity of shares that is worth the requested quantity of stake token.
  function sharesForStake(uint256 _amount) external view virtual returns (uint256) {
    Totals memory _totals = totals;
    return _calcSharesForStakeUp(_amount, _totals);
  }

  /// @notice Returns the quantity of stake tokens that a given number of shares is valued at. In other words,
  /// ownership of a given number of shares translates to a claim on the quantity of stake tokens returned.
  /// @param _shares The quantity of shares that will be converted to stake tokens.
  /// @return The quantity of stake tokens which backs the provided quantity of shares.
  function stakeForShares(uint256 _shares) public view virtual returns (uint256) {
    Totals memory _totals = totals;
    return _calcStakeForShares(_shares, _totals);
  }

  /// @notice The current balance of LST tokens owned by the holder. Unlike a standard ERC20, this amount is calculated
  /// dynamically based on the holder's shares and the total supply of the LST. As rewards are distributed, a holder's
  /// balance will increase, even if they take no actions. In certain circumstances, a holder's balance can also
  /// decrease by tiny amounts without any action taken by the holder. This is due to changes in the global number of
  /// shares and supply resulting in a slightly different balance calculation after rounding.
  function balanceOf(address _holder) external view virtual returns (uint256) {
    HolderState memory _holderState = holderStates[_holder];
    Totals memory _totals = totals;

    return _calcBalanceOf(_holderState, _totals);
  }

  /// @notice The number of shares a given holder owns. Unlike a holder's balance, shares are stored statically and do
  /// not change unless the user is subject to some action, such as staking, unstaking, or transferring. The user's
  /// balance is calculated based on their proportion of the total outstanding shares.
  function sharesOf(address _holder) external view virtual returns (uint256 _sharesOf) {
    _sharesOf = holderStates[_holder].shares;
  }

  /// @notice The portion of the holder's balance that is currently delegated to the delegatee of their
  /// choosing. When a user stakes or receives LST tokens via transfer, they are a assigned to their delegatee, and
  /// accounted for in the balance checkpoint. This means the tokens are held in the corresponding deposit, and the
  /// voting weight for these tokens is assigned to the holder's chosen delegatee. When rewards are distributed, they
  /// accrue to the default delegatee unless the holder chooses to consolidate them. Therefore, the difference between
  /// the user's live balance and their balance checkpoint represents the number of tokens the holder has claim to that
  /// are currently held in the default deposit. Holders who leave their delegatee set to the default have a balance
  /// checkpoint of zero by definition.
  function balanceCheckpoint(address _holder) external view virtual returns (uint256 _balanceCheckpoint) {
    _balanceCheckpoint = holderStates[_holder].balanceCheckpoint;
  }

  /// @notice The delegatee to whom a given holder's stake is currently delegated. This will be the delegatee to whom
  /// the user has chosen to assign their voting weight OR the default delegatee, if the user's deposit has been
  /// moved to the override state.
  /// @param _holder The holder in question.
  /// @return _delegatee The address to which this holder's voting weight is currently delegated.
  function delegateeForHolder(address _holder) public view virtual returns (address _delegatee) {
    HolderState memory _holderState = holderStates[_holder];
    (,,, _delegatee,,,) = STAKER.deposits(_calcDepositId(_holderState));
  }

  /// @notice The delegatee to whom a given holder's stake is currently delegated. This will be the delegatee to whom
  /// the user has chosen to assign their voting weight OR the default delegatee, if the user's deposit has been
  /// moved to the override state.
  /// @param _holder The holder in question.
  /// @return The address to which this holder's voting weight is currently delegated.
  /// @dev This method is included for partial compatibility with the `IVotes` interface. It returns the same data as
  /// the `delegateeForHolder` method.
  function delegates(address _holder) external view virtual returns (address) {
    return delegateeForHolder(_holder);
  }

  /// @notice The stake deposit identifier associated with a given delegatee address.
  /// @param _delegatee The delegatee in question.
  /// @return The deposit identifier of the deposit in question.
  function depositForDelegatee(address _delegatee) public view virtual returns (Staker.DepositIdentifier) {
    if (_delegatee == defaultDelegatee || _delegatee == address(0)) {
      return DEFAULT_DEPOSIT_ID;
    } else {
      return storedDepositIdForDelegatee[_delegatee];
    }
  }

  /// @notice Returns the stake deposit identifier a given LST holder address is currently assigned to. If the
  /// address has not set a deposit identifier, it returns the default deposit.
  function depositIdForHolder(address _holder) external view virtual returns (Staker.DepositIdentifier) {
    HolderState memory _holderState = holderStates[_holder];
    return _calcDepositId(_holderState);
  }

  /// @notice Returns the current fee amount based on feeBips and payoutAmount.
  function feeAmount() external view virtual returns (uint256) {
    return _calcFeeAmount(rewardParams);
  }

  /// @notice Returns the current fee collector address.
  function feeCollector() external view virtual returns (address) {
    return rewardParams.feeCollector;
  }

  /// @notice Returns the current payout amount.
  function payoutAmount() external view virtual returns (uint256) {
    return uint256(rewardParams.payoutAmount);
  }

  /// @notice Get the current nonce for an owner
  /// @dev This function explicitly overrides both Nonces and IERC20Permit to allow compatibility
  /// @param _owner The address of the owner
  /// @return The current nonce for the owner
  function nonces(address _owner) public view virtual override(Nonces, IERC20Permit) returns (uint256) {
    return Nonces.nonces(_owner);
  }

  /// @notice The domain separator used by this contract for all EIP712 signature based methods.
  function DOMAIN_SEPARATOR() external view virtual returns (bytes32) {
    return _domainSeparatorV4();
  }

  /// @notice Returns the deposit identifier managed by the LST for a given delegatee. If that deposit does not yet
  /// exist, it initializes it. A depositor can call this method if the deposit for their chosen delegatee has not been
  /// previously initialized.
  /// @param _delegatee The address of the delegatee.
  /// @return The deposit identifier of the existing, or newly created, stake deposit for this delegatee.
  function fetchOrInitializeDepositForDelegatee(address _delegatee) public virtual returns (Staker.DepositIdentifier) {
    Staker.DepositIdentifier _depositId = depositForDelegatee(_delegatee);

    if (Staker.DepositIdentifier.unwrap(_depositId) != 0) {
      return _depositId;
    }

    // Create a new deposit for this delegatee if one is not yet managed by the LST
    _depositId = STAKER.stake(0, _delegatee);
    storedDepositIdForDelegatee[_delegatee] = _depositId;
    emit DepositInitialized(_delegatee, _depositId);
    return _depositId;
  }

  /// @notice Sets the deposit to which the message sender is choosing to assign their staked tokens. By using offchain
  /// indexing to find the deposit identifier corresponding with the delegatee of their choice, LST holders can choose
  /// to which address they want to assign the voting weight of their staked tokens. Additional staked tokens, or
  /// tokens transferred to the holder, will be moved into this deposit. Tokens distributed as rewards will remain in
  /// the default deposit, however holders may consolidate their reward tokens back to their preferred delegatee by
  /// calling this method again, even with their existing deposit identifier.
  /// @param _newDepositId The stake deposit identifier to which this holder's staked tokens will be moved to and
  /// kept in henceforth.
  function updateDeposit(Staker.DepositIdentifier _newDepositId) public virtual {
    Staker.DepositIdentifier _oldDepositId = _updateDeposit(msg.sender, _newDepositId);
    _emitDepositUpdatedEvent(msg.sender, _oldDepositId, _newDepositId);
  }

  /// @notice Stake tokens to receive liquid stake tokens. The caller must pre-approve the LST contract to spend at
  /// least the would-be amount of tokens.
  /// @param _amount The quantity of tokens that will be staked.
  /// @dev The increase in the holder's balance after staking may be slightly less than the amount staked due to
  /// rounding.
  function stake(uint256 _amount) external virtual returns (uint256) {
    STAKE_TOKEN.safeTransferFrom(msg.sender, address(this), _amount);
    _emitStakedEvent(msg.sender, _amount);
    _emitTransferEvent(address(0), msg.sender, _amount);
    return _stake(msg.sender, _amount);
  }

  /// @notice Stake tokens to receive liquid stake tokens, while also declaring the address that is responsible for
  /// referring the holder to the LST. This can be, for example, the owner of the frontend client who allowed the
  /// holder to interact with the contracts onchain. The call must pre-approve the LST contract to spend at least the
  /// would-be amount of tokens.
  /// @param _amount The quantity of tokens that will be staked.
  /// @param _referrer The address the holder is declaring has referred them to the LST. It will be emitted in an
  /// attribution event, but not otherwise used.
  function stakeWithAttribution(uint256 _amount, address _referrer) external virtual returns (uint256) {
    Staker.DepositIdentifier _depositId = _calcDepositId(holderStates[msg.sender]);
    emit StakedWithAttribution(_depositId, _amount, _referrer);
    STAKE_TOKEN.safeTransferFrom(msg.sender, address(this), _amount);
    _emitStakedEvent(msg.sender, _amount);
    _emitTransferEvent(address(0), msg.sender, _amount);
    return _stake(msg.sender, _amount);
  }

  /// @notice Destroy liquid staked tokens to receive the underlying token in exchange. Tokens are removed first from
  /// the default deposit, if any are present, then from holder's specified deposit if any are needed.
  /// @param _amount The amount of tokens to unstake.
  /// @dev The amount of tokens actually unstaked may be slightly less than the amount specified due to rounding.
  function unstake(uint256 _amount) external virtual returns (uint256) {
    _emitUnstakedEvent(msg.sender, _amount);
    _emitTransferEvent(msg.sender, address(0), _amount);
    return _unstake(msg.sender, _amount);
  }

  /// @notice Grant an allowance to the spender to transfer up to a certain amount of LST tokens on behalf of the
  /// message sender.
  /// @param _spender The address which is granted the allowance to transfer from the message sender.
  /// @param _amount The total amount of the message sender's LST tokens that the spender will be permitted to transfer.
  function approve(address _spender, uint256 _amount) external virtual returns (bool) {
    allowance[msg.sender][_spender] = _amount;
    emit Approval(msg.sender, _spender, _amount);
    return true;
  }

  /// @notice Grant an allowance to the spender to transfer up to a certain amount of LST tokens on behalf of a user
  /// who has signed a message testifying to their intent to grant this allowance.
  /// @param _owner The account which is granting the allowance.
  /// @param _spender The address which is granted the allowance to transfer from the holder.
  /// @param _value The total amount of LST tokens the spender will be permitted to transfer from the holder.
  /// @param _deadline The timestamp after which the signature should expire.
  /// @param _v ECDSA signature component: Parity of the `y` coordinate of point `R`
  /// @param _r ECDSA signature component: x-coordinate of `R`
  /// @param _s ECDSA signature component: `s` value of the signature
  function permit(address _owner, address _spender, uint256 _value, uint256 _deadline, uint8 _v, bytes32 _r, bytes32 _s)
    external
    virtual
  {
    if (block.timestamp > _deadline) {
      revert GovLst__SignatureExpired();
    }

    bytes32 _structHash;
    // Unchecked because the only math done is incrementing
    // the owner's nonce which cannot realistically overflow.
    unchecked {
      _structHash = keccak256(abi.encode(PERMIT_TYPEHASH, _owner, _spender, _value, _useNonce(_owner), _deadline));
    }

    bytes32 _hash = _hashTypedDataV4(_structHash);

    address _recoveredAddress = ecrecover(_hash, _v, _r, _s);

    if (_recoveredAddress == address(0) || _recoveredAddress != _owner) {
      revert GovLst__InvalidSignature();
    }

    allowance[_recoveredAddress][_spender] = _value;

    emit Approval(_owner, _spender, _value);
  }

  /// @notice Send liquid stake tokens from the message sender to the receiver.
  /// @param _to The address that will receive the message sender's tokens.
  /// @param _value The quantity of liquid stake tokens to send.
  /// @dev The sender's underlying tokens are moved first from the default deposit, if any are present, then from
  /// sender's specified deposit, if any are needed. All tokens are moved into the receiver's specified deposit.
  /// @dev The amount of tokens received by the user can be slightly less than the amount lost by the sender.
  /// Furthermore, both amounts can be less the value requested by the sender. All such effects are due to truncation.
  function transfer(address _to, uint256 _value) external virtual returns (bool) {
    _emitTransferEvent(msg.sender, _to, _value);
    _transfer(msg.sender, _to, _value);
    return true;
  }

  /// @notice Send liquid stake tokens from the message sender to the receiver, returning the changes in balances of
  /// each. Primarily intended for use by integrators, who might need to know the exact balance changes for internal
  /// accounting in other contracts.
  /// @param _receiver The address that will receive the message sender's tokens.
  /// @param _value The quantity of liquid stake tokens to send.
  /// @return _senderBalanceDecrease The amount by which the sender's balance of lst tokens decreased.
  /// @return _receiverBalanceIncrease The amount by which the receiver's balance of lst tokens increased.
  /// @dev The amount of tokens received by the user can be slightly less than the amount lost by the sender.
  /// Furthermore, both amounts can be less the value requested by the sender. All such effects are due to truncation.
  function transferAndReturnBalanceDiffs(address _receiver, uint256 _value)
    external
    virtual
    returns (uint256 _senderBalanceDecrease, uint256 _receiverBalanceIncrease)
  {
    _emitTransferEvent(msg.sender, _receiver, _value);
    return _transfer(msg.sender, _receiver, _value);
  }

  /// @notice Send liquid stake tokens from one account to the another on behalf of a user who has granted the
  /// message sender an allowance to do so.
  /// @param _from The address from where tokens will be transferred, which has previously granted the message sender
  /// an allowance of at least the quantity of tokens being transferred.
  /// @param _to The address that will receive the sender's tokens.
  /// @param _value The quantity of liquid stake tokens to send.
  /// @dev The sender's underlying tokens are moved first from the default deposit, if any are present, then from
  /// sender's specified deposit, if any are needed. All tokens are moved into the receiver's specified deposit.
  /// @dev The amount of tokens received by the receiver can be slightly less than the amount lost by the sender.
  /// Furthermore, both amounts can be less the value requested by the sender. All such effects are due to truncation.
  function transferFrom(address _from, address _to, uint256 _value) external virtual returns (bool) {
    _checkAndUpdateAllowance(_from, _value);
    _emitTransferEvent(_from, _to, _value);
    _transfer(_from, _to, _value);
    return true;
  }

  /// @notice Send liquid stake tokens from one account to the another on behalf of a user who has granted
  /// the message sender an allowance to do so, returning the changes in balances of each. Primarily intended for use
  /// by integrators, who might need to know the exact balance changes for internal accounting in other contracts.
  /// @param _from The address from where tokens will be transferred, which has previously granted the message sender
  /// an allowance of at least the quantity of tokens being transferred.
  /// @param _to The address that will receive the message sender's tokens.
  /// @param _value The quantity of liquid stake tokens to send.
  /// @return _senderBalanceDecrease The amount by which the sender's balance of lst tokens decreased.
  /// @return _receiverBalanceIncrease The amount by which the receiver's balance of lst tokens increased.
  /// @dev The amount of tokens received by the user can be slightly less than the amount lost by the sender.
  /// Furthermore, both amounts can be less the value requested by the sender. All such effects are due to truncation.
  function transferFromAndReturnBalanceDiffs(address _from, address _to, uint256 _value)
    external
    virtual
    returns (uint256 _senderBalanceDecrease, uint256 _receiverBalanceIncrease)
  {
    _checkAndUpdateAllowance(_from, _value);
    _emitTransferEvent(_from, _to, _value);
    return _transfer(_from, _to, _value);
  }

  /// @notice Public method that allows any caller to claim the stake rewards earned by the LST. Caller must pre-
  /// approve the LST on the stake token contract for at least the payout amount, which is transferred from the caller
  /// to the LST, added to the total supply, and sent to the default deposit. The effect of this is to distribute the
  /// reward proportionally to all LST holders, whose underlying shares will now be worth more of the stake token due
  /// the addition of the reward to the total supply. Because all holders' balances change simultaneously, transfer
  /// events cannot be emitted for all users. This makes the LST a non-standard ERC20, similar in nature to stETH.
  ///
  /// A quick example can help illustrate why an external party, such as an MEV searcher, would be incentivized to call
  /// this method. Imagine, purely for the sake of example, that the LST contract has accrued rewards of 1 ETH in the
  /// staking contract, and the payout amount here in the LST is set to 500 governance tokens. Imagine ETH is trading at
  /// $2,500 and the governance token is trading at $5. At this point, the value of ETH available to be claimed is equal
  /// to the value of the payout amount required in staking token. Once a bit more ETH accrues, it will be profitable
  /// for a searcher to trade the 500 staking tokens in exchange for the accrued ETH rewards. (This ignores other
  /// details, which real searchers would take into consideration, such as the gas/builder fee they would pay to call
  /// the method).
  ///
  /// Note that `payoutAmount` may be changed by the admin (governance). Any proposal that changes this amount is
  /// expected to be subject to the governance process, including a timelocked execution, and so it's unlikely that a
  /// caller would be surprised by a change in this value. Still, callers should be aware of the edge case where:
  /// 1. The caller grants a higher-than-necessary payout token approval to this LST.
  /// 2. Caller's claimAndDistributeReward transaction is in the mempool.
  /// 3. The payoutAmount is changed.
  /// 4. The claimAndDistributeReward transaction is now included in a block.
  ///
  /// The number of deposits owned by the LST may grow to such a size that all deposits cannot be included in a single
  /// call. This can cause the total payout of the LST's deposits to be processed with slight lag temporarily reducing
  /// the value of liquid staking tokens compared to direct staking.
  /// @param _recipient The address that will receive the stake reward payout.
  /// @param _minExpectedReward The minimum reward payout, in the reward token of the underlying staker contract, that
  /// the caller will accept in exchange for providing the payout amount of stake token. If the amount claimed is less
  /// than this, the transaction will revert. This parameter is a last line of defense against the MEV caller losing
  /// funds because they've been frontrun by another searcher.
  /// @param _depositIds List of deposits owned by the LST from which rewards will be claimed by the caller.
  function claimAndDistributeReward(
    address _recipient,
    uint256 _minExpectedReward,
    Staker.DepositIdentifier[] calldata _depositIds
  ) external virtual {
    RewardParameters memory _rewardParams = rewardParams;

    uint256 _feeAmount = _calcFeeAmount(_rewardParams);

    Totals memory _totals = totals;

    // By increasing the total supply by the amount of tokens that are distributed as part of the reward, the balance
    // of every holder increases proportional to the underlying shares which they hold.
    uint96 _newTotalSupply = _totals.supply + _rewardParams.payoutAmount; // payoutAmount is assumed safe

    uint160 _feeShares;
    if (_feeAmount > 0) {
      // Our goal is to issue shares to the fee collector such that the new shares the fee collector receives are
      // worth `feeAmount` of `stakeToken` after the reward is distributed. This can be expressed mathematically
      // as feeAmount = (feeShares * newTotalSupply) / newTotalShares, where the newTotalShares is equal to the sum of
      // the fee shares and the total existing shares. In this equation, all the terms are known except the fee shares.
      // Solving for the fee shares yields the following calculation.
      _feeShares = _calcFeeShares(_feeAmount, _newTotalSupply, _totals.shares);

      // By issuing these new shares to the `feeCollector` we effectively give the it `feeAmount` of the reward by
      // slightly diluting all other LST holders.
      holderStates[rewardParams.feeCollector].shares += uint128(_feeShares);
    }

    totals = Totals({supply: _newTotalSupply, shares: _totals.shares + _feeShares});

    // Transfer stake token to the LST
    STAKE_TOKEN.safeTransferFrom(msg.sender, address(this), _rewardParams.payoutAmount);
    // Stake the rewards with the default delegatee
    STAKER.stakeMore(DEFAULT_DEPOSIT_ID, _rewardParams.payoutAmount);

    // Claim the reward tokens earned by the LST for each deposit
    uint256 _rewards;
    for (uint256 _index = 0; _index < _depositIds.length; _index++) {
      _rewards += STAKER.claimReward(_depositIds[_index]);
    }

    // Ensure rewards distributed meet the claimers expectations; provides protection from frontrunning resulting in
    // loss of funds for the MEV racers.
    if (_rewards < _minExpectedReward) {
      revert GovLst__InsufficientRewards();
    }
    // Transfer the reward tokens to the recipient
    REWARD_TOKEN.safeTransfer(_recipient, _rewards);

    emit RewardDistributed(
      msg.sender, _recipient, _rewards, rewardParams.payoutAmount, _feeAmount, rewardParams.feeCollector
    );
  }

  /// @notice Allow a depositor to change the address they are delegating their staked tokens.
  /// @param _delegatee The address where voting is delegated.
  /// @return _depositId The deposit identifier for the delegatee.
  /// @dev This operation can be completed in a more gas efficient manner by calling `updateDeposit` with the depositId
  /// of the user's chosen delegatee, assuming it has already been initialized. This method is included primarily for
  /// partial compatibility with the `IVotes` interface.
  function delegate(address _delegatee) public virtual returns (Staker.DepositIdentifier _depositId) {
    _depositId = fetchOrInitializeDepositForDelegatee(_delegatee);
    updateDeposit(_depositId);
  }

  /// @notice Open method which allows anyone to add funds to a stake deposit owned by the LST. These funds are not
  /// added to the LST's supply and no tokens or shares are issues to the caller. The  purpose of this method is to
  /// provide buffer funds for shortfalls in deposits due to rounding errors. In particular, the system is designed
  /// such that rounding errors are known to accrue to the default deposit. Being able to provably provide a buffer for
  /// the default deposit is the primary intended use case for this method. That said, if other unknown issues were to
  /// arise, it could also be used to ensure the internal solvency of the other stake deposits as well.
  /// @param _depositId The stake deposit identifier that is being subsidized.
  /// @param _amount The quantity of stake tokens that will be sent to the deposit.
  /// @dev Caller must approve the LST contract for at least the `_amount` on the stake token before calling this
  /// method.
  function subsidizeDeposit(Staker.DepositIdentifier _depositId, uint256 _amount) external virtual {
    STAKE_TOKEN.safeTransferFrom(msg.sender, address(this), _amount);

    // This will revert if the deposit is not owned by this contract
    STAKER.stakeMore(_depositId, _amount);

    emit DepositSubsidized(_depositId, _amount);
  }

  /// @notice An open method which allows anyone to override the delegatee of a deposit to the default delegatee
  /// if the deposit's earning power is below the minimum qualifying earning power.
  /// @param _depositId The id of the deposit to override the delegatee.
  function enactOverride(Staker.DepositIdentifier _depositId) external virtual {
    (uint96 _balance,, uint96 _earningPower,,,,) = STAKER.deposits(_depositId);

    if (_isSameDepositId(_depositId, DEFAULT_DEPOSIT_ID) || isOverridden[_depositId] || _balance == 0) {
      revert GovLst__InvalidOverride();
    }

    bool _isAboveMin = uint256(_earningPower) * BIPS >= minQualifyingEarningPowerBips * _balance;
    if (_isAboveMin) {
      revert GovLst__EarningPowerNotQualified(
        uint256(_earningPower) * BIPS, uint256(minQualifyingEarningPowerBips) * _balance
      );
    }

    // Move the deposit delegatee to the default delegatee
    STAKER.alterDelegatee(_depositId, defaultDelegatee);

    isOverridden[_depositId] = true;

    emit OverrideEnacted(_depositId);
  }

  /// @notice An open method which allows anyone to reset a deposit with an overridden delegatee to the original
  /// deposit delegatee if the deposit's earning power is above the minimum qualifying earning power.
  /// @param _depositId The id of the deposit in the override state.
  /// @param _originalDelegatee The address of the delegatee that is supposed to be associated with this deposit, when
  /// it is not in the override state.
  function revokeOverride(Staker.DepositIdentifier _depositId, address _originalDelegatee) external virtual {
    if (!_isSameDepositId(storedDepositIdForDelegatee[_originalDelegatee], _depositId) || !isOverridden[_depositId]) {
      revert GovLst__InvalidOverride();
    }

    // Move the deposit's delegatee back to the original
    STAKER.alterDelegatee(_depositId, _originalDelegatee);

    (uint96 _balance,, uint96 _earningPower,,,,) = STAKER.deposits(_depositId);
    if (_balance == 0) {
      revert GovLst__InvalidOverride();
    }

    // Make sure earning power is above min earning power
    bool _isBelowMin = uint256(_earningPower) * BIPS < minQualifyingEarningPowerBips * _balance;
    if (_isBelowMin) {
      revert GovLst__EarningPowerNotQualified(
        uint256(_earningPower) * BIPS, uint256(minQualifyingEarningPowerBips) * _balance
      );
    }

    isOverridden[_depositId] = false;

    emit OverrideRevoked(_depositId);
  }

  /// @notice An open method that allows anyone to migrate an overridden deposit to a new default delegatee. This method
  /// handles cases where the `GovLst` owner sets a new default delegatee while some existing deposits are overridden,
  /// still referencing the old default delegatee.
  /// @param _depositId The id of the deposit in the override state.
  function migrateOverride(Staker.DepositIdentifier _depositId) external virtual {
    // Deposit must be overridden
    if (!isOverridden[_depositId]) {
      revert GovLst__InvalidOverride();
    }

    (,,, address _currentDelegatee,,,) = STAKER.deposits(_depositId);
    // Deposit cannot be the current default delegatee
    if (_currentDelegatee == defaultDelegatee) {
      revert GovLst__InvalidOverride();
    }

    // Move the deposit's delegatee back to the current default delegatee.
    STAKER.alterDelegatee(_depositId, defaultDelegatee);

    // Emit event
    emit OverrideMigrated(_depositId, _currentDelegatee, defaultDelegatee);
  }

  /// @notice Sets the reward parameters including payout amount, fee in bips, and fee collector.
  /// @param _params The new reward parameters.
  function setRewardParameters(RewardParameters memory _params) external virtual {
    _checkOwner();
    _setRewardParams(_params.payoutAmount, _params.feeBips, _params.feeCollector);
  }

  /// @notice Sets the minimum qualifying earning power amount in bips (1/10,000). This value determines whether a
  /// deposits delegatee needs to be overridden because it isn't earning enough of its possible staking rewards.
  /// @param _minQualifyingEarningPowerBips The new minimum qualifying earning power amount in bips (1/10,000).
  function setMinQualifyingEarningPowerBips(uint256 _minQualifyingEarningPowerBips) external virtual {
    _checkOwner();
    _setMinQualifyingEarningPowerBips(_minQualifyingEarningPowerBips);
  }

  /// @notice Update the default delegatee. Can only be called by the delegatee guardian or by the LST owner. Once the
  /// guardian takes an action on the LST, the owner can no longer override it.
  /// @param _newDelegatee The address which will be assigned as the delegatee for the default staker deposit.
  function setDefaultDelegatee(address _newDelegatee) external virtual {
    _checkAndToggleGuardianControlOrOwner();
    _setDefaultDelegatee(_newDelegatee);
    STAKER.alterDelegatee(DEFAULT_DEPOSIT_ID, _newDelegatee);
  }

  /// @notice Update the delegatee guardian. Can only be called by the delegatee guardian or by the LST owner. Once the
  /// guardian takes an action on the LST, the owner can no longer override it.
  /// @param _newDelegateeGuardian The address which w...

// [truncated — 82013 bytes total]
IERC20Permit.sol 90 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/extensions/IERC20Permit.sol)

pragma solidity ^0.8.20;

/**
 * @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);
}
ECDSA.sol 174 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/cryptography/ECDSA.sol)

pragma solidity ^0.8.20;

/**
 * @dev Elliptic Curve Digital Signature Algorithm (ECDSA) operations.
 *
 * These functions can be used to verify that a message was signed by the holder
 * of the private keys of a given address.
 */
library ECDSA {
    enum RecoverError {
        NoError,
        InvalidSignature,
        InvalidSignatureLength,
        InvalidSignatureS
    }

    /**
     * @dev The signature derives the `address(0)`.
     */
    error ECDSAInvalidSignature();

    /**
     * @dev The signature has an invalid length.
     */
    error ECDSAInvalidSignatureLength(uint256 length);

    /**
     * @dev The signature has an S value that is in the upper half order.
     */
    error ECDSAInvalidSignatureS(bytes32 s);

    /**
     * @dev Returns the address that signed a hashed message (`hash`) with `signature` or an error. This will not
     * return address(0) without also returning an error description. Errors are documented using an enum (error type)
     * and a bytes32 providing additional information about the error.
     *
     * If no error is returned, then the address can be used for verification purposes.
     *
     * The `ecrecover` EVM precompile allows for malleable (non-unique) signatures:
     * this function rejects them by requiring the `s` value to be in the lower
     * half order, and the `v` value to be either 27 or 28.
     *
     * IMPORTANT: `hash` _must_ be the result of a hash operation for the
     * verification to be secure: it is possible to craft signatures that
     * recover to arbitrary addresses for non-hashed data. A safe way to ensure
     * this is by receiving a hash of the original message (which may otherwise
     * be too long), and then calling {MessageHashUtils-toEthSignedMessageHash} on it.
     *
     * Documentation for signature generation:
     * - with https://web3js.readthedocs.io/en/v1.3.4/web3-eth-accounts.html#sign[Web3.js]
     * - with https://docs.ethers.io/v5/api/signer/#Signer-signMessage[ethers]
     */
    function tryRecover(bytes32 hash, bytes memory signature) internal pure returns (address, RecoverError, bytes32) {
        if (signature.length == 65) {
            bytes32 r;
            bytes32 s;
            uint8 v;
            // ecrecover takes the signature parameters, and the only way to get them
            // currently is to use assembly.
            /// @solidity memory-safe-assembly
            assembly {
                r := mload(add(signature, 0x20))
                s := mload(add(signature, 0x40))
                v := byte(0, mload(add(signature, 0x60)))
            }
            return tryRecover(hash, v, r, s);
        } else {
            return (address(0), RecoverError.InvalidSignatureLength, bytes32(signature.length));
        }
    }

    /**
     * @dev Returns the address that signed a hashed message (`hash`) with
     * `signature`. This address can then be used for verification purposes.
     *
     * The `ecrecover` EVM precompile allows for malleable (non-unique) signatures:
     * this function rejects them by requiring the `s` value to be in the lower
     * half order, and the `v` value to be either 27 or 28.
     *
     * IMPORTANT: `hash` _must_ be the result of a hash operation for the
     * verification to be secure: it is possible to craft signatures that
     * recover to arbitrary addresses for non-hashed data. A safe way to ensure
     * this is by receiving a hash of the original message (which may otherwise
     * be too long), and then calling {MessageHashUtils-toEthSignedMessageHash} on it.
     */
    function recover(bytes32 hash, bytes memory signature) internal pure returns (address) {
        (address recovered, RecoverError error, bytes32 errorArg) = tryRecover(hash, signature);
        _throwError(error, errorArg);
        return recovered;
    }

    /**
     * @dev Overload of {ECDSA-tryRecover} that receives the `r` and `vs` short-signature fields separately.
     *
     * See https://eips.ethereum.org/EIPS/eip-2098[EIP-2098 short signatures]
     */
    function tryRecover(bytes32 hash, bytes32 r, bytes32 vs) internal pure returns (address, RecoverError, bytes32) {
        unchecked {
            bytes32 s = vs & bytes32(0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
            // We do not check for an overflow here since the shift operation results in 0 or 1.
            uint8 v = uint8((uint256(vs) >> 255) + 27);
            return tryRecover(hash, v, r, s);
        }
    }

    /**
     * @dev Overload of {ECDSA-recover} that receives the `r and `vs` short-signature fields separately.
     */
    function recover(bytes32 hash, bytes32 r, bytes32 vs) internal pure returns (address) {
        (address recovered, RecoverError error, bytes32 errorArg) = tryRecover(hash, r, vs);
        _throwError(error, errorArg);
        return recovered;
    }

    /**
     * @dev Overload of {ECDSA-tryRecover} that receives the `v`,
     * `r` and `s` signature fields separately.
     */
    function tryRecover(
        bytes32 hash,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) internal pure returns (address, RecoverError, bytes32) {
        // EIP-2 still allows signature malleability for ecrecover(). Remove this possibility and make the signature
        // unique. Appendix F in the Ethereum Yellow paper (https://ethereum.github.io/yellowpaper/paper.pdf), defines
        // the valid range for s in (301): 0 < s < secp256k1n ÷ 2 + 1, and for v in (302): v ∈ {27, 28}. Most
        // signatures from current libraries generate a unique signature with an s-value in the lower half order.
        //
        // If your library generates malleable signatures, such as s-values in the upper range, calculate a new s-value
        // with 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 - s1 and flip v from 27 to 28 or
        // vice versa. If your library also generates signatures with 0/1 for v instead 27/28, add 27 to v to accept
        // these malleable signatures as well.
        if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) {
            return (address(0), RecoverError.InvalidSignatureS, s);
        }

        // If the signature is valid (and not malleable), return the signer address
        address signer = ecrecover(hash, v, r, s);
        if (signer == address(0)) {
            return (address(0), RecoverError.InvalidSignature, bytes32(0));
        }

        return (signer, RecoverError.NoError, bytes32(0));
    }

    /**
     * @dev Overload of {ECDSA-recover} that receives the `v`,
     * `r` and `s` signature fields separately.
     */
    function recover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) internal pure returns (address) {
        (address recovered, RecoverError error, bytes32 errorArg) = tryRecover(hash, v, r, s);
        _throwError(error, errorArg);
        return recovered;
    }

    /**
     * @dev Optionally reverts with the corresponding custom error according to the `error` argument provided.
     */
    function _throwError(RecoverError error, bytes32 errorArg) private pure {
        if (error == RecoverError.NoError) {
            return; // no error: do nothing
        } else if (error == RecoverError.InvalidSignature) {
            revert ECDSAInvalidSignature();
        } else if (error == RecoverError.InvalidSignatureLength) {
            revert ECDSAInvalidSignatureLength(uint256(errorArg));
        } else if (error == RecoverError.InvalidSignatureS) {
            revert ECDSAInvalidSignatureS(errorArg);
        }
    }
}
EIP712.sol 160 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/cryptography/EIP712.sol)

pragma solidity ^0.8.20;

import {MessageHashUtils} from "./MessageHashUtils.sol";
import {ShortStrings, ShortString} from "../ShortStrings.sol";
import {IERC5267} from "../../interfaces/IERC5267.sol";

/**
 * @dev https://eips.ethereum.org/EIPS/eip-712[EIP 712] is a standard for hashing and signing of typed structured data.
 *
 * The encoding scheme specified in the EIP requires a domain separator and a hash of the typed structured data, whose
 * encoding is very generic and therefore its implementation in Solidity is not feasible, thus this contract
 * does not implement the encoding itself. Protocols need to implement the type-specific encoding they need in order to
 * produce the hash of their typed data using a combination of `abi.encode` and `keccak256`.
 *
 * This contract implements the EIP 712 domain separator ({_domainSeparatorV4}) that is used as part of the encoding
 * scheme, and the final step of the encoding to obtain the message digest that is then signed via ECDSA
 * ({_hashTypedDataV4}).
 *
 * The implementation of the domain separator was designed to be as efficient as possible while still properly updating
 * the chain id to protect against replay attacks on an eventual fork of the chain.
 *
 * NOTE: This contract implements the version of the encoding known as "v4", as implemented by the JSON RPC method
 * https://docs.metamask.io/guide/signing-data.html[`eth_signTypedDataV4` in MetaMask].
 *
 * NOTE: In the upgradeable version of this contract, the cached values will correspond to the address, and the domain
 * separator of the implementation contract. This will cause the {_domainSeparatorV4} function to always rebuild the
 * separator from the immutable values, which is cheaper than accessing a cached version in cold storage.
 *
 * @custom:oz-upgrades-unsafe-allow state-variable-immutable
 */
abstract contract EIP712 is IERC5267 {
    using ShortStrings for *;

    bytes32 private constant TYPE_HASH =
        keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");

    // Cache the domain separator as an immutable value, but also store the chain id that it corresponds to, in order to
    // invalidate the cached domain separator if the chain id changes.
    bytes32 private immutable _cachedDomainSeparator;
    uint256 private immutable _cachedChainId;
    address private immutable _cachedThis;

    bytes32 private immutable _hashedName;
    bytes32 private immutable _hashedVersion;

    ShortString private immutable _name;
    ShortString private immutable _version;
    string private _nameFallback;
    string private _versionFallback;

    /**
     * @dev Initializes the domain separator and parameter caches.
     *
     * The meaning of `name` and `version` is specified in
     * https://eips.ethereum.org/EIPS/eip-712#definition-of-domainseparator[EIP 712]:
     *
     * - `name`: the user readable name of the signing domain, i.e. the name of the DApp or the protocol.
     * - `version`: the current major version of the signing domain.
     *
     * NOTE: These parameters cannot be changed except through a xref:learn::upgrading-smart-contracts.adoc[smart
     * contract upgrade].
     */
    constructor(string memory name, string memory version) {
        _name = name.toShortStringWithFallback(_nameFallback);
        _version = version.toShortStringWithFallback(_versionFallback);
        _hashedName = keccak256(bytes(name));
        _hashedVersion = keccak256(bytes(version));

        _cachedChainId = block.chainid;
        _cachedDomainSeparator = _buildDomainSeparator();
        _cachedThis = address(this);
    }

    /**
     * @dev Returns the domain separator for the current chain.
     */
    function _domainSeparatorV4() internal view returns (bytes32) {
        if (address(this) == _cachedThis && block.chainid == _cachedChainId) {
            return _cachedDomainSeparator;
        } else {
            return _buildDomainSeparator();
        }
    }

    function _buildDomainSeparator() private view returns (bytes32) {
        return keccak256(abi.encode(TYPE_HASH, _hashedName, _hashedVersion, block.chainid, address(this)));
    }

    /**
     * @dev Given an already https://eips.ethereum.org/EIPS/eip-712#definition-of-hashstruct[hashed struct], this
     * function returns the hash of the fully encoded EIP712 message for this domain.
     *
     * This hash can be used together with {ECDSA-recover} to obtain the signer of a message. For example:
     *
     * ```solidity
     * bytes32 digest = _hashTypedDataV4(keccak256(abi.encode(
     *     keccak256("Mail(address to,string contents)"),
     *     mailTo,
     *     keccak256(bytes(mailContents))
     * )));
     * address signer = ECDSA.recover(digest, signature);
     * ```
     */
    function _hashTypedDataV4(bytes32 structHash) internal view virtual returns (bytes32) {
        return MessageHashUtils.toTypedDataHash(_domainSeparatorV4(), structHash);
    }

    /**
     * @dev See {IERC-5267}.
     */
    function eip712Domain()
        public
        view
        virtual
        returns (
            bytes1 fields,
            string memory name,
            string memory version,
            uint256 chainId,
            address verifyingContract,
            bytes32 salt,
            uint256[] memory extensions
        )
    {
        return (
            hex"0f", // 01111
            _EIP712Name(),
            _EIP712Version(),
            block.chainid,
            address(this),
            bytes32(0),
            new uint256[](0)
        );
    }

    /**
     * @dev The name parameter for the EIP712 domain.
     *
     * NOTE: By default this function reads _name which is an immutable value.
     * It only reads from storage if necessary (in case the value is too large to fit in a ShortString).
     */
    // solhint-disable-next-line func-name-mixedcase
    function _EIP712Name() internal view returns (string memory) {
        return _name.toStringWithFallback(_nameFallback);
    }

    /**
     * @dev The version parameter for the EIP712 domain.
     *
     * NOTE: By default this function reads _version which is an immutable value.
     * It only reads from storage if necessary (in case the value is too large to fit in a ShortString).
     */
    // solhint-disable-next-line func-name-mixedcase
    function _EIP712Version() internal view returns (string memory) {
        return _version.toStringWithFallback(_versionFallback);
    }
}
Nonces.sol 46 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/Nonces.sol)
pragma solidity ^0.8.20;

/**
 * @dev Provides tracking nonces for addresses. Nonces will only increment.
 */
abstract contract Nonces {
    /**
     * @dev The nonce used for an `account` is not the expected current nonce.
     */
    error InvalidAccountNonce(address account, uint256 currentNonce);

    mapping(address account => uint256) private _nonces;

    /**
     * @dev Returns the next unused nonce for an address.
     */
    function nonces(address owner) public view virtual returns (uint256) {
        return _nonces[owner];
    }

    /**
     * @dev Consumes a nonce.
     *
     * Returns the current value and increments nonce.
     */
    function _useNonce(address owner) internal virtual returns (uint256) {
        // For each account, the nonce has an initial value of 0, can only be incremented by one, and cannot be
        // decremented or reset. This guarantees that the nonce never overflows.
        unchecked {
            // It is important to do x++ and not ++x here.
            return _nonces[owner]++;
        }
    }

    /**
     * @dev Same as {_useNonce} but checking that `nonce` is the next valid for `owner`.
     */
    function _useCheckedNonce(address owner, uint256 nonce) internal virtual {
        uint256 current = _useNonce(owner);
        if (nonce != current) {
            revert InvalidAccountNonce(owner, current);
        }
    }
}
IERC20Metadata.sol 26 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/extensions/IERC20Metadata.sol)

pragma solidity ^0.8.20;

import {IERC20} from "../IERC20.sol";

/**
 * @dev Interface for the optional metadata functions from the ERC20 standard.
 */
interface IERC20Metadata is IERC20 {
    /**
     * @dev Returns the name of the token.
     */
    function name() external view returns (string memory);

    /**
     * @dev Returns the symbol of the token.
     */
    function symbol() external view returns (string memory);

    /**
     * @dev Returns the decimals places of the token.
     */
    function decimals() external view returns (uint8);
}
Context.sol 28 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)

pragma solidity ^0.8.20;

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

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

    function _contextSuffixLength() internal view virtual returns (uint256) {
        return 0;
    }
}
draft-IERC6093.sol 161 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (interfaces/draft-IERC6093.sol)
pragma solidity ^0.8.20;

/**
 * @dev Standard ERC20 Errors
 * Interface of the https://eips.ethereum.org/EIPS/eip-6093[ERC-6093] custom errors for ERC20 tokens.
 */
interface IERC20Errors {
    /**
     * @dev Indicates an error related to the current `balance` of a `sender`. Used in transfers.
     * @param sender Address whose tokens are being transferred.
     * @param balance Current balance for the interacting account.
     * @param needed Minimum amount required to perform a transfer.
     */
    error ERC20InsufficientBalance(address sender, uint256 balance, uint256 needed);

    /**
     * @dev Indicates a failure with the token `sender`. Used in transfers.
     * @param sender Address whose tokens are being transferred.
     */
    error ERC20InvalidSender(address sender);

    /**
     * @dev Indicates a failure with the token `receiver`. Used in transfers.
     * @param receiver Address to which tokens are being transferred.
     */
    error ERC20InvalidReceiver(address receiver);

    /**
     * @dev Indicates a failure with the `spender`’s `allowance`. Used in transfers.
     * @param spender Address that may be allowed to operate on tokens without being their owner.
     * @param allowance Amount of tokens a `spender` is allowed to operate with.
     * @param needed Minimum amount required to perform a transfer.
     */
    error ERC20InsufficientAllowance(address spender, uint256 allowance, uint256 needed);

    /**
     * @dev Indicates a failure with the `approver` of a token to be approved. Used in approvals.
     * @param approver Address initiating an approval operation.
     */
    error ERC20InvalidApprover(address approver);

    /**
     * @dev Indicates a failure with the `spender` to be approved. Used in approvals.
     * @param spender Address that may be allowed to operate on tokens without being their owner.
     */
    error ERC20InvalidSpender(address spender);
}

/**
 * @dev Standard ERC721 Errors
 * Interface of the https://eips.ethereum.org/EIPS/eip-6093[ERC-6093] custom errors for ERC721 tokens.
 */
interface IERC721Errors {
    /**
     * @dev Indicates that an address can't be an owner. For example, `address(0)` is a forbidden owner in EIP-20.
     * Used in balance queries.
     * @param owner Address of the current owner of a token.
     */
    error ERC721InvalidOwner(address owner);

    /**
     * @dev Indicates a `tokenId` whose `owner` is the zero address.
     * @param tokenId Identifier number of a token.
     */
    error ERC721NonexistentToken(uint256 tokenId);

    /**
     * @dev Indicates an error related to the ownership over a particular token. Used in transfers.
     * @param sender Address whose tokens are being transferred.
     * @param tokenId Identifier number of a token.
     * @param owner Address of the current owner of a token.
     */
    error ERC721IncorrectOwner(address sender, uint256 tokenId, address owner);

    /**
     * @dev Indicates a failure with the token `sender`. Used in transfers.
     * @param sender Address whose tokens are being transferred.
     */
    error ERC721InvalidSender(address sender);

    /**
     * @dev Indicates a failure with the token `receiver`. Used in transfers.
     * @param receiver Address to which tokens are being transferred.
     */
    error ERC721InvalidReceiver(address receiver);

    /**
     * @dev Indicates a failure with the `operator`’s approval. Used in transfers.
     * @param operator Address that may be allowed to operate on tokens without being their owner.
     * @param tokenId Identifier number of a token.
     */
    error ERC721InsufficientApproval(address operator, uint256 tokenId);

    /**
     * @dev Indicates a failure with the `approver` of a token to be approved. Used in approvals.
     * @param approver Address initiating an approval operation.
     */
    error ERC721InvalidApprover(address approver);

    /**
     * @dev Indicates a failure with the `operator` to be approved. Used in approvals.
     * @param operator Address that may be allowed to operate on tokens without being their owner.
     */
    error ERC721InvalidOperator(address operator);
}

/**
 * @dev Standard ERC1155 Errors
 * Interface of the https://eips.ethereum.org/EIPS/eip-6093[ERC-6093] custom errors for ERC1155 tokens.
 */
interface IERC1155Errors {
    /**
     * @dev Indicates an error related to the current `balance` of a `sender`. Used in transfers.
     * @param sender Address whose tokens are being transferred.
     * @param balance Current balance for the interacting account.
     * @param needed Minimum amount required to perform a transfer.
     * @param tokenId Identifier number of a token.
     */
    error ERC1155InsufficientBalance(address sender, uint256 balance, uint256 needed, uint256 tokenId);

    /**
     * @dev Indicates a failure with the token `sender`. Used in transfers.
     * @param sender Address whose tokens are being transferred.
     */
    error ERC1155InvalidSender(address sender);

    /**
     * @dev Indicates a failure with the token `receiver`. Used in transfers.
     * @param receiver Address to which tokens are being transferred.
     */
    error ERC1155InvalidReceiver(address receiver);

    /**
     * @dev Indicates a failure with the `operator`’s approval. Used in transfers.
     * @param operator Address that may be allowed to operate on tokens without being their owner.
     * @param owner Address of the current owner of a token.
     */
    error ERC1155MissingApprovalForAll(address operator, address owner);

    /**
     * @dev Indicates a failure with the `approver` of a token to be approved. Used in approvals.
     * @param approver Address initiating an approval operation.
     */
    error ERC1155InvalidApprover(address approver);

    /**
     * @dev Indicates a failure with the `operator` to be approved. Used in approvals.
     * @param operator Address that may be allowed to operate on tokens without being their owner.
     */
    error ERC1155InvalidOperator(address operator);

    /**
     * @dev Indicates an array length mismatch between ids and values in a safeBatchTransferFrom operation.
     * Used in batch transfers.
     * @param idsLength Length of the array of token identifiers
     * @param valuesLength Length of the array of token amounts
     */
    error ERC1155InvalidArrayLength(uint256 idsLength, uint256 valuesLength);
}
Address.sol 159 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/Address.sol)

pragma solidity ^0.8.20;

/**
 * @dev Collection of functions related to the address type
 */
library Address {
    /**
     * @dev The ETH balance of the account is not enough to perform the operation.
     */
    error AddressInsufficientBalance(address account);

    /**
     * @dev There's no code at `target` (it is not a contract).
     */
    error AddressEmptyCode(address target);

    /**
     * @dev A call to an address target failed. The target may have reverted.
     */
    error FailedInnerCall();

    /**
     * @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.20/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
     */
    function sendValue(address payable recipient, uint256 amount) internal {
        if (address(this).balance < amount) {
            revert AddressInsufficientBalance(address(this));
        }

        (bool success, ) = recipient.call{value: amount}("");
        if (!success) {
            revert FailedInnerCall();
        }
    }

    /**
     * @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 or custom error, it is bubbled
     * up by this function (like regular Solidity function calls). However, if
     * the call reverted with no returned reason, this function reverts with a
     * {FailedInnerCall} error.
     *
     * 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.
     */
    function functionCall(address target, bytes memory data) internal returns (bytes memory) {
        return functionCallWithValue(target, data, 0);
    }

    /**
     * @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`.
     */
    function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
        if (address(this).balance < value) {
            revert AddressInsufficientBalance(address(this));
        }
        (bool success, bytes memory returndata) = target.call{value: value}(data);
        return verifyCallResultFromTarget(target, success, returndata);
    }

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

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

    /**
     * @dev Tool to verify that a low level call to smart-contract was successful, and reverts if the target
     * was not a contract or bubbling up the revert reason (falling back to {FailedInnerCall}) in case of an
     * unsuccessful call.
     */
    function verifyCallResultFromTarget(
        address target,
        bool success,
        bytes memory returndata
    ) internal view returns (bytes memory) {
        if (!success) {
            _revert(returndata);
        } else {
            // only check if target is a contract if the call was successful and the return data is empty
            // otherwise we already know that it was a contract
            if (returndata.length == 0 && target.code.length == 0) {
                revert AddressEmptyCode(target);
            }
            return returndata;
        }
    }

    /**
     * @dev Tool to verify that a low level call was successful, and reverts if it wasn't, either by bubbling the
     * revert reason or with a default {FailedInnerCall} error.
     */
    function verifyCallResult(bool success, bytes memory returndata) internal pure returns (bytes memory) {
        if (!success) {
            _revert(returndata);
        } else {
            return returndata;
        }
    }

    /**
     * @dev Reverts with returndata if present. Otherwise reverts with {FailedInnerCall}.
     */
    function _revert(bytes memory returndata) 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 FailedInnerCall();
        }
    }
}
DelegationSurrogate.sol 15 lines
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.23;

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

/// @title DelegationSurrogate
/// @author [ScopeLift](https://scopelift.co)
/// @notice A dead-simple contract whose only purpose is to hold ERC20 tokens which can always be
/// moved by the Surrogate's deployer.
abstract contract DelegationSurrogate {
  /// @param _token The token that will be held by this surrogate.
  constructor(IERC20 _token) {
    _token.approve(msg.sender, type(uint256).max);
  }
}
INotifiableRewardReceiver.sol 19 lines
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.23;

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

/// @title INotifiableRewardReceiver
/// @author [ScopeLift](https://scopelift.co)
/// @notice The communication interface between contracts that distribute rewards and the
/// Staker contract. In particular, said contracts only need to know the staker
/// implements the specified methods in order to forward payouts to the staker contract. The
/// Staker contract receives the rewards and abstracts the distribution mechanics.
interface INotifiableRewardReceiver {
  /// @notice ERC20 token in which rewards are denominated and distributed.
  function REWARD_TOKEN() external view returns (IERC20);

  /// @notice Method called to notify a reward receiver it has received a reward.
  /// @param _amount The amount of reward.
  function notifyRewardAmount(uint256 _amount) external;
}
IEarningPowerCalculator.sol 50 lines
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.23;

/// @title IEarningPowerCalculator
/// @author [ScopeLift](https://scopelift.co)
/// @notice Interface to which Earning Power Calculators must conform in order to provide earning
/// power updates to an instance of Staker. Well behaving earning power calculators should:
///
/// 1. Be deterministic, i.e. produce the same output for the same input at a given time.
/// 2. Return values that are in the same order of magnitude as reasonable stake token amounts.
///    Avoid returning values that are dramatically detached from the staked amount.
/// 3. Avoid too much "churn" on earning power values, in particular, avoid returning "true" for
///    the `getNewEarningPower` method's `_isQualifiedForBump` too frequently, as such an earning
///    calculator would result in repeated bumps on a user's deposit, requiring excessive
///    monitoring on their behalf to avoid eating into their rewards.
interface IEarningPowerCalculator {
  /// @notice Returns the current earning power for a given staker, delegatee and staking amount.
  /// @param _amountStaked The amount of tokens staked.
  /// @param _staker The address of the staker.
  /// @param _delegatee The address of their chosen delegatee.
  /// @return _earningPower The calculated earning power.
  function getEarningPower(uint256 _amountStaked, address _staker, address _delegatee)
    external
    view
    returns (uint256 _earningPower);

  /// @notice Returns the current earning power for a given staker, delegatee, staking amount, and
  /// old earning power, along with a flag denoting whether the change in earning power warrants
  /// "bumping." Bumping means paying a third party a bit of the rewards to update the deposit's
  /// earning power on the depositor's behalf.
  /// @param _amountStaked The amount of tokens staked.
  /// @param _staker The address of the staker.
  /// @param _delegatee The address of their chosen delegatee.
  /// @param _oldEarningPower The earning power currently assigned to the deposit for which new
  /// earning power is being calculated.
  /// @return _newEarningPower The calculated earning power.
  /// @return _isQualifiedForBump A flag indicating whether or not this new earning power qualifies
  /// the deposit for having its earning power bumped.
  /// @dev Earning Power calculators should only "qualify" a bump when the difference warrants a
  /// forced update by a third party. This could be, for example, to reduce a deposit's earning
  /// power because their delegatee has become inactive. Even in these cases, a calculator should
  /// avoid qualifying for a bump too frequently. A calculator implementer may, for example, want
  /// to implement a grace period or a threshold difference before qualifying a deposit for a bump.
  function getNewEarningPower(
    uint256 _amountStaked,
    address _staker,
    address _delegatee,
    uint256 _oldEarningPower
  ) external view returns (uint256 _newEarningPower, bool _isQualifiedForBump);
}
Multicall.sol 37 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.1) (utils/Multicall.sol)

pragma solidity ^0.8.20;

import {Address} from "./Address.sol";
import {Context} from "./Context.sol";

/**
 * @dev Provides a function to batch together multiple calls in a single external call.
 *
 * Consider any assumption about calldata validation performed by the sender may be violated if it's not especially
 * careful about sending transactions invoking {multicall}. For example, a relay address that filters function
 * selectors won't filter calls nested within a {multicall} operation.
 *
 * NOTE: Since 5.0.1 and 4.9.4, this contract identifies non-canonical contexts (i.e. `msg.sender` is not {_msgSender}).
 * If a non-canonical context is identified, the following self `delegatecall` appends the last bytes of `msg.data`
 * to the subcall. This makes it safe to use with {ERC2771Context}. Contexts that don't affect the resolution of
 * {_msgSender} are not propagated to subcalls.
 */
abstract contract Multicall is Context {
    /**
     * @dev Receives and executes a batch of function calls on this contract.
     * @custom:oz-upgrades-unsafe-allow-reachable delegatecall
     */
    function multicall(bytes[] calldata data) external virtual returns (bytes[] memory results) {
        bytes memory context = msg.sender == _msgSender()
            ? new bytes(0)
            : msg.data[msg.data.length - _contextSuffixLength():];

        results = new bytes[](data.length);
        for (uint256 i = 0; i < data.length; i++) {
            results[i] = Address.functionDelegateCall(address(this), bytes.concat(data[i], context));
        }
        return results;
    }
}
SafeCast.sol 1153 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/math/SafeCast.sol)
// This file was procedurally generated from scripts/generate/templates/SafeCast.js.

pragma solidity ^0.8.20;

/**
 * @dev Wrappers over Solidity's uintXX/intXX casting operators with added overflow
 * checks.
 *
 * Downcasting from uint256/int256 in Solidity does not revert on overflow. This can
 * easily result in undesired exploitation or bugs, since developers usually
 * assume that overflows raise errors. `SafeCast` restores this intuition by
 * reverting the transaction when such an operation overflows.
 *
 * Using this library instead of the unchecked operations eliminates an entire
 * class of bugs, so it's recommended to use it always.
 */
library SafeCast {
    /**
     * @dev Value doesn't fit in an uint of `bits` size.
     */
    error SafeCastOverflowedUintDowncast(uint8 bits, uint256 value);

    /**
     * @dev An int value doesn't fit in an uint of `bits` size.
     */
    error SafeCastOverflowedIntToUint(int256 value);

    /**
     * @dev Value doesn't fit in an int of `bits` size.
     */
    error SafeCastOverflowedIntDowncast(uint8 bits, int256 value);

    /**
     * @dev An uint value doesn't fit in an int of `bits` size.
     */
    error SafeCastOverflowedUintToInt(uint256 value);

    /**
     * @dev Returns the downcasted uint248 from uint256, reverting on
     * overflow (when the input is greater than largest uint248).
     *
     * Counterpart to Solidity's `uint248` operator.
     *
     * Requirements:
     *
     * - input must fit into 248 bits
     */
    function toUint248(uint256 value) internal pure returns (uint248) {
        if (value > type(uint248).max) {
            revert SafeCastOverflowedUintDowncast(248, value);
        }
        return uint248(value);
    }

    /**
     * @dev Returns the downcasted uint240 from uint256, reverting on
     * overflow (when the input is greater than largest uint240).
     *
     * Counterpart to Solidity's `uint240` operator.
     *
     * Requirements:
     *
     * - input must fit into 240 bits
     */
    function toUint240(uint256 value) internal pure returns (uint240) {
        if (value > type(uint240).max) {
            revert SafeCastOverflowedUintDowncast(240, value);
        }
        return uint240(value);
    }

    /**
     * @dev Returns the downcasted uint232 from uint256, reverting on
     * overflow (when the input is greater than largest uint232).
     *
     * Counterpart to Solidity's `uint232` operator.
     *
     * Requirements:
     *
     * - input must fit into 232 bits
     */
    function toUint232(uint256 value) internal pure returns (uint232) {
        if (value > type(uint232).max) {
            revert SafeCastOverflowedUintDowncast(232, value);
        }
        return uint232(value);
    }

    /**
     * @dev Returns the downcasted uint224 from uint256, reverting on
     * overflow (when the input is greater than largest uint224).
     *
     * Counterpart to Solidity's `uint224` operator.
     *
     * Requirements:
     *
     * - input must fit into 224 bits
     */
    function toUint224(uint256 value) internal pure returns (uint224) {
        if (value > type(uint224).max) {
            revert SafeCastOverflowedUintDowncast(224, value);
        }
        return uint224(value);
    }

    /**
     * @dev Returns the downcasted uint216 from uint256, reverting on
     * overflow (when the input is greater than largest uint216).
     *
     * Counterpart to Solidity's `uint216` operator.
     *
     * Requirements:
     *
     * - input must fit into 216 bits
     */
    function toUint216(uint256 value) internal pure returns (uint216) {
        if (value > type(uint216).max) {
            revert SafeCastOverflowedUintDowncast(216, value);
        }
        return uint216(value);
    }

    /**
     * @dev Returns the downcasted uint208 from uint256, reverting on
     * overflow (when the input is greater than largest uint208).
     *
     * Counterpart to Solidity's `uint208` operator.
     *
     * Requirements:
     *
     * - input must fit into 208 bits
     */
    function toUint208(uint256 value) internal pure returns (uint208) {
        if (value > type(uint208).max) {
            revert SafeCastOverflowedUintDowncast(208, value);
        }
        return uint208(value);
    }

    /**
     * @dev Returns the downcasted uint200 from uint256, reverting on
     * overflow (when the input is greater than largest uint200).
     *
     * Counterpart to Solidity's `uint200` operator.
     *
     * Requirements:
     *
     * - input must fit into 200 bits
     */
    function toUint200(uint256 value) internal pure returns (uint200) {
        if (value > type(uint200).max) {
            revert SafeCastOverflowedUintDowncast(200, value);
        }
        return uint200(value);
    }

    /**
     * @dev Returns the downcasted uint192 from uint256, reverting on
     * overflow (when the input is greater than largest uint192).
     *
     * Counterpart to Solidity's `uint192` operator.
     *
     * Requirements:
     *
     * - input must fit into 192 bits
     */
    function toUint192(uint256 value) internal pure returns (uint192) {
        if (value > type(uint192).max) {
            revert SafeCastOverflowedUintDowncast(192, value);
        }
        return uint192(value);
    }

    /**
     * @dev Returns the downcasted uint184 from uint256, reverting on
     * overflow (when the input is greater than largest uint184).
     *
     * Counterpart to Solidity's `uint184` operator.
     *
     * Requirements:
     *
     * - input must fit into 184 bits
     */
    function toUint184(uint256 value) internal pure returns (uint184) {
        if (value > type(uint184).max) {
            revert SafeCastOverflowedUintDowncast(184, value);
        }
        return uint184(value);
    }

    /**
     * @dev Returns the downcasted uint176 from uint256, reverting on
     * overflow (when the input is greater than largest uint176).
     *
     * Counterpart to Solidity's `uint176` operator.
     *
     * Requirements:
     *
     * - input must fit into 176 bits
     */
    function toUint176(uint256 value) internal pure returns (uint176) {
        if (value > type(uint176).max) {
            revert SafeCastOverflowedUintDowncast(176, value);
        }
        return uint176(value);
    }

    /**
     * @dev Returns the downcasted uint168 from uint256, reverting on
     * overflow (when the input is greater than largest uint168).
     *
     * Counterpart to Solidity's `uint168` operator.
     *
     * Requirements:
     *
     * - input must fit into 168 bits
     */
    function toUint168(uint256 value) internal pure returns (uint168) {
        if (value > type(uint168).max) {
            revert SafeCastOverflowedUintDowncast(168, value);
        }
        return uint168(value);
    }

    /**
     * @dev Returns the downcasted uint160 from uint256, reverting on
     * overflow (when the input is greater than largest uint160).
     *
     * Counterpart to Solidity's `uint160` operator.
     *
     * Requirements:
     *
     * - input must fit into 160 bits
     */
    function toUint160(uint256 value) internal pure returns (uint160) {
        if (value > type(uint160).max) {
            revert SafeCastOverflowedUintDowncast(160, value);
        }
        return uint160(value);
    }

    /**
     * @dev Returns the downcasted uint152 from uint256, reverting on
     * overflow (when the input is greater than largest uint152).
     *
     * Counterpart to Solidity's `uint152` operator.
     *
     * Requirements:
     *
     * - input must fit into 152 bits
     */
    function toUint152(uint256 value) internal pure returns (uint152) {
        if (value > type(uint152).max) {
            revert SafeCastOverflowedUintDowncast(152, value);
        }
        return uint152(value);
    }

    /**
     * @dev Returns the downcasted uint144 from uint256, reverting on
     * overflow (when the input is greater than largest uint144).
     *
     * Counterpart to Solidity's `uint144` operator.
     *
     * Requirements:
     *
     * - input must fit into 144 bits
     */
    function toUint144(uint256 value) internal pure returns (uint144) {
        if (value > type(uint144).max) {
            revert SafeCastOverflowedUintDowncast(144, value);
        }
        return uint144(value);
    }

    /**
     * @dev Returns the downcasted uint136 from uint256, reverting on
     * overflow (when the input is greater than largest uint136).
     *
     * Counterpart to Solidity's `uint136` operator.
     *
     * Requirements:
     *
     * - input must fit into 136 bits
     */
    function toUint136(uint256 value) internal pure returns (uint136) {
        if (value > type(uint136).max) {
            revert SafeCastOverflowedUintDowncast(136, value);
        }
        return uint136(value);
    }

    /**
     * @dev Returns the downcasted uint128 from uint256, reverting on
     * overflow (when the input is greater than largest uint128).
     *
     * Counterpart to Solidity's `uint128` operator.
     *
     * Requirements:
     *
     * - input must fit into 128 bits
     */
    function toUint128(uint256 value) internal pure returns (uint128) {
        if (value > type(uint128).max) {
            revert SafeCastOverflowedUintDowncast(128, value);
        }
        return uint128(value);
    }

    /**
     * @dev Returns the downcasted uint120 from uint256, reverting on
     * overflow (when the input is greater than largest uint120).
     *
     * Counterpart to Solidity's `uint120` operator.
     *
     * Requirements:
     *
     * - input must fit into 120 bits
     */
    function toUint120(uint256 value) internal pure returns (uint120) {
        if (value > type(uint120).max) {
            revert SafeCastOverflowedUintDowncast(120, value);
        }
        return uint120(value);
    }

    /**
     * @dev Returns the downcasted uint112 from uint256, reverting on
     * overflow (when the input is greater than largest uint112).
     *
     * Counterpart to Solidity's `uint112` operator.
     *
     * Requirements:
     *
     * - input must fit into 112 bits
     */
    function toUint112(uint256 value) internal pure returns (uint112) {
        if (value > type(uint112).max) {
            revert SafeCastOverflowedUintDowncast(112, value);
        }
        return uint112(value);
    }

    /**
     * @dev Returns the downcasted uint104 from uint256, reverting on
     * overflow (when the input is greater than largest uint104).
     *
     * Counterpart to Solidity's `uint104` operator.
     *
     * Requirements:
     *
     * - input must fit into 104 bits
     */
    function toUint104(uint256 value) internal pure returns (uint104) {
        if (value > type(uint104).max) {
            revert SafeCastOverflowedUintDowncast(104, value);
        }
        return uint104(value);
    }

    /**
     * @dev Returns the downcasted uint96 from uint256, reverting on
     * overflow (when the input is greater than largest uint96).
     *
     * Counterpart to Solidity's `uint96` operator.
     *
     * Requirements:
     *
     * - input must fit into 96 bits
     */
    function toUint96(uint256 value) internal pure returns (uint96) {
        if (value > type(uint96).max) {
            revert SafeCastOverflowedUintDowncast(96, value);
        }
        return uint96(value);
    }

    /**
     * @dev Returns the downcasted uint88 from uint256, reverting on
     * overflow (when the input is greater than largest uint88).
     *
     * Counterpart to Solidity's `uint88` operator.
     *
     * Requirements:
     *
     * - input must fit into 88 bits
     */
    function toUint88(uint256 value) internal pure returns (uint88) {
        if (value > type(uint88).max) {
            revert SafeCastOverflowedUintDowncast(88, value);
        }
        return uint88(value);
    }

    /**
     * @dev Returns the downcasted uint80 from uint256, reverting on
     * overflow (when the input is greater than largest uint80).
     *
     * Counterpart to Solidity's `uint80` operator.
     *
     * Requirements:
     *
     * - input must fit into 80 bits
     */
    function toUint80(uint256 value) internal pure returns (uint80) {
        if (value > type(uint80).max) {
            revert SafeCastOverflowedUintDowncast(80, value);
        }
        return uint80(value);
    }

    /**
     * @dev Returns the downcasted uint72 from uint256, reverting on
     * overflow (when the input is greater than largest uint72).
     *
     * Counterpart to Solidity's `uint72` operator.
     *
     * Requirements:
     *
     * - input must fit into 72 bits
     */
    function toUint72(uint256 value) internal pure returns (uint72) {
        if (value > type(uint72).max) {
            revert SafeCastOverflowedUintDowncast(72, value);
        }
        return uint72(value);
    }

    /**
     * @dev Returns the downcasted uint64 from uint256, reverting on
     * overflow (when the input is greater than largest uint64).
     *
     * Counterpart to Solidity's `uint64` operator.
     *
     * Requirements:
     *
     * - input must fit into 64 bits
     */
    function toUint64(uint256 value) internal pure returns (uint64) {
        if (value > type(uint64).max) {
            revert SafeCastOverflowedUintDowncast(64, value);
        }
        return uint64(value);
    }

    /**
     * @dev Returns the downcasted uint56 from uint256, reverting on
     * overflow (when the input is greater than largest uint56).
     *
     * Counterpart to Solidity's `uint56` operator.
     *
     * Requirements:
     *
     * - input must fit into 56 bits
     */
    function toUint56(uint256 value) internal pure returns (uint56) {
        if (value > type(uint56).max) {
            revert SafeCastOverflowedUintDowncast(56, value);
        }
        return uint56(value);
    }

    /**
     * @dev Returns the downcasted uint48 from uint256, reverting on
     * overflow (when the input is greater than largest uint48).
     *
     * Counterpart to Solidity's `uint48` operator.
     *
     * Requirements:
     *
     * - input must fit into 48 bits
     */
    function toUint48(uint256 value) internal pure returns (uint48) {
        if (value > type(uint48).max) {
            revert SafeCastOverflowedUintDowncast(48, value);
        }
        return uint48(value);
    }

    /**
     * @dev Returns the downcasted uint40 from uint256, reverting on
     * overflow (when the input is greater than largest uint40).
     *
     * Counterpart to Solidity's `uint40` operator.
     *
     * Requirements:
     *
     * - input must fit into 40 bits
     */
    function toUint40(uint256 value) internal pure returns (uint40) {
        if (value > type(uint40).max) {
            revert SafeCastOverflowedUintDowncast(40, value);
        }
        return uint40(value);
    }

    /**
     * @dev Returns the downcasted uint32 from uint256, reverting on
     * overflow (when the input is greater than largest uint32).
     *
     * Counterpart to Solidity's `uint32` operator.
     *
     * Requirements:
     *
     * - input must fit into 32 bits
     */
    function toUint32(uint256 value) internal pure returns (uint32) {
        if (value > type(uint32).max) {
            revert SafeCastOverflowedUintDowncast(32, value);
        }
        return uint32(value);
    }

    /**
     * @dev Returns the downcasted uint24 from uint256, reverting on
     * overflow (when the input is greater than largest uint24).
     *
     * Counterpart to Solidity's `uint24` operator.
     *
     * Requirements:
     *
     * - input must fit into 24 bits
     */
    function toUint24(uint256 value) internal pure returns (uint24) {
        if (value > type(uint24).max) {
            revert SafeCastOverflowedUintDowncast(24, value);
        }
        return uint24(value);
    }

    /**
     * @dev Returns the downcasted uint16 from uint256, reverting on
     * overflow (when the input is greater than largest uint16).
     *
     * Counterpart to Solidity's `uint16` operator.
     *
     * Requirements:
     *
     * - input must fit into 16 bits
     */
    function toUint16(uint256 value) internal pure returns (uint16) {
        if (value > type(uint16).max) {
            revert SafeCastOverflowedUintDowncast(16, value);
        }
        return uint16(value);
    }

    /**
     * @dev Returns the downcasted uint8 from uint256, reverting on
     * overflow (when the input is greater than largest uint8).
     *
     * Counterpart to Solidity's `uint8` operator.
     *
     * Requirements:
     *
     * - input must fit into 8 bits
     */
    function toUint8(uint256 value) internal pure returns (uint8) {
        if (value > type(uint8).max) {
            revert SafeCastOverflowedUintDowncast(8, value);
        }
        return uint8(value);
    }

    /**
     * @dev Converts a signed int256 into an unsigned uint256.
     *
     * Requirements:
     *
     * - input must be greater than or equal to 0.
     */
    function toUint256(int256 value) internal pure returns (uint256) {
        if (value < 0) {
            revert SafeCastOverflowedIntToUint(value);
        }
        return uint256(value);
    }

    /**
     * @dev Returns the downcasted int248 from int256, reverting on
     * overflow (when the input is less than smallest int248 or
     * greater than largest int248).
     *
     * Counterpart to Solidity's `int248` operator.
     *
     * Requirements:
     *
     * - input must fit into 248 bits
     */
    function toInt248(int256 value) internal pure returns (int248 downcasted) {
        downcasted = int248(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(248, value);
        }
    }

    /**
     * @dev Returns the downcasted int240 from int256, reverting on
     * overflow (when the input is less than smallest int240 or
     * greater than largest int240).
     *
     * Counterpart to Solidity's `int240` operator.
     *
     * Requirements:
     *
     * - input must fit into 240 bits
     */
    function toInt240(int256 value) internal pure returns (int240 downcasted) {
        downcasted = int240(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(240, value);
        }
    }

    /**
     * @dev Returns the downcasted int232 from int256, reverting on
     * overflow (when the input is less than smallest int232 or
     * greater than largest int232).
     *
     * Counterpart to Solidity's `int232` operator.
     *
     * Requirements:
     *
     * - input must fit into 232 bits
     */
    function toInt232(int256 value) internal pure returns (int232 downcasted) {
        downcasted = int232(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(232, value);
        }
    }

    /**
     * @dev Returns the downcasted int224 from int256, reverting on
     * overflow (when the input is less than smallest int224 or
     * greater than largest int224).
     *
     * Counterpart to Solidity's `int224` operator.
     *
     * Requirements:
     *
     * - input must fit into 224 bits
     */
    function toInt224(int256 value) internal pure returns (int224 downcasted) {
        downcasted = int224(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(224, value);
        }
    }

    /**
     * @dev Returns the downcasted int216 from int256, reverting on
     * overflow (when the input is less than smallest int216 or
     * greater than largest int216).
     *
     * Counterpart to Solidity's `int216` operator.
     *
     * Requirements:
     *
     * - input must fit into 216 bits
     */
    function toInt216(int256 value) internal pure returns (int216 downcasted) {
        downcasted = int216(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(216, value);
        }
    }

    /**
     * @dev Returns the downcasted int208 from int256, reverting on
     * overflow (when the input is less than smallest int208 or
     * greater than largest int208).
     *
     * Counterpart to Solidity's `int208` operator.
     *
     * Requirements:
     *
     * - input must fit into 208 bits
     */
    function toInt208(int256 value) internal pure returns (int208 downcasted) {
        downcasted = int208(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(208, value);
        }
    }

    /**
     * @dev Returns the downcasted int200 from int256, reverting on
     * overflow (when the input is less than smallest int200 or
     * greater than largest int200).
     *
     * Counterpart to Solidity's `int200` operator.
     *
     * Requirements:
     *
     * - input must fit into 200 bits
     */
    function toInt200(int256 value) internal pure returns (int200 downcasted) {
        downcasted = int200(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(200, value);
        }
    }

    /**
     * @dev Returns the downcasted int192 from int256, reverting on
     * overflow (when the input is less than smallest int192 or
     * greater than largest int192).
     *
     * Counterpart to Solidity's `int192` operator.
     *
     * Requirements:
     *
     * - input must fit into 192 bits
     */
    function toInt192(int256 value) internal pure returns (int192 downcasted) {
        downcasted = int192(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(192, value);
        }
    }

    /**
     * @dev Returns the downcasted int184 from int256, reverting on
     * overflow (when the input is less than smallest int184 or
     * greater than largest int184).
     *
     * Counterpart to Solidity's `int184` operator.
     *
     * Requirements:
     *
     * - input must fit into 184 bits
     */
    function toInt184(int256 value) internal pure returns (int184 downcasted) {
        downcasted = int184(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(184, value);
        }
    }

    /**
     * @dev Returns the downcasted int176 from int256, reverting on
     * overflow (when the input is less than smallest int176 or
     * greater than largest int176).
     *
     * Counterpart to Solidity's `int176` operator.
     *
     * Requirements:
     *
     * - input must fit into 176 bits
     */
    function toInt176(int256 value) internal pure returns (int176 downcasted) {
        downcasted = int176(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(176, value);
        }
    }

    /**
     * @dev Returns the downcasted int168 from int256, reverting on
     * overflow (when the input is less than smallest int168 or
     * greater than largest int168).
     *
     * Counterpart to Solidity's `int168` operator.
     *
     * Requirements:
     *
     * - input must fit into 168 bits
     */
    function toInt168(int256 value) internal pure returns (int168 downcasted) {
        downcasted = int168(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(168, value);
        }
    }

    /**
     * @dev Returns the downcasted int160 from int256, reverting on
     * overflow (when the input is less than smallest int160 or
     * greater than largest int160).
     *
     * Counterpart to Solidity's `int160` operator.
     *
     * Requirements:
     *
     * - input must fit into 160 bits
     */
    function toInt160(int256 value) internal pure returns (int160 downcasted) {
        downcasted = int160(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(160, value);
        }
    }

    /**
     * @dev Returns the downcasted int152 from int256, reverting on
     * overflow (when the input is less than smallest int152 or
     * greater than largest int152).
     *
     * Counterpart to Solidity's `int152` operator.
     *
     * Requirements:
     *
     * - input must fit into 152 bits
     */
    function toInt152(int256 value) internal pure returns (int152 downcasted) {
        downcasted = int152(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(152, value);
        }
    }

    /**
     * @dev Returns the downcasted int144 from int256, reverting on
     * overflow (when the input is less than smallest int144 or
     * greater than largest int144).
     *
     * Counterpart to Solidity's `int144` operator.
     *
     * Requirements:
     *
     * - input must fit into 144 bits
     */
    function toInt144(int256 value) internal pure returns (int144 downcasted) {
        downcasted = int144(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(144, value);
        }
    }

    /**
     * @dev Returns the downcasted int136 from int256, reverting on
     * overflow (when the input is less than smallest int136 or
     * greater than largest int136).
     *
     * Counterpart to Solidity's `int136` operator.
     *
     * Requirements:
     *
     * - input must fit into 136 bits
     */
    function toInt136(int256 value) internal pure returns (int136 downcasted) {
        downcasted = int136(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(136, value);
        }
    }

    /**
     * @dev Returns the downcasted int128 from int256, reverting on
     * overflow (when the input is less than smallest int128 or
     * greater than largest int128).
     *
     * Counterpart to Solidity's `int128` operator.
     *
     * Requirements:
     *
     * - input must fit into 128 bits
     */
    function toInt128(int256 value) internal pure returns (int128 downcasted) {
        downcasted = int128(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(128, value);
        }
    }

    /**
     * @dev Returns the downcasted int120 from int256, reverting on
     * overflow (when the input is less than smallest int120 or
     * greater than largest int120).
     *
     * Counterpart to Solidity's `int120` operator.
     *
     * Requirements:
     *
     * - input must fit into 120 bits
     */
    function toInt120(int256 value) internal pure returns (int120 downcasted) {
        downcasted = int120(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(120, value);
        }
    }

    /**
     * @dev Returns the downcasted int112 from int256, reverting on
     * overflow (when the input is less than smallest int112 or
     * greater than largest int112).
     *
     * Counterpart to Solidity's `int112` operator.
     *
     * Requirements:
     *
     * - input must fit into 112 bits
     */
    function toInt112(int256 value) internal pure returns (int112 downcasted) {
        downcasted = int112(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(112, value);
        }
    }

    /**
     * @dev Returns the downcasted int104 from int256, reverting on
     * overflow (when the input is less than smallest int104 or
     * greater than largest int104).
     *
     * Counterpart to Solidity's `int104` operator.
     *
     * Requirements:
     *
     * - input must fit into 104 bits
     */
    function toInt104(int256 value) internal pure returns (int104 downcasted) {
        downcasted = int104(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(104, value);
        }
    }

    /**
     * @dev Returns the downcasted int96 from int256, reverting on
     * overflow (when the input is less than smallest int96 or
     * greater than largest int96).
     *
     * Counterpart to Solidity's `int96` operator.
     *
     * Requirements:
     *
     * - input must fit into 96 bits
     */
    function toInt96(int256 value) internal pure returns (int96 downcasted) {
        downcasted = int96(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(96, value);
        }
    }

    /**
     * @dev Returns the downcasted int88 from int256, reverting on
     * overflow (when the input is less than smallest int88 or
     * greater than largest int88).
     *
     * Counterpart to Solidity's `int88` operator.
     *
     * Requirements:
     *
     * - input must fit into 88 bits
     */
    function toInt88(int256 value) internal pure returns (int88 downcasted) {
        downcasted = int88(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(88, value);
        }
    }

    /**
     * @dev Returns the downcasted int80 from int256, reverting on
     * overflow (when the input is less than smallest int80 or
     * greater than largest int80).
     *
     * Counterpart to Solidity's `int80` operator.
     *
     * Requirements:
     *
     * - input must fit into 80 bits
     */
    function toInt80(int256 value) internal pure returns (int80 downcasted) {
        downcasted = int80(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(80, value);
        }
    }

    /**
     * @dev Returns the downcasted int72 from int256, reverting on
     * overflow (when the input is less than smallest int72 or
     * greater than largest int72).
     *
     * Counterpart to Solidity's `int72` operator.
     *
     * Requirements:
     *
     * - input must fit into 72 bits
     */
    function toInt72(int256 value) internal pure returns (int72 downcasted) {
        downcasted = int72(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(72, value);
        }
    }

    /**
     * @dev Returns the downcasted int64 from int256, reverting on
     * overflow (when the input is less than smallest int64 or
     * greater than largest int64).
     *
     * Counterpart to Solidity's `int64` operator.
     *
     * Requirements:
     *
     * - input must fit into 64 bits
     */
    function toInt64(int256 value) internal pure returns (int64 downcasted) {
        downcasted = int64(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(64, value);
        }
    }

    /**
     * @dev Returns the downcasted int56 from int256, reverting on
     * overflow (when the input is less than smallest int56 or
     * greater than largest int56).
     *
     * Counterpart to Solidity's `int56` operator.
     *
     * Requirements:
     *
     * - input must fit into 56 bits
     */
    function toInt56(int256 value) internal pure returns (int56 downcasted) {
        downcasted = int56(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(56, value);
        }
    }

    /**
     * @dev Returns the downcasted int48 from int256, reverting on
     * overflow (when the input is less than smallest int48 or
     * greater than largest int48).
     *
     * Counterpart to Solidity's `int48` operator.
     *
     * Requirements:
     *
     * - input must fit into 48 bits
     */
    function toInt48(int256 value) internal pure returns (int48 downcasted) {
        downcasted = int48(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(48, value);
        }
    }

    /**
     * @dev Returns the downcasted int40 from int256, reverting on
     * overflow (when the input is less than smallest int40 or
     * greater than largest int40).
     *
     * Counterpart to Solidity's `int40` operator.
     *
     * Requirements:
     *
     * - input must fit into 40 bits
     */
    function toInt40(int256 value) internal pure returns (int40 downcasted) {
        downcasted = int40(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(40, value);
        }
    }

    /**
     * @dev Returns the downcasted int32 from int256, reverting on
     * overflow (when the input is less than smallest int32 or
     * greater than largest int32).
     *
     * Counterpart to Solidity's `int32` operator.
     *
     * Requirements:
     *
     * - input must fit into 32 bits
     */
    function toInt32(int256 value) internal pure returns (int32 downcasted) {
        downcasted = int32(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(32, value);
        }
    }

    /**
     * @dev Returns the downcasted int24 from int256, reverting on
     * overflow (when the input is less than smallest int24 or
     * greater than largest int24).
     *
     * Counterpart to Solidity's `int24` operator.
     *
     * Requirements:
     *
     * - input must fit into 24 bits
     */
    function toInt24(int256 value) internal pure returns (int24 downcasted) {
        downcasted = int24(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(24, value);
        }
    }

    /**
     * @dev Returns the downcasted int16 from int256, reverting on
     * overflow (when the input is less than smallest int16 or
     * greater than largest int16).
     *
     * Counterpart to Solidity's `int16` operator.
     *
     * Requirements:
     *
     * - input must fit into 16 bits
     */
    function toInt16(int256 value) internal pure returns (int16 downcasted) {
        downcasted = int16(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(16, value);
        }
    }

    /**
     * @dev Returns the downcasted int8 from int256, reverting on
     * overflow (when the input is less than smallest int8 or
     * greater than largest int8).
     *
     * Counterpart to Solidity's `int8` operator.
     *
     * Requirements:
     *
     * - input must fit into 8 bits
     */
    function toInt8(int256 value) internal pure returns (int8 downcasted) {
        downcasted = int8(value);
        if (downcasted != value) {
            revert SafeCastOverflowedIntDowncast(8, value);
        }
    }

    /**
     * @dev Converts an unsigned uint256 into a signed int256.
     *
     * Requirements:
     *
     * - input must be less than or equal to maxInt256.
     */
    function toInt256(uint256 value) internal pure returns (int256) {
        // Note: Unsafe cast below is okay because `type(int256).max` is guaranteed to be positive
        if (value > uint256(type(int256).max)) {
            revert SafeCastOverflowedUintToInt(value);
        }
        return int256(value);
    }
}
FixedLstAddressAlias.sol 24 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

/// @title FixedLstAddressAlias
/// @author [ScopeLift](https://scopelift.co)
/// @notice Library code for calculating the alias of an address.
library FixedLstAddressAlias {
  /// @notice The constant value that is added to an existing address to calculate its offset before it is hashed.
  uint160 public constant ALIAS_OFFSET_SALT = 0x1000010100000110011011010010;

  /// @notice Calculates the alias of a given address by adding a fixed constant and hashing it.
  /// @param _account The address for which the alias will be calculated.
  /// @return _alias The calculated alias of the address provided.
  /// @dev Adding a fixed constant before hashing acts as a kind of "salt", ensuring the address produced is not simply
  /// the keccak256 of the address in question, as this might conflict in some unexpected way with some other
  /// application. At the same time, adding the "salt" to the address, rather than concatenating it, avoids adding
  /// extra bytes to the keccak operation, which would incur additional gas costs.
  function fixedAlias(address _account) internal pure returns (address _alias) {
    /// Overflow is desirable as addresses that are close to 0xFF...FF will overflow to a lower alias address.
    unchecked {
      _alias = address(uint160(uint256(keccak256(abi.encode(uint160(_account) + ALIAS_OFFSET_SALT)))));
    }
  }
}
IERC20Metadata.sol 6 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (interfaces/IERC20Metadata.sol)

pragma solidity ^0.8.20;

import {IERC20Metadata} from "../token/ERC20/extensions/IERC20Metadata.sol";
WithdrawGate.sol 210 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {EIP712} from "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
import {SignatureChecker} from "@openzeppelin/contracts/utils/cryptography/SignatureChecker.sol";
import {Multicall} from "@openzeppelin/contracts/utils/Multicall.sol";

/// @title WithdrawGate
/// @author ScopeLift
/// @notice A contract to enforce a withdrawal delay for users exiting the LST.
contract WithdrawGate is Ownable, Multicall, EIP712 {
  using SafeERC20 for IERC20;

  /// @notice Thrown when an invalid LST address is provided.
  error WithdrawGate__InvalidLSTAddress();

  /// @notice Thrown when an invalid delay is set.
  error WithdrawGate__InvalidDelay();

  /// @notice Thrown when the caller is not the LST.
  error WithdrawGate__CallerNotLST();

  /// @notice Thrown when the withdrawal is not found.
  error WithdrawGate__WithdrawalNotFound();

  /// @notice Thrown when the withdrawal is not yet eligible.
  error WithdrawGate__WithdrawalNotEligible();

  /// @notice Thrown when the withdrawal has already been completed.
  error WithdrawGate__WithdrawalAlreadyCompleted();

  /// @notice Thrown when the caller is not the designated receiver.
  error WithdrawGate__CallerNotReceiver();

  /// @notice Thrown when the signature is invalid.
  error WithdrawGate__InvalidSignature();

  /// @notice Thrown when the deadline has expired.
  error WithdrawGate__ExpiredDeadline();

  /// @notice The address of the LST contract.
  address public immutable LST;

  /// @notice The address of the token that can be withdrawn, assumed to revert on failed transfers.
  address public immutable WITHDRAWAL_TOKEN;

  /// @notice The maximum allowed delay for withdrawals.
  uint256 public constant DELAY_MAX = 30 days;

  /// @notice The current delay period for withdrawals.
  uint256 public delay;

  /// @notice The EIP-712 typehash for the CompleteWithdrawal struct.
  bytes32 public constant COMPLETE_WITHDRAWAL_TYPEHASH =
    keccak256("CompleteWithdrawal(uint256 identifier,uint256 deadline)");

  /// @notice A struct to store withdrawal information.
  struct Withdrawal {
    address receiver;
    uint96 amount;
    uint256 eligibleTimestamp;
  }

  /// @notice Mapping from withdrawal identifier to Withdrawal struct.
  mapping(uint256 withdrawId => Withdrawal withdrawal) public withdrawals;

  /// @notice Counter for generating unique withdrawal identifiers.
  uint256 internal nextWithdrawalId;

  /// @notice Emitted when the delay period is set.
  event DelaySet(uint256 oldDelay, uint256 newDelay);

  /// @notice Emitted when a withdrawal is initiated.
  event WithdrawalInitiated(uint256 amount, address receiver, uint256 eligibleTimestamp, uint256 identifier);

  /// @notice Emitted when a withdrawal is completed.
  event WithdrawalCompleted(uint256 identifier, address receiver, uint256 amount);

  /// @notice Initializes the WithdrawGate contract.
  /// @param _owner The address that will own this contract.
  /// @param _lst The address of the LST contract.
  /// @param _initialDelay The initial withdrawal delay period.
  constructor(address _owner, address _lst, address _withdrawalToken, uint256 _initialDelay)
    Ownable(_owner)
    EIP712("WithdrawGate", "1")
  {
    if (_lst == address(0)) {
      revert WithdrawGate__InvalidLSTAddress();
    }
    if (_initialDelay > DELAY_MAX) {
      revert WithdrawGate__InvalidDelay();
    }

    LST = _lst;
    WITHDRAWAL_TOKEN = _withdrawalToken;
    _setDelay(_initialDelay);
    nextWithdrawalId = 1;
  }

  /// @notice Sets a new delay period for withdrawals.
  /// @param _newDelay The new delay period to set.
  /// @dev Only the contract owner can call this function.
  /// @dev Reverts if the new delay exceeds DELAY_MAX.
  function setDelay(uint256 _newDelay) external virtual {
    _checkOwner();
    _setDelay(_newDelay);
  }

  /// @notice Internal function to set the delay period.
  /// @param _newDelay The new delay period to set.
  /// @dev Reverts if the new delay exceeds DELAY_MAX.
  function _setDelay(uint256 _newDelay) internal virtual {
    if (_newDelay > DELAY_MAX) {
      revert WithdrawGate__InvalidDelay();
    }

    uint256 _oldDelay = delay;

    emit DelaySet(_oldDelay, _newDelay);
    delay = _newDelay;
  }

  /// @notice Initiates a withdrawal for a user.
  /// @param _amount The amount of tokens to withdraw.
  /// @param _receiver The address that will receive the tokens.
  /// @return _identifier The unique identifier for this withdrawal.
  /// @dev Can only be called by the LST contract.
  /// @dev Assumes the WITHDRAW_TOKENs have already been transferred to this contract.
  function initiateWithdrawal(uint96 _amount, address _receiver) external virtual returns (uint256 _identifier) {
    if (msg.sender != LST) {
      revert WithdrawGate__CallerNotLST();
    }
    if (_receiver == address(0)) {
      revert WithdrawGate__CallerNotReceiver();
    }

    _identifier = nextWithdrawalId++;
    uint256 _eligibleTimestamp = block.timestamp + delay;

    withdrawals[_identifier] = Withdrawal({receiver: _receiver, amount: _amount, eligibleTimestamp: _eligibleTimestamp});

    emit WithdrawalInitiated(_amount, _receiver, _eligibleTimestamp, _identifier);
  }

  /// @notice Completes a previously initiated withdrawal.
  /// @param _identifier The unique identifier of the withdrawal to complete.
  function completeWithdrawal(uint256 _identifier) external virtual {
    if (nextWithdrawalId <= _identifier) {
      revert WithdrawGate__WithdrawalNotFound();
    }

    Withdrawal memory _withdrawal = withdrawals[_identifier];

    if (msg.sender != _withdrawal.receiver) {
      revert WithdrawGate__CallerNotReceiver();
    }

    _completeWithdrawal(_identifier, _withdrawal);
  }

  /// @notice Completes a previously initiated withdrawal on behalf of the receiver.
  /// @param _deadline The deadline by which the withdrawal must be completed.
  /// @param _identifier The unique identifier of the withdrawal to complete.
  /// @param _signature The EIP-712 or EIP-1271 signature authorizing the withdrawal.
  function completeWithdrawalOnBehalf(uint256 _identifier, uint256 _deadline, bytes memory _signature) external virtual {
    if (nextWithdrawalId <= _identifier) {
      revert WithdrawGate__WithdrawalNotFound();
    }

    Withdrawal memory _withdrawal = withdrawals[_identifier];
    if (block.timestamp > _deadline) {
      revert WithdrawGate__ExpiredDeadline();
    }

    bytes32 _structHash = keccak256(abi.encode(COMPLETE_WITHDRAWAL_TYPEHASH, _identifier, _deadline));
    bool _isValid =
      SignatureChecker.isValidSignatureNow(_withdrawal.receiver, _hashTypedDataV4(_structHash), _signature);
    if (!_isValid) {
      revert WithdrawGate__InvalidSignature();
    }

    _completeWithdrawal(_identifier, _withdrawal);
  }

  /// @notice Internal function to complete a withdrawal.
  /// @param _identifier The unique identifier of the withdrawal to complete.
  /// @param _withdrawal The memory reference to the Withdrawal struct.
  function _completeWithdrawal(uint256 _identifier, Withdrawal memory _withdrawal) internal virtual {
    if (block.timestamp < _withdrawal.eligibleTimestamp || _withdrawal.eligibleTimestamp == 0) {
      revert WithdrawGate__WithdrawalNotEligible();
    }

    // Clear the withdrawal by zeroing the eligibleTimestamp
    withdrawals[_identifier].eligibleTimestamp = 0;

    // This transfer assumes WITHDRAWAL_TOKEN will revert if the transfer fails.
    IERC20(WITHDRAWAL_TOKEN).safeTransfer(_withdrawal.receiver, _withdrawal.amount);

    emit WithdrawalCompleted(_identifier, _withdrawal.receiver, _withdrawal.amount);
  }

  /// @notice Gets the next withdrawal identifier.
  /// @return The next withdrawal identifier.
  function getNextWithdrawalId() external view virtual returns (uint256) {
    return nextWithdrawalId;
  }
}
MessageHashUtils.sol 86 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/cryptography/MessageHashUtils.sol)

pragma solidity ^0.8.20;

import {Strings} from "../Strings.sol";

/**
 * @dev Signature message hash utilities for producing digests to be consumed by {ECDSA} recovery or signing.
 *
 * The library provides methods for generating a hash of a message that conforms to the
 * https://eips.ethereum.org/EIPS/eip-191[EIP 191] and https://eips.ethereum.org/EIPS/eip-712[EIP 712]
 * specifications.
 */
library MessageHashUtils {
    /**
     * @dev Returns the keccak256 digest of an EIP-191 signed data with version
     * `0x45` (`personal_sign` messages).
     *
     * The digest is calculated by prefixing a bytes32 `messageHash` with
     * `"\x19Ethereum Signed Message:\n32"` and hashing the result. It corresponds with the
     * hash signed when using the https://eth.wiki/json-rpc/API#eth_sign[`eth_sign`] JSON-RPC method.
     *
     * NOTE: The `messageHash` parameter is intended to be the result of hashing a raw message with
     * keccak256, although any bytes32 value can be safely used because the final digest will
     * be re-hashed.
     *
     * See {ECDSA-recover}.
     */
    function toEthSignedMessageHash(bytes32 messageHash) internal pure returns (bytes32 digest) {
        /// @solidity memory-safe-assembly
        assembly {
            mstore(0x00, "\x19Ethereum Signed Message:\n32") // 32 is the bytes-length of messageHash
            mstore(0x1c, messageHash) // 0x1c (28) is the length of the prefix
            digest := keccak256(0x00, 0x3c) // 0x3c is the length of the prefix (0x1c) + messageHash (0x20)
        }
    }

    /**
     * @dev Returns the keccak256 digest of an EIP-191 signed data with version
     * `0x45` (`personal_sign` messages).
     *
     * The digest is calculated by prefixing an arbitrary `message` with
     * `"\x19Ethereum Signed Message:\n" + len(message)` and hashing the result. It corresponds with the
     * hash signed when using the https://eth.wiki/json-rpc/API#eth_sign[`eth_sign`] JSON-RPC method.
     *
     * See {ECDSA-recover}.
     */
    function toEthSignedMessageHash(bytes memory message) internal pure returns (bytes32) {
        return
            keccak256(bytes.concat("\x19Ethereum Signed Message:\n", bytes(Strings.toString(message.length)), message));
    }

    /**
     * @dev Returns the keccak256 digest of an EIP-191 signed data with version
     * `0x00` (data with intended validator).
     *
     * The digest is calculated by prefixing an arbitrary `data` with `"\x19\x00"` and the intended
     * `validator` address. Then hashing the result.
     *
     * See {ECDSA-recover}.
     */
    function toDataWithIntendedValidatorHash(address validator, bytes memory data) internal pure returns (bytes32) {
        return keccak256(abi.encodePacked(hex"19_00", validator, data));
    }

    /**
     * @dev Returns the keccak256 digest of an EIP-712 typed data (EIP-191 version `0x01`).
     *
     * The digest is calculated from a `domainSeparator` and a `structHash`, by prefixing them with
     * `\x19\x01` and hashing the result. It corresponds to the hash signed by the
     * https://eips.ethereum.org/EIPS/eip-712[`eth_signTypedData`] JSON-RPC method as part of EIP-712.
     *
     * See {ECDSA-recover}.
     */
    function toTypedDataHash(bytes32 domainSeparator, bytes32 structHash) internal pure returns (bytes32 digest) {
        /// @solidity memory-safe-assembly
        assembly {
            let ptr := mload(0x40)
            mstore(ptr, hex"19_01")
            mstore(add(ptr, 0x02), domainSeparator)
            mstore(add(ptr, 0x22), structHash)
            digest := keccak256(ptr, 0x42)
        }
    }
}
ShortStrings.sol 123 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/ShortStrings.sol)

pragma solidity ^0.8.20;

import {StorageSlot} from "./StorageSlot.sol";

// | string  | 0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA   |
// | length  | 0x                                                              BB |
type ShortString is bytes32;

/**
 * @dev This library provides functions to convert short memory strings
 * into a `ShortString` type that can be used as an immutable variable.
 *
 * Strings of arbitrary length can be optimized using this library if
 * they are short enough (up to 31 bytes) by packing them with their
 * length (1 byte) in a single EVM word (32 bytes). Additionally, a
 * fallback mechanism can be used for every other case.
 *
 * Usage example:
 *
 * ```solidity
 * contract Named {
 *     using ShortStrings for *;
 *
 *     ShortString private immutable _name;
 *     string private _nameFallback;
 *
 *     constructor(string memory contractName) {
 *         _name = contractName.toShortStringWithFallback(_nameFallback);
 *     }
 *
 *     function name() external view returns (string memory) {
 *         return _name.toStringWithFallback(_nameFallback);
 *     }
 * }
 * ```
 */
library ShortStrings {
    // Used as an identifier for strings longer than 31 bytes.
    bytes32 private constant FALLBACK_SENTINEL = 0x00000000000000000000000000000000000000000000000000000000000000FF;

    error StringTooLong(string str);
    error InvalidShortString();

    /**
     * @dev Encode a string of at most 31 chars into a `ShortString`.
     *
     * This will trigger a `StringTooLong` error is the input string is too long.
     */
    function toShortString(string memory str) internal pure returns (ShortString) {
        bytes memory bstr = bytes(str);
        if (bstr.length > 31) {
            revert StringTooLong(str);
        }
        return ShortString.wrap(bytes32(uint256(bytes32(bstr)) | bstr.length));
    }

    /**
     * @dev Decode a `ShortString` back to a "normal" string.
     */
    function toString(ShortString sstr) internal pure returns (string memory) {
        uint256 len = byteLength(sstr);
        // using `new string(len)` would work locally but is not memory safe.
        string memory str = new string(32);
        /// @solidity memory-safe-assembly
        assembly {
            mstore(str, len)
            mstore(add(str, 0x20), sstr)
        }
        return str;
    }

    /**
     * @dev Return the length of a `ShortString`.
     */
    function byteLength(ShortString sstr) internal pure returns (uint256) {
        uint256 result = uint256(ShortString.unwrap(sstr)) & 0xFF;
        if (result > 31) {
            revert InvalidShortString();
        }
        return result;
    }

    /**
     * @dev Encode a string into a `ShortString`, or write it to storage if it is too long.
     */
    function toShortStringWithFallback(string memory value, string storage store) internal returns (ShortString) {
        if (bytes(value).length < 32) {
            return toShortString(value);
        } else {
            StorageSlot.getStringSlot(store).value = value;
            return ShortString.wrap(FALLBACK_SENTINEL);
        }
    }

    /**
     * @dev Decode a string that was encoded to `ShortString` or written to storage using {setWithFallback}.
     */
    function toStringWithFallback(ShortString value, string storage store) internal pure returns (string memory) {
        if (ShortString.unwrap(value) != FALLBACK_SENTINEL) {
            return toString(value);
        } else {
            return store;
        }
    }

    /**
     * @dev Return the length of a string that was encoded to `ShortString` or written to storage using
     * {setWithFallback}.
     *
     * WARNING: This will return the "byte length" of the string. This may not reflect the actual length in terms of
     * actual characters as the UTF-8 encoding of a single character can span over multiple bytes.
     */
    function byteLengthWithFallback(ShortString value, string storage store) internal view returns (uint256) {
        if (ShortString.unwrap(value) != FALLBACK_SENTINEL) {
            return byteLength(value);
        } else {
            return bytes(store).length;
        }
    }
}
IERC5267.sol 28 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (interfaces/IERC5267.sol)

pragma solidity ^0.8.20;

interface IERC5267 {
    /**
     * @dev MAY be emitted to signal that the domain could have changed.
     */
    event EIP712DomainChanged();

    /**
     * @dev returns the fields and values that describe the domain separator used by this contract for EIP-712
     * signature.
     */
    function eip712Domain()
        external
        view
        returns (
            bytes1 fields,
            string memory name,
            string memory version,
            uint256 chainId,
            address verifyingContract,
            bytes32 salt,
            uint256[] memory extensions
        );
}
SignatureChecker.sol 48 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/cryptography/SignatureChecker.sol)

pragma solidity ^0.8.20;

import {ECDSA} from "./ECDSA.sol";
import {IERC1271} from "../../interfaces/IERC1271.sol";

/**
 * @dev Signature verification helper that can be used instead of `ECDSA.recover` to seamlessly support both ECDSA
 * signatures from externally owned accounts (EOAs) as well as ERC1271 signatures from smart contract wallets like
 * Argent and Safe Wallet (previously Gnosis Safe).
 */
library SignatureChecker {
    /**
     * @dev Checks if a signature is valid for a given signer and data hash. If the signer is a smart contract, the
     * signature is validated against that smart contract using ERC1271, otherwise it's validated using `ECDSA.recover`.
     *
     * NOTE: Unlike ECDSA signatures, contract signatures are revocable, and the outcome of this function can thus
     * change through time. It could return true at block N and false at block N+1 (or the opposite).
     */
    function isValidSignatureNow(address signer, bytes32 hash, bytes memory signature) internal view returns (bool) {
        (address recovered, ECDSA.RecoverError error, ) = ECDSA.tryRecover(hash, signature);
        return
            (error == ECDSA.RecoverError.NoError && recovered == signer) ||
            isValidERC1271SignatureNow(signer, hash, signature);
    }

    /**
     * @dev Checks if a signature is valid for a given signer and data hash. The signature is validated
     * against the signer smart contract using ERC1271.
     *
     * NOTE: Unlike ECDSA signatures, contract signatures are revocable, and the outcome of this function can thus
     * change through time. It could return true at block N and false at block N+1 (or the opposite).
     */
    function isValidERC1271SignatureNow(
        address signer,
        bytes32 hash,
        bytes memory signature
    ) internal view returns (bool) {
        (bool success, bytes memory result) = signer.staticcall(
            abi.encodeCall(IERC1271.isValidSignature, (hash, signature))
        );
        return (success &&
            result.length >= 32 &&
            abi.decode(result, (bytes32)) == bytes32(IERC1271.isValidSignature.selector));
    }
}
Strings.sol 94 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/Strings.sol)

pragma solidity ^0.8.20;

import {Math} from "./math/Math.sol";
import {SignedMath} from "./math/SignedMath.sol";

/**
 * @dev String operations.
 */
library Strings {
    bytes16 private constant HEX_DIGITS = "0123456789abcdef";
    uint8 private constant ADDRESS_LENGTH = 20;

    /**
     * @dev The `value` string doesn't fit in the specified `length`.
     */
    error StringsInsufficientHexLength(uint256 value, uint256 length);

    /**
     * @dev Converts a `uint256` to its ASCII `string` decimal representation.
     */
    function toString(uint256 value) internal pure returns (string memory) {
        unchecked {
            uint256 length = Math.log10(value) + 1;
            string memory buffer = new string(length);
            uint256 ptr;
            /// @solidity memory-safe-assembly
            assembly {
                ptr := add(buffer, add(32, length))
            }
            while (true) {
                ptr--;
                /// @solidity memory-safe-assembly
                assembly {
                    mstore8(ptr, byte(mod(value, 10), HEX_DIGITS))
                }
                value /= 10;
                if (value == 0) break;
            }
            return buffer;
        }
    }

    /**
     * @dev Converts a `int256` to its ASCII `string` decimal representation.
     */
    function toStringSigned(int256 value) internal pure returns (string memory) {
        return string.concat(value < 0 ? "-" : "", toString(SignedMath.abs(value)));
    }

    /**
     * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
     */
    function toHexString(uint256 value) internal pure returns (string memory) {
        unchecked {
            return toHexString(value, Math.log256(value) + 1);
        }
    }

    /**
     * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
     */
    function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
        uint256 localValue = value;
        bytes memory buffer = new bytes(2 * length + 2);
        buffer[0] = "0";
        buffer[1] = "x";
        for (uint256 i = 2 * length + 1; i > 1; --i) {
            buffer[i] = HEX_DIGITS[localValue & 0xf];
            localValue >>= 4;
        }
        if (localValue != 0) {
            revert StringsInsufficientHexLength(value, length);
        }
        return string(buffer);
    }

    /**
     * @dev Converts an `address` with fixed length of 20 bytes to its not checksummed ASCII `string` hexadecimal
     * representation.
     */
    function toHexString(address addr) internal pure returns (string memory) {
        return toHexString(uint256(uint160(addr)), ADDRESS_LENGTH);
    }

    /**
     * @dev Returns true if the two strings are equal.
     */
    function equal(string memory a, string memory b) internal pure returns (bool) {
        return bytes(a).length == bytes(b).length && keccak256(bytes(a)) == keccak256(bytes(b));
    }
}
StorageSlot.sol 135 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/StorageSlot.sol)
// This file was procedurally generated from scripts/generate/templates/StorageSlot.js.

pragma solidity ^0.8.20;

/**
 * @dev Library for reading and writing primitive types to specific storage slots.
 *
 * Storage slots are often used to avoid storage conflict when dealing with upgradeable contracts.
 * This library helps with reading and writing to such slots without the need for inline assembly.
 *
 * The functions in this library return Slot structs that contain a `value` member that can be used to read or write.
 *
 * Example usage to set ERC1967 implementation slot:
 * ```solidity
 * contract ERC1967 {
 *     bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
 *
 *     function _getImplementation() internal view returns (address) {
 *         return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
 *     }
 *
 *     function _setImplementation(address newImplementation) internal {
 *         require(newImplementation.code.length > 0);
 *         StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
 *     }
 * }
 * ```
 */
library StorageSlot {
    struct AddressSlot {
        address value;
    }

    struct BooleanSlot {
        bool value;
    }

    struct Bytes32Slot {
        bytes32 value;
    }

    struct Uint256Slot {
        uint256 value;
    }

    struct StringSlot {
        string value;
    }

    struct BytesSlot {
        bytes value;
    }

    /**
     * @dev Returns an `AddressSlot` with member `value` located at `slot`.
     */
    function getAddressSlot(bytes32 slot) internal pure returns (AddressSlot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := slot
        }
    }

    /**
     * @dev Returns an `BooleanSlot` with member `value` located at `slot`.
     */
    function getBooleanSlot(bytes32 slot) internal pure returns (BooleanSlot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := slot
        }
    }

    /**
     * @dev Returns an `Bytes32Slot` with member `value` located at `slot`.
     */
    function getBytes32Slot(bytes32 slot) internal pure returns (Bytes32Slot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := slot
        }
    }

    /**
     * @dev Returns an `Uint256Slot` with member `value` located at `slot`.
     */
    function getUint256Slot(bytes32 slot) internal pure returns (Uint256Slot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := slot
        }
    }

    /**
     * @dev Returns an `StringSlot` with member `value` located at `slot`.
     */
    function getStringSlot(bytes32 slot) internal pure returns (StringSlot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := slot
        }
    }

    /**
     * @dev Returns an `StringSlot` representation of the string storage pointer `store`.
     */
    function getStringSlot(string storage store) internal pure returns (StringSlot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := store.slot
        }
    }

    /**
     * @dev Returns an `BytesSlot` with member `value` located at `slot`.
     */
    function getBytesSlot(bytes32 slot) internal pure returns (BytesSlot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := slot
        }
    }

    /**
     * @dev Returns an `BytesSlot` representation of the bytes storage pointer `store`.
     */
    function getBytesSlot(bytes storage store) internal pure returns (BytesSlot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := store.slot
        }
    }
}
IERC1271.sol 17 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (interfaces/IERC1271.sol)

pragma solidity ^0.8.20;

/**
 * @dev Interface of the ERC1271 standard signature validation method for
 * contracts as defined in https://eips.ethereum.org/EIPS/eip-1271[ERC-1271].
 */
interface IERC1271 {
    /**
     * @dev Should return whether the signature provided is valid for the provided data
     * @param hash      Hash of the data to be signed
     * @param signature Signature byte array associated with _data
     */
    function isValidSignature(bytes32 hash, bytes memory signature) external view returns (bytes4 magicValue);
}
Math.sol 415 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/math/Math.sol)

pragma solidity ^0.8.20;

/**
 * @dev Standard math utilities missing in the Solidity language.
 */
library Math {
    /**
     * @dev Muldiv operation overflow.
     */
    error MathOverflowedMulDiv();

    enum Rounding {
        Floor, // Toward negative infinity
        Ceil, // Toward positive infinity
        Trunc, // Toward zero
        Expand // Away from zero
    }

    /**
     * @dev Returns the addition of two unsigned integers, with an overflow flag.
     */
    function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            uint256 c = a + b;
            if (c < a) return (false, 0);
            return (true, c);
        }
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, with an overflow flag.
     */
    function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            if (b > a) return (false, 0);
            return (true, a - b);
        }
    }

    /**
     * @dev Returns the multiplication of two unsigned integers, with an overflow flag.
     */
    function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
            // benefit is lost if 'b' is also tested.
            // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
            if (a == 0) return (true, 0);
            uint256 c = a * b;
            if (c / a != b) return (false, 0);
            return (true, c);
        }
    }

    /**
     * @dev Returns the division of two unsigned integers, with a division by zero flag.
     */
    function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            if (b == 0) return (false, 0);
            return (true, a / b);
        }
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
     */
    function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            if (b == 0) return (false, 0);
            return (true, a % b);
        }
    }

    /**
     * @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 towards infinity instead
     * of rounding towards zero.
     */
    function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
        if (b == 0) {
            // Guarantee the same behavior as in a regular Solidity division.
            return a / b;
        }

        // (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 = x * y; // Least significant 256 bits of the product
            uint256 prod1; // Most significant 256 bits of the product
            assembly {
                let mm := mulmod(x, y, not(0))
                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.
            if (denominator <= prod1) {
                revert MathOverflowedMulDiv();
            }

            ///////////////////////////////////////////////
            // 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.

            uint256 twos = denominator & (0 - denominator);
            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 (unsignedRoundsUp(rounding) && 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
     * towards zero.
     *
     * 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 + (unsignedRoundsUp(rounding) && result * result < a ? 1 : 0);
        }
    }

    /**
     * @dev Return the log in base 2 of a positive value rounded towards zero.
     * 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 + (unsignedRoundsUp(rounding) && 1 << result < value ? 1 : 0);
        }
    }

    /**
     * @dev Return the log in base 10 of a positive value rounded towards zero.
     * 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 + (unsignedRoundsUp(rounding) && 10 ** result < value ? 1 : 0);
        }
    }

    /**
     * @dev Return the log in base 256 of a positive value rounded towards zero.
     * 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 + (unsignedRoundsUp(rounding) && 1 << (result << 3) < value ? 1 : 0);
        }
    }

    /**
     * @dev Returns whether a provided rounding mode is considered rounding up for unsigned integers.
     */
    function unsignedRoundsUp(Rounding rounding) internal pure returns (bool) {
        return uint8(rounding) % 2 == 1;
    }
}
SignedMath.sol 43 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/math/SignedMath.sol)

pragma solidity ^0.8.20;

/**
 * @dev Standard signed math utilities missing in the Solidity language.
 */
library SignedMath {
    /**
     * @dev Returns the largest of two signed numbers.
     */
    function max(int256 a, int256 b) internal pure returns (int256) {
        return a > b ? a : b;
    }

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

    /**
     * @dev Returns the average of two signed numbers without overflow.
     * The result is rounded towards zero.
     */
    function average(int256 a, int256 b) internal pure returns (int256) {
        // Formula from the book "Hacker's Delight"
        int256 x = (a & b) + ((a ^ b) >> 1);
        return x + (int256(uint256(x) >> 255) & (a ^ b));
    }

    /**
     * @dev Returns the absolute unsigned value of a signed value.
     */
    function abs(int256 n) internal pure returns (uint256) {
        unchecked {
            // must be unchecked in order to support `n = type(int256).min`
            return uint256(n >= 0 ? n : -n);
        }
    }
}

Read Contract

DOMAIN_SEPARATOR 0x3644e515 → bytes32
FIXED_LST 0x52000ec7 → address
LST 0x55d5c37b → address
STAKE_TOKEN 0x1c39b672 → address
allowance 0xdd62ed3e → uint256
balanceOf 0x70a08231 → uint256
decimals 0x313ce567 → uint8
delegatee 0x1e31d053 → address
depositId 0x9852099c → uint256
eip712Domain 0x84b0196e → bytes1, string, string, uint256, address, bytes32, uint256[]
name 0x06fdde03 → string
nonces 0x7ecebe00 → uint256
owner 0x8da5cb5b → address
previewUnwrapToFixed 0xa2fddee8 → uint256
previewUnwrapToRebasing 0xea8b63ed → uint256
previewWrapFixed 0x79bc915e → uint256
previewWrapRebasing 0x517b7faf → uint256
previewWrapUnderlying 0xe68a49dd → uint256
symbol 0x95d89b41 → string
totalSupply 0x18160ddd → uint256

Write Contract 12 functions

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

approve 0x095ea7b3
address spender
uint256 value
returns: bool
permit 0xd505accf
address owner
address spender
uint256 value
uint256 deadline
uint8 v
bytes32 r
bytes32 s
renounceOwnership 0x715018a6
No parameters
setDelegatee 0xe842a64b
address _newDelegatee
transfer 0xa9059cbb
address to
uint256 value
returns: bool
transferFrom 0x23b872dd
address from
address to
uint256 value
returns: bool
transferOwnership 0xf2fde38b
address newOwner
unwrapToFixed 0x2fd06421
uint256 _wrappedAmount
returns: uint256
unwrapToRebasing 0x663ecad7
uint256 _wrappedAmount
returns: uint256
wrapFixed 0x1f7fa309
uint256 _fixedTokensToWrap
returns: uint256
wrapRebasing 0xbb428479
uint256 _lstAmountToWrap
returns: uint256
wrapUnderlying 0x4aae9fed
uint256 _stakeTokensToWrap
returns: uint256

Recent Transactions

No transactions found for this address