Skip to content

Commit

Permalink
style: better naming and internalize computation
Browse files Browse the repository at this point in the history
  • Loading branch information
MathisGD committed Aug 10, 2023
1 parent a5d2a0f commit de861cd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
20 changes: 15 additions & 5 deletions src/Blue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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)");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
);
}
}
12 changes: 8 additions & 4 deletions test/forge/Blue.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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");
Expand All @@ -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);
Expand All @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion test/hardhat/Blue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down

0 comments on commit de861cd

Please sign in to comment.