gas

# `_burnDsc` function on `DSCEngine` can be simplified

Reward

Total

24.49 USDC

Selected
24.49 USDC
Selected Submission

# _burnDsc function on DSCEngine can be simplified

Severity

Gas Optimization

Relevant GitHub Links

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

Summary

_burnDsc function on DSCEngine can be simplified to avoid extra calls.

Vulnerability Details

Instead of doing a transfer and a burn, you can just use burnFrom.

Impact

Gas impact Overall gas change: -336291 (-2.229%)

testRevertsIfTokenLengthDoesntMatchPriceFeeds() (gas: -40 (-0.022%)) 
testRevertsIfMintFails() (gas: -36694 (-1.805%)) 
testRevertsIfTransferFromFails() (gas: -36694 (-1.876%)) 
testRevertsIfTransferFails() (gas: -36694 (-1.901%)) 
testMustImproveHealthFactorOnLiquidation() (gas: -81010 (-3.427%)) 
testUserStillHasSomeEthAfterLiquidation() (gas: -20737 (-4.160%)) 
testLiquidationPayoutIsCorrect() (gas: -20737 (-4.281%)) 
testLiquidatorTakesOnUsersDebt() (gas: -20737 (-4.289%)) 
testUserHasNoMoreDebt() (gas: -20737 (-4.290%)) 
testCanRedeemDepositedCollateral() (gas: -20737 (-8.240%)) 
testCanBurnDsc() (gas: -20737 (-8.739%)) 
testMustRedeemMoreThanZero() (gas: -20737 (-8.751%)) 
Overall gas change: -336291 (-2.229%)

Tools Used

Manual revision

Recommendations

diff --git a/src/DSCEngine.sol b/src/DSCEngine.sol
index a7a6639..1906a92 100644
--- a/src/DSCEngine.sol
+++ b/src/DSCEngine.sol
@@ -271,12 +271,7 @@ contract DSCEngine is ReentrancyGuard {
      */
     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);
+        i_dsc.burnFrom(dscFrom, amountDscToBurn);
     }
 
     function _redeemCollateral(address from, address to, address tokenCollateralAddress, uint256 amountCollateral)