Skip to content

Commit

Permalink
test(simulation): make handler tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubilmax committed Oct 8, 2024
1 parent 4ed13d4 commit 17dd903
Show file tree
Hide file tree
Showing 14 changed files with 471 additions and 329 deletions.
6 changes: 3 additions & 3 deletions packages/blue-sdk-ethers/test/e2e/Market.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Wallet, toBigInt } from "ethers";
import { MorphoBlue__factory } from "ethers-types";
import { ethers } from "hardhat";

import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers";
import type { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers";
import { setCode, time } from "@nomicfoundation/hardhat-network-helpers";
import { setNextBlockTimestamp } from "@nomicfoundation/hardhat-network-helpers/dist/src/helpers/time";

Expand Down Expand Up @@ -61,7 +61,7 @@ describe("augment/Market", () => {

it("should fetch price and rate if idle market", async () => {
const expectedData = {
config: MAINNET_MARKETS.idle_usdc,
config: MAINNET_MARKETS.usdc_idle,
totalSupplyAssets: 0n,
totalSupplyShares: 0n,
totalBorrowAssets: 0n,
Expand All @@ -72,7 +72,7 @@ describe("augment/Market", () => {
rateAtTarget: undefined,
};

const value = await Market.fetch(MAINNET_MARKETS.idle_usdc.id, signer);
const value = await Market.fetch(MAINNET_MARKETS.usdc_idle.id, signer);

expect(value).to.eql(expectedData);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,19 @@ export const handleMetaMorphoDepositOperation: OperationHandler<

let toSupply = assets;
for (const id of vault.supplyQueue) {
const { supplyAssets } = data
.getAccrualPosition(address, id)
.accrueInterest(data.block.timestamp);
const { cap } = data.getVaultMarketConfig(address, id);
if (cap === 0n) continue;

handleBlueOperation(
{
type: "Blue_AccrueInterest",
sender: address,
args: { id },
},
data,
);

const { supplyAssets } = data.getAccrualPosition(address, id);

const suppliable = MathLib.zeroFloorSub(cap, supplyAssets);
if (suppliable === 0n) continue;
Expand All @@ -86,7 +95,7 @@ export const handleMetaMorphoDepositOperation: OperationHandler<
handleBlueOperation(
{
type: "Blue_Supply",
sender: zeroAddress, // Bypass the vault balance check.
sender: address,
args: {
id,
assets: toSupplyInMarket,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const handleMetaMorphoWithdrawOperation: OperationHandler<
address: vault.config.asset,
args: {
amount: assets,
from: zeroAddress, // Bypass the vault balance check.
from: address,
to: receiver,
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,84 @@
import _omit from "lodash/omit";

import { ChainId, addresses } from "@morpho-org/blue-sdk";

import { blueAbi } from "@morpho-org/blue-sdk-viem";
import { markets } from "@morpho-org/morpho-test";
import { getLast } from "@morpho-org/morpho-ts";
import { renderHook, waitFor } from "@morpho-org/test";
import { describe, expect } from "vitest";
import { type Operation, simulateOperations } from "../../../../src";
import { test } from "../../setup";
import {
type MinimalBlock,
simulateOperations,
useSimulationState,
} from "../../../../src/index.js";
import { test } from "../../setup.js";

const { morpho } = addresses[ChainId.EthMainnet];
const { usdc_wstEth } = markets[ChainId.EthMainnet];

describe("Blue_AccrueInterest", () => {
test("should accrue interest accurately", async ({ client }) => {
const operations: Operation[] = [
{
type: "Blue_AccrueInterest",
sender: client.account.address,
args: {
id: usdc_wstEth.id,
},
},
];
test("should accrue interest accurately", async ({
wagmi: { config, client },
}) => {
const block = await client.getBlock();

const { value: dataBefore } = await simulationService.data;
const { result, rerender } = await renderHook(
config,
(block: MinimalBlock) =>
useSimulationState({
marketIds: [usdc_wstEth.id],
users: [],
tokens: [],
vaults: [],
block,
accrueInterest: false,
}),
{ initialProps: block },
);

const steps = simulateOperations(operations, dataBefore);
await waitFor(() => expect(result.current.isFetchingAny).toBeFalsy());

expect(steps.length).to.equal(2);
const dataBefore = result.current.data!;

await client.setNextBlockTimestamp(dataBefore.timestamp);
dataBefore.block.number += 1n;
dataBefore.block.timestamp += 1n;

await MorphoBlue__factory.connect(morpho, signer).accrueInterest(
usdc_wstEth,
const steps = simulateOperations(
[
{
type: "Blue_AccrueInterest",
sender: client.account.address,
args: {
id: usdc_wstEth.id,
},
},
],
dataBefore,
);
await mine(0);

const expected = getLast(steps);
const { value: data } = await simulationService.data;
expect(steps.length).toBe(2);

await client.setNextBlockTimestamp({
timestamp: dataBefore.block.timestamp,
});

await client.writeContractWait({
address: morpho,
abi: blueAbi,
functionName: "accrueInterest",
args: [
{
collateralToken: usdc_wstEth.collateralToken,
loanToken: usdc_wstEth.loanToken,
oracle: usdc_wstEth.oracle,
irm: usdc_wstEth.irm,
lltv: usdc_wstEth.lltv,
},
],
});

expected.blockNumber += 1n;
await rerender(await client.getBlock());
await waitFor(() => expect(result.current.isFetchingAny).toBeFalsy());

expect(_omit(data, "cacheId")).to.eql(_omit(expected, "cacheId"));
expect(result.current.data).toStrictEqual(getLast(steps));
});
});
Original file line number Diff line number Diff line change
@@ -1,88 +1,99 @@
import { expect } from "chai";
import { parseUnits } from "ethers";
import { ERC20__factory, MetaMorpho__factory } from "ethers-types";
import { ethers } from "hardhat";
import { deal } from "hardhat-deal";
import _omit from "lodash/omit";

import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers";
import { setNextBlockTimestamp } from "@nomicfoundation/hardhat-network-helpers/dist/src/helpers/time";

import { BlueService, ChainService, getLast } from "@morpho-org/blue-core-sdk";
import { MetaMorphoService } from "@morpho-org/blue-metamorpho-sdk";
import { mine, setUp } from "@morpho-org/morpho-test";

import { SimulationService, simulateOperations } from "../../../../src";
import { steakUsdc } from "../../fixtures";
import { renderHook, waitFor } from "@morpho-org/test";
import { describe, expect } from "vitest";

import { ChainId } from "@morpho-org/blue-sdk";
import { metaMorphoAbi } from "@morpho-org/blue-sdk-viem";
import { markets, vaults } from "@morpho-org/morpho-test";
import { getLast } from "@morpho-org/morpho-ts";
import { erc20Abi, parseUnits } from "viem";
import {
type MinimalBlock,
simulateOperations,
useSimulationState,
} from "../../../../src/index.js";
import { test } from "../../setup.js";

const { usdc_wstEth, usdc_idle, usdc_wbtc, usdc_wbIB01 } =
markets[ChainId.EthMainnet];
const { steakUsdc } = vaults[ChainId.EthMainnet];

describe("MetaMorpho_AccrueInterest", () => {
let signer: SignerWithAddress;

let simulationService: SimulationService;

setUp(async () => {
signer = (await ethers.getSigners())[0]!;
});

afterEach(async () => {
simulationService?.chainService.close();
simulationService?.metaMorphoService.blueService.close();
simulationService?.metaMorphoService.close();
simulationService?.close();
});

test("should accrue interest accurately upon deposit", async () => {
test("should accrue interest accurately upon deposit", async ({
wagmi: { config, client },
}) => {
const assets = parseUnits("100", 6);

await deal(steakUsdc.asset, signer.address, assets);

await (
await ERC20__factory.connect(steakUsdc.asset, signer).approve(
steakUsdc.address,
assets,
)
).wait();

simulationService = new SimulationService(
new MetaMorphoService(
new BlueService(new ChainService(signer), {
users: [signer.address],
await client.deal({
erc20: steakUsdc.asset,
recipient: client.account.address,
amount: assets,
});

await client.writeContract({
address: steakUsdc.asset,
abi: erc20Abi,
functionName: "approve",
args: [steakUsdc.address, assets],
});

const block = await client.getBlock();

const { result, rerender } = await renderHook(
config,
(block: MinimalBlock) =>
useSimulationState({
marketIds: [
usdc_wstEth.id,
usdc_idle.id,
usdc_wbtc.id,
usdc_wbIB01.id,
],
users: [client.account.address, steakUsdc.address],
tokens: [steakUsdc.asset],
vaults: [steakUsdc.address],
block,
accrueInterest: false,
}),
{ vaults: [steakUsdc.address] },
),
{ initialProps: block },
);

const { value: dataBefore } = await simulationService.data;
await waitFor(() => expect(result.current.isFetchingAny).toBeFalsy());

const dataBefore = result.current.data!;

dataBefore.block.number += 1n;
dataBefore.block.timestamp += 1n;

const steps = simulateOperations(
[
{
type: "MetaMorpho_Deposit",
sender: signer.address,
sender: client.account.address,
address: steakUsdc.address,
args: {
assets,
owner: signer.address,
owner: client.account.address,
},
},
],
dataBefore,
);

expect(steps.length).to.equal(2);

await setNextBlockTimestamp(dataBefore.timestamp);
await MetaMorpho__factory.connect(steakUsdc.address, signer).deposit(
assets,
signer.address,
);
await mine(0);
expect(steps.length).toBe(2);

const { value: data } = await simulationService.data;
await client.setNextBlockTimestamp({
timestamp: dataBefore.block.timestamp,
});
await client.writeContractWait({
address: steakUsdc.address,
abi: metaMorphoAbi,
functionName: "deposit",
args: [assets, client.account.address],
});

const expected = getLast(steps);
expected.blockNumber += 1n;
await rerender(await client.getBlock());
await waitFor(() => expect(result.current.isFetchingAny).toBeFalsy());

expect(_omit(data, "cacheId")).to.eql(_omit(expected, "cacheId"));
expect(result.current.data).toStrictEqual(getLast(steps));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { MetaMorpho__factory, PublicAllocator__factory } from "ethers-types";
import { ethers } from "hardhat";
import _omit from "lodash/omit";

import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers";
import type { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers";
import { setNextBlockTimestamp } from "@nomicfoundation/hardhat-network-helpers/dist/src/helpers/time";

import { BlueService, ChainService, getLast } from "@morpho-org/blue-core-sdk";
Expand Down Expand Up @@ -55,7 +55,7 @@ describe("MetaMorpho_PublicReallocate", () => {
},
},
{
id: MAINNET_MARKETS.idle_usdc.id,
id: MAINNET_MARKETS.usdc_idle.id,
caps: {
maxIn: assets,
maxOut: 0n,
Expand All @@ -82,7 +82,7 @@ describe("MetaMorpho_PublicReallocate", () => {
address: steakUsdc.address,
args: {
withdrawals: [{ id: MAINNET_MARKETS.usdc_wstEth.id, assets }],
supplyMarketId: MAINNET_MARKETS.idle_usdc.id,
supplyMarketId: MAINNET_MARKETS.usdc_idle.id,
},
},
],
Expand All @@ -97,7 +97,7 @@ describe("MetaMorpho_PublicReallocate", () => {
.reallocateTo(
steakUsdc.address,
[{ marketParams: MAINNET_MARKETS.usdc_wstEth, amount: assets }],
MAINNET_MARKETS.idle_usdc,
MAINNET_MARKETS.usdc_idle,
{ value: fee },
);
await mine(0);
Expand Down
Loading

0 comments on commit 17dd903

Please sign in to comment.