From de861cd72fd62222e0e56c6fde3a2fe46e47611f Mon Sep 17 00:00:00 2001 From: MathisGD Date: Thu, 10 Aug 2023 23:34:46 +0200 Subject: [PATCH] style: better naming and internalize computation --- src/Blue.sol | 20 +++++++++++++++----- test/forge/Blue.t.sol | 12 ++++++++---- test/hardhat/Blue.spec.ts | 2 +- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/Blue.sol b/src/Blue.sol index 2e745e1bb..ce595af96 100644 --- a/src/Blue.sol +++ b/src/Blue.sol @@ -16,8 +16,10 @@ import {SafeTransferLib} from "./libraries/SafeTransferLib.sol"; import {FixedPointMathLib, WAD} from "./libraries/FixedPointMathLib.sol"; uint256 constant MAX_FEE = 0.25e18; -uint256 constant BETA = 0.3e18; -uint256 constant MAX_INCENTIVE = 0.15e18; +// Liquidation cursor. +uint256 constant LIQUIDATION_CURSOR = 0.3e18; +// Max liquidation scalar (1 + maxLiquidationIncentive). +uint256 constant MAX_LIQUIDATION_SCALAR = 1.15e18; /// @dev The EIP-712 typeHash for EIP712Domain. bytes32 constant DOMAIN_TYPEHASH = keccak256("EIP712Domain(string name,uint256 chainId,address verifyingContract)"); @@ -295,9 +297,7 @@ contract Blue is IBlue { require(!_isHealthy(market, id, borrower, collateralPrice, priceScale), ErrorsLib.HEALTHY_POSITION); - // The liquidation incentive is min(MAX_INCENTIVE, 1/(BETA * lltv + 1 - BETA) - 1). - uint256 incentive = UtilsLib.min(MAX_INCENTIVE, WAD.wDivDown(BETA.wMulDown(market.lltv) + WAD - BETA) - WAD); - uint256 repaid = seized.mulDivUp(collateralPrice, priceScale).wDivUp(WAD + incentive); + uint256 repaid = seized.mulDivUp(collateralPrice, priceScale).wDivUp(liquidationScalar(market.lltv)); uint256 repaidShares = repaid.toSharesDown(totalBorrow[id], totalBorrowShares[id]); borrowShares[id][borrower] -= repaidShares; @@ -442,4 +442,14 @@ contract Blue is IBlue { } } } + + // Liquidation scalar. + + /// @dev The liquidation incentive is min(maxIncentive, 1/(cursor * lltv + 1 - cursor) - 1). + /// @dev The liquidation scalar is 1 + incentive. + function liquidationScalar(uint256 lltv) private pure returns (uint256) { + return UtilsLib.min( + MAX_LIQUIDATION_SCALAR, WAD.wDivDown(LIQUIDATION_CURSOR.wMulDown(lltv) + WAD - LIQUIDATION_CURSOR) + ); + } } diff --git a/test/forge/Blue.t.sol b/test/forge/Blue.t.sol index 9ccb442ac..af0e8064c 100644 --- a/test/forge/Blue.t.sol +++ b/test/forge/Blue.t.sol @@ -616,7 +616,9 @@ contract BlueTest is uint256 borrowingPower = amountCollateral.wMulDown(LLTV); uint256 amountBorrowed = borrowingPower.wMulDown(0.8e18); uint256 toSeize = amountCollateral.wMulDown(LLTV); - uint256 incentive = UtilsLib.min(MAX_INCENTIVE, WAD.wDivDown(BETA.wMulDown(market.lltv) + WAD - BETA) - WAD); + uint256 liquidationScalar = UtilsLib.min( + MAX_LIQUIDATION_SCALAR, WAD.wDivDown(LIQUIDATION_CURSOR.wMulDown(market.lltv) + WAD - LIQUIDATION_CURSOR) + ); borrowableAsset.setBalance(address(this), amountLent); collateralAsset.setBalance(BORROWER, amountCollateral); @@ -643,7 +645,7 @@ contract BlueTest is uint256 liquidatorNetWorthAfter = netWorth(LIQUIDATOR); (uint256 collateralPrice, uint256 priceScale) = IOracle(market.oracle).price(); - uint256 expectedRepaid = toSeize.mulDivUp(collateralPrice, priceScale).wDivUp(WAD + incentive); + uint256 expectedRepaid = toSeize.mulDivUp(collateralPrice, priceScale).wDivUp(liquidationScalar); uint256 expectedNetWorthAfter = liquidatorNetWorthBefore + toSeize.mulDivDown(collateralPrice, priceScale) - expectedRepaid; assertEq(liquidatorNetWorthAfter, expectedNetWorthAfter, "LIQUIDATOR net worth"); @@ -659,7 +661,9 @@ contract BlueTest is uint256 borrowingPower = amountCollateral.wMulDown(LLTV); uint256 amountBorrowed = borrowingPower.wMulDown(0.8e18); uint256 toSeize = amountCollateral; - uint256 incentive = UtilsLib.min(MAX_INCENTIVE, WAD.wDivDown(BETA.wMulDown(market.lltv) + WAD - BETA) - WAD); + uint256 liquidationScalar = UtilsLib.min( + MAX_LIQUIDATION_SCALAR, WAD.wDivDown(LIQUIDATION_CURSOR.wMulDown(market.lltv) + WAD - LIQUIDATION_CURSOR) + ); borrowableAsset.setBalance(address(this), amountLent); collateralAsset.setBalance(BORROWER, amountCollateral); @@ -686,7 +690,7 @@ contract BlueTest is uint256 liquidatorNetWorthAfter = netWorth(LIQUIDATOR); (uint256 collateralPrice, uint256 priceScale) = IOracle(market.oracle).price(); - uint256 expectedRepaid = toSeize.mulDivUp(collateralPrice, priceScale).wDivUp(WAD + incentive); + uint256 expectedRepaid = toSeize.mulDivUp(collateralPrice, priceScale).wDivUp(liquidationScalar); uint256 expectedNetWorthAfter = liquidatorNetWorthBefore + toSeize.mulDivDown(collateralPrice, priceScale) - expectedRepaid; assertEq(liquidatorNetWorthAfter, expectedNetWorthAfter, "LIQUIDATOR net worth"); diff --git a/test/hardhat/Blue.spec.ts b/test/hardhat/Blue.spec.ts index 72cf8d345..65fa7208b 100644 --- a/test/hardhat/Blue.spec.ts +++ b/test/hardhat/Blue.spec.ts @@ -175,7 +175,7 @@ describe("Blue", () => { await oracle.setPrice(BigNumber.WAD.div(10)); - const seized = closePositions ? constants.MaxUint256 : amount.div(2); + const seized = closePositions ? amount : amount.div(2); await blue.connect(liquidator).liquidate(market, borrower.address, seized, "0x");