@morpho-org/morpho-test 4.1.0-next.0 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -25,6 +25,8 @@
25
25
 
26
26
  Framework-agnostic extension of [`@morpho-org/blue-sdk`](../blue-sdk/) that exports test fixtures useful for E2E tests on forks, and registry-driven helpers (`getE2eRuns`, `getE2eMatrix`) that decide which chains an E2E scenario runs on from the deployments it requires.
27
27
 
28
+ Registry-matrix helpers are pure; fork-setup helpers mutate fork state through an injected `AnvilTestClient`. Shared Midnight fixtures are available separately from `@morpho-org/morpho-test/midnight` and require the optional `@morpho-org/midnight-sdk` peer. Root imports do not load Midnight SDK.
29
+
28
30
  ## Installation
29
31
 
30
32
  ```bash
@@ -1,2 +1,3 @@
1
1
  export * from "./asset.js";
2
2
  export * from "./registryMatrix.js";
3
+ export { installE2eAsset, installE2eOracle, wrapE2eNative } from "./setup.js";
@@ -14,5 +14,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.wrapE2eNative = exports.installE2eOracle = exports.installE2eAsset = void 0;
17
18
  __exportStar(require("./asset.js"), exports);
18
19
  __exportStar(require("./registryMatrix.js"), exports);
20
+ var setup_js_1 = require("./setup.js");
21
+ Object.defineProperty(exports, "installE2eAsset", { enumerable: true, get: function () { return setup_js_1.installE2eAsset; } });
22
+ Object.defineProperty(exports, "installE2eOracle", { enumerable: true, get: function () { return setup_js_1.installE2eOracle; } });
23
+ Object.defineProperty(exports, "wrapE2eNative", { enumerable: true, get: function () { return setup_js_1.wrapE2eNative; } });
@@ -0,0 +1,54 @@
1
+ import { Market, MarketParams, type OfferStruct, type Tree, type TreeSnapshot } from "@morpho-org/midnight-sdk";
2
+ import type { AnvilTestClient } from "@morpho-org/test";
3
+ import { type Address, type Chain, type Hex } from "viem";
4
+ /** Fork addresses of the named E2E wallets. */
5
+ export declare const E2E_MIDNIGHT_WALLETS: {
6
+ readonly A: "0x00000000000000000000000000000000e2E0000A";
7
+ readonly B: "0x00000000000000000000000000000000e2E0000b";
8
+ readonly C: "0x00000000000000000000000000000000e2E0000C";
9
+ readonly D: "0x00000000000000000000000000000000e2e0000d";
10
+ };
11
+ /** Names of the reusable Midnight E2E wallets. */
12
+ export type E2eMidnightWallet = keyof typeof E2E_MIDNIGHT_WALLETS;
13
+ /** Parameters and initial snapshot for an E2E Midnight market. */
14
+ export interface E2eMidnightMarket {
15
+ readonly params: MarketParams;
16
+ readonly marketData: Market;
17
+ }
18
+ /** Thrown when wallet assets are requested without Midnight market parameters. */
19
+ export declare class E2eMidnightMarketRequiredError extends Error {
20
+ constructor();
21
+ }
22
+ /** Installs assets and the fixed-price oracle, then creates an empty Midnight market snapshot. */
23
+ export declare const createE2eMidnightMarket: (client: AnvilTestClient<Chain>, chainId: number, options?: {
24
+ readonly maturity?: bigint;
25
+ }) => Promise<E2eMidnightMarket>;
26
+ /**
27
+ * Clears code and funds gas for named wallets, adding requested assets and approvals to their
28
+ * existing balances and allowances.
29
+ */
30
+ export declare const fundE2eMidnightWallets: (client: AnvilTestClient<Chain>, params: {
31
+ readonly market?: MarketParams;
32
+ readonly wallets: Partial<Record<E2eMidnightWallet, {
33
+ readonly loanAssets?: bigint;
34
+ readonly collateralAssets?: bigint;
35
+ }>>;
36
+ readonly approve?: Address;
37
+ }) => Promise<void>;
38
+ /** Authorizes the SetterRatifier for a maker and marks the tree root ratified. */
39
+ export declare const postE2eSetterTree: (client: AnvilTestClient<Chain>, params: {
40
+ readonly chainId: number;
41
+ readonly maker: Address;
42
+ readonly tree: Tree | Tree<"setter">;
43
+ }) => Promise<void>;
44
+ /** A readonly offer fill with the matching ratifier's data. */
45
+ export interface E2eTakeableOffer {
46
+ readonly units: bigint;
47
+ readonly offer: OfferStruct;
48
+ readonly ratifierData: Hex;
49
+ }
50
+ /** Converts legacy or tagged trees and snapshots into SDK takeable offers using the matching ratifier. */
51
+ export declare const getE2eTakeableOffers: (params: {
52
+ readonly tree: Tree | Tree<"setter"> | Tree<"priceV1"> | Tree<"rateV1"> | TreeSnapshot<"setter"> | TreeSnapshot<"priceV1"> | TreeSnapshot<"rateV1">;
53
+ readonly units: bigint;
54
+ }) => readonly E2eTakeableOffer[];
@@ -0,0 +1,187 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getE2eTakeableOffers = exports.postE2eSetterTree = exports.fundE2eMidnightWallets = exports.createE2eMidnightMarket = exports.E2eMidnightMarketRequiredError = exports.E2E_MIDNIGHT_WALLETS = void 0;
4
+ const node_assert_1 = require("node:assert");
5
+ const midnight_sdk_1 = require("@morpho-org/midnight-sdk");
6
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
7
+ const viem_1 = require("viem");
8
+ const asset_js_1 = require("./asset.js");
9
+ const setup_js_1 = require("./setup.js");
10
+ /** Fork addresses of the named E2E wallets. */
11
+ exports.E2E_MIDNIGHT_WALLETS = {
12
+ A: "0x00000000000000000000000000000000e2E0000A",
13
+ B: "0x00000000000000000000000000000000e2E0000b",
14
+ C: "0x00000000000000000000000000000000e2E0000C",
15
+ D: "0x00000000000000000000000000000000e2e0000d",
16
+ };
17
+ /** Thrown when wallet assets are requested without Midnight market parameters. */
18
+ class E2eMidnightMarketRequiredError extends Error {
19
+ constructor() {
20
+ super("Wallet asset funding requires a Midnight market. Pass `market` to fundE2eMidnightWallets or remove loanAssets/collateralAssets.");
21
+ this.name = "E2eMidnightMarketRequiredError";
22
+ }
23
+ }
24
+ exports.E2eMidnightMarketRequiredError = E2eMidnightMarketRequiredError;
25
+ /** Installs assets and the fixed-price oracle, then creates an empty Midnight market snapshot. */
26
+ // biome-ignore lint/complexity/useMaxParams: Keep the fixture interface's chain and maturity inputs explicit.
27
+ const createE2eMidnightMarket = async (client, chainId, options = {}) => {
28
+ const midnight = (0, morpho_ts_1.getChainAddress)(chainId, "midnight");
29
+ const loanAsset = (0, asset_js_1.getE2eAsset)(chainId);
30
+ const loanToken = loanAsset.address.toLowerCase();
31
+ const collateralToken = "0xE2e0000000000000000000000000000000000004";
32
+ if (loanAsset.requiresInstall)
33
+ await (0, setup_js_1.installE2eAsset)(client, loanToken);
34
+ await (0, setup_js_1.installE2eAsset)(client, collateralToken);
35
+ const oracle = await (0, setup_js_1.installE2eOracle)(client);
36
+ const params = new midnight_sdk_1.MarketParams({
37
+ chainId,
38
+ midnight,
39
+ loanToken,
40
+ collateralParams: [
41
+ {
42
+ token: collateralToken,
43
+ lltv: 770000000000000000n,
44
+ liquidationCursor: 300000000000000000n,
45
+ oracle,
46
+ },
47
+ ],
48
+ maturity: options.maturity ?? 2000000000n,
49
+ rcfThreshold: 0n,
50
+ enterGate: viem_1.zeroAddress,
51
+ liquidatorGate: viem_1.zeroAddress,
52
+ });
53
+ const marketData = new midnight_sdk_1.Market({
54
+ params,
55
+ totalUnits: 0n,
56
+ lossFactor: 0n,
57
+ withdrawable: 0n,
58
+ continuousFeeCredit: 0n,
59
+ settlementFeeCbps: [0, 0, 0, 0, 0, 0, 0],
60
+ continuousFee: 0,
61
+ tickSpacing: 1,
62
+ });
63
+ return { params, marketData };
64
+ };
65
+ exports.createE2eMidnightMarket = createE2eMidnightMarket;
66
+ /**
67
+ * Clears code and funds gas for named wallets, adding requested assets and approvals to their
68
+ * existing balances and allowances.
69
+ */
70
+ const fundE2eMidnightWallets = async (client, params) => {
71
+ const hasAssetFunding = Object.values(params.wallets).some((wallet) => wallet?.loanAssets != null || wallet?.collateralAssets != null);
72
+ if (hasAssetFunding && params.market == null)
73
+ throw new E2eMidnightMarketRequiredError();
74
+ for (const walletName of Object.keys(exports.E2E_MIDNIGHT_WALLETS)) {
75
+ const funding = params.wallets[walletName];
76
+ if (funding == null)
77
+ continue;
78
+ const account = exports.E2E_MIDNIGHT_WALLETS[walletName];
79
+ await client.setCode({ address: account, bytecode: "0x" });
80
+ await client.setBalance({ address: account, value: (0, viem_1.parseEther)("1") });
81
+ const assetsToFund = [];
82
+ if (funding.collateralAssets != null) {
83
+ assetsToFund.push({
84
+ token: params.market.collateralParams[0].token,
85
+ amount: funding.collateralAssets,
86
+ useNativeFunding: false,
87
+ });
88
+ }
89
+ if (funding.loanAssets != null) {
90
+ const market = params.market;
91
+ assetsToFund.push({
92
+ token: market.loanToken,
93
+ amount: funding.loanAssets,
94
+ useNativeFunding: true,
95
+ });
96
+ }
97
+ for (const { token, amount, useNativeFunding } of assetsToFund) {
98
+ const balanceBefore = await client.readContract({
99
+ address: token,
100
+ abi: viem_1.erc20Abi,
101
+ functionName: "balanceOf",
102
+ args: [account],
103
+ });
104
+ if (useNativeFunding) {
105
+ await (0, setup_js_1.wrapE2eNative)(client, {
106
+ wNative: token,
107
+ amount,
108
+ account,
109
+ });
110
+ }
111
+ else {
112
+ await client.deal({
113
+ erc20: token,
114
+ account,
115
+ amount: balanceBefore + amount,
116
+ });
117
+ }
118
+ (0, node_assert_1.strictEqual)(await client.readContract({
119
+ address: token,
120
+ abi: viem_1.erc20Abi,
121
+ functionName: "balanceOf",
122
+ args: [account],
123
+ }), balanceBefore + amount);
124
+ if (params.approve != null) {
125
+ const allowanceBefore = await client.readContract({
126
+ address: token,
127
+ abi: viem_1.erc20Abi,
128
+ functionName: "allowance",
129
+ args: [account, params.approve],
130
+ });
131
+ const target = allowanceBefore + amount > viem_1.maxUint256
132
+ ? viem_1.maxUint256
133
+ : allowanceBefore + amount;
134
+ await client.writeContract({
135
+ account,
136
+ address: token,
137
+ abi: viem_1.erc20Abi,
138
+ functionName: "approve",
139
+ args: [params.approve, target],
140
+ });
141
+ (0, node_assert_1.strictEqual)(await client.readContract({
142
+ address: token,
143
+ abi: viem_1.erc20Abi,
144
+ functionName: "allowance",
145
+ args: [account, params.approve],
146
+ }), target);
147
+ }
148
+ }
149
+ }
150
+ };
151
+ exports.fundE2eMidnightWallets = fundE2eMidnightWallets;
152
+ /** Authorizes the SetterRatifier for a maker and marks the tree root ratified. */
153
+ const postE2eSetterTree = async (client, params) => {
154
+ const midnight = (0, morpho_ts_1.getChainAddress)(params.chainId, "midnight");
155
+ const setterRatifier = (0, morpho_ts_1.getChainAddress)(params.chainId, "setterRatifier");
156
+ await client.writeContract({
157
+ account: params.maker,
158
+ address: midnight,
159
+ abi: midnight_sdk_1.midnightAbi,
160
+ functionName: "setIsAuthorized",
161
+ args: [setterRatifier, true, params.maker],
162
+ });
163
+ await client.writeContract({
164
+ account: params.maker,
165
+ address: setterRatifier,
166
+ abi: midnight_sdk_1.setterRatifierAbi,
167
+ functionName: "setIsRootRatified",
168
+ args: [params.maker, params.tree.root, true],
169
+ });
170
+ };
171
+ exports.postE2eSetterTree = postE2eSetterTree;
172
+ /** Converts legacy or tagged trees and snapshots into SDK takeable offers using the matching ratifier. */
173
+ const getE2eTakeableOffers = (params) => {
174
+ const items = params.tree.type === "priceV1"
175
+ ? midnight_sdk_1.PriceRatifierV1.ratify({ tree: params.tree })
176
+ : params.tree.type === "rateV1"
177
+ ? midnight_sdk_1.RateRatifierV1.ratify({ tree: params.tree })
178
+ : params.tree.type === "setter"
179
+ ? midnight_sdk_1.SetterRatifier.ratify({ tree: params.tree })
180
+ : midnight_sdk_1.SetterRatifierUtils.ratify({ tree: params.tree });
181
+ return items.map(({ offer, ratifierData }) => ({
182
+ units: params.units,
183
+ offer: midnight_sdk_1.OfferUtils.toStruct({ offer }),
184
+ ratifierData,
185
+ }));
186
+ };
187
+ exports.getE2eTakeableOffers = getE2eTakeableOffers;
@@ -0,0 +1,29 @@
1
+ import type { AnvilTestClient } from "@morpho-org/test";
2
+ import { type Address, type Chain } from "viem";
3
+ /**
4
+ * Installs the E2E test asset code and storage at `address`.
5
+ *
6
+ * @param client - Anvil client used to install the asset.
7
+ * @param address - Address where the E2E asset should be installed.
8
+ */
9
+ export declare const installE2eAsset: (client: AnvilTestClient<Chain>, address: Address) => Promise<void>;
10
+ /**
11
+ * Installs the fixed 1:1 oracle used by E2E markets.
12
+ *
13
+ * @param client - Anvil client used to install the oracle.
14
+ * @returns The registered oracle address.
15
+ */
16
+ export declare const installE2eOracle: (client: AnvilTestClient<Chain>) => Promise<Address>;
17
+ /**
18
+ * Adds `amount` wNative to the selected account. Fork-installed assets use `client.deal` because
19
+ * chains like Tempo reject native value transfers; real wNative is funded by wrapping native
20
+ * tokens. A second ERC-20 `client.deal` after `setUpVault` fails on Robinhood with "cannot find
21
+ * valid balanceOf storage slot", so wrapping remains for real wNative.
22
+ *
23
+ * @param params.account - Account to fund; defaults to the test account.
24
+ */
25
+ export declare const wrapE2eNative: (client: AnvilTestClient<Chain>, params: {
26
+ readonly wNative: Address;
27
+ readonly amount: bigint;
28
+ readonly account?: Address;
29
+ }) => Promise<void>;
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.wrapE2eNative = exports.installE2eOracle = exports.installE2eAsset = void 0;
4
+ const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
+ const viem_1 = require("viem");
6
+ const asset_js_1 = require("./asset.js");
7
+ /** Address of the fixed-price oracle installed on every fork. */
8
+ const E2E_ORACLE = "0xE2E0000000000000000000000000000000000001";
9
+ /**
10
+ * Installs the E2E test asset code and storage at `address`.
11
+ *
12
+ * @param client - Anvil client used to install the asset.
13
+ * @param address - Address where the E2E asset should be installed.
14
+ */
15
+ const installE2eAsset = async (client, address) => {
16
+ await client.setCode({
17
+ address,
18
+ bytecode: asset_js_1.E2E_ASSET_BYTECODE,
19
+ });
20
+ for (const { slot, value } of asset_js_1.E2E_ASSET_STORAGE)
21
+ await client.setStorageAt({
22
+ address,
23
+ index: slot,
24
+ value,
25
+ });
26
+ };
27
+ exports.installE2eAsset = installE2eAsset;
28
+ /**
29
+ * Installs the fixed 1:1 oracle used by E2E markets.
30
+ *
31
+ * @param client - Anvil client used to install the oracle.
32
+ * @returns The registered oracle address.
33
+ */
34
+ const installE2eOracle = async (client) => {
35
+ // PUSH15 price; PUSH1 0; MSTORE; PUSH1 32; PUSH1 0; RETURN: answers `price()` with 1e36.
36
+ await client.setCode({
37
+ address: E2E_ORACLE,
38
+ bytecode: `0x6e${(0, viem_1.numberToHex)(blue_sdk_1.ORACLE_PRICE_SCALE, { size: 15 }).slice(2)}60005260206000f3`,
39
+ });
40
+ return E2E_ORACLE;
41
+ };
42
+ exports.installE2eOracle = installE2eOracle;
43
+ /** Minimal ABI for wrapping native currency into wNative. */
44
+ const wNativeDepositAbi = [
45
+ {
46
+ type: "function",
47
+ name: "deposit",
48
+ stateMutability: "payable",
49
+ inputs: [],
50
+ outputs: [],
51
+ },
52
+ ];
53
+ /**
54
+ * Adds `amount` wNative to the selected account. Fork-installed assets use `client.deal` because
55
+ * chains like Tempo reject native value transfers; real wNative is funded by wrapping native
56
+ * tokens. A second ERC-20 `client.deal` after `setUpVault` fails on Robinhood with "cannot find
57
+ * valid balanceOf storage slot", so wrapping remains for real wNative.
58
+ *
59
+ * @param params.account - Account to fund; defaults to the test account.
60
+ */
61
+ const wrapE2eNative = async (client, params) => {
62
+ const account = params.account ?? client.account.address;
63
+ if ((0, asset_js_1.getE2eAsset)(client.chain.id).requiresInstall) {
64
+ await client.deal({
65
+ erc20: params.wNative,
66
+ account,
67
+ amount: (await client.balanceOf({ erc20: params.wNative, owner: account })) +
68
+ params.amount,
69
+ });
70
+ return;
71
+ }
72
+ if (params.account == null) {
73
+ await client.deal({ amount: params.amount + (0, viem_1.parseEther)("1") });
74
+ }
75
+ else {
76
+ await client.setBalance({
77
+ address: account,
78
+ value: (await client.getBalance({ address: account })) +
79
+ params.amount +
80
+ (0, viem_1.parseEther)("1"),
81
+ });
82
+ }
83
+ await client.writeContract({
84
+ account,
85
+ address: params.wNative,
86
+ abi: wNativeDepositAbi,
87
+ functionName: "deposit",
88
+ value: params.amount,
89
+ });
90
+ };
91
+ exports.wrapE2eNative = wrapE2eNative;
@@ -0,0 +1,2 @@
1
+ /** Shared Midnight fork fixtures; requires the optional midnight-sdk peer. */
2
+ export { createE2eMidnightMarket, E2E_MIDNIGHT_WALLETS, type E2eMidnightMarket, E2eMidnightMarketRequiredError, type E2eMidnightWallet, type E2eTakeableOffer, fundE2eMidnightWallets, getE2eTakeableOffers, postE2eSetterTree, } from "./e2e/midnight.js";
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.postE2eSetterTree = exports.getE2eTakeableOffers = exports.fundE2eMidnightWallets = exports.E2eMidnightMarketRequiredError = exports.E2E_MIDNIGHT_WALLETS = exports.createE2eMidnightMarket = void 0;
4
+ /** Shared Midnight fork fixtures; requires the optional midnight-sdk peer. */
5
+ var midnight_js_1 = require("./e2e/midnight.js");
6
+ Object.defineProperty(exports, "createE2eMidnightMarket", { enumerable: true, get: function () { return midnight_js_1.createE2eMidnightMarket; } });
7
+ Object.defineProperty(exports, "E2E_MIDNIGHT_WALLETS", { enumerable: true, get: function () { return midnight_js_1.E2E_MIDNIGHT_WALLETS; } });
8
+ Object.defineProperty(exports, "E2eMidnightMarketRequiredError", { enumerable: true, get: function () { return midnight_js_1.E2eMidnightMarketRequiredError; } });
9
+ Object.defineProperty(exports, "fundE2eMidnightWallets", { enumerable: true, get: function () { return midnight_js_1.fundE2eMidnightWallets; } });
10
+ Object.defineProperty(exports, "getE2eTakeableOffers", { enumerable: true, get: function () { return midnight_js_1.getE2eTakeableOffers; } });
11
+ Object.defineProperty(exports, "postE2eSetterTree", { enumerable: true, get: function () { return midnight_js_1.postE2eSetterTree; } });
@@ -1,2 +1,3 @@
1
1
  export * from "./asset.js";
2
2
  export * from "./registryMatrix.js";
3
+ export { installE2eAsset, installE2eOracle, wrapE2eNative } from "./setup.js";
@@ -1,2 +1,3 @@
1
1
  export * from "./asset.js";
2
2
  export * from "./registryMatrix.js";
3
+ export { installE2eAsset, installE2eOracle, wrapE2eNative } from "./setup.js";
@@ -0,0 +1,54 @@
1
+ import { Market, MarketParams, type OfferStruct, type Tree, type TreeSnapshot } from "@morpho-org/midnight-sdk";
2
+ import type { AnvilTestClient } from "@morpho-org/test";
3
+ import { type Address, type Chain, type Hex } from "viem";
4
+ /** Fork addresses of the named E2E wallets. */
5
+ export declare const E2E_MIDNIGHT_WALLETS: {
6
+ readonly A: "0x00000000000000000000000000000000e2E0000A";
7
+ readonly B: "0x00000000000000000000000000000000e2E0000b";
8
+ readonly C: "0x00000000000000000000000000000000e2E0000C";
9
+ readonly D: "0x00000000000000000000000000000000e2e0000d";
10
+ };
11
+ /** Names of the reusable Midnight E2E wallets. */
12
+ export type E2eMidnightWallet = keyof typeof E2E_MIDNIGHT_WALLETS;
13
+ /** Parameters and initial snapshot for an E2E Midnight market. */
14
+ export interface E2eMidnightMarket {
15
+ readonly params: MarketParams;
16
+ readonly marketData: Market;
17
+ }
18
+ /** Thrown when wallet assets are requested without Midnight market parameters. */
19
+ export declare class E2eMidnightMarketRequiredError extends Error {
20
+ constructor();
21
+ }
22
+ /** Installs assets and the fixed-price oracle, then creates an empty Midnight market snapshot. */
23
+ export declare const createE2eMidnightMarket: (client: AnvilTestClient<Chain>, chainId: number, options?: {
24
+ readonly maturity?: bigint;
25
+ }) => Promise<E2eMidnightMarket>;
26
+ /**
27
+ * Clears code and funds gas for named wallets, adding requested assets and approvals to their
28
+ * existing balances and allowances.
29
+ */
30
+ export declare const fundE2eMidnightWallets: (client: AnvilTestClient<Chain>, params: {
31
+ readonly market?: MarketParams;
32
+ readonly wallets: Partial<Record<E2eMidnightWallet, {
33
+ readonly loanAssets?: bigint;
34
+ readonly collateralAssets?: bigint;
35
+ }>>;
36
+ readonly approve?: Address;
37
+ }) => Promise<void>;
38
+ /** Authorizes the SetterRatifier for a maker and marks the tree root ratified. */
39
+ export declare const postE2eSetterTree: (client: AnvilTestClient<Chain>, params: {
40
+ readonly chainId: number;
41
+ readonly maker: Address;
42
+ readonly tree: Tree | Tree<"setter">;
43
+ }) => Promise<void>;
44
+ /** A readonly offer fill with the matching ratifier's data. */
45
+ export interface E2eTakeableOffer {
46
+ readonly units: bigint;
47
+ readonly offer: OfferStruct;
48
+ readonly ratifierData: Hex;
49
+ }
50
+ /** Converts legacy or tagged trees and snapshots into SDK takeable offers using the matching ratifier. */
51
+ export declare const getE2eTakeableOffers: (params: {
52
+ readonly tree: Tree | Tree<"setter"> | Tree<"priceV1"> | Tree<"rateV1"> | TreeSnapshot<"setter"> | TreeSnapshot<"priceV1"> | TreeSnapshot<"rateV1">;
53
+ readonly units: bigint;
54
+ }) => readonly E2eTakeableOffer[];
@@ -0,0 +1,179 @@
1
+ import { strictEqual } from "node:assert";
2
+ import { Market, MarketParams, midnightAbi, OfferUtils, PriceRatifierV1, RateRatifierV1, SetterRatifier, SetterRatifierUtils, setterRatifierAbi, } from "@morpho-org/midnight-sdk";
3
+ import { getChainAddress } from "@morpho-org/morpho-ts";
4
+ import { erc20Abi, maxUint256, parseEther, zeroAddress, } from "viem";
5
+ import { getE2eAsset } from "./asset.js";
6
+ import { installE2eAsset, installE2eOracle, wrapE2eNative } from "./setup.js";
7
+ /** Fork addresses of the named E2E wallets. */
8
+ export const E2E_MIDNIGHT_WALLETS = {
9
+ A: "0x00000000000000000000000000000000e2E0000A",
10
+ B: "0x00000000000000000000000000000000e2E0000b",
11
+ C: "0x00000000000000000000000000000000e2E0000C",
12
+ D: "0x00000000000000000000000000000000e2e0000d",
13
+ };
14
+ /** Thrown when wallet assets are requested without Midnight market parameters. */
15
+ export class E2eMidnightMarketRequiredError extends Error {
16
+ constructor() {
17
+ super("Wallet asset funding requires a Midnight market. Pass `market` to fundE2eMidnightWallets or remove loanAssets/collateralAssets.");
18
+ this.name = "E2eMidnightMarketRequiredError";
19
+ }
20
+ }
21
+ /** Installs assets and the fixed-price oracle, then creates an empty Midnight market snapshot. */
22
+ // biome-ignore lint/complexity/useMaxParams: Keep the fixture interface's chain and maturity inputs explicit.
23
+ export const createE2eMidnightMarket = async (client, chainId, options = {}) => {
24
+ const midnight = getChainAddress(chainId, "midnight");
25
+ const loanAsset = getE2eAsset(chainId);
26
+ const loanToken = loanAsset.address.toLowerCase();
27
+ const collateralToken = "0xE2e0000000000000000000000000000000000004";
28
+ if (loanAsset.requiresInstall)
29
+ await installE2eAsset(client, loanToken);
30
+ await installE2eAsset(client, collateralToken);
31
+ const oracle = await installE2eOracle(client);
32
+ const params = new MarketParams({
33
+ chainId,
34
+ midnight,
35
+ loanToken,
36
+ collateralParams: [
37
+ {
38
+ token: collateralToken,
39
+ lltv: 770000000000000000n,
40
+ liquidationCursor: 300000000000000000n,
41
+ oracle,
42
+ },
43
+ ],
44
+ maturity: options.maturity ?? 2000000000n,
45
+ rcfThreshold: 0n,
46
+ enterGate: zeroAddress,
47
+ liquidatorGate: zeroAddress,
48
+ });
49
+ const marketData = new Market({
50
+ params,
51
+ totalUnits: 0n,
52
+ lossFactor: 0n,
53
+ withdrawable: 0n,
54
+ continuousFeeCredit: 0n,
55
+ settlementFeeCbps: [0, 0, 0, 0, 0, 0, 0],
56
+ continuousFee: 0,
57
+ tickSpacing: 1,
58
+ });
59
+ return { params, marketData };
60
+ };
61
+ /**
62
+ * Clears code and funds gas for named wallets, adding requested assets and approvals to their
63
+ * existing balances and allowances.
64
+ */
65
+ export const fundE2eMidnightWallets = async (client, params) => {
66
+ const hasAssetFunding = Object.values(params.wallets).some((wallet) => wallet?.loanAssets != null || wallet?.collateralAssets != null);
67
+ if (hasAssetFunding && params.market == null)
68
+ throw new E2eMidnightMarketRequiredError();
69
+ for (const walletName of Object.keys(E2E_MIDNIGHT_WALLETS)) {
70
+ const funding = params.wallets[walletName];
71
+ if (funding == null)
72
+ continue;
73
+ const account = E2E_MIDNIGHT_WALLETS[walletName];
74
+ await client.setCode({ address: account, bytecode: "0x" });
75
+ await client.setBalance({ address: account, value: parseEther("1") });
76
+ const assetsToFund = [];
77
+ if (funding.collateralAssets != null) {
78
+ assetsToFund.push({
79
+ token: params.market.collateralParams[0].token,
80
+ amount: funding.collateralAssets,
81
+ useNativeFunding: false,
82
+ });
83
+ }
84
+ if (funding.loanAssets != null) {
85
+ const market = params.market;
86
+ assetsToFund.push({
87
+ token: market.loanToken,
88
+ amount: funding.loanAssets,
89
+ useNativeFunding: true,
90
+ });
91
+ }
92
+ for (const { token, amount, useNativeFunding } of assetsToFund) {
93
+ const balanceBefore = await client.readContract({
94
+ address: token,
95
+ abi: erc20Abi,
96
+ functionName: "balanceOf",
97
+ args: [account],
98
+ });
99
+ if (useNativeFunding) {
100
+ await wrapE2eNative(client, {
101
+ wNative: token,
102
+ amount,
103
+ account,
104
+ });
105
+ }
106
+ else {
107
+ await client.deal({
108
+ erc20: token,
109
+ account,
110
+ amount: balanceBefore + amount,
111
+ });
112
+ }
113
+ strictEqual(await client.readContract({
114
+ address: token,
115
+ abi: erc20Abi,
116
+ functionName: "balanceOf",
117
+ args: [account],
118
+ }), balanceBefore + amount);
119
+ if (params.approve != null) {
120
+ const allowanceBefore = await client.readContract({
121
+ address: token,
122
+ abi: erc20Abi,
123
+ functionName: "allowance",
124
+ args: [account, params.approve],
125
+ });
126
+ const target = allowanceBefore + amount > maxUint256
127
+ ? maxUint256
128
+ : allowanceBefore + amount;
129
+ await client.writeContract({
130
+ account,
131
+ address: token,
132
+ abi: erc20Abi,
133
+ functionName: "approve",
134
+ args: [params.approve, target],
135
+ });
136
+ strictEqual(await client.readContract({
137
+ address: token,
138
+ abi: erc20Abi,
139
+ functionName: "allowance",
140
+ args: [account, params.approve],
141
+ }), target);
142
+ }
143
+ }
144
+ }
145
+ };
146
+ /** Authorizes the SetterRatifier for a maker and marks the tree root ratified. */
147
+ export const postE2eSetterTree = async (client, params) => {
148
+ const midnight = getChainAddress(params.chainId, "midnight");
149
+ const setterRatifier = getChainAddress(params.chainId, "setterRatifier");
150
+ await client.writeContract({
151
+ account: params.maker,
152
+ address: midnight,
153
+ abi: midnightAbi,
154
+ functionName: "setIsAuthorized",
155
+ args: [setterRatifier, true, params.maker],
156
+ });
157
+ await client.writeContract({
158
+ account: params.maker,
159
+ address: setterRatifier,
160
+ abi: setterRatifierAbi,
161
+ functionName: "setIsRootRatified",
162
+ args: [params.maker, params.tree.root, true],
163
+ });
164
+ };
165
+ /** Converts legacy or tagged trees and snapshots into SDK takeable offers using the matching ratifier. */
166
+ export const getE2eTakeableOffers = (params) => {
167
+ const items = params.tree.type === "priceV1"
168
+ ? PriceRatifierV1.ratify({ tree: params.tree })
169
+ : params.tree.type === "rateV1"
170
+ ? RateRatifierV1.ratify({ tree: params.tree })
171
+ : params.tree.type === "setter"
172
+ ? SetterRatifier.ratify({ tree: params.tree })
173
+ : SetterRatifierUtils.ratify({ tree: params.tree });
174
+ return items.map(({ offer, ratifierData }) => ({
175
+ units: params.units,
176
+ offer: OfferUtils.toStruct({ offer }),
177
+ ratifierData,
178
+ }));
179
+ };
@@ -0,0 +1,29 @@
1
+ import type { AnvilTestClient } from "@morpho-org/test";
2
+ import { type Address, type Chain } from "viem";
3
+ /**
4
+ * Installs the E2E test asset code and storage at `address`.
5
+ *
6
+ * @param client - Anvil client used to install the asset.
7
+ * @param address - Address where the E2E asset should be installed.
8
+ */
9
+ export declare const installE2eAsset: (client: AnvilTestClient<Chain>, address: Address) => Promise<void>;
10
+ /**
11
+ * Installs the fixed 1:1 oracle used by E2E markets.
12
+ *
13
+ * @param client - Anvil client used to install the oracle.
14
+ * @returns The registered oracle address.
15
+ */
16
+ export declare const installE2eOracle: (client: AnvilTestClient<Chain>) => Promise<Address>;
17
+ /**
18
+ * Adds `amount` wNative to the selected account. Fork-installed assets use `client.deal` because
19
+ * chains like Tempo reject native value transfers; real wNative is funded by wrapping native
20
+ * tokens. A second ERC-20 `client.deal` after `setUpVault` fails on Robinhood with "cannot find
21
+ * valid balanceOf storage slot", so wrapping remains for real wNative.
22
+ *
23
+ * @param params.account - Account to fund; defaults to the test account.
24
+ */
25
+ export declare const wrapE2eNative: (client: AnvilTestClient<Chain>, params: {
26
+ readonly wNative: Address;
27
+ readonly amount: bigint;
28
+ readonly account?: Address;
29
+ }) => Promise<void>;
@@ -0,0 +1,85 @@
1
+ import { ORACLE_PRICE_SCALE } from "@morpho-org/blue-sdk";
2
+ import { numberToHex, parseEther } from "viem";
3
+ import { E2E_ASSET_BYTECODE, E2E_ASSET_STORAGE, getE2eAsset } from "./asset.js";
4
+ /** Address of the fixed-price oracle installed on every fork. */
5
+ const E2E_ORACLE = "0xE2E0000000000000000000000000000000000001";
6
+ /**
7
+ * Installs the E2E test asset code and storage at `address`.
8
+ *
9
+ * @param client - Anvil client used to install the asset.
10
+ * @param address - Address where the E2E asset should be installed.
11
+ */
12
+ export const installE2eAsset = async (client, address) => {
13
+ await client.setCode({
14
+ address,
15
+ bytecode: E2E_ASSET_BYTECODE,
16
+ });
17
+ for (const { slot, value } of E2E_ASSET_STORAGE)
18
+ await client.setStorageAt({
19
+ address,
20
+ index: slot,
21
+ value,
22
+ });
23
+ };
24
+ /**
25
+ * Installs the fixed 1:1 oracle used by E2E markets.
26
+ *
27
+ * @param client - Anvil client used to install the oracle.
28
+ * @returns The registered oracle address.
29
+ */
30
+ export const installE2eOracle = async (client) => {
31
+ // PUSH15 price; PUSH1 0; MSTORE; PUSH1 32; PUSH1 0; RETURN: answers `price()` with 1e36.
32
+ await client.setCode({
33
+ address: E2E_ORACLE,
34
+ bytecode: `0x6e${numberToHex(ORACLE_PRICE_SCALE, { size: 15 }).slice(2)}60005260206000f3`,
35
+ });
36
+ return E2E_ORACLE;
37
+ };
38
+ /** Minimal ABI for wrapping native currency into wNative. */
39
+ const wNativeDepositAbi = [
40
+ {
41
+ type: "function",
42
+ name: "deposit",
43
+ stateMutability: "payable",
44
+ inputs: [],
45
+ outputs: [],
46
+ },
47
+ ];
48
+ /**
49
+ * Adds `amount` wNative to the selected account. Fork-installed assets use `client.deal` because
50
+ * chains like Tempo reject native value transfers; real wNative is funded by wrapping native
51
+ * tokens. A second ERC-20 `client.deal` after `setUpVault` fails on Robinhood with "cannot find
52
+ * valid balanceOf storage slot", so wrapping remains for real wNative.
53
+ *
54
+ * @param params.account - Account to fund; defaults to the test account.
55
+ */
56
+ export const wrapE2eNative = async (client, params) => {
57
+ const account = params.account ?? client.account.address;
58
+ if (getE2eAsset(client.chain.id).requiresInstall) {
59
+ await client.deal({
60
+ erc20: params.wNative,
61
+ account,
62
+ amount: (await client.balanceOf({ erc20: params.wNative, owner: account })) +
63
+ params.amount,
64
+ });
65
+ return;
66
+ }
67
+ if (params.account == null) {
68
+ await client.deal({ amount: params.amount + parseEther("1") });
69
+ }
70
+ else {
71
+ await client.setBalance({
72
+ address: account,
73
+ value: (await client.getBalance({ address: account })) +
74
+ params.amount +
75
+ parseEther("1"),
76
+ });
77
+ }
78
+ await client.writeContract({
79
+ account,
80
+ address: params.wNative,
81
+ abi: wNativeDepositAbi,
82
+ functionName: "deposit",
83
+ value: params.amount,
84
+ });
85
+ };
@@ -0,0 +1,2 @@
1
+ /** Shared Midnight fork fixtures; requires the optional midnight-sdk peer. */
2
+ export { createE2eMidnightMarket, E2E_MIDNIGHT_WALLETS, type E2eMidnightMarket, E2eMidnightMarketRequiredError, type E2eMidnightWallet, type E2eTakeableOffer, fundE2eMidnightWallets, getE2eTakeableOffers, postE2eSetterTree, } from "./e2e/midnight.js";
@@ -0,0 +1,2 @@
1
+ /** Shared Midnight fork fixtures; requires the optional midnight-sdk peer. */
2
+ export { createE2eMidnightMarket, E2E_MIDNIGHT_WALLETS, E2eMidnightMarketRequiredError, fundE2eMidnightWallets, getE2eTakeableOffers, postE2eSetterTree, } from "./e2e/midnight.js";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@morpho-org/morpho-test",
3
3
  "description": "Framework-agnostic extension of `@morpho-org/blue-sdk` that exports test fixtures and registry-driven E2E chain selection for tests on forks.",
4
- "version": "4.1.0-next.0",
4
+ "version": "4.2.0",
5
5
  "author": "Morpho Association <contact@morpho.org>",
6
6
  "repository": {
7
7
  "type": "git",
@@ -14,6 +14,25 @@
14
14
  "email": "contact@morpho.org"
15
15
  },
16
16
  "main": "./lib/cjs/index.js",
17
+ "exports": {
18
+ ".": {
19
+ "types": "./lib/esm/index.d.ts",
20
+ "import": "./lib/esm/index.js",
21
+ "require": "./lib/cjs/index.js"
22
+ },
23
+ "./midnight": {
24
+ "types": "./lib/esm/midnight.d.ts",
25
+ "import": "./lib/esm/midnight.js",
26
+ "require": "./lib/cjs/midnight.js"
27
+ }
28
+ },
29
+ "typesVersions": {
30
+ "*": {
31
+ "midnight": [
32
+ "lib/esm/midnight.d.ts"
33
+ ]
34
+ }
35
+ },
17
36
  "files": [
18
37
  "lib"
19
38
  ],
@@ -23,14 +42,21 @@
23
42
  "@morpho-org/blue-sdk": "^6.0.0 || ^7.0.0 || ^8.0.0",
24
43
  "@morpho-org/morpho-ts": "^2.7.0 || ^3.0.0 || ^4.0.0",
25
44
  "@morpho-org/test": "^2.11.0",
26
- "viem": "^2.21.10"
45
+ "viem": "^2.21.10",
46
+ "@morpho-org/midnight-sdk": "^2.2.0"
27
47
  },
28
48
  "devDependencies": {
29
- "@morpho-org/blue-sdk": "^8.1.0-next.0",
30
- "@morpho-org/morpho-ts": "^4.1.0-next.0",
31
- "@morpho-org/test": "^2.11.0-next.0",
49
+ "@morpho-org/blue-sdk": "^8.1.0",
50
+ "@morpho-org/morpho-ts": "^4.1.0",
51
+ "@morpho-org/test": "^2.12.0",
32
52
  "typescript": "7.0.2",
33
- "viem": "^2.55.10"
53
+ "viem": "^2.55.10",
54
+ "@morpho-org/midnight-sdk": "^2.3.0"
55
+ },
56
+ "peerDependenciesMeta": {
57
+ "@morpho-org/midnight-sdk": {
58
+ "optional": true
59
+ }
34
60
  },
35
61
  "scripts": {
36
62
  "prepublish": "pnpm build",
@@ -38,12 +64,5 @@
38
64
  "build:cjs": "tsc --build tsconfig.build.cjs.json && echo '{\"type\":\"commonjs\"}' > lib/cjs/package.json",
39
65
  "build:esm": "tsc --build tsconfig.build.esm.json && echo '{\"type\":\"module\"}' > lib/esm/package.json"
40
66
  },
41
- "types": "./lib/esm/index.d.ts",
42
- "exports": {
43
- ".": {
44
- "types": "./lib/esm/index.d.ts",
45
- "import": "./lib/esm/index.js",
46
- "require": "./lib/cjs/index.js"
47
- }
48
- }
67
+ "types": "./lib/esm/index.d.ts"
49
68
  }