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

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

Gas Optimizations #230

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

Comments

@code423n4
Copy link
Contributor

GAS OPT

  1. Title : Using ++i than i++ for saving more gas

Using i++ instead ++i for all the loops, the variable i is incremented using i++. It is known that implementation by using ++i costs less gas per iteration than i++.

Tools Used

Manual Review

Occurances

main/contracts/Booster.sol#L329                for (uint256 i = 0; i < poolInfo.length; i++) {
main/contracts/BaseRewardPool.sol#L282         for (uint256 i = 0; i < poolInfo.length; i++) { 
main/contracts/BaseRewardPool.sol#L176         for (uint256 i = 0; i < extraRewards.length; i++) {
main/contracts/BaseRewardPool.sol#L199         for (uint256 i = 0; i < extraRewards.length; i++) {
main/contracts/BaseRewardPool.sol#L218         for (uint256 i = 0; i < extraRewards.length; i++) {
main/contracts/BaseRewardPool.sol#L244         for (uint256 i = 0; i < extraRewards.length; i++) {
main/contracts/BaseRewardPool.sol#L282         for (uint256 i = 0; i < extraRewards.length; i++) {
main/contracts/VE3DRewardPool.sol#L148         for (uint256 i = 0; i < rewardTokens.length(); i++) {
main/contracts/VE3DRewardPool.sol#L214         for (uint256 i = 0; i < length; i++) {
main/contracts/VE3DRewardPool.sol#L238         for (uint256 i = 0; i < length; i++) {
main/contracts/VE3DRewardPool.sol#L257         for (uint256 i = 0; i < length; i++) {
main/contracts/VE3DRewardPool.sol#L281         for (uint256 i = 0; i < rewardTokens.length(); i++) {
main/contracts/VE3DRewardPool.sol#L326         for (uint256 i = 0; i < length; i++) {
main/contracts/VoterProxy.sol#L217             for (uint256 i = 0; i < _tokenVote.length; i++) {
  1. Title : change uint256 i = 0 into uint256 i for saving more gas

using this implementation can saving more gas for each loops.

Tool Used

Manual Review

Recommended Mitigation

Change it

Occurances

main/contracts/Booster.sol#L329                for (uint256 i = 0; i < poolInfo.length; i++) {
main/contracts/BaseRewardPool.sol#L282         for (uint256 i = 0; i < poolInfo.length; i++) { 
main/contracts/BaseRewardPool.sol#L176         for (uint256 i = 0; i < extraRewards.length; i++) {
main/contracts/BaseRewardPool.sol#L199         for (uint256 i = 0; i < extraRewards.length; i++) {
main/contracts/BaseRewardPool.sol#L218         for (uint256 i = 0; i < extraRewards.length; i++) {
main/contracts/BaseRewardPool.sol#L244         for (uint256 i = 0; i < extraRewards.length; i++) {
main/contracts/BaseRewardPool.sol#L282         for (uint256 i = 0; i < extraRewards.length; i++) {
main/contracts/VE3DRewardPool.sol#L148         for (uint256 i = 0; i < rewardTokens.length(); i++) {
main/contracts/VE3DRewardPool.sol#L214         for (uint256 i = 0; i < length; i++) {
main/contracts/VE3DRewardPool.sol#L238         for (uint256 i = 0; i < length; i++) {
main/contracts/VE3DRewardPool.sol#L257         for (uint256 i = 0; i < length; i++) {
main/contracts/VE3DRewardPool.sol#L281         for (uint256 i = 0; i < rewardTokens.length(); i++) {
main/contracts/VE3DRewardPool.sol#L326         for (uint256 i = 0; i < length; i++) {
main/contracts/VoterProxy.sol#L217             for (uint256 i = 0; i < _tokenVote.length; i++) {
  1. Title : Caching array length can saving more gas

This implementation can be saving more gas, since if caching the array length is more gas efficient.
just because access to a local variable in solidity is more efficient.

Tool Used

Manual Review

Occurances

main/contracts/Booster.sol#L329                for (uint256 i = 0; i < poolInfo.length; i++) {
main/contracts/BaseRewardPool.sol#L282         for (uint256 i = 0; i < poolInfo.length; i++) { 
main/contracts/BaseRewardPool.sol#L176         for (uint256 i = 0; i < extraRewards.length; i++) {
main/contracts/BaseRewardPool.sol#L199         for (uint256 i = 0; i < extraRewards.length; i++) {
main/contracts/BaseRewardPool.sol#L218         for (uint256 i = 0; i < extraRewards.length; i++) {
main/contracts/BaseRewardPool.sol#L244         for (uint256 i = 0; i < extraRewards.length; i++) {
main/contracts/BaseRewardPool.sol#L282         for (uint256 i = 0; i < extraRewards.length; i++) {
main/contracts/VE3DRewardPool.sol#L148         for (uint256 i = 0; i < rewardTokens.length(); i++) {
main/contracts/VE3DRewardPool.sol#L281         for (uint256 i = 0; i < rewardTokens.length(); i++) {
main/contracts/VoterProxy.sol#L217             for (uint256 i = 0; i < _tokenVote.length; i++) {
  1. Title : Using > 0 costs more gas than != 0 when used on uints in a require() statement

1.) File : contracts/VeAssetDepositor.sol (Line.132)

        require(_amount > 0, "!>0");

2.) FIle : contracts/BaseRewardPool.sol (Line.215)

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

3.) File : contracts/BaseRewardPool.sol (Line.173)

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

4.) File : contracts/BaseRewardPool.sol (Line.196)

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

5.) contracts/BaseRewardPool.sol (Line.215)

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

6.) File : contracts/VE3DRewardPool.sol (Line.210)

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

7.) File : contracts/VE3DRewardPool.sol (Line.234)

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

8.) File : contracts/VE3DRewardPool.sol (Line.254)

        require(_amount > 0, "RewardPool : Cannot stake 0");
  1. Title : Saving gas by removing = 0

This implementation code can be saving more gas by removing = 0, it because If a variable was not set/initialized, it is assumed to have default value to 0

Tool Used

Manual Review

Mitigation Step

Remove = 0

Occurances

main/contracts/BaseRewardPool.sol#L274             rewards[_account] = 0;
main/contracts/VeAssetDepositor.sol#L28             uint256 public incentiveVeAsset = 0;
main/contracts/VeAssetDepositor.sol#L119            incentiveVeAsset = 0;
main/contracts/VeAssetDepositor.sol#L141            incentiveVeAsset = 0;
main/contracts/BaseRewardPool.sol#L66               uint256 public periodFinish = 0;
main/contracts/BaseRewardPool.sol#L67               uint256 public rewardRate = 0;
main/contracts/BaseRewardPool.sol#L70                uint256 public queuedRewards = 0;
main/contracts/BaseRewardPool.sol#L71                uint256 public currentRewards = 0;
main/contracts/BaseRewardPool.sol#L72                uint256 public historicalRewards = 0;
main/contracts/VE3DRewardPool.sol#L361             rewardTokenInfo[_rewardToken].queuedRewards = 0;
  1. Title : Value can be set as immutable

This can be set as immutable for saving more gas

Tool Used

Remix

Recommended Mitigation

add immutable

Occurances

1.) File : contracts/VeTokenMinter.sol (Line.16)

    ERC20 public veToken;

2.) File : contracts/VoterProxy.sol (Line.31)

    IVoteEscrow.EscrowModle public escrowModle;

3.) File : contracts/BaseRewardPool.sol (Lines.55-56)

    IERC20 public rewardToken;
    IERC20 public stakingToken;

4.) File : contracts/BaseRewardPool.sol (Lines.62-63)

    address public operator;
    address public rewardManager;
  1. reorder function addExtraReward for saving more gas

https://github.com/code-423n4/2022-05-vetoken/blob/main/contracts/BaseRewardPool.sol#L121-L129

this implementation below can be saving +- 200 gas

Tool Used

Manual Review, Remix

Recommended Mitigation

    function addExtraReward(address _reward) external returns (bool) {
        require(_reward != address(0), "!reward setting"); // => this can be excecuted first
        require(msg.sender == rewardManager, "!authorized");
        require(extraRewards.length < EXTRA_REWARD_POOLS, "!extra reward pools exceed");


        extraRewards.push(_reward);
        emit ExtraRewardAdded(_reward);
        return true; 
    }
// 3676423 before changed 
// 3676216 after changed (saving +- 200 gas)
  1. Title : public functions should be declared external for saving more gas

1.) File : contracts/BaseRewardPool.sol (Line.196)

    function stakeFor(address _for, uint256 _amount) public updateReward(_for) returns (bool) {

2.) File : contracts/Booster.sol (Line.434)

    function withdrawAll(uint256 _pid) public returns (bool) {

3.) File : contracts/VeTokenMinter.sol (Line.32)

    function addOperator(address _newOperator) public onlyOwner {

4.) File : contracts/VeTokenMinter.sol (Line.36)

        function removeOperator(address _operator) public onlyOwner {
  1. Title Caching in calldata instead of memory

1.) File : contracts/VoterProxy.sol (Line.199)

    function isValidSignature(bytes32 _hash, bytes memory) public view returns (bytes4) {
@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

6 Immutables * 2100 = 12600

Rest is roughly another 1k

13600

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