From 38e76d3827cf53bd508702f91fda44cba0530e9d Mon Sep 17 00:00:00 2001 From: Quentin Garchery Date: Thu, 14 Mar 2024 10:08:20 +0100 Subject: [PATCH 1/5] test: skip lens rate tests with deltas --- test/prod/aave-v2/TestUpgradeLens.t.sol | 8 +++++++- test/prod/compound/TestUpgradeLens.t.sol | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/test/prod/aave-v2/TestUpgradeLens.t.sol b/test/prod/aave-v2/TestUpgradeLens.t.sol index b7c807e94..0da9beba2 100644 --- a/test/prod/aave-v2/TestUpgradeLens.t.sol +++ b/test/prod/aave-v2/TestUpgradeLens.t.sol @@ -83,6 +83,12 @@ contract TestUpgradeLens is TestSetup { supplyAmount ); + (uint256 supplyDelta, uint256 borrowDelta, , ) = morpho.deltas( + supplyMarket.poolToken + ); + + if (supplyDelta > 0) continue; + _tip(supplyMarket.underlying, address(user), supplyAmount); user.approve(supplyMarket.underlying, supplyAmount); @@ -95,7 +101,7 @@ contract TestUpgradeLens is TestSetup { string.concat(supplyMarket.symbol, " supply rate") ); - if (borrowMarket.status.isBorrowPaused) continue; + if (borrowDelta > 0 || borrowMarket.status.isBorrowPaused) continue; (uint256 expectedBorrowRate, , , ) = lens.getNextUserBorrowRatePerYear( borrowMarket.poolToken, diff --git a/test/prod/compound/TestUpgradeLens.t.sol b/test/prod/compound/TestUpgradeLens.t.sol index 119a7e93a..42382527f 100644 --- a/test/prod/compound/TestUpgradeLens.t.sol +++ b/test/prod/compound/TestUpgradeLens.t.sol @@ -116,6 +116,12 @@ contract TestUpgradeLens is TestSetup { supplyAmount ); + (uint256 supplyDelta, uint256 borrowDelta, , ) = morpho.deltas( + supplyMarket.poolToken + ); + + if (supplyDelta > 0) continue; + _tip(supplyMarket.underlying, address(user), supplyAmount); user.approve(supplyMarket.underlying, supplyAmount); @@ -128,7 +134,7 @@ contract TestUpgradeLens is TestSetup { string.concat(supplyMarket.symbol, " supply rate") ); - if (borrowMarket.status.isBorrowPaused) continue; + if (borrowDelta > 0 || borrowMarket.status.isBorrowPaused) continue; (uint256 expectedBorrowRate, , , ) = lens.getNextUserBorrowRatePerBlock( borrowMarket.poolToken, From a17e5bcba2d977d225cbbf523bd7516122fe8cdd Mon Sep 17 00:00:00 2001 From: Quentin Garchery Date: Thu, 14 Mar 2024 10:20:54 +0100 Subject: [PATCH 2/5] 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 a9052672e13eb41dc6121bc6292afdf365b80ef2 Mon Sep 17 00:00:00 2001 From: Quentin Garchery Date: Thu, 14 Mar 2024 10:54:15 +0100 Subject: [PATCH 3/5] fix: skip corresponding delta --- test/prod/aave-v2/TestUpgradeLens.t.sol | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/prod/aave-v2/TestUpgradeLens.t.sol b/test/prod/aave-v2/TestUpgradeLens.t.sol index 0da9beba2..228f82a12 100644 --- a/test/prod/aave-v2/TestUpgradeLens.t.sol +++ b/test/prod/aave-v2/TestUpgradeLens.t.sol @@ -83,10 +83,7 @@ contract TestUpgradeLens is TestSetup { supplyAmount ); - (uint256 supplyDelta, uint256 borrowDelta, , ) = morpho.deltas( - supplyMarket.poolToken - ); - + (uint256 supplyDelta, , , ) = morpho.deltas(supplyMarket.poolToken); if (supplyDelta > 0) continue; _tip(supplyMarket.underlying, address(user), supplyAmount); @@ -101,6 +98,7 @@ contract TestUpgradeLens is TestSetup { string.concat(supplyMarket.symbol, " supply rate") ); + (, uint256 borrowDelta, , ) = morpho.deltas(borrowMarket.poolToken); if (borrowDelta > 0 || borrowMarket.status.isBorrowPaused) continue; (uint256 expectedBorrowRate, , , ) = lens.getNextUserBorrowRatePerYear( From 72bf09c305bd4dd19e7e56df579710e2e642dae5 Mon Sep 17 00:00:00 2001 From: Quentin Garchery Date: Thu, 14 Mar 2024 10:55:59 +0100 Subject: [PATCH 4/5] fix: corresponding delta for compound --- test/prod/compound/TestUpgradeLens.t.sol | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/prod/compound/TestUpgradeLens.t.sol b/test/prod/compound/TestUpgradeLens.t.sol index 42382527f..8d4ba7fd2 100644 --- a/test/prod/compound/TestUpgradeLens.t.sol +++ b/test/prod/compound/TestUpgradeLens.t.sol @@ -116,10 +116,7 @@ contract TestUpgradeLens is TestSetup { supplyAmount ); - (uint256 supplyDelta, uint256 borrowDelta, , ) = morpho.deltas( - supplyMarket.poolToken - ); - + (uint256 supplyDelta, , , ) = morpho.deltas(supplyMarket.poolToken); if (supplyDelta > 0) continue; _tip(supplyMarket.underlying, address(user), supplyAmount); @@ -134,6 +131,7 @@ contract TestUpgradeLens is TestSetup { string.concat(supplyMarket.symbol, " supply rate") ); + (, uint256 borrowDelta, , ) = morpho.deltas(borrowMarket.poolToken); if (borrowDelta > 0 || borrowMarket.status.isBorrowPaused) continue; (uint256 expectedBorrowRate, , , ) = lens.getNextUserBorrowRatePerBlock( From 5126bdbf613c5fb0224ef6e294c74655b9d09a61 Mon Sep 17 00:00:00 2001 From: Quentin Garchery Date: Thu, 14 Mar 2024 14:47:42 +0100 Subject: [PATCH 5/5] 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);