20,000 USDC
View results
Submission Details
Severity: gas
Valid

Multiple Address Mappings Can Be Combined Into A Single Mapping Of An Address To A Struct, Where Appropriate

Vulnerability Details

Saves a storage slot for the mapping. Depending on the circumstances and sizes of types, can avoid a Gsset (20000 gas) per mapping combined. Reads and subsequent writes can also be cheaper when a function requires both values and they both fit in the same storage slot.
Finally, if both fields are accessed in the same function, can save ~42 gas per access due to not having to recalculate the key’s keccak256 hash (Gkeccak256 - 30 gas) and that calculation’s associated stack operations.

Proof of Concept

File: src/Staking.sol
18 /// @notice mapping of user indexes
19 mapping(address => uint256) public supplyIndex;
20
21 /// @notice mapping of user balances
22 mapping(address => uint256) public balances;
23 /// @notice mapping of user claimable rewards
24: mapping(address => uint256) public claimable;

Staking.sol#L18-L24

Tools Used

VSCode

Recommendations

Consider editing to the following struct:

- mapping(address => uint256) public supplyIndex;
- mapping(address => uint256) public balances;
- mapping(address => uint256) public claimable;
+ struct addressBalancesStruct {
+ uint256 supplyIndex;
+ uint256 balances;
+ uint256 claimable;
+ }

Support

FAQs

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