By liquidating a user, the liquidator is using his own funds that do not impact the liquidator HF directly.
Because the function reverts, the system is preventing a user's to perform an action that should be able to do.
By liquidating a user, the liquidator is using his own funds that do not impact the liquidator HF directly.
Because the function reverts, the system is preventing a user's to perform an action that should be able to do.
pragma solidity 0.8.19;
import {DSCEngine} from "../../src/DSCEngine.sol";
import {DecentralizedStableCoin} from "../../src/DecentralizedStableCoin.sol";
import {HelperConfig} from "../../script/HelperConfig.s.sol";
import {ERC20Mock} from "@openzeppelin/contracts/mocks/ERC20Mock.sol";
import {Test, console} from "forge-std/Test.sol";
import {StdCheats} from "forge-std/StdCheats.sol";
import {MockV3Aggregator} from "../mocks/MockV3Aggregator.sol";
contract CannotLiquidateWhenHFTest is StdCheats, Test {
DSCEngine public dsce;
DecentralizedStableCoin public dsc;
HelperConfig public helperConfig;
address[] public tokenAddresses;
address[] public priceFeedAddresses;
address public ethUsdPriceFeed;
address public btcUsdPriceFeed;
address public weth;
address public wbtc;
uint256 public deployerKey;
uint256 amountCollateral = 10 ether;
uint256 amountToMint = 100 ether;
address public user = address(1);
uint256 public constant STARTING_USER_BALANCE = 10 ether;
uint256 public constant MIN_HEALTH_FACTOR = 1e18;
uint256 public constant LIQUIDATION_THRESHOLD = 50;
function setUp() external {
helperConfig = new HelperConfig();
(ethUsdPriceFeed, btcUsdPriceFeed, weth, wbtc, deployerKey) = helperConfig.activeNetworkConfig();
tokenAddresses = [weth, wbtc];
priceFeedAddresses = [ethUsdPriceFeed, btcUsdPriceFeed];
dsc = new DecentralizedStableCoin();
dsce = new DSCEngine(tokenAddresses, priceFeedAddresses, address(dsc));
dsc.transferOwnership(address(dsce));
if (block.chainid == 31337) {
vm.deal(user, STARTING_USER_BALANCE);
}
ERC20Mock(weth).mint(user, STARTING_USER_BALANCE);
ERC20Mock(wbtc).mint(user, STARTING_USER_BALANCE);
}
function testLiquidateRevertIfLiquidatorHFBelow() public {
vm.startPrank(user);
ERC20Mock(weth).approve(address(dsce), amountCollateral);
dsce.depositCollateralAndMintDsc(weth, amountCollateral, amountToMint);
vm.stopPrank();
address liquidator = makeAddr("liquidator");
ERC20Mock(weth).mint(liquidator, STARTING_USER_BALANCE);
vm.startPrank(liquidator);
ERC20Mock(weth).approve(address(dsce), amountCollateral);
dsce.depositCollateralAndMintDsc(weth, amountCollateral, amountToMint);
vm.stopPrank();
int256 ethUsdUpdatedPrice = 18e8;
MockV3Aggregator(ethUsdPriceFeed).updateAnswer(ethUsdUpdatedPrice);
assertLt(dsce.getHealthFactor(user), 1e18);
assertLt(dsce.getHealthFactor(liquidator), 1e18);
vm.startPrank(liquidator);
dsc.approve(address(dsce), 1 ether);
vm.expectRevert();
dsce.liquidate(weth, user, 1 ether);
vm.stopPrank();
vm.startPrank(liquidator);
ERC20Mock(weth).mint(liquidator, 1000 ether);
ERC20Mock(weth).approve(address(dsce), 1000 ether);
dsce.depositCollateral(weth, 1000 ether);
uint256 liquidatorHFBefore = dsce.getHealthFactor(liquidator);
assertGe(liquidatorHFBefore, 1e18);
dsc.approve(address(dsce), 1 ether);
dsce.liquidate(weth, user, 1 ether);
assertEq(dsce.getHealthFactor(liquidator), liquidatorHFBefore);
vm.stopPrank();
}
}