code-423n4 / 2022-04-jpegd-findings

1 stars 1 forks source link

Gas Optimizations #127

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Functions Visibility Can Be Declared External

Context: [JPEGLock.sol#L31-L34](), [yVaultLPFarming.sol#L41-L47](), [yVaultLPFarming.sol#L41-L47](), [JPEG.sol#L12-L18](), [StableCoin.sol#L25-L27](), [StrategyPUSDConvex.sol#L96-L165](), [Controller.sol#L25-L29](), [yVault.sol#L39-L54]()

Description: Several functions across multiple contracts have a public visibility and can be marked with external visibility to save gas.

Recommendation: Change the functions visibility to external to save gas.

In require(), Use != 0 Instead of > 0 With Uint Values

Context: [LPFarming.sol#L107-L136](), [LPFarming.sol#L214-L229](), [LPFarming.sol#L235-L252](), [LPFarming.sol#L332-L343](), [LPFarming.sol#L347-L360](), [yVaultLPFarming.sol#L100-L112](), [yVaultLPFarming.sol#L117-L130](), [yVaultLPFarming.sol#L134-L152](), [JPEGLock.sol#L39-L42](), [JPEGStaking.sol#L31-L39](), [JPEGStaking.sol#L44-L55](), [StrategyPUSDConvex.sol#L311-L351](), [yVault.sol#L98-L104](), [yVault.sol#L142-L157](), [yVault.sol#L166-L184 (For both requires)](), [FungibleAssetVaultForDAO.sol#L92-L100](), [FungibleAssetVaultForDAO.sol#L104-L115](), [FungibleAssetVaultForDAO.sol#L141-L158](), [FungibleAssetVaultForDAO.sol#L163-L174](), [FungibleAssetVaultForDAO.sol#L179-L188](), [FungibleAssetVaultForDAO.sol#L193-L206](), [NFTVault.sol#L277-L281](), [NFTVault.sol#L321-L331](), [NFTVault.sol#L360-L381](), [NFTVault.sol#L400-L405](), [NFTVault.sol#L675-L748](), [NFTVault.sol#L756-L818 (For 2 requires L764 and L770)](), [NFTVault.sol#L879-L913](), [NFTVault.sol#L919-L948]()

Description: In a require, when checking a uint, using != 0 instead of > 0 saves 6 gas. This will jump over or avoid an extra ISZERO opcode.

Recommendation: Use != 0 instead of > 0 with uint values but only in require() statements.

Catching The Array Length Prior To Loop

Context: [LPFarming.sol#L347-L360](), [StrategyPUSDConvex.sol#L311-L402](), [NFTVault.sol#L139-L188 (For both for loops)]()

Description: One can save gas by caching the array length (in stack) and using that set variable in the loop. Replace state variable reads and writes within loops with local variable reads and writes. This is done by assigning state variable values to new local variables, reading and/or writing the local variables in a loop, then after the loop assigning any changed local variables to their equivalent state variables.

Recommendation: Simply do something like so before the for loop: uint length = variable.length. Then add length in place of variable.length in the for loop.

Function Ordering via Method ID

Context: All Contracts

Description: Contracts most called functions could simply save gas by function ordering via Method ID. Calling a function at runtime will be cheaper if the function is positioned earlier in the order (has a relatively lower Method ID) because 22 gas are added to the cost of a function for every position that came before it. The caller can save on gas if you prioritize most called functions. One could use This tool to help find alternative function names with lower Method IDs while keeping the original name intact.

Recommendation: Find a lower method ID name for the most called functions for example mostCalled() vs. mostCalled_41q() is cheaper by 44 gas.

Use ++index instead of index++ to increment a loop counter

Context: [LPFarming.sol#L347-L360](), [StrategyPUSDConvex.sol#L226-L241](), [StrategyPUSDConvex.sol#L311-L402](), [NFTVault.sol#L139-L188 (For both for loops)]()

Description: Due to reduced stack operations, using ++index saves 5 gas per iteration.

Recommendation: Use ++indexto increment a loop counter.

Setting The Constructor To Payable

Context: [JPEGLock.sol#L31-L34](), [yVaultLPFarming.sol#L41-L47](), [yVaultLPFarming.sol#L41-L47](), [JPEG.sol#L12-L18](), [StableCoin.sol#L25-L27](), [StrategyPUSDConvex.sol#L96-L165](), [Controller.sol#L25-L29](), [yVault.sol#L39-L54]()

Description: You can cut out 10 opcodes in the creation-time EVM bytecode if you declare a constructor payable. Making the constructor payable eliminates the need for an initial check of msg.value == 0 and saves 21 gas on deployment with no security risks.

Recommendation: Set the constructor to payable.

Upgrade To At Least 0.8.4

Context: All Contracts

Description: Using newer compiler versions and the optimizer gives gas optimizations and additional safety checks for free!

The advantages of versions =0.8.*= over =<0.8.0= are:

Recommendation: Upgrade to at least 0.8.4 for the additional benifits.