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

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

Gas Optimizations #147

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

Comments

@code423n4
Copy link
Contributor

  1. In function https://github.com/code-423n4/2022-05-vetoken/blob/2d7cd1f6780a9bcc8387dea8fecfbd758462c152/contracts/VE3DLocker.sol#L206, we could avoid extra storage reads by declaring the following in memory

rewardData[_rewardsToken].ve3Token,
rewardData[_rewardsToken].ve3TokenStaking,
rewardData[_rewardsToken].veAssetDeposits.
Since the calls are made inside a for loop, this saves around 3100-32*3=282 gas per iteration.

  1. Very similar to the above point, in the function, https://github.com/code-423n4/2022-05-vetoken/blob/2d7cd1f6780a9bcc8387dea8fecfbd758462c152/contracts/VE3DRewardPool.sol#L275 , the following state variables can be stored in memory before accessing them.
    rewardTokenInfo[_rewardToken].veAssetDeposits,
    rewardTokenInfo[_rewardToken].ve3Token,
    rewardTokenInfo[_rewardToken].ve3TokenRewards

They are all called inside a for loop, so this will save a lot of gas.

  1. The if statement here, https://github.com/code-423n4/2022-05-vetoken/blob/2d7cd1f6780a9bcc8387dea8fecfbd758462c152/contracts/ExtraRewardStashV3.sol#L87 is redundant. You could simply assign tokenCount to i and save 100 gas.

  2. There is some redundancy in the claimRewards function.
    https://github.com/code-423n4/2022-05-vetoken/blob/2d7cd1f6780a9bcc8387dea8fecfbd758462c152/contracts/ExtraRewardStashV1.sol#L77 to line 81 could be rewritten as:

    if (tokenInfo.token == address(0)) {
        setToken();
    } else 
        address tic = tokenInfo.token; 
        address st = staker;
        uint256 before = IERC20(tic).balanceOf(st);
    

Thus replacing the two state variable reads to memory reads. It saves gas and increases code readability.

@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