TangibleTNFT / baskets-foundry

baskets-foundry
2 stars 0 forks source link

[BTE-06C] Redundant Evaluation of Claimable Rent #43

Closed chasebrownn closed 9 months ago

chasebrownn commented 9 months ago

BTE-06C: Redundant Evaluation of Claimable Rent

Type Severity Location
Gas Optimization Basket.sol:L881

Description:

The way the claimableRent array is constructed guarantees that all entries within it have a non-zero rent balance to be claimed.

Example:

// iterate through all TNFT contracts supported by this basket.
for (uint256 i; i < tnftsSupported.length;) {
    address tnft = tnftsSupported[i];

    // for each TNFT supported, make a batch call to the rent manager for all rent claimable for the array of tokenIds.
    uint256[] memory claimables = _getRentManager(tnft).claimableRentForTokenBatch(tokenIdLibrary[tnft]);

    // iterate through the array of claimable rent for each tokenId for each TNFT and push it to the master claimableRent array.
    for (uint256 j; j < claimables.length;) {
        uint256 amountClaimable = claimables[j];

        if (amountClaimable > 0) {
            claimableRent[counter] = RentData(tnft, tokenIdLibrary[tnft][j], amountClaimable);
            unchecked {
                ++counter;
            }
        }
        unchecked {
            ++j;
        }
    }
    unchecked {
        ++i;
    }
}

// start iterating through the master claimable rent array claiming rent for each token.
uint256 index;
while (_withdrawAmount > primaryRentToken.balanceOf(address(this)) && index < counter) {

    IRentManager rentManager = _getRentManager(claimableRent[index].tnft);
    uint256 tokenId = claimableRent[index].tokenId;

    if (rentManager.claimableRentForToken(tokenId) > 0) {

        uint256 preBal = primaryRentToken.balanceOf(address(this));
        uint256 claimedRent = rentManager.claimRentForToken(tokenId);

        require(primaryRentToken.balanceOf(address(this)) == (preBal + claimedRent), "claiming error");
    }

    unchecked {
        ++index;
    }
}

Recommendation:

We advise the referenced if conditional to be omitted, greatly optimizing the function's gas cost.

chasebrownn commented 9 months ago

Fixed