code-423n4 / 2022-09-artgobblers-findings

0 stars 0 forks source link

Gas Optimizations #470

Closed code423n4 closed 1 year ago

code423n4 commented 2 years ago

AN ARRAY’S LENGTH SHOULD BE CACHED TO SAVE GAS IN FOR-LOOPS

File:   main/src/utils/GobblerReserve.sol   #1
37            for (uint256 i = 0; i < ids.length; i++) {

State Variables declared only set in the constructor should be declared immutable

File:   main/src/utils/token/GobblersERC721.sol   #1
23    string public name;

https://github.com/code-423n4/2022-09-artgobblers/blob/main/src/utils/token/GobblersERC721.sol#L25

++i costs less gas than i++, especially when it’s used in for-loops (--i/i-- too)

Saves 6 gas PER LOOP

File:   main/src/Pages.sol   #1
251            for (uint256 i = 0; i < numPages; i++) _mint(community, ++lastMintedPageId);

Using private rather than public for constants, saves gas

If needed, the value can be read from the verified contract source code

File:   main/src/ArtGobblers.sol   #1
112    uint256 public constant MAX_SUPPLY = 10000;

It costs more gas to initialize variables to zero than to let the default of zero be applied

File:   main/src/ArtGobblers.sol   #1
432            for (uint256 i = 0; i < cost; ++i) {

Use custom errors rather than revert()/require() strings to save deployment gas

Custom errors are available from solidity version 0.8.4. The instances below match or exceed that version

 File:   main/src/ArtGobblers.sol   #1
 696            if (gobblerId == 0) revert("NOT_MINTED"); // 0 is not a valid id for Art Gobblers.

Use Solidity version of atleast 0.8.15

GalloDaSballo commented 2 years ago

100 gas at most