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