code-423n4 / 2022-04-badger-citadel-findings

0 stars 1 forks source link

Gas Optimizations #226

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

1.)No need to save an array value in a variable

Summary

use an array value right away without saving it to a variable first will save gas

POC

https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/lib/GlobalAccessControlManaged.sol#L49-L50

Before ...

for (uint256 i = 0; i < roles.length; i++) {
       bytes32 role = roles[i];
       if (gac.hasRole(role, msg.sender)) {
          validRoleFound = true;
          break;
       }
   }

...

After ...

for (uint256 i = 0; i < roles.length; i++) {
       if (gac.hasRole(roles[i], msg.sender)) {
           validRoleFound = true;
           break;
       }
   }

...

2.)No need to save the same value to another variable

POC

https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/Funding.sol#L233-L234

Before '''

function getRemainingFundable() external view returns (uint256 limitLeft_) {
    uint256 assetCumulativeFunded = funding.assetCumulativeFunded;
    uint256 assetCap = funding.assetCap;
    if (assetCumulativeFunded < assetCap) {
        limitLeft_ = assetCap - assetCumulativeFunded;
    }
}

''' After '''

function getRemainingFundable() external view returns (uint256 limitLeft_) {
    if (funding.assetCumulativeFunded < funding.assetCap) {
        limitLeft_ = funding.assetCap - funding.assetCumulativeFunded;
    }
}

'''