Cyfrin / 2023-07-foundry-defi-stablecoin

37 stars 32 forks source link

collateral and debt to cover not validated #1108

Open codehawks-bot opened 1 year ago

codehawks-bot commented 1 year ago

collateral and debt to cover not validated

Severity

Gas Optimization / Informational

Summary

In DSCEngine.sol, collateral and debtToCover are not checked if valid in liquidate() function.

Vulnerability Details

The liquidate function does not revert early if collateral and debtToCover are not valid

Impact

Gas can be wasted during runtime

Tools Used

Manual review

Recommendations

Use the following

 function liquidate(address collateral, address user, uint256 debtToCover)
        external
        moreThanZero(debtToCover)
        nonReentrant
    {
        // need to check health factor of the user
        uint256 startingUserHealthFactor = _healthFactor(user);
        if (startingUserHealthFactor >= MIN_HEALTH_FACTOR) {
            revert DSCEngine__HealthFactorOk();
        }
        if(debtToCover > getCollateralBalanceOfUser(user,collateral)){
            revert DSCEngine__ExcessDebtToCover();
        }

        //......

    }