-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(simulation): make handler tests pass
- Loading branch information
Showing
14 changed files
with
471 additions
and
329 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 63 additions & 25 deletions
88
packages/blue-sdk-viem-simulation/test/e2e/handlers/blue/accrueInterest.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
}); |
135 changes: 73 additions & 62 deletions
135
packages/blue-sdk-viem-simulation/test/e2e/handlers/metamorpho/deposit.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.