Duplicate require() Checks Should be a Modifier or a Function
Use Bit Shifting Instead of Multiplication/Division of 2
Using Elements Smaller than 32 bytes (256 bits) Might Use More Gas
++i Costs Less Gas than i++
Use Custom Errors to Save Gas
Defined Variables Used Only Once
Issue
Certain variables is defined even though they are used only once.
Remove these unnecessary variables to save gas.
For cases where it will reduce the readability, one can use comments to help describe what the code is doing.
Use bit shifting instead of multiplication/division.
Example:
uint256 center = upper - (upper - lower) / 2;
Change to:
uint256 center = upper - (upper - lower) >> 2;
Using Elements Smaller than 32 bytes (256 bits) Might Use More Gas
Issue
Since EVM operates on 32 bytes at a time, if the element is smaller than that, the EVM must use more operations
in order to reduce the elements from 32 bytes to specified size. Therefore it is more gas saving to use 32 bytes
unless it is used in a struct or state variable in order to reduce storage slot.
Table of Contents
Total of 6 issues found.
Defined Variables Used Only Once
Issue
Certain variables is defined even though they are used only once. Remove these unnecessary variables to save gas. For cases where it will reduce the readability, one can use comments to help describe what the code is doing.
PoC
Total of 4 instances found.
Remove 'startPrice' variable of
legendaryGobblerPrice()
ofArtGobblers.sol
https://github.com/code-423n4/2022-09-artgobblers/blob/d2087c5a8a6a4f1b9784520e7fe75afa3a9cbdbe/src/ArtGobblers.sol#L480 https://github.com/code-423n4/2022-09-artgobblers/blob/d2087c5a8a6a4f1b9784520e7fe75afa3a9cbdbe/src/ArtGobblers.sol#L499 Mitigation: Delete line 480 and replace line 499 with code belowRemove 'numSold' variable of
legendaryGobblerPrice()
ofArtGobblers.sol
https://github.com/code-423n4/2022-09-artgobblers/blob/d2087c5a8a6a4f1b9784520e7fe75afa3a9cbdbe/src/ArtGobblers.sol#L481 https://github.com/code-423n4/2022-09-artgobblers/blob/d2087c5a8a6a4f1b9784520e7fe75afa3a9cbdbe/src/ArtGobblers.sol#L487 Mitigation: Delete line 481 and replace line 487 with code belowRemove 'remainingIds' and 'distance' variable of
revealGobblers()
ofArtGobblers.sol
https://github.com/code-423n4/2022-09-artgobblers/blob/d2087c5a8a6a4f1b9784520e7fe75afa3a9cbdbe/src/ArtGobblers.sol#L597-L608 Mitigation: Delete line 599 and 602 and replace line 608 with code belowMitigation
Don't define variable that is used only once. Details are listed on above PoC.
Duplicate require() Checks Should be a Modifier or a Function
Issue
Since below require checks are used more than once, I recommend making these to a modifier or a function.
PoC
Total of 1 instance found.
Mitigation
I recommend making duplicate require statement into modifier or a function.
Use Bit Shifting Instead of Multiplication/Division of 2
Issue
The MUL and DIV opcodes cost 5 gas but SHL and SHR only costs 3 gas. Since MUL/DIV and SHL/SHR result the same, use cheaper bit shifting.
PoC
Total of 5 instances found.
Mitigation
Use bit shifting instead of multiplication/division. Example:
Using Elements Smaller than 32 bytes (256 bits) Might Use More Gas
Issue
Since EVM operates on 32 bytes at a time, if the element is smaller than that, the EVM must use more operations in order to reduce the elements from 32 bytes to specified size. Therefore it is more gas saving to use 32 bytes unless it is used in a struct or state variable in order to reduce storage slot.
Reference: https://docs.soliditylang.org/en/v0.8.15/internals/layout_in_storage.html
PoC
Total of 3 instances found.
Mitigation
I suggeot using uint256 instead of anything smaller and downcast where needed.
++i Costs Less Gas than i++
Issue
Prefix increments/decrements (++i or --i) costs cheaper gas than postfix increment/decrements (i++ or i--).
PoC
Total of 9 instances found.
Mitigation
Change it to postfix increments/decrements. It saves 6 gas per instances (for for-loop it saves 6 gas per loop).
Use Custom Errors to Save Gas
Issue
Custom errors from Solidity 0.8.4 are cheaper than revert strings. Reference: https://blog.soliditylang.org/2021/04/21/custom-errors/
PoC
Total of 36 instances found.
Mitigation
I suggest implementing custom errors to save gas.