Skip to content

Commit

Permalink
Merge pull request #66 from morpho-org/test/metamorpho-mock
Browse files Browse the repository at this point in the history
test: add metamorpho mock
  • Loading branch information
MathisGD authored Dec 19, 2024
2 parents e3cb903 + e7e2ba1 commit 5a0717b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 32 deletions.
27 changes: 27 additions & 0 deletions src/mocks/MetaMorphoMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.26;

import "../MetaMorphoV1_1.sol";

contract MetaMorphoMock is MetaMorphoV1_1 {
constructor(
address owner,
address morpho,
uint256 initialTimelock,
address _asset,
string memory __name,
string memory __symbol
) MetaMorphoV1_1(owner, morpho, initialTimelock, _asset, __name, __symbol) {}

function mockSetCap(MarketParams memory marketParams, Id id, uint184 supplyCap) external {
_setCap(marketParams, id, supplyCap);
}

function mockSimulateWithdrawMorpho(uint256 assets) external view returns (uint256) {
return _simulateWithdrawMorpho(assets);
}

function mockSetSupplyQueue(Id[] memory ids) external {
supplyQueue = ids;
}
}
42 changes: 33 additions & 9 deletions test/forge/MetaMorphoInternalTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,58 @@ pragma solidity ^0.8.0;
import {UtilsLib} from "../../lib/morpho-blue/src/libraries/UtilsLib.sol";
import {SharesMathLib} from "../../lib/morpho-blue/src/libraries/SharesMathLib.sol";

import "./helpers/InternalTest.sol";
import "./helpers/BaseTest.sol";
import {MetaMorphoMock} from "../../src/mocks/MetaMorphoMock.sol";

contract MetaMorphoInternalTest is InternalTest {
contract MetaMorphoInternalTest is BaseTest {
using MathLib for uint256;
using MorphoLib for IMorpho;
using MorphoBalancesLib for IMorpho;
using MarketParamsLib for MarketParams;
using SharesMathLib for uint256;
using UtilsLib for uint256;

MetaMorphoMock internal metaMorphoMock;

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

metaMorphoMock =
new MetaMorphoMock(OWNER, address(morpho), 1 days, address(loanToken), "MetaMorpho Vault", "MM");

vm.startPrank(OWNER);
metaMorphoMock.setCurator(CURATOR);
metaMorphoMock.setIsAllocator(ALLOCATOR, true);
vm.stopPrank();

vm.startPrank(SUPPLIER);
loanToken.approve(address(metaMorphoMock), type(uint256).max);
collateralToken.approve(address(metaMorphoMock), type(uint256).max);
vm.stopPrank();
}

function testSetCapMaxQueueLengthExcedeed() public {
for (uint256 i; i < NB_MARKETS - 1; ++i) {
_setCap(allMarkets[i], allMarkets[i].id(), CAP);
metaMorphoMock.mockSetCap(allMarkets[i], allMarkets[i].id(), CAP);
}

vm.expectRevert(ErrorsLib.MaxQueueLengthExceeded.selector);
_setCap(allMarkets[NB_MARKETS - 1], allMarkets[NB_MARKETS - 1].id(), CAP);
metaMorphoMock.mockSetCap(allMarkets[NB_MARKETS - 1], allMarkets[NB_MARKETS - 1].id(), CAP);
}

function testSimulateWithdraw(uint256 suppliedAmount, uint256 borrowedAmount, uint256 assets) public {
suppliedAmount = bound(suppliedAmount, MIN_TEST_ASSETS, MAX_TEST_ASSETS);
borrowedAmount = bound(borrowedAmount, MIN_TEST_ASSETS, suppliedAmount);

_setCap(allMarkets[0], allMarkets[0].id(), CAP);
supplyQueue = [allMarkets[0].id()];
metaMorphoMock.mockSetCap(allMarkets[0], allMarkets[0].id(), CAP);

Id[] memory ids = new Id[](1);
ids[0] = allMarkets[0].id();
metaMorphoMock.mockSetSupplyQueue(ids);

loanToken.setBalance(SUPPLIER, suppliedAmount);
vm.prank(SUPPLIER);
this.deposit(suppliedAmount, SUPPLIER);
metaMorphoMock.deposit(suppliedAmount, SUPPLIER);

uint256 collateral = suppliedAmount.wDivUp(allMarkets[0].lltv);
collateralToken.setBalance(BORROWER, collateral);
Expand All @@ -42,9 +65,10 @@ contract MetaMorphoInternalTest is InternalTest {
morpho.borrow(allMarkets[0], borrowedAmount, 0, BORROWER, BORROWER);
vm.stopPrank();

uint256 remaining = _simulateWithdrawMorpho(assets);
uint256 remaining = metaMorphoMock.mockSimulateWithdrawMorpho(assets);

uint256 expectedWithdrawable = MORPHO.expectedSupplyAssets(allMarkets[0], address(this)) - borrowedAmount;
uint256 expectedWithdrawable =
morpho.expectedSupplyAssets(allMarkets[0], address(metaMorphoMock)) - borrowedAmount;
uint256 expectedRemaining = assets.zeroFloorSub(expectedWithdrawable);

assertEq(remaining, expectedRemaining, "remaining");
Expand Down
23 changes: 0 additions & 23 deletions test/forge/helpers/InternalTest.sol

This file was deleted.

0 comments on commit 5a0717b

Please sign in to comment.