From de168dd6cc41a733023c65d69728d5f9c1ec4981 Mon Sep 17 00:00:00 2001 From: Quentin Garchery Date: Sat, 5 Oct 2024 18:49:25 +0200 Subject: [PATCH] feat: rearrange requires to ensure no division by zero --- src/PreLiquidation.sol | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/PreLiquidation.sol b/src/PreLiquidation.sol index 2de16af..880c9c2 100644 --- a/src/PreLiquidation.sol +++ b/src/PreLiquidation.sol @@ -141,13 +141,13 @@ contract PreLiquidation is IPreLiquidation, IMorphoRepayCallback { uint256 collateralPrice = IOracle(PRE_LIQUIDATION_ORACLE).price(); uint256 collateralQuoted = uint256(position.collateral).mulDivDown(collateralPrice, ORACLE_PRICE_SCALE); uint256 borrowed = uint256(position.borrowShares).toAssetsUp(market.totalBorrowAssets, market.totalBorrowShares); - uint256 ltv = borrowed.wDivUp(collateralQuoted); - - // The following require is equivalent to checking that borrowed > collateralQuoted.wMulDown(PRE_LLTV). - require(ltv > PRE_LLTV, ErrorsLib.NotPreLiquidatablePosition()); - require(ltv <= LLTV, ErrorsLib.LiquidatablePosition()); + // The following require is equivalent to checking that ltv > PRE_LLTV. + require(borrowed > collateralQuoted.wMulDown(PRE_LLTV), ErrorsLib.NotPreLiquidatablePosition()); + require(borrowed <= collateralQuoted.wMulDown(LLTV), ErrorsLib.LiquidatablePosition()); + // The two preceding requires ensures that collateralQuoted is different from zero. + uint256 ltv = borrowed.wDivUp(collateralQuoted); uint256 preLIF = (ltv - PRE_LLTV).wDivDown(LLTV - PRE_LLTV).wMulDown(PRE_LIF_2 - PRE_LIF_1) + PRE_LIF_1; if (seizedAssets > 0) {