@morpho-org/morpho-sdk 7.0.0 → 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 (192) hide show
  1. package/MIGRATION-v6-to-v7.md +6 -0
  2. package/MIGRATION-v7-to-v8.md +290 -0
  3. package/README.md +11 -6
  4. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +2 -0
  5. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +2 -0
  6. package/lib/cjs/actions/midnight/cancelAndMake.d.ts +17 -71
  7. package/lib/cjs/actions/midnight/cancelAndMake.js +41 -29
  8. package/lib/cjs/actions/midnight/redeem.d.ts +8 -1
  9. package/lib/cjs/actions/midnight/redeem.js +18 -1
  10. package/lib/cjs/actions/midnight/repayWithdrawCollateral.d.ts +19 -3
  11. package/lib/cjs/actions/midnight/repayWithdrawCollateral.js +24 -2
  12. package/lib/cjs/actions/midnight/supplyCollateral.d.ts +1 -0
  13. package/lib/cjs/actions/midnight/supplyCollateral.js +3 -0
  14. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +21 -6
  15. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +49 -14
  16. package/lib/cjs/actions/midnight/takeBorrow.d.ts +18 -5
  17. package/lib/cjs/actions/midnight/takeBorrow.js +44 -12
  18. package/lib/cjs/actions/midnight/takeLend.d.ts +10 -5
  19. package/lib/cjs/actions/midnight/takeLend.js +27 -9
  20. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.d.ts +22 -7
  21. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +51 -27
  22. package/lib/cjs/actions/midnight/takeWithdraw.d.ts +14 -5
  23. package/lib/cjs/actions/midnight/takeWithdraw.js +40 -9
  24. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +6 -2
  25. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +11 -6
  26. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +3 -0
  27. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +2 -1
  28. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +13 -2
  29. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +17 -6
  30. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +3 -0
  31. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +2 -1
  32. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +5 -0
  33. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +4 -1
  34. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +3 -1
  35. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.js +5 -1
  36. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +3 -1
  37. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +5 -1
  38. package/lib/cjs/actions/requirements/validateClientChainId.d.ts +26 -0
  39. package/lib/cjs/actions/requirements/validateClientChainId.js +34 -0
  40. package/lib/cjs/blue/errors.d.ts +1 -1
  41. package/lib/cjs/blue/errors.js +3 -1
  42. package/lib/cjs/blue/types.d.ts +1 -1
  43. package/lib/cjs/blue/utils.d.ts +1 -1
  44. package/lib/cjs/blue/utils.js +5 -1
  45. package/lib/cjs/entities/blue/blue.d.ts +109 -27
  46. package/lib/cjs/entities/blue/blue.js +103 -28
  47. package/lib/cjs/entities/index.d.ts +1 -1
  48. package/lib/cjs/entities/midnight/deadline.d.ts +15 -0
  49. package/lib/cjs/entities/midnight/deadline.js +24 -0
  50. package/lib/cjs/entities/midnight/midnight.d.ts +432 -74
  51. package/lib/cjs/entities/midnight/midnight.js +869 -188
  52. package/lib/cjs/entities/midnight/publication.d.ts +134 -0
  53. package/lib/cjs/entities/midnight/publication.js +404 -0
  54. package/lib/cjs/entities/midnight/types.d.ts +26 -5
  55. package/lib/cjs/entities/requirements/assertContractCode.d.ts +49 -0
  56. package/lib/cjs/entities/requirements/assertContractCode.js +79 -0
  57. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +19 -4
  58. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +45 -18
  59. package/lib/cjs/entities/requirements/getUnusedPermit2Nonce.d.ts +3 -2
  60. package/lib/cjs/entities/requirements/getUnusedPermit2Nonce.js +5 -2
  61. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -1
  62. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +8 -2
  63. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +63 -11
  64. package/lib/cjs/entities/vaultV1/vaultV1.js +83 -49
  65. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +54 -12
  66. package/lib/cjs/entities/vaultV2/vaultV2.js +86 -55
  67. package/lib/cjs/errors.d.ts +1 -1
  68. package/lib/cjs/errors.js +4 -2
  69. package/lib/cjs/helpers/constant.d.ts +5 -0
  70. package/lib/cjs/helpers/constant.js +6 -1
  71. package/lib/cjs/helpers/index.d.ts +1 -1
  72. package/lib/cjs/helpers/index.js +2 -1
  73. package/lib/cjs/helpers/validate.d.ts +163 -8
  74. package/lib/cjs/helpers/validate.js +179 -8
  75. package/lib/cjs/helpers/validateMidnightBounds.d.ts +47 -0
  76. package/lib/cjs/helpers/validateMidnightBounds.js +79 -0
  77. package/lib/cjs/helpers/validateMidnightPublication.d.ts +12 -0
  78. package/lib/cjs/helpers/validateMidnightPublication.js +144 -0
  79. package/lib/cjs/helpers/validateMidnightReceiver.d.ts +29 -0
  80. package/lib/cjs/helpers/validateMidnightReceiver.js +84 -0
  81. package/lib/cjs/helpers/validateMidnightTakeFeasibility.d.ts +102 -0
  82. package/lib/cjs/helpers/validateMidnightTakeFeasibility.js +231 -0
  83. package/lib/cjs/index.d.ts +1 -0
  84. package/lib/cjs/index.js +3 -0
  85. package/lib/cjs/types/action.d.ts +102 -1
  86. package/lib/cjs/types/action.js +102 -1
  87. package/lib/cjs/types/error.d.ts +197 -4
  88. package/lib/cjs/types/error.js +265 -5
  89. package/lib/cjs/types/index.d.ts +1 -0
  90. package/lib/cjs/types/index.js +1 -0
  91. package/lib/cjs/types/midnightCancelAndMake.d.ts +70 -0
  92. package/lib/cjs/types/midnightCancelAndMake.js +2 -0
  93. package/lib/cjs/types/midnightPublicationError.d.ts +332 -0
  94. package/lib/cjs/types/midnightPublicationError.js +376 -0
  95. package/lib/cjs/types.d.ts +1 -1
  96. package/lib/cjs/utils.d.ts +2 -1
  97. package/lib/cjs/utils.js +8 -2
  98. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +2 -0
  99. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +2 -0
  100. package/lib/esm/actions/midnight/cancelAndMake.d.ts +17 -71
  101. package/lib/esm/actions/midnight/cancelAndMake.js +42 -30
  102. package/lib/esm/actions/midnight/redeem.d.ts +8 -1
  103. package/lib/esm/actions/midnight/redeem.js +18 -1
  104. package/lib/esm/actions/midnight/repayWithdrawCollateral.d.ts +19 -3
  105. package/lib/esm/actions/midnight/repayWithdrawCollateral.js +24 -2
  106. package/lib/esm/actions/midnight/supplyCollateral.d.ts +1 -0
  107. package/lib/esm/actions/midnight/supplyCollateral.js +3 -0
  108. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +21 -6
  109. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +52 -17
  110. package/lib/esm/actions/midnight/takeBorrow.d.ts +18 -5
  111. package/lib/esm/actions/midnight/takeBorrow.js +47 -15
  112. package/lib/esm/actions/midnight/takeLend.d.ts +10 -5
  113. package/lib/esm/actions/midnight/takeLend.js +27 -9
  114. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.d.ts +22 -7
  115. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +52 -28
  116. package/lib/esm/actions/midnight/takeWithdraw.d.ts +14 -5
  117. package/lib/esm/actions/midnight/takeWithdraw.js +41 -10
  118. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +6 -2
  119. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +13 -8
  120. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +3 -0
  121. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +2 -1
  122. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +13 -2
  123. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +17 -6
  124. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +3 -0
  125. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +2 -1
  126. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +5 -0
  127. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +4 -1
  128. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +3 -1
  129. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.js +5 -1
  130. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +3 -1
  131. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +5 -1
  132. package/lib/esm/actions/requirements/validateClientChainId.d.ts +26 -0
  133. package/lib/esm/actions/requirements/validateClientChainId.js +30 -0
  134. package/lib/esm/blue/errors.d.ts +1 -1
  135. package/lib/esm/blue/errors.js +1 -1
  136. package/lib/esm/blue/types.d.ts +1 -1
  137. package/lib/esm/blue/utils.d.ts +1 -1
  138. package/lib/esm/blue/utils.js +1 -1
  139. package/lib/esm/entities/blue/blue.d.ts +109 -27
  140. package/lib/esm/entities/blue/blue.js +104 -29
  141. package/lib/esm/entities/index.d.ts +1 -1
  142. package/lib/esm/entities/midnight/deadline.d.ts +15 -0
  143. package/lib/esm/entities/midnight/deadline.js +20 -0
  144. package/lib/esm/entities/midnight/midnight.d.ts +432 -74
  145. package/lib/esm/entities/midnight/midnight.js +870 -189
  146. package/lib/esm/entities/midnight/publication.d.ts +134 -0
  147. package/lib/esm/entities/midnight/publication.js +397 -0
  148. package/lib/esm/entities/midnight/types.d.ts +26 -5
  149. package/lib/esm/entities/requirements/assertContractCode.d.ts +49 -0
  150. package/lib/esm/entities/requirements/assertContractCode.js +74 -0
  151. package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +19 -4
  152. package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +47 -20
  153. package/lib/esm/entities/requirements/getUnusedPermit2Nonce.d.ts +3 -2
  154. package/lib/esm/entities/requirements/getUnusedPermit2Nonce.js +5 -2
  155. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -1
  156. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +8 -2
  157. package/lib/esm/entities/vaultV1/vaultV1.d.ts +63 -11
  158. package/lib/esm/entities/vaultV1/vaultV1.js +85 -51
  159. package/lib/esm/entities/vaultV2/vaultV2.d.ts +54 -12
  160. package/lib/esm/entities/vaultV2/vaultV2.js +88 -57
  161. package/lib/esm/errors.d.ts +1 -1
  162. package/lib/esm/errors.js +1 -1
  163. package/lib/esm/helpers/constant.d.ts +5 -0
  164. package/lib/esm/helpers/constant.js +5 -0
  165. package/lib/esm/helpers/index.d.ts +1 -1
  166. package/lib/esm/helpers/index.js +1 -1
  167. package/lib/esm/helpers/validate.d.ts +163 -8
  168. package/lib/esm/helpers/validate.js +177 -7
  169. package/lib/esm/helpers/validateMidnightBounds.d.ts +47 -0
  170. package/lib/esm/helpers/validateMidnightBounds.js +72 -0
  171. package/lib/esm/helpers/validateMidnightPublication.d.ts +12 -0
  172. package/lib/esm/helpers/validateMidnightPublication.js +139 -0
  173. package/lib/esm/helpers/validateMidnightReceiver.d.ts +29 -0
  174. package/lib/esm/helpers/validateMidnightReceiver.js +80 -0
  175. package/lib/esm/helpers/validateMidnightTakeFeasibility.d.ts +102 -0
  176. package/lib/esm/helpers/validateMidnightTakeFeasibility.js +226 -0
  177. package/lib/esm/index.d.ts +1 -0
  178. package/lib/esm/index.js +1 -0
  179. package/lib/esm/types/action.d.ts +102 -1
  180. package/lib/esm/types/action.js +102 -1
  181. package/lib/esm/types/error.d.ts +197 -4
  182. package/lib/esm/types/error.js +253 -2
  183. package/lib/esm/types/index.d.ts +1 -0
  184. package/lib/esm/types/index.js +1 -0
  185. package/lib/esm/types/midnightCancelAndMake.d.ts +70 -0
  186. package/lib/esm/types/midnightCancelAndMake.js +1 -0
  187. package/lib/esm/types/midnightPublicationError.d.ts +332 -0
  188. package/lib/esm/types/midnightPublicationError.js +359 -0
  189. package/lib/esm/types.d.ts +1 -1
  190. package/lib/esm/utils.d.ts +2 -1
  191. package/lib/esm/utils.js +2 -1
  192. package/package.json +10 -10
@@ -31,7 +31,8 @@ export interface BlueActions {
31
31
  *
32
32
  * @param parameters - Optional fetch parameters (block number, state overrides).
33
33
  * @returns Market state including total supply/borrow assets and shares.
34
- * @throws {ChainIdMismatchError} when the connected client targets another chain or has no chain.
34
+ * @throws {ChainIdMismatchError} when the connected client declares another chain or none, or its RPC reports another chain via `eth_chainId`.
35
+ * @throws {viem.BaseError} when the `eth_chainId` request or a state read fails.
35
36
  * @example
36
37
  * ```ts
37
38
  * import { markets } from "@morpho-org/morpho-test";
@@ -52,7 +53,8 @@ export interface BlueActions {
52
53
  * @param userAddress - The user whose position to fetch.
53
54
  * @param parameters - Optional fetch parameters (block number, state overrides).
54
55
  * @returns Accrual position with health metrics (maxBorrowAssets, ltv, isHealthy).
55
- * @throws {ChainIdMismatchError} when the connected client targets another chain or has no chain.
56
+ * @throws {ChainIdMismatchError} when the connected client declares another chain or none, or its RPC reports another chain via `eth_chainId`.
57
+ * @throws {viem.BaseError} when the `eth_chainId` request or a state read fails.
56
58
  * @example
57
59
  * ```ts
58
60
  * import { markets } from "@morpho-org/morpho-test";
@@ -81,7 +83,8 @@ export interface BlueActions {
81
83
  * @param params.referralFeePct - Optional WAD-scaled referral fee below 100%.
82
84
  * @param params.referralFeeRecipient - Recipient required for a positive fee.
83
85
  * @returns Lazy token prerequisite resolution and a synchronous deep-frozen transaction.
84
- * @throws {ChainIdMismatchError} when the client targets another chain.
86
+ * @throws {ChainIdMismatchError} when the client targets another chain, or from
87
+ * `getRequirements()` when the RPC reports another chain via `eth_chainId`.
85
88
  * @throws {ExpiredDeadlineError} when the deadline is stale.
86
89
  * @throws {NonPositiveInputError} when `collateralAssets` is not positive.
87
90
  * @throws {NegativeInputError} when collateral, native funding, or the referral fee is negative.
@@ -101,7 +104,14 @@ export interface BlueActions {
101
104
  * @throws {BundlesRequirementSignatureMismatchError} from `buildTx()` when a signature cannot be encoded safely.
102
105
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
103
106
  * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
104
- * @throws {viem.BaseError} from `getRequirements()` when an allowance, nonce, or token metadata read fails.
107
+ * @throws {UnknownTokenError} from `getRequirements()` when the funded token's `decimals()` is unusable.
108
+ * @throws {MalformedTokenMetadataError} when the token's `eip712Domain()` or
109
+ * `bytes32` `name`/`symbol` retry returns non-empty data that does not decode, or when the
110
+ * deployless query reports malformed `name()`/`symbol()`/`eip712Domain()` returndata (no fallback).
111
+ * @throws {MissingContractCodeError} from `getRequirements()` when a fixed bundles or protocol
112
+ * contract has no code at its registered address.
113
+ * @throws {viem.BaseError} from `getRequirements()` when an allowance, nonce, or token metadata
114
+ * read or `eth_chainId` request fails.
105
115
  * @example
106
116
  * ```ts
107
117
  * import { markets } from "@morpho-org/morpho-test";
@@ -132,9 +142,11 @@ export interface BlueActions {
132
142
  /**
133
143
  * Prepares a direct BlueBundlesV1 loan-asset supply.
134
144
  *
135
- * `getRequirements()` reads only the loan-token allowance and selected ERC-2612 or Permit2
136
- * nonce state. Direct approvals and ERC-2612 name BlueBundlesV1 as spender; Permit2 keeps its
137
- * ERC-20 approval on canonical Permit2. Native funding is exclusive and skips token requirements.
145
+ * `getRequirements()` first checks that BlueBundlesV1, Blue and, for ERC-20 funding with
146
+ * signatures enabled, Permit2 hold code at their registered addresses, then reads the loan-token
147
+ * allowance and selected ERC-2612 or Permit2 nonce state. Direct approvals and ERC-2612 name
148
+ * BlueBundlesV1 as spender; Permit2 keeps its ERC-20 approval on canonical Permit2. Native
149
+ * funding is exclusive and skips token requirements (the code check still runs).
138
150
  * The referral fee is deducted from the gross `assets` supplied. This route exposes no
139
151
  * share-price bound or `slippageTolerance`.
140
152
  *
@@ -145,7 +157,8 @@ export interface BlueActions {
145
157
  * @param params.referralFeePct - Optional WAD-scaled referral fee below 100%.
146
158
  * @param params.referralFeeRecipient - Recipient required for a positive fee.
147
159
  * @returns Lazy prerequisite resolution and a synchronous deep-frozen transaction builder.
148
- * @throws {ChainIdMismatchError} when the client targets another chain.
160
+ * @throws {ChainIdMismatchError} when the client targets another chain, or from
161
+ * `getRequirements()` when the RPC reports another chain via `eth_chainId`.
149
162
  * @throws {ExpiredDeadlineError} when the deadline is not in the future at creation or requirement resolution.
150
163
  * @throws {NonPositiveInputError} when `assets` is not positive.
151
164
  * @throws {NegativeInputError} when native funding or the referral fee is negative, or from `getRequirements()` when an explicit `permit2Nonce` is negative.
@@ -166,7 +179,14 @@ export interface BlueActions {
166
179
  * @throws {BundlesRequirementSignatureMismatchError} from `buildTx()` when a signature cannot be encoded safely.
167
180
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
168
181
  * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
169
- * @throws {viem.BaseError} from `getRequirements()` when a required allowance, nonce, or token metadata read fails.
182
+ * @throws {UnknownTokenError} from `getRequirements()` when the funded token's `decimals()` is unusable.
183
+ * @throws {MalformedTokenMetadataError} when the token's `eip712Domain()` or
184
+ * `bytes32` `name`/`symbol` retry returns non-empty data that does not decode, or when the
185
+ * deployless query reports malformed `name()`/`symbol()`/`eip712Domain()` returndata (no fallback).
186
+ * @throws {MissingContractCodeError} from `getRequirements()` when a fixed bundles or protocol
187
+ * contract has no code at its registered address.
188
+ * @throws {viem.BaseError} from `getRequirements()` when a required allowance, nonce, or token
189
+ * metadata read or `eth_chainId` request fails.
170
190
  * @example
171
191
  * ```ts
172
192
  * import { markets } from "@morpho-org/morpho-test";
@@ -213,7 +233,8 @@ export interface BlueActions {
213
233
  * @param params.referralFeePct - Optional WAD-scaled referral fee below 100%.
214
234
  * @param params.referralFeeRecipient - Recipient required for a positive fee.
215
235
  * @returns Lazy authorization resolution and a synchronous deep-frozen transaction builder.
216
- * @throws {ChainIdMismatchError} when the client targets another chain.
236
+ * @throws {ChainIdMismatchError} when the client targets another chain, or from
237
+ * `getRequirements()` when the RPC reports another chain via `eth_chainId`.
217
238
  * @throws {ExpiredDeadlineError} when the deadline is stale.
218
239
  * @throws {MissingAccrualPositionError} when no position snapshot is provided at runtime.
219
240
  * @throws {MarketIdMismatchError} when `positionData` belongs to another market.
@@ -237,7 +258,10 @@ export interface BlueActions {
237
258
  * @throws {BundlesRequirementSignatureMismatchError} from `buildTx()` when authorization cannot be encoded safely.
238
259
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
239
260
  * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
240
- * @throws {viem.BaseError} from `getRequirements()` when authorization reads fail.
261
+ * @throws {MissingContractCodeError} from `getRequirements()` when a fixed bundles or protocol
262
+ * contract has no code at its registered address.
263
+ * @throws {viem.BaseError} from `getRequirements()` when an authorization read or
264
+ * `eth_chainId` request fails.
241
265
  * @example
242
266
  * ```ts
243
267
  * import { AccrualPosition } from "@morpho-org/blue-sdk";
@@ -288,7 +312,8 @@ export interface BlueActions {
288
312
  * @param params.referralFeePct - Optional WAD-scaled referral fee below 100%.
289
313
  * @param params.referralFeeRecipient - Recipient required for a positive fee.
290
314
  * @returns Lazy Blue authorization resolution and a synchronous deep-frozen transaction.
291
- * @throws {ChainIdMismatchError} when the client targets another chain.
315
+ * @throws {ChainIdMismatchError} when the client targets another chain, or from
316
+ * `getRequirements()` when the RPC reports another chain via `eth_chainId`.
292
317
  * @throws {UnsupportedBlueMarketIrmError} when positive debt requires an unsupported IRM projection.
293
318
  * @throws {ExpiredDeadlineError} when the deadline is stale.
294
319
  * @throws {MissingAccrualPositionError} when no position snapshot is provided.
@@ -312,7 +337,10 @@ export interface BlueActions {
312
337
  * @throws {BundlesRequirementSignatureMismatchError} from `buildTx()` when authorization cannot be encoded safely.
313
338
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
314
339
  * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
315
- * @throws {viem.BaseError} from `getRequirements()` when authorization reads fail.
340
+ * @throws {MissingContractCodeError} from `getRequirements()` when a fixed bundles or protocol
341
+ * contract has no code at its registered address.
342
+ * @throws {viem.BaseError} from `getRequirements()` when an authorization read or
343
+ * `eth_chainId` request fails.
316
344
  * @example
317
345
  * ```ts
318
346
  * import { AccrualPosition } from "@morpho-org/blue-sdk";
@@ -364,7 +392,8 @@ export interface BlueActions {
364
392
  * @param params.referralFeePct - Optional WAD-scaled referral fee below 100%.
365
393
  * @param params.referralFeeRecipient - Recipient required for a positive fee.
366
394
  * @returns Lazy token prerequisite resolution and a synchronous deep-frozen transaction.
367
- * @throws {ChainIdMismatchError} when the client targets another chain.
395
+ * @throws {ChainIdMismatchError} when the client targets another chain, or from
396
+ * `getRequirements()` when the RPC reports another chain via `eth_chainId`.
368
397
  * @throws {UnsupportedBlueMarketIrmError} when positive debt requires an unsupported IRM projection.
369
398
  * @throws {ExpiredDeadlineError} when the deadline is stale.
370
399
  * @throws {MissingAccrualPositionError} when no position snapshot is provided.
@@ -394,7 +423,14 @@ export interface BlueActions {
394
423
  * @throws {BundlesRequirementSignatureMismatchError} from `buildTx()` when a signature cannot be encoded safely.
395
424
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
396
425
  * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
397
- * @throws {viem.BaseError} from `getRequirements()` when an allowance, nonce, or token metadata read fails.
426
+ * @throws {UnknownTokenError} from `getRequirements()` when the funded token's `decimals()` is unusable.
427
+ * @throws {MalformedTokenMetadataError} when the token's `eip712Domain()` or
428
+ * `bytes32` `name`/`symbol` retry returns non-empty data that does not decode, or when the
429
+ * deployless query reports malformed `name()`/`symbol()`/`eip712Domain()` returndata (no fallback).
430
+ * @throws {MissingContractCodeError} from `getRequirements()` when a fixed bundles or protocol
431
+ * contract has no code at its registered address.
432
+ * @throws {viem.BaseError} from `getRequirements()` when an allowance, nonce, token metadata,
433
+ * or `eth_chainId` read fails.
398
434
  * @example
399
435
  * ```ts
400
436
  * import { AccrualPosition } from "@morpho-org/blue-sdk";
@@ -449,7 +485,8 @@ export interface BlueActions {
449
485
  * @param params.referralFeePct - Optional WAD-scaled referral fee below 100%.
450
486
  * @param params.referralFeeRecipient - Recipient required for a positive fee.
451
487
  * @returns Lazy Blue authorization resolution and a synchronous deep-frozen transaction.
452
- * @throws {ChainIdMismatchError} when the client targets another chain.
488
+ * @throws {ChainIdMismatchError} when the client targets another chain, or from
489
+ * `getRequirements()` when the RPC reports another chain via `eth_chainId`.
453
490
  * @throws {UnsupportedBlueMarketIrmError} when positive debt requires an unsupported IRM projection.
454
491
  * @throws {ExpiredDeadlineError} when the deadline is stale.
455
492
  * @throws {MissingAccrualPositionError} when no position snapshot is provided.
@@ -468,7 +505,10 @@ export interface BlueActions {
468
505
  * @throws {BundlesRequirementSignatureMismatchError} from `buildTx()` when authorization cannot be encoded safely.
469
506
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
470
507
  * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
471
- * @throws {viem.BaseError} from `getRequirements()` when authorization reads fail.
508
+ * @throws {MissingContractCodeError} from `getRequirements()` when a fixed bundles or protocol
509
+ * contract has no code at its registered address.
510
+ * @throws {viem.BaseError} from `getRequirements()` when an authorization read or
511
+ * `eth_chainId` request fails.
472
512
  * @example
473
513
  * ```ts
474
514
  * import { AccrualPosition } from "@morpho-org/blue-sdk";
@@ -526,7 +566,8 @@ export interface BlueActions {
526
566
  * @param params.referralFeePct - Optional WAD-scaled referral fee below 100%.
527
567
  * @param params.referralFeeRecipient - Recipient required for a positive fee.
528
568
  * @returns Lazy funding/authorization resolution and a synchronous deep-frozen transaction builder.
529
- * @throws {ChainIdMismatchError} when the client targets another chain.
569
+ * @throws {ChainIdMismatchError} when the client targets another chain, or from
570
+ * `getRequirements()` when the RPC reports another chain via `eth_chainId`.
530
571
  * @throws {UnsupportedBlueMarketIrmError} when positive debt requires an unsupported IRM projection.
531
572
  * @throws {MissingAccrualPositionError} when no position snapshot is provided at runtime.
532
573
  * @throws {MarketIdMismatchError} when `positionData` belongs to another market.
@@ -559,7 +600,14 @@ export interface BlueActions {
559
600
  * @throws {BundlesRequirementSignatureMismatchError} from `buildTx()` when a signature cannot be encoded safely.
560
601
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
561
602
  * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
562
- * @throws {viem.BaseError} from `getRequirements()` when a required allowance, nonce, token metadata, or authorization read fails.
603
+ * @throws {UnknownTokenError} from `getRequirements()` when the funded token's `decimals()` is unusable.
604
+ * @throws {MalformedTokenMetadataError} when the token's `eip712Domain()` or
605
+ * `bytes32` `name`/`symbol` retry returns non-empty data that does not decode, or when the
606
+ * deployless query reports malformed `name()`/`symbol()`/`eip712Domain()` returndata (no fallback).
607
+ * @throws {MissingContractCodeError} from `getRequirements()` when a fixed bundles or protocol
608
+ * contract has no code at its registered address.
609
+ * @throws {viem.BaseError} from `getRequirements()` when a required allowance, nonce, token
610
+ * metadata, authorization, or `eth_chainId` read fails.
563
611
  * @example
564
612
  * ```ts
565
613
  * import { AccrualPosition } from "@morpho-org/blue-sdk";
@@ -625,7 +673,8 @@ export interface BlueActions {
625
673
  * @param params.referralFeePct - Optional WAD-scaled referral fee below 100%.
626
674
  * @param params.referralFeeRecipient - Recipient required for a positive fee.
627
675
  * @returns Lazy funding/authorization resolution and a synchronous deep-frozen transaction builder.
628
- * @throws {ChainIdMismatchError} when the client targets another chain.
676
+ * @throws {ChainIdMismatchError} when the client targets another chain, or from
677
+ * `getRequirements()` when the RPC reports another chain via `eth_chainId`.
629
678
  * @throws {UnsupportedBlueMarketIrmError} when positive debt requires an unsupported IRM projection.
630
679
  * @throws {MissingAccrualPositionError} when a borrow has no position snapshot.
631
680
  * @throws {MarketIdMismatchError} when `positionData` belongs to another market.
@@ -658,7 +707,14 @@ export interface BlueActions {
658
707
  * @throws {BundlesRequirementSignatureMismatchError} from `buildTx()` when a signature cannot be encoded safely.
659
708
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
660
709
  * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
661
- * @throws {viem.BaseError} from `getRequirements()` when a required allowance, nonce, token metadata, or authorization read fails.
710
+ * @throws {UnknownTokenError} from `getRequirements()` when the funded token's `decimals()` is unusable.
711
+ * @throws {MalformedTokenMetadataError} when the token's `eip712Domain()` or
712
+ * `bytes32` `name`/`symbol` retry returns non-empty data that does not decode, or when the
713
+ * deployless query reports malformed `name()`/`symbol()`/`eip712Domain()` returndata (no fallback).
714
+ * @throws {MissingContractCodeError} from `getRequirements()` when a fixed bundles or protocol
715
+ * contract has no code at its registered address.
716
+ * @throws {viem.BaseError} from `getRequirements()` when a required allowance, nonce, token
717
+ * metadata, authorization, or `eth_chainId` read fails.
662
718
  * @example
663
719
  * ```ts
664
720
  * import { markets } from "@morpho-org/morpho-test";
@@ -714,7 +770,8 @@ export interface BlueActions {
714
770
  * @param params.referralFeePct - Optional WAD-scaled referral fee below 100%.
715
771
  * @param params.referralFeeRecipient - Recipient required for a positive fee.
716
772
  * @returns Lazy Blue authorization resolution and a synchronous deep-frozen transaction builder.
717
- * @throws {ChainIdMismatchError} when the client targets another chain.
773
+ * @throws {ChainIdMismatchError} when the client targets another chain, or from
774
+ * `getRequirements()` when the RPC reports another chain via `eth_chainId`.
718
775
  * @throws {UnsupportedBlueMarketIrmError} when positive source or destination debt requires an unsupported IRM projection.
719
776
  * @throws {MissingAccrualPositionError} when either position snapshot is absent at runtime.
720
777
  * @throws {MarketIdMismatchError} when a position snapshot belongs to another market.
@@ -740,7 +797,10 @@ export interface BlueActions {
740
797
  * @throws {BundlesRequirementSignatureMismatchError} from `buildTx()` when authorization cannot be encoded safely.
741
798
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
742
799
  * @throws {UnknownAddressError} when BlueBundlesV1 is not registered.
743
- * @throws {viem.BaseError} from `getRequirements()` when authorization reads fail.
800
+ * @throws {MissingContractCodeError} from `getRequirements()` when a fixed bundles or protocol
801
+ * contract has no code at its registered address.
802
+ * @throws {viem.BaseError} from `getRequirements()` when an authorization read or
803
+ * `eth_chainId` request fails.
744
804
  * @example
745
805
  * ```ts
746
806
  * import { AccrualPosition } from "@morpho-org/blue-sdk";
@@ -797,13 +857,19 @@ export interface BlueActions {
797
857
  * @param params.block.number - Block number used for every RPC read.
798
858
  * @param params.block.timestamp - Timestamp corresponding to the fetched block.
799
859
  * @returns A `VaultV2BlueReallocationData` snapshot ready for {@link getVaultV2BlueReallocations}.
800
- * @throws {ChainIdMismatchError} when the client chain does not match this market.
860
+ * @throws {ChainIdMismatchError} when the client declares another chain or its RPC reports another
861
+ * chain via `eth_chainId`.
801
862
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
802
863
  * @throws {UnknownAddressError} when the chain has no BluePublicAllocator deployment.
803
864
  * @throws {UnknownBlueFactory} when the chain has no Vault V2 factory.
804
865
  * @throws {UnknownBlueOfFactory} when a requested address is not a Vault V2 from that factory.
805
866
  * @throws {UnsupportedBlueVaultV2AdapterError} when a vault contains an unsupported adapter.
806
- * @throws {viem.BaseError} when an RPC or contract read fails with no fallback left.
867
+ * @throws {UnknownTokenError} when a vault's share token has unusable decimals.
868
+ * @throws {MalformedTokenMetadataError} when the share token's `eip712Domain()` or
869
+ * `bytes32` `name`/`symbol` retry returns non-empty data that does not decode, or when the
870
+ * deployless query reports malformed `name()`/`symbol()`/`eip712Domain()` returndata (no fallback).
871
+ * @throws {viem.BaseError} when the `eth_chainId` request fails or a later RPC or contract read
872
+ * fails with no fallback left.
807
873
  */
808
874
  getVaultV2BlueReallocationData: (params: {
809
875
  vaultAddresses: readonly Address[];
@@ -862,6 +928,16 @@ export declare class MorphoBlue implements BlueActions {
862
928
  constructor(client: MorphoClientType, marketParams: MarketParams, chainId: number);
863
929
  private validateDeadline;
864
930
  private validateWriteCommon;
931
+ /**
932
+ * Asserts that every registry contract this flow's transaction targets or names as
933
+ * spender/authorized operator holds code, with one `eth_getCode` per distinct address
934
+ * before any other requirement read. Blue write flows probe `eth_chainId` only after a target
935
+ * has no code and report a chain mismatch when the RPC serves another chain.
936
+ *
937
+ * @param params.erc20Funding - Whether the flow resolves an ERC-20 funding leg, which
938
+ * may name canonical Permit2 when signatures are supported.
939
+ */
940
+ private assertWriteTargets;
865
941
  private getMaxLtv;
866
942
  private getBlueBundlesV1QuoteTimestamp;
867
943
  private getTokenRequirements;
@@ -998,13 +1074,19 @@ export declare class MorphoBlue implements BlueActions {
998
1074
  * @param params.block.number - Block number used for every RPC read.
999
1075
  * @param params.block.timestamp - Timestamp corresponding to the fetched block.
1000
1076
  * @returns A `VaultV2BlueReallocationData` snapshot ready for {@link getVaultV2BlueReallocations}.
1001
- * @throws {ChainIdMismatchError} when the client chain does not match this market.
1077
+ * @throws {ChainIdMismatchError} when the client declares another chain or its RPC reports another
1078
+ * chain via `eth_chainId`.
1002
1079
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
1003
1080
  * @throws {UnknownAddressError} when the chain has no BluePublicAllocator deployment.
1004
1081
  * @throws {UnknownBlueFactory} when the chain has no Vault V2 factory.
1005
1082
  * @throws {UnknownBlueOfFactory} when a requested address is not a Vault V2 from that factory.
1006
1083
  * @throws {UnsupportedBlueVaultV2AdapterError} when a vault contains an unsupported adapter.
1007
- * @throws {viem.BaseError} when an RPC or contract read fails with no fallback left.
1084
+ * @throws {UnknownTokenError} when a vault's share token has unusable decimals.
1085
+ * @throws {MalformedTokenMetadataError} when the share token's `eip712Domain()` or
1086
+ * `bytes32` `name`/`symbol` retry returns non-empty data that does not decode, or when the
1087
+ * deployless query reports malformed `name()`/`symbol()`/`eip712Domain()` returndata (no fallback).
1088
+ * @throws {viem.BaseError} when the `eth_chainId` request fails or a later RPC or contract read
1089
+ * fails with no fallback left.
1008
1090
  * @example
1009
1091
  * ```ts
1010
1092
  * import { markets } from "@morpho-org/morpho-test";
@@ -1,11 +1,13 @@
1
1
  import { MathLib, } from "@morpho-org/blue-sdk";
2
2
  import { fetchAccrualPosition, fetchAccrualVaultV2, fetchMarket, fetchVaultV2BluePublicAllocatorData, } from "@morpho-org/blue-sdk-viem";
3
- import { getChainAddress, Time } from "@morpho-org/morpho-ts";
3
+ import { getChainAddress, getChainAddresses, Time, } from "@morpho-org/morpho-ts";
4
4
  import { isAddressEqual, maxUint256 } from "viem";
5
5
  import { getBlueBundlesV1PenaltyAssets, getBlueBundlesV1PublicAllocations, getBlueBundlesV1ReferralFeeAssets, normalizeBlueBundlesV1CommonParams, selectBlueBundlesV1RequirementSignatures, validateBlueBundlesV1NativeFunding, } from "../../actions/blue/common.js";
6
6
  import { blueBorrow, blueRefinance, blueRepay, blueRepayWithdrawCollateral, blueSupply, blueSupplyCollateral, blueSupplyCollateralBorrow, blueWithdraw, blueWithdrawCollateral, getBlueAuthorizationRequirement, } from "../../actions/index.js";
7
+ import { validateClientChainId } from "../../actions/requirements/validateClientChainId.js";
7
8
  import { DEFAULT_LLTV_BUFFER, validateAccrualPosition, validateChainId, validatePositionHealth, validatePositionHealthAfterWithdraw, validateRepayAmount, validateRepayShares, validateWithdrawAmount, validateWithdrawShares, } from "../../helpers/index.js";
8
9
  import { ExpiredDeadlineError, InputExceedsMaxError, MaxRepayAssetsBelowRepayAssetsError, MissingAccrualPositionError, MutuallyExclusiveRepayAmountsError, MutuallyExclusiveWithdrawAmountsError, NegativeInputError, NonPositiveInputError, ReallocationsRequireBorrowError, RefinanceSameMarketError, RefinanceTokenMismatchError, } from "../../types/index.js";
10
+ import { assertRegistryTargetsHaveCode } from "../requirements/assertContractCode.js";
9
11
  import { getBundlesTokenRequirements } from "../requirements/index.js";
10
12
  import { VaultV2BlueReallocationData } from "../vaultV2BlueReallocationData.js";
11
13
  /** Binds a viem client to a Morpho Blue market's action builders. */
@@ -19,10 +21,9 @@ export class MorphoBlue {
19
21
  this.marketParams = marketParams;
20
22
  this.chainId = chainId;
21
23
  }
22
- validateDeadline(deadline) {
23
- const timestamp = Time.timestamp();
24
- if (deadline <= timestamp) {
25
- throw new ExpiredDeadlineError(deadline, timestamp);
24
+ validateDeadline(deadline, now = Time.timestamp()) {
25
+ if (deadline <= now) {
26
+ throw new ExpiredDeadlineError(deadline, now);
26
27
  }
27
28
  }
28
29
  validateWriteCommon(params) {
@@ -40,6 +41,30 @@ export class MorphoBlue {
40
41
  referralFeeRecipient: params.referralFeeRecipient,
41
42
  });
42
43
  }
44
+ /**
45
+ * Asserts that every registry contract this flow's transaction targets or names as
46
+ * spender/authorized operator holds code, with one `eth_getCode` per distinct address
47
+ * before any other requirement read. Blue write flows probe `eth_chainId` only after a target
48
+ * has no code and report a chain mismatch when the RPC serves another chain.
49
+ *
50
+ * @param params.erc20Funding - Whether the flow resolves an ERC-20 funding leg, which
51
+ * may name canonical Permit2 when signatures are supported.
52
+ */
53
+ async assertWriteTargets(params) {
54
+ const targets = [
55
+ {
56
+ role: "bundles.blueBundlesV1",
57
+ address: getChainAddress(this.chainId, "bundles.blueBundlesV1"),
58
+ },
59
+ { role: "blue", address: getChainAddress(this.chainId, "blue") },
60
+ ];
61
+ if (params?.erc20Funding && this.client.options.supportSignature) {
62
+ const permit2 = getChainAddresses(this.chainId).permit2;
63
+ if (permit2 != null)
64
+ targets.push({ role: "permit2", address: permit2 });
65
+ }
66
+ await assertRegistryTargetsHaveCode(this.client.viemClient, this.chainId, targets);
67
+ }
43
68
  getMaxLtv(marketParams = this.marketParams) {
44
69
  return MathLib.zeroFloorSub(marketParams.lltv, DEFAULT_LLTV_BUFFER);
45
70
  }
@@ -47,7 +72,7 @@ export class MorphoBlue {
47
72
  return MathLib.max(Time.timestamp(), lastUpdate) + Time.s.from.h(2n);
48
73
  }
49
74
  async getTokenRequirements(params) {
50
- this.validateDeadline(params.deadline);
75
+ this.validateDeadline(params.deadline, params.now);
51
76
  return getBundlesTokenRequirements(this.client.viemClient, {
52
77
  token: params.token,
53
78
  spender: getChainAddress(this.chainId, "bundles.blueBundlesV1"),
@@ -60,10 +85,11 @@ export class MorphoBlue {
60
85
  useSimplePermit: params.useSimplePermit,
61
86
  permit2Nonce: params.permit2Nonce,
62
87
  approvalAmount: params.approvalAmount,
88
+ now: params.now,
63
89
  });
64
90
  }
65
91
  async getAuthorizationRequirements(params) {
66
- this.validateDeadline(params.deadline);
92
+ this.validateDeadline(params.deadline, params.now);
67
93
  const requirement = await getBlueAuthorizationRequirement({
68
94
  viemClient: this.client.viemClient,
69
95
  chainId: this.chainId,
@@ -71,18 +97,19 @@ export class MorphoBlue {
71
97
  supportSignature: this.client.options.supportSignature,
72
98
  authorized: getChainAddress(this.chainId, "bundles.blueBundlesV1"),
73
99
  deadline: params.deadline,
100
+ now: params.now,
74
101
  });
75
102
  return requirement == null ? [] : [requirement];
76
103
  }
77
104
  async getMarketData(parameters) {
78
- validateChainId(this.client.viemClient.chain?.id, this.chainId);
105
+ await validateClientChainId(this.client.viemClient, this.chainId);
79
106
  return fetchMarket(this.marketParams.id, this.client.viemClient, {
80
107
  ...parameters,
81
108
  deployless: this.client.options.supportDeployless,
82
109
  });
83
110
  }
84
111
  async getPositionData(userAddress, parameters) {
85
- validateChainId(this.client.viemClient.chain?.id, this.chainId);
112
+ await validateClientChainId(this.client.viemClient, this.chainId);
86
113
  return fetchAccrualPosition(userAddress, this.marketParams.id, this.client.viemClient, {
87
114
  ...parameters,
88
115
  deployless: this.client.options.supportDeployless,
@@ -102,17 +129,23 @@ export class MorphoBlue {
102
129
  });
103
130
  return {
104
131
  getRequirements: (requirementsParams) => {
105
- this.validateDeadline(deadline);
106
- if (nativeValue > 0n)
107
- return Promise.resolve([]);
108
- return this.getTokenRequirements({
109
- token: this.marketParams.loanToken,
110
- amount: assets,
111
- userAddress,
112
- deadline,
113
- useSimplePermit: requirementsParams?.useSimplePermit,
114
- permit2Nonce: requirementsParams?.permit2Nonce,
115
- approvalAmount: requirementsParams?.approvalAmount,
132
+ const now = Time.timestamp();
133
+ this.validateDeadline(deadline, now);
134
+ return this.assertWriteTargets({
135
+ erc20Funding: nativeValue === 0n,
136
+ }).then(() => {
137
+ if (nativeValue > 0n)
138
+ return [];
139
+ return this.getTokenRequirements({
140
+ token: this.marketParams.loanToken,
141
+ amount: assets,
142
+ userAddress,
143
+ deadline,
144
+ now,
145
+ useSimplePermit: requirementsParams?.useSimplePermit,
146
+ permit2Nonce: requirementsParams?.permit2Nonce,
147
+ approvalAmount: requirementsParams?.approvalAmount,
148
+ });
116
149
  });
117
150
  },
118
151
  buildTx: (signatures) => {
@@ -186,7 +219,16 @@ export class MorphoBlue {
186
219
  });
187
220
  }
188
221
  return {
189
- getRequirements: () => this.getAuthorizationRequirements({ userAddress, deadline }),
222
+ getRequirements: async () => {
223
+ const now = Time.timestamp();
224
+ this.validateDeadline(deadline, now);
225
+ await this.assertWriteTargets();
226
+ return this.getAuthorizationRequirements({
227
+ userAddress,
228
+ deadline,
229
+ now,
230
+ });
231
+ },
190
232
  buildTx: (signatures) => {
191
233
  const { authorization } = selectBlueBundlesV1RequirementSignatures(signatures, {
192
234
  authorization: true,
@@ -430,7 +472,11 @@ export class MorphoBlue {
430
472
  }
431
473
  return {
432
474
  getRequirements: async (requirementsParams) => {
433
- this.validateDeadline(deadline);
475
+ const now = Time.timestamp();
476
+ this.validateDeadline(deadline, now);
477
+ await this.assertWriteTargets({
478
+ erc20Funding: hasRepay && nativeValue === 0n,
479
+ });
434
480
  const [tokenRequirements, authorizationRequirements] = await Promise.all([
435
481
  hasRepay && nativeValue === 0n
436
482
  ? this.getTokenRequirements({
@@ -439,12 +485,17 @@ export class MorphoBlue {
439
485
  approvalAmount: requirementsParams?.approvalAmount,
440
486
  userAddress,
441
487
  deadline,
488
+ now,
442
489
  useSimplePermit: requirementsParams?.useSimplePermit,
443
490
  permit2Nonce: requirementsParams?.permit2Nonce,
444
491
  })
445
492
  : Promise.resolve([]),
446
493
  collateralAssets > 0n
447
- ? this.getAuthorizationRequirements({ userAddress, deadline })
494
+ ? this.getAuthorizationRequirements({
495
+ userAddress,
496
+ deadline,
497
+ now,
498
+ })
448
499
  : Promise.resolve([]),
449
500
  ]);
450
501
  return [...tokenRequirements, ...authorizationRequirements];
@@ -533,7 +584,11 @@ export class MorphoBlue {
533
584
  });
534
585
  return {
535
586
  getRequirements: async (requirementsParams) => {
536
- this.validateDeadline(deadline);
587
+ const now = Time.timestamp();
588
+ this.validateDeadline(deadline, now);
589
+ await this.assertWriteTargets({
590
+ erc20Funding: collateralAssets > 0n && nativeValue === 0n,
591
+ });
537
592
  const [tokenRequirements, authorizationRequirements] = await Promise.all([
538
593
  collateralAssets > 0n && nativeValue === 0n
539
594
  ? this.getTokenRequirements({
@@ -542,12 +597,17 @@ export class MorphoBlue {
542
597
  approvalAmount: requirementsParams?.approvalAmount,
543
598
  userAddress,
544
599
  deadline,
600
+ now,
545
601
  useSimplePermit: requirementsParams?.useSimplePermit,
546
602
  permit2Nonce: requirementsParams?.permit2Nonce,
547
603
  })
548
604
  : Promise.resolve([]),
549
605
  borrowAssets > 0n
550
- ? this.getAuthorizationRequirements({ userAddress, deadline })
606
+ ? this.getAuthorizationRequirements({
607
+ userAddress,
608
+ deadline,
609
+ now,
610
+ })
551
611
  : Promise.resolve([]),
552
612
  ]);
553
613
  return [...tokenRequirements, ...authorizationRequirements];
@@ -625,7 +685,16 @@ export class MorphoBlue {
625
685
  lltv: destination.marketParams.lltv,
626
686
  });
627
687
  return {
628
- getRequirements: () => this.getAuthorizationRequirements({ userAddress, deadline }),
688
+ getRequirements: async () => {
689
+ const now = Time.timestamp();
690
+ this.validateDeadline(deadline, now);
691
+ await this.assertWriteTargets();
692
+ return this.getAuthorizationRequirements({
693
+ userAddress,
694
+ deadline,
695
+ now,
696
+ });
697
+ },
629
698
  buildTx: (signatures) => {
630
699
  const { authorization } = selectBlueBundlesV1RequirementSignatures(signatures, {
631
700
  authorization: true,
@@ -660,13 +729,19 @@ export class MorphoBlue {
660
729
  * @param params.block.number - Block number used for every RPC read.
661
730
  * @param params.block.timestamp - Timestamp corresponding to the fetched block.
662
731
  * @returns A `VaultV2BlueReallocationData` snapshot ready for {@link getVaultV2BlueReallocations}.
663
- * @throws {ChainIdMismatchError} when the client chain does not match this market.
732
+ * @throws {ChainIdMismatchError} when the client declares another chain or its RPC reports another
733
+ * chain via `eth_chainId`.
664
734
  * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
665
735
  * @throws {UnknownAddressError} when the chain has no BluePublicAllocator deployment.
666
736
  * @throws {UnknownBlueFactory} when the chain has no Vault V2 factory.
667
737
  * @throws {UnknownBlueOfFactory} when a requested address is not a Vault V2 from that factory.
668
738
  * @throws {UnsupportedBlueVaultV2AdapterError} when a vault contains an unsupported adapter.
669
- * @throws {viem.BaseError} when an RPC or contract read fails with no fallback left.
739
+ * @throws {UnknownTokenError} when a vault's share token has unusable decimals.
740
+ * @throws {MalformedTokenMetadataError} when the share token's `eip712Domain()` or
741
+ * `bytes32` `name`/`symbol` retry returns non-empty data that does not decode, or when the
742
+ * deployless query reports malformed `name()`/`symbol()`/`eip712Domain()` returndata (no fallback).
743
+ * @throws {viem.BaseError} when the `eth_chainId` request fails or a later RPC or contract read
744
+ * fails with no fallback left.
670
745
  * @example
671
746
  * ```ts
672
747
  * import { markets } from "@morpho-org/morpho-test";
@@ -688,7 +763,7 @@ export class MorphoBlue {
688
763
  * ```
689
764
  */
690
765
  async getVaultV2BlueReallocationData({ vaultAddresses, block, }) {
691
- validateChainId(this.client.viemClient.chain?.id, this.chainId);
766
+ await validateClientChainId(this.client.viemClient, this.chainId);
692
767
  const client = this.client.viemClient;
693
768
  const fetchParams = {
694
769
  blockNumber: block.number,
@@ -12,7 +12,7 @@ export type { AnyTree as MidnightAnyTree, AnyTreeSnapshot as MidnightAnyTreeSnap
12
12
  export { AccrualPosition as MidnightAccrualPosition, Group as MidnightGroup, Market as MidnightMarket, MarketParams as MidnightMarketParams, Offer as MidnightOffer, Position as MidnightPosition, Tree as MidnightTree, } from "@morpho-org/midnight-sdk";
13
13
  export type { BlueActions, BlueTokenRequirementsParams, } from "./blue/index.js";
14
14
  export { MorphoBlue } from "./blue/index.js";
15
- export type { GetOffersDataParams as MidnightGetOffersDataParams, GetPositionDataParams as MidnightGetPositionDataParams, MakeLendParams as MidnightMakeLendParams, MakeOffersOutput as MidnightMakeOffersOutput, MakeOffersParams as MidnightMakeOffersParams, MarketActionParams as MidnightMarketActionParams, MidnightMakerTreeInput, OffersData as MidnightOffersData, OfferValidationParams as MidnightOfferValidationParams, RedeemParams as MidnightRedeemParams, RepayWithdrawCollateralParams as MidnightRepayWithdrawCollateralParams, SupplyBlueMakeLendParams as MidnightSupplyBlueMakeLendParams, SupplyCollateralMakeBorrowParams as MidnightSupplyCollateralMakeBorrowParams, SupplyCollateralParams as MidnightSupplyCollateralParams, SupplyCollateralTakeBorrowParams as MidnightSupplyCollateralTakeBorrowParams, TakeBorrowParams as MidnightTakeBorrowParams, TakeLendParams as MidnightTakeLendParams, TakeRepayWithdrawCollateralParams as MidnightTakeRepayWithdrawCollateralParams, TakeWithdrawParams as MidnightTakeWithdrawParams, } from "./midnight/index.js";
15
+ export type { GetOffersDataParams as MidnightGetOffersDataParams, GetPositionDataParams as MidnightGetPositionDataParams, MakeLendParams as MidnightMakeLendParams, MakeOffersOutput as MidnightMakeOffersOutput, MakeOffersParams as MidnightMakeOffersParams, MarketActionParams as MidnightMarketActionParams, MidnightMakerTreeInput, MidnightPositionHealthParams, OffersData as MidnightOffersData, OfferValidationParams as MidnightOfferValidationParams, RedeemParams as MidnightRedeemParams, RepayWithdrawCollateralParams as MidnightRepayWithdrawCollateralParams, SupplyBlueMakeLendParams as MidnightSupplyBlueMakeLendParams, SupplyCollateralMakeBorrowParams as MidnightSupplyCollateralMakeBorrowParams, SupplyCollateralParams as MidnightSupplyCollateralParams, SupplyCollateralTakeBorrowParams as MidnightSupplyCollateralTakeBorrowParams, TakeBorrowParams as MidnightTakeBorrowParams, TakeLendParams as MidnightTakeLendParams, TakeRepayWithdrawCollateralParams as MidnightTakeRepayWithdrawCollateralParams, TakeWithdrawParams as MidnightTakeWithdrawParams, } from "./midnight/index.js";
16
16
  export { type MidnightActionOutput, type MidnightActions, type MidnightRequirementsParams, MorphoMidnight, } from "./midnight/index.js";
17
17
  export * from "./requirements/index.js";
18
18
  export { MorphoVaultV1 } from "./vaultV1/index.js";
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Rejects a Midnight bundle deadline that is not strictly after `now`.
3
+ *
4
+ * Midnight bundles revert with `DeadlinePassed()` once the deadline has passed, so a deadline at
5
+ * or before `now` builds a transaction that cannot succeed. `maxUint256` still means
6
+ * "no expiry". The check uses the local clock (`Time.timestamp()`), not chain time. Transaction
7
+ * builders stay clock-free and only check the deadline's `uint256` shape.
8
+ *
9
+ * @param deadline - Bundle execution deadline timestamp, in seconds.
10
+ * @param now - Reference timestamp, in seconds. Defaults to `Time.timestamp()`.
11
+ * @returns Nothing when `deadline` is after `now`.
12
+ * @throws {ExpiredDeadlineError} when `deadline` is at or before `now`.
13
+ * @internal
14
+ */
15
+ export declare const validateMidnightDeadline: (deadline: bigint, now?: bigint) => void;
@@ -0,0 +1,20 @@
1
+ import { Time } from "@morpho-org/morpho-ts";
2
+ import { ExpiredDeadlineError } from "../../types/index.js";
3
+ /**
4
+ * Rejects a Midnight bundle deadline that is not strictly after `now`.
5
+ *
6
+ * Midnight bundles revert with `DeadlinePassed()` once the deadline has passed, so a deadline at
7
+ * or before `now` builds a transaction that cannot succeed. `maxUint256` still means
8
+ * "no expiry". The check uses the local clock (`Time.timestamp()`), not chain time. Transaction
9
+ * builders stay clock-free and only check the deadline's `uint256` shape.
10
+ *
11
+ * @param deadline - Bundle execution deadline timestamp, in seconds.
12
+ * @param now - Reference timestamp, in seconds. Defaults to `Time.timestamp()`.
13
+ * @returns Nothing when `deadline` is after `now`.
14
+ * @throws {ExpiredDeadlineError} when `deadline` is at or before `now`.
15
+ * @internal
16
+ */
17
+ export const validateMidnightDeadline = (deadline, now = Time.timestamp()) => {
18
+ if (deadline <= now)
19
+ throw new ExpiredDeadlineError(deadline, now);
20
+ };