-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathStabilityPool.sol
More file actions
801 lines (660 loc) · 35.6 KB
/
StabilityPool.sol
File metadata and controls
801 lines (660 loc) · 35.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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.24;
import "openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
import "./Interfaces/IStabilityPool.sol";
import "./Interfaces/IAddressesRegistry.sol";
import "./Interfaces/IStabilityPoolEvents.sol";
import "./Interfaces/ITroveManager.sol";
import "./Interfaces/IBoldToken.sol";
import "./Interfaces/ISortedTroves.sol";
import "./Dependencies/LiquityBase.sol";
/*
* The Stability Pool holds Bold tokens deposited by Stability Pool depositors.
*
* When a trove is liquidated, then depending on system conditions, some of its Bold debt gets offset with
* Bold in the Stability Pool: that is, the offset debt evaporates, and an equal amount of Bold tokens in the Stability Pool is burned.
*
* Thus, a liquidation causes each depositor to receive a Bold loss, in proportion to their deposit as a share of total deposits.
* They also receive an Coll gain, as the collateral of the liquidated trove is distributed among Stability depositors,
* in the same proportion.
*
* When a liquidation occurs, it depletes every deposit by the same fraction: for example, a liquidation that depletes 40%
* of the total Bold in the Stability Pool, depletes 40% of each deposit.
*
* A deposit that has experienced a series of liquidations is termed a "compounded deposit": each liquidation depletes the deposit,
* multiplying it by some factor in range ]0,1[
*
*
* --- IMPLEMENTATION ---
*
* We use a highly scalable method of tracking deposits and Coll gains that has O(1) complexity.
*
* When a liquidation occurs, rather than updating each depositor's deposit and Coll gain, we simply update two state variables:
* a product P, and a sum S.
*
* A mathematical manipulation allows us to factor out the initial deposit, and accurately track all depositors' compounded deposits
* and accumulated Coll gains over time, as liquidations occur, using just these two variables P and S. When depositors join the
* Stability Pool, they get a snapshot of the latest P and S: P_t and S_t, respectively.
*
* The formula for a depositor's accumulated Coll gain is derived here:
* https://github.com/liquity/dev/blob/main/papers/Scalable_Reward_Distribution_with_Compounding_Stakes.pdf
*
* For a given deposit d_t, the ratio P/P_t tells us the factor by which a deposit has decreased since it joined the Stability Pool,
* and the term d_t * (S - S_t)/P_t gives us the deposit's total accumulated Coll gain.
*
* Each liquidation updates the product P and sum S. After a series of liquidations, a compounded deposit and corresponding Coll gain
* can be calculated using the initial deposit, the depositor’s snapshots of P and S, and the latest values of P and S.
*
* Any time a depositor updates their deposit (withdrawal, top-up) their accumulated Coll gain is paid out, their new deposit is recorded
* (based on their latest compounded deposit and modified by the withdrawal/top-up), and they receive new snapshots of the latest P and S.
* Essentially, they make a fresh deposit that overwrites the old one.
*
*
* --- SCALE FACTOR ---
*
* Since P is a running product in range ]0,1] that is always-decreasing, it should never reach 0 when multiplied by a number in range ]0,1[.
* Unfortunately, Solidity floor division always reaches 0, sooner or later.
*
* A series of liquidations that nearly empty the Pool (and thus each multiply P by a very small number in range ]0,1[ ) may push P
* to its 18 digit decimal limit, and round it to 0, when in fact the Pool hasn't been emptied: this would break deposit tracking.
*
* So, to track P accurately, we use a scale factor: if a liquidation would cause P to decrease to <1e-9 (and be rounded to 0 by Solidity),
* we first multiply P by 1e9, and increment a currentScale factor by 1.
*
* The added benefit of using 1e9 for the scale factor (rather than 1e18) is that it ensures negligible precision loss close to the
* scale boundary: when P is at its minimum value of 1e9, the relative precision loss in P due to floor division is only on the
* order of 1e-9.
*
* --- EPOCHS ---
*
* Whenever a liquidation fully empties the Stability Pool, all deposits should become 0. However, setting P to 0 would make P be 0
* forever, and break all future reward calculations.
*
* So, every time the Stability Pool is emptied by a liquidation, we reset P = 1 and currentScale = 0, and increment the currentEpoch by 1.
*
* --- TRACKING DEPOSIT OVER SCALE CHANGES AND EPOCHS ---
*
* When a deposit is made, it gets snapshots of the currentEpoch and the currentScale.
*
* When calculating a compounded deposit, we compare the current epoch to the deposit's epoch snapshot. If the current epoch is newer,
* then the deposit was present during a pool-emptying liquidation, and necessarily has been depleted to 0.
*
* Otherwise, we then compare the current scale to the deposit's scale snapshot. If they're equal, the compounded deposit is given by d_t * P/P_t.
* If it spans one scale change, it is given by d_t * P/(P_t * 1e9). If it spans more than one scale change, we define the compounded deposit
* as 0, since it is now less than 1e-9'th of its initial value (e.g. a deposit of 1 billion Bold has depleted to < 1 Bold).
*
*
* --- TRACKING DEPOSITOR'S Coll GAIN OVER SCALE CHANGES AND EPOCHS ---
*
* In the current epoch, the latest value of S is stored upon each scale change, and the mapping (scale -> S) is stored for each epoch.
*
* This allows us to calculate a deposit's accumulated Coll gain, during the epoch in which the deposit was non-zero and earned Coll.
*
* We calculate the depositor's accumulated Coll gain for the scale at which they made the deposit, using the Coll gain formula:
* e_1 = d_t * (S - S_t) / P_t
*
* and also for scale after, taking care to divide the latter by a factor of 1e9:
* e_2 = d_t * S / (P_t * 1e9)
*
* The gain in the second scale will be full, as the starting point was in the previous scale, thus no need to subtract anything.
* The deposit therefore was present for reward events from the beginning of that second scale.
*
* S_i-S_t + S_{i+1}
* .<--------.------------>
* . .
* . S_i . S_{i+1}
* <--.-------->.<----------->
* S_t. .
* <->. .
* t .
* |---+---------|-------------|-----...
* i i+1
*
* The sum of (e_1 + e_2) captures the depositor's total accumulated Coll gain, handling the case where their
* deposit spanned one scale change. We only care about gains across one scale change, since the compounded
* deposit is defined as being 0 once it has spanned more than one scale change.
*
*
* --- UPDATING P WHEN A LIQUIDATION OCCURS ---
*
* Please see the implementation spec in the proof document, which closely follows on from the compounded deposit / Coll gain derivations:
* https://github.com/liquity/liquity/blob/master/papers/Scalable_Reward_Distribution_with_Compounding_Stakes.pdf
*
*
*/
contract StabilityPool is LiquityBase, IStabilityPool, IStabilityPoolEvents {
using SafeERC20 for IERC20;
string public constant NAME = "StabilityPool";
IERC20 public immutable collToken;
ITroveManager public immutable troveManager;
IBoldToken public immutable boldToken;
uint256 internal collBalance; // deposited coll tracker
// Tracker for Bold held in the pool. Changes when users deposit/withdraw, and when Trove debt is offset.
uint256 internal totalBoldDeposits;
// Total remaining Bold yield gains (from Trove interest mints) held by SP, and not yet paid out to depositors
// From the contract's perspective, this is a write-only variable.
uint256 internal yieldGainsOwed;
// Total remaining Bold yield gains (from Trove interest mints) held by SP, not yet paid out to depositors,
// and not accounted for because they were received when the total deposits were too small
uint256 internal yieldGainsPending;
// --- Data structures ---
struct Deposit {
uint256 initialValue;
}
struct Snapshots {
uint256 S; // Coll reward sum liqs
uint256 P;
uint256 B; // Bold reward sum from minted interest
uint128 scale;
uint128 epoch;
}
mapping(address => Deposit) public deposits; // depositor address -> Deposit struct
mapping(address => Snapshots) public depositSnapshots; // depositor address -> snapshots struct
mapping(address => uint256) public stashedColl;
/* Product 'P': Running product by which to multiply an initial deposit, in order to find the current compounded deposit,
* after a series of liquidations have occurred, each of which cancel some Bold debt with the deposit.
*
* During its lifetime, a deposit's value evolves from d_t to d_t * P / P_t , where P_t
* is the snapshot of P taken at the instant the deposit was made. 18-digit decimal.
*/
uint256 public P = DECIMAL_PRECISION;
uint256 public constant SCALE_FACTOR = 1e9;
// Each time the scale of P shifts by SCALE_FACTOR, the scale is incremented by 1
uint128 public currentScale;
// With each offset that fully empties the Pool, the epoch is incremented by 1
uint128 public currentEpoch;
/* Coll Gain sum 'S': During its lifetime, each deposit d_t earns an Coll gain of ( d_t * [S - S_t] )/P_t, where S_t
* is the depositor's snapshot of S taken at the time t when the deposit was made.
*
* The 'S' sums are stored in a nested mapping (epoch => scale => sum):
*
* - The inner mapping records the sum S at different scales
* - The outer mapping records the (scale => sum) mappings, for different epochs.
*/
mapping(uint128 => mapping(uint128 => uint256)) public epochToScaleToS;
mapping(uint128 => mapping(uint128 => uint256)) public epochToScaleToB;
// Error trackers for the error correction in the offset calculation
uint256 public lastCollError_Offset;
uint256 public lastBoldLossError_Offset;
uint256 public lastBoldLossError_TotalDeposits;
// Error tracker fror the error correction in the BOLD reward calculation
uint256 public lastYieldError;
// --- Events ---
event TroveManagerAddressChanged(address _newTroveManagerAddress);
event BoldTokenAddressChanged(address _newBoldTokenAddress);
constructor(IAddressesRegistry _addressesRegistry) LiquityBase(_addressesRegistry) {
collToken = _addressesRegistry.collToken();
troveManager = _addressesRegistry.troveManager();
boldToken = _addressesRegistry.boldToken();
emit TroveManagerAddressChanged(address(troveManager));
emit BoldTokenAddressChanged(address(boldToken));
}
// --- Getters for public variables. Required by IPool interface ---
function getCollBalance() external view override returns (uint256) {
return collBalance;
}
function getTotalBoldDeposits() external view override returns (uint256) {
return totalBoldDeposits;
}
function getYieldGainsOwed() external view override returns (uint256) {
return yieldGainsOwed;
}
function getYieldGainsPending() external view override returns (uint256) {
return yieldGainsPending;
}
// --- External Depositor Functions ---
/* provideToSP():
* - Calculates depositor's Coll gain
* - Calculates the compounded deposit
* - Increases deposit, and takes new snapshots of accumulators P and S
* - Sends depositor's accumulated Coll gains to depositor
*/
function provideToSP(uint256 _topUp, bool _doClaim) external override {
_requireNonZeroAmount(_topUp);
activePool.mintAggInterest();
uint256 initialDeposit = deposits[msg.sender].initialValue;
uint256 currentCollGain = getDepositorCollGain(msg.sender);
uint256 currentYieldGain = getDepositorYieldGain(msg.sender);
uint256 compoundedBoldDeposit = getCompoundedBoldDeposit(msg.sender);
(uint256 keptYieldGain, uint256 yieldGainToSend) = _getYieldToKeepOrSend(currentYieldGain, _doClaim);
uint256 newDeposit = compoundedBoldDeposit + _topUp + keptYieldGain;
(uint256 newStashedColl, uint256 collToSend) =
_getNewStashedCollAndCollToSend(msg.sender, currentCollGain, _doClaim);
emit DepositOperation(
msg.sender,
Operation.provideToSP,
initialDeposit - compoundedBoldDeposit,
int256(_topUp),
currentYieldGain,
yieldGainToSend,
currentCollGain,
collToSend
);
_updateDepositAndSnapshots(msg.sender, newDeposit, newStashedColl);
boldToken.sendToPool(msg.sender, address(this), _topUp);
_updateTotalBoldDeposits(_topUp + keptYieldGain, 0);
_decreaseYieldGainsOwed(currentYieldGain);
_sendBoldtoDepositor(msg.sender, yieldGainToSend);
_sendCollGainToDepositor(collToSend);
// If there were pending yields and with the new deposit we are reaching the threshold, let’s move the yield to owed
_updateYieldRewardsSum(0);
}
function _getYieldToKeepOrSend(uint256 _currentYieldGain, bool _doClaim) internal pure returns (uint256, uint256) {
uint256 yieldToKeep;
uint256 yieldToSend;
if (_doClaim) {
yieldToKeep = 0;
yieldToSend = _currentYieldGain;
} else {
yieldToKeep = _currentYieldGain;
yieldToSend = 0;
}
return (yieldToKeep, yieldToSend);
}
/* withdrawFromSP():
* - Calculates depositor's Coll gain
* - Calculates the compounded deposit
* - Sends the requested BOLD withdrawal to depositor
* - (If _amount > userDeposit, the user withdraws all of their compounded deposit)
* - Decreases deposit by withdrawn amount and takes new snapshots of accumulators P and S
*/
function withdrawFromSP(uint256 _amount, bool _doClaim) external override {
uint256 initialDeposit = deposits[msg.sender].initialValue;
_requireUserHasDeposit(initialDeposit);
activePool.mintAggInterest();
uint256 currentCollGain = getDepositorCollGain(msg.sender);
uint256 currentYieldGain = getDepositorYieldGain(msg.sender);
uint256 compoundedBoldDeposit = getCompoundedBoldDeposit(msg.sender);
uint256 boldToWithdraw = LiquityMath._min(_amount, compoundedBoldDeposit);
(uint256 keptYieldGain, uint256 yieldGainToSend) = _getYieldToKeepOrSend(currentYieldGain, _doClaim);
uint256 newDeposit = compoundedBoldDeposit - boldToWithdraw + keptYieldGain;
(uint256 newStashedColl, uint256 collToSend) =
_getNewStashedCollAndCollToSend(msg.sender, currentCollGain, _doClaim);
emit DepositOperation(
msg.sender,
Operation.withdrawFromSP,
initialDeposit - compoundedBoldDeposit,
-int256(boldToWithdraw),
currentYieldGain,
yieldGainToSend,
currentCollGain,
collToSend
);
_updateDepositAndSnapshots(msg.sender, newDeposit, newStashedColl);
_decreaseYieldGainsOwed(currentYieldGain);
_updateTotalBoldDeposits(keptYieldGain, boldToWithdraw);
_sendBoldtoDepositor(msg.sender, boldToWithdraw + yieldGainToSend);
_sendCollGainToDepositor(collToSend);
// If there were pending yields and with the new deposit we are reaching the threshold, let’s move the yield to owed
// (it may happen if the user is not claiming)
_updateYieldRewardsSum(0);
}
function _getNewStashedCollAndCollToSend(address _depositor, uint256 _currentCollGain, bool _doClaim)
internal
view
returns (uint256 newStashedColl, uint256 collToSend)
{
if (_doClaim) {
newStashedColl = 0;
collToSend = stashedColl[_depositor] + _currentCollGain;
} else {
newStashedColl = stashedColl[_depositor] + _currentCollGain;
collToSend = 0;
}
}
// This function is only needed in the case a user has no deposit but still has remaining stashed Coll gains.
function claimAllCollGains() external {
_requireUserHasNoDeposit(msg.sender);
activePool.mintAggInterest();
uint256 collToSend = stashedColl[msg.sender];
_requireNonZeroAmount(collToSend);
stashedColl[msg.sender] = 0;
emit DepositOperation(msg.sender, Operation.claimAllCollGains, 0, 0, 0, 0, 0, collToSend);
emit DepositUpdated(msg.sender, 0, 0, 0, 0, 0, 0, 0);
_sendCollGainToDepositor(collToSend);
}
// --- BOLD reward functions ---
function triggerBoldRewards(uint256 _boldYield) external {
_requireCallerIsActivePool();
assert(_boldYield > 0); // TODO: remove before deploying
_updateYieldRewardsSum(_boldYield);
}
function _updateYieldRewardsSum(uint256 _newYield) internal {
uint256 accumulatedYieldGains = yieldGainsPending + _newYield;
if (accumulatedYieldGains == 0) return;
// When total deposits is very small, B is not updated. In this case, the BOLD issued is hold
// until the total deposits reach 1 BOLD (remains in the balance of the SP).
uint256 totalBoldDepositsCached = totalBoldDeposits; // cached to save an SLOAD
if (totalBoldDepositsCached < DECIMAL_PRECISION) {
yieldGainsPending = accumulatedYieldGains;
return;
}
yieldGainsOwed += accumulatedYieldGains;
yieldGainsPending = 0;
/*
* Calculate the BOLD-per-unit staked. Division uses a "feedback" error correction, to keep the
* cumulative error low in the running total B:
*
* 1) Form a numerator which compensates for the floor division error that occurred the last time this
* function was called.
* 2) Calculate "per-unit-staked" ratio.
* 3) Multiply the ratio back by its denominator, to reveal the current floor division error.
* 4) Store this error 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 yieldNumerator = accumulatedYieldGains * DECIMAL_PRECISION + lastYieldError;
uint256 yieldPerUnitStaked = yieldNumerator / totalBoldDepositsCached;
lastYieldError = yieldNumerator - yieldPerUnitStaked * totalBoldDepositsCached;
uint256 marginalYieldGain = yieldPerUnitStaked * (P - 1);
epochToScaleToB[currentEpoch][currentScale] = epochToScaleToB[currentEpoch][currentScale] + marginalYieldGain;
emit B_Updated(epochToScaleToB[currentEpoch][currentScale], currentEpoch, currentScale);
}
// --- Liquidation functions ---
/*
* Cancels out the specified debt against the Bold contained in the Stability Pool (as far as possible)
* and transfers the Trove's Coll collateral from ActivePool to StabilityPool.
* Only called by liquidation functions in the TroveManager.
*/
function offset(uint256 _debtToOffset, uint256 _collToAdd) external override {
_requireCallerIsTroveManager();
uint256 totalBold = totalBoldDeposits; // cached to save an SLOAD
if (totalBold == 0 || _debtToOffset == 0) return;
_updateCollRewardSumAndProduct(_collToAdd, _debtToOffset, totalBold); // updates S and P
_moveOffsetCollAndDebt(_collToAdd, _debtToOffset);
}
// --- Offset helper functions ---
function _computeCollRewardsPerUnitStaked(uint256 _collToAdd, uint256 _debtToOffset, uint256 _totalBoldDeposits)
internal
returns (uint256 collGainPerUnitStaked, uint256 boldLossPerUnitStaked, uint256 newLastBoldLossErrorOffset)
{
/*
* Compute the Bold and Coll rewards. Uses a "feedback" error correction, to keep
* the cumulative error in the P and S state variables low:
*
* 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 collNumerator = _collToAdd * DECIMAL_PRECISION + lastCollError_Offset;
assert(_debtToOffset <= _totalBoldDeposits);
if (_debtToOffset == _totalBoldDeposits) {
boldLossPerUnitStaked = DECIMAL_PRECISION; // When the Pool depletes to 0, so does each deposit
lastBoldLossError_Offset = 0;
lastBoldLossError_TotalDeposits = _totalBoldDeposits;
} else {
uint256 boldLossNumerator = _debtToOffset * DECIMAL_PRECISION;
/*
* Add 1 to make error in quotient positive. We want "slightly too much" Bold loss,
* which ensures the error in any given compoundedBoldDeposit favors the Stability Pool.
*/
boldLossPerUnitStaked = boldLossNumerator / _totalBoldDeposits + 1;
newLastBoldLossErrorOffset = boldLossPerUnitStaked * _totalBoldDeposits - boldLossNumerator;
}
collGainPerUnitStaked = collNumerator / _totalBoldDeposits;
lastCollError_Offset = collNumerator - collGainPerUnitStaked * _totalBoldDeposits;
return (collGainPerUnitStaked, boldLossPerUnitStaked, newLastBoldLossErrorOffset);
}
// Update the Stability Pool reward sum S and product P
function _updateCollRewardSumAndProduct(uint256 _collToAdd, uint256 _debtToOffset, uint256 _totalBoldDeposits)
internal
{
(uint256 collGainPerUnitStaked, uint256 boldLossPerUnitStaked, uint256 newLastBoldLossErrorOffset) =
_computeCollRewardsPerUnitStaked(_collToAdd, _debtToOffset, _totalBoldDeposits);
uint256 currentP = P;
uint256 newP;
assert(boldLossPerUnitStaked <= DECIMAL_PRECISION);
/*
* The newProductFactor is the factor by which to change all deposits, due to the depletion of Stability Pool Bold in the liquidation.
* We make the product factor 0 if there was a pool-emptying. Otherwise, it is (1 - boldLossPerUnitStaked)
*/
uint256 newProductFactor = uint256(DECIMAL_PRECISION) - boldLossPerUnitStaked;
uint128 currentScaleCached = currentScale;
uint128 currentEpochCached = currentEpoch;
uint256 currentS = epochToScaleToS[currentEpochCached][currentScaleCached];
/*
* Calculate the new S first, before we update P.
* The Coll gain for any given depositor from a liquidation depends on the value of their deposit
* (and the value of totalDeposits) prior to the Stability being depleted by the debt in the liquidation.
*
* Since S corresponds to Coll gain, and P to deposit loss, we update S first.
*/
uint256 marginalCollGain = collGainPerUnitStaked * (currentP - 1);
uint256 newS = currentS + marginalCollGain;
epochToScaleToS[currentEpochCached][currentScaleCached] = newS;
emit S_Updated(newS, currentEpochCached, currentScaleCached);
// If the Stability Pool was emptied, increment the epoch, and reset the scale and product P
if (newProductFactor == 0) {
currentEpoch = currentEpochCached + 1;
emit EpochUpdated(currentEpoch);
currentScale = 0;
emit ScaleUpdated(currentScale);
newP = DECIMAL_PRECISION;
// If multiplying P by a non-zero product factor would reduce P below the scale boundary, increment the scale
} else if (currentP * newProductFactor / DECIMAL_PRECISION < SCALE_FACTOR) {
newP = currentP * newProductFactor * SCALE_FACTOR / DECIMAL_PRECISION;
currentScale = currentScaleCached + 1;
// Increment the scale again if it's still below the boundary. This ensures the invariant P >= 1e9 holds and addresses this issue
// from Liquity v1: https://github.com/liquity/dev/security/advisories/GHSA-m9f3-hrx8-x2g3
if (newP < SCALE_FACTOR) {
newP *= SCALE_FACTOR;
currentScale = currentScaleCached + 2;
}
emit ScaleUpdated(currentScale);
// If there's no scale change and no pool-emptying, just do a standard multiplication
} else {
uint256 errorFactor;
if (lastBoldLossError_Offset > 0) {
errorFactor = lastBoldLossError_Offset * newProductFactor / lastBoldLossError_TotalDeposits;
}
newP = (currentP * newProductFactor + errorFactor) / DECIMAL_PRECISION;
}
lastBoldLossError_Offset = newLastBoldLossErrorOffset;
lastBoldLossError_TotalDeposits = _totalBoldDeposits;
assert(newP > 0);
P = newP;
emit P_Updated(newP);
}
function _moveOffsetCollAndDebt(uint256 _collToAdd, uint256 _debtToOffset) internal {
// Cancel the liquidated Bold debt with the Bold in the stability pool
_updateTotalBoldDeposits(0, _debtToOffset);
// Burn the debt that was successfully offset
boldToken.burn(address(this), _debtToOffset);
// Update internal Coll balance tracker
uint256 newCollBalance = collBalance + _collToAdd;
collBalance = newCollBalance;
// Pull Coll from Active Pool
activePool.sendColl(address(this), _collToAdd);
emit StabilityPoolCollBalanceUpdated(newCollBalance);
}
function _updateTotalBoldDeposits(uint256 _depositIncrease, uint256 _depositDecrease) internal {
if (_depositIncrease == 0 && _depositDecrease == 0) return;
uint256 newTotalBoldDeposits = totalBoldDeposits + _depositIncrease - _depositDecrease;
totalBoldDeposits = newTotalBoldDeposits;
emit StabilityPoolBoldBalanceUpdated(newTotalBoldDeposits);
}
function _decreaseYieldGainsOwed(uint256 _amount) internal {
if (_amount == 0) return;
uint256 newYieldGainsOwed = yieldGainsOwed - _amount;
yieldGainsOwed = newYieldGainsOwed;
}
// --- Reward calculator functions for depositor ---
/* Calculates the Coll gain earned by the deposit since its last snapshots were taken.
* Given by the formula: E = d0 * (S - S(0))/P(0)
* where S(0) and P(0) are the depositor's snapshots of the sum S and product P, respectively.
* d0 is the last recorded deposit value.
*/
function getDepositorCollGain(address _depositor) public view override returns (uint256) {
uint256 initialDeposit = deposits[_depositor].initialValue;
if (initialDeposit == 0) return 0;
Snapshots memory snapshots = depositSnapshots[_depositor];
/*
* Grab the sum 'S' from the epoch at which the stake was made. The Coll gain may span up to one scale change.
* If it does, the second portion of the Coll gain is scaled by 1e9.
* If the gain spans no scale change, the second portion will be 0.
*/
uint128 epochSnapshot = snapshots.epoch;
uint128 scaleSnapshot = snapshots.scale;
uint256 S_Snapshot = snapshots.S;
uint256 P_Snapshot = snapshots.P;
uint256 firstPortion = epochToScaleToS[epochSnapshot][scaleSnapshot] - S_Snapshot;
uint256 secondPortion = epochToScaleToS[epochSnapshot][scaleSnapshot + 1] / SCALE_FACTOR;
uint256 collGain = initialDeposit * (firstPortion + secondPortion) / P_Snapshot / DECIMAL_PRECISION;
return LiquityMath._min(collGain, collBalance);
}
function getDepositorYieldGain(address _depositor) public view override returns (uint256) {
uint256 initialDeposit = deposits[_depositor].initialValue;
if (initialDeposit == 0) return 0;
Snapshots memory snapshots = depositSnapshots[_depositor];
/*
* Grab the sum 'B' from the epoch at which the stake was made. The Bold gain may span up to one scale change.
* If it does, the second portion of the Bold gain is scaled by 1e9.
* If the gain spans no scale change, the second portion will be 0.
*/
uint128 epochSnapshot = snapshots.epoch;
uint128 scaleSnapshot = snapshots.scale;
uint256 B_Snapshot = snapshots.B;
uint256 P_Snapshot = snapshots.P;
uint256 firstPortion = epochToScaleToB[epochSnapshot][scaleSnapshot] - B_Snapshot;
uint256 secondPortion = epochToScaleToB[epochSnapshot][scaleSnapshot + 1] / SCALE_FACTOR;
uint256 yieldGain = initialDeposit * (firstPortion + secondPortion) / P_Snapshot / DECIMAL_PRECISION;
return LiquityMath._min(yieldGain, yieldGainsOwed);
}
function getDepositorYieldGainWithPending(address _depositor) external view override returns (uint256) {
uint256 initialDeposit = deposits[_depositor].initialValue;
if (initialDeposit == 0) return 0;
Snapshots memory snapshots = depositSnapshots[_depositor];
uint256 pendingSPYield = activePool.calcPendingSPYield() + yieldGainsPending;
uint256 newYieldGainsOwed = yieldGainsOwed + (totalBoldDeposits >= DECIMAL_PRECISION ? pendingSPYield : 0);
uint256 firstPortionPending;
uint256 secondPortionPending;
if (pendingSPYield > 0 && snapshots.epoch == currentEpoch && totalBoldDeposits >= DECIMAL_PRECISION) {
uint256 yieldNumerator = pendingSPYield * DECIMAL_PRECISION + lastYieldError;
uint256 yieldPerUnitStaked = yieldNumerator / totalBoldDeposits;
uint256 marginalYieldGain = yieldPerUnitStaked * (P - 1);
if (currentScale == snapshots.scale) firstPortionPending = marginalYieldGain;
else if (currentScale == snapshots.scale + 1) secondPortionPending = marginalYieldGain;
}
uint256 firstPortion = epochToScaleToB[snapshots.epoch][snapshots.scale] + firstPortionPending - snapshots.B;
uint256 secondPortion =
(epochToScaleToB[snapshots.epoch][snapshots.scale + 1] + secondPortionPending) / SCALE_FACTOR;
uint256 yieldGain = initialDeposit * (firstPortion + secondPortion) / snapshots.P / DECIMAL_PRECISION;
return LiquityMath._min(yieldGain, newYieldGainsOwed);
}
// --- Compounded deposit ---
/*
* Return the user's compounded deposit. Given by the formula: d = d0 * P/P(0)
* where P(0) is the depositor's snapshot of the product P, taken when they last updated their deposit.
*/
function getCompoundedBoldDeposit(address _depositor) public view override returns (uint256) {
uint256 initialDeposit = deposits[_depositor].initialValue;
if (initialDeposit == 0) return 0;
Snapshots memory snapshots = depositSnapshots[_depositor];
uint256 compoundedDeposit = _getCompoundedStakeFromSnapshots(initialDeposit, snapshots);
return compoundedDeposit;
}
// Internal function, used to calculcate compounded deposits and compounded front end stakes.
function _getCompoundedStakeFromSnapshots(uint256 initialStake, Snapshots memory snapshots)
internal
view
returns (uint256)
{
uint256 snapshot_P = snapshots.P;
uint128 scaleSnapshot = snapshots.scale;
uint128 epochSnapshot = snapshots.epoch;
// If stake was made before a pool-emptying event, then it has been fully cancelled with debt -- so, return 0
if (epochSnapshot < currentEpoch) return 0;
uint256 compoundedStake;
uint128 scaleDiff = currentScale - scaleSnapshot;
// To make sure rouning errors favour the system, we use P - 1 if P decreased
uint256 cachedP = P;
uint256 currentPToUse = cachedP != snapshot_P ? cachedP - 1 : cachedP;
/* Compute the compounded stake. If a scale change in P was made during the stake's lifetime,
* account for it. If more than one scale change was made, then the stake has decreased by a factor of
* at least 1e-9 -- so return 0.
*/
if (scaleDiff == 0) {
compoundedStake = initialStake * currentPToUse / snapshot_P;
} else if (scaleDiff == 1) {
compoundedStake = initialStake * currentPToUse / snapshot_P / SCALE_FACTOR;
} else {
// if scaleDiff >= 2
compoundedStake = 0;
}
/*
* If compounded deposit is less than a billionth of the initial deposit, return 0.
*
* NOTE: originally, this line was in place to stop rounding errors making the deposit too large. However, the error
* corrections should ensure the error in P "favors the Pool", i.e. any given compounded deposit should slightly less
* than it's theoretical value.
*
* Thus it's unclear whether this line is still really needed.
*/
if (compoundedStake < initialStake / 1e9) return 0;
return compoundedStake;
}
// --- Sender functions for Bold deposit and Coll gains ---
function _sendCollGainToDepositor(uint256 _collAmount) internal {
if (_collAmount == 0) return;
uint256 newCollBalance = collBalance - _collAmount;
collBalance = newCollBalance;
emit StabilityPoolCollBalanceUpdated(newCollBalance);
collToken.safeTransfer(msg.sender, _collAmount);
}
// Send Bold to user and decrease Bold in Pool
function _sendBoldtoDepositor(address _depositor, uint256 _boldToSend) internal {
if (_boldToSend == 0) return;
boldToken.returnFromPool(address(this), _depositor, _boldToSend);
}
// --- Stability Pool Deposit Functionality ---
function _updateDepositAndSnapshots(address _depositor, uint256 _newDeposit, uint256 _newStashedColl) internal {
deposits[_depositor].initialValue = _newDeposit;
stashedColl[_depositor] = _newStashedColl;
if (_newDeposit == 0) {
delete depositSnapshots[_depositor];
emit DepositUpdated(_depositor, 0, _newStashedColl, 0, 0, 0, 0, 0);
return;
}
uint128 currentScaleCached = currentScale;
uint128 currentEpochCached = currentEpoch;
uint256 currentP = P;
// Get S for the current epoch and current scale
uint256 currentS = epochToScaleToS[currentEpochCached][currentScaleCached];
uint256 currentB = epochToScaleToB[currentEpochCached][currentScaleCached];
// Record new snapshots of the latest running product P and sum S for the depositor
depositSnapshots[_depositor].P = currentP;
depositSnapshots[_depositor].S = currentS;
depositSnapshots[_depositor].B = currentB;
depositSnapshots[_depositor].scale = currentScaleCached;
depositSnapshots[_depositor].epoch = currentEpochCached;
emit DepositUpdated(
_depositor,
_newDeposit,
_newStashedColl,
currentP,
currentS,
currentB,
currentScaleCached,
currentEpochCached
);
}
// --- 'require' functions ---
function _requireCallerIsActivePool() internal view {
require(msg.sender == address(activePool), "StabilityPool: Caller is not ActivePool");
}
function _requireCallerIsTroveManager() internal view {
require(msg.sender == address(troveManager), "StabilityPool: Caller is not TroveManager");
}
function _requireUserHasDeposit(uint256 _initialDeposit) internal pure {
require(_initialDeposit > 0, "StabilityPool: User must have a non-zero deposit");
}
function _requireUserHasNoDeposit(address _address) internal view {
uint256 initialDeposit = deposits[_address].initialValue;
require(initialDeposit == 0, "StabilityPool: User must have no deposit");
}
function _requireNonZeroAmount(uint256 _amount) internal pure {
require(_amount > 0, "StabilityPool: Amount must be non-zero");
}
}