@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
@@ -0,0 +1,102 @@
1
+ import type { AccrualPosition } from "@morpho-org/midnight-sdk";
2
+ import type { Address, Client } from "viem";
3
+ import { type ReferenceTimestampParams } from "../../types/index.js";
4
+ import { type IntentParams } from "../intent.js";
5
+ import type { Action, TransactionPlan } from "../types.js";
6
+ /**
7
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
8
+ * Owner-bound snapshot and intent for redeeming Midnight credit.
9
+ */
10
+ export interface MidnightRedeemPlanParams extends Pick<IntentParams, "chainId" | "account" | "expiresAt" | "calldataOrigin">, ReferenceTimestampParams {
11
+ /** Position to redeem, as returned by the Midnight accrual position fetcher. */
12
+ readonly positionData: AccrualPosition;
13
+ /** Exact credit units to redeem. Defaults to the position's face value at `now`. */
14
+ readonly units?: bigint;
15
+ /** Recipient of the loan assets. Defaults to `account`. */
16
+ readonly receiver?: Address;
17
+ }
18
+ /**
19
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
20
+ * Prepares one direct Midnight withdrawal to redeem the account's credit. Validates the snapshot
21
+ * and amounts with `parseMidnightRedeemPreparation`, which applies the entity `redeem` checks in
22
+ * the same order.
23
+ * The selected units are fixed against the supplied snapshot and reference time.
24
+ * Fees may continue accruing before maturity. Expiry is checked during preparation only;
25
+ * it is not an onchain deadline.
26
+ *
27
+ * @param client - Client used to verify the chain, not retained by the plan.
28
+ * @param params.chainId - Chain containing the Midnight market.
29
+ * @param params.account - Position owner who submits the withdrawal.
30
+ * @param params.positionData - Owner-bound snapshot containing the market and its available liquidity.
31
+ * @param params.units - Optional positive credit units; defaults to the accrued position face value.
32
+ * @param params.receiver - Optional recipient; defaults to the owner. Midnight and market token contracts are rejected.
33
+ * @param params.now - Reference timestamp in seconds, normally the snapshot block's timestamp; omitting it uses the device clock (an untyped JavaScript `null` does too).
34
+ * @param params.expiresAt - Optional plan expiry in Unix seconds; defaults to two hours after `now`.
35
+ * @param params.calldataOrigin - Optional attribution bytes appended to the built transaction.
36
+ * @returns A frozen plan with one redemption step and no approvals, authorizations, or signature requests.
37
+ * @throws {InvalidBigIntInputError} If a supplied non-null `now` is not a bigint, or supplied
38
+ * units are not a bigint, including `null`.
39
+ * @throws {ZeroAccountError} If the account is the zero address.
40
+ * @throws {NonIntegerInputError} If expiry is not a safe integer.
41
+ * @throws {ExpiredDeadlineError} If expiry is not later than `now`.
42
+ * @throws {InvalidCalldataOriginError} If the attribution is not non-empty, even-length hex.
43
+ * @throws {InvalidTransactionPlanError} If the account is not a valid address or attribution exceeds 1 MiB.
44
+ * @throws {MissingAccrualPositionError} If no position snapshot is supplied.
45
+ * @throws {AccrualPositionUserMismatchError} If the snapshot is unbound or belongs to another account.
46
+ * @throws {ChainIdMismatchError} If the snapshot or client targets a different chain.
47
+ * @throws {MidnightMarketAddressMismatchError} If the snapshot targets a different Midnight deployment.
48
+ * @throws {InvalidMarketParameterError} If the snapshot market's chain ID or collateral configuration is invalid, or the position's market ID does not match its market.
49
+ * @throws {InvalidMidnightPositionLossFactorError} If the market loss factor is below the
50
+ * position's `lastLossFactor`, which `fetchMidnightAccrualPosition` snapshots never are.
51
+ * @throws {InvalidMidnightPositionAccrualStateError} If the snapshot's credit, pending fee or
52
+ * loss factor are inconsistent, which `fetchMidnightAccrualPosition` snapshots never are.
53
+ * @throws {NoMidnightCreditToRedeemError} If the default face value is zero.
54
+ * @throws {NonPositiveInputError} If expiry or explicitly supplied units are not positive.
55
+ * @throws {InputExceedsMaxError} If units exceed uint128.
56
+ * @throws {MidnightRedeemExceedsCreditError} If units exceed accrued credit.
57
+ * @throws {InsufficientMidnightWithdrawableLiquidityError} If units exceed available market liquidity.
58
+ * @throws {MidnightZeroReceiverError} If the receiver is the zero address.
59
+ * @throws {MidnightContractReceiverError} If the receiver is a blocked contract.
60
+ * @throws {MidnightCalldataEncodingError} If market fields cannot be ABI-encoded.
61
+ * @throws {UnsupportedChainIdError} If the chain is absent from the address registry.
62
+ * @throws {UnknownAddressError} If Midnight core is not registered on the chain.
63
+ * @throws {MissingContractCodeError} If the registered Midnight core has no deployed code.
64
+ * @throws {viem.BaseError} If the `eth_chainId` or bytecode request fails.
65
+ * @example
66
+ * ```ts
67
+ * import {
68
+ * buildTransactions,
69
+ * prepareMidnightRedeem,
70
+ * type TransactionPlan,
71
+ * } from "@morpho-org/morpho-sdk";
72
+ * import { fetchMidnightAccrualPosition } from "@morpho-org/morpho-sdk/fetch";
73
+ * import { type Address, type Hash, createPublicClient, http } from "viem";
74
+ * import { base } from "viem/chains";
75
+ *
76
+ * async function prepareCreditRedemption(account: Address, marketId: Hash) {
77
+ * const client = createPublicClient({ chain: base, transport: http() });
78
+ * const block = await client.getBlock();
79
+ * const positionData = await fetchMidnightAccrualPosition(client, {
80
+ * marketId,
81
+ * user: account,
82
+ * blockNumber: block.number,
83
+ * });
84
+ * const plan: TransactionPlan = await prepareMidnightRedeem(client, {
85
+ * chainId: base.id,
86
+ * account,
87
+ * positionData,
88
+ * now: block.timestamp,
89
+ * });
90
+ * // plan.steps: a single "midnightRedeem" step; plan.signatureRequests is empty.
91
+ * // Send the transaction from plan.account on plan.chainId.
92
+ * return buildTransactions(plan);
93
+ * }
94
+ * ```
95
+ */
96
+ export declare function prepareMidnightRedeem(client: Client, params: MidnightRedeemPlanParams): Promise<TransactionPlan>;
97
+ /** @internal Encodes the stored direct withdrawal without retaining the snapshot. */
98
+ export declare function buildMidnightRedeem(action: Extract<Action, {
99
+ type: "midnightRedeem";
100
+ }>): Readonly<import("../../types/action.js").Transaction<import("../../types/action.js").MidnightRedeemAction>>;
101
+ /** @internal Checks the stored redemption shape without consulting live state. */
102
+ export declare function validateMidnightRedeemPlanAction(action: Record<string, unknown>, path: string): void;
@@ -0,0 +1,165 @@
1
+ import { deepFreeze, getChainAddress, Time } from "@morpho-org/morpho-ts";
2
+ import { midnightRedeem, parseMidnightRedeemPreparation, } from "../../actions/midnight/redeem.js";
3
+ import { createChainCheck } from "../../actions/requirements/chainCheck.js";
4
+ import { assertRegistryTargetsHaveCode } from "../../entities/requirements/assertContractCode.js";
5
+ import { validateReferenceTime } from "../../helpers/validate.js";
6
+ import { InvalidBigIntInputError, } from "../../types/index.js";
7
+ import { resolveIntent } from "../intent.js";
8
+ import { getPlanObject, validatePlanFields } from "../validation.js";
9
+ import { validateSignaturelessMidnightAction } from "./authorization.js";
10
+ import { storedMarketTuple } from "./market.js";
11
+ import { validateStoredMidnightMarket } from "./validation.js";
12
+ /**
13
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
14
+ * Prepares one direct Midnight withdrawal to redeem the account's credit. Validates the snapshot
15
+ * and amounts with `parseMidnightRedeemPreparation`, which applies the entity `redeem` checks in
16
+ * the same order.
17
+ * The selected units are fixed against the supplied snapshot and reference time.
18
+ * Fees may continue accruing before maturity. Expiry is checked during preparation only;
19
+ * it is not an onchain deadline.
20
+ *
21
+ * @param client - Client used to verify the chain, not retained by the plan.
22
+ * @param params.chainId - Chain containing the Midnight market.
23
+ * @param params.account - Position owner who submits the withdrawal.
24
+ * @param params.positionData - Owner-bound snapshot containing the market and its available liquidity.
25
+ * @param params.units - Optional positive credit units; defaults to the accrued position face value.
26
+ * @param params.receiver - Optional recipient; defaults to the owner. Midnight and market token contracts are rejected.
27
+ * @param params.now - Reference timestamp in seconds, normally the snapshot block's timestamp; omitting it uses the device clock (an untyped JavaScript `null` does too).
28
+ * @param params.expiresAt - Optional plan expiry in Unix seconds; defaults to two hours after `now`.
29
+ * @param params.calldataOrigin - Optional attribution bytes appended to the built transaction.
30
+ * @returns A frozen plan with one redemption step and no approvals, authorizations, or signature requests.
31
+ * @throws {InvalidBigIntInputError} If a supplied non-null `now` is not a bigint, or supplied
32
+ * units are not a bigint, including `null`.
33
+ * @throws {ZeroAccountError} If the account is the zero address.
34
+ * @throws {NonIntegerInputError} If expiry is not a safe integer.
35
+ * @throws {ExpiredDeadlineError} If expiry is not later than `now`.
36
+ * @throws {InvalidCalldataOriginError} If the attribution is not non-empty, even-length hex.
37
+ * @throws {InvalidTransactionPlanError} If the account is not a valid address or attribution exceeds 1 MiB.
38
+ * @throws {MissingAccrualPositionError} If no position snapshot is supplied.
39
+ * @throws {AccrualPositionUserMismatchError} If the snapshot is unbound or belongs to another account.
40
+ * @throws {ChainIdMismatchError} If the snapshot or client targets a different chain.
41
+ * @throws {MidnightMarketAddressMismatchError} If the snapshot targets a different Midnight deployment.
42
+ * @throws {InvalidMarketParameterError} If the snapshot market's chain ID or collateral configuration is invalid, or the position's market ID does not match its market.
43
+ * @throws {InvalidMidnightPositionLossFactorError} If the market loss factor is below the
44
+ * position's `lastLossFactor`, which `fetchMidnightAccrualPosition` snapshots never are.
45
+ * @throws {InvalidMidnightPositionAccrualStateError} If the snapshot's credit, pending fee or
46
+ * loss factor are inconsistent, which `fetchMidnightAccrualPosition` snapshots never are.
47
+ * @throws {NoMidnightCreditToRedeemError} If the default face value is zero.
48
+ * @throws {NonPositiveInputError} If expiry or explicitly supplied units are not positive.
49
+ * @throws {InputExceedsMaxError} If units exceed uint128.
50
+ * @throws {MidnightRedeemExceedsCreditError} If units exceed accrued credit.
51
+ * @throws {InsufficientMidnightWithdrawableLiquidityError} If units exceed available market liquidity.
52
+ * @throws {MidnightZeroReceiverError} If the receiver is the zero address.
53
+ * @throws {MidnightContractReceiverError} If the receiver is a blocked contract.
54
+ * @throws {MidnightCalldataEncodingError} If market fields cannot be ABI-encoded.
55
+ * @throws {UnsupportedChainIdError} If the chain is absent from the address registry.
56
+ * @throws {UnknownAddressError} If Midnight core is not registered on the chain.
57
+ * @throws {MissingContractCodeError} If the registered Midnight core has no deployed code.
58
+ * @throws {viem.BaseError} If the `eth_chainId` or bytecode request fails.
59
+ * @example
60
+ * ```ts
61
+ * import {
62
+ * buildTransactions,
63
+ * prepareMidnightRedeem,
64
+ * type TransactionPlan,
65
+ * } from "@morpho-org/morpho-sdk";
66
+ * import { fetchMidnightAccrualPosition } from "@morpho-org/morpho-sdk/fetch";
67
+ * import { type Address, type Hash, createPublicClient, http } from "viem";
68
+ * import { base } from "viem/chains";
69
+ *
70
+ * async function prepareCreditRedemption(account: Address, marketId: Hash) {
71
+ * const client = createPublicClient({ chain: base, transport: http() });
72
+ * const block = await client.getBlock();
73
+ * const positionData = await fetchMidnightAccrualPosition(client, {
74
+ * marketId,
75
+ * user: account,
76
+ * blockNumber: block.number,
77
+ * });
78
+ * const plan: TransactionPlan = await prepareMidnightRedeem(client, {
79
+ * chainId: base.id,
80
+ * account,
81
+ * positionData,
82
+ * now: block.timestamp,
83
+ * });
84
+ * // plan.steps: a single "midnightRedeem" step; plan.signatureRequests is empty.
85
+ * // Send the transaction from plan.account on plan.chainId.
86
+ * return buildTransactions(plan);
87
+ * }
88
+ * ```
89
+ */
90
+ export async function prepareMidnightRedeem(client, params) {
91
+ const { account, chainId, positionData } = params;
92
+ const now = params.now ?? Time.timestamp();
93
+ validateReferenceTime(now);
94
+ const { expiresAt, calldataOrigin } = resolveIntent({
95
+ account,
96
+ chainId,
97
+ expiresAt: params.expiresAt,
98
+ calldataOrigin: params.calldataOrigin,
99
+ }, now);
100
+ if (params.units !== undefined && typeof params.units !== "bigint") {
101
+ throw new InvalidBigIntInputError("units", params.units);
102
+ }
103
+ const parsed = parseMidnightRedeemPreparation({
104
+ chainId,
105
+ accountAddress: account,
106
+ positionData,
107
+ units: params.units,
108
+ receiver: params.receiver,
109
+ now,
110
+ });
111
+ const chainCheck = createChainCheck(client, chainId);
112
+ await chainCheck();
113
+ // A code-less core would accept the withdrawal as a silent no-op.
114
+ await assertRegistryTargetsHaveCode(client, chainId, [{ role: "midnight", address: getChainAddress(chainId, "midnight") }], chainCheck);
115
+ return deepFreeze({
116
+ version: 2,
117
+ account,
118
+ chainId,
119
+ expiresAt,
120
+ ...(calldataOrigin && { calldataOrigin }),
121
+ steps: [
122
+ {
123
+ id: "midnightRedeem",
124
+ label: "Redeem Midnight credit",
125
+ action: {
126
+ type: "midnightRedeem",
127
+ inputs: {
128
+ account,
129
+ chainId,
130
+ market: parsed.market.struct,
131
+ units: parsed.units,
132
+ receiver: parsed.receiver,
133
+ },
134
+ },
135
+ },
136
+ ],
137
+ signatureRequests: [],
138
+ });
139
+ }
140
+ /** @internal Encodes the stored direct withdrawal without retaining the snapshot. */
141
+ export function buildMidnightRedeem(action) {
142
+ const { account, chainId, market, units, receiver } = action.inputs;
143
+ return midnightRedeem({
144
+ chainId,
145
+ market: storedMarketTuple(market),
146
+ units,
147
+ onBehalf: account,
148
+ receiver,
149
+ });
150
+ }
151
+ /** @internal Checks the stored redemption shape without consulting live state. */
152
+ export function validateMidnightRedeemPlanAction(action, path) {
153
+ const inputPath = `${path}.inputs`;
154
+ const inputs = getPlanObject(action.inputs, inputPath);
155
+ validatePlanFields(inputs, {
156
+ path: inputPath,
157
+ fields: { units: "uint128 bigint", receiver: "address" },
158
+ });
159
+ validateStoredMidnightMarket(inputs.market, {
160
+ path: `${inputPath}.market`,
161
+ chainId: inputs.chainId,
162
+ });
163
+ // Midnight never consumes a signature reference.
164
+ validateSignaturelessMidnightAction(action, path);
165
+ }
@@ -0,0 +1,25 @@
1
+ import { type MidnightTakeFeasibilityParams } from "../../helpers/validateMidnightTakeFeasibility.js";
2
+ type ReduceOnlySellFeasibilityParams<T extends MidnightTakeFeasibilityParams["takeableOffers"][number]> = Extract<MidnightTakeFeasibilityParams<T>, {
3
+ side: "sell";
4
+ }> & {
5
+ readonly increasesDebt: false;
6
+ readonly credit: bigint;
7
+ };
8
+ /**
9
+ * Runs {@link validateMidnightTakeFeasibility} for a reduce-only sell, then rejects a target that
10
+ * needs more units than the taker's `credit`: offers and the fallback withdrawal together can
11
+ * only sell credit the taker holds.
12
+ *
13
+ * @param params - Feasibility inputs of a reduce-only sell with the accrued taker credit.
14
+ * @returns `params.takeableOffers` itself, or a copy without the removed offers.
15
+ * @throws {ExpiredDeadlineError} when `deadline` is not after `now`.
16
+ * @throws {MidnightTakeableOffersNotFillableError} when the credit covers the target but every
17
+ * offer is dead and the flow has no fallback, or every offer would abort the bundle.
18
+ * @throws {MidnightTakeCreditInsufficientError} when `credit` is below the target's units, unless
19
+ * no offer is supplied and the fallback alone can fill the target.
20
+ * @throws {MidnightTakeTargetUnreachableError} with field `takeableOffers` when the credit covers
21
+ * the target but the live offers plus the fallback cannot hold its units.
22
+ * @internal
23
+ */
24
+ export declare const validateMidnightReduceOnlySellFeasibility: <T extends MidnightTakeFeasibilityParams["takeableOffers"][number]>(params: ReduceOnlySellFeasibilityParams<T>) => readonly T[];
25
+ export {};
@@ -0,0 +1,47 @@
1
+ import { grossFromNetAssets } from "../../helpers/referralFee.js";
2
+ import { validateMidnightTakeFeasibility, } from "../../helpers/validateMidnightTakeFeasibility.js";
3
+ import { MidnightTakeableOffersNotFillableError, MidnightTakeTargetUnreachableError, } from "../../types/error.js";
4
+ import { MidnightTakeCreditInsufficientError } from "../error.js";
5
+ /**
6
+ * Runs {@link validateMidnightTakeFeasibility} for a reduce-only sell, then rejects a target that
7
+ * needs more units than the taker's `credit`: offers and the fallback withdrawal together can
8
+ * only sell credit the taker holds.
9
+ *
10
+ * @param params - Feasibility inputs of a reduce-only sell with the accrued taker credit.
11
+ * @returns `params.takeableOffers` itself, or a copy without the removed offers.
12
+ * @throws {ExpiredDeadlineError} when `deadline` is not after `now`.
13
+ * @throws {MidnightTakeableOffersNotFillableError} when the credit covers the target but every
14
+ * offer is dead and the flow has no fallback, or every offer would abort the bundle.
15
+ * @throws {MidnightTakeCreditInsufficientError} when `credit` is below the target's units, unless
16
+ * no offer is supplied and the fallback alone can fill the target.
17
+ * @throws {MidnightTakeTargetUnreachableError} with field `takeableOffers` when the credit covers
18
+ * the target but the live offers plus the fallback cannot hold its units.
19
+ * @internal
20
+ */
21
+ export const validateMidnightReduceOnlySellFeasibility = (params) => {
22
+ const { target, referralFeePct, credit } = params;
23
+ const requiredUnits = target.type === "units"
24
+ ? target.units
25
+ : grossFromNetAssets({ netAssets: target.assets, referralFeePct });
26
+ let takeableOffers;
27
+ try {
28
+ takeableOffers = validateMidnightTakeFeasibility(params);
29
+ }
30
+ catch (error) {
31
+ const offerShortfall = error instanceof MidnightTakeableOffersNotFillableError ||
32
+ (error instanceof MidnightTakeTargetUnreachableError &&
33
+ error.field === "takeableOffers");
34
+ if (offerShortfall && credit < requiredUnits) {
35
+ throw new MidnightTakeCreditInsufficientError({
36
+ credit,
37
+ requiredUnits,
38
+ cause: error,
39
+ });
40
+ }
41
+ throw error;
42
+ }
43
+ if (params.takeableOffers.length > 0 && credit < requiredUnits) {
44
+ throw new MidnightTakeCreditInsufficientError({ credit, requiredUnits });
45
+ }
46
+ return takeableOffers;
47
+ };
@@ -0,0 +1,162 @@
1
+ import type { AccrualPosition, Market } from "@morpho-org/midnight-sdk";
2
+ import type { Client } from "viem";
3
+ import { type MidnightRepayWithdrawCollateralParams } from "../../actions/midnight/repayWithdrawCollateral.js";
4
+ import { type ReferenceTimestampParams } from "../../types/index.js";
5
+ import { type IntentParams } from "../intent.js";
6
+ import type { Action, TransactionPlan } from "../types.js";
7
+ type RepayAction = Extract<Action, {
8
+ type: "midnightRepayWithdrawCollateral";
9
+ }>;
10
+ /**
11
+ * Inputs for directly repaying a Midnight position and optionally withdrawing collateral.
12
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
13
+ */
14
+ export interface MidnightRepayWithdrawCollateralPlanParams extends Pick<IntentParams, "chainId" | "account" | "expiresAt" | "calldataOrigin">, ReferenceTimestampParams, Pick<MidnightRepayWithdrawCollateralParams, "repay" | "nativeAmount"> {
15
+ /** Hydrated market snapshot from the same block as `positionData`. */
16
+ readonly marketData: Market;
17
+ /** Position snapshot for the same market, owned by the submitting account. */
18
+ readonly positionData: AccrualPosition;
19
+ /**
20
+ * Raw values from `price()` on each `marketData.params.collateralParams[i].oracle`,
21
+ * indexed like `positionData.collateral` and scaled by `ORACLE_PRICE_SCALE` (1e36).
22
+ * Use prices from the snapshot block. No prices are needed when there are no withdrawals
23
+ * or the repayment leaves no debt in the snapshot.
24
+ */
25
+ readonly collateralPrices: readonly bigint[];
26
+ /**
27
+ * Defaults to no withdrawals. Each `collateralIndex` is a zero-based index into
28
+ * `marketData.params.collateralParams` and `positionData.collateral`. Amounts are in
29
+ * that collateral token's smallest units; `assets: maxUint256` withdraws the remaining
30
+ * balance at that index at execution.
31
+ */
32
+ readonly collateralWithdrawals?: MidnightRepayWithdrawCollateralParams["collateralWithdrawals"];
33
+ /**
34
+ * Defaults to `account`. With withdrawals, the receiver must be nonzero and not a
35
+ * blocked Midnight protocol or market token contract. Unused without withdrawals.
36
+ */
37
+ readonly collateralReceiver?: MidnightRepayWithdrawCollateralParams["collateralReceiver"];
38
+ }
39
+ /**
40
+ * Prepares a direct Midnight repayment with no offer fills or referral fee. Use an assets
41
+ * target for an exact repayment (`0n` for collateral-only), or full repayment with a finite
42
+ * asset cap. Inputs go through the same parse-once constructor and position checks as the
43
+ * entity `repayWithdrawCollateral`, in the same order: the position is bound to the sender and
44
+ * market, then withdrawals, debt and buffered health are checked against the snapshot.
45
+ * Restoring and building offline does not refresh those checks; simulate against current
46
+ * state before sending.
47
+ *
48
+ * If allowance is below the full pull bound, the plan requests a standalone ERC-20 approval
49
+ * to MidnightBundlesV2 for `maxUint256`, subject to `MAX_TOKEN_APPROVALS`, even with native
50
+ * funding. The approval does not expire with the plan. Withdrawal-only plans do not read
51
+ * allowance. In a full repayment, any native funding above the debt paid stays in the
52
+ * sender's wallet as wrapped native, not as an ETH refund.
53
+ * The plan adds a standalone bundle authorization if needed and never requests signatures.
54
+ * The bundle enforces the plan expiry, which defaults to two hours after the reference time.
55
+ * @param client - Viem client used to check the network and deployments and read prerequisites.
56
+ * @param params - Repayment, market and position snapshots, and optional withdrawals and funding.
57
+ * @param params.chainId - EIP-155 chain ID of the market and submitting client.
58
+ * @param params.account - Nonzero position owner who funds and sends the transactions.
59
+ * @param params.repay - Use `{ type: "assets", assets }` for an exact amount (`0n` only withdraws)
60
+ * or `{ type: "full", maxBuyerAssets }` for full repayment. Both amounts are in the loan
61
+ * token's smallest units and must be below `maxUint256`. The full cap must be positive
62
+ * and cover all debt at execution, or the bundle reverts.
63
+ * @param params.nativeAmount - ETH in wei sent as transaction value for a wrapped-native loan,
64
+ * up to the exact repayment amount or full repayment cap. Omit it or set it to `0n` for no ETH funding.
65
+ * @param params.marketData - Hydrated market snapshot from the same block as `positionData`.
66
+ * @param params.positionData - Position snapshot for the same market, owned by `account`.
67
+ * @param params.collateralPrices - Raw `price()` values from each collateral oracle at the snapshot
68
+ * block, indexed like `positionData.collateral` and scaled by `ORACLE_PRICE_SCALE` (1e36). Not
69
+ * needed without withdrawals or when the repayment leaves no snapshot debt.
70
+ * @param params.collateralWithdrawals - Optional withdrawals; defaults to none. Each
71
+ * `collateralIndex` indexes `marketData.params.collateralParams`, and `assets` is in that
72
+ * token's smallest units, with `maxUint256` withdrawing the remaining balance at execution.
73
+ * @param params.collateralReceiver - Optional recipient of withdrawn collateral; defaults to
74
+ * `account`. With withdrawals it must be nonzero and not a blocked Midnight protocol or market
75
+ * token contract.
76
+ * @param params.now - Reference time in Unix seconds, normally the latest block's timestamp.
77
+ * omitting it uses the device clock (an untyped JavaScript `null` does too); that fallback is deprecated.
78
+ * @param params.expiresAt - Bundle execution deadline in Unix seconds. Defaults to two hours after `now`.
79
+ * @param params.calldataOrigin - Optional attribution bytes (at most 1 MiB) appended to each operation transaction's calldata; approval and authorization steps carry none.
80
+ * @returns A frozen plan with prerequisite calls first and no signature requests.
81
+ * @throws {InvalidBigIntInputError} If a supplied non-null `now`, the active repayment amount or a withdrawal's `collateralIndex` or `assets` is not a bigint; checked before any request.
82
+ * @throws {MarketIdMismatchError} If the position belongs to another market.
83
+ * @throws {AccrualPositionUserMismatchError} If the position is owned by another account.
84
+ * @throws {MidnightRepayExceedsDebtError} If an exact repayment exceeds the snapshot debt.
85
+ * @throws {MidnightTakeTargetUnreachableError} If the full repayment cap is below the snapshot debt.
86
+ * @throws {WithdrawExceedsCollateralError} If a withdrawal exceeds the supplied collateral balance.
87
+ * @throws {WithdrawMakesPositionUnhealthyError} If remaining debt exceeds buffered collateral capacity.
88
+ * @throws {MissingMarketPriceError} If remaining collateral has no price for the health check.
89
+ * @throws {UnknownCollateralIndexError} If a withdrawal targets an unconfigured index.
90
+ * @throws {MidnightZeroReceiverError} If withdrawn collateral is sent to the zero address.
91
+ * @throws {MidnightContractReceiverError} If withdrawn collateral is sent to a blocked contract.
92
+ * @throws {ChainIdMismatchError} If the market, configured client or transport is on another chain.
93
+ * @throws {MidnightMarketAddressMismatchError} If the market targets another Midnight deployment.
94
+ * @throws {InvalidMarketParameterError} If the market configuration is invalid.
95
+ * @throws {MissingContractCodeError} If the registered bundle or Midnight core has no code.
96
+ * @throws {viem.BaseError} If a chain, bytecode, allowance or authorization read fails.
97
+ * @throws {UnsupportedChainIdError} If the chain is not registered.
98
+ * @throws {UnknownAddressError} If a Midnight deployment is not registered on that chain.
99
+ * @throws {ChainWNativeMissingError} If native funding is requested without a registered wrapped-native token.
100
+ * @throws {NativeAmountOnNonWNativeAssetError} If native funding is requested for a different loan token.
101
+ * @throws {InputExceedsMaxError} If an amount exceeds its encoding bound or native funding exceeds the pull.
102
+ * @throws {NegativeInputError} If the exact repayment amount or native funding is negative.
103
+ * @throws {InvalidMidnightNativeAmountTypeError} If `nativeAmount` is not a bigint.
104
+ * @throws {NonPositiveInputError} If there is neither repayment nor withdrawal, the cap or a withdrawal is nonpositive, or expiry is nonpositive.
105
+ * @throws {ZeroAccountError} If the sender is the zero address.
106
+ * @throws {NonIntegerInputError} If expiry is not a safe integer.
107
+ * @throws {ExpiredDeadlineError} If expiry is at or before the reference time.
108
+ * @throws {InvalidCalldataOriginError} If attribution bytes are empty or malformed.
109
+ * @throws {InvalidTransactionPlanError} If the sender address or attribution length is invalid, `repay` is not an object, or its type is unsupported.
110
+ * @throws {MidnightCalldataEncodingError} If a market, position owner or receiver address is malformed, or calldata cannot be encoded.
111
+ * @example
112
+ * ```ts
113
+ * import { buildTransactions, prepareMidnightRepayWithdrawCollateral } from "@morpho-org/morpho-sdk";
114
+ * import { fetchMidnightAccrualPosition } from "@morpho-org/morpho-sdk/fetch";
115
+ * import { type Address, type Hash, createPublicClient, http, maxUint256 } from "viem";
116
+ * import { base } from "viem/chains";
117
+ *
118
+ * async function prepareClose(account: Address, marketId: Hash, maxBuyerAssets: bigint) {
119
+ * const client = createPublicClient({ chain: base, transport: http() });
120
+ * const block = await client.getBlock();
121
+ * const positionData = await fetchMidnightAccrualPosition(client, {
122
+ * marketId, user: account, blockNumber: block.number,
123
+ * });
124
+ * const plan = await prepareMidnightRepayWithdrawCollateral(client, {
125
+ * chainId: base.id, account, marketData: positionData.market, positionData,
126
+ * collateralPrices: [], // No price check is needed for full repayment.
127
+ * now: block.timestamp,
128
+ * repay: { type: "full", maxBuyerAssets },
129
+ * collateralWithdrawals: [{ collateralIndex: 0n, assets: maxUint256 }],
130
+ * });
131
+ * // Simulate, then send in order from plan.account on plan.chainId.
132
+ * return buildTransactions(plan);
133
+ * }
134
+ * ```
135
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
136
+ */
137
+ export declare function prepareMidnightRepayWithdrawCollateral(client: Client, params: MidnightRepayWithdrawCollateralPlanParams): Promise<TransactionPlan>;
138
+ /**
139
+ * @internal Encodes only the persisted inputs; no position, client or clock is needed.
140
+ * @throws {InvalidTransactionPlanError} If `repay` is not an object or its type is unsupported.
141
+ * @throws {NegativeInputError} If the exact asset amount or native funding is negative.
142
+ * @throws {NonPositiveInputError} If the full repayment cap, a withdrawal amount or the deadline is nonpositive, or the step neither repays nor withdraws.
143
+ * @throws {InputExceedsMaxError} If `repay.assets` or `repay.maxBuyerAssets` is at least `maxUint256`, a withdrawal amount or deadline exceeds uint256, or native funding exceeds the repayment pull.
144
+ * @throws {ChainIdMismatchError} If the market targets another chain.
145
+ * @throws {MidnightMarketAddressMismatchError} If the market targets another Midnight deployment.
146
+ * @throws {InvalidMarketParameterError} If the market configuration is invalid.
147
+ * @throws {UnknownCollateralIndexError} If a withdrawal targets an index not configured on the market.
148
+ * @throws {MidnightZeroReceiverError} If a collateral withdrawal names the zero address as its receiver.
149
+ * @throws {MidnightContractReceiverError} If a collateral withdrawal names a blocked protocol or token contract as its receiver.
150
+ * @throws {MidnightCalldataEncodingError} If an address is malformed or an input cannot be ABI-encoded.
151
+ * @throws {ChainWNativeMissingError} If native funding targets a chain without wNative.
152
+ * @throws {NativeAmountOnNonWNativeAssetError} If native funding targets a loan token other than wNative.
153
+ * @throws {UnsupportedChainIdError} If the chain is absent from the address registry.
154
+ * @throws {UnknownAddressError} If Midnight or MidnightBundlesV2 is not registered on the chain.
155
+ */
156
+ export declare function buildMidnightRepayWithdrawCollateral({ inputs }: RepayAction): Readonly<import("../../types/action.js").Transaction<import("../../types/action.js").MidnightRepayWithdrawCollateralAction>>;
157
+ /**
158
+ * @internal Validates the format of a stored repayment recipe without reading live state.
159
+ * @throws {InvalidTransactionPlanError} If the recipe has invalid fields or signature references.
160
+ */
161
+ export declare function validateMidnightRepayWithdrawCollateralPlanAction(action: Record<string, unknown>, path: string): void;
162
+ export {};