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

Open
code423n4 opened this issue May 25, 2022 · 0 comments
Open

Gas Optimizations #328

code423n4 opened this issue May 25, 2022 · 0 comments
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

1. 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

2. use prefix increment (++i) instead of postfix increment(i++) to save gas

Gas can be saved by using prefix increment instead of postfix increment which is very tiny, but since this is used in several places especially the loop increments, it is called several times in single transaction.

Recommended solution

  // Before
  for(uint i = 0; i < limit; i++)

  // After
  for(uint i = 0; i < limit; ++i)

3. Loop increment can never overflow since it starts from 0, so it can be unchecked

The loops counters usually start from 0 and usually only incremented by 1 on each loop iterations. So, they can never overflow/underflow. So these kinds of operations can be kept in unchecked block to save gas.

Recommended solution

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

  // After
  for(uint i = 0; i < limit; ) {
    ...
    unchecked {
      ++i;
    }
  }

4. Array length can be cached in iteration of loops

The array length is computed in each iteration which can be cached to avoid computing in each loop iteration. This will save gas.

Recommended solution

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

  // After
  uint256 len = arr.len;
  for(uint i = 0; i < len; i++) {

  }

usage

./contracts/AuraLocker.sol:696:        for (uint256 i = nextUnlockIndex; i < locks.length; i++) {
./contracts/AuraClaimZap.sol:143:        for (uint256 i = 0; i < rewardContracts.length; i++) {
./contracts/AuraClaimZap.sol:147:        for (uint256 i = 0; i < extraRewardContracts.length; i++) {
./contracts/AuraClaimZap.sol:151:        for (uint256 i = 0; i < tokenRewardContracts.length; i++) {
./contracts/AuraVestedEscrow.sol:100:        for (uint256 i = 0; i < _recipient.length; i++) {  

5. Incorrect type used for emitting a event which increases gas cost

in contracts/AuraLocker.sol#L295 event Staked takes lockAmount as its second argument of type uint112
But the event Staked takes uint256 as its argument. The use of incorrect variable types cause extra gas.

event Staked(address indexed _user, uint256 _paidAmount, uint256 _lockedAmount);

........
uint112 lockAmount = _amount.to112();

Recommended Solution

Cast the variable to uint256 before emitting the event

        emit Staked(_account, uint256(lockAmount), lockAmount);

6. uint variables are initialized to 0 by default, extra initialazation requires extra gas

When declaring state variables, uint types are set to zero by default. by, initializing them to zero, it costs extra gas on deployment. So to save gas it is better to avoid initializing them to zero.

eg.
in contracts/AuraBalRewardPool.sol#L35-L39

in contracts/AuraLocker.sol#L72

Recommended solution

  // Before
    uint256 public pendingPenalty = 0;
    uint256 public immutable startTime;

    uint256 public periodFinish = 0;
    uint256 public rewardRate = 0;

  // After
    uint256 public pendingPenalty;
    uint256 public immutable startTime;

    uint256 public periodFinish;
    uint256 public rewardRate;
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels May 25, 2022
code423n4 added a commit that referenced this issue May 25, 2022
@0xMaharishi 0xMaharishi added the duplicate This issue or pull request already exists label May 28, 2022
@dmvt dmvt removed the duplicate This issue or pull request already exists label Jun 25, 2022
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

3 participants