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 fad39e72e..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); @@ -393,7 +396,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 +408,8 @@ contract TestLifecycle is TestSetup { supplyMarket.collateralFactor ).mul(1.01 ether); + if (overUtilized) continue; + MarketSideTest memory supply = _supply(supplyMarket, supplyAmount); _testSupply(supply); @@ -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) @@ -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); diff --git a/test/prod/compound/TestUpgradeLens.t.sol b/test/prod/compound/TestUpgradeLens.t.sol index 8d4ba7fd2..9084ad132 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) {