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

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

Gas Optimizations #173

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

Comments

@code423n4
Copy link
Contributor

1. Uint variables have default value to 0, so dont have to be initialized

In Cally.sol#L94-L95, uint varibles are initialzied to 0, this can be avoided as they default to zero when they are declared

    // Before
    uint256 public feeRate = 0;
    uint256 public protocolUnclaimedFees = 0;

    // After
    uint256 public feeRate;
    uint256 public protocolUnclaimedFees;

Similar also in src/Cally.sol#L282

      // Before
      uint256 fee = 0;

      // After
      uint256 fee;

2. checking a != 0 is cheaper than a > 0 for uint

in Cally.sol#L170, durationDays > 0 can be replaced with durationDays != 0 since it is uint.

3. Several variables are declared for one time use only

Several variables are declared for using only one times, This can be avoided by using the expression directly to save gas.
eg in src/Cally.sol#L223-L224

    // Before:
        uint256 premium = getPremium(vaultId);
        require(msg.value >= premium, "Incorrect ETH amount sent");

    // After
        require(msg.value >= getPremium(vaultId), "Incorrect ETH amount sent");

Similar issues are also in:

  • Cally.sol#L227-L228

          // Before
          uint32 auctionStartTimestamp = vault.currentExpiration;
          require(block.timestamp >= auctionStartTimestamp, "Auction not started");
    
          // After
          require(block.timestamp >= vault.currentExpiration, "Auction not started");
  • src/Cally.sol#L278-L279

          // Before
          vault.isExercised = true;
          _vaults[vaultId] = vault;
          
          // After
          _vaults[vaultId].isExercised = vault;
  • src/Cally.sol#L333-L334

        // Before
          uint256 optionId = vaultId + 1;
          _burn(optionId);
          _burn(vaultId);
    
        // After
          _burn(vaultId);
          _burn(vaultId + 1);
  • src/Cally.sol#L395-L396

    // Before
          Vault memory vault = _vaults[vaultId];
          return premiumOptions[vault.premiumIndex];
    // After
          return premiumOptions[_vaults[vaultId].premiumIndex];

4. prefix increment can be used in loops instead of postfix increment as it is cheaper

In src/CallyNft.sol#L244

  // Before
        for (uint256 i = 0; i < data.length; i++) {
        }
  // After
        for (uint256 i = 0; i < data.length; ++i) {
        }

5. data.length is calculated for each iteration in loop, this can be cached to save gas

In src/CallyNft.sol#L244

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

6. Using custom errors to save gase

There are a lots of require statements that use revert strings. They can be replaced with custom Errors introduced in solidity 0.8.4 which are cheaper in terms of gas.

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels May 14, 2022
code423n4 added a commit that referenced this issue May 14, 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

1 participant