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

Remove `endingUserHealthFactor` check in `liquidate` function to save gas

Summary

The endingUserHealthFactor <= startingUserHealthFactor check in the liquidate function is not required, by removing it you can save a lot of gas every time a user calls the liquidate function.

Vulnerability Details

Since we apply the moreThanZero(debtToCover) on the liquidate function, the debtToCover can't be 0 which means that the state of endingUserHealthFactor <= startingUserHealthFactor can never be achieved.

Adding this extra check at the end of the liquidate function add additional unnecessary computation (we calculate the liquidated user health factor again and add an if statement).

Since the state can never be achieved this recalculation and comparison can be removed to significantly reduce the gas cost of the liquidate function.

Impact

Save a lot of gas every time users call the liquidate function.

Tools Used

VSCode

Recommendations

Remove the following check from the liquidate function all at once:

uint256 endingUserHealthFactor = _healthFactor(user);
if (endingUserHealthFactor <= startingUserHealthFactor) {
revert DSCEngine__HealthFactorNotImproved();
}
"""

Support

FAQs

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