@morpho-org/morpho-sdk 9.8.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 (303) hide show
  1. package/MIGRATION-v9-to-v10.md +114 -0
  2. package/README.md +109 -13
  3. package/lib/cjs/actions/blue/common.js +4 -2
  4. package/lib/cjs/actions/blue/withdrawBuyCallback.js +2 -1
  5. package/lib/cjs/actions/bundles/common.js +20 -14
  6. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +3 -4
  7. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +3 -2
  8. package/lib/cjs/actions/midnight/cancelAndMake.d.ts +2 -1
  9. package/lib/cjs/actions/midnight/cancelAndMake.js +2 -1
  10. package/lib/cjs/actions/midnight/cancelOffers.d.ts +54 -0
  11. package/lib/cjs/actions/midnight/cancelOffers.js +108 -0
  12. package/lib/cjs/actions/midnight/index.d.ts +1 -0
  13. package/lib/cjs/actions/midnight/index.js +1 -0
  14. package/lib/cjs/actions/midnight/supplyCollateralBatch.js +4 -2
  15. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +1 -1
  16. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +3 -2
  17. package/lib/cjs/actions/midnight/takeBorrow.d.ts +1 -1
  18. package/lib/cjs/actions/midnight/takeBorrow.js +3 -2
  19. package/lib/cjs/actions/midnight/takeLend.d.ts +1 -1
  20. package/lib/cjs/actions/midnight/takeLend.js +3 -2
  21. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.d.ts +1 -1
  22. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +3 -2
  23. package/lib/cjs/actions/midnight/takeWithdraw.d.ts +1 -1
  24. package/lib/cjs/actions/midnight/takeWithdraw.js +3 -2
  25. package/lib/cjs/actions/midnight/withdrawCollateralBatch.js +4 -2
  26. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +3 -4
  27. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +1 -1
  28. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +4 -6
  29. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +3 -3
  30. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +4 -6
  31. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +3 -3
  32. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +5 -6
  33. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +5 -13
  34. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +3 -5
  35. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +26 -23
  36. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.js +2 -1
  37. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +2 -1
  38. package/lib/cjs/actions/vaultV1/inKindRedeem.js +6 -3
  39. package/lib/cjs/actions/vaultV1/migrateToV2.js +1 -1
  40. package/lib/cjs/actions/vaultV2/forceWithdraw.js +4 -2
  41. package/lib/cjs/actions/vaultV2/inKindRedeem.js +4 -2
  42. package/lib/cjs/entities/blue/blue.d.ts +34 -28
  43. package/lib/cjs/entities/blue/blue.js +107 -77
  44. package/lib/cjs/entities/midnight/deadline.d.ts +2 -2
  45. package/lib/cjs/entities/midnight/deadline.js +4 -3
  46. package/lib/cjs/entities/midnight/midnight.d.ts +49 -51
  47. package/lib/cjs/entities/midnight/midnight.js +158 -138
  48. package/lib/cjs/entities/midnight/publication.d.ts +2 -0
  49. package/lib/cjs/entities/midnight/publication.js +193 -138
  50. package/lib/cjs/entities/midnight/types.d.ts +4 -4
  51. package/lib/cjs/entities/requirements/assertContractCode.js +4 -2
  52. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +3 -5
  53. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +24 -28
  54. package/lib/cjs/entities/requirements/getErc2612Permit.js +10 -5
  55. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -4
  56. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +8 -2
  57. package/lib/cjs/entities/requirements/getVaultV1InKindRedeemRequirements.d.ts +21 -0
  58. package/lib/cjs/entities/requirements/getVaultV1InKindRedeemRequirements.js +111 -0
  59. package/lib/cjs/entities/requirements/getVaultV2ForceWithdrawRequirements.d.ts +20 -0
  60. package/lib/cjs/entities/requirements/getVaultV2ForceWithdrawRequirements.js +76 -0
  61. package/lib/cjs/entities/requirements/getVaultV2InKindRedeemRequirements.d.ts +21 -0
  62. package/lib/cjs/entities/requirements/getVaultV2InKindRedeemRequirements.js +93 -0
  63. package/lib/cjs/entities/vaultV1/inKindRedeemQuote.d.ts +25 -0
  64. package/lib/cjs/entities/vaultV1/inKindRedeemQuote.js +85 -0
  65. package/lib/cjs/entities/vaultV1/migrateToV2Quote.d.ts +20 -0
  66. package/lib/cjs/entities/vaultV1/migrateToV2Quote.js +37 -0
  67. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +12 -12
  68. package/lib/cjs/entities/vaultV1/vaultV1.js +90 -194
  69. package/lib/cjs/entities/vaultV2/forceWithdrawQuote.d.ts +25 -0
  70. package/lib/cjs/entities/vaultV2/forceWithdrawQuote.js +189 -0
  71. package/lib/cjs/entities/vaultV2/inKindRedeemQuote.d.ts +28 -0
  72. package/lib/cjs/entities/vaultV2/inKindRedeemQuote.js +110 -0
  73. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +16 -16
  74. package/lib/cjs/entities/vaultV2/vaultV2.js +94 -385
  75. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +14 -0
  76. package/lib/cjs/entities/vaultV2BlueReallocationData.js +1599 -883
  77. package/lib/cjs/errors.d.ts +1 -1
  78. package/lib/cjs/errors.js +2 -1
  79. package/lib/cjs/helpers/deadShares.js +83 -47
  80. package/lib/cjs/helpers/encodeMidnightCalldata.js +4 -2
  81. package/lib/cjs/helpers/metadata.d.ts +14 -2
  82. package/lib/cjs/helpers/metadata.js +49 -8
  83. package/lib/cjs/helpers/previewVaultV2ForceWithdraw.js +44 -27
  84. package/lib/cjs/helpers/previewVaultV2InKindRedeem.js +11 -2
  85. package/lib/cjs/helpers/utilization.js +10 -5
  86. package/lib/cjs/helpers/validate.js +88 -67
  87. package/lib/cjs/helpers/validateMidnightBounds.js +2 -1
  88. package/lib/cjs/helpers/validateMidnightCollateralBatch.js +2 -1
  89. package/lib/cjs/helpers/validateMidnightPublication.js +4 -2
  90. package/lib/cjs/helpers/validateMidnightTakeFeasibility.js +177 -104
  91. package/lib/cjs/helpers/validatePositiveInput.js +2 -1
  92. package/lib/cjs/helpers/validateTakeableOffers.js +2 -1
  93. package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.js +40 -22
  94. package/lib/cjs/index.d.ts +2 -2
  95. package/lib/cjs/index.js +10 -1
  96. package/lib/cjs/transactionPlan/approval.js +12 -1
  97. package/lib/cjs/transactionPlan/deposit.d.ts +22 -0
  98. package/lib/cjs/transactionPlan/deposit.js +139 -0
  99. package/lib/cjs/transactionPlan/error.d.ts +10 -8
  100. package/lib/cjs/transactionPlan/error.js +10 -8
  101. package/lib/cjs/transactionPlan/getTokenPermit.js +2 -1
  102. package/lib/cjs/transactionPlan/index.d.ts +18 -0
  103. package/lib/cjs/transactionPlan/index.js +23 -5
  104. package/lib/cjs/transactionPlan/intent.d.ts +3 -3
  105. package/lib/cjs/transactionPlan/intent.js +14 -7
  106. package/lib/cjs/transactionPlan/permit.d.ts +1 -1
  107. package/lib/cjs/transactionPlan/permit.js +86 -20
  108. package/lib/cjs/transactionPlan/plan.d.ts +16 -6
  109. package/lib/cjs/transactionPlan/plan.js +77 -24
  110. package/lib/cjs/transactionPlan/redeem.d.ts +18 -0
  111. package/lib/cjs/transactionPlan/redeem.js +63 -0
  112. package/lib/cjs/transactionPlan/requirements.d.ts +4 -3
  113. package/lib/cjs/transactionPlan/requirements.js +12 -6
  114. package/lib/cjs/transactionPlan/serde.d.ts +2 -2
  115. package/lib/cjs/transactionPlan/serde.js +45 -19
  116. package/lib/cjs/transactionPlan/shareSpending.d.ts +18 -0
  117. package/lib/cjs/transactionPlan/shareSpending.js +50 -0
  118. package/lib/cjs/transactionPlan/types.d.ts +117 -11
  119. package/lib/cjs/transactionPlan/validateActions.js +11 -1
  120. package/lib/cjs/transactionPlan/validation.js +6 -3
  121. package/lib/cjs/transactionPlan/vaultv1/deposit.d.ts +93 -0
  122. package/lib/cjs/transactionPlan/vaultv1/deposit.js +94 -0
  123. package/lib/cjs/transactionPlan/vaultv1/inKindRedeem.d.ts +96 -0
  124. package/lib/cjs/transactionPlan/vaultv1/inKindRedeem.js +203 -0
  125. package/lib/cjs/transactionPlan/vaultv1/migrateToV2.d.ts +94 -0
  126. package/lib/cjs/transactionPlan/vaultv1/migrateToV2.js +217 -0
  127. package/lib/cjs/transactionPlan/vaultv1/redeem.d.ts +69 -0
  128. package/lib/cjs/transactionPlan/vaultv1/redeem.js +78 -0
  129. package/lib/cjs/transactionPlan/vaultv1/withdraw.d.ts +77 -0
  130. package/lib/cjs/transactionPlan/vaultv1/withdraw.js +88 -0
  131. package/lib/cjs/transactionPlan/vaultv2/deposit.d.ts +18 -33
  132. package/lib/cjs/transactionPlan/vaultv2/deposit.js +19 -143
  133. package/lib/cjs/transactionPlan/vaultv2/forceRedeem.d.ts +74 -0
  134. package/lib/cjs/transactionPlan/vaultv2/forceRedeem.js +155 -0
  135. package/lib/cjs/transactionPlan/vaultv2/forceWithdraw.d.ts +98 -0
  136. package/lib/cjs/transactionPlan/vaultv2/forceWithdraw.js +206 -0
  137. package/lib/cjs/transactionPlan/vaultv2/inKindRedeem.d.ts +113 -0
  138. package/lib/cjs/transactionPlan/vaultv2/inKindRedeem.js +206 -0
  139. package/lib/cjs/transactionPlan/vaultv2/redeem.d.ts +84 -0
  140. package/lib/cjs/transactionPlan/vaultv2/redeem.js +86 -0
  141. package/lib/cjs/transactionPlan/vaultv2/withdraw.d.ts +18 -29
  142. package/lib/cjs/transactionPlan/vaultv2/withdraw.js +21 -126
  143. package/lib/cjs/transactionPlan/version.js +2 -1
  144. package/lib/cjs/transactionPlan/withdraw.d.ts +18 -0
  145. package/lib/cjs/transactionPlan/withdraw.js +94 -0
  146. package/lib/cjs/types/action.d.ts +26 -2
  147. package/lib/cjs/types/action.js +17 -9
  148. package/lib/cjs/types/error.d.ts +8 -1
  149. package/lib/cjs/types/error.js +21 -6
  150. package/lib/cjs/types/metadata.d.ts +12 -2
  151. package/lib/cjs/version.d.ts +1 -1
  152. package/lib/cjs/version.js +1 -1
  153. package/lib/esm/actions/blue/common.js +4 -2
  154. package/lib/esm/actions/blue/withdrawBuyCallback.js +2 -1
  155. package/lib/esm/actions/bundles/common.js +20 -14
  156. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +3 -4
  157. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +3 -2
  158. package/lib/esm/actions/midnight/cancelAndMake.d.ts +2 -1
  159. package/lib/esm/actions/midnight/cancelAndMake.js +2 -1
  160. package/lib/esm/actions/midnight/cancelOffers.d.ts +54 -0
  161. package/lib/esm/actions/midnight/cancelOffers.js +104 -0
  162. package/lib/esm/actions/midnight/index.d.ts +1 -0
  163. package/lib/esm/actions/midnight/index.js +1 -0
  164. package/lib/esm/actions/midnight/supplyCollateralBatch.js +4 -2
  165. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +1 -1
  166. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +3 -2
  167. package/lib/esm/actions/midnight/takeBorrow.d.ts +1 -1
  168. package/lib/esm/actions/midnight/takeBorrow.js +3 -2
  169. package/lib/esm/actions/midnight/takeLend.d.ts +1 -1
  170. package/lib/esm/actions/midnight/takeLend.js +3 -2
  171. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.d.ts +1 -1
  172. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +3 -2
  173. package/lib/esm/actions/midnight/takeWithdraw.d.ts +1 -1
  174. package/lib/esm/actions/midnight/takeWithdraw.js +3 -2
  175. package/lib/esm/actions/midnight/withdrawCollateralBatch.js +4 -2
  176. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +3 -4
  177. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +1 -1
  178. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +4 -6
  179. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +3 -3
  180. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +4 -6
  181. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +3 -3
  182. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +5 -6
  183. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +7 -15
  184. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +3 -5
  185. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +26 -23
  186. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.js +2 -1
  187. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +2 -1
  188. package/lib/esm/actions/vaultV1/inKindRedeem.js +6 -3
  189. package/lib/esm/actions/vaultV1/migrateToV2.js +1 -1
  190. package/lib/esm/actions/vaultV2/forceWithdraw.js +4 -2
  191. package/lib/esm/actions/vaultV2/inKindRedeem.js +4 -2
  192. package/lib/esm/entities/blue/blue.d.ts +34 -28
  193. package/lib/esm/entities/blue/blue.js +107 -77
  194. package/lib/esm/entities/midnight/deadline.d.ts +2 -2
  195. package/lib/esm/entities/midnight/deadline.js +4 -3
  196. package/lib/esm/entities/midnight/midnight.d.ts +49 -51
  197. package/lib/esm/entities/midnight/midnight.js +159 -139
  198. package/lib/esm/entities/midnight/publication.d.ts +2 -0
  199. package/lib/esm/entities/midnight/publication.js +193 -138
  200. package/lib/esm/entities/midnight/types.d.ts +4 -4
  201. package/lib/esm/entities/requirements/assertContractCode.js +4 -2
  202. package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +3 -5
  203. package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +25 -29
  204. package/lib/esm/entities/requirements/getErc2612Permit.js +10 -5
  205. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -4
  206. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +8 -2
  207. package/lib/esm/entities/requirements/getVaultV1InKindRedeemRequirements.d.ts +21 -0
  208. package/lib/esm/entities/requirements/getVaultV1InKindRedeemRequirements.js +108 -0
  209. package/lib/esm/entities/requirements/getVaultV2ForceWithdrawRequirements.d.ts +20 -0
  210. package/lib/esm/entities/requirements/getVaultV2ForceWithdrawRequirements.js +73 -0
  211. package/lib/esm/entities/requirements/getVaultV2InKindRedeemRequirements.d.ts +21 -0
  212. package/lib/esm/entities/requirements/getVaultV2InKindRedeemRequirements.js +90 -0
  213. package/lib/esm/entities/vaultV1/inKindRedeemQuote.d.ts +25 -0
  214. package/lib/esm/entities/vaultV1/inKindRedeemQuote.js +82 -0
  215. package/lib/esm/entities/vaultV1/migrateToV2Quote.d.ts +20 -0
  216. package/lib/esm/entities/vaultV1/migrateToV2Quote.js +34 -0
  217. package/lib/esm/entities/vaultV1/vaultV1.d.ts +12 -12
  218. package/lib/esm/entities/vaultV1/vaultV1.js +96 -200
  219. package/lib/esm/entities/vaultV2/forceWithdrawQuote.d.ts +25 -0
  220. package/lib/esm/entities/vaultV2/forceWithdrawQuote.js +186 -0
  221. package/lib/esm/entities/vaultV2/inKindRedeemQuote.d.ts +28 -0
  222. package/lib/esm/entities/vaultV2/inKindRedeemQuote.js +107 -0
  223. package/lib/esm/entities/vaultV2/vaultV2.d.ts +16 -16
  224. package/lib/esm/entities/vaultV2/vaultV2.js +102 -393
  225. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +14 -0
  226. package/lib/esm/entities/vaultV2BlueReallocationData.js +1599 -883
  227. package/lib/esm/errors.d.ts +1 -1
  228. package/lib/esm/errors.js +1 -1
  229. package/lib/esm/helpers/deadShares.js +83 -47
  230. package/lib/esm/helpers/encodeMidnightCalldata.js +4 -2
  231. package/lib/esm/helpers/metadata.d.ts +14 -2
  232. package/lib/esm/helpers/metadata.js +48 -8
  233. package/lib/esm/helpers/previewVaultV2ForceWithdraw.js +44 -27
  234. package/lib/esm/helpers/previewVaultV2InKindRedeem.js +11 -2
  235. package/lib/esm/helpers/utilization.js +10 -5
  236. package/lib/esm/helpers/validate.js +88 -67
  237. package/lib/esm/helpers/validateMidnightBounds.js +2 -1
  238. package/lib/esm/helpers/validateMidnightCollateralBatch.js +2 -1
  239. package/lib/esm/helpers/validateMidnightPublication.js +4 -2
  240. package/lib/esm/helpers/validateMidnightTakeFeasibility.js +176 -103
  241. package/lib/esm/helpers/validatePositiveInput.js +2 -1
  242. package/lib/esm/helpers/validateTakeableOffers.js +2 -1
  243. package/lib/esm/helpers/vaultV2ForceWithdrawPlan.js +40 -22
  244. package/lib/esm/index.d.ts +2 -2
  245. package/lib/esm/index.js +1 -1
  246. package/lib/esm/transactionPlan/approval.js +13 -2
  247. package/lib/esm/transactionPlan/deposit.d.ts +22 -0
  248. package/lib/esm/transactionPlan/deposit.js +135 -0
  249. package/lib/esm/transactionPlan/error.d.ts +10 -8
  250. package/lib/esm/transactionPlan/error.js +10 -8
  251. package/lib/esm/transactionPlan/getTokenPermit.js +2 -1
  252. package/lib/esm/transactionPlan/index.d.ts +18 -0
  253. package/lib/esm/transactionPlan/index.js +9 -0
  254. package/lib/esm/transactionPlan/intent.d.ts +3 -3
  255. package/lib/esm/transactionPlan/intent.js +14 -7
  256. package/lib/esm/transactionPlan/permit.d.ts +1 -1
  257. package/lib/esm/transactionPlan/permit.js +86 -20
  258. package/lib/esm/transactionPlan/plan.d.ts +16 -6
  259. package/lib/esm/transactionPlan/plan.js +73 -20
  260. package/lib/esm/transactionPlan/redeem.d.ts +18 -0
  261. package/lib/esm/transactionPlan/redeem.js +59 -0
  262. package/lib/esm/transactionPlan/requirements.d.ts +4 -3
  263. package/lib/esm/transactionPlan/requirements.js +12 -6
  264. package/lib/esm/transactionPlan/serde.d.ts +2 -2
  265. package/lib/esm/transactionPlan/serde.js +45 -19
  266. package/lib/esm/transactionPlan/shareSpending.d.ts +18 -0
  267. package/lib/esm/transactionPlan/shareSpending.js +47 -0
  268. package/lib/esm/transactionPlan/types.d.ts +117 -11
  269. package/lib/esm/transactionPlan/validateActions.js +11 -1
  270. package/lib/esm/transactionPlan/validation.js +6 -3
  271. package/lib/esm/transactionPlan/vaultv1/deposit.d.ts +93 -0
  272. package/lib/esm/transactionPlan/vaultv1/deposit.js +90 -0
  273. package/lib/esm/transactionPlan/vaultv1/inKindRedeem.d.ts +96 -0
  274. package/lib/esm/transactionPlan/vaultv1/inKindRedeem.js +198 -0
  275. package/lib/esm/transactionPlan/vaultv1/migrateToV2.d.ts +94 -0
  276. package/lib/esm/transactionPlan/vaultv1/migrateToV2.js +212 -0
  277. package/lib/esm/transactionPlan/vaultv1/redeem.d.ts +69 -0
  278. package/lib/esm/transactionPlan/vaultv1/redeem.js +74 -0
  279. package/lib/esm/transactionPlan/vaultv1/withdraw.d.ts +77 -0
  280. package/lib/esm/transactionPlan/vaultv1/withdraw.js +84 -0
  281. package/lib/esm/transactionPlan/vaultv2/deposit.d.ts +18 -33
  282. package/lib/esm/transactionPlan/vaultv2/deposit.js +21 -144
  283. package/lib/esm/transactionPlan/vaultv2/forceRedeem.d.ts +74 -0
  284. package/lib/esm/transactionPlan/vaultv2/forceRedeem.js +150 -0
  285. package/lib/esm/transactionPlan/vaultv2/forceWithdraw.d.ts +98 -0
  286. package/lib/esm/transactionPlan/vaultv2/forceWithdraw.js +201 -0
  287. package/lib/esm/transactionPlan/vaultv2/inKindRedeem.d.ts +113 -0
  288. package/lib/esm/transactionPlan/vaultv2/inKindRedeem.js +201 -0
  289. package/lib/esm/transactionPlan/vaultv2/redeem.d.ts +84 -0
  290. package/lib/esm/transactionPlan/vaultv2/redeem.js +82 -0
  291. package/lib/esm/transactionPlan/vaultv2/withdraw.d.ts +18 -29
  292. package/lib/esm/transactionPlan/vaultv2/withdraw.js +23 -127
  293. package/lib/esm/transactionPlan/version.js +2 -1
  294. package/lib/esm/transactionPlan/withdraw.d.ts +18 -0
  295. package/lib/esm/transactionPlan/withdraw.js +90 -0
  296. package/lib/esm/types/action.d.ts +26 -2
  297. package/lib/esm/types/action.js +17 -9
  298. package/lib/esm/types/error.d.ts +8 -1
  299. package/lib/esm/types/error.js +21 -6
  300. package/lib/esm/types/metadata.d.ts +12 -2
  301. package/lib/esm/version.d.ts +1 -1
  302. package/lib/esm/version.js +1 -1
  303. package/package.json +15 -14
@@ -1,20 +1,21 @@
1
- import { AccrualVaultV2MorphoMarketV1AdapterV2, DEFAULT_SLIPPAGE_TOLERANCE, MarketParams, MarketUtils, MathLib, } from "@morpho-org/blue-sdk";
2
- import { erc2612Abi, fetchAccrualVaultV2 } from "@morpho-org/blue-sdk-viem";
3
- import { _try, getChainAddress, Time, } from "@morpho-org/morpho-ts";
4
- import { erc20Abi, isAddressEqual, maxUint256 } from "viem";
5
- import { readContract } from "viem/actions";
1
+ import { DEFAULT_SLIPPAGE_TOLERANCE, MarketParams, } from "@morpho-org/blue-sdk";
2
+ import { fetchAccrualVaultV2 } from "@morpho-org/blue-sdk-viem";
3
+ import { getChainAddress, Time, } from "@morpho-org/morpho-ts";
4
+ import { isAddressEqual } from "viem";
6
5
  import { getBundlesReferralFeeAssets, normalizeBundlesCommonParams, resolveBundlesFunding, selectBundlesSharesPermitSignature, selectBundlesTokenRequirementSignature, } from "../../actions/bundles/common.js";
7
- import { encodeErc20Approval, encodeVaultSharesPermit, vaultV2Deposit, vaultV2ForceRedeem, vaultV2ForceWithdraw, vaultV2InKindRedeem, vaultV2Redeem, vaultV2Withdraw, } from "../../actions/index.js";
6
+ import { vaultV2Deposit, vaultV2ForceRedeem, vaultV2ForceWithdraw, vaultV2InKindRedeem, vaultV2Redeem, vaultV2Withdraw, } from "../../actions/index.js";
8
7
  import { validateClientChainId } from "../../actions/requirements/validateClientChainId.js";
9
- import { computeMinForceWithdrawSharePrice, computeVaultMaxShareAllowance, computeVaultMaxSharePrice, computeVaultV2ForceWithdrawFeeSharesMinted, computeVaultV2ForceWithdrawMinSharesBurnt, computeVaultV2ForceWithdrawPlan, computeVaultV2ForceWithdrawSharesBurnt, MAX_SLIPPAGE_TOLERANCE, resolveVaultV2ForceWithdrawEligibility, validateChainId, validateSlippageTolerance, } from "../../helpers/index.js";
10
- import { validateDeadline, validateNativeVaultAsset, validateReferralFee, validateUint256Field, } from "../../helpers/validate.js";
11
- import { AdapterNotPartOfVaultError, EmptyMarketParamsListError, ExpiredDeadlineError, InKindRedeemCoverageError, InKindRedeemZeroDeallocationError, InputExceedsMaxError, InsufficientBlueBalanceForInKindRedeemError, NonPositiveInputError, VaultAddressMismatchError, VaultV2ForceWithdrawCoverageError, VaultV2ForceWithdrawFeeSharesExceedBurnError, VaultV2ForceWithdrawSharePriceBelowFloorError, VaultV2ForceWithdrawZeroSharePriceError, VaultV2ForceWithdrawZeroWithdrawalError, VaultV2SingleAdapterRequiredError, VaultV2UndecodableLiquidityDataError, VaultV2UnsupportedExitAdapterError, VaultV2UnsupportedLiquidityAdapterError, } from "../../types/index.js";
8
+ import { computeVaultMaxShareAllowance, computeVaultMaxSharePrice, validateChainId, validateSlippageTolerance, } from "../../helpers/index.js";
9
+ import { resolveTransactionMetadata } from "../../helpers/metadata.js";
10
+ import { validateNativeVaultAsset, validateUint256Field, } from "../../helpers/validate.js";
11
+ import { ExpiredDeadlineError, NonPositiveInputError, VaultAddressMismatchError, } from "../../types/index.js";
12
12
  import { assertRegistryTargetsHaveCode } from "../requirements/assertContractCode.js";
13
13
  import { getVaultBundlesSharesRequirements } from "../requirements/getVaultBundlesSharesRequirements.js";
14
+ import { getVaultV2ForceWithdrawRequirements } from "../requirements/getVaultV2ForceWithdrawRequirements.js";
15
+ import { getVaultV2InKindRedeemRequirements } from "../requirements/getVaultV2InKindRedeemRequirements.js";
14
16
  import { getBundlesTokenRequirements } from "../requirements/index.js";
15
- // Maximum accepted deadline horizon; the on-chain management fee is capped at 5%/yr so the
16
- // accrual model stays well-defined inside it.
17
- const VAULT_V2_FEE_PROJECTION_HORIZON = Time.s.from.y(1n);
17
+ import { computeVaultV2ForceWithdrawQuote } from "./forceWithdrawQuote.js";
18
+ import { computeVaultV2InKindRedeemQuote } from "./inKindRedeemQuote.js";
18
19
  /** Binds a viem client to a Vault V2 vault's action builders. */
19
20
  export class MorphoVaultV2 {
20
21
  client;
@@ -51,16 +52,19 @@ export class MorphoVaultV2 {
51
52
  }
52
53
  /** {@inheritDoc VaultV2Actions.deposit} */
53
54
  deposit(params) {
55
+ const suppliedNow = params.now;
56
+ const metadata = resolveTransactionMetadata(this.client.options.metadata, suppliedNow);
54
57
  const { userAddress, vaultData } = params;
55
58
  const { asset: vaultAsset } = vaultData;
56
59
  validateChainId(this.client.viemClient.chain?.id, this.chainId);
57
60
  if (!isAddressEqual(vaultData.address, this.vault)) {
58
61
  throw new VaultAddressMismatchError(this.vault, vaultData.address);
59
62
  }
60
- const createdAt = Time.timestamp();
63
+ const createdAt = suppliedNow ?? Time.timestamp();
61
64
  const deadline = params.deadline ?? createdAt + Time.s.from.h(2n);
62
- if (deadline <= createdAt)
65
+ if (deadline <= createdAt) {
63
66
  throw new ExpiredDeadlineError(deadline, createdAt);
67
+ }
64
68
  const common = normalizeBundlesCommonParams({
65
69
  deadline,
66
70
  referralFeePct: params.referralFeePct,
@@ -84,11 +88,13 @@ export class MorphoVaultV2 {
84
88
  let pendingRequirements;
85
89
  return Object.freeze({
86
90
  getRequirements: async (requirementOptions) => {
87
- const now = Time.timestamp();
88
- if (deadline <= now)
91
+ const now = suppliedNow ?? Time.timestamp();
92
+ if (deadline <= now) {
89
93
  throw new ExpiredDeadlineError(deadline, now);
90
- if (pendingRequirements != null)
94
+ }
95
+ if (pendingRequirements != null) {
91
96
  return await pendingRequirements;
97
+ }
92
98
  // Memoize the in-flight promise so concurrent callers share one round of
93
99
  // allowance/nonce reads.
94
100
  const pending = (async () => {
@@ -114,8 +120,9 @@ export class MorphoVaultV2 {
114
120
  }
115
121
  finally {
116
122
  // Later calls must re-read live allowances/nonces and honor their own options.
117
- if (pendingRequirements === pending)
123
+ if (pendingRequirements === pending) {
118
124
  pendingRequirements = undefined;
125
+ }
119
126
  }
120
127
  },
121
128
  buildTx: (signatures) => {
@@ -138,25 +145,29 @@ export class MorphoVaultV2 {
138
145
  referralFeeRecipient: common.referralFeeRecipient,
139
146
  deadline,
140
147
  },
141
- metadata: this.client.options.metadata,
148
+ metadata,
142
149
  });
143
150
  },
144
151
  });
145
152
  }
146
153
  withdraw(params) {
154
+ const suppliedNow = params.now;
155
+ const metadata = resolveTransactionMetadata(this.client.options.metadata, suppliedNow);
147
156
  const { amount, userAddress, vaultData } = params;
148
157
  validateChainId(this.client.viemClient.chain?.id, this.chainId);
149
158
  if (!isAddressEqual(vaultData.address, this.vault)) {
150
159
  throw new VaultAddressMismatchError(this.vault, vaultData.address);
151
160
  }
152
- if (amount <= 0n)
161
+ if (amount <= 0n) {
153
162
  throw new NonPositiveInputError("amount", amount);
163
+ }
154
164
  // Reject values outside the ABI range before resolving any requirements.
155
165
  validateUint256Field("amount", amount);
156
- const createdAt = Time.timestamp();
166
+ const createdAt = suppliedNow ?? Time.timestamp();
157
167
  const deadline = params.deadline ?? createdAt + Time.s.from.h(2n);
158
- if (deadline <= createdAt)
168
+ if (deadline <= createdAt) {
159
169
  throw new ExpiredDeadlineError(deadline, createdAt);
170
+ }
160
171
  const common = normalizeBundlesCommonParams({
161
172
  deadline,
162
173
  referralFeePct: params.referralFeePct,
@@ -174,9 +185,10 @@ export class MorphoVaultV2 {
174
185
  });
175
186
  return Object.freeze({
176
187
  getRequirements: async () => {
177
- const now = Time.timestamp();
178
- if (deadline <= now)
188
+ const now = suppliedNow ?? Time.timestamp();
189
+ if (deadline <= now) {
179
190
  throw new ExpiredDeadlineError(deadline, now);
191
+ }
180
192
  await this.assertWriteTargets(["bundles.vaultBundlesV1"]);
181
193
  // Re-read the live share allowance on every call instead of caching the resolved requirements.
182
194
  return getVaultBundlesSharesRequirements(this.client.viemClient, {
@@ -206,21 +218,25 @@ export class MorphoVaultV2 {
206
218
  referralFeeRecipient: common.referralFeeRecipient,
207
219
  deadline,
208
220
  },
209
- metadata: this.client.options.metadata,
221
+ metadata,
210
222
  });
211
223
  },
212
224
  });
213
225
  }
214
226
  /** {@inheritDoc VaultV2Actions.redeem} */
215
227
  redeem(params) {
228
+ const suppliedNow = params.now;
229
+ const metadata = resolveTransactionMetadata(this.client.options.metadata, suppliedNow);
216
230
  validateChainId(this.client.viemClient.chain?.id, this.chainId);
217
231
  const { shares, userAddress } = params;
218
- if (shares <= 0n)
232
+ if (shares <= 0n) {
219
233
  throw new NonPositiveInputError("shares", shares);
220
- const createdAt = Time.timestamp();
234
+ }
235
+ const createdAt = suppliedNow ?? Time.timestamp();
221
236
  const deadline = params.deadline ?? createdAt + Time.s.from.h(2n);
222
- if (deadline <= createdAt)
237
+ if (deadline <= createdAt) {
223
238
  throw new ExpiredDeadlineError(deadline, createdAt);
239
+ }
224
240
  const common = normalizeBundlesCommonParams({
225
241
  deadline,
226
242
  referralFeePct: params.referralFeePct,
@@ -229,9 +245,10 @@ export class MorphoVaultV2 {
229
245
  const spender = getChainAddress(this.chainId, "bundles.vaultBundlesV1");
230
246
  return Object.freeze({
231
247
  getRequirements: async () => {
232
- const now = Time.timestamp();
233
- if (deadline <= now)
248
+ const now = suppliedNow ?? Time.timestamp();
249
+ if (deadline <= now) {
234
250
  throw new ExpiredDeadlineError(deadline, now);
251
+ }
235
252
  await this.assertWriteTargets(["bundles.vaultBundlesV1"]);
236
253
  return getVaultBundlesSharesRequirements(this.client.viemClient, {
237
254
  vaultData: await this.getData(),
@@ -260,172 +277,42 @@ export class MorphoVaultV2 {
260
277
  referralFeeRecipient: common.referralFeeRecipient,
261
278
  deadline,
262
279
  },
263
- metadata: this.client.options.metadata,
280
+ metadata,
264
281
  });
265
282
  },
266
283
  });
267
284
  }
268
285
  /** {@inheritDoc VaultV2Actions.inKindRedeem} */
269
- inKindRedeem({ amount, marketParamsList, vaultData, userAddress, adapter: adapterOverride, deadline: deadlineOverride, }) {
286
+ inKindRedeem(params) {
287
+ const suppliedNow = params.now;
288
+ const metadata = resolveTransactionMetadata(this.client.options.metadata, suppliedNow);
289
+ const { amount, marketParamsList, vaultData, userAddress, adapter: adapterOverride, deadline: deadlineOverride, } = params;
270
290
  validateChainId(this.client.viemClient.chain?.id, this.chainId);
271
291
  if (!isAddressEqual(vaultData.address, this.vault)) {
272
292
  throw new VaultAddressMismatchError(this.vault, vaultData.address);
273
293
  }
274
- if (amount <= 0n)
275
- throw new NonPositiveInputError("amount", amount);
276
- const marketParamsListSnapshot = marketParamsList.map(({ loanToken, collateralToken, oracle, irm, lltv }) => ({
277
- loanToken,
278
- collateralToken,
279
- oracle,
280
- irm,
281
- lltv,
282
- }));
283
- const marketIdListSnapshot = marketParamsListSnapshot.map((marketParams) => MarketUtils.getMarketId(marketParams));
284
- const now = Time.timestamp();
285
- const deadline = deadlineOverride ?? now + Time.s.from.h(2n);
286
- // Reject the same bounds the action enforces, before `getRequirements()` can walk the caller
287
- // through a vault-share approval or an EIP-712 permit for a deadline `buildTx()` cannot encode.
288
- validateDeadline(deadline);
289
- if (deadline <= now)
290
- throw new ExpiredDeadlineError(deadline, now);
291
- if (vaultData.accrualAdapters.length !== 1) {
292
- throw new VaultV2SingleAdapterRequiredError(this.vault, vaultData.accrualAdapters.length);
293
- }
294
- const soleAdapter = vaultData.accrualAdapters[0];
295
- if (soleAdapter == null) {
296
- throw new VaultV2SingleAdapterRequiredError(this.vault, 0);
297
- }
298
- const adapter = adapterOverride ?? soleAdapter.address;
299
- if (!isAddressEqual(adapter, soleAdapter.address)) {
300
- throw new AdapterNotPartOfVaultError(this.vault, adapter);
301
- }
302
- if (!(soleAdapter instanceof AccrualVaultV2MorphoMarketV1AdapterV2)) {
303
- throw new VaultV2UnsupportedExitAdapterError(adapter);
304
- }
305
- const penalty = vaultData.forceDeallocatePenalties[soleAdapter.address] ?? 0n;
306
- const idleAssets = MathLib.min(vaultData.assetBalance, amount);
307
- const assetsToDeallocate = MathLib.wDivDown(amount - idleAssets, MathLib.WAD + penalty);
308
- if (idleAssets === 0n && assetsToDeallocate === 0n) {
309
- throw new InKindRedeemZeroDeallocationError({
310
- vault: this.vault,
311
- amount,
312
- penalty,
313
- });
314
- }
315
- if (assetsToDeallocate > 0n && marketParamsListSnapshot.length === 0) {
316
- throw new EmptyMarketParamsListError();
317
- }
318
- const assetsByMarket = new Map(soleAdapter.markets
319
- .filter((market) => (soleAdapter.supplyShares[market.id] ?? 0n) !== 0n)
320
- .map((market) => [
321
- market.id,
322
- market
323
- .accrueInterest(now)
324
- .toSupplyAssets(soleAdapter.supplyShares[market.id] ?? 0n),
325
- ]));
326
- const uniqueMarketIds = new Set(marketIdListSnapshot);
327
- let covered = 0n;
328
- for (const id of uniqueMarketIds)
329
- covered += assetsByMarket.get(id) ?? 0n;
330
- if (covered < assetsToDeallocate) {
331
- const maxExitAssets = covered === 0n
332
- ? idleAssets
333
- : idleAssets +
334
- MathLib.wMulUp(covered + 1n, MathLib.WAD + penalty) -
335
- 1n;
336
- throw new InKindRedeemCoverageError({
337
- required: assetsToDeallocate,
338
- covered,
339
- maxExitAssets,
340
- });
341
- }
342
- let remaining = assetsToDeallocate;
343
- let peak = 0n;
344
- const consumedMarketIds = new Set();
345
- const { vault: allowanceVault } = vaultData.accrueInterest(deadline);
346
- // Interest can lower the burn, while management fees can raise it; bound both endpoints.
347
- const previewAllowance = (assets) => MathLib.max(vaultData.toShares(assets, "Up"), allowanceVault.toShares(assets, "Up"));
348
- let requiredShareAllowance = previewAllowance(BigInt(marketParamsListSnapshot.length));
349
- // Pre-burn previews upper-bound each separately rounded idle, penalty, and main burn.
350
- requiredShareAllowance += previewAllowance(idleAssets);
351
- for (const id of marketIdListSnapshot) {
352
- const available = consumedMarketIds.has(id)
353
- ? 0n
354
- : (assetsByMarket.get(id) ?? 0n);
355
- consumedMarketIds.add(id);
356
- const chunk = MathLib.min(available, remaining);
357
- peak = MathLib.max(peak, chunk);
358
- requiredShareAllowance += previewAllowance(MathLib.wMulUp(chunk, penalty));
359
- requiredShareAllowance += previewAllowance(chunk);
360
- remaining -= chunk;
361
- }
294
+ const { deadline, adapter, marketParamsListSnapshot, requiredShareAllowance, peak, } = computeVaultV2InKindRedeemQuote({
295
+ amount,
296
+ marketParamsList,
297
+ vaultData,
298
+ deadline: deadlineOverride,
299
+ adapter: adapterOverride,
300
+ now: suppliedNow ?? Time.timestamp(),
301
+ vault: this.vault,
302
+ });
362
303
  const vaultExitBundlesV1 = getChainAddress(this.chainId, "bundles.vaultExitBundlesV1");
363
- const blue = getChainAddress(this.chainId, "blue");
364
304
  return {
365
- getRequirements: async () => {
366
- const requirementsTimestamp = Time.timestamp();
367
- if (deadline <= requirementsTimestamp) {
368
- throw new ExpiredDeadlineError(deadline, requirementsTimestamp);
369
- }
370
- await validateClientChainId(this.client.viemClient, this.chainId);
371
- await this.assertWriteTargets(["bundles.vaultExitBundlesV1", "blue"]);
372
- // Vault gates are intentionally left to the final transaction. A receive gate may inspect
373
- // VaultExitBundlesV1's transient `initiator`, which is populated only during the actual
374
- // periphery call. A send-share gate is arbitrary external code and is evaluated repeatedly
375
- // after intermediate penalty share burns, so one standalone read is not execution-equivalent
376
- // either. Simulate the finalized transaction after authorization when gate compatibility
377
- // must be checked before submission.
378
- const [allowance, nonce, blueBalance] = await Promise.all([
379
- readContract(this.client.viemClient, {
380
- address: this.vault,
381
- abi: erc20Abi,
382
- functionName: "allowance",
383
- args: [userAddress, vaultExitBundlesV1],
384
- }),
385
- readContract(this.client.viemClient, {
386
- address: this.vault,
387
- abi: erc2612Abi,
388
- functionName: "nonces",
389
- args: [userAddress],
390
- }),
391
- readContract(this.client.viemClient, {
392
- address: vaultData.asset,
393
- abi: erc20Abi,
394
- functionName: "balanceOf",
395
- args: [blue],
396
- }),
397
- ]);
398
- if (blueBalance < peak) {
399
- throw new InsufficientBlueBalanceForInKindRedeemError({
400
- asset: vaultData.asset,
401
- available: blueBalance,
402
- required: peak,
403
- });
404
- }
405
- if (allowance >= requiredShareAllowance)
406
- return [];
407
- if (this.client.options.supportSignature) {
408
- return [
409
- encodeVaultSharesPermit({
410
- vault: vaultData,
411
- version: "vaultV2",
412
- spender: vaultExitBundlesV1,
413
- owner: userAddress,
414
- chainId: this.chainId,
415
- nonce,
416
- deadline,
417
- }),
418
- ];
419
- }
420
- return [
421
- encodeErc20Approval({
422
- token: this.vault,
423
- spender: vaultExitBundlesV1,
424
- amount: maxUint256,
425
- chainId: this.chainId,
426
- }),
427
- ];
428
- },
305
+ getRequirements: () => getVaultV2InKindRedeemRequirements(this.client.viemClient, {
306
+ vaultData,
307
+ vault: this.vault,
308
+ userAddress,
309
+ chainId: this.chainId,
310
+ deadline,
311
+ requiredShareAllowance,
312
+ supportSignature: this.client.options.supportSignature,
313
+ peak,
314
+ now: suppliedNow,
315
+ }),
429
316
  buildTx: (signatures) => {
430
317
  const permit = selectBundlesSharesPermitSignature(signatures, {
431
318
  spender: vaultExitBundlesV1,
@@ -442,225 +329,45 @@ export class MorphoVaultV2 {
442
329
  deadline,
443
330
  requirementSignature: permit,
444
331
  },
445
- metadata: this.client.options.metadata,
332
+ metadata,
446
333
  });
447
334
  },
448
335
  };
449
336
  }
450
337
  /** {@inheritDoc VaultV2Actions.forceWithdraw} */
451
- forceWithdraw({ exitAssets, vaultData, userAddress, adapter: adapterOverride, deadline: deadlineOverride, slippageTolerance = DEFAULT_SLIPPAGE_TOLERANCE, minSharePriceE27: minSharePriceE27Override, referralFeePct = 0n, referralFeeRecipient, }) {
338
+ forceWithdraw(params) {
339
+ const suppliedNow = params.now;
340
+ const metadata = resolveTransactionMetadata(this.client.options.metadata, suppliedNow);
341
+ const { exitAssets, vaultData, userAddress, adapter: adapterOverride, deadline: deadlineOverride, slippageTolerance = DEFAULT_SLIPPAGE_TOLERANCE, minSharePriceE27: minSharePriceE27Override, referralFeePct = 0n, referralFeeRecipient, } = params;
452
342
  validateChainId(this.client.viemClient.chain?.id, this.chainId);
453
343
  if (!isAddressEqual(vaultData.address, this.vault)) {
454
344
  throw new VaultAddressMismatchError(this.vault, vaultData.address);
455
345
  }
456
- if (exitAssets <= 0n)
457
- throw new NonPositiveInputError("exitAssets", exitAssets);
458
- validateUint256Field("exitAssets", exitAssets);
459
- validateSlippageTolerance(slippageTolerance);
460
- // Called for its throws only: the raw pair is forwarded to the action, which normalizes it
461
- // again on the encode path so a direct action caller gets the same guarantees.
462
- validateReferralFee({ referralFeePct, referralFeeRecipient });
463
- if (minSharePriceE27Override != null) {
464
- // An unbounded override reopens the exact hole this path closes: the contract reads
465
- // `minSharePriceE27 == 0` as "no bound".
466
- if (minSharePriceE27Override <= 0n) {
467
- throw new NonPositiveInputError("minSharePriceE27", minSharePriceE27Override);
468
- }
469
- // And an override the uint256 slot cannot hold must fail here rather than after
470
- // `getRequirements()` has already asked for an approval or a permit signature.
471
- validateUint256Field("minSharePriceE27", minSharePriceE27Override);
472
- }
473
- const now = Time.timestamp();
474
- const deadline = deadlineOverride ?? now + Time.s.from.h(2n);
475
- // Reject the same bounds the action enforces, before `getRequirements()` can walk the caller
476
- // through a vault-share approval or an EIP-712 permit for a deadline `buildTx()` cannot encode.
477
- validateDeadline(deadline);
478
- if (deadline <= now)
479
- throw new ExpiredDeadlineError(deadline, now);
480
- const maxDeadline = now + VAULT_V2_FEE_PROJECTION_HORIZON;
481
- if (deadline > maxDeadline) {
482
- throw new InputExceedsMaxError({
483
- field: "deadline",
484
- value: deadline,
485
- max: maxDeadline,
486
- });
487
- }
488
- const eligibility = resolveVaultV2ForceWithdrawEligibility(vaultData, adapterOverride);
489
- switch (eligibility.type) {
490
- case "eligible":
491
- break;
492
- case "adapterCount":
493
- throw new VaultV2SingleAdapterRequiredError(this.vault, eligibility.adapters);
494
- case "adapterMismatch":
495
- throw new AdapterNotPartOfVaultError(this.vault, eligibility.adapter);
496
- case "unsupportedAdapter":
497
- throw new VaultV2UnsupportedExitAdapterError(eligibility.adapter);
498
- case "unsupportedLiquidityAdapter":
499
- throw new VaultV2UnsupportedLiquidityAdapterError({
500
- vault: this.vault,
501
- liquidityAdapter: eligibility.liquidityAdapter,
502
- adapter: eligibility.adapter,
503
- });
504
- case "undecodableLiquidityData":
505
- throw new VaultV2UndecodableLiquidityDataError({
506
- vault: this.vault,
507
- liquidityAdapter: eligibility.liquidityAdapter,
508
- liquidityData: eligibility.liquidityData,
509
- cause: eligibility.cause,
510
- });
511
- }
512
- const { adapter: accrualAdapter, liquidityMarketId } = eligibility;
513
- const adapter = accrualAdapter.address;
514
- const plan = computeVaultV2ForceWithdrawPlan({
515
- vaultData,
516
- adapter: accrualAdapter,
517
- liquidityMarketId,
346
+ const { deadline, adapter, minSharePriceE27, requiredShareAllowance } = computeVaultV2ForceWithdrawQuote({
518
347
  exitAssets,
519
- timestamp: now,
520
- });
521
- if (plan.withdrawnAssets <= 0n) {
522
- throw new VaultV2ForceWithdrawZeroWithdrawalError({
523
- vault: this.vault,
524
- exitAssets,
525
- penalty: plan.penalty,
526
- });
527
- }
528
- // The contract's force-deallocation loop is unbounded: under-coverage panics on-chain.
529
- if (plan.coveredAssets < plan.assetsToDeallocate) {
530
- throw new VaultV2ForceWithdrawCoverageError({
531
- required: plan.assetsToDeallocate,
532
- covered: plan.coveredAssets,
533
- maxExitAssets: plan.maxExitAssets,
534
- });
535
- }
536
- const projectionTimestamp = deadline;
537
- // VaultExitBundlesV1 measures shares as `sharesBefore - balanceAfter`; its first withdrawal
538
- // accrues the vault before burning. The realized price sits between the raw snapshot price
539
- // (pending fee mints not yet diluting it) and the `now` projection (interest the chain may not
540
- // have accrued yet, e.g. when the wall clock runs ahead of the chain). Price the floor off the
541
- // larger *net* burn of the two endpoints — the raw snapshot mints no fee shares, the `now`
542
- // accrual mints `feeSharesNow` — so it never exceeds either, and use the projected deadline
543
- // accrual for the fee-mint guard.
544
- const { vault: nowVaultData } = vaultData.accrueInterest(MathLib.max(now, vaultData.lastUpdate));
545
- const { vault: projectedVaultData } = vaultData.accrueInterest(MathLib.max(projectionTimestamp, vaultData.lastUpdate));
546
- const sharesBurntRaw = computeVaultV2ForceWithdrawSharesBurnt({
547
348
  vaultData,
548
- deadlineVaultData: vaultData,
549
- plan,
550
- });
551
- const sharesBurntNow = computeVaultV2ForceWithdrawSharesBurnt({
552
- vaultData: nowVaultData,
553
- deadlineVaultData: nowVaultData,
554
- plan,
555
- });
556
- const feeSharesNow = computeVaultV2ForceWithdrawFeeSharesMinted({
557
- vaultData,
558
- owner: userAddress,
559
- timestamp: now,
560
- });
561
- const feeSharesProjected = computeVaultV2ForceWithdrawFeeSharesMinted({
562
- vaultData,
563
- owner: userAddress,
564
- timestamp: projectionTimestamp,
565
- });
566
- const minSharesBurntProjected = computeVaultV2ForceWithdrawMinSharesBurnt({
567
- vaultData: projectedVaultData,
568
- plan,
349
+ userAddress,
350
+ adapter: adapterOverride,
351
+ deadline: deadlineOverride,
352
+ slippageTolerance,
353
+ minSharePriceE27: minSharePriceE27Override,
354
+ referralFeePct,
355
+ referralFeeRecipient,
356
+ now: suppliedNow ?? Time.timestamp(),
357
+ vault: this.vault,
569
358
  });
570
- // When fee shares are minted, the projected pair bounds every execution time in the window.
571
- if (feeSharesProjected > 0n &&
572
- feeSharesProjected >= minSharesBurntProjected) {
573
- throw new VaultV2ForceWithdrawFeeSharesExceedBurnError({
574
- vault: this.vault,
575
- userAddress,
576
- sharesBurnt: minSharesBurntProjected,
577
- feeShares: feeSharesProjected,
578
- });
579
- }
580
- // sharesBurntNow ≥ minSharesBurntNow ≥ minSharesBurntProjected > feeSharesProjected ≥ feeSharesNow
581
- const sharesBurntForFloor = MathLib.max(sharesBurntRaw, sharesBurntNow - feeSharesNow);
582
- if (minSharePriceE27Override != null) {
583
- // The maximum-slippage threshold only bounds the override; unlike the transaction floor it
584
- // may round to zero, in which case every positive override is acceptable.
585
- const minAllowedSharePriceE27 = _try(() => computeMinForceWithdrawSharePrice({
586
- withdrawnAssets: plan.withdrawnAssets,
587
- sharesBurnt: sharesBurntForFloor,
588
- slippageTolerance: MAX_SLIPPAGE_TOLERANCE,
589
- }), VaultV2ForceWithdrawZeroSharePriceError) ?? 1n;
590
- if (minSharePriceE27Override < minAllowedSharePriceE27) {
591
- throw new VaultV2ForceWithdrawSharePriceBelowFloorError({
592
- minSharePriceE27: minSharePriceE27Override,
593
- floorE27: minAllowedSharePriceE27,
594
- });
595
- }
596
- }
597
- const minSharePriceE27 = minSharePriceE27Override ??
598
- computeMinForceWithdrawSharePrice({
599
- withdrawnAssets: plan.withdrawnAssets,
600
- sharesBurnt: sharesBurntForFloor,
601
- slippageTolerance,
602
- });
603
- // The floor is deliberately *not* capped at `MAX_ABSOLUTE_SHARE_PRICE` (100 assets/share):
604
- // lowering a lower bound weakens it, so any vault whose share price grew past that would be left
605
- // with no real protection. Its two sibling `computeMin*SharePrice` helpers cap nothing either —
606
- // only the `computeMax*` ones do, where capping relaxes an upper bound and is safe.
607
- validateUint256Field("minSharePriceE27", minSharePriceE27);
608
- // This is the minimum allowance needed for successful execution, not an exit cap: the bundle
609
- // independently rejects a realized share price below `minSharePriceE27`. Include the fee
610
- // shares minted by the first withdrawal and enough headroom for the configured tolerance.
611
- const allowanceSharePriceE27 = MathLib.max(MathLib.min(MathLib.mulDivDown(minSharePriceE27, MathLib.WAD - slippageTolerance, MathLib.WAD), minSharePriceE27 - 1n), 1n);
612
- const requiredShareAllowance = MathLib.min(MathLib.max(MathLib.mulDivUp(exitAssets, MathLib.RAY, allowanceSharePriceE27), MathLib.mulDivUp(exitAssets, MathLib.RAY, minSharePriceE27) + 1n) + feeSharesProjected, maxUint256);
613
359
  const vaultExitBundlesV1 = getChainAddress(this.chainId, "bundles.vaultExitBundlesV1");
614
360
  return {
615
- getRequirements: async () => {
616
- const requirementsTimestamp = Time.timestamp();
617
- if (deadline <= requirementsTimestamp) {
618
- throw new ExpiredDeadlineError(deadline, requirementsTimestamp);
619
- }
620
- await validateClientChainId(this.client.viemClient, this.chainId);
621
- await this.assertWriteTargets(["bundles.vaultExitBundlesV1"]);
622
- // Vault gates are intentionally left to the final transaction: the receive-assets gate may
623
- // inspect VaultExitBundlesV1's transient `initiator`, and a send-shares gate is arbitrary
624
- // external code re-evaluated after each intermediate penalty burn, so neither is
625
- // execution-equivalent from a standalone read. Unlike in-kind redemption, this exit never
626
- // supplies into Morpho Blue, so no Blue token-balance check is needed.
627
- const [allowance, nonce] = await Promise.all([
628
- readContract(this.client.viemClient, {
629
- address: this.vault,
630
- abi: erc20Abi,
631
- functionName: "allowance",
632
- args: [userAddress, vaultExitBundlesV1],
633
- }),
634
- readContract(this.client.viemClient, {
635
- address: this.vault,
636
- abi: erc2612Abi,
637
- functionName: "nonces",
638
- args: [userAddress],
639
- }),
640
- ]);
641
- if (allowance >= requiredShareAllowance)
642
- return [];
643
- if (this.client.options.supportSignature) {
644
- const requirement = encodeVaultSharesPermit({
645
- vault: vaultData,
646
- version: "vaultV2",
647
- spender: vaultExitBundlesV1,
648
- owner: userAddress,
649
- chainId: this.chainId,
650
- nonce,
651
- deadline,
652
- });
653
- return [requirement];
654
- }
655
- return [
656
- encodeErc20Approval({
657
- token: this.vault,
658
- spender: vaultExitBundlesV1,
659
- amount: maxUint256,
660
- chainId: this.chainId,
661
- }),
662
- ];
663
- },
361
+ getRequirements: () => getVaultV2ForceWithdrawRequirements(this.client.viemClient, {
362
+ vaultData,
363
+ vault: this.vault,
364
+ userAddress,
365
+ chainId: this.chainId,
366
+ deadline,
367
+ requiredShareAllowance,
368
+ supportSignature: this.client.options.supportSignature,
369
+ now: suppliedNow,
370
+ }),
664
371
  buildTx: (signatures) => {
665
372
  const permit = selectBundlesSharesPermitSignature(signatures, {
666
373
  spender: vaultExitBundlesV1,
@@ -679,12 +386,14 @@ export class MorphoVaultV2 {
679
386
  referralFeeRecipient,
680
387
  requirementSignature: permit,
681
388
  },
682
- metadata: this.client.options.metadata,
389
+ metadata,
683
390
  });
684
391
  },
685
392
  };
686
393
  }
687
- forceRedeem({ deallocations, redeem, userAddress, }) {
394
+ forceRedeem(params) {
395
+ const metadata = resolveTransactionMetadata(this.client.options.metadata, params.now);
396
+ const { deallocations, redeem, userAddress } = params;
688
397
  validateChainId(this.client.viemClient.chain?.id, this.chainId);
689
398
  const shares = redeem.shares;
690
399
  const deallocationsSnapshot = deallocations.map(({ adapter, marketParams, amount }) => ({
@@ -700,7 +409,7 @@ export class MorphoVaultV2 {
700
409
  redeem: { shares, recipient: userAddress },
701
410
  onBehalf: userAddress,
702
411
  },
703
- metadata: this.client.options.metadata,
412
+ metadata,
704
413
  }),
705
414
  };
706
415
  }