@morpho-org/morpho-sdk 9.7.0 → 10.0.0-next.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 (185) hide show
  1. package/MIGRATION-v9-to-v10.md +114 -0
  2. package/README.md +5 -1
  3. package/lib/cjs/abis.d.ts +1 -1
  4. package/lib/cjs/abis.js +5 -1
  5. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +3 -4
  6. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +1 -1
  7. package/lib/cjs/actions/midnight/cancelAndMake.d.ts +2 -1
  8. package/lib/cjs/actions/midnight/cancelAndMake.js +2 -1
  9. package/lib/cjs/actions/midnight/cancelOffers.d.ts +54 -0
  10. package/lib/cjs/actions/midnight/cancelOffers.js +107 -0
  11. package/lib/cjs/actions/midnight/index.d.ts +1 -0
  12. package/lib/cjs/actions/midnight/index.js +1 -0
  13. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +1 -1
  14. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +1 -1
  15. package/lib/cjs/actions/midnight/takeBorrow.d.ts +1 -1
  16. package/lib/cjs/actions/midnight/takeBorrow.js +1 -1
  17. package/lib/cjs/actions/midnight/takeLend.d.ts +1 -1
  18. package/lib/cjs/actions/midnight/takeLend.js +1 -1
  19. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.d.ts +1 -1
  20. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +1 -1
  21. package/lib/cjs/actions/midnight/takeWithdraw.d.ts +1 -1
  22. package/lib/cjs/actions/midnight/takeWithdraw.js +1 -1
  23. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +3 -4
  24. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +1 -1
  25. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +4 -6
  26. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +3 -3
  27. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +4 -6
  28. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +3 -3
  29. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +5 -6
  30. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +3 -2
  31. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +3 -5
  32. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +26 -23
  33. package/lib/cjs/blue/abis.d.ts +1 -1
  34. package/lib/cjs/blue/abis.js +5 -1
  35. package/lib/cjs/blue/entities.d.ts +4 -4
  36. package/lib/cjs/blue/entities.js +3 -1
  37. package/lib/cjs/blue/errors.d.ts +2 -2
  38. package/lib/cjs/blue/errors.js +5 -1
  39. package/lib/cjs/blue/fetch.d.ts +2 -2
  40. package/lib/cjs/blue/fetch.js +3 -1
  41. package/lib/cjs/blue/utils.d.ts +1 -1
  42. package/lib/cjs/blue/utils.js +2 -1
  43. package/lib/cjs/entities/blue/blue.d.ts +34 -28
  44. package/lib/cjs/entities/blue/blue.js +99 -73
  45. package/lib/cjs/entities/index.d.ts +4 -4
  46. package/lib/cjs/entities/index.js +3 -1
  47. package/lib/cjs/entities/midnight/deadline.d.ts +2 -2
  48. package/lib/cjs/entities/midnight/deadline.js +2 -2
  49. package/lib/cjs/entities/midnight/midnight.d.ts +49 -51
  50. package/lib/cjs/entities/midnight/midnight.js +144 -131
  51. package/lib/cjs/entities/midnight/publication.d.ts +2 -0
  52. package/lib/cjs/entities/midnight/publication.js +126 -100
  53. package/lib/cjs/entities/midnight/types.d.ts +4 -4
  54. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +3 -5
  55. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +17 -17
  56. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +3 -4
  57. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +1 -1
  58. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +12 -12
  59. package/lib/cjs/entities/vaultV1/vaultV1.js +83 -57
  60. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +50 -24
  61. package/lib/cjs/entities/vaultV2/vaultV2.js +166 -90
  62. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +17 -2
  63. package/lib/cjs/entities/vaultV2BlueReallocationData.js +1410 -806
  64. package/lib/cjs/errors.d.ts +4 -4
  65. package/lib/cjs/errors.js +7 -5
  66. package/lib/cjs/fetch.d.ts +2 -2
  67. package/lib/cjs/fetch.js +3 -1
  68. package/lib/cjs/helpers/deadShares.js +55 -34
  69. package/lib/cjs/helpers/metadata.d.ts +14 -2
  70. package/lib/cjs/helpers/metadata.js +39 -4
  71. package/lib/cjs/helpers/previewVaultV2ForceWithdraw.js +34 -22
  72. package/lib/cjs/helpers/previewVaultV2InKindRedeem.js +7 -1
  73. package/lib/cjs/helpers/validate.js +76 -61
  74. package/lib/cjs/helpers/validateMidnightTakeFeasibility.js +154 -103
  75. package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.js +31 -20
  76. package/lib/cjs/index.d.ts +1 -0
  77. package/lib/cjs/index.js +3 -1
  78. package/lib/cjs/midnight/errors.d.ts +2 -1
  79. package/lib/cjs/midnight/errors.js +5 -4
  80. package/lib/cjs/transactionPlan/plan.js +6 -3
  81. package/lib/cjs/transactionPlan/serde.js +7 -7
  82. package/lib/cjs/transactionPlan/vaultv2/deposit.d.ts +6 -4
  83. package/lib/cjs/transactionPlan/vaultv2/deposit.js +5 -3
  84. package/lib/cjs/transactionPlan/vaultv2/withdraw.d.ts +6 -4
  85. package/lib/cjs/transactionPlan/vaultv2/withdraw.js +5 -3
  86. package/lib/cjs/types/action.d.ts +26 -2
  87. package/lib/cjs/types/action.js +1 -1
  88. package/lib/cjs/types/error.js +11 -5
  89. package/lib/cjs/types/metadata.d.ts +12 -2
  90. package/lib/cjs/utils.d.ts +2 -2
  91. package/lib/cjs/utils.js +4 -2
  92. package/lib/cjs/version.d.ts +2 -0
  93. package/lib/cjs/version.js +6 -0
  94. package/lib/esm/abis.d.ts +1 -1
  95. package/lib/esm/abis.js +1 -1
  96. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +3 -4
  97. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +1 -1
  98. package/lib/esm/actions/midnight/cancelAndMake.d.ts +2 -1
  99. package/lib/esm/actions/midnight/cancelAndMake.js +2 -1
  100. package/lib/esm/actions/midnight/cancelOffers.d.ts +54 -0
  101. package/lib/esm/actions/midnight/cancelOffers.js +103 -0
  102. package/lib/esm/actions/midnight/index.d.ts +1 -0
  103. package/lib/esm/actions/midnight/index.js +1 -0
  104. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +1 -1
  105. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +1 -1
  106. package/lib/esm/actions/midnight/takeBorrow.d.ts +1 -1
  107. package/lib/esm/actions/midnight/takeBorrow.js +1 -1
  108. package/lib/esm/actions/midnight/takeLend.d.ts +1 -1
  109. package/lib/esm/actions/midnight/takeLend.js +1 -1
  110. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.d.ts +1 -1
  111. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +1 -1
  112. package/lib/esm/actions/midnight/takeWithdraw.d.ts +1 -1
  113. package/lib/esm/actions/midnight/takeWithdraw.js +1 -1
  114. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +3 -4
  115. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +1 -1
  116. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +4 -6
  117. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +3 -3
  118. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +4 -6
  119. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +3 -3
  120. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +5 -6
  121. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +3 -2
  122. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +3 -5
  123. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +26 -23
  124. package/lib/esm/blue/abis.d.ts +1 -1
  125. package/lib/esm/blue/abis.js +1 -1
  126. package/lib/esm/blue/entities.d.ts +4 -4
  127. package/lib/esm/blue/entities.js +2 -2
  128. package/lib/esm/blue/errors.d.ts +2 -2
  129. package/lib/esm/blue/errors.js +2 -2
  130. package/lib/esm/blue/fetch.d.ts +2 -2
  131. package/lib/esm/blue/fetch.js +2 -2
  132. package/lib/esm/blue/utils.d.ts +1 -1
  133. package/lib/esm/blue/utils.js +1 -1
  134. package/lib/esm/entities/blue/blue.d.ts +34 -28
  135. package/lib/esm/entities/blue/blue.js +99 -73
  136. package/lib/esm/entities/index.d.ts +4 -4
  137. package/lib/esm/entities/index.js +2 -2
  138. package/lib/esm/entities/midnight/deadline.d.ts +2 -2
  139. package/lib/esm/entities/midnight/deadline.js +2 -2
  140. package/lib/esm/entities/midnight/midnight.d.ts +49 -51
  141. package/lib/esm/entities/midnight/midnight.js +145 -132
  142. package/lib/esm/entities/midnight/publication.d.ts +2 -0
  143. package/lib/esm/entities/midnight/publication.js +126 -100
  144. package/lib/esm/entities/midnight/types.d.ts +4 -4
  145. package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +3 -5
  146. package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +17 -17
  147. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +3 -4
  148. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +1 -1
  149. package/lib/esm/entities/vaultV1/vaultV1.d.ts +12 -12
  150. package/lib/esm/entities/vaultV1/vaultV1.js +83 -57
  151. package/lib/esm/entities/vaultV2/vaultV2.d.ts +50 -24
  152. package/lib/esm/entities/vaultV2/vaultV2.js +166 -90
  153. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +17 -2
  154. package/lib/esm/entities/vaultV2BlueReallocationData.js +1410 -806
  155. package/lib/esm/errors.d.ts +4 -4
  156. package/lib/esm/errors.js +4 -4
  157. package/lib/esm/fetch.d.ts +2 -2
  158. package/lib/esm/fetch.js +2 -2
  159. package/lib/esm/helpers/deadShares.js +56 -35
  160. package/lib/esm/helpers/metadata.d.ts +14 -2
  161. package/lib/esm/helpers/metadata.js +38 -4
  162. package/lib/esm/helpers/previewVaultV2ForceWithdraw.js +34 -22
  163. package/lib/esm/helpers/previewVaultV2InKindRedeem.js +7 -1
  164. package/lib/esm/helpers/validate.js +76 -61
  165. package/lib/esm/helpers/validateMidnightTakeFeasibility.js +153 -102
  166. package/lib/esm/helpers/vaultV2ForceWithdrawPlan.js +31 -20
  167. package/lib/esm/index.d.ts +1 -0
  168. package/lib/esm/index.js +1 -0
  169. package/lib/esm/midnight/errors.d.ts +2 -1
  170. package/lib/esm/midnight/errors.js +2 -1
  171. package/lib/esm/transactionPlan/plan.js +6 -3
  172. package/lib/esm/transactionPlan/serde.js +7 -7
  173. package/lib/esm/transactionPlan/vaultv2/deposit.d.ts +6 -4
  174. package/lib/esm/transactionPlan/vaultv2/deposit.js +5 -3
  175. package/lib/esm/transactionPlan/vaultv2/withdraw.d.ts +6 -4
  176. package/lib/esm/transactionPlan/vaultv2/withdraw.js +5 -3
  177. package/lib/esm/types/action.d.ts +26 -2
  178. package/lib/esm/types/action.js +1 -1
  179. package/lib/esm/types/error.js +11 -5
  180. package/lib/esm/types/metadata.d.ts +12 -2
  181. package/lib/esm/utils.d.ts +2 -2
  182. package/lib/esm/utils.js +2 -2
  183. package/lib/esm/version.d.ts +2 -0
  184. package/lib/esm/version.js +3 -0
  185. package/package.json +8 -7
@@ -10,11 +10,58 @@ const common_js_1 = require("../../actions/bundles/common.js");
10
10
  const index_js_1 = require("../../actions/index.js");
11
11
  const validateClientChainId_js_1 = require("../../actions/requirements/validateClientChainId.js");
12
12
  const index_js_2 = require("../../helpers/index.js");
13
+ const metadata_js_1 = require("../../helpers/metadata.js");
13
14
  const validate_js_1 = require("../../helpers/validate.js");
14
15
  const index_js_3 = require("../../types/index.js");
15
16
  const assertContractCode_js_1 = require("../requirements/assertContractCode.js");
16
17
  const getVaultBundlesSharesRequirements_js_1 = require("../requirements/getVaultBundlesSharesRequirements.js");
17
18
  const index_js_4 = require("../requirements/index.js");
19
+ function validateInKindCoverage({ amount, marketParamsListSnapshot, vaultData, now, }) {
20
+ let covered = 0n;
21
+ const assignedByMarket = new Map();
22
+ for (const marketParams of marketParamsListSnapshot) {
23
+ if (covered === amount)
24
+ break;
25
+ const marketId = blue_sdk_1.MarketUtils.getMarketId(marketParams);
26
+ const allocation = vaultData.allocations.get(marketId);
27
+ if (allocation?.config.enabled !== true)
28
+ continue;
29
+ if (allocation.position.supplyShares === 0n)
30
+ continue;
31
+ const market = allocation.position.market.accrueInterest(now);
32
+ const available = market.toSupplyAssets(allocation.position.supplyShares);
33
+ const assigned = assignedByMarket.get(marketId) ?? 0n;
34
+ const chunk = blue_sdk_1.MathLib.min(available, amount - covered);
35
+ if (assigned + chunk > available) {
36
+ throw new index_js_3.InKindRedeemCoverageError({
37
+ required: amount,
38
+ covered,
39
+ maxExitAssets: covered,
40
+ });
41
+ }
42
+ assignedByMarket.set(marketId, assigned + chunk);
43
+ covered += chunk;
44
+ }
45
+ if (covered < amount) {
46
+ throw new index_js_3.InKindRedeemCoverageError({
47
+ required: amount,
48
+ covered,
49
+ maxExitAssets: covered,
50
+ });
51
+ }
52
+ }
53
+ function getMigrationAmounts(params) {
54
+ const assets = "assets" in params ? params.assets : undefined;
55
+ const shares = "shares" in params ? params.shares : undefined;
56
+ if ((assets == null) === (shares == null)) {
57
+ throw new index_js_3.AmountAndSharesExclusiveError();
58
+ }
59
+ const selectedAmount = assets ?? shares ?? 0n;
60
+ if (selectedAmount <= 0n) {
61
+ throw new index_js_3.NonPositiveInputError(assets != null ? "assets" : "shares", selectedAmount);
62
+ }
63
+ return { assets, shares };
64
+ }
18
65
  /** Binds a viem client to a Vault V1 (MetaMorpho) vault's action builders. */
19
66
  class MorphoVaultV1 {
20
67
  client;
@@ -51,13 +98,15 @@ class MorphoVaultV1 {
51
98
  }
52
99
  /** {@inheritDoc VaultV1Actions.deposit} */
53
100
  deposit(params) {
101
+ const suppliedNow = params.now;
102
+ const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, suppliedNow);
54
103
  const { userAddress, vaultData } = params;
55
104
  const { asset: vaultAsset } = vaultData;
56
105
  (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
57
106
  if (!(0, viem_1.isAddressEqual)(vaultData.address, this.vault)) {
58
107
  throw new index_js_3.VaultAddressMismatchError(this.vault, vaultData.address);
59
108
  }
60
- const createdAt = morpho_ts_1.Time.timestamp();
109
+ const createdAt = suppliedNow ?? morpho_ts_1.Time.timestamp();
61
110
  const deadline = params.deadline ?? createdAt + morpho_ts_1.Time.s.from.h(2n);
62
111
  if (deadline <= createdAt)
63
112
  throw new index_js_3.ExpiredDeadlineError(deadline, createdAt);
@@ -84,7 +133,7 @@ class MorphoVaultV1 {
84
133
  let pendingRequirements;
85
134
  return Object.freeze({
86
135
  getRequirements: async (requirementOptions) => {
87
- const now = morpho_ts_1.Time.timestamp();
136
+ const now = suppliedNow ?? morpho_ts_1.Time.timestamp();
88
137
  if (deadline <= now)
89
138
  throw new index_js_3.ExpiredDeadlineError(deadline, now);
90
139
  if (pendingRequirements != null)
@@ -138,12 +187,14 @@ class MorphoVaultV1 {
138
187
  referralFeeRecipient: common.referralFeeRecipient,
139
188
  deadline,
140
189
  },
141
- metadata: this.client.options.metadata,
190
+ metadata,
142
191
  });
143
192
  },
144
193
  });
145
194
  }
146
195
  withdraw(params) {
196
+ const suppliedNow = params.now;
197
+ const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, suppliedNow);
147
198
  const { amount, userAddress, vaultData } = params;
148
199
  (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
149
200
  if (!(0, viem_1.isAddressEqual)(vaultData.address, this.vault)) {
@@ -153,7 +204,7 @@ class MorphoVaultV1 {
153
204
  throw new index_js_3.NonPositiveInputError("amount", amount);
154
205
  // Reject values outside the ABI range before resolving any requirements.
155
206
  (0, validate_js_1.validateUint256Field)("amount", amount);
156
- const createdAt = morpho_ts_1.Time.timestamp();
207
+ const createdAt = suppliedNow ?? morpho_ts_1.Time.timestamp();
157
208
  const deadline = params.deadline ?? createdAt + morpho_ts_1.Time.s.from.h(2n);
158
209
  if (deadline <= createdAt)
159
210
  throw new index_js_3.ExpiredDeadlineError(deadline, createdAt);
@@ -174,7 +225,7 @@ class MorphoVaultV1 {
174
225
  });
175
226
  return Object.freeze({
176
227
  getRequirements: async () => {
177
- const now = morpho_ts_1.Time.timestamp();
228
+ const now = suppliedNow ?? morpho_ts_1.Time.timestamp();
178
229
  if (deadline <= now)
179
230
  throw new index_js_3.ExpiredDeadlineError(deadline, now);
180
231
  await this.assertWriteTargets(["bundles.vaultBundlesV1"]);
@@ -206,18 +257,20 @@ class MorphoVaultV1 {
206
257
  referralFeeRecipient: common.referralFeeRecipient,
207
258
  deadline,
208
259
  },
209
- metadata: this.client.options.metadata,
260
+ metadata,
210
261
  });
211
262
  },
212
263
  });
213
264
  }
214
265
  /** {@inheritDoc VaultV1Actions.redeem} */
215
266
  redeem(params) {
267
+ const suppliedNow = params.now;
268
+ const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, suppliedNow);
216
269
  (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
217
270
  const { shares, userAddress } = params;
218
271
  if (shares <= 0n)
219
272
  throw new index_js_3.NonPositiveInputError("shares", shares);
220
- const createdAt = morpho_ts_1.Time.timestamp();
273
+ const createdAt = suppliedNow ?? morpho_ts_1.Time.timestamp();
221
274
  const deadline = params.deadline ?? createdAt + morpho_ts_1.Time.s.from.h(2n);
222
275
  if (deadline <= createdAt)
223
276
  throw new index_js_3.ExpiredDeadlineError(deadline, createdAt);
@@ -229,7 +282,7 @@ class MorphoVaultV1 {
229
282
  const spender = (0, morpho_ts_1.getChainAddress)(this.chainId, "bundles.vaultBundlesV1");
230
283
  return Object.freeze({
231
284
  getRequirements: async () => {
232
- const now = morpho_ts_1.Time.timestamp();
285
+ const now = suppliedNow ?? morpho_ts_1.Time.timestamp();
233
286
  if (deadline <= now)
234
287
  throw new index_js_3.ExpiredDeadlineError(deadline, now);
235
288
  await this.assertWriteTargets(["bundles.vaultBundlesV1"]);
@@ -260,13 +313,16 @@ class MorphoVaultV1 {
260
313
  referralFeeRecipient: common.referralFeeRecipient,
261
314
  deadline,
262
315
  },
263
- metadata: this.client.options.metadata,
316
+ metadata,
264
317
  });
265
318
  },
266
319
  });
267
320
  }
268
321
  /** {@inheritDoc VaultV1Actions.inKindRedeem} */
269
- inKindRedeem({ amount, marketParamsList, vaultData, userAddress, deadline: deadlineOverride, }) {
322
+ inKindRedeem(params) {
323
+ const suppliedNow = params.now;
324
+ const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, suppliedNow);
325
+ const { amount, marketParamsList, vaultData, userAddress, deadline: deadlineOverride, } = params;
270
326
  (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
271
327
  if (!(0, viem_1.isAddressEqual)(vaultData.address, this.vault)) {
272
328
  throw new index_js_3.VaultAddressMismatchError(this.vault, vaultData.address);
@@ -282,7 +338,7 @@ class MorphoVaultV1 {
282
338
  irm,
283
339
  lltv,
284
340
  }));
285
- const now = morpho_ts_1.Time.timestamp();
341
+ const now = suppliedNow ?? morpho_ts_1.Time.timestamp();
286
342
  const deadline = deadlineOverride ?? now + morpho_ts_1.Time.s.from.h(2n);
287
343
  // Reject the same bounds the action enforces, before `getRequirements()` can walk the caller
288
344
  // through a vault-share approval or an EIP-712 permit for a deadline `buildTx()` cannot encode.
@@ -291,38 +347,13 @@ class MorphoVaultV1 {
291
347
  throw new index_js_3.ExpiredDeadlineError(deadline, now);
292
348
  const vaultExitBundlesV1 = (0, morpho_ts_1.getChainAddress)(this.chainId, "bundles.vaultExitBundlesV1");
293
349
  const blue = (0, morpho_ts_1.getChainAddress)(this.chainId, "blue");
294
- let covered = 0n;
295
- const assignedByMarket = new Map();
296
- for (const marketParams of marketParamsListSnapshot) {
297
- if (covered === amount)
298
- break;
299
- const marketId = blue_sdk_1.MarketUtils.getMarketId(marketParams);
300
- const allocation = vaultData.allocations.get(marketId);
301
- if (allocation?.config.enabled !== true)
302
- continue;
303
- if (allocation.position.supplyShares === 0n)
304
- continue;
305
- const market = allocation.position.market.accrueInterest(now);
306
- const available = market.toSupplyAssets(allocation.position.supplyShares);
307
- const assigned = assignedByMarket.get(marketId) ?? 0n;
308
- const chunk = blue_sdk_1.MathLib.min(available, amount - covered);
309
- if (assigned + chunk > available) {
310
- throw new index_js_3.InKindRedeemCoverageError({
311
- required: amount,
312
- covered,
313
- maxExitAssets: covered,
314
- });
315
- }
316
- assignedByMarket.set(marketId, assigned + chunk);
317
- covered += chunk;
318
- }
319
- if (covered < amount) {
320
- throw new index_js_3.InKindRedeemCoverageError({
321
- required: amount,
322
- covered,
323
- maxExitAssets: covered,
324
- });
325
- }
350
+ // Reject insufficient ordered market coverage before preparing share requirements.
351
+ validateInKindCoverage({
352
+ amount,
353
+ marketParamsListSnapshot,
354
+ vaultData,
355
+ now,
356
+ });
326
357
  const requiredShareAllowance = (0, index_js_2.computeVaultMaxShareAllowance)({
327
358
  vaultData,
328
359
  deadline: now,
@@ -331,7 +362,7 @@ class MorphoVaultV1 {
331
362
  });
332
363
  return {
333
364
  getRequirements: async () => {
334
- const requirementsTimestamp = morpho_ts_1.Time.timestamp();
365
+ const requirementsTimestamp = suppliedNow ?? morpho_ts_1.Time.timestamp();
335
366
  if (deadline <= requirementsTimestamp) {
336
367
  throw new index_js_3.ExpiredDeadlineError(deadline, requirementsTimestamp);
337
368
  }
@@ -393,6 +424,7 @@ class MorphoVaultV1 {
393
424
  if (this.client.options.supportSignature) {
394
425
  return [
395
426
  (0, index_js_1.encodeVaultSharesPermit)({
427
+ now: suppliedNow == null ? undefined : now,
396
428
  vault: vaultData,
397
429
  version: "vaultV1",
398
430
  spender: vaultExitBundlesV1,
@@ -427,13 +459,15 @@ class MorphoVaultV1 {
427
459
  deadline,
428
460
  requirementSignature: permit,
429
461
  },
430
- metadata: this.client.options.metadata,
462
+ metadata,
431
463
  });
432
464
  },
433
465
  };
434
466
  }
435
467
  /** {@inheritDoc VaultV1Actions.migrateToV2} */
436
468
  migrateToV2(params) {
469
+ const suppliedNow = params.now;
470
+ const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, suppliedNow);
437
471
  (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
438
472
  if (!(0, viem_1.isAddressEqual)(params.sourceVault.address, this.vault)) {
439
473
  throw new index_js_3.VaultAddressMismatchError(this.vault, params.sourceVault.address);
@@ -444,18 +478,10 @@ class MorphoVaultV1 {
444
478
  if ((0, viem_1.isAddressEqual)(this.vault, params.targetVault.address)) {
445
479
  throw new index_js_3.SameVaultMigrationError(this.vault);
446
480
  }
447
- const assets = "assets" in params ? params.assets : undefined;
448
- const shares = "shares" in params ? params.shares : undefined;
449
- if ((assets == null) === (shares == null)) {
450
- throw new index_js_3.AmountAndSharesExclusiveError();
451
- }
452
- const selectedAmount = assets ?? shares ?? 0n;
453
- if (selectedAmount <= 0n) {
454
- throw new index_js_3.NonPositiveInputError(assets != null ? "assets" : "shares", selectedAmount);
455
- }
481
+ const { assets, shares } = getMigrationAmounts(params);
456
482
  const slippageTolerance = params.slippageTolerance ?? blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE;
457
483
  (0, index_js_2.validateSlippageTolerance)(slippageTolerance);
458
- const createdAt = morpho_ts_1.Time.timestamp();
484
+ const createdAt = suppliedNow ?? morpho_ts_1.Time.timestamp();
459
485
  const deadline = params.deadline ?? createdAt + morpho_ts_1.Time.s.from.h(2n);
460
486
  if (deadline <= createdAt)
461
487
  throw new index_js_3.ExpiredDeadlineError(deadline, createdAt);
@@ -504,7 +530,7 @@ class MorphoVaultV1 {
504
530
  });
505
531
  return Object.freeze({
506
532
  getRequirements: async () => {
507
- const now = morpho_ts_1.Time.timestamp();
533
+ const now = suppliedNow ?? morpho_ts_1.Time.timestamp();
508
534
  if (deadline <= now)
509
535
  throw new index_js_3.ExpiredDeadlineError(deadline, now);
510
536
  await this.assertWriteTargets(["bundles.vaultBundlesV1"]);
@@ -543,7 +569,7 @@ class MorphoVaultV1 {
543
569
  referralFeeRecipient: common.referralFeeRecipient,
544
570
  deadline,
545
571
  },
546
- metadata: this.client.options.metadata,
572
+ metadata,
547
573
  });
548
574
  },
549
575
  });
@@ -2,7 +2,7 @@ import { type AccrualVaultV2, MarketParams } from "@morpho-org/blue-sdk";
2
2
  import { fetchAccrualVaultV2 } from "@morpho-org/blue-sdk-viem";
3
3
  import { type Address } from "viem";
4
4
  import type { FetchParameters } from "../../types/data.js";
5
- import { type ActionOutput, type ActionRequirement, type BundlesFundingArgs, type BundlesTokenRequirementsOptions, type Deallocation, type MorphoClientType, type RequirementSignature, type Transaction, type VaultV2DepositAction, type VaultV2ForceRedeemAction, type VaultV2ForceWithdrawAction, type VaultV2InKindRedeemAction, type VaultV2RedeemAction, type VaultV2WithdrawAction } from "../../types/index.js";
5
+ import { type ActionOutput, type ActionRequirement, type BundlesFundingArgs, type BundlesTokenRequirementsOptions, type Deallocation, type MorphoClientType, type ReferenceTimestampParams, type RequirementSignature, type Transaction, type VaultV2DepositAction, type VaultV2ForceRedeemAction, type VaultV2ForceWithdrawAction, type VaultV2InKindRedeemAction, type VaultV2RedeemAction, type VaultV2WithdrawAction } from "../../types/index.js";
6
6
  /**
7
7
  * Action surface for Vault V2 reads and writes; writes route through VaultBundlesV1 and
8
8
  * VaultExitBundlesV1, except `forceRedeem`, which uses the vault's native multicall.
@@ -65,7 +65,9 @@ export interface VaultV2Actions {
65
65
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient.
66
66
  * @throws {ChainWNativeMissingError} when native funding is requested on a chain without wNative.
67
67
  * @throws {NativeAmountOnNonWNativeVaultError} when native funding targets a non-wNative vault.
68
- * @throws {InputExceedsMaxError} when funding or the deadline exceeds uint256.
68
+ * @throws {InputExceedsMaxError} when funding or the deadline exceeds uint256, or when the vault's
69
+ * liquidity adapter is a Midnight adapter (it cannot receive vault allocations, so no deposit
70
+ * fits and `max` is `0n`).
69
71
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when multiple token signatures are supplied.
70
72
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when an unsupported signature is supplied.
71
73
  * @throws {BundlesPermitMismatchError} from `buildTx()` when the signature's spender, amount or
@@ -113,7 +115,7 @@ export interface VaultV2Actions {
113
115
  readonly referralFeePct?: bigint;
114
116
  readonly referralFeeRecipient?: Address;
115
117
  readonly deadline?: bigint;
116
- } & BundlesFundingArgs) => ActionOutput<VaultV2DepositAction, readonly RequirementSignature[], BundlesTokenRequirementsOptions>;
118
+ } & ReferenceTimestampParams & BundlesFundingArgs) => ActionOutput<VaultV2DepositAction, readonly RequirementSignature[], BundlesTokenRequirementsOptions>;
117
119
  /**
118
120
  * Prepares an exact-assets Vault V2 withdrawal through VaultBundlesV1.
119
121
  *
@@ -147,7 +149,9 @@ export interface VaultV2Actions {
147
149
  * @throws {NonPositiveInputError} when `amount` or the computed required share amount is not positive.
148
150
  * @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
149
151
  * at any `getRequirements()` call.
150
- * @throws {InputExceedsMaxError} when `amount` or `deadline` exceeds uint256 at handle creation.
152
+ * @throws {InputExceedsMaxError} when `amount` or `deadline` exceeds uint256 at handle creation,
153
+ * or when the vault's liquidity adapter is a Midnight adapter and `amount` exceeds the vault's
154
+ * idle assets plus what the adapter can withdraw from the liquidity market.
151
155
  * @throws {NegativeInputError} when `referralFeePct` or `slippageTolerance` is negative.
152
156
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is at least WAD.
153
157
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no nonzero recipient.
@@ -192,7 +196,7 @@ export interface VaultV2Actions {
192
196
  readonly referralFeePct?: bigint;
193
197
  readonly referralFeeRecipient?: Address;
194
198
  readonly deadline?: bigint;
195
- }) => ActionOutput<VaultV2WithdrawAction, readonly RequirementSignature[], undefined>;
199
+ } & ReferenceTimestampParams) => ActionOutput<VaultV2WithdrawAction, readonly RequirementSignature[], undefined>;
196
200
  /**
197
201
  * Prepares an exact-shares Vault V2 redemption through VaultBundlesV1.
198
202
  *
@@ -213,7 +217,25 @@ export interface VaultV2Actions {
213
217
  * @throws {NonPositiveInputError} when `shares` is not positive.
214
218
  * @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
215
219
  * requirement resolution.
216
- * @throws {InputExceedsMaxError} when `shares` or `deadline` exceeds uint256.
220
+ * @throws {InputExceedsMaxError} when `shares` or `deadline` exceeds uint256, or from
221
+ * `getRequirements()` when the vault's liquidity adapter is a Midnight adapter and `shares`
222
+ * are worth more than the vault's idle assets plus what the adapter can withdraw from the
223
+ * liquidity market.
224
+ * @throws {UnknownBlueFactory} from `getRequirements()` when the configured chain has no VaultV2 factory.
225
+ * @throws {UnknownBlueOfFactory} from `getRequirements()` when the vault is not a VaultV2 from the
226
+ * configured factory.
227
+ * @throws {UnsupportedBlueVaultV2AdapterError} from `getRequirements()` when the vault or one of its
228
+ * adapters uses an unsupported adapter class.
229
+ * @throws {UnsupportedBlueMarketIrmError} from `getRequirements()` when the vault's liquidity adapter
230
+ * is a Midnight adapter and accrual projects an underlying market with positive debt that uses an
231
+ * unsupported IRM.
232
+ * @throws {UnknownBlueMarketAllocationError} from `getRequirements()` when the vault's liquidity
233
+ * adapter is a Midnight adapter and accrual meets a nested Vault V1 withdraw queue market without
234
+ * allocation data.
235
+ * @throws {InvalidMidnightPositionLossFactorError} from `getRequirements()` when the vault's liquidity
236
+ * adapter is a Midnight adapter and accrual meets a market loss factor below a position's.
237
+ * @throws {InvalidMidnightPositionAccrualStateError} from `getRequirements()` when the vault's
238
+ * liquidity adapter is a Midnight adapter and accrual meets invalid Midnight position inputs.
217
239
  * @throws {NegativeInputError} when `referralFeePct` is negative.
218
240
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
219
241
  * @throws {ReferralFeeRecipientMissingError} when a positive fee has no non-zero recipient.
@@ -221,7 +243,7 @@ export interface VaultV2Actions {
221
243
  * @throws {UnknownAddressError} when VaultBundlesV1 is not registered on the target chain.
222
244
  * @throws {MissingContractCodeError} from `getRequirements()` when a fixed bundles or protocol
223
245
  * contract has no code at its registered address.
224
- * @throws {viem.BaseError} from `getRequirements()` when an allowance, nonce, `eth_chainId`, or
246
+ * @throws {viem.BaseError} from `getRequirements()` when a vault-state, allowance, nonce, `eth_chainId`, or
225
247
  * `eth_getCode` read fails.
226
248
  * @throws {UnknownTokenError} from `getRequirements()` when the share token fetched by
227
249
  * `getData()` has unusable decimals.
@@ -257,7 +279,7 @@ export interface VaultV2Actions {
257
279
  readonly referralFeePct?: bigint;
258
280
  readonly referralFeeRecipient?: Address;
259
281
  readonly deadline?: bigint;
260
- }) => ActionOutput<VaultV2RedeemAction, readonly RequirementSignature[], undefined>;
282
+ } & ReferenceTimestampParams) => ActionOutput<VaultV2RedeemAction, readonly RequirementSignature[], undefined>;
261
283
  /**
262
284
  * Prepares an illiquid Vault V2 exit into idle assets and Morpho Blue supply positions.
263
285
  *
@@ -299,9 +321,11 @@ export interface VaultV2Actions {
299
321
  * @throws {InputExceedsMaxError} when `deadline` exceeds `uint256`.
300
322
  * @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
301
323
  * requirement resolution.
302
- * @throws {VaultV2SingleAdapterRequiredError} when the vault does not have one adapter.
324
+ * @throws {VaultV2SingleAdapterRequiredError} when the vault does not have one adapter, including
325
+ * a vault that mixes Blue and Midnight adapters.
303
326
  * @throws {AdapterNotPartOfVaultError} when `adapter` is not the vault's adapter.
304
- * @throws {VaultV2UnsupportedExitAdapterError} when the adapter is not a MorphoMarketV1AdapterV2.
327
+ * @throws {VaultV2UnsupportedExitAdapterError} when the adapter is not a MorphoMarketV1AdapterV2,
328
+ * including a Midnight adapter.
305
329
  * @throws {InKindRedeemCoverageError} when the deduplicated list cannot cover the exit.
306
330
  * @throws {UnsupportedChainIdError} when no address registry exists for the target chain.
307
331
  * @throws {UnknownAddressError} when VaultExitBundlesV1 is not registered on the target chain.
@@ -346,7 +370,7 @@ export interface VaultV2Actions {
346
370
  readonly userAddress: Address;
347
371
  readonly adapter?: Address;
348
372
  readonly deadline?: bigint;
349
- }) => ActionOutput<VaultV2InKindRedeemAction, readonly RequirementSignature[], undefined>;
373
+ } & ReferenceTimestampParams) => ActionOutput<VaultV2InKindRedeemAction, readonly RequirementSignature[], undefined>;
350
374
  /**
351
375
  * Prepares a Vault V2 force withdrawal through VaultExitBundlesV1.
352
376
  *
@@ -426,9 +450,11 @@ export interface VaultV2Actions {
426
450
  * @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
427
451
  * requirement resolution.
428
452
  * @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` exceeds the SDK maximum.
429
- * @throws {VaultV2SingleAdapterRequiredError} when the vault does not have exactly one adapter.
453
+ * @throws {VaultV2SingleAdapterRequiredError} when the vault does not have exactly one adapter,
454
+ * including a vault that mixes Blue and Midnight adapters.
430
455
  * @throws {AdapterNotPartOfVaultError} when `adapter` is not the vault's adapter.
431
- * @throws {VaultV2UnsupportedExitAdapterError} when the adapter is not a MorphoMarketV1AdapterV2.
456
+ * @throws {VaultV2UnsupportedExitAdapterError} when the adapter is not a MorphoMarketV1AdapterV2,
457
+ * including a Midnight adapter.
432
458
  * @throws {VaultV2UnsupportedLiquidityAdapterError} when the vault routes liquidity through
433
459
  * another adapter.
434
460
  * @throws {VaultV2UndecodableLiquidityDataError} when the vault's `liquidityData` does not decode
@@ -488,7 +514,7 @@ export interface VaultV2Actions {
488
514
  readonly minSharePriceE27?: bigint;
489
515
  readonly referralFeePct?: bigint;
490
516
  readonly referralFeeRecipient?: Address;
491
- }) => ActionOutput<VaultV2ForceWithdrawAction, readonly RequirementSignature[], undefined>;
517
+ } & ReferenceTimestampParams) => ActionOutput<VaultV2ForceWithdrawAction, readonly RequirementSignature[], undefined>;
492
518
  /**
493
519
  * Prepares a force redeem transaction for the VaultV2 contract using the vault's native multicall.
494
520
  *
@@ -533,7 +559,7 @@ export interface VaultV2Actions {
533
559
  shares: bigint;
534
560
  };
535
561
  userAddress: Address;
536
- }) => {
562
+ } & ReferenceTimestampParams) => {
537
563
  buildTx: () => Readonly<Transaction<VaultV2ForceRedeemAction>>;
538
564
  };
539
565
  }
@@ -561,7 +587,7 @@ export declare class MorphoVaultV2 implements VaultV2Actions {
561
587
  readonly referralFeePct?: bigint;
562
588
  readonly referralFeeRecipient?: Address;
563
589
  readonly deadline?: bigint;
564
- } & BundlesFundingArgs): Readonly<{
590
+ } & ReferenceTimestampParams & BundlesFundingArgs): Readonly<{
565
591
  getRequirements: (requirementOptions?: BundlesTokenRequirementsOptions) => Promise<readonly ActionRequirement[]>;
566
592
  buildTx: (signatures?: readonly RequirementSignature[]) => Readonly<Transaction<VaultV2DepositAction>>;
567
593
  }>;
@@ -573,7 +599,7 @@ export declare class MorphoVaultV2 implements VaultV2Actions {
573
599
  readonly referralFeePct?: bigint;
574
600
  readonly referralFeeRecipient?: Address;
575
601
  readonly deadline?: bigint;
576
- }): Readonly<{
602
+ } & ReferenceTimestampParams): Readonly<{
577
603
  getRequirements: () => Promise<readonly (Readonly<Transaction<import("../../index.js").ERC20ApprovalAction>> | import("../../index.js").Requirement<import("../../index.js").Erc2612RequirementSignature, undefined>)[]>;
578
604
  buildTx: (signatures?: readonly RequirementSignature[]) => Readonly<Transaction<VaultV2WithdrawAction>>;
579
605
  }>;
@@ -584,21 +610,21 @@ export declare class MorphoVaultV2 implements VaultV2Actions {
584
610
  readonly referralFeePct?: bigint;
585
611
  readonly referralFeeRecipient?: Address;
586
612
  readonly deadline?: bigint;
587
- }): Readonly<{
613
+ } & ReferenceTimestampParams): Readonly<{
588
614
  getRequirements: () => Promise<readonly (Readonly<Transaction<import("../../index.js").ERC20ApprovalAction>> | import("../../index.js").Requirement<import("../../index.js").Erc2612RequirementSignature, undefined>)[]>;
589
615
  buildTx: (signatures?: readonly RequirementSignature[]) => Readonly<Transaction<VaultV2RedeemAction>>;
590
616
  }>;
591
617
  /** {@inheritDoc VaultV2Actions.inKindRedeem} */
592
- inKindRedeem({ amount, marketParamsList, vaultData, userAddress, adapter: adapterOverride, deadline: deadlineOverride, }: {
618
+ inKindRedeem(params: {
593
619
  readonly amount: bigint;
594
620
  readonly marketParamsList: readonly MarketParams[];
595
621
  readonly vaultData: AccrualVaultV2;
596
622
  readonly userAddress: Address;
597
623
  readonly adapter?: Address;
598
624
  readonly deadline?: bigint;
599
- }): ActionOutput<VaultV2InKindRedeemAction, readonly RequirementSignature[], undefined>;
625
+ } & ReferenceTimestampParams): ActionOutput<VaultV2InKindRedeemAction, readonly RequirementSignature[], undefined>;
600
626
  /** {@inheritDoc VaultV2Actions.forceWithdraw} */
601
- forceWithdraw({ exitAssets, vaultData, userAddress, adapter: adapterOverride, deadline: deadlineOverride, slippageTolerance, minSharePriceE27: minSharePriceE27Override, referralFeePct, referralFeeRecipient, }: {
627
+ forceWithdraw(params: {
602
628
  readonly exitAssets: bigint;
603
629
  readonly vaultData: AccrualVaultV2;
604
630
  readonly userAddress: Address;
@@ -608,14 +634,14 @@ export declare class MorphoVaultV2 implements VaultV2Actions {
608
634
  readonly minSharePriceE27?: bigint;
609
635
  readonly referralFeePct?: bigint;
610
636
  readonly referralFeeRecipient?: Address;
611
- }): ActionOutput<VaultV2ForceWithdrawAction, readonly RequirementSignature[], undefined>;
612
- forceRedeem({ deallocations, redeem, userAddress, }: {
637
+ } & ReferenceTimestampParams): ActionOutput<VaultV2ForceWithdrawAction, readonly RequirementSignature[], undefined>;
638
+ forceRedeem(params: {
613
639
  deallocations: readonly Deallocation[];
614
640
  redeem: {
615
641
  shares: bigint;
616
642
  };
617
643
  userAddress: Address;
618
- }): {
644
+ } & ReferenceTimestampParams): {
619
645
  buildTx: () => Readonly<Transaction<VaultV2ForceRedeemAction>>;
620
646
  };
621
647
  }