-
Notifications
You must be signed in to change notification settings - Fork 1
/
ITroveManager.sol
353 lines (303 loc) · 15.8 KB
/
ITroveManager.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// SPDX-License-Identifier: MIT
pragma solidity 0.6.11;
pragma experimental ABIEncoderV2;
import "./ILiquityBase.sol";
import "./IStabilityPool.sol";
import "./IZUSDToken.sol";
import "./IZEROToken.sol";
import "./IZEROStaking.sol";
import "../Dependencies/Mynt/IMassetManager.sol";
import { IPermit2, ISignatureTransfer } from "./IPermit2.sol";
/// Common interface for the Trove Manager.
interface ITroveManager is ILiquityBase {
// --- Events ---
event FeeDistributorAddressChanged(address _feeDistributorAddress);
event TroveManagerRedeemOpsAddressChanged(address _troveManagerRedeemOps);
event LiquityBaseParamsAddressChanges(address _borrowerOperationsAddress);
event BorrowerOperationsAddressChanged(address _newBorrowerOperationsAddress);
event PriceFeedAddressChanged(address _newPriceFeedAddress);
event ZUSDTokenAddressChanged(address _newZUSDTokenAddress);
event ActivePoolAddressChanged(address _activePoolAddress);
event DefaultPoolAddressChanged(address _defaultPoolAddress);
event StabilityPoolAddressChanged(address _stabilityPoolAddress);
event GasPoolAddressChanged(address _gasPoolAddress);
event CollSurplusPoolAddressChanged(address _collSurplusPoolAddress);
event SortedTrovesAddressChanged(address _sortedTrovesAddress);
event ZEROTokenAddressChanged(address _zeroTokenAddress);
event ZEROStakingAddressChanged(address _zeroStakingAddress);
event Liquidation(
uint256 _liquidatedDebt,
uint256 _liquidatedColl,
uint256 _collGasCompensation,
uint256 _ZUSDGasCompensation
);
event Redemption(
uint256 _attemptedZUSDAmount,
uint256 _actualZUSDAmount,
uint256 _ETHSent,
uint256 _ETHFee
);
event TroveUpdated(
address indexed _borrower,
uint256 _debt,
uint256 _coll,
uint256 stake,
uint8 operation
);
event TroveLiquidated(
address indexed _borrower,
uint256 _debt,
uint256 _coll,
uint8 operation
);
event BaseRateUpdated(uint256 _baseRate);
event LastFeeOpTimeUpdated(uint256 _lastFeeOpTime);
event TotalStakesUpdated(uint256 _newTotalStakes);
event SystemSnapshotsUpdated(uint256 _totalStakesSnapshot, uint256 _totalCollateralSnapshot);
event LTermsUpdated(uint256 _L_ETH, uint256 _L_ZUSDDebt);
event TroveSnapshotsUpdated(uint256 _L_ETH, uint256 _L_ZUSDDebt);
event TroveIndexUpdated(address _borrower, uint256 _newIndex);
struct TroveManagerInitAddressesParams {
address _feeDistributorAddress;
address _troveManagerRedeemOps;
address _liquityBaseParamsAddress;
address _borrowerOperationsAddress;
address _activePoolAddress;
address _defaultPoolAddress;
address _stabilityPoolAddress;
address _gasPoolAddress;
address _collSurplusPoolAddress;
address _priceFeedAddress;
address _zusdTokenAddress;
address _sortedTrovesAddress;
address _zeroTokenAddress;
address _zeroStakingAddress;
}
// --- Functions ---
/**
* @notice Called only once on init, to set addresses of other Zero contracts. Callable only by owner
* @dev initializer function, checks addresses are contracts
* @param _troveManagerInitAddresses addresses list to intialize TroveManager with _
* _feeDistributorAddress feeDistributor contract address
* _troveManagerRedeemOps TroveManagerRedeemOps contract address
* _liquityBaseParamsAddress LiquityBaseParams contract address
* _borrowerOperationsAddress BorrowerOperations contract address
* _activePoolAddress ActivePool contract address
* _defaultPoolAddress DefaultPool contract address
* _stabilityPoolAddress StabilityPool contract address
* _gasPoolAddress GasPool contract address
* _collSurplusPoolAddress CollSurplusPool contract address
* _priceFeedAddress PriceFeed contract address
* _zusdTokenAddress ZUSDToken contract address
* _sortedTrovesAddress SortedTroves contract address
* _zeroTokenAddress ZEROToken contract address
* _zeroStakingAddress ZEROStaking contract address
*/
function setAddresses(
TroveManagerInitAddressesParams memory _troveManagerInitAddresses
) external;
function setTroveManagerRedeemOps(address _troveManagerRedeemOps) external;
/// @return Trove owners count
function getTroveOwnersCount() external view returns (uint256);
/// @param _index Trove owner index
/// @return Trove from TroveOwners array in given index
function getTroveFromTroveOwnersArray(uint256 _index) external view returns (address);
/// @param _borrower borrower address
/// @return the nominal collateral ratio (ICR) of a given Trove, without the price. Takes a trove's pending coll and debt rewards from redistributions into account.
function getNominalICR(address _borrower) external view returns (uint256);
/// @notice computes the user’s individual collateralization ratio (ICR) based on their total collateral and total ZUSD debt. Returns 2^256 -1 if they have 0 debt.
/// @param _borrower borrower address
/// @param _price ETH price
/// @return the current collateral ratio (ICR) of a given Trove. Takes a trove's pending coll and debt rewards from redistributions into account.
function getCurrentICR(address _borrower, uint256 _price) external view returns (uint256);
/// @notice Closes the trove if its ICR is lower than the minimum collateral ratio.
/// @param _borrower borrower address
function liquidate(address _borrower) external;
/**
* @notice Liquidate a sequence of troves. Closes a maximum number of n under-collateralized Troves,
* starting from the one with the lowest collateral ratio in the system, and moving upwards
* @param _n max number of under-collateralized Troves to liquidate
*/
function liquidateTroves(uint256 _n) external;
/**
* @notice Attempt to liquidate a custom list of troves provided by the caller.
* @param _troveArray list of trove addresses
*/
function batchLiquidateTroves(address[] calldata _troveArray) external;
/**
* @notice Send _ZUSDamount ZUSD to the system and redeem the corresponding amount of collateral from as many Troves as are needed to fill the redemption
* request. Applies pending rewards to a Trove before reducing its debt and coll.
*
* Note that if _amount is very large, this function can run out of gas, specially if traversed troves are small. This can be easily avoided by
* splitting the total _amount in appropriate chunks and calling the function multiple times.
*
* Param `_maxIterations` can also be provided, so the loop through Troves is capped (if it’s zero, it will be ignored).This makes it easier to
* avoid OOG for the frontend, as only knowing approximately the average cost of an iteration is enough, without needing to know the “topology”
* of the trove list. It also avoids the need to set the cap in stone in the contract, nor doing gas calculations, as both gas price and opcode
* costs can vary.
*
* All Troves that are redeemed from -- with the likely exception of the last one -- will end up with no debt left, therefore they will be closed.
* If the last Trove does have some remaining debt, it has a finite ICR, and the reinsertion could be anywhere in the list, therefore it requires a hint.
* A frontend should use getRedemptionHints() to calculate what the ICR of this Trove will be after redemption, and pass a hint for its position
* in the sortedTroves list along with the ICR value that the hint was found for.
*
* If another transaction modifies the list between calling getRedemptionHints() and passing the hints to redeemCollateral(), it
* is very likely that the last (partially) redeemed Trove would end up with a different ICR than what the hint is for. In this case the
* redemption will stop after the last completely redeemed Trove and the sender will keep the remaining ZUSD amount, which they can attempt
* to redeem later.
*
* @param _ZUSDAmount ZUSD amount to send to the system
* @param _firstRedemptionHint calculated ICR hint of first trove after redemption
* @param _maxIterations max Troves iterations (can be 0)
* @param _maxFee max fee percentage to accept
*/
function redeemCollateral(
uint256 _ZUSDAmount,
address _firstRedemptionHint,
address _upperPartialRedemptionHint,
address _lowerPartialRedemptionHint,
uint256 _partialRedemptionHintNICR,
uint256 _maxIterations,
uint256 _maxFee
) external;
function redeemCollateralViaDLLR(
uint256 _dllrAmount,
address _firstRedemptionHint,
address _upperPartialRedemptionHint,
address _lowerPartialRedemptionHint,
uint256 _partialRedemptionHintNICR,
uint256 _maxIterations,
uint256 _maxFeePercentage,
IMassetManager.PermitParams calldata _permitParams
) external;
function redeemCollateralViaDllrWithPermit2(
uint256 _dllrAmount,
address _firstRedemptionHint,
address _upperPartialRedemptionHint,
address _lowerPartialRedemptionHint,
uint256 _partialRedemptionHintNICR,
uint256 _maxIterations,
uint256 _maxFeePercentage,
ISignatureTransfer.PermitTransferFrom memory _permit,
bytes calldata _signature
) external;
/// @notice Update borrower's stake based on their latest collateral value
/// @param _borrower borrower address
function updateStakeAndTotalStakes(address _borrower) external returns (uint256);
/// @notice Update borrower's snapshots of L_ETH and L_ZUSDDebt to reflect the current values
/// @param _borrower borrower address
function updateTroveRewardSnapshots(address _borrower) external;
/// @notice Push the owner's address to the Trove owners list, and record the corresponding array index on the Trove struct
/// @param _borrower borrower address
/// @return index where Trove was inserted
function addTroveOwnerToArray(address _borrower) external returns (uint256 index);
/// @notice Add the borrowers's coll and debt rewards earned from redistributions, to their Trove
/// @param _borrower borrower address
function applyPendingRewards(address _borrower) external;
/// @param _borrower borrower address
/// @return the borrower's pending accumulated ETH reward, earned by their stake
function getPendingETHReward(address _borrower) external view returns (uint256);
/// @param _borrower borrower address
/// @return the borrower's pending accumulated ZUSD reward, earned by their stake
function getPendingZUSDDebtReward(address _borrower) external view returns (uint256);
/*
* @notice A Trove has pending rewards if its snapshot is less than the current rewards per-unit-staked sum:
* this indicates that rewards have occured since the snapshot was made, and the user therefore has
* pending rewards
*
* @param _borrower borrower address
* @return true if has pending rewards
*/
function hasPendingRewards(address _borrower) external view returns (bool);
/// @notice returns the Troves entire debt and coll, including pending rewards from redistributions.
/// @param _borrower borrower address
function getEntireDebtAndColl(
address _borrower
)
external
view
returns (
uint256 debt,
uint256 coll,
uint256 pendingZUSDDebtReward,
uint256 pendingETHReward
);
/// @notice Close given trove. Called by BorrowerOperations.
/// @param _borrower borrower address
function closeTrove(address _borrower) external;
/// @notice Remove borrower's stake from the totalStakes sum, and set their stake to 0
/// @param _borrower borrower address
function removeStake(address _borrower) external;
/// @return calculated redemption rate using baseRate
function getRedemptionRate() external view returns (uint256);
/// @return calculated redemption rate using calculated decayed as base rate
function getRedemptionRateWithDecay() external view returns (uint256);
/// @notice The redemption fee is taken as a cut of the total ETH drawn from the system in a redemption. It is based on the current redemption rate.
/// @param _ETHDrawn ETH drawn
function getRedemptionFeeWithDecay(uint256 _ETHDrawn) external view returns (uint256);
/// @return borrowing rate
function getBorrowingRate() external view returns (uint256);
/// @return borrowing rate calculated using decayed as base rate
function getBorrowingRateWithDecay() external view returns (uint256);
/// @param ZUSDDebt ZUSD debt amount to calculate fee
/// @return borrowing fee using borrowing rate
function getBorrowingFee(uint256 ZUSDDebt) external view returns (uint256);
/// @param _ZUSDDebt ZUSD debt amount to calculate fee
/// @return borrowing fee using borrowing rate with decay
function getBorrowingFeeWithDecay(uint256 _ZUSDDebt) external view returns (uint256);
/// @notice Updates the baseRate state variable based on time elapsed since the last redemption or ZUSD borrowing operation.
function decayBaseRateFromBorrowing() external;
/// @param _borrower borrower address
/// @return Trove status from given trove
function getTroveStatus(address _borrower) external view returns (uint256);
/// @param _borrower borrower address
/// @return Trove stake from given trove
function getTroveStake(address _borrower) external view returns (uint256);
/// @param _borrower borrower address
/// @return Trove debt from given trove
function getTroveDebt(address _borrower) external view returns (uint256);
/// @param _borrower borrower address
/// @return Trove collateral from given trove
function getTroveColl(address _borrower) external view returns (uint256);
/// @param _borrower borrower address
/// @param num status to set
function setTroveStatus(address _borrower, uint256 num) external;
/// @param _borrower borrower address
/// @param _collIncrease amount of collateral to increase
/// @return new trove collateral
function increaseTroveColl(
address _borrower,
uint256 _collIncrease
) external returns (uint256);
/// @param _borrower borrower address
/// @param _collDecrease amount of collateral to decrease
/// @return new trove collateral
function decreaseTroveColl(
address _borrower,
uint256 _collDecrease
) external returns (uint256);
/// @param _borrower borrower address
/// @param _debtIncrease amount of debt to increase
/// @return new trove debt
function increaseTroveDebt(
address _borrower,
uint256 _debtIncrease
) external returns (uint256);
/// @param _borrower borrower address
/// @param _debtDecrease amount of debt to decrease
/// @return new trove debt
function decreaseTroveDebt(
address _borrower,
uint256 _debtDecrease
) external returns (uint256);
/**
* @param _price ETH price
* @return the total collateralization ratio (TCR) of the system.
* The TCR is based on the the entire system debt and collateral (including pending rewards).
*/
function getTCR(uint256 _price) external view returns (uint256);
function MCR() external view returns (uint256);
function CCR() external view returns (uint256);
/// @notice reveals whether or not the system is in Recovery Mode (i.e. whether the Total Collateralization Ratio (TCR) is below the Critical Collateralization Ratio (CCR)).
function checkRecoveryMode(uint256 _price) external view returns (bool);
}