@morpho-org/morpho-sdk 7.1.0-next.1 → 8.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (217) hide show
  1. package/MIGRATION-v7-to-v8.md +290 -0
  2. package/README.md +11 -6
  3. package/lib/cjs/abis.d.ts +1 -1
  4. package/lib/cjs/abis.js +1 -5
  5. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +2 -0
  6. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +2 -0
  7. package/lib/cjs/actions/midnight/cancelAndMake.d.ts +17 -71
  8. package/lib/cjs/actions/midnight/cancelAndMake.js +41 -29
  9. package/lib/cjs/actions/midnight/redeem.d.ts +8 -1
  10. package/lib/cjs/actions/midnight/redeem.js +18 -1
  11. package/lib/cjs/actions/midnight/repayWithdrawCollateral.d.ts +19 -3
  12. package/lib/cjs/actions/midnight/repayWithdrawCollateral.js +24 -2
  13. package/lib/cjs/actions/midnight/supplyCollateral.d.ts +1 -0
  14. package/lib/cjs/actions/midnight/supplyCollateral.js +3 -0
  15. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +21 -6
  16. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +49 -14
  17. package/lib/cjs/actions/midnight/takeBorrow.d.ts +18 -5
  18. package/lib/cjs/actions/midnight/takeBorrow.js +44 -12
  19. package/lib/cjs/actions/midnight/takeLend.d.ts +10 -5
  20. package/lib/cjs/actions/midnight/takeLend.js +27 -9
  21. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.d.ts +22 -7
  22. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +51 -27
  23. package/lib/cjs/actions/midnight/takeWithdraw.d.ts +14 -5
  24. package/lib/cjs/actions/midnight/takeWithdraw.js +40 -9
  25. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +6 -2
  26. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +11 -6
  27. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +3 -0
  28. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +2 -1
  29. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +13 -2
  30. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +17 -6
  31. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +3 -0
  32. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +2 -1
  33. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +5 -0
  34. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +4 -1
  35. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +3 -1
  36. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.js +5 -1
  37. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +3 -1
  38. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +5 -1
  39. package/lib/cjs/actions/requirements/validateClientChainId.d.ts +26 -0
  40. package/lib/cjs/actions/requirements/validateClientChainId.js +34 -0
  41. package/lib/cjs/blue/abis.d.ts +1 -1
  42. package/lib/cjs/blue/abis.js +1 -5
  43. package/lib/cjs/blue/entities.d.ts +4 -4
  44. package/lib/cjs/blue/entities.js +1 -3
  45. package/lib/cjs/blue/errors.d.ts +2 -2
  46. package/lib/cjs/blue/errors.js +3 -5
  47. package/lib/cjs/blue/fetch.d.ts +2 -2
  48. package/lib/cjs/blue/fetch.js +1 -3
  49. package/lib/cjs/blue/types.d.ts +1 -1
  50. package/lib/cjs/blue/utils.d.ts +2 -2
  51. package/lib/cjs/blue/utils.js +5 -2
  52. package/lib/cjs/entities/blue/blue.d.ts +109 -27
  53. package/lib/cjs/entities/blue/blue.js +103 -28
  54. package/lib/cjs/entities/index.d.ts +5 -5
  55. package/lib/cjs/entities/index.js +1 -3
  56. package/lib/cjs/entities/midnight/deadline.d.ts +15 -0
  57. package/lib/cjs/entities/midnight/deadline.js +24 -0
  58. package/lib/cjs/entities/midnight/midnight.d.ts +427 -72
  59. package/lib/cjs/entities/midnight/midnight.js +864 -186
  60. package/lib/cjs/entities/midnight/publication.d.ts +134 -0
  61. package/lib/cjs/entities/midnight/publication.js +404 -0
  62. package/lib/cjs/entities/midnight/types.d.ts +26 -5
  63. package/lib/cjs/entities/requirements/assertContractCode.d.ts +49 -0
  64. package/lib/cjs/entities/requirements/assertContractCode.js +79 -0
  65. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +19 -4
  66. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +45 -18
  67. package/lib/cjs/entities/requirements/getUnusedPermit2Nonce.d.ts +3 -2
  68. package/lib/cjs/entities/requirements/getUnusedPermit2Nonce.js +5 -2
  69. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -1
  70. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +8 -2
  71. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +63 -11
  72. package/lib/cjs/entities/vaultV1/vaultV1.js +52 -15
  73. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +62 -44
  74. package/lib/cjs/entities/vaultV2/vaultV2.js +53 -48
  75. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +3 -5
  76. package/lib/cjs/entities/vaultV2BlueReallocationData.js +3 -9
  77. package/lib/cjs/errors.d.ts +4 -4
  78. package/lib/cjs/errors.js +7 -10
  79. package/lib/cjs/fetch.d.ts +2 -2
  80. package/lib/cjs/fetch.js +1 -3
  81. package/lib/cjs/helpers/constant.d.ts +5 -0
  82. package/lib/cjs/helpers/constant.js +6 -1
  83. package/lib/cjs/helpers/index.d.ts +1 -1
  84. package/lib/cjs/helpers/index.js +2 -1
  85. package/lib/cjs/helpers/validate.d.ts +163 -8
  86. package/lib/cjs/helpers/validate.js +179 -8
  87. package/lib/cjs/helpers/validateMidnightBounds.d.ts +47 -0
  88. package/lib/cjs/helpers/validateMidnightBounds.js +79 -0
  89. package/lib/cjs/helpers/validateMidnightPublication.d.ts +12 -0
  90. package/lib/cjs/helpers/validateMidnightPublication.js +144 -0
  91. package/lib/cjs/helpers/validateMidnightReceiver.d.ts +29 -0
  92. package/lib/cjs/helpers/validateMidnightReceiver.js +84 -0
  93. package/lib/cjs/helpers/validateMidnightTakeFeasibility.d.ts +102 -0
  94. package/lib/cjs/helpers/validateMidnightTakeFeasibility.js +231 -0
  95. package/lib/cjs/index.d.ts +1 -0
  96. package/lib/cjs/index.js +3 -0
  97. package/lib/cjs/midnight/errors.d.ts +1 -2
  98. package/lib/cjs/midnight/errors.js +4 -5
  99. package/lib/cjs/types/error.d.ts +197 -4
  100. package/lib/cjs/types/error.js +265 -5
  101. package/lib/cjs/types/index.d.ts +1 -0
  102. package/lib/cjs/types/index.js +1 -0
  103. package/lib/cjs/types/midnightCancelAndMake.d.ts +70 -0
  104. package/lib/cjs/types/midnightCancelAndMake.js +2 -0
  105. package/lib/cjs/types/midnightPublicationError.d.ts +332 -0
  106. package/lib/cjs/types/midnightPublicationError.js +376 -0
  107. package/lib/cjs/types.d.ts +1 -1
  108. package/lib/cjs/utils.d.ts +4 -3
  109. package/lib/cjs/utils.js +8 -4
  110. package/lib/esm/abis.d.ts +1 -1
  111. package/lib/esm/abis.js +1 -1
  112. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +2 -0
  113. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +2 -0
  114. package/lib/esm/actions/midnight/cancelAndMake.d.ts +17 -71
  115. package/lib/esm/actions/midnight/cancelAndMake.js +42 -30
  116. package/lib/esm/actions/midnight/redeem.d.ts +8 -1
  117. package/lib/esm/actions/midnight/redeem.js +18 -1
  118. package/lib/esm/actions/midnight/repayWithdrawCollateral.d.ts +19 -3
  119. package/lib/esm/actions/midnight/repayWithdrawCollateral.js +24 -2
  120. package/lib/esm/actions/midnight/supplyCollateral.d.ts +1 -0
  121. package/lib/esm/actions/midnight/supplyCollateral.js +3 -0
  122. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +21 -6
  123. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +52 -17
  124. package/lib/esm/actions/midnight/takeBorrow.d.ts +18 -5
  125. package/lib/esm/actions/midnight/takeBorrow.js +47 -15
  126. package/lib/esm/actions/midnight/takeLend.d.ts +10 -5
  127. package/lib/esm/actions/midnight/takeLend.js +27 -9
  128. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.d.ts +22 -7
  129. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +52 -28
  130. package/lib/esm/actions/midnight/takeWithdraw.d.ts +14 -5
  131. package/lib/esm/actions/midnight/takeWithdraw.js +41 -10
  132. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +6 -2
  133. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +13 -8
  134. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +3 -0
  135. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +2 -1
  136. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +13 -2
  137. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +17 -6
  138. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +3 -0
  139. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +2 -1
  140. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +5 -0
  141. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +4 -1
  142. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +3 -1
  143. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.js +5 -1
  144. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +3 -1
  145. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +5 -1
  146. package/lib/esm/actions/requirements/validateClientChainId.d.ts +26 -0
  147. package/lib/esm/actions/requirements/validateClientChainId.js +30 -0
  148. package/lib/esm/blue/abis.d.ts +1 -1
  149. package/lib/esm/blue/abis.js +1 -1
  150. package/lib/esm/blue/entities.d.ts +4 -4
  151. package/lib/esm/blue/entities.js +2 -2
  152. package/lib/esm/blue/errors.d.ts +2 -2
  153. package/lib/esm/blue/errors.js +2 -2
  154. package/lib/esm/blue/fetch.d.ts +2 -2
  155. package/lib/esm/blue/fetch.js +2 -2
  156. package/lib/esm/blue/types.d.ts +1 -1
  157. package/lib/esm/blue/utils.d.ts +2 -2
  158. package/lib/esm/blue/utils.js +2 -2
  159. package/lib/esm/entities/blue/blue.d.ts +109 -27
  160. package/lib/esm/entities/blue/blue.js +104 -29
  161. package/lib/esm/entities/index.d.ts +5 -5
  162. package/lib/esm/entities/index.js +2 -2
  163. package/lib/esm/entities/midnight/deadline.d.ts +15 -0
  164. package/lib/esm/entities/midnight/deadline.js +20 -0
  165. package/lib/esm/entities/midnight/midnight.d.ts +427 -72
  166. package/lib/esm/entities/midnight/midnight.js +865 -187
  167. package/lib/esm/entities/midnight/publication.d.ts +134 -0
  168. package/lib/esm/entities/midnight/publication.js +397 -0
  169. package/lib/esm/entities/midnight/types.d.ts +26 -5
  170. package/lib/esm/entities/requirements/assertContractCode.d.ts +49 -0
  171. package/lib/esm/entities/requirements/assertContractCode.js +74 -0
  172. package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +19 -4
  173. package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +47 -20
  174. package/lib/esm/entities/requirements/getUnusedPermit2Nonce.d.ts +3 -2
  175. package/lib/esm/entities/requirements/getUnusedPermit2Nonce.js +5 -2
  176. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -1
  177. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +8 -2
  178. package/lib/esm/entities/vaultV1/vaultV1.d.ts +63 -11
  179. package/lib/esm/entities/vaultV1/vaultV1.js +53 -16
  180. package/lib/esm/entities/vaultV2/vaultV2.d.ts +62 -44
  181. package/lib/esm/entities/vaultV2/vaultV2.js +54 -49
  182. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +3 -5
  183. package/lib/esm/entities/vaultV2BlueReallocationData.js +3 -9
  184. package/lib/esm/errors.d.ts +4 -4
  185. package/lib/esm/errors.js +4 -4
  186. package/lib/esm/fetch.d.ts +2 -2
  187. package/lib/esm/fetch.js +2 -2
  188. package/lib/esm/helpers/constant.d.ts +5 -0
  189. package/lib/esm/helpers/constant.js +5 -0
  190. package/lib/esm/helpers/index.d.ts +1 -1
  191. package/lib/esm/helpers/index.js +1 -1
  192. package/lib/esm/helpers/validate.d.ts +163 -8
  193. package/lib/esm/helpers/validate.js +177 -7
  194. package/lib/esm/helpers/validateMidnightBounds.d.ts +47 -0
  195. package/lib/esm/helpers/validateMidnightBounds.js +72 -0
  196. package/lib/esm/helpers/validateMidnightPublication.d.ts +12 -0
  197. package/lib/esm/helpers/validateMidnightPublication.js +139 -0
  198. package/lib/esm/helpers/validateMidnightReceiver.d.ts +29 -0
  199. package/lib/esm/helpers/validateMidnightReceiver.js +80 -0
  200. package/lib/esm/helpers/validateMidnightTakeFeasibility.d.ts +102 -0
  201. package/lib/esm/helpers/validateMidnightTakeFeasibility.js +226 -0
  202. package/lib/esm/index.d.ts +1 -0
  203. package/lib/esm/index.js +1 -0
  204. package/lib/esm/midnight/errors.d.ts +1 -2
  205. package/lib/esm/midnight/errors.js +1 -2
  206. package/lib/esm/types/error.d.ts +197 -4
  207. package/lib/esm/types/error.js +253 -2
  208. package/lib/esm/types/index.d.ts +1 -0
  209. package/lib/esm/types/index.js +1 -0
  210. package/lib/esm/types/midnightCancelAndMake.d.ts +70 -0
  211. package/lib/esm/types/midnightCancelAndMake.js +1 -0
  212. package/lib/esm/types/midnightPublicationError.d.ts +332 -0
  213. package/lib/esm/types/midnightPublicationError.js +359 -0
  214. package/lib/esm/types.d.ts +1 -1
  215. package/lib/esm/utils.d.ts +4 -3
  216. package/lib/esm/utils.js +4 -3
  217. package/package.json +9 -8
@@ -8,10 +8,12 @@ const viem_1 = require("viem");
8
8
  const actions_1 = require("viem/actions");
9
9
  const common_js_1 = require("../../actions/bundles/common.js");
10
10
  const index_js_1 = require("../../actions/index.js");
11
+ const validateClientChainId_js_1 = require("../../actions/requirements/validateClientChainId.js");
11
12
  const index_js_2 = require("../../helpers/index.js");
12
13
  const slippage_js_1 = require("../../helpers/slippage.js");
13
14
  const validate_js_1 = require("../../helpers/validate.js");
14
15
  const index_js_3 = require("../../types/index.js");
16
+ const assertContractCode_js_1 = require("../requirements/assertContractCode.js");
15
17
  const getVaultBundlesSharesRequirements_js_1 = require("../requirements/getVaultBundlesSharesRequirements.js");
16
18
  const index_js_4 = require("../requirements/index.js");
17
19
  /** Binds a viem client to a Vault V1 (MetaMorpho) vault's action builders. */
@@ -26,12 +28,33 @@ class MorphoVaultV1 {
26
28
  this.chainId = chainId;
27
29
  }
28
30
  async getData(parameters) {
29
- (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
31
+ await (0, validateClientChainId_js_1.validateClientChainId)(this.client.viemClient, this.chainId);
30
32
  return (0, blue_sdk_viem_1.fetchAccrualVault)(this.vault, this.client.viemClient, {
31
33
  ...parameters,
32
34
  deployless: this.client.options.supportDeployless,
33
35
  });
34
36
  }
37
+ /**
38
+ * Asserts that every registry contract this flow's transaction targets or names as
39
+ * spender/authorized operator holds code, with one `eth_getCode` per distinct address
40
+ * before any other requirement read. This VaultV1 `inKindRedeem` path validates the client chain
41
+ * before this code check.
42
+ *
43
+ * @param roles - Registry labels to resolve and check; `permit2` is skipped when the
44
+ * chain registers none.
45
+ */
46
+ async assertWriteTargets(roles) {
47
+ const targets = [];
48
+ for (const role of roles) {
49
+ const address = role === "permit2"
50
+ ? (0, morpho_ts_1.getChainAddresses)(this.chainId).permit2
51
+ : (0, morpho_ts_1.getChainAddress)(this.chainId, role);
52
+ if (address == null)
53
+ continue;
54
+ targets.push({ role, address });
55
+ }
56
+ await (0, assertContractCode_js_1.assertRegistryTargetsHaveCode)(this.client.viemClient, this.chainId, targets);
57
+ }
35
58
  /** {@inheritDoc VaultV1Actions.deposit} */
36
59
  deposit(params) {
37
60
  const { userAddress, vaultData } = params;
@@ -74,20 +97,26 @@ class MorphoVaultV1 {
74
97
  return await pendingRequirements;
75
98
  // Memoize the in-flight promise so concurrent callers share one round of
76
99
  // allowance/nonce reads.
77
- const pending = funding.value > 0n
78
- ? Promise.resolve([])
79
- : (0, index_js_4.getBundlesTokenRequirements)(this.client.viemClient, {
80
- token: vaultAsset,
81
- spender,
82
- amount: funding.assets,
83
- owner: userAddress,
84
- chainId: this.chainId,
85
- deadline,
86
- supportSignature: this.client.options.supportSignature,
87
- supportDeployless: this.client.options.supportDeployless,
88
- useSimplePermit: requirementOptions?.useSimplePermit,
89
- permit2Nonce: requirementOptions?.permit2Nonce,
90
- });
100
+ const pending = (async () => {
101
+ await this.assertWriteTargets(funding.value === 0n && this.client.options.supportSignature
102
+ ? ["bundles.vaultBundlesV1", "permit2"]
103
+ : ["bundles.vaultBundlesV1"]);
104
+ return funding.value > 0n
105
+ ? []
106
+ : (0, index_js_4.getBundlesTokenRequirements)(this.client.viemClient, {
107
+ token: vaultAsset,
108
+ spender,
109
+ amount: funding.assets,
110
+ owner: userAddress,
111
+ chainId: this.chainId,
112
+ deadline,
113
+ supportSignature: this.client.options.supportSignature,
114
+ supportDeployless: this.client.options.supportDeployless,
115
+ useSimplePermit: requirementOptions?.useSimplePermit,
116
+ permit2Nonce: requirementOptions?.permit2Nonce,
117
+ now,
118
+ });
119
+ })();
91
120
  pendingRequirements = pending;
92
121
  try {
93
122
  return await pending;
@@ -161,6 +190,7 @@ class MorphoVaultV1 {
161
190
  const now = morpho_ts_1.Time.timestamp();
162
191
  if (deadline <= now)
163
192
  throw new index_js_3.ExpiredDeadlineError(deadline, now);
193
+ await this.assertWriteTargets(["bundles.vaultBundlesV1"]);
164
194
  // Re-read the live share allowance on every call instead of caching the resolved
165
195
  // requirements: the allowance is the sole cap on the burn, so a caller that executed the
166
196
  // returned approval must see it satisfied on the next call, and an allowance revoked or
@@ -175,6 +205,7 @@ class MorphoVaultV1 {
175
205
  maxShareAllowance,
176
206
  deadline,
177
207
  supportSignature: this.client.options.supportSignature,
208
+ now,
178
209
  });
179
210
  },
180
211
  buildTx: (signatures) => {
@@ -220,6 +251,7 @@ class MorphoVaultV1 {
220
251
  const now = morpho_ts_1.Time.timestamp();
221
252
  if (deadline <= now)
222
253
  throw new index_js_3.ExpiredDeadlineError(deadline, now);
254
+ await this.assertWriteTargets(["bundles.vaultBundlesV1"]);
223
255
  return (0, getVaultBundlesSharesRequirements_js_1.getVaultBundlesSharesRequirements)(this.client.viemClient, {
224
256
  vaultData: await this.getData(),
225
257
  version: "vaultV1",
@@ -228,6 +260,7 @@ class MorphoVaultV1 {
228
260
  requiredShareAllowance: shares,
229
261
  deadline,
230
262
  supportSignature: this.client.options.supportSignature,
263
+ now,
231
264
  });
232
265
  },
233
266
  buildTx: (signatures) => {
@@ -321,6 +354,8 @@ class MorphoVaultV1 {
321
354
  if (deadline <= requirementsTimestamp) {
322
355
  throw new index_js_3.ExpiredDeadlineError(deadline, requirementsTimestamp);
323
356
  }
357
+ await (0, validateClientChainId_js_1.validateClientChainId)(this.client.viemClient, this.chainId);
358
+ await this.assertWriteTargets(["bundles.vaultExitBundlesV1", "blue"]);
324
359
  const [allowance, nonce, blueBalance, morpho, blueFeeRecipient] = await Promise.all([
325
360
  (0, actions_1.readContract)(this.client.viemClient, {
326
361
  address: this.vault,
@@ -502,6 +537,7 @@ class MorphoVaultV1 {
502
537
  const now = morpho_ts_1.Time.timestamp();
503
538
  if (deadline <= now)
504
539
  throw new index_js_3.ExpiredDeadlineError(deadline, now);
540
+ await this.assertWriteTargets(["bundles.vaultBundlesV1"]);
505
541
  // Re-read the live share allowance on every call instead of caching the resolved
506
542
  // requirements: in assets mode the calldata carries no share limit, so the allowance is
507
543
  // the sole cap on the burn. A caller that executed the returned approval must see it
@@ -517,6 +553,7 @@ class MorphoVaultV1 {
517
553
  maxShareAllowance,
518
554
  deadline,
519
555
  supportSignature: this.client.options.supportSignature,
556
+ now,
520
557
  });
521
558
  },
522
559
  buildTx: (signatures) => {
@@ -15,12 +15,17 @@ export interface VaultV2Actions {
15
15
  *
16
16
  * @param parameters - Optional viem fetch parameters (block number, block tag, state override).
17
17
  * @returns The hydrated `AccrualVaultV2` snapshot.
18
- * @throws {ChainIdMismatchError} when the connected client targets another chain or has no chain.
18
+ * @throws {ChainIdMismatchError} when the connected client declares another chain or none, or its RPC reports another chain via `eth_chainId`.
19
+ * @throws {viem.BaseError} when the `eth_chainId` request or a state read fails.
19
20
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
20
21
  * @throws {UnknownBlueFactory} when the configured chain has no VaultV2 factory.
21
22
  * @throws {UnknownBlueOfFactory} when the vault is not a VaultV2 from the configured factory.
22
23
  * @throws {UnsupportedBlueVaultV2AdapterError} when the vault or one of its adapters uses an
23
24
  * unsupported adapter class.
25
+ * @throws {UnknownTokenError} when the vault's share token has unusable decimals.
26
+ * @throws {MalformedTokenMetadataError} when the share token's `eip712Domain()` or
27
+ * `bytes32` `name`/`symbol` retry returns non-empty data that does not decode, or when the
28
+ * deployless query reports malformed `name()`/`symbol()`/`eip712Domain()` returndata (no fallback).
24
29
  */
25
30
  getData: (parameters?: FetchParameters) => Promise<Awaited<ReturnType<typeof fetchAccrualVaultV2>>>;
26
31
  /**
@@ -47,7 +52,8 @@ export interface VaultV2Actions {
47
52
  * two hours from handle creation.
48
53
  * @returns Lazy token prerequisite resolution and a synchronous deep-frozen VaultBundlesV1
49
54
  * transaction builder.
50
- * @throws {ChainIdMismatchError} when the connected client targets another chain.
55
+ * @throws {ChainIdMismatchError} when the connected client targets another chain, or from
56
+ * `getRequirements()` when the RPC reports another chain via `eth_chainId`.
51
57
  * @throws {UnsupportedBlueMarketIrmError} when an underlying market with positive debt uses an unsupported IRM.
52
58
  * @throws {VaultAddressMismatchError} when `vaultData` belongs to another vault.
53
59
  * @throws {ExpiredDeadlineError} when the deadline is stale at creation or requirement resolution.
@@ -63,9 +69,8 @@ export interface VaultV2Actions {
63
69
  * owner is consumed and none was passed explicitly.
64
70
  * @throws {Permit2SignatureTransferNonceAlreadyUsedError} from `getRequirements()` when the
65
71
  * explicit Permit2 nonce is consumed.
66
- * @throws {InputExceedsMaxError} when funding or the deadline exceeds uint256, when the vault's
67
- * liquidity adapter is a Midnight adapter (it cannot receive vault allocations, so no deposit
68
- * fits and `max` is `0n`), or from `getRequirements()` when the Permit2 nonce exceeds uint256.
72
+ * @throws {InputExceedsMaxError} when funding or the deadline exceeds uint256, or from
73
+ * `getRequirements()` when the Permit2 nonce exceeds uint256.
69
74
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when multiple token signatures are supplied.
70
75
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when an unsupported signature is supplied.
71
76
  * @throws {BundlesPermitMismatchError} from `buildTx()` when the signature's spender, amount or
@@ -76,7 +81,14 @@ export interface VaultV2Actions {
76
81
  * @throws {BundlesRequirementSignatureMismatchError} from `buildTx()` when signature metadata is malformed.
77
82
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
78
83
  * @throws {UnknownAddressError} when VaultBundlesV1 is not registered.
79
- * @throws {viem.BaseError} from `getRequirements()` when an allowance, nonce, or token metadata read fails.
84
+ * @throws {UnknownTokenError} from `getRequirements()` when the token's `decimals()` is unusable.
85
+ * @throws {MalformedTokenMetadataError} when the token's `eip712Domain()` or
86
+ * `bytes32` `name`/`symbol` retry returns non-empty data that does not decode, or when the
87
+ * deployless query reports malformed `name()`/`symbol()`/`eip712Domain()` returndata (no fallback).
88
+ * @throws {MissingContractCodeError} from `getRequirements()` when a fixed bundles or protocol
89
+ * contract has no code at its registered address.
90
+ * @throws {viem.BaseError} from `getRequirements()` when an allowance, nonce, token metadata
91
+ * read, or `eth_chainId` request fails.
80
92
  * @example
81
93
  * ```ts
82
94
  * import { morphoViemExtension } from "@morpho-org/morpho-sdk";
@@ -136,13 +148,12 @@ export interface VaultV2Actions {
136
148
  * @remarks VaultBundlesV1 skips a share permit whose nonce was already consumed and proceeds
137
149
  * under the live allowance, so execute every requirement returned by the latest
138
150
  * `getRequirements()` — including the oversized-allowance reset — before submitting.
139
- * @throws {ChainIdMismatchError} when the connected client targets another chain.
151
+ * @throws {ChainIdMismatchError} when the connected client targets another chain, or from
152
+ * `getRequirements()` when the RPC reports another chain via `eth_chainId`.
140
153
  * @throws {NonPositiveInputError} when `amount` or the computed share cap is not positive.
141
154
  * @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
142
155
  * at any `getRequirements()` call.
143
- * @throws {InputExceedsMaxError} when `amount` or `deadline` exceeds uint256 at handle creation,
144
- * or when the vault's liquidity adapter is a Midnight adapter and `amount` exceeds the vault's
145
- * idle assets.
156
+ * @throws {InputExceedsMaxError} when `amount` or `deadline` exceeds uint256 at handle creation.
146
157
  * @throws {NegativeInputError} when `referralFeePct` or `slippageTolerance` is negative.
147
158
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is at least WAD.
148
159
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no nonzero recipient.
@@ -155,7 +166,10 @@ export interface VaultV2Actions {
155
166
  * @throws {BundlesPermitMismatchError} from `buildTx()` when the share permit is malformed or
156
167
  * its spender or deadline differ from this operation's, or its amount is outside
157
168
  * `[cap, cap / (1 - slippageTolerance)]`.
158
- * @throws {viem.BaseError} when an allowance or nonce read or transaction encoding fails.
169
+ * @throws {MissingContractCodeError} from `getRequirements()` when a fixed bundles or protocol
170
+ * contract has no code at its registered address.
171
+ * @throws {viem.BaseError} from `getRequirements()` when an allowance, nonce, or `eth_chainId`
172
+ * read fails, or transaction encoding fails.
159
173
  * @example
160
174
  * ```ts
161
175
  * import { morphoViemExtension } from "@morpho-org/morpho-sdk";
@@ -199,34 +213,26 @@ export interface VaultV2Actions {
199
213
  * @param [params.referralFeeRecipient] - Non-zero recipient required when `referralFeePct` is positive.
200
214
  * @param [params.deadline] - VaultBundlesV1 execution deadline; defaults to two hours from now.
201
215
  * @returns Lazy exact share-allowance requirements and a synchronous transaction builder.
202
- * @throws {ChainIdMismatchError} when the client and entity target different chains.
216
+ * @throws {ChainIdMismatchError} when the client and entity target different chains, or from
217
+ * `getRequirements()` when the RPC reports another chain via `eth_chainId`.
203
218
  * @throws {NonPositiveInputError} when `shares` is not positive.
204
219
  * @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
205
220
  * requirement resolution.
206
- * @throws {InputExceedsMaxError} when `shares` or `deadline` exceeds uint256, or from
207
- * `getRequirements()` when the vault's liquidity adapter is a Midnight adapter and `shares`
208
- * are worth more than the vault's idle assets.
209
- * @throws {UnknownBlueFactory} from `getRequirements()` when the configured chain has no VaultV2 factory.
210
- * @throws {UnknownBlueOfFactory} from `getRequirements()` when the vault is not a VaultV2 from the
211
- * configured factory.
212
- * @throws {UnsupportedBlueVaultV2AdapterError} from `getRequirements()` when the vault or one of its
213
- * adapters uses an unsupported adapter class.
214
- * @throws {UnsupportedBlueMarketIrmError} from `getRequirements()` when the vault's liquidity adapter
215
- * is a Midnight adapter and accrual projects an underlying market with positive debt that uses an
216
- * unsupported IRM.
217
- * @throws {UnknownBlueMarketAllocationError} from `getRequirements()` when the vault's liquidity
218
- * adapter is a Midnight adapter and accrual meets a nested Vault V1 withdraw queue market without
219
- * allocation data.
220
- * @throws {InvalidMidnightPositionLossFactorError} from `getRequirements()` when the vault's liquidity
221
- * adapter is a Midnight adapter and accrual meets a market loss factor below a position's.
222
- * @throws {InvalidMidnightPositionAccrualStateError} from `getRequirements()` when the vault's
223
- * liquidity adapter is a Midnight adapter and accrual meets invalid Midnight position inputs.
221
+ * @throws {InputExceedsMaxError} when `shares` or `deadline` exceeds uint256.
224
222
  * @throws {NegativeInputError} when `referralFeePct` is negative.
225
223
  * @throws {ReferralFeePctExceededError} when `referralFeePct` is not below WAD.
226
224
  * @throws {ReferralFeeRecipientMissingError} when a positive fee has no non-zero recipient.
227
225
  * @throws {UnsupportedChainIdError} when no address registry exists for the target chain.
228
226
  * @throws {UnknownAddressError} when VaultBundlesV1 is not registered on the target chain.
229
- * @throws {viem.BaseError} from `getRequirements()` when a vault-state, allowance or nonce read fails.
227
+ * @throws {MissingContractCodeError} from `getRequirements()` when a fixed bundles or protocol
228
+ * contract has no code at its registered address.
229
+ * @throws {viem.BaseError} from `getRequirements()` when an allowance, nonce, `eth_chainId`, or
230
+ * `eth_getCode` read fails.
231
+ * @throws {UnknownTokenError} from `getRequirements()` when the share token fetched by
232
+ * `getData()` has unusable decimals.
233
+ * @throws {MalformedTokenMetadataError} when the share token's `eip712Domain()` or
234
+ * `bytes32` `name`/`symbol` retry returns non-empty data that does not decode, or when the
235
+ * deployless query reports malformed `name()`/`symbol()`/`eip712Domain()` returndata (no fallback).
230
236
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when more than one permit signature is supplied.
231
237
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when a non-permit signature is supplied.
232
238
  * @throws {BundlesPermitMismatchError} from `buildTx()` when the signature's spender, amount or
@@ -286,7 +292,8 @@ export interface VaultV2Actions {
286
292
  * @param params.adapter - Optional adapter override; defaults to the vault's sole adapter.
287
293
  * @param params.deadline - Optional shared permit/bundle deadline; defaults to two hours from now.
288
294
  * @returns Lazy prerequisite resolution and a synchronous transaction builder.
289
- * @throws {ChainIdMismatchError} when the client and entity target different chains.
295
+ * @throws {ChainIdMismatchError} when the client and entity target different chains, or from
296
+ * `getRequirements()` when the RPC reports another chain via `eth_chainId`.
290
297
  * @throws {UnsupportedBlueMarketIrmError} when an adapter-listed market with positive debt uses an unsupported IRM.
291
298
  * @throws {VaultAddressMismatchError} when `vaultData` belongs to another vault.
292
299
  * @throws {NonPositiveInputError} when `amount` or `deadline` is not positive.
@@ -296,15 +303,15 @@ export interface VaultV2Actions {
296
303
  * @throws {InputExceedsMaxError} when `deadline` exceeds `uint256`.
297
304
  * @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
298
305
  * requirement resolution.
299
- * @throws {VaultV2SingleAdapterRequiredError} when the vault does not have one adapter, including
300
- * a vault that mixes Blue and Midnight adapters.
306
+ * @throws {VaultV2SingleAdapterRequiredError} when the vault does not have one adapter.
301
307
  * @throws {AdapterNotPartOfVaultError} when `adapter` is not the vault's adapter.
302
- * @throws {VaultV2UnsupportedExitAdapterError} when the adapter is not a MorphoMarketV1AdapterV2,
303
- * including a Midnight adapter.
308
+ * @throws {VaultV2UnsupportedExitAdapterError} when the adapter is not a MorphoMarketV1AdapterV2.
304
309
  * @throws {InKindRedeemCoverageError} when the deduplicated list cannot cover the exit.
305
310
  * @throws {UnsupportedChainIdError} when no address registry exists for the target chain.
306
311
  * @throws {UnknownAddressError} when VaultExitBundlesV1 is not registered on the target chain.
307
- * @throws {viem.BaseError} from `getRequirements()` when an RPC contract read fails.
312
+ * @throws {viem.BaseError} from `getRequirements()` when the `eth_chainId` request or an
313
+ * RPC contract read fails.
314
+ * @throws {MissingContractCodeError} from `getRequirements()` when a fixed bundles or protocol contract has no code at its registered address.
308
315
  * @throws {InsufficientBlueBalanceForInKindRedeemError} from `getRequirements()` when Blue cannot fund the largest callback.
309
316
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when more than one permit signature is supplied.
310
317
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when a non-permit signature is supplied.
@@ -410,7 +417,8 @@ export interface VaultV2Actions {
410
417
  * @param params.referralFeeRecipient - Optional referral fee recipient, required when
411
418
  * `referralFeePct` is positive.
412
419
  * @returns Lazy prerequisite resolution and a synchronous transaction builder.
413
- * @throws {ChainIdMismatchError} when the client and entity target different chains.
420
+ * @throws {ChainIdMismatchError} when the client and entity target different chains, or from
421
+ * `getRequirements()` when the RPC reports another chain via `eth_chainId`.
414
422
  * @throws {VaultAddressMismatchError} when `vaultData` belongs to another vault.
415
423
  * @throws {NonPositiveInputError} when `exitAssets`, `deadline`, or a supplied
416
424
  * `minSharePriceE27` is not positive.
@@ -421,11 +429,9 @@ export interface VaultV2Actions {
421
429
  * @throws {ExpiredDeadlineError} when `deadline` is not in the future at handle creation or
422
430
  * requirement resolution.
423
431
  * @throws {ExcessiveSlippageToleranceError} when `slippageTolerance` exceeds the SDK maximum.
424
- * @throws {VaultV2SingleAdapterRequiredError} when the vault does not have exactly one adapter,
425
- * including a vault that mixes Blue and Midnight adapters.
432
+ * @throws {VaultV2SingleAdapterRequiredError} when the vault does not have exactly one adapter.
426
433
  * @throws {AdapterNotPartOfVaultError} when `adapter` is not the vault's adapter.
427
- * @throws {VaultV2UnsupportedExitAdapterError} when the adapter is not a MorphoMarketV1AdapterV2,
428
- * including a Midnight adapter.
434
+ * @throws {VaultV2UnsupportedExitAdapterError} when the adapter is not a MorphoMarketV1AdapterV2.
429
435
  * @throws {VaultV2UnsupportedLiquidityAdapterError} when the vault routes liquidity through
430
436
  * another adapter.
431
437
  * @throws {VaultV2UndecodableLiquidityDataError} when the vault's `liquidityData` does not decode
@@ -442,7 +448,9 @@ export interface VaultV2Actions {
442
448
  * @throws {ReferralFeeRecipientMissingError} when a positive `referralFeePct` has no recipient.
443
449
  * @throws {UnsupportedChainIdError} when no address registry exists for the target chain.
444
450
  * @throws {UnknownAddressError} when VaultExitBundlesV1 is not registered on the target chain.
445
- * @throws {viem.BaseError} from `getRequirements()` when an RPC contract read fails.
451
+ * @throws {viem.BaseError} from `getRequirements()` when the `eth_chainId` request or an
452
+ * RPC contract read fails.
453
+ * @throws {MissingContractCodeError} from `getRequirements()` when a fixed bundles or protocol contract has no code at its registered address.
446
454
  * @throws {AmbiguousRequirementSignaturesError} from `buildTx()` when more than one permit signature is supplied.
447
455
  * @throws {UnexpectedRequirementSignatureError} from `buildTx()` when a non-permit signature is supplied.
448
456
  * @throws {BundlesPermitMismatchError} from `buildTx()` when the permit's spender, amount, or
@@ -503,7 +511,7 @@ export interface VaultV2Actions {
503
511
  * @param params.userAddress - Account that pays the deallocation penalties and receives the redeemed assets.
504
512
  * @returns An object whose `buildTx()` returns a deep-frozen `Transaction<VaultV2ForceRedeemAction>`
505
513
  * encoding the vault `multicall`.
506
- * @throws {ChainIdMismatchError} when the connected client targets another chain or has no chain.
514
+ * @throws {ChainIdMismatchError} when the connected client declares another chain or none.
507
515
  * @throws {EmptyDeallocationsError} from `buildTx()` when `deallocations` is empty.
508
516
  * @throws {NonPositiveInputError} from `buildTx()` when `redeem.shares <= 0n` or any deallocation
509
517
  * amount is non-positive.
@@ -537,6 +545,16 @@ export declare class MorphoVaultV2 implements VaultV2Actions {
537
545
  private readonly chainId;
538
546
  constructor(client: MorphoClientType, vault: Address, chainId: number);
539
547
  getData(parameters?: FetchParameters): Promise<AccrualVaultV2>;
548
+ /**
549
+ * Asserts that every registry contract this flow's transaction targets or names as
550
+ * spender/authorized operator holds code, with one `eth_getCode` per distinct address
551
+ * before any other requirement read. The `inKindRedeem` and `forceWithdraw` paths validate the
552
+ * client chain before this code check.
553
+ *
554
+ * @param roles - Registry labels to resolve and check; `permit2` is skipped when the
555
+ * chain registers none.
556
+ */
557
+ private assertWriteTargets;
540
558
  /** {@inheritDoc VaultV2Actions.deposit} */
541
559
  deposit(params: {
542
560
  readonly userAddress: Address;
@@ -8,20 +8,17 @@ const viem_1 = require("viem");
8
8
  const actions_1 = require("viem/actions");
9
9
  const common_js_1 = require("../../actions/bundles/common.js");
10
10
  const index_js_1 = require("../../actions/index.js");
11
+ const validateClientChainId_js_1 = require("../../actions/requirements/validateClientChainId.js");
11
12
  const index_js_2 = require("../../helpers/index.js");
12
13
  const slippage_js_1 = require("../../helpers/slippage.js");
13
14
  const validate_js_1 = require("../../helpers/validate.js");
14
15
  const index_js_3 = require("../../types/index.js");
16
+ const assertContractCode_js_1 = require("../requirements/assertContractCode.js");
15
17
  const getVaultBundlesSharesRequirements_js_1 = require("../requirements/getVaultBundlesSharesRequirements.js");
16
18
  const index_js_4 = require("../requirements/index.js");
17
19
  // Maximum accepted deadline horizon; the on-chain management fee is capped at 5%/yr so the
18
20
  // accrual model stays well-defined inside it.
19
21
  const VAULT_V2_FEE_PROJECTION_HORIZON = morpho_ts_1.Time.s.from.y(1n);
20
- /**
21
- * Midnight adapters revert vault-initiated `allocate` and `deallocate` (`SelfAllocationOnly`), so a
22
- * vault routing liquidity through one accepts no deposit and serves exits from idle assets only.
23
- */
24
- const isMidnightLiquidityAdapter = (vaultData) => vaultData.accrualLiquidityAdapter?.type === "VaultV2MidnightAdapter";
25
22
  /** Binds a viem client to a Vault V2 vault's action builders. */
26
23
  class MorphoVaultV2 {
27
24
  client;
@@ -34,12 +31,33 @@ class MorphoVaultV2 {
34
31
  this.chainId = chainId;
35
32
  }
36
33
  async getData(parameters) {
37
- (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
34
+ await (0, validateClientChainId_js_1.validateClientChainId)(this.client.viemClient, this.chainId);
38
35
  return (0, blue_sdk_viem_1.fetchAccrualVaultV2)(this.vault, this.client.viemClient, {
39
36
  ...parameters,
40
37
  deployless: this.client.options.supportDeployless,
41
38
  });
42
39
  }
40
+ /**
41
+ * Asserts that every registry contract this flow's transaction targets or names as
42
+ * spender/authorized operator holds code, with one `eth_getCode` per distinct address
43
+ * before any other requirement read. The `inKindRedeem` and `forceWithdraw` paths validate the
44
+ * client chain before this code check.
45
+ *
46
+ * @param roles - Registry labels to resolve and check; `permit2` is skipped when the
47
+ * chain registers none.
48
+ */
49
+ async assertWriteTargets(roles) {
50
+ const targets = [];
51
+ for (const role of roles) {
52
+ const address = role === "permit2"
53
+ ? (0, morpho_ts_1.getChainAddresses)(this.chainId).permit2
54
+ : (0, morpho_ts_1.getChainAddress)(this.chainId, role);
55
+ if (address == null)
56
+ continue;
57
+ targets.push({ role, address });
58
+ }
59
+ await (0, assertContractCode_js_1.assertRegistryTargetsHaveCode)(this.client.viemClient, this.chainId, targets);
60
+ }
43
61
  /** {@inheritDoc VaultV2Actions.deposit} */
44
62
  deposit(params) {
45
63
  const { userAddress, vaultData } = params;
@@ -64,13 +82,6 @@ class MorphoVaultV2 {
64
82
  // The native path must target the chain's registered wrapped-native asset.
65
83
  (0, validate_js_1.validateNativeVaultAsset)(this.chainId, vaultAsset);
66
84
  }
67
- if (isMidnightLiquidityAdapter(vaultData)) {
68
- throw new index_js_3.InputExceedsMaxError({
69
- field: funding.value > 0n ? "nativeAmount" : "amount",
70
- value: funding.assets,
71
- max: 0n,
72
- });
73
- }
74
85
  const referralFeeAssets = (0, common_js_1.getBundlesReferralFeeAssets)(funding.assets, common.referralFeePct);
75
86
  const maxSharePrice = (0, index_js_2.computeVaultMaxSharePrice)({
76
87
  vaultData,
@@ -89,20 +100,26 @@ class MorphoVaultV2 {
89
100
  return await pendingRequirements;
90
101
  // Memoize the in-flight promise so concurrent callers share one round of
91
102
  // allowance/nonce reads.
92
- const pending = funding.value > 0n
93
- ? Promise.resolve([])
94
- : (0, index_js_4.getBundlesTokenRequirements)(this.client.viemClient, {
95
- token: vaultAsset,
96
- spender,
97
- amount: funding.assets,
98
- owner: userAddress,
99
- chainId: this.chainId,
100
- deadline,
101
- supportSignature: this.client.options.supportSignature,
102
- supportDeployless: this.client.options.supportDeployless,
103
- useSimplePermit: requirementOptions?.useSimplePermit,
104
- permit2Nonce: requirementOptions?.permit2Nonce,
105
- });
103
+ const pending = (async () => {
104
+ await this.assertWriteTargets(funding.value === 0n && this.client.options.supportSignature
105
+ ? ["bundles.vaultBundlesV1", "permit2"]
106
+ : ["bundles.vaultBundlesV1"]);
107
+ return funding.value > 0n
108
+ ? []
109
+ : (0, index_js_4.getBundlesTokenRequirements)(this.client.viemClient, {
110
+ token: vaultAsset,
111
+ spender,
112
+ amount: funding.assets,
113
+ owner: userAddress,
114
+ chainId: this.chainId,
115
+ deadline,
116
+ supportSignature: this.client.options.supportSignature,
117
+ supportDeployless: this.client.options.supportDeployless,
118
+ useSimplePermit: requirementOptions?.useSimplePermit,
119
+ permit2Nonce: requirementOptions?.permit2Nonce,
120
+ now,
121
+ });
122
+ })();
106
123
  pendingRequirements = pending;
107
124
  try {
108
125
  return await pending;
@@ -148,14 +165,6 @@ class MorphoVaultV2 {
148
165
  throw new index_js_3.NonPositiveInputError("amount", amount);
149
166
  // Reject values outside the ABI range before resolving any requirements.
150
167
  (0, validate_js_1.validateUint256Field)("amount", amount);
151
- if (isMidnightLiquidityAdapter(vaultData) &&
152
- amount > vaultData.assetBalance) {
153
- throw new index_js_3.InputExceedsMaxError({
154
- field: "amount",
155
- value: amount,
156
- max: vaultData.assetBalance,
157
- });
158
- }
159
168
  const createdAt = morpho_ts_1.Time.timestamp();
160
169
  const deadline = params.deadline ?? createdAt + morpho_ts_1.Time.s.from.h(2n);
161
170
  if (deadline <= createdAt)
@@ -184,6 +193,7 @@ class MorphoVaultV2 {
184
193
  const now = morpho_ts_1.Time.timestamp();
185
194
  if (deadline <= now)
186
195
  throw new index_js_3.ExpiredDeadlineError(deadline, now);
196
+ await this.assertWriteTargets(["bundles.vaultBundlesV1"]);
187
197
  // Re-read the live share allowance on every call instead of caching the resolved
188
198
  // requirements: the allowance is the sole cap on the burn, so a caller that executed the
189
199
  // returned approval must see it satisfied on the next call, and an allowance revoked or
@@ -198,6 +208,7 @@ class MorphoVaultV2 {
198
208
  maxShareAllowance,
199
209
  deadline,
200
210
  supportSignature: this.client.options.supportSignature,
211
+ now,
201
212
  });
202
213
  },
203
214
  buildTx: (signatures) => {
@@ -243,26 +254,16 @@ class MorphoVaultV2 {
243
254
  const now = morpho_ts_1.Time.timestamp();
244
255
  if (deadline <= now)
245
256
  throw new index_js_3.ExpiredDeadlineError(deadline, now);
246
- const vaultData = await this.getData();
247
- if (isMidnightLiquidityAdapter(vaultData)) {
248
- // Onchain redeem accrues interest before converting shares to assets.
249
- const { vault: accrued } = vaultData.accrueInterest(now);
250
- if (accrued.toAssets(shares) > accrued.assetBalance) {
251
- throw new index_js_3.InputExceedsMaxError({
252
- field: "shares",
253
- value: shares,
254
- max: accrued.toShares(accrued.assetBalance),
255
- });
256
- }
257
- }
257
+ await this.assertWriteTargets(["bundles.vaultBundlesV1"]);
258
258
  return (0, getVaultBundlesSharesRequirements_js_1.getVaultBundlesSharesRequirements)(this.client.viemClient, {
259
- vaultData,
259
+ vaultData: await this.getData(),
260
260
  version: "vaultV2",
261
261
  owner: userAddress,
262
262
  chainId: this.chainId,
263
263
  requiredShareAllowance: shares,
264
264
  deadline,
265
265
  supportSignature: this.client.options.supportSignature,
266
+ now,
266
267
  });
267
268
  },
268
269
  buildTx: (signatures) => {
@@ -388,6 +389,8 @@ class MorphoVaultV2 {
388
389
  if (deadline <= requirementsTimestamp) {
389
390
  throw new index_js_3.ExpiredDeadlineError(deadline, requirementsTimestamp);
390
391
  }
392
+ await (0, validateClientChainId_js_1.validateClientChainId)(this.client.viemClient, this.chainId);
393
+ await this.assertWriteTargets(["bundles.vaultExitBundlesV1", "blue"]);
391
394
  // Vault gates are intentionally left to the final transaction. A receive gate may inspect
392
395
  // VaultExitBundlesV1's transient `initiator`, which is populated only during the actual
393
396
  // periphery call. A send-share gate is arbitrary external code and is evaluated repeatedly
@@ -650,6 +653,8 @@ class MorphoVaultV2 {
650
653
  if (deadline <= requirementsTimestamp) {
651
654
  throw new index_js_3.ExpiredDeadlineError(deadline, requirementsTimestamp);
652
655
  }
656
+ await (0, validateClientChainId_js_1.validateClientChainId)(this.client.viemClient, this.chainId);
657
+ await this.assertWriteTargets(["bundles.vaultExitBundlesV1"]);
653
658
  // Vault gates are intentionally left to the final transaction: the receive-assets gate may
654
659
  // inspect VaultExitBundlesV1's transient `initiator`, and a send-shares gate is arbitrary
655
660
  // external code re-evaluated after each intermediate penalty burn, so neither is
@@ -39,9 +39,8 @@ export interface InputVaultV2BlueReallocationData {
39
39
  /**
40
40
  * Immutable-by-convention state container for Vault V2 BluePublicAllocator simulations.
41
41
  *
42
- * Constructor inputs are cloned, except Midnight adapter snapshots: those are
43
- * shared by reference and must be treated as read-only. Every simulated
44
- * reallocation returns a new instance. Planning keeps the transaction-scoped `firstTotalAssets`
42
+ * Constructor inputs are cloned. Every simulated reallocation returns a new
43
+ * instance. Planning keeps the transaction-scoped `firstTotalAssets`
45
44
  * denominator outside the durable snapshot while applying reallocations in
46
45
  * contract order. Penalty donations update simulated vault accounting but are
47
46
  * excluded from same-plan idle liquidity so round-up dust cannot create
@@ -101,8 +100,7 @@ export declare class VaultV2BlueReallocationData implements InputVaultV2BlueReal
101
100
  /**
102
101
  * Clones the complete simulation snapshot.
103
102
  *
104
- * @returns A deep clone of this simulation state. Midnight adapter snapshots are
105
- * shared by reference and must be treated as read-only.
103
+ * @returns A deep clone of this simulation state.
106
104
  * @example
107
105
  * ```ts
108
106
  * import { markets } from "@morpho-org/morpho-test";
@@ -78,10 +78,6 @@ const cloneAccrualVault = (vault, markets) => new blue_sdk_1.AccrualVault({
78
78
  position: clonePosition(position, markets),
79
79
  })));
80
80
  const cloneAdapter = (adapter, markets) => {
81
- // BluePublicAllocator never moves Midnight liquidity, and reallocation never mutates a Midnight
82
- // adapter, so its read-only snapshot is shared as-is.
83
- if (adapter.type === "VaultV2MidnightAdapter")
84
- return adapter;
85
81
  const base = {
86
82
  address: adapter.address,
87
83
  parentVault: adapter.parentVault,
@@ -129,9 +125,8 @@ const cloneVault = (vault, { markets, adapterKeysToClone, }) => {
129
125
  /**
130
126
  * Immutable-by-convention state container for Vault V2 BluePublicAllocator simulations.
131
127
  *
132
- * Constructor inputs are cloned, except Midnight adapter snapshots: those are
133
- * shared by reference and must be treated as read-only. Every simulated
134
- * reallocation returns a new instance. Planning keeps the transaction-scoped `firstTotalAssets`
128
+ * Constructor inputs are cloned. Every simulated reallocation returns a new
129
+ * instance. Planning keeps the transaction-scoped `firstTotalAssets`
135
130
  * denominator outside the durable snapshot while applying reallocations in
136
131
  * contract order. Penalty donations update simulated vault accounting but are
137
132
  * excluded from same-plan idle liquidity so round-up dust cannot create
@@ -253,8 +248,7 @@ class VaultV2BlueReallocationData {
253
248
  /**
254
249
  * Clones the complete simulation snapshot.
255
250
  *
256
- * @returns A deep clone of this simulation state. Midnight adapter snapshots are
257
- * shared by reference and must be treated as read-only.
251
+ * @returns A deep clone of this simulation state.
258
252
  * @example
259
253
  * ```ts
260
254
  * import { markets } from "@morpho-org/morpho-test";
@@ -1,7 +1,7 @@
1
- export { BlueErrors, IncompleteChainRegistryError, InvalidMarketParamsError as InvalidBlueMarketParamsError, InvalidVaultV2MidnightAdapterMarketsError as InvalidBlueVaultV2MidnightAdapterMarketsError, UnknownAddressError, UnknownDataError as UnknownBlueDataError, UnknownFactory as UnknownBlueFactory, UnknownMarketAllocationError as UnknownBlueMarketAllocationError, UnknownMarketParamsError as UnknownBlueMarketParamsError, UnknownOfFactory as UnknownBlueOfFactory, UnknownTokenError as UnknownBlueTokenError, UnknownTokenPriceError as UnknownBlueTokenPriceError, UnknownVaultConfigError as UnknownBlueVaultConfigError, UnsupportedChainIdError, UnsupportedMarketIrmError as UnsupportedBlueMarketIrmError,
1
+ export { BlueErrors, IncompleteChainRegistryError, InvalidMarketParamsError as InvalidBlueMarketParamsError, UnknownAddressError, UnknownDataError as UnknownBlueDataError, UnknownFactory as UnknownBlueFactory, UnknownMarketAllocationError as UnknownBlueMarketAllocationError, UnknownMarketParamsError as UnknownBlueMarketParamsError, UnknownOfFactory as UnknownBlueOfFactory, UnknownTokenError as UnknownBlueTokenError, UnknownTokenPriceError as UnknownBlueTokenPriceError, UnknownVaultConfigError as UnknownBlueVaultConfigError, UnsupportedChainIdError, UnsupportedMarketIrmError as UnsupportedBlueMarketIrmError,
2
2
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
3
3
  UnsupportedPreLiquidationParamsError as UnsupportedBluePreLiquidationParamsError, UnsupportedVaultV2AdapterError as UnsupportedBlueVaultV2AdapterError, VaultV2Errors, } from "@morpho-org/blue-sdk";
4
- export { getUnsupportedVaultV2Adapter as getBlueUnsupportedVaultV2Adapter, InvalidNumberError, InvalidPermitDomainChainIdError, InvalidPermitDomainVerifyingContractError, isUnknownOfFactoryError as isBlueUnknownOfFactoryError, MarketParamsIdMismatchError as BlueMarketParamsIdMismatchError, NonTupleReturnValueError, UnknownAbiFunctionError, UnnamedAbiOutputsError, UnresolvedBlockNumberError as BlueUnresolvedBlockNumberError, UnsupportedPermitDomainExtensionsError, } from "@morpho-org/blue-sdk-viem";
5
- export { ChainIdMismatchError as MidnightChainIdMismatchError, InvalidEcrecoverSignatureVError as InvalidMidnightEcrecoverSignatureVError, InvalidMarketParameterError as InvalidMidnightMarketParameterError, InvalidMidnightApiQuoteTargetError, InvalidMidnightApiResponseError, InvalidOfferGroupError as InvalidMidnightOfferGroupError, InvalidOfferParameterError as InvalidMidnightOfferParameterError, InvalidRateRatifierV1RateError as InvalidMidnightRateRatifierV1RateError, InvalidRateRatifierV1TickError as InvalidMidnightRateRatifierV1TickError, InvalidRateRatifierV1TimeError as InvalidMidnightRateRatifierV1TimeError, InvalidRatifierV1AddressError as InvalidMidnightRatifierV1AddressError, InvalidTickSpacingError as InvalidMidnightTickSpacingError, InvalidTreeError as InvalidMidnightTreeError, InvalidTreeHeightError as InvalidMidnightTreeHeightError, InvalidTypedDataSignatureError as InvalidMidnightTypedDataSignatureError, MidnightApiError, MidnightMempoolValidationError, PayloadDecodeError as MidnightPayloadDecodeError, PriceGreaterThanOneError as MidnightPriceGreaterThanOneError, RateRatifierV1BoundOverflowError as MidnightRateRatifierV1BoundOverflowError, RatifierV1TakerNotAllowedError as MidnightRatifierV1TakerNotAllowedError, SettlementFeeExceedsPriceError as MidnightSettlementFeeExceedsPriceError, TickOutOfRangeError as MidnightTickOutOfRangeError, UnknownCollateralIndexError as UnknownMidnightCollateralIndexError, } from "@morpho-org/midnight-sdk";
4
+ export { getUnsupportedVaultV2Adapter as getBlueUnsupportedVaultV2Adapter, InvalidNumberError, InvalidPermitDomainChainIdError, InvalidPermitDomainVerifyingContractError, isUnknownOfFactoryError as isBlueUnknownOfFactoryError, MalformedTokenMetadataError, MarketParamsIdMismatchError as BlueMarketParamsIdMismatchError, MissingPermitDomainNameError, NonTupleReturnValueError, UnknownAbiFunctionError, UnnamedAbiOutputsError, UnsupportedPermitDomainExtensionsError, } from "@morpho-org/blue-sdk-viem";
5
+ export { ChainIdMismatchError as MidnightChainIdMismatchError, InvalidEcrecoverSignatureVError as InvalidMidnightEcrecoverSignatureVError, InvalidMarketParameterError as InvalidMidnightMarketParameterError, InvalidMidnightApiQuoteTargetError, InvalidMidnightApiResponseError, InvalidOfferGroupError as InvalidMidnightOfferGroupError, InvalidOfferParameterError as InvalidMidnightOfferParameterError, InvalidPositionAccrualStateError as InvalidMidnightPositionAccrualStateError, InvalidPositionAccrualTimestampError as InvalidMidnightPositionAccrualTimestampError, InvalidPositionLossFactorError as InvalidMidnightPositionLossFactorError, InvalidRateRatifierV1RateError as InvalidMidnightRateRatifierV1RateError, InvalidRateRatifierV1TickError as InvalidMidnightRateRatifierV1TickError, InvalidRateRatifierV1TimeError as InvalidMidnightRateRatifierV1TimeError, InvalidRatifierV1AddressError as InvalidMidnightRatifierV1AddressError, InvalidTickSpacingError as InvalidMidnightTickSpacingError, InvalidTreeError as InvalidMidnightTreeError, InvalidTreeHeightError as InvalidMidnightTreeHeightError, InvalidTypedDataSignatureError as InvalidMidnightTypedDataSignatureError, MidnightApiError, MidnightMempoolValidationError, PayloadDecodeError as MidnightPayloadDecodeError, PriceGreaterThanOneError as MidnightPriceGreaterThanOneError, RateRatifierV1BoundOverflowError as MidnightRateRatifierV1BoundOverflowError, RatifierV1TakerNotAllowedError as MidnightRatifierV1TakerNotAllowedError, SettlementFeeExceedsPriceError as MidnightSettlementFeeExceedsPriceError, TickOutOfRangeError as MidnightTickOutOfRangeError, UnknownCollateralIndexError as UnknownMidnightCollateralIndexError, } from "@morpho-org/midnight-sdk";
6
6
  export type { ErrorClass } from "@morpho-org/morpho-ts";
7
- export { _try, DivisionByZeroError, InvalidAddressError, InvalidBitLengthError, InvalidPositionAccrualStateError, InvalidPositionAccrualStateError as InvalidMidnightPositionAccrualStateError, InvalidPositionAccrualTimestampError, InvalidPositionAccrualTimestampError as InvalidMidnightPositionAccrualTimestampError, InvalidPositionLossFactorError, InvalidPositionLossFactorError as InvalidMidnightPositionLossFactorError, NegativeValueError, RegistryValueAlreadyRegisteredError, } from "@morpho-org/morpho-ts";
7
+ export { _try, DivisionByZeroError, InvalidAddressError, InvalidBitLengthError, NegativeValueError, RegistryValueAlreadyRegisteredError, } from "@morpho-org/morpho-ts";