Skip to content

Commit

Permalink
add external getters for borrower and bucketTokenIds (#1028)
Browse files Browse the repository at this point in the history
* add external getters for borrower and bucketTokenIds

* update erc721 tests to use new methods

* update comment pr feedback

---------

Co-authored-by: Mike <[email protected]>
  • Loading branch information
MikeHathaway and Mike authored Dec 19, 2023
1 parent 7506ef5 commit f7bc7dc
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 29 deletions.
8 changes: 4 additions & 4 deletions src/ERC721Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -615,13 +615,13 @@ contract ERC721Pool is FlashloanablePool, IERC721Pool {
/*******************************/

/// @inheritdoc IERC721PoolState
function totalBorrowerTokens(address borrower_) external view override returns(uint256) {
return borrowerTokenIds[borrower_].length;
function getBorrowerTokenIds(address borrower_) external view override returns(uint256[] memory) {
return borrowerTokenIds[borrower_];
}

/// @inheritdoc IERC721PoolState
function totalBucketTokens() external view override returns(uint256) {
return bucketTokenIds.length;
function getBucketTokenIds() external view override returns(uint256[] memory) {
return bucketTokenIds;
}

}
19 changes: 9 additions & 10 deletions src/interfaces/pool/erc721/IERC721PoolState.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,16 @@ interface IERC721PoolState {
) external view returns (uint256 tokenId);

/**
* @notice Returns the total `NFT` pledged by a borrower.
* @param borrower_ The address of borrower that pledged the `NFT`.
* @return Total number of `NFT`s pledged by borrower.
* @notice Returns the list of `NFT` tokenIds pledged by a borrower in a pool.
* @param borrower_ The address of borrower that pledged the `NFT`s.
* @return List of `NFT`s pledged by borrower.
*/
function totalBorrowerTokens(
address borrower_
) external view returns (uint256);
function getBorrowerTokenIds(address borrower_) external view returns(uint256[] memory);

/**
* @notice Returns the total `NFT` added in pool bucket.
* @return Total number of `NFT`s in buckets (claimable from pool).
* @notice Returns the list of `NFT` tokenIds added to pool buckets.
* @return List of `NFT`s that are claimable from the pool.
*/
function totalBucketTokens() external view returns (uint256);
}
function getBucketTokenIds() external view returns(uint256[] memory);

}
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ contract BasicERC721PoolInvariants is BasicInvariants {

for (uint256 i = 0; i < actorCount; i++) {
address borrower = IBaseHandler(_handler).actors(i);
borrowerTokens += _erc721pool.totalBorrowerTokens(borrower);
borrowerTokens += _erc721pool.getBorrowerTokenIds(borrower).length;
}

uint256 bucketTokens = _erc721pool.totalBucketTokens();
uint256 bucketTokens = _erc721pool.getBucketTokenIds().length;
require(collateralBalance == borrowerTokens + bucketTokens, "Collateral Invariant CT3");
}

Expand All @@ -117,7 +117,7 @@ contract BasicERC721PoolInvariants is BasicInvariants {

for (uint256 i = 0; i < actorCount; i++) {
address borrower = IBaseHandler(_handler).actors(i);
uint256 borrowerTokens = _erc721pool.totalBorrowerTokens(borrower);
uint256 borrowerTokens = _erc721pool.getBorrowerTokenIds(borrower).length;

(, uint256 borrowerCollateral, ) = _erc721pool.borrowerInfo(borrower);

Expand All @@ -130,7 +130,7 @@ contract BasicERC721PoolInvariants is BasicInvariants {

for (uint256 i = 0; i < actorCount; i++) {
address borrower = IBaseHandler(_handler).actors(i);
uint256 borrowerTokens = _erc721pool.totalBorrowerTokens(borrower);
uint256 borrowerTokens = _erc721pool.getBorrowerTokenIds(borrower).length;

for (uint256 tokenIndex = 0; tokenIndex < borrowerTokens; tokenIndex++) {
uint256 borrowerTokenId = _erc721pool.borrowerTokenIds(borrower, tokenIndex);
Expand All @@ -139,7 +139,7 @@ contract BasicERC721PoolInvariants is BasicInvariants {
}
}

uint256 bucketTokens = _erc721pool.totalBucketTokens();
uint256 bucketTokens = _erc721pool.getBucketTokenIds().length;
for (uint256 tokenIndex = 0; tokenIndex < bucketTokens; tokenIndex++) {
uint256 bucketTokenId = _erc721pool.bucketTokenIds(tokenIndex);

Expand All @@ -152,7 +152,7 @@ contract BasicERC721PoolInvariants is BasicInvariants {
uint256 actorCount = IBaseHandler(_handler).getActorsCount();
for (uint256 i = 0; i < actorCount; i++) {
address borrower = IBaseHandler(_handler).actors(i);
uint256 borrowerTokens = _erc721pool.totalBorrowerTokens(borrower);
uint256 borrowerTokens = _erc721pool.getBorrowerTokenIds(borrower).length;

for (uint256 tokenIndex = 0; tokenIndex < borrowerTokens; tokenIndex++) {
uint256 borrowerTokenId = _erc721pool.borrowerTokenIds(borrower, tokenIndex);
Expand All @@ -161,7 +161,7 @@ contract BasicERC721PoolInvariants is BasicInvariants {
}
}

uint256 bucketTokens = _erc721pool.totalBucketTokens();
uint256 bucketTokens = _erc721pool.getBucketTokenIds().length;
for (uint256 tokenIndex = 0; tokenIndex < bucketTokens; tokenIndex++) {
uint256 bucketTokenId = _erc721pool.bucketTokenIds(tokenIndex);

Expand Down
8 changes: 8 additions & 0 deletions tests/forge/unit/ERC721Pool/ERC721PoolBorrow.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ contract ERC721SubsetPoolBorrowTest is ERC721PoolBorrowTest {
borrower: _borrower,
tokenIds: tokenIdsToAdd
});

// check token balances after pledge
uint256[] memory pledgedTokens = ERC721Pool(address(_pool)).getBorrowerTokenIds(_borrower);
assertEq(pledgedTokens.length, 3);
assertEq(pledgedTokens[0], 1);
assertEq(pledgedTokens[1], 3);
assertEq(pledgedTokens[2], 5);

// borrower borrows from the pool
uint256 borrowAmount = 3_000 * 1e18;
_borrow({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ contract ERC721PoolLiquidationsSettleAuctionTest is ERC721HelperContract {
_assertCollateralInvariants();

// 1 token id (token id 3, the most recent pledged token) was moved from borrower token ids array to pool claimable token ids array after partial bad debt settle
assertEq(ERC721Pool(address(_pool)).totalBorrowerTokens(_borrower), 1);
assertEq(ERC721Pool(address(_pool)).totalBucketTokens(), 1);
assertEq(ERC721Pool(address(_pool)).borrowerTokenIds(_borrower, 0), 1);
assertEq(ERC721Pool(address(_pool)).bucketTokenIds(0), 3);
assertEq(ERC721Pool(address(_pool)).getBorrowerTokenIds(_borrower).length, 1);
assertEq(ERC721Pool(address(_pool)).getBorrowerTokenIds(_borrower)[0], 1);
assertEq(ERC721Pool(address(_pool)).getBucketTokenIds().length, 1);
assertEq(ERC721Pool(address(_pool)).getBucketTokenIds()[0], 3);

// all NFTs are owned by the pool
assertEq(_collateral.ownerOf(1), address(_pool));
Expand All @@ -203,11 +203,11 @@ contract ERC721PoolLiquidationsSettleAuctionTest is ERC721HelperContract {
});

// no token id left in borrower token ids array
assertEq(ERC721Pool(address(_pool)).totalBorrowerTokens(_borrower), 0);
assertEq(ERC721Pool(address(_pool)).totalBucketTokens(), 2);
assertEq(ERC721Pool(address(_pool)).getBorrowerTokenIds(_borrower).length, 0);
assertEq(ERC721Pool(address(_pool)).getBucketTokenIds().length, 2);
// tokens used to settle entire bad debt (settle auction) are moved to pool claimable array
assertEq(ERC721Pool(address(_pool)).bucketTokenIds(0), 3);
assertEq(ERC721Pool(address(_pool)).bucketTokenIds(1), 1);
assertEq(ERC721Pool(address(_pool)).getBucketTokenIds()[0], 3);
assertEq(ERC721Pool(address(_pool)).getBucketTokenIds()[1], 1);

_assertBucket({
index: 2500,
Expand Down

0 comments on commit f7bc7dc

Please sign in to comment.