@morpho-org/morpho-sdk 10.0.0-next.0 → 10.0.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 (282) hide show
  1. package/README.md +104 -12
  2. package/lib/cjs/abis.d.ts +1 -1
  3. package/lib/cjs/abis.js +1 -5
  4. package/lib/cjs/actions/blue/common.js +4 -2
  5. package/lib/cjs/actions/blue/withdrawBuyCallback.js +2 -1
  6. package/lib/cjs/actions/bundles/common.js +20 -14
  7. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +2 -1
  8. package/lib/cjs/actions/midnight/cancelOffers.js +2 -1
  9. package/lib/cjs/actions/midnight/supplyCollateralBatch.js +4 -2
  10. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +2 -1
  11. package/lib/cjs/actions/midnight/takeBorrow.js +2 -1
  12. package/lib/cjs/actions/midnight/takeLend.js +2 -1
  13. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +2 -1
  14. package/lib/cjs/actions/midnight/takeWithdraw.js +2 -1
  15. package/lib/cjs/actions/midnight/withdrawCollateralBatch.js +4 -2
  16. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +2 -11
  17. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.js +2 -1
  18. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +2 -1
  19. package/lib/cjs/actions/vaultV1/inKindRedeem.js +6 -3
  20. package/lib/cjs/actions/vaultV1/migrateToV2.js +1 -1
  21. package/lib/cjs/actions/vaultV2/forceWithdraw.js +4 -2
  22. package/lib/cjs/actions/vaultV2/inKindRedeem.js +4 -2
  23. package/lib/cjs/blue/abis.d.ts +1 -1
  24. package/lib/cjs/blue/abis.js +1 -5
  25. package/lib/cjs/blue/entities.d.ts +4 -4
  26. package/lib/cjs/blue/entities.js +1 -3
  27. package/lib/cjs/blue/errors.d.ts +2 -2
  28. package/lib/cjs/blue/errors.js +1 -5
  29. package/lib/cjs/blue/fetch.d.ts +2 -2
  30. package/lib/cjs/blue/fetch.js +1 -3
  31. package/lib/cjs/blue/utils.d.ts +1 -1
  32. package/lib/cjs/blue/utils.js +1 -2
  33. package/lib/cjs/entities/blue/blue.js +8 -4
  34. package/lib/cjs/entities/index.d.ts +4 -4
  35. package/lib/cjs/entities/index.js +1 -3
  36. package/lib/cjs/entities/midnight/deadline.js +2 -1
  37. package/lib/cjs/entities/midnight/midnight.js +14 -7
  38. package/lib/cjs/entities/midnight/publication.js +73 -44
  39. package/lib/cjs/entities/requirements/assertContractCode.js +4 -2
  40. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +9 -13
  41. package/lib/cjs/entities/requirements/getErc2612Permit.js +10 -5
  42. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +2 -0
  43. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +7 -1
  44. package/lib/cjs/entities/requirements/getVaultV1InKindRedeemRequirements.d.ts +21 -0
  45. package/lib/cjs/entities/requirements/getVaultV1InKindRedeemRequirements.js +111 -0
  46. package/lib/cjs/entities/requirements/getVaultV2ForceWithdrawRequirements.d.ts +20 -0
  47. package/lib/cjs/entities/requirements/getVaultV2ForceWithdrawRequirements.js +76 -0
  48. package/lib/cjs/entities/requirements/getVaultV2InKindRedeemRequirements.d.ts +21 -0
  49. package/lib/cjs/entities/requirements/getVaultV2InKindRedeemRequirements.js +93 -0
  50. package/lib/cjs/entities/vaultV1/inKindRedeemQuote.d.ts +25 -0
  51. package/lib/cjs/entities/vaultV1/inKindRedeemQuote.js +85 -0
  52. package/lib/cjs/entities/vaultV1/migrateToV2Quote.d.ts +20 -0
  53. package/lib/cjs/entities/vaultV1/migrateToV2Quote.js +37 -0
  54. package/lib/cjs/entities/vaultV1/vaultV1.js +51 -181
  55. package/lib/cjs/entities/vaultV2/forceWithdrawQuote.d.ts +25 -0
  56. package/lib/cjs/entities/vaultV2/forceWithdrawQuote.js +189 -0
  57. package/lib/cjs/entities/vaultV2/inKindRedeemQuote.d.ts +28 -0
  58. package/lib/cjs/entities/vaultV2/inKindRedeemQuote.js +110 -0
  59. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +8 -34
  60. package/lib/cjs/entities/vaultV2/vaultV2.js +62 -429
  61. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +2 -3
  62. package/lib/cjs/entities/vaultV2BlueReallocationData.js +346 -234
  63. package/lib/cjs/errors.d.ts +4 -4
  64. package/lib/cjs/errors.js +6 -7
  65. package/lib/cjs/fetch.d.ts +2 -2
  66. package/lib/cjs/fetch.js +1 -3
  67. package/lib/cjs/helpers/deadShares.js +34 -19
  68. package/lib/cjs/helpers/encodeMidnightCalldata.js +4 -2
  69. package/lib/cjs/helpers/metadata.js +12 -6
  70. package/lib/cjs/helpers/previewVaultV2ForceWithdraw.js +10 -5
  71. package/lib/cjs/helpers/previewVaultV2InKindRedeem.js +6 -3
  72. package/lib/cjs/helpers/utilization.js +10 -5
  73. package/lib/cjs/helpers/validate.js +12 -6
  74. package/lib/cjs/helpers/validateMidnightBounds.js +2 -1
  75. package/lib/cjs/helpers/validateMidnightCollateralBatch.js +2 -1
  76. package/lib/cjs/helpers/validateMidnightPublication.js +4 -2
  77. package/lib/cjs/helpers/validateMidnightTakeFeasibility.js +44 -22
  78. package/lib/cjs/helpers/validatePositiveInput.js +2 -1
  79. package/lib/cjs/helpers/validateTakeableOffers.js +2 -1
  80. package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.js +14 -7
  81. package/lib/cjs/index.d.ts +2 -2
  82. package/lib/cjs/index.js +10 -1
  83. package/lib/cjs/midnight/errors.d.ts +1 -2
  84. package/lib/cjs/midnight/errors.js +4 -5
  85. package/lib/cjs/transactionPlan/approval.js +12 -1
  86. package/lib/cjs/transactionPlan/deposit.d.ts +22 -0
  87. package/lib/cjs/transactionPlan/deposit.js +139 -0
  88. package/lib/cjs/transactionPlan/error.d.ts +10 -8
  89. package/lib/cjs/transactionPlan/error.js +10 -8
  90. package/lib/cjs/transactionPlan/getTokenPermit.js +2 -1
  91. package/lib/cjs/transactionPlan/index.d.ts +18 -0
  92. package/lib/cjs/transactionPlan/index.js +23 -5
  93. package/lib/cjs/transactionPlan/intent.d.ts +3 -3
  94. package/lib/cjs/transactionPlan/intent.js +14 -7
  95. package/lib/cjs/transactionPlan/permit.d.ts +1 -1
  96. package/lib/cjs/transactionPlan/permit.js +86 -20
  97. package/lib/cjs/transactionPlan/plan.d.ts +16 -6
  98. package/lib/cjs/transactionPlan/plan.js +72 -22
  99. package/lib/cjs/transactionPlan/redeem.d.ts +18 -0
  100. package/lib/cjs/transactionPlan/redeem.js +63 -0
  101. package/lib/cjs/transactionPlan/requirements.d.ts +4 -3
  102. package/lib/cjs/transactionPlan/requirements.js +12 -6
  103. package/lib/cjs/transactionPlan/serde.d.ts +2 -2
  104. package/lib/cjs/transactionPlan/serde.js +41 -15
  105. package/lib/cjs/transactionPlan/shareSpending.d.ts +18 -0
  106. package/lib/cjs/transactionPlan/shareSpending.js +50 -0
  107. package/lib/cjs/transactionPlan/types.d.ts +117 -11
  108. package/lib/cjs/transactionPlan/validateActions.js +11 -1
  109. package/lib/cjs/transactionPlan/validation.js +6 -3
  110. package/lib/cjs/transactionPlan/vaultv1/deposit.d.ts +93 -0
  111. package/lib/cjs/transactionPlan/vaultv1/deposit.js +94 -0
  112. package/lib/cjs/transactionPlan/vaultv1/inKindRedeem.d.ts +96 -0
  113. package/lib/cjs/transactionPlan/vaultv1/inKindRedeem.js +203 -0
  114. package/lib/cjs/transactionPlan/vaultv1/migrateToV2.d.ts +94 -0
  115. package/lib/cjs/transactionPlan/vaultv1/migrateToV2.js +217 -0
  116. package/lib/cjs/transactionPlan/vaultv1/redeem.d.ts +69 -0
  117. package/lib/cjs/transactionPlan/vaultv1/redeem.js +78 -0
  118. package/lib/cjs/transactionPlan/vaultv1/withdraw.d.ts +77 -0
  119. package/lib/cjs/transactionPlan/vaultv1/withdraw.js +88 -0
  120. package/lib/cjs/transactionPlan/vaultv2/deposit.d.ts +14 -31
  121. package/lib/cjs/transactionPlan/vaultv2/deposit.js +15 -141
  122. package/lib/cjs/transactionPlan/vaultv2/forceRedeem.d.ts +74 -0
  123. package/lib/cjs/transactionPlan/vaultv2/forceRedeem.js +155 -0
  124. package/lib/cjs/transactionPlan/vaultv2/forceWithdraw.d.ts +98 -0
  125. package/lib/cjs/transactionPlan/vaultv2/forceWithdraw.js +206 -0
  126. package/lib/cjs/transactionPlan/vaultv2/inKindRedeem.d.ts +113 -0
  127. package/lib/cjs/transactionPlan/vaultv2/inKindRedeem.js +206 -0
  128. package/lib/cjs/transactionPlan/vaultv2/redeem.d.ts +84 -0
  129. package/lib/cjs/transactionPlan/vaultv2/redeem.js +86 -0
  130. package/lib/cjs/transactionPlan/vaultv2/withdraw.d.ts +14 -27
  131. package/lib/cjs/transactionPlan/vaultv2/withdraw.js +17 -124
  132. package/lib/cjs/transactionPlan/version.js +2 -1
  133. package/lib/cjs/transactionPlan/withdraw.d.ts +18 -0
  134. package/lib/cjs/transactionPlan/withdraw.js +94 -0
  135. package/lib/cjs/types/action.js +16 -8
  136. package/lib/cjs/types/error.d.ts +8 -1
  137. package/lib/cjs/types/error.js +10 -1
  138. package/lib/cjs/utils.d.ts +2 -2
  139. package/lib/cjs/utils.js +2 -4
  140. package/lib/cjs/version.d.ts +1 -1
  141. package/lib/cjs/version.js +1 -1
  142. package/lib/esm/abis.d.ts +1 -1
  143. package/lib/esm/abis.js +1 -1
  144. package/lib/esm/actions/blue/common.js +4 -2
  145. package/lib/esm/actions/blue/withdrawBuyCallback.js +2 -1
  146. package/lib/esm/actions/bundles/common.js +20 -14
  147. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +2 -1
  148. package/lib/esm/actions/midnight/cancelOffers.js +2 -1
  149. package/lib/esm/actions/midnight/supplyCollateralBatch.js +4 -2
  150. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +2 -1
  151. package/lib/esm/actions/midnight/takeBorrow.js +2 -1
  152. package/lib/esm/actions/midnight/takeLend.js +2 -1
  153. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +2 -1
  154. package/lib/esm/actions/midnight/takeWithdraw.js +2 -1
  155. package/lib/esm/actions/midnight/withdrawCollateralBatch.js +4 -2
  156. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +4 -13
  157. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.js +2 -1
  158. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +2 -1
  159. package/lib/esm/actions/vaultV1/inKindRedeem.js +6 -3
  160. package/lib/esm/actions/vaultV1/migrateToV2.js +1 -1
  161. package/lib/esm/actions/vaultV2/forceWithdraw.js +4 -2
  162. package/lib/esm/actions/vaultV2/inKindRedeem.js +4 -2
  163. package/lib/esm/blue/abis.d.ts +1 -1
  164. package/lib/esm/blue/abis.js +1 -1
  165. package/lib/esm/blue/entities.d.ts +4 -4
  166. package/lib/esm/blue/entities.js +2 -2
  167. package/lib/esm/blue/errors.d.ts +2 -2
  168. package/lib/esm/blue/errors.js +2 -2
  169. package/lib/esm/blue/fetch.d.ts +2 -2
  170. package/lib/esm/blue/fetch.js +2 -2
  171. package/lib/esm/blue/utils.d.ts +1 -1
  172. package/lib/esm/blue/utils.js +1 -1
  173. package/lib/esm/entities/blue/blue.js +8 -4
  174. package/lib/esm/entities/index.d.ts +4 -4
  175. package/lib/esm/entities/index.js +2 -2
  176. package/lib/esm/entities/midnight/deadline.js +2 -1
  177. package/lib/esm/entities/midnight/midnight.js +14 -7
  178. package/lib/esm/entities/midnight/publication.js +73 -44
  179. package/lib/esm/entities/requirements/assertContractCode.js +4 -2
  180. package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +10 -14
  181. package/lib/esm/entities/requirements/getErc2612Permit.js +10 -5
  182. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +2 -0
  183. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +7 -1
  184. package/lib/esm/entities/requirements/getVaultV1InKindRedeemRequirements.d.ts +21 -0
  185. package/lib/esm/entities/requirements/getVaultV1InKindRedeemRequirements.js +108 -0
  186. package/lib/esm/entities/requirements/getVaultV2ForceWithdrawRequirements.d.ts +20 -0
  187. package/lib/esm/entities/requirements/getVaultV2ForceWithdrawRequirements.js +73 -0
  188. package/lib/esm/entities/requirements/getVaultV2InKindRedeemRequirements.d.ts +21 -0
  189. package/lib/esm/entities/requirements/getVaultV2InKindRedeemRequirements.js +90 -0
  190. package/lib/esm/entities/vaultV1/inKindRedeemQuote.d.ts +25 -0
  191. package/lib/esm/entities/vaultV1/inKindRedeemQuote.js +82 -0
  192. package/lib/esm/entities/vaultV1/migrateToV2Quote.d.ts +20 -0
  193. package/lib/esm/entities/vaultV1/migrateToV2Quote.js +34 -0
  194. package/lib/esm/entities/vaultV1/vaultV1.js +57 -187
  195. package/lib/esm/entities/vaultV2/forceWithdrawQuote.d.ts +25 -0
  196. package/lib/esm/entities/vaultV2/forceWithdrawQuote.js +186 -0
  197. package/lib/esm/entities/vaultV2/inKindRedeemQuote.d.ts +28 -0
  198. package/lib/esm/entities/vaultV2/inKindRedeemQuote.js +107 -0
  199. package/lib/esm/entities/vaultV2/vaultV2.d.ts +8 -34
  200. package/lib/esm/entities/vaultV2/vaultV2.js +70 -437
  201. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +2 -3
  202. package/lib/esm/entities/vaultV2BlueReallocationData.js +346 -234
  203. package/lib/esm/errors.d.ts +4 -4
  204. package/lib/esm/errors.js +4 -4
  205. package/lib/esm/fetch.d.ts +2 -2
  206. package/lib/esm/fetch.js +2 -2
  207. package/lib/esm/helpers/deadShares.js +35 -20
  208. package/lib/esm/helpers/encodeMidnightCalldata.js +4 -2
  209. package/lib/esm/helpers/metadata.js +12 -6
  210. package/lib/esm/helpers/previewVaultV2ForceWithdraw.js +10 -5
  211. package/lib/esm/helpers/previewVaultV2InKindRedeem.js +6 -3
  212. package/lib/esm/helpers/utilization.js +10 -5
  213. package/lib/esm/helpers/validate.js +12 -6
  214. package/lib/esm/helpers/validateMidnightBounds.js +2 -1
  215. package/lib/esm/helpers/validateMidnightCollateralBatch.js +2 -1
  216. package/lib/esm/helpers/validateMidnightPublication.js +4 -2
  217. package/lib/esm/helpers/validateMidnightTakeFeasibility.js +44 -22
  218. package/lib/esm/helpers/validatePositiveInput.js +2 -1
  219. package/lib/esm/helpers/validateTakeableOffers.js +2 -1
  220. package/lib/esm/helpers/vaultV2ForceWithdrawPlan.js +14 -7
  221. package/lib/esm/index.d.ts +2 -2
  222. package/lib/esm/index.js +1 -1
  223. package/lib/esm/midnight/errors.d.ts +1 -2
  224. package/lib/esm/midnight/errors.js +1 -2
  225. package/lib/esm/transactionPlan/approval.js +13 -2
  226. package/lib/esm/transactionPlan/deposit.d.ts +22 -0
  227. package/lib/esm/transactionPlan/deposit.js +135 -0
  228. package/lib/esm/transactionPlan/error.d.ts +10 -8
  229. package/lib/esm/transactionPlan/error.js +10 -8
  230. package/lib/esm/transactionPlan/getTokenPermit.js +2 -1
  231. package/lib/esm/transactionPlan/index.d.ts +18 -0
  232. package/lib/esm/transactionPlan/index.js +9 -0
  233. package/lib/esm/transactionPlan/intent.d.ts +3 -3
  234. package/lib/esm/transactionPlan/intent.js +14 -7
  235. package/lib/esm/transactionPlan/permit.d.ts +1 -1
  236. package/lib/esm/transactionPlan/permit.js +86 -20
  237. package/lib/esm/transactionPlan/plan.d.ts +16 -6
  238. package/lib/esm/transactionPlan/plan.js +68 -18
  239. package/lib/esm/transactionPlan/redeem.d.ts +18 -0
  240. package/lib/esm/transactionPlan/redeem.js +59 -0
  241. package/lib/esm/transactionPlan/requirements.d.ts +4 -3
  242. package/lib/esm/transactionPlan/requirements.js +12 -6
  243. package/lib/esm/transactionPlan/serde.d.ts +2 -2
  244. package/lib/esm/transactionPlan/serde.js +41 -15
  245. package/lib/esm/transactionPlan/shareSpending.d.ts +18 -0
  246. package/lib/esm/transactionPlan/shareSpending.js +47 -0
  247. package/lib/esm/transactionPlan/types.d.ts +117 -11
  248. package/lib/esm/transactionPlan/validateActions.js +11 -1
  249. package/lib/esm/transactionPlan/validation.js +6 -3
  250. package/lib/esm/transactionPlan/vaultv1/deposit.d.ts +93 -0
  251. package/lib/esm/transactionPlan/vaultv1/deposit.js +90 -0
  252. package/lib/esm/transactionPlan/vaultv1/inKindRedeem.d.ts +96 -0
  253. package/lib/esm/transactionPlan/vaultv1/inKindRedeem.js +198 -0
  254. package/lib/esm/transactionPlan/vaultv1/migrateToV2.d.ts +94 -0
  255. package/lib/esm/transactionPlan/vaultv1/migrateToV2.js +212 -0
  256. package/lib/esm/transactionPlan/vaultv1/redeem.d.ts +69 -0
  257. package/lib/esm/transactionPlan/vaultv1/redeem.js +74 -0
  258. package/lib/esm/transactionPlan/vaultv1/withdraw.d.ts +77 -0
  259. package/lib/esm/transactionPlan/vaultv1/withdraw.js +84 -0
  260. package/lib/esm/transactionPlan/vaultv2/deposit.d.ts +14 -31
  261. package/lib/esm/transactionPlan/vaultv2/deposit.js +17 -142
  262. package/lib/esm/transactionPlan/vaultv2/forceRedeem.d.ts +74 -0
  263. package/lib/esm/transactionPlan/vaultv2/forceRedeem.js +150 -0
  264. package/lib/esm/transactionPlan/vaultv2/forceWithdraw.d.ts +98 -0
  265. package/lib/esm/transactionPlan/vaultv2/forceWithdraw.js +201 -0
  266. package/lib/esm/transactionPlan/vaultv2/inKindRedeem.d.ts +113 -0
  267. package/lib/esm/transactionPlan/vaultv2/inKindRedeem.js +201 -0
  268. package/lib/esm/transactionPlan/vaultv2/redeem.d.ts +84 -0
  269. package/lib/esm/transactionPlan/vaultv2/redeem.js +82 -0
  270. package/lib/esm/transactionPlan/vaultv2/withdraw.d.ts +14 -27
  271. package/lib/esm/transactionPlan/vaultv2/withdraw.js +19 -125
  272. package/lib/esm/transactionPlan/version.js +2 -1
  273. package/lib/esm/transactionPlan/withdraw.d.ts +18 -0
  274. package/lib/esm/transactionPlan/withdraw.js +90 -0
  275. package/lib/esm/types/action.js +16 -8
  276. package/lib/esm/types/error.d.ts +8 -1
  277. package/lib/esm/types/error.js +10 -1
  278. package/lib/esm/utils.d.ts +2 -2
  279. package/lib/esm/utils.js +2 -2
  280. package/lib/esm/version.d.ts +1 -1
  281. package/lib/esm/version.js +1 -1
  282. package/package.json +15 -15
@@ -0,0 +1,82 @@
1
+ import { getChainAddress } from "@morpho-org/morpho-ts";
2
+ import { selectBundlesSharesPermitSignature } from "../../actions/bundles/common.js";
3
+ import { vaultV2Redeem } from "../../actions/vaultV2/redeem.js";
4
+ import { getVaultPermit } from "../permit.js";
5
+ import { prepareVaultRedeem } from "../redeem.js";
6
+ /**
7
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
8
+ * Prepares an exact-share redemption through VaultBundlesV1. Reads share allowance and, when a
9
+ * permit is needed, the vault nonce. No quote or fetch request is needed for an exact share count.
10
+ * @param client - Viem client targeting `params.chainId`.
11
+ * @param params.shareToken - Vault share token; an address-only Token is sufficient, without an accrual snapshot.
12
+ * @param params.chainId - Chain containing the vault and registered VaultBundlesV1 contract.
13
+ * @param params.account - Non-zero share owner, permit signer and transaction sender; receives the net assets.
14
+ * @param params.shares - Positive exact shares to burn, in share base units; use the account's balance for full redemption.
15
+ * @param params.referralFee.bps - Optional integer fee in basis points of gross redeemed assets; defaults to zero and must be below 10,000.
16
+ * @param params.referralFee.recipient - Optional fee recipient; must be non-zero when the fee is positive.
17
+ * @param params.supportSignature - Optional ERC-2612 share-permit preference; defaults to `false` for direct approval.
18
+ * @param params.now - Reference timestamp in seconds, normally the latest block's `timestamp`; uses the device clock when omitted.
19
+ * @param params.expiresAt - Optional Unix timestamp in seconds as a safe integer, later than `now`; defaults to `now + 2h` (device clock when `now` is omitted).
20
+ * @param params.calldataOrigin - Optional non-empty, even-length hex bytes appended to each built transaction's calldata.
21
+ * @returns A deep-frozen, serializable `TransactionPlan` with version, chain, account, expiry,
22
+ * ordered approval/redeem steps and signature requests. Any required share approval or permit is unlimited.
23
+ * @throws {ZeroAccountError} when `account` is the zero address.
24
+ * @throws {NonIntegerInputError} when `expiresAt` or `referralFee.bps` is not a safe integer.
25
+ * @throws {NonPositiveInputError} when `expiresAt` or `shares` is not positive.
26
+ * @throws {ExpiredDeadlineError} when `expiresAt` is not in the future.
27
+ * @throws {NegativeInputError} when `referralFee.bps` is negative.
28
+ * @throws {InputExceedsMaxError} when `referralFee.bps` exceeds 10,000 or `shares` exceeds uint256.
29
+ * @throws {InvalidCalldataOriginError} when `calldataOrigin` is empty, malformed or has an odd number of hex digits.
30
+ * @throws {ReferralFeePctExceededError} when `referralFee.bps` equals 10,000; the normalized fee must be below WAD.
31
+ * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no non-zero recipient.
32
+ * @throws {ChainIdMismatchError} when the declared or RPC chain differs from `chainId`.
33
+ * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
34
+ * @throws {UnknownAddressError} when VaultBundlesV1 is not registered on the target chain.
35
+ * @throws {MissingContractCodeError} when VaultBundlesV1 is absent on the target chain.
36
+ * @throws {viem.BaseError} when a chain, bytecode, allowance or permit nonce read fails.
37
+ * @example
38
+ * ```ts
39
+ * import { Token } from "@morpho-org/morpho-sdk/blue/entities";
40
+ * import { prepareVaultV2Redeem, type TransactionPlan } from "@morpho-org/morpho-sdk";
41
+ * import { type Address, createPublicClient, http } from "viem";
42
+ * import { mainnet } from "viem/chains";
43
+ *
44
+ * // Supply a non-zero share owner and its desired redemption shares in share base units.
45
+ * export async function prepareRedeem(account: Address, shares: bigint): Promise<TransactionPlan> {
46
+ * const client = createPublicClient({ chain: mainnet, transport: http() });
47
+ * const vault = "0x04422053aDDbc9bB2759b248B574e3FCA76Bc145";
48
+ * const plan = await prepareVaultV2Redeem(client, {
49
+ * shareToken: new Token({ address: vault }), chainId: mainnet.id, account, shares, supportSignature: false,
50
+ * });
51
+ * // plan: TransactionPlan = { version: 2, chainId: 1, account, expiresAt,
52
+ * // steps: [optional unlimited approval, vaultV2Redeem], signatureRequests: [] }.
53
+ * return plan;
54
+ * }
55
+ * ```
56
+ */
57
+ export async function prepareVaultV2Redeem(client, params) {
58
+ return prepareVaultRedeem(client, { ...params, type: "vaultV2Redeem" });
59
+ }
60
+ /** @internal Encodes a redemption from its fixed inputs and referenced share permit. */
61
+ export function buildVaultV2Redeem(action, requests) {
62
+ const { chainId, account, vault, shares, ...common } = action.inputs;
63
+ const signature = getVaultPermit(requests, {
64
+ id: action.signatureRequestIds.tokenPermit,
65
+ asset: vault,
66
+ owner: account,
67
+ });
68
+ const permit = selectBundlesSharesPermitSignature(signature ? [signature] : [], {
69
+ spender: getChainAddress(chainId, "bundles.vaultBundlesV1"),
70
+ amount: shares,
71
+ deadline: action.inputs.deadline,
72
+ });
73
+ return vaultV2Redeem({
74
+ vault: { chainId, address: vault },
75
+ args: {
76
+ ...common,
77
+ shares,
78
+ userAddress: account,
79
+ requirementSignature: permit,
80
+ },
81
+ });
82
+ }
@@ -1,25 +1,16 @@
1
- import { type AccrualVaultV2 } from "@morpho-org/blue-sdk";
2
- import { type Address, type Client } from "viem";
3
- import { type ReferenceTimestampParams, type Transaction, type VaultV2WithdrawAction } from "../../types/index.js";
4
- import { type IntentParams } from "../intent.js";
1
+ import type { AccrualVaultV2 } from "@morpho-org/blue-sdk";
2
+ import type { Client } from "viem";
3
+ import type { Transaction, VaultV2WithdrawAction } from "../../types/index.js";
5
4
  import type { Action, SignatureRequest, TransactionPlan } from "../types.js";
6
- /** @internal Validates the stored Vault V2 withdrawal action's input shape. */
7
- export declare function validateWithdrawPlanAction(action: Record<string, unknown>, path: string): void;
5
+ import { type WithdrawPlanParams } from "../withdraw.js";
8
6
  /**
9
- * Caller intent for a Vault V2 asset withdrawal, mirroring the Actions API withdraw inputs, plus the snapshot.
10
7
  * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
8
+ * Caller intent for a Vault V2 asset withdrawal, mirroring the Actions API withdraw inputs, plus the snapshot.
11
9
  */
12
- export interface VaultV2WithdrawPlanParams extends IntentParams, ReferenceTimestampParams {
13
- readonly vault: Address;
14
- /** Gross assets withdrawn, before the referral fee. */
15
- readonly amount: bigint;
16
- /**
17
- * Accrual snapshot of `vault`, fetched by the caller with `fetchAccrualVaultV2`. The required
18
- * share allowance is derived from it at preparation; the snapshot itself is never stored.
19
- */
20
- readonly vaultData: AccrualVaultV2;
10
+ export interface VaultV2WithdrawPlanParams extends WithdrawPlanParams<AccrualVaultV2> {
21
11
  }
22
12
  /**
13
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
23
14
  * Prepares a withdrawal plan from caller intent and a vault snapshot. The share quote is fixed on
24
15
  * the withdrawal step at preparation, because the required share allowance decides the approval or
25
16
  * permit prerequisite, so the returned plan is static: only signatures are added before building.
@@ -28,29 +19,26 @@ export interface VaultV2WithdrawPlanParams extends IntentParams, ReferenceTimest
28
19
  * The share-allowance quote uses `DEFAULT_SLIPPAGE_TOLERANCE` internally. The withdrawal has no
29
20
  * on-chain share-price or max-shares bound and accepts no slippage tolerance input.
30
21
  * @param client - Viem client for `params.chainId`, used for allowance and nonce reads only.
31
- * @param params.vault - Vault V2 contract whose shares are burned for the withdrawal.
32
- * @param params.vaultData - Caller-fetched `AccrualVaultV2` snapshot of `vault`, used to derive the required share allowance.
22
+ * @param params.vault - Caller-fetched AccrualVaultV2 snapshot used for the quote; the address is vault.address.
33
23
  * @param params.chainId - Chain containing the vault and registered VaultBundlesV1 contract.
34
24
  * @param params.account - Non-zero account approving or permitting shares and sending the withdrawal.
35
- * @param params.amount - Positive gross withdrawal in asset base units, before referral fees.
36
- * @param params.referralFee.bps - Optional integer fee in basis points of the gross amount; defaults to zero and must be below 10,000.
25
+ * @param params.assets - Positive gross withdrawal in asset base units, before referral fees.
26
+ * @param params.referralFee.bps - Optional integer fee in basis points of the gross assets; defaults to zero and must be below 10,000.
37
27
  * @param params.referralFee.recipient - Recipient of the optional fee; must be non-zero when the fee is positive.
38
28
  * @param params.supportSignature - Optional unlimited share-permit preference, defaulting to `false`; otherwise uses unlimited direct share approval when allowance is insufficient.
39
29
  * @param params.now - Reference timestamp in seconds, normally the latest block's `timestamp`; uses the device clock when omitted.
40
30
  * @param params.expiresAt - Optional Unix timestamp in seconds as a safe integer, later than `now`; defaults to `now + 2h` (device clock when `now` is omitted).
41
31
  * @param params.calldataOrigin - Optional non-empty, even-length hex bytes appended to each built transaction's calldata.
42
- * @param params.deployless - Optional shared intent field; unused by withdrawal share-permit reads.
43
32
  * @returns A plan with the withdrawal step and any required share approval or signature request.
44
33
  * @throws {ZeroAccountError} when `account` is the zero address.
45
34
  * @throws {NonIntegerInputError} when `expiresAt` or `referralFee.bps` is not a safe integer.
46
- * @throws {NonPositiveInputError} when `expiresAt`, `amount` or the computed required shares are not positive.
35
+ * @throws {NonPositiveInputError} when `expiresAt`, `assets` or the computed required shares are not positive.
47
36
  * @throws {ExpiredDeadlineError} when `expiresAt` is at or before `now` (device clock when `now` is omitted).
48
37
  * @throws {NegativeInputError} when `referralFee.bps` is negative.
49
- * @throws {InputExceedsMaxError} when `referralFee.bps` exceeds 10,000 or `amount` exceeds uint256.
38
+ * @throws {InputExceedsMaxError} when `referralFee.bps` exceeds 10,000 or `assets` exceeds uint256.
50
39
  * @throws {InvalidCalldataOriginError} when `calldataOrigin` is empty, malformed or has an odd number of hex digits.
51
40
  * @throws {ReferralFeePctExceededError} when `referralFee.bps` equals 10,000; the normalized fee must be below WAD.
52
41
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient or a zero recipient.
53
- * @throws {VaultAddressMismatchError} when `vaultData` describes another vault.
54
42
  * @throws {ChainIdMismatchError} when the client targets another chain.
55
43
  * @throws {MissingContractCodeError} when the VaultBundlesV1 contract is absent.
56
44
  * @see computeVaultMaxShareAllowance for quote errors.
@@ -64,13 +52,12 @@ export interface VaultV2WithdrawPlanParams extends IntentParams, ReferenceTimest
64
52
  * const client = createPublicClient({ chain: mainnet, transport: http() });
65
53
  * const vault = "0x04422053aDDbc9bB2759b248B574e3FCA76Bc145";
66
54
  * const plan = await prepareVaultV2Withdraw(client, {
67
- * vault, chainId: 1, account: "0x0000000000000000000000000000000000000001", amount: 1_000_000n, supportSignature: true,
68
- * vaultData: await fetchAccrualVaultV2(vault, client),
55
+ * chainId: 1, account: "0x0000000000000000000000000000000000000001", assets: 1_000_000n, supportSignature: true,
56
+ * vault: await fetchAccrualVaultV2(vault, client),
69
57
  * now: (await client.getBlock()).timestamp,
70
58
  * });
71
59
  * // Sign the pending requests, verify and attach with applySignatures, then build.
72
60
  * ```
73
- * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
74
61
  */
75
62
  export declare function prepareVaultV2Withdraw(client: Client, params: VaultV2WithdrawPlanParams): Promise<TransactionPlan>;
76
63
  /** @internal Encodes the withdrawal from its self-contained inputs and resolved signature requests. */
@@ -1,53 +1,10 @@
1
- import { DEFAULT_SLIPPAGE_TOLERANCE, Token, } from "@morpho-org/blue-sdk";
2
- import { deepFreeze, getChainAddress, Time } from "@morpho-org/morpho-ts";
3
- import { isAddressEqual } from "viem";
4
- import { normalizeBundlesCommonParams, selectBundlesSharesPermitSignature, } from "../../actions/bundles/common.js";
5
- import { validateClientChainId } from "../../actions/requirements/validateClientChainId.js";
1
+ import { getChainAddress } from "@morpho-org/morpho-ts";
2
+ import { selectBundlesSharesPermitSignature } from "../../actions/bundles/common.js";
6
3
  import { vaultV2Withdraw } from "../../actions/vaultV2/withdraw.js";
7
- import { assertRegistryTargetsHaveCode } from "../../entities/requirements/assertContractCode.js";
8
- import { getVaultBundlesSharesRequirements } from "../../entities/requirements/getVaultBundlesSharesRequirements.js";
9
- import { computeVaultMaxShareAllowance } from "../../helpers/index.js";
10
- import { validateUint256Field } from "../../helpers/validate.js";
11
- import { NonPositiveInputError, VaultAddressMismatchError, } from "../../types/index.js";
12
- import { resolveIntent } from "../intent.js";
13
4
  import { getVaultPermit } from "../permit.js";
14
- import { buildApprovalStep, buildPermitSignatureRequest, } from "../requirements.js";
15
- import { validatePlanFields, validateVaultPlanAction } from "../validation.js";
16
- /** @internal Validates the stored Vault V2 withdrawal action's input shape. */
17
- export function validateWithdrawPlanAction(action, path) {
18
- const inputs = validateVaultPlanAction(action, path);
19
- validatePlanFields(inputs, {
20
- path: `${path}.inputs`,
21
- fields: {
22
- amount: "uint256 bigint",
23
- requiredShareAllowance: "uint256 bigint",
24
- },
25
- });
26
- }
27
- function resolveWithdrawInputs(params, now) {
28
- const { vault, amount, vaultData } = params;
29
- const intent = resolveIntent(params, now);
30
- if (amount <= 0n)
31
- throw new NonPositiveInputError("amount", amount);
32
- // Reject amounts that cannot be encoded as uint256 before making RPC requests.
33
- validateUint256Field("amount", amount);
34
- const common = normalizeBundlesCommonParams({
35
- deadline: intent.deadline,
36
- referralFeePct: intent.referralFeePct,
37
- referralFeeRecipient: intent.referralFeeRecipient,
38
- });
39
- if (!isAddressEqual(vaultData.address, vault))
40
- throw new VaultAddressMismatchError(vaultData.address, vault);
41
- // The quote depends only on the snapshot and the intent, so it is fixed before any read.
42
- const requiredShareAllowance = computeVaultMaxShareAllowance({
43
- vaultData,
44
- deadline: intent.deadline,
45
- assets: amount,
46
- slippageTolerance: DEFAULT_SLIPPAGE_TOLERANCE,
47
- });
48
- return { intent, common, requiredShareAllowance };
49
- }
5
+ import { prepareVaultWithdraw } from "../withdraw.js";
50
6
  /**
7
+ * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
51
8
  * Prepares a withdrawal plan from caller intent and a vault snapshot. The share quote is fixed on
52
9
  * the withdrawal step at preparation, because the required share allowance decides the approval or
53
10
  * permit prerequisite, so the returned plan is static: only signatures are added before building.
@@ -56,29 +13,26 @@ function resolveWithdrawInputs(params, now) {
56
13
  * The share-allowance quote uses `DEFAULT_SLIPPAGE_TOLERANCE` internally. The withdrawal has no
57
14
  * on-chain share-price or max-shares bound and accepts no slippage tolerance input.
58
15
  * @param client - Viem client for `params.chainId`, used for allowance and nonce reads only.
59
- * @param params.vault - Vault V2 contract whose shares are burned for the withdrawal.
60
- * @param params.vaultData - Caller-fetched `AccrualVaultV2` snapshot of `vault`, used to derive the required share allowance.
16
+ * @param params.vault - Caller-fetched AccrualVaultV2 snapshot used for the quote; the address is vault.address.
61
17
  * @param params.chainId - Chain containing the vault and registered VaultBundlesV1 contract.
62
18
  * @param params.account - Non-zero account approving or permitting shares and sending the withdrawal.
63
- * @param params.amount - Positive gross withdrawal in asset base units, before referral fees.
64
- * @param params.referralFee.bps - Optional integer fee in basis points of the gross amount; defaults to zero and must be below 10,000.
19
+ * @param params.assets - Positive gross withdrawal in asset base units, before referral fees.
20
+ * @param params.referralFee.bps - Optional integer fee in basis points of the gross assets; defaults to zero and must be below 10,000.
65
21
  * @param params.referralFee.recipient - Recipient of the optional fee; must be non-zero when the fee is positive.
66
22
  * @param params.supportSignature - Optional unlimited share-permit preference, defaulting to `false`; otherwise uses unlimited direct share approval when allowance is insufficient.
67
23
  * @param params.now - Reference timestamp in seconds, normally the latest block's `timestamp`; uses the device clock when omitted.
68
24
  * @param params.expiresAt - Optional Unix timestamp in seconds as a safe integer, later than `now`; defaults to `now + 2h` (device clock when `now` is omitted).
69
25
  * @param params.calldataOrigin - Optional non-empty, even-length hex bytes appended to each built transaction's calldata.
70
- * @param params.deployless - Optional shared intent field; unused by withdrawal share-permit reads.
71
26
  * @returns A plan with the withdrawal step and any required share approval or signature request.
72
27
  * @throws {ZeroAccountError} when `account` is the zero address.
73
28
  * @throws {NonIntegerInputError} when `expiresAt` or `referralFee.bps` is not a safe integer.
74
- * @throws {NonPositiveInputError} when `expiresAt`, `amount` or the computed required shares are not positive.
29
+ * @throws {NonPositiveInputError} when `expiresAt`, `assets` or the computed required shares are not positive.
75
30
  * @throws {ExpiredDeadlineError} when `expiresAt` is at or before `now` (device clock when `now` is omitted).
76
31
  * @throws {NegativeInputError} when `referralFee.bps` is negative.
77
- * @throws {InputExceedsMaxError} when `referralFee.bps` exceeds 10,000 or `amount` exceeds uint256.
32
+ * @throws {InputExceedsMaxError} when `referralFee.bps` exceeds 10,000 or `assets` exceeds uint256.
78
33
  * @throws {InvalidCalldataOriginError} when `calldataOrigin` is empty, malformed or has an odd number of hex digits.
79
34
  * @throws {ReferralFeePctExceededError} when `referralFee.bps` equals 10,000; the normalized fee must be below WAD.
80
35
  * @throws {ReferralFeeRecipientMissingError} when a positive referral fee has no recipient or a zero recipient.
81
- * @throws {VaultAddressMismatchError} when `vaultData` describes another vault.
82
36
  * @throws {ChainIdMismatchError} when the client targets another chain.
83
37
  * @throws {MissingContractCodeError} when the VaultBundlesV1 contract is absent.
84
38
  * @see computeVaultMaxShareAllowance for quote errors.
@@ -92,84 +46,19 @@ function resolveWithdrawInputs(params, now) {
92
46
  * const client = createPublicClient({ chain: mainnet, transport: http() });
93
47
  * const vault = "0x04422053aDDbc9bB2759b248B574e3FCA76Bc145";
94
48
  * const plan = await prepareVaultV2Withdraw(client, {
95
- * vault, chainId: 1, account: "0x0000000000000000000000000000000000000001", amount: 1_000_000n, supportSignature: true,
96
- * vaultData: await fetchAccrualVaultV2(vault, client),
49
+ * chainId: 1, account: "0x0000000000000000000000000000000000000001", assets: 1_000_000n, supportSignature: true,
50
+ * vault: await fetchAccrualVaultV2(vault, client),
97
51
  * now: (await client.getBlock()).timestamp,
98
52
  * });
99
53
  * // Sign the pending requests, verify and attach with applySignatures, then build.
100
54
  * ```
101
- * @experimental TransactionPlan is in beta; breaking changes may ship in minor releases.
102
55
  */
103
56
  export async function prepareVaultV2Withdraw(client, params) {
104
- const { vault, chainId, account, amount } = params;
105
- const now = params.now ?? Time.timestamp();
106
- const { intent, common, requiredShareAllowance } = resolveWithdrawInputs(params, now);
107
- const spender = getChainAddress(chainId, "bundles.vaultBundlesV1");
108
- const [, , requirements] = await Promise.all([
109
- validateClientChainId(client, chainId),
110
- assertRegistryTargetsHaveCode(client, chainId, [
111
- { role: "bundles.vaultBundlesV1", address: spender },
112
- ]),
113
- getVaultBundlesSharesRequirements(client, {
114
- vaultData: new Token({ address: vault }),
115
- version: "vaultV2",
116
- owner: account,
117
- chainId,
118
- requiredShareAllowance,
119
- deadline: intent.deadline,
120
- supportSignature: intent.supportSignature,
121
- now,
122
- }),
123
- ]);
124
- const approvals = [];
125
- const signatureRequests = [];
126
- const tokenPermitRequestId = "vaultV2Withdraw:tokenPermit";
127
- const requirement = requirements[0];
128
- if (requirement !== undefined) {
129
- if ("sign" in requirement) {
130
- signatureRequests.push(buildPermitSignatureRequest(requirement, tokenPermitRequestId));
131
- }
132
- else {
133
- approvals.push(buildApprovalStep(requirement, {
134
- id: "vaultV2Withdraw:approval",
135
- chainId,
136
- account,
137
- }));
138
- }
139
- }
140
- return deepFreeze(structuredClone({
141
- version: 1,
142
- account,
143
- chainId,
144
- expiresAt: intent.expiresAt,
145
- ...(intent.calldataOrigin && { calldataOrigin: intent.calldataOrigin }),
146
- steps: [
147
- ...approvals,
148
- {
149
- id: "vaultV2Withdraw",
150
- label: "Withdraw from vault",
151
- action: {
152
- type: "vaultV2Withdraw",
153
- inputs: {
154
- chainId,
155
- account,
156
- vault,
157
- amount,
158
- requiredShareAllowance,
159
- ...common,
160
- },
161
- signatureRequestIds: signatureRequests.length > 0
162
- ? { tokenPermit: tokenPermitRequestId }
163
- : {},
164
- },
165
- },
166
- ],
167
- signatureRequests,
168
- }));
57
+ return prepareVaultWithdraw(client, { ...params, type: "vaultV2Withdraw" });
169
58
  }
170
59
  /** @internal Encodes the withdrawal from its self-contained inputs and resolved signature requests. */
171
60
  export function buildVaultV2Withdraw(action, requests) {
172
- const { requiredShareAllowance, chainId, account, ...args } = action.inputs;
61
+ const { requiredShareAllowance, chainId, account, assets, ...args } = action.inputs;
173
62
  const signature = getVaultPermit(requests, {
174
63
  id: action.signatureRequestIds.tokenPermit,
175
64
  asset: action.inputs.vault,
@@ -182,6 +71,11 @@ export function buildVaultV2Withdraw(action, requests) {
182
71
  });
183
72
  return vaultV2Withdraw({
184
73
  vault: { chainId, address: action.inputs.vault },
185
- args: { ...args, userAddress: account, requirementSignature: permit },
74
+ args: {
75
+ ...args,
76
+ amount: assets,
77
+ userAddress: account,
78
+ requirementSignature: permit,
79
+ },
186
80
  });
187
81
  }
@@ -1,6 +1,7 @@
1
1
  import { UnsupportedTransactionPlanVersionError } from "./error.js";
2
2
  /** @internal Rejects unsupported plan versions before interpreting stored fields. */
3
3
  export function validateTransactionPlanVersion(version) {
4
- if (version !== 1)
4
+ if (version !== 2) {
5
5
  throw new UnsupportedTransactionPlanVersionError(version);
6
+ }
6
7
  }
@@ -0,0 +1,18 @@
1
+ import { type AccrualVault, type AccrualVaultV2 } from "@morpho-org/blue-sdk";
2
+ import type { Client } from "viem";
3
+ import { type ReferenceTimestampParams } from "../types/index.js";
4
+ import { type IntentParams } from "./intent.js";
5
+ import type { TransactionPlan } from "./types.js";
6
+ /** @internal Validates the stored vault withdrawal action's input shape. */
7
+ export declare function validateWithdrawPlanAction(action: Record<string, unknown>, path: string): void;
8
+ /** @internal Snapshot-based asset withdrawal intent shared by V1 and V2. */
9
+ export interface WithdrawPlanParams<TSnapshot extends AccrualVault | AccrualVaultV2> extends Omit<IntentParams, "deployless">, ReferenceTimestampParams {
10
+ /** Caller-fetched accrual snapshot used to quote the required share allowance. */
11
+ readonly vault: TSnapshot;
12
+ /** Gross assets withdrawn, before the referral fee. */
13
+ readonly assets: bigint;
14
+ }
15
+ /** @internal Fixes a withdrawal quote and resolves the vault-share prerequisites. */
16
+ export declare function prepareVaultWithdraw<TSnapshot extends AccrualVault | AccrualVaultV2>(client: Client, params: WithdrawPlanParams<TSnapshot> & {
17
+ readonly type: "vaultV1Withdraw" | "vaultV2Withdraw";
18
+ }): Promise<TransactionPlan>;
@@ -0,0 +1,90 @@
1
+ import { DEFAULT_SLIPPAGE_TOLERANCE, Token, } from "@morpho-org/blue-sdk";
2
+ import { deepFreeze, Time } from "@morpho-org/morpho-ts";
3
+ import { normalizeBundlesCommonParams } from "../actions/bundles/common.js";
4
+ import { computeVaultMaxShareAllowance } from "../helpers/index.js";
5
+ import { validateUint256Field } from "../helpers/validate.js";
6
+ import { NonPositiveInputError, } from "../types/index.js";
7
+ import { resolveIntent } from "./intent.js";
8
+ import { getVaultShareSpendingRequirements } from "./shareSpending.js";
9
+ import { validatePlanFields, validateVaultPlanAction } from "./validation.js";
10
+ /** @internal Validates the stored vault withdrawal action's input shape. */
11
+ export function validateWithdrawPlanAction(action, path) {
12
+ const inputs = validateVaultPlanAction(action, path);
13
+ validatePlanFields(inputs, {
14
+ path: `${path}.inputs`,
15
+ fields: {
16
+ assets: "uint256 bigint",
17
+ requiredShareAllowance: "uint256 bigint",
18
+ },
19
+ });
20
+ }
21
+ function resolveWithdrawInputs(params, now) {
22
+ const { vault, assets } = params;
23
+ const intent = resolveIntent(params, now);
24
+ if (assets <= 0n) {
25
+ throw new NonPositiveInputError("assets", assets);
26
+ }
27
+ // Reject amounts that cannot be encoded as uint256 before making RPC requests.
28
+ validateUint256Field("assets", assets);
29
+ const common = normalizeBundlesCommonParams({
30
+ deadline: intent.deadline,
31
+ referralFeePct: intent.referralFeePct,
32
+ referralFeeRecipient: intent.referralFeeRecipient,
33
+ });
34
+ // The quote depends only on the snapshot and the intent, so it is fixed before any read.
35
+ const requiredShareAllowance = computeVaultMaxShareAllowance({
36
+ vaultData: vault,
37
+ deadline: intent.deadline,
38
+ assets,
39
+ slippageTolerance: DEFAULT_SLIPPAGE_TOLERANCE,
40
+ });
41
+ return { intent, common, requiredShareAllowance };
42
+ }
43
+ /** @internal Fixes a withdrawal quote and resolves the vault-share prerequisites. */
44
+ export async function prepareVaultWithdraw(client, params) {
45
+ const { chainId, account, assets } = params;
46
+ const vault = params.vault.address;
47
+ const now = params.now ?? Time.timestamp();
48
+ const { intent, common, requiredShareAllowance } = resolveWithdrawInputs(params, now);
49
+ const { approvals, signatureRequests, tokenPermitRequestId } = await getVaultShareSpendingRequirements(client, {
50
+ type: params.type,
51
+ shareToken: params.type === "vaultV1Withdraw"
52
+ ? params.vault
53
+ : new Token({ address: vault }),
54
+ chainId,
55
+ account,
56
+ requiredShareAllowance,
57
+ deadline: intent.deadline,
58
+ supportSignature: intent.supportSignature,
59
+ now,
60
+ });
61
+ return deepFreeze(structuredClone({
62
+ version: 2,
63
+ account,
64
+ chainId,
65
+ expiresAt: intent.expiresAt,
66
+ ...(intent.calldataOrigin && { calldataOrigin: intent.calldataOrigin }),
67
+ steps: [
68
+ ...approvals,
69
+ {
70
+ id: params.type,
71
+ label: "Withdraw from vault",
72
+ action: {
73
+ type: params.type,
74
+ inputs: {
75
+ chainId,
76
+ account,
77
+ vault,
78
+ assets,
79
+ requiredShareAllowance,
80
+ ...common,
81
+ },
82
+ signatureRequestIds: signatureRequests.length > 0
83
+ ? { tokenPermit: tokenPermitRequestId }
84
+ : {},
85
+ },
86
+ },
87
+ ],
88
+ signatureRequests,
89
+ }));
90
+ }
@@ -209,28 +209,36 @@ export function isAuthorizationSignature(signature) {
209
209
  * ```
210
210
  */
211
211
  export function selectRequirementSignatures(signatures, accepts) {
212
- if (signatures == null)
212
+ if (signatures == null) {
213
213
  return {};
214
+ }
214
215
  const permits = signatures.filter(isPermitSignature);
215
216
  const permit2Transfers = signatures.filter(isPermit2SignatureTransferSignature);
216
217
  const authorizations = signatures.filter(isAuthorizationSignature);
217
218
  const unsupported = signatures.find((signature) => !isPermitSignature(signature) &&
218
219
  !isPermit2SignatureTransferSignature(signature) &&
219
220
  !isAuthorizationSignature(signature));
220
- if (unsupported != null)
221
+ if (unsupported != null) {
221
222
  throw new UnsupportedRequirementSignatureError(unsupported.action.type);
222
- if (!accepts.permit && permits.length > 0)
223
+ }
224
+ if (!accepts.permit && permits.length > 0) {
223
225
  throw new UnexpectedRequirementSignatureError("permit");
224
- if (!accepts.permit2SignatureTransfer && permit2Transfers.length > 0)
226
+ }
227
+ if (!accepts.permit2SignatureTransfer && permit2Transfers.length > 0) {
225
228
  throw new UnexpectedRequirementSignatureError("permit2SignatureTransfer");
226
- if (!accepts.authorization && authorizations.length > 0)
229
+ }
230
+ if (!accepts.authorization && authorizations.length > 0) {
227
231
  throw new UnexpectedRequirementSignatureError("authorization");
228
- if (permits.length > 1)
232
+ }
233
+ if (permits.length > 1) {
229
234
  throw new AmbiguousRequirementSignaturesError("permit", permits.length);
230
- if (permit2Transfers.length > 1)
235
+ }
236
+ if (permit2Transfers.length > 1) {
231
237
  throw new AmbiguousRequirementSignaturesError("permit2SignatureTransfer", permit2Transfers.length);
232
- if (authorizations.length > 1)
238
+ }
239
+ if (authorizations.length > 1) {
233
240
  throw new AmbiguousRequirementSignaturesError("authorization", authorizations.length);
241
+ }
234
242
  return {
235
243
  permit: permits[0],
236
244
  permit2SignatureTransfer: permit2Transfers[0],
@@ -711,7 +711,10 @@ export declare class AddressMismatchError extends Error {
711
711
  export declare class ChainIdMismatchError extends Error {
712
712
  constructor(clientChainId: number | undefined, argsChainId: number);
713
713
  }
714
- /** Thrown when a runtime crypto API is required but unavailable. */
714
+ /**
715
+ * Thrown when a runtime crypto API is required but unavailable.
716
+ * @deprecated This error is never thrown by the SDK and will be removed in the next major version.
717
+ */
715
718
  export declare class CryptoUnavailableError extends Error {
716
719
  constructor(feature: string);
717
720
  }
@@ -775,6 +778,10 @@ export declare class ChainWNativeMissingError extends Error {
775
778
  }
776
779
  /** Thrown when a vault entity's address does not match the vault address embedded in the call's args. */
777
780
  export declare class VaultAddressMismatchError extends Error {
781
+ /** Vault targeted by the caller. */
782
+ readonly expected: Address;
783
+ /** Vault address found in the supplied snapshot. */
784
+ readonly actual: Address;
778
785
  constructor(vaultAddress: Address, argsVaultAddress: Address);
779
786
  }
780
787
  /** Thrown when an action uses `nativeAmount` but the target asset is not the chain's wNative. */
@@ -851,7 +851,10 @@ export class ChainIdMismatchError extends Error {
851
851
  this.name = "ChainIdMismatchError";
852
852
  }
853
853
  }
854
- /** Thrown when a runtime crypto API is required but unavailable. */
854
+ /**
855
+ * Thrown when a runtime crypto API is required but unavailable.
856
+ * @deprecated This error is never thrown by the SDK and will be removed in the next major version.
857
+ */
855
858
  export class CryptoUnavailableError extends Error {
856
859
  constructor(feature) {
857
860
  super(`Crypto API is required for ${feature} but is unavailable.`);
@@ -949,9 +952,15 @@ export class ChainWNativeMissingError extends Error {
949
952
  }
950
953
  /** Thrown when a vault entity's address does not match the vault address embedded in the call's args. */
951
954
  export class VaultAddressMismatchError extends Error {
955
+ /** Vault targeted by the caller. */
956
+ expected;
957
+ /** Vault address found in the supplied snapshot. */
958
+ actual;
952
959
  constructor(vaultAddress, argsVaultAddress) {
953
960
  super(`Vault address mismatch between vault: ${vaultAddress} and args: ${argsVaultAddress}`);
954
961
  this.name = "VaultAddressMismatchError";
962
+ this.expected = vaultAddress;
963
+ this.actual = argsVaultAddress;
955
964
  }
956
965
  }
957
966
  /** Thrown when an action uses `nativeAmount` but the target asset is not the chain's wNative. */
@@ -3,7 +3,7 @@ export { AdaptiveCurveIrmLib as BlueAdaptiveCurveIrmLib, CapacityLimitReason,
3
3
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
4
4
  defaultPreLiquidationParamsRegistry as blueDefaultPreLiquidationParamsRegistry,
5
5
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
6
- getDefaultPreLiquidationParams as getBlueDefaultPreLiquidationParams, MarketUtils as BlueMarketUtils, MathLib, SharesMath as BlueSharesMath, VaultUtils, VaultV2BlueMarketPublicAllocatorConfigUtils, VaultV2BluePublicAllocatorConfigUtils, VaultV2MidnightAdapterUtils, VaultV2Utils, } from "@morpho-org/blue-sdk";
6
+ getDefaultPreLiquidationParams as getBlueDefaultPreLiquidationParams, MarketUtils as BlueMarketUtils, MathLib, SharesMath as BlueSharesMath, VaultUtils, VaultV2BlueMarketPublicAllocatorConfigUtils, VaultV2BluePublicAllocatorConfigUtils, VaultV2Utils, } from "@morpho-org/blue-sdk";
7
7
  export { catchRevertOnly, decodeBytes32String, getAuthorizationTypedData as getBlueAuthorizationTypedData, getPermit2PermitTypedData, getPermit2TransferFromTypedData, getPermitTypedData, isContractRevertError, isTransportError, isUndecodableResultError, MetaMorphoAction as BlueMetaMorphoAction, optionalBoolean, readContractRestructured, restructure, safeGetAddress, safeParseNumber, safeParseUnits, } from "@morpho-org/blue-sdk-viem";
8
8
  export { EcrecoverRatifier as MidnightEcrecoverRatifier,
9
9
  /** @deprecated Use MidnightEcrecoverRatifier with a tagged tree. */
@@ -17,7 +17,7 @@ RatifierUtils as MidnightRatifierUtils, SetterRatifier as MidnightSetterRatifier
17
17
  /** @deprecated Use MidnightSetterRatifier with a tagged tree. */
18
18
  SetterRatifierUtils as MidnightSetterRatifierUtils, TakeAmountsLib as MidnightTakeAmountsLib, TickLib as MidnightTickLib, TreeUtils as MidnightTreeUtils, } from "@morpho-org/midnight-sdk";
19
19
  export type { ArrayElementType, DeepPartial, DottedKeys, FieldType, PartialDottedKeys, } from "@morpho-org/morpho-ts";
20
- export { bigIntComparator, createGetValue, createHasValue, deepFreeze, entries, filterDefined, fromEntries, getLast, getLastDefined, getSubdomainBaseUrl, getValue, hasValue, isDefined, isNotNull, isNotUndefined, keys, MidnightPositionLib, mergeEntries, retryPromiseLinearBackoff, Time, transformValue, values, } from "@morpho-org/morpho-ts";
20
+ export { bigIntComparator, createGetValue, createHasValue, deepFreeze, entries, filterDefined, fromEntries, getLast, getLastDefined, getSubdomainBaseUrl, getValue, hasValue, isDefined, isNotNull, isNotUndefined, keys, mergeEntries, retryPromiseLinearBackoff, Time, transformValue, values, } from "@morpho-org/morpho-ts";
21
21
  export { validateClientChainId } from "./actions/requirements/validateClientChainId.js";
22
22
  export { addTransactionMetadata } from "./helpers/metadata.js";
23
23
  export { type PreviewVaultV2ForceWithdrawParams, previewVaultV2ForceWithdraw, type VaultV2ForceWithdrawPreview, } from "./helpers/previewVaultV2ForceWithdraw.js";
package/lib/esm/utils.js CHANGED
@@ -2,7 +2,7 @@ export { AdaptiveCurveIrmLib as BlueAdaptiveCurveIrmLib, CapacityLimitReason,
2
2
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
3
3
  defaultPreLiquidationParamsRegistry as blueDefaultPreLiquidationParamsRegistry,
4
4
  /** @deprecated Pre-liquidation support is deprecated and will be removed in the next major. */
5
- getDefaultPreLiquidationParams as getBlueDefaultPreLiquidationParams, MarketUtils as BlueMarketUtils, MathLib, SharesMath as BlueSharesMath, VaultUtils, VaultV2BlueMarketPublicAllocatorConfigUtils, VaultV2BluePublicAllocatorConfigUtils, VaultV2MidnightAdapterUtils, VaultV2Utils, } from "@morpho-org/blue-sdk";
5
+ getDefaultPreLiquidationParams as getBlueDefaultPreLiquidationParams, MarketUtils as BlueMarketUtils, MathLib, SharesMath as BlueSharesMath, VaultUtils, VaultV2BlueMarketPublicAllocatorConfigUtils, VaultV2BluePublicAllocatorConfigUtils, VaultV2Utils, } from "@morpho-org/blue-sdk";
6
6
  export { catchRevertOnly, decodeBytes32String, getAuthorizationTypedData as getBlueAuthorizationTypedData, getPermit2PermitTypedData, getPermit2TransferFromTypedData, getPermitTypedData, isContractRevertError, isTransportError, isUndecodableResultError, MetaMorphoAction as BlueMetaMorphoAction, optionalBoolean, readContractRestructured, restructure, safeGetAddress, safeParseNumber, safeParseUnits, } from "@morpho-org/blue-sdk-viem";
7
7
  export { EcrecoverRatifier as MidnightEcrecoverRatifier,
8
8
  /** @deprecated Use MidnightEcrecoverRatifier with a tagged tree. */
@@ -15,7 +15,7 @@ RateRatifierV1Utils as MidnightRateRatifierV1Utils, Ratifier as MidnightRatifier
15
15
  RatifierUtils as MidnightRatifierUtils, SetterRatifier as MidnightSetterRatifier,
16
16
  /** @deprecated Use MidnightSetterRatifier with a tagged tree. */
17
17
  SetterRatifierUtils as MidnightSetterRatifierUtils, TakeAmountsLib as MidnightTakeAmountsLib, TickLib as MidnightTickLib, TreeUtils as MidnightTreeUtils, } from "@morpho-org/midnight-sdk";
18
- export { bigIntComparator, createGetValue, createHasValue, deepFreeze, entries, filterDefined, fromEntries, getLast, getLastDefined, getSubdomainBaseUrl, getValue, hasValue, isDefined, isNotNull, isNotUndefined, keys, MidnightPositionLib, mergeEntries, retryPromiseLinearBackoff, Time, transformValue, values, } from "@morpho-org/morpho-ts";
18
+ export { bigIntComparator, createGetValue, createHasValue, deepFreeze, entries, filterDefined, fromEntries, getLast, getLastDefined, getSubdomainBaseUrl, getValue, hasValue, isDefined, isNotNull, isNotUndefined, keys, mergeEntries, retryPromiseLinearBackoff, Time, transformValue, values, } from "@morpho-org/morpho-ts";
19
19
  export { validateClientChainId } from "./actions/requirements/validateClientChainId.js";
20
20
  export { addTransactionMetadata } from "./helpers/metadata.js";
21
21
  export { previewVaultV2ForceWithdraw, } from "./helpers/previewVaultV2ForceWithdraw.js";
@@ -1,2 +1,2 @@
1
1
  /** Installed @morpho-org/morpho-sdk package version. */
2
- export declare const version = "10.0.0-next.0";
2
+ export declare const version = "10.0.0";
@@ -1,3 +1,3 @@
1
1
  // Generated by scripts/generate-package-versions.ts. Do not edit.
2
2
  /** Installed @morpho-org/morpho-sdk package version. */
3
- export const version = "10.0.0-next.0";
3
+ export const version = "10.0.0";