Cryo Explorer Ethereum Mainnet

Address Contract Verified

Address 0x4762AFD98cc78b5D662a3714FB086B658e9575FC
Balance 0 ETH
Nonce 1
Code Size 19204 bytes
Indexed Transactions 0
External Etherscan · Sourcify

Contract Bytecode

19204 bytes
0x6080604052600436106104845760003560e01c80638f81c12c1161025e578063b85a8b2011610143578063e1c84ea4116100bb578063fa6bd2ee1161008a578063fdbb3acf1161006f578063fdbb3acf14610d8b578063fdc10daf14610dc4578063ffbfb8ad14610de457600080fd5b8063fa6bd2ee14610d55578063fc7b9c1814610d7557600080fd5b8063e1c84ea414610cdf578063ecbed39b14610cf5578063f2fde38b14610d15578063f7b6090814610d3557600080fd5b8063d1400f6811610112578063d984d5b2116100f7578063d984d5b214610c93578063d9aa047314610ca9578063dd351b9a14610cc957600080fd5b8063d1400f6814610c53578063d8fdf91714610c7357600080fd5b8063b85a8b2014610be8578063c3971f6514610bfd578063c914b43714610c13578063c99cb2b714610c3357600080fd5b8063a3320222116101d6578063abdc5541116101a5578063ad09014d1161018a578063ad09014d14610b93578063b1511cc914610ba8578063b6b2196d14610bc857600080fd5b8063abdc554114610b69578063aceb2d0414610b7e57600080fd5b8063a332022214610ae7578063a5fdc5de14610b07578063a74dd4c414610b34578063a7c6a10014610b5457600080fd5b806394bcfdaa1161022d5780639ce11f2d116102125780639ce11f2d14610a93578063a0609bd414610ac7578063a20baee614610a1957600080fd5b806394bcfdaa14610a5557806396f57b6e14610a7557600080fd5b80638f81c12c146109e35780638ffadd0c146109f9578063923c1eec14610a19578063924adef014610a3557600080fd5b80633fe1da88116103845780635b8b5770116102fc578063724e78da116102cb578063839936c0116102b0578063839936c0146109535780638abca673146109915780638da5cb5b146109c557600080fd5b8063724e78da14610913578063741bef1a1461093357600080fd5b80635b8b577014610882578063679fda70146108be5780636c665a55146108de578063715018a6146108fe57600080fd5b80634bb9704211610353578063540385a311610338578063540385a31461081c5780635818869314610832578063597810341461086257600080fd5b80634bb97042146107e65780634d656faa146107fb57600080fd5b80633fe1da88146107705780634349d0811461079057806344ba8b0e146107a3578063451e8739146107b957600080fd5b806323010c2e116104175780632a807f7c116103e65780632f865568116103cb5780632f865568146107105780633cf57c61146107305780633d9138181461075057600080fd5b80632a807f7c146106d25780632f17e030146106f057600080fd5b806323010c2e146106525780632389609114610672578063274b600a14610692578063294c6095146106b257600080fd5b80630b3392d8116104535780630b3392d8146105a65780631a0121db146105c657806322867d78146105fe57806322be3de11461061e57600080fd5b8063027d59461461050d578063042e02cf1461052d57806304aecb7614610562578063098d32281461058257600080fd5b3661050857336001600160a01b037f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc216146105065760405162461bcd60e51b815260206004820152601360248201527f6f6e6c792d6e61746976652d777261707065640000000000000000000000000060448201526064015b60405180910390fd5b005b600080fd5b34801561051957600080fd5b50610506610528366004614591565b610dfa565b34801561053957600080fd5b5061054d6105483660046145d3565b611195565b60405190151581526020015b60405180910390f35b34801561056e57600080fd5b5061054d61057d3660046145f0565b61126d565b34801561058e57600080fd5b5061059860001981565b604051908152602001610559565b3480156105b257600080fd5b506105066105c1366004614629565b6115e2565b3480156105d257600080fd5b506105e66105e1366004614642565b61162b565b6040516001600160a01b039091168152602001610559565b34801561060a57600080fd5b50610506610619366004614642565b611654565b34801561062a57600080fd5b506105e67f000000000000000000000000f6a63bb53f9049e7c6bdcbe1e25a157035eeb65781565b34801561065e57600080fd5b5061050661066d36600461467c565b6118ac565b34801561067e57600080fd5b5061050661068d3660046145d3565b6118df565b34801561069e57600080fd5b506105066106ad3660046145d3565b61195f565b3480156106be57600080fd5b506007546105e6906001600160a01b031681565b3480156106de57600080fd5b506012546001600160a01b03166105e6565b3480156106fc57600080fd5b50600f546105e6906001600160a01b031681565b34801561071c57600080fd5b5061050661072b3660046145d3565b6119df565b34801561073c57600080fd5b5061050661074b366004614629565b611b42565b34801561075c57600080fd5b5061050661076b366004614642565b611bdb565b34801561077c57600080fd5b506105e661078b3660046146f1565b611ca8565b61050661079e3660046145d3565b611d9b565b3480156107af57600080fd5b50610598600c5481565b3480156107c557600080fd5b506105986107d43660046145d3565b60096020526000908152604090205481565b3480156107f257600080fd5b5061059861207f565b34801561080757600080fd5b5060105461054d90600160a01b900460ff1681565b34801561082857600080fd5b5061059860055481565b34801561083e57600080fd5b5061054d61084d3660046145d3565b60116020526000908152604090205460ff1681565b34801561086e57600080fd5b5061050661087d366004614786565b6120a1565b34801561088e57600080fd5b506105e661089d3660046145d3565b6001600160a01b039081166000908152601560205260409020600101541690565b3480156108ca57600080fd5b506010546105e6906001600160a01b031681565b3480156108ea57600080fd5b506105066108f9366004614591565b61223c565b34801561090a57600080fd5b506105066126ca565b34801561091f57600080fd5b5061050661092e3660046145d3565b6126de565b34801561093f57600080fd5b506002546105e6906001600160a01b031681565b34801561095f57600080fd5b5061054d61096e3660046145d3565b6001600160a01b0390811660009081526015602052604090206001015416151590565b34801561099d57600080fd5b506105e67f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc281565b3480156109d157600080fd5b506001546001600160a01b03166105e6565b3480156109ef57600080fd5b50610598600e5481565b348015610a0557600080fd5b50610598610a143660046145d3565b612786565b348015610a2557600080fd5b50610598670de0b6b3a764000081565b348015610a4157600080fd5b50610506610a503660046145d3565b6127a7565b348015610a6157600080fd5b506008546105e6906001600160a01b031681565b348015610a8157600080fd5b506013546001600160a01b03166105e6565b348015610a9f57600080fd5b506105e67f000000000000000000000000bdea8a2e648c2beb237bb1a48a13a2395b5058da81565b348015610ad357600080fd5b50610506610ae23660046147c7565b612827565b348015610af357600080fd5b50610506610b02366004614629565b612a2c565b348015610b1357600080fd5b50610598610b223660046145d3565b60176020526000908152604090205481565b348015610b4057600080fd5b50610506610b4f366004614629565b612a75565b348015610b6057600080fd5b50601454610598565b348015610b7557600080fd5b50610598612abe565b348015610b8a57600080fd5b50610598612ad3565b348015610b9f57600080fd5b50610598612af4565b348015610bb457600080fd5b50610506610bc3366004614629565b612b1e565b348015610bd457600080fd5b50610506610be3366004614629565b612b67565b348015610bf457600080fd5b50610598612c1e565b348015610c0957600080fd5b5061059860065481565b348015610c1f57600080fd5b506003546105e6906001600160a01b031681565b348015610c3f57600080fd5b50610506610c4e3660046145d3565b612c33565b348015610c5f57600080fd5b50610506610c6e366004614642565b612eb1565b348015610c7f57600080fd5b50610506610c8e3660046145d3565b613003565b348015610c9f57600080fd5b50610598600d5481565b348015610cb557600080fd5b50610506610cc43660046147c7565b6130ca565b348015610cd557600080fd5b50610598600b5481565b348015610ceb57600080fd5b50610598600a5481565b348015610d0157600080fd5b50610506610d1036600461481a565b6134a3565b348015610d2157600080fd5b50610506610d303660046145d3565b61351e565b348015610d4157600080fd5b50610506610d503660046145d3565b613572565b348015610d6157600080fd5b5061054d610d703660046145d3565b61361a565b348015610d8157600080fd5b5061059860185481565b348015610d9757600080fd5b506105e6610da63660046145d3565b6001600160a01b039081166000908152601560205260409020541690565b348015610dd057600080fd5b50610506610ddf3660046145f0565b613625565b348015610df057600080fd5b5061059860045481565b82336001600160a01b0316816001600160a01b0316631f52692b6040518163ffffffff1660e01b8152600401602060405180830381865afa158015610e43573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610e679190614837565b6001600160a01b031614610eb05760405162461bcd60e51b815260206004820152601060248201526f37b7363c96bb30bab63a16b7bbb732b960811b60448201526064016104fd565b6001600160a01b0380851660009081526015602052604090206001015416610f0c5760405162461bcd60e51b815260206004820152600f60248201526e1d985d5b1d0b5b9bdd0b599bdd5b99608a1b60448201526064016104fd565b610f357f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2613857565b610f815760405162461bcd60e51b815260206004820152601860248201527f636f6c6c61746572616c2d6e6f742d737570706f72746564000000000000000060448201526064016104fd565b604051633879b0c560e01b81526001600160a01b037f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc28116600483015260248201859052306044830152851690633879b0c590606401600060405180830381600087803b158015610ff157600080fd5b505af1158015611005573d6000803e3d6000fd5b5050506001600160a01b037f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2166000908152601760205260408120805486935090919061105390849061486a565b90915550506040517f2e1a7d4d000000000000000000000000000000000000000000000000000000008152600481018490527f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc26001600160a01b031690632e1a7d4d90602401600060405180830381600087803b1580156110d357600080fd5b505af11580156110e7573d6000803e3d6000fd5b505050506000826001600160a01b03168460405160006040518083038185875af1925050503d8060008114611138576040519150601f19603f3d011682016040523d82523d6000602084013e61113d565b606091505b505090508061118e5760405162461bcd60e51b815260206004820152600f60248201527f7472616e736665722d6661696c6564000000000000000000000000000000000060448201526064016104fd565b5050505050565b6001600160a01b038082166000908152601560205260408120600101549091166111f35760405162461bcd60e51b815260206004820152600f60248201526e1d985d5b1d0b5b9bdd0b599bdd5b99608a1b60448201526064016104fd565b60405163c3f7e0b760e01b815260016004820152670de0b6b3a7640000906001600160a01b0384169063c3f7e0b790602401602060405180830381865afa158015611242573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611266919061487d565b1092915050565b600061127882613857565b6112c45760405162461bcd60e51b815260206004820152601860248201527f636f6c6c61746572616c2d6e6f742d737570706f72746564000000000000000060448201526064016104fd565b6040517f4bfc894d0000000000000000000000000000000000000000000000000000000081526001600160a01b038381166004830152841690634bfc894d90602401602060405180830381865afa158015611323573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906113479190614896565b611353575060006115dc565b60405163c3f7e0b760e01b8152600060048201819052906001600160a01b0385169063c3f7e0b790602401602060405180830381865afa15801561139b573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906113bf919061487d565b905060065481106113d45760009150506115dc565b6002546040517faa41911f0000000000000000000000000000000000000000000000000000000081526001600160a01b03858116600483015290911690600090829063aa41911f90602401602060405180830381865afa15801561143c573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611460919061487d565b90506000866001600160a01b0316634113e5ca6040518163ffffffff1660e01b8152600401600060405180830381865afa1580156114a2573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526114ca91908101906148b3565b805190915060005b818110156115d157876001600160a01b03168382815181106114f6576114f6614965565b60200260200101516001600160a01b0316146115bf576000856001600160a01b031663aa41911f85848151811061152f5761152f614965565b60200260200101516040518263ffffffff1660e01b815260040161156291906001600160a01b0391909116815260200190565b602060405180830381865afa15801561157f573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906115a3919061487d565b9050848111156115bd5760009750505050505050506115dc565b505b806115c98161497b565b9150506114d2565b506001955050505050505b92915050565b6115ea6138f5565b600b5460408051918252602082018390527f83243d8dfaeb36f198b8b4fae6e54c48ee1a5a89dcf13fcafe5486801cac5464910160405180910390a1600b55565b6001600160a01b038216600090815260166020526040812061164d908361393b565b9392505050565b81336001600160a01b0316816001600160a01b0316631f52692b6040518163ffffffff1660e01b8152600401602060405180830381865afa15801561169d573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906116c19190614837565b6001600160a01b03161461170a5760405162461bcd60e51b815260206004820152601060248201526f37b7363c96bb30bab63a16b7bbb732b960811b60448201526064016104fd565b6001600160a01b03808416600090815260156020526040902060010154166117665760405162461bcd60e51b815260206004820152600f60248201526e1d985d5b1d0b5b9bdd0b599bdd5b99608a1b60448201526064016104fd565b8160186000828254611778919061486a565b90915550506040517f371fd8e6000000000000000000000000000000000000000000000000000000008152600481018390526001600160a01b0384169063371fd8e690602401600060405180830381600087803b1580156117d857600080fd5b505af11580156117ec573d6000803e3d6000fd5b5050505061182c6117fa3390565b6001600160a01b037f000000000000000000000000f6a63bb53f9049e7c6bdcbe1e25a157035eeb65716903085613947565b604051630852cd8d60e31b8152600481018390527f000000000000000000000000f6a63bb53f9049e7c6bdcbe1e25a157035eeb6576001600160a01b0316906342966c68906024015b600060405180830381600087803b15801561188f57600080fd5b505af11580156118a3573d6000803e3d6000fd5b50505050505050565b6118b46138f5565b6001600160a01b03919091166000908152601160205260409020805460ff1916911515919091179055565b6118e76138f5565b6001600160a01b03811661193d5760405162461bcd60e51b815260206004820152601360248201527f7661756c742d6465706c6f7965722d69732d300000000000000000000000000060448201526064016104fd565b600f80546001600160a01b0319166001600160a01b0392909216919091179055565b6119676138f5565b6001600160a01b0381166119bd5760405162461bcd60e51b815260206004820152601960248201527f626f72726f772d6665652d726563697069656e742d69732d300000000000000060448201526064016104fd565b600780546001600160a01b0319166001600160a01b0392909216919091179055565b6119e76139c3565b6001600160a01b0380821660009081526015602052604090206001015416611a435760405162461bcd60e51b815260206004820152600f60248201526e1d985d5b1d0b5b9bdd0b599bdd5b99608a1b60448201526064016104fd565b6000816001600160a01b0316631f52692b6040518163ffffffff1660e01b8152600401602060405180830381865afa158015611a83573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611aa79190614837565b90506000826001600160a01b03166328a070256040518163ffffffff1660e01b81526004016020604051808303816000875af1158015611aeb573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611b0f919061487d565b90508060186000828254611b23919061486a565b90915550611b3390508284613a06565b5050611b3f6001600055565b50565b611b4a6138f5565b60008111611b9a5760405162461bcd60e51b815260206004820152601960248201527f6d61782d746f6b656e732d7065722d7661756c742d69732d300000000000000060448201526064016104fd565b60045460408051918252602082018390527fcaf5778278cf2f59a6d497322a6e5685e22da86955a754ebb64f427f1c4f4a3b910160405180910390a1600455565b611be36138f5565b6001600160a01b038216611c395760405162461bcd60e51b815260206004820152600f60248201527f636f6c6c61746572616c2d69732d30000000000000000000000000000000000060448201526064016104fd565b6001600160a01b0382166000818152600960209081526040918290205482519081529081018490527fd77d4caf1a3d54b0174aa0175ec5db7866556e06ae85f72cfc27becb1d39c381910160405180910390a26001600160a01b03909116600090815260096020526040902055565b600f546040517f1233d5f4000000000000000000000000000000000000000000000000000000008152600091339183916001600160a01b031690631233d5f490611cfa903090869089906004016149e4565b6020604051808303816000875af1158015611d19573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611d3d9190614837565b9050611d498282613a84565b816001600160a01b0316816001600160a01b03167fd8e508729ff9830b43ef839ead95019f779b9b8ea6e4d30055512fb4ae58195986604051611d8c9190614a19565b60405180910390a39392505050565b6001600160a01b0380821660009081526015602052604090206001015416611df75760405162461bcd60e51b815260206004820152600f60248201526e1d985d5b1d0b5b9bdd0b599bdd5b99608a1b60448201526064016104fd565b611e207f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2613857565b611e6c5760405162461bcd60e51b815260206004820152601860248201527f636f6c6c61746572616c2d6e6f742d737570706f72746564000000000000000060448201526064016104fd565b6001600160a01b037f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc216600090815260176020526040812080543492839291611eb6908490614a2c565b90915550506001600160a01b037f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2166000908152600960209081526040808320546017909252909120541115611f4e5760405162461bcd60e51b815260206004820152601660248201527f636f6c6c61746572616c2d6361702d726561636865640000000000000000000060448201526064016104fd565b7f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc26001600160a01b031663d0e30db0826040518263ffffffff1660e01b81526004016000604051808303818588803b158015611fa957600080fd5b505af1158015611fbd573d6000803e3d6000fd5b50611ff99350506001600160a01b037f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc216915084905083613b00565b6040516336badcf760e11b81526001600160a01b037f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc28116600483015260248201839052831690636d75b9ee90604401600060405180830381600087803b15801561206357600080fd5b505af1158015612077573d6000803e3d6000fd5b505050505050565b6064612094670de0b6b3a7640000600a614a3f565b61209e9190614a56565b81565b6001600160a01b03808416600090815260156020526040902060010154166120fd5760405162461bcd60e51b815260206004820152600f60248201526e1d985d5b1d0b5b9bdd0b599bdd5b99608a1b60448201526064016104fd565b61210682613857565b6121525760405162461bcd60e51b815260206004820152601860248201527f636f6c6c61746572616c2d6e6f742d737570706f72746564000000000000000060448201526064016104fd565b6001600160a01b0382166000908152601760205260408120805483929061217a908490614a2c565b90915550506001600160a01b03821660009081526009602090815260408083205460179092529091205411156121f25760405162461bcd60e51b815260206004820152601660248201527f636f6c6c61746572616c2d6361702d726561636865640000000000000000000060448201526064016104fd565b6122076001600160a01b038316338584613947565b6040516336badcf760e11b81526001600160a01b03838116600483015260248201839052841690636d75b9ee90604401611875565b82336001600160a01b0316816001600160a01b0316631f52692b6040518163ffffffff1660e01b8152600401602060405180830381865afa158015612285573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906122a99190614837565b6001600160a01b0316148061234957506001600160a01b038116636d70f7ae336040517fffffffff0000000000000000000000000000000000000000000000000000000060e084901b1681526001600160a01b039091166004820152602401602060405180830381865afa158015612325573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906123499190614896565b6123955760405162461bcd60e51b815260206004820152601c60248201527f6f6e6c792d7661756c742d6f776e65722d6f722d6f70657261746f720000000060448201526064016104fd565b6001600160a01b03808516600090815260156020526040902060010154166123f15760405162461bcd60e51b815260206004820152600f60248201526e1d985d5b1d0b5b9bdd0b599bdd5b99608a1b60448201526064016104fd565b6001600160a01b0382166124315760405162461bcd60e51b81526020600482015260076024820152660746f2d69732d360cc1b60448201526064016104fd565b82601860008282546124439190614a2c565b90915550612452905083613b31565b6040517fc5ebeaec000000000000000000000000000000000000000000000000000000008152600481018490526001600160a01b0385169063c5ebeaec90602401600060405180830381600087803b1580156124ad57600080fd5b505af11580156124c1573d6000803e3d6000fd5b50506003546040517fd71275f60000000000000000000000000000000000000000000000000000000081526001600160a01b03888116600483015260009450909116915063d71275f690602401602060405180830381865afa15801561252b573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061254f919061487d565b90506000670de0b6b3a76400006125668387614a3f565b6125709190614a56565b90506001600160a01b037f000000000000000000000000bdea8a2e648c2beb237bb1a48a13a2395b5058da166340c10f19856125ac848961486a565b6040517fffffffff0000000000000000000000000000000000000000000000000000000060e085901b1681526001600160a01b0390921660048301526024820152604401600060405180830381600087803b15801561260a57600080fd5b505af115801561261e573d6000803e3d6000fd5b50506007546040517f40c10f190000000000000000000000000000000000000000000000000000000081526001600160a01b039182166004820152602481018590527f000000000000000000000000bdea8a2e648c2beb237bb1a48a13a2395b5058da90911692506340c10f199150604401600060405180830381600087803b1580156126aa57600080fd5b505af11580156126be573d6000803e3d6000fd5b50505050505050505050565b6126d26138f5565b6126dc6000613c16565b565b6126e66138f5565b6001600160a01b03811661273c5760405162461bcd60e51b815260206004820152600e60248201527f7072696365666565642d69732d3000000000000000000000000000000000000060448201526064016104fd565b600280546001600160a01b0319166001600160a01b0383169081179091556040517fe5b20b8497e4f3e2435ef9c20e2e26b47497ee13745ce1c681ad6640653119e690600090a250565b6001600160a01b03811660009081526016602052604081206115dc90613c68565b6127af6138f5565b6001600160a01b0381166128055760405162461bcd60e51b815260206004820152601d60248201527f726564656d7074696f6e2d6665652d726563697069656e742d69732d3000000060448201526064016104fd565b600880546001600160a01b0319166001600160a01b0392909216919091179055565b83336001600160a01b0316816001600160a01b0316631f52692b6040518163ffffffff1660e01b8152600401602060405180830381865afa158015612870573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906128949190614837565b6001600160a01b0316146128dd5760405162461bcd60e51b815260206004820152601060248201526f37b7363c96bb30bab63a16b7bbb732b960811b60448201526064016104fd565b6001600160a01b03808616600090815260156020526040902060010154166129395760405162461bcd60e51b815260206004820152600f60248201526e1d985d5b1d0b5b9bdd0b599bdd5b99608a1b60448201526064016104fd565b61294284613857565b61298e5760405162461bcd60e51b815260206004820152601860248201527f636f6c6c61746572616c2d6e6f742d737570706f72746564000000000000000060448201526064016104fd565b6001600160a01b038416600090815260176020526040812080548592906129b690849061486a565b9091555050604051633879b0c560e01b81526001600160a01b038581166004830152602482018590528381166044830152861690633879b0c590606401600060405180830381600087803b158015612a0d57600080fd5b505af1158015612a21573d6000803e3d6000fd5b505050505050505050565b612a346138f5565b60065460408051918252602082018390527f7d8c8ed0e3c555998cab69931038b9fdb45b5aa9781c72eda38d58283817e165910160405180910390a1600655565b612a7d6138f5565b600c5460408051918252602082018390527f2064bbd23980e8daa68cdd49af066139432d778de4b015dd88a98ebdaa882060910160405180910390a1600c55565b6064612094670de0b6b3a76400006005614a3f565b60026064612aea670de0b6b3a76400006001614a3f565b6120949190614a56565b6064612b09670de0b6b3a76400006001614a3f565b612b139190614a56565b61209e90600a614a3f565b612b266138f5565b600a5460408051918252602082018390527f30ed4a4a8845d4c122766feed8fe1fbc1ca7a25750ad51a5763d59179da8b5df910160405180910390a1600a55565b612b6f6138f5565b6064612b84670de0b6b3a7640000600a614a3f565b612b8e9190614a56565b811115612bdd5760405162461bcd60e51b815260206004820152601860248201527f726564656d7074696f6e2d726174652d746f6f2d68696768000000000000000060448201526064016104fd565b60055460408051918252602082018390527f5c7ab69b0f96f475a3e817a94ff5726fbdbd80972444269449645cd383d5e122910160405180910390a1600555565b6064612094670de0b6b3a76400006001614a3f565b80336001600160a01b0316816001600160a01b0316631f52692b6040518163ffffffff1660e01b8152600401602060405180830381865afa158015612c7c573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612ca09190614837565b6001600160a01b031614612ce95760405162461bcd60e51b815260206004820152601060248201526f37b7363c96bb30bab63a16b7bbb732b960811b60448201526064016104fd565b6001600160a01b0380831660009081526015602052604090206001015416612d455760405162461bcd60e51b815260206004820152600f60248201526e1d985d5b1d0b5b9bdd0b599bdd5b99608a1b60448201526064016104fd565b816001600160a01b0316630dca59c16040518163ffffffff1660e01b8152600401602060405180830381865afa158015612d83573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612da7919061487d565b15612df45760405162461bcd60e51b815260206004820152600a60248201527f646562742d6e6f742d300000000000000000000000000000000000000000000060448201526064016104fd565b816001600160a01b03166329ad2fb26040518163ffffffff1660e01b8152600401602060405180830381865afa158015612e32573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612e56919061487d565b15612ea35760405162461bcd60e51b815260206004820152601060248201527f636f6c6c61746572616c2d6e6f742d300000000000000000000000000000000060448201526064016104fd565b612ead3383613a06565b5050565b612eb96139c3565b6010546001600160a01b03163314612f135760405162461bcd60e51b815260206004820152601760248201527f6f6e6c792d6c69717569646174696f6e2d726f7574657200000000000000000060448201526064016104fd565b6001600160a01b0380831660009081526015602052604090206001015416612f6f5760405162461bcd60e51b815260206004820152600f60248201526e1d985d5b1d0b5b9bdd0b599bdd5b99608a1b60448201526064016104fd565b8060186000828254612f819190614a2c565b90915550506040517f6b5bc994000000000000000000000000000000000000000000000000000000008152600481018290526001600160a01b03831690636b5bc99490602401600060405180830381600087803b158015612fe157600080fd5b505af1158015612ff5573d6000803e3d6000fd5b50505050612ead6001600055565b61300b6138f5565b6001600160a01b0381166130615760405162461bcd60e51b815260206004820152601060248201527f626f72726f772d726174652d69732d300000000000000000000000000000000060448201526064016104fd565b600354604080516001600160a01b03928316815291831660208301527f853cd4966a79d315d37b55e894f983b1b88af529ebcd5cedd9af2ec85f76e352910160405180910390a1600380546001600160a01b0319166001600160a01b0392909216919091179055565b6130d26139c3565b601054600160a01b900460ff16806130f957503360009081526011602052604090205460ff165b6131455760405162461bcd60e51b815260206004820152601660248201527f726564656d7074696f6e2d6e6f742d616c6c6f7765640000000000000000000060448201526064016104fd565b6001600160a01b03808516600090815260156020526040902060010154166131a15760405162461bcd60e51b815260206004820152600f60248201526e1d985d5b1d0b5b9bdd0b599bdd5b99608a1b60448201526064016104fd565b6001600160a01b0381166131e15760405162461bcd60e51b81526020600482015260076024820152660746f2d69732d360cc1b60448201526064016104fd565b6131eb848461126d565b6132375760405162461bcd60e51b815260206004820152600e60248201527f6e6f742d72656465656d61626c6500000000000000000000000000000000000060448201526064016104fd565b6040517f1e9a69500000000000000000000000000000000000000000000000000000000081526001600160a01b038481166004830152602482018490526000918291871690631e9a69509060440160408051808303816000875af11580156132a3573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906132c79190614a78565b9150915081601860008282546132dd919061486a565b90915550506001600160a01b0385166000908152601760205260408120805486929061330a90849061486a565b909155506133519050333061331f8486614a2c565b6001600160a01b037f000000000000000000000000f6a63bb53f9049e7c6bdcbe1e25a157035eeb65716929190613947565b604051630852cd8d60e31b8152600481018390527f000000000000000000000000f6a63bb53f9049e7c6bdcbe1e25a157035eeb6576001600160a01b0316906342966c6890602401600060405180830381600087803b1580156133b357600080fd5b505af11580156133c7573d6000803e3d6000fd5b50506008546040517fa9059cbb0000000000000000000000000000000000000000000000000000000081526001600160a01b039182166004820152602481018590527f000000000000000000000000f6a63bb53f9049e7c6bdcbe1e25a157035eeb657909116925063a9059cbb91506044016020604051808303816000875af1158015613458573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061347c9190614896565b506134916001600160a01b0386168486613b00565b505061349d6001600055565b50505050565b6134ab6138f5565b60108054821515600160a01b027fffffffffffffffffffffff00ffffffffffffffffffffffffffffffffffffffff9091161790556040517f160b89d2c4306cead20abc65b2bfbea13f9743714c23d0d26cc1044f478e4d149061351390831515815260200190565b60405180910390a150565b6135266138f5565b6001600160a01b038116613569576040517f1e4fbdf7000000000000000000000000000000000000000000000000000000008152600060048201526024016104fd565b611b3f81613c16565b61357a6138f5565b6001600160a01b0381166135d05760405162461bcd60e51b815260206004820152601760248201527f6c69717569646174696f6e2d726f757465722d69732d3000000000000000000060448201526064016104fd565b601080546001600160a01b0319166001600160a01b0383169081179091556040517fdf69295b476b36b3b7c7c84e1673f901b426d42b283a85fce98bb99ba0323caf90600090a250565b60006115dc82613857565b81336001600160a01b0316816001600160a01b0316631f52692b6040518163ffffffff1660e01b8152600401602060405180830381865afa15801561366e573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906136929190614837565b6001600160a01b0316146136db5760405162461bcd60e51b815260206004820152601060248201526f37b7363c96bb30bab63a16b7bbb732b960811b60448201526064016104fd565b336001600160a01b0383166137325760405162461bcd60e51b815260206004820152600e60248201527f6e65772d6f776e65722d69732d3000000000000000000000000000000000000060448201526064016104fd565b6001600160a01b038085166000908152601560205260409020600101541661378e5760405162461bcd60e51b815260206004820152600f60248201526e1d985d5b1d0b5b9bdd0b599bdd5b99608a1b60448201526064016104fd565b826001600160a01b0316816001600160a01b0316856001600160a01b03167fff056cfc1a0ac5c7b42c2095ad4902b58b2896a2cf3c3f782c7d0087430af37360405160405180910390a46040517fe071c0ca0000000000000000000000000000000000000000000000000000000081526001600160a01b03848116600483015285169063e071c0ca90602401600060405180830381600087803b15801561383457600080fd5b505af1158015613848573d6000803e3d6000fd5b5050505061349d818486613c72565b6002546040517f9878cbb30000000000000000000000000000000000000000000000000000000081526001600160a01b038381166004830152600092169082908290639878cbb390602401602060405180830381865afa1580156138bf573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906138e39190614837565b6001600160a01b031614159392505050565b6001546001600160a01b031633146126dc576040517f118cdaa70000000000000000000000000000000000000000000000000000000081523360048201526024016104fd565b600061164d8383613cb7565b6040516001600160a01b03848116602483015283811660448301526064820183905261349d9186918216906323b872dd906084015b604051602081830303815290604052915060e01b6020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff8381831617835250505050613ce1565b6002600054036139ff576040517f3ee5aeb500000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6002600055565b613a11601282613d5d565b613a5d5760405162461bcd60e51b815260206004820152601a60248201527f7661756c742d636f756c642d6e6f742d62652d72656d6f76656400000000000060448201526064016104fd565b6001600160a01b0382166000908152601660205260409020613a7f9082613f4c565b505050565b613a92601282600080613f61565b613ade5760405162461bcd60e51b815260206004820152601860248201527f7661756c742d636f756c642d6e6f742d62652d6164646564000000000000000060448201526064016104fd565b6001600160a01b0382166000908152601660205260409020613a7f90826142aa565b6040516001600160a01b03838116602483015260448201839052613a7f91859182169063a9059cbb9060640161397c565b600a546018541115613b855760405162461bcd60e51b815260206004820152601460248201527f646562742d6365696c696e672d7265616368656400000000000000000000000060448201526064016104fd565b600c54600d54613b959190614a2c565b421115613baa57600e81905542600d55613bc2565b80600e6000828254613bbc9190614a2c565b90915550505b600b54600e541115611b3f5760405162461bcd60e51b815260206004820152601a60248201527f646562742d77696e646f772d616d6f756e742d7265616368656400000000000060448201526064016104fd565b600180546001600160a01b038381166001600160a01b0319831681179093556040519116919082907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e090600090a35050565b60006115dc825490565b6001600160a01b0383166000908152601660205260409020613c949082613f4c565b506001600160a01b038216600090815260166020526040902061349d90826142aa565b6000826000018281548110613cce57613cce614965565b9060005260206000200154905092915050565b6000613cf66001600160a01b038416836142bf565b90508051600014158015613d1b575080806020019051810190613d199190614896565b155b15613a7f576040517f5274afe70000000000000000000000000000000000000000000000000000000081526001600160a01b03841660048201526024016104fd565b6001600160a01b038082166000908152600384016020908152604080832081518083019092528054851682526001015490931690830181905290919015613f425783546001600160a01b038481169116148015613dc9575060018401546001600160a01b038481169116145b15613dee5783546001600160a01b031990811685556001850180549091169055613eee565b60018401546001600160a01b0390811690841603613e4c576020808201516001600160a01b0316600081815260038701909252604090912080546001600160a01b031990811683179091556001860180549091169091179055613eee565b83546001600160a01b0390811690841603613ea057805184546001600160a01b039091166001600160a01b031991821681178655600081815260038701602052604090206001018054909216179055613eee565b60208082015182516001600160a01b0391821660008181526003890190945260408085208054949093166001600160a01b031994851681179093559184529220600101805490911690911790555b6001600160a01b0383166000908152600385016020526040902080546001600160a01b031990811682556001918201805490911690556002850154613f33919061486a565b600285015550600190506115dc565b5060009392505050565b600061164d836001600160a01b0384166142cd565b60006001600160a01b0383161580613f9757506001600160a01b0383811660009081526003870160205260409020600101541615155b613fe35760405162461bcd60e51b815260206004820152601f60248201527f3739643364205f726566206e6569746865722076616c6964206e6f722030780060448201526064016104fd565b6001600160a01b0380851660009081526003870160205260409020805490911661429c5785546001600160a01b03166140565780546001600160a01b0386166001600160a01b0319918216811783556001808401805484168317905588018054831682179055875490911617865561427d565b82801561408357506001600160a01b0384161580614083575060018601546001600160a01b038581169116145b156140e157600180870180546001600160a01b03908116600081815260038b01602052604090208054928a166001600160a01b031993841681179091558554831681178655938501805483169091179055815416909117905561427d565b8215801561410c57506001600160a01b038416158061410c575085546001600160a01b038581169116145b156141685785546001600160a01b0390811660008181526003890160205260409020600190810180546001600160a01b0319908116948a169485179091558454811690921784558301805482168317905587541617865561427d565b6001600160a01b03808516600090815260038801602090815260409182902082518084019093528054841683526001015490921691810191909152831561421057805182546001600160a01b039182166001600160a01b0319918216178455600180850180548985169084168117909155600090815260038b01602052604080822080548c87169086168117909155865190951682529020018054909116909117905561427b565b81546001600160a01b038087166001600160a01b031992831681178555602080850180516001808901805492871692881692909217909155600093845260038d01909252604080842090920180548c8616908716811790915590519093168252902080549092161790555b505b600286015461428d906001614a2c565b600287015550600190506142a2565b60009150505b949350505050565b600061164d836001600160a01b0384166143c0565b606061164d8383600061440f565b600081815260018301602052604081205480156143b65760006142f160018361486a565b85549091506000906143059060019061486a565b905080821461436a57600086600001828154811061432557614325614965565b906000526020600020015490508087600001848154811061434857614348614965565b6000918252602080832090910192909255918252600188019052604090208390555b855486908061437b5761437b614a9c565b6001900381819060005260206000200160009055905585600101600086815260200190815260200160002060009055600193505050506115dc565b60009150506115dc565b6000818152600183016020526040812054614407575081546001818101845560008481526020808220909301849055845484825282860190935260409020919091556115dc565b5060006115dc565b60608147101561444d576040517fcd7860590000000000000000000000000000000000000000000000000000000081523060048201526024016104fd565b600080856001600160a01b031684866040516144699190614ab2565b60006040518083038185875af1925050503d80600081146144a6576040519150601f19603f3d011682016040523d82523d6000602084013e6144ab565b606091505b50915091506144bb8683836144c5565b9695505050505050565b6060826144da576144d58261453a565b61164d565b81511580156144f157506001600160a01b0384163b155b15614533576040517f9996b3150000000000000000000000000000000000000000000000000000000081526001600160a01b03851660048201526024016104fd565b508061164d565b80511561454a5780518082602001fd5b6040517f1425ea4200000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6001600160a01b0381168114611b3f57600080fd5b6000806000606084860312156145a657600080fd5b83356145b18161457c565b92506020840135915060408401356145c88161457c565b809150509250925092565b6000602082840312156145e557600080fd5b813561164d8161457c565b6000806040838503121561460357600080fd5b823561460e8161457c565b9150602083013561461e8161457c565b809150509250929050565b60006020828403121561463b57600080fd5b5035919050565b6000806040838503121561465557600080fd5b82356146608161457c565b946020939093013593505050565b8015158114611b3f57600080fd5b6000806040838503121561468f57600080fd5b823561469a8161457c565b9150602083013561461e8161466e565b634e487b7160e01b600052604160045260246000fd5b604051601f8201601f1916810167ffffffffffffffff811182821017156146e9576146e96146aa565b604052919050565b6000602080838503121561470457600080fd5b823567ffffffffffffffff8082111561471c57600080fd5b818501915085601f83011261473057600080fd5b813581811115614742576147426146aa565b614754601f8201601f191685016146c0565b9150808252868482850101111561476a57600080fd5b8084840185840137600090820190930192909252509392505050565b60008060006060848603121561479b57600080fd5b83356147a68161457c565b925060208401356147b68161457c565b929592945050506040919091013590565b600080600080608085870312156147dd57600080fd5b84356147e88161457c565b935060208501356147f88161457c565b925060408501359150606085013561480f8161457c565b939692955090935050565b60006020828403121561482c57600080fd5b813561164d8161466e565b60006020828403121561484957600080fd5b815161164d8161457c565b634e487b7160e01b600052601160045260246000fd5b818103818111156115dc576115dc614854565b60006020828403121561488f57600080fd5b5051919050565b6000602082840312156148a857600080fd5b815161164d8161466e565b600060208083850312156148c657600080fd5b825167ffffffffffffffff808211156148de57600080fd5b818501915085601f8301126148f257600080fd5b815181811115614904576149046146aa565b8060051b91506149158483016146c0565b818152918301840191848101908884111561492f57600080fd5b938501935b8385101561495957845192506149498361457c565b8282529385019390850190614934565b98975050505050505050565b634e487b7160e01b600052603260045260246000fd5b60006001820161498d5761498d614854565b5060010190565b60005b838110156149af578181015183820152602001614997565b50506000910152565b600081518084526149d0816020860160208601614994565b601f01601f19169290920160200192915050565b60006001600160a01b03808616835280851660208401525060606040830152614a1060608301846149b8565b95945050505050565b60208152600061164d60208301846149b8565b808201808211156115dc576115dc614854565b80820281158282048414176115dc576115dc614854565b600082614a7357634e487b7160e01b600052601260045260246000fd5b500490565b60008060408385031215614a8b57600080fd5b505080516020909101519092909150565b634e487b7160e01b600052603160045260246000fd5b60008251614ac4818460208701614994565b919091019291505056fea2646970667358221220415db9ba4affecb58f1586412ba4603dec08c9a1aa2b77e17ed96337c475b34564736f6c63430008150033

Verified Source Code Full Match

Compiler: v0.8.21+commit.d9974bed EVM: paris Optimization: Yes (1000 runs)
Vault.sol 559 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;

import '@openzeppelin/contracts/access/Ownable.sol';
import '@openzeppelin/contracts/interfaces/IERC20Metadata.sol';
import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';
import '@openzeppelin/contracts/utils/Context.sol';
import '@openzeppelin/contracts/utils/structs/EnumerableSet.sol';

import './interfaces/IPriceFeed.sol';
import './interfaces/IVaultFactory.sol';
import './interfaces/IVaultFactoryConfig.sol';
import './interfaces/ILiquidationRouter.sol';

import './utils/constants.sol';
import './interfaces/ITokenPriceFeed.sol';
import './interfaces/IVaultExtraSettings.sol';
import './utils/linked-address-list.sol';

/**
 * @title Vault
 * @dev Manages creation, collateralization, borrowing, and liquidation of Vaults.
 */
contract Vault is Context, Constants {
    string public constant VERSION = '1.2.0';

    // Events emitted by the contract
    event CollateralAdded(
        address indexed collateral,
        uint256 amount,
        uint256 newTotalAmount
    );
    event CollateralRemoved(
        address indexed collateral,
        uint256 amount,
        uint256 newTotalAmount
    );
    event CollateralRedeemed(
        address indexed collateral,
        uint256 amount,
        uint256 newTotalAmount,
        uint256 stableAmountUsed,
        uint256 feePaid
    );

    event DebtAdded(uint256 amount, uint256 newTotalDebt);
    event DebtRepaid(uint256 amount, uint256 newTotalDebt);

    modifier onlyFactory() {
        require(_msgSender() == factory, 'only-factory');
        _;
    }

    using SafeERC20 for IERC20;
    using EnumerableSet for EnumerableSet.AddressSet;

    address public immutable stable;
    address public immutable factory;
    address public vaultOwner;

    string public name;

    EnumerableSet.AddressSet private collateralSet;
    EnumerableSet.AddressSet private operators;

    IVaultExtraSettings public vaultExtraSettings;
    mapping(address => uint256) public collateral;

    uint256 public debt;

    modifier onlyVaultOwner() {
        require(_msgSender() == vaultOwner, 'only-vault-owner');
        _;
    }

    /**
     * @dev Constructor to initialize the Vault contract.
     * @param _factory Address of the VaultFactory contract.
     * @param _vaultOwner Address of the initial owner of the Vault.
     * @param _name Name of the Vault.
     */
    constructor(
        address _factory,
        address _vaultOwner,
        string memory _name,
        IVaultExtraSettings _vaultExtraSettings
    ) {
        require(_vaultOwner != address(0x0), 'vault-owner-is-0');
        require(bytes(_name).length > 0, 'name-is-empty');
        require(_factory != address(0x0), 'factory-is-0');
        require(
            address(_vaultExtraSettings) != address(0x0),
            'vault-extra-settings-is-0'
        );

        factory = _factory;
        vaultOwner = _vaultOwner;
        stable = IVaultFactory(factory).stable();
        name = _name;
        vaultExtraSettings = _vaultExtraSettings;
    }

    /**
     * @dev Transfers ownership of the Vault to a new owner.
     * @param _newOwner Address of the new owner.
     */
    function transferVaultOwnership(address _newOwner) external onlyFactory {
        vaultOwner = _newOwner;
    }

    /**
     * @dev Sets a new name for the Vault.
     * @param _name New name for the Vault.
     */
    function setName(string memory _name) external onlyVaultOwner {
        require(bytes(_name).length > 0, 'name-is-empty');
        name = _name;
    }

    /**
     * @dev Adds an operator to the Vault, allowing them certain permissions.
     * @param _operator Address of the operator to be added.
     */
    function addOperator(address _operator) external onlyVaultOwner {
        require(_operator != address(0x0), 'operator-is-0');
        operators.add(_operator);
    }

    /**
     * @dev Removes an operator from the Vault, revoking their permissions.
     * @param _operator Address of the operator to be removed.
     */
    function removeOperator(address _operator) external onlyVaultOwner {
        require(_operator != address(0x0), 'operator-is-0');
        operators.remove(_operator);
    }

    /**
     * @dev Checks if an address is an operator for this Vault.
     * @param _operator Address to check.
     * @return Boolean indicating whether the address is an operator.
     */
    function isOperator(address _operator) external view returns (bool) {
        return operators.contains(_operator);
    }

    /**
     * @dev Returns the number of operators in the Vault.
     * @return Length of the operators set.
     */
    function operatorsLength() external view returns (uint256) {
        return operators.length();
    }

    /**
     * @dev Returns the operator at a given index in the operators set.
     * @param _index Index of the operator.
     * @return Address of the operator at the given index.
     */
    function operatorAt(uint256 _index) external view returns (address) {
        return operators.at(_index);
    }

    /**
     * @dev Checks if a collateral token is added to the Vault.
     * @param _collateral Address of the collateral token to check.
     * @return Boolean indicating whether the collateral token is added.
     */
    function containsCollateral(
        address _collateral
    ) external view returns (bool) {
        return collateralSet.contains(_collateral);
    }

    /**
     * @dev Returns the number of collateral tokens added to the Vault.
     * @return Length of the collateral set.
     */
    function collateralsLength() external view returns (uint256) {
        return collateralSet.length();
    }

    /**
     * @dev Returns the collateral token address at a given index in the collateral set.
     * @param _index Index of the collateral token.
     * @return Address of the collateral token at the given index.
     */
    function collateralAt(uint256 _index) external view returns (address) {
        return collateralSet.at(_index);
    }

    /**
     * @dev Returns an array containing all collateral token addresses in the Vault.
     * @return Array of collateral token addresses.
     */
    function collaterals() external view returns (address[] memory) {
        address[] memory _collaterals = new address[](collateralSet.length());
        for (uint256 i = 0; i < collateralSet.length(); i++) {
            _collaterals[i] = collateralSet.at(i);
        }
        return _collaterals;
    }

    /**
     * @dev Adds a new collateral token to the Vault and updates the collateral amount.
     * @param _collateral Address of the collateral token to add.
     * @param _amount Amount of the collateral token to add.
     */
    function addCollateral(
        address _collateral,
        uint256 _amount
    ) external onlyFactory {
        require(_collateral != address(0x0), 'collateral-is-0');
        require(_amount > 0, 'amount-is-0');

        collateralSet.add(_collateral);
        uint256 _maxTokens = IVaultFactory(factory).MAX_TOKENS_PER_VAULT();
        require(collateralSet.length() <= _maxTokens, 'max-tokens-reached');

        collateral[_collateral] += _amount;

        emit CollateralAdded(_collateral, _amount, collateral[_collateral]);
    }

    /**
     * @dev Removes a collateral token from the Vault and transfers it back to the sender.
     * @param _collateral Address of the collateral token to remove.
     * @param _amount Amount of the collateral token to remove.
     * @param _to Address to receive the removed collateral.
     */
    function removeCollateral(
        address _collateral,
        uint256 _amount,
        address _to
    ) external onlyFactory {
        require(_collateral != address(0x0), 'collateral-is-0');
        require(_amount > 0, 'amount-is-0');

        collateral[_collateral] -= _amount;
        if (collateral[_collateral] == 0) {
            collateralSet.remove(_collateral);
        }

        uint256 _healthFactor = healthFactor(false);
        require(_healthFactor >= DECIMAL_PRECISION, 'health-factor-below-1');

        IERC20(_collateral).safeTransfer(_to, _amount);

        emit CollateralRemoved(_collateral, _amount, collateral[_collateral]);
    }

    /**
     * @dev Adds bad debt to the Vault.
     * @param _amount Amount of bad debt to add.
     */
    function addBadDebt(uint256 _amount) external onlyFactory {
        require(_amount > 0, 'amount-is-0');

        debt += _amount;
        emit DebtAdded(_amount, debt);
    }

    /**
     * @dev Calculates the maximum borrowable amount and the current borrowable amount.
     * @return _maxBorrowable Maximum borrowable amount.
     * @return _borrowable Current borrowable amount.
     */
    function borrowable()
        public
        view
        returns (uint256 _maxBorrowable, uint256 _borrowable)
    {
        (_maxBorrowable, _borrowable) = borrowableWithDiff(
            address(0x0),
            0,
            false,
            false
        );
    }

    /**
     * @dev Borrows a specified amount from the Vault.
     * @param _amount Amount to borrow.
     */
    function borrow(uint256 _amount) external onlyFactory {
        require(_amount > 0, 'amount-is-0');

        (uint256 _maxBorrowable, uint256 _borrowable) = borrowable();
        require(_amount <= _borrowable, 'not-enough-borrowable');

        debt += _amount;
        require(debt <= _maxBorrowable, 'max-borrowable-reached');

        emit DebtAdded(_amount, debt);
    }

    /**
     * @dev Repays a specified amount to the Vault's debt.
     * @param _amount Amount to repay.
     */
    function repay(uint256 _amount) external onlyFactory {
        require(_amount <= debt, 'amount-exceeds-debt');

        debt -= _amount;
        emit DebtRepaid(_amount, debt);
    }

    /**
     * @dev Calculates the stable amount needed and the redemption fee for redeeming collateral.
     * @param _collateral Address of the collateral token.
     * @param _collateralAmount Amount of collateral to redeem.
     * @return _stableAmountNeeded Stablecoin amount required to redeem collateral.
     * @return _redemptionFee Fee charged for the redemption.
     */
    function calcRedeem(
        address _collateral,
        uint256 _collateralAmount
    )
        public
        view
        returns (uint256 _stableAmountNeeded, uint256 _redemptionFee)
    {
        ITokenPriceFeed _priceFeed = ITokenPriceFeed(
            IVaultFactory(factory).priceFeed()
        );
        uint256 _price = _priceFeed.tokenPrice(_collateral);

        uint256 _normalizedCollateralAmount = _collateralAmount *
            (10 ** (18 - _priceFeed.decimals(_collateral)));
        _stableAmountNeeded =
            (_normalizedCollateralAmount * _price) /
            DECIMAL_PRECISION;

        (, , uint256 _redemptionKickbackRate) = vaultExtraSettings
            .getExtraSettings();

        if (_redemptionKickbackRate > 0) {
            uint256 _kickbackAmount = (_stableAmountNeeded *
                _redemptionKickbackRate) / DECIMAL_PRECISION;
            _stableAmountNeeded += _kickbackAmount;
        }
        uint256 _redemptionRate = IVaultFactoryConfig(factory).redemptionRate();
        _redemptionFee =
            (_stableAmountNeeded * _redemptionRate) /
            DECIMAL_PRECISION;
    }

    /**
     * @dev Redeems a specified amount of collateral, repays debt, and transfers collateral back to the redeemer.
     * @param _collateral Address of the collateral token to redeem.
     * @param _collateralAmount Amount of collateral to redeem.
     * @return _debtRepaid Amount of debt repaid.
     * @return _feeCollected Fee collected for the redemption.
     */
    function redeem(
        address _collateral,
        uint256 _collateralAmount
    )
        external
        onlyFactory
        returns (uint256 _debtRepaid, uint256 _feeCollected)
    {
        require(_collateral != address(0x0), 'collateral-is-0');
        require(_collateralAmount > 0, 'amount-is-0');
        require(collateralSet.contains(_collateral), 'collateral-not-added');
        require(
            collateral[_collateral] >= _collateralAmount,
            'not-enough-collateral'
        );

        uint256 _currentHealthFactor = healthFactor(true);
        uint256 _redemptionHealthFactorLimit = IVaultFactoryConfig(factory)
            .redemptionHealthFactorLimit();
        require(
            _currentHealthFactor < _redemptionHealthFactorLimit,
            'health-factor-above-redemption-limit'
        );

        (uint256 _debtTreshold, uint256 _maxRedeemablePercentage, ) = vaultExtraSettings
            .getExtraSettings();

        
        collateral[_collateral] -= _collateralAmount;
        (_debtRepaid, _feeCollected) = calcRedeem(
            _collateral,
            _collateralAmount
        );

        
        if (debt > _debtTreshold) {
            uint256 _redeemableDebt = (debt * _maxRedeemablePercentage) /
                DECIMAL_PRECISION;
            require(_debtRepaid <= _redeemableDebt, 'redeemable-debt-exceeded');

        }

        debt -= _debtRepaid;

        if (collateral[_collateral] == 0) {
            collateralSet.remove(_collateral);
        }

        IERC20(_collateral).safeTransfer(_msgSender(), _collateralAmount);

        emit CollateralRedeemed(
            _collateral,
            _collateralAmount,
            collateral[_collateral],
            _debtRepaid,
            _feeCollected
        );
        emit DebtRepaid(_debtRepaid, debt);
    }

    /**
     * @dev Computes the health factor of the Vault.
     * @param _useMlr Flag to use Minimum Loan Ratio (MLR) in health factor computation.
     * @return _healthFactor Current health factor.
     */
    function healthFactor(
        bool _useMlr
    ) public view returns (uint256 _healthFactor) {
        if (debt == 0) {
            return type(uint256).max;
        }

        (uint256 _maxBorrowable, ) = borrowableWithDiff(
            address(0x0),
            0,
            false,
            _useMlr
        );

        _healthFactor = (_maxBorrowable * DECIMAL_PRECISION) / debt;
    }

    /**
     * @dev Computes a new health factor given a new debt value.
     * @param _newDebt New debt amount to calculate the health factor.
     * @param _useMlr Flag to use Minimum Loan Ratio (MLR) in health factor computation.
     * @return _newHealthFactor Calculated new health factor based on the new debt value.
     */
    function newHealthFactor(
        uint256 _newDebt,
        bool _useMlr
    ) public view returns (uint256 _newHealthFactor) {
        if (_newDebt == 0) {
            return type(uint256).max;
        }

        (uint256 _maxBorrowable, ) = borrowableWithDiff(
            address(0x0),
            0,
            false,
            _useMlr
        );
        _newHealthFactor = (_maxBorrowable * DECIMAL_PRECISION) / _newDebt;
    }

    /**
     * @dev Computes the maximum borrowable amount and the current borrowable amount.
     * @param _collateral Address of the collateral token (0x0 for total vault borrowable).
     * @param _diffAmount Difference in collateral amount when adding/removing collateral.
     * @param _isAdd Flag indicating whether the collateral is added or removed.
     * @param _useMlr Flag to use Minimum Loan Ratio (MLR) in borrowable computation.
     * @return _maxBorrowable Maximum borrowable amount.
     * @return _borrowable Current borrowable amount based on the collateral.
     */
    function borrowableWithDiff(
        address _collateral,
        uint256 _diffAmount,
        bool _isAdd,
        bool _useMlr
    ) public view returns (uint256 _maxBorrowable, uint256 _borrowable) {
        uint256 _newCollateralAmount = collateral[_collateral];

        uint256 _borrowableAmount = 0;

        if (_collateral != address(0x0)) {
            require(
                IVaultFactory(factory).isCollateralSupported(_collateral),
                'collateral-not-supported'
            );
            if (_isAdd) {
                _newCollateralAmount += _diffAmount;
            } else {
                _newCollateralAmount -= _diffAmount;
            }
        }

        ITokenPriceFeed _priceFeed = ITokenPriceFeed(
            IVaultFactory(factory).priceFeed()
        );

        for (uint256 i = 0; i < collateralSet.length(); i++) {
            address _c = collateralSet.at(i);
            uint256 _collateralAmount = _c == _collateral
                ? _newCollateralAmount
                : collateral[_c];
            uint256 _price = _priceFeed.tokenPrice(_c);
            uint256 _divisor = _useMlr
                ? _priceFeed.mlr(_c)
                : _priceFeed.mcr(_c);
            uint256 _normalizedCollateralAmount = _collateralAmount *
                (10 ** (18 - _priceFeed.decimals(_c)));

            uint256 _collateralBorrowable = (_normalizedCollateralAmount *
                _price) / DECIMAL_PRECISION;

            _borrowableAmount +=
                (_collateralBorrowable * DECIMAL_PRECISION) /
                _divisor;
        }

        return (
            _borrowableAmount,
            (_borrowableAmount > debt) ? _borrowableAmount - debt : 0
        );
    }

    /**
     * @dev Liquidates the vault by repaying all debts with seized collateral.
     * @return _forgivenDebt Amount of debt forgiven during liquidation.
     */
    function liquidate() external onlyFactory returns (uint256 _forgivenDebt) {
        require(
            healthFactor(true) < DECIMAL_PRECISION,
            'liquidation-factor-above-1'
        );

        uint256 _debt = debt;
        debt = 0;
        ILiquidationRouter router = ILiquidationRouter(
            IVaultFactory(factory).liquidationRouter()
        );
        for (uint256 i = 0; i < collateralSet.length(); i++) {
            address _collateral = collateralSet.at(i);
            uint256 _collateralAmount = collateral[_collateral];
            uint256 _actualCollateralBalance = IERC20(_collateral).balanceOf(
                address(this)
            );
            if (_actualCollateralBalance < _collateralAmount) {
                _collateralAmount = _actualCollateralBalance;
            }
            collateral[_collateral] = 0;

            IERC20(_collateral).safeIncreaseAllowance(
                IVaultFactory(factory).liquidationRouter(),
                _collateralAmount
            );

            router.addSeizedCollateral(_collateral, _collateralAmount);
        }
        router.addUnderWaterDebt(address(this), _debt);
        router.tryLiquidate();
        _forgivenDebt = _debt;
    }
}
VaultFactory.sol 485 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;

/*

                                              
 _____ _____ _____    _____ _____ _____ _____ 
|_   _|  _  |     |  | __  |  _  |   | |  |  |
  | | |     |  |  |  | __ -|     | | | |    -|
  |_| |__|__|_____|  |_____|__|__|_|___|__|__|
                                              
*/

import '@openzeppelin/contracts/interfaces/IERC20Metadata.sol';
import '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';
import '@openzeppelin/contracts/utils/structs/EnumerableSet.sol';
import '@openzeppelin/contracts/utils/ReentrancyGuard.sol';

import './utils/linked-address-list.sol';
import './Vault.sol';
import './VaultFactoryConfig.sol';
import './VaultFactoryList.sol';
import './interfaces/IWETH.sol';
import './interfaces/ITokenPriceFeed.sol';
import './interfaces/IPriceFeed.sol';
import './interfaces/IMintableTokenOwner.sol';
import './interfaces/IMintableToken.sol';
import './interfaces/IVaultDeployer.sol';
import './interfaces/IVaultBorrowRate.sol';

/**
 * @title VaultFactory
 * @dev Manages the creation, configuration, and operations of Vaults with collateral and borrowing functionality.
 */
contract VaultFactory is ReentrancyGuard, VaultFactoryConfig, VaultFactoryList {
    // Events emitted by the contract
    event NewVault(address indexed vault, string name, address indexed owner);
    event VaultOwnerChanged(
        address indexed vault,
        address indexed oldOwner,
        address indexed newOwner
    );

    // Libraries used by the contract
    using LinkedAddressList for LinkedAddressList.List;
    using SafeERC20 for IERC20;
    using SafeERC20 for IMintableToken;

    // Immutable state variables
    address public immutable stable;
    address public immutable nativeWrapped;
    IMintableTokenOwner public immutable mintableTokenOwner;

    // State variables
    mapping(address => uint256) public collateral;
    uint256 public totalDebt;

    /**
     * @dev Constructor to initialize essential addresses and contracts for VaultFactory.
     * @param _mintableTokenOwner Address of the Mintable Token Owner contract.
     * @param _nativeWrapped Address of the native wrapped token.
     * @param _priceFeed Address of the price feed contract.
     * @param _vaultDeployer Address of the Vault Deployer contract.
     * @param _liquidationRouter Address of the liquidation router contract.
     * @param _borrowRate Address of the borrow rate contract.
     */
    constructor(
        address _mintableTokenOwner,
        address _nativeWrapped,
        address _priceFeed,
        address _vaultDeployer,
        address _liquidationRouter,
        address _borrowRate
    ) VaultFactoryConfig(_vaultDeployer, _liquidationRouter) Ownable(msg.sender) {
        require(
            _mintableTokenOwner != address(0x0),
            'mintable-token-owner-is-0'
        );

        mintableTokenOwner = IMintableTokenOwner(_mintableTokenOwner);
        stable = address(mintableTokenOwner.token());

        require(stable != address(0x0), 'stable-is-0');
        require(_nativeWrapped != address(0x0), 'nativew-is-0');
        require(_priceFeed != address(0x0), 'pricefeed-is-0');
        require(_borrowRate != address(0x0), 'borrow-rate-is-0');

        borrowRate = _borrowRate;
        nativeWrapped = _nativeWrapped;
        priceFeed = _priceFeed;
    }

    /**
     * @dev Fallback function to receive Ether and restricts its usage to a designated sender.
     */
    receive() external payable {
        require(msg.sender == nativeWrapped, 'only-native-wrapped');
    }

    /**
     * @dev Modifier: Allows function execution only by the owner of a specific vault.
     * @param _vault The address of the vault to check ownership.
     */
    modifier onlyVaultOwner(address _vault) {
        require(Vault(_vault).vaultOwner() == _msgSender(), 'only-vault-owner');
        _;
    }

    /**
     * @dev Modifier: Allows function execution by the owner or an operator of a specific vault.
     * @param _vault The address of the vault to check ownership or operator status.
     */
    modifier onlyVaultOwnerOrOperator(address _vault) {
        require(
            Vault(_vault).vaultOwner() == _msgSender() ||
                Vault(_vault).isOperator(_msgSender()),
            'only-vault-owner-or-operator'
        );
        _;
    }

    /**
     * @dev Modifier: Allows function execution only by the liquidation router.
     */
    modifier onlyLiquidationRouter() {
        require(liquidationRouter == _msgSender(), 'only-liquidation-router');
        _;
    }

    /**
     * @dev Checks if a given collateral token is supported.
     * @param _collateral The address of the collateral token.
     * @return A boolean indicating whether the collateral token is supported.
     */
    function isCollateralSupported(
        address _collateral
    ) external view returns (bool) {
        return _isCollateralSupported(_collateral);
    }

    /**
     * @dev Transfers ownership of a vault to a new owner.
     * @param _vault The address of the vault to transfer ownership.
     * @param _newOwner The address of the new owner to receive the vault ownership.
     */
    function transferVaultOwnership(
        address _vault,
        address _newOwner
    ) external onlyVaultOwner(_vault) {
        address _msgSender = _msgSender();
        require(_newOwner != address(0x0), 'new-owner-is-0');
        require(containsVault(_vault), 'vault-not-found');

        emit VaultOwnerChanged(_vault, _msgSender, _newOwner);
        Vault(_vault).transferVaultOwnership(_newOwner);
        _transferVault(_msgSender, _newOwner, _vault);
    }

    /**
     * @dev Creates a new vault with a specified name.
     * @param _name The name of the new vault.
     * @return The address of the newly created vault.
     */
    function createVault(string memory _name) public returns (address) {
        address _msgSender = _msgSender();
        address _vaultAddress = IVaultDeployer(vaultDeployer).deployVault(
            address(this),
            _msgSender,
            _name
        );
        _addVault(_msgSender, _vaultAddress);
        emit NewVault(_vaultAddress, _name, _msgSender);

        return _vaultAddress;
    }

    /**
     * @dev Checks if a specific collateral token is supported for the vault.
     * @param _collateral The address of the collateral token to check.
     * @return A boolean indicating whether the collateral token is supported.
     */
    function _isCollateralSupported(
        address _collateral
    ) internal view returns (bool) {
        ITokenPriceFeed _priceFeed = ITokenPriceFeed(priceFeed);
        return (_priceFeed.tokenPriceFeed(_collateral) != address(0x0));
    }

    /**
     * @dev Adds native-wrapped collateral to a specific vault.
     * @param _vault The address of the vault to add collateral.
     */
    function addCollateralNative(address _vault) external payable {
        require(containsVault(_vault), 'vault-not-found');
        require(
            _isCollateralSupported(nativeWrapped),
            'collateral-not-supported'
        );
        uint256 _amount = msg.value;

        collateral[nativeWrapped] += _amount;

        require(
            collateral[nativeWrapped] <= collateralCap[nativeWrapped],
            'collateral-cap-reached'
        );

        IWETH(nativeWrapped).deposit{value: _amount}();


        IERC20(nativeWrapped).safeTransfer(_vault, _amount);

        Vault(_vault).addCollateral(nativeWrapped, _amount);

    }

    /**
     * @dev Removes native-wrapped collateral from a specific vault.
     * @param _vault The address of the vault to remove collateral.
     * @param _amount The amount of collateral to be removed.
     * @param _to The address where the removed collateral is transferred.
     */
    function removeCollateralNative(
        address _vault,
        uint256 _amount,
        address _to
    ) external onlyVaultOwner(_vault) {
        require(containsVault(_vault), 'vault-not-found');
        require(
            _isCollateralSupported(nativeWrapped),
            'collateral-not-supported'
        );

        Vault(_vault).removeCollateral(nativeWrapped, _amount, address(this));

        collateral[nativeWrapped] -= _amount;

        IWETH(nativeWrapped).withdraw(_amount);
        (bool success, ) = payable(_to).call{value: _amount}("");
        require(success, 'transfer-failed');
    }

    /**
     * @dev Adds a specific collateral to a vault.
     * @param _vault The address of the vault to add collateral.
     * @param _collateral The address of the collateral token to add.
     * @param _amount The amount of collateral to add.
     */
    function addCollateral(
        address _vault,
        address _collateral,
        uint256 _amount
    ) external {
        require(containsVault(_vault), 'vault-not-found');
        require(
            _isCollateralSupported(_collateral),
            'collateral-not-supported'
        );

        collateral[_collateral] += _amount;

        require(
            collateral[_collateral] <= collateralCap[_collateral],
            'collateral-cap-reached'
        );

        IERC20(_collateral).safeTransferFrom(_msgSender(), _vault, _amount);
        Vault(_vault).addCollateral(_collateral, _amount);
    }

    /**
     * @dev Removes a specific collateral from a vault.
     * @param _vault The address of the vault to remove collateral.
     * @param _collateral The address of the collateral token to remove.
     * @param _amount The amount of collateral to remove.
     * @param _to The address where the removed collateral is transferred.
     */
    function removeCollateral(
        address _vault,
        address _collateral,
        uint256 _amount,
        address _to
    ) external onlyVaultOwner(_vault) {
        require(containsVault(_vault), 'vault-not-found');
        require(
            _isCollateralSupported(_collateral),
            'collateral-not-supported'
        );

        collateral[_collateral] -= _amount;
        Vault(_vault).removeCollateral(_collateral, _amount, _to);
    }

    /**
     * @dev Borrows funds from a vault by its owner or an operator.
     * @param _vault The address of the vault from which funds are borrowed.
     * @param _amount The amount of funds to borrow.
     * @param _to The address where borrowed funds are sent.
     */
    function borrow(
        address _vault,
        uint256 _amount,
        address _to
    ) external onlyVaultOwnerOrOperator(_vault) {
        require(containsVault(_vault), 'vault-not-found');
        require(_to != address(0x0), 'to-is-0');

        totalDebt += _amount;
        _updateDebtWindow(_amount);
        Vault(_vault).borrow(_amount);
        uint256 _borrowRate = IVaultBorrowRate(borrowRate).getBorrowRate(
            _vault
        );
        uint256 _feeAmount = (_amount * _borrowRate) / DECIMAL_PRECISION;

        mintableTokenOwner.mint(_to, _amount - _feeAmount);
        mintableTokenOwner.mint(borrowFeeRecipient, _feeAmount);
    }

    /**
     * @dev Distributes bad debt to a specific vault.
     * @param _vault The address of the vault to distribute bad debt.
     * @param _amount The amount of bad debt to be distributed.
     */
    function distributeBadDebt(
        address _vault,
        uint256 _amount
    ) external nonReentrant onlyLiquidationRouter {
        require(containsVault(_vault), 'vault-not-found');
        totalDebt += _amount;
        Vault(_vault).addBadDebt(_amount);
    }

    /**
     * @dev Closes a vault if it meets specific conditions.
     * @param _vault The address of the vault to close.
     */
    function closeVault(address _vault) external onlyVaultOwner(_vault) {
        require(containsVault(_vault), 'vault-not-found');
        require(Vault(_vault).debt() == 0, 'debt-not-0');
        require(Vault(_vault).collateralsLength() == 0, 'collateral-not-0');

        _removeVault(_msgSender(), _vault);
    }

    /**
     * @dev Repays borrowed funds for a specific vault.
     * @param _vault The address of the vault for which funds are repaid.
     * @param _amount The amount of funds to repay.
     */
    function repay(address _vault, uint256 _amount) external onlyVaultOwner(_vault) {
        require(containsVault(_vault), 'vault-not-found');
        totalDebt -= _amount;
        Vault(_vault).repay(_amount);

        IMintableToken(stable).safeTransferFrom(
            _msgSender(),
            address(this),
            _amount
        );
        IMintableToken(stable).burn(_amount);
    }

    /**
     * @dev Redeems collateral from a vault after meeting specific conditions.
     * @param _vault The address of the vault from which collateral is redeemed.
     * @param _collateral The address of the collateral token to redeem.
     * @param _collateralAmount The amount of collateral to redeem.
     * @param _to The address where the redeemed collateral is transferred.
     */
    function redeem(
        address _vault,
        address _collateral,
        uint256 _collateralAmount,
        address _to
    ) external nonReentrant {
        require(publicRedemptions || isAddressRedemptionAllowed[_msgSender()], 'redemption-not-allowed');
        require(containsVault(_vault), 'vault-not-found');
        require(_to != address(0x0), 'to-is-0');

        require(isReedemable(_vault, _collateral), 'not-redeemable');

        (uint256 _debtRepaid, uint256 _feeCollected) = Vault(_vault).redeem(
            _collateral,
            _collateralAmount
        );

        totalDebt -= _debtRepaid;
        collateral[_collateral] -= _collateralAmount;

        IMintableToken(stable).safeTransferFrom(
            _msgSender(),
            address(this),
            _debtRepaid + _feeCollected
        );
        IMintableToken(stable).burn(_debtRepaid);
        IMintableToken(stable).transfer(redemptionFeeRecipient, _feeCollected);

        IERC20(_collateral).safeTransfer(_to, _collateralAmount);
    }

    /**
     * @dev Liquidates a specific vault if it is eligible for liquidation.
     * @param _vault The address of the vault to be liquidated.
     */
    function liquidate(address _vault) external nonReentrant {
        require(containsVault(_vault), 'vault-not-found');

        address _vaultOwner = Vault(_vault).vaultOwner();
        uint256 _forgivenDebt = Vault(_vault).liquidate();

        totalDebt -= _forgivenDebt;

        _removeVault(_vaultOwner, _vault);
    }

    /**
     * @dev Checks if a vault is eligible for liquidation.
     * @param _vault The address of the vault to check for liquidation eligibility.
     * @return A boolean indicating whether the vault is liquidatable.
     */
    function isLiquidatable(address _vault) external view returns (bool) {
        require(containsVault(_vault), 'vault-not-found');
        return Vault(_vault).healthFactor(true) < DECIMAL_PRECISION;
    }

    /**
     * @dev Checks if a specific collateral can be redeemed from a vault based on conditions.
     * @param _vault The address of the vault to check for collateral redemption.
     * @param _collateral The address of the collateral token to check for redemption.
     * @notice Collateral with higher MCR can be redeemed first
     * @return A boolean indicating whether the collateral is redeemable.
     */
    function isReedemable(
        address _vault,
        address _collateral
    ) public view returns (bool) {
        require(
            _isCollateralSupported(_collateral),
            'collateral-not-supported'
        );
        if (!Vault(_vault).containsCollateral(_collateral)) {
            return false;
        }
        uint256 _healthFactor = Vault(_vault).healthFactor(false);
        if (_healthFactor >= redemptionHealthFactorLimit) {
            return false;
        }

        ITokenPriceFeed _priceFeed = ITokenPriceFeed(priceFeed);
        uint256 _collateralMcr = _priceFeed.mcr(_collateral);

        address[] memory _collaterals = Vault(_vault).collaterals();
        uint256 _length = _collaterals.length;

        for (uint256 i; i < _length; i++) {
            if (_collaterals[i] != _collateral) {
                uint256 _mcr = _priceFeed.mcr(_collaterals[i]);
                if (_mcr > _collateralMcr) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * @dev Updates the debt window with the newly incurred debt.
     * @param _newDebt The amount of new debt to update in the debt window.
     */
    function _updateDebtWindow(uint256 _newDebt) internal {
        require(totalDebt <= debtCeiling, 'debt-ceiling-reached');

        if (block.timestamp > lastDebtWindow + debtWindowSize) {
            debtWindowAmount = _newDebt;
            lastDebtWindow = block.timestamp;
        } else {
            debtWindowAmount += _newDebt;
        }
        require(
            debtWindowAmount <= maxDebtPerWindow,
            'debt-window-amount-reached'
        );
    }
}
constants.sol 27 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;

/**
 * @title Constants
 * @dev This contract defines various constants used within the system.
 */
contract Constants {
    // Precision used for decimal calculations
    uint256 public constant DECIMAL_PRECISION = 1e18;

    // Reserve required for liquidation purposes
    uint256 public constant LIQUIDATION_RESERVE = 1e18;

    // Maximum value for uint256
    uint256 public constant MAX_INT = 2 ** 256 - 1;

    // Constants for percentage calculations
    uint256 public constant PERCENT = (DECIMAL_PRECISION * 1) / 100; // Represents 1%
    uint256 public constant PERCENT10 = PERCENT * 10; // Represents 10%
    uint256 public constant PERCENT_05 = PERCENT / 2; // Represents 0.5%

    // Maximum borrowing and redemption rates
    uint256 public constant MAX_BORROWING_RATE = (DECIMAL_PRECISION * 5) / 100; // Represents 5%
    uint256 public constant MAX_REDEMPTION_RATE =
        (DECIMAL_PRECISION * 10) / 100; // Represents 10%
}
VaultFactoryList.sol 98 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;
import './utils/constants.sol';
import './utils/linked-address-list.sol';
// import openzeppelin context
import '@openzeppelin/contracts/utils/Context.sol';
import '@openzeppelin/contracts/utils/structs/EnumerableSet.sol';

/**
 * @title VaultFactoryList
 * @dev Manages a list of vaults by their owners, allowing addition, removal, and transfer of vaults.
 */
abstract contract VaultFactoryList is Context {
    using LinkedAddressList for LinkedAddressList.List;
    using EnumerableSet for EnumerableSet.AddressSet;

    LinkedAddressList.List _vaults;
    mapping(address => EnumerableSet.AddressSet) private _vaultsByOwner;

    function vaultsByOwnerLength(
        address _owner
    ) external view returns (uint256) {
        return _vaultsByOwner[_owner].length();
    }

    function vaultsByOwner(
        address _owner,
        uint256 _index
    ) external view returns (address) {
        return _vaultsByOwner[_owner].at(_index);
    }

    function _addVault(address _owner, address _vault) internal {
        require(
            _vaults.add(_vault, address(0x0), false),
            'vault-could-not-be-added'
        );
        _vaultsByOwner[_owner].add(_vault);
    }

    function _transferVault(
        address _from,
        address _to,
        address _vault
    ) internal {
        _vaultsByOwner[_from].remove(_vault);
        _vaultsByOwner[_to].add(_vault);
    }

    function _removeVault(address _owner, address _vault) internal {
        require(_vaults.remove(_vault), 'vault-could-not-be-removed');
        _vaultsByOwner[_owner].remove(_vault);
    }

    /**
     * @dev returns the number of vaults for specific token
     */
    function vaultCount() public view returns (uint256) {
        return _vaults._size;
    }

    /**
     * @dev returns the last vault by maximum collaterization ratio
     */
    function lastVault() public view returns (address) {
        return _vaults._last;
    }

    /**
     * @dev returns the first vault by minimal collaterization ratio
     */
    function firstVault() public view returns (address) {
        return _vaults._first;
    }

    /**
     * @dev returns the next vault by collaterization ratio
     */
    function nextVault(address _vault) public view returns (address) {
        return _vaults._values[_vault].next;
    }

    /**
     * @dev returns the previous vault by collaterization ratio
     */
    function prevVault(address _vault) public view returns (address) {
        return _vaults._values[_vault].prev;
    }

    /**
     * @dev Checks if a vault exists for a specific token.
     * @param _vault The address of the vault to check.
     * @return A boolean indicating whether the vault exists.
     */
    function containsVault(address _vault) public view returns (bool) {
        return _vaults._values[_vault].next != address(0x0);
    }
}
IWETH.sol 12 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;

interface IWETH {
    function deposit() external payable;

    function approve(address, uint256) external returns (bool);

    function transfer(address _to, uint256 _value) external returns (bool);

    function withdraw(uint256) external;
}
VaultFactoryConfig.sol 255 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;
import './utils/constants.sol';
import '@openzeppelin/contracts/access/Ownable.sol';

abstract contract VaultFactoryConfig is Constants, Ownable {
    event PriceFeedUpdated(address indexed priceFeed);
    event MaxTokensPerVaultUpdated(
        uint256 oldMaxTokensPerVault,
        uint256 newMaxTokensPerVault
    );
    event RedemptionRateUpdated(
        uint256 oldRedemptionRate,
        uint256 newRedemptionRate
    );
    event BorrowRateUpdated(address oldBorrowRate, address newBorrowRate);
    event RedemptionHealthFactorLimitUpdated(
        uint256 oldRedemptionHealthFactorLimit,
        uint256 newRedemptionHealthFactorLimit
    );
    event DebtCeilingUpdated(uint256 oldDebtCeiling, uint256 newDebtCeiling);
    event MaxDebtPerWindowUpdated(
        uint256 oldMaxDebtPerWindow,
        uint256 newMaxDebtPerWindow
    );
    event DebtWindowSizeUpdated(
        uint256 oldDebtWindowSize,
        uint256 newDebtWindowSize
    );
    event CollateralCapacityUpdated(
        address indexed collateral,
        uint256 oldCapacity,
        uint256 newCapacity
    );
    event liquidationRouterUpdated(address indexed liquidationRouter);

    event publicRedemptionsUpdated(bool publicRedemptions);

    // Various configuration parameters
    address public priceFeed;
    address public borrowRate;

    uint256 public MAX_TOKENS_PER_VAULT = 5;
    uint256 public redemptionRate = PERCENT_05; // 0.5%

    uint256 public redemptionHealthFactorLimit = 1.5 ether; // 1.5 HF

    address public borrowFeeRecipient;
    address public redemptionFeeRecipient;

    mapping(address => uint256) public collateralCap;

    uint256 public debtCeiling = type(uint256).max; // max stablecoin debt issued by the protocol

    uint256 public maxDebtPerWindow = 200_000 ether; // 200K
    uint256 public debtWindowSize = 1 hours;
    uint256 public lastDebtWindow;
    uint256 public debtWindowAmount;

    address public vaultDeployer;
    address public liquidationRouter;

    bool public publicRedemptions;

    mapping(address => bool) public isAddressRedemptionAllowed;

    /**
     * @dev Set the public redemptions flag
     * @param _publicRedemptions The new public redemptions flag to be set.
     */
    function setPublicRedemptions(bool _publicRedemptions) external onlyOwner {
        publicRedemptions = _publicRedemptions;
        emit publicRedemptionsUpdated(_publicRedemptions);
    }

    /**
     * @dev Set the redemption allowed for a specific address
     * @param _address Address of the account
     * @param _allowed The new redemption allowed flag to be set.
     */
    function setRedemptionAllowed(address _address, bool _allowed)
        external
        onlyOwner
    {
        isAddressRedemptionAllowed[_address] = _allowed;
    }

    /**
     * @dev Set the address for the Vault Deployer
     * @param _vaultDeployer Address of the Vault Deployer
     */
    function setVaultDeployer(address _vaultDeployer) external onlyOwner {
        require(_vaultDeployer != address(0x0), 'vault-deployer-is-0');
        vaultDeployer = _vaultDeployer;
    }

    /**
     * @dev Set the address for the Liquidation Router
     * @param _liquidationRouter Address of the Liquidation Router
     */
    function setLiquidationRouter(
        address _liquidationRouter
    ) external onlyOwner {
        require(_liquidationRouter != address(0x0), 'liquidation-router-is-0');
        liquidationRouter = _liquidationRouter;
        emit liquidationRouterUpdated(_liquidationRouter);
    }

    /**
     * @dev Set the collateral capacity for a specific collateral token
     * @param _collateral Address of the collateral token
     * @param _cap The new capacity for the collateral token
     */
    function setCollateralCapacity(
        address _collateral,
        uint256 _cap
    ) external onlyOwner {
        require(_collateral != address(0x0), 'collateral-is-0');
        emit CollateralCapacityUpdated(
            _collateral,
            collateralCap[_collateral],
            _cap
        );
        collateralCap[_collateral] = _cap;
    }

    /**
     * @dev Set the debt ceiling value.
     * @param _debtCeiling The new debt ceiling value to be set.
     */
    function setDebtCeiling(uint256 _debtCeiling) external onlyOwner {
        emit DebtCeilingUpdated(debtCeiling, _debtCeiling);
        debtCeiling = _debtCeiling;
    }

    /**
     * @dev Set the maximum debt allowed per window.
     * @param _maxDebtPerWindow The new maximum debt per window value to be set.
     */
    function setMaxDebtPerWindow(uint256 _maxDebtPerWindow) external onlyOwner {
        emit MaxDebtPerWindowUpdated(maxDebtPerWindow, _maxDebtPerWindow);
        maxDebtPerWindow = _maxDebtPerWindow;
    }

    /**
     * @dev Set the window size for debt.
     * @param _debtWindowSize The new debt window size value to be set.
     */
    function setDebtWindowSize(uint256 _debtWindowSize) external onlyOwner {
        emit DebtWindowSizeUpdated(debtWindowSize, _debtWindowSize);
        debtWindowSize = _debtWindowSize;
    }

    /**
     * @dev Set the maximum tokens allowed per vault.
     * @param _maxTokensPerVault The new maximum tokens per vault value to be set.
     */
    function setMaxTokensPerVault(
        uint256 _maxTokensPerVault
    ) external onlyOwner {
        require(_maxTokensPerVault > 0, 'max-tokens-per-vault-is-0');
        emit MaxTokensPerVaultUpdated(MAX_TOKENS_PER_VAULT, _maxTokensPerVault);
        MAX_TOKENS_PER_VAULT = _maxTokensPerVault;
    }

    /**
     * @dev Set the address for the price feed.
     * @param _priceFeed Address of the new price feed contract.
     */
    function setPriceFeed(address _priceFeed) external onlyOwner {
        require(_priceFeed != address(0x0), 'pricefeed-is-0');
        priceFeed = _priceFeed;
        emit PriceFeedUpdated(_priceFeed);
    }

    /**
     * @dev Set the redemption rate for the protocol.
     * @param _redemptionRate The new redemption rate value to be set.
     */
    function setRedemptionRate(uint256 _redemptionRate) external onlyOwner {
        require(
            _redemptionRate <= MAX_REDEMPTION_RATE,
            'redemption-rate-too-high'
        );
        emit RedemptionRateUpdated(redemptionRate, _redemptionRate);
        redemptionRate = _redemptionRate;
    }

    /**
     * @dev Set the address for the borrow rate.
     * @param _borrowRate Address of the new borrow rate contract.
     */
    function setBorrowRate(address _borrowRate) external onlyOwner {
        require(_borrowRate != address(0), 'borrow-rate-is-0');
        emit BorrowRateUpdated(borrowRate, _borrowRate);
        borrowRate = _borrowRate;
    }

    /**
     * @dev Set the redemption health factor limit.
     * @param _redemptionHealthFactorLimit The new redemption health factor limit to be set.
     */
    function setRedemptionHealthFactorLimit(
        uint256 _redemptionHealthFactorLimit
    ) external onlyOwner {
        emit RedemptionHealthFactorLimitUpdated(
            redemptionHealthFactorLimit,
            _redemptionHealthFactorLimit
        );
        redemptionHealthFactorLimit = _redemptionHealthFactorLimit;
    }

    /**
     * @dev Set the address for the borrow fee recipient.
     * @param _borrowFeeRecipient Address of the new borrow fee recipient.
     */
    function setBorrowFeeRecipient(
        address _borrowFeeRecipient
    ) external onlyOwner {
        require(
            _borrowFeeRecipient != address(0x0),
            'borrow-fee-recipient-is-0'
        );
        borrowFeeRecipient = _borrowFeeRecipient;
    }

    /**
     * @dev Set the address for the redemption fee recipient.
     * @param _redemptionFeeRecipient Address of the new redemption fee recipient.
     */
    function setRedemptionFeeRecipient(
        address _redemptionFeeRecipient
    ) external onlyOwner {
        require(
            _redemptionFeeRecipient != address(0x0),
            'redemption-fee-recipient-is-0'
        );
        redemptionFeeRecipient = _redemptionFeeRecipient;
    }

    /**
     * @dev Constructor to initialize the configuration settings upon deployment
     * @param _vaultDeployer Address of the Vault Deployer
     * @param _liquidationRouter Address of the Liquidation Router
     */
    constructor(address _vaultDeployer, address _liquidationRouter) {
        require(_vaultDeployer != address(0x0), 'vault-deployer-is-0');
        require(_liquidationRouter != address(0x0), 'liquidation-factory-is-0');
        vaultDeployer = _vaultDeployer;
        borrowFeeRecipient = _msgSender();
        redemptionFeeRecipient = _msgSender();
        lastDebtWindow = block.timestamp;
        liquidationRouter = _liquidationRouter;
    }
}
IOwnable.sol 15 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;

interface IOwnable {
    /**
     * @dev Returns the address of the current owner.
     */
    function owner() external view returns (address);

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) external;
}
IPriceFeed.sol 14 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;

interface IPriceFeed {
    function token() external view returns (address);

    function price() external view returns (uint256);

    function pricePoint() external view returns (uint256);

    function emitPriceSignal() external;

    event PriceUpdate(address token, uint256 price, uint256 average);
}
IVaultFactory.sol 61 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;

interface IVaultFactory {
    event NewVault(address indexed vault, string name, address indexed owner);
    event PriceFeedUpdated(address indexed priceFeed);

    function setPriceFeed(address _priceFeed) external;
    function vaultCount() external view returns (uint256);
    function lastVault() external view returns (address);
    function firstVault() external view returns (address);
    function nextVault(address _vault) external view returns (address);
    function prevVault(address _vault) external view returns (address);
    function liquidationRouter() external view returns (address);
    function MAX_TOKENS_PER_VAULT() external view returns (uint256);
    function priceFeed() external view returns (address);
    function transferVaultOwnership(address _vault, address _newOwner) external;
    function createVault(string memory _name) external returns (address);
    function addCollateralNative(address _vault) external payable;
    function removeCollateralNative(
        address _vault,
        uint256 _amount,
        address _to
    ) external;
    function addCollateral(
        address _vault,
        address _collateral,
        uint256 _amount
    ) external;
    function removeCollateral(
        address _vault,
        address _collateral,
        uint256 _amount,
        address _to
    ) external;
    function borrow(address _vault, uint256 _amount, address _to) external;
    function distributeBadDebt(address _vault, uint256 _amount) external;
    function closeVault(address _vault) external;
    function repay(address _vault, uint256 _amount) external;
    function redeem(
        address _vault,
        address _collateral,
        uint256 _collateralAmount,
        address _to
    ) external;
    function liquidate(address _vault) external;
    function isLiquidatable(address _vault) external view returns (bool);
    function isReedemable(
        address _vault,
        address _collateral
    ) external view returns (bool);
    function containsVault(address _vault) external view returns (bool);
    function stable() external view returns (address);
    function isCollateralSupported(
        address _collateral
    ) external view returns (bool);
    function vaultsByOwnerLength(
        address _owner
    ) external view returns (uint256);
    function redemptionHealthFactorLimit() external view returns (uint256);
}
IMintableToken.sol 20 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;

import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import './IOwnable.sol';

interface IMintableToken is IERC20, IOwnable {
    function mint(address recipient, uint256 amount) external;

    function burn(uint256 amount) external;

    function name() external view returns (string memory);

    function symbol() external view returns (string memory);

    function approve(
        address spender,
        uint256 amount
    ) external override returns (bool);
}
IVaultDeployer.sol 10 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;

interface IVaultDeployer {
    function deployVault(
        address _factory,
        address _vaultOwner,
        string memory _name
    ) external returns (address);
}
linked-address-list.sol 134 lines
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.21;

/**
 * @title LinkedAddressList
 * @dev Library implementing a linked list structure to store and operate sorted Troves.
 */
library LinkedAddressList {
    struct EntryLink {
        address prev;
        address next;
    }

    struct List {
        address _last;
        address _first;
        uint256 _size;
        mapping(address => EntryLink) _values;
    }

    /**
     * @dev Adds an element to the linked list.
     * @param _list The storage pointer to the linked list.
     * @param _element The element to be added.
     * @param _reference The reference element to determine the position for addition.
     * @param _before A boolean indicating whether to add the element before the reference.
     * @return A boolean indicating the success of the addition.
     */
    function add(
        List storage _list,
        address _element,
        address _reference,
        bool _before
    ) internal returns (bool) {
        require(
            _reference == address(0x0) ||
                _list._values[_reference].next != address(0x0),
            '79d3d _ref neither valid nor 0x'
        );

        // Element must not exist to be added
        EntryLink storage element_values = _list._values[_element];
        if (element_values.prev == address(0x0)) {
            if (_list._last == address(0x0)) {
                // If the list is empty, set the element as both first and last
                element_values.prev = _element;
                element_values.next = _element;
                _list._first = _element;
                _list._last = _element;
            } else {
                if (
                    _before &&
                    (_reference == address(0x0) || _reference == _list._first)
                ) {
                    // Adding the element as the first element
                    address first = _list._first;
                    _list._values[first].prev = _element;
                    element_values.prev = _element;
                    element_values.next = first;
                    _list._first = _element;
                } else if (
                    !_before &&
                    (_reference == address(0x0) || _reference == _list._last)
                ) {
                    // Adding the element as the last element
                    address last = _list._last;
                    _list._values[last].next = _element;
                    element_values.prev = last;
                    element_values.next = _element;
                    _list._last = _element;
                } else {
                    // Inserting the element between two elements
                    EntryLink memory ref = _list._values[_reference];
                    if (_before) {
                        element_values.prev = ref.prev;
                        element_values.next = _reference;
                        _list._values[_reference].prev = _element;
                        _list._values[ref.prev].next = _element;
                    } else {
                        element_values.prev = _reference;
                        element_values.next = ref.next;
                        _list._values[_reference].next = _element;
                        _list._values[ref.next].prev = _element;
                    }
                }
            }
            _list._size = _list._size + 1;
            return true;
        }
        return false;
    }

    /**
     * @dev Removes an element from the linked list.
     * @param _list The storage pointer to the linked list.
     * @param _element The element to be removed.
     * @return A boolean indicating the success of the removal.
     */
    function remove(
        List storage _list,
        address _element
    ) internal returns (bool) {
        EntryLink memory element_values = _list._values[_element];
        if (element_values.next != address(0x0)) {
            if (_element == _list._last && _element == _list._first) {
                // Removing the last and only element in the list
                delete _list._last;
                delete _list._first;
            } else if (_element == _list._first) {
                // Removing the first element
                address next = element_values.next;
                _list._values[next].prev = next;
                _list._first = next;
            } else if (_element == _list._last) {
                // Removing the last element
                address new_list_last = element_values.prev;
                _list._last = new_list_last;
                _list._values[new_list_last].next = new_list_last;
            } else {
                // Removing an element in between two other elements
                address next = element_values.next;
                address prev = element_values.prev;
                _list._values[next].prev = prev;
                _list._values[prev].next = next;
            }
            // Delete the element itself
            delete _list._values[_element];
            _list._size = _list._size - 1;
            return true;
        }
        return false;
    }
}
ITokenPriceFeed.sol 46 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;

import './IOwnable.sol';

interface ITokenPriceFeed is IOwnable {
    struct TokenInfo {
        address priceFeed;
        uint256 mcr; // Minimum Collateralization Ratio
        uint256 mlr; // Minimum Liquidation Ratio
        uint256 borrowRate;
        uint256 decimals;
    }

    function tokenPriceFeed(address) external view returns (address);

    function tokenPrice(address _token) external view returns (uint256);

    function mcr(address _token) external view returns (uint256);

    function decimals(address _token) external view returns (uint256);

    function mlr(address _token) external view returns (uint256);

    function borrowRate(address _token) external view returns (uint256);

    function setTokenPriceFeed(
        address _token,
        address _priceFeed,
        uint256 _mcr,
        uint256 _mlr,
        uint256 _borrowRate,
        uint256 /* _decimals */
    ) external;

    event NewTokenPriceFeed(
        address _token,
        address _priceFeed,
        string _name,
        string _symbol,
        uint256 _mcr,
        uint256 _mlr,
        uint256 _borrowRate,
        uint256 _decimals
    );
}
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();
        }
    }
}
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;
    }
}
IVaultBorrowRate.sol 8 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;

interface IVaultBorrowRate {
    function getBorrowRate(
        address _vaultAddress
    ) external view returns (uint256);
}
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);
    }
}
ILiquidationRouter.sol 24 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;

interface ILiquidationRouter {
    function addSeizedCollateral(address _collateral, uint256 _amount) external;

    function addUnderWaterDebt(address _vault, uint256 _amount) external;

    function removeUnderWaterDebt(uint256 _amount) external;

    function underWaterDebt() external view returns (uint256);

    function collaterals() external view returns (address[] memory);

    function collateral(address _collateral) external view returns (uint256);

    function tryLiquidate() external;

    function stabilityPool() external view returns (address);
    function auctionManager() external view returns (address);
    function lastResortLiquidation() external view returns (address);
    function distributeBadDebt(address _vault, uint256 _amount) external;
    function transferOwnership(address newOwner) external;
}
IMintableTokenOwner.sol 18 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;

import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import './IOwnable.sol';
import './IMintableToken.sol';

interface IMintableTokenOwner is IOwnable {
    function token() external view returns (IMintableToken);

    function mint(address _recipient, uint256 _amount) external;

    function transferTokenOwnership(address _newOwner) external;

    function addMinter(address _newMinter) external;

    function revokeMinter(address _minter) external;
}
IVaultExtraSettings.sol 19 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;

interface IVaultExtraSettings {
    function setMaxRedeemablePercentage(
        uint256 _debtTreshold,
        uint256 _maxRedeemablePercentage
    ) external;
    function setRedemptionKickback(uint256 _redemptionKickback) external;

    function getExtraSettings()
        external
        view
        returns (
            uint256 _debtTreshold,
            uint256 _maxRedeemablePercentage,
            uint256 _redemptionKickback
        );
}
IVaultFactoryConfig.sol 39 lines
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;

interface IVaultFactoryConfig {
    event PriceFeedUpdated(address indexed priceFeed);
    event MaxTokensPerVaultUpdated(
        uint256 oldMaxTokensPerVault,
        uint256 newMaxTokensPerVault
    );
    event RedemptionRateUpdated(
        uint256 oldRedemptionRate,
        uint256 newRedemptionRate
    );
    event BorrowRateUpdated(uint256 oldBorrowRate, uint256 newBorrowRate);
    event RedemptionHealthFactorLimitUpdated(
        uint256 oldRedemptionHealthFactorLimit,
        uint256 newRedemptionHealthFactorLimit
    );

    function setMaxTokensPerVault(uint256 _maxTokensPerVault) external;
    function setPriceFeed(address _priceFeed) external;
    function setRedemptionRate(uint256 _redemptionRate) external;
    function setBorrowRate(uint256 _borrowRate) external;
    function setRedemptionHealthFactorLimit(
        uint256 _redemptionHealthFactorLimit
    ) external;
    function setBorrowFeeRecipient(address _borrowFeeRecipient) external;
    function setRedemptionFeeRecipient(
        address _redemptionFeeRecipient
    ) external;

    function priceFeed() external view returns (address);
    function MAX_TOKENS_PER_VAULT() external view returns (uint256);
    function redemptionRate() external view returns (uint256);
    function borrowRate() external view returns (uint256);
    function redemptionHealthFactorLimit() external view returns (uint256);
    function borrowFeeRecipient() external view returns (address);
    function redemptionFeeRecipient() external view returns (address);
}
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);
}
ReentrancyGuard.sol 84 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/ReentrancyGuard.sol)

pragma solidity ^0.8.20;

/**
 * @dev Contract module that helps prevent reentrant calls to a function.
 *
 * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
 * available, which can be applied to functions to make sure there are no nested
 * (reentrant) calls to them.
 *
 * Note that because there is a single `nonReentrant` guard, functions marked as
 * `nonReentrant` may not call one another. This can be worked around by making
 * those functions `private`, and then adding `external` `nonReentrant` entry
 * points to them.
 *
 * TIP: If you would like to learn more about reentrancy and alternative ways
 * to protect against it, check out our blog post
 * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
 */
abstract contract ReentrancyGuard {
    // Booleans are more expensive than uint256 or any type that takes up a full
    // word because each write operation emits an extra SLOAD to first read the
    // slot's contents, replace the bits taken up by the boolean, and then write
    // back. This is the compiler's defense against contract upgrades and
    // pointer aliasing, and it cannot be disabled.

    // The values being non-zero value makes deployment a bit more expensive,
    // but in exchange the refund on every call to nonReentrant will be lower in
    // amount. Since refunds are capped to a percentage of the total
    // transaction's gas, it is best to keep them low in cases like this one, to
    // increase the likelihood of the full refund coming into effect.
    uint256 private constant NOT_ENTERED = 1;
    uint256 private constant ENTERED = 2;

    uint256 private _status;

    /**
     * @dev Unauthorized reentrant call.
     */
    error ReentrancyGuardReentrantCall();

    constructor() {
        _status = NOT_ENTERED;
    }

    /**
     * @dev Prevents a contract from calling itself, directly or indirectly.
     * Calling a `nonReentrant` function from another `nonReentrant`
     * function is not supported. It is possible to prevent this from happening
     * by making the `nonReentrant` function external, and making it call a
     * `private` function that does the actual work.
     */
    modifier nonReentrant() {
        _nonReentrantBefore();
        _;
        _nonReentrantAfter();
    }

    function _nonReentrantBefore() private {
        // On the first call to nonReentrant, _status will be NOT_ENTERED
        if (_status == ENTERED) {
            revert ReentrancyGuardReentrantCall();
        }

        // Any calls to nonReentrant after this point will fail
        _status = ENTERED;
    }

    function _nonReentrantAfter() private {
        // By storing the original value once again, a refund is triggered (see
        // https://eips.ethereum.org/EIPS/eip-2200)
        _status = NOT_ENTERED;
    }

    /**
     * @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
     * `nonReentrant` function in the call stack.
     */
    function _reentrancyGuardEntered() internal view returns (bool) {
        return _status == ENTERED;
    }
}
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";
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;
    }
}
EnumerableSet.sol 378 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/structs/EnumerableSet.sol)
// This file was procedurally generated from scripts/generate/templates/EnumerableSet.js.

pragma solidity ^0.8.20;

/**
 * @dev Library for managing
 * https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets] of primitive
 * types.
 *
 * Sets have the following properties:
 *
 * - Elements are added, removed, and checked for existence in constant time
 * (O(1)).
 * - Elements are enumerated in O(n). No guarantees are made on the ordering.
 *
 * ```solidity
 * contract Example {
 *     // Add the library methods
 *     using EnumerableSet for EnumerableSet.AddressSet;
 *
 *     // Declare a set state variable
 *     EnumerableSet.AddressSet private mySet;
 * }
 * ```
 *
 * As of v3.3.0, sets of type `bytes32` (`Bytes32Set`), `address` (`AddressSet`)
 * and `uint256` (`UintSet`) are supported.
 *
 * [WARNING]
 * ====
 * Trying to delete such a structure from storage will likely result in data corruption, rendering the structure
 * unusable.
 * See https://github.com/ethereum/solidity/pull/11843[ethereum/solidity#11843] for more info.
 *
 * In order to clean an EnumerableSet, you can either remove all elements one by one or create a fresh instance using an
 * array of EnumerableSet.
 * ====
 */
library EnumerableSet {
    // To implement this library for multiple types with as little code
    // repetition as possible, we write it in terms of a generic Set type with
    // bytes32 values.
    // The Set implementation uses private functions, and user-facing
    // implementations (such as AddressSet) are just wrappers around the
    // underlying Set.
    // This means that we can only create new EnumerableSets for types that fit
    // in bytes32.

    struct Set {
        // Storage of set values
        bytes32[] _values;
        // Position is the index of the value in the `values` array plus 1.
        // Position 0 is used to mean a value is not in the set.
        mapping(bytes32 value => uint256) _positions;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function _add(Set storage set, bytes32 value) private returns (bool) {
        if (!_contains(set, value)) {
            set._values.push(value);
            // The value is stored at length-1, but we add 1 to all indexes
            // and use 0 as a sentinel value
            set._positions[value] = set._values.length;
            return true;
        } else {
            return false;
        }
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function _remove(Set storage set, bytes32 value) private returns (bool) {
        // We cache the value's position to prevent multiple reads from the same storage slot
        uint256 position = set._positions[value];

        if (position != 0) {
            // Equivalent to contains(set, value)
            // To delete an element from the _values array in O(1), we swap the element to delete with the last one in
            // the array, and then remove the last element (sometimes called as 'swap and pop').
            // This modifies the order of the array, as noted in {at}.

            uint256 valueIndex = position - 1;
            uint256 lastIndex = set._values.length - 1;

            if (valueIndex != lastIndex) {
                bytes32 lastValue = set._values[lastIndex];

                // Move the lastValue to the index where the value to delete is
                set._values[valueIndex] = lastValue;
                // Update the tracked position of the lastValue (that was just moved)
                set._positions[lastValue] = position;
            }

            // Delete the slot where the moved value was stored
            set._values.pop();

            // Delete the tracked position for the deleted slot
            delete set._positions[value];

            return true;
        } else {
            return false;
        }
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function _contains(Set storage set, bytes32 value) private view returns (bool) {
        return set._positions[value] != 0;
    }

    /**
     * @dev Returns the number of values on the set. O(1).
     */
    function _length(Set storage set) private view returns (uint256) {
        return set._values.length;
    }

    /**
     * @dev Returns the value stored at position `index` in the set. O(1).
     *
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function _at(Set storage set, uint256 index) private view returns (bytes32) {
        return set._values[index];
    }

    /**
     * @dev Return the entire set in an array
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function _values(Set storage set) private view returns (bytes32[] memory) {
        return set._values;
    }

    // Bytes32Set

    struct Bytes32Set {
        Set _inner;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function add(Bytes32Set storage set, bytes32 value) internal returns (bool) {
        return _add(set._inner, value);
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function remove(Bytes32Set storage set, bytes32 value) internal returns (bool) {
        return _remove(set._inner, value);
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function contains(Bytes32Set storage set, bytes32 value) internal view returns (bool) {
        return _contains(set._inner, value);
    }

    /**
     * @dev Returns the number of values in the set. O(1).
     */
    function length(Bytes32Set storage set) internal view returns (uint256) {
        return _length(set._inner);
    }

    /**
     * @dev Returns the value stored at position `index` in the set. O(1).
     *
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function at(Bytes32Set storage set, uint256 index) internal view returns (bytes32) {
        return _at(set._inner, index);
    }

    /**
     * @dev Return the entire set in an array
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function values(Bytes32Set storage set) internal view returns (bytes32[] memory) {
        bytes32[] memory store = _values(set._inner);
        bytes32[] memory result;

        /// @solidity memory-safe-assembly
        assembly {
            result := store
        }

        return result;
    }

    // AddressSet

    struct AddressSet {
        Set _inner;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function add(AddressSet storage set, address value) internal returns (bool) {
        return _add(set._inner, bytes32(uint256(uint160(value))));
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function remove(AddressSet storage set, address value) internal returns (bool) {
        return _remove(set._inner, bytes32(uint256(uint160(value))));
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function contains(AddressSet storage set, address value) internal view returns (bool) {
        return _contains(set._inner, bytes32(uint256(uint160(value))));
    }

    /**
     * @dev Returns the number of values in the set. O(1).
     */
    function length(AddressSet storage set) internal view returns (uint256) {
        return _length(set._inner);
    }

    /**
     * @dev Returns the value stored at position `index` in the set. O(1).
     *
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function at(AddressSet storage set, uint256 index) internal view returns (address) {
        return address(uint160(uint256(_at(set._inner, index))));
    }

    /**
     * @dev Return the entire set in an array
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function values(AddressSet storage set) internal view returns (address[] memory) {
        bytes32[] memory store = _values(set._inner);
        address[] memory result;

        /// @solidity memory-safe-assembly
        assembly {
            result := store
        }

        return result;
    }

    // UintSet

    struct UintSet {
        Set _inner;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function add(UintSet storage set, uint256 value) internal returns (bool) {
        return _add(set._inner, bytes32(value));
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function remove(UintSet storage set, uint256 value) internal returns (bool) {
        return _remove(set._inner, bytes32(value));
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function contains(UintSet storage set, uint256 value) internal view returns (bool) {
        return _contains(set._inner, bytes32(value));
    }

    /**
     * @dev Returns the number of values in the set. O(1).
     */
    function length(UintSet storage set) internal view returns (uint256) {
        return _length(set._inner);
    }

    /**
     * @dev Returns the value stored at position `index` in the set. O(1).
     *
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function at(UintSet storage set, uint256 index) internal view returns (uint256) {
        return uint256(_at(set._inner, index));
    }

    /**
     * @dev Return the entire set in an array
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function values(UintSet storage set) internal view returns (uint256[] memory) {
        bytes32[] memory store = _values(set._inner);
        uint256[] memory result;

        /// @solidity memory-safe-assembly
        assembly {
            result := store
        }

        return result;
    }
}
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);
}
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);
}

Read Contract

DECIMAL_PRECISION 0xa20baee6 → uint256
LIQUIDATION_RESERVE 0x923c1eec → uint256
MAX_BORROWING_RATE 0xabdc5541 → uint256
MAX_INT 0x098d3228 → uint256
MAX_REDEMPTION_RATE 0x4bb97042 → uint256
MAX_TOKENS_PER_VAULT 0xffbfb8ad → uint256
PERCENT 0xb85a8b20 → uint256
PERCENT10 0xad09014d → uint256
PERCENT_05 0xaceb2d04 → uint256
borrowFeeRecipient 0x294c6095 → address
borrowRate 0xc914b437 → address
collateral 0xa5fdc5de → uint256
collateralCap 0x451e8739 → uint256
containsVault 0x839936c0 → bool
debtCeiling 0xe1c84ea4 → uint256
debtWindowAmount 0x8f81c12c → uint256
debtWindowSize 0x44ba8b0e → uint256
firstVault 0x96f57b6e → address
isAddressRedemptionAllowed 0x58188693 → bool
isCollateralSupported 0xfa6bd2ee → bool
isLiquidatable 0x042e02cf → bool
isReedemable 0x04aecb76 → bool
lastDebtWindow 0xd984d5b2 → uint256
lastVault 0x2a807f7c → address
liquidationRouter 0x679fda70 → address
maxDebtPerWindow 0xdd351b9a → uint256
mintableTokenOwner 0x9ce11f2d → address
nativeWrapped 0x8abca673 → address
nextVault 0x5b8b5770 → address
owner 0x8da5cb5b → address
prevVault 0xfdbb3acf → address
priceFeed 0x741bef1a → address
publicRedemptions 0x4d656faa → bool
redemptionFeeRecipient 0x94bcfdaa → address
redemptionHealthFactorLimit 0xc3971f65 → uint256
redemptionRate 0x540385a3 → uint256
stable 0x22be3de1 → address
totalDebt 0xfc7b9c18 → uint256
vaultCount 0xa7c6a100 → uint256
vaultDeployer 0x2f17e030 → address
vaultsByOwner 0x1a0121db → address
vaultsByOwnerLength 0x8ffadd0c → uint256

Write Contract 29 functions

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

addCollateral 0x59781034
address _vault
address _collateral
uint256 _amount
addCollateralNative 0x4349d081
address _vault
borrow 0x6c665a55
address _vault
uint256 _amount
address _to
closeVault 0xc99cb2b7
address _vault
createVault 0x3fe1da88
string _name
returns: address
distributeBadDebt 0xd1400f68
address _vault
uint256 _amount
liquidate 0x2f865568
address _vault
redeem 0xd9aa0473
address _vault
address _collateral
uint256 _collateralAmount
address _to
removeCollateral 0xa0609bd4
address _vault
address _collateral
uint256 _amount
address _to
removeCollateralNative 0x027d5946
address _vault
uint256 _amount
address _to
renounceOwnership 0x715018a6
No parameters
repay 0x22867d78
address _vault
uint256 _amount
setBorrowFeeRecipient 0x274b600a
address _borrowFeeRecipient
setBorrowRate 0xd8fdf917
address _borrowRate
setCollateralCapacity 0x3d913818
address _collateral
uint256 _cap
setDebtCeiling 0xb1511cc9
uint256 _debtCeiling
setDebtWindowSize 0xa74dd4c4
uint256 _debtWindowSize
setLiquidationRouter 0xf7b60908
address _liquidationRouter
setMaxDebtPerWindow 0x0b3392d8
uint256 _maxDebtPerWindow
setMaxTokensPerVault 0x3cf57c61
uint256 _maxTokensPerVault
setPriceFeed 0x724e78da
address _priceFeed
setPublicRedemptions 0xecbed39b
bool _publicRedemptions
setRedemptionAllowed 0x23010c2e
address _address
bool _allowed
setRedemptionFeeRecipient 0x924adef0
address _redemptionFeeRecipient
setRedemptionHealthFactorLimit 0xa3320222
uint256 _redemptionHealthFactorLimit
setRedemptionRate 0xb6b2196d
uint256 _redemptionRate
setVaultDeployer 0x23896091
address _vaultDeployer
transferOwnership 0xf2fde38b
address newOwner
transferVaultOwnership 0xfdc10daf
address _vault
address _newOwner

Recent Transactions

No transactions found for this address