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

L-09 alternative #490

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 1 addition & 5 deletions src/Morpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -488,10 +488,6 @@ contract Morpho is IMorpho {
/// @dev Returns whether the position of `borrower` in the given market `marketParams` with the given
/// `collateralPrice` is healthy.
/// @dev Assumes that the inputs `marketParams` and `id` match.
/// @dev The total amount of borrowed assets can be rounding up two times in a row:
/// 1. When converting the borrowed assets to shares before updating the storage.
/// 2. When converting `borrowShares` to `borrowed` in `_isHealthy`.
/// This behavior can lead an healthy position to be marked as unhealthy by 1 wei off and is acknowledged.
function _isHealthy(MarketParams memory marketParams, Id id, address borrower, uint256 collateralPrice)
internal
view
Expand All @@ -503,7 +499,7 @@ contract Morpho is IMorpho {
uint256 maxBorrow = uint256(position[id][borrower].collateral).mulDivDown(collateralPrice, ORACLE_PRICE_SCALE)
.wMulDown(marketParams.lltv);

return maxBorrow >= borrowed;
return maxBorrow > borrowed;
}

/* STORAGE VIEW */
Expand Down
3 changes: 2 additions & 1 deletion test/forge/BaseTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ contract BaseTest is Test {
priceCollateral = bound(priceCollateral, MIN_COLLATERAL_PRICE, MAX_COLLATERAL_PRICE);
amountBorrowed = bound(amountBorrowed, MIN_TEST_AMOUNT, MAX_TEST_AMOUNT);

uint256 minCollateral = amountBorrowed.wDivUp(marketParams.lltv).mulDivUp(ORACLE_PRICE_SCALE, priceCollateral);
uint256 minCollateral =
amountBorrowed.wDivUp(marketParams.lltv).mulDivUp(ORACLE_PRICE_SCALE, priceCollateral) + 2;

if (minCollateral <= MAX_COLLATERAL_ASSETS) {
amountCollateral = bound(amountCollateral, minCollateral, MAX_COLLATERAL_ASSETS);
Expand Down
2 changes: 1 addition & 1 deletion test/forge/integration/BorrowIntegrationTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ contract BorrowIntegrationTest is BaseTest {
vm.stopPrank();
}

function testBorrowUnsufficientLiquidity(
function testBorrowInsufficientLiquidity(
uint256 amountCollateral,
uint256 amountSupplied,
uint256 amountBorrowed,
Expand Down
12 changes: 3 additions & 9 deletions test/forge/libraries/periphery/MorphoBalancesLibTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,11 @@ contract MorphoBalancesLibTest is BaseTest {

if (amountBorrowed > 0) {
uint256 collateralPrice = IOracle(marketParams.oracle).price();
collateralToken.setBalance(
BORROWER, amountBorrowed.wDivUp(lltv).mulDivUp(ORACLE_PRICE_SCALE, collateralPrice)
);
uint256 collateral = amountBorrowed.wDivUp(lltv).mulDivUp(ORACLE_PRICE_SCALE, collateralPrice) + 2;
collateralToken.setBalance(BORROWER, collateral);

vm.startPrank(BORROWER);
morpho.supplyCollateral(
marketParams,
amountBorrowed.wDivUp(lltv).mulDivUp(ORACLE_PRICE_SCALE, collateralPrice),
BORROWER,
hex""
);
morpho.supplyCollateral(marketParams, collateral, BORROWER, hex"");
morpho.borrow(marketParams, amountBorrowed, 0, BORROWER, BORROWER);
vm.stopPrank();
}
Expand Down
9 changes: 3 additions & 6 deletions test/forge/libraries/periphery/MorphoLibTest.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;

import {MorphoBalancesLib} from "src/libraries/periphery/MorphoBalancesLib.sol";

import "../../BaseTest.sol";

contract MorphoLibTest is BaseTest {
Expand Down Expand Up @@ -32,12 +30,11 @@ contract MorphoLibTest is BaseTest {
morpho.supply(marketParams, amountSupplied, 0, address(this), hex"");

uint256 collateralPrice = IOracle(marketParams.oracle).price();
collateralToken.setBalance(BORROWER, amountBorrowed.wDivUp(lltv).mulDivUp(ORACLE_PRICE_SCALE, collateralPrice));
uint256 collateral = amountBorrowed.wDivUp(lltv).mulDivUp(ORACLE_PRICE_SCALE, collateralPrice) + 2;
collateralToken.setBalance(BORROWER, collateral);

vm.startPrank(BORROWER);
morpho.supplyCollateral(
marketParams, amountBorrowed.wDivUp(lltv).mulDivUp(ORACLE_PRICE_SCALE, collateralPrice), BORROWER, hex""
);
morpho.supplyCollateral(marketParams, collateral, BORROWER, hex"");
morpho.borrow(marketParams, amountBorrowed, 0, BORROWER, BORROWER);
vm.stopPrank();
}
Expand Down
7 changes: 3 additions & 4 deletions test/forge/libraries/periphery/MorphoStorageLibTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ contract MorphoStorageLibTest is BaseTest {
morpho.supply(marketParams, amountSupplied, 0, address(this), hex"");

uint256 collateralPrice = IOracle(marketParams.oracle).price();
collateralToken.setBalance(BORROWER, amountBorrowed.wDivUp(lltv).mulDivUp(ORACLE_PRICE_SCALE, collateralPrice));
uint256 collateral = amountBorrowed.wDivUp(lltv).mulDivUp(ORACLE_PRICE_SCALE, collateralPrice) + 2;
collateralToken.setBalance(BORROWER, collateral);

vm.startPrank(BORROWER);
morpho.supplyCollateral(
marketParams, amountBorrowed.wDivUp(lltv).mulDivUp(ORACLE_PRICE_SCALE, collateralPrice), BORROWER, hex""
);
morpho.supplyCollateral(marketParams, collateral, BORROWER, hex"");
morpho.borrow(marketParams, amountBorrowed, 0, BORROWER, BORROWER);
vm.stopPrank();

Expand Down
Loading