Cyfrin / foundry-defi-stablecoin-cu

249 stars 117 forks source link

liquidate Test Error #72

Closed utsav832424 closed 6 months ago

utsav832424 commented 6 months ago

i find the error when i write liquidate Test in defi stablecoin. so give me a solution about this error. here is my error:

[FAIL. Reason: DSCEngine__HealthFactorIsOk()] testliquidated()

My test code :

 function testliquidated() public {
        vm.startPrank(USER);
        ERC20Mock(weth).approve(address(dsce), AMOUNT_COLLATERAL);
        dsce.depositCollateralAndMintDsc(weth, AMOUNT_COLLATERAL, amountTomint);
        vm.stopPrank();

        int256 ethUsdUpdatePrice = 18e8;

        MockV3Aggregator(ethUsdPriceFeed).updateAnswer(ethUsdUpdatePrice);
        uint256 userhealthfactor = dsce.getCalculateHealthFactor(USER);
        console.log("userhealthfactor ", userhealthfactor);
        ERC20Mock(weth).mint(liquidator, collateralTocover);

        vm.startPrank(liquidator);
        ERC20Mock(weth).approve(address(dsce), collateralTocover);
        dsce.depositCollateralAndMintDsc(weth, collateralTocover, amountTomint);
        dsc.approve(address(dsce), amountTomint);
        dsce.liquidate(weth, USER, amountTomint);
        vm.stopPrank();
    }

here my liquidate function:

function liquidate(address collateral, address user, uint256 debtToCover)
        external
        moreThanZero(debtToCover)
        nonReentrant
    {
        uint256 startingUserHealthFactor = _healthFactor(user);
        if (startingUserHealthFactor >= MIN_HEALTH_FACTOR) {
            revert DSCEngine__HealthFactorIsOk();
        }

        uint256 tokenAmountFromDebtCovered = getTokenAmountFromUsd(collateral, debtToCover);

        uint256 bounsCollateral = (tokenAmountFromDebtCovered * LIQUIDATION_BOUNS) / LIQUIDATION_PRECISION;
        uint256 totalCollateralToRedeem = tokenAmountFromDebtCovered + bounsCollateral;
        _redeemCollateral(user, msg.sender, collateral, totalCollateralToRedeem);
        // We need to burn dsc
        _burnDsc(debtToCover, user, msg.sender);

        uint256 endingUserHealthfactor = _healthFactor(user);
        if (endingUserHealthfactor <= MIN_HEALTH_FACTOR) {
            revert DSCEngine__HealthFactorNotImproved();
        }
        _revertIfHealthFactorIsBroken(msg.sender);
    }

healthFactor Function:

 function _healthFactor(address user) private view returns (uint256) {
        (uint256 totalDscMinted, uint256 collateralValueInUsd) = _getAccountInformation(user);
        if (totalDscMinted == 0) return type(uint256).max;
        uint256 collateralAdjustForThreshold = (collateralValueInUsd * LIQUIDATION_THRESHOLD) / LIQUIDATION_PRECISION;
        return (collateralAdjustForThreshold * PRECISION) / totalDscMinted;
    }

getAccountInformation Function:

function _getAccountInformation(address user)
        private
        view
        returns (uint256 totalDscMinted, uint256 collateralValueInUsd)
    {
        totalDscMinted = s_DSCMinted[user];
        collateralValueInUsd = getTotalCollateralValue(user);
    }

getTotalCollateralValue Function:

function getTotalCollateralValue(address user) public view returns (uint256 totalValueInUsd) {

        for (uint256 i = 0; i < s_collateralTokens.length; i++) {
            address token = s_collateralTokens[i];
            uint256 amount = s_collateralDeposited[user][token];
            totalValueInUsd += getValueInUsd(token, amount);
        }
        return totalValueInUsd;
    }

getValueInUsd Function:

function getValueInUsd(address token, uint256 amount) public view returns (uint256) {
        AggregatorV3Interface priceFeed = AggregatorV3Interface(s_priceFeeds[token]);
        (, int256 price,,,) = priceFeed.latestRoundData();

        return ((uint256(price) * ADDITIONAL_FEED_PRECISION) * amount) / PRECISION;
    }

Give me a solution about error.