@morpho-org/morpho-sdk 10.3.1 → 10.5.0-next.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 (344) hide show
  1. package/README.md +169 -5
  2. package/lib/cjs/abis.d.ts +1 -1
  3. package/lib/cjs/abis.js +5 -1
  4. package/lib/cjs/actions/blue/common.js +12 -3
  5. package/lib/cjs/actions/midnight/redeem.d.ts +20 -3
  6. package/lib/cjs/actions/midnight/redeem.js +58 -1
  7. package/lib/cjs/actions/midnight/supplyCollateralWithNative.d.ts +24 -1
  8. package/lib/cjs/actions/midnight/supplyCollateralWithNative.js +41 -0
  9. package/lib/cjs/actions/midnight/withdrawCollateral.d.ts +16 -2
  10. package/lib/cjs/actions/midnight/withdrawCollateral.js +26 -1
  11. package/lib/cjs/blue/abis.d.ts +1 -1
  12. package/lib/cjs/blue/abis.js +5 -1
  13. package/lib/cjs/blue/entities.d.ts +4 -4
  14. package/lib/cjs/blue/entities.js +3 -1
  15. package/lib/cjs/blue/errors.d.ts +2 -2
  16. package/lib/cjs/blue/errors.js +5 -1
  17. package/lib/cjs/blue/fetch.d.ts +2 -2
  18. package/lib/cjs/blue/fetch.js +3 -1
  19. package/lib/cjs/blue/utils.d.ts +1 -1
  20. package/lib/cjs/blue/utils.js +2 -1
  21. package/lib/cjs/entities/index.d.ts +4 -4
  22. package/lib/cjs/entities/index.js +3 -1
  23. package/lib/cjs/entities/midnight/midnight.d.ts +100 -103
  24. package/lib/cjs/entities/midnight/midnight.js +99 -99
  25. package/lib/cjs/entities/vaultV1/vaultV1.js +0 -1
  26. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +34 -8
  27. package/lib/cjs/entities/vaultV2/vaultV2.js +39 -2
  28. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +3 -2
  29. package/lib/cjs/entities/vaultV2BlueReallocationData.js +6 -2
  30. package/lib/cjs/errors.d.ts +4 -4
  31. package/lib/cjs/errors.js +7 -5
  32. package/lib/cjs/fetch.d.ts +2 -2
  33. package/lib/cjs/fetch.js +3 -1
  34. package/lib/cjs/helpers/callback.d.ts +3 -2
  35. package/lib/cjs/helpers/callback.js +4 -20
  36. package/lib/cjs/helpers/computeMidnightPostWithdrawalCollateral.d.ts +17 -0
  37. package/lib/cjs/helpers/computeMidnightPostWithdrawalCollateral.js +30 -0
  38. package/lib/cjs/helpers/deadShares.js +3 -0
  39. package/lib/cjs/helpers/parseMidnightMaker.d.ts +21 -0
  40. package/lib/cjs/helpers/parseMidnightMaker.js +41 -1
  41. package/lib/cjs/helpers/validate.d.ts +60 -2
  42. package/lib/cjs/helpers/validate.js +87 -1
  43. package/lib/cjs/helpers/validateMidnightPublication.d.ts +10 -0
  44. package/lib/cjs/helpers/validateMidnightPublication.js +18 -1
  45. package/lib/cjs/helpers/validateMidnightTakeableOfferFields.d.ts +28 -0
  46. package/lib/cjs/helpers/validateMidnightTakeableOfferFields.js +85 -0
  47. package/lib/cjs/index.d.ts +2 -2
  48. package/lib/cjs/index.js +23 -1
  49. package/lib/cjs/midnight/errors.d.ts +2 -1
  50. package/lib/cjs/midnight/errors.js +5 -4
  51. package/lib/cjs/midnight/utils.d.ts +1 -1
  52. package/lib/cjs/midnight/utils.js +2 -1
  53. package/lib/cjs/transactionPlan/assemble.d.ts +24 -0
  54. package/lib/cjs/transactionPlan/assemble.js +25 -0
  55. package/lib/cjs/transactionPlan/blue/borrow.js +14 -64
  56. package/lib/cjs/transactionPlan/blue/refinance.js +9 -11
  57. package/lib/cjs/transactionPlan/blue/repay.js +10 -24
  58. package/lib/cjs/transactionPlan/blue/repayWithdrawCollateral.js +49 -45
  59. package/lib/cjs/transactionPlan/blue/stored.d.ts +26 -0
  60. package/lib/cjs/transactionPlan/blue/stored.js +87 -0
  61. package/lib/cjs/transactionPlan/blue/supply.js +7 -22
  62. package/lib/cjs/transactionPlan/blue/supplyCollateral.d.ts +1 -1
  63. package/lib/cjs/transactionPlan/blue/supplyCollateral.js +10 -15
  64. package/lib/cjs/transactionPlan/blue/supplyCollateralBorrow.js +14 -64
  65. package/lib/cjs/transactionPlan/blue/withdraw.d.ts +2 -3
  66. package/lib/cjs/transactionPlan/blue/withdraw.js +67 -79
  67. package/lib/cjs/transactionPlan/blue/withdrawBuyCallback.d.ts +101 -0
  68. package/lib/cjs/transactionPlan/blue/withdrawBuyCallback.js +264 -0
  69. package/lib/cjs/transactionPlan/blue/withdrawCollateral.d.ts +2 -3
  70. package/lib/cjs/transactionPlan/blue/withdrawCollateral.js +31 -30
  71. package/lib/cjs/transactionPlan/deposit.js +4 -7
  72. package/lib/cjs/transactionPlan/error.d.ts +27 -0
  73. package/lib/cjs/transactionPlan/error.js +30 -1
  74. package/lib/cjs/transactionPlan/index.d.ts +40 -1
  75. package/lib/cjs/transactionPlan/index.js +46 -5
  76. package/lib/cjs/transactionPlan/intent.d.ts +1 -1
  77. package/lib/cjs/transactionPlan/midnight/authorization.d.ts +15 -0
  78. package/lib/cjs/transactionPlan/midnight/authorization.js +45 -0
  79. package/lib/cjs/transactionPlan/midnight/cancelOffers.d.ts +80 -0
  80. package/lib/cjs/transactionPlan/midnight/cancelOffers.js +125 -0
  81. package/lib/cjs/transactionPlan/midnight/makeBorrow.d.ts +104 -0
  82. package/lib/cjs/transactionPlan/midnight/makeBorrow.js +126 -0
  83. package/lib/cjs/transactionPlan/midnight/makeLend.d.ts +140 -0
  84. package/lib/cjs/transactionPlan/midnight/makeLend.js +176 -0
  85. package/lib/cjs/transactionPlan/midnight/makeRepay.d.ts +118 -0
  86. package/lib/cjs/transactionPlan/midnight/makeRepay.js +118 -0
  87. package/lib/cjs/transactionPlan/midnight/makeWithdraw.d.ts +104 -0
  88. package/lib/cjs/transactionPlan/midnight/makeWithdraw.js +126 -0
  89. package/lib/cjs/transactionPlan/midnight/makerPublication.d.ts +55 -0
  90. package/lib/cjs/transactionPlan/midnight/makerPublication.js +315 -0
  91. package/lib/cjs/transactionPlan/midnight/market.d.ts +49 -0
  92. package/lib/cjs/transactionPlan/midnight/market.js +120 -0
  93. package/lib/cjs/transactionPlan/midnight/prepare.d.ts +244 -0
  94. package/lib/cjs/transactionPlan/midnight/prepare.js +547 -0
  95. package/lib/cjs/transactionPlan/midnight/publicationState.d.ts +24 -0
  96. package/lib/cjs/transactionPlan/midnight/publicationState.js +50 -0
  97. package/lib/cjs/transactionPlan/midnight/redeem.d.ts +102 -0
  98. package/lib/cjs/transactionPlan/midnight/redeem.js +170 -0
  99. package/lib/cjs/transactionPlan/midnight/reduceOnlySellFeasibility.d.ts +25 -0
  100. package/lib/cjs/transactionPlan/midnight/reduceOnlySellFeasibility.js +51 -0
  101. package/lib/cjs/transactionPlan/midnight/repayWithdrawCollateral.d.ts +162 -0
  102. package/lib/cjs/transactionPlan/midnight/repayWithdrawCollateral.js +328 -0
  103. package/lib/cjs/transactionPlan/midnight/supplyBlueMakeLend.d.ts +155 -0
  104. package/lib/cjs/transactionPlan/midnight/supplyBlueMakeLend.js +199 -0
  105. package/lib/cjs/transactionPlan/midnight/supplyCollateral.d.ts +96 -0
  106. package/lib/cjs/transactionPlan/midnight/supplyCollateral.js +190 -0
  107. package/lib/cjs/transactionPlan/midnight/supplyCollateralBatch.d.ts +112 -0
  108. package/lib/cjs/transactionPlan/midnight/supplyCollateralBatch.js +221 -0
  109. package/lib/cjs/transactionPlan/midnight/supplyCollateralMakeBorrow.d.ts +129 -0
  110. package/lib/cjs/transactionPlan/midnight/supplyCollateralMakeBorrow.js +179 -0
  111. package/lib/cjs/transactionPlan/midnight/supplyCollateralWithNative.d.ts +115 -0
  112. package/lib/cjs/transactionPlan/midnight/supplyCollateralWithNative.js +259 -0
  113. package/lib/cjs/transactionPlan/midnight/takeBorrow.d.ts +304 -0
  114. package/lib/cjs/transactionPlan/midnight/takeBorrow.js +545 -0
  115. package/lib/cjs/transactionPlan/midnight/takeLend.d.ts +99 -0
  116. package/lib/cjs/transactionPlan/midnight/takeLend.js +263 -0
  117. package/lib/cjs/transactionPlan/midnight/takeRepayWithdrawCollateral.d.ts +175 -0
  118. package/lib/cjs/transactionPlan/midnight/takeRepayWithdrawCollateral.js +440 -0
  119. package/lib/cjs/transactionPlan/midnight/takeWithdraw.d.ts +139 -0
  120. package/lib/cjs/transactionPlan/midnight/takeWithdraw.js +240 -0
  121. package/lib/cjs/transactionPlan/midnight/validation.d.ts +22 -0
  122. package/lib/cjs/transactionPlan/midnight/validation.js +158 -0
  123. package/lib/cjs/transactionPlan/midnight/withdrawCollateral.d.ts +130 -0
  124. package/lib/cjs/transactionPlan/midnight/withdrawCollateral.js +228 -0
  125. package/lib/cjs/transactionPlan/midnight/withdrawCollateralBatch.d.ts +142 -0
  126. package/lib/cjs/transactionPlan/midnight/withdrawCollateralBatch.js +232 -0
  127. package/lib/cjs/transactionPlan/permit.d.ts +15 -2
  128. package/lib/cjs/transactionPlan/permit.js +26 -68
  129. package/lib/cjs/transactionPlan/plan.d.ts +99 -15
  130. package/lib/cjs/transactionPlan/plan.js +159 -31
  131. package/lib/cjs/transactionPlan/redeem.js +4 -7
  132. package/lib/cjs/transactionPlan/requirements.d.ts +7 -4
  133. package/lib/cjs/transactionPlan/requirements.js +2 -2
  134. package/lib/cjs/transactionPlan/serde.d.ts +13 -2
  135. package/lib/cjs/transactionPlan/serde.js +68 -21
  136. package/lib/cjs/transactionPlan/types.d.ts +151 -3
  137. package/lib/cjs/transactionPlan/validateActions.d.ts +3 -1
  138. package/lib/cjs/transactionPlan/validateActions.js +31 -3
  139. package/lib/cjs/transactionPlan/validation.d.ts +1 -1
  140. package/lib/cjs/transactionPlan/validation.js +37 -1
  141. package/lib/cjs/transactionPlan/vaultBinding.d.ts +29 -0
  142. package/lib/cjs/transactionPlan/vaultBinding.js +69 -0
  143. package/lib/cjs/transactionPlan/vaultRequirements.d.ts +20 -0
  144. package/lib/cjs/transactionPlan/vaultRequirements.js +37 -0
  145. package/lib/cjs/transactionPlan/vaultv1/deposit.d.ts +1 -1
  146. package/lib/cjs/transactionPlan/vaultv1/deposit.js +2 -13
  147. package/lib/cjs/transactionPlan/vaultv1/inKindRedeem.d.ts +2 -2
  148. package/lib/cjs/transactionPlan/vaultv1/inKindRedeem.js +10 -41
  149. package/lib/cjs/transactionPlan/vaultv1/migrateToV2.d.ts +1 -1
  150. package/lib/cjs/transactionPlan/vaultv1/migrateToV2.js +6 -18
  151. package/lib/cjs/transactionPlan/vaultv1/redeem.js +1 -12
  152. package/lib/cjs/transactionPlan/vaultv1/withdraw.d.ts +1 -1
  153. package/lib/cjs/transactionPlan/vaultv1/withdraw.js +2 -13
  154. package/lib/cjs/transactionPlan/vaultv2/deposit.d.ts +1 -1
  155. package/lib/cjs/transactionPlan/vaultv2/deposit.js +2 -13
  156. package/lib/cjs/transactionPlan/vaultv2/forceRedeem.d.ts +1 -1
  157. package/lib/cjs/transactionPlan/vaultv2/forceRedeem.js +4 -7
  158. package/lib/cjs/transactionPlan/vaultv2/forceWithdraw.d.ts +2 -2
  159. package/lib/cjs/transactionPlan/vaultv2/forceWithdraw.js +12 -42
  160. package/lib/cjs/transactionPlan/vaultv2/inKindRedeem.d.ts +2 -2
  161. package/lib/cjs/transactionPlan/vaultv2/inKindRedeem.js +10 -41
  162. package/lib/cjs/transactionPlan/vaultv2/redeem.d.ts +1 -1
  163. package/lib/cjs/transactionPlan/vaultv2/redeem.js +2 -13
  164. package/lib/cjs/transactionPlan/vaultv2/withdraw.d.ts +1 -1
  165. package/lib/cjs/transactionPlan/vaultv2/withdraw.js +2 -13
  166. package/lib/cjs/transactionPlan/withdraw.js +4 -7
  167. package/lib/cjs/types/error.d.ts +4 -1
  168. package/lib/cjs/types/error.js +5 -2
  169. package/lib/cjs/utils.d.ts +2 -2
  170. package/lib/cjs/utils.js +4 -2
  171. package/lib/cjs/version.d.ts +1 -1
  172. package/lib/cjs/version.js +1 -1
  173. package/lib/esm/abis.d.ts +1 -1
  174. package/lib/esm/abis.js +1 -1
  175. package/lib/esm/actions/blue/common.js +13 -4
  176. package/lib/esm/actions/midnight/redeem.d.ts +20 -3
  177. package/lib/esm/actions/midnight/redeem.js +56 -0
  178. package/lib/esm/actions/midnight/supplyCollateralWithNative.d.ts +24 -1
  179. package/lib/esm/actions/midnight/supplyCollateralWithNative.js +40 -0
  180. package/lib/esm/actions/midnight/withdrawCollateral.d.ts +16 -2
  181. package/lib/esm/actions/midnight/withdrawCollateral.js +25 -1
  182. package/lib/esm/blue/abis.d.ts +1 -1
  183. package/lib/esm/blue/abis.js +1 -1
  184. package/lib/esm/blue/entities.d.ts +4 -4
  185. package/lib/esm/blue/entities.js +2 -2
  186. package/lib/esm/blue/errors.d.ts +2 -2
  187. package/lib/esm/blue/errors.js +2 -2
  188. package/lib/esm/blue/fetch.d.ts +2 -2
  189. package/lib/esm/blue/fetch.js +2 -2
  190. package/lib/esm/blue/utils.d.ts +1 -1
  191. package/lib/esm/blue/utils.js +1 -1
  192. package/lib/esm/entities/index.d.ts +4 -4
  193. package/lib/esm/entities/index.js +2 -2
  194. package/lib/esm/entities/midnight/midnight.d.ts +100 -103
  195. package/lib/esm/entities/midnight/midnight.js +99 -99
  196. package/lib/esm/entities/vaultV1/vaultV1.js +0 -1
  197. package/lib/esm/entities/vaultV2/vaultV2.d.ts +34 -8
  198. package/lib/esm/entities/vaultV2/vaultV2.js +40 -3
  199. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +3 -2
  200. package/lib/esm/entities/vaultV2BlueReallocationData.js +6 -2
  201. package/lib/esm/errors.d.ts +4 -4
  202. package/lib/esm/errors.js +4 -4
  203. package/lib/esm/fetch.d.ts +2 -2
  204. package/lib/esm/fetch.js +2 -2
  205. package/lib/esm/helpers/callback.d.ts +3 -2
  206. package/lib/esm/helpers/callback.js +5 -21
  207. package/lib/esm/helpers/computeMidnightPostWithdrawalCollateral.d.ts +17 -0
  208. package/lib/esm/helpers/computeMidnightPostWithdrawalCollateral.js +27 -0
  209. package/lib/esm/helpers/deadShares.js +4 -1
  210. package/lib/esm/helpers/parseMidnightMaker.d.ts +21 -0
  211. package/lib/esm/helpers/parseMidnightMaker.js +40 -1
  212. package/lib/esm/helpers/validate.d.ts +60 -2
  213. package/lib/esm/helpers/validate.js +84 -1
  214. package/lib/esm/helpers/validateMidnightPublication.d.ts +10 -0
  215. package/lib/esm/helpers/validateMidnightPublication.js +16 -0
  216. package/lib/esm/helpers/validateMidnightTakeableOfferFields.d.ts +28 -0
  217. package/lib/esm/helpers/validateMidnightTakeableOfferFields.js +81 -0
  218. package/lib/esm/index.d.ts +2 -2
  219. package/lib/esm/index.js +1 -1
  220. package/lib/esm/midnight/errors.d.ts +2 -1
  221. package/lib/esm/midnight/errors.js +2 -1
  222. package/lib/esm/midnight/utils.d.ts +1 -1
  223. package/lib/esm/midnight/utils.js +1 -1
  224. package/lib/esm/transactionPlan/assemble.d.ts +24 -0
  225. package/lib/esm/transactionPlan/assemble.js +21 -0
  226. package/lib/esm/transactionPlan/blue/borrow.js +15 -65
  227. package/lib/esm/transactionPlan/blue/refinance.js +10 -12
  228. package/lib/esm/transactionPlan/blue/repay.js +11 -25
  229. package/lib/esm/transactionPlan/blue/repayWithdrawCollateral.js +50 -46
  230. package/lib/esm/transactionPlan/blue/stored.d.ts +26 -0
  231. package/lib/esm/transactionPlan/blue/stored.js +80 -0
  232. package/lib/esm/transactionPlan/blue/supply.js +7 -22
  233. package/lib/esm/transactionPlan/blue/supplyCollateral.d.ts +1 -1
  234. package/lib/esm/transactionPlan/blue/supplyCollateral.js +10 -15
  235. package/lib/esm/transactionPlan/blue/supplyCollateralBorrow.js +15 -65
  236. package/lib/esm/transactionPlan/blue/withdraw.d.ts +2 -3
  237. package/lib/esm/transactionPlan/blue/withdraw.js +70 -82
  238. package/lib/esm/transactionPlan/blue/withdrawBuyCallback.d.ts +101 -0
  239. package/lib/esm/transactionPlan/blue/withdrawBuyCallback.js +259 -0
  240. package/lib/esm/transactionPlan/blue/withdrawCollateral.d.ts +2 -3
  241. package/lib/esm/transactionPlan/blue/withdrawCollateral.js +32 -31
  242. package/lib/esm/transactionPlan/deposit.js +5 -8
  243. package/lib/esm/transactionPlan/error.d.ts +27 -0
  244. package/lib/esm/transactionPlan/error.js +28 -0
  245. package/lib/esm/transactionPlan/index.d.ts +40 -1
  246. package/lib/esm/transactionPlan/index.js +20 -1
  247. package/lib/esm/transactionPlan/intent.d.ts +1 -1
  248. package/lib/esm/transactionPlan/midnight/authorization.d.ts +15 -0
  249. package/lib/esm/transactionPlan/midnight/authorization.js +40 -0
  250. package/lib/esm/transactionPlan/midnight/cancelOffers.d.ts +80 -0
  251. package/lib/esm/transactionPlan/midnight/cancelOffers.js +120 -0
  252. package/lib/esm/transactionPlan/midnight/makeBorrow.d.ts +104 -0
  253. package/lib/esm/transactionPlan/midnight/makeBorrow.js +123 -0
  254. package/lib/esm/transactionPlan/midnight/makeLend.d.ts +140 -0
  255. package/lib/esm/transactionPlan/midnight/makeLend.js +172 -0
  256. package/lib/esm/transactionPlan/midnight/makeRepay.d.ts +118 -0
  257. package/lib/esm/transactionPlan/midnight/makeRepay.js +115 -0
  258. package/lib/esm/transactionPlan/midnight/makeWithdraw.d.ts +104 -0
  259. package/lib/esm/transactionPlan/midnight/makeWithdraw.js +123 -0
  260. package/lib/esm/transactionPlan/midnight/makerPublication.d.ts +55 -0
  261. package/lib/esm/transactionPlan/midnight/makerPublication.js +310 -0
  262. package/lib/esm/transactionPlan/midnight/market.d.ts +49 -0
  263. package/lib/esm/transactionPlan/midnight/market.js +112 -0
  264. package/lib/esm/transactionPlan/midnight/prepare.d.ts +244 -0
  265. package/lib/esm/transactionPlan/midnight/prepare.js +536 -0
  266. package/lib/esm/transactionPlan/midnight/publicationState.d.ts +24 -0
  267. package/lib/esm/transactionPlan/midnight/publicationState.js +47 -0
  268. package/lib/esm/transactionPlan/midnight/redeem.d.ts +102 -0
  269. package/lib/esm/transactionPlan/midnight/redeem.js +165 -0
  270. package/lib/esm/transactionPlan/midnight/reduceOnlySellFeasibility.d.ts +25 -0
  271. package/lib/esm/transactionPlan/midnight/reduceOnlySellFeasibility.js +47 -0
  272. package/lib/esm/transactionPlan/midnight/repayWithdrawCollateral.d.ts +162 -0
  273. package/lib/esm/transactionPlan/midnight/repayWithdrawCollateral.js +323 -0
  274. package/lib/esm/transactionPlan/midnight/supplyBlueMakeLend.d.ts +155 -0
  275. package/lib/esm/transactionPlan/midnight/supplyBlueMakeLend.js +196 -0
  276. package/lib/esm/transactionPlan/midnight/supplyCollateral.d.ts +96 -0
  277. package/lib/esm/transactionPlan/midnight/supplyCollateral.js +185 -0
  278. package/lib/esm/transactionPlan/midnight/supplyCollateralBatch.d.ts +112 -0
  279. package/lib/esm/transactionPlan/midnight/supplyCollateralBatch.js +216 -0
  280. package/lib/esm/transactionPlan/midnight/supplyCollateralMakeBorrow.d.ts +129 -0
  281. package/lib/esm/transactionPlan/midnight/supplyCollateralMakeBorrow.js +176 -0
  282. package/lib/esm/transactionPlan/midnight/supplyCollateralWithNative.d.ts +115 -0
  283. package/lib/esm/transactionPlan/midnight/supplyCollateralWithNative.js +254 -0
  284. package/lib/esm/transactionPlan/midnight/takeBorrow.d.ts +304 -0
  285. package/lib/esm/transactionPlan/midnight/takeBorrow.js +539 -0
  286. package/lib/esm/transactionPlan/midnight/takeLend.d.ts +99 -0
  287. package/lib/esm/transactionPlan/midnight/takeLend.js +258 -0
  288. package/lib/esm/transactionPlan/midnight/takeRepayWithdrawCollateral.d.ts +175 -0
  289. package/lib/esm/transactionPlan/midnight/takeRepayWithdrawCollateral.js +435 -0
  290. package/lib/esm/transactionPlan/midnight/takeWithdraw.d.ts +139 -0
  291. package/lib/esm/transactionPlan/midnight/takeWithdraw.js +236 -0
  292. package/lib/esm/transactionPlan/midnight/validation.d.ts +22 -0
  293. package/lib/esm/transactionPlan/midnight/validation.js +151 -0
  294. package/lib/esm/transactionPlan/midnight/withdrawCollateral.d.ts +130 -0
  295. package/lib/esm/transactionPlan/midnight/withdrawCollateral.js +223 -0
  296. package/lib/esm/transactionPlan/midnight/withdrawCollateralBatch.d.ts +142 -0
  297. package/lib/esm/transactionPlan/midnight/withdrawCollateralBatch.js +227 -0
  298. package/lib/esm/transactionPlan/permit.d.ts +15 -2
  299. package/lib/esm/transactionPlan/permit.js +25 -68
  300. package/lib/esm/transactionPlan/plan.d.ts +99 -15
  301. package/lib/esm/transactionPlan/plan.js +154 -27
  302. package/lib/esm/transactionPlan/redeem.js +5 -8
  303. package/lib/esm/transactionPlan/requirements.d.ts +7 -4
  304. package/lib/esm/transactionPlan/requirements.js +2 -2
  305. package/lib/esm/transactionPlan/serde.d.ts +13 -2
  306. package/lib/esm/transactionPlan/serde.js +49 -2
  307. package/lib/esm/transactionPlan/types.d.ts +151 -3
  308. package/lib/esm/transactionPlan/validateActions.d.ts +3 -1
  309. package/lib/esm/transactionPlan/validateActions.js +31 -3
  310. package/lib/esm/transactionPlan/validation.d.ts +1 -1
  311. package/lib/esm/transactionPlan/validation.js +38 -2
  312. package/lib/esm/transactionPlan/vaultBinding.d.ts +29 -0
  313. package/lib/esm/transactionPlan/vaultBinding.js +65 -0
  314. package/lib/esm/transactionPlan/vaultRequirements.d.ts +20 -0
  315. package/lib/esm/transactionPlan/vaultRequirements.js +34 -0
  316. package/lib/esm/transactionPlan/vaultv1/deposit.d.ts +1 -1
  317. package/lib/esm/transactionPlan/vaultv1/deposit.js +3 -14
  318. package/lib/esm/transactionPlan/vaultv1/inKindRedeem.d.ts +2 -2
  319. package/lib/esm/transactionPlan/vaultv1/inKindRedeem.js +12 -43
  320. package/lib/esm/transactionPlan/vaultv1/migrateToV2.d.ts +1 -1
  321. package/lib/esm/transactionPlan/vaultv1/migrateToV2.js +9 -21
  322. package/lib/esm/transactionPlan/vaultv1/redeem.js +2 -13
  323. package/lib/esm/transactionPlan/vaultv1/withdraw.d.ts +1 -1
  324. package/lib/esm/transactionPlan/vaultv1/withdraw.js +3 -14
  325. package/lib/esm/transactionPlan/vaultv2/deposit.d.ts +1 -1
  326. package/lib/esm/transactionPlan/vaultv2/deposit.js +3 -14
  327. package/lib/esm/transactionPlan/vaultv2/forceRedeem.d.ts +1 -1
  328. package/lib/esm/transactionPlan/vaultv2/forceRedeem.js +5 -8
  329. package/lib/esm/transactionPlan/vaultv2/forceWithdraw.d.ts +2 -2
  330. package/lib/esm/transactionPlan/vaultv2/forceWithdraw.js +15 -45
  331. package/lib/esm/transactionPlan/vaultv2/inKindRedeem.d.ts +2 -2
  332. package/lib/esm/transactionPlan/vaultv2/inKindRedeem.js +12 -43
  333. package/lib/esm/transactionPlan/vaultv2/redeem.d.ts +1 -1
  334. package/lib/esm/transactionPlan/vaultv2/redeem.js +3 -14
  335. package/lib/esm/transactionPlan/vaultv2/withdraw.d.ts +1 -1
  336. package/lib/esm/transactionPlan/vaultv2/withdraw.js +3 -14
  337. package/lib/esm/transactionPlan/withdraw.js +5 -8
  338. package/lib/esm/types/error.d.ts +4 -1
  339. package/lib/esm/types/error.js +5 -2
  340. package/lib/esm/utils.d.ts +2 -2
  341. package/lib/esm/utils.js +2 -2
  342. package/lib/esm/version.d.ts +1 -1
  343. package/lib/esm/version.js +1 -1
  344. package/package.json +7 -7
@@ -1,9 +1,10 @@
1
1
  import { Token } from "@morpho-org/blue-sdk";
2
2
  import { getPermitTypedData } from "@morpho-org/blue-sdk-viem";
3
3
  import { getChainAddress } from "@morpho-org/morpho-ts";
4
- import { maxUint256 } from "viem";
4
+ import { selectBundlesSharesPermitSignature, selectBundlesTokenRequirementSignature, } from "../actions/bundles/common.js";
5
5
  import { IncompleteTransactionPlanError, TransactionPlanSignatureMismatchError, } from "./error.js";
6
6
  import { getPlanObject, matchesAddress, validatePlanField, validatePlanFields, } from "./validation.js";
7
+ import { getVaultPermitBinding, isVaultPermitAction, } from "./vaultBinding.js";
7
8
  /** @internal Validates the stored ERC-2612 request shape without verifying its signature. */
8
9
  export function validatePermitPlanRequest(request, path) {
9
10
  const args = getPlanObject(request.args, `${path}.args`);
@@ -76,72 +77,10 @@ export function validatePermitPlanRequest(request, path) {
76
77
  }
77
78
  /** @internal Checks a permit's signing payload against its referencing action and encoding arguments. */
78
79
  export function validateVaultPermitRequest(request, action) {
79
- if (request.kind !== "permit") {
80
+ if (request.kind !== "permit" || !isVaultPermitAction(action)) {
80
81
  throw new TransactionPlanSignatureMismatchError(request.id, "reference");
81
82
  }
82
- let spenderKey;
83
- let asset;
84
- let amount;
85
- let exact = false;
86
- switch (action.type) {
87
- case "vaultV2Deposit":
88
- spenderKey = "bundles.vaultBundlesV1";
89
- asset = action.inputs.asset;
90
- amount = action.inputs.assets;
91
- exact = true;
92
- break;
93
- case "vaultV1Deposit":
94
- spenderKey = "bundles.vaultBundlesV1";
95
- asset = action.inputs.asset;
96
- amount = action.inputs.assets;
97
- exact = true;
98
- break;
99
- case "vaultV1Withdraw":
100
- spenderKey = "bundles.vaultBundlesV1";
101
- asset = action.inputs.vault;
102
- amount = action.inputs.requiredShareAllowance;
103
- break;
104
- case "vaultV2Withdraw":
105
- spenderKey = "bundles.vaultBundlesV1";
106
- asset = action.inputs.vault;
107
- amount = action.inputs.requiredShareAllowance;
108
- break;
109
- case "vaultV2Redeem":
110
- spenderKey = "bundles.vaultBundlesV1";
111
- asset = action.inputs.vault;
112
- amount = action.inputs.shares;
113
- break;
114
- case "vaultV1Redeem":
115
- spenderKey = "bundles.vaultBundlesV1";
116
- asset = action.inputs.vault;
117
- amount = action.inputs.shares;
118
- break;
119
- case "vaultV2ForceWithdraw":
120
- spenderKey = "bundles.vaultExitBundlesV1";
121
- asset = action.inputs.vault;
122
- amount = maxUint256;
123
- exact = true;
124
- break;
125
- case "vaultV1MigrateToV2":
126
- spenderKey = "bundles.vaultBundlesV1";
127
- asset = action.inputs.sourceVault;
128
- amount = action.inputs.requiredShareAllowance;
129
- break;
130
- case "vaultV2InKindRedeem":
131
- spenderKey = "bundles.vaultExitBundlesV1";
132
- asset = action.inputs.vault;
133
- amount = maxUint256;
134
- exact = true;
135
- break;
136
- case "vaultV1InKindRedeem":
137
- spenderKey = "bundles.vaultExitBundlesV1";
138
- asset = action.inputs.vault;
139
- amount = maxUint256;
140
- exact = true;
141
- break;
142
- default:
143
- throw new TransactionPlanSignatureMismatchError(request.id, "reference");
144
- }
83
+ const { bundler, asset, restore } = getVaultPermitBinding(action);
145
84
  if (action.signatureRequestIds.tokenPermit !== request.id) {
146
85
  throw new TransactionPlanSignatureMismatchError(request.id, "reference");
147
86
  }
@@ -149,10 +88,10 @@ export function validateVaultPermitRequest(request, action) {
149
88
  chainId: action.inputs.chainId,
150
89
  account: action.inputs.account,
151
90
  deadline: action.inputs.deadline,
152
- bundler: spenderKey,
91
+ bundler,
153
92
  asset,
154
- amount,
155
- exact,
93
+ amount: restore.amount,
94
+ exact: restore.exact,
156
95
  });
157
96
  }
158
97
  /** @internal Binds the signing payload and encoding arguments to a registered bundle's token pull. */
@@ -234,5 +173,23 @@ export function getSignedTokenPermit(requests, { id, asset, owner, }) {
234
173
  },
235
174
  };
236
175
  }
176
+ export function selectVaultPermit(action, requests) {
177
+ const { chainId, account, deadline } = action.inputs;
178
+ const { bundler, asset, build } = getVaultPermitBinding(action);
179
+ const signature = getSignedTokenPermit(requests, {
180
+ id: action.signatureRequestIds.tokenPermit,
181
+ asset,
182
+ owner: account,
183
+ });
184
+ const signatures = signature ? [signature] : [];
185
+ const expected = {
186
+ spender: getChainAddress(chainId, bundler),
187
+ amount: build.amount,
188
+ deadline,
189
+ };
190
+ return build.pull === "token"
191
+ ? selectBundlesTokenRequirementSignature(signatures, expected)
192
+ : selectBundlesSharesPermitSignature(signatures, expected);
193
+ }
237
194
  /** @internal Alias for existing Vault V2 plan builders. */
238
195
  export { getSignedTokenPermit as getVaultPermit };
@@ -57,6 +57,56 @@ export declare function verifyPlanSignature(request: SignatureRequest, signature
57
57
  * ```
58
58
  */
59
59
  export declare function getPendingSignatureRequests(plan: TransactionPlan): readonly SignatureRequest[];
60
+ /**
61
+ * @internal Whether a step's transaction carries the plan's `calldataOrigin`. Operation steps do;
62
+ * approval and authorization prerequisites never do, matching the entity path. Every action type
63
+ * must be listed, so a new type does not compile until its role is chosen.
64
+ */
65
+ export declare const CALLDATA_ORIGIN_ROLE: {
66
+ readonly blueBorrow: "operation";
67
+ readonly blueRefinance: "operation";
68
+ readonly blueRepay: "operation";
69
+ readonly blueRepayWithdrawCollateral: "operation";
70
+ readonly blueSupply: "operation";
71
+ readonly blueSupplyCollateral: "operation";
72
+ readonly blueSupplyCollateralBorrow: "operation";
73
+ readonly blueWithdraw: "operation";
74
+ readonly blueWithdrawBuyCallback: "operation";
75
+ readonly blueWithdrawCollateral: "operation";
76
+ readonly erc20Approval: "prerequisite";
77
+ readonly midnightAuthorization: "prerequisite";
78
+ readonly midnightCancelOffers: "operation";
79
+ readonly midnightMakeBorrow: "operation";
80
+ readonly midnightMakeLend: "operation";
81
+ readonly midnightMakeRepay: "operation";
82
+ readonly midnightMakeWithdraw: "operation";
83
+ readonly midnightRedeem: "operation";
84
+ readonly midnightRepayWithdrawCollateral: "operation";
85
+ readonly midnightSupplyBlueMakeLend: "operation";
86
+ readonly midnightSupplyCollateral: "operation";
87
+ readonly midnightSupplyCollateralBatch: "operation";
88
+ readonly midnightSupplyCollateralMakeBorrow: "operation";
89
+ readonly midnightSupplyCollateralTakeBorrow: "operation";
90
+ readonly midnightSupplyCollateralWithNative: "operation";
91
+ readonly midnightTakeBorrow: "operation";
92
+ readonly midnightTakeLend: "operation";
93
+ readonly midnightTakeRepayWithdrawCollateral: "operation";
94
+ readonly midnightTakeWithdraw: "operation";
95
+ readonly midnightWithdrawCollateral: "operation";
96
+ readonly midnightWithdrawCollateralBatch: "operation";
97
+ readonly morphoAuthorization: "prerequisite";
98
+ readonly vaultV1Deposit: "operation";
99
+ readonly vaultV1InKindRedeem: "operation";
100
+ readonly vaultV1MigrateToV2: "operation";
101
+ readonly vaultV1Redeem: "operation";
102
+ readonly vaultV1Withdraw: "operation";
103
+ readonly vaultV2Deposit: "operation";
104
+ readonly vaultV2ForceRedeem: "operation";
105
+ readonly vaultV2ForceWithdraw: "operation";
106
+ readonly vaultV2InKindRedeem: "operation";
107
+ readonly vaultV2Redeem: "operation";
108
+ readonly vaultV2Withdraw: "operation";
109
+ };
60
110
  /**
61
111
  * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
62
112
  * Builds one transaction from a step, its fixed inputs and the signatures its action names.
@@ -68,17 +118,25 @@ export declare function getPendingSignatureRequests(plan: TransactionPlan): read
68
118
  * not operation amounts, market parameters or referral fees/recipients. Protect those unsigned fields.
69
119
  * @param plan - Plan supplying referenced signature requests and the calldata origin; chain and account come from the action inputs.
70
120
  * @param step - Recipe to encode; missing signatures on unrelated supported steps do not block it.
71
- * @returns A frozen `{ to, data, value, action }`, ready to simulate or submit. `data` ends with the plan's `calldataOrigin` when one was set.
72
- * @throws {InvalidTransactionPlanError} If the plan header, action context, IDs or deadlines are inconsistent.
73
- * @throws {InvalidBigIntInputError} If a supplied Blue maxPenalty is not a bigint; uint256 schema validation is performed by parseTransactionPlan, not this builder.
121
+ * @returns A frozen `{ to, data, value, action }`, ready to simulate or submit. `data` ends with the plan's `calldataOrigin` when one was set, except for ERC-20 approval, Morpho authorization and Midnight authorization prerequisite steps.
122
+ * @throws {InvalidTransactionPlanError} If the plan header, action context, IDs or deadlines are inconsistent, a Midnight repayment type is unsupported, a Midnight market lists its collateral out of ascending token order, a Midnight single-collateral step has no bigint `collateralIndex`, or a Midnight collateral batch's list is not an array or holds a non-object entry (path under `$.inputs`).
123
+ * @throws {InvalidBigIntInputError} If a supplied Blue maxPenalty, or a Midnight collateral batch entry's `collateralIndex` or `assets`, is not a bigint; uint256 schema validation is performed by parseTransactionPlan, not this builder.
74
124
  * @throws {InvalidSignatureError} If a stored signature has high s, cannot recover a public key or does not match the request owner.
75
125
  * @throws {UnsupportedTransactionPlanVersionError} If the plan version is unsupported.
76
126
  * @throws {UnsupportedTransactionPlanActionError} If any plan step or the selected step names an unsupported action type.
77
127
  * @throws {UnsupportedErc20ApprovalSpenderError} If an approval names an unregistered spender.
78
128
  * @throws {SameVaultMigrationError} If a migration's source and destination vaults are identical.
79
129
  * @throws {AmountAndSharesExclusiveError} If a migration supplies both `assets` and `shares`, or neither.
80
- * @throws {NonPositiveInputError} If a deposit, withdrawal or migration amount, a deposit or migration destination share-price bound, a Blue collateral amount, a deadline, force-redeem shares or deallocation amount is not positive.
130
+ * @throws {NonPositiveInputError} If a deposit, withdrawal or migration amount, a deposit or migration destination share-price bound, a collateral amount, Midnight redeem units, a deadline, force-redeem shares or deallocation amount is not positive, or a Midnight repayment step has a nonpositive full repayment cap or neither repays nor withdraws.
81
131
  * @throws {MarketIdMismatchError} If a Blue collateral step's market ID doesn't match its market parameters.
132
+ * @throws {MidnightMarketAddressMismatchError} If a Midnight collateral, redeem or repayment step's market targets another Midnight deployment.
133
+ * @throws {ChainIdMismatchError} If a Midnight redeem or repayment step's market targets another chain.
134
+ * @throws {MidnightZeroOnBehalfError} If a Midnight redeem, `midnightWithdrawCollateral`, `midnightWithdrawCollateralBatch` or `midnightSupplyCollateralWithNative` step names the zero address as its account.
135
+ * @throws {MidnightZeroReceiverError} If a Midnight redeem, `midnightWithdrawCollateral` or `midnightWithdrawCollateralBatch` step, or a `midnightRepayWithdrawCollateral` step that withdraws collateral, names the zero address as its receiver.
136
+ * @throws {MidnightContractReceiverError} If a Midnight redeem, `midnightWithdrawCollateral` or `midnightWithdrawCollateralBatch` step, or a `midnightRepayWithdrawCollateral` step that withdraws collateral, names a blocked protocol or token contract as its receiver.
137
+ * @throws {MidnightCalldataEncodingError} If the market tuple or withdrawal arguments cannot be encoded for a Midnight redeem step, or a Midnight repayment step has malformed addresses or other unencodable inputs.
138
+ * @throws {InvalidMarketParameterError} If a Midnight collateral, redeem, take or repayment step has an invalid market configuration.
139
+ * @throws {UnknownCollateralIndexError} If a Midnight collateral or repayment step targets an index not configured on its market.
82
140
  * @throws {NativeAmountOnNonWNativeAssetError} If a native-funded Blue collateral step targets a token other than wNative.
83
141
  * @throws {ChainWNativeMissingError} If a native-funded Blue collateral step targets a chain without registered wNative.
84
142
  * @throws {EmptyDeallocationsError} If a force-redeem step has no deallocations.
@@ -90,8 +148,10 @@ export declare function getPendingSignatureRequests(plan: TransactionPlan): read
90
148
  * @throws {TransactionPlanSignatureMismatchError} If a referenced request disagrees with the step's action or its encoding arguments.
91
149
  * @throws {BundlesPermitMismatchError} If a stored signature cannot be parsed or a signed permit's encoding arguments do not match the step.
92
150
  * @throws {MarketIdMismatchError} If a Blue market ID differs from its parameter tuple.
93
- * @throws {NegativeInputError} If a Blue amount, native value, LTV bound, maxPenalty or referral fee is negative.
94
- * @throws {InputExceedsMaxError} If a Blue amount, bound or deadline exceeds uint256, maxPenalty exceeds WAD, a reallocation penalty exceeds maxPenalty (default zero, rejecting any positive penalty), or withdrawal-only funding is nonzero.
151
+ * @throws {NegativeInputError} If a Blue amount, native value, LTV bound, maxPenalty or referral fee is negative, or a Midnight repayment's exact asset amount or native funding is negative.
152
+ * @throws {InputExceedsMaxError} If a Blue amount, bound or deadline exceeds uint256, maxPenalty exceeds WAD, a reallocation penalty exceeds maxPenalty (default zero, rejecting any positive penalty), withdrawal-only funding is nonzero, Midnight redeem units exceed uint128, or a Midnight batch collateral supply amount exceeds uint128.
153
+ * For `midnightRepayWithdrawCollateral`, if `repay.assets` or `repay.maxBuyerAssets` is at least `maxUint256`,
154
+ * a withdrawal amount or deadline exceeds uint256, or native funding exceeds the repayment pull.
95
155
  * @throws {MutuallyExclusiveRepayAmountsError} If a Blue repayment specifies both nonzero assets and shares.
96
156
  * @throws {MaxRepayAssetsBelowRepayAssetsError} If exact-asset funding cannot cover repayment plus referral fees.
97
157
  * @throws {NativeFundingAmountMismatchError} If Blue native funding does not equal the full repayment cap.
@@ -104,8 +164,15 @@ export declare function getPendingSignatureRequests(plan: TransactionPlan): read
104
164
  * @throws {DepositSpenderMismatchError} If a token signature names another spender.
105
165
  * @throws {UnsupportedRequirementSignatureError} If a token signature has an unsupported action type.
106
166
  * @throws {BundlesRequirementSignatureMismatchError} If a Blue signature cannot be bound or parsed safely.
107
- * @throws {UnsupportedChainIdError} If a Blue action's chain is absent from the registry.
108
- * @throws {UnknownAddressError} If BlueBundlesV1 is not registered on the action's chain.
167
+ * @throws {UnsupportedChainIdError} If a Blue or Midnight action's chain is absent from the registry.
168
+ * @throws {UnknownAddressError} If BlueBundlesV1, Midnight core, or MidnightBundlesV2 for a Midnight repayment step is not registered on the action's chain.
169
+ * @throws {EmptyMidnightGroupCancellationsError} If a Midnight cancellation lists no groups.
170
+ * @throws {InvalidMidnightHexInputError} If a Midnight cancellation group is not 32 bytes.
171
+ * @throws {DuplicateMidnightGroupCancellationError} If a Midnight cancellation lists a group twice.
172
+ * @throws {EmptyMidnightCollateralWithdrawalsError} If a Midnight batch collateral withdrawal lists no withdrawals.
173
+ * @throws {EmptyMidnightCollateralSuppliesError} If a Midnight batch collateral supply lists no supplies.
174
+ * @throws {DuplicateMidnightCollateralIndexError} If a Midnight batch collateral supply or withdrawal repeats a collateral index.
175
+ * @throws {MidnightZeroOnBehalfError} If a Midnight cancellation's account is the zero address.
109
176
  * @example
110
177
  * ```ts
111
178
  * import { buildTransaction, parseTransactionPlan } from "@morpho-org/morpho-sdk";
@@ -121,16 +188,23 @@ export declare function buildTransaction(plan: TransactionPlan, step: Step): Rea
121
188
  * Builds every step of the plan in order.
122
189
  * @param plan - Plan whose referenced signature requests all carry a signature.
123
190
  * @returns Frozen SDK transactions in step order; discriminate on `action.type` for per-step handling.
124
- * @throws {InvalidTransactionPlanError} If the plan header, action context, IDs or deadlines are inconsistent.
125
- * @throws {InvalidBigIntInputError} If a supplied Blue maxPenalty is not a bigint; uint256 schema validation is performed by parseTransactionPlan, not this builder.
191
+ * @throws {InvalidTransactionPlanError} If the plan header, action context, IDs or deadlines are inconsistent, a Midnight repayment type is unsupported, a Midnight market lists its collateral out of ascending token order, a Midnight single-collateral step has no bigint `collateralIndex`, or a Midnight collateral batch's list is not an array or holds a non-object entry (path under `$.inputs`).
192
+ * @throws {InvalidBigIntInputError} If a supplied Blue maxPenalty, or a Midnight collateral batch entry's `collateralIndex` or `assets`, is not a bigint; uint256 schema validation is performed by parseTransactionPlan, not this builder.
126
193
  * @throws {InvalidSignatureError} If a stored signature has high s, cannot recover a public key or does not match the request owner.
127
194
  * @throws {UnsupportedTransactionPlanVersionError} If the plan version is unsupported, including empty plans.
128
195
  * @throws {UnsupportedTransactionPlanActionError} If any plan step names an unsupported action type, before reading requests.
129
196
  * @throws {UnsupportedErc20ApprovalSpenderError} If an approval names an unregistered spender.
130
197
  * @throws {SameVaultMigrationError} If a migration's source and destination vaults are identical.
131
198
  * @throws {AmountAndSharesExclusiveError} If a migration supplies both `assets` and `shares`, or neither.
132
- * @throws {NonPositiveInputError} If a deposit, withdrawal or migration amount, a deposit or migration destination share-price bound, a Blue collateral amount, a deadline, force-redeem shares or deallocation amount is not positive.
199
+ * @throws {NonPositiveInputError} If a deposit, withdrawal or migration amount, a deposit or migration destination share-price bound, a collateral amount, Midnight redeem units, a deadline, force-redeem shares or deallocation amount is not positive, or a Midnight repayment step has a nonpositive full repayment cap or neither repays nor withdraws.
133
200
  * @throws {MarketIdMismatchError} If a Blue collateral step's market ID doesn't match its market parameters.
201
+ * @throws {MidnightMarketAddressMismatchError} If a Midnight collateral, redeem or repayment step's market targets another Midnight deployment.
202
+ * @throws {ChainIdMismatchError} If a Midnight redeem or repayment step's market targets another chain.
203
+ * @throws {MidnightZeroOnBehalfError} If a Midnight redeem, `midnightWithdrawCollateral`, `midnightWithdrawCollateralBatch` or `midnightSupplyCollateralWithNative` step names the zero address as its account.
204
+ * @throws {MidnightZeroReceiverError} If a Midnight redeem, `midnightWithdrawCollateral` or `midnightWithdrawCollateralBatch` step, or a `midnightRepayWithdrawCollateral` step that withdraws collateral, names the zero address as its receiver.
205
+ * @throws {MidnightContractReceiverError} If a Midnight redeem, `midnightWithdrawCollateral` or `midnightWithdrawCollateralBatch` step, or a `midnightRepayWithdrawCollateral` step that withdraws collateral, names a blocked protocol or token contract as its receiver.
206
+ * @throws {MidnightCalldataEncodingError} If the market tuple or withdrawal arguments cannot be encoded for a Midnight redeem step, or a Midnight repayment step has malformed addresses or other unencodable inputs.
207
+ * @throws {UnknownCollateralIndexError} If a Midnight collateral or repayment step targets an index not configured on its market.
134
208
  * @throws {NativeAmountOnNonWNativeAssetError} If a native-funded Blue collateral step targets a token other than wNative.
135
209
  * @throws {ChainWNativeMissingError} If a native-funded Blue collateral step targets a chain without registered wNative.
136
210
  * @throws {EmptyDeallocationsError} If a force-redeem step has no deallocations.
@@ -139,11 +213,14 @@ export declare function buildTransaction(plan: TransactionPlan, step: Step): Rea
139
213
  * @throws {MidnightReferralFeePctExceededError} when the referral fee is above 10% but below WAD.
140
214
  * @throws {ReferralFeeRecipientMissingError} If a positive referral fee has no nonzero recipient.
141
215
  * @throws {IncompleteTransactionPlanError} If any step references a signature request or signature that is missing.
216
+ * @throws {InvalidMarketParameterError} If a Midnight collateral, redeem, take or repayment step has an invalid market configuration.
142
217
  * @throws {TransactionPlanSignatureMismatchError} If a referenced request disagrees with its action or encoding arguments.
143
218
  * @throws {BundlesPermitMismatchError} If a stored signature cannot be parsed or a signed permit's encoding arguments do not match the step.
144
219
  * @throws {MarketIdMismatchError} If a Blue market ID differs from its parameter tuple.
145
- * @throws {NegativeInputError} If a Blue amount, native value, LTV bound, maxPenalty or referral fee is negative.
146
- * @throws {InputExceedsMaxError} If a Blue amount, bound or deadline exceeds uint256, maxPenalty exceeds WAD, a reallocation penalty exceeds maxPenalty (default zero, rejecting any positive penalty), or withdrawal-only funding is nonzero.
220
+ * @throws {NegativeInputError} If a Blue amount, native value, LTV bound, maxPenalty or referral fee is negative, or a Midnight repayment's exact asset amount or native funding is negative.
221
+ * @throws {InputExceedsMaxError} If a Blue amount, bound or deadline exceeds uint256, maxPenalty exceeds WAD, a reallocation penalty exceeds maxPenalty (default zero, rejecting any positive penalty), withdrawal-only funding is nonzero, Midnight redeem units exceed uint128, or a Midnight batch collateral supply amount exceeds uint128.
222
+ * For `midnightRepayWithdrawCollateral`, if `repay.assets` or `repay.maxBuyerAssets` is at least `maxUint256`,
223
+ * a withdrawal amount or deadline exceeds uint256, or native funding exceeds the repayment pull.
147
224
  * @throws {MutuallyExclusiveRepayAmountsError} If a Blue repayment specifies both nonzero assets and shares.
148
225
  * @throws {MaxRepayAssetsBelowRepayAssetsError} If exact-asset funding cannot cover repayment plus referral fees.
149
226
  * @throws {NativeFundingAmountMismatchError} If Blue native funding does not equal the full repayment cap.
@@ -156,8 +233,15 @@ export declare function buildTransaction(plan: TransactionPlan, step: Step): Rea
156
233
  * @throws {DepositSpenderMismatchError} If a token signature names another spender.
157
234
  * @throws {UnsupportedRequirementSignatureError} If a token signature has an unsupported action type.
158
235
  * @throws {BundlesRequirementSignatureMismatchError} If a Blue signature cannot be bound or parsed safely.
159
- * @throws {UnsupportedChainIdError} If a Blue action's chain is absent from the registry.
160
- * @throws {UnknownAddressError} If BlueBundlesV1 is not registered on the action's chain.
236
+ * @throws {UnsupportedChainIdError} If a Blue or Midnight action's chain is absent from the registry.
237
+ * @throws {UnknownAddressError} If BlueBundlesV1, Midnight core, or MidnightBundlesV2 for a Midnight repayment step is not registered on the action's chain.
238
+ * @throws {EmptyMidnightGroupCancellationsError} If a Midnight cancellation lists no groups.
239
+ * @throws {InvalidMidnightHexInputError} If a Midnight cancellation group is not 32 bytes.
240
+ * @throws {DuplicateMidnightGroupCancellationError} If a Midnight cancellation lists a group twice.
241
+ * @throws {EmptyMidnightCollateralWithdrawalsError} If a Midnight batch collateral withdrawal lists no withdrawals.
242
+ * @throws {EmptyMidnightCollateralSuppliesError} If a Midnight batch collateral supply lists no supplies.
243
+ * @throws {DuplicateMidnightCollateralIndexError} If a Midnight batch collateral supply or withdrawal repeats a collateral index.
244
+ * @throws {MidnightZeroOnBehalfError} If a Midnight cancellation's account is the zero address.
161
245
  * @example
162
246
  * ```ts
163
247
  * import { buildTransactions, type TransactionPlan } from "@morpho-org/morpho-sdk";
@@ -14,11 +14,27 @@ import { buildBlueSupply, validateBlueSupplyPermitRequest, } from "./blue/supply
14
14
  import { buildBlueSupplyCollateral, validateSupplyCollateralPermitRequest, } from "./blue/supplyCollateral.js";
15
15
  import { buildBlueSupplyCollateralBorrow, validateSupplyCollateralBorrowRequest, } from "./blue/supplyCollateralBorrow.js";
16
16
  import { buildBlueWithdraw, validateBlueWithdrawAuthorizationRequest, } from "./blue/withdraw.js";
17
+ import { buildBlueWithdrawBuyCallback } from "./blue/withdrawBuyCallback.js";
17
18
  import { buildBlueWithdrawCollateral, validateBlueWithdrawCollateralAuthorizationRequest, } from "./blue/withdrawCollateral.js";
18
19
  import { IncompleteTransactionPlanError, TransactionPlanSignatureMismatchError, } from "./error.js";
20
+ import { buildMidnightAuthorization } from "./midnight/authorization.js";
21
+ import { buildMidnightCancelOffers } from "./midnight/cancelOffers.js";
22
+ import { buildMidnightMakerPublication } from "./midnight/makerPublication.js";
23
+ import { buildMidnightRedeem } from "./midnight/redeem.js";
24
+ import { buildMidnightRepayWithdrawCollateral } from "./midnight/repayWithdrawCollateral.js";
25
+ import { buildMidnightSupplyCollateral } from "./midnight/supplyCollateral.js";
26
+ import { buildMidnightSupplyCollateralBatch } from "./midnight/supplyCollateralBatch.js";
27
+ import { buildMidnightSupplyCollateralWithNative } from "./midnight/supplyCollateralWithNative.js";
28
+ import { buildMidnightTakeBorrow } from "./midnight/takeBorrow.js";
29
+ import { buildMidnightTakeLend } from "./midnight/takeLend.js";
30
+ import { buildMidnightTakeRepayWithdrawCollateral } from "./midnight/takeRepayWithdrawCollateral.js";
31
+ import { buildMidnightTakeWithdraw } from "./midnight/takeWithdraw.js";
32
+ import { buildMidnightWithdrawCollateral } from "./midnight/withdrawCollateral.js";
33
+ import { buildMidnightWithdrawCollateralBatch } from "./midnight/withdrawCollateralBatch.js";
19
34
  import { validateVaultPermitRequest } from "./permit.js";
20
35
  import { validateTransactionPlanActions } from "./validateActions.js";
21
36
  import { validateTransactionPlanHeader } from "./validation.js";
37
+ import { isVaultPermitAction } from "./vaultBinding.js";
22
38
  import { buildVaultV1Deposit } from "./vaultv1/deposit.js";
23
39
  import { buildVaultV1InKindRedeem } from "./vaultv1/inKindRedeem.js";
24
40
  import { buildVaultV1MigrateToV2 } from "./vaultv1/migrateToV2.js";
@@ -229,17 +245,7 @@ function validateSignatureRequest(request, action) {
229
245
  nonce: action.inputs.authorizationNonce,
230
246
  });
231
247
  }
232
- if (request.kind === "permit" &&
233
- (action.type === "vaultV2Deposit" ||
234
- action.type === "vaultV1Deposit" ||
235
- action.type === "vaultV1Withdraw" ||
236
- action.type === "vaultV2Withdraw" ||
237
- action.type === "vaultV2Redeem" ||
238
- action.type === "vaultV1Redeem" ||
239
- action.type === "vaultV2ForceWithdraw" ||
240
- action.type === "vaultV1MigrateToV2" ||
241
- action.type === "vaultV2InKindRedeem" ||
242
- action.type === "vaultV1InKindRedeem")) {
248
+ if (request.kind === "permit" && isVaultPermitAction(action)) {
243
249
  validateVaultPermitRequest(request, action);
244
250
  return;
245
251
  }
@@ -322,6 +328,8 @@ function encodeStep(action, requests) {
322
328
  return buildBlueWithdraw(action, requests);
323
329
  case "blueWithdrawCollateral":
324
330
  return buildBlueWithdrawCollateral(action, requests);
331
+ case "blueWithdrawBuyCallback":
332
+ return buildBlueWithdrawBuyCallback(action);
325
333
  case "blueRepayWithdrawCollateral":
326
334
  return buildBlueRepayWithdrawCollateral(action, requests);
327
335
  case "blueRefinance":
@@ -335,6 +343,40 @@ function encodeStep(action, requests) {
335
343
  return buildVaultV1Deposit(action, requests);
336
344
  case "morphoAuthorization":
337
345
  return buildMorphoAuthorization(action);
346
+ case "midnightAuthorization":
347
+ return buildMidnightAuthorization(action);
348
+ case "midnightMakeBorrow":
349
+ case "midnightMakeLend":
350
+ case "midnightMakeRepay":
351
+ case "midnightMakeWithdraw":
352
+ case "midnightSupplyCollateralMakeBorrow":
353
+ case "midnightSupplyBlueMakeLend":
354
+ return buildMidnightMakerPublication(action);
355
+ case "midnightTakeBorrow":
356
+ case "midnightSupplyCollateralTakeBorrow":
357
+ return buildMidnightTakeBorrow(action);
358
+ case "midnightTakeWithdraw":
359
+ return buildMidnightTakeWithdraw(action);
360
+ case "midnightRepayWithdrawCollateral":
361
+ return buildMidnightRepayWithdrawCollateral(action);
362
+ case "midnightTakeLend":
363
+ return buildMidnightTakeLend(action);
364
+ case "midnightSupplyCollateral":
365
+ return buildMidnightSupplyCollateral(action);
366
+ case "midnightSupplyCollateralBatch":
367
+ return buildMidnightSupplyCollateralBatch(action);
368
+ case "midnightSupplyCollateralWithNative":
369
+ return buildMidnightSupplyCollateralWithNative(action);
370
+ case "midnightWithdrawCollateral":
371
+ return buildMidnightWithdrawCollateral(action);
372
+ case "midnightWithdrawCollateralBatch":
373
+ return buildMidnightWithdrawCollateralBatch(action);
374
+ case "midnightRedeem":
375
+ return buildMidnightRedeem(action);
376
+ case "midnightTakeRepayWithdrawCollateral":
377
+ return buildMidnightTakeRepayWithdrawCollateral(action);
378
+ case "midnightCancelOffers":
379
+ return buildMidnightCancelOffers(action);
338
380
  case "vaultV2Deposit":
339
381
  return buildVaultV2Deposit(action, requests);
340
382
  case "vaultV2Withdraw":
@@ -351,6 +393,56 @@ function encodeStep(action, requests) {
351
393
  return buildVaultV2InKindRedeem(action, requests);
352
394
  }
353
395
  }
396
+ /**
397
+ * @internal Whether a step's transaction carries the plan's `calldataOrigin`. Operation steps do;
398
+ * approval and authorization prerequisites never do, matching the entity path. Every action type
399
+ * must be listed, so a new type does not compile until its role is chosen.
400
+ */
401
+ export const CALLDATA_ORIGIN_ROLE = {
402
+ blueBorrow: "operation",
403
+ blueRefinance: "operation",
404
+ blueRepay: "operation",
405
+ blueRepayWithdrawCollateral: "operation",
406
+ blueSupply: "operation",
407
+ blueSupplyCollateral: "operation",
408
+ blueSupplyCollateralBorrow: "operation",
409
+ blueWithdraw: "operation",
410
+ blueWithdrawBuyCallback: "operation",
411
+ blueWithdrawCollateral: "operation",
412
+ erc20Approval: "prerequisite",
413
+ midnightAuthorization: "prerequisite",
414
+ midnightCancelOffers: "operation",
415
+ midnightMakeBorrow: "operation",
416
+ midnightMakeLend: "operation",
417
+ midnightMakeRepay: "operation",
418
+ midnightMakeWithdraw: "operation",
419
+ midnightRedeem: "operation",
420
+ midnightRepayWithdrawCollateral: "operation",
421
+ midnightSupplyBlueMakeLend: "operation",
422
+ midnightSupplyCollateral: "operation",
423
+ midnightSupplyCollateralBatch: "operation",
424
+ midnightSupplyCollateralMakeBorrow: "operation",
425
+ midnightSupplyCollateralTakeBorrow: "operation",
426
+ midnightSupplyCollateralWithNative: "operation",
427
+ midnightTakeBorrow: "operation",
428
+ midnightTakeLend: "operation",
429
+ midnightTakeRepayWithdrawCollateral: "operation",
430
+ midnightTakeWithdraw: "operation",
431
+ midnightWithdrawCollateral: "operation",
432
+ midnightWithdrawCollateralBatch: "operation",
433
+ morphoAuthorization: "prerequisite",
434
+ vaultV1Deposit: "operation",
435
+ vaultV1InKindRedeem: "operation",
436
+ vaultV1MigrateToV2: "operation",
437
+ vaultV1Redeem: "operation",
438
+ vaultV1Withdraw: "operation",
439
+ vaultV2Deposit: "operation",
440
+ vaultV2ForceRedeem: "operation",
441
+ vaultV2ForceWithdraw: "operation",
442
+ vaultV2InKindRedeem: "operation",
443
+ vaultV2Redeem: "operation",
444
+ vaultV2Withdraw: "operation",
445
+ };
354
446
  /**
355
447
  * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
356
448
  * Builds one transaction from a step, its fixed inputs and the signatures its action names.
@@ -362,17 +454,25 @@ function encodeStep(action, requests) {
362
454
  * not operation amounts, market parameters or referral fees/recipients. Protect those unsigned fields.
363
455
  * @param plan - Plan supplying referenced signature requests and the calldata origin; chain and account come from the action inputs.
364
456
  * @param step - Recipe to encode; missing signatures on unrelated supported steps do not block it.
365
- * @returns A frozen `{ to, data, value, action }`, ready to simulate or submit. `data` ends with the plan's `calldataOrigin` when one was set.
366
- * @throws {InvalidTransactionPlanError} If the plan header, action context, IDs or deadlines are inconsistent.
367
- * @throws {InvalidBigIntInputError} If a supplied Blue maxPenalty is not a bigint; uint256 schema validation is performed by parseTransactionPlan, not this builder.
457
+ * @returns A frozen `{ to, data, value, action }`, ready to simulate or submit. `data` ends with the plan's `calldataOrigin` when one was set, except for ERC-20 approval, Morpho authorization and Midnight authorization prerequisite steps.
458
+ * @throws {InvalidTransactionPlanError} If the plan header, action context, IDs or deadlines are inconsistent, a Midnight repayment type is unsupported, a Midnight market lists its collateral out of ascending token order, a Midnight single-collateral step has no bigint `collateralIndex`, or a Midnight collateral batch's list is not an array or holds a non-object entry (path under `$.inputs`).
459
+ * @throws {InvalidBigIntInputError} If a supplied Blue maxPenalty, or a Midnight collateral batch entry's `collateralIndex` or `assets`, is not a bigint; uint256 schema validation is performed by parseTransactionPlan, not this builder.
368
460
  * @throws {InvalidSignatureError} If a stored signature has high s, cannot recover a public key or does not match the request owner.
369
461
  * @throws {UnsupportedTransactionPlanVersionError} If the plan version is unsupported.
370
462
  * @throws {UnsupportedTransactionPlanActionError} If any plan step or the selected step names an unsupported action type.
371
463
  * @throws {UnsupportedErc20ApprovalSpenderError} If an approval names an unregistered spender.
372
464
  * @throws {SameVaultMigrationError} If a migration's source and destination vaults are identical.
373
465
  * @throws {AmountAndSharesExclusiveError} If a migration supplies both `assets` and `shares`, or neither.
374
- * @throws {NonPositiveInputError} If a deposit, withdrawal or migration amount, a deposit or migration destination share-price bound, a Blue collateral amount, a deadline, force-redeem shares or deallocation amount is not positive.
466
+ * @throws {NonPositiveInputError} If a deposit, withdrawal or migration amount, a deposit or migration destination share-price bound, a collateral amount, Midnight redeem units, a deadline, force-redeem shares or deallocation amount is not positive, or a Midnight repayment step has a nonpositive full repayment cap or neither repays nor withdraws.
375
467
  * @throws {MarketIdMismatchError} If a Blue collateral step's market ID doesn't match its market parameters.
468
+ * @throws {MidnightMarketAddressMismatchError} If a Midnight collateral, redeem or repayment step's market targets another Midnight deployment.
469
+ * @throws {ChainIdMismatchError} If a Midnight redeem or repayment step's market targets another chain.
470
+ * @throws {MidnightZeroOnBehalfError} If a Midnight redeem, `midnightWithdrawCollateral`, `midnightWithdrawCollateralBatch` or `midnightSupplyCollateralWithNative` step names the zero address as its account.
471
+ * @throws {MidnightZeroReceiverError} If a Midnight redeem, `midnightWithdrawCollateral` or `midnightWithdrawCollateralBatch` step, or a `midnightRepayWithdrawCollateral` step that withdraws collateral, names the zero address as its receiver.
472
+ * @throws {MidnightContractReceiverError} If a Midnight redeem, `midnightWithdrawCollateral` or `midnightWithdrawCollateralBatch` step, or a `midnightRepayWithdrawCollateral` step that withdraws collateral, names a blocked protocol or token contract as its receiver.
473
+ * @throws {MidnightCalldataEncodingError} If the market tuple or withdrawal arguments cannot be encoded for a Midnight redeem step, or a Midnight repayment step has malformed addresses or other unencodable inputs.
474
+ * @throws {InvalidMarketParameterError} If a Midnight collateral, redeem, take or repayment step has an invalid market configuration.
475
+ * @throws {UnknownCollateralIndexError} If a Midnight collateral or repayment step targets an index not configured on its market.
376
476
  * @throws {NativeAmountOnNonWNativeAssetError} If a native-funded Blue collateral step targets a token other than wNative.
377
477
  * @throws {ChainWNativeMissingError} If a native-funded Blue collateral step targets a chain without registered wNative.
378
478
  * @throws {EmptyDeallocationsError} If a force-redeem step has no deallocations.
@@ -384,8 +484,10 @@ function encodeStep(action, requests) {
384
484
  * @throws {TransactionPlanSignatureMismatchError} If a referenced request disagrees with the step's action or its encoding arguments.
385
485
  * @throws {BundlesPermitMismatchError} If a stored signature cannot be parsed or a signed permit's encoding arguments do not match the step.
386
486
  * @throws {MarketIdMismatchError} If a Blue market ID differs from its parameter tuple.
387
- * @throws {NegativeInputError} If a Blue amount, native value, LTV bound, maxPenalty or referral fee is negative.
388
- * @throws {InputExceedsMaxError} If a Blue amount, bound or deadline exceeds uint256, maxPenalty exceeds WAD, a reallocation penalty exceeds maxPenalty (default zero, rejecting any positive penalty), or withdrawal-only funding is nonzero.
487
+ * @throws {NegativeInputError} If a Blue amount, native value, LTV bound, maxPenalty or referral fee is negative, or a Midnight repayment's exact asset amount or native funding is negative.
488
+ * @throws {InputExceedsMaxError} If a Blue amount, bound or deadline exceeds uint256, maxPenalty exceeds WAD, a reallocation penalty exceeds maxPenalty (default zero, rejecting any positive penalty), withdrawal-only funding is nonzero, Midnight redeem units exceed uint128, or a Midnight batch collateral supply amount exceeds uint128.
489
+ * For `midnightRepayWithdrawCollateral`, if `repay.assets` or `repay.maxBuyerAssets` is at least `maxUint256`,
490
+ * a withdrawal amount or deadline exceeds uint256, or native funding exceeds the repayment pull.
389
491
  * @throws {MutuallyExclusiveRepayAmountsError} If a Blue repayment specifies both nonzero assets and shares.
390
492
  * @throws {MaxRepayAssetsBelowRepayAssetsError} If exact-asset funding cannot cover repayment plus referral fees.
391
493
  * @throws {NativeFundingAmountMismatchError} If Blue native funding does not equal the full repayment cap.
@@ -398,8 +500,15 @@ function encodeStep(action, requests) {
398
500
  * @throws {DepositSpenderMismatchError} If a token signature names another spender.
399
501
  * @throws {UnsupportedRequirementSignatureError} If a token signature has an unsupported action type.
400
502
  * @throws {BundlesRequirementSignatureMismatchError} If a Blue signature cannot be bound or parsed safely.
401
- * @throws {UnsupportedChainIdError} If a Blue action's chain is absent from the registry.
402
- * @throws {UnknownAddressError} If BlueBundlesV1 is not registered on the action's chain.
503
+ * @throws {UnsupportedChainIdError} If a Blue or Midnight action's chain is absent from the registry.
504
+ * @throws {UnknownAddressError} If BlueBundlesV1, Midnight core, or MidnightBundlesV2 for a Midnight repayment step is not registered on the action's chain.
505
+ * @throws {EmptyMidnightGroupCancellationsError} If a Midnight cancellation lists no groups.
506
+ * @throws {InvalidMidnightHexInputError} If a Midnight cancellation group is not 32 bytes.
507
+ * @throws {DuplicateMidnightGroupCancellationError} If a Midnight cancellation lists a group twice.
508
+ * @throws {EmptyMidnightCollateralWithdrawalsError} If a Midnight batch collateral withdrawal lists no withdrawals.
509
+ * @throws {EmptyMidnightCollateralSuppliesError} If a Midnight batch collateral supply lists no supplies.
510
+ * @throws {DuplicateMidnightCollateralIndexError} If a Midnight batch collateral supply or withdrawal repeats a collateral index.
511
+ * @throws {MidnightZeroOnBehalfError} If a Midnight cancellation's account is the zero address.
403
512
  * @example
404
513
  * ```ts
405
514
  * import { buildTransaction, parseTransactionPlan } from "@morpho-org/morpho-sdk";
@@ -450,7 +559,8 @@ export function buildTransaction(plan, step) {
450
559
  return deepFreeze({
451
560
  to,
452
561
  value,
453
- data: plan.calldataOrigin === undefined
562
+ data: plan.calldataOrigin === undefined ||
563
+ CALLDATA_ORIGIN_ROLE[step.action.type] === "prerequisite"
454
564
  ? data
455
565
  : concatHex([data, plan.calldataOrigin]),
456
566
  action,
@@ -461,16 +571,23 @@ export function buildTransaction(plan, step) {
461
571
  * Builds every step of the plan in order.
462
572
  * @param plan - Plan whose referenced signature requests all carry a signature.
463
573
  * @returns Frozen SDK transactions in step order; discriminate on `action.type` for per-step handling.
464
- * @throws {InvalidTransactionPlanError} If the plan header, action context, IDs or deadlines are inconsistent.
465
- * @throws {InvalidBigIntInputError} If a supplied Blue maxPenalty is not a bigint; uint256 schema validation is performed by parseTransactionPlan, not this builder.
574
+ * @throws {InvalidTransactionPlanError} If the plan header, action context, IDs or deadlines are inconsistent, a Midnight repayment type is unsupported, a Midnight market lists its collateral out of ascending token order, a Midnight single-collateral step has no bigint `collateralIndex`, or a Midnight collateral batch's list is not an array or holds a non-object entry (path under `$.inputs`).
575
+ * @throws {InvalidBigIntInputError} If a supplied Blue maxPenalty, or a Midnight collateral batch entry's `collateralIndex` or `assets`, is not a bigint; uint256 schema validation is performed by parseTransactionPlan, not this builder.
466
576
  * @throws {InvalidSignatureError} If a stored signature has high s, cannot recover a public key or does not match the request owner.
467
577
  * @throws {UnsupportedTransactionPlanVersionError} If the plan version is unsupported, including empty plans.
468
578
  * @throws {UnsupportedTransactionPlanActionError} If any plan step names an unsupported action type, before reading requests.
469
579
  * @throws {UnsupportedErc20ApprovalSpenderError} If an approval names an unregistered spender.
470
580
  * @throws {SameVaultMigrationError} If a migration's source and destination vaults are identical.
471
581
  * @throws {AmountAndSharesExclusiveError} If a migration supplies both `assets` and `shares`, or neither.
472
- * @throws {NonPositiveInputError} If a deposit, withdrawal or migration amount, a deposit or migration destination share-price bound, a Blue collateral amount, a deadline, force-redeem shares or deallocation amount is not positive.
582
+ * @throws {NonPositiveInputError} If a deposit, withdrawal or migration amount, a deposit or migration destination share-price bound, a collateral amount, Midnight redeem units, a deadline, force-redeem shares or deallocation amount is not positive, or a Midnight repayment step has a nonpositive full repayment cap or neither repays nor withdraws.
473
583
  * @throws {MarketIdMismatchError} If a Blue collateral step's market ID doesn't match its market parameters.
584
+ * @throws {MidnightMarketAddressMismatchError} If a Midnight collateral, redeem or repayment step's market targets another Midnight deployment.
585
+ * @throws {ChainIdMismatchError} If a Midnight redeem or repayment step's market targets another chain.
586
+ * @throws {MidnightZeroOnBehalfError} If a Midnight redeem, `midnightWithdrawCollateral`, `midnightWithdrawCollateralBatch` or `midnightSupplyCollateralWithNative` step names the zero address as its account.
587
+ * @throws {MidnightZeroReceiverError} If a Midnight redeem, `midnightWithdrawCollateral` or `midnightWithdrawCollateralBatch` step, or a `midnightRepayWithdrawCollateral` step that withdraws collateral, names the zero address as its receiver.
588
+ * @throws {MidnightContractReceiverError} If a Midnight redeem, `midnightWithdrawCollateral` or `midnightWithdrawCollateralBatch` step, or a `midnightRepayWithdrawCollateral` step that withdraws collateral, names a blocked protocol or token contract as its receiver.
589
+ * @throws {MidnightCalldataEncodingError} If the market tuple or withdrawal arguments cannot be encoded for a Midnight redeem step, or a Midnight repayment step has malformed addresses or other unencodable inputs.
590
+ * @throws {UnknownCollateralIndexError} If a Midnight collateral or repayment step targets an index not configured on its market.
474
591
  * @throws {NativeAmountOnNonWNativeAssetError} If a native-funded Blue collateral step targets a token other than wNative.
475
592
  * @throws {ChainWNativeMissingError} If a native-funded Blue collateral step targets a chain without registered wNative.
476
593
  * @throws {EmptyDeallocationsError} If a force-redeem step has no deallocations.
@@ -479,11 +596,14 @@ export function buildTransaction(plan, step) {
479
596
  * @throws {MidnightReferralFeePctExceededError} when the referral fee is above 10% but below WAD.
480
597
  * @throws {ReferralFeeRecipientMissingError} If a positive referral fee has no nonzero recipient.
481
598
  * @throws {IncompleteTransactionPlanError} If any step references a signature request or signature that is missing.
599
+ * @throws {InvalidMarketParameterError} If a Midnight collateral, redeem, take or repayment step has an invalid market configuration.
482
600
  * @throws {TransactionPlanSignatureMismatchError} If a referenced request disagrees with its action or encoding arguments.
483
601
  * @throws {BundlesPermitMismatchError} If a stored signature cannot be parsed or a signed permit's encoding arguments do not match the step.
484
602
  * @throws {MarketIdMismatchError} If a Blue market ID differs from its parameter tuple.
485
- * @throws {NegativeInputError} If a Blue amount, native value, LTV bound, maxPenalty or referral fee is negative.
486
- * @throws {InputExceedsMaxError} If a Blue amount, bound or deadline exceeds uint256, maxPenalty exceeds WAD, a reallocation penalty exceeds maxPenalty (default zero, rejecting any positive penalty), or withdrawal-only funding is nonzero.
603
+ * @throws {NegativeInputError} If a Blue amount, native value, LTV bound, maxPenalty or referral fee is negative, or a Midnight repayment's exact asset amount or native funding is negative.
604
+ * @throws {InputExceedsMaxError} If a Blue amount, bound or deadline exceeds uint256, maxPenalty exceeds WAD, a reallocation penalty exceeds maxPenalty (default zero, rejecting any positive penalty), withdrawal-only funding is nonzero, Midnight redeem units exceed uint128, or a Midnight batch collateral supply amount exceeds uint128.
605
+ * For `midnightRepayWithdrawCollateral`, if `repay.assets` or `repay.maxBuyerAssets` is at least `maxUint256`,
606
+ * a withdrawal amount or deadline exceeds uint256, or native funding exceeds the repayment pull.
487
607
  * @throws {MutuallyExclusiveRepayAmountsError} If a Blue repayment specifies both nonzero assets and shares.
488
608
  * @throws {MaxRepayAssetsBelowRepayAssetsError} If exact-asset funding cannot cover repayment plus referral fees.
489
609
  * @throws {NativeFundingAmountMismatchError} If Blue native funding does not equal the full repayment cap.
@@ -496,8 +616,15 @@ export function buildTransaction(plan, step) {
496
616
  * @throws {DepositSpenderMismatchError} If a token signature names another spender.
497
617
  * @throws {UnsupportedRequirementSignatureError} If a token signature has an unsupported action type.
498
618
  * @throws {BundlesRequirementSignatureMismatchError} If a Blue signature cannot be bound or parsed safely.
499
- * @throws {UnsupportedChainIdError} If a Blue action's chain is absent from the registry.
500
- * @throws {UnknownAddressError} If BlueBundlesV1 is not registered on the action's chain.
619
+ * @throws {UnsupportedChainIdError} If a Blue or Midnight action's chain is absent from the registry.
620
+ * @throws {UnknownAddressError} If BlueBundlesV1, Midnight core, or MidnightBundlesV2 for a Midnight repayment step is not registered on the action's chain.
621
+ * @throws {EmptyMidnightGroupCancellationsError} If a Midnight cancellation lists no groups.
622
+ * @throws {InvalidMidnightHexInputError} If a Midnight cancellation group is not 32 bytes.
623
+ * @throws {DuplicateMidnightGroupCancellationError} If a Midnight cancellation lists a group twice.
624
+ * @throws {EmptyMidnightCollateralWithdrawalsError} If a Midnight batch collateral withdrawal lists no withdrawals.
625
+ * @throws {EmptyMidnightCollateralSuppliesError} If a Midnight batch collateral supply lists no supplies.
626
+ * @throws {DuplicateMidnightCollateralIndexError} If a Midnight batch collateral supply or withdrawal repeats a collateral index.
627
+ * @throws {MidnightZeroOnBehalfError} If a Midnight cancellation's account is the zero address.
501
628
  * @example
502
629
  * ```ts
503
630
  * import { buildTransactions, type TransactionPlan } from "@morpho-org/morpho-sdk";
@@ -1,7 +1,8 @@
1
- import { deepFreeze, Time } from "@morpho-org/morpho-ts";
1
+ import { Time } from "@morpho-org/morpho-ts";
2
2
  import { normalizeBundlesCommonParams } from "../actions/bundles/common.js";
3
3
  import { validateUint256Field } from "../helpers/validate.js";
4
4
  import { NonPositiveInputError, } from "../types/index.js";
5
+ import { freezePlan, planHeader } from "./assemble.js";
5
6
  import { resolveIntent } from "./intent.js";
6
7
  import { getVaultShareSpendingRequirements } from "./shareSpending.js";
7
8
  import { validatePlanFields, validateVaultPlanAction } from "./validation.js";
@@ -26,12 +27,8 @@ export async function prepareVaultRedeem(client, params) {
26
27
  supportSignature: intent.supportSignature,
27
28
  now,
28
29
  });
29
- return deepFreeze(structuredClone({
30
- version: 2,
31
- account,
32
- chainId,
33
- expiresAt: intent.expiresAt,
34
- ...(intent.calldataOrigin && { calldataOrigin: intent.calldataOrigin }),
30
+ return freezePlan({
31
+ ...planHeader({ chainId, account }, intent),
35
32
  steps: [
36
33
  ...approvals,
37
34
  {
@@ -47,7 +44,7 @@ export async function prepareVaultRedeem(client, params) {
47
44
  },
48
45
  ],
49
46
  signatureRequests,
50
- }));
47
+ }, { detach: true });
51
48
  }
52
49
  /** @internal Validates stored V1 and V2 exact-share redemption inputs. */
53
50
  export function validateRedeemPlanAction(action, path) {