code-423n4 / 2022-06-badger-findings

0 stars 0 forks source link

Gas Optimizations #132

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

To optimize the for loop and make it consume less gas, i suggest to:

  1. If a variable is not set/initialized, it is assumed to have the default value (0 for uint, false for bool, address(0) for address…). Explicitly initializing it with its default value is an anti-pattern and wastes gas.

  2. Use ++i instead of i++, which is a cheaper operation (in this case there is no difference between i++ and ++i because we dont use the return value of this expression, which is the only difference between these two expression).

As an example:

for (uint256 i = 0; i < numIterations; ++i) { 

should be replaced with

for (uint256 i; i < numIterations; ++i) {

and also i suggest to save the variable that load and array in a new variable like

for (uint256 i = 0; i < protectedTokens.length; i++) {

change to :

uint256 _protectedTokens=protectedTokens.length;
for (uint256 i = 0; i < _protectedTokens; i++) {

i suggest to change code below: https://github.com/Badger-Finance/vested-aura/blob/v0.0.2/contracts/MyStrategy.sol#L118 https://github.com/Badger-Finance/vested-aura/blob/v0.0.2/contracts/MyStrategy.sol#L300 https://github.com/Badger-Finance/vested-aura/blob/v0.0.2/contracts/MyStrategy.sol#L317 https://github.com/Badger-Finance/badger-vaults-1.5/blob/0.1.0/contracts/BaseStrategy.sol#L158

GalloDaSballo commented 2 years ago

3 gas per instance + 5 gas per increment

JeeberC4 commented 2 years ago

Warden submitted multiple Gas Optimizations. Will not be judged.