Skip to content

Commit

Permalink
Merge pull request #1675 from morpho-org/test/skip-lens-delta-rate
Browse files Browse the repository at this point in the history
Skip upgrade tests in revert conditions
  • Loading branch information
QGarchery authored Mar 15, 2024
2 parents ca7e6c0 + 06a0616 commit 52363dc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 22 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
6 changes: 5 additions & 1 deletion test/prod/aave-v2/TestUpgradeLens.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ contract TestUpgradeLens is TestSetup {
supplyAmount
);

(uint256 supplyDelta, , , ) = morpho.deltas(supplyMarket.poolToken);
if (supplyDelta > 0) continue;

_tip(supplyMarket.underlying, address(user), supplyAmount);

user.approve(supplyMarket.underlying, supplyAmount);
Expand All @@ -95,7 +98,8 @@ contract TestUpgradeLens is TestSetup {
string.concat(supplyMarket.symbol, " supply rate")
);

if (borrowMarket.status.isBorrowPaused) continue;
(, uint256 borrowDelta, , ) = morpho.deltas(borrowMarket.poolToken);
if (borrowDelta > 0 || borrowMarket.status.isBorrowPaused) continue;

(uint256 expectedBorrowRate, , , ) = lens.getNextUserBorrowRatePerYear(
borrowMarket.poolToken,
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
14 changes: 12 additions & 2 deletions 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 All @@ -116,6 +122,9 @@ contract TestUpgradeLens is TestSetup {
supplyAmount
);

(uint256 supplyDelta, , , ) = morpho.deltas(supplyMarket.poolToken);
if (supplyDelta > 0) continue;

_tip(supplyMarket.underlying, address(user), supplyAmount);

user.approve(supplyMarket.underlying, supplyAmount);
Expand All @@ -128,7 +137,8 @@ contract TestUpgradeLens is TestSetup {
string.concat(supplyMarket.symbol, " supply rate")
);

if (borrowMarket.status.isBorrowPaused) continue;
(, uint256 borrowDelta, , ) = morpho.deltas(borrowMarket.poolToken);
if (borrowDelta > 0 || borrowMarket.status.isBorrowPaused) continue;

(uint256 expectedBorrowRate, , , ) = lens.getNextUserBorrowRatePerBlock(
borrowMarket.poolToken,
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 52363dc

Please sign in to comment.