re-al-Foundation / rwa-contracts

0 stars 0 forks source link

[DFY-03C] Optimization of Code Structure #80

Closed chasebrownn closed 5 months ago

chasebrownn commented 5 months ago

DFY-03C: Optimization of Code Structure

Type Severity Location
Gas Optimization DelegateFactory.sol:L154-L160

Description:

The referenced code block is inefficient and can be removed in favour of an else clause that always increments the i iterator.

Example:

for (uint256 i; i < length;) {
    address delegator = delegators[i];
    if (isExpiredDelegator(delegator)) {
        // call withdrawDelegatedToken
        (bool success,) = delegator.call(abi.encodeWithSignature("withdrawDelegatedToken()"));
        require(success, "withdraw unsuccessful");

        // delete delegator from contract
        delete isDelegator[delegator];
        delete delegatorExpiration[delegator];
        delegators[i] = delegators[length - 1];
        delegators.pop();
        --length;

        emit DelegatorDeleted(delegator);
    }
    if (i < length) {
        if (!isExpiredDelegator(delegators[i])) {
            unchecked {
                ++i;
            }
        }
    }
}

Recommendation:

We advise the aforementioned else clause to be introduced to the first if conditional within the for loop, optimizing the code's clarity and gas cost.

chasebrownn commented 5 months ago

Acknowledged