This repository has been archived by the owner on May 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ERC721Pool.sol
647 lines (539 loc) · 23.4 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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.14;
import {
IERC721Token,
IPoolErrors,
IPoolLenderActions,
IPoolLiquidationActions
} from './interfaces/pool/IPool.sol';
import {
BucketTakeResult,
DrawDebtResult,
RepayDebtResult,
SettleParams,
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 {
ICryptoPunks,
ICryptoKitties,
NFTTypes
} from './interfaces/pool/erc721/IERC721NonStandard.sol';
import { FlashloanablePool } from './base/FlashloanablePool.sol';
import { _revertIfAuctionClearable } 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 { Auctions } from './libraries/external/Auctions.sol';
import { LenderActions } from './libraries/external/LenderActions.sol';
import { BorrowerActions } from './libraries/external/BorrowerActions.sol';
/**
* @title ERC721 Pool contract
* @notice Entrypoint of ERC721 Pool actions for pool actors:
* - Lenders: add, remove and move quote tokens; transfer LPs
* - 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 flash loan logic.
* @dev Contract is base Pool with logic to handle ERC721 collateral.
* @dev Calls logic from external PoolCommons, LenderActions, BorrowerActions and Auctions libraries.
*/
contract ERC721Pool is FlashloanablePool, IERC721Pool {
/*****************/
/*** Constants ***/
/*****************/
// immutable args offset
uint256 internal constant SUBSET = 93;
uint256 internal constant NFT_TYPE = 125;
/***********************/
/*** State Variables ***/
/***********************/
mapping(uint256 => bool) public tokenIdsAllowed; // set of tokenIds that can be used for a given NFT Subset type pool
mapping(address => uint256[]) public borrowerTokenIds; // borrower address => array of tokenIds pledged by borrower
uint256[] public bucketTokenIds; // array of tokenIds added in pool buckets
/****************************/
/*** 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:
* - decrement poolBalances.t0DebtInAuction accumulator
* - increment poolBalances.pledgedCollateral accumulator
* - increment poolBalances.t0Debt accumulator
* - update borrowerTokenIds and bucketTokenIds arrays
* @dev emit events:
* - 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 pool interest rate state
poolState.debt = result.poolDebt;
poolState.collateral = result.poolCollateral;
_updateInterestState(poolState, result.newLup);
if (tokenIdsToPledge_.length != 0) {
// update pool balances state
if (result.t0DebtInAuctionChange != 0) {
poolBalances.t0DebtInAuction -= result.t0DebtInAuctionChange;
}
poolBalances.pledgedCollateral += Maths.wad(tokenIdsToPledge_.length);
// 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 += result.t0DebtChange;
// move borrowed amount from pool to sender
_transferQuoteToken(msg.sender, amountToBorrow_);
}
}
/**
* @inheritdoc IERC721PoolBorrowerActions
* @dev write state:
* - decrement poolBalances.t0Debt accumulator
* - decrement poolBalances.t0DebtInAuction accumulator
* - decrement poolBalances.pledgedCollateral accumulator
* - update borrowerTokenIds arrays
* @dev emit events:
* - RepayDebt
*/
function repayDebt(
address borrowerAddress_,
uint256 maxQuoteTokenAmountToRepay_,
uint256 noOfNFTsToPull_
) external nonReentrant {
PoolState memory poolState = _accruePoolInterest();
RepayDebtResult memory result = BorrowerActions.repayDebt(
auctions,
buckets,
deposits,
loans,
poolState,
borrowerAddress_,
maxQuoteTokenAmountToRepay_,
Maths.wad(noOfNFTsToPull_)
);
emit RepayDebt(borrowerAddress_, result.quoteTokenToRepay, noOfNFTsToPull_, result.newLup);
if (result.settledAuction) _rebalanceTokens(borrowerAddress_, result.remainingCollateral);
// update pool interest rate state
poolState.debt = result.poolDebt;
poolState.collateral = result.poolCollateral;
_updateInterestState(poolState, result.newLup);
if (result.quoteTokenToRepay != 0) {
// update pool balances state
poolBalances.t0Debt -= result.t0RepaidDebt;
if (result.t0DebtInAuctionChange != 0) {
poolBalances.t0DebtInAuction -= result.t0DebtInAuctionChange;
}
// move amount to repay from sender to pool
_transferQuoteTokenFrom(msg.sender, result.quoteTokenToRepay);
}
if (noOfNFTsToPull_ != 0) {
// update pool balances state
poolBalances.pledgedCollateral = result.poolCollateral;
// move collateral from pool to sender
_transferFromPoolToAddress(msg.sender, borrowerTokenIds[msg.sender], noOfNFTsToPull_);
}
}
/*********************************/
/*** Lender External Functions ***/
/*********************************/
/**
* @inheritdoc IERC721PoolLenderActions
* @dev write state:
* - update borrowerTokenIds arrays
* @dev emit events:
* - AddCollateralNFT
*/
function addCollateral(
uint256[] calldata tokenIdsToAdd_,
uint256 index_
) external override nonReentrant returns (uint256 bucketLPs_) {
PoolState memory poolState = _accruePoolInterest();
bucketLPs_ = LenderActions.addCollateral(
buckets,
deposits,
Maths.wad(tokenIdsToAdd_.length),
index_
);
emit AddCollateralNFT(msg.sender, index_, tokenIdsToAdd_, bucketLPs_);
// update pool interest rate state
_updateInterestState(poolState, _lup(poolState.debt));
// move required collateral from sender to pool
_transferFromSenderToPool(bucketTokenIds, tokenIdsToAdd_);
}
/**
* @inheritdoc IERC721PoolLenderActions
* @dev write state:
* - update bucketTokenIds arrays
* @dev emit events:
* - MergeOrRemoveCollateralNFT
*/
function mergeOrRemoveCollateral(
uint256[] calldata removalIndexes_,
uint256 noOfNFTsToRemove_,
uint256 toIndex_
) external override nonReentrant returns (uint256 collateralMerged_, uint256 bucketLPs_) {
PoolState memory poolState = _accruePoolInterest();
uint256 collateralAmount = Maths.wad(noOfNFTsToRemove_);
(
collateralMerged_,
bucketLPs_
) = LenderActions.mergeOrRemoveCollateral(
buckets,
deposits,
removalIndexes_,
collateralAmount,
toIndex_
);
emit MergeOrRemoveCollateralNFT(msg.sender, collateralMerged_, bucketLPs_);
// update pool interest rate state
_updateInterestState(poolState, _lup(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:
* - update bucketTokenIds arrays
* @dev emit events:
* - RemoveCollateral
*/
function removeCollateral(
uint256 noOfNFTsToRemove_,
uint256 index_
) external override nonReentrant returns (uint256 collateralAmount_, uint256 lpAmount_) {
_revertIfAuctionClearable(auctions, loans);
PoolState memory poolState = _accruePoolInterest();
collateralAmount_ = Maths.wad(noOfNFTsToRemove_);
lpAmount_ = LenderActions.removeCollateral(
buckets,
deposits,
collateralAmount_,
index_
);
emit RemoveCollateral(msg.sender, index_, noOfNFTsToRemove_, lpAmount_);
// update pool interest rate state
_updateInterestState(poolState, _lup(poolState.debt));
_transferFromPoolToAddress(msg.sender, bucketTokenIds, noOfNFTsToRemove_);
}
/*******************************/
/*** Pool Auctions Functions ***/
/*******************************/
/**
* @inheritdoc IPoolLiquidationActions
* @dev write state:
* - decrement poolBalances.t0Debt accumulator
* - decrement poolBalances.t0DebtInAuction accumulator
* - decrement poolBalances.pledgedCollateral accumulator
*/
function settle(
address borrowerAddress_,
uint256 maxDepth_
) external nonReentrant override {
PoolState memory poolState = _accruePoolInterest();
uint256 assets = Maths.wmul(poolBalances.t0Debt, poolState.inflator) + _getPoolQuoteTokenBalance();
uint256 liabilities = Deposits.treeSum(deposits) + auctions.totalBondEscrowed + reserveAuction.unclaimed;
SettleParams memory params = SettleParams(
{
borrower: borrowerAddress_,
reserves: (assets > liabilities) ? (assets-liabilities) : 0,
inflator: poolState.inflator,
bucketDepth: maxDepth_,
poolType: poolState.poolType
}
);
(
uint256 collateralRemaining,
uint256 t0DebtRemaining,
uint256 collateralSettled,
uint256 t0DebtSettled
) = Auctions.settlePoolDebt(
auctions,
buckets,
deposits,
loans,
params
);
// slither-disable-next-line incorrect-equality
if (t0DebtRemaining == 0) _rebalanceTokens(params.borrower, collateralRemaining);
// update pool balances state
poolBalances.t0Debt -= t0DebtSettled;
poolBalances.t0DebtInAuction -= t0DebtSettled;
poolBalances.pledgedCollateral -= collateralSettled;
// update pool interest rate state
poolState.debt -= Maths.wmul(t0DebtSettled, poolState.inflator);
poolState.collateral -= collateralSettled;
_updateInterestState(poolState, _lup(poolState.debt));
}
/**
* @inheritdoc IPoolLiquidationActions
* @dev write state:
* - decrement poolBalances.t0Debt accumulator
* - decrement poolBalances.t0DebtInAuction accumulator
* - decrement poolBalances.pledgedCollateral accumulator
*/
function take(
address borrowerAddress_,
uint256 collateral_,
address callee_,
bytes calldata data_
) external override nonReentrant {
PoolState memory poolState = _accruePoolInterest();
TakeResult memory result = Auctions.take(
auctions,
buckets,
deposits,
loans,
poolState,
borrowerAddress_,
Maths.wad(collateral_),
1
);
// update pool balances state
uint256 t0PoolDebt = poolBalances.t0Debt;
uint256 t0DebtInAuction = poolBalances.t0DebtInAuction;
if (result.t0DebtPenalty != 0) {
t0PoolDebt += result.t0DebtPenalty;
t0DebtInAuction += result.t0DebtPenalty;
}
t0PoolDebt -= result.t0RepayAmount;
t0DebtInAuction -= result.t0DebtInAuctionChange;
poolBalances.t0Debt = t0PoolDebt;
poolBalances.t0DebtInAuction = t0DebtInAuction;
poolBalances.pledgedCollateral -= result.collateralAmount;
// update pool interest rate state
poolState.debt = result.poolDebt;
poolState.collateral -= result.collateralAmount;
_updateInterestState(poolState, result.newLup);
// transfer rounded collateral from pool to taker
uint256[] memory tokensTaken = _transferFromPoolToAddress(
callee_,
borrowerTokenIds[borrowerAddress_],
result.collateralAmount / 1e18
);
if (data_.length != 0) {
IERC721Taker(callee_).atomicSwapCallback(
tokensTaken,
result.quoteTokenAmount / _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(callee_, result.quoteTokenAmount + result.excessQuoteToken);
// transfer from pool to borrower the excess of quote tokens after rounding collateral auctioned
if (result.excessQuoteToken != 0) _transferQuoteToken(borrowerAddress_, result.excessQuoteToken);
}
/**
* @inheritdoc IPoolLiquidationActions
* @dev write state:
* - decrement poolBalances.t0Debt accumulator
* - decrement poolBalances.t0DebtInAuction accumulator
* - decrement poolBalances.pledgedCollateral accumulator
*/
function bucketTake(
address borrowerAddress_,
bool depositTake_,
uint256 index_
) external override nonReentrant {
PoolState memory poolState = _accruePoolInterest();
BucketTakeResult memory result = Auctions.bucketTake(
auctions,
buckets,
deposits,
loans,
poolState,
borrowerAddress_,
depositTake_,
index_,
1
);
// update pool balances state
uint256 t0PoolDebt = poolBalances.t0Debt;
uint256 t0DebtInAuction = poolBalances.t0DebtInAuction;
if (result.t0DebtPenalty != 0) {
t0PoolDebt += result.t0DebtPenalty;
t0DebtInAuction += result.t0DebtPenalty;
}
t0PoolDebt -= result.t0RepayAmount;
t0DebtInAuction -= result.t0DebtInAuctionChange;
poolBalances.t0Debt = t0PoolDebt;
poolBalances.t0DebtInAuction = t0DebtInAuction;
poolBalances.pledgedCollateral -= result.collateralAmount;
// update pool interest rate state
poolState.debt = result.poolDebt;
poolState.collateral -= result.collateralAmount;
_updateInterestState(poolState, result.newLup);
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:
* - update borrowerTokens and bucketTokenIds arrays
* @dev emit events:
* - 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;
uint256 noOfTokensToTransfer = borrowerCollateral_ != 0 ? noOfTokensPledged - borrowerCollateral_ / 1e18 : noOfTokensPledged;
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.
* @notice Reverts in case token id is not supported by subset pool.
* @param poolTokens_ Array in pool that tracks NFT ids (could be tracking NFTs pledged by borrower or NFTs 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;
uint8 nftType = _getArgUint8(NFT_TYPE);
for (uint256 i = 0; i < tokenIds_.length;) {
uint256 tokenId = tokenIds_[i];
if (subset && !tokenIdsAllowed[tokenId]) revert OnlySubset();
poolTokens_.push(tokenId);
if (nftType == uint8(NFTTypes.STANDARD_ERC721)){
_transferNFT(msg.sender, address(this), tokenId);
}
else if (nftType == uint8(NFTTypes.CRYPTOKITTIES)) {
ICryptoKitties(_getArgAddress(COLLATERAL_ADDRESS)).transferFrom(msg.sender ,address(this), tokenId);
}
else{
ICryptoPunks(_getArgAddress(COLLATERAL_ADDRESS)).buyPunk(tokenId);
}
unchecked { ++i; }
}
}
/**
* @notice Helper function for transferring multiple NFT tokens from pool to given address.
* @notice It transfers NFTs from the most recent one added into the pool (pop from array tracking NFTs in pool).
* @param toAddress_ Address where pool should transfer tokens to.
* @param poolTokens_ Array in pool that tracks NFT ids (could be tracking NFTs pledged by borrower or NFTs 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;
uint8 nftType = _getArgUint8(NFT_TYPE);
for (uint256 i = 0; i < amountToRemove_;) {
uint256 tokenId = poolTokens_[--noOfNFTsInPool]; // start with transferring the last token added in bucket
poolTokens_.pop();
if (nftType == uint8(NFTTypes.STANDARD_ERC721)){
_transferNFT(address(this), toAddress_, tokenId);
}
else if (nftType == uint8(NFTTypes.CRYPTOKITTIES)) {
ICryptoKitties(_getArgAddress(COLLATERAL_ADDRESS)).transfer(toAddress_, tokenId);
}
else {
ICryptoPunks(_getArgAddress(COLLATERAL_ADDRESS)).transferPunk(toAddress_, tokenId);
}
tokensTransferred[i] = tokenId;
unchecked { ++i; }
}
return tokensTransferred;
}
/**
* @dev Helper function to transfer an NFT from owner to target address (reused in code to reduce contract deployment bytecode size).
* @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)).safeTransferFrom(from_, to_, tokenId_);
}
/************************/
/*** Helper Functions ***/
/************************/
/** @notice Implementing this method allows contracts to receive ERC721 tokens
* @dev https://forum.openzeppelin.com/t/erc721holder-ierc721receiver-and-onerc721received/11828
*/
function onERC721Received(address, address, uint256, bytes memory) external pure returns (bytes4) {
return this.onERC721Received.selector;
}
}