@morpho-org/morpho-sdk 5.4.1 → 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 (140) hide show
  1. package/README.md +23 -7
  2. package/lib/cjs/abis.d.ts +314 -1
  3. package/lib/cjs/abis.js +169 -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/encode/encodeErc20Approval.d.ts +1 -1
  17. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +9 -2
  18. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +64 -0
  19. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +131 -0
  20. package/lib/cjs/actions/requirements/encode/index.d.ts +1 -0
  21. package/lib/cjs/actions/requirements/encode/index.js +1 -0
  22. package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +3 -2
  23. package/lib/cjs/actions/requirements/getRequirementsApproval.js +5 -3
  24. package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +52 -0
  25. package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.js +91 -0
  26. package/lib/cjs/actions/signatures/index.d.ts +1 -0
  27. package/lib/cjs/actions/signatures/index.js +1 -0
  28. package/lib/cjs/actions/vaultV1/inKindRedeem.d.ts +54 -0
  29. package/lib/cjs/actions/vaultV1/inKindRedeem.js +96 -0
  30. package/lib/cjs/actions/vaultV1/index.d.ts +1 -0
  31. package/lib/cjs/actions/vaultV1/index.js +1 -0
  32. package/lib/cjs/actions/vaultV2/inKindRedeem.d.ts +56 -0
  33. package/lib/cjs/actions/vaultV2/inKindRedeem.js +97 -0
  34. package/lib/cjs/actions/vaultV2/index.d.ts +1 -0
  35. package/lib/cjs/actions/vaultV2/index.js +1 -0
  36. package/lib/cjs/bundler/actions.d.ts +118 -2
  37. package/lib/cjs/bundler/actions.js +212 -6
  38. package/lib/cjs/bundler/types.d.ts +20 -0
  39. package/lib/cjs/constants.d.ts +1 -1
  40. package/lib/cjs/constants.js +3 -1
  41. package/lib/cjs/entities/blue/blue.d.ts +365 -76
  42. package/lib/cjs/entities/blue/blue.js +285 -52
  43. package/lib/cjs/entities/index.d.ts +2 -1
  44. package/lib/cjs/entities/index.js +6 -3
  45. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +82 -4
  46. package/lib/cjs/entities/vaultV1/vaultV1.js +164 -0
  47. package/lib/cjs/entities/{reallocationData.d.ts → vaultV1ReallocationData.d.ts} +41 -17
  48. package/lib/cjs/entities/{reallocationData.js → vaultV1ReallocationData.js} +30 -18
  49. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +89 -3
  50. package/lib/cjs/entities/vaultV2/vaultV2.js +181 -0
  51. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +511 -0
  52. package/lib/cjs/entities/vaultV2BlueReallocationData.js +1311 -0
  53. package/lib/{esm/helpers/computeReallocations.d.ts → cjs/helpers/computeVaultV1Reallocations.d.ts} +14 -8
  54. package/lib/cjs/helpers/{computeReallocations.js → computeVaultV1Reallocations.js} +27 -13
  55. package/lib/cjs/helpers/constant.d.ts +5 -3
  56. package/lib/cjs/helpers/constant.js +6 -4
  57. package/lib/cjs/helpers/index.d.ts +3 -2
  58. package/lib/cjs/helpers/index.js +8 -3
  59. package/lib/cjs/helpers/previewVaultV2InKindRedeem.d.ts +49 -0
  60. package/lib/cjs/helpers/previewVaultV2InKindRedeem.js +73 -0
  61. package/lib/cjs/helpers/validate.d.ts +31 -15
  62. package/lib/cjs/helpers/validate.js +140 -27
  63. package/lib/cjs/helpers/validateRequirementSpender.d.ts +1 -1
  64. package/lib/cjs/helpers/validateRequirementSpender.js +2 -1
  65. package/lib/cjs/types/action.d.ts +33 -1
  66. package/lib/cjs/types/error.d.ts +363 -5
  67. package/lib/cjs/types/error.js +455 -6
  68. package/lib/cjs/types/sharedLiquidity.d.ts +72 -8
  69. package/lib/cjs/utils.d.ts +2 -2
  70. package/lib/cjs/utils.js +6 -4
  71. package/lib/esm/abis.d.ts +314 -1
  72. package/lib/esm/abis.js +167 -1
  73. package/lib/esm/actions/blue/borrow.d.ts +21 -15
  74. package/lib/esm/actions/blue/borrow.js +26 -19
  75. package/lib/esm/actions/blue/buildAssetFundingActions.d.ts +1 -1
  76. package/lib/esm/actions/blue/buildAssetFundingActions.js +1 -1
  77. package/lib/esm/actions/blue/buildReallocationActions.d.ts +62 -19
  78. package/lib/esm/actions/blue/buildReallocationActions.js +119 -25
  79. package/lib/esm/actions/blue/refinance.d.ts +14 -7
  80. package/lib/esm/actions/blue/refinance.js +19 -11
  81. package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +28 -19
  82. package/lib/esm/actions/blue/supplyCollateralBorrow.js +40 -24
  83. package/lib/esm/actions/blue/withdraw.d.ts +20 -13
  84. package/lib/esm/actions/blue/withdraw.js +24 -19
  85. package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +1 -1
  86. package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +9 -2
  87. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +64 -0
  88. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +127 -0
  89. package/lib/esm/actions/requirements/encode/index.d.ts +1 -0
  90. package/lib/esm/actions/requirements/encode/index.js +1 -0
  91. package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +3 -2
  92. package/lib/esm/actions/requirements/getRequirementsApproval.js +5 -3
  93. package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +52 -0
  94. package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.js +87 -0
  95. package/lib/esm/actions/signatures/index.d.ts +1 -0
  96. package/lib/esm/actions/signatures/index.js +1 -0
  97. package/lib/esm/actions/vaultV1/inKindRedeem.d.ts +54 -0
  98. package/lib/esm/actions/vaultV1/inKindRedeem.js +92 -0
  99. package/lib/esm/actions/vaultV1/index.d.ts +1 -0
  100. package/lib/esm/actions/vaultV1/index.js +1 -0
  101. package/lib/esm/actions/vaultV2/inKindRedeem.d.ts +56 -0
  102. package/lib/esm/actions/vaultV2/inKindRedeem.js +93 -0
  103. package/lib/esm/actions/vaultV2/index.d.ts +1 -0
  104. package/lib/esm/actions/vaultV2/index.js +1 -0
  105. package/lib/esm/bundler/actions.d.ts +118 -2
  106. package/lib/esm/bundler/actions.js +215 -9
  107. package/lib/esm/bundler/types.d.ts +20 -0
  108. package/lib/esm/constants.d.ts +1 -1
  109. package/lib/esm/constants.js +1 -1
  110. package/lib/esm/entities/blue/blue.d.ts +362 -73
  111. package/lib/esm/entities/blue/blue.js +288 -55
  112. package/lib/esm/entities/index.d.ts +2 -1
  113. package/lib/esm/entities/index.js +2 -1
  114. package/lib/esm/entities/vaultV1/vaultV1.d.ts +80 -2
  115. package/lib/esm/entities/vaultV1/vaultV1.js +170 -6
  116. package/lib/esm/entities/{reallocationData.d.ts → vaultV1ReallocationData.d.ts} +41 -17
  117. package/lib/esm/entities/{reallocationData.js → vaultV1ReallocationData.js} +33 -16
  118. package/lib/esm/entities/vaultV2/vaultV2.d.ts +88 -2
  119. package/lib/esm/entities/vaultV2/vaultV2.js +187 -6
  120. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +511 -0
  121. package/lib/esm/entities/vaultV2BlueReallocationData.js +1307 -0
  122. package/lib/{cjs/helpers/computeReallocations.d.ts → esm/helpers/computeVaultV1Reallocations.d.ts} +14 -8
  123. package/lib/esm/helpers/{computeReallocations.js → computeVaultV1Reallocations.js} +25 -11
  124. package/lib/esm/helpers/constant.d.ts +5 -3
  125. package/lib/esm/helpers/constant.js +5 -3
  126. package/lib/esm/helpers/index.d.ts +3 -2
  127. package/lib/esm/helpers/index.js +3 -2
  128. package/lib/esm/helpers/previewVaultV2InKindRedeem.d.ts +49 -0
  129. package/lib/esm/helpers/previewVaultV2InKindRedeem.js +70 -0
  130. package/lib/esm/helpers/validate.d.ts +31 -15
  131. package/lib/esm/helpers/validate.js +141 -30
  132. package/lib/esm/helpers/validateRequirementSpender.d.ts +1 -1
  133. package/lib/esm/helpers/validateRequirementSpender.js +2 -1
  134. package/lib/esm/types/action.d.ts +33 -1
  135. package/lib/esm/types/error.d.ts +363 -5
  136. package/lib/esm/types/error.js +428 -4
  137. package/lib/esm/types/sharedLiquidity.d.ts +72 -8
  138. package/lib/esm/utils.d.ts +2 -2
  139. package/lib/esm/utils.js +2 -2
  140. package/package.json +9 -11
@@ -1,11 +1,12 @@
1
- import { DEFAULT_SLIPPAGE_TOLERANCE, getChainAddresses, MathLib, } from "@morpho-org/blue-sdk";
2
- import { fetchAccrualVault } from "@morpho-org/blue-sdk-viem";
3
- import { Time } from "@morpho-org/morpho-ts";
4
- import { isAddressEqual } from "viem";
5
- import { getGeneralAdapterRequirements, vaultV1Deposit, vaultV1MigrateToV2, vaultV1Redeem, vaultV1Withdraw, } from "../../actions/index.js";
1
+ import { DEFAULT_SLIPPAGE_TOLERANCE, getChainAddresses, MarketUtils, MathLib, } from "@morpho-org/blue-sdk";
2
+ import { blueAbi, erc2612Abi, fetchAccrualVault, metaMorphoAbi, } from "@morpho-org/blue-sdk-viem";
3
+ import { getChainAddress, Time } from "@morpho-org/morpho-ts";
4
+ import { erc20Abi, isAddressEqual } from "viem";
5
+ import { multicall } from "viem/actions";
6
+ import { encodeErc20Approval, encodeVaultSharesPermit, getGeneralAdapterRequirements, vaultV1Deposit, vaultV1InKindRedeem, vaultV1MigrateToV2, vaultV1Redeem, vaultV1Withdraw, } from "../../actions/index.js";
6
7
  import { MAX_ABSOLUTE_SHARE_PRICE } from "../../helpers/constant.js";
7
8
  import { validateChainId, validateSlippageTolerance, } from "../../helpers/index.js";
8
- import { ChainIdMismatchError, ChainWNativeMissingError, NativeAmountOnNonWNativeVaultError, NegativeInputError, NonPositiveInputError, selectRequirementSignatures, VaultAddressMismatchError, VaultAssetMismatchError, } from "../../types/index.js";
9
+ import { ChainIdMismatchError, ChainWNativeMissingError, EmptyMarketParamsListError, ExpiredDeadlineError, InKindRedeemCoverageError, InsufficientBlueBalanceForInKindRedeemError, NativeAmountOnNonWNativeVaultError, NegativeInputError, NonPositiveInputError, selectRequirementSignatures, VaultAddressMismatchError, VaultAssetMismatchError, VaultIsBlueFeeRecipientError, VaultMorphoMismatchError, } from "../../types/index.js";
9
10
  export class MorphoVaultV1 {
10
11
  client;
11
12
  vault;
@@ -128,6 +129,169 @@ export class MorphoVaultV1 {
128
129
  }),
129
130
  };
130
131
  }
132
+ /** {@inheritDoc VaultV1Actions.inKindRedeem} */
133
+ inKindRedeem({ amount, marketParamsList, vaultData, userAddress, deadline: deadlineOverride, }) {
134
+ if (this.client.viemClient.chain?.id !== this.chainId) {
135
+ throw new ChainIdMismatchError(this.client.viemClient.chain?.id, this.chainId);
136
+ }
137
+ if (!isAddressEqual(vaultData.address, this.vault)) {
138
+ throw new VaultAddressMismatchError(this.vault, vaultData.address);
139
+ }
140
+ if (amount <= 0n)
141
+ throw new NonPositiveInputError("amount", amount);
142
+ if (marketParamsList.length === 0)
143
+ throw new EmptyMarketParamsListError();
144
+ const marketParamsListSnapshot = marketParamsList.map(({ loanToken, collateralToken, oracle, irm, lltv }) => ({
145
+ loanToken,
146
+ collateralToken,
147
+ oracle,
148
+ irm,
149
+ lltv,
150
+ }));
151
+ const now = Time.timestamp();
152
+ const deadline = deadlineOverride ?? now + Time.s.from.h(2n);
153
+ if (deadline <= now)
154
+ throw new ExpiredDeadlineError(deadline, now);
155
+ const vaultExitBundlesV1 = getChainAddress(this.chainId, "bundles.vaultExitBundlesV1");
156
+ const addresses = getChainAddresses(this.chainId);
157
+ const blue = addresses.blue ?? addresses.morpho;
158
+ let covered = 0n;
159
+ const assignedByMarket = new Map();
160
+ for (const marketParams of marketParamsListSnapshot) {
161
+ if (covered === amount)
162
+ break;
163
+ const marketId = MarketUtils.getMarketId(marketParams);
164
+ const allocation = vaultData.allocations.get(marketId);
165
+ if (allocation?.config.enabled !== true)
166
+ continue;
167
+ const market = allocation.position.market.accrueInterest(now);
168
+ const available = market.toSupplyAssets(allocation.position.supplyShares);
169
+ const assigned = assignedByMarket.get(marketId) ?? 0n;
170
+ const chunk = MathLib.min(available, amount - covered);
171
+ if (assigned + chunk > available) {
172
+ throw new InKindRedeemCoverageError({
173
+ required: amount,
174
+ covered,
175
+ maxExitAssets: covered,
176
+ });
177
+ }
178
+ assignedByMarket.set(marketId, assigned + chunk);
179
+ covered += chunk;
180
+ }
181
+ if (covered < amount) {
182
+ throw new InKindRedeemCoverageError({
183
+ required: amount,
184
+ covered,
185
+ maxExitAssets: covered,
186
+ });
187
+ }
188
+ // Account for pending performance-fee shares before previewing the burn. Once accrued, future
189
+ // V1 interest cannot lower the share price, so this upper-bounds execution.
190
+ const requiredShareAllowance = vaultData
191
+ .accrueInterest(now)
192
+ .toShares(amount);
193
+ return {
194
+ getRequirements: async () => {
195
+ const requirementsTimestamp = Time.timestamp();
196
+ if (deadline <= requirementsTimestamp) {
197
+ throw new ExpiredDeadlineError(deadline, requirementsTimestamp);
198
+ }
199
+ const [allowance, nonce, blueBalance, morpho, blueFeeRecipient] = await multicall(this.client.viemClient, {
200
+ allowFailure: false,
201
+ contracts: [
202
+ {
203
+ address: this.vault,
204
+ abi: erc20Abi,
205
+ functionName: "allowance",
206
+ args: [userAddress, vaultExitBundlesV1],
207
+ },
208
+ {
209
+ address: this.vault,
210
+ abi: erc2612Abi,
211
+ functionName: "nonces",
212
+ args: [userAddress],
213
+ },
214
+ {
215
+ address: vaultData.asset,
216
+ abi: erc20Abi,
217
+ functionName: "balanceOf",
218
+ args: [blue],
219
+ },
220
+ {
221
+ address: this.vault,
222
+ abi: metaMorphoAbi,
223
+ functionName: "MORPHO",
224
+ },
225
+ {
226
+ address: blue,
227
+ abi: blueAbi,
228
+ functionName: "feeRecipient",
229
+ },
230
+ ],
231
+ });
232
+ if (!isAddressEqual(morpho, blue)) {
233
+ throw new VaultMorphoMismatchError({
234
+ vault: this.vault,
235
+ expected: blue,
236
+ actual: morpho,
237
+ });
238
+ }
239
+ // Reject this unsupported configuration before authorizing vault shares:
240
+ // VaultExitBundlesV1 omits newly accrued Morpho fee shares when estimating the
241
+ // vault position, so its callback can exhaust marketParamsList and panic on-chain.
242
+ if (isAddressEqual(blueFeeRecipient, this.vault)) {
243
+ throw new VaultIsBlueFeeRecipientError(this.vault, blue);
244
+ }
245
+ if (blueBalance < amount) {
246
+ throw new InsufficientBlueBalanceForInKindRedeemError({
247
+ asset: vaultData.asset,
248
+ available: blueBalance,
249
+ required: amount,
250
+ });
251
+ }
252
+ if (allowance >= requiredShareAllowance)
253
+ return [];
254
+ if (this.client.options.supportSignature) {
255
+ return [
256
+ encodeVaultSharesPermit({
257
+ vault: vaultData,
258
+ version: "vaultV1",
259
+ spender: vaultExitBundlesV1,
260
+ owner: userAddress,
261
+ chainId: this.chainId,
262
+ nonce,
263
+ amount: requiredShareAllowance,
264
+ deadline,
265
+ }),
266
+ ];
267
+ }
268
+ return [
269
+ encodeErc20Approval({
270
+ token: this.vault,
271
+ spender: vaultExitBundlesV1,
272
+ amount: requiredShareAllowance,
273
+ chainId: this.chainId,
274
+ }),
275
+ ];
276
+ },
277
+ buildTx: (signatures) => {
278
+ const { permit } = selectRequirementSignatures(signatures, {
279
+ permit: true,
280
+ });
281
+ return vaultV1InKindRedeem({
282
+ vault: { chainId: this.chainId, address: this.vault },
283
+ args: {
284
+ amount,
285
+ marketParamsList: marketParamsListSnapshot,
286
+ userAddress,
287
+ deadline,
288
+ requirementSignature: permit,
289
+ },
290
+ metadata: this.client.options.metadata,
291
+ });
292
+ },
293
+ };
294
+ }
131
295
  migrateToV2({ userAddress, sourceVault, targetVault, shares, slippageTolerance = DEFAULT_SLIPPAGE_TOLERANCE, }) {
132
296
  validateChainId(this.client.viemClient.chain?.id, this.chainId);
133
297
  if (!isAddressEqual(sourceVault.address, this.vault)) {
@@ -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.
@@ -109,8 +115,7 @@ export declare class ReallocationData implements InputReallocationData {
109
115
  *
110
116
  * Pass `options.timestamp` to evaluate market interest and pending public
111
117
  * allocator caps at the same block as the fetched reallocation data.
112
- * This method does not add the legacy one-hour delay margin that
113
- * `SimulationState.getMarketPublicReallocations` applied before measuring
118
+ * This method does not add an implicit delay margin before measuring
114
119
  * target-market vault headroom. If a transaction may land later than the
115
120
  * fetched block, pass a future `timestamp` or reserve your own headroom so
116
121
  * interest accrued before inclusion does not make `reallocateTo` exceed the
@@ -132,7 +137,7 @@ export declare class ReallocationData implements InputReallocationData {
132
137
  * morphoViemExtension,
133
138
  * type PublicReallocation,
134
139
  * } from "@morpho-org/morpho-sdk";
135
- * import type { ReallocationData } from "@morpho-org/morpho-sdk/entities";
140
+ * import type { VaultV1ReallocationData } from "@morpho-org/morpho-sdk/entities";
136
141
  *
137
142
  * const client = createPublicClient({
138
143
  * chain: mainnet,
@@ -142,22 +147,35 @@ export declare class ReallocationData implements InputReallocationData {
142
147
  * const marketParams = markets[mainnet.id].usdc_wbtc;
143
148
  * const market = client.morpho.blue(marketParams, mainnet.id);
144
149
  * const block = await client.getBlock();
145
- * const reallocationData = await market.getReallocationData({
150
+ * const reallocationData = await market.getVaultV1ReallocationData({
146
151
  * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
147
152
  * block: { number: block.number, timestamp: block.timestamp },
148
153
  * });
149
154
  *
150
155
  * const result: {
151
156
  * withdrawals: readonly PublicReallocation[];
152
- * data: ReallocationData;
153
- * } = reallocationData.getMarketPublicReallocations(marketParams.id, {
157
+ * data: VaultV1ReallocationData;
158
+ * } = reallocationData.computeVaultV1Reallocations(marketParams.id, {
154
159
  * timestamp: block.timestamp,
155
160
  * });
156
161
  * ```
157
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
+ */
158
176
  getMarketPublicReallocations(marketId: MarketId, options?: PublicAllocatorOptions): {
159
177
  readonly withdrawals: readonly PublicReallocation[];
160
- data: ReallocationData;
178
+ data: VaultV1ReallocationData;
161
179
  };
162
180
  /**
163
181
  * Sums the public-allocator liquidity reallocatable into `marketId` from
@@ -187,7 +205,7 @@ export declare class ReallocationData implements InputReallocationData {
187
205
  * const marketParams = markets[mainnet.id].usdc_wbtc;
188
206
  * const market = client.morpho.blue(marketParams, mainnet.id);
189
207
  * const block = await client.getBlock();
190
- * const reallocationData = await market.getReallocationData({
208
+ * const reallocationData = await market.getVaultV1ReallocationData({
191
209
  * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
192
210
  * block: { number: block.number, timestamp: block.timestamp },
193
211
  * });
@@ -238,7 +256,7 @@ export declare class ReallocationData implements InputReallocationData {
238
256
  * const marketParams = markets[mainnet.id].usdc_wbtc;
239
257
  * const market = client.morpho.blue(marketParams, mainnet.id);
240
258
  * const block = await client.getBlock();
241
- * const reallocationData = await market.getReallocationData({
259
+ * const reallocationData = await market.getVaultV1ReallocationData({
242
260
  * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
243
261
  * block: { number: block.number, timestamp: block.timestamp },
244
262
  * });
@@ -283,5 +301,11 @@ export declare class ReallocationData implements InputReallocationData {
283
301
  readonly assets: bigint;
284
302
  };
285
303
  readonly timestamp: bigint;
286
- }): ReallocationData;
304
+ }): VaultV1ReallocationData;
287
305
  }
306
+ /**
307
+ * Deprecated class name for Vault V1 reallocation data.
308
+ *
309
+ * @deprecated Use {@link VaultV1ReallocationData} instead.
310
+ */
311
+ export { VaultV1ReallocationData as ReallocationData };
@@ -50,7 +50,7 @@ const cloneVaultMarketConfig = (config) => new VaultMarketConfig({
50
50
  * @remarks
51
51
  * The class owns only the market, vault, position, vault-market-config,
52
52
  * and chain data needed by the shared-liquidity algorithm. Constructor inputs
53
- * are cloned, and simulation steps return cloned `ReallocationData` instances
53
+ * are cloned, and simulation steps return cloned `VaultV1ReallocationData` instances
54
54
  * so fetched caller inputs are not mutated.
55
55
  *
56
56
  * Public records are exposed for inspection and snapshotting only. Treat
@@ -58,7 +58,7 @@ const cloneVaultMarketConfig = (config) => new VaultMarketConfig({
58
58
  * contract keyed by market id or address; use the getters for typed absence
59
59
  * errors and use simulation methods to produce updated state.
60
60
  */
61
- export class ReallocationData {
61
+ export class VaultV1ReallocationData {
62
62
  /** Chain id associated with the fetched reallocation data. */
63
63
  chainId;
64
64
  /** Markets indexed by market id. */
@@ -106,13 +106,13 @@ export class ReallocationData {
106
106
  /**
107
107
  * Creates a deep clone of this reallocation state.
108
108
  *
109
- * @returns A new `ReallocationData` instance with cloned entity objects.
109
+ * @returns A new `VaultV1ReallocationData` instance with cloned entity objects.
110
110
  */
111
111
  clone() {
112
- return new ReallocationData(this);
112
+ return new VaultV1ReallocationData(this);
113
113
  }
114
114
  forkAliasedState() {
115
- const data = new ReallocationData({ chainId: this.chainId });
115
+ const data = new VaultV1ReallocationData({ chainId: this.chainId });
116
116
  Object.assign(data.markets, this.markets);
117
117
  Object.assign(data.vaults, this.vaults);
118
118
  for (const [user, positions] of Object.entries(this.positions)) {
@@ -199,8 +199,7 @@ export class ReallocationData {
199
199
  *
200
200
  * Pass `options.timestamp` to evaluate market interest and pending public
201
201
  * allocator caps at the same block as the fetched reallocation data.
202
- * This method does not add the legacy one-hour delay margin that
203
- * `SimulationState.getMarketPublicReallocations` applied before measuring
202
+ * This method does not add an implicit delay margin before measuring
204
203
  * target-market vault headroom. If a transaction may land later than the
205
204
  * fetched block, pass a future `timestamp` or reserve your own headroom so
206
205
  * interest accrued before inclusion does not make `reallocateTo` exceed the
@@ -222,7 +221,7 @@ export class ReallocationData {
222
221
  * morphoViemExtension,
223
222
  * type PublicReallocation,
224
223
  * } from "@morpho-org/morpho-sdk";
225
- * import type { ReallocationData } from "@morpho-org/morpho-sdk/entities";
224
+ * import type { VaultV1ReallocationData } from "@morpho-org/morpho-sdk/entities";
226
225
  *
227
226
  * const client = createPublicClient({
228
227
  * chain: mainnet,
@@ -232,27 +231,27 @@ export class ReallocationData {
232
231
  * const marketParams = markets[mainnet.id].usdc_wbtc;
233
232
  * const market = client.morpho.blue(marketParams, mainnet.id);
234
233
  * const block = await client.getBlock();
235
- * const reallocationData = await market.getReallocationData({
234
+ * const reallocationData = await market.getVaultV1ReallocationData({
236
235
  * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
237
236
  * block: { number: block.number, timestamp: block.timestamp },
238
237
  * });
239
238
  *
240
239
  * const result: {
241
240
  * withdrawals: readonly PublicReallocation[];
242
- * data: ReallocationData;
243
- * } = reallocationData.getMarketPublicReallocations(marketParams.id, {
241
+ * data: VaultV1ReallocationData;
242
+ * } = reallocationData.computeVaultV1Reallocations(marketParams.id, {
244
243
  * timestamp: block.timestamp,
245
244
  * });
246
245
  * ```
247
246
  */
248
- getMarketPublicReallocations(marketId, options = {}) {
247
+ computeVaultV1Reallocations(marketId, options = {}) {
249
248
  const { enabled = true, timestamp, reallocatableVaults, defaultMaxWithdrawalUtilization = DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, maxWithdrawalUtilization = {}, } = options;
250
249
  if (!enabled)
251
250
  return { withdrawals: [], data: this };
252
251
  const accrualTimestamp = BigInt(timestamp ?? this.getMarket(marketId).lastUpdate);
253
252
  const configuredVaults = Object.keys(this.vaultMarketConfigs);
254
253
  const vaultKeyByLower = new Map(configuredVaults.map((vault) => [vault.toLowerCase(), vault]));
255
- const vaults = Array.from(new Set((reallocatableVaults ?? configuredVaults)
254
+ const vaults = Array.from(new Set([...(reallocatableVaults ?? configuredVaults)]
256
255
  .map((vault) => vaultKeyByLower.get(vault.toLowerCase()))
257
256
  .filter((vault) => vault != null))).filter((vault) => {
258
257
  const vaultMarketConfig = this.vaultMarketConfigs[vault]?.[marketId];
@@ -301,6 +300,18 @@ export class ReallocationData {
301
300
  });
302
301
  }
303
302
  }
303
+ /**
304
+ * Calculates Vault V1 public reallocations that can supply liquidity to `marketId`.
305
+ *
306
+ * @param marketId - Target market to supply with shared liquidity.
307
+ * @param options - Optional allocator discovery options.
308
+ * @returns Computed source-market withdrawals and the post-reallocation state.
309
+ * @throws {@link UnknownReallocationMarketError} when the target market is absent.
310
+ * @deprecated Use {@link computeVaultV1Reallocations} instead.
311
+ */
312
+ getMarketPublicReallocations(marketId, options = {}) {
313
+ return this.computeVaultV1Reallocations(marketId, options);
314
+ }
304
315
  /**
305
316
  * Sums the public-allocator liquidity reallocatable into `marketId` from
306
317
  * sibling markets.
@@ -329,7 +340,7 @@ export class ReallocationData {
329
340
  * const marketParams = markets[mainnet.id].usdc_wbtc;
330
341
  * const market = client.morpho.blue(marketParams, mainnet.id);
331
342
  * const block = await client.getBlock();
332
- * const reallocationData = await market.getReallocationData({
343
+ * const reallocationData = await market.getVaultV1ReallocationData({
333
344
  * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
334
345
  * block: { number: block.number, timestamp: block.timestamp },
335
346
  * });
@@ -341,7 +352,7 @@ export class ReallocationData {
341
352
  * ```
342
353
  */
343
354
  getPublicReallocationLiquidity(marketId, options) {
344
- const { withdrawals } = this.getMarketPublicReallocations(marketId, options);
355
+ const { withdrawals } = this.computeVaultV1Reallocations(marketId, options);
345
356
  return withdrawals.reduce((total, { assets }) => total + assets, 0n);
346
357
  }
347
358
  /**
@@ -383,7 +394,7 @@ export class ReallocationData {
383
394
  * const marketParams = markets[mainnet.id].usdc_wbtc;
384
395
  * const market = client.morpho.blue(marketParams, mainnet.id);
385
396
  * const block = await client.getBlock();
386
- * const reallocationData = await market.getReallocationData({
397
+ * const reallocationData = await market.getVaultV1ReallocationData({
387
398
  * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
388
399
  * block: { number: block.number, timestamp: block.timestamp },
389
400
  * });
@@ -514,3 +525,9 @@ export class ReallocationData {
514
525
  return data;
515
526
  }
516
527
  }
528
+ /**
529
+ * Deprecated class name for Vault V1 reallocation data.
530
+ *
531
+ * @deprecated Use {@link VaultV1ReallocationData} instead.
532
+ */
533
+ export { VaultV1ReallocationData as ReallocationData };
@@ -1,8 +1,8 @@
1
- import { type AccrualVaultV2 } from "@morpho-org/blue-sdk";
1
+ import { type AccrualVaultV2, type MarketParams } from "@morpho-org/blue-sdk";
2
2
  import { fetchAccrualVaultV2 } from "@morpho-org/blue-sdk-viem";
3
3
  import { type Address } from "viem";
4
4
  import type { FetchParameters } from "../../types/data.js";
5
- import { type Deallocation, type DepositAmountArgs, type ERC20ApprovalAction, type MorphoClientType, type PermitRequirementSignature, type Requirement, type RequirementSignature, type Transaction, type VaultV2DepositAction, type VaultV2ForceRedeemAction, type VaultV2ForceWithdrawAction, type VaultV2RedeemAction, type VaultV2WithdrawAction } from "../../types/index.js";
5
+ import { type ActionOutput, type Deallocation, type DepositAmountArgs, type ERC20ApprovalAction, type MorphoClientType, type PermitRequirementSignature, type Requirement, type RequirementSignature, type Transaction, type VaultV2DepositAction, type VaultV2ForceRedeemAction, type VaultV2ForceWithdrawAction, type VaultV2InKindRedeemAction, type VaultV2RedeemAction, type VaultV2WithdrawAction } from "../../types/index.js";
6
6
  export interface VaultV2Actions {
7
7
  /**
8
8
  * Fetches the latest vault data.
@@ -75,6 +75,83 @@ export interface VaultV2Actions {
75
75
  }) => {
76
76
  buildTx: () => Readonly<Transaction<VaultV2RedeemAction>>;
77
77
  };
78
+ /**
79
+ * Prepares an illiquid Vault V2 exit into idle assets and Morpho Blue supply positions.
80
+ *
81
+ * The vault must have exactly one `MorphoMarketV1AdapterV2`. `amount` is penalty-inclusive and
82
+ * the caller controls market order. Call `getRequirements()` before `buildTx()` so Blue balance,
83
+ * allowance, and nonce are checked on-chain. Vault gates are enforced by the final transaction
84
+ * and are not preflighted: receive gates may depend on VaultExitBundlesV1's transient initiator,
85
+ * while arbitrary send-share gates may depend on intermediate state changed by the exit's
86
+ * multiple share burns. The SDK intentionally does not validate the user's share balance; size
87
+ * it so `amount + BigInt(marketParamsList.length) <= vault.previewRedeem(sharesHeld)`. The
88
+ * per-market term covers V2 withdrawal rounding and is not needed for V1. The share allowance
89
+ * includes that buffer, the penalty burns, and accrual through the bundle deadline.
90
+ *
91
+ * Idle balance, penalty, and adapter positions can drift after the snapshot, so an on-chain
92
+ * under-coverage panic remains possible if vault state changes between preparation and inclusion.
93
+ *
94
+ * @param params - In-kind redemption parameters.
95
+ * @param params.amount - Penalty-inclusive, asset-denominated amount to exit.
96
+ * @param params.marketParamsList - Ordered adapter markets consumed greedily after idle assets;
97
+ * its length is also the V2 share-sufficiency rounding buffer.
98
+ * @param params.vaultData - Pre-fetched Vault V2 accrual snapshot.
99
+ * @param params.userAddress - Account that signs and submits the exit.
100
+ * @param params.adapter - Optional adapter override; defaults to the vault's sole adapter.
101
+ * @param params.deadline - Optional shared permit/bundle deadline; defaults to two hours from now.
102
+ * @returns Lazy prerequisite resolution and a synchronous transaction builder.
103
+ * @throws {ChainIdMismatchError} when the client and entity target different chains.
104
+ * @throws {VaultAddressMismatchError} when `vaultData` belongs to another vault.
105
+ * @throws {NonPositiveInputError} when `amount` is not positive.
106
+ * @throws {InKindRedeemZeroDeallocationError} when the vault has no idle assets and the
107
+ * penalty-adjusted amount rounds to zero deallocated assets.
108
+ * @throws {EmptyMarketParamsListError} when assets must be deallocated and the market list is empty.
109
+ * @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
110
+ * requirement resolution.
111
+ * @throws {InKindRedeemRequiresSingleAdapterError} when the vault does not have one adapter.
112
+ * @throws {AdapterNotPartOfVaultError} when `adapter` is not the vault's adapter.
113
+ * @throws {UnsupportedInKindAdapterError} when the adapter is not a MorphoMarketV1AdapterV2.
114
+ * @throws {InKindRedeemCoverageError} when the deduplicated list cannot cover the exit.
115
+ * @throws {UnsupportedChainIdError} when no address registry exists for the target chain.
116
+ * @throws {UnknownAddressError} when VaultExitBundlesV1 is not registered on the target chain.
117
+ * @throws {viem.BaseError} from `getRequirements()` when an RPC or multicall contract read fails.
118
+ * @throws {InsufficientBlueBalanceForInKindRedeemError} from `getRequirements()` when Blue cannot fund the largest callback.
119
+ * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when more than one permit signature is supplied.
120
+ * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when a non-permit signature is supplied.
121
+ * @throws {VaultExitBundlesV1PermitMismatchError} from `buildTx()` when the requirement has the wrong permit kind, asset, or signature encoding.
122
+ * @example
123
+ * ```ts
124
+ * import { isRequirementSignature } from "@morpho-org/morpho-sdk";
125
+ *
126
+ * const vault = client.morpho.vaultV2(vaultAddress, 1);
127
+ * const vaultData = await vault.getData();
128
+ * const exit = vault.inKindRedeem({
129
+ * amount: 1_000_000n,
130
+ * marketParamsList,
131
+ * vaultData,
132
+ * userAddress,
133
+ * });
134
+ * const signatures = [];
135
+ * for (const requirement of await exit.getRequirements()) {
136
+ * if (isRequirementSignature(requirement)) {
137
+ * signatures.push(await requirement.sign(walletClient, userAddress));
138
+ * } else {
139
+ * const hash = await walletClient.sendTransaction(requirement);
140
+ * await client.waitForTransactionReceipt({ hash });
141
+ * }
142
+ * }
143
+ * const tx = exit.buildTx(signatures);
144
+ * // tx satisfies Readonly<Transaction<VaultV2InKindRedeemAction>>
145
+ * ```
146
+ */
147
+ readonly inKindRedeem: (params: {
148
+ readonly amount: bigint;
149
+ readonly marketParamsList: readonly MarketParams[];
150
+ readonly vaultData: AccrualVaultV2;
151
+ readonly userAddress: Address;
152
+ readonly adapter?: Address;
153
+ readonly deadline?: bigint;
154
+ }) => ActionOutput<VaultV2InKindRedeemAction, readonly RequirementSignature[], undefined>;
78
155
  /**
79
156
  * Prepares a force withdraw transaction for the VaultV2 contract using the vault's native multicall.
80
157
  *
@@ -159,6 +236,15 @@ export declare class MorphoVaultV2 implements VaultV2Actions {
159
236
  }): {
160
237
  buildTx: () => Readonly<Transaction<VaultV2RedeemAction>>;
161
238
  };
239
+ /** {@inheritDoc VaultV2Actions.inKindRedeem} */
240
+ inKindRedeem({ amount, marketParamsList, vaultData, userAddress, adapter: adapterOverride, deadline: deadlineOverride, }: {
241
+ readonly amount: bigint;
242
+ readonly marketParamsList: readonly MarketParams[];
243
+ readonly vaultData: AccrualVaultV2;
244
+ readonly userAddress: Address;
245
+ readonly adapter?: Address;
246
+ readonly deadline?: bigint;
247
+ }): ActionOutput<VaultV2InKindRedeemAction, readonly RequirementSignature[], undefined>;
162
248
  forceWithdraw({ deallocations, withdraw, userAddress, }: {
163
249
  deallocations: readonly Deallocation[];
164
250
  withdraw: {