Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LIF naming #506

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/Morpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -350,18 +350,20 @@ contract Morpho is IMorpho {

uint256 repaidAssets;
{
// The liquidation incentive factor is min(maxIncentiveFactor, 1/(1 - cursor*(1 - lltv))).
uint256 incentiveFactor = UtilsLib.min(
// The liquidation incentive factor is min(maxLiquidationIncentiveFactor, 1/(1 - cursor*(1 - lltv))).
uint256 liquidationIncentiveFactor = UtilsLib.min(
MAX_LIQUIDATION_INCENTIVE_FACTOR,
WAD.wDivDown(WAD - LIQUIDATION_CURSOR.wMulDown(WAD - marketParams.lltv))
);

if (seizedAssets > 0) {
repaidAssets = seizedAssets.mulDivUp(collateralPrice, ORACLE_PRICE_SCALE).wDivUp(incentiveFactor);
repaidAssets =
seizedAssets.mulDivUp(collateralPrice, ORACLE_PRICE_SCALE).wDivUp(liquidationIncentiveFactor);
repaidShares = repaidAssets.toSharesDown(market[id].totalBorrowAssets, market[id].totalBorrowShares);
} else {
repaidAssets = repaidShares.toAssetsUp(market[id].totalBorrowAssets, market[id].totalBorrowShares);
seizedAssets = repaidAssets.wMulDown(incentiveFactor).mulDivDown(ORACLE_PRICE_SCALE, collateralPrice);
seizedAssets =
repaidAssets.wMulDown(liquidationIncentiveFactor).mulDivDown(ORACLE_PRICE_SCALE, collateralPrice);
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/forge/BaseTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ contract BaseTest is Test {
uint256 collateral = morpho.collateral(_id, borrower);
uint256 collateralPrice = IOracle(_marketParams.oracle).price();
uint256 maxRepaidAssets = morpho.expectedBorrowBalance(_marketParams, borrower);
uint256 maxSeizedAssets = maxRepaidAssets.wMulDown(_liquidationIncentive(_marketParams.lltv)).mulDivDown(
uint256 maxSeizedAssets = maxRepaidAssets.wMulDown(_liquidationIncentiveFactor(_marketParams.lltv)).mulDivDown(
ORACLE_PRICE_SCALE, collateralPrice
);

Expand All @@ -357,7 +357,7 @@ contract BaseTest is Test {
uint256 borrowShares = morpho.borrowShares(_id, borrower);
uint256 collateralPrice = IOracle(_marketParams.oracle).price();
uint256 maxRepaidAssets = morpho.collateral(_id, borrower).mulDivUp(collateralPrice, ORACLE_PRICE_SCALE).wDivUp(
_liquidationIncentive(_marketParams.lltv)
_liquidationIncentiveFactor(_marketParams.lltv)
);
(,, uint256 totalBorrowAssets, uint256 totalBorrowShares) = morpho.expectedMarketBalances(marketParams);
uint256 maxRepaidShares = maxRepaidAssets.toSharesDown(totalBorrowAssets, totalBorrowShares);
Expand All @@ -380,7 +380,7 @@ contract BaseTest is Test {
return maxBorrow >= borrowed;
}

function _liquidationIncentive(uint256 lltv) internal pure returns (uint256) {
function _liquidationIncentiveFactor(uint256 lltv) internal pure returns (uint256) {
return Math.min(MAX_LIQUIDATION_INCENTIVE_FACTOR, WAD.wDivDown(WAD - LIQUIDATION_CURSOR.wMulDown(WAD - lltv)));
}

Expand Down
19 changes: 11 additions & 8 deletions test/forge/integration/LiquidateIntegrationTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,13 @@ contract LiquidateIntegrationTest is BaseTest {
amountSupplied = bound(amountSupplied, amountBorrowed, amountBorrowed + MAX_TEST_AMOUNT);
_supply(amountSupplied);

uint256 incentive = _liquidationIncentive(marketParams.lltv);
uint256 maxSeized = amountBorrowed.wMulDown(incentive).mulDivDown(ORACLE_PRICE_SCALE, priceCollateral);
uint256 liquidationIncentiveFactor = _liquidationIncentiveFactor(marketParams.lltv);
uint256 maxSeized =
amountBorrowed.wMulDown(liquidationIncentiveFactor).mulDivDown(ORACLE_PRICE_SCALE, priceCollateral);
vm.assume(maxSeized != 0);
amountSeized = bound(amountSeized, 1, Math.min(maxSeized, amountCollateral - 1));
uint256 expectedRepaid = amountSeized.mulDivUp(priceCollateral, ORACLE_PRICE_SCALE).wDivUp(incentive);
uint256 expectedRepaid =
amountSeized.mulDivUp(priceCollateral, ORACLE_PRICE_SCALE).wDivUp(liquidationIncentiveFactor);

borrowableToken.setBalance(LIQUIDATOR, amountBorrowed);
collateralToken.setBalance(BORROWER, amountCollateral);
Expand Down Expand Up @@ -150,12 +152,12 @@ contract LiquidateIntegrationTest is BaseTest {

uint256 expectedBorrowShares = amountBorrowed.toSharesUp(0, 0);
uint256 maxRepaidShares = amountCollateral.mulDivDown(priceCollateral, ORACLE_PRICE_SCALE).wDivDown(
_liquidationIncentive(marketParams.lltv)
_liquidationIncentiveFactor(marketParams.lltv)
);
vm.assume(maxRepaidShares != 0);
sharesRepaid = bound(sharesRepaid, 1, Math.min(maxRepaidShares, expectedBorrowShares));
uint256 expectedRepaid = sharesRepaid.toAssetsUp(amountBorrowed, expectedBorrowShares);
uint256 expectedSeized = expectedRepaid.wMulDown(_liquidationIncentive(marketParams.lltv)).mulDivDown(
uint256 expectedSeized = expectedRepaid.wMulDown(_liquidationIncentiveFactor(marketParams.lltv)).mulDivDown(
ORACLE_PRICE_SCALE, priceCollateral
);

Expand Down Expand Up @@ -199,7 +201,7 @@ contract LiquidateIntegrationTest is BaseTest {
}

struct LiquidateBadDebtTestParams {
uint256 incentive;
uint256 liquidationIncentiveFactor;
uint256 expectedRepaid;
uint256 expectedRepaidShares;
uint256 borrowSharesBeforeLiquidation;
Expand All @@ -224,8 +226,9 @@ contract LiquidateIntegrationTest is BaseTest {

vm.assume(amountCollateral > 1);

params.incentive = _liquidationIncentive(marketParams.lltv);
params.expectedRepaid = amountCollateral.mulDivUp(priceCollateral, ORACLE_PRICE_SCALE).wDivUp(params.incentive);
params.liquidationIncentiveFactor = _liquidationIncentiveFactor(marketParams.lltv);
params.expectedRepaid =
amountCollateral.mulDivUp(priceCollateral, ORACLE_PRICE_SCALE).wDivUp(params.liquidationIncentiveFactor);

uint256 minBorrowed = Math.max(params.expectedRepaid, amountBorrowed);
amountBorrowed = bound(amountBorrowed, minBorrowed, Math.max(minBorrowed, MAX_TEST_AMOUNT));
Expand Down
5 changes: 3 additions & 2 deletions test/forge/invariant/MorphoInvariantTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,9 @@ contract MorphoInvariantTest is InvariantTest {
logCall("liquidateSeizedAssets")
{
uint256 collateralPrice = oracle.price();
uint256 repaidAssets =
seizedAssets.mulDivUp(collateralPrice, ORACLE_PRICE_SCALE).wDivUp(_liquidationIncentive(_marketParams.lltv));
uint256 repaidAssets = seizedAssets.mulDivUp(collateralPrice, ORACLE_PRICE_SCALE).wDivUp(
_liquidationIncentiveFactor(_marketParams.lltv)
);

borrowableToken.setBalance(msg.sender, repaidAssets);

Expand Down
2 changes: 1 addition & 1 deletion test/morpho_tests.tree
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@
│ └── it should compute repaidShares = assetsRepaid.toSharesDown(totalBorrow[marketParams.id], totalBorrowShares[market.id])
├── when repaidShares is not zero
│ ├── it should compute assetsRepaid = repaidShares.toAssetsUp(totalBorrow[marketParams.id], totalBorrowShares[marketParams.id])
│ └── it should compute seizedAssets = assetsRepaid.wMulDown(incentiveFactor).mulDivDown(ORACLE_PRICE_SCALE, collateralPrice)
│ └── it should compute seizedAssets = assetsRepaid.wMulDown(liquidationIncentiveFactor).mulDivDown(ORACLE_PRICE_SCALE, collateralPrice)
├── it should remove repaidShares from totalBorrowShares[marketParams.id]
├── it should remove assetsRepaid from totalBorrow[marketParams.id]
├── it should remove repaidShares from collateral[marketParams.id][borrower]
Expand Down