Cyfrin / 2023-07-foundry-defi-stablecoin

37 stars 32 forks source link

x - y is more efficient than using -= #1093

Open codehawks-bot opened 1 year ago

codehawks-bot commented 1 year ago

x - y is more efficient than using -=

Severity

Gas Optimization / Informational

Summary

In DSCEngine.sol, the _burnDsc() uses addition assignment which costs more gas

Vulnerability Details

The following code is used in _burnDsc()

 function _burnDsc(uint256 amountDscToBurn, address onBehalfOf, address dscFrom) private {
        s_DSCMinted[onBehalfOf] -= amountDscToBurn;
        bool success = i_dsc.transferFrom(dscFrom, address(this), amountDscToBurn);
        // This conditional is hypothtically unreachable
        if (!success) {
            revert DSCEngine__TransferFailed();
        }
        i_dsc.burn(amountDscToBurn);
    }

Impact

Extra runtime gas is used.

Tools Used

Manual review

Recommendations

Consider using the following:

  s_DSCMinted[onBehalfOf] = s_DSCMinted[onBehalfOf] - amountDscToBurn;