@morpho-org/morpho-sdk 6.0.0-next.3 → 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 (123) hide show
  1. package/MIGRATION-v5-to-v6.md +24 -16
  2. package/README.md +6 -4
  3. package/lib/cjs/abis.d.ts +5 -1
  4. package/lib/cjs/abis.js +2 -0
  5. package/lib/cjs/actions/bundles/common.d.ts +20 -25
  6. package/lib/cjs/actions/bundles/common.js +46 -85
  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/inKindRedeem.d.ts +2 -1
  17. package/lib/cjs/actions/vaultV1/inKindRedeem.js +2 -1
  18. package/lib/cjs/actions/vaultV1/migrateToV2.d.ts +62 -17
  19. package/lib/cjs/actions/vaultV1/migrateToV2.js +48 -17
  20. package/lib/cjs/actions/vaultV1/redeem.d.ts +28 -1
  21. package/lib/cjs/actions/vaultV1/redeem.js +17 -1
  22. package/lib/cjs/actions/vaultV1/withdraw.d.ts +1 -1
  23. package/lib/cjs/actions/vaultV1/withdraw.js +1 -1
  24. package/lib/cjs/actions/vaultV2/forceWithdraw.d.ts +3 -2
  25. package/lib/cjs/actions/vaultV2/forceWithdraw.js +3 -2
  26. package/lib/cjs/actions/vaultV2/inKindRedeem.d.ts +2 -1
  27. package/lib/cjs/actions/vaultV2/inKindRedeem.js +2 -1
  28. package/lib/cjs/actions/vaultV2/redeem.d.ts +28 -1
  29. package/lib/cjs/actions/vaultV2/redeem.js +17 -1
  30. package/lib/cjs/actions/vaultV2/withdraw.d.ts +1 -1
  31. package/lib/cjs/actions/vaultV2/withdraw.js +1 -1
  32. package/lib/cjs/augment/AccrualPosition.d.ts +1 -0
  33. package/lib/cjs/blue/abis.d.ts +5 -1
  34. package/lib/cjs/blue/abis.js +2 -0
  35. package/lib/cjs/blue/entities.d.ts +10 -2
  36. package/lib/cjs/blue/entities.js +2 -0
  37. package/lib/cjs/blue/errors.d.ts +4 -2
  38. package/lib/cjs/blue/errors.js +3 -1
  39. package/lib/cjs/blue/fetch.d.ts +5 -1
  40. package/lib/cjs/blue/fetch.js +2 -0
  41. package/lib/cjs/blue/utils.d.ts +5 -1
  42. package/lib/cjs/blue/utils.js +2 -0
  43. package/lib/cjs/entities/index.d.ts +10 -2
  44. package/lib/cjs/entities/index.js +2 -0
  45. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -2
  46. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +8 -3
  47. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +31 -21
  48. package/lib/cjs/entities/vaultV1/vaultV1.js +50 -83
  49. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +52 -35
  50. package/lib/cjs/entities/vaultV2/vaultV2.js +71 -81
  51. package/lib/cjs/errors.d.ts +5 -3
  52. package/lib/cjs/errors.js +4 -1
  53. package/lib/cjs/fetch.d.ts +5 -1
  54. package/lib/cjs/fetch.js +2 -0
  55. package/lib/cjs/helpers/previewVaultV2ForceWithdraw.js +11 -5
  56. package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.d.ts +48 -15
  57. package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.js +48 -15
  58. package/lib/cjs/midnight/errors.d.ts +1 -1
  59. package/lib/cjs/midnight/errors.js +2 -1
  60. package/lib/cjs/types/action.d.ts +1 -0
  61. package/lib/cjs/utils.d.ts +5 -1
  62. package/lib/cjs/utils.js +2 -0
  63. package/lib/esm/abis.d.ts +5 -1
  64. package/lib/esm/abis.js +5 -1
  65. package/lib/esm/actions/bundles/common.d.ts +20 -25
  66. package/lib/esm/actions/bundles/common.js +44 -82
  67. package/lib/esm/actions/bundles/index.d.ts +1 -1
  68. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +6 -2
  69. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +6 -2
  70. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +1 -0
  71. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +1 -0
  72. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +11 -3
  73. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +21 -3
  74. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +4 -1
  75. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +14 -2
  76. package/lib/esm/actions/vaultV1/inKindRedeem.d.ts +2 -1
  77. package/lib/esm/actions/vaultV1/inKindRedeem.js +2 -1
  78. package/lib/esm/actions/vaultV1/migrateToV2.d.ts +62 -17
  79. package/lib/esm/actions/vaultV1/migrateToV2.js +48 -17
  80. package/lib/esm/actions/vaultV1/redeem.d.ts +28 -1
  81. package/lib/esm/actions/vaultV1/redeem.js +17 -1
  82. package/lib/esm/actions/vaultV1/withdraw.d.ts +1 -1
  83. package/lib/esm/actions/vaultV1/withdraw.js +1 -1
  84. package/lib/esm/actions/vaultV2/forceWithdraw.d.ts +3 -2
  85. package/lib/esm/actions/vaultV2/forceWithdraw.js +3 -2
  86. package/lib/esm/actions/vaultV2/inKindRedeem.d.ts +2 -1
  87. package/lib/esm/actions/vaultV2/inKindRedeem.js +2 -1
  88. package/lib/esm/actions/vaultV2/redeem.d.ts +28 -1
  89. package/lib/esm/actions/vaultV2/redeem.js +17 -1
  90. package/lib/esm/actions/vaultV2/withdraw.d.ts +1 -1
  91. package/lib/esm/actions/vaultV2/withdraw.js +1 -1
  92. package/lib/esm/augment/AccrualPosition.d.ts +1 -0
  93. package/lib/esm/blue/abis.d.ts +5 -1
  94. package/lib/esm/blue/abis.js +5 -1
  95. package/lib/esm/blue/entities.d.ts +10 -2
  96. package/lib/esm/blue/entities.js +5 -1
  97. package/lib/esm/blue/errors.d.ts +4 -2
  98. package/lib/esm/blue/errors.js +4 -2
  99. package/lib/esm/blue/fetch.d.ts +5 -1
  100. package/lib/esm/blue/fetch.js +5 -1
  101. package/lib/esm/blue/utils.d.ts +5 -1
  102. package/lib/esm/blue/utils.js +5 -1
  103. package/lib/esm/entities/index.d.ts +10 -2
  104. package/lib/esm/entities/index.js +5 -1
  105. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -2
  106. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +8 -3
  107. package/lib/esm/entities/vaultV1/vaultV1.d.ts +31 -21
  108. package/lib/esm/entities/vaultV1/vaultV1.js +52 -85
  109. package/lib/esm/entities/vaultV2/vaultV2.d.ts +52 -35
  110. package/lib/esm/entities/vaultV2/vaultV2.js +73 -83
  111. package/lib/esm/errors.d.ts +5 -3
  112. package/lib/esm/errors.js +5 -3
  113. package/lib/esm/fetch.d.ts +5 -1
  114. package/lib/esm/fetch.js +5 -1
  115. package/lib/esm/helpers/previewVaultV2ForceWithdraw.js +11 -5
  116. package/lib/esm/helpers/vaultV2ForceWithdrawPlan.d.ts +48 -15
  117. package/lib/esm/helpers/vaultV2ForceWithdrawPlan.js +48 -15
  118. package/lib/esm/midnight/errors.d.ts +1 -1
  119. package/lib/esm/midnight/errors.js +1 -1
  120. package/lib/esm/types/action.d.ts +1 -0
  121. package/lib/esm/utils.d.ts +5 -1
  122. package/lib/esm/utils.js +5 -1
  123. package/package.json +6 -9
@@ -3,11 +3,11 @@ import { blueAbi, erc2612Abi, fetchAccrualVault, metaMorphoAbi, } from "@morpho-
3
3
  import { getChainAddress, Time } from "@morpho-org/morpho-ts";
4
4
  import { erc20Abi, isAddressEqual } from "viem";
5
5
  import { multicall } from "viem/actions";
6
- import { getBundlesReferralFeeAssets, normalizeBundlesCommonParams, resolveBundlesFunding, selectBundlesSharesPermitSignature, selectBundlesSharesRequirementSignature, selectBundlesTokenRequirementSignature, } from "../../actions/bundles/common.js";
6
+ import { getBundlesReferralFeeAssets, normalizeBundlesCommonParams, resolveBundlesFunding, selectBundlesSharesPermitSignature, selectBundlesTokenRequirementSignature, } from "../../actions/bundles/common.js";
7
7
  import { encodeErc20Approval, encodeVaultSharesPermit, vaultV1Deposit, vaultV1InKindRedeem, vaultV1MigrateToV2, vaultV1Redeem, vaultV1Withdraw, } from "../../actions/index.js";
8
8
  import { computeVaultMaxShareAllowance, computeVaultMaxSharePrice, validateChainId, validateSlippageTolerance, } from "../../helpers/index.js";
9
9
  import { validateDeadline, validateNativeVaultAsset, validateUint256Field, } from "../../helpers/validate.js";
10
- import { AmountAndSharesExclusiveError, ChainIdMismatchError, EmptyMarketParamsListError, ExpiredDeadlineError, InKindRedeemCoverageError, InsufficientBlueBalanceForInKindRedeemError, isRequirementSignature, NonPositiveInputError, SameVaultMigrationError, VaultAddressMismatchError, VaultAssetMismatchError, VaultIsBlueFeeRecipientError, VaultMorphoMismatchError, } from "../../types/index.js";
10
+ import { AmountAndSharesExclusiveError, EmptyMarketParamsListError, ExpiredDeadlineError, InKindRedeemCoverageError, InsufficientBlueBalanceForInKindRedeemError, NonPositiveInputError, SameVaultMigrationError, VaultAddressMismatchError, VaultAssetMismatchError, VaultIsBlueFeeRecipientError, VaultMorphoMismatchError, } from "../../types/index.js";
11
11
  import { getVaultBundlesSharesRequirements } from "../requirements/getVaultBundlesSharesRequirements.js";
12
12
  import { getBundlesTokenRequirements } from "../requirements/index.js";
13
13
  export class MorphoVaultV1 {
@@ -21,10 +21,7 @@ export class MorphoVaultV1 {
21
21
  this.chainId = chainId;
22
22
  }
23
23
  async getData(parameters) {
24
- if (this.client.viemClient.chain?.id &&
25
- this.client.viemClient.chain?.id !== this.chainId) {
26
- throw new ChainIdMismatchError(this.client.viemClient.chain?.id, this.chainId);
27
- }
24
+ validateChainId(this.client.viemClient.chain?.id, this.chainId);
28
25
  return fetchAccrualVault(this.vault, this.client.viemClient, {
29
26
  ...parameters,
30
27
  deployless: this.client.options.supportDeployless,
@@ -63,7 +60,6 @@ export class MorphoVaultV1 {
63
60
  });
64
61
  const spender = getChainAddress(this.chainId, "bundles.vaultBundlesV1");
65
62
  let pendingRequirements;
66
- let expectedRequirement;
67
63
  return Object.freeze({
68
64
  getRequirements: async (requirementOptions) => {
69
65
  const now = Time.timestamp();
@@ -71,33 +67,22 @@ export class MorphoVaultV1 {
71
67
  throw new ExpiredDeadlineError(deadline, now);
72
68
  if (pendingRequirements != null)
73
69
  return await pendingRequirements;
74
- // Memoize the in-flight promise, not just its result: concurrent callers
75
- // requesting different routes would otherwise both resolve requirements and
76
- // the slower one would overwrite `expectedRequirement`, making `buildTx()`
77
- // reject the signature returned by the other call.
78
- const pending = (async () => {
79
- const requirements = funding.value > 0n
80
- ? []
81
- : await getBundlesTokenRequirements(this.client.viemClient, {
82
- token: vaultAsset,
83
- spender,
84
- amount: funding.assets,
85
- owner: userAddress,
86
- chainId: this.chainId,
87
- deadline,
88
- supportSignature: this.client.options.supportSignature,
89
- supportDeployless: this.client.options.supportDeployless,
90
- useSimplePermit: requirementOptions?.useSimplePermit,
91
- permit2Nonce: requirementOptions?.permit2Nonce,
92
- });
93
- const signatureRequirement = requirements.find(isRequirementSignature);
94
- expectedRequirement =
95
- signatureRequirement?.action.type === "permit" ||
96
- signatureRequirement?.action.type === "permit2SignatureTransfer"
97
- ? signatureRequirement.action
98
- : undefined;
99
- return requirements;
100
- })();
70
+ // Memoize the in-flight promise so concurrent callers share one round of
71
+ // allowance/nonce reads.
72
+ const pending = funding.value > 0n
73
+ ? Promise.resolve([])
74
+ : getBundlesTokenRequirements(this.client.viemClient, {
75
+ token: vaultAsset,
76
+ spender,
77
+ amount: funding.assets,
78
+ owner: userAddress,
79
+ chainId: this.chainId,
80
+ deadline,
81
+ supportSignature: this.client.options.supportSignature,
82
+ supportDeployless: this.client.options.supportDeployless,
83
+ useSimplePermit: requirementOptions?.useSimplePermit,
84
+ permit2Nonce: requirementOptions?.permit2Nonce,
85
+ });
101
86
  pendingRequirements = pending;
102
87
  try {
103
88
  return await pending;
@@ -109,7 +94,8 @@ export class MorphoVaultV1 {
109
94
  }
110
95
  },
111
96
  buildTx: (signatures) => {
112
- const requirementSignature = selectBundlesTokenRequirementSignature(signatures, expectedRequirement);
97
+ // The pure deposit action rejects a token signature supplied alongside native funding.
98
+ const requirementSignature = selectBundlesTokenRequirementSignature(signatures, { spender, amount: funding.assets, deadline });
113
99
  return vaultV1Deposit({
114
100
  vault: {
115
101
  chainId: this.chainId,
@@ -133,8 +119,11 @@ export class MorphoVaultV1 {
133
119
  });
134
120
  }
135
121
  withdraw(params) {
136
- const { amount, userAddress } = params;
122
+ const { amount, userAddress, vaultData } = params;
137
123
  validateChainId(this.client.viemClient.chain?.id, this.chainId);
124
+ if (!isAddressEqual(vaultData.address, this.vault)) {
125
+ throw new VaultAddressMismatchError(this.vault, vaultData.address);
126
+ }
138
127
  if (amount <= 0n)
139
128
  throw new NonPositiveInputError("amount", amount);
140
129
  // Reject values outside the ABI range before resolving any requirements.
@@ -150,11 +139,14 @@ export class MorphoVaultV1 {
150
139
  });
151
140
  const slippageTolerance = params.slippageTolerance ?? DEFAULT_SLIPPAGE_TOLERANCE;
152
141
  validateSlippageTolerance(slippageTolerance);
153
- // Fail eagerly if VaultBundlesV1 is unavailable; only validation is needed here.
154
- getChainAddress(this.chainId, "bundles.vaultBundlesV1");
155
- let requiredShareAllowance;
156
- let vaultSnapshot;
157
- let expectedRequirement;
142
+ // Resolve the spender eagerly so an unregistered VaultBundlesV1 fails at handle creation.
143
+ const spender = getChainAddress(this.chainId, "bundles.vaultBundlesV1");
144
+ const requiredShareAllowance = computeVaultMaxShareAllowance({
145
+ vaultData,
146
+ deadline,
147
+ assets: amount,
148
+ slippageTolerance,
149
+ });
158
150
  return Object.freeze({
159
151
  getRequirements: async () => {
160
152
  const now = Time.timestamp();
@@ -163,18 +155,9 @@ export class MorphoVaultV1 {
163
155
  // Re-read the live share allowance on every call instead of caching the resolved
164
156
  // requirements: the allowance is the sole cap on the burn, so a caller that executed the
165
157
  // returned approval must see it satisfied on the next call, and an allowance revoked or
166
- // raised afterwards must resurface as an outstanding requirement. Only the vault snapshot
167
- // and the cap derived from it are pinned, so re-reading cannot move the cap this handle
168
- // already committed to; the snapshot is used for immutable identity and permit-domain
169
- // fields only.
170
- const vaultData = (vaultSnapshot ??= await this.getData());
171
- requiredShareAllowance ??= computeVaultMaxShareAllowance({
172
- vaultData,
173
- deadline,
174
- assets: amount,
175
- slippageTolerance,
176
- });
177
- const requirements = await getVaultBundlesSharesRequirements(this.client.viemClient, {
158
+ // raised afterwards must resurface as an outstanding requirement. The cap derived from
159
+ // `vaultData` is pinned at handle creation, so re-reading cannot move it.
160
+ return getVaultBundlesSharesRequirements(this.client.viemClient, {
178
161
  vaultData,
179
162
  version: "vaultV1",
180
163
  owner: userAddress,
@@ -183,16 +166,12 @@ export class MorphoVaultV1 {
183
166
  deadline,
184
167
  supportSignature: this.client.options.supportSignature,
185
168
  });
186
- const signatureRequirement = requirements.find(isRequirementSignature);
187
- if (signatureRequirement?.action.type === "permit") {
188
- expectedRequirement = signatureRequirement.action;
189
- }
190
- return requirements;
191
169
  },
192
170
  buildTx: (signatures) => {
193
- const permit = selectBundlesSharesRequirementSignature(signatures, {
194
- requiredShareAllowance,
195
- expectedRequirement,
171
+ const permit = selectBundlesSharesPermitSignature(signatures, {
172
+ spender,
173
+ amount: requiredShareAllowance,
174
+ deadline,
196
175
  });
197
176
  return vaultV1Withdraw({
198
177
  vault: { chainId: this.chainId, address: this.vault },
@@ -224,14 +203,13 @@ export class MorphoVaultV1 {
224
203
  referralFeePct: params.referralFeePct,
225
204
  referralFeeRecipient: params.referralFeeRecipient,
226
205
  });
227
- getChainAddress(this.chainId, "bundles.vaultBundlesV1");
228
- let expectedRequirement;
206
+ const spender = getChainAddress(this.chainId, "bundles.vaultBundlesV1");
229
207
  return Object.freeze({
230
208
  getRequirements: async () => {
231
209
  const now = Time.timestamp();
232
210
  if (deadline <= now)
233
211
  throw new ExpiredDeadlineError(deadline, now);
234
- const requirements = await getVaultBundlesSharesRequirements(this.client.viemClient, {
212
+ return getVaultBundlesSharesRequirements(this.client.viemClient, {
235
213
  vaultData: await this.getData(),
236
214
  version: "vaultV1",
237
215
  owner: userAddress,
@@ -240,17 +218,12 @@ export class MorphoVaultV1 {
240
218
  deadline,
241
219
  supportSignature: this.client.options.supportSignature,
242
220
  });
243
- const signatureRequirement = requirements.find(isRequirementSignature);
244
- expectedRequirement =
245
- signatureRequirement?.action.type === "permit"
246
- ? signatureRequirement.action
247
- : undefined;
248
- return requirements;
249
221
  },
250
222
  buildTx: (signatures) => {
251
- const permit = selectBundlesSharesRequirementSignature(signatures, {
252
- requiredShareAllowance: shares,
253
- expectedRequirement,
223
+ const permit = selectBundlesSharesPermitSignature(signatures, {
224
+ spender,
225
+ amount: shares,
226
+ deadline,
254
227
  });
255
228
  return vaultV1Redeem({
256
229
  vault: { chainId: this.chainId, address: this.vault },
@@ -493,7 +466,7 @@ export class MorphoVaultV1 {
493
466
  assets: assets ?? 0n,
494
467
  slippageTolerance,
495
468
  });
496
- getChainAddress(this.chainId, "bundles.vaultBundlesV1");
469
+ const spender = getChainAddress(this.chainId, "bundles.vaultBundlesV1");
497
470
  const { userAddress } = params;
498
471
  const sourceAsset = params.sourceVault.asset;
499
472
  const targetAddress = params.targetVault.address;
@@ -509,7 +482,6 @@ export class MorphoVaultV1 {
509
482
  extensions: [...domain.extensions],
510
483
  }),
511
484
  });
512
- let expectedRequirement;
513
485
  return Object.freeze({
514
486
  getRequirements: async () => {
515
487
  const now = Time.timestamp();
@@ -521,7 +493,7 @@ export class MorphoVaultV1 {
521
493
  // satisfied on the next call, and an allowance revoked or raised afterwards must resurface
522
494
  // as an outstanding requirement. The pinned snapshot and the cap derived from it stay
523
495
  // fixed, so re-reading cannot move the cap this handle already committed to.
524
- const requirements = await getVaultBundlesSharesRequirements(this.client.viemClient, {
496
+ return getVaultBundlesSharesRequirements(this.client.viemClient, {
525
497
  vaultData: sourceToken,
526
498
  version: "vaultV1",
527
499
  owner: userAddress,
@@ -530,17 +502,12 @@ export class MorphoVaultV1 {
530
502
  deadline,
531
503
  supportSignature: this.client.options.supportSignature,
532
504
  });
533
- const signatureRequirement = requirements.find(isRequirementSignature);
534
- expectedRequirement =
535
- signatureRequirement?.action.type === "permit"
536
- ? signatureRequirement.action
537
- : undefined;
538
- return requirements;
539
505
  },
540
506
  buildTx: (signatures) => {
541
- const permit = selectBundlesSharesRequirementSignature(signatures, {
542
- requiredShareAllowance,
543
- expectedRequirement,
507
+ const permit = selectBundlesSharesPermitSignature(signatures, {
508
+ spender,
509
+ amount: requiredShareAllowance,
510
+ deadline,
544
511
  });
545
512
  return vaultV1MigrateToV2({
546
513
  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%.
@@ -115,9 +118,13 @@ export interface VaultV2Actions {
115
118
  * defaults to two hours from handle creation.
116
119
  * @returns A frozen handle with lazy `getRequirements()` and synchronous `buildTx(signatures?)`,
117
120
  * which returns a deep-frozen `Transaction<VaultV2WithdrawAction>`. Requirements are empty
118
- * when the allowance equals the cap; otherwise they contain an exact approval or, with
119
- * signature support, an ERC-2612 request. The cap stays pinned to the first resolution while
120
- * each call re-reads the allowance. Confirm the approval or pass its signed permit to `buildTx`.
121
+ * when the allowance equals the cap; an oversized allowance is always reset with an exact
122
+ * onchain approval, and an insufficient one is raised by an approval or, with signature
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.
121
128
  * @throws {ChainIdMismatchError} when the connected client targets another chain.
122
129
  * @throws {NonPositiveInputError} when `amount` or the computed share cap is not positive.
123
130
  * @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
@@ -127,14 +134,14 @@ export interface VaultV2Actions {
127
134
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is at least WAD.
128
135
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no nonzero recipient.
129
136
  * @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` exceeds 10%.
137
+ * @throws {VaultAddressMismatchError} when `vaultData` belongs to another vault.
130
138
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
131
139
  * @throws {UnknownAddressError} when VaultBundlesV1 is not registered on the target chain.
132
140
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when an unsupported signature is supplied.
133
141
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when multiple permits are supplied.
134
- * @throws {BundlesPermitMismatchError} from `buildTx()` when the share permit is malformed,
135
- * was not resolved for this handle, or has incompatible vault, owner, spender, amount,
136
- * deadline, or nonce values.
137
- * @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.
138
145
  * @example
139
146
  * ```ts
140
147
  * import { morphoViemExtension } from "@morpho-org/morpho-sdk";
@@ -146,7 +153,8 @@ export interface VaultV2Actions {
146
153
  * const client = createPublicClient({ chain: mainnet, transport: http() })
147
154
  * .extend(morphoViemExtension({ supportSignature: false }));
148
155
  * const vault = client.morpho.vaultV2(vaultAddress, mainnet.id);
149
- * 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 });
150
158
  * const requirements = await action.getRequirements();
151
159
  * // Send and confirm each approval before calling action.buildTx().
152
160
  * // action.buildTx() returns Readonly<Transaction<VaultV2WithdrawAction>>.
@@ -157,6 +165,7 @@ export interface VaultV2Actions {
157
165
  withdraw: (params: {
158
166
  readonly amount: bigint;
159
167
  readonly userAddress: Address;
168
+ readonly vaultData: AccrualVaultV2;
160
169
  readonly slippageTolerance?: bigint;
161
170
  readonly referralFeePct?: bigint;
162
171
  readonly referralFeeRecipient?: Address;
@@ -168,19 +177,19 @@ export interface VaultV2Actions {
168
177
  * Captures `shares` and `userAddress` at handle creation for both requirements and `buildTx()`.
169
178
  * The caller must satisfy the exact vault-share allowance returned by `getRequirements()` before
170
179
  * `buildTx()`; every requirement resolution re-reads the live allowance and checks the deadline.
171
- * `buildTx()` accepts permits from the latest completed requirement resolution.
172
180
  *
173
- * @param {Object} params - The redeem parameters.
174
- * @param {bigint} params.shares - Exact vault shares to burn.
175
- * @param {Address} params.userAddress - Account that must sign and submit the transaction; VaultBundlesV1 burns `msg.sender`'s shares and pays `msg.sender`.
176
- * @param {bigint} [params.referralFeePct=0n] - WAD-scaled referral fee deducted from the redeemed assets; must be below WAD.
177
- * @param {Address} [params.referralFeeRecipient] - Non-zero recipient required when `referralFeePct` is positive.
178
- * @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.
179
187
  * @returns Lazy exact share-allowance requirements and a synchronous transaction builder.
180
188
  * @throws {ChainIdMismatchError} when the client and entity target different chains.
181
189
  * @throws {NonPositiveInputError} when `shares` is not positive.
182
190
  * @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
183
191
  * requirement resolution.
192
+ * @throws {InputExceedsMaxError} when `shares` or `deadline` exceeds uint256.
184
193
  * @throws {NegativeInputError} when `referralFeePct` is negative.
185
194
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
186
195
  * @throws {ReferralFeeRecipientMissingError} when a positive fee has no non-zero recipient.
@@ -189,8 +198,8 @@ export interface VaultV2Actions {
189
198
  * @throws {viem.BaseError} from `getRequirements()` when an allowance or nonce read fails.
190
199
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when more than one permit signature is supplied.
191
200
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when a non-permit signature is supplied.
192
- * @throws {BundlesPermitMismatchError} from `buildTx()` when the supplied permit does not match the
193
- * 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.
194
203
  * @example
195
204
  * ```ts
196
205
  * import { isRequirementSignature } from "@morpho-org/morpho-sdk";
@@ -320,9 +329,11 @@ export interface VaultV2Actions {
320
329
  * and permit nonce are read on-chain.
321
330
  *
322
331
  * The SDK derives a conservative lower bound on the realized exit share price from the snapshot
323
- * and `slippageTolerance`. A supplied `minSharePriceE27` replaces the tolerance-derived bound
324
- * (it may be tighter or looser) but must be at least the floor derived at
325
- * `MAX_SLIPPAGE_TOLERANCE`, so an override can never disable meaningful protection.
332
+ * and `slippageTolerance`: the floor is priced off whichever of the raw snapshot or its accrual
333
+ * to `now` burns more shares, so neither pending fee mints nor interest the chain has not yet
334
+ * accrued can lift it above the realized price. A supplied `minSharePriceE27` replaces the
335
+ * tolerance-derived bound (it may be tighter or looser) but must be at least the floor derived
336
+ * at `MAX_SLIPPAGE_TOLERANCE`, so an override can never disable meaningful protection.
326
337
  * The bound rejects a share price drop, a penalty increase, and liquidity shifting from the
327
338
  * penalty-free leg to the penalised leg. It does **not** cover the referral fee, which the
328
339
  * contract deducts afterwards.
@@ -332,16 +343,21 @@ export interface VaultV2Actions {
332
343
  * send-shares gate is arbitrary code re-evaluated after each penalty burn. The SDK also does not
333
344
  * validate the user's share balance. Per-market penalties and each vault withdrawal round the
334
345
  * share burn up independently, so the exit can burn marginally more shares than `exitAssets` alone
335
- * implies; size `exitAssets` a small buffer below `vault.previewRedeem(sharesHeld)` — the approved
336
- * share allowance this handle returns is the exact upper bound on the burn — so a full-balance
337
- * exit does not revert for insufficient shares.
346
+ * implies; size `exitAssets` a small buffer below `vault.previewRedeem(sharesHeld)` so a
347
+ * full-balance exit does not revert for insufficient shares. The approved share allowance this
348
+ * handle returns covers a burn at least one `slippageTolerance` step below the price floor
349
+ * whenever the floor exceeds one RAY unit and, in any case, at least one share above the burn
350
+ * at the floor, so a price within that headroom below the floor reverts on the contract's
351
+ * `minSharePriceE27` check rather than on the allowance; a deeper drop can still surface as an
352
+ * ERC-20 allowance underflow.
338
353
  *
339
354
  * Idle balance, penalty, adapter positions, and market liquidity can drift after the snapshot, so
340
355
  * an on-chain revert remains possible if vault state changes between preparation and inclusion.
341
- * A fee-recipient `userAddress` gets a floor from the net share burn after fee mints at `now`,
342
- * and a guard against fee mints reaching the lower burn bound by the deadline. Its allowance
343
- * includes the projected fee shares through that same deadline. Deadlines beyond one year after
344
- * handle creation are rejected, so the guard and allowance cover the whole accepted window.
356
+ * A fee-recipient `userAddress` gets a floor from the larger of the raw snapshot burn and the
357
+ * `now` burn net of its own fee mints, and a guard against fee mints reaching the lower burn
358
+ * bound by the deadline. Its allowance includes the projected fee shares through that same
359
+ * deadline. Deadlines beyond one year after handle creation are rejected, so the guard and
360
+ * allowance cover the whole accepted window.
345
361
  *
346
362
  * @param params - Force withdrawal parameters.
347
363
  * @param params.exitAssets - Penalty-inclusive, asset-denominated amount to exit.
@@ -396,7 +412,7 @@ export interface VaultV2Actions {
396
412
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when a non-permit signature is supplied.
397
413
  * @throws {BundlesPermitMismatchError} from `buildTx()` when the permit's spender, amount, or
398
414
  * deadline differs from this operation. Any handle built from the same inputs accepts the
399
- * 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.
400
416
  * @throws {BundlesPermitMismatchError} from `buildTx()` when the requirement has the wrong permit kind, asset, or signature encoding.
401
417
  * @example
402
418
  * ```ts
@@ -486,6 +502,7 @@ export declare class MorphoVaultV2 implements VaultV2Actions {
486
502
  withdraw(params: {
487
503
  readonly amount: bigint;
488
504
  readonly userAddress: Address;
505
+ readonly vaultData: AccrualVaultV2;
489
506
  readonly slippageTolerance?: bigint;
490
507
  readonly referralFeePct?: bigint;
491
508
  readonly referralFeeRecipient?: Address;