Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas Optimizations #188

Open
code423n4 opened this issue Apr 27, 2022 · 0 comments
Open

Gas Optimizations #188

code423n4 opened this issue Apr 27, 2022 · 0 comments
Assignees
Labels
bug Something isn't working G (Gas Optimization) resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) reviewed Issues that Backd has reviewed (just for internal tracking, can ignore this)

Comments

@code423n4
Copy link
Contributor

GAS

G-01: Unnecessary if statement in processExpiredLocks

CvxCrvRewardsLocker.processExpiredLocks; L133-L145

Gas could be saved by changing:

    function processExpiredLocks(bool relock) external override returns (bool) {
        if (relock) {
            require(!prepareWithdrawal, Error.PREPARED_WITHDRAWAL);
        }

        if (relock) {
            ICvxLocker(CVX_LOCKER).processExpiredLocks(relock);
        } else {
            ICvxLocker(CVX_LOCKER).withdrawExpiredLocksTo(treasury);
        }

        return true;
    }

To:

    function processExpiredLocks(bool relock) external override returns (bool) {
        if (relock) {
            require(!prepareWithdrawal, Error.PREPARED_WITHDRAWAL);
            ICvxLocker(CVX_LOCKER).processExpiredLocks(relock);
        } else {
            ICvxLocker(CVX_LOCKER).withdrawExpiredLocksTo(treasury);
        }

        return true;
    }

G-02: Changing parameters order could save some gas

TopUpActionFeeHandler.payFees#L81-L109

If lpToken.safeTransferFrom reverts keeperAmount and treasuryAmount will be unnecessarily calculated.

uint256 keeperAmount = amount.scaledMul(getKeeperFeeFraction());
uint256 treasuryAmount = amount.scaledMul(getTreasuryFeeFraction());
LpToken lpToken = LpToken(lpTokenAddress);

lpToken.safeTransferFrom(msg.sender, address(this), amount);

I suggest setting keeperAmount and treasuryAmount after lpToken.safeTransferFrom as seen below.


LpToken lpToken = LpToken(lpTokenAddress);

lpToken.safeTransferFrom(msg.sender, address(this), amount);

uint256 keeperAmount = amount.scaledMul(getKeeperFeeFraction());
uint256 treasuryAmount = amount.scaledMul(getTreasuryFeeFraction());

Ga-03: For loop in hasAnyRole can be more gas efficient

RoleManager.hasAnyRole#L73-L86

  • It is unnecessary to initialize i = 0 as zero is already the default value.
  • Caching roles.length would save gas from calculating it every iteration
  • Using a prefix ++i is more effecient than using a postfix i++

In summary I suggest changing it from:

for (uint256 i = 0; i < roles.length; i++) {
    if (hasRole(roles[i], account)) {
        return true;
    }
}

To:

size = roles.length;
for (uint256 i; i < size; ++i) {
    if (hasRole(roles[i], account)) {
        return true;
    }
}

Also valid for(list not exhaustive):

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Apr 27, 2022
code423n4 added a commit that referenced this issue Apr 27, 2022
@chase-manning chase-manning added the reviewed Issues that Backd has reviewed (just for internal tracking, can ignore this) label Apr 28, 2022
@chase-manning chase-manning self-assigned this May 2, 2022
@chase-manning chase-manning added the resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) label May 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization) resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) reviewed Issues that Backd has reviewed (just for internal tracking, can ignore this)
Projects
None yet
Development

No branches or pull requests

2 participants