From f7bc7dc3db4ce3ea59c884ccba8e965dbc1f4c67 Mon Sep 17 00:00:00 2001 From: Mike Hathaway Date: Tue, 19 Dec 2023 09:42:52 -0500 Subject: [PATCH] add external getters for borrower and bucketTokenIds (#1028) * add external getters for borrower and bucketTokenIds * update erc721 tests to use new methods * update comment pr feedback --------- Co-authored-by: Mike --- src/ERC721Pool.sol | 8 ++++---- .../pool/erc721/IERC721PoolState.sol | 19 +++++++++---------- .../BasicERC721PoolInvariants.t.sol | 14 +++++++------- .../unit/ERC721Pool/ERC721PoolBorrow.t.sol | 8 ++++++++ .../ERC721PoolLiquidationsSettleAuction.t.sol | 16 ++++++++-------- 5 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/ERC721Pool.sol b/src/ERC721Pool.sol index ddc0e6379..697e167c8 100644 --- a/src/ERC721Pool.sol +++ b/src/ERC721Pool.sol @@ -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; } } diff --git a/src/interfaces/pool/erc721/IERC721PoolState.sol b/src/interfaces/pool/erc721/IERC721PoolState.sol index e6fb2777a..ba50ee2cb 100644 --- a/src/interfaces/pool/erc721/IERC721PoolState.sol +++ b/src/interfaces/pool/erc721/IERC721PoolState.sol @@ -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); -} \ No newline at end of file + function getBucketTokenIds() external view returns(uint256[] memory); + +} diff --git a/tests/forge/invariants/ERC721Pool/BasicERC721PoolInvariants.t.sol b/tests/forge/invariants/ERC721Pool/BasicERC721PoolInvariants.t.sol index 50c65d9db..659b6bee7 100644 --- a/tests/forge/invariants/ERC721Pool/BasicERC721PoolInvariants.t.sol +++ b/tests/forge/invariants/ERC721Pool/BasicERC721PoolInvariants.t.sol @@ -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"); } @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/tests/forge/unit/ERC721Pool/ERC721PoolBorrow.t.sol b/tests/forge/unit/ERC721Pool/ERC721PoolBorrow.t.sol index 6bf35a852..2c45beccd 100644 --- a/tests/forge/unit/ERC721Pool/ERC721PoolBorrow.t.sol +++ b/tests/forge/unit/ERC721Pool/ERC721PoolBorrow.t.sol @@ -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({ diff --git a/tests/forge/unit/ERC721Pool/ERC721PoolLiquidationsSettleAuction.t.sol b/tests/forge/unit/ERC721Pool/ERC721PoolLiquidationsSettleAuction.t.sol index f6d5b6fbf..da6021631 100644 --- a/tests/forge/unit/ERC721Pool/ERC721PoolLiquidationsSettleAuction.t.sol +++ b/tests/forge/unit/ERC721Pool/ERC721PoolLiquidationsSettleAuction.t.sol @@ -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)); @@ -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,