@morpho-org/morpho-sdk 10.1.0-next.0 → 10.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 (470) hide show
  1. package/README.md +6 -2
  2. package/lib/cjs/abis.d.ts +1 -1
  3. package/lib/cjs/abis.js +1 -5
  4. package/lib/cjs/actions/blue/borrow.d.ts +2 -1
  5. package/lib/cjs/actions/blue/borrow.internal.d.ts +13 -0
  6. package/lib/cjs/actions/blue/borrow.internal.js +35 -0
  7. package/lib/cjs/actions/blue/borrow.js +4 -16
  8. package/lib/cjs/actions/blue/common.d.ts +50 -28
  9. package/lib/cjs/actions/blue/common.js +21 -34
  10. package/lib/cjs/actions/blue/encode.d.ts +23 -0
  11. package/lib/cjs/actions/blue/encode.js +171 -0
  12. package/lib/cjs/actions/blue/refinance.d.ts +3 -2
  13. package/lib/cjs/actions/blue/refinance.internal.d.ts +42 -0
  14. package/lib/cjs/actions/blue/refinance.internal.js +62 -0
  15. package/lib/cjs/actions/blue/refinance.js +5 -63
  16. package/lib/cjs/actions/blue/refinanceValidation.d.ts +4 -5
  17. package/lib/cjs/actions/blue/refinanceValidation.js +1 -3
  18. package/lib/cjs/actions/blue/repay.d.ts +1 -1
  19. package/lib/cjs/actions/blue/repay.internal.d.ts +13 -0
  20. package/lib/cjs/actions/blue/repay.internal.js +38 -0
  21. package/lib/cjs/actions/blue/repay.js +2 -17
  22. package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +2 -2
  23. package/lib/cjs/actions/blue/repayWithdrawCollateral.internal.d.ts +61 -0
  24. package/lib/cjs/actions/blue/repayWithdrawCollateral.internal.js +112 -0
  25. package/lib/cjs/actions/blue/repayWithdrawCollateral.js +3 -116
  26. package/lib/cjs/actions/blue/signatures.d.ts +30 -0
  27. package/lib/cjs/actions/blue/signatures.js +90 -0
  28. package/lib/cjs/actions/blue/supply.d.ts +1 -1
  29. package/lib/cjs/actions/blue/supply.internal.d.ts +32 -0
  30. package/lib/cjs/actions/blue/supply.internal.js +101 -0
  31. package/lib/cjs/actions/blue/supply.js +2 -67
  32. package/lib/cjs/actions/blue/supplyCollateral.d.ts +1 -1
  33. package/lib/cjs/actions/blue/supplyCollateral.internal.d.ts +13 -0
  34. package/lib/cjs/actions/blue/supplyCollateral.internal.js +37 -0
  35. package/lib/cjs/actions/blue/supplyCollateral.js +2 -17
  36. package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +4 -3
  37. package/lib/cjs/actions/blue/supplyCollateralBorrow.internal.d.ts +63 -0
  38. package/lib/cjs/actions/blue/supplyCollateralBorrow.internal.js +94 -0
  39. package/lib/cjs/actions/blue/supplyCollateralBorrow.js +5 -106
  40. package/lib/cjs/actions/blue/withdraw.d.ts +4 -3
  41. package/lib/cjs/actions/blue/withdraw.internal.d.ts +56 -0
  42. package/lib/cjs/actions/blue/withdraw.internal.js +84 -0
  43. package/lib/cjs/actions/blue/withdraw.js +5 -78
  44. package/lib/cjs/actions/blue/withdrawBuyCallback.d.ts +2 -2
  45. package/lib/cjs/actions/blue/withdrawBuyCallback.internal.d.ts +20 -0
  46. package/lib/cjs/actions/blue/withdrawBuyCallback.internal.js +58 -0
  47. package/lib/cjs/actions/blue/withdrawBuyCallback.js +2 -39
  48. package/lib/cjs/actions/blue/withdrawCollateral.internal.d.ts +16 -0
  49. package/lib/cjs/actions/blue/withdrawCollateral.internal.js +39 -0
  50. package/lib/cjs/actions/blue/withdrawCollateral.js +2 -17
  51. package/lib/cjs/actions/bundles/common.d.ts +9 -0
  52. package/lib/cjs/actions/bundles/common.js +18 -6
  53. package/lib/cjs/actions/bundles/index.d.ts +1 -1
  54. package/lib/cjs/actions/bundles/index.js +3 -16
  55. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +14 -0
  56. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +59 -60
  57. package/lib/cjs/actions/midnight/cancelAndMake.d.ts +22 -1
  58. package/lib/cjs/actions/midnight/cancelAndMake.js +46 -87
  59. package/lib/cjs/actions/midnight/cancelOffers.d.ts +11 -0
  60. package/lib/cjs/actions/midnight/cancelOffers.js +22 -4
  61. package/lib/cjs/actions/midnight/encode.d.ts +36 -0
  62. package/lib/cjs/actions/midnight/encode.js +554 -0
  63. package/lib/cjs/actions/midnight/index.d.ts +14 -13
  64. package/lib/cjs/actions/midnight/index.js +29 -13
  65. package/lib/cjs/actions/midnight/parse.d.ts +35 -0
  66. package/lib/cjs/actions/midnight/parse.js +73 -0
  67. package/lib/cjs/actions/midnight/parseCollateralBatch.d.ts +58 -0
  68. package/lib/cjs/actions/midnight/parseCollateralBatch.js +216 -0
  69. package/lib/cjs/actions/midnight/redeem.d.ts +16 -3
  70. package/lib/cjs/actions/midnight/redeem.js +22 -51
  71. package/lib/cjs/actions/midnight/repayWithdrawCollateral.d.ts +13 -0
  72. package/lib/cjs/actions/midnight/repayWithdrawCollateral.js +42 -67
  73. package/lib/cjs/actions/midnight/supplyCollateral.d.ts +40 -5
  74. package/lib/cjs/actions/midnight/supplyCollateral.js +60 -54
  75. package/lib/cjs/actions/midnight/supplyCollateralBatch.d.ts +3 -3
  76. package/lib/cjs/actions/midnight/supplyCollateralBatch.js +3 -62
  77. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +24 -0
  78. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +39 -66
  79. package/lib/cjs/actions/midnight/supplyCollateralWithNative.d.ts +77 -0
  80. package/lib/cjs/actions/midnight/supplyCollateralWithNative.js +142 -0
  81. package/lib/cjs/actions/midnight/takeBorrow.d.ts +17 -2
  82. package/lib/cjs/actions/midnight/takeBorrow.js +25 -55
  83. package/lib/cjs/actions/midnight/takeLend.d.ts +21 -1
  84. package/lib/cjs/actions/midnight/takeLend.js +30 -57
  85. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.d.ts +22 -2
  86. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +34 -61
  87. package/lib/cjs/actions/midnight/takeWithdraw.d.ts +17 -2
  88. package/lib/cjs/actions/midnight/takeWithdraw.js +78 -115
  89. package/lib/cjs/actions/midnight/withdrawCollateral.d.ts +16 -3
  90. package/lib/cjs/actions/midnight/withdrawCollateral.js +25 -56
  91. package/lib/cjs/actions/midnight/withdrawCollateralBatch.d.ts +3 -3
  92. package/lib/cjs/actions/midnight/withdrawCollateralBatch.js +3 -71
  93. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +13 -0
  94. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +39 -19
  95. package/lib/cjs/actions/requirements/blue/index.d.ts +1 -1
  96. package/lib/cjs/actions/requirements/blue/index.js +3 -15
  97. package/lib/cjs/actions/requirements/chainCheck.d.ts +5 -0
  98. package/lib/cjs/actions/requirements/chainCheck.js +10 -0
  99. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +10 -0
  100. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +40 -27
  101. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +11 -1
  102. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +36 -17
  103. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +14 -1
  104. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +44 -9
  105. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +10 -0
  106. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +61 -24
  107. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +10 -0
  108. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +25 -4
  109. package/lib/cjs/actions/requirements/encode/index.d.ts +5 -5
  110. package/lib/cjs/actions/requirements/encode/index.js +11 -19
  111. package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +26 -11
  112. package/lib/cjs/actions/requirements/getRequirementsApproval.js +40 -12
  113. package/lib/cjs/actions/requirements/index.d.ts +1 -1
  114. package/lib/cjs/actions/requirements/index.js +3 -1
  115. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +11 -0
  116. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.js +34 -19
  117. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +11 -0
  118. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +26 -12
  119. package/lib/cjs/actions/requirements/midnight/index.d.ts +2 -2
  120. package/lib/cjs/actions/requirements/midnight/index.js +5 -16
  121. package/lib/cjs/actions/vaultV1/deposit.d.ts +21 -0
  122. package/lib/cjs/actions/vaultV1/deposit.js +46 -10
  123. package/lib/cjs/actions/vaultV1/inKindRedeem.d.ts +18 -1
  124. package/lib/cjs/actions/vaultV1/inKindRedeem.js +36 -16
  125. package/lib/cjs/actions/vaultV1/index.d.ts +5 -5
  126. package/lib/cjs/actions/vaultV1/index.js +11 -19
  127. package/lib/cjs/actions/vaultV1/migrateToV2.d.ts +21 -0
  128. package/lib/cjs/actions/vaultV1/migrateToV2.js +49 -11
  129. package/lib/cjs/actions/vaultV1/redeem.d.ts +19 -0
  130. package/lib/cjs/actions/vaultV1/redeem.js +28 -7
  131. package/lib/cjs/actions/vaultV1/withdraw.d.ts +19 -0
  132. package/lib/cjs/actions/vaultV1/withdraw.js +28 -7
  133. package/lib/cjs/actions/vaultV2/deposit.d.ts +21 -0
  134. package/lib/cjs/actions/vaultV2/deposit.js +46 -10
  135. package/lib/cjs/actions/vaultV2/forceRedeem.d.ts +20 -2
  136. package/lib/cjs/actions/vaultV2/forceRedeem.js +39 -8
  137. package/lib/cjs/actions/vaultV2/forceWithdraw.d.ts +23 -1
  138. package/lib/cjs/actions/vaultV2/forceWithdraw.js +36 -14
  139. package/lib/cjs/actions/vaultV2/inKindRedeem.d.ts +18 -1
  140. package/lib/cjs/actions/vaultV2/inKindRedeem.js +35 -15
  141. package/lib/cjs/actions/vaultV2/index.d.ts +6 -6
  142. package/lib/cjs/actions/vaultV2/index.js +13 -20
  143. package/lib/cjs/actions/vaultV2/redeem.d.ts +19 -0
  144. package/lib/cjs/actions/vaultV2/redeem.js +28 -7
  145. package/lib/cjs/actions/vaultV2/withdraw.d.ts +19 -0
  146. package/lib/cjs/actions/vaultV2/withdraw.js +28 -7
  147. package/lib/cjs/blue/abis.d.ts +1 -1
  148. package/lib/cjs/blue/abis.js +1 -5
  149. package/lib/cjs/blue/entities.d.ts +4 -4
  150. package/lib/cjs/blue/entities.js +1 -3
  151. package/lib/cjs/blue/errors.d.ts +2 -2
  152. package/lib/cjs/blue/errors.js +1 -5
  153. package/lib/cjs/blue/fetch.d.ts +2 -2
  154. package/lib/cjs/blue/fetch.js +1 -3
  155. package/lib/cjs/blue/utils.d.ts +1 -1
  156. package/lib/cjs/blue/utils.js +1 -2
  157. package/lib/cjs/entities/blue/blue.d.ts +35 -8
  158. package/lib/cjs/entities/blue/blue.js +467 -406
  159. package/lib/cjs/entities/index.d.ts +4 -4
  160. package/lib/cjs/entities/index.js +1 -3
  161. package/lib/cjs/entities/midnight/midnight.d.ts +117 -41
  162. package/lib/cjs/entities/midnight/midnight.js +472 -843
  163. package/lib/cjs/entities/midnight/parse.d.ts +74 -0
  164. package/lib/cjs/entities/midnight/parse.js +336 -0
  165. package/lib/cjs/entities/midnight/prepare.d.ts +47 -0
  166. package/lib/cjs/entities/midnight/prepare.js +293 -0
  167. package/lib/cjs/entities/midnight/publication.d.ts +7 -3
  168. package/lib/cjs/entities/midnight/publication.js +10 -7
  169. package/lib/cjs/entities/midnight/types.d.ts +7 -0
  170. package/lib/cjs/entities/requirements/assertContractCode.d.ts +10 -5
  171. package/lib/cjs/entities/requirements/assertContractCode.js +24 -15
  172. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +15 -0
  173. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +59 -51
  174. package/lib/cjs/entities/requirements/getErc2612Permit.d.ts +7 -3
  175. package/lib/cjs/entities/requirements/getErc2612Permit.js +20 -4
  176. package/lib/cjs/entities/requirements/getUnusedPermit2Nonce.d.ts +15 -0
  177. package/lib/cjs/entities/requirements/getUnusedPermit2Nonce.js +24 -7
  178. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +14 -0
  179. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +27 -7
  180. package/lib/cjs/entities/requirements/getVaultV1InKindRedeemRequirements.js +4 -3
  181. package/lib/cjs/entities/requirements/getVaultV2ForceWithdrawRequirements.js +4 -3
  182. package/lib/cjs/entities/requirements/getVaultV2InKindRedeemRequirements.js +4 -3
  183. package/lib/cjs/entities/requirements/index.d.ts +2 -2
  184. package/lib/cjs/entities/requirements/index.js +5 -16
  185. package/lib/cjs/entities/requirements/validateNativeRequirements.d.ts +21 -0
  186. package/lib/cjs/entities/requirements/validateNativeRequirements.js +37 -0
  187. package/lib/cjs/entities/vaultV1/inKindRedeemQuote.d.ts +2 -2
  188. package/lib/cjs/entities/vaultV1/inKindRedeemQuote.js +1 -7
  189. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +17 -40
  190. package/lib/cjs/entities/vaultV1/vaultV1.js +228 -159
  191. package/lib/cjs/entities/vaultV2/forceWithdrawQuote.d.ts +4 -0
  192. package/lib/cjs/entities/vaultV2/forceWithdrawQuote.js +9 -4
  193. package/lib/cjs/entities/vaultV2/inKindRedeemQuote.d.ts +2 -2
  194. package/lib/cjs/entities/vaultV2/inKindRedeemQuote.js +1 -7
  195. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +29 -86
  196. package/lib/cjs/entities/vaultV2/vaultV2.js +226 -188
  197. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +2 -3
  198. package/lib/cjs/entities/vaultV2BlueReallocationData.js +2 -6
  199. package/lib/cjs/errors.d.ts +4 -4
  200. package/lib/cjs/errors.js +5 -7
  201. package/lib/cjs/fetch.d.ts +2 -2
  202. package/lib/cjs/fetch.js +1 -3
  203. package/lib/cjs/helpers/deadShares.js +0 -3
  204. package/lib/cjs/helpers/parse.d.ts +33 -0
  205. package/lib/cjs/helpers/parse.js +71 -0
  206. package/lib/cjs/helpers/parseMidnight.d.ts +48 -0
  207. package/lib/cjs/helpers/parseMidnight.js +95 -0
  208. package/lib/cjs/helpers/parseMidnightMaker.d.ts +44 -0
  209. package/lib/cjs/helpers/parseMidnightMaker.js +154 -0
  210. package/lib/cjs/helpers/validate.d.ts +3 -2
  211. package/lib/cjs/helpers/validate.js +4 -0
  212. package/lib/cjs/helpers/validateMidnightCancelAndMakePublication.d.ts +8 -0
  213. package/lib/cjs/helpers/validateMidnightCancelAndMakePublication.js +4 -4
  214. package/lib/cjs/helpers/validateMidnightPublication.d.ts +17 -1
  215. package/lib/cjs/index.d.ts +1 -0
  216. package/lib/cjs/midnight/errors.d.ts +1 -2
  217. package/lib/cjs/midnight/errors.js +4 -5
  218. package/lib/cjs/transactionPlan/blue/refinance.js +17 -9
  219. package/lib/cjs/transactionPlan/blue/repay.js +2 -1
  220. package/lib/cjs/transactionPlan/blue/repayWithdrawCollateral.js +23 -4
  221. package/lib/cjs/transactionPlan/blue/supplyCollateralBorrow.js +2 -2
  222. package/lib/cjs/transactionPlan/blue/withdraw.d.ts +2 -2
  223. package/lib/cjs/transactionPlan/blue/withdraw.js +3 -3
  224. package/lib/cjs/transactionPlan/blue/withdrawCollateral.js +6 -2
  225. package/lib/cjs/transactionPlan/validation.d.ts +1 -1
  226. package/lib/cjs/transactionPlan/validation.js +3 -0
  227. package/lib/cjs/transactionPlan/vaultv1/deposit.d.ts +2 -2
  228. package/lib/cjs/transactionPlan/vaultv1/deposit.js +2 -2
  229. package/lib/cjs/types/action.d.ts +13 -2
  230. package/lib/cjs/types/error.d.ts +20 -2
  231. package/lib/cjs/types/error.js +32 -3
  232. package/lib/cjs/utils.d.ts +2 -2
  233. package/lib/cjs/utils.js +2 -4
  234. package/lib/cjs/version.d.ts +1 -1
  235. package/lib/cjs/version.js +1 -1
  236. package/lib/esm/abis.d.ts +1 -1
  237. package/lib/esm/abis.js +1 -1
  238. package/lib/esm/actions/blue/borrow.d.ts +2 -1
  239. package/lib/esm/actions/blue/borrow.internal.d.ts +13 -0
  240. package/lib/esm/actions/blue/borrow.internal.js +30 -0
  241. package/lib/esm/actions/blue/borrow.js +4 -16
  242. package/lib/esm/actions/blue/common.d.ts +50 -28
  243. package/lib/esm/actions/blue/common.js +21 -33
  244. package/lib/esm/actions/blue/encode.d.ts +23 -0
  245. package/lib/esm/actions/blue/encode.js +164 -0
  246. package/lib/esm/actions/blue/refinance.d.ts +3 -2
  247. package/lib/esm/actions/blue/refinance.internal.d.ts +42 -0
  248. package/lib/esm/actions/blue/refinance.internal.js +56 -0
  249. package/lib/esm/actions/blue/refinance.js +5 -63
  250. package/lib/esm/actions/blue/refinanceValidation.d.ts +4 -5
  251. package/lib/esm/actions/blue/refinanceValidation.js +2 -4
  252. package/lib/esm/actions/blue/repay.d.ts +1 -1
  253. package/lib/esm/actions/blue/repay.internal.d.ts +13 -0
  254. package/lib/esm/actions/blue/repay.internal.js +33 -0
  255. package/lib/esm/actions/blue/repay.js +2 -17
  256. package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +2 -2
  257. package/lib/esm/actions/blue/repayWithdrawCollateral.internal.d.ts +61 -0
  258. package/lib/esm/actions/blue/repayWithdrawCollateral.internal.js +104 -0
  259. package/lib/esm/actions/blue/repayWithdrawCollateral.js +3 -116
  260. package/lib/esm/actions/blue/signatures.d.ts +30 -0
  261. package/lib/esm/actions/blue/signatures.js +82 -0
  262. package/lib/esm/actions/blue/supply.d.ts +1 -1
  263. package/lib/esm/actions/blue/supply.internal.d.ts +32 -0
  264. package/lib/esm/actions/blue/supply.internal.js +95 -0
  265. package/lib/esm/actions/blue/supply.js +2 -67
  266. package/lib/esm/actions/blue/supplyCollateral.d.ts +1 -1
  267. package/lib/esm/actions/blue/supplyCollateral.internal.d.ts +13 -0
  268. package/lib/esm/actions/blue/supplyCollateral.internal.js +32 -0
  269. package/lib/esm/actions/blue/supplyCollateral.js +2 -17
  270. package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +4 -3
  271. package/lib/esm/actions/blue/supplyCollateralBorrow.internal.d.ts +63 -0
  272. package/lib/esm/actions/blue/supplyCollateralBorrow.internal.js +87 -0
  273. package/lib/esm/actions/blue/supplyCollateralBorrow.js +5 -106
  274. package/lib/esm/actions/blue/withdraw.d.ts +4 -3
  275. package/lib/esm/actions/blue/withdraw.internal.d.ts +56 -0
  276. package/lib/esm/actions/blue/withdraw.internal.js +77 -0
  277. package/lib/esm/actions/blue/withdraw.js +5 -78
  278. package/lib/esm/actions/blue/withdrawBuyCallback.d.ts +2 -2
  279. package/lib/esm/actions/blue/withdrawBuyCallback.internal.d.ts +20 -0
  280. package/lib/esm/actions/blue/withdrawBuyCallback.internal.js +53 -0
  281. package/lib/esm/actions/blue/withdrawBuyCallback.js +2 -39
  282. package/lib/esm/actions/blue/withdrawCollateral.internal.d.ts +16 -0
  283. package/lib/esm/actions/blue/withdrawCollateral.internal.js +34 -0
  284. package/lib/esm/actions/blue/withdrawCollateral.js +2 -17
  285. package/lib/esm/actions/bundles/common.d.ts +9 -0
  286. package/lib/esm/actions/bundles/common.js +15 -4
  287. package/lib/esm/actions/bundles/index.d.ts +1 -1
  288. package/lib/esm/actions/bundles/index.js +1 -1
  289. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +14 -0
  290. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +58 -61
  291. package/lib/esm/actions/midnight/cancelAndMake.d.ts +22 -1
  292. package/lib/esm/actions/midnight/cancelAndMake.js +43 -85
  293. package/lib/esm/actions/midnight/cancelOffers.d.ts +11 -0
  294. package/lib/esm/actions/midnight/cancelOffers.js +18 -2
  295. package/lib/esm/actions/midnight/encode.d.ts +36 -0
  296. package/lib/esm/actions/midnight/encode.js +539 -0
  297. package/lib/esm/actions/midnight/index.d.ts +14 -13
  298. package/lib/esm/actions/midnight/index.js +14 -13
  299. package/lib/esm/actions/midnight/parse.d.ts +35 -0
  300. package/lib/esm/actions/midnight/parse.js +68 -0
  301. package/lib/esm/actions/midnight/parseCollateralBatch.d.ts +58 -0
  302. package/lib/esm/actions/midnight/parseCollateralBatch.js +209 -0
  303. package/lib/esm/actions/midnight/redeem.d.ts +16 -3
  304. package/lib/esm/actions/midnight/redeem.js +20 -50
  305. package/lib/esm/actions/midnight/repayWithdrawCollateral.d.ts +13 -0
  306. package/lib/esm/actions/midnight/repayWithdrawCollateral.js +38 -64
  307. package/lib/esm/actions/midnight/supplyCollateral.d.ts +40 -5
  308. package/lib/esm/actions/midnight/supplyCollateral.js +57 -53
  309. package/lib/esm/actions/midnight/supplyCollateralBatch.d.ts +3 -3
  310. package/lib/esm/actions/midnight/supplyCollateralBatch.js +3 -62
  311. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +24 -0
  312. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +33 -61
  313. package/lib/esm/actions/midnight/supplyCollateralWithNative.d.ts +77 -0
  314. package/lib/esm/actions/midnight/supplyCollateralWithNative.js +137 -0
  315. package/lib/esm/actions/midnight/takeBorrow.d.ts +17 -2
  316. package/lib/esm/actions/midnight/takeBorrow.js +20 -51
  317. package/lib/esm/actions/midnight/takeLend.d.ts +21 -1
  318. package/lib/esm/actions/midnight/takeLend.js +26 -54
  319. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.d.ts +22 -2
  320. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +30 -58
  321. package/lib/esm/actions/midnight/takeWithdraw.d.ts +17 -2
  322. package/lib/esm/actions/midnight/takeWithdraw.js +76 -114
  323. package/lib/esm/actions/midnight/withdrawCollateral.d.ts +16 -3
  324. package/lib/esm/actions/midnight/withdrawCollateral.js +23 -55
  325. package/lib/esm/actions/midnight/withdrawCollateralBatch.d.ts +3 -3
  326. package/lib/esm/actions/midnight/withdrawCollateralBatch.js +3 -71
  327. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +13 -0
  328. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +36 -18
  329. package/lib/esm/actions/requirements/blue/index.d.ts +1 -1
  330. package/lib/esm/actions/requirements/blue/index.js +1 -1
  331. package/lib/esm/actions/requirements/chainCheck.d.ts +5 -0
  332. package/lib/esm/actions/requirements/chainCheck.js +6 -0
  333. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +10 -0
  334. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +36 -25
  335. package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +11 -1
  336. package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +33 -16
  337. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +14 -1
  338. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +41 -8
  339. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +10 -0
  340. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +59 -24
  341. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +10 -0
  342. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +21 -2
  343. package/lib/esm/actions/requirements/encode/index.d.ts +5 -5
  344. package/lib/esm/actions/requirements/encode/index.js +5 -5
  345. package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +26 -11
  346. package/lib/esm/actions/requirements/getRequirementsApproval.js +38 -12
  347. package/lib/esm/actions/requirements/index.d.ts +1 -1
  348. package/lib/esm/actions/requirements/index.js +1 -1
  349. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.d.ts +11 -0
  350. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.js +31 -18
  351. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.d.ts +11 -0
  352. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +22 -10
  353. package/lib/esm/actions/requirements/midnight/index.d.ts +2 -2
  354. package/lib/esm/actions/requirements/midnight/index.js +2 -2
  355. package/lib/esm/actions/vaultV1/deposit.d.ts +21 -0
  356. package/lib/esm/actions/vaultV1/deposit.js +43 -9
  357. package/lib/esm/actions/vaultV1/inKindRedeem.d.ts +18 -1
  358. package/lib/esm/actions/vaultV1/inKindRedeem.js +33 -15
  359. package/lib/esm/actions/vaultV1/index.d.ts +5 -5
  360. package/lib/esm/actions/vaultV1/index.js +5 -5
  361. package/lib/esm/actions/vaultV1/migrateToV2.d.ts +21 -0
  362. package/lib/esm/actions/vaultV1/migrateToV2.js +47 -11
  363. package/lib/esm/actions/vaultV1/redeem.d.ts +19 -0
  364. package/lib/esm/actions/vaultV1/redeem.js +26 -7
  365. package/lib/esm/actions/vaultV1/withdraw.d.ts +19 -0
  366. package/lib/esm/actions/vaultV1/withdraw.js +26 -7
  367. package/lib/esm/actions/vaultV2/deposit.d.ts +21 -0
  368. package/lib/esm/actions/vaultV2/deposit.js +43 -9
  369. package/lib/esm/actions/vaultV2/forceRedeem.d.ts +20 -2
  370. package/lib/esm/actions/vaultV2/forceRedeem.js +35 -6
  371. package/lib/esm/actions/vaultV2/forceWithdraw.d.ts +23 -1
  372. package/lib/esm/actions/vaultV2/forceWithdraw.js +33 -13
  373. package/lib/esm/actions/vaultV2/inKindRedeem.d.ts +18 -1
  374. package/lib/esm/actions/vaultV2/inKindRedeem.js +32 -14
  375. package/lib/esm/actions/vaultV2/index.d.ts +6 -6
  376. package/lib/esm/actions/vaultV2/index.js +6 -6
  377. package/lib/esm/actions/vaultV2/redeem.d.ts +19 -0
  378. package/lib/esm/actions/vaultV2/redeem.js +26 -7
  379. package/lib/esm/actions/vaultV2/withdraw.d.ts +19 -0
  380. package/lib/esm/actions/vaultV2/withdraw.js +26 -7
  381. package/lib/esm/blue/abis.d.ts +1 -1
  382. package/lib/esm/blue/abis.js +1 -1
  383. package/lib/esm/blue/entities.d.ts +4 -4
  384. package/lib/esm/blue/entities.js +2 -2
  385. package/lib/esm/blue/errors.d.ts +2 -2
  386. package/lib/esm/blue/errors.js +2 -2
  387. package/lib/esm/blue/fetch.d.ts +2 -2
  388. package/lib/esm/blue/fetch.js +2 -2
  389. package/lib/esm/blue/utils.d.ts +1 -1
  390. package/lib/esm/blue/utils.js +1 -1
  391. package/lib/esm/entities/blue/blue.d.ts +35 -8
  392. package/lib/esm/entities/blue/blue.js +449 -388
  393. package/lib/esm/entities/index.d.ts +4 -4
  394. package/lib/esm/entities/index.js +2 -2
  395. package/lib/esm/entities/midnight/midnight.d.ts +117 -41
  396. package/lib/esm/entities/midnight/midnight.js +455 -826
  397. package/lib/esm/entities/midnight/parse.d.ts +74 -0
  398. package/lib/esm/entities/midnight/parse.js +320 -0
  399. package/lib/esm/entities/midnight/prepare.d.ts +47 -0
  400. package/lib/esm/entities/midnight/prepare.js +282 -0
  401. package/lib/esm/entities/midnight/publication.d.ts +7 -3
  402. package/lib/esm/entities/midnight/publication.js +7 -3
  403. package/lib/esm/entities/midnight/types.d.ts +7 -0
  404. package/lib/esm/entities/requirements/assertContractCode.d.ts +10 -5
  405. package/lib/esm/entities/requirements/assertContractCode.js +24 -15
  406. package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +15 -0
  407. package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +53 -47
  408. package/lib/esm/entities/requirements/getErc2612Permit.d.ts +7 -3
  409. package/lib/esm/entities/requirements/getErc2612Permit.js +19 -4
  410. package/lib/esm/entities/requirements/getUnusedPermit2Nonce.d.ts +15 -0
  411. package/lib/esm/entities/requirements/getUnusedPermit2Nonce.js +20 -5
  412. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +14 -0
  413. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +23 -5
  414. package/lib/esm/entities/requirements/getVaultV1InKindRedeemRequirements.js +4 -3
  415. package/lib/esm/entities/requirements/getVaultV2ForceWithdrawRequirements.js +4 -3
  416. package/lib/esm/entities/requirements/getVaultV2InKindRedeemRequirements.js +4 -3
  417. package/lib/esm/entities/requirements/index.d.ts +2 -2
  418. package/lib/esm/entities/requirements/index.js +2 -2
  419. package/lib/esm/entities/requirements/validateNativeRequirements.d.ts +21 -0
  420. package/lib/esm/entities/requirements/validateNativeRequirements.js +33 -0
  421. package/lib/esm/entities/vaultV1/inKindRedeemQuote.d.ts +2 -2
  422. package/lib/esm/entities/vaultV1/inKindRedeemQuote.js +1 -7
  423. package/lib/esm/entities/vaultV1/vaultV1.d.ts +17 -40
  424. package/lib/esm/entities/vaultV1/vaultV1.js +208 -139
  425. package/lib/esm/entities/vaultV2/forceWithdrawQuote.d.ts +4 -0
  426. package/lib/esm/entities/vaultV2/forceWithdrawQuote.js +10 -5
  427. package/lib/esm/entities/vaultV2/inKindRedeemQuote.d.ts +2 -2
  428. package/lib/esm/entities/vaultV2/inKindRedeemQuote.js +1 -7
  429. package/lib/esm/entities/vaultV2/vaultV2.d.ts +29 -86
  430. package/lib/esm/entities/vaultV2/vaultV2.js +210 -172
  431. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +2 -3
  432. package/lib/esm/entities/vaultV2BlueReallocationData.js +2 -6
  433. package/lib/esm/errors.d.ts +4 -4
  434. package/lib/esm/errors.js +4 -4
  435. package/lib/esm/fetch.d.ts +2 -2
  436. package/lib/esm/fetch.js +2 -2
  437. package/lib/esm/helpers/deadShares.js +1 -4
  438. package/lib/esm/helpers/parse.d.ts +33 -0
  439. package/lib/esm/helpers/parse.js +61 -0
  440. package/lib/esm/helpers/parseMidnight.d.ts +48 -0
  441. package/lib/esm/helpers/parseMidnight.js +80 -0
  442. package/lib/esm/helpers/parseMidnightMaker.d.ts +44 -0
  443. package/lib/esm/helpers/parseMidnightMaker.js +147 -0
  444. package/lib/esm/helpers/validate.d.ts +3 -2
  445. package/lib/esm/helpers/validate.js +5 -1
  446. package/lib/esm/helpers/validateMidnightCancelAndMakePublication.d.ts +8 -0
  447. package/lib/esm/helpers/validateMidnightCancelAndMakePublication.js +2 -2
  448. package/lib/esm/helpers/validateMidnightPublication.d.ts +17 -1
  449. package/lib/esm/index.d.ts +1 -0
  450. package/lib/esm/midnight/errors.d.ts +1 -2
  451. package/lib/esm/midnight/errors.js +1 -2
  452. package/lib/esm/transactionPlan/blue/refinance.js +18 -10
  453. package/lib/esm/transactionPlan/blue/repay.js +3 -2
  454. package/lib/esm/transactionPlan/blue/repayWithdrawCollateral.js +24 -5
  455. package/lib/esm/transactionPlan/blue/supplyCollateralBorrow.js +2 -2
  456. package/lib/esm/transactionPlan/blue/withdraw.d.ts +2 -2
  457. package/lib/esm/transactionPlan/blue/withdraw.js +3 -3
  458. package/lib/esm/transactionPlan/blue/withdrawCollateral.js +6 -2
  459. package/lib/esm/transactionPlan/validation.d.ts +1 -1
  460. package/lib/esm/transactionPlan/validation.js +3 -0
  461. package/lib/esm/transactionPlan/vaultv1/deposit.d.ts +2 -2
  462. package/lib/esm/transactionPlan/vaultv1/deposit.js +2 -2
  463. package/lib/esm/types/action.d.ts +13 -2
  464. package/lib/esm/types/error.d.ts +20 -2
  465. package/lib/esm/types/error.js +27 -0
  466. package/lib/esm/utils.d.ts +2 -2
  467. package/lib/esm/utils.js +2 -2
  468. package/lib/esm/version.d.ts +1 -1
  469. package/lib/esm/version.js +1 -1
  470. package/package.json +7 -7
@@ -8,22 +8,30 @@ const viem_1 = require("viem");
8
8
  const actions_1 = require("viem/actions");
9
9
  const abis_js_1 = require("../../abis.js");
10
10
  const common_js_1 = require("../../actions/blue/common.js");
11
+ const refinance_internal_js_1 = require("../../actions/blue/refinance.internal.js");
11
12
  const refinanceValidation_js_1 = require("../../actions/blue/refinanceValidation.js");
12
- const index_js_1 = require("../../actions/index.js");
13
- const validateClientChainId_js_1 = require("../../actions/requirements/validateClientChainId.js");
14
- const index_js_2 = require("../../helpers/index.js");
13
+ const repayWithdrawCollateral_internal_js_1 = require("../../actions/blue/repayWithdrawCollateral.internal.js");
14
+ const supply_internal_js_1 = require("../../actions/blue/supply.internal.js");
15
+ const supplyCollateralBorrow_internal_js_1 = require("../../actions/blue/supplyCollateralBorrow.internal.js");
16
+ const withdraw_internal_js_1 = require("../../actions/blue/withdraw.internal.js");
17
+ const withdrawBuyCallback_internal_js_1 = require("../../actions/blue/withdrawBuyCallback.internal.js");
18
+ const getBlueAuthorizationRequirement_js_1 = require("../../actions/requirements/blue/getBlueAuthorizationRequirement.js");
19
+ const chainCheck_js_1 = require("../../actions/requirements/chainCheck.js");
20
+ const index_js_1 = require("../../helpers/index.js");
15
21
  const metadata_js_1 = require("../../helpers/metadata.js");
22
+ const parse_js_1 = require("../../helpers/parse.js");
16
23
  const quoteBlueRepay_js_1 = require("../../helpers/quoteBlueRepay.js");
17
- const validate_js_1 = require("../../helpers/validate.js");
18
- const index_js_3 = require("../../types/index.js");
24
+ const index_js_2 = require("../../types/index.js");
19
25
  const assertContractCode_js_1 = require("../requirements/assertContractCode.js");
20
- const index_js_4 = require("../requirements/index.js");
26
+ const getBundlesTokenRequirements_js_1 = require("../requirements/getBundlesTokenRequirements.js");
27
+ const validateNativeRequirements_js_1 = require("../requirements/validateNativeRequirements.js");
21
28
  const vaultV2BlueReallocationData_js_1 = require("../vaultV2BlueReallocationData.js");
22
29
  /** Binds a viem client to a Morpho Blue market's action builders. */
23
30
  class MorphoBlue {
24
31
  client;
25
32
  marketParams;
26
33
  chainId;
34
+ parsedMarket;
27
35
  /**
28
36
  * Binds `client` to the market described by `marketParams` on `chainId`.
29
37
  *
@@ -37,21 +45,21 @@ class MorphoBlue {
37
45
  this.client = client;
38
46
  this.marketParams = marketParams;
39
47
  this.chainId = chainId;
40
- (0, validate_js_1.validateMarketParamsId)(marketParams);
48
+ this.parsedMarket = (0, parse_js_1.parseBlueMarket)(marketParams);
41
49
  }
42
50
  validateDeadline(deadline, now = morpho_ts_1.Time.timestamp()) {
43
51
  if (deadline <= now) {
44
- throw new index_js_3.ExpiredDeadlineError(deadline, now);
52
+ throw new index_js_2.ExpiredDeadlineError(deadline, now);
45
53
  }
46
54
  }
47
55
  validateWriteCommon(params, now) {
48
- (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
56
+ // Reject cross-chain inputs before continuing.
57
+ (0, index_js_1.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
58
+ // Reject expiry before continuing this call.
49
59
  this.validateDeadline(params.deadline, now);
50
60
  // Resolve at handle creation so unsupported deployments fail before any RPC prerequisite work.
51
61
  (0, morpho_ts_1.getChainAddress)(this.chainId, "bundles.blueBundlesV1");
52
- // Validate the referral inputs eagerly; the normalized result is intentionally discarded here
53
- // because the pure action re-normalizes it at encode time.
54
- (0, common_js_1.normalizeBlueBundlesV1CommonParams)({
62
+ return (0, common_js_1.parseBlueBundlesV1Common)({
55
63
  chainId: this.chainId,
56
64
  userAddress: params.userAddress,
57
65
  deadline: params.deadline,
@@ -62,10 +70,13 @@ class MorphoBlue {
62
70
  /**
63
71
  * Asserts that every registry contract this flow's transaction targets or names as
64
72
  * spender/authorized operator holds code, with one `eth_getCode` per distinct address
65
- * before any other requirement read. Blue write flows probe `eth_chainId` only after a target
66
- * has no code and report a chain mismatch when the RPC serves another chain.
73
+ * before any other requirement read. Native-only funding validates the served chain
74
+ * concurrently; other flows probe it here only when a target has no code.
75
+ *
76
+ * @param chainCheck - Chain validation shared within this operation.
77
+ * @param nativeFundingOnly - Whether no token or authorization resolver checks the chain.
67
78
  */
68
- async assertWriteTargets() {
79
+ async assertWriteTargets(chainCheck, nativeFundingOnly = false) {
69
80
  const targets = [
70
81
  {
71
82
  role: "bundles.blueBundlesV1",
@@ -73,27 +84,28 @@ class MorphoBlue {
73
84
  },
74
85
  { role: "blue", address: (0, morpho_ts_1.getChainAddress)(this.chainId, "blue") },
75
86
  ];
76
- await (0, assertContractCode_js_1.assertRegistryTargetsHaveCode)(this.client.viemClient, this.chainId, targets);
87
+ const assertTargets = () => (0, assertContractCode_js_1.assertRegistryTargetsHaveCode)(this.client.viemClient, this.chainId, targets, chainCheck);
88
+ if (nativeFundingOnly) {
89
+ await (0, validateNativeRequirements_js_1.validateNativeRequirements)(this.client.viemClient, {
90
+ chainId: this.chainId,
91
+ chainCheck,
92
+ assertWriteTargets: assertTargets,
93
+ });
94
+ }
95
+ else {
96
+ await assertTargets();
97
+ }
77
98
  }
78
- getMaxLtv(marketParams = this.marketParams) {
79
- return blue_sdk_1.MathLib.zeroFloorSub(marketParams.lltv, index_js_2.DEFAULT_LLTV_BUFFER);
99
+ getMaxLtv(marketParams = this.parsedMarket) {
100
+ return blue_sdk_1.MathLib.zeroFloorSub(marketParams.lltv, index_js_1.DEFAULT_LLTV_BUFFER);
80
101
  }
81
102
  getBlueBundlesV1QuoteTimestamp(lastUpdate, now = morpho_ts_1.Time.timestamp()) {
82
103
  return blue_sdk_1.MathLib.max(now, lastUpdate) + morpho_ts_1.Time.s.from.h(2n);
83
104
  }
84
- quoteRepay({ positionData, repayAssets, repayShares, deadline, now, }) {
85
- return (0, quoteBlueRepay_js_1.quoteBlueRepay)({
86
- positionData,
87
- marketId: this.marketParams.id,
88
- repayAssets,
89
- repayShares,
90
- deadline,
91
- now,
92
- });
93
- }
94
- async getTokenRequirements(params) {
105
+ async getTokenRequirements(chainCheck, params) {
106
+ // Reject expiry before continuing this call.
95
107
  this.validateDeadline(params.deadline, params.now);
96
- return (0, index_js_4.getBundlesTokenRequirements)(this.client.viemClient, {
108
+ return (0, getBundlesTokenRequirements_js_1.getParsedBundlesTokenRequirements)({ viemClient: this.client.viemClient, chainCheck }, (0, getBundlesTokenRequirements_js_1.parseGetBundlesTokenRequirements)(this.client.viemClient, {
97
109
  token: params.token,
98
110
  spender: (0, morpho_ts_1.getChainAddress)(this.chainId, "bundles.blueBundlesV1"),
99
111
  amount: params.amount,
@@ -104,11 +116,12 @@ class MorphoBlue {
104
116
  supportDeployless: this.client.options.supportDeployless,
105
117
  useSimplePermit: params.useSimplePermit,
106
118
  now: params.now,
107
- });
119
+ }));
108
120
  }
109
- async getAuthorizationRequirements(params) {
121
+ async getAuthorizationRequirements(chainCheck, params) {
122
+ // Reject expiry before continuing this call.
110
123
  this.validateDeadline(params.deadline, params.now);
111
- const requirement = await (0, index_js_1.getBlueAuthorizationRequirement)({
124
+ const requirement = await (0, getBlueAuthorizationRequirement_js_1.getParsedBlueAuthorizationRequirement)((0, getBlueAuthorizationRequirement_js_1.parseBlueAuthorizationRequirement)({
112
125
  viemClient: this.client.viemClient,
113
126
  chainId: this.chainId,
114
127
  userAddress: params.userAddress,
@@ -116,135 +129,165 @@ class MorphoBlue {
116
129
  authorized: (0, morpho_ts_1.getChainAddress)(this.chainId, "bundles.blueBundlesV1"),
117
130
  deadline: params.deadline,
118
131
  now: params.now,
119
- });
132
+ }), chainCheck);
120
133
  return requirement == null ? [] : [requirement];
121
134
  }
122
135
  async getMarketData(parameters) {
123
- await (0, validateClientChainId_js_1.validateClientChainId)(this.client.viemClient, this.chainId);
124
- return (0, blue_sdk_viem_1.fetchMarket)(this.marketParams.id, this.client.viemClient, {
136
+ // Confirm the RPC chain before contract reads.
137
+ await (0, chainCheck_js_1.createChainCheck)(this.client.viemClient, this.chainId)();
138
+ return (0, blue_sdk_viem_1.fetchMarket)(this.parsedMarket.id, this.client.viemClient, {
125
139
  ...parameters,
126
140
  deployless: this.client.options.supportDeployless,
127
141
  });
128
142
  }
129
143
  async getPositionData(userAddress, parameters) {
130
- await (0, validateClientChainId_js_1.validateClientChainId)(this.client.viemClient, this.chainId);
131
- return (0, blue_sdk_viem_1.fetchAccrualPosition)(userAddress, this.marketParams.id, this.client.viemClient, {
144
+ // Confirm the RPC chain before contract reads.
145
+ await (0, chainCheck_js_1.createChainCheck)(this.client.viemClient, this.chainId)();
146
+ return (0, blue_sdk_viem_1.fetchAccrualPosition)(userAddress, this.parsedMarket.id, this.client.viemClient, {
132
147
  ...parameters,
133
148
  deployless: this.client.options.supportDeployless,
134
149
  });
135
150
  }
136
151
  supply(params) {
137
- const suppliedNow = params.now;
138
- const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, suppliedNow);
139
- const { userAddress, assets, nativeAmount, deadline, referralFeePct, referralFeeRecipient, } = params;
140
- const createdAt = suppliedNow ?? morpho_ts_1.Time.timestamp();
141
- this.validateWriteCommon(params, createdAt);
152
+ return this.supplyPrepared(this.parseSupplyParams(params));
153
+ }
154
+ parseSupplyParams(params) {
155
+ const { assets } = params;
156
+ const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, params.now);
157
+ const createdAt = params.now ?? morpho_ts_1.Time.timestamp();
158
+ const normalized = this.validateWriteCommon(params, createdAt);
142
159
  if (assets <= 0n) {
143
- throw new index_js_3.NonPositiveInputError("assets", assets);
160
+ throw new index_js_2.NonPositiveInputError("assets", assets);
144
161
  }
145
- const nativeValue = (0, common_js_1.validateBlueBundlesV1NativeFunding)({
162
+ const nativeValue = (0, parse_js_1.parseNativeFunding)({
146
163
  chainId: this.chainId,
147
- token: this.marketParams.loanToken,
164
+ token: this.parsedMarket.loanToken,
148
165
  fundedAmount: assets,
149
- nativeAmount,
166
+ nativeAmount: params.nativeAmount,
150
167
  });
151
- if (params.marketData != null) {
152
- if (params.marketData.id !== this.marketParams.id) {
153
- throw new index_js_3.MarketIdMismatchError(params.marketData.id, this.marketParams.id);
154
- }
168
+ if (params.marketData != null &&
169
+ params.marketData.id !== this.parsedMarket.id) {
170
+ throw new index_js_2.MarketIdMismatchError(params.marketData.id, this.parsedMarket.id);
155
171
  }
172
+ const input = {
173
+ ...normalized,
174
+ chainId: this.chainId,
175
+ userAddress: params.userAddress,
176
+ marketParams: this.parsedMarket,
177
+ now: params.now,
178
+ metadata,
179
+ assets: (0, parse_js_1.parseUint256Max)("assets", assets),
180
+ value: nativeValue,
181
+ };
182
+ return input;
183
+ }
184
+ supplyPrepared(parsed) {
185
+ const { userAddress, assets, deadline, value: nativeValue, marketParams, } = parsed;
156
186
  return {
157
187
  getRequirements: (requirementsParams) => {
158
- const now = suppliedNow ?? morpho_ts_1.Time.timestamp();
188
+ const chainCheck = (0, chainCheck_js_1.createChainCheck)(this.client.viemClient, this.chainId);
189
+ const now = parsed.now ?? morpho_ts_1.Time.timestamp();
190
+ // Reject expiry before continuing this call.
159
191
  this.validateDeadline(deadline, now);
160
- return this.assertWriteTargets().then(() => {
161
- if (nativeValue > 0n) {
162
- return [];
163
- }
164
- return this.getTokenRequirements({
165
- token: this.marketParams.loanToken,
166
- amount: assets,
167
- userAddress,
168
- deadline,
169
- now,
170
- useSimplePermit: requirementsParams?.useSimplePermit,
171
- });
172
- });
192
+ if (nativeValue > 0n) {
193
+ // Neither token requirements nor authorization will validate the chain.
194
+ return this.assertWriteTargets(chainCheck, true).then(() => []);
195
+ }
196
+ return this.assertWriteTargets(chainCheck).then(() => this.getTokenRequirements(chainCheck, {
197
+ token: marketParams.loanToken,
198
+ amount: assets,
199
+ userAddress,
200
+ deadline,
201
+ now,
202
+ useSimplePermit: requirementsParams?.useSimplePermit,
203
+ }));
173
204
  },
174
205
  buildTx: (signatures) => {
175
206
  const { token } = (0, common_js_1.selectBlueBundlesV1RequirementSignatures)(signatures, {
176
207
  token: nativeValue === 0n,
177
208
  });
178
- return (0, index_js_1.blueSupply)({
179
- market: { chainId: this.chainId, marketParams: this.marketParams },
180
- args: {
181
- userAddress,
182
- assets,
183
- nativeAmount: nativeValue > 0n ? nativeValue : undefined,
184
- deadline,
185
- referralFeePct,
186
- referralFeeRecipient,
187
- requirementSignature: token,
188
- },
189
- metadata,
190
- });
209
+ return (0, supply_internal_js_1.buildBlueSupply)(parsed, { requirementSignature: token });
191
210
  },
192
211
  };
193
212
  }
194
213
  /** {@inheritDoc BlueActions.withdraw} */
195
214
  withdraw(params) {
196
- const suppliedNow = params.now;
197
- const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, suppliedNow);
198
- const { userAddress, positionData, deadline, referralFeePct, referralFeeRecipient, } = params;
199
- const createdAt = suppliedNow ?? morpho_ts_1.Time.timestamp();
200
- this.validateWriteCommon(params, createdAt);
215
+ return this.withdrawPrepared(this.parseWithdrawParams(params));
216
+ }
217
+ parseWithdrawParams(params) {
218
+ const { userAddress, positionData } = params;
219
+ const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, params.now);
220
+ const createdAt = params.now ?? morpho_ts_1.Time.timestamp();
221
+ const normalized = this.validateWriteCommon(params, createdAt);
201
222
  const assets = ("assets" in params ? params.assets : undefined) ?? 0n;
202
223
  const shares = ("shares" in params ? params.shares : undefined) ?? 0n;
203
- if (assets < 0n) {
204
- throw new index_js_3.NegativeInputError("assets", assets);
205
- }
206
- if (shares < 0n) {
207
- throw new index_js_3.NegativeInputError("shares", shares);
208
- }
209
- if (assets > 0n && shares > 0n) {
210
- throw new index_js_3.MutuallyExclusiveWithdrawAmountsError(this.marketParams.id);
211
- }
212
- if (assets === 0n && shares === 0n) {
213
- throw new index_js_3.NonPositiveInputError("assets or shares", 0n);
214
- }
224
+ const lowerAssets = (0, parse_js_1.parseNonnegative)("assets", assets);
225
+ const lowerShares = (0, parse_js_1.parseNonnegative)("shares", shares);
226
+ const mode = (0, withdraw_internal_js_1.parseBlueWithdrawMode)(this.parsedMarket, {
227
+ withdrawAssets: assets,
228
+ withdrawShares: shares,
229
+ field: "assets or shares",
230
+ });
215
231
  if (positionData == null) {
216
- throw new index_js_3.MissingAccrualPositionError(this.marketParams.id);
232
+ throw new index_js_2.MissingAccrualPositionError(this.parsedMarket.id);
217
233
  }
218
- (0, index_js_2.validateAccrualPosition)({
234
+ // Reject a snapshot for another user or market.
235
+ (0, index_js_1.validateAccrualPosition)({
219
236
  positionData,
220
- expectedMarketId: this.marketParams.id,
237
+ expectedMarketId: this.parsedMarket.id,
221
238
  expectedUser: userAddress,
222
239
  });
223
- const validateWithdrawal = shares > 0n ? index_js_2.validateWithdrawShares : index_js_2.validateWithdrawAmount;
224
- validateWithdrawal({
225
- positionData,
226
- withdrawShares: shares,
227
- withdrawAssets: assets,
228
- marketId: this.marketParams.id,
229
- });
230
- const reallocations = [...(params.reallocations ?? [])];
231
- // Validate the exact PublicAllocations mapping before requirement reads.
232
- const publicAllocations = (0, common_js_1.getBlueBundlesV1PublicAllocations)(reallocations, this.marketParams);
233
- const reallocationPenaltyAssets = (0, common_js_1.getBlueBundlesV1PenaltyAssets)(publicAllocations);
240
+ if (shares > 0n) {
241
+ // Reject a withdrawal larger than the supplied share balance.
242
+ (0, index_js_1.validateWithdrawShares)({
243
+ positionData,
244
+ withdrawShares: shares,
245
+ marketId: this.parsedMarket.id,
246
+ });
247
+ }
248
+ else {
249
+ // Reject a withdrawal larger than the supplied asset balance.
250
+ (0, index_js_1.validateWithdrawAmount)({
251
+ positionData,
252
+ withdrawAssets: assets,
253
+ marketId: this.parsedMarket.id,
254
+ });
255
+ }
256
+ const allocations = (0, withdraw_internal_js_1.parseBlueWithdrawPenalty)((0, common_js_1.parseBlueAllocations)([...(params.reallocations ?? [])], this.parsedMarket), mode);
257
+ const reallocationPenaltyAssets = allocations.penaltyAssets;
234
258
  const expectedWithdrawAssets = assets > 0n ? assets : positionData.market.toSupplyAssets(shares, "Down");
235
- if (reallocationPenaltyAssets > expectedWithdrawAssets) {
236
- throw new index_js_3.InputExceedsMaxError({
259
+ if (shares > 0n && reallocationPenaltyAssets > expectedWithdrawAssets) {
260
+ throw new index_js_2.InputExceedsMaxError({
237
261
  field: "reallocationPenaltyAssets",
238
262
  value: reallocationPenaltyAssets,
239
263
  max: expectedWithdrawAssets,
240
264
  });
241
265
  }
266
+ const input = {
267
+ ...normalized,
268
+ chainId: this.chainId,
269
+ userAddress,
270
+ marketParams: this.parsedMarket,
271
+ now: params.now,
272
+ metadata,
273
+ publicAllocations: allocations.publicAllocations,
274
+ reallocationPenaltyAssets,
275
+ withdrawAssets: (0, parse_js_1.parseUint256Max)("withdrawAssets", lowerAssets),
276
+ withdrawShares: (0, parse_js_1.parseUint256Max)("withdrawShares", lowerShares),
277
+ };
278
+ return input;
279
+ }
280
+ withdrawPrepared(parsed) {
281
+ const { userAddress, deadline } = parsed;
242
282
  return {
243
283
  getRequirements: async () => {
244
- const now = suppliedNow ?? morpho_ts_1.Time.timestamp();
284
+ const chainCheck = (0, chainCheck_js_1.createChainCheck)(this.client.viemClient, this.chainId);
285
+ const now = parsed.now ?? morpho_ts_1.Time.timestamp();
286
+ // Reject expiry before continuing this call.
245
287
  this.validateDeadline(deadline, now);
246
- await this.assertWriteTargets();
247
- return this.getAuthorizationRequirements({
288
+ // Reject missing write targets before resolving requirements.
289
+ await this.assertWriteTargets(chainCheck);
290
+ return this.getAuthorizationRequirements(chainCheck, {
248
291
  userAddress,
249
292
  deadline,
250
293
  now,
@@ -254,67 +297,76 @@ class MorphoBlue {
254
297
  const { authorization } = (0, common_js_1.selectBlueBundlesV1RequirementSignatures)(signatures, {
255
298
  authorization: true,
256
299
  });
257
- return (0, index_js_1.blueWithdraw)({
258
- market: { chainId: this.chainId, marketParams: this.marketParams },
259
- args: {
260
- userAddress,
261
- withdrawAssets: assets,
262
- withdrawShares: shares,
263
- reallocations,
264
- deadline,
265
- referralFeePct,
266
- referralFeeRecipient,
267
- authorizationSignature: authorization,
268
- },
269
- metadata,
300
+ return (0, withdraw_internal_js_1.buildBlueWithdraw)(parsed, {
301
+ authorizationSignature: authorization,
270
302
  });
271
303
  },
272
304
  };
273
305
  }
274
306
  /** {@inheritDoc BlueActions.withdrawBuyCallback} */
275
307
  withdrawBuyCallback(params) {
308
+ return this.withdrawBuyCallbackPrepared(this.parseWithdrawBuyCallbackParams(params));
309
+ }
310
+ parseWithdrawBuyCallbackParams(params) {
276
311
  const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, params.now);
277
312
  const { userAddress, callbackAddress, positionData } = params;
278
- (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
313
+ // Reject cross-chain inputs before continuing.
314
+ (0, index_js_1.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
279
315
  if (positionData == null) {
280
- throw new index_js_3.MissingAccrualPositionError(this.marketParams.id);
316
+ throw new index_js_2.MissingAccrualPositionError(this.parsedMarket.id);
281
317
  }
282
- (0, index_js_2.validateAccrualPosition)({
318
+ // Reject a snapshot for another user or market.
319
+ (0, index_js_1.validateAccrualPosition)({
283
320
  positionData,
284
- expectedMarketId: this.marketParams.id,
321
+ expectedMarketId: this.parsedMarket.id,
285
322
  expectedUser: callbackAddress,
286
323
  });
287
324
  const shares = positionData.supplyShares;
288
325
  if (shares === 0n) {
289
- throw new index_js_3.EmptyBlueSupplyPositionError({
290
- market: this.marketParams.id,
326
+ throw new index_js_2.EmptyBlueSupplyPositionError({
327
+ market: this.parsedMarket.id,
291
328
  user: callbackAddress,
292
329
  });
293
330
  }
294
331
  const assets = positionData.supplyAssets;
295
332
  const liquidity = positionData.market.liquidity;
296
333
  if (assets > liquidity) {
297
- throw new index_js_3.InsufficientBlueLiquidityError({
298
- market: this.marketParams.id,
334
+ throw new index_js_2.InsufficientBlueLiquidityError({
335
+ market: this.parsedMarket.id,
299
336
  assets,
300
337
  liquidity,
301
338
  });
302
339
  }
303
340
  const blue = (0, morpho_ts_1.getChainAddress)(this.chainId, "blue");
304
341
  const factory = (0, morpho_ts_1.getChainAddress)(this.chainId, "midnightBlueBuyCallbackFactory");
305
- const tx = (0, index_js_1.blueWithdrawBuyCallback)({
306
- market: { chainId: this.chainId, marketParams: this.marketParams },
307
- args: { callbackAddress, userAddress, shares },
342
+ const boundedShares = (0, parse_js_1.parseUint256)("shares", shares);
343
+ if (boundedShares === 0n) {
344
+ throw new index_js_2.NonPositiveInputError("shares", boundedShares);
345
+ }
346
+ return {
347
+ marketParams: this.parsedMarket,
348
+ callbackAddress,
349
+ userAddress,
350
+ shares: boundedShares,
308
351
  metadata,
309
- });
352
+ to: blue,
353
+ factory,
354
+ };
355
+ }
356
+ withdrawBuyCallbackPrepared(parsed) {
357
+ const { userAddress, callbackAddress, factory, to: blue } = parsed;
358
+ const tx = (0, withdrawBuyCallback_internal_js_1.buildBlueWithdrawBuyCallback)(parsed);
310
359
  return {
311
360
  getRequirements: async () => {
361
+ const chainCheck = (0, chainCheck_js_1.createChainCheck)(this.client.viemClient, this.chainId);
312
362
  const viemClient = this.client.viemClient;
313
- await (0, validateClientChainId_js_1.validateClientChainId)(viemClient, this.chainId);
363
+ // Confirm the RPC chain before prerequisite reads.
364
+ await chainCheck();
365
+ // Reject missing registry contracts before prerequisite reads.
314
366
  await (0, assertContractCode_js_1.assertRegistryTargetsHaveCode)(viemClient, this.chainId, [
315
367
  { role: "blue", address: blue },
316
368
  { role: "midnightBlueBuyCallbackFactory", address: factory },
317
- ]);
369
+ ], chainCheck);
318
370
  // Check the factory first: OWNER() on an arbitrary address can revert or lie.
319
371
  const isBlueBuyCallback = await (0, actions_1.readContract)(viemClient, {
320
372
  address: factory,
@@ -323,7 +375,7 @@ class MorphoBlue {
323
375
  args: [callbackAddress],
324
376
  });
325
377
  if (!isBlueBuyCallback) {
326
- throw new index_js_3.NotBlueBuyCallbackError(callbackAddress);
378
+ throw new index_js_2.NotBlueBuyCallbackError(callbackAddress);
327
379
  }
328
380
  const [owner, isAuthorized] = await Promise.all([
329
381
  (0, actions_1.readContract)(viemClient, {
@@ -339,14 +391,14 @@ class MorphoBlue {
339
391
  }),
340
392
  ]);
341
393
  if (!(0, viem_1.isAddressEqual)(owner, userAddress)) {
342
- throw new index_js_3.BlueBuyCallbackOwnerMismatchError({
394
+ throw new index_js_2.BlueBuyCallbackOwnerMismatchError({
343
395
  callback: callbackAddress,
344
396
  owner,
345
397
  userAddress,
346
398
  });
347
399
  }
348
400
  if (!isAuthorized) {
349
- throw new index_js_3.BlueBuyCallbackUnauthorizedError({
401
+ throw new index_js_2.BlueBuyCallbackUnauthorizedError({
350
402
  callback: callbackAddress,
351
403
  userAddress,
352
404
  });
@@ -358,183 +410,173 @@ class MorphoBlue {
358
410
  }
359
411
  /** {@inheritDoc BlueActions.supplyCollateral} */
360
412
  supplyCollateral(params) {
361
- const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, params.now);
413
+ return this.supplyCollateralPrepared(this.parseSupplyCollateralParams(params));
414
+ }
415
+ parseSupplyCollateralParams(params) {
362
416
  const { userAddress, collateralAssets, nativeAmount, deadline, referralFeePct, referralFeeRecipient, } = params;
363
- const combinedAction = this.supplyCollateralBorrow({
364
- now: params.now,
365
- userAddress,
366
- collateralAssets,
417
+ return {
418
+ ...this.parseSupplyCollateralBorrowParams({
419
+ now: params.now,
420
+ userAddress,
421
+ collateralAssets,
422
+ borrowAssets: 0n,
423
+ nativeAmount,
424
+ deadline,
425
+ referralFeePct,
426
+ referralFeeRecipient,
427
+ }),
367
428
  borrowAssets: 0n,
368
- nativeAmount,
369
- deadline,
370
- referralFeePct,
371
- referralFeeRecipient,
372
- });
429
+ };
430
+ }
431
+ supplyCollateralPrepared(parsed) {
432
+ const combinedAction = this.supplyCollateralBorrowPrepared(parsed);
373
433
  return {
374
434
  getRequirements: combinedAction.getRequirements,
375
435
  buildTx: (signatures) => {
376
- const { token } = (0, common_js_1.selectBlueBundlesV1RequirementSignatures)(signatures, {
377
- token: (nativeAmount ?? 0n) === 0n,
378
- });
379
- return (0, index_js_1.blueSupplyCollateral)({
380
- market: { chainId: this.chainId, marketParams: this.marketParams },
381
- args: {
382
- userAddress,
383
- collateralAssets,
384
- nativeAmount,
385
- deadline,
386
- referralFeePct,
387
- referralFeeRecipient,
388
- requirementSignature: token,
436
+ const transaction = combinedAction.buildTx(signatures);
437
+ return (0, morpho_ts_1.deepFreeze)({
438
+ ...transaction,
439
+ action: {
440
+ ...transaction.action,
441
+ type: "blueSupplyCollateral",
389
442
  },
390
- metadata,
391
443
  });
392
444
  },
393
445
  };
394
446
  }
395
447
  /** {@inheritDoc BlueActions.borrow} */
396
448
  borrow(params) {
397
- const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, params.now);
449
+ return this.borrowPrepared(this.parseBorrowParams(params));
450
+ }
451
+ parseBorrowParams(params) {
398
452
  const { userAddress, borrowAssets, positionData, reallocations, deadline, referralFeePct, referralFeeRecipient, } = params;
399
453
  const normalizedReallocations = [...(reallocations ?? [])];
400
- const combinedAction = this.supplyCollateralBorrow({
401
- now: params.now,
402
- userAddress,
454
+ return {
455
+ ...this.parseSupplyCollateralBorrowParams({
456
+ now: params.now,
457
+ userAddress,
458
+ collateralAssets: 0n,
459
+ borrowAssets,
460
+ positionData,
461
+ reallocations: normalizedReallocations,
462
+ deadline,
463
+ referralFeePct,
464
+ referralFeeRecipient,
465
+ }),
403
466
  collateralAssets: 0n,
404
- borrowAssets,
405
- positionData,
406
- reallocations: normalizedReallocations,
407
- deadline,
408
- referralFeePct,
409
- referralFeeRecipient,
410
- });
467
+ };
468
+ }
469
+ borrowPrepared(parsed) {
470
+ const combinedAction = this.supplyCollateralBorrowPrepared(parsed);
411
471
  return {
412
472
  getRequirements: () => combinedAction.getRequirements(),
413
473
  buildTx: (signatures) => {
414
- const { authorization } = (0, common_js_1.selectBlueBundlesV1RequirementSignatures)(signatures, { authorization: true });
415
- return (0, index_js_1.blueBorrow)({
416
- market: { chainId: this.chainId, marketParams: this.marketParams },
417
- args: {
418
- userAddress,
419
- borrowAssets,
420
- maxLtv: this.getMaxLtv(),
421
- reallocations: normalizedReallocations,
422
- deadline,
423
- referralFeePct,
424
- referralFeeRecipient,
425
- authorizationSignature: authorization,
426
- },
427
- metadata,
474
+ const transaction = combinedAction.buildTx(signatures);
475
+ return (0, morpho_ts_1.deepFreeze)({
476
+ ...transaction,
477
+ action: { ...transaction.action, type: "blueBorrow" },
428
478
  });
429
479
  },
430
480
  };
431
481
  }
432
482
  /** {@inheritDoc BlueActions.repay} */
433
483
  repay(params) {
434
- const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, params.now);
435
- const combinedAction = this.repayWithdrawCollateral({
436
- ...params,
484
+ return this.repayPrepared(this.parseRepayParams(params));
485
+ }
486
+ parseRepayParams(params) {
487
+ return {
488
+ ...this.parseRepayWithdrawCollateralParams({
489
+ now: params.now,
490
+ ...params,
491
+ collateralAssets: 0n,
492
+ }),
437
493
  collateralAssets: 0n,
438
- });
494
+ };
495
+ }
496
+ repayPrepared(parsed) {
497
+ const combinedAction = this.repayWithdrawCollateralPrepared(parsed);
439
498
  return {
440
499
  getRequirements: combinedAction.getRequirements,
441
500
  buildTx: (signatures) => {
442
- const combinedTransaction = combinedAction.buildTx(signatures);
443
- const { token } = (0, common_js_1.selectBlueBundlesV1RequirementSignatures)(signatures, {
444
- token: combinedTransaction.value === 0n,
445
- });
446
- const { repayAssets, repayShares, maxRepayAssets, nativeAmount, referralFeePct, referralFeeRecipient, deadline, } = combinedTransaction.action.args;
447
- return (0, index_js_1.blueRepay)({
448
- market: { chainId: this.chainId, marketParams: this.marketParams },
449
- args: {
450
- userAddress: params.userAddress,
451
- repayAssets,
452
- repayShares,
453
- maxRepayAssets,
454
- nativeAmount,
455
- deadline,
456
- referralFeePct,
457
- referralFeeRecipient,
458
- requirementSignature: token,
459
- },
460
- metadata,
501
+ const transaction = combinedAction.buildTx(signatures);
502
+ return (0, morpho_ts_1.deepFreeze)({
503
+ ...transaction,
504
+ action: { ...transaction.action, type: "blueRepay" },
461
505
  });
462
506
  },
463
507
  };
464
508
  }
465
509
  /** {@inheritDoc BlueActions.withdrawCollateral} */
466
510
  withdrawCollateral(params) {
467
- const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, params.now);
511
+ return this.withdrawCollateralPrepared(this.parseWithdrawCollateralParams(params));
512
+ }
513
+ parseWithdrawCollateralParams(params) {
468
514
  const { userAddress, positionData, collateralAssets, deadline, referralFeePct, referralFeeRecipient, } = params;
469
- const combinedAction = this.repayWithdrawCollateral({
470
- now: params.now,
471
- userAddress,
472
- positionData,
515
+ return {
516
+ ...this.parseRepayWithdrawCollateralParams({
517
+ now: params.now,
518
+ userAddress,
519
+ positionData,
520
+ repayAssets: 0n,
521
+ collateralAssets,
522
+ deadline,
523
+ referralFeePct,
524
+ referralFeeRecipient,
525
+ }),
473
526
  repayAssets: 0n,
474
- collateralAssets,
475
- deadline,
476
- referralFeePct,
477
- referralFeeRecipient,
478
- });
527
+ repayShares: 0n,
528
+ maxRepayAssets: 0n,
529
+ hasRepay: false,
530
+ };
531
+ }
532
+ withdrawCollateralPrepared(parsed) {
533
+ const combinedAction = this.repayWithdrawCollateralPrepared(parsed);
479
534
  return {
480
535
  getRequirements: () => combinedAction.getRequirements(),
481
536
  buildTx: (signatures) => {
482
- const combinedTransaction = combinedAction.buildTx(signatures);
483
- const { authorization } = (0, common_js_1.selectBlueBundlesV1RequirementSignatures)(signatures, { authorization: true });
484
- const combinedArgs = combinedTransaction.action.args;
485
- return (0, index_js_1.blueWithdrawCollateral)({
486
- market: { chainId: this.chainId, marketParams: this.marketParams },
487
- args: {
488
- userAddress,
489
- collateralAssets: combinedArgs.collateralAssets,
490
- maxLtv: combinedArgs.maxLtv,
491
- deadline: combinedArgs.deadline,
492
- referralFeePct: combinedArgs.referralFeePct,
493
- referralFeeRecipient: combinedArgs.referralFeeRecipient,
494
- authorizationSignature: authorization,
537
+ const transaction = combinedAction.buildTx(signatures);
538
+ return (0, morpho_ts_1.deepFreeze)({
539
+ ...transaction,
540
+ action: {
541
+ ...transaction.action,
542
+ type: "blueWithdrawCollateral",
495
543
  },
496
- metadata,
497
544
  });
498
545
  },
499
546
  };
500
547
  }
501
548
  /** {@inheritDoc BlueActions.repayWithdrawCollateral} */
502
549
  repayWithdrawCollateral(params) {
503
- const suppliedNow = params.now;
504
- const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, suppliedNow);
505
- const { userAddress, positionData, collateralAssets, nativeAmount, deadline, referralFeeRecipient, } = params;
506
- const createdAt = suppliedNow ?? morpho_ts_1.Time.timestamp();
507
- this.validateWriteCommon(params, createdAt);
508
- // `validateWriteCommon` returns void, so default an omitted `referralFeePct` to `0n`
509
- // here, matching the pure action's normalization.
510
- const referralFeePct = params.referralFeePct ?? 0n;
550
+ return this.repayWithdrawCollateralPrepared(this.parseRepayWithdrawCollateralParams(params));
551
+ }
552
+ parseRepayWithdrawCollateralParams(params) {
553
+ const { userAddress, positionData, collateralAssets, nativeAmount, deadline, } = params;
554
+ const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, params.now);
555
+ const createdAt = params.now ?? morpho_ts_1.Time.timestamp();
556
+ const normalized = this.validateWriteCommon(params, createdAt);
511
557
  const repayAssets = params.repayAssets ?? 0n;
512
558
  const repayShares = params.repayShares ?? 0n;
513
- if (repayAssets < 0n) {
514
- throw new index_js_3.NegativeInputError("repayAssets", repayAssets);
515
- }
516
- if (repayShares < 0n) {
517
- throw new index_js_3.NegativeInputError("repayShares", repayShares);
518
- }
519
- if (collateralAssets < 0n) {
520
- throw new index_js_3.NegativeInputError("collateralAssets", collateralAssets);
521
- }
522
- if (repayAssets > 0n && repayShares > 0n) {
523
- throw new index_js_3.MutuallyExclusiveRepayAmountsError(this.marketParams.id);
524
- }
559
+ const lowerRepayAssets = (0, parse_js_1.parseNonnegative)("repayAssets", repayAssets);
560
+ const lowerRepayShares = (0, parse_js_1.parseNonnegative)("repayShares", repayShares);
561
+ const lowerCollateralAssets = (0, parse_js_1.parseNonnegative)("collateralAssets", collateralAssets);
562
+ const mode = (0, repayWithdrawCollateral_internal_js_1.parseBlueRepayMode)(this.parsedMarket, {
563
+ repayAssets,
564
+ repayShares,
565
+ collateralAssets,
566
+ });
567
+ const referralFeePct = normalized.referralFeePct;
525
568
  const hasRepay = repayAssets > 0n || repayShares > 0n;
526
- if (!hasRepay && collateralAssets === 0n) {
527
- throw new index_js_3.NonPositiveInputError("repayAssets, repayShares, or collateralAssets", 0n);
528
- }
529
569
  if (positionData == null) {
530
- throw new index_js_3.MissingAccrualPositionError(this.marketParams.id);
570
+ throw new index_js_2.MissingAccrualPositionError(this.parsedMarket.id);
531
571
  }
532
- (0, index_js_2.validateAccrualPosition)({
572
+ // Reject a snapshot for another user or market.
573
+ (0, index_js_1.validateAccrualPosition)({
533
574
  positionData,
534
- expectedMarketId: this.marketParams.id,
575
+ expectedMarketId: this.parsedMarket.id,
535
576
  expectedUser: userAddress,
536
577
  });
537
- const { simulatedRepayShares, accruedPosition, expectedRepayAssets } = this.quoteRepay({
578
+ const { simulatedRepayShares, accruedPosition, expectedRepayAssets } = (0, quoteBlueRepay_js_1.quoteBlueRepay)({
579
+ marketId: this.parsedMarket.id,
538
580
  positionData,
539
581
  repayAssets,
540
582
  repayShares,
@@ -543,9 +585,9 @@ class MorphoBlue {
543
585
  });
544
586
  const referralFeeAssets = (0, common_js_1.getBlueBundlesV1ReferralFeeAssets)(expectedRepayAssets, referralFeePct);
545
587
  const maxRepayAssets = expectedRepayAssets + referralFeeAssets;
546
- const nativeValue = (0, common_js_1.validateBlueBundlesV1NativeFunding)({
588
+ const nativeValue = (0, parse_js_1.parseNativeFunding)({
547
589
  chainId: this.chainId,
548
- token: this.marketParams.loanToken,
590
+ token: this.parsedMarket.loanToken,
549
591
  fundedAmount: maxRepayAssets,
550
592
  nativeAmount,
551
593
  });
@@ -553,22 +595,52 @@ class MorphoBlue {
553
595
  const postRepayPosition = hasRepay
554
596
  ? accruedPosition.repay(repayAssets, simulatedRepayShares).position
555
597
  : accruedPosition;
556
- (0, index_js_2.validatePositionHealthAfterWithdraw)({
598
+ // Reject collateral withdrawal that would leave unsafe debt.
599
+ (0, index_js_1.validatePositionHealthAfterWithdraw)({
557
600
  positionData: postRepayPosition,
558
601
  withdrawAmount: collateralAssets,
559
- lltv: this.marketParams.lltv,
560
- marketId: this.marketParams.id,
602
+ lltv: this.parsedMarket.lltv,
603
+ marketId: this.parsedMarket.id,
561
604
  });
562
605
  }
606
+ const legs = {
607
+ ...normalized,
608
+ chainId: this.chainId,
609
+ userAddress,
610
+ marketParams: this.parsedMarket,
611
+ now: params.now,
612
+ metadata,
613
+ repayAssets: (0, parse_js_1.parseUint256Max)("repayAssets", lowerRepayAssets),
614
+ repayShares: (0, parse_js_1.parseUint256Max)("repayShares", lowerRepayShares),
615
+ collateralAssets: (0, parse_js_1.parseUint256Max)("collateralAssets", lowerCollateralAssets),
616
+ maxLtv: (0, parse_js_1.parseUint256)("maxLtv", collateralAssets > 0n ? this.getMaxLtv() : viem_1.maxUint256),
617
+ hasRepay,
618
+ minimumRepayAssets: repayAssets +
619
+ (0, common_js_1.getBlueBundlesV1ReferralFeeAssets)(repayAssets, referralFeePct),
620
+ };
621
+ const cap = (0, repayWithdrawCollateral_internal_js_1.parseBlueRepayCap)(mode, (0, parse_js_1.parseUint256)("maxRepayAssets", maxRepayAssets));
622
+ const coveredCap = (0, repayWithdrawCollateral_internal_js_1.parseBlueRepayCoverage)(legs, cap);
623
+ const input = {
624
+ ...legs,
625
+ maxRepayAssets: coveredCap,
626
+ value: nativeValue,
627
+ };
628
+ return input;
629
+ }
630
+ repayWithdrawCollateralPrepared(parsed) {
631
+ const { userAddress, collateralAssets, deadline, hasRepay, maxRepayAssets, value: nativeValue, marketParams, } = parsed;
563
632
  return {
564
633
  getRequirements: async (requirementsParams) => {
565
- const now = suppliedNow ?? morpho_ts_1.Time.timestamp();
634
+ const chainCheck = (0, chainCheck_js_1.createChainCheck)(this.client.viemClient, this.chainId);
635
+ const now = parsed.now ?? morpho_ts_1.Time.timestamp();
636
+ // Reject expiry before continuing this call.
566
637
  this.validateDeadline(deadline, now);
567
- await this.assertWriteTargets();
638
+ const nativeOnly = nativeValue > 0n && collateralAssets === 0n;
639
+ await this.assertWriteTargets(chainCheck, nativeOnly);
568
640
  const [tokenRequirements, authorizationRequirements] = await Promise.all([
569
641
  hasRepay && nativeValue === 0n
570
- ? this.getTokenRequirements({
571
- token: this.marketParams.loanToken,
642
+ ? this.getTokenRequirements(chainCheck, {
643
+ token: marketParams.loanToken,
572
644
  amount: maxRepayAssets,
573
645
  userAddress,
574
646
  deadline,
@@ -577,7 +649,7 @@ class MorphoBlue {
577
649
  })
578
650
  : Promise.resolve([]),
579
651
  collateralAssets > 0n
580
- ? this.getAuthorizationRequirements({
652
+ ? this.getAuthorizationRequirements(chainCheck, {
581
653
  userAddress,
582
654
  deadline,
583
655
  now,
@@ -591,95 +663,89 @@ class MorphoBlue {
591
663
  token: hasRepay && nativeValue === 0n,
592
664
  authorization: collateralAssets > 0n,
593
665
  });
594
- const selectedMaxRepayAssets = repayShares > 0n && token != null
595
- ? token.args.amount
596
- : maxRepayAssets;
597
- if (selectedMaxRepayAssets < maxRepayAssets) {
598
- throw new index_js_3.MaxRepayAssetsBelowRepayAssetsError(selectedMaxRepayAssets, maxRepayAssets);
599
- }
600
- return (0, index_js_1.blueRepayWithdrawCollateral)({
601
- market: { chainId: this.chainId, marketParams: this.marketParams },
602
- args: {
603
- userAddress,
604
- repayAssets,
605
- repayShares,
606
- maxRepayAssets: selectedMaxRepayAssets,
607
- collateralAssets,
608
- maxLtv: collateralAssets > 0n ? this.getMaxLtv() : viem_1.maxUint256,
609
- nativeAmount: nativeValue > 0n ? nativeValue : undefined,
610
- deadline,
611
- referralFeePct,
612
- referralFeeRecipient,
613
- requirementSignature: token,
614
- authorizationSignature: authorization,
615
- },
616
- metadata,
666
+ return (0, repayWithdrawCollateral_internal_js_1.buildBlueRepayWithdrawCollateral)(parsed, {
667
+ requirementSignature: token,
668
+ authorizationSignature: authorization,
669
+ signatureFunding: true,
617
670
  });
618
671
  },
619
672
  };
620
673
  }
621
674
  /** {@inheritDoc BlueActions.supplyCollateralBorrow} */
622
675
  supplyCollateralBorrow(params) {
623
- const suppliedNow = params.now;
624
- const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, suppliedNow);
625
- const { userAddress, collateralAssets, borrowAssets, positionData, nativeAmount, deadline, referralFeePct, referralFeeRecipient, } = params;
626
- const createdAt = suppliedNow ?? morpho_ts_1.Time.timestamp();
627
- this.validateWriteCommon(params, createdAt);
628
- if (collateralAssets < 0n) {
629
- throw new index_js_3.NegativeInputError("collateralAssets", collateralAssets);
630
- }
631
- if (borrowAssets < 0n) {
632
- throw new index_js_3.NegativeInputError("borrowAssets", borrowAssets);
633
- }
676
+ return this.supplyCollateralBorrowPrepared(this.parseSupplyCollateralBorrowParams(params));
677
+ }
678
+ parseSupplyCollateralBorrowParams(params) {
679
+ const { userAddress, collateralAssets, borrowAssets, positionData } = params;
680
+ const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, params.now);
681
+ const createdAt = params.now ?? morpho_ts_1.Time.timestamp();
682
+ const normalized = this.validateWriteCommon(params, createdAt);
683
+ const lowerCollateralAssets = (0, parse_js_1.parseNonnegative)("collateralAssets", collateralAssets);
684
+ const lowerBorrowAssets = (0, parse_js_1.parseNonnegative)("borrowAssets", borrowAssets);
634
685
  if (collateralAssets === 0n && borrowAssets === 0n) {
635
- throw new index_js_3.NonPositiveInputError("collateralAssets or borrowAssets", 0n);
636
- }
637
- const reallocations = [...(params.reallocations ?? [])];
638
- if (borrowAssets === 0n && reallocations.length > 0) {
639
- throw new index_js_3.ReallocationsRequireBorrowError();
640
- }
641
- // Validate the exact PublicAllocations mapping before requirement reads.
642
- const publicAllocations = (0, common_js_1.getBlueBundlesV1PublicAllocations)(reallocations, this.marketParams);
643
- const reallocationPenaltyAssets = (0, common_js_1.getBlueBundlesV1PenaltyAssets)(publicAllocations);
644
- if (reallocationPenaltyAssets > borrowAssets) {
645
- throw new index_js_3.InputExceedsMaxError({
646
- field: "reallocationPenaltyAssets",
647
- value: reallocationPenaltyAssets,
648
- max: borrowAssets,
649
- });
686
+ throw new index_js_2.NonPositiveInputError("collateralAssets or borrowAssets", 0n);
650
687
  }
688
+ const mode = (0, supplyCollateralBorrow_internal_js_1.parseBlueSupplyCollateralBorrowMode)({
689
+ collateralAssets,
690
+ borrowAssets,
691
+ reallocations: [...(params.reallocations ?? [])],
692
+ });
693
+ const allocations = (0, supplyCollateralBorrow_internal_js_1.parseBlueSupplyCollateralBorrowAllocations)(this.parsedMarket, mode);
651
694
  if (borrowAssets > 0n) {
652
695
  if (positionData == null) {
653
- throw new index_js_3.MissingAccrualPositionError(this.marketParams.id);
696
+ throw new index_js_2.MissingAccrualPositionError(this.parsedMarket.id);
654
697
  }
655
- (0, index_js_2.validateAccrualPosition)({
698
+ // Reject a snapshot for another user or market.
699
+ (0, index_js_1.validateAccrualPosition)({
656
700
  positionData,
657
- expectedMarketId: this.marketParams.id,
701
+ expectedMarketId: this.parsedMarket.id,
658
702
  expectedUser: userAddress,
659
703
  });
660
- (0, index_js_2.validatePositionHealth)({
704
+ // Reject resulting debt above the buffered LLTV.
705
+ (0, index_js_1.validatePositionHealth)({
661
706
  positionData: positionData.accrueInterest(this.getBlueBundlesV1QuoteTimestamp(positionData.market.lastUpdate, createdAt)),
662
707
  additionalCollateral: collateralAssets,
663
708
  borrowAmount: borrowAssets,
664
- marketId: this.marketParams.id,
665
- lltv: this.marketParams.lltv,
709
+ marketId: this.parsedMarket.id,
710
+ lltv: this.parsedMarket.lltv,
666
711
  });
667
712
  }
668
- const nativeValue = (0, common_js_1.validateBlueBundlesV1NativeFunding)({
713
+ const nativeValue = (0, parse_js_1.parseNativeFunding)({
669
714
  chainId: this.chainId,
670
- token: this.marketParams.collateralToken,
715
+ token: this.parsedMarket.collateralToken,
671
716
  fundedAmount: collateralAssets,
672
- nativeAmount,
717
+ nativeAmount: params.nativeAmount,
673
718
  });
719
+ const input = {
720
+ ...normalized,
721
+ chainId: this.chainId,
722
+ userAddress,
723
+ marketParams: this.parsedMarket,
724
+ now: params.now,
725
+ metadata,
726
+ publicAllocations: allocations.publicAllocations,
727
+ reallocationPenaltyAssets: allocations.penaltyAssets,
728
+ value: nativeValue,
729
+ collateralAssets: (0, parse_js_1.parseUint256Max)("collateralAssets", lowerCollateralAssets),
730
+ borrowAssets: (0, parse_js_1.parseUint256Max)("borrowAssets", lowerBorrowAssets),
731
+ maxLtv: (0, parse_js_1.parseUint256)("maxLtv", borrowAssets > 0n ? this.getMaxLtv() : viem_1.maxUint256),
732
+ };
733
+ return input;
734
+ }
735
+ supplyCollateralBorrowPrepared(parsed) {
736
+ const { userAddress, collateralAssets, borrowAssets, deadline, value: nativeValue, marketParams, } = parsed;
674
737
  return {
675
738
  getRequirements: async (requirementsParams) => {
676
- const now = suppliedNow ?? morpho_ts_1.Time.timestamp();
739
+ const chainCheck = (0, chainCheck_js_1.createChainCheck)(this.client.viemClient, this.chainId);
740
+ const now = parsed.now ?? morpho_ts_1.Time.timestamp();
741
+ // Reject expiry before continuing this call.
677
742
  this.validateDeadline(deadline, now);
678
- await this.assertWriteTargets();
743
+ // Native-only funding has no token or authorization chain check.
744
+ await this.assertWriteTargets(chainCheck, nativeValue > 0n && borrowAssets === 0n);
679
745
  const [tokenRequirements, authorizationRequirements] = await Promise.all([
680
746
  collateralAssets > 0n && nativeValue === 0n
681
- ? this.getTokenRequirements({
682
- token: this.marketParams.collateralToken,
747
+ ? this.getTokenRequirements(chainCheck, {
748
+ token: marketParams.collateralToken,
683
749
  amount: collateralAssets,
684
750
  userAddress,
685
751
  deadline,
@@ -688,7 +754,7 @@ class MorphoBlue {
688
754
  })
689
755
  : Promise.resolve([]),
690
756
  borrowAssets > 0n
691
- ? this.getAuthorizationRequirements({
757
+ ? this.getAuthorizationRequirements(chainCheck, {
692
758
  userAddress,
693
759
  deadline,
694
760
  now,
@@ -702,75 +768,82 @@ class MorphoBlue {
702
768
  token: collateralAssets > 0n && nativeValue === 0n,
703
769
  authorization: borrowAssets > 0n,
704
770
  });
705
- return (0, index_js_1.blueSupplyCollateralBorrow)({
706
- market: { chainId: this.chainId, marketParams: this.marketParams },
707
- args: {
708
- userAddress,
709
- collateralAssets,
710
- borrowAssets,
711
- maxLtv: borrowAssets > 0n ? this.getMaxLtv() : viem_1.maxUint256,
712
- nativeAmount: nativeValue > 0n ? nativeValue : undefined,
713
- reallocations,
714
- deadline,
715
- referralFeePct,
716
- referralFeeRecipient,
717
- requirementSignature: token,
718
- authorizationSignature: authorization,
719
- },
720
- metadata,
771
+ return (0, supplyCollateralBorrow_internal_js_1.buildBlueSupplyCollateralBorrow)(parsed, {
772
+ requirementSignature: token,
773
+ authorizationSignature: authorization,
721
774
  });
722
775
  },
723
776
  };
724
777
  }
725
778
  /** {@inheritDoc BlueActions.refinance} */
726
779
  refinance(params) {
727
- const suppliedNow = params.now;
728
- const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, suppliedNow);
729
- const { userAddress, positionData, destination, deadline, referralFeeRecipient, } = params;
730
- const createdAt = suppliedNow ?? morpho_ts_1.Time.timestamp();
731
- this.validateWriteCommon(params, createdAt);
732
- (0, validate_js_1.validateMarketParamsId)(destination.marketParams);
733
- // `validateWriteCommon` returns void, so default an omitted `referralFeePct` to `0n`
734
- // here, matching the pure action's normalization.
735
- const referralFeePct = params.referralFeePct ?? 0n;
780
+ return this.refinancePrepared(this.parseRefinanceParams(params));
781
+ }
782
+ parseRefinanceParams(params) {
783
+ const { userAddress, positionData, destination } = params;
784
+ const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, params.now);
785
+ const createdAt = params.now ?? morpho_ts_1.Time.timestamp();
786
+ const normalized = this.validateWriteCommon(params, createdAt);
787
+ const destinationMarketParams = (0, parse_js_1.parseBlueMarket)(destination.marketParams);
788
+ const referralFeePct = normalized.referralFeePct;
736
789
  if (positionData == null) {
737
- throw new index_js_3.MissingAccrualPositionError(this.marketParams.id);
790
+ throw new index_js_2.MissingAccrualPositionError(this.parsedMarket.id);
738
791
  }
739
- (0, index_js_2.validateAccrualPosition)({
792
+ // Reject a snapshot for another user or market.
793
+ (0, index_js_1.validateAccrualPosition)({
740
794
  positionData,
741
- expectedMarketId: this.marketParams.id,
795
+ expectedMarketId: this.parsedMarket.id,
742
796
  expectedUser: userAddress,
743
797
  });
744
- // Reject an empty source debt or incompatible markets before quoting.
745
- (0, refinanceValidation_js_1.validateBlueRefinanceSource)({
746
- sourceMarketParams: this.marketParams,
747
- destinationMarketParams: destination.marketParams,
748
- borrowShares: positionData.borrowShares,
798
+ if (positionData.borrowShares <= 0n) {
799
+ throw new index_js_2.NonPositiveInputError("positionData.borrowShares", positionData.borrowShares);
800
+ }
801
+ const markets = (0, refinance_internal_js_1.parseBlueRefinanceMarkets)({
802
+ sourceMarketParams: this.parsedMarket,
803
+ destinationMarketParams,
749
804
  });
750
805
  if (destination.positionData == null) {
751
- throw new index_js_3.MissingAccrualPositionError(destination.marketParams.id);
806
+ throw new index_js_2.MissingAccrualPositionError(destinationMarketParams.id);
752
807
  }
753
- (0, index_js_2.validateAccrualPosition)({
808
+ // Reject a snapshot for another user or market.
809
+ (0, index_js_1.validateAccrualPosition)({
754
810
  positionData: destination.positionData,
755
- expectedMarketId: destination.marketParams.id,
811
+ expectedMarketId: destinationMarketParams.id,
756
812
  expectedUser: userAddress,
757
813
  });
758
- const reallocations = [...(params.reallocations ?? [])];
759
- // Reject migrations that cannot meet the buffered destination LLTV.
814
+ const allocations = (0, common_js_1.parseBlueAllocations)([...(params.reallocations ?? [])], markets.destinationMarketParams);
760
815
  (0, refinanceValidation_js_1.validateBlueRefinanceQuote)({
761
- destinationMarketParams: destination.marketParams,
816
+ destinationMarketParams,
762
817
  sourcePosition: positionData,
763
818
  destinationPosition: destination.positionData,
764
- reallocations,
819
+ penaltyAssets: allocations.penaltyAssets,
765
820
  referralFeePct,
766
821
  now: createdAt,
767
822
  });
823
+ const input = {
824
+ ...markets,
825
+ ...normalized,
826
+ chainId: this.chainId,
827
+ userAddress,
828
+ now: params.now,
829
+ metadata,
830
+ publicAllocations: allocations.publicAllocations,
831
+ reallocationPenaltyAssets: allocations.penaltyAssets,
832
+ maxLtv: (0, parse_js_1.parseUint256)("maxLtv", this.getMaxLtv(destinationMarketParams)),
833
+ };
834
+ return input;
835
+ }
836
+ refinancePrepared(parsed) {
837
+ const { userAddress, deadline } = parsed;
768
838
  return {
769
839
  getRequirements: async () => {
770
- const now = suppliedNow ?? morpho_ts_1.Time.timestamp();
840
+ const chainCheck = (0, chainCheck_js_1.createChainCheck)(this.client.viemClient, this.chainId);
841
+ const now = parsed.now ?? morpho_ts_1.Time.timestamp();
842
+ // Reject expiry before continuing this call.
771
843
  this.validateDeadline(deadline, now);
772
- await this.assertWriteTargets();
773
- return this.getAuthorizationRequirements({
844
+ // Reject missing write targets before resolving requirements.
845
+ await this.assertWriteTargets(chainCheck);
846
+ return this.getAuthorizationRequirements(chainCheck, {
774
847
  userAddress,
775
848
  deadline,
776
849
  now,
@@ -780,22 +853,8 @@ class MorphoBlue {
780
853
  const { authorization } = (0, common_js_1.selectBlueBundlesV1RequirementSignatures)(signatures, {
781
854
  authorization: true,
782
855
  });
783
- return (0, index_js_1.blueRefinance)({
784
- market: {
785
- chainId: this.chainId,
786
- sourceMarketParams: this.marketParams,
787
- destinationMarketParams: destination.marketParams,
788
- },
789
- args: {
790
- userAddress,
791
- maxLtv: this.getMaxLtv(destination.marketParams),
792
- reallocations,
793
- deadline,
794
- referralFeePct,
795
- referralFeeRecipient,
796
- authorizationSignature: authorization,
797
- },
798
- metadata,
856
+ return (0, refinance_internal_js_1.buildBlueRefinance)(parsed, {
857
+ authorizationSignature: authorization,
799
858
  });
800
859
  },
801
860
  };
@@ -854,7 +913,8 @@ class MorphoBlue {
854
913
  * ```
855
914
  */
856
915
  async getVaultV2BlueReallocationData({ vaultAddresses, block, }) {
857
- await (0, validateClientChainId_js_1.validateClientChainId)(this.client.viemClient, this.chainId);
916
+ // Confirm the RPC chain before prerequisite reads.
917
+ await (0, chainCheck_js_1.createChainCheck)(this.client.viemClient, this.chainId)();
858
918
  const client = this.client.viemClient;
859
919
  const fetchParams = {
860
920
  blockNumber: block.number,
@@ -862,17 +922,17 @@ class MorphoBlue {
862
922
  deployless: this.client.options.supportDeployless,
863
923
  };
864
924
  const [targetMarket, vaultEntries, blueBalance] = await Promise.all([
865
- (0, blue_sdk_viem_1.fetchMarket)(this.marketParams.id, client, fetchParams),
925
+ (0, blue_sdk_viem_1.fetchMarket)(this.parsedMarket.id, client, fetchParams),
866
926
  Promise.all(vaultAddresses.map(async (address) => {
867
927
  const vault = await (0, blue_sdk_viem_1.fetchAccrualVaultV2)(address, client, fetchParams);
868
928
  const publicAllocatorData = await (0, blue_sdk_viem_1.fetchVaultV2BluePublicAllocatorData)(vault, client, {
869
929
  ...fetchParams,
870
- targetMarketParams: this.marketParams,
930
+ targetMarketParams: new blue_sdk_1.MarketParams(this.parsedMarket),
871
931
  });
872
932
  return { publicAllocatorData, vault };
873
933
  })),
874
934
  (0, actions_1.readContract)(client, {
875
- address: this.marketParams.loanToken,
935
+ address: this.parsedMarket.loanToken,
876
936
  abi: viem_1.erc20Abi,
877
937
  functionName: "balanceOf",
878
938
  args: [(0, morpho_ts_1.getChainAddresses)(this.chainId).blue],
@@ -882,7 +942,7 @@ class MorphoBlue {
882
942
  return new vaultV2BlueReallocationData_js_1.VaultV2BlueReallocationData({
883
943
  chainId: this.chainId,
884
944
  timestamp: block.timestamp,
885
- blueBalances: { [this.marketParams.loanToken]: blueBalance },
945
+ blueBalances: { [this.parsedMarket.loanToken]: blueBalance },
886
946
  markets: { [targetMarket.id]: targetMarket },
887
947
  vaults: Object.fromEntries(vaultEntries.map(({ vault }) => [vault.address, vault])),
888
948
  allocations: Object.fromEntries(vaultEntries.map(({ publicAllocatorData, vault }) => [
@@ -955,8 +1015,9 @@ class MorphoBlue {
955
1015
  * ```
956
1016
  */
957
1017
  getVaultV2BlueReallocations({ reallocationData, options, }) {
958
- (0, index_js_2.validateChainId)(reallocationData.chainId, this.chainId);
959
- return reallocationData.computeVaultV2BlueReallocations(this.marketParams.id, options);
1018
+ // Reject cross-chain inputs before continuing.
1019
+ (0, index_js_1.validateChainId)(reallocationData.chainId, this.chainId);
1020
+ return reallocationData.computeVaultV2BlueReallocations(this.parsedMarket.id, options);
960
1021
  }
961
1022
  }
962
1023
  exports.MorphoBlue = MorphoBlue;