@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
@@ -4,4 +4,4 @@ UnsupportedPreLiquidationParamsError as UnsupportedBluePreLiquidationParamsError
4
4
  export { getUnsupportedVaultV2Adapter as getBlueUnsupportedVaultV2Adapter, InvalidNumberError, InvalidPermitDomainChainIdError, InvalidPermitDomainVerifyingContractError, isUnknownOfFactoryError as isBlueUnknownOfFactoryError, MalformedTokenMetadataError, MarketParamsIdMismatchError as BlueMarketParamsIdMismatchError, MissingPermitDomainNameError, NonTupleReturnValueError, Permit2ExpirationOutOfRangeError, UnknownAbiFunctionError, UnnamedAbiOutputsError, UnsupportedPermitDomainExtensionsError, } from "@morpho-org/blue-sdk-viem";
5
5
  export { ChainIdMismatchError as MidnightChainIdMismatchError, InvalidEcrecoverSignatureError as InvalidMidnightEcrecoverSignatureError, InvalidEcrecoverSignatureVError as InvalidMidnightEcrecoverSignatureVError, InvalidMarketParameterError as InvalidMidnightMarketParameterError, InvalidMidnightApiQuoteTargetError, InvalidMidnightApiResponseError, InvalidOfferGroupError as InvalidMidnightOfferGroupError, InvalidOfferParameterError as InvalidMidnightOfferParameterError, InvalidPositionAccrualStateError as InvalidMidnightPositionAccrualStateError, InvalidPositionAccrualTimestampError as InvalidMidnightPositionAccrualTimestampError, InvalidPositionLossFactorError as InvalidMidnightPositionLossFactorError, InvalidRateRatifierV1RateError as InvalidMidnightRateRatifierV1RateError, InvalidRateRatifierV1TickError as InvalidMidnightRateRatifierV1TickError, InvalidRateRatifierV1TimeError as InvalidMidnightRateRatifierV1TimeError, InvalidRatifierV1AddressError as InvalidMidnightRatifierV1AddressError, InvalidTickSpacingError as InvalidMidnightTickSpacingError, InvalidTreeError as InvalidMidnightTreeError, InvalidTreeHeightError as InvalidMidnightTreeHeightError, InvalidTypedDataSignatureError as InvalidMidnightTypedDataSignatureError, MidnightApiError, MidnightMempoolValidationError, PayloadDecodeError as MidnightPayloadDecodeError, PriceGreaterThanOneError as MidnightPriceGreaterThanOneError, RateRatifierV1BoundOverflowError as MidnightRateRatifierV1BoundOverflowError, RatifierV1TakerNotAllowedError as MidnightRatifierV1TakerNotAllowedError, SettlementFeeExceedsPriceError as MidnightSettlementFeeExceedsPriceError, TickOutOfRangeError as MidnightTickOutOfRangeError, UnknownCollateralIndexError as UnknownMidnightCollateralIndexError, } from "@morpho-org/midnight-sdk";
6
6
  export type { ErrorClass } from "@morpho-org/morpho-ts";
7
- export { _try, DivisionByZeroError, InvalidAddressError, InvalidBitLengthError, NegativeValueError, RegistryValueAlreadyRegisteredError, RetriesExhaustedError, RetryStoppedError, } from "@morpho-org/morpho-ts";
7
+ export { _try, DivisionByZeroError, InvalidAddressError, InvalidBitLengthError, MissingLocalePartError, NegativeValueError, RegistryValueAlreadyRegisteredError, RetriesExhaustedError, RetryStoppedError, } from "@morpho-org/morpho-ts";
package/lib/esm/errors.js CHANGED
@@ -3,4 +3,4 @@ export { BlueErrors, IncompleteChainRegistryError, InvalidMarketParamsError as I
3
3
  UnsupportedPreLiquidationParamsError as UnsupportedBluePreLiquidationParamsError, UnsupportedVaultV2AdapterError as UnsupportedBlueVaultV2AdapterError, VaultV2Errors, } from "@morpho-org/blue-sdk";
4
4
  export { getUnsupportedVaultV2Adapter as getBlueUnsupportedVaultV2Adapter, InvalidNumberError, InvalidPermitDomainChainIdError, InvalidPermitDomainVerifyingContractError, isUnknownOfFactoryError as isBlueUnknownOfFactoryError, MalformedTokenMetadataError, MarketParamsIdMismatchError as BlueMarketParamsIdMismatchError, MissingPermitDomainNameError, NonTupleReturnValueError, Permit2ExpirationOutOfRangeError, UnknownAbiFunctionError, UnnamedAbiOutputsError, UnsupportedPermitDomainExtensionsError, } from "@morpho-org/blue-sdk-viem";
5
5
  export { ChainIdMismatchError as MidnightChainIdMismatchError, InvalidEcrecoverSignatureError as InvalidMidnightEcrecoverSignatureError, InvalidEcrecoverSignatureVError as InvalidMidnightEcrecoverSignatureVError, InvalidMarketParameterError as InvalidMidnightMarketParameterError, InvalidMidnightApiQuoteTargetError, InvalidMidnightApiResponseError, InvalidOfferGroupError as InvalidMidnightOfferGroupError, InvalidOfferParameterError as InvalidMidnightOfferParameterError, InvalidPositionAccrualStateError as InvalidMidnightPositionAccrualStateError, InvalidPositionAccrualTimestampError as InvalidMidnightPositionAccrualTimestampError, InvalidPositionLossFactorError as InvalidMidnightPositionLossFactorError, InvalidRateRatifierV1RateError as InvalidMidnightRateRatifierV1RateError, InvalidRateRatifierV1TickError as InvalidMidnightRateRatifierV1TickError, InvalidRateRatifierV1TimeError as InvalidMidnightRateRatifierV1TimeError, InvalidRatifierV1AddressError as InvalidMidnightRatifierV1AddressError, InvalidTickSpacingError as InvalidMidnightTickSpacingError, InvalidTreeError as InvalidMidnightTreeError, InvalidTreeHeightError as InvalidMidnightTreeHeightError, InvalidTypedDataSignatureError as InvalidMidnightTypedDataSignatureError, MidnightApiError, MidnightMempoolValidationError, PayloadDecodeError as MidnightPayloadDecodeError, PriceGreaterThanOneError as MidnightPriceGreaterThanOneError, RateRatifierV1BoundOverflowError as MidnightRateRatifierV1BoundOverflowError, RatifierV1TakerNotAllowedError as MidnightRatifierV1TakerNotAllowedError, SettlementFeeExceedsPriceError as MidnightSettlementFeeExceedsPriceError, TickOutOfRangeError as MidnightTickOutOfRangeError, UnknownCollateralIndexError as UnknownMidnightCollateralIndexError, } from "@morpho-org/midnight-sdk";
6
- export { _try, DivisionByZeroError, InvalidAddressError, InvalidBitLengthError, NegativeValueError, RegistryValueAlreadyRegisteredError, RetriesExhaustedError, RetryStoppedError, } from "@morpho-org/morpho-ts";
6
+ export { _try, DivisionByZeroError, InvalidAddressError, InvalidBitLengthError, MissingLocalePartError, NegativeValueError, RegistryValueAlreadyRegisteredError, RetriesExhaustedError, RetryStoppedError, } from "@morpho-org/morpho-ts";
@@ -25,15 +25,17 @@ import { MIN_DEAD_SHARES, MIN_VAULT_V2_DEAD_ASSETS } from "./constant.js";
25
25
  */
26
26
  export function validateBlueMarketDeadShares(market, path = []) {
27
27
  const entity = [...path, market.id];
28
- if (market.deadSupplyShares == null)
28
+ if (market.deadSupplyShares == null) {
29
29
  throw new MissingDeadSharesDataError({ entity, field: "deadSupplyShares" });
30
- if (market.deadSupplyShares < MIN_DEAD_SHARES)
30
+ }
31
+ if (market.deadSupplyShares < MIN_DEAD_SHARES) {
31
32
  throw new InsufficientDeadSharesError({
32
33
  entity,
33
34
  reason: "deadShares",
34
35
  observed: market.deadSupplyShares,
35
36
  required: MIN_DEAD_SHARES,
36
37
  });
38
+ }
37
39
  }
38
40
  /**
39
41
  * Checks that a Vault V1 holds at least `MIN_DEAD_SHARES` shares at `DEAD_ADDRESS` and that every
@@ -62,22 +64,25 @@ export function validateBlueMarketDeadShares(market, path = []) {
62
64
  */
63
65
  export function validateVaultV1DeadShares(vault, path = []) {
64
66
  const entity = [...path, vault.address];
65
- if (vault.deadShares == null)
67
+ if (vault.deadShares == null) {
66
68
  throw new MissingDeadSharesDataError({ entity, field: "deadShares" });
67
- if (vault.deadShares < MIN_DEAD_SHARES)
69
+ }
70
+ if (vault.deadShares < MIN_DEAD_SHARES) {
68
71
  throw new InsufficientDeadSharesError({
69
72
  entity,
70
73
  reason: "deadShares",
71
74
  observed: vault.deadShares,
72
75
  required: MIN_DEAD_SHARES,
73
76
  });
77
+ }
74
78
  for (const marketId of vault.withdrawQueue) {
75
79
  const allocation = vault.allocations.get(marketId);
76
- if (allocation == null)
80
+ if (allocation == null) {
77
81
  throw new MissingDeadSharesDataError({
78
82
  entity: [...entity, marketId],
79
83
  field: "allocation",
80
84
  });
85
+ }
81
86
  validateBlueMarketDeadShares(allocation.position.market, entity);
82
87
  }
83
88
  }
@@ -111,86 +116,117 @@ export function validateVaultV1DeadShares(vault, path = []) {
111
116
  export function validateVaultV2DeadShares(vault, path = []) {
112
117
  const entity = [...path, vault.address];
113
118
  const { deadShares, deadAssets, absoluteCaps } = vault;
114
- if (deadShares == null)
119
+ if (deadShares == null) {
115
120
  throw new MissingDeadSharesDataError({ entity, field: "deadShares" });
116
- if (deadAssets == null)
121
+ }
122
+ if (deadAssets == null) {
117
123
  throw new MissingDeadSharesDataError({ entity, field: "deadAssets" });
118
- if (absoluteCaps == null)
124
+ }
125
+ if (absoluteCaps == null) {
119
126
  throw new MissingDeadSharesDataError({ entity, field: "absoluteCaps" });
120
- if (deadShares < MIN_DEAD_SHARES)
127
+ }
128
+ if (deadShares < MIN_DEAD_SHARES) {
121
129
  throw new InsufficientDeadSharesError({
122
130
  entity,
123
131
  reason: "deadShares",
124
132
  observed: deadShares,
125
133
  required: MIN_DEAD_SHARES,
126
134
  });
127
- if (deadAssets < MIN_VAULT_V2_DEAD_ASSETS)
135
+ }
136
+ if (deadAssets < MIN_VAULT_V2_DEAD_ASSETS) {
128
137
  throw new InsufficientDeadSharesError({
129
138
  entity,
130
139
  reason: "deadAssets",
131
140
  observed: deadAssets,
132
141
  required: MIN_VAULT_V2_DEAD_ASSETS,
133
142
  });
143
+ }
134
144
  // Share price (totalAssets + 1) / (totalSupply + virtualShares) must exceed 1 / (10 * virtualShares).
135
145
  const priceNumerator = (vault._totalAssets + 1n) * 10n * vault.virtualShares;
136
146
  const priceDenominator = vault.totalSupply + vault.virtualShares;
137
- if (priceNumerator <= priceDenominator)
147
+ if (priceNumerator <= priceDenominator) {
138
148
  throw new InsufficientDeadSharesError({
139
149
  entity,
140
150
  reason: "sharePrice",
141
151
  observed: priceNumerator,
142
152
  required: priceDenominator + 1n,
143
153
  });
144
- const capOf = (id, adapter) => {
145
- const cap = absoluteCaps[id];
146
- if (cap == null)
147
- throw new MissingDeadSharesDataError({
148
- entity: [...entity, adapter],
149
- field: "absoluteCaps",
150
- });
151
- return cap;
152
- };
154
+ }
153
155
  for (const adapter of vault.accrualAdapters) {
154
- if (adapter instanceof AccrualVaultV2MorphoVaultV1Adapter) {
155
- const id = VaultV2MorphoVaultV1Adapter.adapterCapId(adapter.address);
156
- if (capOf(id, adapter.address) > 0n)
157
- validateVaultV1DeadShares(adapter.accrualVaultV1, entity);
158
- }
159
- else if (adapter instanceof AccrualVaultV2MorphoMarketV1Adapter) {
160
- for (const { market } of adapter.positions) {
161
- const id = VaultV2MorphoMarketV1Adapter.adapterMarketCapId(adapter.address, market.params);
162
- if (capOf(id, adapter.address) > 0n)
163
- validateBlueMarketDeadShares(market, entity);
164
- }
165
- }
166
- else if (adapter instanceof AccrualVaultV2MorphoMarketV1AdapterV2) {
167
- for (const market of adapter.markets) {
168
- const id = VaultV2MorphoMarketV1AdapterV2.adapterMarketCapId(adapter.address, market.params);
169
- if (capOf(id, adapter.address) > 0n)
170
- validateBlueMarketDeadShares(market, entity);
171
- }
172
- }
173
- else {
174
- throw new MissingDeadSharesDataError({
175
- entity: [...entity, adapter.address],
176
- field: "adapter",
177
- });
178
- }
156
+ // Reject incomplete or underfunded adapter snapshots before checking liquidity.
157
+ assertAdapterDeadShares(adapter, { absoluteCaps, entity });
179
158
  }
180
159
  // Deposits are allocated to the liquidity market, so the snapshot must hold it even when it
181
160
  // has no allocation yet (the adapters only list markets they currently supply to).
182
161
  const liquidityAdapter = vault.accrualLiquidityAdapter;
183
162
  if (vault.liquidityData === "0x" ||
184
163
  !(liquidityAdapter instanceof AccrualVaultV2MorphoMarketV1Adapter ||
185
- liquidityAdapter instanceof AccrualVaultV2MorphoMarketV1AdapterV2))
164
+ liquidityAdapter instanceof AccrualVaultV2MorphoMarketV1AdapterV2)) {
186
165
  return;
166
+ }
187
167
  const liquidityMarketId = MarketParams.fromHex(vault.liquidityData).id;
188
168
  const markets = liquidityAdapter instanceof AccrualVaultV2MorphoMarketV1Adapter
189
169
  ? liquidityAdapter.positions.map(({ market }) => market)
190
170
  : liquidityAdapter.markets;
191
- if (!markets.some(({ id }) => id === liquidityMarketId))
171
+ if (!markets.some(({ id }) => id === liquidityMarketId)) {
192
172
  throw new MissingDeadSharesDataError({
193
173
  entity: [...entity, liquidityAdapter.address, liquidityMarketId],
194
174
  field: "liquidityMarket",
195
175
  });
176
+ }
177
+ }
178
+ /** Reads a fetched adapter cap before applying the dead-share rule. @internal */
179
+ function getDeadSharesAbsoluteCap(id, { absoluteCaps, entity, adapter, }) {
180
+ const cap = absoluteCaps[id];
181
+ if (cap == null) {
182
+ throw new MissingDeadSharesDataError({
183
+ entity: [...entity, adapter],
184
+ field: "absoluteCaps",
185
+ });
186
+ }
187
+ return cap;
188
+ }
189
+ /** Checks the currently allocated positions of one supported adapter. @internal */
190
+ function assertAdapterDeadShares(adapter, { absoluteCaps, entity, }) {
191
+ if (adapter instanceof AccrualVaultV2MorphoVaultV1Adapter) {
192
+ const id = VaultV2MorphoVaultV1Adapter.adapterCapId(adapter.address);
193
+ if (getDeadSharesAbsoluteCap(id, {
194
+ absoluteCaps,
195
+ entity,
196
+ adapter: adapter.address,
197
+ }) > 0n) {
198
+ validateVaultV1DeadShares(adapter.accrualVaultV1, entity);
199
+ }
200
+ return;
201
+ }
202
+ if (adapter instanceof AccrualVaultV2MorphoMarketV1Adapter) {
203
+ for (const { market } of adapter.positions) {
204
+ const id = VaultV2MorphoMarketV1Adapter.adapterMarketCapId(adapter.address, market.params);
205
+ if (getDeadSharesAbsoluteCap(id, {
206
+ absoluteCaps,
207
+ entity,
208
+ adapter: adapter.address,
209
+ }) > 0n) {
210
+ validateBlueMarketDeadShares(market, entity);
211
+ }
212
+ }
213
+ return;
214
+ }
215
+ if (adapter instanceof AccrualVaultV2MorphoMarketV1AdapterV2) {
216
+ for (const market of adapter.markets) {
217
+ const id = VaultV2MorphoMarketV1AdapterV2.adapterMarketCapId(adapter.address, market.params);
218
+ if (getDeadSharesAbsoluteCap(id, {
219
+ absoluteCaps,
220
+ entity,
221
+ adapter: adapter.address,
222
+ }) > 0n) {
223
+ validateBlueMarketDeadShares(market, entity);
224
+ }
225
+ }
226
+ return;
227
+ }
228
+ throw new MissingDeadSharesDataError({
229
+ entity: [...entity, adapter.address],
230
+ field: "adapter",
231
+ });
196
232
  }
@@ -24,8 +24,9 @@ export function encodeMidnightCalldata(parameters) {
24
24
  return encodeFunctionData(parameters);
25
25
  }
26
26
  catch (error) {
27
- if (!(error instanceof BaseError))
27
+ if (!(error instanceof BaseError)) {
28
28
  throw error;
29
+ }
29
30
  throw new MidnightCalldataEncodingError(String(parameters.functionName), error);
30
31
  }
31
32
  }
@@ -47,8 +48,9 @@ export function wrapMidnightEncoding(name, encode) {
47
48
  return encode();
48
49
  }
49
50
  catch (error) {
50
- if (!(error instanceof BaseError))
51
+ if (!(error instanceof BaseError)) {
51
52
  throw error;
53
+ }
52
54
  throw new MidnightCalldataEncodingError(name, error);
53
55
  }
54
56
  }
@@ -1,5 +1,13 @@
1
1
  import { type Address, type Hex } from "viem";
2
2
  import type { Metadata } from "../types/index.js";
3
+ /**
4
+ * Replaces a device-clock metadata timestamp with the flow's explicit reference time.
5
+ * Returns the input unchanged when no replacement is needed; never mutates it.
6
+ * @throws {NegativeInputError} when the resolved bigint timestamp is negative.
7
+ * @throws {InputExceedsMaxError} when the resolved bigint timestamp exceeds uint32 seconds.
8
+ * @internal
9
+ */
10
+ export declare function resolveTransactionMetadata(metadata: Metadata | undefined, now?: bigint): Metadata | undefined;
3
11
  /**
4
12
  * Adds metadata to a transaction object by concatenating additional
5
13
  * hex-encoded data to the transaction's `data` field. The additional
@@ -29,17 +37,21 @@ import type { Metadata } from "../types/index.js";
29
37
  * @returns The same `tx` object when `data` is missing, an empty string, or
30
38
  * canonical empty calldata (`"0x"`); otherwise, a new transaction object
31
39
  * with `data` containing any appended metadata. The input is never mutated.
40
+ * @throws {NegativeInputError} when an explicit bigint timestamp is negative.
41
+ * @throws {InputExceedsMaxError} when an explicit bigint timestamp exceeds uint32 seconds.
32
42
  * @example
33
43
  * ```ts
34
44
  * import { addTransactionMetadata } from "@morpho-org/morpho-sdk";
35
45
  * import { getChainAddresses } from "@morpho-org/morpho-sdk/addresses";
36
- * import type { Address, Hex } from "viem";
46
+ * import { type Address, createPublicClient, type Hex, http } from "viem";
37
47
  * import { mainnet } from "viem/chains";
38
48
  *
49
+ * const client = createPublicClient({ chain: mainnet, transport: http() });
39
50
  * const { blue } = getChainAddresses(mainnet.id);
40
51
  * const tx = { to: blue, value: 0n, data: "0x1234" as Hex };
52
+ * const { timestamp } = await client.getBlock();
41
53
  * const result: { to: Address; value: bigint; data: Hex } =
42
- * addTransactionMetadata(tx, { origin: "deadbeef", timestamp: true });
54
+ * addTransactionMetadata(tx, { origin: "deadbeef", timestamp });
43
55
  * // => { to: blue, value: 0n, data: "0x1234" + <4-byte timestamp> + "deadbeef" }
44
56
  * ```
45
57
  */
@@ -1,5 +1,36 @@
1
1
  import { Time } from "@morpho-org/morpho-ts";
2
2
  import { concatHex, isHex, numberToHex } from "viem";
3
+ import { InputExceedsMaxError, NegativeInputError } from "../types/error.js";
4
+ const MAX_METADATA_TIMESTAMP = (1n << 32n) - 1n;
5
+ function validateMetadataTimestamp(timestamp) {
6
+ if (typeof timestamp !== "bigint") {
7
+ return;
8
+ }
9
+ if (timestamp < 0n) {
10
+ throw new NegativeInputError("metadata.timestamp", timestamp);
11
+ }
12
+ if (timestamp > MAX_METADATA_TIMESTAMP) {
13
+ throw new InputExceedsMaxError({
14
+ field: "metadata.timestamp",
15
+ value: timestamp,
16
+ max: MAX_METADATA_TIMESTAMP,
17
+ });
18
+ }
19
+ }
20
+ /**
21
+ * Replaces a device-clock metadata timestamp with the flow's explicit reference time.
22
+ * Returns the input unchanged when no replacement is needed; never mutates it.
23
+ * @throws {NegativeInputError} when the resolved bigint timestamp is negative.
24
+ * @throws {InputExceedsMaxError} when the resolved bigint timestamp exceeds uint32 seconds.
25
+ * @internal
26
+ */
27
+ export function resolveTransactionMetadata(metadata, now) {
28
+ const resolved = metadata?.timestamp === true && now != null
29
+ ? { ...metadata, timestamp: now }
30
+ : metadata;
31
+ validateMetadataTimestamp(resolved?.timestamp);
32
+ return resolved;
33
+ }
3
34
  /**
4
35
  * Adds metadata to a transaction object by concatenating additional
5
36
  * hex-encoded data to the transaction's `data` field. The additional
@@ -29,27 +60,33 @@ import { concatHex, isHex, numberToHex } from "viem";
29
60
  * @returns The same `tx` object when `data` is missing, an empty string, or
30
61
  * canonical empty calldata (`"0x"`); otherwise, a new transaction object
31
62
  * with `data` containing any appended metadata. The input is never mutated.
63
+ * @throws {NegativeInputError} when an explicit bigint timestamp is negative.
64
+ * @throws {InputExceedsMaxError} when an explicit bigint timestamp exceeds uint32 seconds.
32
65
  * @example
33
66
  * ```ts
34
67
  * import { addTransactionMetadata } from "@morpho-org/morpho-sdk";
35
68
  * import { getChainAddresses } from "@morpho-org/morpho-sdk/addresses";
36
- * import type { Address, Hex } from "viem";
69
+ * import { type Address, createPublicClient, type Hex, http } from "viem";
37
70
  * import { mainnet } from "viem/chains";
38
71
  *
72
+ * const client = createPublicClient({ chain: mainnet, transport: http() });
39
73
  * const { blue } = getChainAddresses(mainnet.id);
40
74
  * const tx = { to: blue, value: 0n, data: "0x1234" as Hex };
75
+ * const { timestamp } = await client.getBlock();
41
76
  * const result: { to: Address; value: bigint; data: Hex } =
42
- * addTransactionMetadata(tx, { origin: "deadbeef", timestamp: true });
77
+ * addTransactionMetadata(tx, { origin: "deadbeef", timestamp });
43
78
  * // => { to: blue, value: 0n, data: "0x1234" + <4-byte timestamp> + "deadbeef" }
44
79
  * ```
45
80
  */
46
81
  export function addTransactionMetadata(tx, metadata) {
82
+ validateMetadataTimestamp(metadata.timestamp);
47
83
  const { data, ..._tx } = tx;
48
- if (!data || data === "0x")
84
+ if (!data || data === "0x") {
49
85
  return tx;
86
+ }
50
87
  const concatItems = [data];
51
- if (metadata.timestamp) {
52
- concatItems.push(numberToHex(Time.timestamp(), { size: 4 }));
88
+ if (metadata.timestamp === true || typeof metadata.timestamp === "bigint") {
89
+ concatItems.push(numberToHex(metadata.timestamp === true ? Time.timestamp() : metadata.timestamp, { size: 4 }));
53
90
  }
54
91
  try {
55
92
  // Strip a leading "0x" / "0X" case-insensitively so callers can pass
@@ -57,15 +94,18 @@ export function addTransactionMetadata(tx, metadata) {
57
94
  // origin. The case-insensitive form prevents "0Xcafe" from falling
58
95
  // through to `isHex("0x0Xcafe")` and being silently dropped.
59
96
  const origin = metadata.origin.replace(/^0x/i, "");
60
- if (!isHex(`0x${origin}`))
97
+ if (!isHex(`0x${origin}`)) {
61
98
  throw Error("Calldata origin must consists of only hex characters");
62
- if (origin.length > 8)
99
+ }
100
+ if (origin.length > 8) {
63
101
  throw Error("Calldata origin must be at most 8 characters long");
102
+ }
64
103
  // Reject odd-length raw fragments. viem's `concatHex` would pad the
65
104
  // stray nibble to a whole byte at broadcast, corrupting the trailing
66
105
  // analytics byte on-chain.
67
- if (origin.length % 2 !== 0)
106
+ if (origin.length % 2 !== 0) {
68
107
  throw Error("Calldata origin must have an even number of hex characters");
108
+ }
69
109
  concatItems.push(`0x${origin}`);
70
110
  }
71
111
  catch (error) {
@@ -56,20 +56,23 @@ import { computeVaultV2ForceWithdrawFeeSharesMinted, computeVaultV2ForceWithdraw
56
56
  */
57
57
  export function previewVaultV2ForceWithdraw(vaultData, params) {
58
58
  const { requestedExitAssets, timestamp, adapter: adapterOverride, userAddress, feeProjectionTimestamp = timestamp, referralFeePct = 0n, referralFeeRecipient, } = params;
59
- if (requestedExitAssets <= 0n)
59
+ if (requestedExitAssets <= 0n) {
60
60
  return undefined;
61
+ }
61
62
  // Out of the range the action accepts, `netAssets` would quote a payout the contract can never
62
63
  // deliver — above `withdrawnAssets` for a negative fee, non-positive at or beyond WAD.
63
- if (referralFeePct < 0n || referralFeePct >= MathLib.WAD)
64
+ if (referralFeePct < 0n || referralFeePct >= MathLib.WAD) {
64
65
  return undefined;
66
+ }
65
67
  if (referralFeePct > 0n &&
66
68
  (referralFeeRecipient == null ||
67
69
  isAddressEqual(referralFeeRecipient, zeroAddress))) {
68
70
  return undefined;
69
71
  }
70
72
  const eligibility = resolveVaultV2ForceWithdrawEligibility(vaultData, adapterOverride);
71
- if (eligibility.type !== "eligible")
73
+ if (eligibility.type !== "eligible") {
72
74
  return undefined;
75
+ }
73
76
  const capacity = computeVaultV2ForceWithdrawPlan({
74
77
  vaultData,
75
78
  adapter: eligibility.adapter,
@@ -80,8 +83,9 @@ export function previewVaultV2ForceWithdraw(vaultData, params) {
80
83
  const exitAssets = MathLib.min(requestedExitAssets, capacity.maxExitAssets);
81
84
  // A vault with no exitable capacity caps `maxExitAssets` to `0`; the planner rejects a
82
85
  // non-positive amount, so short-circuit to the "not previewable" verdict before re-planning.
83
- if (exitAssets <= 0n)
86
+ if (exitAssets <= 0n) {
84
87
  return undefined;
88
+ }
85
89
  // Re-plan at the capped amount so every returned leg describes the same exit.
86
90
  const plan = exitAssets === requestedExitAssets
87
91
  ? capacity
@@ -92,8 +96,42 @@ export function previewVaultV2ForceWithdraw(vaultData, params) {
92
96
  exitAssets,
93
97
  timestamp,
94
98
  });
95
- if (plan.withdrawnAssets <= 0n)
99
+ if (plan.withdrawnAssets <= 0n) {
96
100
  return undefined;
101
+ }
102
+ const defaultMinSharePriceE27 = computePreviewMinSharePrice(vaultData, {
103
+ timestamp,
104
+ feeProjectionTimestamp,
105
+ userAddress,
106
+ plan,
107
+ });
108
+ if (defaultMinSharePriceE27 == null ||
109
+ defaultMinSharePriceE27 <= 0n ||
110
+ defaultMinSharePriceE27 > maxUint256) {
111
+ return undefined;
112
+ }
113
+ const referralFeeAssets = MathLib.mulDivDown(plan.withdrawnAssets, referralFeePct, MathLib.WAD);
114
+ const selfPaidReferralFee = userAddress != null &&
115
+ referralFeeRecipient != null &&
116
+ isAddressEqual(userAddress, referralFeeRecipient);
117
+ return {
118
+ maxExitAssets: capacity.maxExitAssets,
119
+ exitAssets,
120
+ remainingExitAssets: requestedExitAssets - exitAssets,
121
+ assetsToWithdraw: plan.assetsToWithdraw,
122
+ assetsToDeallocate: plan.assetsToDeallocate,
123
+ // The tight per-leg charge `ceil(assetsToDeallocate × penalty)`, not `plan.penaltyAssets`: that
124
+ // bound carries a `+ (penaltyLegs - 1)` allowance slack, which would overstate the quote and
125
+ // break reconciliation with `exitAssets` for a multi-market exit.
126
+ penaltyAssets: MathLib.wMulUp(plan.assetsToDeallocate, plan.penalty),
127
+ referralFeeAssets,
128
+ netAssets: selfPaidReferralFee
129
+ ? plan.withdrawnAssets
130
+ : plan.withdrawnAssets - referralFeeAssets,
131
+ };
132
+ }
133
+ /** Computes the preview's fee-adjusted floor, excluding fee mints that consume the burn. @internal */
134
+ function computePreviewMinSharePrice(vaultData, { timestamp, feeProjectionTimestamp, userAddress, plan, }) {
97
135
  // Mirror the entity's zero-floor guard. `forceWithdraw()` derives its slippage floor from
98
136
  // `withdrawnAssets / sharesBurnt` and rejects an exit whose realized share price rounds down to
99
137
  // zero (`VaultV2ForceWithdrawZeroSharePriceError`) — a degenerate vault (e.g. `totalSupply`
@@ -146,26 +184,5 @@ export function previewVaultV2ForceWithdraw(vaultData, params) {
146
184
  const defaultMinSharePriceE27 = sharesBurntForFloor > 0n
147
185
  ? MathLib.mulDivDown(plan.withdrawnAssets, MathLib.wToRay(MathLib.WAD - DEFAULT_SLIPPAGE_TOLERANCE), sharesBurntForFloor)
148
186
  : 0n;
149
- if (defaultMinSharePriceE27 <= 0n || defaultMinSharePriceE27 > maxUint256) {
150
- return undefined;
151
- }
152
- const referralFeeAssets = MathLib.mulDivDown(plan.withdrawnAssets, referralFeePct, MathLib.WAD);
153
- const selfPaidReferralFee = userAddress != null &&
154
- referralFeeRecipient != null &&
155
- isAddressEqual(userAddress, referralFeeRecipient);
156
- return {
157
- maxExitAssets: capacity.maxExitAssets,
158
- exitAssets,
159
- remainingExitAssets: requestedExitAssets - exitAssets,
160
- assetsToWithdraw: plan.assetsToWithdraw,
161
- assetsToDeallocate: plan.assetsToDeallocate,
162
- // The tight per-leg charge `ceil(assetsToDeallocate × penalty)`, not `plan.penaltyAssets`: that
163
- // bound carries a `+ (penaltyLegs - 1)` allowance slack, which would overstate the quote and
164
- // break reconciliation with `exitAssets` for a multi-market exit.
165
- penaltyAssets: MathLib.wMulUp(plan.assetsToDeallocate, plan.penalty),
166
- referralFeeAssets,
167
- netAssets: selfPaidReferralFee
168
- ? plan.withdrawnAssets
169
- : plan.withdrawnAssets - referralFeeAssets,
170
- };
187
+ return defaultMinSharePriceE27;
171
188
  }
@@ -46,7 +46,15 @@ export function previewVaultV2InKindRedeem(vaultData, params) {
46
46
  .toSupplyAssets(adapter.supplyShares[market.id] ?? 0n),
47
47
  }))
48
48
  .filter(({ allocationAssets }) => allocationAssets > 0n)
49
- .toSorted(({ allocationAssets: a }, { allocationAssets: b }) => a === b ? 0 : a > b ? -1 : 1);
49
+ .toSorted(({ allocationAssets: a }, { allocationAssets: b }) => {
50
+ if (a === b) {
51
+ return 0;
52
+ }
53
+ if (a > b) {
54
+ return -1;
55
+ }
56
+ return 1;
57
+ });
50
58
  return allocations.flatMap(({ market, allocationAssets }) => {
51
59
  const maxExitAssets = availableIdleAssets +
52
60
  MathLib.wMulUp(allocationAssets + 1n, MathLib.WAD + penalty) -
@@ -55,8 +63,9 @@ export function previewVaultV2InKindRedeem(vaultData, params) {
55
63
  const idleAssets = MathLib.min(exitAssets, availableIdleAssets);
56
64
  const inKindExitAssets = exitAssets - idleAssets;
57
65
  const netAssets = MathLib.wDivDown(inKindExitAssets, MathLib.WAD + penalty);
58
- if (idleAssets === 0n && netAssets === 0n)
66
+ if (idleAssets === 0n && netAssets === 0n) {
59
67
  return [];
68
+ }
60
69
  return [
61
70
  {
62
71
  marketParams: market.params,
@@ -14,8 +14,9 @@ import { DEFAULT_ALLOCATOR_CAP_HEADROOM, DEFAULT_CAP_ACCRUAL_BUFFER, DEFAULT_WIT
14
14
  */
15
15
  export const resolveCapAccrualBuffer = (value) => {
16
16
  const buffer = value == null ? DEFAULT_CAP_ACCRUAL_BUFFER : BigInt(value);
17
- if (buffer < 0n)
17
+ if (buffer < 0n) {
18
18
  throw new NegativeInputError("capAccrualBuffer", buffer);
19
+ }
19
20
  return buffer;
20
21
  };
21
22
  /**
@@ -32,14 +33,16 @@ export const resolveCapAccrualBuffer = (value) => {
32
33
  */
33
34
  export const resolveAllocatorCapHeadroom = (value) => {
34
35
  const headroom = value ?? DEFAULT_ALLOCATOR_CAP_HEADROOM;
35
- if (headroom < 0n)
36
+ if (headroom < 0n) {
36
37
  throw new NegativeInputError("allocatorCapHeadroom", headroom);
37
- if (headroom > MathLib.WAD)
38
+ }
39
+ if (headroom > MathLib.WAD) {
38
40
  throw new InputExceedsMaxError({
39
41
  field: "allocatorCapHeadroom",
40
42
  value: headroom,
41
43
  max: MathLib.WAD,
42
44
  });
45
+ }
43
46
  return headroom;
44
47
  };
45
48
  /**
@@ -57,13 +60,15 @@ export const resolveAllocatorCapHeadroom = (value) => {
57
60
  */
58
61
  export const resolveMaxWithdrawalUtilization = (value, field = "maxWithdrawalUtilization") => {
59
62
  const utilization = value ?? DEFAULT_WITHDRAWAL_TARGET_UTILIZATION;
60
- if (utilization < 0n)
63
+ if (utilization < 0n) {
61
64
  throw new NegativeInputError(field, utilization);
62
- if (utilization > MathLib.WAD)
65
+ }
66
+ if (utilization > MathLib.WAD) {
63
67
  throw new InputExceedsMaxError({
64
68
  field,
65
69
  value: utilization,
66
70
  max: MathLib.WAD,
67
71
  });
72
+ }
68
73
  return utilization;
69
74
  };