You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Combining require statement conditions with && logic uses unnecessary gas. It is better to split up each part of the logical statement into separate require statements
Use separate require statements instead of concatenating with &&
2. Use prefix not postfix in loops
Impact
Using a prefix increment (++i) instead of a postfix increment (i++) saves gas for each loop cycle and so can have a big gas impact when the loop executes on a large number of elements.
Caching the array length outside a loop saves reading it on each iteration, as long as the array's length is not changed during the loop. This saves gas.
1. Split up require statements instead of &&
Impact
Combining require statement conditions with && logic uses unnecessary gas. It is better to split up each part of the logical statement into separate require statements
Proof of Concept
Several instances of this issue was found
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/ChainlinkPriceOracle.sol#L51
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/ChainlinkPriceOracle.sol#L86
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/ManagedIndexReweightingLogic.sol#L30-L31
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/UniswapV2PriceOracle.sol#L46
Tools Used
Manual analysis
Recommended Mitigation Steps
Use separate require statements instead of concatenating with &&
2. Use prefix not postfix in loops
Impact
Using a prefix increment (++i) instead of a postfix increment (i++) saves gas for each loop cycle and so can have a big gas impact when the loop executes on a large number of elements.
Proof of Concept
There are three examples of this
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/UniswapV2PathPriceOracle.sol#L34
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/UniswapV2PathPriceOracle.sol#L49
Tools Used
Manual analysis
Recommended Mitigation Steps
Use prefix not postfix to increment in a loop
3. Short require strings save gas
Impact
Strings in solidity are handled in 32 byte chunks. A require string longer than 32 bytes uses more gas. Shortening these strings will save gas.
Proof of Concept
Several cases of this gas optimization were found. These are a few examples, but more may exist
Tools Used
Manual analysis
Recommended Mitigation Steps
Shorten all require strings to less than 32 characters
4. Use != 0 instead of > 0
Impact
Using
> 0
uses slightly more gas than using!= 0
. Use!= 0
when comparing uint variables to zero, which cannot hold values below zeroProof of Concept
Locations where this was found include
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/ManagedIndexReweightingLogic.sol#L56
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/ManagedIndexReweightingLogic.sol#L61
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/ManagedIndexReweightingLogic.sol#L98
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/IndexLogic.sol#L76
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/IndexLogic.sol#L86
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/IndexLogic.sol#L98
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/IndexLogic.sol#L114
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/IndexLogic.sol#L141
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/PhutureIndex.sol#L56
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/PhutureIndex.sol#L64
Tools Used
grep
Recommended Mitigation Steps
Replace
> 0
with!= 0
to save gas5. Cache array length before loop
Impact
Caching the array length outside a loop saves reading it on each iteration, as long as the array's length is not changed during the loop. This saves gas.
Proof of Concept
This optimization is already used in some places, but is not used in these places
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/IndexLogic.sol#L39
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/IndexLogic.sol#L60
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/IndexLogic.sol#L125
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/ManagedIndexReweightingLogic.sol#L38
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/TopNMarketCapReweightingLogic.sol#L37
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/TrackedIndexReweightingLogic.sol#L37
https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/TrackedIndexReweightingLogic.sol#L66
Tools Used
Manual analysis
Recommended Mitigation Steps
Cache the array length before the for loop
The text was updated successfully, but these errors were encountered: