@morpho-org/morpho-sdk 5.5.0 → 5.6.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.
Files changed (90) hide show
  1. package/README.md +8 -6
  2. package/lib/cjs/abis.d.ts +1 -1
  3. package/lib/cjs/abis.js +3 -1
  4. package/lib/cjs/actions/blue/borrow.d.ts +21 -15
  5. package/lib/cjs/actions/blue/borrow.js +25 -18
  6. package/lib/cjs/actions/blue/buildAssetFundingActions.d.ts +1 -1
  7. package/lib/cjs/actions/blue/buildAssetFundingActions.js +1 -1
  8. package/lib/cjs/actions/blue/buildReallocationActions.d.ts +62 -19
  9. package/lib/cjs/actions/blue/buildReallocationActions.js +123 -27
  10. package/lib/cjs/actions/blue/refinance.d.ts +14 -7
  11. package/lib/cjs/actions/blue/refinance.js +18 -10
  12. package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +28 -19
  13. package/lib/cjs/actions/blue/supplyCollateralBorrow.js +39 -23
  14. package/lib/cjs/actions/blue/withdraw.d.ts +20 -13
  15. package/lib/cjs/actions/blue/withdraw.js +23 -18
  16. package/lib/cjs/actions/requirements/getRequirementsApproval.js +2 -1
  17. package/lib/cjs/bundler/actions.d.ts +117 -0
  18. package/lib/cjs/bundler/actions.js +211 -4
  19. package/lib/cjs/bundler/types.d.ts +20 -0
  20. package/lib/cjs/constants.d.ts +1 -1
  21. package/lib/cjs/constants.js +3 -1
  22. package/lib/cjs/entities/blue/blue.d.ts +360 -71
  23. package/lib/cjs/entities/blue/blue.js +285 -52
  24. package/lib/cjs/entities/index.d.ts +2 -1
  25. package/lib/cjs/entities/index.js +6 -3
  26. package/lib/cjs/entities/{reallocationData.d.ts → vaultV1ReallocationData.d.ts} +40 -15
  27. package/lib/cjs/entities/{reallocationData.js → vaultV1ReallocationData.js} +29 -16
  28. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +1 -1
  29. package/lib/cjs/entities/vaultV2/vaultV2.js +1 -1
  30. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +511 -0
  31. package/lib/cjs/entities/vaultV2BlueReallocationData.js +1311 -0
  32. package/lib/{esm/helpers/computeReallocations.d.ts → cjs/helpers/computeVaultV1Reallocations.d.ts} +14 -8
  33. package/lib/cjs/helpers/{computeReallocations.js → computeVaultV1Reallocations.js} +27 -13
  34. package/lib/cjs/helpers/constant.d.ts +5 -3
  35. package/lib/cjs/helpers/constant.js +6 -4
  36. package/lib/cjs/helpers/index.d.ts +2 -2
  37. package/lib/cjs/helpers/index.js +6 -3
  38. package/lib/cjs/helpers/validate.d.ts +30 -14
  39. package/lib/cjs/helpers/validate.js +140 -27
  40. package/lib/cjs/types/action.d.ts +12 -0
  41. package/lib/cjs/types/error.d.ts +224 -5
  42. package/lib/cjs/types/error.js +269 -7
  43. package/lib/cjs/types/sharedLiquidity.d.ts +72 -8
  44. package/lib/cjs/utils.d.ts +2 -2
  45. package/lib/cjs/utils.js +6 -4
  46. package/lib/esm/abis.d.ts +1 -1
  47. package/lib/esm/abis.js +1 -1
  48. package/lib/esm/actions/blue/borrow.d.ts +21 -15
  49. package/lib/esm/actions/blue/borrow.js +26 -19
  50. package/lib/esm/actions/blue/buildAssetFundingActions.d.ts +1 -1
  51. package/lib/esm/actions/blue/buildAssetFundingActions.js +1 -1
  52. package/lib/esm/actions/blue/buildReallocationActions.d.ts +62 -19
  53. package/lib/esm/actions/blue/buildReallocationActions.js +119 -25
  54. package/lib/esm/actions/blue/refinance.d.ts +14 -7
  55. package/lib/esm/actions/blue/refinance.js +19 -11
  56. package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +28 -19
  57. package/lib/esm/actions/blue/supplyCollateralBorrow.js +40 -24
  58. package/lib/esm/actions/blue/withdraw.d.ts +20 -13
  59. package/lib/esm/actions/blue/withdraw.js +24 -19
  60. package/lib/esm/actions/requirements/getRequirementsApproval.js +2 -1
  61. package/lib/esm/bundler/actions.d.ts +117 -0
  62. package/lib/esm/bundler/actions.js +214 -7
  63. package/lib/esm/bundler/types.d.ts +20 -0
  64. package/lib/esm/constants.d.ts +1 -1
  65. package/lib/esm/constants.js +1 -1
  66. package/lib/esm/entities/blue/blue.d.ts +360 -71
  67. package/lib/esm/entities/blue/blue.js +288 -55
  68. package/lib/esm/entities/index.d.ts +2 -1
  69. package/lib/esm/entities/index.js +2 -1
  70. package/lib/esm/entities/{reallocationData.d.ts → vaultV1ReallocationData.d.ts} +40 -15
  71. package/lib/esm/entities/{reallocationData.js → vaultV1ReallocationData.js} +32 -14
  72. package/lib/esm/entities/vaultV2/vaultV2.d.ts +1 -1
  73. package/lib/esm/entities/vaultV2/vaultV2.js +1 -1
  74. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +511 -0
  75. package/lib/esm/entities/vaultV2BlueReallocationData.js +1307 -0
  76. package/lib/{cjs/helpers/computeReallocations.d.ts → esm/helpers/computeVaultV1Reallocations.d.ts} +14 -8
  77. package/lib/esm/helpers/{computeReallocations.js → computeVaultV1Reallocations.js} +25 -11
  78. package/lib/esm/helpers/constant.d.ts +5 -3
  79. package/lib/esm/helpers/constant.js +5 -3
  80. package/lib/esm/helpers/index.d.ts +2 -2
  81. package/lib/esm/helpers/index.js +2 -2
  82. package/lib/esm/helpers/validate.d.ts +30 -14
  83. package/lib/esm/helpers/validate.js +141 -30
  84. package/lib/esm/types/action.d.ts +12 -0
  85. package/lib/esm/types/error.d.ts +224 -5
  86. package/lib/esm/types/error.js +253 -4
  87. package/lib/esm/types/sharedLiquidity.d.ts +72 -8
  88. package/lib/esm/utils.d.ts +2 -2
  89. package/lib/esm/utils.js +2 -2
  90. package/package.json +5 -5
@@ -2,9 +2,9 @@ import { AccrualPosition, Market, type MarketId, Position, Vault, VaultMarketCon
2
2
  import type { Address } from "viem";
3
3
  import type { PublicAllocatorOptions, PublicReallocation, ReallocationComputeOptions } from "../types/index.js";
4
4
  /**
5
- * Input state required to construct {@link ReallocationData}.
5
+ * Input state required to construct {@link VaultV1ReallocationData}.
6
6
  */
7
- export interface InputReallocationData {
7
+ export interface InputVaultV1ReallocationData {
8
8
  /** Chain id associated with the fetched state. */
9
9
  readonly chainId: number;
10
10
  /** Markets indexed by market id. */
@@ -16,13 +16,19 @@ export interface InputReallocationData {
16
16
  /** Vault market configs indexed by vault address, then by market id. */
17
17
  readonly vaultMarketConfigs?: Readonly<Record<Address, Readonly<Record<MarketId, VaultMarketConfig | undefined>>>>;
18
18
  }
19
+ /**
20
+ * Deprecated input name for Vault V1 reallocation data.
21
+ *
22
+ * @deprecated Use {@link InputVaultV1ReallocationData} instead.
23
+ */
24
+ export type InputReallocationData = InputVaultV1ReallocationData;
19
25
  /**
20
26
  * Narrow state container for computing public allocator reallocations.
21
27
  *
22
28
  * @remarks
23
29
  * The class owns only the market, vault, position, vault-market-config,
24
30
  * and chain data needed by the shared-liquidity algorithm. Constructor inputs
25
- * are cloned, and simulation steps return cloned `ReallocationData` instances
31
+ * are cloned, and simulation steps return cloned `VaultV1ReallocationData` instances
26
32
  * so fetched caller inputs are not mutated.
27
33
  *
28
34
  * Public records are exposed for inspection and snapshotting only. Treat
@@ -30,7 +36,7 @@ export interface InputReallocationData {
30
36
  * contract keyed by market id or address; use the getters for typed absence
31
37
  * errors and use simulation methods to produce updated state.
32
38
  */
33
- export declare class ReallocationData implements InputReallocationData {
39
+ export declare class VaultV1ReallocationData implements InputVaultV1ReallocationData {
34
40
  /** Chain id associated with the fetched reallocation data. */
35
41
  readonly chainId: number;
36
42
  /** Markets indexed by market id. */
@@ -47,13 +53,13 @@ export declare class ReallocationData implements InputReallocationData {
47
53
  *
48
54
  * @param input - Reallocation input data fetched at a consistent chain state.
49
55
  */
50
- constructor(input: InputReallocationData);
56
+ constructor(input: InputVaultV1ReallocationData);
51
57
  /**
52
58
  * Creates a deep clone of this reallocation state.
53
59
  *
54
- * @returns A new `ReallocationData` instance with cloned entity objects.
60
+ * @returns A new `VaultV1ReallocationData` instance with cloned entity objects.
55
61
  */
56
- clone(): ReallocationData;
62
+ clone(): VaultV1ReallocationData;
57
63
  private forkAliasedState;
58
64
  /**
59
65
  * Gets a market by id.
@@ -131,7 +137,7 @@ export declare class ReallocationData implements InputReallocationData {
131
137
  * morphoViemExtension,
132
138
  * type PublicReallocation,
133
139
  * } from "@morpho-org/morpho-sdk";
134
- * import type { ReallocationData } from "@morpho-org/morpho-sdk/entities";
140
+ * import type { VaultV1ReallocationData } from "@morpho-org/morpho-sdk/entities";
135
141
  *
136
142
  * const client = createPublicClient({
137
143
  * chain: mainnet,
@@ -141,22 +147,35 @@ export declare class ReallocationData implements InputReallocationData {
141
147
  * const marketParams = markets[mainnet.id].usdc_wbtc;
142
148
  * const market = client.morpho.blue(marketParams, mainnet.id);
143
149
  * const block = await client.getBlock();
144
- * const reallocationData = await market.getReallocationData({
150
+ * const reallocationData = await market.getVaultV1ReallocationData({
145
151
  * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
146
152
  * block: { number: block.number, timestamp: block.timestamp },
147
153
  * });
148
154
  *
149
155
  * const result: {
150
156
  * withdrawals: readonly PublicReallocation[];
151
- * data: ReallocationData;
152
- * } = reallocationData.getMarketPublicReallocations(marketParams.id, {
157
+ * data: VaultV1ReallocationData;
158
+ * } = reallocationData.computeVaultV1Reallocations(marketParams.id, {
153
159
  * timestamp: block.timestamp,
154
160
  * });
155
161
  * ```
156
162
  */
163
+ computeVaultV1Reallocations(marketId: MarketId, options?: PublicAllocatorOptions): {
164
+ readonly withdrawals: readonly PublicReallocation[];
165
+ data: VaultV1ReallocationData;
166
+ };
167
+ /**
168
+ * Calculates Vault V1 public reallocations that can supply liquidity to `marketId`.
169
+ *
170
+ * @param marketId - Target market to supply with shared liquidity.
171
+ * @param options - Optional allocator discovery options.
172
+ * @returns Computed source-market withdrawals and the post-reallocation state.
173
+ * @throws {@link UnknownReallocationMarketError} when the target market is absent.
174
+ * @deprecated Use {@link computeVaultV1Reallocations} instead.
175
+ */
157
176
  getMarketPublicReallocations(marketId: MarketId, options?: PublicAllocatorOptions): {
158
177
  readonly withdrawals: readonly PublicReallocation[];
159
- data: ReallocationData;
178
+ data: VaultV1ReallocationData;
160
179
  };
161
180
  /**
162
181
  * Sums the public-allocator liquidity reallocatable into `marketId` from
@@ -186,7 +205,7 @@ export declare class ReallocationData implements InputReallocationData {
186
205
  * const marketParams = markets[mainnet.id].usdc_wbtc;
187
206
  * const market = client.morpho.blue(marketParams, mainnet.id);
188
207
  * const block = await client.getBlock();
189
- * const reallocationData = await market.getReallocationData({
208
+ * const reallocationData = await market.getVaultV1ReallocationData({
190
209
  * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
191
210
  * block: { number: block.number, timestamp: block.timestamp },
192
211
  * });
@@ -237,7 +256,7 @@ export declare class ReallocationData implements InputReallocationData {
237
256
  * const marketParams = markets[mainnet.id].usdc_wbtc;
238
257
  * const market = client.morpho.blue(marketParams, mainnet.id);
239
258
  * const block = await client.getBlock();
240
- * const reallocationData = await market.getReallocationData({
259
+ * const reallocationData = await market.getVaultV1ReallocationData({
241
260
  * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
242
261
  * block: { number: block.number, timestamp: block.timestamp },
243
262
  * });
@@ -282,5 +301,11 @@ export declare class ReallocationData implements InputReallocationData {
282
301
  readonly assets: bigint;
283
302
  };
284
303
  readonly timestamp: bigint;
285
- }): ReallocationData;
304
+ }): VaultV1ReallocationData;
286
305
  }
306
+ /**
307
+ * Deprecated class name for Vault V1 reallocation data.
308
+ *
309
+ * @deprecated Use {@link VaultV1ReallocationData} instead.
310
+ */
311
+ export { VaultV1ReallocationData as ReallocationData };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ReallocationData = void 0;
3
+ exports.ReallocationData = exports.VaultV1ReallocationData = void 0;
4
4
  const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
5
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
6
  const constant_js_1 = require("../helpers/constant.js");
@@ -53,7 +53,7 @@ const cloneVaultMarketConfig = (config) => new blue_sdk_1.VaultMarketConfig({
53
53
  * @remarks
54
54
  * The class owns only the market, vault, position, vault-market-config,
55
55
  * and chain data needed by the shared-liquidity algorithm. Constructor inputs
56
- * are cloned, and simulation steps return cloned `ReallocationData` instances
56
+ * are cloned, and simulation steps return cloned `VaultV1ReallocationData` instances
57
57
  * so fetched caller inputs are not mutated.
58
58
  *
59
59
  * Public records are exposed for inspection and snapshotting only. Treat
@@ -61,7 +61,7 @@ const cloneVaultMarketConfig = (config) => new blue_sdk_1.VaultMarketConfig({
61
61
  * contract keyed by market id or address; use the getters for typed absence
62
62
  * errors and use simulation methods to produce updated state.
63
63
  */
64
- class ReallocationData {
64
+ class VaultV1ReallocationData {
65
65
  /** Chain id associated with the fetched reallocation data. */
66
66
  chainId;
67
67
  /** Markets indexed by market id. */
@@ -109,13 +109,13 @@ class ReallocationData {
109
109
  /**
110
110
  * Creates a deep clone of this reallocation state.
111
111
  *
112
- * @returns A new `ReallocationData` instance with cloned entity objects.
112
+ * @returns A new `VaultV1ReallocationData` instance with cloned entity objects.
113
113
  */
114
114
  clone() {
115
- return new ReallocationData(this);
115
+ return new VaultV1ReallocationData(this);
116
116
  }
117
117
  forkAliasedState() {
118
- const data = new ReallocationData({ chainId: this.chainId });
118
+ const data = new VaultV1ReallocationData({ chainId: this.chainId });
119
119
  Object.assign(data.markets, this.markets);
120
120
  Object.assign(data.vaults, this.vaults);
121
121
  for (const [user, positions] of Object.entries(this.positions)) {
@@ -224,7 +224,7 @@ class ReallocationData {
224
224
  * morphoViemExtension,
225
225
  * type PublicReallocation,
226
226
  * } from "@morpho-org/morpho-sdk";
227
- * import type { ReallocationData } from "@morpho-org/morpho-sdk/entities";
227
+ * import type { VaultV1ReallocationData } from "@morpho-org/morpho-sdk/entities";
228
228
  *
229
229
  * const client = createPublicClient({
230
230
  * chain: mainnet,
@@ -234,27 +234,27 @@ class ReallocationData {
234
234
  * const marketParams = markets[mainnet.id].usdc_wbtc;
235
235
  * const market = client.morpho.blue(marketParams, mainnet.id);
236
236
  * const block = await client.getBlock();
237
- * const reallocationData = await market.getReallocationData({
237
+ * const reallocationData = await market.getVaultV1ReallocationData({
238
238
  * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
239
239
  * block: { number: block.number, timestamp: block.timestamp },
240
240
  * });
241
241
  *
242
242
  * const result: {
243
243
  * withdrawals: readonly PublicReallocation[];
244
- * data: ReallocationData;
245
- * } = reallocationData.getMarketPublicReallocations(marketParams.id, {
244
+ * data: VaultV1ReallocationData;
245
+ * } = reallocationData.computeVaultV1Reallocations(marketParams.id, {
246
246
  * timestamp: block.timestamp,
247
247
  * });
248
248
  * ```
249
249
  */
250
- getMarketPublicReallocations(marketId, options = {}) {
250
+ computeVaultV1Reallocations(marketId, options = {}) {
251
251
  const { enabled = true, timestamp, reallocatableVaults, defaultMaxWithdrawalUtilization = constant_js_1.DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, maxWithdrawalUtilization = {}, } = options;
252
252
  if (!enabled)
253
253
  return { withdrawals: [], data: this };
254
254
  const accrualTimestamp = BigInt(timestamp ?? this.getMarket(marketId).lastUpdate);
255
255
  const configuredVaults = Object.keys(this.vaultMarketConfigs);
256
256
  const vaultKeyByLower = new Map(configuredVaults.map((vault) => [vault.toLowerCase(), vault]));
257
- const vaults = Array.from(new Set((reallocatableVaults ?? configuredVaults)
257
+ const vaults = Array.from(new Set([...(reallocatableVaults ?? configuredVaults)]
258
258
  .map((vault) => vaultKeyByLower.get(vault.toLowerCase()))
259
259
  .filter((vault) => vault != null))).filter((vault) => {
260
260
  const vaultMarketConfig = this.vaultMarketConfigs[vault]?.[marketId];
@@ -303,6 +303,18 @@ class ReallocationData {
303
303
  });
304
304
  }
305
305
  }
306
+ /**
307
+ * Calculates Vault V1 public reallocations that can supply liquidity to `marketId`.
308
+ *
309
+ * @param marketId - Target market to supply with shared liquidity.
310
+ * @param options - Optional allocator discovery options.
311
+ * @returns Computed source-market withdrawals and the post-reallocation state.
312
+ * @throws {@link UnknownReallocationMarketError} when the target market is absent.
313
+ * @deprecated Use {@link computeVaultV1Reallocations} instead.
314
+ */
315
+ getMarketPublicReallocations(marketId, options = {}) {
316
+ return this.computeVaultV1Reallocations(marketId, options);
317
+ }
306
318
  /**
307
319
  * Sums the public-allocator liquidity reallocatable into `marketId` from
308
320
  * sibling markets.
@@ -331,7 +343,7 @@ class ReallocationData {
331
343
  * const marketParams = markets[mainnet.id].usdc_wbtc;
332
344
  * const market = client.morpho.blue(marketParams, mainnet.id);
333
345
  * const block = await client.getBlock();
334
- * const reallocationData = await market.getReallocationData({
346
+ * const reallocationData = await market.getVaultV1ReallocationData({
335
347
  * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
336
348
  * block: { number: block.number, timestamp: block.timestamp },
337
349
  * });
@@ -343,7 +355,7 @@ class ReallocationData {
343
355
  * ```
344
356
  */
345
357
  getPublicReallocationLiquidity(marketId, options) {
346
- const { withdrawals } = this.getMarketPublicReallocations(marketId, options);
358
+ const { withdrawals } = this.computeVaultV1Reallocations(marketId, options);
347
359
  return withdrawals.reduce((total, { assets }) => total + assets, 0n);
348
360
  }
349
361
  /**
@@ -385,7 +397,7 @@ class ReallocationData {
385
397
  * const marketParams = markets[mainnet.id].usdc_wbtc;
386
398
  * const market = client.morpho.blue(marketParams, mainnet.id);
387
399
  * const block = await client.getBlock();
388
- * const reallocationData = await market.getReallocationData({
400
+ * const reallocationData = await market.getVaultV1ReallocationData({
389
401
  * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
390
402
  * block: { number: block.number, timestamp: block.timestamp },
391
403
  * });
@@ -516,4 +528,5 @@ class ReallocationData {
516
528
  return data;
517
529
  }
518
530
  }
519
- exports.ReallocationData = ReallocationData;
531
+ exports.VaultV1ReallocationData = VaultV1ReallocationData;
532
+ exports.ReallocationData = VaultV1ReallocationData;
@@ -86,7 +86,7 @@ export interface VaultV2Actions {
86
86
  * multiple share burns. The SDK intentionally does not validate the user's share balance; size
87
87
  * it so `amount + BigInt(marketParamsList.length) <= vault.previewRedeem(sharesHeld)`. The
88
88
  * per-market term covers V2 withdrawal rounding and is not needed for V1. The share allowance
89
- * includes the penalty burns and accrual through the bundle deadline.
89
+ * includes that buffer, the penalty burns, and accrual through the bundle deadline.
90
90
  *
91
91
  * Idle balance, penalty, and adapter positions can drift after the snapshot, so an on-chain
92
92
  * under-coverage panic remains possible if vault state changes between preparation and inclusion.
@@ -208,11 +208,11 @@ class MorphoVaultV2 {
208
208
  }
209
209
  let remaining = assetsToDeallocate;
210
210
  let peak = 0n;
211
- let requiredShareAllowance = 0n;
212
211
  const consumedMarketIds = new Set();
213
212
  const { vault: allowanceVault } = vaultData.accrueInterest(deadline);
214
213
  // Interest can lower the burn, while management fees can raise it; bound both endpoints.
215
214
  const previewAllowance = (assets) => blue_sdk_1.MathLib.max(vaultData.toShares(assets, "Up"), allowanceVault.toShares(assets, "Up"));
215
+ let requiredShareAllowance = previewAllowance(BigInt(marketParamsListSnapshot.length));
216
216
  // Pre-burn previews upper-bound each separately rounded idle, penalty, and main burn.
217
217
  requiredShareAllowance += previewAllowance(idleAssets);
218
218
  for (const id of marketIdListSnapshot) {