From a17e5bcba2d977d225cbbf523bd7516122fe8cdd Mon Sep 17 00:00:00 2001 From: Quentin Garchery Date: Thu, 14 Mar 2024 10:20:54 +0100 Subject: [PATCH 1/2] test: skip test lens when over utilized --- test/prod/compound/TestLifecycle.t.sol | 12 ++++++++-- test/prod/compound/TestUpgradeLens.t.sol | 8 ++++++- test/prod/compound/setup/TestSetup.sol | 28 +++++++++++------------- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/test/prod/compound/TestLifecycle.t.sol b/test/prod/compound/TestLifecycle.t.sol index fad39e72e..1226a9e03 100644 --- a/test/prod/compound/TestLifecycle.t.sol +++ b/test/prod/compound/TestLifecycle.t.sol @@ -393,7 +393,11 @@ 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, @@ -401,6 +405,8 @@ contract TestLifecycle is TestSetup { supplyMarket.collateralFactor ).mul(1.01 ether); + if (overUtilized) continue; + MarketSideTest memory supply = _supply(supplyMarket, supplyAmount); _testSupply(supply); @@ -443,7 +449,7 @@ contract TestLifecycle is TestSetup { if (borrowMarket.status.isBorrowPaused) continue; - uint256 borrowAmount = _boundBorrowAmount( + (uint256 borrowAmount, bool overUtilized) = _boundBorrowAmount( borrowMarket, _amount, oracle.getUnderlyingPrice(borrowMarket.poolToken) @@ -455,6 +461,8 @@ contract TestLifecycle is TestSetup { supplyMarket.collateralFactor ).mul(0.995 ether); + if (overUtilized) continue; + _supply(supplyMarket, supplyAmount); vm.expectRevert(PositionsManager.UnauthorisedBorrow.selector); diff --git a/test/prod/compound/TestUpgradeLens.t.sol b/test/prod/compound/TestUpgradeLens.t.sol index 42382527f..d685a6194 100644 --- a/test/prod/compound/TestUpgradeLens.t.sol +++ b/test/prod/compound/TestUpgradeLens.t.sol @@ -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, diff --git a/test/prod/compound/setup/TestSetup.sol b/test/prod/compound/setup/TestSetup.sol index d620ab1ce..de06d96db 100644 --- a/test/prod/compound/setup/TestSetup.sol +++ b/test/prod/compound/setup/TestSetup.sol @@ -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) { From 5126bdbf613c5fb0224ef6e294c74655b9d09a61 Mon Sep 17 00:00:00 2001 From: Quentin Garchery Date: Thu, 14 Mar 2024 14:47:42 +0100 Subject: [PATCH 2/2] test: do not revert when nothing to tip --- test/prod/aave-v2/TestLifecycle.t.sol | 5 ++++- test/prod/compound/TestLifecycle.t.sol | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/test/prod/aave-v2/TestLifecycle.t.sol b/test/prod/aave-v2/TestLifecycle.t.sol index 7fe6f8968..0f4e6b535 100644 --- a/test/prod/aave-v2/TestLifecycle.t.sol +++ b/test/prod/aave-v2/TestLifecycle.t.sol @@ -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); diff --git a/test/prod/compound/TestLifecycle.t.sol b/test/prod/compound/TestLifecycle.t.sol index 1226a9e03..c1998b772 100644 --- a/test/prod/compound/TestLifecycle.t.sol +++ b/test/prod/compound/TestLifecycle.t.sol @@ -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);