Skip to content

Commit

Permalink
feat: implement uncollateralized position error
Browse files Browse the repository at this point in the history
  • Loading branch information
peyha committed Oct 4, 2024
1 parent c3cd94f commit a0fe467
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/PreLiquidation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ contract PreLiquidation is IPreLiquidation, IMorphoRepayCallback {

uint256 collateralPrice = IOracle(PRE_LIQUIDATION_ORACLE).price();
uint256 collateralQuoted = uint256(position.collateral).mulDivDown(collateralPrice, ORACLE_PRICE_SCALE);

require(collateralQuoted > 0, ErrorsLib.UncollateralizedPosition());

uint256 borrowed = uint256(position.borrowShares).toAssetsUp(market.totalBorrowAssets, market.totalBorrowShares);
uint256 ltv = borrowed.wDivUp(collateralQuoted);

Expand Down
2 changes: 2 additions & 0 deletions src/libraries/ErrorsLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ library ErrorsLib {

error NotPreLiquidatablePosition();

error UncollateralizedPosition();

error PreLiquidationTooLarge(uint256 repaidShares, uint256 repayableShares);

error NotMorpho();
Expand Down
27 changes: 27 additions & 0 deletions test/PreLiquidationErrorTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,31 @@ contract PreLiquidationErrorTest is BaseTest {
);
preLiquidation.preLiquidate(BORROWER, seizedAssets, 0, hex"");
}

function testPreLiquidationUndercollateralizedPosition(PreLiquidationParams memory preLiquidationParams, uint256 collateralAmount, uint256 oraclePrice, uint256 repaidShares) public virtual {
preLiquidationParams = boundPreLiquidationParameters({
preLiquidationParams: preLiquidationParams,
minPreLltv: WAD / 100,
maxPreLltv: marketParams.lltv - 1,
minPreLCF: WAD / 100,
maxPreLCF: WAD,
minPreLIF: WAD,
maxPreLIF: WAD.wDivDown(lltv),
preLiqOracle: marketParams.oracle
});

oraclePrice = bound(oraclePrice, 1, ORACLE_PRICE_SCALE / 2);
OracleMock(preLiquidationParams.preLiquidationOracle).setPrice(oraclePrice);

uint256 collateralPrice = IOracle(preLiquidationParams.preLiquidationOracle).price();

collateralAmount = bound(collateralAmount, 1, ORACLE_PRICE_SCALE / collateralPrice - 1);

_preparePreLiquidation(preLiquidationParams, collateralAmount, 0, LIQUIDATOR);

repaidShares = bound(repaidShares, 1, type(uint128).max);

vm.expectRevert(ErrorsLib.UncollateralizedPosition.selector);
preLiquidation.preLiquidate(BORROWER, 0, repaidShares, hex"");
}
}

0 comments on commit a0fe467

Please sign in to comment.