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

1 stars 1 forks source link

Gas Optimizations #149

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago
  1. Change using > instead of!= for saving more gas

https://github.com/code-423n4/2022-04-jpegd/blob/e72861a9ccb707ced9015166fbded5c97c6991b6/contracts/farming/LPFarming.sol#L193

using > can be saving more gas

Tool Used

Remix

Recommendation Mitigation

Change it into !=

  1. Unnecessary code

https://github.com/code-423n4/2022-04-jpegd/blob/e72861a9ccb707ced9015166fbded5c97c6991b6/contracts/staking/JPEGStaking.sol#L50

this can be deleted to saving more gas since it unnecessary and it can be bug if can be burn amount more than msg.sender had.

Tool Used

Manual Review & Remix

  1. using ++i than i++ for saving more gas

Using i++ instead ++i for all the loops, the variable i is incremented using i++. It is known that implementation by using ++i costs less gas per iteration than i++.

Tools Used

Remix

Occurances

main/contracts/farming/LPFarming.sol#L348
main/contracts/vaults/yVault/strategies/StrategyPUSDConvex.sol#L145
main/contracts/vaults/yVault/strategies/StrategyPUSDConvex.sol#L231
main/contracts/vaults/yVault/strategies/StrategyPUSDConvex.sol#L319
  1. change uint256 i = 0 into uint ifor saving more gas

this implementation can saving more gas for each loops.

Tool Used

Manual Review & Remix

Recommended Mitigation

Change it

Occurances

main/contracts/farming/LPFarming.sol#L348
main/contracts/vaults/yVault/strategies/StrategyPUSDConvex.sol#L145
main/contracts/vaults/yVault/strategies/StrategyPUSDConvex.sol#L231
main/contracts/vaults/yVault/strategies/StrategyPUSDConvex.sol#L319
  1. Caching array length can saving more gas

this implementation can be saving more gas, since if caching the array length is more gas efficient. This is because access to a local variable in solidity is more efficient.

Tool Used

Manual Review

Occurances

main/contracts/farming/LPFarming.sol#L348
main/contracts/vaults/yVault/strategies/StrategyPUSDConvex.sol#L145
main/contracts/vaults/yVault/strategies/StrategyPUSDConvex.sol#L231
main/contracts/vaults/yVault/strategies/StrategyPUSDConvex.sol#L319
  1. Saving gas by removing = 0

If a variable was not set/initialized, it is assumed to have default value to 0 this implementation was used for saving more gas by removing = 0

POC

https://blog.polymath.network/solidity-tips-and-tricks-to-save-gas-and-reduce-bytecode-size-c44580b218e6

TOOLS USED

Remix, Manual Review

Mitigation Step

Remove = 0

Occurances

contracts/farming/LPFarming.sol#L340
contracts/farming/LPFarming.sol#L357