-
Notifications
You must be signed in to change notification settings - Fork 13
/
ERC721Pool.sol
629 lines (524 loc) · 23.6 KB
/
ERC721Pool.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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.14;
import {
IERC721Token,
IPoolErrors,
IPoolLenderActions,
IPoolKickerActions,
IPoolTakerActions,
IPoolSettlerActions
} from './interfaces/pool/IPool.sol';
import {
DrawDebtResult,
RepayDebtResult,
SettleParams,
SettleResult,
TakeResult
} from './interfaces/pool/commons/IPoolInternals.sol';
import { PoolState } from './interfaces/pool/commons/IPoolState.sol';
import {
IERC721Pool,
IERC721PoolBorrowerActions,
IERC721PoolImmutables,
IERC721PoolLenderActions
} from './interfaces/pool/erc721/IERC721Pool.sol';
import { IERC721Taker } from './interfaces/pool/erc721/IERC721Taker.sol';
import { IERC721PoolState } from './interfaces/pool/erc721/IERC721PoolState.sol';
import { FlashloanablePool } from './base/FlashloanablePool.sol';
import {
_revertIfAuctionClearable,
_revertOnExpiry
} from './libraries/helpers/RevertsHelper.sol';
import { Maths } from './libraries/internal/Maths.sol';
import { Deposits } from './libraries/internal/Deposits.sol';
import { Loans } from './libraries/internal/Loans.sol';
import { LenderActions } from './libraries/external/LenderActions.sol';
import { BorrowerActions } from './libraries/external/BorrowerActions.sol';
import { SettlerActions } from './libraries/external/SettlerActions.sol';
import { TakerActions } from './libraries/external/TakerActions.sol';
/**
* @title ERC721 Pool contract
* @notice Entrypoint of `ERC721` 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`: auction undercollateralized loans; settle auctions; claim bond rewards
* - `Bidders`: take auctioned collateral
* - `Reserve purchasers`: start auctions; take reserves
* - `Flash borrowers`: initiate flash loans on ERC20 quote tokens
* @dev Contract is `FlashloanablePool` with flashloan logic.
* @dev Contract is base `Pool` with logic to handle `ERC721` collateral.
* @dev Calls logic from external `PoolCommons`, `LenderActions`, `BorrowerActions` and `Auction` actions libraries.
*/
contract ERC721Pool is FlashloanablePool, IERC721Pool {
/*****************/
/*** Constants ***/
/*****************/
/// @dev Immutable NFT subset pool arg offset.
uint256 internal constant SUBSET = 93;
/***********************/
/*** State Variables ***/
/***********************/
/// @dev Mapping of `tokenIds` allowed in `NFT` Subset type pool.
mapping(uint256 => bool) public tokenIdsAllowed;
/// @dev Borrower `address => array` of tokenIds pledged by borrower mapping.
mapping(address => uint256[]) public borrowerTokenIds;
/// @dev Array of `tokenIds` in pool buckets (claimable from pool).
uint256[] public bucketTokenIds;
/****************************/
/*** Initialize Functions ***/
/****************************/
/// @inheritdoc IERC721Pool
function initialize(
uint256[] memory tokenIds_,
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);
uint256 noOfTokens = tokenIds_.length;
if (noOfTokens != 0) {
// add subset of tokenIds allowed in the pool
for (uint256 id = 0; id < noOfTokens;) {
tokenIdsAllowed[tokenIds_[id]] = true;
unchecked { ++id; }
}
}
Loans.init(loans);
// increment initializations count to ensure these values can't be updated
isPoolInitialized = true;
}
/******************/
/*** Immutables ***/
/******************/
/// @inheritdoc IERC721PoolImmutables
function isSubset() external pure override returns (bool) {
return _getArgUint256(SUBSET) != 0;
}
/***********************************/
/*** Borrower External Functions ***/
/***********************************/
/**
* @inheritdoc IERC721PoolBorrowerActions
* @dev === Write state ===
* @dev - decrement `poolBalances.t0DebtInAuction` accumulator
* @dev - increment `poolBalances.pledgedCollateral` accumulator
* @dev - increment `poolBalances.t0Debt` accumulator
* @dev - update `t0Debt2ToCollateral` ratio only if loan not in auction, debt and collateral pre action are considered 0 if auction settled
* @dev - update `borrowerTokenIds` and `bucketTokenIds` arrays
* @dev === Emit events ===
* @dev - `DrawDebtNFT`
*/
function drawDebt(
address borrowerAddress_,
uint256 amountToBorrow_,
uint256 limitIndex_,
uint256[] calldata tokenIdsToPledge_
) external nonReentrant {
PoolState memory poolState = _accruePoolInterest();
DrawDebtResult memory result = BorrowerActions.drawDebt(
auctions,
buckets,
deposits,
loans,
poolState,
borrowerAddress_,
amountToBorrow_,
limitIndex_,
Maths.wad(tokenIdsToPledge_.length)
);
emit DrawDebtNFT(borrowerAddress_, amountToBorrow_, tokenIdsToPledge_, result.newLup);
// update in memory pool state struct
poolState.debt = result.poolDebt;
poolState.t0Debt = result.t0PoolDebt;
if (result.t0DebtInAuctionChange != 0) poolState.t0DebtInAuction -= result.t0DebtInAuctionChange;
poolState.collateral = result.poolCollateral;
// adjust t0Debt2ToCollateral ratio if loan not in auction
if (!result.inAuction) {
_updateT0Debt2ToCollateral(
result.settledAuction ? 0 : result.debtPreAction, // debt pre settle (for loan in auction) not taken into account
result.debtPostAction,
result.settledAuction ? 0 : result.collateralPreAction, // collateral pre settle (for loan in auction) not taken into account
result.collateralPostAction
);
}
// update pool interest rate state
_updateInterestState(poolState, result.newLup);
if (tokenIdsToPledge_.length != 0) {
// update pool balances state
if (result.t0DebtInAuctionChange != 0) {
poolBalances.t0DebtInAuction = poolState.t0DebtInAuction;
}
poolBalances.pledgedCollateral = poolState.collateral;
// move collateral from sender to pool
_transferFromSenderToPool(borrowerTokenIds[borrowerAddress_], tokenIdsToPledge_);
}
if (result.settledAuction) _rebalanceTokens(borrowerAddress_, result.remainingCollateral);
// move borrowed amount from pool to sender
if (amountToBorrow_ != 0) {
// update pool balances state
poolBalances.t0Debt = poolState.t0Debt;
// move borrowed amount from pool to sender
_transferQuoteToken(msg.sender, amountToBorrow_);
}
}
/**
* @inheritdoc IERC721PoolBorrowerActions
* @dev === Write state ===
* @dev - decrement `poolBalances.t0Debt accumulator`
* @dev - decrement `poolBalances.t0DebtInAuction accumulator`
* @dev - decrement `poolBalances.pledgedCollateral accumulator`
* @dev - update `t0Debt2ToCollateral` ratio only if loan not in auction, debt and collateral pre action are considered 0 if auction settled
* @dev - update `borrowerTokenIds` and `bucketTokenIds` arrays
* @dev === Emit events ===
* @dev - `RepayDebt`
*/
function repayDebt(
address borrowerAddress_,
uint256 maxQuoteTokenAmountToRepay_,
uint256 noOfNFTsToPull_,
address collateralReceiver_,
uint256 limitIndex_
) external nonReentrant {
PoolState memory poolState = _accruePoolInterest();
RepayDebtResult memory result = BorrowerActions.repayDebt(
auctions,
buckets,
deposits,
loans,
poolState,
borrowerAddress_,
maxQuoteTokenAmountToRepay_,
Maths.wad(noOfNFTsToPull_),
limitIndex_
);
emit RepayDebt(borrowerAddress_, result.quoteTokenToRepay, noOfNFTsToPull_, result.newLup);
// update in memory pool state struct
poolState.debt = result.poolDebt;
poolState.t0Debt = result.t0PoolDebt;
if (result.t0DebtInAuctionChange != 0) poolState.t0DebtInAuction -= result.t0DebtInAuctionChange;
poolState.collateral = result.poolCollateral;
if (result.settledAuction) _rebalanceTokens(borrowerAddress_, result.remainingCollateral);
// adjust t0Debt2ToCollateral ratio if loan not in auction
if (!result.inAuction) {
_updateT0Debt2ToCollateral(
result.settledAuction ? 0 : result.debtPreAction, // debt pre settle (for loan in auction) not taken into account
result.debtPostAction,
result.settledAuction ? 0 : result.collateralPreAction, // collateral pre settle (for loan in auction) not taken into account
result.collateralPostAction
);
}
// update pool interest rate state
_updateInterestState(poolState, result.newLup);
// update pool balances state
poolBalances.pledgedCollateral = poolState.collateral;
if (result.quoteTokenToRepay != 0) {
// update pool balances state
poolBalances.t0Debt = poolState.t0Debt;
if (result.t0DebtInAuctionChange != 0) {
poolBalances.t0DebtInAuction = poolState.t0DebtInAuction;
}
// move amount to repay from sender to pool
_transferQuoteTokenFrom(msg.sender, result.quoteTokenToRepay);
}
if (noOfNFTsToPull_ != 0) {
// move collateral from pool to address specified as collateral receiver
_transferFromPoolToAddress(collateralReceiver_, borrowerTokenIds[msg.sender], noOfNFTsToPull_);
}
}
/*********************************/
/*** Lender External Functions ***/
/*********************************/
/**
* @inheritdoc IERC721PoolLenderActions
* @dev === Write state ===
* @dev - update `bucketTokenIds` arrays
* @dev === Emit events ===
* @dev - `AddCollateralNFT`
*/
function addCollateral(
uint256[] calldata tokenIds_,
uint256 index_,
uint256 expiry_
) external override nonReentrant returns (uint256 bucketLP_) {
_revertOnExpiry(expiry_);
PoolState memory poolState = _accruePoolInterest();
bucketLP_ = LenderActions.addCollateral(
buckets,
deposits,
Maths.wad(tokenIds_.length),
index_
);
emit AddCollateralNFT(msg.sender, index_, tokenIds_, bucketLP_);
// update pool interest rate state
_updateInterestState(poolState, Deposits.getLup(deposits, poolState.debt));
// move required collateral from sender to pool
_transferFromSenderToPool(bucketTokenIds, tokenIds_);
}
/**
* @inheritdoc IERC721PoolLenderActions
* @dev === Write state ===
* @dev - update `bucketTokenIds` arrays
* @dev === Emit events ===
* @dev - `MergeOrRemoveCollateralNFT`
*/
function mergeOrRemoveCollateral(
uint256[] calldata removalIndexes_,
uint256 noOfNFTsToRemove_,
uint256 toIndex_
) external override nonReentrant returns (uint256 collateralMerged_, uint256 bucketLP_) {
_revertIfAuctionClearable(auctions, loans);
PoolState memory poolState = _accruePoolInterest();
uint256 collateralAmount = Maths.wad(noOfNFTsToRemove_);
(
collateralMerged_,
bucketLP_
) = LenderActions.mergeOrRemoveCollateral(
buckets,
deposits,
removalIndexes_,
collateralAmount,
toIndex_
);
emit MergeOrRemoveCollateralNFT(msg.sender, collateralMerged_, bucketLP_);
// update pool interest rate state
_updateInterestState(poolState, Deposits.getLup(deposits, poolState.debt));
if (collateralMerged_ == collateralAmount) {
// Total collateral in buckets meets the requested removal amount, noOfNFTsToRemove_
_transferFromPoolToAddress(msg.sender, bucketTokenIds, noOfNFTsToRemove_);
}
}
/**
* @inheritdoc IPoolLenderActions
* @dev === Write state ===
* @dev - update `bucketTokenIds` arrays
* @dev === Emit events ===
* @dev - `RemoveCollateral`
* @param noOfNFTsToRemove_ Number of `NFT` tokens to remove.
*/
function removeCollateral(
uint256 noOfNFTsToRemove_,
uint256 index_
) external override nonReentrant returns (uint256 removedAmount_, uint256 redeemedLP_) {
_revertIfAuctionClearable(auctions, loans);
PoolState memory poolState = _accruePoolInterest();
removedAmount_ = Maths.wad(noOfNFTsToRemove_);
redeemedLP_ = LenderActions.removeCollateral(
buckets,
deposits,
removedAmount_,
index_
);
emit RemoveCollateral(msg.sender, index_, noOfNFTsToRemove_, redeemedLP_);
// update pool interest rate state
_updateInterestState(poolState, Deposits.getLup(deposits, poolState.debt));
_transferFromPoolToAddress(msg.sender, bucketTokenIds, noOfNFTsToRemove_);
}
/*******************************/
/*** 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)
*/
function settle(
address borrowerAddress_,
uint256 maxDepth_
) external nonReentrant override {
PoolState memory poolState = _accruePoolInterest();
SettleParams memory params = SettleParams({
borrower: borrowerAddress_,
poolBalance: _getNormalizedPoolQuoteTokenBalance(),
bucketDepth: maxDepth_
});
SettleResult memory result = SettlerActions.settlePoolDebt(
auctions,
buckets,
deposits,
loans,
reserveAuction,
poolState,
params
);
_updatePostSettleState(result, poolState);
// move token ids from borrower array to pool claimable array if any collateral used to settle bad debt
if (result.collateralSettled != 0) _rebalanceTokens(params.borrower, result.collateralRemaining);
}
/**
* @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 collateral_,
address callee_,
bytes calldata data_
) external override nonReentrant {
PoolState memory poolState = _accruePoolInterest();
TakeResult memory result = TakerActions.take(
auctions,
buckets,
deposits,
loans,
poolState,
borrowerAddress_,
Maths.wad(collateral_),
1
);
_updatePostTakeState(result, poolState);
// transfer rounded collateral from pool to taker
uint256[] memory tokensTaken = _transferFromPoolToAddress(
callee_,
borrowerTokenIds[borrowerAddress_],
result.collateralAmount / 1e18
);
uint256 totalQuoteTokenAmount = result.quoteTokenAmount + result.excessQuoteToken;
if (data_.length != 0) {
IERC721Taker(callee_).atomicSwapCallback(
tokensTaken,
totalQuoteTokenAmount / _getArgUint256(QUOTE_SCALE),
data_
);
}
if (result.settledAuction) _rebalanceTokens(borrowerAddress_, result.remainingCollateral);
// transfer from taker to pool the amount of quote tokens needed to cover collateral auctioned (including excess for rounded collateral)
_transferQuoteTokenFrom(msg.sender, totalQuoteTokenAmount);
// transfer from pool to borrower the excess of quote tokens after rounding collateral auctioned
if (result.excessQuoteToken != 0) _transferQuoteToken(borrowerAddress_, result.excessQuoteToken);
}
/**
* @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_,
1
);
_updatePostTakeState(result, poolState);
if (result.settledAuction) _rebalanceTokens(borrowerAddress_, result.remainingCollateral);
}
/**************************/
/*** Internal Functions ***/
/**************************/
/**
* @notice Rebalance `NFT` token and transfer difference to floor collateral from borrower to pool claimable array.
* @dev === Write state ===
* @dev - update `borrowerTokens` and `bucketTokenIds` arrays
* @dev === Emit events ===
* @dev - `RemoveCollateral`
* @param borrowerAddress_ Address of borrower.
* @param borrowerCollateral_ Current borrower collateral to be rebalanced.
*/
function _rebalanceTokens(
address borrowerAddress_,
uint256 borrowerCollateral_
) internal {
// rebalance borrower's collateral, transfer difference to floor collateral from borrower to pool claimable array
uint256[] storage borrowerTokens = borrowerTokenIds[borrowerAddress_];
uint256 noOfTokensPledged = borrowerTokens.length;
/*
eg1. borrowerCollateral_ = 4.1, noOfTokensPledged = 6; noOfTokensToTransfer = 1
eg2. borrowerCollateral_ = 4, noOfTokensPledged = 6; noOfTokensToTransfer = 2
*/
uint256 borrowerCollateralRoundedUp = (borrowerCollateral_ + 1e18 - 1) / 1e18;
uint256 noOfTokensToTransfer = noOfTokensPledged - borrowerCollateralRoundedUp;
for (uint256 i = 0; i < noOfTokensToTransfer;) {
uint256 tokenId = borrowerTokens[--noOfTokensPledged]; // start with moving the last token pledged by borrower
borrowerTokens.pop(); // remove token id from borrower
bucketTokenIds.push(tokenId); // add token id to pool claimable tokens
unchecked { ++i; }
}
}
/**
* @notice Helper function for transferring multiple `NFT` tokens from msg.sender to pool.
* @dev Reverts in case token id is not supported by subset pool.
* @param poolTokens_ Array in pool that tracks `NFT` ids (could be tracking `NFT`s pledged by borrower or `NFT`s added by a lender in a specific bucket).
* @param tokenIds_ Array of `NFT` token ids to transfer from `msg.sender` to pool.
*/
function _transferFromSenderToPool(
uint256[] storage poolTokens_,
uint256[] calldata tokenIds_
) internal {
bool subset = _getArgUint256(SUBSET) != 0;
for (uint256 i = 0; i < tokenIds_.length;) {
uint256 tokenId = tokenIds_[i];
if (subset && !tokenIdsAllowed[tokenId]) revert OnlySubset();
poolTokens_.push(tokenId);
_transferNFT(msg.sender, address(this), tokenId);
unchecked { ++i; }
}
}
/**
* @notice Helper function for transferring multiple `NFT` tokens from pool to given address.
* @dev It transfers `NFT`s from the most recent one added into the pool (pop from array tracking `NFT`s in pool).
* @param toAddress_ Address where pool should transfer tokens to.
* @param poolTokens_ Array in pool that tracks `NFT` ids (could be tracking `NFT`s pledged by borrower or `NFT`s added by a lender in a specific bucket).
* @param amountToRemove_ Number of `NFT` tokens to transfer from pool to given address.
* @return Array containing token ids that were transferred from pool to address.
*/
function _transferFromPoolToAddress(
address toAddress_,
uint256[] storage poolTokens_,
uint256 amountToRemove_
) internal returns (uint256[] memory) {
uint256[] memory tokensTransferred = new uint256[](amountToRemove_);
uint256 noOfNFTsInPool = poolTokens_.length;
for (uint256 i = 0; i < amountToRemove_;) {
uint256 tokenId = poolTokens_[--noOfNFTsInPool]; // start with transferring the last token added in bucket
poolTokens_.pop();
_transferNFT(address(this), toAddress_, tokenId);
tokensTransferred[i] = tokenId;
unchecked { ++i; }
}
return tokensTransferred;
}
/**
* @notice Helper function to transfer an `NFT` from owner to target address (reused in code to reduce contract deployment bytecode size).
* @dev Since `transferFrom` is used instead of `safeTransferFrom`, calling smart contracts must be careful to check that they support any received `NFT`s.
* @param from_ `NFT` owner address.
* @param to_ New `NFT` owner address.
* @param tokenId_ `NFT` token id to be transferred.
*/
function _transferNFT(address from_, address to_, uint256 tokenId_) internal {
// slither-disable-next-line calls-loop
IERC721Token(_getArgAddress(COLLATERAL_ADDRESS)).transferFrom(from_, to_, tokenId_);
}
/*******************************/
/*** External View Functions ***/
/*******************************/
/// @inheritdoc IERC721PoolState
function totalBorrowerTokens(address borrower_) external view override returns(uint256) {
return borrowerTokenIds[borrower_].length;
}
/// @inheritdoc IERC721PoolState
function totalBucketTokens() external view override returns(uint256) {
return bucketTokenIds.length;
}
}