-
Notifications
You must be signed in to change notification settings - Fork 13
/
Pool.sol
957 lines (829 loc) · 33.3 KB
/
Pool.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
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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.14;
import { Clone } from '@clones/Clone.sol';
import { ReentrancyGuard } from '@openzeppelin/contracts/security/ReentrancyGuard.sol';
import { Multicall } from '@openzeppelin/contracts/utils/Multicall.sol';
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {
IPool,
IPoolImmutables,
IPoolBorrowerActions,
IPoolLPActions,
IPoolLenderActions,
IPoolKickerActions,
IPoolTakerActions,
IPoolSettlerActions,
IPoolState,
IPoolDerivedState,
IERC20Token
} from '../interfaces/pool/IPool.sol';
import {
PoolState,
AuctionsState,
DepositsState,
LoansState,
InflatorState,
EmaState,
InterestState,
PoolBalancesState,
ReserveAuctionState,
Bucket,
BurnEvent,
Liquidation
} from '../interfaces/pool/commons/IPoolState.sol';
import {
KickResult,
SettleResult,
TakeResult,
RemoveQuoteParams,
MoveQuoteParams,
AddQuoteParams,
KickReserveAuctionParams
} from '../interfaces/pool/commons/IPoolInternals.sol';
import {
_priceAt,
_roundToScale
} from '../libraries/helpers/PoolHelper.sol';
import {
_revertIfAuctionDebtLocked,
_revertIfAuctionClearable,
_revertOnExpiry
} from '../libraries/helpers/RevertsHelper.sol';
import { Buckets } from '../libraries/internal/Buckets.sol';
import { Deposits } from '../libraries/internal/Deposits.sol';
import { Loans } from '../libraries/internal/Loans.sol';
import { Maths } from '../libraries/internal/Maths.sol';
import { BorrowerActions } from '../libraries/external/BorrowerActions.sol';
import { LenderActions } from '../libraries/external/LenderActions.sol';
import { LPActions } from '../libraries/external/LPActions.sol';
import { KickerActions } from '../libraries/external/KickerActions.sol';
import { TakerActions } from '../libraries/external/TakerActions.sol';
import { PoolCommons } from '../libraries/external/PoolCommons.sol';
/**
* @title Pool Contract
* @dev Base contract and entrypoint for commong logic of both `ERC20` and `ERC721` pools.
*/
abstract contract Pool is Clone, ReentrancyGuard, Multicall, IPool {
using SafeERC20 for IERC20;
/*****************/
/*** Constants ***/
/*****************/
/// @dev Immutable pool type arg offset.
uint256 internal constant POOL_TYPE = 0;
/// @dev Immutable `Ajna` token address arg offset.
uint256 internal constant AJNA_ADDRESS = 1;
/// @dev Immutable collateral token address arg offset.
uint256 internal constant COLLATERAL_ADDRESS = 21;
/// @dev Immutable quote token address arg offset.
uint256 internal constant QUOTE_ADDRESS = 41;
/// @dev Immutable quote token scale arg offset.
uint256 internal constant QUOTE_SCALE = 61;
/***********************/
/*** State Variables ***/
/***********************/
AuctionsState internal auctions;
DepositsState internal deposits;
LoansState internal loans;
InflatorState internal inflatorState;
EmaState internal emaState;
InterestState internal interestState;
PoolBalancesState internal poolBalances;
ReserveAuctionState internal reserveAuction;
/// @dev deposit index -> bucket mapping
mapping(uint256 => Bucket) internal buckets;
bool internal isPoolInitialized;
/// @dev owner address -> new owner address -> deposit index -> allowed amount mapping
mapping(address => mapping(address => mapping(uint256 => uint256))) private _lpAllowances;
/// @dev owner address -> transferor address -> approved flag mapping
mapping(address => mapping(address => bool)) public override approvedTransferors;
/******************/
/*** Immutables ***/
/******************/
/// @inheritdoc IPoolImmutables
function poolType() external pure override returns (uint8) {
return _getArgUint8(POOL_TYPE);
}
/// @inheritdoc IPoolImmutables
function collateralAddress() external pure override returns (address) {
return _getArgAddress(COLLATERAL_ADDRESS);
}
/// @inheritdoc IPoolImmutables
function quoteTokenAddress() external pure override returns (address) {
return _getArgAddress(QUOTE_ADDRESS);
}
/// @inheritdoc IPoolImmutables
function quoteTokenScale() external pure override returns (uint256) {
return _getArgUint256(QUOTE_SCALE);
}
function quoteTokenDust() external pure override returns (uint256) {
return _getArgUint256(QUOTE_SCALE);
}
/*********************************/
/*** Lender External Functions ***/
/*********************************/
/// @inheritdoc IPoolLenderActions
function addQuoteToken(
uint256 amount_,
uint256 index_,
uint256 expiry_
) external override nonReentrant returns (uint256 bucketLP_) {
_revertOnExpiry(expiry_);
PoolState memory poolState = _accruePoolInterest();
// round to token precision
amount_ = _roundToScale(amount_, poolState.quoteDustLimit);
uint256 newLup;
(bucketLP_, newLup) = LenderActions.addQuoteToken(
buckets,
deposits,
poolState,
AddQuoteParams({
amount: amount_,
index: index_
})
);
// update pool interest rate state
_updateInterestState(poolState, newLup);
// move quote token amount from lender to pool
_transferQuoteTokenFrom(msg.sender, amount_);
}
/// @inheritdoc IPoolLenderActions
function moveQuoteToken(
uint256 maxAmount_,
uint256 fromIndex_,
uint256 toIndex_,
uint256 expiry_
) external override nonReentrant returns (uint256 fromBucketLP_, uint256 toBucketLP_, uint256 movedAmount_) {
_revertOnExpiry(expiry_);
PoolState memory poolState = _accruePoolInterest();
_revertIfAuctionDebtLocked(deposits, poolState.t0DebtInAuction, fromIndex_, poolState.inflator);
uint256 newLup;
(
fromBucketLP_,
toBucketLP_,
movedAmount_,
newLup
) = LenderActions.moveQuoteToken(
buckets,
deposits,
poolState,
MoveQuoteParams({
maxAmountToMove: maxAmount_,
fromIndex: fromIndex_,
toIndex: toIndex_,
thresholdPrice: Loans.getMax(loans).thresholdPrice
})
);
// update pool interest rate state
_updateInterestState(poolState, newLup);
}
/// @inheritdoc IPoolLenderActions
function removeQuoteToken(
uint256 maxAmount_,
uint256 index_
) external override nonReentrant returns (uint256 removedAmount_, uint256 redeemedLP_) {
_revertIfAuctionClearable(auctions, loans);
PoolState memory poolState = _accruePoolInterest();
_revertIfAuctionDebtLocked(deposits, poolState.t0DebtInAuction, index_, poolState.inflator);
uint256 newLup;
(
removedAmount_,
redeemedLP_,
newLup
) = LenderActions.removeQuoteToken(
buckets,
deposits,
poolState,
RemoveQuoteParams({
maxAmount: maxAmount_,
index: index_,
thresholdPrice: Loans.getMax(loans).thresholdPrice
})
);
// update pool interest rate state
_updateInterestState(poolState, newLup);
// move quote token amount from pool to lender
_transferQuoteToken(msg.sender, removedAmount_);
}
/// @inheritdoc IPoolLenderActions
function updateInterest() external override nonReentrant {
PoolState memory poolState = _accruePoolInterest();
_updateInterestState(poolState, Deposits.getLup(deposits, poolState.debt));
}
/***********************************/
/*** Borrower External Functions ***/
/***********************************/
/// @inheritdoc IPoolBorrowerActions
function stampLoan() external override nonReentrant {
PoolState memory poolState = _accruePoolInterest();
uint256 newLup = BorrowerActions.stampLoan(
auctions,
deposits,
loans,
poolState
);
_updateInterestState(poolState, newLup);
}
/*****************************/
/*** Liquidation Functions ***/
/*****************************/
/**
* @inheritdoc IPoolKickerActions
* @dev === Write state ===
* @dev increment `poolBalances.t0DebtInAuction` and `poolBalances.t0Debt` accumulators
* @dev update `t0Debt2ToCollateral` ratio, debt and collateral post action are considered 0
*/
function kick(
address borrower_,
uint256 npLimitIndex_
) external override nonReentrant {
PoolState memory poolState = _accruePoolInterest();
// kick auction
KickResult memory result = KickerActions.kick(
auctions,
deposits,
loans,
poolState,
borrower_,
npLimitIndex_
);
// update in memory pool state struct
poolState.debt = Maths.wmul(result.t0PoolDebt, poolState.inflator);
poolState.t0Debt = result.t0PoolDebt;
poolState.t0DebtInAuction += result.t0KickedDebt;
// adjust t0Debt2ToCollateral ratio
_updateT0Debt2ToCollateral(
result.debtPreAction,
0, // debt post kick (for loan in auction) not taken into account
result.collateralPreAction,
0 // collateral post kick (for loan in auction) not taken into account
);
// update pool balances state
poolBalances.t0Debt = poolState.t0Debt;
poolBalances.t0DebtInAuction = poolState.t0DebtInAuction;
// update pool interest rate state
_updateInterestState(poolState, result.lup);
if (result.amountToCoverBond != 0) _transferQuoteTokenFrom(msg.sender, result.amountToCoverBond);
}
/**
* @inheritdoc IPoolKickerActions
* @dev === Write state ===
* @dev increment `poolBalances.t0DebtInAuction` and `poolBalances.t0Debt` accumulators
* @dev update `t0Debt2ToCollateral` ratio, debt and collateral post action are considered 0
*/
function kickWithDeposit(
uint256 index_,
uint256 npLimitIndex_
) external override nonReentrant {
PoolState memory poolState = _accruePoolInterest();
// kick auctions
KickResult memory result = KickerActions.kickWithDeposit(
auctions,
deposits,
buckets,
loans,
poolState,
index_,
npLimitIndex_
);
// update in memory pool state struct
poolState.debt = Maths.wmul(result.t0PoolDebt, poolState.inflator);
poolState.t0Debt = result.t0PoolDebt;
poolState.t0DebtInAuction += result.t0KickedDebt;
// adjust t0Debt2ToCollateral ratio
_updateT0Debt2ToCollateral(
result.debtPreAction,
0, // debt post kick (for loan in auction) not taken into account
result.collateralPreAction,
0 // collateral post kick (for loan in auction) not taken into account
);
// update pool balances state
poolBalances.t0Debt = poolState.t0Debt;
poolBalances.t0DebtInAuction = poolState.t0DebtInAuction;
// update pool interest rate state
_updateInterestState(poolState, result.lup);
// transfer from kicker to pool the difference to cover bond
if (result.amountToCoverBond != 0) _transferQuoteTokenFrom(msg.sender, result.amountToCoverBond);
}
/**
* @inheritdoc IPoolKickerActions
* @dev === Write state ===
* @dev decrease kicker's `claimable` accumulator
* @dev decrease auctions `totalBondEscrowed` accumulator
*/
function withdrawBonds(
address recipient_,
uint256 maxAmount_
) external override nonReentrant {
uint256 claimable = auctions.kickers[msg.sender].claimable;
// the amount to claim is constrained by the claimable balance of sender and by pool balance
maxAmount_ = Maths.min(maxAmount_, claimable);
maxAmount_ = Maths.min(maxAmount_, _getNormalizedPoolQuoteTokenBalance());
// revert if no amount to claim
if (maxAmount_ == 0) revert InsufficientLiquidity();
// decrement total bond escrowed
auctions.totalBondEscrowed -= maxAmount_;
auctions.kickers[msg.sender].claimable -= maxAmount_;
emit BondWithdrawn(msg.sender, recipient_, maxAmount_);
_transferQuoteToken(recipient_, maxAmount_);
}
/*********************************/
/*** Reserve Auction Functions ***/
/*********************************/
/**
* @inheritdoc IPoolKickerActions
* @dev === Write state ===
* @dev increment `latestBurnEpoch` counter
* @dev update `reserveAuction.latestBurnEventEpoch` and burn event `timestamp` state
* @dev === Reverts on ===
* @dev 2 weeks not passed `ReserveAuctionTooSoon()`
* @dev === Emit events ===
* @dev - `KickReserveAuction`
*/
function kickReserveAuction() external override nonReentrant {
// start a new claimable reserve auction, passing in relevant parameters such as the current pool size, debt, balance, and inflator value
uint256 kickerAward = KickerActions.kickReserveAuction(
auctions,
reserveAuction,
KickReserveAuctionParams({
poolSize: Deposits.treeSum(deposits),
t0PoolDebt: poolBalances.t0Debt,
poolBalance: _getNormalizedPoolQuoteTokenBalance(),
inflator: inflatorState.inflator
})
);
// transfer kicker award to msg.sender
_transferQuoteToken(msg.sender, kickerAward);
}
/**
* @inheritdoc IPoolTakerActions
* @dev === Write state ===
* @dev increment `reserveAuction.totalAjnaBurned` accumulator
* @dev update burn event `totalInterest` and `totalBurned` accumulators
*/
function takeReserves(
uint256 maxAmount_
) external override nonReentrant returns (uint256 amount_) {
uint256 ajnaRequired;
(amount_, ajnaRequired) = TakerActions.takeReserves(
reserveAuction,
maxAmount_
);
// burn required number of ajna tokens to take quote from reserves
IERC20(_getArgAddress(AJNA_ADDRESS)).safeTransferFrom(msg.sender, address(this), ajnaRequired);
IERC20Token(_getArgAddress(AJNA_ADDRESS)).burn(ajnaRequired);
// transfer quote token to caller
_transferQuoteToken(msg.sender, amount_);
}
/*****************************/
/*** Transfer LP Functions ***/
/*****************************/
/// @inheritdoc IPoolLPActions
function increaseLPAllowance(
address spender_,
uint256[] calldata indexes_,
uint256[] calldata amounts_
) external override nonReentrant {
LPActions.increaseLPAllowance(
_lpAllowances[msg.sender][spender_],
spender_,
indexes_,
amounts_
);
}
/// @inheritdoc IPoolLPActions
function decreaseLPAllowance(
address spender_,
uint256[] calldata indexes_,
uint256[] calldata amounts_
) external override nonReentrant {
LPActions.decreaseLPAllowance(
_lpAllowances[msg.sender][spender_],
spender_,
indexes_,
amounts_
);
}
/// @inheritdoc IPoolLPActions
function revokeLPAllowance(
address spender_,
uint256[] calldata indexes_
) external override nonReentrant {
LPActions.revokeLPAllowance(
_lpAllowances[msg.sender][spender_],
spender_,
indexes_
);
}
/// @inheritdoc IPoolLPActions
function approveLPTransferors(
address[] calldata transferors_
) external override {
LPActions.approveLPTransferors(
approvedTransferors[msg.sender],
transferors_
);
}
/// @inheritdoc IPoolLPActions
function revokeLPTransferors(
address[] calldata transferors_
) external override {
LPActions.revokeLPTransferors(
approvedTransferors[msg.sender],
transferors_
);
}
/// @inheritdoc IPoolLPActions
function transferLP(
address owner_,
address newOwner_,
uint256[] calldata indexes_
) external override nonReentrant {
LPActions.transferLP(
buckets,
_lpAllowances,
approvedTransferors,
owner_,
newOwner_,
indexes_
);
}
/*****************************/
/*** Pool Helper Functions ***/
/*****************************/
/**
* @notice Accrues pool interest in current block and returns pool details.
* @dev external libraries call: `PoolCommons.accrueInterest`
* @dev === Write state ===
* @dev - `PoolCommons.accrueInterest` - `Deposits.mult` (scale `Fenwick` tree with new interest accrued):
* @dev update scaling array state
* @dev - increment `reserveAuction.totalInterestEarned` accumulator
* @return poolState_ Struct containing pool details.
*/
function _accruePoolInterest() internal returns (PoolState memory poolState_) {
poolState_.t0Debt = poolBalances.t0Debt;
poolState_.t0DebtInAuction = poolBalances.t0DebtInAuction;
poolState_.collateral = poolBalances.pledgedCollateral;
poolState_.inflator = inflatorState.inflator;
poolState_.rate = interestState.interestRate;
poolState_.poolType = _getArgUint8(POOL_TYPE);
poolState_.quoteDustLimit = _getArgUint256(QUOTE_SCALE);
// check if t0Debt is not equal to 0, indicating that there is debt to be tracked for the pool
if (poolState_.t0Debt != 0) {
// Calculate prior pool debt
poolState_.debt = Maths.wmul(poolState_.t0Debt, poolState_.inflator);
// calculate elapsed time since inflator was last updated
uint256 elapsed = block.timestamp - inflatorState.inflatorUpdate;
// set isNewInterestAccrued field to true if elapsed time is not 0, indicating that new interest may have accrued
poolState_.isNewInterestAccrued = elapsed != 0;
// if new interest may have accrued, call accrueInterest function and update inflator and debt fields of poolState_ struct
if (poolState_.isNewInterestAccrued) {
(uint256 newInflator, uint256 newInterest) = PoolCommons.accrueInterest(
emaState,
deposits,
poolState_,
Loans.getMax(loans).thresholdPrice,
elapsed
);
poolState_.inflator = newInflator;
// After debt owed to lenders has accrued, calculate current debt owed by borrowers
poolState_.debt = Maths.wmul(poolState_.t0Debt, poolState_.inflator);
// update total interest earned accumulator with the newly accrued interest
reserveAuction.totalInterestEarned += newInterest;
}
}
}
/**
* @notice Helper function to update pool state post take and bucket take actions.
* @param result_ Struct containing details of take result.
* @param poolState_ Struct containing pool details.
*/
function _updatePostTakeState(
TakeResult memory result_,
PoolState memory poolState_
) internal {
// update in memory pool state struct
poolState_.debt = result_.poolDebt;
poolState_.t0Debt = result_.t0PoolDebt;
poolState_.t0DebtInAuction += result_.t0DebtPenalty;
poolState_.t0DebtInAuction -= result_.t0DebtInAuctionChange;
poolState_.collateral -= (result_.collateralAmount + result_.compensatedCollateral); // deduct collateral taken plus collateral compensated if NFT auction settled
// adjust t0Debt2ToCollateral ratio if auction settled by take action
if (result_.settledAuction) {
_updateT0Debt2ToCollateral(
0, // debt pre take (for loan in auction) not taken into account
result_.debtPostAction,
0, // collateral pre take (for loan in auction) not taken into account
result_.collateralPostAction
);
}
// update pool balances state
poolBalances.t0Debt = poolState_.t0Debt;
poolBalances.t0DebtInAuction = poolState_.t0DebtInAuction;
poolBalances.pledgedCollateral = poolState_.collateral;
// update pool interest rate state
_updateInterestState(poolState_, result_.newLup);
}
/**
* @notice Helper function to update pool state post settle action.
* @param result_ Struct containing details of settle result.
* @param poolState_ Struct containing pool details.
*/
function _updatePostSettleState(
SettleResult memory result_,
PoolState memory poolState_
) internal {
// update in memory pool state struct
poolState_.debt -= Maths.wmul(result_.t0DebtSettled, poolState_.inflator);
poolState_.t0Debt -= result_.t0DebtSettled;
poolState_.t0DebtInAuction -= result_.t0DebtSettled;
poolState_.collateral -= result_.collateralSettled;
// update pool balances state
poolBalances.t0Debt = poolState_.t0Debt;
poolBalances.t0DebtInAuction = poolState_.t0DebtInAuction;
poolBalances.pledgedCollateral = poolState_.collateral;
// update pool interest rate state
_updateInterestState(poolState_, Deposits.getLup(deposits, poolState_.debt));
}
/**
* @notice Adjusts the `t0` debt 2 to collateral ratio, `interestState.t0Debt2ToCollateral`.
* @dev Anytime a borrower's debt or collateral changes, the `interestState.t0Debt2ToCollateral` must be updated.
* @dev === Write state ===
* @dev update `interestState.t0Debt2ToCollateral` accumulator
* @param debtPreAction_ Borrower's debt before the action
* @param debtPostAction_ Borrower's debt after the action
* @param colPreAction_ Borrower's collateral before the action
* @param colPostAction_ Borrower's collateral after the action
*/
function _updateT0Debt2ToCollateral(
uint256 debtPreAction_,
uint256 debtPostAction_,
uint256 colPreAction_,
uint256 colPostAction_
) internal {
uint256 debt2ColAccumPreAction = colPreAction_ != 0 ? debtPreAction_ ** 2 / colPreAction_ : 0;
uint256 debt2ColAccumPostAction = colPostAction_ != 0 ? debtPostAction_ ** 2 / colPostAction_ : 0;
if (debt2ColAccumPreAction != 0 || debt2ColAccumPostAction != 0) {
uint256 curT0Debt2ToCollateral = interestState.t0Debt2ToCollateral;
curT0Debt2ToCollateral += debt2ColAccumPostAction;
curT0Debt2ToCollateral -= debt2ColAccumPreAction;
interestState.t0Debt2ToCollateral = curT0Debt2ToCollateral;
}
}
/**
* @notice Update interest rate and inflator of the pool.
* @dev external libraries call: `PoolCommons.updateInterestState`
* @dev === Write state ===
* @dev - `PoolCommons.updateInterestState`
* @dev `EMA`s accumulators
* @dev interest rate accumulator and `interestRateUpdate` state
* @dev pool inflator and `inflatorUpdate` state
* @dev === Emit events ===
* @dev `PoolCommons.updateInterestState`: `UpdateInterestRate`
* @param poolState_ Struct containing pool details.
* @param lup_ Current `LUP` in pool.
*/
function _updateInterestState(
PoolState memory poolState_,
uint256 lup_
) internal {
PoolCommons.updateInterestState(interestState, emaState, deposits, poolState_, lup_);
// update pool inflator
if (poolState_.isNewInterestAccrued) {
inflatorState.inflator = uint208(poolState_.inflator);
inflatorState.inflatorUpdate = uint48(block.timestamp);
// if the debt in the current pool state is 0, also update the inflator and inflatorUpdate fields in inflatorState
// slither-disable-next-line incorrect-equality
} else if (poolState_.debt == 0) {
inflatorState.inflator = uint208(Maths.WAD);
inflatorState.inflatorUpdate = uint48(block.timestamp);
}
}
/**
* @notice Helper function to transfer amount of quote tokens (in quote token precision) from sender to pool contract.
* @param from_ Sender address.
* @param amount_ Amount to transfer from sender.
*/
function _transferQuoteTokenFrom(address from_, uint256 amount_) internal {
IERC20(_getArgAddress(QUOTE_ADDRESS)).safeTransferFrom(from_, address(this), amount_ / _getArgUint256(QUOTE_SCALE));
}
/**
* @notice Helper function to transfer amount of quote tokens (in quote token precision) from pool contract.
* @param to_ Receiver address.
* @param amount_ Amount to transfer to receiver.
*/
function _transferQuoteToken(address to_, uint256 amount_) internal {
IERC20(_getArgAddress(QUOTE_ADDRESS)).safeTransfer(to_, amount_ / _getArgUint256(QUOTE_SCALE));
}
/**
* @notice Returns the pool quote token balance normalized to `WAD` to be used for calculating pool reserves.
*/
function _getNormalizedPoolQuoteTokenBalance() internal view returns (uint256) {
return IERC20(_getArgAddress(QUOTE_ADDRESS)).balanceOf(address(this)) * _getArgUint256(QUOTE_SCALE);
}
/*******************************/
/*** External View Functions ***/
/*******************************/
/// @inheritdoc IPoolState
function auctionInfo(
address borrower_
) external
view override returns (
address kicker_,
uint256 bondFactor_,
uint256 bondSize_,
uint256 kickTime_,
uint256 kickMomp_,
uint256 neutralPrice_,
address head_,
address next_,
address prev_,
bool alreadyTaken_
) {
Liquidation memory liquidation = auctions.liquidations[borrower_];
return (
liquidation.kicker,
liquidation.bondFactor,
liquidation.bondSize,
liquidation.kickTime,
liquidation.kickMomp,
liquidation.neutralPrice,
auctions.head,
liquidation.next,
liquidation.prev,
liquidation.alreadyTaken
);
}
/// @inheritdoc IPoolState
function borrowerInfo(
address borrower_
) external view override returns (uint256, uint256, uint256) {
return (
loans.borrowers[borrower_].t0Debt,
loans.borrowers[borrower_].collateral,
loans.borrowers[borrower_].t0Np
);
}
/// @inheritdoc IPoolState
function bucketInfo(
uint256 index_
) external view override returns (uint256, uint256, uint256, uint256, uint256) {
uint256 scale = Deposits.scale(deposits, index_);
return (
buckets[index_].lps,
buckets[index_].collateral,
buckets[index_].bankruptcyTime,
Maths.wmul(scale, Deposits.unscaledValueAt(deposits, index_)),
scale
);
}
/// @inheritdoc IPoolDerivedState
function bucketExchangeRate(
uint256 index_
) external view returns (uint256 exchangeRate_) {
Bucket storage bucket = buckets[index_];
exchangeRate_ = Buckets.getExchangeRate(
bucket.collateral,
bucket.lps,
Deposits.valueAt(deposits, index_),
_priceAt(index_)
);
}
/// @inheritdoc IPoolState
function currentBurnEpoch() external view returns (uint256) {
return reserveAuction.latestBurnEventEpoch;
}
/// @inheritdoc IPoolState
function burnInfo(uint256 burnEventEpoch_) external view returns (uint256, uint256, uint256) {
BurnEvent memory burnEvent = reserveAuction.burnEvents[burnEventEpoch_];
return (
burnEvent.timestamp,
burnEvent.totalInterest,
burnEvent.totalBurned
);
}
/// @inheritdoc IPoolState
function debtInfo() external view returns (uint256, uint256, uint256, uint256) {
uint256 pendingInflator = PoolCommons.pendingInflator(
inflatorState.inflator,
inflatorState.inflatorUpdate,
interestState.interestRate
);
return (
Maths.wmul(poolBalances.t0Debt, pendingInflator),
Maths.wmul(poolBalances.t0Debt, inflatorState.inflator),
Maths.wmul(poolBalances.t0DebtInAuction, inflatorState.inflator),
interestState.t0Debt2ToCollateral
);
}
/// @inheritdoc IPoolDerivedState
function depositUpToIndex(uint256 index_) external view override returns (uint256) {
return Deposits.prefixSum(deposits, index_);
}
/// @inheritdoc IPoolDerivedState
function depositIndex(uint256 debt_) external view override returns (uint256) {
return Deposits.findIndexOfSum(deposits, debt_);
}
/// @inheritdoc IPoolDerivedState
function depositSize() external view override returns (uint256) {
return Deposits.treeSum(deposits);
}
/// @inheritdoc IPoolDerivedState
function depositUtilization() external view override returns (uint256) {
return PoolCommons.utilization(emaState);
}
/// @inheritdoc IPoolDerivedState
function depositScale(uint256 index_) external view override returns (uint256) {
return deposits.scaling[index_];
}
/// @inheritdoc IPoolState
function emasInfo() external view override returns (uint256, uint256, uint256, uint256) {
return (
emaState.debtColEma,
emaState.lupt0DebtEma,
emaState.debtEma,
emaState.depositEma
);
}
/// @inheritdoc IPoolState
function inflatorInfo() external view override returns (uint256, uint256) {
return (
inflatorState.inflator,
inflatorState.inflatorUpdate
);
}
/// @inheritdoc IPoolState
function interestRateInfo() external view returns (uint256, uint256) {
return (
interestState.interestRate,
interestState.interestRateUpdate
);
}
/// @inheritdoc IPoolState
function kickerInfo(
address kicker_
) external view override returns (uint256, uint256) {
return(
auctions.kickers[kicker_].claimable,
auctions.kickers[kicker_].locked
);
}
/// @inheritdoc IPoolState
function lenderInfo(
uint256 index_,
address lender_
) external view override returns (uint256 lpBalance_, uint256 depositTime_) {
depositTime_ = buckets[index_].lenders[lender_].depositTime;
if (buckets[index_].bankruptcyTime < depositTime_) lpBalance_ = buckets[index_].lenders[lender_].lps;
}
/// @inheritdoc IPoolState
function lpAllowance(
uint256 index_,
address spender_,
address owner_
) external view override returns (uint256 allowance_) {
allowance_ = _lpAllowances[owner_][spender_][index_];
}
/// @inheritdoc IPoolState
function loanInfo(
uint256 loanId_
) external view override returns (address, uint256) {
return (
Loans.getByIndex(loans, loanId_).borrower,
Loans.getByIndex(loans, loanId_).thresholdPrice
);
}
/// @inheritdoc IPoolState
function loansInfo() external view override returns (address, uint256, uint256) {
return (
Loans.getMax(loans).borrower,
Maths.wmul(Loans.getMax(loans).thresholdPrice, inflatorState.inflator),
Loans.noOfLoans(loans)
);
}
/// @inheritdoc IPoolState
function pledgedCollateral() external view override returns (uint256) {
return poolBalances.pledgedCollateral;
}
/// @inheritdoc IPoolState
function reservesInfo() external view override returns (uint256, uint256, uint256, uint256) {
return (
auctions.totalBondEscrowed,
reserveAuction.unclaimed,
reserveAuction.kicked,
reserveAuction.totalInterestEarned
);
}
/// @inheritdoc IPoolState
function totalAuctionsInPool() external view override returns (uint256) {
return auctions.noOfAuctions;
}
/// @inheritdoc IPoolState
function totalT0Debt() external view override returns (uint256) {
return poolBalances.t0Debt;
}
/// @inheritdoc IPoolState
function totalT0DebtInAuction() external view override returns (uint256) {
return poolBalances.t0DebtInAuction;
}
}