@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
@@ -13,10 +13,12 @@ const validateMidnightAccount_js_1 = require("./validateMidnightAccount.js");
13
13
  const compareMarketIds = (idA, idB) => {
14
14
  const normalizedIdA = idA.toLowerCase();
15
15
  const normalizedIdB = idB.toLowerCase();
16
- if (normalizedIdA > normalizedIdB)
16
+ if (normalizedIdA > normalizedIdB) {
17
17
  return 1;
18
- if (normalizedIdA < normalizedIdB)
18
+ }
19
+ if (normalizedIdA < normalizedIdB) {
19
20
  return -1;
21
+ }
20
22
  return 0;
21
23
  };
22
24
  /**
@@ -32,8 +34,9 @@ const compareMarketIds = (idA, idB) => {
32
34
  * @internal
33
35
  */
34
36
  const validateUint256Field = (field, value) => {
35
- if (value < 0n)
37
+ if (value < 0n) {
36
38
  throw new index_js_1.NegativeInputError(field, value);
39
+ }
37
40
  if (value > viem_1.maxUint256) {
38
41
  throw new index_js_1.InputExceedsMaxError({ field, value, max: viem_1.maxUint256 });
39
42
  }
@@ -67,8 +70,9 @@ exports.validateMarketParamsId = validateMarketParamsId;
67
70
  * @internal
68
71
  */
69
72
  const validateDeadline = (deadline) => {
70
- if (deadline <= 0n)
73
+ if (deadline <= 0n) {
71
74
  throw new index_js_1.NonPositiveInputError("deadline", deadline);
75
+ }
72
76
  (0, exports.validateUint256Field)("deadline", deadline);
73
77
  };
74
78
  exports.validateDeadline = validateDeadline;
@@ -261,8 +265,9 @@ const getMidnightMaxSafeDebt = (params) => {
261
265
  let maxSafeDebt = 0n;
262
266
  for (let index = 0; index < params.collateral.length; index += 1) {
263
267
  const amount = params.collateral[index] ?? 0n;
264
- if (amount === 0n)
268
+ if (amount === 0n) {
265
269
  continue;
270
+ }
266
271
  const collateral = midnight_sdk_1.MarketUtils.getCollateralByIndex(params.market.params, BigInt(index));
267
272
  const price = params.collateralPrices[index];
268
273
  if (price === undefined) {
@@ -428,8 +433,9 @@ const validateMidnightNativeAmount = (params) => {
428
433
  throw new index_js_1.ChainWNativeMissingError(chainId);
429
434
  }
430
435
  // Reject a malformed address before viem compares it with wNative.
431
- for (const pull of pulls)
436
+ for (const pull of pulls) {
432
437
  (0, validateMidnightAccount_js_1.validateMidnightAddress)("token", pull.token);
438
+ }
433
439
  const wNativePulled = pulls.reduce((total, pull) => (0, viem_1.isAddressEqual)(pull.token, wNative) ? total + pull.assets : total, 0n);
434
440
  const firstPull = pulls[0];
435
441
  if (firstPull && !pulls.some((pull) => (0, viem_1.isAddressEqual)(pull.token, wNative))) {
@@ -597,67 +603,11 @@ exports.validateRepayShares = validateRepayShares;
597
603
  const validateVaultV2BlueReallocations = (reallocations, targetMarketId) => {
598
604
  const penaltyByVault = new Map();
599
605
  for (const reallocation of reallocations) {
600
- // Reject non-object entries and Vault V1-shaped entries (which carry
601
- // `withdrawals`/`fee`) with a typed, instructive error instead of a raw
602
- // TypeError or a misleading `to.adapter` address error.
603
- if (typeof reallocation !== "object" ||
604
- reallocation === null ||
605
- "withdrawals" in reallocation ||
606
- "fee" in reallocation) {
607
- throw new index_js_1.InvalidVaultV2BlueReallocationShapeError();
608
- }
609
- if (typeof reallocation.vault !== "string" ||
610
- !(0, viem_1.isAddress)(reallocation.vault)) {
611
- throw new index_js_1.InvalidReallocationAddressError("vault");
612
- }
613
- if (reallocation.to == null ||
614
- typeof reallocation.to.adapter !== "string" ||
615
- !(0, viem_1.isAddress)(reallocation.to.adapter)) {
616
- throw new index_js_1.InvalidReallocationAddressError("to.adapter");
617
- }
618
- const source = reallocation.from;
619
- if (source == null) {
620
- throw new index_js_1.InvalidReallocationSourceTypeError(undefined);
621
- }
622
- const sourceType = source.type;
623
- if (sourceType !== "market" && sourceType !== "idle") {
624
- throw new index_js_1.InvalidReallocationSourceTypeError(sourceType);
625
- }
626
- let sourceMarketId;
627
- if (source.type === "market") {
628
- if (typeof source.adapter !== "string" || !(0, viem_1.isAddress)(source.adapter)) {
629
- throw new index_js_1.InvalidReallocationAddressError("from.adapter");
630
- }
631
- if (source.marketParams == null ||
632
- !(0, viem_1.isAddress)(source.marketParams.loanToken) ||
633
- !(0, viem_1.isAddress)(source.marketParams.collateralToken) ||
634
- !(0, viem_1.isAddress)(source.marketParams.oracle) ||
635
- !(0, viem_1.isAddress)(source.marketParams.irm) ||
636
- typeof source.marketParams.lltv !== "bigint") {
637
- throw new index_js_1.InvalidReallocationSourceTypeError("market", "marketParams");
638
- }
639
- sourceMarketId = blue_sdk_1.MarketUtils.getMarketId(source.marketParams);
640
- }
641
- if (reallocation.penalty < 0n) {
642
- throw new index_js_1.NegativeInputError("reallocation.penalty", reallocation.penalty);
643
- }
644
- if (reallocation.penalty > constant_js_1.MAX_REALLOCATION_PENALTY) {
645
- throw new index_js_1.InputExceedsMaxError({
646
- field: "reallocation.penalty",
647
- value: reallocation.penalty,
648
- max: constant_js_1.MAX_REALLOCATION_PENALTY,
649
- });
650
- }
651
- if (reallocation.assets <= 0n) {
652
- throw new index_js_1.NonPositiveInputError("reallocation.assets", reallocation.assets);
653
- }
654
- if (reallocation.assets > viem_1.maxUint128) {
655
- throw new index_js_1.InputExceedsMaxError({
656
- field: "reallocation.assets",
657
- value: reallocation.assets,
658
- max: viem_1.maxUint128,
659
- });
660
- }
606
+ // Validate shape before source and amounts so malformed entries keep their original error.
607
+ assertReallocationShape(reallocation);
608
+ const sourceMarketId = resolveReallocationSourceMarketId(reallocation);
609
+ // Reject out-of-range amounts before comparing penalties across vault legs.
610
+ assertReallocationAmounts(reallocation);
661
611
  const penaltyKey = reallocation.vault.toLowerCase();
662
612
  const expectedPenalty = penaltyByVault.get(penaltyKey);
663
613
  if (expectedPenalty !== undefined &&
@@ -772,3 +722,74 @@ const validateWithdrawShares = (params) => {
772
722
  }
773
723
  };
774
724
  exports.validateWithdrawShares = validateWithdrawShares;
725
+ /** Rejects malformed reallocation entries before reading source or amount fields. @internal */
726
+ function assertReallocationShape(reallocation) {
727
+ // Reject non-object entries and Vault V1-shaped entries (which carry
728
+ // `withdrawals`/`fee`) with a typed, instructive error instead of a raw
729
+ // TypeError or a misleading `to.adapter` address error.
730
+ if (typeof reallocation !== "object" ||
731
+ reallocation === null ||
732
+ "withdrawals" in reallocation ||
733
+ "fee" in reallocation) {
734
+ throw new index_js_1.InvalidVaultV2BlueReallocationShapeError();
735
+ }
736
+ if (typeof reallocation.vault !== "string" ||
737
+ !(0, viem_1.isAddress)(reallocation.vault)) {
738
+ throw new index_js_1.InvalidReallocationAddressError("vault");
739
+ }
740
+ if (reallocation.to == null ||
741
+ typeof reallocation.to.adapter !== "string" ||
742
+ !(0, viem_1.isAddress)(reallocation.to.adapter)) {
743
+ throw new index_js_1.InvalidReallocationAddressError("to.adapter");
744
+ }
745
+ }
746
+ /** Validates the reallocation source and resolves its market identifier. @internal */
747
+ function resolveReallocationSourceMarketId(reallocation) {
748
+ const source = reallocation.from;
749
+ if (source == null) {
750
+ throw new index_js_1.InvalidReallocationSourceTypeError(undefined);
751
+ }
752
+ const sourceType = source.type;
753
+ if (sourceType !== "market" && sourceType !== "idle") {
754
+ throw new index_js_1.InvalidReallocationSourceTypeError(sourceType);
755
+ }
756
+ let sourceMarketId;
757
+ if (source.type === "market") {
758
+ if (typeof source.adapter !== "string" || !(0, viem_1.isAddress)(source.adapter)) {
759
+ throw new index_js_1.InvalidReallocationAddressError("from.adapter");
760
+ }
761
+ if (source.marketParams == null ||
762
+ !(0, viem_1.isAddress)(source.marketParams.loanToken) ||
763
+ !(0, viem_1.isAddress)(source.marketParams.collateralToken) ||
764
+ !(0, viem_1.isAddress)(source.marketParams.oracle) ||
765
+ !(0, viem_1.isAddress)(source.marketParams.irm) ||
766
+ typeof source.marketParams.lltv !== "bigint") {
767
+ throw new index_js_1.InvalidReallocationSourceTypeError("market", "marketParams");
768
+ }
769
+ sourceMarketId = blue_sdk_1.MarketUtils.getMarketId(source.marketParams);
770
+ }
771
+ return sourceMarketId;
772
+ }
773
+ /** Checks penalty and asset amounts against the allocator's ABI bounds. @internal */
774
+ function assertReallocationAmounts(reallocation) {
775
+ if (reallocation.penalty < 0n) {
776
+ throw new index_js_1.NegativeInputError("reallocation.penalty", reallocation.penalty);
777
+ }
778
+ if (reallocation.penalty > constant_js_1.MAX_REALLOCATION_PENALTY) {
779
+ throw new index_js_1.InputExceedsMaxError({
780
+ field: "reallocation.penalty",
781
+ value: reallocation.penalty,
782
+ max: constant_js_1.MAX_REALLOCATION_PENALTY,
783
+ });
784
+ }
785
+ if (reallocation.assets <= 0n) {
786
+ throw new index_js_1.NonPositiveInputError("reallocation.assets", reallocation.assets);
787
+ }
788
+ if (reallocation.assets > viem_1.maxUint128) {
789
+ throw new index_js_1.InputExceedsMaxError({
790
+ field: "reallocation.assets",
791
+ value: reallocation.assets,
792
+ max: viem_1.maxUint128,
793
+ });
794
+ }
795
+ }
@@ -15,8 +15,9 @@ const constant_js_1 = require("./constant.js");
15
15
  * @internal
16
16
  */
17
17
  const validateMidnightUint128Field = (field, value) => {
18
- if (value < 0n)
18
+ if (value < 0n) {
19
19
  throw new error_js_1.NegativeInputError(field, value);
20
+ }
20
21
  if (value > viem_1.maxUint128) {
21
22
  throw new error_js_1.InputExceedsMaxError({ field, value, max: viem_1.maxUint128 });
22
23
  }
@@ -16,8 +16,9 @@ const error_js_1 = require("../types/error.js");
16
16
  * ```
17
17
  */
18
18
  function validateMidnightCollateralBatch(transfers, emptyError) {
19
- if (transfers.length === 0)
19
+ if (transfers.length === 0) {
20
20
  throw emptyError;
21
+ }
21
22
  const indexes = new Set();
22
23
  for (const { collateralIndex } of transfers) {
23
24
  if (indexes.has(collateralIndex)) {
@@ -84,8 +84,9 @@ const validateMidnightPublication = (params) => {
84
84
  });
85
85
  }
86
86
  const publication = params.publication;
87
- if (publication == null)
87
+ if (publication == null) {
88
88
  return;
89
+ }
89
90
  validateHexInput(publication.root, "publication.root", { exactBytes: 32 });
90
91
  for (const [index, group] of publication.groups.entries()) {
91
92
  validateHexInput(group, `publication.groups[${index}]`, {
@@ -103,8 +104,9 @@ const validateMidnightPublication = (params) => {
103
104
  exactBytes: 32,
104
105
  });
105
106
  }
106
- if (publication.payload === "0x")
107
+ if (publication.payload === "0x") {
107
108
  return;
109
+ }
108
110
  validateHexInput(publication.payload, "publication.payload", {
109
111
  maxBytes: midnight_sdk_1.Payload.MAX_PAYLOAD_BYTES,
110
112
  });
@@ -78,42 +78,18 @@ exports.validateMidnightSellTargetBounds = validateMidnightSellTargetBounds;
78
78
  */
79
79
  const validateMidnightTakeFeasibility = (params) => {
80
80
  // The raw builder rejects an empty list with `EmptyMidnightTakeableOffersError`.
81
- if (params.takeableOffers.length === 0)
81
+ if (params.takeableOffers.length === 0) {
82
82
  return params.takeableOffers;
83
- const { marketData, now, deadline, referralFeePct, target } = params;
83
+ }
84
+ const { marketData, now, deadline } = params;
84
85
  // The entity checked the deadline on an earlier clock read; recheck so the fee window is never empty.
85
- if (deadline <= now)
86
+ if (deadline <= now) {
86
87
  throw new index_js_1.ExpiredDeadlineError(deadline, now);
88
+ }
87
89
  const maturity = marketData.params.maturity;
88
90
  const marketFee = BigInt(marketData.continuousFee);
89
- if (params.side === "buy") {
90
- if (params.maxContinuousFee < marketFee) {
91
- throw new index_js_1.MidnightContinuousFeeAboveMaxError({
92
- maxContinuousFee: params.maxContinuousFee,
93
- continuousFee: marketFee,
94
- });
95
- }
96
- const { debt, target: buyTarget } = params;
97
- if (debt !== undefined) {
98
- // Reduce-only buys fill at most the debt, and an assets target pays at most one asset per unit.
99
- const repay = buyTarget.type === "units"
100
- ? buyTarget.units === viem_1.maxUint256
101
- ? undefined
102
- : { field: "target.units", value: buyTarget.units }
103
- : buyTarget.minUnits > debt
104
- ? {
105
- field: "target.minUnits",
106
- value: buyTarget.minUnits,
107
- }
108
- : {
109
- field: "target.assets",
110
- value: netOfReferralFee(buyTarget.assets, referralFeePct),
111
- };
112
- if (repay && repay.value > debt) {
113
- throw new index_js_1.MidnightRepayExceedsDebtError({ ...repay, debt });
114
- }
115
- }
116
- }
91
+ // Reject buy fee and debt violations before evaluating settlement fees or offers.
92
+ assertMidnightBuyBounds(params, marketFee);
117
93
  const fees = [
118
94
  maturity - deadline,
119
95
  maturity - now,
@@ -126,62 +102,163 @@ const validateMidnightTakeFeasibility = (params) => {
126
102
  }));
127
103
  const minFee = blue_sdk_1.MathLib.min(...fees);
128
104
  const maxFee = blue_sdk_1.MathLib.max(...fees);
129
- const dead = [];
130
- const removed = new Set();
131
105
  // Post-maturity sells cannot open debt, so offers only take the credit left after the withdrawal.
132
106
  const sellableCredit = params.side === "sell" && params.increasesDebt && now > maturity
133
107
  ? blue_sdk_1.MathLib.zeroFloorSub(params.credit ?? 0n, params.fallbackUnits)
134
108
  : undefined;
109
+ const { dead, removed, liveUnits, minBuyerPrice } = collectOfferCapacity(params, {
110
+ minFee,
111
+ maxFee,
112
+ marketFee,
113
+ sellableCredit,
114
+ });
115
+ if (dead.length === params.takeableOffers.length &&
116
+ (params.fallbackUnits === 0n || removed.size === dead.length)) {
117
+ throw new index_js_1.MidnightTakeableOffersNotFillableError(dead);
118
+ }
119
+ const requiredUnits = resolveRequiredUnits(params);
120
+ const fillableUnits = (sellableCredit === undefined
121
+ ? liveUnits
122
+ : blue_sdk_1.MathLib.min(liveUnits, sellableCredit)) + params.fallbackUnits;
123
+ if (fillableUnits < requiredUnits) {
124
+ throw new index_js_1.MidnightTakeTargetUnreachableError({
125
+ field: "takeableOffers",
126
+ value: fillableUnits,
127
+ limit: requiredUnits,
128
+ });
129
+ }
130
+ // Capacity errors precede the buyer's maximum-pull bound.
131
+ assertBuyerAssetCapacity(params, { requiredUnits, minBuyerPrice });
132
+ return removed.size === 0
133
+ ? params.takeableOffers
134
+ : params.takeableOffers.filter((_, index) => !removed.has(index));
135
+ };
136
+ exports.validateMidnightTakeFeasibility = validateMidnightTakeFeasibility;
137
+ /** Resolves a reduce-only buy's repay bound without changing target precedence. @internal */
138
+ function resolveRepayTarget(target, { debt, referralFeePct, }) {
139
+ if (target.type === "units") {
140
+ if (target.units === viem_1.maxUint256) {
141
+ return undefined;
142
+ }
143
+ return { field: "target.units", value: target.units };
144
+ }
145
+ if (target.minUnits > debt) {
146
+ return { field: "target.minUnits", value: target.minUnits };
147
+ }
148
+ return {
149
+ field: "target.assets",
150
+ value: netOfReferralFee(target.assets, referralFeePct),
151
+ };
152
+ }
153
+ /** Rejects excessive continuous fees and reduce-only buys exceeding debt. @internal */
154
+ function assertMidnightBuyBounds(params, marketFee) {
155
+ if (params.side !== "buy") {
156
+ return;
157
+ }
158
+ if (params.maxContinuousFee < marketFee) {
159
+ throw new index_js_1.MidnightContinuousFeeAboveMaxError({
160
+ maxContinuousFee: params.maxContinuousFee,
161
+ continuousFee: marketFee,
162
+ });
163
+ }
164
+ const { debt, target: buyTarget } = params;
165
+ if (debt !== undefined) {
166
+ // Reduce-only buys fill at most the debt, and an assets target pays at most one asset per unit.
167
+ const repay = resolveRepayTarget(buyTarget, {
168
+ debt,
169
+ referralFeePct: params.referralFeePct,
170
+ });
171
+ if (repay && repay.value > debt) {
172
+ throw new index_js_1.MidnightRepayExceedsDebtError({ ...repay, debt });
173
+ }
174
+ }
175
+ }
176
+ /** Resolves price failures that abort bundle conversion outside the offer catch. @internal */
177
+ function getOfferPriceReason(params, { tickInRange, offerPrice, minFee, maxFee, }) {
178
+ // The bundle converts every buy assets target price outside its per-offer catch.
179
+ if (params.side === "buy" && params.target.type === "assets") {
180
+ if (!tickInRange) {
181
+ return "tickOutOfRange";
182
+ }
183
+ if (offerPrice + maxFee === 0n) {
184
+ return "zeroBuyerPrice";
185
+ }
186
+ if (offerPrice + minFee > blue_sdk_1.MathLib.WAD) {
187
+ return "buyerPriceAboveOne";
188
+ }
189
+ return undefined;
190
+ }
191
+ // Every sell converts a buy offer's price outside the catch too.
192
+ if (params.side !== "sell") {
193
+ return undefined;
194
+ }
195
+ if (!tickInRange) {
196
+ return "tickOutOfRange";
197
+ }
198
+ if (offerPrice === 0n) {
199
+ return "zeroBuyerPrice";
200
+ }
201
+ // An assets target also divides by the seller price, which is zero at the fee.
202
+ if (offerPrice < minFee ||
203
+ (params.target.type === "assets" && offerPrice === minFee)) {
204
+ return "priceBelowSettlementFee";
205
+ }
206
+ return undefined;
207
+ }
208
+ /** Finds the first non-price condition preventing an offer from filling. @internal */
209
+ function getOfferDeadReason({ units, offer }, { params, tickInRange, marketFee, sellableCredit, }) {
210
+ if (units === 0n) {
211
+ return "zeroUnits";
212
+ }
213
+ if (!tickInRange) {
214
+ return "tickOutOfRange";
215
+ }
216
+ if (offer.expiry < params.now) {
217
+ return "expired";
218
+ }
219
+ if (offer.start > params.deadline) {
220
+ return "notStarted";
221
+ }
222
+ if ((0, viem_1.isAddressEqual)(offer.maker, params.accountAddress)) {
223
+ return "selfTake";
224
+ }
225
+ if (offer.continuousFeeCap < marketFee) {
226
+ return "continuousFeeCapBelowMarketFee";
227
+ }
228
+ if (params.side === "sell" &&
229
+ !(0, viem_1.isAddressEqual)(offer.receiverIfMakerIsSeller, viem_1.zeroAddress)) {
230
+ return "receiverIfMakerIsSellerNotZero";
231
+ }
232
+ if (sellableCredit === 0n) {
233
+ return "marketMatured";
234
+ }
235
+ return undefined;
236
+ }
237
+ /** Accumulates live offer capacity and preserves removal and error order. @internal */
238
+ function collectOfferCapacity(params, { minFee, maxFee, marketFee, sellableCredit, }) {
239
+ const dead = [];
240
+ const removed = new Set();
135
241
  let liveUnits = 0n;
136
242
  let minBuyerPrice = params.fallbackUnits > 0n ? blue_sdk_1.MathLib.WAD : viem_1.maxUint256;
137
243
  for (const [index, { units, offer }] of params.takeableOffers.entries()) {
138
244
  const tickInRange = offer.tick >= 0n && offer.tick <= midnight_sdk_1.MAX_TICK;
139
245
  const offerPrice = tickInRange ? midnight_sdk_1.TickLib.tickToPrice(offer.tick) : 0n;
140
- let reason;
141
- // The bundle converts every buy assets target price outside its per-offer catch,
142
- // so a degenerate price aborts the bundle even when the offer is otherwise dead.
143
- if (params.side === "buy" && target.type === "assets") {
144
- if (!tickInRange)
145
- reason = "tickOutOfRange";
146
- else if (offerPrice + maxFee === 0n)
147
- reason = "zeroBuyerPrice";
148
- else if (offerPrice + minFee > blue_sdk_1.MathLib.WAD)
149
- reason = "buyerPriceAboveOne";
150
- if (reason)
151
- removed.add(index);
152
- }
153
- // Every sell converts a buy offer's price outside the catch too, which underflows below the fee.
154
- if (params.side === "sell") {
155
- if (!tickInRange)
156
- reason = "tickOutOfRange";
157
- else if (offerPrice === 0n)
158
- reason = "zeroBuyerPrice";
159
- // An assets target also divides by the seller price, which is zero at the fee.
160
- else if (offerPrice < minFee ||
161
- (target.type === "assets" && offerPrice === minFee))
162
- reason = "priceBelowSettlementFee";
163
- if (reason)
164
- removed.add(index);
165
- }
166
- if (!reason) {
167
- if (units === 0n)
168
- reason = "zeroUnits";
169
- else if (!tickInRange)
170
- reason = "tickOutOfRange";
171
- else if (offer.expiry < now)
172
- reason = "expired";
173
- else if (offer.start > deadline)
174
- reason = "notStarted";
175
- else if ((0, viem_1.isAddressEqual)(offer.maker, params.accountAddress))
176
- reason = "selfTake";
177
- else if (offer.continuousFeeCap < marketFee)
178
- reason = "continuousFeeCapBelowMarketFee";
179
- else if (params.side === "sell" &&
180
- !(0, viem_1.isAddressEqual)(offer.receiverIfMakerIsSeller, viem_1.zeroAddress))
181
- reason = "receiverIfMakerIsSellerNotZero";
182
- else if (sellableCredit === 0n)
183
- reason = "marketMatured";
246
+ const priceReason = getOfferPriceReason(params, {
247
+ tickInRange,
248
+ offerPrice,
249
+ minFee,
250
+ maxFee,
251
+ });
252
+ if (priceReason) {
253
+ removed.add(index);
184
254
  }
255
+ const reason = priceReason ??
256
+ getOfferDeadReason({ units, offer }, {
257
+ params,
258
+ tickInRange,
259
+ marketFee,
260
+ sellableCredit,
261
+ });
185
262
  if (reason) {
186
263
  dead.push({ index, reason });
187
264
  continue;
@@ -189,33 +266,33 @@ const validateMidnightTakeFeasibility = (params) => {
189
266
  liveUnits += units;
190
267
  minBuyerPrice = blue_sdk_1.MathLib.min(minBuyerPrice, offerPrice + minFee);
191
268
  }
192
- if (dead.length === params.takeableOffers.length &&
193
- (params.fallbackUnits === 0n || removed.size === dead.length)) {
194
- throw new index_js_1.MidnightTakeableOffersNotFillableError(dead);
269
+ return { dead, removed, liveUnits, minBuyerPrice };
270
+ }
271
+ /** Converts the taker's target into its required gross unit capacity. @internal */
272
+ function resolveRequiredUnits(params) {
273
+ const { target, referralFeePct } = params;
274
+ if (params.side === "sell") {
275
+ if (target.type === "units") {
276
+ return target.units;
277
+ }
278
+ return (0, referralFee_js_1.grossFromNetAssets)({ netAssets: target.assets, referralFeePct });
195
279
  }
196
- const requiredUnits = params.side === "sell"
197
- ? target.type === "units"
198
- ? target.units
199
- : (0, referralFee_js_1.grossFromNetAssets)({ netAssets: target.assets, referralFeePct })
200
- : params.target.type === "units"
201
- ? params.target.units === viem_1.maxUint256
202
- ? (params.debt ?? 0n)
203
- : params.target.units
204
- : blue_sdk_1.MathLib.max(netOfReferralFee(params.target.assets, referralFeePct), params.target.minUnits);
205
- const fillableUnits = (sellableCredit === undefined
206
- ? liveUnits
207
- : blue_sdk_1.MathLib.min(liveUnits, sellableCredit)) + params.fallbackUnits;
208
- if (fillableUnits < requiredUnits) {
209
- throw new index_js_1.MidnightTakeTargetUnreachableError({
210
- field: "takeableOffers",
211
- value: fillableUnits,
212
- limit: requiredUnits,
213
- });
280
+ if (params.target.type === "units") {
281
+ return params.target.units === viem_1.maxUint256
282
+ ? (params.debt ?? 0n)
283
+ : params.target.units;
214
284
  }
285
+ return blue_sdk_1.MathLib.max(netOfReferralFee(params.target.assets, referralFeePct), params.target.minUnits);
286
+ }
287
+ /** Rejects a units buy whose cheapest possible asset pull exceeds its cap. @internal */
288
+ function assertBuyerAssetCapacity(params, { requiredUnits, minBuyerPrice, }) {
215
289
  if (params.side === "buy" && params.target.type === "units") {
216
290
  // Every unit costs at least the cheapest live price, and the fee is grossed up on top.
217
291
  const minCost = blue_sdk_1.MathLib.mulDivDown(requiredUnits, minBuyerPrice, blue_sdk_1.MathLib.WAD);
218
- const minPull = (0, referralFee_js_1.grossFromNetAssets)({ netAssets: minCost, referralFeePct });
292
+ const minPull = (0, referralFee_js_1.grossFromNetAssets)({
293
+ netAssets: minCost,
294
+ referralFeePct: params.referralFeePct,
295
+ });
219
296
  if (params.target.maxBuyerAssets < minPull) {
220
297
  throw new index_js_1.MidnightTakeTargetUnreachableError({
221
298
  field: "target.maxBuyerAssets",
@@ -224,8 +301,4 @@ const validateMidnightTakeFeasibility = (params) => {
224
301
  });
225
302
  }
226
303
  }
227
- return removed.size === 0
228
- ? params.takeableOffers
229
- : params.takeableOffers.filter((_, index) => !removed.has(index));
230
- };
231
- exports.validateMidnightTakeFeasibility = validateMidnightTakeFeasibility;
304
+ }
@@ -12,7 +12,8 @@ const error_js_1 = require("../types/error.js");
12
12
  * @internal
13
13
  */
14
14
  const validatePositiveInput = (field, value) => {
15
- if (value <= 0n)
15
+ if (value <= 0n) {
16
16
  throw new error_js_1.NonPositiveInputError(field, value);
17
+ }
17
18
  };
18
19
  exports.validatePositiveInput = validatePositiveInput;
@@ -34,8 +34,9 @@ const validateTakeableOffers = (params) => {
34
34
  actualMarket: actualMarketId,
35
35
  });
36
36
  }
37
- if (take.offer.buy)
37
+ if (take.offer.buy) {
38
38
  continue;
39
+ }
39
40
  // Reject a malformed address before viem compares it.
40
41
  (0, validateMidnightAccount_js_1.validateMidnightAddress)(`takeableOffers[${index}].offer.receiverIfMakerIsSeller`, take.offer.receiverIfMakerIsSeller);
41
42
  if ((0, viem_1.isAddressEqual)(take.offer.receiverIfMakerIsSeller, viem_1.zeroAddress)) {