re-al-Foundation / rwa-contracts

0 stars 0 forks source link

[RDR-02C] Inefficient Array Shift Operation #93

Closed chasebrownn closed 5 months ago

chasebrownn commented 5 months ago

RDR-02C: Inefficient Array Shift Operation

Type Severity Location
Gas Optimization RevenueDistributor.sol:L276

Description:

The referenced operation will be redundantly performed when i is equal to len - 1.

Example:

function removeRevenueToken(address _revToken) external onlyOwner {
    require(isRevToken[_revToken], "token not added");

    isRevToken[_revToken] = false;
    uint256 len = revenueTokens.length;

    for (uint256 i; i < len;) {
        if (revenueTokens[i] == _revToken) {
            revenueTokens[i] = revenueTokens[len - 1];
            revenueTokens.pop();
            break;
        }
        unchecked {
            ++i;
        }
    }

    emit RevTokenRemoved(_revToken);
}

Recommendation:

We advise the statement to be wrapped in an if conditional that prevents this case, optimizing the code's gas cost.

chasebrownn commented 5 months ago

Acknowledged