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

x + y is more efficient that +=

[G-03] x + y is more efficient than using += for state variables (likewise for -=)

Number Of Instances: 5

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

s_collateralDeposited[msg.sender][tokenCollateralAddress] += amountCollateral;

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

s_DSCMinted[msg.sender] += amountDscToMint;

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

totalCollateralValueInUsd += getUsdValue(token, amount);

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

s_DSCMinted[onBehalfOf] -= amountDscToBurn;

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

s_collateralDeposited[from][tokenCollateralAddress] -= amountCollateral;

Recommendation

In instances found where either += or -= are used against state variables use x = x + y/ x = x - y instead

Support

FAQs

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