40,000 USDC
View results
Submission Details
Severity: gas

State variables can be packed together

Each slot saved can avoid an extra Gsset (20000 gas) for the first setting. Subsequent reads as well as writes have smaller gas savings.

Instance (1):

File: Escrow.sol
uint256 private immutable i_price;
/// @dev There is a risk that if a malicious token is used, the dispute process could be manipulated.
/// Therefore, careful consideration should be taken when chosing the token.
IERC20 private immutable i_tokenContract; // @audit 20 Bytes
address private immutable i_buyer; // @audit 20 Bytes
address private immutable i_seller; // @audit 20 Bytes
address private immutable i_arbiter; // @audit 20 Bytes
uint256 private immutable i_arbiterFee;
State private s_state; // @audit 4 Bytes

Link to code

Let's understand one by one all:

  1. address and IERC20 uses 20 bytes of data.

  2. uint256 uses 1 complete slot of 32 bytes.

  3. State enum consists of 4 members with each member using uint8 meaning 1 byte of data. So total size of s_state is 4 bytes.

Recommendation:

  1. As you can clearly see, State can easily be packed with any address or IERC20 by moving up as they have 12 bytes left.

  2. Also, i_price in all practical usecase won't be big enough to keep it uint256. Option to keep it uint96 which gives good enough range of more than 79 Billion Tokens in case of 18 decimal tokens. If both i_price and i_arbiterFee can be make uint96 then both of them can be packed with couple of addresses.

Because of these optimizations, Contract can save on 3 complete storage slots and save 60000 gas.

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.