@morpho-org/morpho-sdk 6.0.0-next.4 → 6.1.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 (121) hide show
  1. package/MIGRATION-v5-to-v6.md +21 -15
  2. package/README.md +7 -5
  3. package/lib/cjs/abis.d.ts +1 -1
  4. package/lib/cjs/abis.js +3 -1
  5. package/lib/cjs/actions/bundles/common.d.ts +18 -23
  6. package/lib/cjs/actions/bundles/common.js +36 -83
  7. package/lib/cjs/actions/bundles/index.d.ts +1 -1
  8. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +6 -2
  9. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +6 -2
  10. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +1 -0
  11. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +1 -0
  12. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +11 -3
  13. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +19 -1
  14. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +4 -1
  15. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +12 -0
  16. package/lib/cjs/actions/vaultV1/migrateToV2.d.ts +62 -17
  17. package/lib/cjs/actions/vaultV1/migrateToV2.js +48 -17
  18. package/lib/cjs/actions/vaultV1/redeem.d.ts +28 -1
  19. package/lib/cjs/actions/vaultV1/redeem.js +17 -1
  20. package/lib/cjs/actions/vaultV1/withdraw.d.ts +1 -1
  21. package/lib/cjs/actions/vaultV1/withdraw.js +1 -1
  22. package/lib/cjs/actions/vaultV2/forceWithdraw.d.ts +19 -7
  23. package/lib/cjs/actions/vaultV2/forceWithdraw.js +19 -7
  24. package/lib/cjs/actions/vaultV2/redeem.d.ts +28 -1
  25. package/lib/cjs/actions/vaultV2/redeem.js +17 -1
  26. package/lib/cjs/actions/vaultV2/withdraw.d.ts +1 -1
  27. package/lib/cjs/actions/vaultV2/withdraw.js +1 -1
  28. package/lib/cjs/constants.d.ts +1 -1
  29. package/lib/cjs/constants.js +4 -1
  30. package/lib/cjs/entities/blue/blue.d.ts +28 -0
  31. package/lib/cjs/entities/blue/blue.js +1 -0
  32. package/lib/cjs/entities/index.d.ts +1 -1
  33. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +38 -22
  34. package/lib/cjs/entities/vaultV1/vaultV1.js +51 -84
  35. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +68 -35
  36. package/lib/cjs/entities/vaultV2/vaultV2.js +45 -72
  37. package/lib/cjs/errors.d.ts +1 -1
  38. package/lib/cjs/errors.js +8 -1
  39. package/lib/cjs/helpers/previewVaultV2ForceWithdraw.d.ts +10 -6
  40. package/lib/cjs/helpers/previewVaultV2ForceWithdraw.js +10 -6
  41. package/lib/cjs/helpers/slippage.d.ts +3 -2
  42. package/lib/cjs/helpers/slippage.js +3 -2
  43. package/lib/cjs/helpers/validate.d.ts +40 -0
  44. package/lib/cjs/helpers/validate.js +40 -0
  45. package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.d.ts +94 -59
  46. package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.js +94 -59
  47. package/lib/cjs/midnight/abis.d.ts +1 -1
  48. package/lib/cjs/midnight/abis.js +3 -1
  49. package/lib/cjs/midnight/constants.d.ts +1 -1
  50. package/lib/cjs/midnight/constants.js +4 -1
  51. package/lib/cjs/midnight/entities.d.ts +1 -1
  52. package/lib/cjs/midnight/errors.d.ts +1 -1
  53. package/lib/cjs/midnight/errors.js +7 -1
  54. package/lib/cjs/midnight/types.d.ts +1 -1
  55. package/lib/cjs/midnight/utils.d.ts +11 -1
  56. package/lib/cjs/midnight/utils.js +13 -1
  57. package/lib/cjs/types/action.d.ts +35 -0
  58. package/lib/cjs/types/action.js +10 -0
  59. package/lib/cjs/types.d.ts +1 -1
  60. package/lib/cjs/utils.d.ts +11 -1
  61. package/lib/cjs/utils.js +14 -2
  62. package/lib/esm/abis.d.ts +1 -1
  63. package/lib/esm/abis.js +1 -1
  64. package/lib/esm/actions/bundles/common.d.ts +18 -23
  65. package/lib/esm/actions/bundles/common.js +34 -80
  66. package/lib/esm/actions/bundles/index.d.ts +1 -1
  67. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +6 -2
  68. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +6 -2
  69. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +1 -0
  70. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +1 -0
  71. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +11 -3
  72. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +21 -3
  73. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +4 -1
  74. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +14 -2
  75. package/lib/esm/actions/vaultV1/migrateToV2.d.ts +62 -17
  76. package/lib/esm/actions/vaultV1/migrateToV2.js +48 -17
  77. package/lib/esm/actions/vaultV1/redeem.d.ts +28 -1
  78. package/lib/esm/actions/vaultV1/redeem.js +17 -1
  79. package/lib/esm/actions/vaultV1/withdraw.d.ts +1 -1
  80. package/lib/esm/actions/vaultV1/withdraw.js +1 -1
  81. package/lib/esm/actions/vaultV2/forceWithdraw.d.ts +19 -7
  82. package/lib/esm/actions/vaultV2/forceWithdraw.js +19 -7
  83. package/lib/esm/actions/vaultV2/redeem.d.ts +28 -1
  84. package/lib/esm/actions/vaultV2/redeem.js +17 -1
  85. package/lib/esm/actions/vaultV2/withdraw.d.ts +1 -1
  86. package/lib/esm/actions/vaultV2/withdraw.js +1 -1
  87. package/lib/esm/constants.d.ts +1 -1
  88. package/lib/esm/constants.js +1 -1
  89. package/lib/esm/entities/blue/blue.d.ts +28 -0
  90. package/lib/esm/entities/blue/blue.js +1 -0
  91. package/lib/esm/entities/index.d.ts +1 -1
  92. package/lib/esm/entities/vaultV1/vaultV1.d.ts +38 -22
  93. package/lib/esm/entities/vaultV1/vaultV1.js +53 -86
  94. package/lib/esm/entities/vaultV2/vaultV2.d.ts +68 -35
  95. package/lib/esm/entities/vaultV2/vaultV2.js +47 -74
  96. package/lib/esm/errors.d.ts +1 -1
  97. package/lib/esm/errors.js +1 -1
  98. package/lib/esm/helpers/previewVaultV2ForceWithdraw.d.ts +10 -6
  99. package/lib/esm/helpers/previewVaultV2ForceWithdraw.js +10 -6
  100. package/lib/esm/helpers/slippage.d.ts +3 -2
  101. package/lib/esm/helpers/slippage.js +3 -2
  102. package/lib/esm/helpers/validate.d.ts +40 -0
  103. package/lib/esm/helpers/validate.js +40 -0
  104. package/lib/esm/helpers/vaultV2ForceWithdrawPlan.d.ts +94 -59
  105. package/lib/esm/helpers/vaultV2ForceWithdrawPlan.js +94 -59
  106. package/lib/esm/midnight/abis.d.ts +1 -1
  107. package/lib/esm/midnight/abis.js +1 -1
  108. package/lib/esm/midnight/constants.d.ts +1 -1
  109. package/lib/esm/midnight/constants.js +1 -1
  110. package/lib/esm/midnight/entities.d.ts +1 -1
  111. package/lib/esm/midnight/errors.d.ts +1 -1
  112. package/lib/esm/midnight/errors.js +1 -1
  113. package/lib/esm/midnight/types.d.ts +1 -1
  114. package/lib/esm/midnight/utils.d.ts +11 -1
  115. package/lib/esm/midnight/utils.js +11 -1
  116. package/lib/esm/types/action.d.ts +35 -0
  117. package/lib/esm/types/action.js +10 -0
  118. package/lib/esm/types.d.ts +1 -1
  119. package/lib/esm/utils.d.ts +11 -1
  120. package/lib/esm/utils.js +11 -1
  121. package/package.json +5 -5
@@ -3,12 +3,19 @@ import { fetchAccrualVault } from "@morpho-org/blue-sdk-viem";
3
3
  import { type Address } from "viem";
4
4
  import type { FetchParameters } from "../../types/data.js";
5
5
  import { type ActionOutput, type ActionRequirement, type BundlesFundingArgs, type BundlesTokenRequirementsOptions, type MorphoClientType, type RequirementSignature, type VaultV1DepositAction, type VaultV1InKindRedeemAction, type VaultV1MigrateToV2Action, type VaultV1MigrateToV2AmountArgs, type VaultV1RedeemAction, type VaultV1WithdrawAction } from "../../types/index.js";
6
+ /** Action surface for Vault V1 reads and writes; writes route through VaultBundlesV1 and VaultExitBundlesV1. */
6
7
  export interface VaultV1Actions {
7
8
  /**
8
9
  * Fetches direct onchain vault and allocation state without applying virtual interest.
9
10
  *
10
- * @param {FetchParameters} [parameters] - Optional fetch parameters (block number, state overrides, etc.).
11
- * @returns {Promise<Awaited<ReturnType<typeof fetchAccrualVault>>>} The requested vault state.
11
+ * Reads the Vault V1 state through `fetchAccrualVault` on the entity's client.
12
+ *
13
+ * @param parameters - Optional viem fetch parameters (block number, block tag, state override).
14
+ * @returns The hydrated `AccrualVault` snapshot.
15
+ * @throws {ChainIdMismatchError} when the connected client targets another chain or has no chain.
16
+ * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
17
+ * @throws {UnknownBlueFactory} when the configured chain has no MetaMorpho factory.
18
+ * @throws {UnknownBlueOfFactory} when the vault is not a MetaMorpho vault from the configured factory.
12
19
  */
13
20
  getData: (parameters?: FetchParameters) => Promise<Awaited<ReturnType<typeof fetchAccrualVault>>>;
14
21
  /**
@@ -19,7 +26,7 @@ export interface VaultV1Actions {
19
26
  * Permit2 nonce state. Native funding is exclusive and skips token requirements. Shares are
20
27
  * always minted to the transaction sender, which must be `userAddress`.
21
28
  * Concurrent requirement reads share the first caller's options; later calls refresh on-chain
22
- * state using their own options. `buildTx()` accepts signatures from the latest completed read.
29
+ * state using their own options.
23
30
  *
24
31
  * @param params.userAddress - Account that funds, signs, submits, and receives the vault shares.
25
32
  * @param params.vaultData - Pre-fetched Vault V1 snapshot used for asset and share conversion.
@@ -55,8 +62,9 @@ export interface VaultV1Actions {
55
62
  * `getRequirements()` when the Permit2 nonce exceeds uint256.
56
63
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when multiple token signatures are supplied.
57
64
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when an unsupported signature is supplied.
58
- * @throws {BundlesPermitMismatchError} from `buildTx()` when the signature was not produced for
59
- * this prepared handle.
65
+ * @throws {BundlesPermitMismatchError} from `buildTx()` when the signature's spender, amount or
66
+ * deadline differ from this operation's, or when the signature's carried action nonce is
67
+ * inconsistent with its signed nonce.
60
68
  * @throws {DepositOwnerMismatchError} from `buildTx()` when the signed owner differs from `userAddress`.
61
69
  * @throws {DepositAssetMismatchError} from `buildTx()` when the signed asset differs from the vault asset.
62
70
  * @throws {BundlesRequirementSignatureMismatchError} from `buildTx()` when signature metadata is malformed.
@@ -95,13 +103,14 @@ export interface VaultV1Actions {
95
103
  /**
96
104
  * Prepares an exact-assets Vault V1 withdrawal through VaultBundlesV1.
97
105
  *
98
- * Reads the vault accrual state on the first `getRequirements()` call, then reads the live
99
- * vault-share allowance and, when a signature is needed, the permit nonce on each call.
100
- * Captures the requested amount and owner at creation, so later changes to `params` do not
101
- * change this handle's requirements or transaction.
106
+ * Derives the share-burn cap from the supplied `vaultData` snapshot at handle creation, then
107
+ * reads the live vault-share allowance and, when a signature is needed, the permit nonce on
108
+ * each `getRequirements()` call. Captures the requested amount and owner at creation, so later
109
+ * changes to `params` do not change this handle's requirements or transaction.
102
110
  *
103
111
  * @param params.amount - Positive gross withdrawal in underlying asset base units, before fees.
104
112
  * @param params.userAddress - Share owner that must sign and submit; receives the net assets.
113
+ * @param params.vaultData - Pre-fetched Vault V1 snapshot used to derive the share cap.
105
114
  * @param params.slippageTolerance - Optional WAD-scaled share-price loss tolerance applied to
106
115
  * the share cap for MetaMorpho 1.0; MetaMorpho 1.1 retains its lost-assets clamp.
107
116
  * Defaults to 0.03% and cannot exceed 10%.
@@ -114,8 +123,11 @@ export interface VaultV1Actions {
114
123
  * which returns a deep-frozen `Transaction<VaultV1WithdrawAction>`. Requirements are empty
115
124
  * when the allowance equals the cap; an oversized allowance is always reset with an exact
116
125
  * onchain approval, and an insufficient one is raised by an approval or, with signature
117
- * support, an ERC-2612 request. The cap stays pinned to the first resolution while each call
118
- * re-reads the allowance. Confirm the approval or pass its signed permit to `buildTx`.
126
+ * support, an ERC-2612 request. The cap is fixed at handle creation while each call re-reads
127
+ * the allowance. Confirm the approval or pass its signed permit to `buildTx`.
128
+ * @remarks VaultBundlesV1 skips a share permit whose nonce was already consumed and proceeds
129
+ * under the live allowance, so execute every requirement returned by the latest
130
+ * `getRequirements()` — including the oversized-allowance reset — before submitting.
119
131
  * @throws {ChainIdMismatchError} when the connected client targets another chain.
120
132
  * @throws {NonPositiveInputError} when `amount` or the computed share cap is not positive.
121
133
  * @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
@@ -125,14 +137,14 @@ export interface VaultV1Actions {
125
137
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is at least WAD.
126
138
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no nonzero recipient.
127
139
  * @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` exceeds 10%.
140
+ * @throws {VaultAddressMismatchError} when `vaultData` belongs to another vault.
128
141
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
129
142
  * @throws {UnknownAddressError} when VaultBundlesV1 is not registered on the target chain.
130
143
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when an unsupported signature is supplied.
131
144
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when multiple permits are supplied.
132
- * @throws {BundlesPermitMismatchError} from `buildTx()` when the share permit is malformed,
133
- * was not resolved for this handle, or has incompatible vault, owner, spender, amount,
134
- * deadline, or nonce values.
135
- * @throws {viem.BaseError} when a vault, allowance, or nonce read or transaction encoding fails.
145
+ * @throws {BundlesPermitMismatchError} from `buildTx()` when the share permit is malformed or
146
+ * its spender, amount or deadline differ from this operation's.
147
+ * @throws {viem.BaseError} when an allowance or nonce read or transaction encoding fails.
136
148
  * @example
137
149
  * ```ts
138
150
  * import { morphoViemExtension } from "@morpho-org/morpho-sdk";
@@ -144,7 +156,8 @@ export interface VaultV1Actions {
144
156
  * const client = createPublicClient({ chain: mainnet, transport: http() })
145
157
  * .extend(morphoViemExtension({ supportSignature: false }));
146
158
  * const vault = client.morpho.vaultV1(vaultAddress, mainnet.id);
147
- * const action = vault.withdraw({ amount: 1_000_000n, userAddress });
159
+ * const vaultData = await vault.getData();
160
+ * const action = vault.withdraw({ amount: 1_000_000n, userAddress, vaultData });
148
161
  * const requirements = await action.getRequirements();
149
162
  * // Send and confirm each approval before calling action.buildTx().
150
163
  * // action.buildTx() returns Readonly<Transaction<VaultV1WithdrawAction>>.
@@ -155,6 +168,7 @@ export interface VaultV1Actions {
155
168
  withdraw: (params: {
156
169
  readonly amount: bigint;
157
170
  readonly userAddress: Address;
171
+ readonly vaultData: AccrualVault;
158
172
  readonly slippageTolerance?: bigint;
159
173
  readonly referralFeePct?: bigint;
160
174
  readonly referralFeeRecipient?: Address;
@@ -166,7 +180,6 @@ export interface VaultV1Actions {
166
180
  * Captures `shares` and `userAddress` at handle creation for both requirements and `buildTx()`.
167
181
  * The caller must satisfy the exact vault-share allowance returned by `getRequirements()` before
168
182
  * `buildTx()`; every requirement resolution re-reads the live allowance and checks the deadline.
169
- * `buildTx()` accepts permits from the latest completed requirement resolution.
170
183
  *
171
184
  * @param params.shares - Exact vault shares to burn.
172
185
  * @param params.userAddress - Account that signs and submits the transaction; VaultBundlesV1
@@ -180,6 +193,7 @@ export interface VaultV1Actions {
180
193
  * @throws {ChainIdMismatchError} when the connected client targets another chain.
181
194
  * @throws {NonPositiveInputError} when `shares` is not positive.
182
195
  * @throws {ExpiredDeadlineError} when the deadline is stale at creation or requirement resolution.
196
+ * @throws {InputExceedsMaxError} when `shares` or `deadline` exceeds uint256.
183
197
  * @throws {NegativeInputError} when `referralFeePct` is negative.
184
198
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is at least WAD.
185
199
  * @throws {ReferralFeeRecipientMissingError} when a positive fee has no non-zero recipient.
@@ -188,8 +202,8 @@ export interface VaultV1Actions {
188
202
  * @throws {viem.BaseError} from `getRequirements()` when an allowance or permit-nonce read fails.
189
203
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when multiple permit signatures are supplied.
190
204
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when a non-permit signature is supplied.
191
- * @throws {BundlesPermitMismatchError} from `buildTx()` when the supplied permit does not match
192
- * the resolved share amount, spender, owner, nonce, or deadline.
205
+ * @throws {BundlesPermitMismatchError} from `buildTx()` when the signature's spender, amount or
206
+ * deadline differ from this operation's.
193
207
  * @example
194
208
  * ```ts
195
209
  * import { vaults } from "@morpho-org/morpho-test";
@@ -314,7 +328,6 @@ export interface VaultV1Actions {
314
328
  * VaultBundlesV1. Only the destination deposit has an onchain share-price bound. The caller must
315
329
  * satisfy the exact source-vault share requirement before building the transaction.
316
330
  * Captures vault identities, permit-domain metadata, and the owner at handle creation.
317
- * `buildTx()` accepts permits from the latest completed requirement resolution.
318
331
  *
319
332
  * @param params.userAddress - Account that signs and submits the transaction; VaultBundlesV1
320
333
  * burns and mints shares for `msg.sender`.
@@ -347,6 +360,7 @@ export interface VaultV1Actions {
347
360
  * @throws {NegativeInputError} when slippage tolerance or `referralFeePct` is negative.
348
361
  * @throws {ExcessiveSlippageToleranceError} when slippage tolerance exceeds the SDK maximum.
349
362
  * @throws {ExpiredDeadlineError} when the deadline is stale at creation or requirement resolution.
363
+ * @throws {InputExceedsMaxError} when the selected amount, `maxSharePriceVaultV2`, or `deadline` exceeds uint256.
350
364
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is at least WAD.
351
365
  * @throws {ReferralFeeRecipientMissingError} when a positive fee has no non-zero recipient.
352
366
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
@@ -354,8 +368,8 @@ export interface VaultV1Actions {
354
368
  * @throws {viem.BaseError} from `getRequirements()` when an allowance or permit-nonce read fails.
355
369
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when multiple permit signatures are supplied.
356
370
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when a non-permit signature is supplied.
357
- * @throws {BundlesPermitMismatchError} from `buildTx()` when the supplied permit does not match
358
- * the prepared source share cap, spender, owner, nonce, or deadline.
371
+ * @throws {BundlesPermitMismatchError} from `buildTx()` when the signature's spender, amount or
372
+ * deadline differ from this operation's.
359
373
  * @example
360
374
  * ```ts
361
375
  * import { vaults } from "@morpho-org/morpho-test";
@@ -406,6 +420,7 @@ export interface VaultV1Actions {
406
420
  readonly deadline?: bigint;
407
421
  } & VaultV1MigrateToV2AmountArgs) => ActionOutput<VaultV1MigrateToV2Action, readonly RequirementSignature[], undefined>;
408
422
  }
423
+ /** Binds a viem client to a Vault V1 (MetaMorpho) vault's action builders. */
409
424
  export declare class MorphoVaultV1 implements VaultV1Actions {
410
425
  private readonly client;
411
426
  private readonly vault;
@@ -427,6 +442,7 @@ export declare class MorphoVaultV1 implements VaultV1Actions {
427
442
  withdraw(params: {
428
443
  readonly amount: bigint;
429
444
  readonly userAddress: Address;
445
+ readonly vaultData: AccrualVault;
430
446
  readonly slippageTolerance?: bigint;
431
447
  readonly referralFeePct?: bigint;
432
448
  readonly referralFeeRecipient?: Address;
@@ -13,6 +13,7 @@ const validate_js_1 = require("../../helpers/validate.js");
13
13
  const index_js_3 = require("../../types/index.js");
14
14
  const getVaultBundlesSharesRequirements_js_1 = require("../requirements/getVaultBundlesSharesRequirements.js");
15
15
  const index_js_4 = require("../requirements/index.js");
16
+ /** Binds a viem client to a Vault V1 (MetaMorpho) vault's action builders. */
16
17
  class MorphoVaultV1 {
17
18
  client;
18
19
  vault;
@@ -24,10 +25,7 @@ class MorphoVaultV1 {
24
25
  this.chainId = chainId;
25
26
  }
26
27
  async getData(parameters) {
27
- if (this.client.viemClient.chain?.id &&
28
- this.client.viemClient.chain?.id !== this.chainId) {
29
- throw new index_js_3.ChainIdMismatchError(this.client.viemClient.chain?.id, this.chainId);
30
- }
28
+ (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
31
29
  return (0, blue_sdk_viem_1.fetchAccrualVault)(this.vault, this.client.viemClient, {
32
30
  ...parameters,
33
31
  deployless: this.client.options.supportDeployless,
@@ -66,7 +64,6 @@ class MorphoVaultV1 {
66
64
  });
67
65
  const spender = (0, morpho_ts_1.getChainAddress)(this.chainId, "bundles.vaultBundlesV1");
68
66
  let pendingRequirements;
69
- let expectedRequirement;
70
67
  return Object.freeze({
71
68
  getRequirements: async (requirementOptions) => {
72
69
  const now = morpho_ts_1.Time.timestamp();
@@ -74,33 +71,22 @@ class MorphoVaultV1 {
74
71
  throw new index_js_3.ExpiredDeadlineError(deadline, now);
75
72
  if (pendingRequirements != null)
76
73
  return await pendingRequirements;
77
- // Memoize the in-flight promise, not just its result: concurrent callers
78
- // requesting different routes would otherwise both resolve requirements and
79
- // the slower one would overwrite `expectedRequirement`, making `buildTx()`
80
- // reject the signature returned by the other call.
81
- const pending = (async () => {
82
- const requirements = funding.value > 0n
83
- ? []
84
- : await (0, index_js_4.getBundlesTokenRequirements)(this.client.viemClient, {
85
- token: vaultAsset,
86
- spender,
87
- amount: funding.assets,
88
- owner: userAddress,
89
- chainId: this.chainId,
90
- deadline,
91
- supportSignature: this.client.options.supportSignature,
92
- supportDeployless: this.client.options.supportDeployless,
93
- useSimplePermit: requirementOptions?.useSimplePermit,
94
- permit2Nonce: requirementOptions?.permit2Nonce,
95
- });
96
- const signatureRequirement = requirements.find(index_js_3.isRequirementSignature);
97
- expectedRequirement =
98
- signatureRequirement?.action.type === "permit" ||
99
- signatureRequirement?.action.type === "permit2SignatureTransfer"
100
- ? signatureRequirement.action
101
- : undefined;
102
- return requirements;
103
- })();
74
+ // Memoize the in-flight promise so concurrent callers share one round of
75
+ // allowance/nonce reads.
76
+ const pending = funding.value > 0n
77
+ ? Promise.resolve([])
78
+ : (0, index_js_4.getBundlesTokenRequirements)(this.client.viemClient, {
79
+ token: vaultAsset,
80
+ spender,
81
+ amount: funding.assets,
82
+ owner: userAddress,
83
+ chainId: this.chainId,
84
+ deadline,
85
+ supportSignature: this.client.options.supportSignature,
86
+ supportDeployless: this.client.options.supportDeployless,
87
+ useSimplePermit: requirementOptions?.useSimplePermit,
88
+ permit2Nonce: requirementOptions?.permit2Nonce,
89
+ });
104
90
  pendingRequirements = pending;
105
91
  try {
106
92
  return await pending;
@@ -112,7 +98,8 @@ class MorphoVaultV1 {
112
98
  }
113
99
  },
114
100
  buildTx: (signatures) => {
115
- const requirementSignature = (0, common_js_1.selectBundlesTokenRequirementSignature)(signatures, expectedRequirement);
101
+ // The pure deposit action rejects a token signature supplied alongside native funding.
102
+ const requirementSignature = (0, common_js_1.selectBundlesTokenRequirementSignature)(signatures, { spender, amount: funding.assets, deadline });
116
103
  return (0, index_js_1.vaultV1Deposit)({
117
104
  vault: {
118
105
  chainId: this.chainId,
@@ -136,8 +123,11 @@ class MorphoVaultV1 {
136
123
  });
137
124
  }
138
125
  withdraw(params) {
139
- const { amount, userAddress } = params;
126
+ const { amount, userAddress, vaultData } = params;
140
127
  (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
128
+ if (!(0, viem_1.isAddressEqual)(vaultData.address, this.vault)) {
129
+ throw new index_js_3.VaultAddressMismatchError(this.vault, vaultData.address);
130
+ }
141
131
  if (amount <= 0n)
142
132
  throw new index_js_3.NonPositiveInputError("amount", amount);
143
133
  // Reject values outside the ABI range before resolving any requirements.
@@ -153,11 +143,14 @@ class MorphoVaultV1 {
153
143
  });
154
144
  const slippageTolerance = params.slippageTolerance ?? blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE;
155
145
  (0, index_js_2.validateSlippageTolerance)(slippageTolerance);
156
- // Fail eagerly if VaultBundlesV1 is unavailable; only validation is needed here.
157
- (0, morpho_ts_1.getChainAddress)(this.chainId, "bundles.vaultBundlesV1");
158
- let requiredShareAllowance;
159
- let vaultSnapshot;
160
- let expectedRequirement;
146
+ // Resolve the spender eagerly so an unregistered VaultBundlesV1 fails at handle creation.
147
+ const spender = (0, morpho_ts_1.getChainAddress)(this.chainId, "bundles.vaultBundlesV1");
148
+ const requiredShareAllowance = (0, index_js_2.computeVaultMaxShareAllowance)({
149
+ vaultData,
150
+ deadline,
151
+ assets: amount,
152
+ slippageTolerance,
153
+ });
161
154
  return Object.freeze({
162
155
  getRequirements: async () => {
163
156
  const now = morpho_ts_1.Time.timestamp();
@@ -166,18 +159,9 @@ class MorphoVaultV1 {
166
159
  // Re-read the live share allowance on every call instead of caching the resolved
167
160
  // requirements: the allowance is the sole cap on the burn, so a caller that executed the
168
161
  // returned approval must see it satisfied on the next call, and an allowance revoked or
169
- // raised afterwards must resurface as an outstanding requirement. Only the vault snapshot
170
- // and the cap derived from it are pinned, so re-reading cannot move the cap this handle
171
- // already committed to; the snapshot is used for immutable identity and permit-domain
172
- // fields only.
173
- const vaultData = (vaultSnapshot ??= await this.getData());
174
- requiredShareAllowance ??= (0, index_js_2.computeVaultMaxShareAllowance)({
175
- vaultData,
176
- deadline,
177
- assets: amount,
178
- slippageTolerance,
179
- });
180
- const requirements = await (0, getVaultBundlesSharesRequirements_js_1.getVaultBundlesSharesRequirements)(this.client.viemClient, {
162
+ // raised afterwards must resurface as an outstanding requirement. The cap derived from
163
+ // `vaultData` is pinned at handle creation, so re-reading cannot move it.
164
+ return (0, getVaultBundlesSharesRequirements_js_1.getVaultBundlesSharesRequirements)(this.client.viemClient, {
181
165
  vaultData,
182
166
  version: "vaultV1",
183
167
  owner: userAddress,
@@ -186,17 +170,12 @@ class MorphoVaultV1 {
186
170
  deadline,
187
171
  supportSignature: this.client.options.supportSignature,
188
172
  });
189
- const signatureRequirement = requirements.find(index_js_3.isRequirementSignature);
190
- expectedRequirement =
191
- signatureRequirement?.action.type === "permit"
192
- ? signatureRequirement.action
193
- : undefined;
194
- return requirements;
195
173
  },
196
174
  buildTx: (signatures) => {
197
- const permit = (0, common_js_1.selectBundlesSharesRequirementSignature)(signatures, {
198
- requiredShareAllowance,
199
- expectedRequirement,
175
+ const permit = (0, common_js_1.selectBundlesSharesPermitSignature)(signatures, {
176
+ spender,
177
+ amount: requiredShareAllowance,
178
+ deadline,
200
179
  });
201
180
  return (0, index_js_1.vaultV1Withdraw)({
202
181
  vault: { chainId: this.chainId, address: this.vault },
@@ -228,14 +207,13 @@ class MorphoVaultV1 {
228
207
  referralFeePct: params.referralFeePct,
229
208
  referralFeeRecipient: params.referralFeeRecipient,
230
209
  });
231
- (0, morpho_ts_1.getChainAddress)(this.chainId, "bundles.vaultBundlesV1");
232
- let expectedRequirement;
210
+ const spender = (0, morpho_ts_1.getChainAddress)(this.chainId, "bundles.vaultBundlesV1");
233
211
  return Object.freeze({
234
212
  getRequirements: async () => {
235
213
  const now = morpho_ts_1.Time.timestamp();
236
214
  if (deadline <= now)
237
215
  throw new index_js_3.ExpiredDeadlineError(deadline, now);
238
- const requirements = await (0, getVaultBundlesSharesRequirements_js_1.getVaultBundlesSharesRequirements)(this.client.viemClient, {
216
+ return (0, getVaultBundlesSharesRequirements_js_1.getVaultBundlesSharesRequirements)(this.client.viemClient, {
239
217
  vaultData: await this.getData(),
240
218
  version: "vaultV1",
241
219
  owner: userAddress,
@@ -244,17 +222,12 @@ class MorphoVaultV1 {
244
222
  deadline,
245
223
  supportSignature: this.client.options.supportSignature,
246
224
  });
247
- const signatureRequirement = requirements.find(index_js_3.isRequirementSignature);
248
- expectedRequirement =
249
- signatureRequirement?.action.type === "permit"
250
- ? signatureRequirement.action
251
- : undefined;
252
- return requirements;
253
225
  },
254
226
  buildTx: (signatures) => {
255
- const permit = (0, common_js_1.selectBundlesSharesRequirementSignature)(signatures, {
256
- requiredShareAllowance: shares,
257
- expectedRequirement,
227
+ const permit = (0, common_js_1.selectBundlesSharesPermitSignature)(signatures, {
228
+ spender,
229
+ amount: shares,
230
+ deadline,
258
231
  });
259
232
  return (0, index_js_1.vaultV1Redeem)({
260
233
  vault: { chainId: this.chainId, address: this.vault },
@@ -497,7 +470,7 @@ class MorphoVaultV1 {
497
470
  assets: assets ?? 0n,
498
471
  slippageTolerance,
499
472
  });
500
- (0, morpho_ts_1.getChainAddress)(this.chainId, "bundles.vaultBundlesV1");
473
+ const spender = (0, morpho_ts_1.getChainAddress)(this.chainId, "bundles.vaultBundlesV1");
501
474
  const { userAddress } = params;
502
475
  const sourceAsset = params.sourceVault.asset;
503
476
  const targetAddress = params.targetVault.address;
@@ -513,7 +486,6 @@ class MorphoVaultV1 {
513
486
  extensions: [...domain.extensions],
514
487
  }),
515
488
  });
516
- let expectedRequirement;
517
489
  return Object.freeze({
518
490
  getRequirements: async () => {
519
491
  const now = morpho_ts_1.Time.timestamp();
@@ -525,7 +497,7 @@ class MorphoVaultV1 {
525
497
  // satisfied on the next call, and an allowance revoked or raised afterwards must resurface
526
498
  // as an outstanding requirement. The pinned snapshot and the cap derived from it stay
527
499
  // fixed, so re-reading cannot move the cap this handle already committed to.
528
- const requirements = await (0, getVaultBundlesSharesRequirements_js_1.getVaultBundlesSharesRequirements)(this.client.viemClient, {
500
+ return (0, getVaultBundlesSharesRequirements_js_1.getVaultBundlesSharesRequirements)(this.client.viemClient, {
529
501
  vaultData: sourceToken,
530
502
  version: "vaultV1",
531
503
  owner: userAddress,
@@ -534,17 +506,12 @@ class MorphoVaultV1 {
534
506
  deadline,
535
507
  supportSignature: this.client.options.supportSignature,
536
508
  });
537
- const signatureRequirement = requirements.find(index_js_3.isRequirementSignature);
538
- expectedRequirement =
539
- signatureRequirement?.action.type === "permit"
540
- ? signatureRequirement.action
541
- : undefined;
542
- return requirements;
543
509
  },
544
510
  buildTx: (signatures) => {
545
- const permit = (0, common_js_1.selectBundlesSharesRequirementSignature)(signatures, {
546
- requiredShareAllowance,
547
- expectedRequirement,
511
+ const permit = (0, common_js_1.selectBundlesSharesPermitSignature)(signatures, {
512
+ spender,
513
+ amount: requiredShareAllowance,
514
+ deadline,
548
515
  });
549
516
  return (0, index_js_1.vaultV1MigrateToV2)({
550
517
  vault: {