gas

Combine Multiple Mapping Address

Reward

Total

15.42 USDC

2.41 USDC
2.41 USDC
Selected
3.37 USDC
2.41 USDC
2.41 USDC
2.41 USDC
Selected Submission

Combine Multiple Mapping Address

Severity

Gas Optimization / Informational

Relevant GitHub Links

https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/d1c5501aa79320ca0aeaa73f47f0dbc88c7b77e2/src/DSCEngine.sol#L78

https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/d1c5501aa79320ca0aeaa73f47f0dbc88c7b77e2/src/DSCEngine.sol#L79

Summary

Combine multiple address mappings

Vulnerability Details

DSCEngine.sol line 78 and 79 mapping(address user => mapping(address token => uint256 amount)) private s_collateralDeposited; mapping(address user => uint256 amountDscMinted) private s_DSCMinted; Above can be combined into single mapping

Impact

Gas: Instead of having 2 mappings we can combine into a single mapping of struct for the user addresses data We will make use on a single mapping instead of 2 saving the computation, hashing of mapping storage slots computations and saving storage costs. Also since we have 2 tokens we can also remove the inner mapping ==> mapping(address token => uint256 amount to add seperate amounts deposited into user struct

Tools Used

Manual Analysis

Recommendations

struct User { address user; uint256 amountWBTCDeposited; uint256 amountWETHDeposited; uint256 amountDSCMinted; } mapping(address user => User) userInfo;