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

X = X + Y CAN BE USED INSTEAD OF X += Y

Summary

<x> += <y> Costs More Gas Than <x> = <x> + <y> For State Variables

Vulnerability Details

x = x + y costs less gas than x += y. For example, balanceOf[to] += amount can be changed to balanceOf[to] = balanceOf[to] + amount

Instances:

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

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

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

    s_DSCMinted[msg.sender] += amountDscToMint;

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

    totalCollateralValueInUsd += getUsdValue(token, amount);

Impact

Use of X = X + Y instead of X += Y saves gas.

Tools Used

Manual review and VS Code

Recommendations

Use X = X + Y wherever possible instead of X += Y

Support

FAQs

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