This repository has been archived by the owner on Apr 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathERC20Pool.sol
499 lines (418 loc) · 18.1 KB
/
ERC20Pool.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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.18;
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {
IERC20Pool,
IERC20PoolBorrowerActions,
IERC20PoolImmutables,
IERC20PoolLenderActions
} from './interfaces/pool/erc20/IERC20Pool.sol';
import { IERC20Taker } from './interfaces/pool/erc20/IERC20Taker.sol';
import {
IPoolLenderActions,
IPoolKickerActions,
IPoolTakerActions,
IPoolSettlerActions
} from './interfaces/pool/IPool.sol';
import {
IERC3156FlashBorrower,
IERC3156FlashLender
} from './interfaces/pool/IERC3156FlashLender.sol';
import {
DrawDebtResult,
RepayDebtResult,
SettleParams,
SettleResult,
TakeResult
} from './interfaces/pool/commons/IPoolInternals.sol';
import { PoolState } from './interfaces/pool/commons/IPoolState.sol';
import { FlashloanablePool } from './base/FlashloanablePool.sol';
import {
_getCollateralDustPricePrecisionAdjustment,
_roundToScale,
_roundUpToScale
} from './libraries/helpers/PoolHelper.sol';
import {
_revertIfAuctionClearable,
_revertAfterExpiry
} from './libraries/helpers/RevertsHelper.sol';
import { Loans } from './libraries/internal/Loans.sol';
import { Deposits } from './libraries/internal/Deposits.sol';
import { Maths } from './libraries/internal/Maths.sol';
import { BorrowerActions } from './libraries/external/BorrowerActions.sol';
import { LenderActions } from './libraries/external/LenderActions.sol';
import { SettlerActions } from './libraries/external/SettlerActions.sol';
import { TakerActions } from './libraries/external/TakerActions.sol';
/**
* @title ERC20 Pool contract
* @notice Entrypoint of `ERC20` Pool actions for pool actors:
* - `Lenders`: add, remove and move quote tokens; transfer `LP`
* - `Borrowers`: draw and repay debt
* - `Traders`: add, remove and move quote tokens; add and remove collateral
* - `Kickers`: kick undercollateralized loans; settle auctions; claim bond rewards
* - `Bidders`: take auctioned collateral
* - `Reserve purchasers`: start auctions; take reserves
* - `Flash borrowers`: initiate flash loans on quote tokens and collateral
* @dev Contract is `FlashloanablePool` with flash loan logic.
* @dev Contract is base `Pool` with logic to handle `ERC20` collateral.
* @dev Calls logic from external `PoolCommons`, `LenderActions`, `BorrowerActions` and `Auction` actions libraries.
*/
contract ERC20Pool is FlashloanablePool, IERC20Pool {
using SafeERC20 for IERC20;
/*****************/
/*** Constants ***/
/*****************/
/// @dev Immutable collateral scale arg offset.
uint256 internal constant COLLATERAL_SCALE = 93;
/****************************/
/*** Initialize Functions ***/
/****************************/
/// @inheritdoc IERC20Pool
function initialize(
uint256 rate_
) external override {
if (isPoolInitialized) revert AlreadyInitialized();
inflatorState.inflator = uint208(1e18);
inflatorState.inflatorUpdate = uint48(block.timestamp);
interestState.interestRate = uint208(rate_);
interestState.interestRateUpdate = uint48(block.timestamp);
Loans.init(loans);
// increment initializations count to ensure these values can't be updated
isPoolInitialized = true;
}
/******************/
/*** Immutables ***/
/******************/
/// @inheritdoc IERC20PoolImmutables
function collateralScale() external pure override returns (uint256) {
return _getArgUint256(COLLATERAL_SCALE);
}
/// @inheritdoc IERC20Pool
function bucketCollateralDust(uint256 bucketIndex_) external pure override returns (uint256) {
return _bucketCollateralDust(bucketIndex_);
}
/***********************************/
/*** Borrower External Functions ***/
/***********************************/
/**
* @inheritdoc IERC20PoolBorrowerActions
* @dev === Write state ===
* @dev - increment `poolBalances.pledgedCollateral` accumulator
* @dev - increment `poolBalances.t0Debt` accumulator
* @dev - update `t0Debt2ToCollateral` ratio
* @dev === Emit events ===
* @dev - `DrawDebt`
*/
function drawDebt(
address borrowerAddress_,
uint256 amountToBorrow_,
uint256 limitIndex_,
uint256 collateralToPledge_
) external nonReentrant {
PoolState memory poolState = _accruePoolInterest();
// ensure the borrower is not charged for additional debt that they did not receive
amountToBorrow_ = _roundToScale(amountToBorrow_, poolState.quoteTokenScale);
// ensure the borrower is not credited with a fractional amount of collateral smaller than the token scale
collateralToPledge_ = _roundToScale(collateralToPledge_, _getArgUint256(COLLATERAL_SCALE));
DrawDebtResult memory result = BorrowerActions.drawDebt(
auctions,
deposits,
loans,
poolState,
_availableQuoteToken(),
borrowerAddress_,
amountToBorrow_,
limitIndex_,
collateralToPledge_
);
emit DrawDebt(borrowerAddress_, amountToBorrow_, collateralToPledge_, result.newLup);
// update in memory pool state struct
poolState.debt = result.poolDebt;
poolState.t0Debt = result.t0PoolDebt;
poolState.collateral = result.poolCollateral;
// adjust t0Debt2ToCollateral ratio
_updateT0Debt2ToCollateral(
result.debtPreAction,
result.debtPostAction,
result.collateralPreAction,
result.collateralPostAction
);
// update pool interest rate state
_updateInterestState(poolState, result.newLup);
if (collateralToPledge_ != 0) {
// update pool balances pledged collateral state
poolBalances.pledgedCollateral = poolState.collateral;
// move collateral from sender to pool
_transferCollateralFrom(msg.sender, collateralToPledge_);
}
if (amountToBorrow_ != 0) {
// update pool balances t0 debt state
poolBalances.t0Debt = poolState.t0Debt;
// move borrowed amount from pool to sender
_transferQuoteToken(msg.sender, amountToBorrow_);
}
}
/**
* @inheritdoc IERC20PoolBorrowerActions
* @dev === Write state ===
* @dev - decrement `poolBalances.t0Debt accumulator`
* @dev - decrement `poolBalances.pledgedCollateral accumulator`
* @dev - update `t0Debt2ToCollateral` ratio
* @dev === Emit events ===
* @dev - `RepayDebt`
*/
function repayDebt(
address borrowerAddress_,
uint256 maxQuoteTokenAmountToRepay_,
uint256 collateralAmountToPull_,
address collateralReceiver_,
uint256 limitIndex_
) external nonReentrant returns (uint256 amountRepaid_) {
PoolState memory poolState = _accruePoolInterest();
// ensure accounting is performed using the appropriate token scale
if (maxQuoteTokenAmountToRepay_ != type(uint256).max)
maxQuoteTokenAmountToRepay_ = _roundToScale(maxQuoteTokenAmountToRepay_, poolState.quoteTokenScale);
collateralAmountToPull_ = _roundToScale(collateralAmountToPull_, _getArgUint256(COLLATERAL_SCALE));
RepayDebtResult memory result = BorrowerActions.repayDebt(
auctions,
deposits,
loans,
poolState,
borrowerAddress_,
maxQuoteTokenAmountToRepay_,
collateralAmountToPull_,
limitIndex_
);
emit RepayDebt(borrowerAddress_, result.quoteTokenToRepay, collateralAmountToPull_, result.newLup);
// update in memory pool state struct
poolState.debt = result.poolDebt;
poolState.t0Debt = result.t0PoolDebt;
poolState.collateral = result.poolCollateral;
// adjust t0Debt2ToCollateral ratio
_updateT0Debt2ToCollateral(
result.debtPreAction,
result.debtPostAction,
result.collateralPreAction,
result.collateralPostAction
);
// update pool interest rate state
_updateInterestState(poolState, result.newLup);
if (result.quoteTokenToRepay != 0) {
// update pool balances t0 debt state
poolBalances.t0Debt = poolState.t0Debt;
// move amount to repay from sender to pool
_transferQuoteTokenFrom(msg.sender, result.quoteTokenToRepay);
}
if (collateralAmountToPull_ != 0) {
// update pool balances pledged collateral state
poolBalances.pledgedCollateral = poolState.collateral;
// move collateral from pool to address specified as collateral receiver
_transferCollateral(collateralReceiver_, collateralAmountToPull_);
}
amountRepaid_ = result.quoteTokenToRepay;
}
/*********************************/
/*** Lender External Functions ***/
/*********************************/
/**
* @inheritdoc IERC20PoolLenderActions
* @dev === Reverts on ===
* @dev - `DustAmountNotExceeded()`
* @dev === Emit events ===
* @dev - `AddCollateral`
*/
function addCollateral(
uint256 amountToAdd_,
uint256 index_,
uint256 expiry_
) external override nonReentrant returns (uint256 bucketLP_) {
_revertAfterExpiry(expiry_);
PoolState memory poolState = _accruePoolInterest();
// revert if the dust amount was not exceeded, but round on the scale amount
if (amountToAdd_ != 0 && amountToAdd_ < _bucketCollateralDust(index_)) revert DustAmountNotExceeded();
amountToAdd_ = _roundToScale(amountToAdd_, _getArgUint256(COLLATERAL_SCALE));
bucketLP_ = LenderActions.addCollateral(
buckets,
deposits,
amountToAdd_,
index_
);
emit AddCollateral(msg.sender, index_, amountToAdd_, bucketLP_);
// update pool interest rate state
_updateInterestState(poolState, Deposits.getLup(deposits, poolState.debt));
// move required collateral from sender to pool
_transferCollateralFrom(msg.sender, amountToAdd_);
}
/**
* @inheritdoc IPoolLenderActions
* @dev === Emit events ===
* @dev - `RemoveCollateral`
*/
function removeCollateral(
uint256 maxAmount_,
uint256 index_
) external override nonReentrant returns (uint256 removedAmount_, uint256 redeemedLP_) {
_revertIfAuctionClearable(auctions, loans);
PoolState memory poolState = _accruePoolInterest();
// round the collateral amount appropriately based on token precision
maxAmount_ = _roundToScale(maxAmount_, _getArgUint256(COLLATERAL_SCALE));
(removedAmount_, redeemedLP_) = LenderActions.removeMaxCollateral(
buckets,
deposits,
_bucketCollateralDust(index_),
maxAmount_,
index_
);
emit RemoveCollateral(msg.sender, index_, removedAmount_, redeemedLP_);
// update pool interest rate state
_updateInterestState(poolState, Deposits.getLup(deposits, poolState.debt));
// move collateral from pool to lender
_transferCollateral(msg.sender, removedAmount_);
}
/*******************************/
/*** Pool Auctions Functions ***/
/*******************************/
/**
* @inheritdoc IPoolSettlerActions
* @dev === Write state ===
* @dev - decrement `poolBalances.t0Debt` accumulator
* @dev - decrement `poolBalances.t0DebtInAuction` accumulator
* @dev - decrement `poolBalances.pledgedCollateral` accumulator
* @dev - no update of `t0Debt2ToCollateral` ratio as debt and collateral pre settle are not taken into account (pre debt and pre collateral = 0)
* @dev and loan is removed from auction queue only when there's no more debt (post debt = 0)
*/
function settle(
address borrowerAddress_,
uint256 maxDepth_
) external override nonReentrant {
PoolState memory poolState = _accruePoolInterest();
SettleResult memory result = SettlerActions.settlePoolDebt(
auctions,
buckets,
deposits,
loans,
reserveAuction,
poolState,
SettleParams({
borrower: borrowerAddress_,
poolBalance: _getNormalizedPoolQuoteTokenBalance(),
bucketDepth: maxDepth_
})
);
_updatePostSettleState(result, poolState);
}
/**
* @inheritdoc IPoolTakerActions
* @dev === Write state ===
* @dev - decrement `poolBalances.t0Debt` accumulator
* @dev - decrement `poolBalances.t0DebtInAuction` accumulator
* @dev - decrement `poolBalances.pledgedCollateral` accumulator
* @dev - update `t0Debt2ToCollateral` ratio only if auction settled, debt and collateral pre action are considered 0
*/
function take(
address borrowerAddress_,
uint256 maxAmount_,
address callee_,
bytes calldata data_
) external override nonReentrant returns (uint256 collateralTaken_) {
PoolState memory poolState = _accruePoolInterest();
uint256 collateralTokenScale = _getArgUint256(COLLATERAL_SCALE);
// round requested collateral to an amount which can actually be transferred
maxAmount_ = _roundToScale(maxAmount_, collateralTokenScale);
TakeResult memory result = TakerActions.take(
auctions,
buckets,
deposits,
loans,
poolState,
borrowerAddress_,
maxAmount_,
collateralTokenScale
);
// round quote token up to cover the cost of purchasing the collateral
result.quoteTokenAmount = _roundUpToScale(result.quoteTokenAmount, poolState.quoteTokenScale);
_updatePostTakeState(result, poolState);
_transferCollateral(callee_, result.collateralAmount);
if (data_.length != 0) {
IERC20Taker(callee_).atomicSwapCallback(
result.collateralAmount / collateralTokenScale,
result.quoteTokenAmount / poolState.quoteTokenScale,
data_
);
}
_transferQuoteTokenFrom(msg.sender, result.quoteTokenAmount);
collateralTaken_ = result.collateralAmount;
}
/**
* @inheritdoc IPoolTakerActions
* @dev === Write state ===
* @dev - decrement `poolBalances.t0Debt` accumulator
* @dev - decrement `poolBalances.t0DebtInAuction` accumulator
* @dev - decrement `poolBalances.pledgedCollateral` accumulator
* @dev - update `t0Debt2ToCollateral` ratio only if auction settled, debt and collateral pre action are considered 0
*/
function bucketTake(
address borrowerAddress_,
bool depositTake_,
uint256 index_
) external override nonReentrant {
PoolState memory poolState = _accruePoolInterest();
TakeResult memory result = TakerActions.bucketTake(
auctions,
buckets,
deposits,
loans,
poolState,
borrowerAddress_,
depositTake_,
index_,
_getArgUint256(COLLATERAL_SCALE)
);
_updatePostTakeState(result, poolState);
}
/***************************/
/*** Flashloan Functions ***/
/***************************/
/**
* @inheritdoc FlashloanablePool
* @dev Override default implementation and allows flashloans for both quote and collateral token.
*/
function _isFlashloanSupported(
address token_
) internal virtual view override returns (bool) {
return token_ == _getArgAddress(QUOTE_ADDRESS) || token_ == _getArgAddress(COLLATERAL_ADDRESS);
}
/************************/
/*** Helper Functions ***/
/************************/
/**
* @notice Helper function to transfer amount of collateral tokens from sender to pool contract.
* @param from_ Sender address.
* @param amount_ Amount to transfer from sender (`WAD` precision). Scaled to collateral precision before transfer.
*/
function _transferCollateralFrom(address from_, uint256 amount_) internal {
// Transfer amount in favour of the pool
uint256 transferAmount = Maths.ceilDiv(amount_, _getArgUint256(COLLATERAL_SCALE));
IERC20(_getArgAddress(COLLATERAL_ADDRESS)).safeTransferFrom(from_, address(this), transferAmount);
}
/**
* @notice Helper function to transfer amount of collateral tokens from pool contract.
* @param to_ Receiver address.
* @param amount_ Amount to transfer to receiver (`WAD` precision). Scaled to collateral precision before transfer.
*/
function _transferCollateral(address to_, uint256 amount_) internal {
IERC20(_getArgAddress(COLLATERAL_ADDRESS)).safeTransfer(to_, amount_ / _getArgUint256(COLLATERAL_SCALE));
}
/**
* @notice Helper function to calculate the minimum amount of collateral an actor may have in a bucket.
* @param bucketIndex_ Bucket index.
* @return Amount of collateral dust amount of the bucket.
*/
function _bucketCollateralDust(uint256 bucketIndex_) internal pure returns (uint256) {
// price precision adjustment will always be 0 for encumbered collateral
uint256 pricePrecisionAdjustment = _getCollateralDustPricePrecisionAdjustment(bucketIndex_);
// difference between the normalized scale and the collateral token's scale
return Maths.max(_getArgUint256(COLLATERAL_SCALE), 10 ** pricePrecisionAdjustment);
}
}