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