Skip to content

Commit

Permalink
feat: rearrange requires to ensure no division by zero
Browse files Browse the repository at this point in the history
  • Loading branch information
QGarchery committed Oct 5, 2024
1 parent bdcffe9 commit de168dd
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/PreLiquidation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit de168dd

Please sign in to comment.