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

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

Gas Optimizations #124

code423n4 opened this issue Jun 3, 2022 · 1 comment
Labels
bug Something isn't working G (Gas Optimization) resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@code423n4
Copy link
Contributor

1. array length in loops can be cached instead of calculating in every iteration

The loop bounds are calculated with array.length which are calculated in every loop iterations which can result in high gas.
The array length can be cached instead of calculating in every loop iteration to save gas.

// Before
for (uint i = 0; i < amounts.length; i++) {
}

// After
uint len = amounts.length;
for (uint i = 0; i < len; i++) {
}

The instances where this pattern can be applied is found as follows

> grep -rn './contracts/tokenomics' -e 'for.*[.]length'
./contracts/tokenomics/FeeBurner.sol:59:        for (uint256 i; i < tokens_.length; i = i.uncheckedInc()) {
./contracts/tokenomics/VestedEscrow.sol:94:        for (uint256 i; i < amounts.length; i = i.uncheckedInc()) {
./contracts/tokenomics/InflationManager.sol:119:        for (uint256 i; i < stakerVaults.length; i = i.uncheckedInc()) {

2. Use solidity custom errors to save gas

solidity 0.8.4 introduces custom errors which are cheaper than using revert strings in terms of gas
Use the custom error patterns to reduce gas cost.

for eg.

  // Before
  require(condition, "Revert strings");

  // After
  error CustomError();
  if (!condition) {
    revert CustomError();
  }

more details can be found here

3. !=0 is cheaper than >0

  • In require statement, using !=0 is cheaper than using >0 for uints
> grep -rn "./contracts/tokenomics" -e "require(.* > 0"
./contracts/tokenomics/AmmConvexGauge.sol:158:        require(amount > 0, Error.INVALID_AMOUNT);
./contracts/tokenomics/AmmConvexGauge.sol:171:        require(amount > 0, Error.INVALID_AMOUNT);
./contracts/tokenomics/VestedEscrow.sol:84:        require(unallocatedSupply > 0, "No reward tokens in contract");
./contracts/tokenomics/KeeperGauge.sol:142:        require(totalClaimable > 0, Error.ZERO_TRANSFER_NOT_ALLOWED);
./contracts/tokenomics/AmmGauge.sol:111:        require(amount > 0, Error.INVALID_AMOUNT);
./contracts/tokenomics/AmmGauge.sol:136:        require(amount > 0, Error.INVALID_AMOUNT);
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Jun 3, 2022
code423n4 added a commit that referenced this issue Jun 3, 2022
@chase-manning chase-manning added sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) labels Jun 6, 2022
@GalloDaSballo
Copy link
Collaborator

1. array length in loops can be cached instead of calculating in every iteration

Would save 3 gas
3 * 3 = 9

2. Use solidity custom errors to save gas

In lack of POC I will not give points

!=0 is cheaper than >0

Will save 3 gas per instance, in Solidity < 0.8.13

3 * 6 = 18

Total Gas Saved
27

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) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

3 participants