re-al-Foundation / rwa-contracts

0 stars 0 forks source link

[RWV-04C] Inefficient Increment of Token IDs #87

Closed chasebrownn closed 5 months ago

chasebrownn commented 5 months ago

RWV-04C: Inefficient Increment of Token IDs

Type Severity Location
Gas Optimization RWAVotingEscrow.sol:L403

Description:

The RWAVotingEscrow::split function will update the $.tokenId counter multiple times redundantly via the RWAVotingEscrow::_incrementAndGetTokenId function.

Example:

for (uint256 i = 1; i < len;) {
    // grab current share
    uint256 share = shares[i];
    // locked balance for this NFT is percentage of shares * total locked balance
    uint256 _lockedBalance = share * lockedBalance / totalShares;
    // fetch new tokenId to mint
    uint256 newTokenId = _incrementAndGetTokenId();
    // store new tokenId in tokenIds array
    tokenIds[i] = newTokenId;
    // store timeststamp for new token
    $._mintingTimestamp[newTokenId] = mintingTimestamp;
    // mint new token
    _mint(owner, newTokenId);
    // update lock info for new token
    _updateLock(newTokenId, _lockedBalance, remainingVestingDuration);
    // subtract locked balance from total balance
    unchecked {
        remainingBalance -= _lockedBalance;
        ++i;
    }
}

Recommendation:

We advise the $.tokenId value to be read once at the beginning, to be locally incremented, and to finally be written to after the for loop concludes and the total token IDs are known.

chasebrownn commented 5 months ago

Acknowledged