-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTroveManager.sol
More file actions
1212 lines (1049 loc) · 47.6 KB
/
TroveManager.sol
File metadata and controls
1212 lines (1049 loc) · 47.6 KB
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: MIT
pragma solidity 0.6.11;
pragma experimental ABIEncoderV2;
import "./Interfaces/ITroveManager.sol";
import "./Interfaces/IStabilityPool.sol";
import "./Interfaces/ICollSurplusPool.sol";
import "./Interfaces/IZUSDToken.sol";
import "./Interfaces/ISortedTroves.sol";
import "./Interfaces/IZEROToken.sol";
import "./Interfaces/IZEROStaking.sol";
import "./Interfaces/IFeeDistributor.sol";
import "./Dependencies/LiquityBase.sol";
import "./Dependencies/CheckContract.sol";
import "./Dependencies/console.sol";
import "./Dependencies/TroveManagerBase.sol";
import "./TroveManagerStorage.sol";
import "./Interfaces/IPermit2.sol";
contract TroveManager is TroveManagerBase, CheckContract, ITroveManager {
/** CONSTANT / IMMUTABLE VARIABLE ONLY */
IPermit2 public immutable permit2;
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);
///@param _bootstrapPeriod During bootsrap period redemptions are not allowed
constructor(uint256 _bootstrapPeriod, address _permit2) public TroveManagerBase(_bootstrapPeriod) {
permit2 = IPermit2(_permit2);
}
// --- Dependency setter ---
function setAddresses(
TroveManagerInitAddressesParams memory _troveManagerInitAddressesParams
) external override onlyOwner {
{
checkContract(_troveManagerInitAddressesParams._feeDistributorAddress);
checkContract(_troveManagerInitAddressesParams._troveManagerRedeemOps);
checkContract(_troveManagerInitAddressesParams._liquityBaseParamsAddress);
checkContract(_troveManagerInitAddressesParams._borrowerOperationsAddress);
checkContract(_troveManagerInitAddressesParams._activePoolAddress);
checkContract(_troveManagerInitAddressesParams._defaultPoolAddress);
checkContract(_troveManagerInitAddressesParams._stabilityPoolAddress);
checkContract(_troveManagerInitAddressesParams._gasPoolAddress);
checkContract(_troveManagerInitAddressesParams._collSurplusPoolAddress);
checkContract(_troveManagerInitAddressesParams._priceFeedAddress);
checkContract(_troveManagerInitAddressesParams._zusdTokenAddress);
checkContract(_troveManagerInitAddressesParams._sortedTrovesAddress);
checkContract(_troveManagerInitAddressesParams._zeroTokenAddress);
checkContract(_troveManagerInitAddressesParams._zeroStakingAddress);
}
feeDistributor = IFeeDistributor(_troveManagerInitAddressesParams._feeDistributorAddress);
troveManagerRedeemOps = _troveManagerInitAddressesParams._troveManagerRedeemOps;
liquityBaseParams = ILiquityBaseParams(
_troveManagerInitAddressesParams._liquityBaseParamsAddress
);
{
borrowerOperationsAddress = _troveManagerInitAddressesParams
._borrowerOperationsAddress;
activePool = IActivePool(_troveManagerInitAddressesParams._activePoolAddress);
defaultPool = IDefaultPool(_troveManagerInitAddressesParams._defaultPoolAddress);
_stabilityPool = IStabilityPool(
_troveManagerInitAddressesParams._stabilityPoolAddress
);
gasPoolAddress = _troveManagerInitAddressesParams._gasPoolAddress;
collSurplusPool = ICollSurplusPool(
_troveManagerInitAddressesParams._collSurplusPoolAddress
);
priceFeed = IPriceFeed(_troveManagerInitAddressesParams._priceFeedAddress);
_zusdToken = IZUSDToken(_troveManagerInitAddressesParams._zusdTokenAddress);
sortedTroves = ISortedTroves(_troveManagerInitAddressesParams._sortedTrovesAddress);
_zeroToken = IZEROToken(_troveManagerInitAddressesParams._zeroTokenAddress);
_zeroStaking = IZEROStaking(_troveManagerInitAddressesParams._zeroStakingAddress);
}
emit FeeDistributorAddressChanged(_troveManagerInitAddressesParams._feeDistributorAddress);
emit TroveManagerRedeemOpsAddressChanged(
_troveManagerInitAddressesParams._troveManagerRedeemOps
);
emit LiquityBaseParamsAddressChanges(
_troveManagerInitAddressesParams._borrowerOperationsAddress
);
emit BorrowerOperationsAddressChanged(
_troveManagerInitAddressesParams._borrowerOperationsAddress
);
emit ActivePoolAddressChanged(_troveManagerInitAddressesParams._activePoolAddress);
emit DefaultPoolAddressChanged(_troveManagerInitAddressesParams._defaultPoolAddress);
emit StabilityPoolAddressChanged(_troveManagerInitAddressesParams._stabilityPoolAddress);
emit GasPoolAddressChanged(_troveManagerInitAddressesParams._gasPoolAddress);
emit CollSurplusPoolAddressChanged(
_troveManagerInitAddressesParams._collSurplusPoolAddress
);
emit PriceFeedAddressChanged(_troveManagerInitAddressesParams._priceFeedAddress);
emit ZUSDTokenAddressChanged(_troveManagerInitAddressesParams._zusdTokenAddress);
emit SortedTrovesAddressChanged(_troveManagerInitAddressesParams._sortedTrovesAddress);
emit ZEROTokenAddressChanged(_troveManagerInitAddressesParams._zeroTokenAddress);
emit ZEROStakingAddressChanged(_troveManagerInitAddressesParams._zeroStakingAddress);
}
function setTroveManagerRedeemOps(address _troveManagerRedeemOps) external override onlyOwner {
checkContract(_troveManagerRedeemOps);
troveManagerRedeemOps = _troveManagerRedeemOps;
emit TroveManagerRedeemOpsAddressChanged(_troveManagerRedeemOps);
}
// --- Getters ---
function getTroveOwnersCount() external view override returns (uint256) {
return TroveOwners.length;
}
function getTroveFromTroveOwnersArray(
uint256 _index
) external view override returns (address) {
return TroveOwners[_index];
}
// --- Trove Liquidation functions ---
/// Single liquidation function. Closes the trove if its ICR is lower than the minimum collateral ratio.
function liquidate(address _borrower) external override {
_requireTroveIsActive(_borrower);
address[] memory borrowers = new address[](1);
borrowers[0] = _borrower;
batchLiquidateTroves(borrowers);
}
// --- Inner single liquidation functions ---
/// Liquidate one trove, in Normal Mode.
function _liquidateNormalMode(
IActivePool _activePool,
IDefaultPool _defaultPool,
address _borrower,
uint256 _ZUSDInStabPool
) internal returns (LiquidationValues memory singleLiquidation) {
LocalVariables_InnerSingleLiquidateFunction memory vars;
(
singleLiquidation.entireTroveDebt,
singleLiquidation.entireTroveColl,
vars.pendingDebtReward,
vars.pendingCollReward
) = getEntireDebtAndColl(_borrower);
_movePendingTroveRewardsToActivePool(
_activePool,
_defaultPool,
vars.pendingDebtReward,
vars.pendingCollReward
);
_removeStake(_borrower);
singleLiquidation.collGasCompensation = _getCollGasCompensation(
singleLiquidation.entireTroveColl
);
singleLiquidation.ZUSDGasCompensation = ZUSD_GAS_COMPENSATION;
uint256 collToLiquidate = singleLiquidation.entireTroveColl.sub(
singleLiquidation.collGasCompensation
);
(
singleLiquidation.debtToOffset,
singleLiquidation.collToSendToSP,
singleLiquidation.debtToRedistribute,
singleLiquidation.collToRedistribute
) = _getOffsetAndRedistributionVals(
singleLiquidation.entireTroveDebt,
collToLiquidate,
_ZUSDInStabPool
);
_closeTrove(_borrower, Status.closedByLiquidation);
emit TroveLiquidated(
_borrower,
singleLiquidation.entireTroveDebt,
singleLiquidation.entireTroveColl,
TroveManagerOperation.liquidateInNormalMode
);
emit TroveUpdated(_borrower, 0, 0, 0, TroveManagerOperation.liquidateInNormalMode);
return singleLiquidation;
}
/// Liquidate one trove, in Recovery Mode.
function _liquidateRecoveryMode(
IActivePool _activePool,
IDefaultPool _defaultPool,
address _borrower,
uint256 _ICR,
uint256 _ZUSDInStabPool,
uint256 _TCR,
uint256 _price
) internal returns (LiquidationValues memory singleLiquidation) {
LocalVariables_InnerSingleLiquidateFunction memory vars;
if (TroveOwners.length <= 1) {
return singleLiquidation;
} // don't liquidate if last trove
(
singleLiquidation.entireTroveDebt,
singleLiquidation.entireTroveColl,
vars.pendingDebtReward,
vars.pendingCollReward
) = getEntireDebtAndColl(_borrower);
singleLiquidation.collGasCompensation = _getCollGasCompensation(
singleLiquidation.entireTroveColl
);
singleLiquidation.ZUSDGasCompensation = ZUSD_GAS_COMPENSATION;
vars.collToLiquidate = singleLiquidation.entireTroveColl.sub(
singleLiquidation.collGasCompensation
);
// If ICR <= 100%, purely redistribute the Trove across all active Troves
if (_ICR <= _100pct) {
_movePendingTroveRewardsToActivePool(
_activePool,
_defaultPool,
vars.pendingDebtReward,
vars.pendingCollReward
);
_removeStake(_borrower);
singleLiquidation.debtToOffset = 0;
singleLiquidation.collToSendToSP = 0;
singleLiquidation.debtToRedistribute = singleLiquidation.entireTroveDebt;
singleLiquidation.collToRedistribute = vars.collToLiquidate;
_closeTrove(_borrower, Status.closedByLiquidation);
emit TroveLiquidated(
_borrower,
singleLiquidation.entireTroveDebt,
singleLiquidation.entireTroveColl,
TroveManagerOperation.liquidateInRecoveryMode
);
emit TroveUpdated(_borrower, 0, 0, 0, TroveManagerOperation.liquidateInRecoveryMode);
// If 100% < ICR < MCR, offset as much as possible, and redistribute the remainder
} else if ((_ICR > _100pct) && (_ICR < liquityBaseParams.MCR())) {
_movePendingTroveRewardsToActivePool(
_activePool,
_defaultPool,
vars.pendingDebtReward,
vars.pendingCollReward
);
_removeStake(_borrower);
(
singleLiquidation.debtToOffset,
singleLiquidation.collToSendToSP,
singleLiquidation.debtToRedistribute,
singleLiquidation.collToRedistribute
) = _getOffsetAndRedistributionVals(
singleLiquidation.entireTroveDebt,
vars.collToLiquidate,
_ZUSDInStabPool
);
_closeTrove(_borrower, Status.closedByLiquidation);
emit TroveLiquidated(
_borrower,
singleLiquidation.entireTroveDebt,
singleLiquidation.entireTroveColl,
TroveManagerOperation.liquidateInRecoveryMode
);
emit TroveUpdated(_borrower, 0, 0, 0, TroveManagerOperation.liquidateInRecoveryMode);
/*
* If 110% <= ICR < current TCR (accounting for the preceding liquidations in the current sequence)
* and there is ZUSD in the Stability Pool, only offset, with no redistribution,
* but at a capped rate of 1.1 and only if the whole debt can be liquidated.
* The remainder due to the capped rate will be claimable as collateral surplus.
*/
} else if (
(_ICR >= liquityBaseParams.MCR()) &&
(_ICR < _TCR) &&
(singleLiquidation.entireTroveDebt <= _ZUSDInStabPool)
) {
_movePendingTroveRewardsToActivePool(
_activePool,
_defaultPool,
vars.pendingDebtReward,
vars.pendingCollReward
);
assert(_ZUSDInStabPool != 0);
_removeStake(_borrower);
singleLiquidation = _getCappedOffsetVals(
singleLiquidation.entireTroveDebt,
singleLiquidation.entireTroveColl,
_price
);
_closeTrove(_borrower, Status.closedByLiquidation);
if (singleLiquidation.collSurplus > 0) {
collSurplusPool.accountSurplus(_borrower, singleLiquidation.collSurplus);
}
emit TroveLiquidated(
_borrower,
singleLiquidation.entireTroveDebt,
singleLiquidation.collToSendToSP,
TroveManagerOperation.liquidateInRecoveryMode
);
emit TroveUpdated(_borrower, 0, 0, 0, TroveManagerOperation.liquidateInRecoveryMode);
} else {
// if (_ICR >= liquityBaseParams.MCR() && ( _ICR >= _TCR || singleLiquidation.entireTroveDebt > _ZUSDInStabPool))
LiquidationValues memory zeroVals;
return zeroVals;
}
return singleLiquidation;
}
/** In a full liquidation, returns the values for a trove's coll and debt to be offset, and coll and debt to be
* redistributed to active troves.
*/
function _getOffsetAndRedistributionVals(
uint256 _debt,
uint256 _coll,
uint256 _ZUSDInStabPool
)
internal
pure
returns (
uint256 debtToOffset,
uint256 collToSendToSP,
uint256 debtToRedistribute,
uint256 collToRedistribute
)
{
if (_ZUSDInStabPool > 0) {
/*
* Offset as much debt & collateral as possible against the Stability Pool, and redistribute the remainder
* between all active troves.
*
* If the trove's debt is larger than the deposited ZUSD in the Stability Pool:
*
* - Offset an amount of the trove's debt equal to the ZUSD in the Stability Pool
* - Send a fraction of the trove's collateral to the Stability Pool, equal to the fraction of its offset debt
*
*/
debtToOffset = LiquityMath._min(_debt, _ZUSDInStabPool);
collToSendToSP = _coll.mul(debtToOffset).div(_debt);
debtToRedistribute = _debt.sub(debtToOffset);
collToRedistribute = _coll.sub(collToSendToSP);
} else {
debtToOffset = 0;
collToSendToSP = 0;
debtToRedistribute = _debt;
collToRedistribute = _coll;
}
}
/**
* Get its offset coll/debt and ETH gas comp, and close the trove.
*/
function _getCappedOffsetVals(
uint256 _entireTroveDebt,
uint256 _entireTroveColl,
uint256 _price
) internal view returns (LiquidationValues memory singleLiquidation) {
singleLiquidation.entireTroveDebt = _entireTroveDebt;
singleLiquidation.entireTroveColl = _entireTroveColl;
uint256 collToOffset = _entireTroveDebt.mul(liquityBaseParams.MCR()).div(_price);
singleLiquidation.collGasCompensation = _getCollGasCompensation(collToOffset);
singleLiquidation.ZUSDGasCompensation = ZUSD_GAS_COMPENSATION;
singleLiquidation.debtToOffset = _entireTroveDebt;
singleLiquidation.collToSendToSP = collToOffset.sub(singleLiquidation.collGasCompensation);
singleLiquidation.collSurplus = _entireTroveColl.sub(collToOffset);
singleLiquidation.debtToRedistribute = 0;
singleLiquidation.collToRedistribute = 0;
}
/**
* 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
*/
function liquidateTroves(uint256 _n) external override {
ContractsCache memory contractsCache = ContractsCache(
activePool,
defaultPool,
IZUSDToken(address(0)),
IZEROStaking(address(0)),
sortedTroves,
ICollSurplusPool(address(0)),
address(0)
);
IStabilityPool stabilityPoolCached = _stabilityPool;
LocalVariables_OuterLiquidationFunction memory vars;
LiquidationTotals memory totals;
vars.price = priceFeed.fetchPrice();
vars.ZUSDInStabPool = stabilityPoolCached.getTotalZUSDDeposits();
vars.recoveryModeAtStart = _checkRecoveryMode(vars.price);
// Perform the appropriate liquidation sequence - tally the values, and obtain their totals
if (vars.recoveryModeAtStart) {
totals = _getTotalsFromLiquidateTrovesSequence_RecoveryMode(
contractsCache,
vars.price,
vars.ZUSDInStabPool,
_n
);
} else {
// if !vars.recoveryModeAtStart
totals = _getTotalsFromLiquidateTrovesSequence_NormalMode(
contractsCache.activePool,
contractsCache.defaultPool,
vars.price,
vars.ZUSDInStabPool,
_n
);
}
require(totals.totalDebtInSequence > 0, "TroveManager: nothing to liquidate");
// Move liquidated ETH and ZUSD to the appropriate pools
stabilityPoolCached.offset(totals.totalDebtToOffset, totals.totalCollToSendToSP);
_redistributeDebtAndColl(
contractsCache.activePool,
contractsCache.defaultPool,
totals.totalDebtToRedistribute,
totals.totalCollToRedistribute
);
if (totals.totalCollSurplus > 0) {
contractsCache.activePool.sendETH(address(collSurplusPool), totals.totalCollSurplus);
}
// Update system snapshots
_updateSystemSnapshots_excludeCollRemainder(
contractsCache.activePool,
totals.totalCollGasCompensation
);
vars.liquidatedDebt = totals.totalDebtInSequence;
vars.liquidatedColl = totals.totalCollInSequence.sub(totals.totalCollGasCompensation).sub(
totals.totalCollSurplus
);
emit Liquidation(
vars.liquidatedDebt,
vars.liquidatedColl,
totals.totalCollGasCompensation,
totals.totalZUSDGasCompensation
);
// Send gas compensation to caller
_sendGasCompensation(
contractsCache.activePool,
msg.sender,
totals.totalZUSDGasCompensation,
totals.totalCollGasCompensation
);
}
/**
* This function is used when the liquidateTroves sequence starts during Recovery Mode. However, it
* handle the case where the system *leaves* Recovery Mode, part way through the liquidation sequence
*/
function _getTotalsFromLiquidateTrovesSequence_RecoveryMode(
ContractsCache memory _contractsCache,
uint256 _price,
uint256 _ZUSDInStabPool,
uint256 _n
) internal returns (LiquidationTotals memory totals) {
LocalVariables_LiquidationSequence memory vars;
LiquidationValues memory singleLiquidation;
vars.remainingZUSDInStabPool = _ZUSDInStabPool;
vars.backToNormalMode = false;
vars.entireSystemDebt = getEntireSystemDebt();
vars.entireSystemColl = getEntireSystemColl();
vars.user = _contractsCache.sortedTroves.getLast();
address firstUser = _contractsCache.sortedTroves.getFirst();
for (vars.i = 0; vars.i < _n && vars.user != firstUser; vars.i++) {
// we need to cache it, because current user is likely going to be deleted
address nextUser = _contractsCache.sortedTroves.getPrev(vars.user);
vars.ICR = _getCurrentICR(vars.user, _price);
if (!vars.backToNormalMode) {
// Break the loop if ICR is greater than liquityBaseParams.MCR() and Stability Pool is empty
if (vars.ICR >= liquityBaseParams.MCR() && vars.remainingZUSDInStabPool == 0) {
break;
}
uint256 TCR = LiquityMath._computeCR(
vars.entireSystemColl,
vars.entireSystemDebt,
_price
);
singleLiquidation = _liquidateRecoveryMode(
_contractsCache.activePool,
_contractsCache.defaultPool,
vars.user,
vars.ICR,
vars.remainingZUSDInStabPool,
TCR,
_price
);
// Update aggregate trackers
vars.remainingZUSDInStabPool = vars.remainingZUSDInStabPool.sub(
singleLiquidation.debtToOffset
);
vars.entireSystemDebt = vars.entireSystemDebt.sub(singleLiquidation.debtToOffset);
vars.entireSystemColl = vars
.entireSystemColl
.sub(singleLiquidation.collToSendToSP)
.sub(singleLiquidation.collSurplus);
// Add liquidation values to their respective running totals
totals = _addLiquidationValuesToTotals(totals, singleLiquidation);
vars.backToNormalMode = !_checkPotentialRecoveryMode(
vars.entireSystemColl,
vars.entireSystemDebt,
_price
);
} else if (vars.backToNormalMode && vars.ICR < liquityBaseParams.MCR()) {
singleLiquidation = _liquidateNormalMode(
_contractsCache.activePool,
_contractsCache.defaultPool,
vars.user,
vars.remainingZUSDInStabPool
);
vars.remainingZUSDInStabPool = vars.remainingZUSDInStabPool.sub(
singleLiquidation.debtToOffset
);
// Add liquidation values to their respective running totals
totals = _addLiquidationValuesToTotals(totals, singleLiquidation);
} else break; // break if the loop reaches a Trove with ICR >= MCR
vars.user = nextUser;
}
}
function _getTotalsFromLiquidateTrovesSequence_NormalMode(
IActivePool _activePool,
IDefaultPool _defaultPool,
uint256 _price,
uint256 _ZUSDInStabPool,
uint256 _n
) internal returns (LiquidationTotals memory totals) {
LocalVariables_LiquidationSequence memory vars;
LiquidationValues memory singleLiquidation;
ISortedTroves sortedTrovesCached = sortedTroves;
vars.remainingZUSDInStabPool = _ZUSDInStabPool;
for (vars.i = 0; vars.i < _n; vars.i++) {
vars.user = sortedTrovesCached.getLast();
vars.ICR = _getCurrentICR(vars.user, _price);
if (vars.ICR < liquityBaseParams.MCR()) {
singleLiquidation = _liquidateNormalMode(
_activePool,
_defaultPool,
vars.user,
vars.remainingZUSDInStabPool
);
vars.remainingZUSDInStabPool = vars.remainingZUSDInStabPool.sub(
singleLiquidation.debtToOffset
);
// Add liquidation values to their respective running totals
totals = _addLiquidationValuesToTotals(totals, singleLiquidation);
} else break; // break if the loop reaches a Trove with ICR >= MCR
}
}
/**
* Attempt to liquidate a custom list of troves provided by the caller.
*/
function batchLiquidateTroves(address[] memory _troveArray) public override {
require(_troveArray.length != 0, "TroveManager: Calldata address array must not be empty");
IActivePool activePoolCached = activePool;
IDefaultPool defaultPoolCached = defaultPool;
IStabilityPool stabilityPoolCached = _stabilityPool;
LocalVariables_OuterLiquidationFunction memory vars;
LiquidationTotals memory totals;
vars.price = priceFeed.fetchPrice();
vars.ZUSDInStabPool = stabilityPoolCached.getTotalZUSDDeposits();
vars.recoveryModeAtStart = _checkRecoveryMode(vars.price);
// Perform the appropriate liquidation sequence - tally values and obtain their totals.
if (vars.recoveryModeAtStart) {
totals = _getTotalFromBatchLiquidate_RecoveryMode(
activePoolCached,
defaultPoolCached,
vars.price,
vars.ZUSDInStabPool,
_troveArray
);
} else {
// if !vars.recoveryModeAtStart
totals = _getTotalsFromBatchLiquidate_NormalMode(
activePoolCached,
defaultPoolCached,
vars.price,
vars.ZUSDInStabPool,
_troveArray
);
}
require(totals.totalDebtInSequence > 0, "TroveManager: nothing to liquidate");
// Move liquidated ETH and ZUSD to the appropriate pools
stabilityPoolCached.offset(totals.totalDebtToOffset, totals.totalCollToSendToSP);
_redistributeDebtAndColl(
activePoolCached,
defaultPoolCached,
totals.totalDebtToRedistribute,
totals.totalCollToRedistribute
);
if (totals.totalCollSurplus > 0) {
activePoolCached.sendETH(address(collSurplusPool), totals.totalCollSurplus);
}
// Update system snapshots
_updateSystemSnapshots_excludeCollRemainder(
activePoolCached,
totals.totalCollGasCompensation
);
vars.liquidatedDebt = totals.totalDebtInSequence;
vars.liquidatedColl = totals.totalCollInSequence.sub(totals.totalCollGasCompensation).sub(
totals.totalCollSurplus
);
emit Liquidation(
vars.liquidatedDebt,
vars.liquidatedColl,
totals.totalCollGasCompensation,
totals.totalZUSDGasCompensation
);
// Send gas compensation to caller
_sendGasCompensation(
activePoolCached,
msg.sender,
totals.totalZUSDGasCompensation,
totals.totalCollGasCompensation
);
}
/**
* This function is used when the batch liquidation sequence starts during Recovery Mode. However, it
* handle the case where the system *leaves* Recovery Mode, part way through the liquidation sequence
*/
function _getTotalFromBatchLiquidate_RecoveryMode(
IActivePool _activePool,
IDefaultPool _defaultPool,
uint256 _price,
uint256 _ZUSDInStabPool,
address[] memory _troveArray
) internal returns (LiquidationTotals memory totals) {
LocalVariables_LiquidationSequence memory vars;
LiquidationValues memory singleLiquidation;
vars.remainingZUSDInStabPool = _ZUSDInStabPool;
vars.backToNormalMode = false;
vars.entireSystemDebt = getEntireSystemDebt();
vars.entireSystemColl = getEntireSystemColl();
for (vars.i = 0; vars.i < _troveArray.length; vars.i++) {
vars.user = _troveArray[vars.i];
// Skip non-active troves
if (Troves[vars.user].status != Status.active) {
continue;
}
vars.ICR = _getCurrentICR(vars.user, _price);
if (!vars.backToNormalMode) {
// Skip this trove if ICR is greater than liquityBaseParams.MCR() and Stability Pool is empty
if (vars.ICR >= liquityBaseParams.MCR() && vars.remainingZUSDInStabPool == 0) {
continue;
}
uint256 TCR = LiquityMath._computeCR(
vars.entireSystemColl,
vars.entireSystemDebt,
_price
);
singleLiquidation = _liquidateRecoveryMode(
_activePool,
_defaultPool,
vars.user,
vars.ICR,
vars.remainingZUSDInStabPool,
TCR,
_price
);
// Update aggregate trackers
vars.remainingZUSDInStabPool = vars.remainingZUSDInStabPool.sub(
singleLiquidation.debtToOffset
);
vars.entireSystemDebt = vars.entireSystemDebt.sub(singleLiquidation.debtToOffset);
vars.entireSystemColl = vars.entireSystemColl.sub(
singleLiquidation.collToSendToSP
);
// Add liquidation values to their respective running totals
totals = _addLiquidationValuesToTotals(totals, singleLiquidation);
vars.backToNormalMode = !_checkPotentialRecoveryMode(
vars.entireSystemColl,
vars.entireSystemDebt,
_price
);
} else if (vars.backToNormalMode && vars.ICR < liquityBaseParams.MCR()) {
singleLiquidation = _liquidateNormalMode(
_activePool,
_defaultPool,
vars.user,
vars.remainingZUSDInStabPool
);
vars.remainingZUSDInStabPool = vars.remainingZUSDInStabPool.sub(
singleLiquidation.debtToOffset
);
// Add liquidation values to their respective running totals
totals = _addLiquidationValuesToTotals(totals, singleLiquidation);
} else continue; // In Normal Mode skip troves with ICR >= MCR
}
}
function _getTotalsFromBatchLiquidate_NormalMode(
IActivePool _activePool,
IDefaultPool _defaultPool,
uint256 _price,
uint256 _ZUSDInStabPool,
address[] memory _troveArray
) internal returns (LiquidationTotals memory totals) {
LocalVariables_LiquidationSequence memory vars;
LiquidationValues memory singleLiquidation;
vars.remainingZUSDInStabPool = _ZUSDInStabPool;
for (vars.i = 0; vars.i < _troveArray.length; vars.i++) {
vars.user = _troveArray[vars.i];
vars.ICR = _getCurrentICR(vars.user, _price);
if (vars.ICR < liquityBaseParams.MCR()) {
singleLiquidation = _liquidateNormalMode(
_activePool,
_defaultPool,
vars.user,
vars.remainingZUSDInStabPool
);
vars.remainingZUSDInStabPool = vars.remainingZUSDInStabPool.sub(
singleLiquidation.debtToOffset
);
// Add liquidation values to their respective running totals
totals = _addLiquidationValuesToTotals(totals, singleLiquidation);
}
}
}
// --- Liquidation helper functions ---
function _addLiquidationValuesToTotals(
LiquidationTotals memory oldTotals,
LiquidationValues memory singleLiquidation
) internal pure returns (LiquidationTotals memory newTotals) {
// Tally all the values with their respective running totals
newTotals.totalCollGasCompensation = oldTotals.totalCollGasCompensation.add(
singleLiquidation.collGasCompensation
);
newTotals.totalZUSDGasCompensation = oldTotals.totalZUSDGasCompensation.add(
singleLiquidation.ZUSDGasCompensation
);
newTotals.totalDebtInSequence = oldTotals.totalDebtInSequence.add(
singleLiquidation.entireTroveDebt
);
newTotals.totalCollInSequence = oldTotals.totalCollInSequence.add(
singleLiquidation.entireTroveColl
);
newTotals.totalDebtToOffset = oldTotals.totalDebtToOffset.add(
singleLiquidation.debtToOffset
);
newTotals.totalCollToSendToSP = oldTotals.totalCollToSendToSP.add(
singleLiquidation.collToSendToSP
);
newTotals.totalDebtToRedistribute = oldTotals.totalDebtToRedistribute.add(
singleLiquidation.debtToRedistribute
);
newTotals.totalCollToRedistribute = oldTotals.totalCollToRedistribute.add(
singleLiquidation.collToRedistribute
);
newTotals.totalCollSurplus = oldTotals.totalCollSurplus.add(singleLiquidation.collSurplus);
return newTotals;
}
function _sendGasCompensation(
IActivePool _activePool,
address _liquidator,
uint256 _ZUSD,
uint256 _ETH
) internal {
if (_ZUSD > 0) {
_zusdToken.returnFromPool(gasPoolAddress, _liquidator, _ZUSD);
}
if (_ETH > 0) {
_activePool.sendETH(_liquidator, _ETH);
}
}
// --- Helper functions ---
/// @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) public view override returns (uint256) {
(uint256 currentETH, uint256 currentZUSDDebt) = _getCurrentTroveAmounts(_borrower);
uint256 NICR = LiquityMath._computeNominalCR(currentETH, currentZUSDDebt);
return NICR;
}
function applyPendingRewards(address _borrower) external override {
_requireCallerIsBorrowerOperations();
return _applyPendingRewards(activePool, defaultPool, _borrower);
}
/// Update borrower's snapshots of L_ETH and L_ZUSDDebt to reflect the current values
function updateTroveRewardSnapshots(address _borrower) external override {
_requireCallerIsBorrowerOperations();
return _updateTroveRewardSnapshots(_borrower);
}
/// Return the Troves entire debt and coll, including pending rewards from redistributions.
function getEntireDebtAndColl(
address _borrower
)
public
view
override
returns (
uint256 debt,
uint256 coll,
uint256 pendingZUSDDebtReward,
uint256 pendingETHReward
)
{
debt = Troves[_borrower].debt;
coll = Troves[_borrower].coll;
pendingZUSDDebtReward = getPendingZUSDDebtReward(_borrower);
pendingETHReward = getPendingETHReward(_borrower);
debt = debt.add(pendingZUSDDebtReward);
coll = coll.add(pendingETHReward);
}
function removeStake(address _borrower) external override {
_requireCallerIsBorrowerOperations();
return _removeStake(_borrower);
}
function updateStakeAndTotalStakes(address _borrower) external override returns (uint256) {
_requireCallerIsBorrowerOperations();
return _updateStakeAndTotalStakes(_borrower);
}
function _redistributeDebtAndColl(
IActivePool _activePool,
IDefaultPool _defaultPool,
uint256 _debt,
uint256 _coll
) internal {
if (_debt == 0) {
return;
}
/*
* Add distributed coll and debt rewards-per-unit-staked to the running totals. Division uses a "feedback"
* error correction, to keep the cumulative error low in the running totals L_ETH and L_ZUSDDebt:
*
* 1) Form numerators which compensate for the floor division errors that occurred the last time this
* function was called.
* 2) Calculate "per-unit-staked" ratios.
* 3) Multiply each ratio back by its denominator, to reveal the current floor division error.
* 4) Store these errors for use in the next correction when this function is called.
* 5) Note: static analysis tools complain about this "division before multiplication", however, it is intended.
*/
uint256 ETHNumerator = _coll.mul(DECIMAL_PRECISION).add(lastETHError_Redistribution);
uint256 ZUSDDebtNumerator = _debt.mul(DECIMAL_PRECISION).add(
lastZUSDDebtError_Redistribution
);
// Get the per-unit-staked terms
uint256 ETHRewardPerUnitStaked = ETHNumerator.div(totalStakes);
uint256 ZUSDDebtRewardPerUnitStaked = ZUSDDebtNumerator.div(totalStakes);
lastETHError_Redistribution = ETHNumerator.sub(ETHRewardPerUnitStaked.mul(totalStakes));
lastZUSDDebtError_Redistribution = ZUSDDebtNumerator.sub(
ZUSDDebtRewardPerUnitStaked.mul(totalStakes)
);
// Add per-unit-staked terms to the running totals
L_ETH = L_ETH.add(ETHRewardPerUnitStaked);
L_ZUSDDebt = L_ZUSDDebt.add(ZUSDDebtRewardPerUnitStaked);
emit LTermsUpdated(L_ETH, L_ZUSDDebt);
// Transfer coll and debt from ActivePool to DefaultPool
_activePool.decreaseZUSDDebt(_debt);
_defaultPool.increaseZUSDDebt(_debt);
_activePool.sendETH(address(_defaultPool), _coll);
}
function closeTrove(address _borrower) external override {
_requireCallerIsBorrowerOperations();
return _closeTrove(_borrower, Status.closedByOwner);
}
/**
* Updates snapshots of system total stakes and total collateral, excluding a given collateral remainder from the calculation.
* Used in a liquidation sequence.
*
* The calculation excludes a portion of collateral that is in the ActivePool:
*
* the total ETH gas compensation from the liquidation sequence
*
* The ETH as compensation must be excluded as it is always sent out at the very end of the liquidation sequence.
*/
function _updateSystemSnapshots_excludeCollRemainder(
IActivePool _activePool,
uint256 _collRemainder
) internal {
totalStakesSnapshot = totalStakes;
uint256 activeColl = _activePool.getETH();
uint256 liquidatedColl = defaultPool.getETH();
totalCollateralSnapshot = activeColl.sub(_collRemainder).add(liquidatedColl);
emit SystemSnapshotsUpdated(totalStakesSnapshot, totalCollateralSnapshot);
}
/// Push the owner's address to the Trove owners list, and record the corresponding array index on the Trove struct
function addTroveOwnerToArray(address _borrower) external override returns (uint256 index) {
_requireCallerIsBorrowerOperations();
return _addTroveOwnerToArray(_borrower);
}
function _addTroveOwnerToArray(address _borrower) internal returns (uint128 index) {
/* Max array size is 2**128 - 1, i.e. ~3e30 troves. No risk of overflow, since troves have minimum ZUSD
debt of liquidation reserve plus MIN_NET_DEBT. 3e30 ZUSD dwarfs the value of all wealth in the world ( which is < 1e15 USD). */
// Push the Troveowner to the array
TroveOwners.push(_borrower);
// Record the index of the new Troveowner on their Trove struct
index = uint128(TroveOwners.length.sub(1));
Troves[_borrower].arrayIndex = index;
return index;
}
// --- Recovery Mode and TCR functions ---
function getTCR(uint256 _price) external view override returns (uint256) {
return _getTCR(_price);
}
function MCR() external view override returns (uint256) {
return liquityBaseParams.MCR();
}
function CCR() external view override returns (uint256) {
return liquityBaseParams.CCR();
}