Skip to content
This repository was archived by the owner on Dec 18, 2025. It is now read-only.

Commit 06a0616

Browse files
authored
Merge pull request #1676 from morpho-org/test/skip-lens-over-utilized
Skip lens rate tests when over utilized
2 parents 72bf09c + 5126bdb commit 06a0616

File tree

4 files changed

+38
-20
lines changed

4 files changed

+38
-20
lines changed

test/prod/aave-v2/TestLifecycle.t.sol

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,10 @@ contract TestLifecycle is TestSetup {
235235
_tip(
236236
borrow.market.underlying,
237237
address(user),
238-
borrow.position.total - ERC20(borrow.market.underlying).balanceOf(address(user))
238+
Math.zeroFloorSub(
239+
borrow.position.total,
240+
ERC20(borrow.market.underlying).balanceOf(address(user))
241+
)
239242
);
240243
user.approve(borrow.market.underlying, borrow.position.total);
241244
user.repay(borrow.market.poolToken, address(user), type(uint256).max);

test/prod/compound/TestLifecycle.t.sol

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,10 @@ contract TestLifecycle is TestSetup {
286286
_tip(
287287
borrow.market.underlying,
288288
address(user),
289-
borrow.position.total - ERC20(borrow.market.underlying).balanceOf(address(user))
289+
Math.zeroFloorSub(
290+
borrow.position.total,
291+
ERC20(borrow.market.underlying).balanceOf(address(user))
292+
)
290293
);
291294
user.approve(borrow.market.underlying, borrow.position.total);
292295
user.repay(borrow.market.poolToken, address(user), type(uint256).max);
@@ -393,14 +396,20 @@ contract TestLifecycle is TestSetup {
393396
TestMarket memory borrowMarket = borrowableMarkets[borrowMarketIndex];
394397

395398
uint256 borrowedPrice = oracle.getUnderlyingPrice(borrowMarket.poolToken);
396-
uint256 borrowAmount = _boundBorrowAmount(borrowMarket, _amount, borrowedPrice);
399+
(uint256 borrowAmount, bool overUtilized) = _boundBorrowAmount(
400+
borrowMarket,
401+
_amount,
402+
borrowedPrice
403+
);
397404
uint256 supplyAmount = _getMinimumCollateralAmount(
398405
borrowAmount,
399406
borrowedPrice,
400407
oracle.getUnderlyingPrice(supplyMarket.poolToken),
401408
supplyMarket.collateralFactor
402409
).mul(1.01 ether);
403410

411+
if (overUtilized) continue;
412+
404413
MarketSideTest memory supply = _supply(supplyMarket, supplyAmount);
405414
_testSupply(supply);
406415

@@ -443,7 +452,7 @@ contract TestLifecycle is TestSetup {
443452

444453
if (borrowMarket.status.isBorrowPaused) continue;
445454

446-
uint256 borrowAmount = _boundBorrowAmount(
455+
(uint256 borrowAmount, bool overUtilized) = _boundBorrowAmount(
447456
borrowMarket,
448457
_amount,
449458
oracle.getUnderlyingPrice(borrowMarket.poolToken)
@@ -455,6 +464,8 @@ contract TestLifecycle is TestSetup {
455464
supplyMarket.collateralFactor
456465
).mul(0.995 ether);
457466

467+
if (overUtilized) continue;
468+
458469
_supply(supplyMarket, supplyAmount);
459470

460471
vm.expectRevert(PositionsManager.UnauthorisedBorrow.selector);

test/prod/compound/TestUpgradeLens.t.sol

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,13 @@ contract TestUpgradeLens is TestSetup {
102102
TestMarket memory borrowMarket = borrowableMarkets[borrowMarketIndex];
103103

104104
uint256 borrowedPrice = oracle.getUnderlyingPrice(borrowMarket.poolToken);
105-
uint256 borrowAmount = _boundBorrowAmount(borrowMarket, _amount, borrowedPrice);
105+
(uint256 borrowAmount, bool overUtilized) = _boundBorrowAmount(
106+
borrowMarket,
107+
_amount,
108+
borrowedPrice
109+
);
110+
if (overUtilized) continue;
111+
106112
uint256 supplyAmount = _getMinimumCollateralAmount(
107113
borrowAmount,
108114
borrowedPrice,

test/prod/compound/setup/TestSetup.sol

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -190,23 +190,21 @@ contract TestSetup is Config, ProdTest {
190190
TestMarket memory _market,
191191
uint96 _amount,
192192
uint256 _price
193-
) internal view returns (uint256) {
193+
) internal view returns (uint256 borrowAmount, bool overUtilized) {
194194
ICToken poolToken = ICToken(_market.poolToken);
195-
return
196-
bound(
197-
_amount,
198-
MIN_USD_AMOUNT.div(_price),
195+
uint256 cash = poolToken.getCash();
196+
borrowAmount = bound(
197+
_amount,
198+
MIN_USD_AMOUNT.div(_price),
199+
Math.min(
199200
Math.min(
200-
Math.min(
201-
Math.min(
202-
(_market.maxBorrows - _market.totalBorrows) / 2,
203-
poolToken.getCash() - poolToken.totalReserves()
204-
),
205-
MAX_USD_AMOUNT.div(_price)
206-
),
207-
type(uint96).max / 2 // so that collateral amount < type(uint96).max
208-
)
209-
);
201+
Math.min((_market.maxBorrows - _market.totalBorrows) / 2, cash),
202+
MAX_USD_AMOUNT.div(_price)
203+
),
204+
type(uint96).max / 2 // so that collateral amount < type(uint96).max
205+
)
206+
);
207+
overUtilized = borrowAmount + poolToken.totalReserves() > cash;
210208
}
211209

212210
function _getUnderlying(address _poolToken) internal view returns (address underlying) {

0 commit comments

Comments
 (0)