re-al-Foundation / rwa-contracts

0 stars 0 forks source link

[RWV-06C] Potentially Inefficient Summation of Shares #89

Closed chasebrownn closed 5 months ago

chasebrownn commented 5 months ago

RWV-06C: Potentially Inefficient Summation of Shares

Type Severity Location
Gas Optimization RWAVotingEscrow.sol:L383, L401

Description:

The RWAVotingEscrow::split function will inefficiently perform two shares.length loops; once for calculating the sum of shares and once for the actual split of the NFT.

Example:

for (uint256 i = len; i != 0;) {
    unchecked {
        --i;
    }
    totalShares += shares[i];
}

// fetch voting escrow data storage
VotingEscrowStorage storage $ = _getVotingEscrowStorage();
// fetch remaining duration of tokenId
uint256 remainingVestingDuration = $._remainingVestingDuration[tokenId];
// fetch lockedBalance of tokenId
uint256 lockedBalance = $._lockedBalance[tokenId];
// store lockedBalance in another var (will be used later)
uint256 remainingBalance = lockedBalance;
// find timestamp of current block
uint48 mintingTimestamp = clock();
// start iterating through shares array
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;

Recommendation:

We advise the function to instead accept the totalShares as an argument, validating it after the for distribution loop concludes by summing it alongside the distributions.

chasebrownn commented 5 months ago

Acknowledged