Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix upgrade tests #1669

Merged
merged 18 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions test/prod/ProdTest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.0;

import {SafeTransferLib, ERC20} from "@rari-capital/solmate/src/utils/SafeTransferLib.sol";

import {PercentageMath} from "@morpho-dao/morpho-utils/math/PercentageMath.sol";
import {Math} from "@morpho-dao/morpho-utils/math/Math.sol";
import {WadRayMath} from "@morpho-dao/morpho-utils/math/WadRayMath.sol";

import {BaseConfig} from "config/BaseConfig.sol";
import "@forge-std/console.sol";
import "@forge-std/Test.sol";

contract ProdTest is Test, BaseConfig {
// Needed because AAVE packs the balance struct.
function deal(
MathisGD marked this conversation as resolved.
Show resolved Hide resolved
address underlying,
address user,
uint256 amount
) internal override {
if (amount == 0) return;

if (underlying == aave) {
uint256 balance = ERC20(underlying).balanceOf(user);

if (amount > balance) {
ERC20(underlying).transfer(user, amount - balance);
} else {
vm.prank(user);
ERC20(underlying).transfer(address(this), balance - amount);
}

return;
}

super.deal(underlying, user, amount);
}
}
12 changes: 6 additions & 6 deletions test/prod/aave-v2/TestDeltas.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ contract TestDeltas is TestSetup {

function testShouldClearP2P() public virtual {
for (uint256 marketIndex; marketIndex < markets.length; ++marketIndex) {
// _revert(); // TODO: re-add as soon as https://github.com/foundry-rs/foundry/issues/3792 is resolved, to avoid sharing state changes with each market test
_revert();

DeltasTest memory test;
test.market = markets[0];
Expand Down Expand Up @@ -72,13 +72,13 @@ contract TestDeltas is TestSetup {
assertApproxEqAbs(
test.p2pSupplyDelta.rayMul(test.indexes.poolSupplyIndex),
p2pSupplyUnderlying,
10,
100,
"p2p supply delta"
);
assertApproxEqAbs(
test.p2pBorrowDelta.rayMul(test.indexes.poolBorrowIndex),
p2pBorrowUnderlying,
10,
100,
"p2p borrow delta"
);
assertEq(test.p2pSupplyAfter, test.p2pSupplyBefore, "p2p supply");
Expand Down Expand Up @@ -108,22 +108,22 @@ contract TestDeltas is TestSetup {
assertApproxEqAbs(
p2pSupplyUnderlying - supplyDeltaUnderlyingBefore,
IAToken(test.market.poolToken).balanceOf(address(morpho)) - test.morphoSupplyBefore,
10,
100,
"morpho pool supply"
);
assertApproxEqAbs(
p2pBorrowUnderlying - borrowDeltaUnderlyingBefore,
IVariableDebtToken(test.market.debtToken).balanceOf(address(morpho)) -
test.morphoBorrowBefore,
10,
100,
"morpho pool borrow"
);
}
}

function testShouldNotClearP2PWhenFullDelta() public virtual {
for (uint256 marketIndex; marketIndex < markets.length; ++marketIndex) {
// _revert(); // TODO: re-add as soon as https://github.com/foundry-rs/foundry/issues/3792 is resolved, to avoid sharing state changes with each market test
_revert();

DeltasTest memory test;
test.market = markets[0];
Expand Down
6 changes: 3 additions & 3 deletions test/prod/aave-v2/TestLifecycle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ contract TestLifecycle is TestSetup {
assertApproxEqAbs(
supply.position.total,
supply.amount,
1,
2,
string.concat(supply.market.symbol, " total supply")
);
if (supply.p2pDisabled)
Expand Down Expand Up @@ -178,7 +178,7 @@ contract TestLifecycle is TestSetup {
assertApproxEqAbs(
borrow.position.total,
borrow.amount,
1,
2,
string.concat(borrow.market.symbol, " total borrow")
);
if (borrow.p2pDisabled)
Expand Down Expand Up @@ -332,7 +332,7 @@ contract TestLifecycle is TestSetup {
oracle.getAssetPrice(supplyMarket.underlying),
supplyMarket.decimals,
supplyMarket.ltv
).wadMul(1.001 ether);
).wadMul(1.01 ether);

MarketSideTest memory supply = _supply(supplyMarket, supplyAmount);
_testSupply(supply);
Expand Down
6 changes: 3 additions & 3 deletions test/prod/aave-v2/TestUpgradeLens.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ contract TestUpgradeLens is TestSetup {
oracle.getAssetPrice(supplyMarket.underlying),
supplyMarket.decimals,
supplyMarket.ltv
).wadMul(1.001 ether);
).wadMul(1.01 ether);

(uint256 expectedSupplyRate, , , ) = lens.getNextUserSupplyRatePerYear(
supplyMarket.poolToken,
Expand All @@ -91,7 +91,7 @@ contract TestUpgradeLens is TestSetup {
assertApproxEqAbs(
lens.getCurrentUserSupplyRatePerYear(supplyMarket.poolToken, address(user)),
expectedSupplyRate,
1e15,
1e16,
string.concat(supplyMarket.symbol, " supply rate")
);

Expand All @@ -108,7 +108,7 @@ contract TestUpgradeLens is TestSetup {
assertApproxEqAbs(
lens.getCurrentUserBorrowRatePerYear(borrowMarket.poolToken, address(user)),
expectedBorrowRate,
1e15,
1e16,
string.concat(borrowMarket.symbol, " borrow rate")
);
}
Expand Down
12 changes: 4 additions & 8 deletions test/prod/aave-v2/setup/TestSetup.sol
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.0;

import "../../ProdTest.sol";

import "src/aave-v2/interfaces/aave/IVariableDebtToken.sol";
import "src/aave-v2/interfaces/aave/IAToken.sol";
import "src/aave-v2/interfaces/lido/ILido.sol";

import {ReserveConfiguration} from "src/aave-v2/libraries/aave/ReserveConfiguration.sol";
import "@morpho-dao/morpho-utils/math/WadRayMath.sol";
import "@morpho-dao/morpho-utils/math/PercentageMath.sol";
import "@rari-capital/solmate/src/utils/SafeTransferLib.sol";
import "@morpho-dao/morpho-utils/math/Math.sol";

import {InterestRatesManager} from "src/aave-v2/InterestRatesManager.sol";
import {MatchingEngine} from "src/aave-v2/MatchingEngine.sol";
Expand All @@ -20,10 +18,8 @@ import "src/aave-v2/Morpho.sol";

import {User} from "../../../aave-v2/helpers/User.sol";
import "config/aave-v2/Config.sol";
import "@forge-std/console.sol";
import "@forge-std/Test.sol";

contract TestSetup is Config, Test {
contract TestSetup is Config, ProdTest {
using ReserveConfiguration for DataTypes.ReserveConfigurationMap;
using WadRayMath for uint256;
using PercentageMath for uint256;
Expand Down Expand Up @@ -81,7 +77,7 @@ contract TestSetup is Config, Test {

vm.label(address(user), "User");

deal(aave, address(this), type(uint256).max);
deal(aave, address(this), type(uint104).max);
deal(dai, address(this), type(uint256).max);
deal(usdc, address(this), type(uint256).max);
deal(usdt, address(this), type(uint256).max);
Expand Down
12 changes: 6 additions & 6 deletions test/prod/compound/TestDeltas.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ contract TestDeltas is TestSetup {

function testShouldClearP2P() public virtual {
for (uint256 marketIndex; marketIndex < markets.length; ++marketIndex) {
// _revert(); // TODO: re-add as soon as https://github.com/foundry-rs/foundry/issues/3792 is resolved, to avoid sharing state changes with each market test
_revert();

DeltasTest memory test;
test.market = markets[marketIndex];
Expand Down Expand Up @@ -76,13 +76,13 @@ contract TestDeltas is TestSetup {
assertApproxEqAbs(
test.p2pSupplyDelta.mul(test.indexes.poolSupplyIndex),
p2pSupplyUnderlying,
10**(test.market.decimals / 2 + 1),
10**(test.market.decimals / 2 + 2),
"p2p supply delta"
);
assertApproxEqAbs(
test.p2pBorrowDelta.mul(test.indexes.poolBorrowIndex),
p2pBorrowUnderlying,
10,
100,
"p2p borrow delta"
);
assertEq(test.p2pSupplyAfter, test.p2pSupplyBefore, "p2p supply");
Expand Down Expand Up @@ -112,22 +112,22 @@ contract TestDeltas is TestSetup {
p2pSupplyUnderlying - supplyDeltaUnderlyingBefore,
ICToken(test.market.poolToken).balanceOfUnderlying(address(morpho)) -
test.morphoSupplyBefore,
10**(test.market.decimals / 2 + 1),
10**(test.market.decimals / 2 + 2),
"morpho pool supply"
);
assertApproxEqAbs(
p2pBorrowUnderlying - borrowDeltaUnderlyingBefore,
ICToken(test.market.poolToken).borrowBalanceCurrent(address(morpho)) -
test.morphoBorrowBefore,
10,
100,
"morpho pool borrow"
);
}
}

function testShouldNotClearP2PWhenFullDelta() public virtual {
for (uint256 marketIndex; marketIndex < markets.length; ++marketIndex) {
// _revert(); // TODO: re-add as soon as https://github.com/foundry-rs/foundry/issues/3792 is resolved, to avoid sharing state changes with each market test
_revert();

DeltasTest memory test;
test.market = markets[marketIndex];
Expand Down
10 changes: 4 additions & 6 deletions test/prod/compound/TestLifecycle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,9 @@ contract TestLifecycle is TestSetup {
0,
string.concat(supply.market.symbol, " balance after supply")
);
assertApproxEqAbs(
supply.position.total,
assertGe(
supply.amount,
1,
supply.position.total,
string.concat(supply.market.symbol, " total supply")
);
if (supply.p2pDisabled)
Expand Down Expand Up @@ -143,10 +142,9 @@ contract TestLifecycle is TestSetup {
supply.morphoUnderlyingBalanceBefore,
string.concat(supply.market.symbol, " morpho balance")
);
assertApproxEqAbs(
assertGe(
ICToken(supply.market.poolToken).balanceOfUnderlying(address(morpho)),
supply.morphoPoolSupplyBefore + supply.position.pool,
10,
string.concat(supply.market.symbol, " morpho pool supply")
);
assertApproxEqAbs(
Expand Down Expand Up @@ -401,7 +399,7 @@ contract TestLifecycle is TestSetup {
borrowedPrice,
oracle.getUnderlyingPrice(supplyMarket.poolToken),
supplyMarket.collateralFactor
).mul(1.001 ether);
).mul(1.01 ether);

MarketSideTest memory supply = _supply(supplyMarket, supplyAmount);
_testSupply(supply);
Expand Down
6 changes: 3 additions & 3 deletions test/prod/compound/TestUpgradeLens.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ contract TestUpgradeLens is TestSetup {
borrowedPrice,
oracle.getUnderlyingPrice(supplyMarket.poolToken),
supplyMarket.collateralFactor
).mul(1.001 ether);
).mul(1.01 ether);

(uint256 expectedSupplyRate, , , ) = lens.getNextUserSupplyRatePerBlock(
supplyMarket.poolToken,
Expand All @@ -124,7 +124,7 @@ contract TestUpgradeLens is TestSetup {
assertApproxEqAbs(
lens.getCurrentUserSupplyRatePerBlock(supplyMarket.poolToken, address(user)),
expectedSupplyRate,
1,
1e16,
QGarchery marked this conversation as resolved.
Show resolved Hide resolved
string.concat(supplyMarket.symbol, " supply rate")
);

Expand All @@ -141,7 +141,7 @@ contract TestUpgradeLens is TestSetup {
assertApproxEqAbs(
lens.getCurrentUserBorrowRatePerBlock(borrowMarket.poolToken, address(user)),
expectedBorrowRate,
1,
1e16,
string.concat(borrowMarket.symbol, " borrow rate")
);
}
Expand Down
11 changes: 4 additions & 7 deletions test/prod/compound/setup/TestSetup.sol
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.0;

import "../../ProdTest.sol";

import {CompoundMath} from "@morpho-dao/morpho-utils/math/CompoundMath.sol";
import {PercentageMath} from "@morpho-dao/morpho-utils/math/PercentageMath.sol";
import {SafeTransferLib, ERC20} from "@rari-capital/solmate/src/utils/SafeTransferLib.sol";
import {Math} from "@morpho-dao/morpho-utils/math/Math.sol";
import {Types} from "src/compound/libraries/Types.sol";

import {PositionsManager} from "src/compound/PositionsManager.sol";
import {InterestRatesManager} from "src/compound/InterestRatesManager.sol";

import {User} from "../../../compound/helpers/User.sol";
import "config/compound/Config.sol";
import "@forge-std/console.sol";
import "@forge-std/Test.sol";

contract TestSetup is Config, Test {
contract TestSetup is Config, ProdTest {
using CompoundMath for uint256;
using PercentageMath for uint256;
using SafeTransferLib for ERC20;
Expand Down Expand Up @@ -72,7 +69,7 @@ contract TestSetup is Config, Test {

vm.label(address(user), "User");

deal(aave, address(this), type(uint256).max);
deal(aave, address(this), type(uint104).max);
deal(dai, address(this), type(uint256).max);
deal(usdc, address(this), type(uint256).max);
deal(usdt, address(this), type(uint256).max);
Expand Down
Loading