@morpho-org/morpho-sdk 6.0.0-next.4 → 6.0.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 (59) hide show
  1. package/MIGRATION-v5-to-v6.md +20 -14
  2. package/README.md +6 -4
  3. package/lib/cjs/actions/bundles/common.d.ts +18 -23
  4. package/lib/cjs/actions/bundles/common.js +36 -83
  5. package/lib/cjs/actions/bundles/index.d.ts +1 -1
  6. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +6 -2
  7. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +6 -2
  8. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +1 -0
  9. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +1 -0
  10. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +11 -3
  11. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +19 -1
  12. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +4 -1
  13. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +12 -0
  14. package/lib/cjs/actions/vaultV1/migrateToV2.d.ts +62 -17
  15. package/lib/cjs/actions/vaultV1/migrateToV2.js +48 -17
  16. package/lib/cjs/actions/vaultV1/redeem.d.ts +28 -1
  17. package/lib/cjs/actions/vaultV1/redeem.js +17 -1
  18. package/lib/cjs/actions/vaultV1/withdraw.d.ts +1 -1
  19. package/lib/cjs/actions/vaultV1/withdraw.js +1 -1
  20. package/lib/cjs/actions/vaultV2/forceWithdraw.d.ts +3 -2
  21. package/lib/cjs/actions/vaultV2/forceWithdraw.js +3 -2
  22. package/lib/cjs/actions/vaultV2/redeem.d.ts +28 -1
  23. package/lib/cjs/actions/vaultV2/redeem.js +17 -1
  24. package/lib/cjs/actions/vaultV2/withdraw.d.ts +1 -1
  25. package/lib/cjs/actions/vaultV2/withdraw.js +1 -1
  26. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +29 -20
  27. package/lib/cjs/entities/vaultV1/vaultV1.js +50 -84
  28. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +33 -24
  29. package/lib/cjs/entities/vaultV2/vaultV2.js +44 -72
  30. package/lib/cjs/types/action.d.ts +1 -0
  31. package/lib/esm/actions/bundles/common.d.ts +18 -23
  32. package/lib/esm/actions/bundles/common.js +34 -80
  33. package/lib/esm/actions/bundles/index.d.ts +1 -1
  34. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +6 -2
  35. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +6 -2
  36. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +1 -0
  37. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +1 -0
  38. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +11 -3
  39. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +21 -3
  40. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +4 -1
  41. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +14 -2
  42. package/lib/esm/actions/vaultV1/migrateToV2.d.ts +62 -17
  43. package/lib/esm/actions/vaultV1/migrateToV2.js +48 -17
  44. package/lib/esm/actions/vaultV1/redeem.d.ts +28 -1
  45. package/lib/esm/actions/vaultV1/redeem.js +17 -1
  46. package/lib/esm/actions/vaultV1/withdraw.d.ts +1 -1
  47. package/lib/esm/actions/vaultV1/withdraw.js +1 -1
  48. package/lib/esm/actions/vaultV2/forceWithdraw.d.ts +3 -2
  49. package/lib/esm/actions/vaultV2/forceWithdraw.js +3 -2
  50. package/lib/esm/actions/vaultV2/redeem.d.ts +28 -1
  51. package/lib/esm/actions/vaultV2/redeem.js +17 -1
  52. package/lib/esm/actions/vaultV2/withdraw.d.ts +1 -1
  53. package/lib/esm/actions/vaultV2/withdraw.js +1 -1
  54. package/lib/esm/entities/vaultV1/vaultV1.d.ts +29 -20
  55. package/lib/esm/entities/vaultV1/vaultV1.js +52 -86
  56. package/lib/esm/entities/vaultV2/vaultV2.d.ts +33 -24
  57. package/lib/esm/entities/vaultV2/vaultV2.js +46 -74
  58. package/lib/esm/types/action.d.ts +1 -0
  59. package/package.json +5 -5
@@ -24,10 +24,7 @@ class MorphoVaultV1 {
24
24
  this.chainId = chainId;
25
25
  }
26
26
  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
- }
27
+ (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
31
28
  return (0, blue_sdk_viem_1.fetchAccrualVault)(this.vault, this.client.viemClient, {
32
29
  ...parameters,
33
30
  deployless: this.client.options.supportDeployless,
@@ -66,7 +63,6 @@ class MorphoVaultV1 {
66
63
  });
67
64
  const spender = (0, morpho_ts_1.getChainAddress)(this.chainId, "bundles.vaultBundlesV1");
68
65
  let pendingRequirements;
69
- let expectedRequirement;
70
66
  return Object.freeze({
71
67
  getRequirements: async (requirementOptions) => {
72
68
  const now = morpho_ts_1.Time.timestamp();
@@ -74,33 +70,22 @@ class MorphoVaultV1 {
74
70
  throw new index_js_3.ExpiredDeadlineError(deadline, now);
75
71
  if (pendingRequirements != null)
76
72
  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
- })();
73
+ // Memoize the in-flight promise so concurrent callers share one round of
74
+ // allowance/nonce reads.
75
+ const pending = funding.value > 0n
76
+ ? Promise.resolve([])
77
+ : (0, index_js_4.getBundlesTokenRequirements)(this.client.viemClient, {
78
+ token: vaultAsset,
79
+ spender,
80
+ amount: funding.assets,
81
+ owner: userAddress,
82
+ chainId: this.chainId,
83
+ deadline,
84
+ supportSignature: this.client.options.supportSignature,
85
+ supportDeployless: this.client.options.supportDeployless,
86
+ useSimplePermit: requirementOptions?.useSimplePermit,
87
+ permit2Nonce: requirementOptions?.permit2Nonce,
88
+ });
104
89
  pendingRequirements = pending;
105
90
  try {
106
91
  return await pending;
@@ -112,7 +97,8 @@ class MorphoVaultV1 {
112
97
  }
113
98
  },
114
99
  buildTx: (signatures) => {
115
- const requirementSignature = (0, common_js_1.selectBundlesTokenRequirementSignature)(signatures, expectedRequirement);
100
+ // The pure deposit action rejects a token signature supplied alongside native funding.
101
+ const requirementSignature = (0, common_js_1.selectBundlesTokenRequirementSignature)(signatures, { spender, amount: funding.assets, deadline });
116
102
  return (0, index_js_1.vaultV1Deposit)({
117
103
  vault: {
118
104
  chainId: this.chainId,
@@ -136,8 +122,11 @@ class MorphoVaultV1 {
136
122
  });
137
123
  }
138
124
  withdraw(params) {
139
- const { amount, userAddress } = params;
125
+ const { amount, userAddress, vaultData } = params;
140
126
  (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
127
+ if (!(0, viem_1.isAddressEqual)(vaultData.address, this.vault)) {
128
+ throw new index_js_3.VaultAddressMismatchError(this.vault, vaultData.address);
129
+ }
141
130
  if (amount <= 0n)
142
131
  throw new index_js_3.NonPositiveInputError("amount", amount);
143
132
  // Reject values outside the ABI range before resolving any requirements.
@@ -153,11 +142,14 @@ class MorphoVaultV1 {
153
142
  });
154
143
  const slippageTolerance = params.slippageTolerance ?? blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE;
155
144
  (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;
145
+ // Resolve the spender eagerly so an unregistered VaultBundlesV1 fails at handle creation.
146
+ const spender = (0, morpho_ts_1.getChainAddress)(this.chainId, "bundles.vaultBundlesV1");
147
+ const requiredShareAllowance = (0, index_js_2.computeVaultMaxShareAllowance)({
148
+ vaultData,
149
+ deadline,
150
+ assets: amount,
151
+ slippageTolerance,
152
+ });
161
153
  return Object.freeze({
162
154
  getRequirements: async () => {
163
155
  const now = morpho_ts_1.Time.timestamp();
@@ -166,18 +158,9 @@ class MorphoVaultV1 {
166
158
  // Re-read the live share allowance on every call instead of caching the resolved
167
159
  // requirements: the allowance is the sole cap on the burn, so a caller that executed the
168
160
  // 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, {
161
+ // raised afterwards must resurface as an outstanding requirement. The cap derived from
162
+ // `vaultData` is pinned at handle creation, so re-reading cannot move it.
163
+ return (0, getVaultBundlesSharesRequirements_js_1.getVaultBundlesSharesRequirements)(this.client.viemClient, {
181
164
  vaultData,
182
165
  version: "vaultV1",
183
166
  owner: userAddress,
@@ -186,17 +169,12 @@ class MorphoVaultV1 {
186
169
  deadline,
187
170
  supportSignature: this.client.options.supportSignature,
188
171
  });
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
172
  },
196
173
  buildTx: (signatures) => {
197
- const permit = (0, common_js_1.selectBundlesSharesRequirementSignature)(signatures, {
198
- requiredShareAllowance,
199
- expectedRequirement,
174
+ const permit = (0, common_js_1.selectBundlesSharesPermitSignature)(signatures, {
175
+ spender,
176
+ amount: requiredShareAllowance,
177
+ deadline,
200
178
  });
201
179
  return (0, index_js_1.vaultV1Withdraw)({
202
180
  vault: { chainId: this.chainId, address: this.vault },
@@ -228,14 +206,13 @@ class MorphoVaultV1 {
228
206
  referralFeePct: params.referralFeePct,
229
207
  referralFeeRecipient: params.referralFeeRecipient,
230
208
  });
231
- (0, morpho_ts_1.getChainAddress)(this.chainId, "bundles.vaultBundlesV1");
232
- let expectedRequirement;
209
+ const spender = (0, morpho_ts_1.getChainAddress)(this.chainId, "bundles.vaultBundlesV1");
233
210
  return Object.freeze({
234
211
  getRequirements: async () => {
235
212
  const now = morpho_ts_1.Time.timestamp();
236
213
  if (deadline <= now)
237
214
  throw new index_js_3.ExpiredDeadlineError(deadline, now);
238
- const requirements = await (0, getVaultBundlesSharesRequirements_js_1.getVaultBundlesSharesRequirements)(this.client.viemClient, {
215
+ return (0, getVaultBundlesSharesRequirements_js_1.getVaultBundlesSharesRequirements)(this.client.viemClient, {
239
216
  vaultData: await this.getData(),
240
217
  version: "vaultV1",
241
218
  owner: userAddress,
@@ -244,17 +221,12 @@ class MorphoVaultV1 {
244
221
  deadline,
245
222
  supportSignature: this.client.options.supportSignature,
246
223
  });
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
224
  },
254
225
  buildTx: (signatures) => {
255
- const permit = (0, common_js_1.selectBundlesSharesRequirementSignature)(signatures, {
256
- requiredShareAllowance: shares,
257
- expectedRequirement,
226
+ const permit = (0, common_js_1.selectBundlesSharesPermitSignature)(signatures, {
227
+ spender,
228
+ amount: shares,
229
+ deadline,
258
230
  });
259
231
  return (0, index_js_1.vaultV1Redeem)({
260
232
  vault: { chainId: this.chainId, address: this.vault },
@@ -497,7 +469,7 @@ class MorphoVaultV1 {
497
469
  assets: assets ?? 0n,
498
470
  slippageTolerance,
499
471
  });
500
- (0, morpho_ts_1.getChainAddress)(this.chainId, "bundles.vaultBundlesV1");
472
+ const spender = (0, morpho_ts_1.getChainAddress)(this.chainId, "bundles.vaultBundlesV1");
501
473
  const { userAddress } = params;
502
474
  const sourceAsset = params.sourceVault.asset;
503
475
  const targetAddress = params.targetVault.address;
@@ -513,7 +485,6 @@ class MorphoVaultV1 {
513
485
  extensions: [...domain.extensions],
514
486
  }),
515
487
  });
516
- let expectedRequirement;
517
488
  return Object.freeze({
518
489
  getRequirements: async () => {
519
490
  const now = morpho_ts_1.Time.timestamp();
@@ -525,7 +496,7 @@ class MorphoVaultV1 {
525
496
  // satisfied on the next call, and an allowance revoked or raised afterwards must resurface
526
497
  // as an outstanding requirement. The pinned snapshot and the cap derived from it stay
527
498
  // 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, {
499
+ return (0, getVaultBundlesSharesRequirements_js_1.getVaultBundlesSharesRequirements)(this.client.viemClient, {
529
500
  vaultData: sourceToken,
530
501
  version: "vaultV1",
531
502
  owner: userAddress,
@@ -534,17 +505,12 @@ class MorphoVaultV1 {
534
505
  deadline,
535
506
  supportSignature: this.client.options.supportSignature,
536
507
  });
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
508
  },
544
509
  buildTx: (signatures) => {
545
- const permit = (0, common_js_1.selectBundlesSharesRequirementSignature)(signatures, {
546
- requiredShareAllowance,
547
- expectedRequirement,
510
+ const permit = (0, common_js_1.selectBundlesSharesPermitSignature)(signatures, {
511
+ spender,
512
+ amount: requiredShareAllowance,
513
+ deadline,
548
514
  });
549
515
  return (0, index_js_1.vaultV1MigrateToV2)({
550
516
  vault: {
@@ -11,6 +11,7 @@ export interface VaultV2Actions {
11
11
  * @param {FetchParameters} [parameters] - The parameters for the fetch operation.
12
12
  *
13
13
  * @returns {Promise<Awaited<ReturnType<typeof fetchAccrualVaultV2>>>} The latest vault data.
14
+ * @throws {ChainIdMismatchError} when the connected client targets another chain or has no chain.
14
15
  */
15
16
  getData: (parameters?: FetchParameters) => Promise<Awaited<ReturnType<typeof fetchAccrualVaultV2>>>;
16
17
  /**
@@ -21,7 +22,7 @@ export interface VaultV2Actions {
21
22
  * Permit2 nonce state. Native funding is exclusive and skips token requirements. Shares are
22
23
  * always minted to the transaction sender, which must be `userAddress`.
23
24
  * Concurrent requirement reads share the first caller's options; later calls refresh on-chain
24
- * state using their own options. `buildTx()` accepts signatures from the latest completed read.
25
+ * state using their own options.
25
26
  *
26
27
  * @param params.userAddress - Account that funds, signs, submits, and receives the vault shares.
27
28
  * @param params.vaultData - Pre-fetched Vault V2 snapshot used for asset and share conversion.
@@ -57,8 +58,9 @@ export interface VaultV2Actions {
57
58
  * `getRequirements()` when the Permit2 nonce exceeds uint256.
58
59
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when multiple token signatures are supplied.
59
60
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when an unsupported signature is supplied.
60
- * @throws {BundlesPermitMismatchError} from `buildTx()` when the signature was not produced for
61
- * this prepared handle.
61
+ * @throws {BundlesPermitMismatchError} from `buildTx()` when the signature's spender, amount or
62
+ * deadline differ from this operation's, or when the signature's carried action nonce is
63
+ * inconsistent with its signed nonce.
62
64
  * @throws {DepositOwnerMismatchError} from `buildTx()` when the signed owner differs from `userAddress`.
63
65
  * @throws {DepositAssetMismatchError} from `buildTx()` when the signed asset differs from the vault asset.
64
66
  * @throws {BundlesRequirementSignatureMismatchError} from `buildTx()` when signature metadata is malformed.
@@ -98,13 +100,14 @@ export interface VaultV2Actions {
98
100
  /**
99
101
  * Prepares an exact-assets Vault V2 withdrawal through VaultBundlesV1.
100
102
  *
101
- * Reads the vault accrual state on the first `getRequirements()` call, then reads the live
102
- * vault-share allowance and, when a signature is needed, the permit nonce on each call.
103
- * Captures the requested amount and owner at creation, so later changes to `params` do not
104
- * change this handle's requirements or transaction.
103
+ * Derives the share-burn cap from the supplied `vaultData` snapshot at handle creation, then
104
+ * reads the live vault-share allowance and, when a signature is needed, the permit nonce on
105
+ * each `getRequirements()` call. Captures the requested amount and owner at creation, so later
106
+ * changes to `params` do not change this handle's requirements or transaction.
105
107
  *
106
108
  * @param params.amount - Positive gross withdrawal in underlying asset base units, before fees.
107
109
  * @param params.userAddress - Share owner that must sign and submit; receives the net assets.
110
+ * @param params.vaultData - Pre-fetched Vault V2 snapshot used to derive the share cap.
108
111
  * @param params.slippageTolerance - Optional WAD-scaled share-price loss tolerance applied to
109
112
  * the share cap.
110
113
  * Defaults to 0.03% and cannot exceed 10%.
@@ -117,8 +120,11 @@ export interface VaultV2Actions {
117
120
  * which returns a deep-frozen `Transaction<VaultV2WithdrawAction>`. Requirements are empty
118
121
  * when the allowance equals the cap; an oversized allowance is always reset with an exact
119
122
  * onchain approval, and an insufficient one is raised by an approval or, with signature
120
- * support, an ERC-2612 request. The cap stays pinned to the first resolution while each call
121
- * re-reads the allowance. Confirm the approval or pass its signed permit to `buildTx`.
123
+ * support, an ERC-2612 request. The cap is fixed at handle creation while each call re-reads
124
+ * the allowance. Confirm the approval or pass its signed permit to `buildTx`.
125
+ * @remarks VaultBundlesV1 skips a share permit whose nonce was already consumed and proceeds
126
+ * under the live allowance, so execute every requirement returned by the latest
127
+ * `getRequirements()` — including the oversized-allowance reset — before submitting.
122
128
  * @throws {ChainIdMismatchError} when the connected client targets another chain.
123
129
  * @throws {NonPositiveInputError} when `amount` or the computed share cap is not positive.
124
130
  * @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
@@ -128,14 +134,14 @@ export interface VaultV2Actions {
128
134
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is at least WAD.
129
135
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no nonzero recipient.
130
136
  * @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` exceeds 10%.
137
+ * @throws {VaultAddressMismatchError} when `vaultData` belongs to another vault.
131
138
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
132
139
  * @throws {UnknownAddressError} when VaultBundlesV1 is not registered on the target chain.
133
140
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when an unsupported signature is supplied.
134
141
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when multiple permits are supplied.
135
- * @throws {BundlesPermitMismatchError} from `buildTx()` when the share permit is malformed,
136
- * was not resolved for this handle, or has incompatible vault, owner, spender, amount,
137
- * deadline, or nonce values.
138
- * @throws {viem.BaseError} when a vault, allowance, or nonce read or transaction encoding fails.
142
+ * @throws {BundlesPermitMismatchError} from `buildTx()` when the share permit is malformed or
143
+ * its spender, amount or deadline differ from this operation's.
144
+ * @throws {viem.BaseError} when an allowance or nonce read or transaction encoding fails.
139
145
  * @example
140
146
  * ```ts
141
147
  * import { morphoViemExtension } from "@morpho-org/morpho-sdk";
@@ -147,7 +153,8 @@ export interface VaultV2Actions {
147
153
  * const client = createPublicClient({ chain: mainnet, transport: http() })
148
154
  * .extend(morphoViemExtension({ supportSignature: false }));
149
155
  * const vault = client.morpho.vaultV2(vaultAddress, mainnet.id);
150
- * const action = vault.withdraw({ amount: 1_000_000n, userAddress });
156
+ * const vaultData = await vault.getData();
157
+ * const action = vault.withdraw({ amount: 1_000_000n, userAddress, vaultData });
151
158
  * const requirements = await action.getRequirements();
152
159
  * // Send and confirm each approval before calling action.buildTx().
153
160
  * // action.buildTx() returns Readonly<Transaction<VaultV2WithdrawAction>>.
@@ -158,6 +165,7 @@ export interface VaultV2Actions {
158
165
  withdraw: (params: {
159
166
  readonly amount: bigint;
160
167
  readonly userAddress: Address;
168
+ readonly vaultData: AccrualVaultV2;
161
169
  readonly slippageTolerance?: bigint;
162
170
  readonly referralFeePct?: bigint;
163
171
  readonly referralFeeRecipient?: Address;
@@ -169,19 +177,19 @@ export interface VaultV2Actions {
169
177
  * Captures `shares` and `userAddress` at handle creation for both requirements and `buildTx()`.
170
178
  * The caller must satisfy the exact vault-share allowance returned by `getRequirements()` before
171
179
  * `buildTx()`; every requirement resolution re-reads the live allowance and checks the deadline.
172
- * `buildTx()` accepts permits from the latest completed requirement resolution.
173
180
  *
174
- * @param {Object} params - The redeem parameters.
175
- * @param {bigint} params.shares - Exact vault shares to burn.
176
- * @param {Address} params.userAddress - Account that must sign and submit the transaction; VaultBundlesV1 burns `msg.sender`'s shares and pays `msg.sender`.
177
- * @param {bigint} [params.referralFeePct=0n] - WAD-scaled referral fee deducted from the redeemed assets; must be below WAD.
178
- * @param {Address} [params.referralFeeRecipient] - Non-zero recipient required when `referralFeePct` is positive.
179
- * @param {bigint} [params.deadline] - VaultBundlesV1 execution deadline; defaults to two hours from now.
181
+ * @param params - The redeem parameters.
182
+ * @param params.shares - Exact vault shares to burn.
183
+ * @param params.userAddress - Account that must sign and submit the transaction; VaultBundlesV1 burns `msg.sender`'s shares and pays `msg.sender`.
184
+ * @param [params.referralFeePct=0n] - WAD-scaled referral fee deducted from the redeemed assets; must be below WAD.
185
+ * @param [params.referralFeeRecipient] - Non-zero recipient required when `referralFeePct` is positive.
186
+ * @param [params.deadline] - VaultBundlesV1 execution deadline; defaults to two hours from now.
180
187
  * @returns Lazy exact share-allowance requirements and a synchronous transaction builder.
181
188
  * @throws {ChainIdMismatchError} when the client and entity target different chains.
182
189
  * @throws {NonPositiveInputError} when `shares` is not positive.
183
190
  * @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
184
191
  * requirement resolution.
192
+ * @throws {InputExceedsMaxError} when `shares` or `deadline` exceeds uint256.
185
193
  * @throws {NegativeInputError} when `referralFeePct` is negative.
186
194
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
187
195
  * @throws {ReferralFeeRecipientMissingError} when a positive fee has no non-zero recipient.
@@ -190,8 +198,8 @@ export interface VaultV2Actions {
190
198
  * @throws {viem.BaseError} from `getRequirements()` when an allowance or nonce read fails.
191
199
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when more than one permit signature is supplied.
192
200
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when a non-permit signature is supplied.
193
- * @throws {BundlesPermitMismatchError} from `buildTx()` when the supplied permit does not match the
194
- * resolved share cap, spender, owner, nonce, or deadline.
201
+ * @throws {BundlesPermitMismatchError} from `buildTx()` when the signature's spender, amount or
202
+ * deadline differ from this operation's.
195
203
  * @example
196
204
  * ```ts
197
205
  * import { isRequirementSignature } from "@morpho-org/morpho-sdk";
@@ -404,7 +412,7 @@ export interface VaultV2Actions {
404
412
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when a non-permit signature is supplied.
405
413
  * @throws {BundlesPermitMismatchError} from `buildTx()` when the permit's spender, amount, or
406
414
  * deadline differs from this operation. Any handle built from the same inputs accepts the
407
- * permit; the nonce is verified onchain by the vault's `permit`.
415
+ * permit; a permit whose nonce was consumed is skipped onchain and the live allowance applies.
408
416
  * @throws {BundlesPermitMismatchError} from `buildTx()` when the requirement has the wrong permit kind, asset, or signature encoding.
409
417
  * @example
410
418
  * ```ts
@@ -494,6 +502,7 @@ export declare class MorphoVaultV2 implements VaultV2Actions {
494
502
  withdraw(params: {
495
503
  readonly amount: bigint;
496
504
  readonly userAddress: Address;
505
+ readonly vaultData: AccrualVaultV2;
497
506
  readonly slippageTolerance?: bigint;
498
507
  readonly referralFeePct?: bigint;
499
508
  readonly referralFeeRecipient?: Address;
@@ -27,10 +27,7 @@ class MorphoVaultV2 {
27
27
  this.chainId = chainId;
28
28
  }
29
29
  async getData(parameters) {
30
- if (this.client.viemClient.chain?.id &&
31
- this.client.viemClient.chain?.id !== this.chainId) {
32
- throw new index_js_3.ChainIdMismatchError(this.client.viemClient.chain?.id, this.chainId);
33
- }
30
+ (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
34
31
  return (0, blue_sdk_viem_1.fetchAccrualVaultV2)(this.vault, this.client.viemClient, {
35
32
  ...parameters,
36
33
  deployless: this.client.options.supportDeployless,
@@ -69,7 +66,6 @@ class MorphoVaultV2 {
69
66
  });
70
67
  const spender = (0, morpho_ts_1.getChainAddress)(this.chainId, "bundles.vaultBundlesV1");
71
68
  let pendingRequirements;
72
- let expectedRequirement;
73
69
  return Object.freeze({
74
70
  getRequirements: async (requirementOptions) => {
75
71
  const now = morpho_ts_1.Time.timestamp();
@@ -77,33 +73,22 @@ class MorphoVaultV2 {
77
73
  throw new index_js_3.ExpiredDeadlineError(deadline, now);
78
74
  if (pendingRequirements != null)
79
75
  return await pendingRequirements;
80
- // Memoize the in-flight promise, not just its result: concurrent callers
81
- // requesting different routes would otherwise both resolve requirements and
82
- // the slower one would overwrite `expectedRequirement`, making `buildTx()`
83
- // reject the signature returned by the other call.
84
- const pending = (async () => {
85
- const requirements = funding.value > 0n
86
- ? []
87
- : await (0, index_js_4.getBundlesTokenRequirements)(this.client.viemClient, {
88
- token: vaultAsset,
89
- spender,
90
- amount: funding.assets,
91
- owner: userAddress,
92
- chainId: this.chainId,
93
- deadline,
94
- supportSignature: this.client.options.supportSignature,
95
- supportDeployless: this.client.options.supportDeployless,
96
- useSimplePermit: requirementOptions?.useSimplePermit,
97
- permit2Nonce: requirementOptions?.permit2Nonce,
98
- });
99
- const signatureRequirement = requirements.find(index_js_3.isRequirementSignature);
100
- expectedRequirement =
101
- signatureRequirement?.action.type === "permit" ||
102
- signatureRequirement?.action.type === "permit2SignatureTransfer"
103
- ? signatureRequirement.action
104
- : undefined;
105
- return requirements;
106
- })();
76
+ // Memoize the in-flight promise so concurrent callers share one round of
77
+ // allowance/nonce reads.
78
+ const pending = funding.value > 0n
79
+ ? Promise.resolve([])
80
+ : (0, index_js_4.getBundlesTokenRequirements)(this.client.viemClient, {
81
+ token: vaultAsset,
82
+ spender,
83
+ amount: funding.assets,
84
+ owner: userAddress,
85
+ chainId: this.chainId,
86
+ deadline,
87
+ supportSignature: this.client.options.supportSignature,
88
+ supportDeployless: this.client.options.supportDeployless,
89
+ useSimplePermit: requirementOptions?.useSimplePermit,
90
+ permit2Nonce: requirementOptions?.permit2Nonce,
91
+ });
107
92
  pendingRequirements = pending;
108
93
  try {
109
94
  return await pending;
@@ -115,7 +100,8 @@ class MorphoVaultV2 {
115
100
  }
116
101
  },
117
102
  buildTx: (signatures) => {
118
- const requirementSignature = (0, common_js_1.selectBundlesTokenRequirementSignature)(signatures, expectedRequirement);
103
+ // The pure deposit action rejects a token signature supplied alongside native funding.
104
+ const requirementSignature = (0, common_js_1.selectBundlesTokenRequirementSignature)(signatures, { spender, amount: funding.assets, deadline });
119
105
  return (0, index_js_1.vaultV2Deposit)({
120
106
  vault: {
121
107
  chainId: this.chainId,
@@ -139,8 +125,11 @@ class MorphoVaultV2 {
139
125
  });
140
126
  }
141
127
  withdraw(params) {
142
- const { amount, userAddress } = params;
128
+ const { amount, userAddress, vaultData } = params;
143
129
  (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
130
+ if (!(0, viem_1.isAddressEqual)(vaultData.address, this.vault)) {
131
+ throw new index_js_3.VaultAddressMismatchError(this.vault, vaultData.address);
132
+ }
144
133
  if (amount <= 0n)
145
134
  throw new index_js_3.NonPositiveInputError("amount", amount);
146
135
  // Reject values outside the ABI range before resolving any requirements.
@@ -156,11 +145,14 @@ class MorphoVaultV2 {
156
145
  });
157
146
  const slippageTolerance = params.slippageTolerance ?? blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE;
158
147
  (0, index_js_2.validateSlippageTolerance)(slippageTolerance);
159
- // Fail eagerly if VaultBundlesV1 is unavailable; only validation is needed here.
160
- (0, morpho_ts_1.getChainAddress)(this.chainId, "bundles.vaultBundlesV1");
161
- let requiredShareAllowance;
162
- let vaultSnapshot;
163
- let expectedRequirement;
148
+ // Resolve the spender eagerly so an unregistered VaultBundlesV1 fails at handle creation.
149
+ const spender = (0, morpho_ts_1.getChainAddress)(this.chainId, "bundles.vaultBundlesV1");
150
+ const requiredShareAllowance = (0, index_js_2.computeVaultMaxShareAllowance)({
151
+ vaultData,
152
+ deadline,
153
+ assets: amount,
154
+ slippageTolerance,
155
+ });
164
156
  return Object.freeze({
165
157
  getRequirements: async () => {
166
158
  const now = morpho_ts_1.Time.timestamp();
@@ -169,18 +161,9 @@ class MorphoVaultV2 {
169
161
  // Re-read the live share allowance on every call instead of caching the resolved
170
162
  // requirements: the allowance is the sole cap on the burn, so a caller that executed the
171
163
  // returned approval must see it satisfied on the next call, and an allowance revoked or
172
- // raised afterwards must resurface as an outstanding requirement. Only the vault snapshot
173
- // and the cap derived from it are pinned, so re-reading cannot move the cap this handle
174
- // already committed to; the snapshot is used for immutable identity and permit-domain
175
- // fields only.
176
- const vaultData = (vaultSnapshot ??= await this.getData());
177
- requiredShareAllowance ??= (0, index_js_2.computeVaultMaxShareAllowance)({
178
- vaultData,
179
- deadline,
180
- assets: amount,
181
- slippageTolerance,
182
- });
183
- const requirements = await (0, getVaultBundlesSharesRequirements_js_1.getVaultBundlesSharesRequirements)(this.client.viemClient, {
164
+ // raised afterwards must resurface as an outstanding requirement. The cap derived from
165
+ // `vaultData` is pinned at handle creation, so re-reading cannot move it.
166
+ return (0, getVaultBundlesSharesRequirements_js_1.getVaultBundlesSharesRequirements)(this.client.viemClient, {
184
167
  vaultData,
185
168
  version: "vaultV2",
186
169
  owner: userAddress,
@@ -189,17 +172,12 @@ class MorphoVaultV2 {
189
172
  deadline,
190
173
  supportSignature: this.client.options.supportSignature,
191
174
  });
192
- const signatureRequirement = requirements.find(index_js_3.isRequirementSignature);
193
- expectedRequirement =
194
- signatureRequirement?.action.type === "permit"
195
- ? signatureRequirement.action
196
- : undefined;
197
- return requirements;
198
175
  },
199
176
  buildTx: (signatures) => {
200
- const permit = (0, common_js_1.selectBundlesSharesRequirementSignature)(signatures, {
201
- requiredShareAllowance,
202
- expectedRequirement,
177
+ const permit = (0, common_js_1.selectBundlesSharesPermitSignature)(signatures, {
178
+ spender,
179
+ amount: requiredShareAllowance,
180
+ deadline,
203
181
  });
204
182
  return (0, index_js_1.vaultV2Withdraw)({
205
183
  vault: { chainId: this.chainId, address: this.vault },
@@ -231,14 +209,13 @@ class MorphoVaultV2 {
231
209
  referralFeePct: params.referralFeePct,
232
210
  referralFeeRecipient: params.referralFeeRecipient,
233
211
  });
234
- (0, morpho_ts_1.getChainAddress)(this.chainId, "bundles.vaultBundlesV1");
235
- let expectedRequirement;
212
+ const spender = (0, morpho_ts_1.getChainAddress)(this.chainId, "bundles.vaultBundlesV1");
236
213
  return Object.freeze({
237
214
  getRequirements: async () => {
238
215
  const now = morpho_ts_1.Time.timestamp();
239
216
  if (deadline <= now)
240
217
  throw new index_js_3.ExpiredDeadlineError(deadline, now);
241
- const requirements = await (0, getVaultBundlesSharesRequirements_js_1.getVaultBundlesSharesRequirements)(this.client.viemClient, {
218
+ return (0, getVaultBundlesSharesRequirements_js_1.getVaultBundlesSharesRequirements)(this.client.viemClient, {
242
219
  vaultData: await this.getData(),
243
220
  version: "vaultV2",
244
221
  owner: userAddress,
@@ -247,17 +224,12 @@ class MorphoVaultV2 {
247
224
  deadline,
248
225
  supportSignature: this.client.options.supportSignature,
249
226
  });
250
- const signatureRequirement = requirements.find(index_js_3.isRequirementSignature);
251
- expectedRequirement =
252
- signatureRequirement?.action.type === "permit"
253
- ? signatureRequirement.action
254
- : undefined;
255
- return requirements;
256
227
  },
257
228
  buildTx: (signatures) => {
258
- const permit = (0, common_js_1.selectBundlesSharesRequirementSignature)(signatures, {
259
- requiredShareAllowance: shares,
260
- expectedRequirement,
229
+ const permit = (0, common_js_1.selectBundlesSharesPermitSignature)(signatures, {
230
+ spender,
231
+ amount: shares,
232
+ deadline,
261
233
  });
262
234
  return (0, index_js_1.vaultV2Redeem)({
263
235
  vault: { chainId: this.chainId, address: this.vault },
@@ -482,6 +482,7 @@ export interface MidnightOfferRootSignatureArgs {
482
482
  }
483
483
  /** EIP-712 payload carried by a signable requirement action. */
484
484
  export type RequirementTypedData = TypedDataDefinition<Record<string, unknown>, string>;
485
+ /** Signable ERC-2612 permit requirement for a fixed-bundles token pull. */
485
486
  export interface PermitAction extends BaseAction<"permit", {
486
487
  readonly spender: Address;
487
488
  readonly amount: bigint;