Skip to content

Commit

Permalink
Merge pull request #1676 from morpho-org/test/skip-lens-over-utilized
Browse files Browse the repository at this point in the history
Skip lens rate tests when over utilized
  • Loading branch information
QGarchery authored Mar 14, 2024
2 parents 72bf09c + 5126bdb commit 06a0616
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
5 changes: 4 additions & 1 deletion test/prod/aave-v2/TestLifecycle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,10 @@ contract TestLifecycle is TestSetup {
_tip(
borrow.market.underlying,
address(user),
borrow.position.total - ERC20(borrow.market.underlying).balanceOf(address(user))
Math.zeroFloorSub(
borrow.position.total,
ERC20(borrow.market.underlying).balanceOf(address(user))
)
);
user.approve(borrow.market.underlying, borrow.position.total);
user.repay(borrow.market.poolToken, address(user), type(uint256).max);
Expand Down
17 changes: 14 additions & 3 deletions test/prod/compound/TestLifecycle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,10 @@ contract TestLifecycle is TestSetup {
_tip(
borrow.market.underlying,
address(user),
borrow.position.total - ERC20(borrow.market.underlying).balanceOf(address(user))
Math.zeroFloorSub(
borrow.position.total,
ERC20(borrow.market.underlying).balanceOf(address(user))
)
);
user.approve(borrow.market.underlying, borrow.position.total);
user.repay(borrow.market.poolToken, address(user), type(uint256).max);
Expand Down Expand Up @@ -393,14 +396,20 @@ contract TestLifecycle is TestSetup {
TestMarket memory borrowMarket = borrowableMarkets[borrowMarketIndex];

uint256 borrowedPrice = oracle.getUnderlyingPrice(borrowMarket.poolToken);
uint256 borrowAmount = _boundBorrowAmount(borrowMarket, _amount, borrowedPrice);
(uint256 borrowAmount, bool overUtilized) = _boundBorrowAmount(
borrowMarket,
_amount,
borrowedPrice
);
uint256 supplyAmount = _getMinimumCollateralAmount(
borrowAmount,
borrowedPrice,
oracle.getUnderlyingPrice(supplyMarket.poolToken),
supplyMarket.collateralFactor
).mul(1.01 ether);

if (overUtilized) continue;

MarketSideTest memory supply = _supply(supplyMarket, supplyAmount);
_testSupply(supply);

Expand Down Expand Up @@ -443,7 +452,7 @@ contract TestLifecycle is TestSetup {

if (borrowMarket.status.isBorrowPaused) continue;

uint256 borrowAmount = _boundBorrowAmount(
(uint256 borrowAmount, bool overUtilized) = _boundBorrowAmount(
borrowMarket,
_amount,
oracle.getUnderlyingPrice(borrowMarket.poolToken)
Expand All @@ -455,6 +464,8 @@ contract TestLifecycle is TestSetup {
supplyMarket.collateralFactor
).mul(0.995 ether);

if (overUtilized) continue;

_supply(supplyMarket, supplyAmount);

vm.expectRevert(PositionsManager.UnauthorisedBorrow.selector);
Expand Down
8 changes: 7 additions & 1 deletion test/prod/compound/TestUpgradeLens.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,13 @@ contract TestUpgradeLens is TestSetup {
TestMarket memory borrowMarket = borrowableMarkets[borrowMarketIndex];

uint256 borrowedPrice = oracle.getUnderlyingPrice(borrowMarket.poolToken);
uint256 borrowAmount = _boundBorrowAmount(borrowMarket, _amount, borrowedPrice);
(uint256 borrowAmount, bool overUtilized) = _boundBorrowAmount(
borrowMarket,
_amount,
borrowedPrice
);
if (overUtilized) continue;

uint256 supplyAmount = _getMinimumCollateralAmount(
borrowAmount,
borrowedPrice,
Expand Down
28 changes: 13 additions & 15 deletions test/prod/compound/setup/TestSetup.sol
Original file line number Diff line number Diff line change
Expand Up @@ -190,23 +190,21 @@ contract TestSetup is Config, ProdTest {
TestMarket memory _market,
uint96 _amount,
uint256 _price
) internal view returns (uint256) {
) internal view returns (uint256 borrowAmount, bool overUtilized) {
ICToken poolToken = ICToken(_market.poolToken);
return
bound(
_amount,
MIN_USD_AMOUNT.div(_price),
uint256 cash = poolToken.getCash();
borrowAmount = bound(
_amount,
MIN_USD_AMOUNT.div(_price),
Math.min(
Math.min(
Math.min(
Math.min(
(_market.maxBorrows - _market.totalBorrows) / 2,
poolToken.getCash() - poolToken.totalReserves()
),
MAX_USD_AMOUNT.div(_price)
),
type(uint96).max / 2 // so that collateral amount < type(uint96).max
)
);
Math.min((_market.maxBorrows - _market.totalBorrows) / 2, cash),
MAX_USD_AMOUNT.div(_price)
),
type(uint96).max / 2 // so that collateral amount < type(uint96).max
)
);
overUtilized = borrowAmount + poolToken.totalReserves() > cash;
}

function _getUnderlying(address _poolToken) internal view returns (address underlying) {
Expand Down

0 comments on commit 06a0616

Please sign in to comment.