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