@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
@@ -5,17 +5,18 @@ exports.validateBlueWithdrawAuthorizationRequest = validateBlueWithdrawAuthoriza
5
5
  exports.buildBlueWithdraw = buildBlueWithdraw;
6
6
  exports.validateBlueWithdrawPlanAction = validateBlueWithdrawPlanAction;
7
7
  const blue_sdk_1 = require("@morpho-org/blue-sdk");
8
- const morpho_ts_1 = require("@morpho-org/morpho-ts");
9
8
  const viem_1 = require("viem");
10
9
  const common_js_1 = require("../../actions/blue/common.js");
11
10
  const withdraw_js_1 = require("../../actions/blue/withdraw.js");
12
11
  const validateClientChainId_js_1 = require("../../actions/requirements/validateClientChainId.js");
13
12
  const validate_js_1 = require("../../helpers/validate.js");
14
13
  const index_js_1 = require("../../types/index.js");
14
+ const assemble_js_1 = require("../assemble.js");
15
15
  const error_js_1 = require("../error.js");
16
16
  const validation_js_1 = require("../validation.js");
17
17
  const authorization_js_1 = require("./authorization.js");
18
18
  const prepare_js_1 = require("./prepare.js");
19
+ const stored_js_1 = require("./stored.js");
19
20
  /**
20
21
  * Prepares a BlueBundlesV1 withdrawal with only Morpho authorization prerequisites, never token approvals.
21
22
  * The caller supplies market and position snapshots; the SDK reads authorization but never signs.
@@ -48,7 +49,7 @@ const prepare_js_1 = require("./prepare.js");
48
49
  * @param params.supportSignature - Optional Morpho authorization signature preference; defaults to `false`, selecting a direct authorization transaction when needed. Neither mode requests a token allowance or Permit2 signature.
49
50
  * @param params.now - Optional reference timestamp in seconds, normally the latest block timestamp; defaults to the device clock.
50
51
  * @param params.expiresAt - Optional Unix expiry in safe-integer seconds, later than `now`; defaults to `now + 2h`.
51
- * @param params.calldataOrigin - Optional non-empty, even-length hex bytes appended to every built transaction; omitted by default.
52
+ * @param params.calldataOrigin - Optional non-empty, even-length hex bytes appended to the action transaction's calldata, not to approval or authorization steps; omitted by default.
52
53
  * @param params.deployless - Optional shared intent field; unused because withdrawal reads no token metadata.
53
54
  * @returns A deep-frozen, data-only `{ version, chainId, account, expiresAt, steps, signatureRequests, calldataOrigin? }` plan with a withdrawal step. Direct Morpho authorization is a preceding step; signed authorization is consumed inline by the withdrawal. Attach signatures before building.
54
55
  * @throws {InvalidBigIntInputError} when a supplied maxPenalty is not a bigint.
@@ -79,7 +80,6 @@ const prepare_js_1 = require("./prepare.js");
79
80
  * @throws {WithdrawSharesExceedSupplyError} when `shares` exceeds the position's supply shares.
80
81
  * @throws {MissingContractCodeError} when registered BlueBundlesV1 or Blue has no deployed code.
81
82
  * @throws {viem.BaseError} when a chain, bytecode, authorization or nonce RPC read fails, or ABI encoding rejects malformed input.
82
- * @throws {UnexpectedRequirementSignatureError} when an authorization requirement returns a nonce that is not a bigint.
83
83
  * @throws {TransactionPlanSignatureMismatchError} when the authorization requirement does not match the prepared account, chain, nonce or deadline.
84
84
  * @example
85
85
  * ```ts
@@ -99,33 +99,65 @@ const prepare_js_1 = require("./prepare.js");
99
99
  * positionData: { chainId: mainnet.id, position },
100
100
  * now: (await client.getBlock()).timestamp,
101
101
  * });
102
- * // plan: { version: 1, chainId, account, expiresAt, steps, signatureRequests }
102
+ * // plan: { version: 2, chainId, account, expiresAt, steps, signatureRequests }
103
103
  * // Sign any pending Morpho authorization request, then build and submit each step in order.
104
104
  * ```
105
105
  * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
106
106
  */
107
107
  async function prepareBlueWithdraw(client, params) {
108
- const { maxPenalty } = params;
109
- await (0, validateClientChainId_js_1.validateClientChainId)(client, params.chainId);
108
+ const { maxPenalty, chainId, account } = params;
109
+ const supplied = params.reallocations ?? [];
110
+ let reallocations;
111
+ try {
112
+ reallocations = structuredClone(Array.isArray(supplied)
113
+ ? supplied.map((entry) => {
114
+ if (entry === null ||
115
+ typeof entry !== "object" ||
116
+ "withdrawals" in entry ||
117
+ "fee" in entry) {
118
+ return entry;
119
+ }
120
+ const { vault, from, to, assets, penalty } = entry;
121
+ return { vault, from, to, assets, penalty };
122
+ })
123
+ : supplied);
124
+ }
125
+ catch (cause) {
126
+ throw new index_js_1.InvalidVaultV2BlueReallocationShapeError({ cause });
127
+ }
128
+ await (0, validateClientChainId_js_1.validateClientChainId)(client, chainId);
110
129
  const { positionData: snapshot } = params;
111
130
  if (snapshot == null) {
112
131
  throw new index_js_1.MissingAccrualPositionError(params.marketId);
113
132
  }
114
133
  const withdrawals = [];
115
- const { intent: resolvedIntent, steps, signatureRequests, } = await (0, prepare_js_1.prepareBlue)({ client, params }, (intent) => {
134
+ const captured = {
135
+ chainId,
136
+ account,
137
+ marketId: params.marketId,
138
+ marketData: params.marketData,
139
+ positionData: snapshot,
140
+ now: params.now,
141
+ expiresAt: params.expiresAt,
142
+ supportSignature: params.supportSignature,
143
+ referralFee: params.referralFee,
144
+ calldataOrigin: params.calldataOrigin,
145
+ deployless: params.deployless,
146
+ };
147
+ const { intent: resolvedIntent, steps, signatureRequests, } = await (0, prepare_js_1.prepareBlue)({ client, params: captured }, (intent) => {
116
148
  const inputs = {
117
- chainId: params.chainId,
118
- account: params.account,
149
+ chainId,
150
+ account,
119
151
  marketParams: {
120
- loanToken: params.marketData.market.params.loanToken,
121
- collateralToken: params.marketData.market.params.collateralToken,
122
- oracle: params.marketData.market.params.oracle,
123
- irm: params.marketData.market.params.irm,
124
- lltv: params.marketData.market.params.lltv,
152
+ loanToken: captured.marketData.market.params.loanToken,
153
+ collateralToken: captured.marketData.market.params.collateralToken,
154
+ oracle: captured.marketData.market.params.oracle,
155
+ irm: captured.marketData.market.params.irm,
156
+ lltv: captured.marketData.market.params.lltv,
125
157
  },
126
158
  withdrawAssets: "assets" in params ? (params.assets ?? 0n) : 0n,
127
159
  withdrawShares: "shares" in params ? (params.shares ?? 0n) : 0n,
128
- reallocations: params.reallocations ?? [],
160
+ reallocations,
129
161
  ...(maxPenalty !== undefined && { maxPenalty }),
130
162
  deadline: intent.deadline,
131
163
  referralFeePct: intent.referralFeePct,
@@ -157,7 +189,7 @@ async function prepareBlueWithdraw(client, params) {
157
189
  positionData,
158
190
  withdrawShares: inputs.withdrawShares,
159
191
  withdrawAssets: inputs.withdrawAssets,
160
- marketId: params.marketId,
192
+ marketId: captured.marketId,
161
193
  });
162
194
  withdrawals.push({
163
195
  id: "blueWithdraw",
@@ -166,14 +198,8 @@ async function prepareBlueWithdraw(client, params) {
166
198
  });
167
199
  return { id: "blueWithdraw", needsAuthorization: true };
168
200
  });
169
- return (0, morpho_ts_1.deepFreeze)(structuredClone({
170
- version: 2,
171
- chainId: params.chainId,
172
- account: params.account,
173
- expiresAt: resolvedIntent.expiresAt,
174
- ...(resolvedIntent.calldataOrigin && {
175
- calldataOrigin: resolvedIntent.calldataOrigin,
176
- }),
201
+ return (0, assemble_js_1.freezePlan)({
202
+ ...(0, assemble_js_1.planHeader)({ chainId, account }, resolvedIntent),
177
203
  steps: [
178
204
  ...steps,
179
205
  ...withdrawals.map((step) => {
@@ -196,7 +222,7 @@ async function prepareBlueWithdraw(client, params) {
196
222
  }),
197
223
  ],
198
224
  signatureRequests,
199
- }));
225
+ }, { detach: true });
200
226
  }
201
227
  /** @internal Binds an inline authorization to the withdrawal that consumes it. */
202
228
  function validateBlueWithdrawAuthorizationRequest(request, action) {
@@ -250,7 +276,7 @@ function buildBlueWithdraw(action, requests = []) {
250
276
  },
251
277
  };
252
278
  }
253
- const targetMarketParams = new blue_sdk_1.MarketParams(marketParams);
279
+ const targetMarketParams = toEncoderMarketParams(marketParams);
254
280
  // Reject malformed entries with typed errors before reading their source fields.
255
281
  (0, validate_js_1.validateVaultV2BlueReallocations)(args.reallocations, {
256
282
  targetMarketId: targetMarketParams.id,
@@ -261,7 +287,7 @@ function buildBlueWithdraw(action, requests = []) {
261
287
  from: entry.from.type === "market"
262
288
  ? {
263
289
  ...entry.from,
264
- marketParams: new blue_sdk_1.MarketParams(entry.from.marketParams),
290
+ marketParams: toEncoderMarketParams(entry.from.marketParams),
265
291
  }
266
292
  : entry.from,
267
293
  }));
@@ -275,6 +301,15 @@ function buildBlueWithdraw(action, requests = []) {
275
301
  },
276
302
  });
277
303
  }
304
+ function toEncoderMarketParams(marketParams) {
305
+ const { loanToken, collateralToken, oracle, irm, lltv } = marketParams;
306
+ const tuple = { loanToken, collateralToken, oracle, irm, lltv };
307
+ return {
308
+ ...tuple,
309
+ id: blue_sdk_1.MarketUtils.getMarketId(tuple),
310
+ liquidationIncentiveFactor: blue_sdk_1.MarketUtils.getLiquidationIncentiveFactor(tuple),
311
+ };
312
+ }
278
313
  /** @internal Validates stored withdrawal scalars and allocator input shapes. */
279
314
  function validateBlueWithdrawPlanAction(action, path) {
280
315
  const inputs = (0, validation_js_1.getPlanObject)(action.inputs, `${path}.inputs`);
@@ -300,60 +335,13 @@ function validateBlueWithdrawPlanAction(action, path) {
300
335
  fields: {
301
336
  withdrawAssets: "uint256 bigint",
302
337
  withdrawShares: "uint256 bigint",
303
- deadline: "uint256 bigint",
304
- referralFeePct: "uint256 bigint",
305
- referralFeeRecipient: "address",
338
+ ...stored_js_1.BUNDLES_COMMON_FIELDS,
306
339
  reallocations: "array",
307
340
  },
308
341
  });
309
- (0, validation_js_1.validatePlanFields)((0, validation_js_1.getPlanObject)(inputs.marketParams, `${path}.inputs.marketParams`), {
310
- path: `${path}.inputs.marketParams`,
311
- fields: {
312
- loanToken: "address",
313
- collateralToken: "address",
314
- oracle: "address",
315
- irm: "address",
316
- lltv: "uint256 bigint",
317
- },
342
+ (0, stored_js_1.validateStoredBlueMarketParams)(inputs.marketParams, `${path}.inputs.marketParams`);
343
+ (0, stored_js_1.validateStoredReallocations)(inputs.reallocations, {
344
+ path: `${path}.inputs.reallocations`,
345
+ marketParams: stored_js_1.validateStoredBlueMarketParams,
318
346
  });
319
- for (const [index, value] of inputs.reallocations.entries()) {
320
- const entryPath = `${path}.inputs.reallocations[${index}]`;
321
- const entry = (0, validation_js_1.getPlanObject)(value, entryPath);
322
- (0, validation_js_1.validatePlanFields)(entry, {
323
- path: entryPath,
324
- fields: {
325
- vault: "address",
326
- assets: "uint256 bigint",
327
- penalty: "uint256 bigint",
328
- },
329
- });
330
- (0, validation_js_1.validatePlanFields)((0, validation_js_1.getPlanObject)(entry.to, `${entryPath}.to`), {
331
- path: `${entryPath}.to`,
332
- fields: { adapter: "address" },
333
- });
334
- const from = (0, validation_js_1.getPlanObject)(entry.from, `${entryPath}.from`);
335
- if (from.type === "idle") {
336
- continue;
337
- }
338
- if (from.type !== "market") {
339
- throw new error_js_1.InvalidTransactionPlanError({
340
- path: `${entryPath}.from.type`,
341
- reason: "expected market or idle",
342
- });
343
- }
344
- (0, validation_js_1.validatePlanFields)(from, {
345
- path: `${entryPath}.from`,
346
- fields: { adapter: "address" },
347
- });
348
- (0, validation_js_1.validatePlanFields)((0, validation_js_1.getPlanObject)(from.marketParams, `${entryPath}.from.marketParams`), {
349
- path: `${entryPath}.from.marketParams`,
350
- fields: {
351
- loanToken: "address",
352
- collateralToken: "address",
353
- oracle: "address",
354
- irm: "address",
355
- lltv: "uint256 bigint",
356
- },
357
- });
358
- }
359
347
  }
@@ -0,0 +1,101 @@
1
+ import { type MarketId } from "@morpho-org/blue-sdk";
2
+ import { type Address, type Client } from "viem";
3
+ import { type BlueWithdrawBuyCallbackAction, type ReferenceTimestampParams, type Transaction } from "../../types/index.js";
4
+ import { type IntentParams } from "../intent.js";
5
+ import type { Action, TransactionPlan } from "../types.js";
6
+ import { type BlueMarketSnapshot, type BluePositionSnapshot } from "./prepare.js";
7
+ /**
8
+ * Caller intent and caller-fetched snapshots for withdrawing a BlueBuyCallback's whole supply
9
+ * position to its owner. The position belongs to `callbackAddress`, not to `account`.
10
+ * @example
11
+ * ```ts
12
+ * import type { BlueWithdrawBuyCallbackPlanParams } from "@morpho-org/morpho-sdk";
13
+ * import type { Address } from "viem";
14
+ * declare const callbackAddress: Address;
15
+ * const withdrawal = { callbackAddress, expiresAt: 1_900_000_000 } satisfies Pick<
16
+ * BlueWithdrawBuyCallbackPlanParams,
17
+ * "callbackAddress" | "expiresAt"
18
+ * >;
19
+ * ```
20
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
21
+ */
22
+ export interface BlueWithdrawBuyCallbackPlanParams extends Pick<IntentParams, "chainId" | "account" | "expiresAt" | "calldataOrigin">, ReferenceTimestampParams {
23
+ readonly marketId: MarketId;
24
+ readonly marketData: BlueMarketSnapshot;
25
+ /** BlueBuyCallback whose supply position is withdrawn; `account` must be its owner. */
26
+ readonly callbackAddress: Address;
27
+ /** Caller-fetched position of `callbackAddress`; every supply share in it is withdrawn. */
28
+ readonly positionData: BluePositionSnapshot;
29
+ }
30
+ /**
31
+ * Prepares a direct Morpho Blue withdrawal of a BlueBuyCallback's whole supply position to its owner.
32
+ * The call is `withdraw(marketParams, 0, shares, callbackAddress, account)` on Morpho Blue, not
33
+ * BlueBundlesV1: the callback authorizes its owner on Blue at construction, so the plan has no
34
+ * approval, authorization step or signature request. Preparation reads the chain and rejects a
35
+ * callback the registered factory did not create, another owner, or a revoked Blue authorization.
36
+ * The plan carries no referral fee, and `expiresAt` is plan metadata, not an onchain deadline.
37
+ * @param client - Viem client on the requested chain.
38
+ * @param params.chainId - Chain containing Morpho Blue and the registered `midnightBlueBuyCallbackFactory`.
39
+ * @param params.account - Non-zero callback owner that sends the transaction and receives the loan assets.
40
+ * @param params.marketId - Market ID matching both snapshots and the market tuple.
41
+ * @param params.marketData.chainId - Chain on which the market snapshot was fetched; must equal `chainId`.
42
+ * @param params.marketData.market - Caller-fetched `Market` snapshot used to fix the market tuple; its state must equal the position snapshot's market. Never stored in the plan.
43
+ * @param params.callbackAddress - BlueBuyCallback whose supply position is withdrawn.
44
+ * @param params.positionData.chainId - Position snapshot's chain; must equal `chainId`.
45
+ * @param params.positionData.position - Caller-fetched `AccrualPosition` of `callbackAddress` on `marketId`; all its supply shares are withdrawn and their assets must not exceed that market's liquidity.
46
+ * @param params.now - Optional reference timestamp in seconds, normally the latest block timestamp; defaults to the device clock.
47
+ * @param params.expiresAt - Optional Unix expiry in safe-integer seconds, later than `now`; defaults to `now + 2h`. Plan metadata only.
48
+ * @param params.calldataOrigin - Optional non-empty, even-length hex bytes appended to the withdrawal calldata; omitted by default.
49
+ * @returns A deep-frozen, data-only `{ version, chainId, account, expiresAt, steps, signatureRequests, calldataOrigin? }` plan with one `blueWithdrawBuyCallback` step and no signature requests.
50
+ * @throws {BlueSnapshotChainMismatchError} when a market or position snapshot's chain differs from `chainId`.
51
+ * @throws {MarketIdMismatchError} when a snapshot's ID differs from `marketId` or does not hash from its market tuple.
52
+ * @throws {BlueSnapshotStateMismatchError} when the market snapshot's state differs from the position snapshot's market.
53
+ * @throws {MissingAccrualPositionError} when no position snapshot is provided.
54
+ * @throws {AccrualPositionUserMismatchError} when the position belongs to another address than `callbackAddress`.
55
+ * @throws {ZeroAccountError} when `account` is the zero address.
56
+ * @throws {InvalidTransactionPlanError} when `account` or `callbackAddress` is not a checksum-valid address, or `calldataOrigin` exceeds 1 MiB.
57
+ * @throws {NonIntegerInputError} when `expiresAt` is not a safe integer.
58
+ * @throws {NonPositiveInputError} when `expiresAt` is not positive.
59
+ * @throws {ExpiredDeadlineError} when `expiresAt` is at or before `now`.
60
+ * @throws {InvalidCalldataOriginError} when `calldataOrigin` is empty, malformed or has an odd number of hex digits.
61
+ * @throws {EmptyBlueSupplyPositionError} when the callback holds no supply shares.
62
+ * @throws {InsufficientBlueLiquidityError} when the position's supply assets exceed the snapshot market's liquidity.
63
+ * @throws {InputExceedsMaxError} when the supply shares exceed `uint256`.
64
+ * @throws {UnsupportedChainIdError} when `chainId` is absent from the registry.
65
+ * @throws {UnknownAddressError} when Morpho Blue or `midnightBlueBuyCallbackFactory` is not registered on `chainId`.
66
+ * @throws {ChainIdMismatchError} when the client declares another chain (checked before any snapshot) or its transport serves one.
67
+ * @throws {MissingContractCodeError} when Morpho Blue or the factory has no deployed code.
68
+ * @throws {NotBlueBuyCallbackError} when the factory did not create `callbackAddress`.
69
+ * @throws {BlueBuyCallbackOwnerMismatchError} when the callback's `OWNER()` is not `account`.
70
+ * @throws {BlueBuyCallbackUnauthorizedError} when Morpho Blue does not authorize `account` for the callback.
71
+ * @throws {viem.BaseError} when a chain, bytecode, factory, owner or authorization read fails.
72
+ * @example
73
+ * ```ts
74
+ * import { markets } from "@morpho-org/morpho-test";
75
+ * import { buildTransactions, prepareBlueWithdrawBuyCallback } from "@morpho-org/morpho-sdk";
76
+ * import { fetchBlueAccrualPosition } from "@morpho-org/morpho-sdk/fetch";
77
+ * import { createPublicClient, type Address, http } from "viem";
78
+ * import { mainnet } from "viem/chains";
79
+ *
80
+ * const client = createPublicClient({ chain: mainnet, transport: http() });
81
+ * declare const account: Address; // Owner of the BlueBuyCallback.
82
+ * declare const callbackAddress: Address; // BlueBuyCallback created by the registered factory.
83
+ * const position = await fetchBlueAccrualPosition(callbackAddress, markets[mainnet.id].usdc_wbtc.id, client);
84
+ * const plan = await prepareBlueWithdrawBuyCallback(client, {
85
+ * chainId: mainnet.id, account, callbackAddress, marketId: position.market.id,
86
+ * marketData: { chainId: mainnet.id, market: position.market },
87
+ * positionData: { chainId: mainnet.id, position },
88
+ * now: (await client.getBlock()).timestamp,
89
+ * });
90
+ * const [transaction] = buildTransactions(plan);
91
+ * // Submit transaction from account; it calls Morpho Blue directly.
92
+ * ```
93
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
94
+ */
95
+ export declare function prepareBlueWithdrawBuyCallback(client: Client, params: BlueWithdrawBuyCallbackPlanParams): Promise<TransactionPlan>;
96
+ /** @internal Encodes the stored callback withdrawal through the same action as the entity handle. */
97
+ export declare function buildBlueWithdrawBuyCallback(action: Extract<Action, {
98
+ type: "blueWithdrawBuyCallback";
99
+ }>): Readonly<Transaction<BlueWithdrawBuyCallbackAction>>;
100
+ /** @internal Validates the stored callback withdrawal's structural inputs. */
101
+ export declare function validateBlueWithdrawBuyCallbackPlanAction(action: Record<string, unknown>, path: string): void;
@@ -0,0 +1,264 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.prepareBlueWithdrawBuyCallback = prepareBlueWithdrawBuyCallback;
4
+ exports.buildBlueWithdrawBuyCallback = buildBlueWithdrawBuyCallback;
5
+ exports.validateBlueWithdrawBuyCallbackPlanAction = validateBlueWithdrawBuyCallbackPlanAction;
6
+ const blue_sdk_1 = require("@morpho-org/blue-sdk");
7
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
8
+ const viem_1 = require("viem");
9
+ const actions_1 = require("viem/actions");
10
+ const abis_js_1 = require("../../abis.js");
11
+ const withdrawBuyCallback_internal_js_1 = require("../../actions/blue/withdrawBuyCallback.internal.js");
12
+ const withdrawBuyCallback_js_1 = require("../../actions/blue/withdrawBuyCallback.js");
13
+ const validateClientChainId_js_1 = require("../../actions/requirements/validateClientChainId.js");
14
+ const index_js_1 = require("../../helpers/index.js");
15
+ const validate_js_1 = require("../../helpers/validate.js");
16
+ const index_js_2 = require("../../types/index.js");
17
+ const error_js_1 = require("../error.js");
18
+ const intent_js_1 = require("../intent.js");
19
+ const validation_js_1 = require("../validation.js");
20
+ const prepare_js_1 = require("./prepare.js");
21
+ const MARKET_STATE_FIELDS = [
22
+ "totalSupplyAssets",
23
+ "totalSupplyShares",
24
+ "totalBorrowAssets",
25
+ "totalBorrowShares",
26
+ "lastUpdate",
27
+ "fee",
28
+ "price",
29
+ "rateAtTarget",
30
+ ];
31
+ /**
32
+ * Prepares a direct Morpho Blue withdrawal of a BlueBuyCallback's whole supply position to its owner.
33
+ * The call is `withdraw(marketParams, 0, shares, callbackAddress, account)` on Morpho Blue, not
34
+ * BlueBundlesV1: the callback authorizes its owner on Blue at construction, so the plan has no
35
+ * approval, authorization step or signature request. Preparation reads the chain and rejects a
36
+ * callback the registered factory did not create, another owner, or a revoked Blue authorization.
37
+ * The plan carries no referral fee, and `expiresAt` is plan metadata, not an onchain deadline.
38
+ * @param client - Viem client on the requested chain.
39
+ * @param params.chainId - Chain containing Morpho Blue and the registered `midnightBlueBuyCallbackFactory`.
40
+ * @param params.account - Non-zero callback owner that sends the transaction and receives the loan assets.
41
+ * @param params.marketId - Market ID matching both snapshots and the market tuple.
42
+ * @param params.marketData.chainId - Chain on which the market snapshot was fetched; must equal `chainId`.
43
+ * @param params.marketData.market - Caller-fetched `Market` snapshot used to fix the market tuple; its state must equal the position snapshot's market. Never stored in the plan.
44
+ * @param params.callbackAddress - BlueBuyCallback whose supply position is withdrawn.
45
+ * @param params.positionData.chainId - Position snapshot's chain; must equal `chainId`.
46
+ * @param params.positionData.position - Caller-fetched `AccrualPosition` of `callbackAddress` on `marketId`; all its supply shares are withdrawn and their assets must not exceed that market's liquidity.
47
+ * @param params.now - Optional reference timestamp in seconds, normally the latest block timestamp; defaults to the device clock.
48
+ * @param params.expiresAt - Optional Unix expiry in safe-integer seconds, later than `now`; defaults to `now + 2h`. Plan metadata only.
49
+ * @param params.calldataOrigin - Optional non-empty, even-length hex bytes appended to the withdrawal calldata; omitted by default.
50
+ * @returns A deep-frozen, data-only `{ version, chainId, account, expiresAt, steps, signatureRequests, calldataOrigin? }` plan with one `blueWithdrawBuyCallback` step and no signature requests.
51
+ * @throws {BlueSnapshotChainMismatchError} when a market or position snapshot's chain differs from `chainId`.
52
+ * @throws {MarketIdMismatchError} when a snapshot's ID differs from `marketId` or does not hash from its market tuple.
53
+ * @throws {BlueSnapshotStateMismatchError} when the market snapshot's state differs from the position snapshot's market.
54
+ * @throws {MissingAccrualPositionError} when no position snapshot is provided.
55
+ * @throws {AccrualPositionUserMismatchError} when the position belongs to another address than `callbackAddress`.
56
+ * @throws {ZeroAccountError} when `account` is the zero address.
57
+ * @throws {InvalidTransactionPlanError} when `account` or `callbackAddress` is not a checksum-valid address, or `calldataOrigin` exceeds 1 MiB.
58
+ * @throws {NonIntegerInputError} when `expiresAt` is not a safe integer.
59
+ * @throws {NonPositiveInputError} when `expiresAt` is not positive.
60
+ * @throws {ExpiredDeadlineError} when `expiresAt` is at or before `now`.
61
+ * @throws {InvalidCalldataOriginError} when `calldataOrigin` is empty, malformed or has an odd number of hex digits.
62
+ * @throws {EmptyBlueSupplyPositionError} when the callback holds no supply shares.
63
+ * @throws {InsufficientBlueLiquidityError} when the position's supply assets exceed the snapshot market's liquidity.
64
+ * @throws {InputExceedsMaxError} when the supply shares exceed `uint256`.
65
+ * @throws {UnsupportedChainIdError} when `chainId` is absent from the registry.
66
+ * @throws {UnknownAddressError} when Morpho Blue or `midnightBlueBuyCallbackFactory` is not registered on `chainId`.
67
+ * @throws {ChainIdMismatchError} when the client declares another chain (checked before any snapshot) or its transport serves one.
68
+ * @throws {MissingContractCodeError} when Morpho Blue or the factory has no deployed code.
69
+ * @throws {NotBlueBuyCallbackError} when the factory did not create `callbackAddress`.
70
+ * @throws {BlueBuyCallbackOwnerMismatchError} when the callback's `OWNER()` is not `account`.
71
+ * @throws {BlueBuyCallbackUnauthorizedError} when Morpho Blue does not authorize `account` for the callback.
72
+ * @throws {viem.BaseError} when a chain, bytecode, factory, owner or authorization read fails.
73
+ * @example
74
+ * ```ts
75
+ * import { markets } from "@morpho-org/morpho-test";
76
+ * import { buildTransactions, prepareBlueWithdrawBuyCallback } from "@morpho-org/morpho-sdk";
77
+ * import { fetchBlueAccrualPosition } from "@morpho-org/morpho-sdk/fetch";
78
+ * import { createPublicClient, type Address, http } from "viem";
79
+ * import { mainnet } from "viem/chains";
80
+ *
81
+ * const client = createPublicClient({ chain: mainnet, transport: http() });
82
+ * declare const account: Address; // Owner of the BlueBuyCallback.
83
+ * declare const callbackAddress: Address; // BlueBuyCallback created by the registered factory.
84
+ * const position = await fetchBlueAccrualPosition(callbackAddress, markets[mainnet.id].usdc_wbtc.id, client);
85
+ * const plan = await prepareBlueWithdrawBuyCallback(client, {
86
+ * chainId: mainnet.id, account, callbackAddress, marketId: position.market.id,
87
+ * marketData: { chainId: mainnet.id, market: position.market },
88
+ * positionData: { chainId: mainnet.id, position },
89
+ * now: (await client.getBlock()).timestamp,
90
+ * });
91
+ * const [transaction] = buildTransactions(plan);
92
+ * // Submit transaction from account; it calls Morpho Blue directly.
93
+ * ```
94
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
95
+ */
96
+ async function prepareBlueWithdrawBuyCallback(client, params) {
97
+ const { chainId, account, callbackAddress, marketId, marketData } = params;
98
+ const snapshot = params.positionData;
99
+ // Reject a client declaring another chain first, as the entity does.
100
+ (0, index_js_1.validateChainId)(client.chain?.id, chainId);
101
+ (0, validation_js_1.validatePlanField)(callbackAddress, {
102
+ path: "$.callbackAddress",
103
+ type: "address",
104
+ });
105
+ // Reject mismatched snapshots before deriving the withdrawal or reading chain state.
106
+ (0, prepare_js_1.validateBlueMarketSnapshot)(marketData, { chainId, marketId });
107
+ if (snapshot == null) {
108
+ throw new index_js_2.MissingAccrualPositionError(marketId);
109
+ }
110
+ if (snapshot.chainId !== chainId) {
111
+ throw new error_js_1.BlueSnapshotChainMismatchError(snapshot.chainId, chainId);
112
+ }
113
+ const { position } = snapshot;
114
+ if (!(0, morpho_ts_1.isHexEqual)(position.marketId, marketId)) {
115
+ throw new index_js_2.MarketIdMismatchError(position.marketId, marketId);
116
+ }
117
+ if (!(0, morpho_ts_1.isHexEqual)(position.market.id, marketId)) {
118
+ throw new index_js_2.MarketIdMismatchError(position.market.id, marketId);
119
+ }
120
+ (0, validate_js_1.validateMarketParamsId)(position.market.params);
121
+ if (!(0, viem_1.isAddressEqual)(position.user, callbackAddress)) {
122
+ throw new index_js_2.AccrualPositionUserMismatchError(position.user, callbackAddress);
123
+ }
124
+ // Bind the withdrawal to one market state: the position's market bounds the liquidity check.
125
+ for (const field of MARKET_STATE_FIELDS) {
126
+ if (position.market[field] !== marketData.market[field]) {
127
+ throw new error_js_1.BlueSnapshotStateMismatchError({
128
+ field,
129
+ positionValue: position.market[field],
130
+ marketValue: marketData.market[field],
131
+ });
132
+ }
133
+ }
134
+ const intent = (0, intent_js_1.resolveIntent)(params, params.now ?? morpho_ts_1.Time.timestamp());
135
+ const market = marketData.market.id;
136
+ const shares = position.supplyShares;
137
+ if (shares === 0n) {
138
+ throw new index_js_2.EmptyBlueSupplyPositionError({ market, user: callbackAddress });
139
+ }
140
+ const assets = position.supplyAssets;
141
+ const liquidity = position.market.liquidity;
142
+ if (assets > liquidity) {
143
+ throw new index_js_2.InsufficientBlueLiquidityError({ market, assets, liquidity });
144
+ }
145
+ const { loanToken, collateralToken, oracle, irm, lltv } = marketData.market.params;
146
+ // Validate shares, the market tuple and the Blue registration before any RPC read.
147
+ (0, withdrawBuyCallback_internal_js_1.parseBlueWithdrawBuyCallback)({
148
+ market: { chainId, marketParams: marketData.market.params },
149
+ args: { callbackAddress, userAddress: account, shares },
150
+ });
151
+ const blue = (0, morpho_ts_1.getChainAddress)(chainId, "blue");
152
+ const factory = (0, morpho_ts_1.getChainAddress)(chainId, "midnightBlueBuyCallbackFactory");
153
+ await (0, validateClientChainId_js_1.validateClientChainId)(client, chainId);
154
+ const targets = [
155
+ { role: "blue", address: blue },
156
+ { role: "midnightBlueBuyCallbackFactory", address: factory },
157
+ ];
158
+ // Start every read, then consume them in target order so a stalled or failed
159
+ // later read cannot hide Blue's missing code, as `assertRegistryTargetsHaveCode` does.
160
+ const reads = targets.map(({ address }) => {
161
+ const read = (0, actions_1.getBytecode)(client, { address });
162
+ read.catch(() => undefined);
163
+ return read;
164
+ });
165
+ for (const [index, { role, address }] of targets.entries()) {
166
+ const bytecode = await reads[index];
167
+ if (bytecode === undefined || bytecode === "0x") {
168
+ throw new index_js_2.MissingContractCodeError(address, chainId, role);
169
+ }
170
+ }
171
+ // Check the factory first: OWNER() on an arbitrary address can revert or lie.
172
+ const isBlueBuyCallback = await (0, actions_1.readContract)(client, {
173
+ address: factory,
174
+ abi: abis_js_1.midnightBlueBuyCallbackFactoryAbi,
175
+ functionName: "isBlueBuyCallback",
176
+ args: [callbackAddress],
177
+ });
178
+ if (!isBlueBuyCallback) {
179
+ throw new index_js_2.NotBlueBuyCallbackError(callbackAddress);
180
+ }
181
+ const [owner, isAuthorized] = await Promise.all([
182
+ (0, actions_1.readContract)(client, {
183
+ address: callbackAddress,
184
+ abi: abis_js_1.midnightBlueBuyCallbackAbi,
185
+ functionName: "OWNER",
186
+ }),
187
+ (0, actions_1.readContract)(client, {
188
+ address: blue,
189
+ abi: abis_js_1.blueAbi,
190
+ functionName: "isAuthorized",
191
+ args: [callbackAddress, account],
192
+ }),
193
+ ]);
194
+ if (!(0, viem_1.isAddressEqual)(owner, account)) {
195
+ throw new index_js_2.BlueBuyCallbackOwnerMismatchError({
196
+ callback: callbackAddress,
197
+ owner,
198
+ userAddress: account,
199
+ });
200
+ }
201
+ if (!isAuthorized) {
202
+ throw new index_js_2.BlueBuyCallbackUnauthorizedError({
203
+ callback: callbackAddress,
204
+ userAddress: account,
205
+ });
206
+ }
207
+ return (0, morpho_ts_1.deepFreeze)({
208
+ version: 2,
209
+ chainId,
210
+ account,
211
+ expiresAt: intent.expiresAt,
212
+ ...(intent.calldataOrigin && { calldataOrigin: intent.calldataOrigin }),
213
+ steps: [
214
+ {
215
+ id: "blueWithdrawBuyCallback",
216
+ label: "Withdraw BlueBuyCallback supply from Blue",
217
+ action: {
218
+ type: "blueWithdrawBuyCallback",
219
+ inputs: {
220
+ chainId,
221
+ account,
222
+ marketParams: { loanToken, collateralToken, oracle, irm, lltv },
223
+ callbackAddress,
224
+ shares,
225
+ },
226
+ },
227
+ },
228
+ ],
229
+ signatureRequests: [],
230
+ });
231
+ }
232
+ /** @internal Encodes the stored callback withdrawal through the same action as the entity handle. */
233
+ function buildBlueWithdrawBuyCallback(action) {
234
+ const { chainId, account, marketParams, callbackAddress, shares } = action.inputs;
235
+ return (0, withdrawBuyCallback_js_1.blueWithdrawBuyCallback)({
236
+ market: {
237
+ chainId,
238
+ marketParams: {
239
+ ...marketParams,
240
+ id: blue_sdk_1.MarketUtils.getMarketId(marketParams),
241
+ liquidationIncentiveFactor: blue_sdk_1.MarketUtils.getLiquidationIncentiveFactor(marketParams),
242
+ },
243
+ },
244
+ args: { callbackAddress, userAddress: account, shares },
245
+ });
246
+ }
247
+ /** @internal Validates the stored callback withdrawal's structural inputs. */
248
+ function validateBlueWithdrawBuyCallbackPlanAction(action, path) {
249
+ const inputs = (0, validation_js_1.getPlanObject)(action.inputs, `${path}.inputs`);
250
+ (0, validation_js_1.validatePlanFields)(inputs, {
251
+ path: `${path}.inputs`,
252
+ fields: { callbackAddress: "address", shares: "uint256 bigint" },
253
+ });
254
+ (0, validation_js_1.validatePlanFields)((0, validation_js_1.getPlanObject)(inputs.marketParams, `${path}.inputs.marketParams`), {
255
+ path: `${path}.inputs.marketParams`,
256
+ fields: {
257
+ loanToken: "address",
258
+ collateralToken: "address",
259
+ oracle: "address",
260
+ irm: "address",
261
+ lltv: "uint256 bigint",
262
+ },
263
+ });
264
+ }
@@ -48,7 +48,7 @@ export interface BlueWithdrawCollateralPlanParams extends IntentParams, Referenc
48
48
  * @param params.supportSignature - Optional Morpho authorization signature preference, defaulting to `false`; otherwise prepares a direct authorization transaction when needed.
49
49
  * @param params.now - Optional reference timestamp in Unix seconds, normally the latest block timestamp; defaults to the device clock.
50
50
  * @param params.expiresAt - Optional safe-integer Unix expiry later than `now`; defaults to `now + 2h` and governs every deadline.
51
- * @param params.calldataOrigin - Optional non-empty, even-length hex bytes appended verbatim to every built transaction.
51
+ * @param params.calldataOrigin - Optional non-empty, even-length hex bytes appended verbatim to the action transaction's calldata; approval and authorization steps carry none.
52
52
  * @param params.deployless - Optional shared intent field; unused because this operation reads no token metadata or allowances.
53
53
  * @returns A deep-frozen, data-only `TransactionPlan` with chain, account, expiry, withdrawal step,
54
54
  * and any preceding Morpho authorization step and signature request. The caller signs; the SDK never does.
@@ -74,7 +74,6 @@ export interface BlueWithdrawCollateralPlanParams extends IntentParams, Referenc
74
74
  * @throws {viem.BaseError} when a contract-code, chain ID, authorization or nonce RPC read fails.
75
75
  * @example
76
76
  * ```ts
77
- * import assert from "node:assert/strict";
78
77
  * import { markets } from "@morpho-org/morpho-test";
79
78
  * import { fetchAccrualPosition } from "@morpho-org/morpho-sdk/blue/fetch";
80
79
  * import {
@@ -89,7 +88,7 @@ export interface BlueWithdrawCollateralPlanParams extends IntentParams, Referenc
89
88
  * if (!ethereum) throw new Error("Install an Ethereum wallet to continue.");
90
89
  * const wallet = createWalletClient({ chain: mainnet, transport: custom(ethereum) });
91
90
  * const [account] = await wallet.requestAddresses();
92
- * assert(account, "Connect a wallet holding collateral in this market.");
91
+ * if (!account) throw new Error("Connect a wallet holding collateral in this market.");
93
92
  * const market = markets[mainnet.id].usdc_wbtc;
94
93
  * const position = await fetchAccrualPosition(account, market.id, client);
95
94
  * const plan = await prepareBlueWithdrawCollateral(client, {