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 #137

Open
code423n4 opened this issue Oct 30, 2022 · 1 comment
Open

Gas Optimizations #137

code423n4 opened this issue Oct 30, 2022 · 1 comment
Labels

Comments

@code423n4
Copy link
Contributor

code423n4 commented Oct 30, 2022

X = X + Y / X = X - Y IS CHEAPER THAN X += Y / X -= Y

The demo of the gas comparison can be seen here.

Consider use X = X + Y / X = X - Y to save gas.

Instances number of this issue:

268        pledgeAvailableRewardAmounts[pledgeId] -= rewardAmount;

340        pledgeAvailableRewardAmounts[vars.newPledgeID] += vars.totalRewardAmount;
401        pledgeAvailableRewardAmounts[pledgeId] += totalRewardAmount;
445        pledgeAvailableRewardAmounts[pledgeId] += totalRewardAmount;

Update value order can be adjusted to simplify the code and save gas

For example, to update the num variable with newVal, the current way is as following:

    uint oldVal = num;
    num = newVal;
    emit update(oldVal, newVal);

If the execution order is adjusted, some operations can be saved (memory space allocation, variable assignment), reducing both the deployment and run time gas cost.

    emit update(num, newVal);
    num = newVal;

The operation saved: 1 sload, 1 memory allocation, 1 mstore.

The demo of the gas comparison can be seen here.

There are multiple places can use this trick for optimization, since the updates of parameters are widely and frequently used, the optimization can be beneficial.

Instances number of this issue: 3

    function updateChest(address chest) external onlyOwner {
        if(chest == address(0)) revert Errors.ZeroAddress();
        address oldChest = chestAddress;
        chestAddress = chest;

        emit ChestUpdated(oldChest, chest);
    }

    function updateMinTargetVotes(uint256 newMinTargetVotes) external onlyOwner {
        if(newMinTargetVotes == 0) revert Errors.InvalidValue();
        uint256 oldMinTarget = minTargetVotes;
        minTargetVotes = newMinTargetVotes;

        emit MinTargetUpdated(oldMinTarget, newMinTargetVotes);
    }

    function updatePlatformFee(uint256 newFee) external onlyOwner {
        if(newFee > 500) revert Errors.InvalidValue();
        uint256 oldfee = protocalFeeRatio;
        protocalFeeRatio = newFee;

        emit PlatformFeeUpdated(oldfee, newFee);
    }

Code reuse

Some function codes overlap.
Consider reuse the common part and save deployment gas.

  • retrievePledgeRewards() and closePledge() codes overlap a lot, only a few lines of difference.

  • the following are used in extendPledge(), increasePledgeRewardPerVote() and _pledge():

        Pledge storage pledgeParams = pledges[pledgeId];
        if(pledgeParams.closed) revert Errors.PledgeClosed();
        if(pledgeParams.endTimestamp <= block.timestamp) revert Errors.ExpiredPledge();

pledgeParams.closed could be deleted

This variable does not seem useful, since the check for if(pledgeParams.endTimestamp <= block.timestamp) can serve the purpose of pledgeParams.closed.

Consider delete pledgeParams.closed.

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Oct 30, 2022
code423n4 added a commit that referenced this issue Oct 30, 2022
code423n4 added a commit that referenced this issue Oct 30, 2022
@c4-judge
Copy link
Contributor

kirk-baird marked the issue as grade-b

@C4-Staff C4-Staff added the G-22 label Dec 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants