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

Open
code423n4 opened this issue Jun 2, 2022 · 1 comment
Open

Gas Optimizations #178

code423n4 opened this issue Jun 2, 2022 · 1 comment
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

Gas Optimizations

Unnecessarily initialized variable

    uint256 public periodFinish = 0;
    uint256 public rewardRate = 0;
    uint256 public lastUpdateTime;
    uint256 public rewardPerTokenStored;
    uint256 public queuedRewards = 0;
    uint256 public currentRewards = 0;
    uint256 public historicalRewards = 0;

proposed change:

    uint256 public periodFinish;
    uint256 public rewardRate;
    uint256 public lastUpdateTime;
    uint256 public rewardPerTokenStored;
    uint256 public queuedRewards;
    uint256 public currentRewards;
    uint256 public historicalRewards;

https://github.com/code-423n4/2022-05-vetoken/blob/2d7cd1f6780a9bcc8387dea8fecfbd758462c152/contracts/BaseRewardPool.sol#L66-L72

Loop optimizations

for (uint256 i = 0; i < extraRewards.length; i++) { 
            IRewards(extraRewards[i]).stake(msg.sender, _amount);
        }

proposed change:

uint extraRewardsLength = extraRewards.length;
for (uint256 i; i < extraRewardsLength;) { 
            IRewards(extraRewards[i]).stake(msg.sender, _amount);
            unchecked { ++i;}
        }

https://github.com/code-423n4/2022-05-vetoken/blob/2d7cd1f6780a9bcc8387dea8fecfbd758462c152/contracts/BaseRewardPool.sol#L176-L178

for (uint256 i = 0; i < extraRewards.length; i++) {
                IRewards(extraRewards[i]).getReward(_account);
            }

proposed change:

uint extraRewardsLength = extraRewards.length;
for (uint256 i; i < extraRewardsLength;) {
                IRewards(extraRewards[i]).getReward(_account);
                unchecked { ++i;}
            }

https://github.com/code-423n4/2022-05-vetoken/blob/2d7cd1f6780a9bcc8387dea8fecfbd758462c152/contracts/BaseRewardPool.sol#L282-L283

for (uint256 i = 0; i < extraRewards.length; i++) {
            IRewards(extraRewards[i]).withdraw(msg.sender, amount);
        }

proposed change:

uint extraRewardsLength = extraRewards.length;
for (uint256 i; i < extraRewardsLength;) { 
            IRewards(extraRewards[i]).withdraw(msg.sender, amount);
            unchecked { ++i;}
        }

https://github.com/code-423n4/2022-05-vetoken/blob/2d7cd1f6780a9bcc8387dea8fecfbd758462c152/contracts/BaseRewardPool.sol#L245-L247

for (uint256 i = 0; i < extraRewards.length; i++) { 
            IRewards(extraRewards[i]).withdraw(msg.sender, amount);
        }

proposed change:

uint extraRewardsLength = extraRewards.length;
for (uint256 i ; i < extraRewardslength;) {
            IRewards(extraRewards[i]).withdraw(msg.sender, amount);
            unchecked {++i;}
        }

https://github.com/code-423n4/2022-05-vetoken/blob/2d7cd1f6780a9bcc8387dea8fecfbd758462c152/contracts/BaseRewardPool.sol#L218-L221

for (uint256 i = 0; i < extraRewards.length; i++) { 
            IRewards(extraRewards[i]).stake(_for, _amount);
        }

proposed change:

uint extraRewardsLength = extraRewards.length;
for (uint256 i; i < extraRewardsLength;) {
            IRewards(extraRewards[i]).stake(_for, _amount);
            unchecked { ++i;}
        }

https://github.com/code-423n4/2022-05-vetoken/blob/2d7cd1f6780a9bcc8387dea8fecfbd758462c152/contracts/BaseRewardPool.sol#L199-L201

≠ 0 is cheaper than > 0 in require statements

require(_amount > 0, "RewardPool : Cannot stake 0"); 

proposed change:

require(_amount != 0, "RewardPool : Cannot stake 0"); 

https://github.com/code-423n4/2022-05-vetoken/blob/2d7cd1f6780a9bcc8387dea8fecfbd758462c152/contracts/BaseRewardPool.sol#L196

require(amount > 0, "RewardPool : Cannot withdraw 0");

proposed change:

require(amount != 0, "RewardPool : Cannot withdraw 0");

https://github.com/code-423n4/2022-05-vetoken/blob/2d7cd1f6780a9bcc8387dea8fecfbd758462c152/contracts/BaseRewardPool.sol#L215

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Jun 2, 2022
code423n4 added a commit that referenced this issue Jun 2, 2022
@GalloDaSballo
Copy link
Collaborator

Less than 500 gas saved

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)
Projects
None yet
Development

No branches or pull requests

2 participants