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

LIF and LTV restrictions #77

Merged
merged 10 commits into from
Oct 7, 2024
7 changes: 4 additions & 3 deletions src/PreLiquidation.sol
QGarchery marked this conversation as resolved.
Show resolved Hide resolved
QGarchery marked this conversation as resolved.
Show resolved Hide resolved
QGarchery marked this conversation as resolved.
Show resolved Hide resolved
MathisGD marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ contract PreLiquidation is IPreLiquidation, IMorphoRepayCallback {
/// @dev The following requirements should be met:
/// - preLltv < LLTV;
/// - preLCF1 <= preLCF2;
/// - WAD <= preLIF1 <= preLIF2.
/// - WAD <= preLIF1 <= preLIF2 <= 1 / LLTV.
QGarchery marked this conversation as resolved.
Show resolved Hide resolved
constructor(address morpho, Id id, PreLiquidationParams memory _preLiquidationParams) {
require(IMorpho(morpho).market(id).lastUpdate != 0, ErrorsLib.NonexistentMarket());
MarketParams memory _marketParams = IMorpho(morpho).idToMarketParams(id);
require(_preLiquidationParams.preLltv < _marketParams.lltv, ErrorsLib.PreLltvTooHigh());
require(_preLiquidationParams.preLCF1 <= _preLiquidationParams.preLCF2, ErrorsLib.PreLCFDecreasing());
require(WAD <= _preLiquidationParams.preLIF1, ErrorsLib.PreLIFTooLow());
require(_preLiquidationParams.preLIF1 <= _preLiquidationParams.preLIF2, ErrorsLib.PreLIFDecreasing());
require(_preLiquidationParams.preLIF2 <= WAD.wDivDown(_marketParams.lltv), ErrorsLib.PreLIFTooHigh());

MORPHO = IMorpho(morpho);

Expand Down Expand Up @@ -160,8 +161,8 @@ contract PreLiquidation is IPreLiquidation, IMorphoRepayCallback {
).mulDivDown(ORACLE_PRICE_SCALE, collateralPrice);
}

// Note that the pre-liquidation close factor can be greater than WAD (100%). In this case the position can be
// fully pre-liquidated.
// Note that the pre-liquidation close factor can be greater than WAD (100%).
// In this case the position can be fully pre-liquidated.
uint256 preLCF = UtilsLib.min(
(ltv - PRE_LLTV).wDivDown(LLTV - PRE_LLTV).wMulDown(PRE_LCF_2 - PRE_LCF_1) + PRE_LCF_1, PRE_LCF_2
);
Expand Down
2 changes: 2 additions & 0 deletions src/libraries/ErrorsLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ library ErrorsLib {

error PreLIFDecreasing();

error PreLIFTooHigh();

error InconsistentInput();

error NotPreLiquidatablePosition();
Expand Down
20 changes: 19 additions & 1 deletion test/PreLiquidationErrorTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,25 @@ contract PreLiquidationErrorTest is BaseTest {
factory.createPreLiquidation(id, preLiquidationParams);
}

function testpreLIFDecreasing(PreLiquidationParams memory preLiquidationParams) public virtual {
function testHighPreLIF(PreLiquidationParams memory preLiquidationParams) public virtual {
preLiquidationParams = boundPreLiquidationParameters({
preLiquidationParams: preLiquidationParams,
minPreLltv: WAD / 100,
maxPreLltv: marketParams.lltv - 1,
minPreLCF: WAD / 100,
maxPreLCF: WAD,
minPreLIF: WAD,
maxPreLIF: type(uint256).max,
preLiqOracle: marketParams.oracle
});
preLiquidationParams.preLIF2 =
bound(preLiquidationParams.preLIF2, WAD.wDivDown(marketParams.lltv) + 1, type(uint256).max);

vm.expectRevert(abi.encodeWithSelector(ErrorsLib.PreLIFTooHigh.selector));
factory.createPreLiquidation(id, preLiquidationParams);
}

function testPreLIFDecreasing(PreLiquidationParams memory preLiquidationParams) public virtual {
preLiquidationParams = boundPreLiquidationParameters({
preLiquidationParams: preLiquidationParams,
minPreLltv: WAD / 100,
Expand Down