Address Contract Verified
Address
0x79012c8d491DcF3A30Db20d1f449b14CAF01da6C
Balance
0 ETH
Nonce
2
Code Size
10934 bytes
Creator
0x20Da9f3D...08d8 at tx 0x8396d9eb...ddc27d
Indexed Transactions
0
Contract Bytecode
10934 bytes
0x608060405234801561001057600080fd5b50600436106102775760003560e01c80635c975abb11610160578063bb04b193116100d8578063dbcb313b1161008c578063ef8d960311610071578063ef8d96031461052a578063f3fdb15a14610533578063fe14112d1461055357600080fd5b8063dbcb313b14610518578063e941fa781461052157600080fd5b8063c5f956af116100bd578063c5f956af146104d2578063ca9505e4146104f2578063cf33d9551461050557600080fd5b8063bb04b193146104ac578063bf28068b146104bf57600080fd5b80638456cb591161012f578063a4e8273e11610114578063a4e8273e1461046a578063a50cf2c814610472578063b6ac642a1461049957600080fd5b80638456cb591461044f5780639aa5d4621461045757600080fd5b80635c975abb1461042b578063609ae31714610436578063743753591461043f578063788c6bfe1461044757600080fd5b806336dda7d5116101f35780634c19386c116101c25780635427c938116101a75780635427c938146103fd57806354fd4d50146104105780635664cacf1461041857600080fd5b80634c19386c146103e15780634d778ad1146103ea57600080fd5b806336dda7d5146103865780633e163df0146103ad5780633f4ba83a146103d057806345d31f9d146103d857600080fd5b80631e16e4fc1161024a5780632954018c1161022f5780632954018c146103205780632e97ca211461034057806331d8bc271461037357600080fd5b80631e16e4fc146102c85780632495a5991461030057600080fd5b8063030dbb041461027c57806305fe138b14610298578063078c4781146102ab5780630fce70fb146102c0575b600080fd5b61028560025481565b6040519081526020015b60405180910390f35b6102856102a63660046127c6565b61055b565b6102be6102b93660046127f6565b610868565b005b6102856109c0565b6102db6102d6366004612813565b6109e8565b60405173ffffffffffffffffffffffffffffffffffffffff909116815260200161028f565b6007546102db9073ffffffffffffffffffffffffffffffffffffffff1681565b6005546102db9073ffffffffffffffffffffffffffffffffffffffff1681565b61036361034e3660046127f6565b60086020526000908152604090205460ff1681565b604051901515815260200161028f565b61028561038136600461282c565b610a1f565b6102db7f0000000000000000000000008a1112afef7f4fc7c066a77aabbc01b3fff31d4781565b6103636103bb3660046127f6565b60096020526000908152604090205460ff1681565b6102be610a7f565b610285600d5481565b61028560045481565b6102856103f8366004612813565b610b6d565b61028561040b366004612813565b610b9d565b610285600181565b6102be6104263660046127f6565b610bbe565b60005460ff16610363565b610285600e5481565b610285610cae565b610285610d46565b6102be610e1c565b6102be610465366004612858565b610f08565b600a54610285565b6102db7f000000000000000000000000523da3a8961e4dd4f6206dbf7e6c749f51796bb381565b6102be6104a7366004612813565b61138c565b6102be6104ba366004612813565b61151d565b6102be6104cd3660046127c6565b611636565b600b546102db9073ffffffffffffffffffffffffffffffffffffffff1681565b6102be61050036600461282c565b6117b6565b6102be6105133660046127f6565b611bc3565b610285600c5481565b610285600f5481565b61028560035481565b6006546102db9073ffffffffffffffffffffffffffffffffffffffff1681565b610285611f33565b6000805460ff16156105ce576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601060248201527f5061757361626c653a207061757365640000000000000000000000000000000060448201526064015b60405180910390fd5b6002600154141561063b576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601f60248201527f5265656e7472616e637947756172643a207265656e7472616e742063616c6c0060448201526064016105c5565b60026001819055604080518082019091529081527f5a30000000000000000000000000000000000000000000000000000000000000602082015273ffffffffffffffffffffffffffffffffffffffff83166106c3576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016105c591906128bc565b5060006106cf84610b9d565b905060006106e8600f5483611fa090919063ffffffff16565b905060006106f6828461293c565b60075490915061071d9073ffffffffffffffffffffffffffffffffffffffff168683611fea565b811561074d57600b5460075461074d9173ffffffffffffffffffffffffffffffffffffffff918216911684611fea565b6040517f9dc29fac000000000000000000000000000000000000000000000000000000008152336004820152602481018790527f0000000000000000000000008a1112afef7f4fc7c066a77aabbc01b3fff31d4773ffffffffffffffffffffffffffffffffffffffff1690639dc29fac90604401600060405180830381600087803b1580156107db57600080fd5b505af11580156107ef573d6000803e3d6000fd5b5050505082600254610801919061293c565b60025561080e60006120c3565b60405186815273ffffffffffffffffffffffffffffffffffffffff86169033907fd8ae9b9ba89e637bcb66a69ac91e8f688018e81d6f92c57e02226425c8efbdf69060200160405180910390a36001805595945050505050565b6040517f5f259aba0000000000000000000000000000000000000000000000000000000081523360048201527f000000000000000000000000523da3a8961e4dd4f6206dbf7e6c749f51796bb373ffffffffffffffffffffffffffffffffffffffff1690635f259aba90602401602060405180830381865afa1580156108f2573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906109169190612953565b61094c576040517f61081c1500000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b73ffffffffffffffffffffffffffffffffffffffff811660008181526008602052604080822080547fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00169055517f9181736fce85d2d4cca2e4406f10679302ae5c387180fdb62963af3cd9a24fd69190a250565b600080600e54426109d1919061293c565b90506109e2600c54600d5483610a1f565b91505090565b600a81815481106109f857600080fd5b60009182526020909120015473ffffffffffffffffffffffffffffffffffffffff16905081565b6000806301e13380610a318486612975565b610a3b91906129e1565b610a51906b033b2e3c9fd0803ce80000006129f5565b90506b033b2e3c9fd0803ce8000000610a6a8287612975565b610a7491906129e1565b9150505b9392505050565b6040517fd4eb5db00000000000000000000000000000000000000000000000000000000081523360048201527f000000000000000000000000523da3a8961e4dd4f6206dbf7e6c749f51796bb373ffffffffffffffffffffffffffffffffffffffff169063d4eb5db090602401602060405180830381865afa158015610b09573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610b2d9190612953565b610b63576040517f10332dee00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b610b6b612198565b565b6000610b77610d46565b610b8d6b033b2e3c9fd0803ce800000084612975565b610b9791906129e1565b92915050565b60006b033b2e3c9fd0803ce8000000610bb4610d46565b610b8d9084612975565b6040517f5f259aba0000000000000000000000000000000000000000000000000000000081523360048201527f000000000000000000000000523da3a8961e4dd4f6206dbf7e6c749f51796bb373ffffffffffffffffffffffffffffffffffffffff1690635f259aba90602401602060405180830381865afa158015610c48573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610c6c9190612953565b610ca2576040517f61081c1500000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b610cab81612279565b50565b6007546040517f70a0823100000000000000000000000000000000000000000000000000000000815230600482015260009173ffffffffffffffffffffffffffffffffffffffff16906370a0823190602401602060405180830381865afa158015610d1d573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610d419190612a0d565b905090565b6000807f0000000000000000000000008a1112afef7f4fc7c066a77aabbc01b3fff31d4773ffffffffffffffffffffffffffffffffffffffff166318160ddd6040518163ffffffff1660e01b8152600401602060405180830381865afa158015610db4573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610dd89190612a0d565b905080610df2576b033b2e3c9fd0803ce800000091505090565b806b033b2e3c9fd0803ce8000000610e08611f33565b610e129190612975565b6109e291906129e1565b6040517f3a41ec640000000000000000000000000000000000000000000000000000000081523360048201527f000000000000000000000000523da3a8961e4dd4f6206dbf7e6c749f51796bb373ffffffffffffffffffffffffffffffffffffffff1690633a41ec6490602401602060405180830381865afa158015610ea6573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610eca9190612953565b610f00576040517fd794b1e700000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b610b6b61238a565b60005460ff1615610f75576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601060248201527f5061757361626c653a207061757365640000000000000000000000000000000060448201526064016105c5565b60026001541415610fe2576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601f60248201527f5265656e7472616e637947756172643a207265656e7472616e742063616c6c0060448201526064016105c5565b60026001819055604080518082019091529081527f5a30000000000000000000000000000000000000000000000000000000000000602082015273ffffffffffffffffffffffffffffffffffffffff831661106a576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016105c591906128bc565b5060035483611077611f33565b61108191906129f5565b11156040518060400160405280600381526020017f5053320000000000000000000000000000000000000000000000000000000000815250906110f1576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016105c591906128bc565b506007546040517f70a0823100000000000000000000000000000000000000000000000000000000815230600482015260009173ffffffffffffffffffffffffffffffffffffffff16906370a0823190602401602060405180830381865afa158015611161573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906111859190612a0d565b6007549091506111ad9073ffffffffffffffffffffffffffffffffffffffff1633308761244a565b6007546040517f70a08231000000000000000000000000000000000000000000000000000000008152306004820152829173ffffffffffffffffffffffffffffffffffffffff16906370a0823190602401602060405180830381865afa15801561121b573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061123f9190612a0d565b611249919061293c565b93507f0000000000000000000000008a1112afef7f4fc7c066a77aabbc01b3fff31d4773ffffffffffffffffffffffffffffffffffffffff166340c10f198461129187610b6d565b6040517fffffffff0000000000000000000000000000000000000000000000000000000060e085901b16815273ffffffffffffffffffffffffffffffffffffffff90921660048301526024820152604401600060405180830381600087803b1580156112fc57600080fd5b505af1158015611310573d6000803e3d6000fd5b505050508360025461132291906129f5565b60025561132f60006120c3565b604080518581526020810184905273ffffffffffffffffffffffffffffffffffffffff85169133917fd2491a9b4fe81a7cd4511e8b7b7743951b061dad5bed7da8a7795b080ee08c7e910160405180910390a35050600180555050565b6040517f5f259aba0000000000000000000000000000000000000000000000000000000081523360048201527f000000000000000000000000523da3a8961e4dd4f6206dbf7e6c749f51796bb373ffffffffffffffffffffffffffffffffffffffff1690635f259aba90602401602060405180830381865afa158015611416573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061143a9190612953565b611470576040517f61081c1500000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b60408051808201909152600381527f5053330000000000000000000000000000000000000000000000000000000000602082015260648211156114e0576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016105c591906128bc565b50600f8190556040518181527fd5fe46099fa396290a7f57e36c3c3c8774e2562c18ed5d1dcc0fa75071e03f1d906020015b60405180910390a150565b6040517f5f259aba0000000000000000000000000000000000000000000000000000000081523360048201527f000000000000000000000000523da3a8961e4dd4f6206dbf7e6c749f51796bb373ffffffffffffffffffffffffffffffffffffffff1690635f259aba90602401602060405180830381865afa1580156115a7573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906115cb9190612953565b611601576040517f61081c1500000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b60038190556040518181527fd7a183c9fe85b604c25d54bd676e0866f6c13bcca9fb9b0850213de118fdc99c90602001611512565b60005460ff16156116a3576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601060248201527f5061757361626c653a207061757365640000000000000000000000000000000060448201526064016105c5565b33600090815260086020908152604091829020548251808401909352600383527f50533000000000000000000000000000000000000000000000000000000000009183019190915260ff16611725576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016105c591906128bc565b5060075461174a9073ffffffffffffffffffffffffffffffffffffffff168284611fea565b61175460006120c3565b8160045461176291906129f5565b60045560405182815273ffffffffffffffffffffffffffffffffffffffff82169033907f312a5e5e1079f5dda4e95dbbd0b908b291fd5b992ef22073643ab691572c5b529060200160405180910390a35050565b60005460ff1615611823576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601060248201527f5061757361626c653a207061757365640000000000000000000000000000000060448201526064016105c5565b33600090815260096020908152604091829020548251808401909352600383527f50533000000000000000000000000000000000000000000000000000000000009183019190915260ff166118a5576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016105c591906128bc565b50811561199157600b5473ffffffffffffffffffffffffffffffffffffffff7f0000000000000000000000008a1112afef7f4fc7c066a77aabbc01b3fff31d478116916340c10f1991166118f885610b6d565b6040517fffffffff0000000000000000000000000000000000000000000000000000000060e085901b16815273ffffffffffffffffffffffffffffffffffffffff90921660048301526024820152604401600060405180830381600087803b15801561196357600080fd5b505af1158015611977573d6000803e3d6000fd5b505050508160025461198991906129f5565b600255611b5b565b600061199c82610b6d565b600b546040517f70a0823100000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff91821660048201529192506000917f0000000000000000000000008a1112afef7f4fc7c066a77aabbc01b3fff31d47909116906370a0823190602401602060405180830381865afa158015611a33573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611a579190612a0d565b905081811015611aab57905080337fef3653ded679720ab04913b6f3820be7cedc8286d42ff5dd8dff17e91bd2964c611a8f83610b9d565b611a99908661293c565b60405190815260200160405180910390a25b600b546040517f9dc29fac00000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff9182166004820152602481018490527f0000000000000000000000008a1112afef7f4fc7c066a77aabbc01b3fff31d4790911690639dc29fac90604401600060405180830381600087803b158015611b4057600080fd5b505af1158015611b54573d6000803e3d6000fd5b5050505050505b611b64816120c3565b8260046000828254611b76919061293c565b9091555050604080518481526020810184905290810182905233907f2fe77b1c99aca6b022b8efc6e3e8dd1b48b30748709339b65c50ef3263443e099060600160405180910390a2505050565b6040517f5f259aba0000000000000000000000000000000000000000000000000000000081523360048201527f000000000000000000000000523da3a8961e4dd4f6206dbf7e6c749f51796bb373ffffffffffffffffffffffffffffffffffffffff1690635f259aba90602401602060405180830381865afa158015611c4d573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611c719190612953565b611ca7576040517f61081c1500000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b8073ffffffffffffffffffffffffffffffffffffffff1663570a7af26040518163ffffffff1660e01b8152600401602060405180830381865afa158015611cf2573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611d169190612a26565b73ffffffffffffffffffffffffffffffffffffffff163073ffffffffffffffffffffffffffffffffffffffff16146040518060400160405280600381526020017f505331000000000000000000000000000000000000000000000000000000000081525090611db2576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016105c591906128bc565b5073ffffffffffffffffffffffffffffffffffffffff8116600090815260096020908152604091829020548251808401909352600383527f50533400000000000000000000000000000000000000000000000000000000009183019190915260ff1615611e4c576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016105c591906128bc565b5073ffffffffffffffffffffffffffffffffffffffff81166000818152600860209081526040808320805460017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff009182168117909255600990935281842080549093168117909255600a8054928301815583527fc65a7bb8d6351c1cf70c95a316cc6a92839c986682d98bc35f958f4883f9d2a890910180547fffffffffffffffffffffffff00000000000000000000000000000000000000001684179055517fe076020e7eac3915d33aec40c24f95e73eb6c9921ff89747d50aa8fd934d2c019190a250565b600080600e5442611f44919061293c565b905060006301e133806b033b2e3c9fd0803ce800000083600d54600454611f6b9190612975565b611f759190612975565b611f7f91906129e1565b611f8991906129e1565b905080600254611f9991906129f5565b9250505090565b6000821580611fad575081155b15611fba57506000610b97565b612710611fc8600282612a43565b61ffff16611fd68486612975565b611fe091906129f5565b610a7891906129e1565b60405173ffffffffffffffffffffffffffffffffffffffff83166024820152604481018290526120be9084907fa9059cbb00000000000000000000000000000000000000000000000000000000906064015b604080517fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe08184030181529190526020810180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167fffffffff00000000000000000000000000000000000000000000000000000000909316929092179091526124ae565b505050565b806120cc611f33565b6120d6919061293c565b6002556120e16109c0565b600c5560065460025473ffffffffffffffffffffffffffffffffffffffff909116906342568d4490612111610cae565b6040517fffffffff0000000000000000000000000000000000000000000000000000000060e085901b16815260048101929092526024820152604401602060405180830381865afa15801561216a573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061218e9190612a0d565b600d555042600e55565b60005460ff16612204576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601460248201527f5061757361626c653a206e6f742070617573656400000000000000000000000060448201526064016105c5565b600080547fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff001690557f5db9ee0a495bf2e6ff9c91a7834c1ba4fdd244a5e8aa4e537bd38aeae4b073aa335b60405173ffffffffffffffffffffffffffffffffffffffff909116815260200160405180910390a1565b60408051808201909152600281527f5a30000000000000000000000000000000000000000000000000000000000000602082015273ffffffffffffffffffffffffffffffffffffffff82166122fb576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016105c591906128bc565b50600680547fffffffffffffffffffffffff00000000000000000000000000000000000000001673ffffffffffffffffffffffffffffffffffffffff831617905561234660006120c3565b60405173ffffffffffffffffffffffffffffffffffffffff8216907f0ec6cb7631d36954a05ffd646135bfd9995c71e7fa36d26abb1ad9f24a040ea190600090a250565b60005460ff16156123f7576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601060248201527f5061757361626c653a207061757365640000000000000000000000000000000060448201526064016105c5565b600080547fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff001660011790557f62e78cea01bee320cd4e420270b5ea74000d11b0c9f74754ebdbfc544b05a25861224f3390565b60405173ffffffffffffffffffffffffffffffffffffffff808516602483015283166044820152606481018290526124a89085907f23b872dd000000000000000000000000000000000000000000000000000000009060840161203c565b50505050565b6000612510826040518060400160405280602081526020017f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c65648152508573ffffffffffffffffffffffffffffffffffffffff166125ba9092919063ffffffff16565b8051909150156120be578080602001905181019061252e9190612953565b6120be576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602a60248201527f5361666545524332303a204552433230206f7065726174696f6e20646964206e60448201527f6f7420737563636565640000000000000000000000000000000000000000000060648201526084016105c5565b60606125c984846000856125d1565b949350505050565b606082471015612663576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602660248201527f416464726573733a20696e73756666696369656e742062616c616e636520666f60448201527f722063616c6c000000000000000000000000000000000000000000000000000060648201526084016105c5565b843b6126cb576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e747261637400000060448201526064016105c5565b6000808673ffffffffffffffffffffffffffffffffffffffff1685876040516126f49190612a64565b60006040518083038185875af1925050503d8060008114612731576040519150601f19603f3d011682016040523d82523d6000602084013e612736565b606091505b5091509150612746828286612751565b979650505050505050565b60608315612760575081610a78565b8251156127705782518084602001fd5b816040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016105c591906128bc565b73ffffffffffffffffffffffffffffffffffffffff81168114610cab57600080fd5b600080604083850312156127d957600080fd5b8235915060208301356127eb816127a4565b809150509250929050565b60006020828403121561280857600080fd5b8135610a78816127a4565b60006020828403121561282557600080fd5b5035919050565b60008060006060848603121561284157600080fd5b505081359360208301359350604090920135919050565b60008060006060848603121561286d57600080fd5b83359250602084013561287f816127a4565b929592945050506040919091013590565b60005b838110156128ab578181015183820152602001612893565b838111156124a85750506000910152565b60208152600082518060208401526128db816040850160208701612890565b601f017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0169190910160400192915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60008282101561294e5761294e61290d565b500390565b60006020828403121561296557600080fd5b81518015158114610a7857600080fd5b6000817fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff04831182151516156129ad576129ad61290d565b500290565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601260045260246000fd5b6000826129f0576129f06129b2565b500490565b60008219821115612a0857612a0861290d565b500190565b600060208284031215612a1f57600080fd5b5051919050565b600060208284031215612a3857600080fd5b8151610a78816127a4565b600061ffff80841680612a5857612a586129b2565b92169190910492915050565b60008251612a76818460208701612890565b919091019291505056fea26469706673582212206f3b5f25efdcc3608fd6fc19175002f7ba249dd0cfb23a43f35ab991c901dac164736f6c634300080a0033
Verified Source Code Full Match
Compiler: v0.8.10+commit.fc410830
EVM: london
Optimization: Yes (1000000 runs)
IDieselToken.sol 15 lines
// SPDX-License-Identifier: MIT
// Gearbox Protocol. Generalized leverage for DeFi protocols
// (c) Gearbox Holdings, 2022
pragma solidity ^0.8.10;
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
interface IDieselTokenExceptions {
/// @dev Thrown if an access-restricted function was called by non-PoolService
error PoolServiceOnlyException();
}
interface IDieselToken is IERC20, IDieselTokenExceptions {
/// @dev Returns the address of the pool this Diesel token belongs to
function poolService() external view returns (address);
}
ICreditManagerV2.sol 420 lines
// SPDX-License-Identifier: MIT
// Gearbox Protocol. Generalized leverage for DeFi protocols
// (c) Gearbox Holdings, 2022
pragma solidity ^0.8.10;
import { IPriceOracleV2 } from "./IPriceOracle.sol";
import { IVersion } from "./IVersion.sol";
enum ClosureAction {
CLOSE_ACCOUNT,
LIQUIDATE_ACCOUNT,
LIQUIDATE_EXPIRED_ACCOUNT,
LIQUIDATE_PAUSED
}
interface ICreditManagerV2Events {
/// @dev Emits when a call to an external contract is made through the Credit Manager
event ExecuteOrder(address indexed borrower, address indexed target);
/// @dev Emits when a configurator is upgraded
event NewConfigurator(address indexed newConfigurator);
}
interface ICreditManagerV2Exceptions {
/// @dev Thrown if an access-restricted function is called by an address that is not
/// the connected Credit Facade, or an allowed adapter
error AdaptersOrCreditFacadeOnlyException();
/// @dev Thrown if an access-restricted function is called by an address that is not
/// the connected Credit Facade
error CreditFacadeOnlyException();
/// @dev Thrown if an access-restricted function is called by an address that is not
/// the connected Credit Configurator
error CreditConfiguratorOnlyException();
/// @dev Thrown on attempting to open a Credit Account for or transfer a Credit Account
/// to the zero address or an address that already owns a Credit Account
error ZeroAddressOrUserAlreadyHasAccountException();
/// @dev Thrown on attempting to execute an order to an address that is not an allowed
/// target contract
error TargetContractNotAllowedException();
/// @dev Thrown on failing a full collateral check after an operation
error NotEnoughCollateralException();
/// @dev Thrown on attempting to receive a token that is not a collateral token
/// or was forbidden
error TokenNotAllowedException();
/// @dev Thrown if an attempt to approve a collateral token to a target contract failed
error AllowanceFailedException();
/// @dev Thrown on attempting to perform an action for an address that owns no Credit Account
error HasNoOpenedAccountException();
/// @dev Thrown on attempting to add a token that is already in a collateral list
error TokenAlreadyAddedException();
/// @dev Thrown on configurator attempting to add more than 256 collateral tokens
error TooManyTokensException();
/// @dev Thrown if more than the maximal number of tokens were enabled on a Credit Account,
/// and there are not enough unused token to disable
error TooManyEnabledTokensException();
/// @dev Thrown when a reentrancy into the contract is attempted
error ReentrancyLockException();
}
/// @notice All Credit Manager functions are access-restricted and can only be called
/// by the Credit Facade or allowed adapters. Users are not allowed to
/// interact with the Credit Manager directly
interface ICreditManagerV2 is
ICreditManagerV2Events,
ICreditManagerV2Exceptions,
IVersion
{
//
// CREDIT ACCOUNT MANAGEMENT
//
/// @dev Opens credit account and borrows funds from the pool.
/// - Takes Credit Account from the factory;
/// - Requests the pool to lend underlying to the Credit Account
///
/// @param borrowedAmount Amount to be borrowed by the Credit Account
/// @param onBehalfOf The owner of the newly opened Credit Account
function openCreditAccount(uint256 borrowedAmount, address onBehalfOf)
external
returns (address);
/// @dev Closes a Credit Account - covers both normal closure and liquidation
/// - Checks whether the contract is paused, and, if so, if the payer is an emergency liquidator.
/// Only emergency liquidators are able to liquidate account while the CM is paused.
/// Emergency liquidations do not pay a liquidator premium or liquidation fees.
/// - Calculates payments to various recipients on closure:
/// + Computes amountToPool, which is the amount to be sent back to the pool.
/// This includes the principal, interest and fees, but can't be more than
/// total position value
/// + Computes remainingFunds during liquidations - these are leftover funds
/// after paying the pool and the liquidator, and are sent to the borrower
/// + Computes protocol profit, which includes interest and liquidation fees
/// + Computes loss if the totalValue is less than borrow amount + interest
/// - Checks the underlying token balance:
/// + if it is larger than amountToPool, then the pool is paid fully from funds on the Credit Account
/// + else tries to transfer the shortfall from the payer - either the borrower during closure, or liquidator during liquidation
/// - Send assets to the "to" address, as long as they are not included into skipTokenMask
/// - If convertWETH is true, the function converts WETH into ETH before sending
/// - Returns the Credit Account back to factory
///
/// @param borrower Borrower address
/// @param closureActionType Whether the account is closed, liquidated or liquidated due to expiry
/// @param totalValue Portfolio value for liqution, 0 for ordinary closure
/// @param payer Address which would be charged if credit account has not enough funds to cover amountToPool
/// @param to Address to which the leftover funds will be sent
/// @param skipTokenMask Tokenmask contains 1 for tokens which needed to be skipped for sending
/// @param convertWETH If true converts WETH to ETH
function closeCreditAccount(
address borrower,
ClosureAction closureActionType,
uint256 totalValue,
address payer,
address to,
uint256 skipTokenMask,
bool convertWETH
) external returns (uint256 remainingFunds);
/// @dev Manages debt size for borrower:
///
/// - Increase debt:
/// + Increases debt by transferring funds from the pool to the credit account
/// + Updates the cumulative index to keep interest the same. Since interest
/// is always computed dynamically as borrowedAmount * (cumulativeIndexNew / cumulativeIndexOpen - 1),
/// cumulativeIndexOpen needs to be updated, as the borrow amount has changed
///
/// - Decrease debt:
/// + Repays debt partially + all interest and fees accrued thus far
/// + Updates cunulativeIndex to cumulativeIndex now
///
/// @param creditAccount Address of the Credit Account to change debt for
/// @param amount Amount to increase / decrease the principal by
/// @param increase True to increase principal, false to decrease
/// @return newBorrowedAmount The new debt principal
function manageDebt(
address creditAccount,
uint256 amount,
bool increase
) external returns (uint256 newBorrowedAmount);
/// @dev Adds collateral to borrower's credit account
/// @param payer Address of the account which will be charged to provide additional collateral
/// @param creditAccount Address of the Credit Account
/// @param token Collateral token to add
/// @param amount Amount to add
function addCollateral(
address payer,
address creditAccount,
address token,
uint256 amount
) external;
/// @dev Transfers Credit Account ownership to another address
/// @param from Address of previous owner
/// @param to Address of new owner
function transferAccountOwnership(address from, address to) external;
/// @dev Requests the Credit Account to approve a collateral token to another contract.
/// @param borrower Borrower's address
/// @param targetContract Spender to change allowance for
/// @param token Collateral token to approve
/// @param amount New allowance amount
function approveCreditAccount(
address borrower,
address targetContract,
address token,
uint256 amount
) external;
/// @dev Requests a Credit Account to make a low-level call with provided data
/// This is the intended pathway for state-changing interactions with 3rd-party protocols
/// @param borrower Borrower's address
/// @param targetContract Contract to be called
/// @param data Data to pass with the call
function executeOrder(
address borrower,
address targetContract,
bytes memory data
) external returns (bytes memory);
//
// COLLATERAL VALIDITY AND ACCOUNT HEALTH CHECKS
//
/// @dev Enables a token on a Credit Account, including it
/// into account health and total value calculations
/// @param creditAccount Address of a Credit Account to enable the token for
/// @param token Address of the token to be enabled
function checkAndEnableToken(address creditAccount, address token) external;
/// @dev Optimized health check for individual swap-like operations.
/// @notice Fast health check assumes that only two tokens (input and output)
/// participate in the operation and computes a % change in weighted value between
/// inbound and outbound collateral. The cumulative negative change across several
/// swaps in sequence cannot be larger than feeLiquidation (a fee that the
/// protocol is ready to waive if needed). Since this records a % change
/// between just two tokens, the corresponding % change in TWV will always be smaller,
/// which makes this check safe.
/// More details at https://dev.gearbox.fi/docs/documentation/risk/fast-collateral-check#fast-check-protection
/// @param creditAccount Address of the Credit Account
/// @param tokenIn Address of the token spent by the swap
/// @param tokenOut Address of the token received from the swap
/// @param balanceInBefore Balance of tokenIn before the operation
/// @param balanceOutBefore Balance of tokenOut before the operation
function fastCollateralCheck(
address creditAccount,
address tokenIn,
address tokenOut,
uint256 balanceInBefore,
uint256 balanceOutBefore
) external;
/// @dev Performs a full health check on an account, summing up
/// value of all enabled collateral tokens
/// @param creditAccount Address of the Credit Account to check
function fullCollateralCheck(address creditAccount) external;
/// @dev Checks that the number of enabled tokens on a Credit Account
/// does not violate the maximal enabled token limit and tries
/// to disable unused tokens if it does
/// @param creditAccount Account to check enabled tokens for
function checkAndOptimizeEnabledTokens(address creditAccount) external;
/// @dev Disables a token on a credit account
/// @notice Usually called by adapters to disable spent tokens during a multicall,
/// but can also be called separately from the Credit Facade to remove
/// unwanted tokens
/// @return True if token mask was change otherwise False
function disableToken(address creditAccount, address token)
external
returns (bool);
//
// GETTERS
//
/// @dev Returns the address of a borrower's Credit Account, or reverts if there is none.
/// @param borrower Borrower's address
function getCreditAccountOrRevert(address borrower)
external
view
returns (address);
/// @dev Computes amounts that must be sent to various addresses before closing an account
/// @param totalValue Credit Accounts total value in underlying
/// @param closureActionType Type of account closure
/// * CLOSE_ACCOUNT: The account is healthy and is closed normally
/// * LIQUIDATE_ACCOUNT: The account is unhealthy and is being liquidated to avoid bad debt
/// * LIQUIDATE_EXPIRED_ACCOUNT: The account has expired and is being liquidated (lowered liquidation premium)
/// * LIQUIDATE_PAUSED: The account is liquidated while the system is paused due to emergency (no liquidation premium)
/// @param borrowedAmount Credit Account's debt principal
/// @param borrowedAmountWithInterest Credit Account's debt principal + interest
/// @return amountToPool Amount of underlying to be sent to the pool
/// @return remainingFunds Amount of underlying to be sent to the borrower (only applicable to liquidations)
/// @return profit Protocol's profit from fees (if any)
/// @return loss Protocol's loss from bad debt (if any)
function calcClosePayments(
uint256 totalValue,
ClosureAction closureActionType,
uint256 borrowedAmount,
uint256 borrowedAmountWithInterest
)
external
view
returns (
uint256 amountToPool,
uint256 remainingFunds,
uint256 profit,
uint256 loss
);
/// @dev Calculates the debt accrued by a Credit Account
/// @param creditAccount Address of the Credit Account
/// @return borrowedAmount The debt principal
/// @return borrowedAmountWithInterest The debt principal + accrued interest
/// @return borrowedAmountWithInterestAndFees The debt principal + accrued interest and protocol fees
function calcCreditAccountAccruedInterest(address creditAccount)
external
view
returns (
uint256 borrowedAmount,
uint256 borrowedAmountWithInterest,
uint256 borrowedAmountWithInterestAndFees
);
/// @dev Maps Credit Accounts to bit masks encoding their enabled token sets
/// Only enabled tokens are counted as collateral for the Credit Account
/// @notice An enabled token mask encodes an enabled token by setting
/// the bit at the position equal to token's index to 1
function enabledTokensMap(address creditAccount)
external
view
returns (uint256);
/// @dev Maps the Credit Account to its current percentage drop across all swaps since
/// the last full check, in RAY format
function cumulativeDropAtFastCheckRAY(address creditAccount)
external
view
returns (uint256);
/// @dev Returns the collateral token at requested index and its liquidation threshold
/// @param id The index of token to return
function collateralTokens(uint256 id)
external
view
returns (address token, uint16 liquidationThreshold);
/// @dev Returns the collateral token with requested mask and its liquidationThreshold
/// @param tokenMask Token mask corresponding to the token
function collateralTokensByMask(uint256 tokenMask)
external
view
returns (address token, uint16 liquidationThreshold);
/// @dev Total number of known collateral tokens.
function collateralTokensCount() external view returns (uint256);
/// @dev Returns the mask for the provided token
/// @param token Token to returns the mask for
function tokenMasksMap(address token) external view returns (uint256);
/// @dev Bit mask encoding a set of forbidden tokens
function forbiddenTokenMask() external view returns (uint256);
/// @dev Maps allowed adapters to their respective target contracts.
function adapterToContract(address adapter) external view returns (address);
/// @dev Maps 3rd party contracts to their respective adapters
function contractToAdapter(address targetContract)
external
view
returns (address);
/// @dev Address of the underlying asset
function underlying() external view returns (address);
/// @dev Address of the connected pool
function pool() external view returns (address);
/// @dev Address of the connected pool
/// @notice [DEPRECATED]: use pool() instead.
function poolService() external view returns (address);
/// @dev A map from borrower addresses to Credit Account addresses
function creditAccounts(address borrower) external view returns (address);
/// @dev Address of the connected Credit Configurator
function creditConfigurator() external view returns (address);
/// @dev Address of WETH
function wethAddress() external view returns (address);
/// @dev Returns the liquidation threshold for the provided token
/// @param token Token to retrieve the LT for
function liquidationThresholds(address token)
external
view
returns (uint16);
/// @dev The maximal number of enabled tokens on a single Credit Account
function maxAllowedEnabledTokenLength() external view returns (uint8);
/// @dev Maps addresses to their status as emergency liquidator.
/// @notice Emergency liquidators are trusted addresses
/// that are able to liquidate positions while the contracts are paused,
/// e.g. when there is a risk of bad debt while an exploit is being patched.
/// In the interest of fairness, emergency liquidators do not receive a premium
/// And are compensated by the Gearbox DAO separately.
function canLiquidateWhilePaused(address) external view returns (bool);
/// @dev Returns the fee parameters of the Credit Manager
/// @return feeInterest Percentage of interest taken by the protocol as profit
/// @return feeLiquidation Percentage of account value taken by the protocol as profit
/// during unhealthy account liquidations
/// @return liquidationDiscount Multiplier that reduces the effective totalValue during unhealthy account liquidations,
/// allowing the liquidator to take the unaccounted for remainder as premium. Equal to (1 - liquidationPremium)
/// @return feeLiquidationExpired Percentage of account value taken by the protocol as profit
/// during expired account liquidations
/// @return liquidationDiscountExpired Multiplier that reduces the effective totalValue during expired account liquidations,
/// allowing the liquidator to take the unaccounted for remainder as premium. Equal to (1 - liquidationPremiumExpired)
function fees()
external
view
returns (
uint16 feeInterest,
uint16 feeLiquidation,
uint16 liquidationDiscount,
uint16 feeLiquidationExpired,
uint16 liquidationDiscountExpired
);
/// @dev Address of the connected Credit Facade
function creditFacade() external view returns (address);
/// @dev Address of the connected Price Oracle
function priceOracle() external view returns (IPriceOracleV2);
/// @dev Address of the universal adapter
function universalAdapter() external view returns (address);
/// @dev Contract's version
function version() external view returns (uint256);
/// @dev Paused() state
function checkEmergencyPausable(address caller, bool state)
external
returns (bool);
}
AddressProvider.sol 190 lines
// SPDX-License-Identifier: BUSL-1.1
// Gearbox Protocol. Generalized leverage for DeFi protocols
// (c) Gearbox Holdings, 2022
pragma solidity ^0.8.10;
import { IAddressProvider } from "../interfaces/IAddressProvider.sol";
import { Claimable } from "./access/Claimable.sol";
import { Errors } from "../libraries/Errors.sol";
// Repositories & services
bytes32 constant CONTRACTS_REGISTER = "CONTRACTS_REGISTER";
bytes32 constant ACL = "ACL";
bytes32 constant PRICE_ORACLE = "PRICE_ORACLE";
bytes32 constant ACCOUNT_FACTORY = "ACCOUNT_FACTORY";
bytes32 constant DATA_COMPRESSOR = "DATA_COMPRESSOR";
bytes32 constant TREASURY_CONTRACT = "TREASURY_CONTRACT";
bytes32 constant GEAR_TOKEN = "GEAR_TOKEN";
bytes32 constant WETH_TOKEN = "WETH_TOKEN";
bytes32 constant WETH_GATEWAY = "WETH_GATEWAY";
bytes32 constant LEVERAGED_ACTIONS = "LEVERAGED_ACTIONS";
/// @title AddressRepository
/// @notice Stores addresses of deployed contracts
contract AddressProvider is Claimable, IAddressProvider {
// Mapping from contract keys to respective addresses
mapping(bytes32 => address) public addresses;
// Contract version
uint256 public constant version = 2;
constructor() {
// @dev Emits first event for contract discovery
emit AddressSet("ADDRESS_PROVIDER", address(this));
}
/// @return Address of ACL contract
function getACL() external view returns (address) {
return _getAddress(ACL); // F:[AP-3]
}
/// @dev Sets address of ACL contract
/// @param _address Address of ACL contract
function setACL(address _address)
external
onlyOwner // F:[AP-12]
{
_setAddress(ACL, _address); // F:[AP-3]
}
/// @return Address of ContractsRegister
function getContractsRegister() external view returns (address) {
return _getAddress(CONTRACTS_REGISTER); // F:[AP-4]
}
/// @dev Sets address of ContractsRegister
/// @param _address Address of ContractsRegister
function setContractsRegister(address _address)
external
onlyOwner // F:[AP-12]
{
_setAddress(CONTRACTS_REGISTER, _address); // F:[AP-4]
}
/// @return Address of PriceOracle
function getPriceOracle() external view override returns (address) {
return _getAddress(PRICE_ORACLE); // F:[AP-5]
}
/// @dev Sets address of PriceOracle
/// @param _address Address of PriceOracle
function setPriceOracle(address _address)
external
onlyOwner // F:[AP-12]
{
_setAddress(PRICE_ORACLE, _address); // F:[AP-5]
}
/// @return Address of AccountFactory
function getAccountFactory() external view returns (address) {
return _getAddress(ACCOUNT_FACTORY); // F:[AP-6]
}
/// @dev Sets address of AccountFactory
/// @param _address Address of AccountFactory
function setAccountFactory(address _address)
external
onlyOwner // F:[AP-12]
{
_setAddress(ACCOUNT_FACTORY, _address); // F:[AP-6]
}
/// @return Address of DataCompressor
function getDataCompressor() external view override returns (address) {
return _getAddress(DATA_COMPRESSOR); // F:[AP-7]
}
/// @dev Sets address of AccountFactory
/// @param _address Address of AccountFactory
function setDataCompressor(address _address)
external
onlyOwner // F:[AP-12]
{
_setAddress(DATA_COMPRESSOR, _address); // F:[AP-7]
}
/// @return Address of Treasury contract
function getTreasuryContract() external view returns (address) {
return _getAddress(TREASURY_CONTRACT); // F:[AP-8]
}
/// @dev Sets address of Treasury Contract
/// @param _address Address of Treasury Contract
function setTreasuryContract(address _address)
external
onlyOwner // F:[AP-12]
{
_setAddress(TREASURY_CONTRACT, _address); // F:[AP-8]
}
/// @return Address of GEAR token
function getGearToken() external view override returns (address) {
return _getAddress(GEAR_TOKEN); // F:[AP-9]
}
/// @dev Sets address of GEAR token
/// @param _address Address of GEAR token
function setGearToken(address _address)
external
onlyOwner // F:[AP-12]
{
_setAddress(GEAR_TOKEN, _address); // F:[AP-9]
}
/// @return Address of WETH token
function getWethToken() external view override returns (address) {
return _getAddress(WETH_TOKEN); // F:[AP-10]
}
/// @dev Sets address of WETH token
/// @param _address Address of WETH token
function setWethToken(address _address)
external
onlyOwner // F:[AP-12]
{
_setAddress(WETH_TOKEN, _address); // F:[AP-10]
}
/// @return Address of WETH token
function getWETHGateway() external view override returns (address) {
return _getAddress(WETH_GATEWAY); // F:[AP-11]
}
/// @dev Sets address of WETH token
/// @param _address Address of WETH token
function setWETHGateway(address _address)
external
onlyOwner // F:[AP-12]
{
_setAddress(WETH_GATEWAY, _address); // F:[AP-11]
}
/// @return Address of PathFinder
function getLeveragedActions() external view returns (address) {
return _getAddress(LEVERAGED_ACTIONS); // T:[AP-7]
}
/// @dev Sets address of PathFinder
/// @param _address Address of PathFinder
function setLeveragedActions(address _address)
external
onlyOwner // T:[AP-15]
{
_setAddress(LEVERAGED_ACTIONS, _address); // T:[AP-7]
}
/// @return Address of key, reverts if the key doesn't exist
function _getAddress(bytes32 key) internal view returns (address) {
address result = addresses[key];
require(result != address(0), Errors.AS_ADDRESS_NOT_FOUND); // F:[AP-1]
return result; // F:[AP-3, 4, 5, 6, 7, 8, 9, 10, 11]
}
/// @dev Sets address to map by its key
/// @param key Key in string format
/// @param value Address
function _setAddress(bytes32 key, address value) internal {
addresses[key] = value; // F:[AP-3, 4, 5, 6, 7, 8, 9, 10, 11]
emit AddressSet(key, value); // F:[AP-2]
}
}
IVersion.sol 11 lines
// SPDX-License-Identifier: MIT
// Gearbox Protocol. Generalized leverage for DeFi protocols
// (c) Gearbox Holdings, 2022
pragma solidity ^0.8.10;
/// @title IVersion
/// @dev Declares a version function which returns the contract's version
interface IVersion {
/// @dev Returns contract version
function version() external view returns (uint256);
}
IAddressProvider.sol 43 lines
// SPDX-License-Identifier: MIT
// Gearbox Protocol. Generalized leverage for DeFi protocols
// (c) Gearbox Holdings, 2022
pragma solidity ^0.8.10;
import { IVersion } from "./IVersion.sol";
interface IAddressProviderEvents {
/// @dev Emits when an address is set for a contract role
event AddressSet(bytes32 indexed service, address indexed newAddress);
}
/// @title Optimised for front-end Address Provider interface
interface IAddressProvider is IAddressProviderEvents, IVersion {
/// @return Address of ACL contract
function getACL() external view returns (address);
/// @return Address of ContractsRegister
function getContractsRegister() external view returns (address);
/// @return Address of AccountFactory
function getAccountFactory() external view returns (address);
/// @return Address of DataCompressor
function getDataCompressor() external view returns (address);
/// @return Address of GEAR token
function getGearToken() external view returns (address);
/// @return Address of WETH token
function getWethToken() external view returns (address);
/// @return Address of WETH Gateway
function getWETHGateway() external view returns (address);
/// @return Address of PriceOracle
function getPriceOracle() external view returns (address);
/// @return Address of DAO Treasury Multisig
function getTreasuryContract() external view returns (address);
/// @return Address of PathFinder
function getLeveragedActions() external view returns (address);
}
Ownable.sol 76 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (access/Ownable.sol)
pragma solidity ^0.8.0;
import "../utils/Context.sol";
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* By default, the owner account will be the one that deploys the contract. This
* can later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
constructor() {
_transferOwnership(_msgSender());
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
_;
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions anymore. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby removing any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
Address.sol 217 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Address.sol)
pragma solidity ^0.8.0;
/**
* @dev Collection of functions related to the address type
*/
library Address {
/**
* @dev Returns true if `account` is a contract.
*
* [IMPORTANT]
* ====
* It is unsafe to assume that an address for which this function returns
* false is an externally-owned account (EOA) and not a contract.
*
* Among others, `isContract` will return false for the following
* types of addresses:
*
* - an externally-owned account
* - a contract in construction
* - an address where a contract will be created
* - an address where a contract lived, but was destroyed
* ====
*/
function isContract(address account) internal view returns (bool) {
// This method relies on extcodesize, which returns 0 for contracts in
// construction, since the code is only stored at the end of the
// constructor execution.
uint256 size;
assembly {
size := extcodesize(account)
}
return size > 0;
}
/**
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
* `recipient`, forwarding all available gas and reverting on errors.
*
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
* of certain opcodes, possibly making contracts go over the 2300 gas limit
* imposed by `transfer`, making them unable to receive funds via
* `transfer`. {sendValue} removes this limitation.
*
* https://diligence.consensys.net/posts/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.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
*/
function sendValue(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, "Address: insufficient balance");
(bool success, ) = recipient.call{value: amount}("");
require(success, "Address: unable to send value, recipient may have reverted");
}
/**
* @dev Performs a Solidity function call using a low level `call`. A
* plain `call` is an unsafe replacement for a function call: use this
* function instead.
*
* If `target` reverts with a revert reason, it is bubbled up by this
* function (like regular Solidity function calls).
*
* Returns the raw returned data. To convert to the expected return value,
* use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
*
* Requirements:
*
* - `target` must be a contract.
* - calling `target` with `data` must not revert.
*
* _Available since v3.1._
*/
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCall(target, data, "Address: low-level call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
* `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but also transferring `value` wei to `target`.
*
* Requirements:
*
* - the calling contract must have an ETH balance of at least `value`.
* - the called Solidity function must be `payable`.
*
* _Available since v3.1._
*/
function functionCallWithValue(
address target,
bytes memory data,
uint256 value
) internal returns (bytes memory) {
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
}
/**
* @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
* with `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCallWithValue(
address target,
bytes memory data,
uint256 value,
string memory errorMessage
) internal returns (bytes memory) {
require(address(this).balance >= value, "Address: insufficient balance for call");
require(isContract(target), "Address: call to non-contract");
(bool success, bytes memory returndata) = target.call{value: value}(data);
return verifyCallResult(success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
return functionStaticCall(target, data, "Address: low-level static call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/
function functionStaticCall(
address target,
bytes memory data,
string memory errorMessage
) internal view returns (bytes memory) {
require(isContract(target), "Address: static call to non-contract");
(bool success, bytes memory returndata) = target.staticcall(data);
return verifyCallResult(success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
return functionDelegateCall(target, data, "Address: low-level delegate call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/
function functionDelegateCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
require(isContract(target), "Address: delegate call to non-contract");
(bool success, bytes memory returndata) = target.delegatecall(data);
return verifyCallResult(success, returndata, errorMessage);
}
/**
* @dev Tool to verifies that a low level call was successful, and revert if it wasn't, either by bubbling the
* revert reason using the provided one.
*
* _Available since v4.3._
*/
function verifyCallResult(
bool success,
bytes memory returndata,
string memory errorMessage
) internal pure returns (bytes memory) {
if (success) {
return returndata;
} else {
// 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
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
}
ReentrancyGuard.sol 63 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (security/ReentrancyGuard.sol)
pragma solidity ^0.8.0;
/**
* @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;
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() {
// On the first call to nonReentrant, _notEntered will be true
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
// Any calls to nonReentrant after this point will fail
_status = _ENTERED;
_;
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = _NOT_ENTERED;
}
}
ACLTrait.sol 46 lines
// SPDX-License-Identifier: BUSL-1.1
// Gearbox Protocol. Generalized leverage for DeFi protocols
// (c) Gearbox Holdings, 2022
pragma solidity ^0.8.10;
import { Pausable } from "@openzeppelin/contracts/security/Pausable.sol";
import { AddressProvider } from "./AddressProvider.sol";
import { IACL } from "../interfaces/IACL.sol";
import { ZeroAddressException, CallerNotConfiguratorException, CallerNotPausableAdminException, CallerNotUnPausableAdminException } from "../interfaces/IErrors.sol";
/// @title ACL Trait
/// @notice Utility class for ACL consumers
abstract contract ACLTrait is Pausable {
// ACL contract to check rights
IACL public immutable _acl;
/// @dev constructor
/// @param addressProvider Address of address repository
constructor(address addressProvider) {
if (addressProvider == address(0)) revert ZeroAddressException(); // F:[AA-2]
_acl = IACL(AddressProvider(addressProvider).getACL());
}
/// @dev Reverts if msg.sender is not configurator
modifier configuratorOnly() {
if (!_acl.isConfigurator(msg.sender))
revert CallerNotConfiguratorException();
_;
}
///@dev Pause contract
function pause() external {
if (!_acl.isPausableAdmin(msg.sender))
revert CallerNotPausableAdminException();
_pause();
}
/// @dev Unpause contract
function unpause() external {
if (!_acl.isUnpausableAdmin(msg.sender))
revert CallerNotUnPausableAdminException();
_unpause();
}
}
PercentageMath.sol 62 lines
// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.8.10;
import { Errors } from "./Errors.sol";
uint16 constant PERCENTAGE_FACTOR = 1e4; //percentage plus two decimals
uint256 constant HALF_PERCENT = PERCENTAGE_FACTOR / 2;
/**
* @title PercentageMath library
* @author Aave
* @notice Provides functions to perform percentage calculations
* @dev Percentages are defined by default with 2 decimals of precision (100.00). The precision is indicated by PERCENTAGE_FACTOR
* @dev Operations are rounded half up
**/
library PercentageMath {
/**
* @dev Executes a percentage multiplication
* @param value The value of which the percentage needs to be calculated
* @param percentage The percentage of the value to be calculated
* @return The percentage of value
**/
function percentMul(uint256 value, uint256 percentage)
internal
pure
returns (uint256)
{
if (value == 0 || percentage == 0) {
return 0; // T:[PM-1]
}
// require(
// value <= (type(uint256).max - HALF_PERCENT) / percentage,
// Errors.MATH_MULTIPLICATION_OVERFLOW
// ); // T:[PM-1]
return (value * percentage + HALF_PERCENT) / PERCENTAGE_FACTOR; // T:[PM-1]
}
/**
* @dev Executes a percentage division
* @param value The value of which the percentage needs to be calculated
* @param percentage The percentage of the value to be calculated
* @return The value divided the percentage
**/
function percentDiv(uint256 value, uint256 percentage)
internal
pure
returns (uint256)
{
require(percentage != 0, Errors.MATH_DIVISION_BY_ZERO); // T:[PM-2]
uint256 halfPercentage = percentage / 2; // T:[PM-2]
// require(
// value <= (type(uint256).max - halfPercentage) / PERCENTAGE_FACTOR,
// Errors.MATH_MULTIPLICATION_OVERFLOW
// ); // T:[PM-2]
return (value * PERCENTAGE_FACTOR + halfPercentage) / percentage;
}
}
IInterestRateModel.sol 16 lines
// SPDX-License-Identifier: MIT
// Gearbox Protocol. Generalized leverage for DeFi protocols
// (c) Gearbox Holdings, 2022
pragma solidity ^0.8.10;
import { IVersion } from "./IVersion.sol";
interface IInterestRateModel is IVersion {
/// @dev Returns the borrow rate calculated based on expectedLiquidity and availableLiquidity
/// @param expectedLiquidity Expected liquidity in the pool
/// @param availableLiquidity Available liquidity in the pool
/// @notice In RAY format
function calcBorrowRate(
uint256 expectedLiquidity,
uint256 availableLiquidity
) external view returns (uint256);
}
IERC20Metadata.sol 28 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/IERC20Metadata.sol)
pragma solidity ^0.8.0;
import "../IERC20.sol";
/**
* @dev Interface for the optional metadata functions from the ERC20 standard.
*
* _Available since v4.1._
*/
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);
}
SafeERC20.sol 99 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC20/utils/SafeERC20.sol)
pragma solidity ^0.8.0;
import "../IERC20.sol";
import "../../../utils/Address.sol";
/**
* @title SafeERC20
* @dev Wrappers around ERC20 operations that throw on failure (when the token
* contract returns false). Tokens that return no value (and instead revert or
* throw on failure) are also supported, non-reverting calls are assumed to be
* successful.
* To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
* which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
*/
library SafeERC20 {
using Address for address;
function safeTransfer(
IERC20 token,
address to,
uint256 value
) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
function safeTransferFrom(
IERC20 token,
address from,
address to,
uint256 value
) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
/**
* @dev Deprecated. This function has issues similar to the ones found in
* {IERC20-approve}, and its usage is discouraged.
*
* Whenever possible, use {safeIncreaseAllowance} and
* {safeDecreaseAllowance} instead.
*/
function safeApprove(
IERC20 token,
address spender,
uint256 value
) internal {
// safeApprove should only be called when setting an initial allowance,
// or when resetting it to zero. To increase and decrease it, use
// 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
require(
(value == 0) || (token.allowance(address(this), spender) == 0),
"SafeERC20: approve from non-zero to non-zero allowance"
);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
function safeIncreaseAllowance(
IERC20 token,
address spender,
uint256 value
) internal {
uint256 newAllowance = token.allowance(address(this), spender) + value;
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function safeDecreaseAllowance(
IERC20 token,
address spender,
uint256 value
) internal {
unchecked {
uint256 oldAllowance = token.allowance(address(this), spender);
require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
uint256 newAllowance = oldAllowance - value;
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
}
/**
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
* on the return value: the return value is optional (but if data is returned, it must not be false).
* @param token The token targeted by the call.
* @param data The call data (encoded using abi.encode or one of its variants).
*/
function _callOptionalReturn(IERC20 token, bytes memory data) private {
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
// we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that
// the target address contains contract code and also asserts for success in the low-level call.
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
if (returndata.length > 0) {
// Return data is optional
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
}
}
DieselToken.sol 41 lines
// SPDX-License-Identifier: BUSL-1.1
// Gearbox Protocol. Generalized leverage for DeFi protocols
// (c) Gearbox Holdings, 2022
pragma solidity ^0.8.10;
import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import { IDieselToken } from "../interfaces/IDieselToken.sol";
/// @dev DieselToken is LP token for Gearbox pools
contract DieselToken is ERC20, IDieselToken {
uint8 private immutable _decimals;
address public immutable poolService;
modifier onlyPoolService() {
if (msg.sender != poolService) {
revert PoolServiceOnlyException();
}
_;
}
constructor(
string memory name_,
string memory symbol_,
uint8 decimals_
) ERC20(name_, symbol_) {
_decimals = decimals_;
poolService = msg.sender;
}
function mint(address to, uint256 amount) external onlyPoolService {
_mint(to, amount);
}
function burn(address to, uint256 amount) external onlyPoolService {
_burn(to, amount);
}
function decimals() public view override returns (uint8) {
return _decimals;
}
}
Errors.sol 81 lines
// SPDX-License-Identifier: MIT
// Gearbox Protocol. Generalized leverage for DeFi protocols
// (c) Gearbox Holdings, 2022
pragma solidity ^0.8.10;
/// @title Errors library
library Errors {
//
// COMMON
//
string public constant ZERO_ADDRESS_IS_NOT_ALLOWED = "Z0";
string public constant NOT_IMPLEMENTED = "NI";
string public constant INCORRECT_PATH_LENGTH = "PL";
string public constant INCORRECT_ARRAY_LENGTH = "CR";
string public constant REGISTERED_CREDIT_ACCOUNT_MANAGERS_ONLY = "CP";
string public constant REGISTERED_POOLS_ONLY = "RP";
string public constant INCORRECT_PARAMETER = "IP";
//
// MATH
//
string public constant MATH_MULTIPLICATION_OVERFLOW = "M1";
string public constant MATH_ADDITION_OVERFLOW = "M2";
string public constant MATH_DIVISION_BY_ZERO = "M3";
//
// POOL
//
string public constant POOL_CONNECTED_CREDIT_MANAGERS_ONLY = "PS0";
string public constant POOL_INCOMPATIBLE_CREDIT_ACCOUNT_MANAGER = "PS1";
string public constant POOL_MORE_THAN_EXPECTED_LIQUIDITY_LIMIT = "PS2";
string public constant POOL_INCORRECT_WITHDRAW_FEE = "PS3";
string public constant POOL_CANT_ADD_CREDIT_MANAGER_TWICE = "PS4";
//
// ACCOUNT FACTORY
//
string public constant AF_CANT_CLOSE_CREDIT_ACCOUNT_IN_THE_SAME_BLOCK =
"AF1";
string public constant AF_MINING_IS_FINISHED = "AF2";
string public constant AF_CREDIT_ACCOUNT_NOT_IN_STOCK = "AF3";
string public constant AF_EXTERNAL_ACCOUNTS_ARE_FORBIDDEN = "AF4";
//
// ADDRESS PROVIDER
//
string public constant AS_ADDRESS_NOT_FOUND = "AP1";
//
// CONTRACTS REGISTER
//
string public constant CR_POOL_ALREADY_ADDED = "CR1";
string public constant CR_CREDIT_MANAGER_ALREADY_ADDED = "CR2";
//
// CREDIT ACCOUNT
//
string public constant CA_CONNECTED_CREDIT_MANAGER_ONLY = "CA1";
string public constant CA_FACTORY_ONLY = "CA2";
//
// ACL
//
string public constant ACL_CALLER_NOT_PAUSABLE_ADMIN = "ACL1";
string public constant ACL_CALLER_NOT_CONFIGURATOR = "ACL2";
//
// WETH GATEWAY
//
string public constant WG_DESTINATION_IS_NOT_WETH_COMPATIBLE = "WG1";
string public constant WG_RECEIVE_IS_NOT_ALLOWED = "WG2";
string public constant WG_NOT_ENOUGH_FUNDS = "WG3";
//
// TOKEN DISTRIBUTOR
//
string public constant TD_WALLET_IS_ALREADY_CONNECTED_TO_VC = "TD1";
string public constant TD_INCORRECT_WEIGHTS = "TD2";
string public constant TD_NON_ZERO_BALANCE_AFTER_DISTRIBUTION = "TD3";
string public constant TD_CONTRIBUTOR_IS_NOT_REGISTERED = "TD4";
}
IERC20.sol 82 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC20/IERC20.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* @dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves `amount` tokens from the caller's account to `recipient`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address recipient, uint256 amount) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 amount) external returns (bool);
/**
* @dev Moves `amount` tokens from `sender` to `recipient` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(
address sender,
address recipient,
uint256 amount
) external returns (bool);
/**
* @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);
}
IPriceOracle.sol 99 lines
// SPDX-License-Identifier: MIT
// Gearbox Protocol. Generalized leverage for DeFi protocols
// (c) Gearbox Holdings, 2022
pragma solidity ^0.8.10;
import { IVersion } from "./IVersion.sol";
interface IPriceOracleV2Events {
/// @dev Emits when a new price feed is added
event NewPriceFeed(address indexed token, address indexed priceFeed);
}
interface IPriceOracleV2Exceptions {
/// @dev Thrown if a price feed returns 0
error ZeroPriceException();
/// @dev Thrown if the last recorded result was not updated in the last round
error ChainPriceStaleException();
/// @dev Thrown on attempting to get a result for a token that does not have a price feed
error PriceOracleNotExistsException();
}
/// @title Price oracle interface
interface IPriceOracleV2 is
IPriceOracleV2Events,
IPriceOracleV2Exceptions,
IVersion
{
/// @dev Converts a quantity of an asset to USD (decimals = 8).
/// @param amount Amount to convert
/// @param token Address of the token to be converted
function convertToUSD(uint256 amount, address token)
external
view
returns (uint256);
/// @dev Converts a quantity of USD (decimals = 8) to an equivalent amount of an asset
/// @param amount Amount to convert
/// @param token Address of the token converted to
function convertFromUSD(uint256 amount, address token)
external
view
returns (uint256);
/// @dev Converts one asset into another
///
/// @param amount Amount to convert
/// @param tokenFrom Address of the token to convert from
/// @param tokenTo Address of the token to convert to
function convert(
uint256 amount,
address tokenFrom,
address tokenTo
) external view returns (uint256);
/// @dev Returns collateral values for two tokens, required for a fast check
/// @param amountFrom Amount of the outbound token
/// @param tokenFrom Address of the outbound token
/// @param amountTo Amount of the inbound token
/// @param tokenTo Address of the inbound token
/// @return collateralFrom Value of the outbound token amount in USD
/// @return collateralTo Value of the inbound token amount in USD
function fastCheck(
uint256 amountFrom,
address tokenFrom,
uint256 amountTo,
address tokenTo
) external view returns (uint256 collateralFrom, uint256 collateralTo);
/// @dev Returns token's price in USD (8 decimals)
/// @param token The token to compute the price for
function getPrice(address token) external view returns (uint256);
/// @dev Returns the price feed address for the passed token
/// @param token Token to get the price feed for
function priceFeeds(address token)
external
view
returns (address priceFeed);
/// @dev Returns the price feed for the passed token,
/// with additional parameters
/// @param token Token to get the price feed for
function priceFeedsWithFlags(address token)
external
view
returns (
address priceFeed,
bool skipCheck,
uint256 decimals
);
}
interface IPriceOracleV2Ext is IPriceOracleV2 {
/// @dev Sets a price feed if it doesn't exist, or updates an existing one
/// @param token Address of the token to set the price feed for
/// @param priceFeed Address of a USD price feed adhering to Chainlink's interface
function addPriceFeed(address token, address priceFeed) external;
}
ERC20.sol 356 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC20/ERC20.sol)
pragma solidity ^0.8.0;
import "./IERC20.sol";
import "./extensions/IERC20Metadata.sol";
import "../../utils/Context.sol";
/**
* @dev Implementation of the {IERC20} interface.
*
* This implementation is agnostic to the way tokens are created. This means
* that a supply mechanism has to be added in a derived contract using {_mint}.
* For a generic mechanism see {ERC20PresetMinterPauser}.
*
* TIP: For a detailed writeup see our guide
* https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226[How
* to implement supply mechanisms].
*
* We have followed general OpenZeppelin Contracts guidelines: functions revert
* instead returning `false` on failure. This behavior is nonetheless
* conventional and does not conflict with the expectations of ERC20
* applications.
*
* Additionally, an {Approval} event is emitted on calls to {transferFrom}.
* This allows applications to reconstruct the allowance for all accounts just
* by listening to said events. Other implementations of the EIP may not emit
* these events, as it isn't required by the specification.
*
* Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
* functions have been added to mitigate the well-known issues around setting
* allowances. See {IERC20-approve}.
*/
contract ERC20 is Context, IERC20, IERC20Metadata {
mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances;
uint256 private _totalSupply;
string private _name;
string private _symbol;
/**
* @dev Sets the values for {name} and {symbol}.
*
* The default value of {decimals} is 18. To select a different value for
* {decimals} you should overload it.
*
* All two of these values are immutable: they can only be set once during
* construction.
*/
constructor(string memory name_, string memory symbol_) {
_name = name_;
_symbol = symbol_;
}
/**
* @dev Returns the name of the token.
*/
function name() public view virtual override returns (string memory) {
return _name;
}
/**
* @dev Returns the symbol of the token, usually a shorter version of the
* name.
*/
function symbol() public view virtual override returns (string memory) {
return _symbol;
}
/**
* @dev Returns the number of decimals used to get its user representation.
* For example, if `decimals` equals `2`, a balance of `505` tokens should
* be displayed to a user as `5.05` (`505 / 10 ** 2`).
*
* Tokens usually opt for a value of 18, imitating the relationship between
* Ether and Wei. This is the value {ERC20} uses, unless this function is
* overridden;
*
* NOTE: This information is only used for _display_ purposes: it in
* no way affects any of the arithmetic of the contract, including
* {IERC20-balanceOf} and {IERC20-transfer}.
*/
function decimals() public view virtual override returns (uint8) {
return 18;
}
/**
* @dev See {IERC20-totalSupply}.
*/
function totalSupply() public view virtual override returns (uint256) {
return _totalSupply;
}
/**
* @dev See {IERC20-balanceOf}.
*/
function balanceOf(address account) public view virtual override returns (uint256) {
return _balances[account];
}
/**
* @dev See {IERC20-transfer}.
*
* Requirements:
*
* - `recipient` cannot be the zero address.
* - the caller must have a balance of at least `amount`.
*/
function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
_transfer(_msgSender(), recipient, amount);
return true;
}
/**
* @dev See {IERC20-allowance}.
*/
function allowance(address owner, address spender) public view virtual override returns (uint256) {
return _allowances[owner][spender];
}
/**
* @dev See {IERC20-approve}.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
function approve(address spender, uint256 amount) public virtual override returns (bool) {
_approve(_msgSender(), spender, amount);
return true;
}
/**
* @dev See {IERC20-transferFrom}.
*
* Emits an {Approval} event indicating the updated allowance. This is not
* required by the EIP. See the note at the beginning of {ERC20}.
*
* Requirements:
*
* - `sender` and `recipient` cannot be the zero address.
* - `sender` must have a balance of at least `amount`.
* - the caller must have allowance for ``sender``'s tokens of at least
* `amount`.
*/
function transferFrom(
address sender,
address recipient,
uint256 amount
) public virtual override returns (bool) {
_transfer(sender, recipient, amount);
uint256 currentAllowance = _allowances[sender][_msgSender()];
require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
unchecked {
_approve(sender, _msgSender(), currentAllowance - amount);
}
return true;
}
/**
* @dev Atomically increases the allowance granted to `spender` by the caller.
*
* This is an alternative to {approve} that can be used as a mitigation for
* problems described in {IERC20-approve}.
*
* Emits an {Approval} event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
_approve(_msgSender(), spender, _allowances[_msgSender()][spender] + addedValue);
return true;
}
/**
* @dev Atomically decreases the allowance granted to `spender` by the caller.
*
* This is an alternative to {approve} that can be used as a mitigation for
* problems described in {IERC20-approve}.
*
* Emits an {Approval} event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
* - `spender` must have allowance for the caller of at least
* `subtractedValue`.
*/
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
uint256 currentAllowance = _allowances[_msgSender()][spender];
require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
unchecked {
_approve(_msgSender(), spender, currentAllowance - subtractedValue);
}
return true;
}
/**
* @dev Moves `amount` of tokens from `sender` to `recipient`.
*
* This internal function is equivalent to {transfer}, and can be used to
* e.g. implement automatic token fees, slashing mechanisms, etc.
*
* Emits a {Transfer} event.
*
* Requirements:
*
* - `sender` cannot be the zero address.
* - `recipient` cannot be the zero address.
* - `sender` must have a balance of at least `amount`.
*/
function _transfer(
address sender,
address recipient,
uint256 amount
) internal virtual {
require(sender != address(0), "ERC20: transfer from the zero address");
require(recipient != address(0), "ERC20: transfer to the zero address");
_beforeTokenTransfer(sender, recipient, amount);
uint256 senderBalance = _balances[sender];
require(senderBalance >= amount, "ERC20: transfer amount exceeds balance");
unchecked {
_balances[sender] = senderBalance - amount;
}
_balances[recipient] += amount;
emit Transfer(sender, recipient, amount);
_afterTokenTransfer(sender, recipient, amount);
}
/** @dev Creates `amount` tokens and assigns them to `account`, increasing
* the total supply.
*
* Emits a {Transfer} event with `from` set to the zero address.
*
* Requirements:
*
* - `account` cannot be the zero address.
*/
function _mint(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: mint to the zero address");
_beforeTokenTransfer(address(0), account, amount);
_totalSupply += amount;
_balances[account] += amount;
emit Transfer(address(0), account, amount);
_afterTokenTransfer(address(0), account, amount);
}
/**
* @dev Destroys `amount` tokens from `account`, reducing the
* total supply.
*
* Emits a {Transfer} event with `to` set to the zero address.
*
* Requirements:
*
* - `account` cannot be the zero address.
* - `account` must have at least `amount` tokens.
*/
function _burn(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: burn from the zero address");
_beforeTokenTransfer(account, address(0), amount);
uint256 accountBalance = _balances[account];
require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
unchecked {
_balances[account] = accountBalance - amount;
}
_totalSupply -= amount;
emit Transfer(account, address(0), amount);
_afterTokenTransfer(account, address(0), amount);
}
/**
* @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
*
* This internal function is equivalent to `approve`, and can be used to
* e.g. set automatic allowances for certain subsystems, etc.
*
* Emits an {Approval} event.
*
* Requirements:
*
* - `owner` cannot be the zero address.
* - `spender` cannot be the zero address.
*/
function _approve(
address owner,
address spender,
uint256 amount
) internal virtual {
require(owner != address(0), "ERC20: approve from the zero address");
require(spender != address(0), "ERC20: approve to the zero address");
_allowances[owner][spender] = amount;
emit Approval(owner, spender, amount);
}
/**
* @dev Hook that is called before any transfer of tokens. This includes
* minting and burning.
*
* Calling conditions:
*
* - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
* will be transferred to `to`.
* - when `from` is zero, `amount` tokens will be minted for `to`.
* - when `to` is zero, `amount` of ``from``'s tokens will be burned.
* - `from` and `to` are never both zero.
*
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
*/
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual {}
/**
* @dev Hook that is called after any transfer of tokens. This includes
* minting and burning.
*
* Calling conditions:
*
* - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
* has been transferred to `to`.
* - when `from` is zero, `amount` tokens have been minted for `to`.
* - when `to` is zero, `amount` of ``from``'s tokens have been burned.
* - `from` and `to` are never both zero.
*
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
*/
function _afterTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual {}
}
Pausable.sol 91 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (security/Pausable.sol)
pragma solidity ^0.8.0;
import "../utils/Context.sol";
/**
* @dev Contract module which allows children to implement an emergency stop
* mechanism that can be triggered by an authorized account.
*
* This module is used through inheritance. It will make available the
* modifiers `whenNotPaused` and `whenPaused`, which can be applied to
* the functions of your contract. Note that they will not be pausable by
* simply including this module, only once the modifiers are put in place.
*/
abstract contract Pausable is Context {
/**
* @dev Emitted when the pause is triggered by `account`.
*/
event Paused(address account);
/**
* @dev Emitted when the pause is lifted by `account`.
*/
event Unpaused(address account);
bool private _paused;
/**
* @dev Initializes the contract in unpaused state.
*/
constructor() {
_paused = false;
}
/**
* @dev Returns true if the contract is paused, and false otherwise.
*/
function paused() public view virtual returns (bool) {
return _paused;
}
/**
* @dev Modifier to make a function callable only when the contract is not paused.
*
* Requirements:
*
* - The contract must not be paused.
*/
modifier whenNotPaused() {
require(!paused(), "Pausable: paused");
_;
}
/**
* @dev Modifier to make a function callable only when the contract is paused.
*
* Requirements:
*
* - The contract must be paused.
*/
modifier whenPaused() {
require(paused(), "Pausable: not paused");
_;
}
/**
* @dev Triggers stopped state.
*
* Requirements:
*
* - The contract must not be paused.
*/
function _pause() internal virtual whenNotPaused {
_paused = true;
emit Paused(_msgSender());
}
/**
* @dev Returns to normal state.
*
* Requirements:
*
* - The contract must be paused.
*/
function _unpause() internal virtual whenPaused {
_paused = false;
emit Unpaused(_msgSender());
}
}
Constants.sol 47 lines
// SPDX-License-Identifier: MIT // Gearbox Protocol. Generalized leverage for DeFi protocols // (c) Gearbox Holdings, 2022 pragma solidity ^0.8.10; // Denominations uint256 constant WAD = 1e18; uint256 constant RAY = 1e27; // 25% of type(uint256).max uint256 constant ALLOWANCE_THRESHOLD = type(uint96).max >> 3; // FEE = 50% uint16 constant DEFAULT_FEE_INTEREST = 50_00; // 50% // LIQUIDATION_FEE 1.5% uint16 constant DEFAULT_FEE_LIQUIDATION = 1_50; // 1.5% // LIQUIDATION PREMIUM 4% uint16 constant DEFAULT_LIQUIDATION_PREMIUM = 4_00; // 4% // LIQUIDATION_FEE_EXPIRED 2% uint16 constant DEFAULT_FEE_LIQUIDATION_EXPIRED = 1_00; // 2% // LIQUIDATION PREMIUM EXPIRED 2% uint16 constant DEFAULT_LIQUIDATION_PREMIUM_EXPIRED = 2_00; // 2% // DEFAULT PROPORTION OF MAX BORROWED PER BLOCK TO MAX BORROWED PER ACCOUNT uint16 constant DEFAULT_LIMIT_PER_BLOCK_MULTIPLIER = 2; // Seconds in a year uint256 constant SECONDS_PER_YEAR = 365 days; uint256 constant SECONDS_PER_ONE_AND_HALF_YEAR = (SECONDS_PER_YEAR * 3) / 2; // OPERATIONS // Leverage decimals - 100 is equal to 2x leverage (100% * collateral amount + 100% * borrowed amount) uint8 constant LEVERAGE_DECIMALS = 100; // Maximum withdraw fee for pool in PERCENTAGE_FACTOR format uint8 constant MAX_WITHDRAW_FEE = 100; uint256 constant EXACT_INPUT = 1; uint256 constant EXACT_OUTPUT = 2; address constant UNIVERSAL_CONTRACT = 0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC;
IErrors.sol 33 lines
// SPDX-License-Identifier: MIT // Gearbox Protocol. Generalized leverage for DeFi protocols // (c) Gearbox Holdings, 2022 pragma solidity ^0.8.10; /// @dev Common contract exceptions /// @dev Thrown on attempting to set an important address to zero address error ZeroAddressException(); /// @dev Thrown on attempting to call a non-implemented function error NotImplementedException(); /// @dev Thrown on attempting to set an EOA as an important contract in the system error AddressIsNotContractException(address); /// @dev Thrown on attempting to use a non-ERC20 contract or an EOA as a token error IncorrectTokenContractException(); /// @dev Thrown on attempting to set a token price feed to an address that is not a /// correct price feed error IncorrectPriceFeedException(); /// @dev Thrown on attempting to call an access restricted function as a non-Configurator error CallerNotConfiguratorException(); /// @dev Thrown on attempting to pause a contract as a non-Pausable admin error CallerNotPausableAdminException(); /// @dev Thrown on attempting to pause a contract as a non-Unpausable admin error CallerNotUnPausableAdminException(); error TokenIsNotAddedToCreditManagerException(address token);
IACL.sol 45 lines
// SPDX-License-Identifier: MIT
// Gearbox Protocol. Generalized leverage for DeFi protocols
// (c) Gearbox Holdings, 2022
pragma solidity ^0.8.10;
import { IVersion } from "./IVersion.sol";
interface IACLExceptions {
/// @dev Thrown when attempting to delete an address from a set that is not a pausable admin
error AddressNotPausableAdminException(address addr);
/// @dev Thrown when attempting to delete an address from a set that is not a unpausable admin
error AddressNotUnpausableAdminException(address addr);
}
interface IACLEvents {
/// @dev Emits when a new admin is added that can pause contracts
event PausableAdminAdded(address indexed newAdmin);
/// @dev Emits when a Pausable admin is removed
event PausableAdminRemoved(address indexed admin);
/// @dev Emits when a new admin is added that can unpause contracts
event UnpausableAdminAdded(address indexed newAdmin);
/// @dev Emits when an Unpausable admin is removed
event UnpausableAdminRemoved(address indexed admin);
}
/// @title ACL interface
interface IACL is IACLEvents, IACLExceptions, IVersion {
/// @dev Returns true if the address is a pausable admin and false if not
/// @param addr Address to check
function isPausableAdmin(address addr) external view returns (bool);
/// @dev Returns true if the address is unpausable admin and false if not
/// @param addr Address to check
function isUnpausableAdmin(address addr) external view returns (bool);
/// @dev Returns true if an address has configurator rights
/// @param account Address to check
function isConfigurator(address account) external view returns (bool);
/// @dev Returns address of configurator
function owner() external view returns (address);
}
Context.sol 24 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)
pragma solidity ^0.8.0;
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}
Claimable.sol 38 lines
// SPDX-License-Identifier: BUSL-1.1
// Gearbox Protocol. Generalized leverage for DeFi protocols
// (c) Gearbox Holdings, 2022
pragma solidity ^0.8.10;
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
/// @title Claimable
/// @dev Implements logic for a two-step ownership transfer on top of Ownable
contract Claimable is Ownable {
/// @dev The new owner that has not claimed ownership yet
address public pendingOwner;
/// @dev A modifier that restricts the function to the pending owner only
modifier onlyPendingOwner() {
if (msg.sender != pendingOwner) {
revert("Claimable: Sender is not pending owner");
}
_;
}
/// @dev Sets pending owner to the new owner, but does not
/// transfer ownership yet
/// @param newOwner The address to become the future owner
function transferOwnership(address newOwner) public override onlyOwner {
require(
newOwner != address(0),
"Claimable: new owner is the zero address"
);
pendingOwner = newOwner;
}
/// @dev Used by the pending owner to claim ownership after transferOwnership
function claimOwnership() external onlyPendingOwner {
_transferOwnership(pendingOwner);
pendingOwner = address(0);
}
}
PoolService.sol 557 lines
// SPDX-License-Identifier: BUSL-1.1
// Gearbox Protocol. Generalized leverage for DeFi protocols
// (c) Gearbox Holdings, 2022
pragma solidity ^0.8.10;
import { ReentrancyGuard } from "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import { ACLTrait } from "../core/ACLTrait.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { IERC20Metadata } from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import { RAY } from "../libraries/Constants.sol";
import { PercentageMath } from "../libraries/PercentageMath.sol";
import { IInterestRateModel } from "../interfaces/IInterestRateModel.sol";
import { IPoolService } from "../interfaces/IPoolService.sol";
import { ICreditManagerV2 } from "../interfaces/ICreditManagerV2.sol";
import { AddressProvider } from "../core/AddressProvider.sol";
import { DieselToken } from "../tokens/DieselToken.sol";
import { SECONDS_PER_YEAR, MAX_WITHDRAW_FEE } from "../libraries/Constants.sol";
import { Errors } from "../libraries/Errors.sol";
/// @title Pool Service Interface
/// @notice Implements business logic:
/// - Adding/removing pool liquidity
/// - Managing diesel tokens & diesel rates
/// - Taking/repaying Credit Manager debt
///
/// More: https://dev.gearbox.fi/developers/pools/pool-service
contract PoolService is IPoolService, ACLTrait, ReentrancyGuard {
using SafeERC20 for IERC20;
using PercentageMath for uint256;
/// @dev Expected liquidity at last update (LU)
uint256 public _expectedLiquidityLU;
/// @dev The limit on expected (total) liquidity
uint256 public override expectedLiquidityLimit;
/// @dev Total borrowed amount
/// @notice https://dev.gearbox.fi/developers/pools/economy/total-borrowed
uint256 public override totalBorrowed;
/// @dev Address provider
AddressProvider public override addressProvider;
/// @dev Interest rate model
IInterestRateModel public interestRateModel;
/// @dev The pool's underlying asset
address public override underlyingToken;
/// @dev Diesel(LP) token address
address public immutable override dieselToken;
/// @dev Map from Credit Manager addresses to the status of their ability to borrow
mapping(address => bool) public override creditManagersCanBorrow;
/// @dev Map from Credit Manager addresses to the status of their ability to repay
mapping(address => bool) public creditManagersCanRepay;
/// @dev The list of all Credit Managers
address[] public override creditManagers;
/// @dev Address of the protocol treasury
address public treasuryAddress;
/// @dev The cumulative interest index at last update
uint256 public override _cumulativeIndex_RAY;
/// @dev The current borrow rate
/// @notice https://dev.gearbox.fi/developers/pools/economy#borrow-apy
uint256 public override borrowAPY_RAY;
/// @dev Timestamp of last update
uint256 public override _timestampLU;
/// @dev Withdrawal fee in PERCENTAGE FORMAT
uint256 public override withdrawFee;
/// @dev Contract version
uint256 public constant override version = 1;
//
// CONSTRUCTOR
//
/// @dev Constructor
/// @param _addressProvider Address provider
/// @param _underlyingToken Address of the underlying token
/// @param _interestRateModelAddress Address of the initial interest rate model
constructor(
address _addressProvider,
address _underlyingToken,
address _interestRateModelAddress,
uint256 _expectedLiquidityLimit
) ACLTrait(_addressProvider) {
require(
_addressProvider != address(0) &&
_underlyingToken != address(0) &&
_interestRateModelAddress != address(0),
Errors.ZERO_ADDRESS_IS_NOT_ALLOWED
);
addressProvider = AddressProvider(_addressProvider);
underlyingToken = _underlyingToken;
dieselToken = address(
new DieselToken(
string(
abi.encodePacked(
"diesel ",
IERC20Metadata(_underlyingToken).name()
)
),
string(
abi.encodePacked(
"d",
IERC20Metadata(_underlyingToken).symbol()
)
),
IERC20Metadata(_underlyingToken).decimals()
)
);
treasuryAddress = addressProvider.getTreasuryContract();
_timestampLU = block.timestamp;
_cumulativeIndex_RAY = RAY; // T:[PS-5]
_updateInterestRateModel(_interestRateModelAddress);
expectedLiquidityLimit = _expectedLiquidityLimit;
}
//
// LIQUIDITY MANAGEMENT
//
/**
* @dev Adds liquidity to the pool
* - Transfers the underlying asset from sender to the pool
* - Mints diesel (LP) token фе current diesel rate
* - Updates expected liquidity
* - Updates borrow rate
*
* More: https://dev.gearbox.fi/developers/pools/pool-service#addliquidity
*
* @param amount Amount of tokens to be deposited
* @param onBehalfOf The address that will receive the dToken
* @param referralCode Code used to register the integrator originating the operation, for potential rewards.
* 0 if the action is executed directly by the user, without a facilitator.
*
* #if_succeeds {:msg "After addLiquidity() the pool gets the correct amoung of underlyingToken(s)"}
* IERC20(underlyingToken).balanceOf(address(this)) == old(IERC20(underlyingToken).balanceOf(address(this))) + amount;
* #if_succeeds {:msg "After addLiquidity() onBehalfOf gets the right amount of dieselTokens"}
* IERC20(dieselToken).balanceOf(onBehalfOf) == old(IERC20(dieselToken).balanceOf(onBehalfOf)) + old(toDiesel(amount));
* #if_succeeds {:msg "After addLiquidity() borrow rate decreases"}
* amount > 0 ==> borrowAPY_RAY <= old(currentBorrowRate());
* #limit {:msg "Not more than 1 day since last borrow rate update"} block.timestamp <= _timestampLU + 3600 * 24;
*/
function addLiquidity(
uint256 amount,
address onBehalfOf,
uint256 referralCode
)
external
override
whenNotPaused // T:[PS-4]
nonReentrant
{
require(onBehalfOf != address(0), Errors.ZERO_ADDRESS_IS_NOT_ALLOWED);
require(
expectedLiquidity() + amount <= expectedLiquidityLimit,
Errors.POOL_MORE_THAN_EXPECTED_LIQUIDITY_LIMIT
); // T:[PS-31]
uint256 balanceBefore = IERC20(underlyingToken).balanceOf(
address(this)
);
IERC20(underlyingToken).safeTransferFrom(
msg.sender,
address(this),
amount
); // T:[PS-2, 7]
amount =
IERC20(underlyingToken).balanceOf(address(this)) -
balanceBefore; // T:[FT-1]
DieselToken(dieselToken).mint(onBehalfOf, toDiesel(amount)); // T:[PS-2, 7]
_expectedLiquidityLU = _expectedLiquidityLU + amount; // T:[PS-2, 7]
_updateBorrowRate(0); // T:[PS-2, 7]
emit AddLiquidity(msg.sender, onBehalfOf, amount, referralCode); // T:[PS-2, 7]
}
/**
* @dev Removes liquidity from pool
* - Transfers to the sender the underlying amount equivalent to the passed Diesel amount
* - Burns Diesel tokens
* - Subtracts the removed underlying from expectedLiquidity
* - Updates borrow rate
*
* More: https://dev.gearbox.fi/developers/pools/pool-service#removeliquidity
*
* @param amount Amount of Diesel tokens to burn
* @param to Address to transfer the underlying to
*
* #if_succeeds {:msg "For removeLiquidity() sender must have sufficient diesel"}
* old(DieselToken(dieselToken).balanceOf(msg.sender)) >= amount;
* #if_succeeds {:msg "After removeLiquidity() `to` gets the liquidity in underlyingToken(s)"}
* (to != address(this) && to != treasuryAddress) ==>
* IERC20(underlyingToken).balanceOf(to) == old(IERC20(underlyingToken).balanceOf(to) + (let t:= fromDiesel(amount) in t.sub(t.percentMul(withdrawFee))));
* #if_succeeds {:msg "After removeLiquidity() treasury gets the withdraw fee in underlyingToken(s)"}
* (to != address(this) && to != treasuryAddress) ==>
* IERC20(underlyingToken).balanceOf(treasuryAddress) == old(IERC20(underlyingToken).balanceOf(treasuryAddress) + fromDiesel(amount).percentMul(withdrawFee));
* #if_succeeds {:msg "After removeLiquidity() borrow rate increases"}
* (to != address(this) && amount > 0) ==> borrowAPY_RAY >= old(currentBorrowRate());
* #limit {:msg "Not more than 1 day since last borrow rate update"} block.timestamp <= _timestampLU + 3600 * 24;
*/
function removeLiquidity(uint256 amount, address to)
external
override
whenNotPaused // T:[PS-4]
nonReentrant
returns (uint256)
{
require(to != address(0), Errors.ZERO_ADDRESS_IS_NOT_ALLOWED);
uint256 underlyingTokensAmount = fromDiesel(amount); // T:[PS-3, 8]
uint256 amountTreasury = underlyingTokensAmount.percentMul(withdrawFee);
uint256 amountSent = underlyingTokensAmount - amountTreasury;
IERC20(underlyingToken).safeTransfer(to, amountSent); // T:[PS-3, 34]
if (amountTreasury > 0) {
IERC20(underlyingToken).safeTransfer(
treasuryAddress,
amountTreasury
);
} // T:[PS-3, 34]
DieselToken(dieselToken).burn(msg.sender, amount); // T:[PS-3, 8]
_expectedLiquidityLU = _expectedLiquidityLU - underlyingTokensAmount; // T:[PS-3, 8]
_updateBorrowRate(0); // T:[PS-3,8 ]
emit RemoveLiquidity(msg.sender, to, amount); // T:[PS-3, 8]
return amountSent;
}
/// @dev Returns expected liquidity - the amount of money that should be in the pool
/// after all users close their Credit accounts and fully repay debts
///
/// More: https://dev.gearbox.fi/developers/pools/economy#expected-liquidity
function expectedLiquidity() public view override returns (uint256) {
// timeDifference = blockTime - previous timeStamp
uint256 timeDifference = block.timestamp - _timestampLU;
// currentBorrowRate * timeDifference
// interestAccrued = totalBorrow * ------------------------------------
// SECONDS_PER_YEAR
//
uint256 interestAccrued = (totalBorrowed *
borrowAPY_RAY *
timeDifference) /
RAY /
SECONDS_PER_YEAR; // T:[PS-29]
return _expectedLiquidityLU + interestAccrued; // T:[PS-29]
}
/// @dev Returns available liquidity in the pool (pool balance)
/// More: https://dev.gearbox.fi/developers/
function availableLiquidity() public view override returns (uint256) {
return IERC20(underlyingToken).balanceOf(address(this));
}
//
// CREDIT ACCOUNT LENDING
//
/// @dev Lends funds to a Credit Account and updates the pool parameters
/// More: https://dev.gearbox.fi/developers/pools/pool-service#lendcreditAccount
///
/// @param borrowedAmount Credit Account's debt principal
/// @param creditAccount Credit Account's address
///
/// #if_succeeds {:msg "After lendCreditAccount() borrow rate increases"}
/// borrowedAmount > 0 ==> borrowAPY_RAY >= old(currentBorrowRate());
/// #limit {:msg "Not more than 1 day since last borrow rate update"} block.timestamp <= _timestampLU + 3600 * 24;
function lendCreditAccount(uint256 borrowedAmount, address creditAccount)
external
override
whenNotPaused // T:[PS-4]
{
require(
creditManagersCanBorrow[msg.sender],
Errors.POOL_CONNECTED_CREDIT_MANAGERS_ONLY
); // T:[PS-12, 13]
// Transfer funds to credit account
IERC20(underlyingToken).safeTransfer(creditAccount, borrowedAmount); // T:[PS-14]
// Update borrow Rate
_updateBorrowRate(0); // T:[PS-17]
// Increase total borrowed amount
totalBorrowed = totalBorrowed + borrowedAmount; // T:[PS-16]
emit Borrow(msg.sender, creditAccount, borrowedAmount); // T:[PS-15]
}
/// @dev Registers Credit Account's debt repayment and updates parameters
/// More: https://dev.gearbox.fi/developers/pools/pool-service#repaycreditAccount
///
/// @param borrowedAmount Amount of principal ro repay
/// @param profit The treasury profit from repayment
/// @param loss Amount of underlying that the CA wan't able to repay
/// @notice Assumes that the underlying (including principal + interest + fees)
/// was already transferred
///
/// #if_succeeds {:msg "Cant have both profit and loss"} !(profit > 0 && loss > 0);
/// #if_succeeds {:msg "After repayCreditAccount() if we are profitabe, or treasury can cover the losses, diesel rate doesn't decrease"}
/// (profit > 0 || toDiesel(loss) >= DieselToken(dieselToken).balanceOf(treasuryAddress)) ==> getDieselRate_RAY() >= old(getDieselRate_RAY());
/// #limit {:msg "Not more than 1 day since last borrow rate update"} block.timestamp <= _timestampLU + 3600 * 24;
function repayCreditAccount(
uint256 borrowedAmount,
uint256 profit,
uint256 loss
)
external
override
whenNotPaused // T:[PS-4]
{
require(
creditManagersCanRepay[msg.sender],
Errors.POOL_CONNECTED_CREDIT_MANAGERS_ONLY
); // T:[PS-12]
// For fee surplus we mint tokens for treasury
if (profit > 0) {
// T:[PS-22] provess that diesel rate will be the same within the margin of error
DieselToken(dieselToken).mint(treasuryAddress, toDiesel(profit)); // T:[PS-21, 22]
_expectedLiquidityLU = _expectedLiquidityLU + profit; // T:[PS-21, 22]
}
// If returned money < borrowed amount + interest accrued
// it tries to compensate loss by burning diesel (LP) tokens
// from treasury fund
else {
uint256 amountToBurn = toDiesel(loss); // T:[PS-19,20]
uint256 treasuryBalance = DieselToken(dieselToken).balanceOf(
treasuryAddress
); // T:[PS-19,20]
if (treasuryBalance < amountToBurn) {
amountToBurn = treasuryBalance;
emit UncoveredLoss(
msg.sender,
loss - fromDiesel(treasuryBalance)
); // T:[PS-23]
}
// If treasury has enough funds, it just burns needed amount
// to keep diesel rate on the same level
DieselToken(dieselToken).burn(treasuryAddress, amountToBurn); // T:[PS-19, 20]
// _expectedLiquidityLU = _expectedLiquidityLU.sub(loss); //T:[PS-19,20]
}
// Update available liquidity
_updateBorrowRate(loss); // T:[PS-19, 20, 21]
// Reduce total borrowed. Should be after _updateBorrowRate() for correct calculations
totalBorrowed -= borrowedAmount; // T:[PS-19, 20]
emit Repay(msg.sender, borrowedAmount, profit, loss); // T:[PS-18]
}
//
// INTEREST RATE MANAGEMENT
//
/**
* @dev Calculates the most current value of the cumulative interest index
*
* / currentBorrowRate * timeDifference \
* newIndex = currentIndex * | 1 + ------------------------------------ |
* \ SECONDS_PER_YEAR /
*
* @return current cumulative index in RAY
*/
function calcLinearCumulative_RAY() public view override returns (uint256) {
//solium-disable-next-line
uint256 timeDifference = block.timestamp - _timestampLU; // T:[PS-28]
return
calcLinearIndex_RAY(
_cumulativeIndex_RAY,
borrowAPY_RAY,
timeDifference
); // T:[PS-28]
}
/// @dev Calculates a new cumulative index value from the initial value, borrow rate and time elapsed
/// @param cumulativeIndex_RAY Cumulative index at last update, in RAY
/// @param currentBorrowRate_RAY Current borrow rate, in RAY
/// @param timeDifference Time elapsed since last update, in seconds
function calcLinearIndex_RAY(
uint256 cumulativeIndex_RAY,
uint256 currentBorrowRate_RAY,
uint256 timeDifference
) public pure returns (uint256) {
// / currentBorrowRate * timeDifference \
// newIndex = currentIndex * | 1 + ------------------------------------ |
// \ SECONDS_PER_YEAR /
//
uint256 linearAccumulated_RAY = RAY +
(currentBorrowRate_RAY * timeDifference) /
SECONDS_PER_YEAR; // T:[GM-2]
return (cumulativeIndex_RAY * linearAccumulated_RAY) / RAY; // T:[GM-2]
}
/// @dev Updates the borrow rate when liquidity parameters are changed
/// @param loss The loss incurred by the pool on last parameter update, if any
function _updateBorrowRate(uint256 loss) internal {
// Update total _expectedLiquidityLU
_expectedLiquidityLU = expectedLiquidity() - loss; // T:[PS-27]
// Update cumulativeIndex
_cumulativeIndex_RAY = calcLinearCumulative_RAY(); // T:[PS-27]
// update borrow APY
borrowAPY_RAY = interestRateModel.calcBorrowRate(
_expectedLiquidityLU,
availableLiquidity()
); // T:[PS-27]
_timestampLU = block.timestamp; // T:[PS-27]
}
//
// DIESEL TOKEN MGMT
//
/// @dev Returns the current exchange rate of Diesel tokens to underlying
/// More info: https://dev.gearbox.fi/developers/pools/economy#diesel-rate
function getDieselRate_RAY() public view override returns (uint256) {
uint256 dieselSupply = IERC20(dieselToken).totalSupply();
if (dieselSupply == 0) return RAY; // T:[PS-1]
return (expectedLiquidity() * RAY) / dieselSupply; // T:[PS-6]
}
/// @dev Converts a quantity of the underlying to Diesel tokens
/// @param amount Amount in underlyingToken tokens to be converted to diesel tokens
function toDiesel(uint256 amount) public view override returns (uint256) {
return (amount * RAY) / getDieselRate_RAY(); // T:[PS-24]
}
/// @dev Converts a quantity of Diesel tokens to the underlying
/// @param amount Amount in diesel tokens to be converted to diesel tokens
function fromDiesel(uint256 amount) public view override returns (uint256) {
return (amount * getDieselRate_RAY()) / RAY; // T:[PS-24]
}
//
// CONFIGURATION
//
/// @dev Connects a new Credit manager to pool
/// @param _creditManager Address of the Credit Manager
function connectCreditManager(address _creditManager)
external
configuratorOnly // T:[PS-9]
{
require(
address(this) == ICreditManagerV2(_creditManager).poolService(),
Errors.POOL_INCOMPATIBLE_CREDIT_ACCOUNT_MANAGER
); // T:[PS-10]
require(
!creditManagersCanRepay[_creditManager],
Errors.POOL_CANT_ADD_CREDIT_MANAGER_TWICE
); // T:[PS-35]
creditManagersCanBorrow[_creditManager] = true; // T:[PS-11]
creditManagersCanRepay[_creditManager] = true; // T:[PS-11]
creditManagers.push(_creditManager); // T:[PS-11]
emit NewCreditManagerConnected(_creditManager); // T:[PS-11]
}
/// @dev Forbids a Credit Manager to borrow
/// @param _creditManager Address of the Credit Manager
function forbidCreditManagerToBorrow(address _creditManager)
external
configuratorOnly // T:[PS-9]
{
creditManagersCanBorrow[_creditManager] = false; // T:[PS-13]
emit BorrowForbidden(_creditManager); // T:[PS-13]
}
/// @dev Sets the new interest rate model for the pool
/// @param _interestRateModel Address of the new interest rate model contract
/// #limit {:msg "Disallow updating the interest rate model after the constructor"} address(interestRateModel) == address(0x0);
function updateInterestRateModel(address _interestRateModel)
public
configuratorOnly // T:[PS-9]
{
_updateInterestRateModel(_interestRateModel);
}
/// @dev IMPLEMENTATION: updateInterestRateModel
function _updateInterestRateModel(address _interestRateModel) internal {
require(
_interestRateModel != address(0),
Errors.ZERO_ADDRESS_IS_NOT_ALLOWED
);
interestRateModel = IInterestRateModel(_interestRateModel); // T:[PS-25]
_updateBorrowRate(0); // T:[PS-26]
emit NewInterestRateModel(_interestRateModel); // T:[PS-25]
}
/// @dev Sets a new expected liquidity limit
/// @param newLimit New expected liquidity limit
function setExpectedLiquidityLimit(uint256 newLimit)
external
configuratorOnly // T:[PS-9]
{
expectedLiquidityLimit = newLimit; // T:[PS-30]
emit NewExpectedLiquidityLimit(newLimit); // T:[PS-30]
}
/// @dev Sets a new withdrawal fee
/// @param fee The new fee amount, in bp
function setWithdrawFee(uint256 fee)
public
configuratorOnly // T:[PS-9]
{
require(fee <= MAX_WITHDRAW_FEE, Errors.POOL_INCORRECT_WITHDRAW_FEE); // T:[PS-32]
withdrawFee = fee; // T:[PS-33]
emit NewWithdrawFee(fee); // T:[PS-33]
}
/// @dev Returns the number of connected Credit Managers
function creditManagersCount() external view override returns (uint256) {
return creditManagers.length; // T:[PS-11]
}
}
IPoolService.sol 201 lines
// SPDX-License-Identifier: MIT
// Gearbox Protocol. Generalized leverage for DeFi protocols
// (c) Gearbox Holdings, 2022
pragma solidity ^0.8.10;
import "../core/AddressProvider.sol";
import { IVersion } from "./IVersion.sol";
interface IPoolServiceEvents {
/// @dev Emits on new liquidity being added to the pool
event AddLiquidity(
address indexed sender,
address indexed onBehalfOf,
uint256 amount,
uint256 referralCode
);
/// @dev Emits on liquidity being removed to the pool
event RemoveLiquidity(
address indexed sender,
address indexed to,
uint256 amount
);
/// @dev Emits on a Credit Manager borrowing funds for a Credit Account
event Borrow(
address indexed creditManager,
address indexed creditAccount,
uint256 amount
);
/// @dev Emits on repayment of a Credit Account's debt
event Repay(
address indexed creditManager,
uint256 borrowedAmount,
uint256 profit,
uint256 loss
);
/// @dev Emits on updating the interest rate model
event NewInterestRateModel(address indexed newInterestRateModel);
/// @dev Emits on connecting a new Credit Manager
event NewCreditManagerConnected(address indexed creditManager);
/// @dev Emits when a Credit Manager is forbidden to borrow
event BorrowForbidden(address indexed creditManager);
/// @dev Emitted when loss is incurred that can't be covered by treasury funds
event UncoveredLoss(address indexed creditManager, uint256 loss);
/// @dev Emits when the liquidity limit is changed
event NewExpectedLiquidityLimit(uint256 newLimit);
/// @dev Emits when the withdrawal fee is changed
event NewWithdrawFee(uint256 fee);
}
/// @title Pool Service Interface
/// @notice Implements business logic:
/// - Adding/removing pool liquidity
/// - Managing diesel tokens & diesel rates
/// - Taking/repaying Credit Manager debt
/// More: https://dev.gearbox.fi/developers/pool/abstractpoolservice
interface IPoolService is IPoolServiceEvents, IVersion {
//
// LIQUIDITY MANAGEMENT
//
/**
* @dev Adds liquidity to pool
* - transfers the underlying to the pool
* - mints Diesel (LP) tokens to onBehalfOf
* @param amount Amount of tokens to be deposited
* @param onBehalfOf The address that will receive the dToken
* @param referralCode Code used to register the integrator originating the operation, for potential rewards.
* 0 if the action is executed directly by the user, without a facilitator.
*/
function addLiquidity(
uint256 amount,
address onBehalfOf,
uint256 referralCode
) external;
/**
* @dev Removes liquidity from pool
* - burns LP's Diesel (LP) tokens
* - returns the equivalent amount of underlying to 'to'
* @param amount Amount of Diesel tokens to burn
* @param to Address to transfer the underlying to
*/
function removeLiquidity(uint256 amount, address to)
external
returns (uint256);
/**
* @dev Lends pool funds to a Credit Account
* @param borrowedAmount Credit Account's debt principal
* @param creditAccount Credit Account's address
*/
function lendCreditAccount(uint256 borrowedAmount, address creditAccount)
external;
/**
* @dev Repays the Credit Account's debt
* @param borrowedAmount Amount of principal ro repay
* @param profit The treasury profit from repayment
* @param loss Amount of underlying that the CA wan't able to repay
* @notice Assumes that the underlying (including principal + interest + fees)
* was already transferred
*/
function repayCreditAccount(
uint256 borrowedAmount,
uint256 profit,
uint256 loss
) external;
//
// GETTERS
//
/**
* @dev Returns the total amount of liquidity in the pool, including borrowed and available funds
*/
function expectedLiquidity() external view returns (uint256);
/**
* @dev Returns the limit on total liquidity
*/
function expectedLiquidityLimit() external view returns (uint256);
/**
* @dev Returns the available liquidity, which is expectedLiquidity - totalBorrowed
*/
function availableLiquidity() external view returns (uint256);
/**
* @dev Calculates the current interest index, RAY format
*/
function calcLinearCumulative_RAY() external view returns (uint256);
/**
* @dev Calculates the current borrow rate, RAY format
*/
function borrowAPY_RAY() external view returns (uint256);
/**
* @dev Returns the total borrowed amount (includes principal only)
*/
function totalBorrowed() external view returns (uint256);
/**
* ç
**/
function getDieselRate_RAY() external view returns (uint256);
/**
* @dev Returns the address of the underlying
*/
function underlyingToken() external view returns (address);
/**
* @dev Returns the address of the diesel token
*/
function dieselToken() external view returns (address);
/**
* @dev Returns the address of a Credit Manager by its id
*/
function creditManagers(uint256 id) external view returns (address);
/**
* @dev Returns the number of known Credit Managers
*/
function creditManagersCount() external view returns (uint256);
/**
* @dev Maps Credit Manager addresses to their status as a borrower.
* Returns false if borrowing is not allowed.
*/
function creditManagersCanBorrow(address id) external view returns (bool);
/// @dev Converts a quantity of the underlying to Diesel tokens
function toDiesel(uint256 amount) external view returns (uint256);
/// @dev Converts a quantity of Diesel tokens to the underlying
function fromDiesel(uint256 amount) external view returns (uint256);
/// @dev Returns the withdrawal fee
function withdrawFee() external view returns (uint256);
/// @dev Returns the timestamp of the pool's last update
function _timestampLU() external view returns (uint256);
/// @dev Returns the interest index at the last pool update
function _cumulativeIndex_RAY() external view returns (uint256);
/// @dev Returns the address provider
function addressProvider() external view returns (AddressProvider);
}
Read Contract
_acl 0xa50cf2c8 → address
_cumulativeIndex_RAY 0xdbcb313b → uint256
_expectedLiquidityLU 0x030dbb04 → uint256
_timestampLU 0x609ae317 → uint256
addressProvider 0x2954018c → address
availableLiquidity 0x74375359 → uint256
borrowAPY_RAY 0x45d31f9d → uint256
calcLinearCumulative_RAY 0x0fce70fb → uint256
calcLinearIndex_RAY 0x31d8bc27 → uint256
creditManagers 0x1e16e4fc → address
creditManagersCanBorrow 0x2e97ca21 → bool
creditManagersCanRepay 0x3e163df0 → bool
creditManagersCount 0xa4e8273e → uint256
dieselToken 0x36dda7d5 → address
expectedLiquidity 0xfe14112d → uint256
expectedLiquidityLimit 0xef8d9603 → uint256
fromDiesel 0x5427c938 → uint256
getDieselRate_RAY 0x788c6bfe → uint256
interestRateModel 0xf3fdb15a → address
paused 0x5c975abb → bool
toDiesel 0x4d778ad1 → uint256
totalBorrowed 0x4c19386c → uint256
treasuryAddress 0xc5f956af → address
underlyingToken 0x2495a599 → address
version 0x54fd4d50 → uint256
withdrawFee 0xe941fa78 → uint256
Write Contract 11 functions
These functions modify contract state and require a wallet transaction to execute.
addLiquidity 0x9aa5d462
uint256 amount
address onBehalfOf
uint256 referralCode
connectCreditManager 0xcf33d955
address _creditManager
forbidCreditManagerToBorrow 0x078c4781
address _creditManager
lendCreditAccount 0xbf28068b
uint256 borrowedAmount
address creditAccount
pause 0x8456cb59
No parameters
removeLiquidity 0x05fe138b
uint256 amount
address to
returns: uint256
repayCreditAccount 0xca9505e4
uint256 borrowedAmount
uint256 profit
uint256 loss
setExpectedLiquidityLimit 0xbb04b193
uint256 newLimit
setWithdrawFee 0xb6ac642a
uint256 fee
unpause 0x3f4ba83a
No parameters
updateInterestRateModel 0x5664cacf
address _interestRateModel
Recent Transactions
No transactions found for this address