15,000 USDC
View results
Submission Details
Severity: gas

Cache storage variables

Summary

Storage variables should be cached as it is cheaper to read from memory

Vulnerability Details

msg.sender can be cached to save gas.

Impact

There are about 20 instances of this.

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

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

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

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

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

Tools Used

Manual review

Recommendations

msg.sender and other storage variables should be cached to save significant amount of gas.

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

address sender = msg.sender;
s_collateralDeposited[sender][tokenCollateralAddress] += amountCollateral;
emit CollateralDeposited(sender, tokenCollateralAddress, amountCollateral);
bool success = IERC20(tokenCollateralAddress).transferFrom(sender, address(this), amountCollateral);

Support

FAQs

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