Skip to content

Commit

Permalink
fix: pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Debugger022 committed Sep 15, 2023
1 parent e062119 commit cbd9b18
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 93 deletions.
15 changes: 1 addition & 14 deletions contracts/Comptroller.sol
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ contract Comptroller is
uint256 borrowBalance = VToken(vTokenBorrowed).borrowBalanceStored(borrower);

/* Allow accounts to be liquidated if the market is deprecated or it is a forced liquidation */
if (skipLiquidityCheck || isDeprecated(VToken(vTokenBorrowed)) || isForcedLiquidationEnabled[vTokenBorrowed]) {
if (skipLiquidityCheck || isForcedLiquidationEnabled[vTokenBorrowed]) {
if (repayAmount > borrowBalance) {
revert TooMuchRepay();
}
Expand Down Expand Up @@ -1244,19 +1244,6 @@ contract Comptroller is
return _actionPaused[market][action];
}

/**
* @notice Check if a vToken market has been deprecated
* @dev All borrows in a deprecated vToken market can be immediately liquidated
* @param vToken The market to check if deprecated
* @return deprecated True if the given vToken market has been deprecated
*/
function isDeprecated(VToken vToken) public view returns (bool) {
return
markets[address(vToken)].collateralFactorMantissa == 0 &&
actionPaused(address(vToken), Action.BORROW) &&
vToken.reserveFactorMantissa() == MANTISSA_ONE;
}

/**
* @notice Add the market to the borrower's "assets in" for liquidity calculations
* @param vToken The market to enter
Expand Down
6 changes: 3 additions & 3 deletions contracts/ComptrollerStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ contract ComptrollerStorage {
// Used to check if rewards distributor is added
mapping(address => bool) internal rewardsDistributorExists;

/// @notice Flag indicating whether forced liquidation enabled for a market
mapping(address => bool) public isForcedLiquidationEnabled;

uint256 internal constant NO_ERROR = 0;

// closeFactorMantissa must be strictly greater than this value
Expand All @@ -118,9 +121,6 @@ contract ComptrollerStorage {
// No collateralFactorMantissa may exceed this value
uint256 internal constant MAX_COLLATERAL_FACTOR_MANTISSA = 0.9e18; // 0.9

/// @notice Flag indicating whether forced liquidation enabled for a market
mapping(address => bool) public isForcedLiquidationEnabled;

/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
Expand Down
132 changes: 56 additions & 76 deletions tests/hardhat/Comptroller/liquidateAccountTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,90 +382,70 @@ describe("liquidateAccount", () => {
describe("preLiquidateHook", async () => {
let accounts: SignerWithAddress[];

before(async () => {
beforeEach(async () => {
accounts = await ethers.getSigners();
await comptroller.setForcedLiquidation(OMG.address, true);
});
const generalTests = () => {
it("reverts if borrowed market is not listed", async () => {
const someVToken = await smock.fake<VToken>("VToken");
await expect(
comptroller.preLiquidateHook(
someVToken.address,
OMG.address,
accounts[0].address,
parseUnits("1", 18),
false,
),
).to.be.revertedWithCustomError(comptroller, "MarketNotListed");
});

it("reverts if collateral market is not listed", async () => {
const someVToken = await smock.fake<VToken>("VToken");
await expect(
comptroller.preLiquidateHook(
OMG.address,
someVToken.address,
accounts[0].address,
parseUnits("1", 18),
false,
),
).to.be.revertedWithCustomError(comptroller, "MarketNotListed");
});
};
it("reverts if borrowed market is not listed", async () => {
const someVToken = await smock.fake<VToken>("VToken");
await expect(
comptroller.preLiquidateHook(someVToken.address, OMG.address, accounts[0].address, parseUnits("1", 18), false),
).to.be.revertedWithCustomError(comptroller, "MarketNotListed");
});

describe("isForcedLiquidationEnabled == true", async () => {
beforeEach(async () => {
await comptroller.setForcedLiquidation(OMG.address, true);
});
it("reverts if collateral market is not listed", async () => {
const someVToken = await smock.fake<VToken>("VToken");
await expect(
comptroller.preLiquidateHook(OMG.address, someVToken.address, accounts[0].address, parseUnits("1", 18), false),
).to.be.revertedWithCustomError(comptroller, "MarketNotListed");
});

generalTests();

it("allows liquidations without shortfall", async () => {
OMG.borrowBalanceStored.returns(parseUnits("100", 18));
await comptroller.callStatic.preLiquidateHook(
OMG.address,
OMG.address,
accounts[0].address,
parseUnits("1", 18),
true,
);
});
it("allows liquidations without shortfall", async () => {
OMG.borrowBalanceStored.returns(parseUnits("100", 18));
await comptroller.callStatic.preLiquidateHook(
OMG.address,
OMG.address,
accounts[0].address,
parseUnits("1", 18),
true,
);
});

it("allows to repay 100% of the borrow", async () => {
OMG.borrowBalanceStored.returns(parseUnits("1", 18));
await comptroller.callStatic.preLiquidateHook(
OMG.address,
OMG.address,
accounts[0].address,
parseUnits("1", 18),
false,
);
});
it("allows to repay 100% of the borrow", async () => {
OMG.borrowBalanceStored.returns(parseUnits("1", 18));
await comptroller.callStatic.preLiquidateHook(
OMG.address,
OMG.address,
accounts[0].address,
parseUnits("1", 18),
false,
);
});

it("fails with TOO_MUCH_REPAY if trying to repay > borrowed amount", async () => {
OMG.borrowBalanceStored.returns(parseUnits("0.99", 18));
const tx = comptroller.callStatic.preLiquidateHook(
OMG.address,
OMG.address,
accounts[0].address,
parseUnits("1", 18),
false,
);
await expect(tx).to.be.revertedWithCustomError(comptroller, "TooMuchRepay");
});
it("fails with TOO_MUCH_REPAY if trying to repay > borrowed amount", async () => {
OMG.borrowBalanceStored.returns(parseUnits("0.99", 18));
const tx = comptroller.callStatic.preLiquidateHook(
OMG.address,
OMG.address,
accounts[0].address,
parseUnits("1", 18),
false,
);
await expect(tx).to.be.revertedWithCustomError(comptroller, "TooMuchRepay");
});

it("checks the shortfall if isForcedLiquidationEnabled is set back to false", async () => {
await comptroller.setForcedLiquidation(OMG.address, false);
OMG.borrowBalanceStored.returns(parseUnits("100", 18));
const tx = comptroller.callStatic.preLiquidateHook(
OMG.address,
OMG.address,
accounts[0].address,
parseUnits("1", 18),
false,
);
await expect(tx).to.be.revertedWithCustomError(comptroller, "MinimalCollateralViolated");
});
it("checks the shortfall if isForcedLiquidationEnabled is set back to false", async () => {
await comptroller.setForcedLiquidation(OMG.address, false);
OMG.borrowBalanceStored.returns(parseUnits("100", 18));
const tx = comptroller.callStatic.preLiquidateHook(
OMG.address,
OMG.address,
accounts[0].address,
parseUnits("1", 18),
false,
);
await expect(tx).to.be.revertedWithCustomError(comptroller, "MinimalCollateralViolated");
});
});
});

0 comments on commit cbd9b18

Please sign in to comment.