Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VEN-1930]: forced liquidation #305

Merged
merged 6 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion contracts/Comptroller.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ contract Comptroller is
/// @notice Emitted when a market is supported
event MarketSupported(VToken vToken);

/// @notice Emitted when forced liquidation is enabled or disabled for a market
event IsForcedLiquidationEnabledUpdated(address indexed vToken, bool enable);

/// @notice Thrown when collateral factor exceeds the upper bound
error InvalidCollateralFactor();

Expand Down Expand Up @@ -475,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))) {
if (skipLiquidityCheck || isDeprecated(VToken(vTokenBorrowed)) || isForcedLiquidationEnabled[vTokenBorrowed]) {
chechu marked this conversation as resolved.
Show resolved Hide resolved
if (repayAmount > borrowBalance) {
revert TooMuchRepay();
}
Expand Down Expand Up @@ -1004,6 +1007,24 @@ contract Comptroller is
_setMaxLoopsLimit(limit);
}

/**
* @notice Enables forced liquidations for a market. If forced liquidation is enabled,
* borrows in the market may be liquidated regardless of the account liquidity
* @param vTokenBorrowed Borrowed vToken
* @param enable Whether to enable forced liquidations
*/
function setForcedLiquidation(address vTokenBorrowed, bool enable) external {
_checkAccessAllowed("setForcedLiquidation(address,bool)");
ensureNonzeroAddress(vTokenBorrowed);

if (!markets[vTokenBorrowed].isListed) {
revert MarketNotListed(vTokenBorrowed);
}

isForcedLiquidationEnabled[address(vTokenBorrowed)] = enable;
emit IsForcedLiquidationEnabledUpdated(vTokenBorrowed, enable);
}

/**
* @notice Determine the current account liquidity with respect to liquidation threshold requirements
* @dev The interface of this function is intentionally kept compatible with Compound and Venus Core
Expand Down
5 changes: 4 additions & 1 deletion contracts/ComptrollerStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,13 @@ 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;

chechu marked this conversation as resolved.
Show resolved Hide resolved
/**
* @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.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[50] private __gap;
uint256[49] private __gap;
}
128 changes: 127 additions & 1 deletion tests/hardhat/Comptroller/liquidateAccountTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe("liquidateAccount", () => {
let OMG: FakeContract<VToken>;
let ZRX: FakeContract<VToken>;
let BAT: FakeContract<VToken>;
let accessControl: FakeContract<AccessControlManager>;
const maxLoopsLimit = 150;

type LiquidateAccountFixture = {
Expand Down Expand Up @@ -113,7 +114,7 @@ describe("liquidateAccount", () => {
[, liquidator, user] = await ethers.getSigners();
const contracts = await loadFixture(liquidateAccountFixture);
configure(contracts);
({ comptroller, OMG, ZRX, BAT } = contracts);
({ comptroller, OMG, ZRX, BAT, accessControl } = contracts);
});

describe("collateral to borrows ratio requirements", async () => {
Expand Down Expand Up @@ -342,4 +343,129 @@ describe("liquidateAccount", () => {
);
});
});

describe("setForcedLiquidation", async () => {
it("fails if asset is not listed", async () => {
const someVToken = await smock.fake<VToken>("VToken");
await expect(comptroller.setForcedLiquidation(someVToken.address, true)).to.be.revertedWithCustomError(
comptroller,
"MarketNotListed",
);
});

it("fails if ACM does not allow the call", async () => {
accessControl.isAllowedToCall.returns(false);
await expect(comptroller.setForcedLiquidation(OMG.address, true)).to.be.revertedWithCustomError(
comptroller,
"Unauthorized",
);
accessControl.isAllowedToCall.returns(true);
});

it("sets forced liquidation", async () => {
await comptroller.setForcedLiquidation(OMG.address, true);
expect(await comptroller.isForcedLiquidationEnabled(OMG.address)).to.be.true;

await comptroller.setForcedLiquidation(OMG.address, false);
expect(await comptroller.isForcedLiquidationEnabled(OMG.address)).to.be.false;
});

it("emits IsForcedLiquidationEnabledUpdated event", async () => {
const tx1 = await comptroller.setForcedLiquidation(OMG.address, true);
await expect(tx1).to.emit(comptroller, "IsForcedLiquidationEnabledUpdated").withArgs(OMG.address, true);

const tx2 = await comptroller.setForcedLiquidation(OMG.address, false);
await expect(tx2).to.emit(comptroller, "IsForcedLiquidationEnabledUpdated").withArgs(OMG.address, false);
});
});

describe("preLiquidateHook", async () => {
let accounts: SignerWithAddress[];

before(async () => {
accounts = await ethers.getSigners();
});
const generalTests = () => {
chechu marked this conversation as resolved.
Show resolved Hide resolved
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");
});
};

describe("isForcedLiquidationEnabled == true", async () => {
beforeEach(async () => {
await comptroller.setForcedLiquidation(OMG.address, true);
});

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 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("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");
});
});
});
});
Loading