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

Separate fork tests from integration tests #122

Merged
merged 5 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions src/interfaces/IMorpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {Types} from "../libraries/Types.sol";
interface IMorphoGetters {
function POOL() external view returns (address);
function ADDRESSES_PROVIDER() external view returns (address);
function DOMAIN_SEPARATOR() external view returns (bytes32);

function market(address underlying) external view returns (Types.Market memory);
function marketsCreated() external view returns (address[] memory);

Expand All @@ -15,6 +17,9 @@ interface IMorphoGetters {
function scaledPoolBorrowBalance(address underlying, address user) external view returns (uint256);
function scaledPoolSupplyBalance(address underlying, address user) external view returns (uint256);

function isManaging(address owner, address manager) external view returns (bool);
function userNonce(address user) external view returns (uint256);

function maxSortedUsers() external view returns (uint256);
function defaultMaxLoops() external view returns (Types.MaxLoops memory);
function positionsManager() external view returns (address);
Expand Down
90 changes: 42 additions & 48 deletions test/TestApproval.sol
Original file line number Diff line number Diff line change
@@ -1,56 +1,50 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.17;

import {Morpho} from "../src/Morpho.sol";
import {Errors} from "../src/libraries/Errors.sol";
import "./helpers/SigUtils.sol";
import "./setup/TestSetup.sol";

contract TestApproval is TestSetup, Morpho {
using TestConfig for TestConfig.Config;
import {Morpho} from "../src/Morpho.sol";

SigUtils internal sigUtils;
uint256 internal ownerPrivateKey;
uint256 internal managerPrivateKey;
address internal ownerAdd;
address internal managerAdd;
import {SigUtils} from "./helpers/SigUtils.sol";
import "./helpers/IntegrationTest.sol";

constructor() Morpho(config.load(vm.envString("NETWORK")).getAddress("addressesProvider")) {}
contract TestApproval is IntegrationTest {
uint256 internal constant OWNER_PK = 0xA11CE;
uint256 internal constant MANAGER_PK = 0xB0B;

function setUp() public override {
super.setUp();
address internal immutable OWNER = vm.addr(OWNER_PK);
address internal immutable MANAGER = vm.addr(MANAGER_PK);

sigUtils = new SigUtils(this.DOMAIN_SEPARATOR());
SigUtils internal sigUtils;

ownerPrivateKey = 0xA11CE;
managerPrivateKey = 0xB0B;
function setUp() public override {
super.setUp();

ownerAdd = vm.addr(ownerPrivateKey);
managerAdd = vm.addr(managerPrivateKey);
sigUtils = new SigUtils(morpho.DOMAIN_SEPARATOR());
}

function testApproveManager(address owner, address manager, bool isAllowed) public {
vm.prank(owner);
this.approveManager(manager, isAllowed);
assertEq(this.isManaging(owner, manager), isAllowed);
morpho.approveManager(manager, isAllowed);
assertEq(morpho.isManaging(owner, manager), isAllowed);
}

function testApproveManagerWithSig(uint128 deadline) public {
vm.assume(deadline > block.timestamp);

SigUtils.Authorization memory authorization = SigUtils.Authorization({
owner: ownerAdd,
manager: managerAdd,
owner: OWNER,
manager: MANAGER,
isAllowed: true,
nonce: this.userNonce(ownerAdd),
nonce: morpho.userNonce(OWNER),
deadline: block.timestamp + deadline
});

bytes32 digest = sigUtils.getTypedDataHash(authorization);

(uint8 v, bytes32 r, bytes32 s) = vm.sign(ownerPrivateKey, digest);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(OWNER_PK, digest);

this.approveManagerWithSig(
morpho.approveManagerWithSig(
authorization.owner,
authorization.manager,
authorization.isAllowed,
Expand All @@ -61,27 +55,27 @@ contract TestApproval is TestSetup, Morpho {
s
);

assertEq(this.isManaging(ownerAdd, managerAdd), true);
assertEq(this.userNonce(ownerAdd), 1);
assertEq(morpho.isManaging(OWNER, MANAGER), true);
assertEq(morpho.userNonce(OWNER), 1);
}

function testRevertExpiredApproveManagerWithSig(uint128 deadline) public {
vm.assume(deadline <= block.timestamp);

SigUtils.Authorization memory authorization = SigUtils.Authorization({
owner: ownerAdd,
manager: managerAdd,
owner: OWNER,
manager: MANAGER,
isAllowed: true,
nonce: this.userNonce(ownerAdd),
nonce: morpho.userNonce(OWNER),
deadline: deadline
});

bytes32 digest = sigUtils.getTypedDataHash(authorization);

(uint8 v, bytes32 r, bytes32 s) = vm.sign(ownerPrivateKey, digest);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(OWNER_PK, digest);

vm.expectRevert(abi.encodeWithSelector(Errors.SignatureExpired.selector));
this.approveManagerWithSig(
morpho.approveManagerWithSig(
authorization.owner,
authorization.manager,
authorization.isAllowed,
Expand All @@ -95,19 +89,19 @@ contract TestApproval is TestSetup, Morpho {

function testRevertInvalidSignatoryApproveManagerWithSig() public {
SigUtils.Authorization memory authorization = SigUtils.Authorization({
owner: ownerAdd,
manager: managerAdd,
owner: OWNER,
manager: MANAGER,
isAllowed: true,
nonce: this.userNonce(ownerAdd),
nonce: morpho.userNonce(OWNER),
deadline: block.timestamp + 1 days
});

bytes32 digest = sigUtils.getTypedDataHash(authorization);

(uint8 v, bytes32 r, bytes32 s) = vm.sign(managerPrivateKey, digest); // manager signs owner's approval.
(uint8 v, bytes32 r, bytes32 s) = vm.sign(MANAGER_PK, digest); // manager signs owner's approval.

vm.expectRevert(abi.encodeWithSelector(Errors.InvalidSignatory.selector));
this.approveManagerWithSig(
morpho.approveManagerWithSig(
authorization.owner,
authorization.manager,
authorization.isAllowed,
Expand All @@ -120,22 +114,22 @@ contract TestApproval is TestSetup, Morpho {
}

function testRevertInvalidNonceApproveManagerWithSig(uint256 nonce) public {
vm.assume(nonce != this.userNonce(ownerAdd));
vm.assume(nonce != morpho.userNonce(OWNER));

SigUtils.Authorization memory authorization = SigUtils.Authorization({
owner: ownerAdd,
manager: managerAdd,
owner: OWNER,
manager: MANAGER,
isAllowed: true,
nonce: nonce,
deadline: block.timestamp + 1 days
});

bytes32 digest = sigUtils.getTypedDataHash(authorization);

(uint8 v, bytes32 r, bytes32 s) = vm.sign(ownerPrivateKey, digest);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(OWNER_PK, digest);

vm.expectRevert(abi.encodeWithSelector(Errors.InvalidNonce.selector));
this.approveManagerWithSig(
morpho.approveManagerWithSig(
authorization.owner,
authorization.manager,
authorization.isAllowed,
Expand All @@ -149,18 +143,18 @@ contract TestApproval is TestSetup, Morpho {

function testRevertSignatureReplayApproveManagerWithSig() public {
SigUtils.Authorization memory authorization = SigUtils.Authorization({
owner: ownerAdd,
manager: managerAdd,
owner: OWNER,
manager: MANAGER,
isAllowed: true,
nonce: this.userNonce(ownerAdd),
nonce: morpho.userNonce(OWNER),
deadline: block.timestamp + 1 days
});

bytes32 digest = sigUtils.getTypedDataHash(authorization);

(uint8 v, bytes32 r, bytes32 s) = vm.sign(ownerPrivateKey, digest);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(OWNER_PK, digest);

this.approveManagerWithSig(
morpho.approveManagerWithSig(
authorization.owner,
authorization.manager,
authorization.isAllowed,
Expand All @@ -172,7 +166,7 @@ contract TestApproval is TestSetup, Morpho {
);

vm.expectRevert(abi.encodeWithSelector(Errors.InvalidNonce.selector));
this.approveManagerWithSig(
morpho.approveManagerWithSig(
authorization.owner,
authorization.manager,
authorization.isAllowed,
Expand Down
15 changes: 4 additions & 11 deletions test/TestMatchingEngine.sol
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.17;

import {TestConfig} from "./helpers/TestConfig.sol";
import {TestSetup} from "./setup/TestSetup.sol";

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

import {MatchingEngine} from "../src/MatchingEngine.sol";
import {MorphoInternal} from "../src/MorphoInternal.sol";
import {MorphoStorage} from "../src/MorphoStorage.sol";

import {Types} from "../src/libraries/Types.sol";
import {MarketLib} from "../src/libraries/MarketLib.sol";
import {MarketBalanceLib} from "../src/libraries/MarketBalanceLib.sol";

contract TestMatchingEngine is TestSetup, MatchingEngine {
import "./helpers/InternalTest.sol";

contract TestMatchingEngine is InternalTest, MatchingEngine {
using MarketLib for Types.Market;
using MarketBalanceLib for Types.MarketBalances;
using WadRayMath for uint256;
using TestConfig for TestConfig.Config;
using Math for uint256;

uint256 internal constant TOTAL_AMOUNT = 20 ether;
uint256 internal constant USER_AMOUNT = 1 ether;

constructor() TestSetup() MorphoStorage(config.load(vm.envString("NETWORK")).getAddress("addressesProvider")) {}

function setUp() public virtual override {
super.setUp();

_market[dai].setIndexes(
Types.Indexes256(
Types.MarketSideIndexes256(WadRayMath.RAY, WadRayMath.RAY),
Expand Down
8 changes: 2 additions & 6 deletions test/TestMorphoGetters.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ pragma solidity ^0.8.17;
import {MorphoStorage} from "../src/MorphoStorage.sol";
import {MorphoGetters} from "../src/MorphoGetters.sol";

import "./setup/TestSetup.sol";

contract TestMorphoGetters is TestSetup, MorphoGetters {
using TestConfig for TestConfig.Config;

constructor() MorphoStorage(config.load(vm.envString("NETWORK")).getAddress("addressesProvider")) {}
import "./helpers/InternalTest.sol";

contract TestMorphoGetters is InternalTest, MorphoGetters {
function testIsManaging(address owner, address manager, bool isAllowed) public {
_approveManager(owner, manager, isAllowed);
assertEq(this.isManaging(owner, manager), isAllowed);
Expand Down
61 changes: 28 additions & 33 deletions test/TestMorphoInternal.sol
Original file line number Diff line number Diff line change
@@ -1,47 +1,37 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.17;

import {TestHelpers} from "./helpers/TestHelpers.sol";
import {TestConfig} from "./helpers/TestConfig.sol";

import {TestSetup} from "./setup/TestSetup.sol";
import {console2} from "@forge-std/console2.sol";

import {WadRayMath} from "@morpho-utils/math/WadRayMath.sol";
import {PercentageMath} from "@morpho-utils/math/PercentageMath.sol";
import {ThreeHeapOrdering} from "@morpho-data-structures/ThreeHeapOrdering.sol";

import {SafeTransferLib, ERC20} from "@solmate/utils/SafeTransferLib.sol";
import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";

import {IPool, IPoolAddressesProvider} from "../src/interfaces/aave/IPool.sol";
import {IPriceOracleGetter} from "@aave/core-v3/contracts/interfaces/IPriceOracleGetter.sol";
import {DataTypes} from "../src/libraries/aave/DataTypes.sol";
import {ReserveConfiguration} from "../src/libraries/aave/ReserveConfiguration.sol";

import {MorphoInternal, MorphoStorage} from "../src/MorphoInternal.sol";
import {Types} from "../src/libraries/Types.sol";
import {MarketLib} from "../src/libraries/MarketLib.sol";
import {MarketBalanceLib} from "../src/libraries/MarketBalanceLib.sol";
import {PoolLib} from "../src/libraries/PoolLib.sol";

contract TestMorphoInternal is TestSetup, MorphoInternal {
import "./helpers/InternalTest.sol";

contract TestMorphoInternal is InternalTest, MorphoInternal {
using MarketLib for Types.Market;
using MarketBalanceLib for Types.MarketBalances;
using PoolLib for IPool;
using WadRayMath for uint256;
using PercentageMath for uint256;
using SafeTransferLib for ERC20;
using ThreeHeapOrdering for ThreeHeapOrdering.HeapArray;
using TestConfig for TestConfig.Config;
using ReserveConfiguration for DataTypes.ReserveConfigurationMap;
using EnumerableSet for EnumerableSet.AddressSet;

IPriceOracleGetter internal oracle;

constructor() TestSetup() MorphoStorage(config.load(vm.envString("NETWORK")).getAddress("addressesProvider")) {}

function setUp() public virtual override {
super.setUp();

_defaultMaxLoops = Types.MaxLoops(10, 10, 10, 10);
_maxSortedUsers = 20;

Expand All @@ -51,7 +41,6 @@ contract TestMorphoInternal is TestSetup, MorphoInternal {
createTestMarket(usdt, 0, 3_333);
createTestMarket(wNative, 0, 3_333);

fillBalance(address(this), type(uint256).max);
ERC20(dai).approve(address(_POOL), type(uint256).max);
ERC20(wbtc).approve(address(_POOL), type(uint256).max);
ERC20(usdc).approve(address(_POOL), type(uint256).max);
Expand Down Expand Up @@ -449,23 +438,29 @@ contract TestMorphoInternal is TestSetup, MorphoInternal {
}

function testSetPauseStatus() public {
Types.PauseStatuses storage pauseStatuses = _market[dai].pauseStatuses;

assertFalse(pauseStatuses.isSupplyPaused);
assertFalse(pauseStatuses.isBorrowPaused);
assertFalse(pauseStatuses.isRepayPaused);
assertFalse(pauseStatuses.isWithdrawPaused);
assertFalse(pauseStatuses.isLiquidateCollateralPaused);
assertFalse(pauseStatuses.isLiquidateBorrowPaused);

_setPauseStatus(dai, true);

assertTrue(pauseStatuses.isSupplyPaused);
assertTrue(pauseStatuses.isBorrowPaused);
assertTrue(pauseStatuses.isRepayPaused);
assertTrue(pauseStatuses.isWithdrawPaused);
assertTrue(pauseStatuses.isLiquidateCollateralPaused);
assertTrue(pauseStatuses.isLiquidateBorrowPaused);
for (uint256 marketIndex; marketIndex < testMarkets.length; ++marketIndex) {
_revert();

address underlying = testMarkets[marketIndex];

Types.PauseStatuses storage pauseStatuses = _market[underlying].pauseStatuses;

assertFalse(pauseStatuses.isSupplyPaused);
assertFalse(pauseStatuses.isBorrowPaused);
assertFalse(pauseStatuses.isRepayPaused);
assertFalse(pauseStatuses.isWithdrawPaused);
assertFalse(pauseStatuses.isLiquidateCollateralPaused);
assertFalse(pauseStatuses.isLiquidateBorrowPaused);

_setPauseStatus(underlying, true);

assertTrue(pauseStatuses.isSupplyPaused);
assertTrue(pauseStatuses.isBorrowPaused);
assertTrue(pauseStatuses.isRepayPaused);
assertTrue(pauseStatuses.isWithdrawPaused);
assertTrue(pauseStatuses.isLiquidateCollateralPaused);
assertTrue(pauseStatuses.isLiquidateBorrowPaused);
}
}

function testApproveManager(address owner, address manager, bool isAllowed) public {
Expand Down
8 changes: 2 additions & 6 deletions test/TestPositionsManagerInternal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ import {Errors} from "../src/libraries/Errors.sol";
import {MorphoStorage} from "../src/MorphoStorage.sol";
import {PositionsManagerInternal} from "../src/PositionsManagerInternal.sol";

import "./setup/TestSetup.sol";

contract TestPositionsManager is TestSetup, PositionsManagerInternal {
using TestConfig for TestConfig.Config;

constructor() MorphoStorage(config.load(vm.envString("NETWORK")).getAddress("addressesProvider")) {}
import "./helpers/InternalTest.sol";

contract TestPositionsManager is InternalTest, PositionsManagerInternal {
function testValidatePermission(address owner, address manager) public {
_validatePermission(owner, owner);

Expand Down
Loading