@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,6 +13,7 @@ const callback_js_1 = require("../../helpers/callback.js");
13
13
  const constant_js_1 = require("../../helpers/constant.js");
14
14
  const encodeMidnightCalldata_js_1 = require("../../helpers/encodeMidnightCalldata.js");
15
15
  const index_js_3 = require("../../helpers/index.js");
16
+ const metadata_js_1 = require("../../helpers/metadata.js");
16
17
  const validateMidnightAccount_js_1 = require("../../helpers/validateMidnightAccount.js");
17
18
  const validateMidnightBounds_js_1 = require("../../helpers/validateMidnightBounds.js");
18
19
  const validateMidnightCollateralBatch_js_1 = require("../../helpers/validateMidnightCollateralBatch.js");
@@ -33,8 +34,7 @@ const validatePositionBinding = (params) => {
33
34
  throw new index_js_4.AccrualPositionUserMismatchError(params.positionData.user, params.accountAddress);
34
35
  }
35
36
  };
36
- const validateTakeDeadlineWindow = (deadline) => {
37
- const now = morpho_ts_1.Time.timestamp();
37
+ const validateTakeDeadlineWindow = (deadline, now = morpho_ts_1.Time.timestamp()) => {
38
38
  // Throws when the deadline is at or before now.
39
39
  (0, deadline_js_1.validateMidnightDeadline)(deadline, now);
40
40
  const maxDeadline = now + constant_js_1.MIDNIGHT_TAKE_MAX_DEADLINE_SECONDS;
@@ -43,17 +43,20 @@ const validateTakeDeadlineWindow = (deadline) => {
43
43
  }
44
44
  };
45
45
  const assertNonNegativeAmount = (label, amount) => {
46
- if (amount < 0n)
46
+ if (amount < 0n) {
47
47
  throw new index_js_4.NegativeInputError(label, amount);
48
+ }
48
49
  };
49
50
  const assertPositiveAmount = (label, amount) => {
50
- if (amount <= 0n)
51
+ if (amount <= 0n) {
51
52
  throw new index_js_4.NonPositiveInputError(label, amount);
53
+ }
52
54
  };
53
55
  const rejectReduceOnlyOffers = (offers) => {
54
56
  offers.forEach((offer, index) => {
55
- if (offer.reduceOnly)
57
+ if (offer.reduceOnly) {
56
58
  throw new index_js_4.MidnightOfferReduceOnlyError({ index });
59
+ }
57
60
  });
58
61
  };
59
62
  const getOffersDataInternal = async ({ client, chainId, params, marketCreationTime = "expiry", }) => {
@@ -150,6 +153,29 @@ const getOffersDataInternal = async ({ client, chainId, params, marketCreationTi
150
153
  payload: await midnight_sdk_1.Payload.encode(items),
151
154
  };
152
155
  };
156
+ const getPostWithdrawalCollateral = (params) => {
157
+ const market = params.marketData.params;
158
+ const collateralWithdrawals = params.collateralWithdrawals ?? [];
159
+ const postCollateral = Array.from({ length: market.collateralParams.length }, (_, index) => params.positionData.collateral[index] ?? 0n);
160
+ let withdrawAmount = 0n;
161
+ for (const withdrawal of collateralWithdrawals) {
162
+ // Reject unconfigured collateral indexes before updating postCollateral.
163
+ midnight_sdk_1.MarketUtils.getCollateralByIndex(market, withdrawal.collateralIndex);
164
+ const index = Number(withdrawal.collateralIndex);
165
+ const available = postCollateral[index] ?? 0n;
166
+ const amount = withdrawal.assets === viem_1.maxUint256 ? available : withdrawal.assets;
167
+ if (amount > available) {
168
+ throw new index_js_4.WithdrawExceedsCollateralError({
169
+ withdrawAmount: amount,
170
+ available,
171
+ market: params.marketData.id,
172
+ });
173
+ }
174
+ postCollateral[index] = available - amount;
175
+ withdrawAmount += amount;
176
+ }
177
+ return { postCollateral, withdrawAmount };
178
+ };
153
179
  const validateMarketData = (market, chainId) => {
154
180
  // Reject snapshots from another chain deployment before exposing requirements.
155
181
  (0, validateMidnightMarket_js_1.validateMidnightMarket)({ market, chainId });
@@ -364,15 +390,18 @@ class MorphoMidnight {
364
390
  * target: { type: "assets", assets: 1_000_000n, minUnits: 900_000n },
365
391
  * takeableOffers: quote.data.takeableOffers,
366
392
  * maxContinuousFee: maxUint256,
367
- * deadline: Time.timestamp() + 600n,
393
+ * now: (await client.getBlock()).timestamp,
394
+ * deadline: (await client.getBlock()).timestamp + 600n,
368
395
  * });
369
396
  * ```
370
397
  */
371
398
  takeLend(params) {
399
+ const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, params.now);
372
400
  (0, index_js_3.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
373
401
  // Reject the zero account before preparing anything for it.
374
402
  (0, validateMidnightAccount_js_1.validateMidnightAccount)("accountAddress", params.accountAddress);
375
- validateTakeDeadlineWindow(params.deadline);
403
+ const now = params.now ?? morpho_ts_1.Time.timestamp();
404
+ validateTakeDeadlineWindow(params.deadline, now);
376
405
  const market = params.marketData.params;
377
406
  const rawParams = {
378
407
  chainId: this.chainId,
@@ -384,7 +413,7 @@ class MorphoMidnight {
384
413
  deadline: params.deadline,
385
414
  referralFeePct: params.referralFeePct,
386
415
  referralFeeRecipient: params.referralFeeRecipient,
387
- metadata: this.client.options.metadata,
416
+ metadata,
388
417
  };
389
418
  let tx = (0, index_js_1.midnightTakeLend)(rawParams);
390
419
  const takeableOffers = (0, validateMidnightTakeFeasibility_js_1.validateMidnightTakeFeasibility)({
@@ -392,7 +421,7 @@ class MorphoMidnight {
392
421
  takeableOffers: params.takeableOffers,
393
422
  accountAddress: params.accountAddress,
394
423
  marketData: params.marketData,
395
- now: morpho_ts_1.Time.timestamp(),
424
+ now: params.now ?? morpho_ts_1.Time.timestamp(),
396
425
  deadline: params.deadline,
397
426
  referralFeePct: tx.action.args.referralFeePct,
398
427
  fallbackUnits: 0n,
@@ -511,15 +540,18 @@ class MorphoMidnight {
511
540
  * collateralPrices,
512
541
  * target: { type: "assets", assets: 1_000_000n, maxUnits: 1_100_000n },
513
542
  * takeableOffers: quote.data.takeableOffers,
514
- * deadline: Time.timestamp() + 600n,
543
+ * now: (await client.getBlock()).timestamp,
544
+ * deadline: (await client.getBlock()).timestamp + 600n,
515
545
  * });
516
546
  * ```
517
547
  */
518
548
  takeBorrow(params) {
549
+ const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, params.now);
519
550
  (0, index_js_3.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
520
551
  // Reject the zero account before preparing anything for it.
521
552
  (0, validateMidnightAccount_js_1.validateMidnightAccount)("accountAddress", params.accountAddress);
522
- validateTakeDeadlineWindow(params.deadline);
553
+ const now = params.now ?? morpho_ts_1.Time.timestamp();
554
+ validateTakeDeadlineWindow(params.deadline, now);
523
555
  const rawParams = {
524
556
  chainId: this.chainId,
525
557
  market: params.marketData.params,
@@ -529,7 +561,7 @@ class MorphoMidnight {
529
561
  deadline: params.deadline,
530
562
  referralFeePct: params.referralFeePct,
531
563
  referralFeeRecipient: params.referralFeeRecipient,
532
- metadata: this.client.options.metadata,
564
+ metadata,
533
565
  };
534
566
  let tx = (0, index_js_1.midnightTakeBorrow)(rawParams);
535
567
  validatePositionBinding({
@@ -551,7 +583,6 @@ class MorphoMidnight {
551
583
  throw new index_js_4.BorrowExceedsSafeLtvError(requestedUnits, blue_sdk_1.MathLib.zeroFloorSub(maxSafeDebt, params.positionData.debt));
552
584
  }
553
585
  }
554
- const now = morpho_ts_1.Time.timestamp();
555
586
  const takeableOffers = (0, validateMidnightTakeFeasibility_js_1.validateMidnightTakeFeasibility)({
556
587
  side: "sell",
557
588
  takeableOffers: params.takeableOffers,
@@ -638,15 +669,18 @@ class MorphoMidnight {
638
669
  * marketData,
639
670
  * target: { type: "units", units: credit, minSellerAssets: 990_000n },
640
671
  * takeableOffers: quote.data.takeableOffers,
641
- * deadline: Time.timestamp() + 600n,
672
+ * now: (await client.getBlock()).timestamp,
673
+ * deadline: (await client.getBlock()).timestamp + 600n,
642
674
  * });
643
675
  * ```
644
676
  */
645
677
  takeWithdraw(params) {
678
+ const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, params.now);
646
679
  (0, index_js_3.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
647
680
  // Reject the zero account before preparing anything for it.
648
681
  (0, validateMidnightAccount_js_1.validateMidnightAccount)("accountAddress", params.accountAddress);
649
- validateTakeDeadlineWindow(params.deadline);
682
+ const now = params.now ?? morpho_ts_1.Time.timestamp();
683
+ validateTakeDeadlineWindow(params.deadline, now);
650
684
  const rawParams = {
651
685
  chainId: this.chainId,
652
686
  market: params.marketData.params,
@@ -656,7 +690,7 @@ class MorphoMidnight {
656
690
  deadline: params.deadline,
657
691
  referralFeePct: params.referralFeePct,
658
692
  referralFeeRecipient: params.referralFeeRecipient,
659
- metadata: this.client.options.metadata,
693
+ metadata,
660
694
  };
661
695
  let tx = (0, index_js_1.midnightTakeWithdraw)(rawParams);
662
696
  const takeableOffers = (0, validateMidnightTakeFeasibility_js_1.validateMidnightTakeFeasibility)({
@@ -664,7 +698,7 @@ class MorphoMidnight {
664
698
  takeableOffers: params.takeableOffers,
665
699
  accountAddress: params.accountAddress,
666
700
  marketData: params.marketData,
667
- now: morpho_ts_1.Time.timestamp(),
701
+ now: params.now ?? morpho_ts_1.Time.timestamp(),
668
702
  deadline: params.deadline,
669
703
  referralFeePct: tx.action.args.referralFeePct,
670
704
  // The bundle redeems credit first, up to the market's withdrawable liquidity.
@@ -771,15 +805,18 @@ class MorphoMidnight {
771
805
  * collateralSupplies: [{ collateralIndex: 0n, assets: 2_000_000n }],
772
806
  * target: { type: "assets", assets: 1_000_000n, maxUnits: 1_100_000n },
773
807
  * takeableOffers: quote.data.takeableOffers,
774
- * deadline: Time.timestamp() + 600n,
808
+ * now: (await client.getBlock()).timestamp,
809
+ * deadline: (await client.getBlock()).timestamp + 600n,
775
810
  * });
776
811
  * ```
777
812
  */
778
813
  supplyCollateralTakeBorrow(params) {
814
+ const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, params.now);
779
815
  (0, index_js_3.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
780
816
  // Reject the zero account before preparing anything for it.
781
817
  (0, validateMidnightAccount_js_1.validateMidnightAccount)("accountAddress", params.accountAddress);
782
- validateTakeDeadlineWindow(params.deadline);
818
+ const now = params.now ?? morpho_ts_1.Time.timestamp();
819
+ validateTakeDeadlineWindow(params.deadline, now);
783
820
  const market = params.marketData.params;
784
821
  const rawParams = {
785
822
  chainId: this.chainId,
@@ -792,7 +829,7 @@ class MorphoMidnight {
792
829
  deadline: params.deadline,
793
830
  referralFeePct: params.referralFeePct,
794
831
  referralFeeRecipient: params.referralFeeRecipient,
795
- metadata: this.client.options.metadata,
832
+ metadata,
796
833
  };
797
834
  let tx = (0, index_js_1.midnightSupplyCollateralTakeBorrow)(rawParams);
798
835
  validatePositionBinding({
@@ -821,7 +858,6 @@ class MorphoMidnight {
821
858
  throw new index_js_4.BorrowExceedsSafeLtvError(requestedUnits, blue_sdk_1.MathLib.zeroFloorSub(maxSafeDebt, params.positionData.debt));
822
859
  }
823
860
  }
824
- const now = morpho_ts_1.Time.timestamp();
825
861
  const takeableOffers = (0, validateMidnightTakeFeasibility_js_1.validateMidnightTakeFeasibility)({
826
862
  side: "sell",
827
863
  takeableOffers: params.takeableOffers,
@@ -965,15 +1001,18 @@ class MorphoMidnight {
965
1001
  * repayEnabled: true,
966
1002
  * collateralWithdrawals: [{ collateralIndex: 0n, assets: maxUint256 }],
967
1003
  * maxContinuousFee: maxUint256,
968
- * deadline: Time.timestamp() + 600n,
1004
+ * now: (await client.getBlock()).timestamp,
1005
+ * deadline: (await client.getBlock()).timestamp + 600n,
969
1006
  * });
970
1007
  * ```
971
1008
  */
972
1009
  takeRepayWithdrawCollateral(params) {
1010
+ const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, params.now);
973
1011
  (0, index_js_3.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
974
1012
  // Reject the zero account before preparing anything for it.
975
1013
  (0, validateMidnightAccount_js_1.validateMidnightAccount)("accountAddress", params.accountAddress);
976
- validateTakeDeadlineWindow(params.deadline);
1014
+ const now = params.now ?? morpho_ts_1.Time.timestamp();
1015
+ validateTakeDeadlineWindow(params.deadline, now);
977
1016
  const market = params.marketData.params;
978
1017
  const rawParams = {
979
1018
  chainId: this.chainId,
@@ -988,7 +1027,7 @@ class MorphoMidnight {
988
1027
  deadline: params.deadline,
989
1028
  referralFeePct: params.referralFeePct,
990
1029
  referralFeeRecipient: params.referralFeeRecipient,
991
- metadata: this.client.options.metadata,
1030
+ metadata,
992
1031
  };
993
1032
  let tx = (0, index_js_1.midnightTakeRepayWithdrawCollateral)(rawParams);
994
1033
  validatePositionBinding({
@@ -997,24 +1036,7 @@ class MorphoMidnight {
997
1036
  marketData: params.marketData,
998
1037
  });
999
1038
  const collateralWithdrawals = params.collateralWithdrawals ?? [];
1000
- const postCollateral = Array.from({ length: market.collateralParams.length }, (_, index) => params.positionData.collateral[index] ?? 0n);
1001
- let withdrawAmount = 0n;
1002
- for (const withdrawal of collateralWithdrawals) {
1003
- // Reject unconfigured collateral indexes before updating postCollateral.
1004
- midnight_sdk_1.MarketUtils.getCollateralByIndex(market, withdrawal.collateralIndex);
1005
- const index = Number(withdrawal.collateralIndex);
1006
- const available = postCollateral[index] ?? 0n;
1007
- const amount = withdrawal.assets === viem_1.maxUint256 ? available : withdrawal.assets;
1008
- if (amount > available) {
1009
- throw new index_js_4.WithdrawExceedsCollateralError({
1010
- withdrawAmount: amount,
1011
- available,
1012
- market: params.marketData.id,
1013
- });
1014
- }
1015
- postCollateral[index] = available - amount;
1016
- withdrawAmount += amount;
1017
- }
1039
+ const { postCollateral, withdrawAmount } = getPostWithdrawalCollateral(params);
1018
1040
  const debtReduction = params.target.type === "units"
1019
1041
  ? params.target.units
1020
1042
  : params.target.minUnits;
@@ -1040,7 +1062,7 @@ class MorphoMidnight {
1040
1062
  takeableOffers: params.takeableOffers,
1041
1063
  accountAddress: params.accountAddress,
1042
1064
  marketData: params.marketData,
1043
- now: morpho_ts_1.Time.timestamp(),
1065
+ now: params.now ?? morpho_ts_1.Time.timestamp(),
1044
1066
  deadline: params.deadline,
1045
1067
  referralFeePct: tx.action.args.referralFeePct,
1046
1068
  // With repayEnabled the bundle repays the unfilled debt directly, at par.
@@ -1120,6 +1142,7 @@ class MorphoMidnight {
1120
1142
  * ```
1121
1143
  */
1122
1144
  withdrawCollateral(params) {
1145
+ const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, params.now);
1123
1146
  (0, index_js_3.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
1124
1147
  // Reject invalid owners before preparing the direct call.
1125
1148
  (0, validateMidnightAccount_js_1.validateMidnightAccount)("accountAddress", params.accountAddress);
@@ -1138,7 +1161,7 @@ class MorphoMidnight {
1138
1161
  assets,
1139
1162
  onBehalf: params.accountAddress,
1140
1163
  receiver: params.receiver ?? params.accountAddress,
1141
- metadata: this.client.options.metadata,
1164
+ metadata,
1142
1165
  });
1143
1166
  if (assets > available) {
1144
1167
  throw new index_js_4.WithdrawExceedsCollateralError({
@@ -1211,6 +1234,7 @@ class MorphoMidnight {
1211
1234
  * ```
1212
1235
  */
1213
1236
  withdrawCollateralBatch(params) {
1237
+ const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, params.now);
1214
1238
  (0, index_js_3.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
1215
1239
  // Reject invalid owners before preparing the direct call.
1216
1240
  (0, validateMidnightAccount_js_1.validateMidnightAccount)("accountAddress", params.accountAddress);
@@ -1234,12 +1258,12 @@ class MorphoMidnight {
1234
1258
  collateralWithdrawals,
1235
1259
  onBehalf: params.accountAddress,
1236
1260
  receiver: params.receiver ?? params.accountAddress,
1237
- metadata: this.client.options.metadata,
1261
+ metadata,
1238
1262
  });
1239
1263
  const collateral = Array.from({ length: params.marketData.params.collateralParams.length }, (_, i) => params.positionData.collateral[i] ?? 0n);
1240
1264
  for (const { collateralIndex, assets } of collateralWithdrawals) {
1241
1265
  const index = Number(collateralIndex);
1242
- const available = collateral[index];
1266
+ const available = collateral[index] ?? 0n;
1243
1267
  if (assets > available) {
1244
1268
  throw new index_js_4.WithdrawExceedsCollateralError({
1245
1269
  withdrawAmount: assets,
@@ -1303,6 +1327,7 @@ class MorphoMidnight {
1303
1327
  * ```
1304
1328
  */
1305
1329
  supplyCollateral(params) {
1330
+ const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, params.now);
1306
1331
  (0, index_js_3.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
1307
1332
  // Reject a malformed account here; the builder rejects the zero account.
1308
1333
  (0, validateMidnightAccount_js_1.validateMidnightAddress)("accountAddress", params.accountAddress);
@@ -1335,7 +1360,7 @@ class MorphoMidnight {
1335
1360
  collateralIndex,
1336
1361
  assets: params.collateralAssets,
1337
1362
  onBehalf: params.accountAddress,
1338
- metadata: this.client.options.metadata,
1363
+ metadata,
1339
1364
  }),
1340
1365
  };
1341
1366
  }
@@ -1367,6 +1392,7 @@ class MorphoMidnight {
1367
1392
  * ```
1368
1393
  */
1369
1394
  supplyCollateralBatch(params) {
1395
+ const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, params.now);
1370
1396
  (0, index_js_3.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
1371
1397
  // Reject malformed accounts before resolving token requirements.
1372
1398
  (0, validateMidnightAccount_js_1.validateMidnightAddress)("accountAddress", params.accountAddress);
@@ -1408,7 +1434,7 @@ class MorphoMidnight {
1408
1434
  market: params.marketData.params,
1409
1435
  collateralSupplies: params.collateralSupplies.map(({ collateralIndex, assets }) => ({ collateralIndex, assets })),
1410
1436
  onBehalf: params.accountAddress,
1411
- metadata: this.client.options.metadata,
1437
+ metadata,
1412
1438
  }),
1413
1439
  };
1414
1440
  }
@@ -1468,7 +1494,7 @@ class MorphoMidnight {
1468
1494
  * @throws {MidnightMarketLiquidationCursorNotEnabledError} when an uncreated market uses a disabled liquidation cursor.
1469
1495
  * @throws {MidnightMarketOracleWithoutCodeError} when an uncreated market's oracle has no code.
1470
1496
  * @throws {viem.BaseError} when a pinned block, contract, or oracle-code read fails.
1471
- * @throws {ExpiredDeadlineError} when `params.deadline` is at or before the local clock (`Time.timestamp()`), including zero or negative values.
1497
+ * @throws {ExpiredDeadlineError} when `params.deadline` is at or before the reference timestamp (`params.now`, or the deprecated device-clock fallback), including zero or negative values.
1472
1498
  * @example
1473
1499
  * ```ts
1474
1500
  * const output = await midnight.makeLend({
@@ -1486,7 +1512,8 @@ class MorphoMidnight {
1486
1512
  // Reject the zero account before preparing anything for it.
1487
1513
  (0, validateMidnightAccount_js_1.validateMidnightAccount)("accountAddress", params.accountAddress);
1488
1514
  // Fails before any API read or build when the deadline has already passed.
1489
- (0, deadline_js_1.validateMidnightDeadline)(params.deadline);
1515
+ const now = params.now ?? morpho_ts_1.Time.timestamp();
1516
+ (0, deadline_js_1.validateMidnightDeadline)(params.deadline, now);
1490
1517
  const tree = (0, encodeMidnightCalldata_js_1.wrapMidnightEncoding)("offers", () => midnight_sdk_1.Tree.from(params.offers));
1491
1518
  rejectReduceOnlyOffers(tree.offers);
1492
1519
  return this.prepareLendOffers({ ...params, offers: tree });
@@ -1542,7 +1569,7 @@ class MorphoMidnight {
1542
1569
  * @throws {MidnightReplacementGroupCancelledError} when a published group is also cancelled.
1543
1570
  * @throws {InvalidMidnightHexInputError} when a cancellation group is not 32-byte hex.
1544
1571
  * @throws {DuplicateMidnightGroupCancellationError} when a cancelled group appears more than once.
1545
- * @throws {ExpiredDeadlineError} when `params.deadline` is at or before the local clock (`Time.timestamp()`), including zero or negative values.
1572
+ * @throws {ExpiredDeadlineError} when `params.deadline` is at or before the reference timestamp (`params.now`, or the deprecated device-clock fallback), including zero or negative values.
1546
1573
  * @example
1547
1574
  * ```ts
1548
1575
  * const output = await midnight.makeBorrow({
@@ -1557,7 +1584,8 @@ class MorphoMidnight {
1557
1584
  // Reject the zero account before preparing anything for it.
1558
1585
  (0, validateMidnightAccount_js_1.validateMidnightAccount)("accountAddress", params.accountAddress);
1559
1586
  // Fails before any API read or build when the deadline has already passed.
1560
- (0, deadline_js_1.validateMidnightDeadline)(params.deadline);
1587
+ const now = params.now ?? morpho_ts_1.Time.timestamp();
1588
+ (0, deadline_js_1.validateMidnightDeadline)(params.deadline, now);
1561
1589
  const tree = (0, encodeMidnightCalldata_js_1.wrapMidnightEncoding)("offers", () => midnight_sdk_1.Tree.from(params.offers));
1562
1590
  rejectReduceOnlyOffers(tree.offers);
1563
1591
  return this.prepareBorrowOffers({ ...params, offers: tree });
@@ -1566,6 +1594,7 @@ class MorphoMidnight {
1566
1594
  * Prepares lend-side offers for publication.
1567
1595
  */
1568
1596
  async prepareLendOffers(params) {
1597
+ const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, params.now);
1569
1598
  // Reject a malformed address before viem compares offer markets with it.
1570
1599
  (0, validateMidnightAccount_js_1.validateMidnightAddress)("loanToken", params.loanToken);
1571
1600
  assertPositiveAmount("loanAssets", params.loanAssets);
@@ -1592,7 +1621,7 @@ class MorphoMidnight {
1592
1621
  chainId: this.chainId,
1593
1622
  cancellations: params.cancellations ?? [],
1594
1623
  deadline: params.deadline,
1595
- metadata: this.client.options.metadata,
1624
+ metadata,
1596
1625
  publication: {
1597
1626
  ratifier: data.ratifier,
1598
1627
  root: data.tree.root,
@@ -1708,7 +1737,7 @@ class MorphoMidnight {
1708
1737
  * @throws {MidnightReplacementGroupCancelledError} when a published group is also cancelled.
1709
1738
  * @throws {InvalidMidnightHexInputError} when a cancellation group or `callbackSalt` is not 32-byte hex.
1710
1739
  * @throws {DuplicateMidnightGroupCancellationError} when a cancelled group appears more than once.
1711
- * @throws {ExpiredDeadlineError} when `params.deadline` is at or before the local clock (`Time.timestamp()`), including zero or negative values.
1740
+ * @throws {ExpiredDeadlineError} when `params.deadline` is at or before the reference timestamp (`params.now`, or the deprecated device-clock fallback), including zero or negative values.
1712
1741
  * @example
1713
1742
  * ```ts
1714
1743
  * const callback = getMidnightBlueBuyCallbackAddress({ chainId, owner: maker, salt: callbackSalt });
@@ -1727,6 +1756,8 @@ class MorphoMidnight {
1727
1756
  * ```
1728
1757
  */
1729
1758
  async supplyBlueMakeLend(params) {
1759
+ const suppliedNow = params.now;
1760
+ const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, suppliedNow);
1730
1761
  // Reject the zero account before preparing anything for it.
1731
1762
  (0, validateMidnightAccount_js_1.validateMidnightAccount)("accountAddress", params.accountAddress);
1732
1763
  // Reject wrong-sized salts before callback derivation pads them or any read occurs.
@@ -1738,7 +1769,8 @@ class MorphoMidnight {
1738
1769
  rejectReduceOnlyOffers(tree.offers);
1739
1770
  await (0, validateClientChainId_js_1.validateClientChainId)(this.client.viemClient, this.chainId);
1740
1771
  // Fails before any API read or build when the deadline has already passed.
1741
- (0, deadline_js_1.validateMidnightDeadline)(params.deadline);
1772
+ const now = suppliedNow ?? morpho_ts_1.Time.timestamp();
1773
+ (0, deadline_js_1.validateMidnightDeadline)(params.deadline, now);
1742
1774
  const { callbackSalt } = params;
1743
1775
  // Encode the whole Blue market first so a malformed field fails typed, before the RPC read.
1744
1776
  const callbackData = (0, encodeMidnightCalldata_js_1.wrapMidnightEncoding)("blueMarket", () => (0, callback_js_1.getMidnightBlueBuyCallbackData)(params.blueMarket));
@@ -1758,7 +1790,7 @@ class MorphoMidnight {
1758
1790
  deployless: this.client.options.supportDeployless,
1759
1791
  }),
1760
1792
  ]);
1761
- const market = (0, morpho_ts_1._try)(() => fetched.accrueInterest(blue_sdk_1.MathLib.max(morpho_ts_1.Time.timestamp(), fetched.lastUpdate)), blue_sdk_1.UnsupportedMarketIrmError) ?? fetched;
1793
+ const market = (0, morpho_ts_1._try)(() => fetched.accrueInterest(blue_sdk_1.MathLib.max(suppliedNow ?? morpho_ts_1.Time.timestamp(), fetched.lastUpdate)), blue_sdk_1.UnsupportedMarketIrmError) ?? fetched;
1762
1794
  if (market.totalSupplyShares === 0n) {
1763
1795
  throw new index_js_4.EmptyBlueParkingMarketError({ marketId: blueMarketId });
1764
1796
  }
@@ -1808,7 +1840,7 @@ class MorphoMidnight {
1808
1840
  nativeAmount: params.nativeAmount,
1809
1841
  cancellations: params.cancellations ?? [],
1810
1842
  deadline: params.deadline,
1811
- metadata: this.client.options.metadata,
1843
+ metadata,
1812
1844
  });
1813
1845
  return {
1814
1846
  groups: data.groups,
@@ -1833,6 +1865,7 @@ class MorphoMidnight {
1833
1865
  * Prepares borrow-side offers for publication.
1834
1866
  */
1835
1867
  async prepareBorrowOffers(params) {
1868
+ const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, params.now);
1836
1869
  const tree = (0, encodeMidnightCalldata_js_1.wrapMidnightEncoding)("offers", () => midnight_sdk_1.Tree.from(params.offers));
1837
1870
  (0, validateOfferSides_js_1.validateOfferSides)(tree.offers, false);
1838
1871
  (0, publication_js_1.validateMidnightMakerOfferCallbacks)(tree.offers);
@@ -1845,7 +1878,7 @@ class MorphoMidnight {
1845
1878
  chainId: this.chainId,
1846
1879
  cancellations: params.cancellations ?? [],
1847
1880
  deadline: params.deadline,
1848
- metadata: this.client.options.metadata,
1881
+ metadata,
1849
1882
  publication: {
1850
1883
  ratifier: data.ratifier,
1851
1884
  root: data.tree.root,
@@ -1915,7 +1948,7 @@ class MorphoMidnight {
1915
1948
  * @throws {InvalidMidnightHexInputError} when a cancellation group is not 32-byte hex.
1916
1949
  * @throws {DuplicateMidnightGroupCancellationError} when a cancelled group appears more than once.
1917
1950
  * @throws {MidnightReplacementGroupCancelledError} when a published group is also cancelled.
1918
- * @throws {ExpiredDeadlineError} when `params.deadline` is at or before the local clock (`Time.timestamp()`), including zero or negative values.
1951
+ * @throws {ExpiredDeadlineError} when `params.deadline` is at or before the reference timestamp (`params.now`, or the deprecated device-clock fallback), including zero or negative values.
1919
1952
  * @example
1920
1953
  * ```ts
1921
1954
  * const output = await midnight.makeWithdraw({
@@ -1932,13 +1965,15 @@ class MorphoMidnight {
1932
1965
  // Reject the zero account before preparing anything for it.
1933
1966
  (0, validateMidnightAccount_js_1.validateMidnightAccount)("accountAddress", params.accountAddress);
1934
1967
  // Fails before any API read or build when the deadline has already passed.
1935
- (0, deadline_js_1.validateMidnightDeadline)(params.deadline);
1968
+ const now = params.now ?? morpho_ts_1.Time.timestamp();
1969
+ (0, deadline_js_1.validateMidnightDeadline)(params.deadline, now);
1936
1970
  const tree = (0, encodeMidnightCalldata_js_1.wrapMidnightEncoding)("offers", () => midnight_sdk_1.Tree.from(params.offers));
1937
1971
  // Reject the wrong exit intent before calling the mempool validation API.
1938
1972
  (0, validateOfferSides_js_1.validateOfferSides)(tree.offers, false);
1939
1973
  for (const [index, offer] of tree.offers.entries()) {
1940
- if (!offer.reduceOnly)
1974
+ if (!offer.reduceOnly) {
1941
1975
  throw new index_js_4.MidnightOfferNotReduceOnlyError({ index });
1976
+ }
1942
1977
  }
1943
1978
  return this.prepareBorrowOffers({ ...params, offers: tree });
1944
1979
  }
@@ -2003,7 +2038,7 @@ class MorphoMidnight {
2003
2038
  * @throws {InvalidMidnightHexInputError} when a cancellation group is not 32-byte hex.
2004
2039
  * @throws {DuplicateMidnightGroupCancellationError} when a cancelled group appears more than once.
2005
2040
  * @throws {MidnightReplacementGroupCancelledError} when a published group is also cancelled.
2006
- * @throws {ExpiredDeadlineError} when `params.deadline` is at or before the local clock (`Time.timestamp()`), including zero or negative values.
2041
+ * @throws {ExpiredDeadlineError} when `params.deadline` is at or before the reference timestamp (`params.now`, or the deprecated device-clock fallback), including zero or negative values.
2007
2042
  * @example
2008
2043
  * ```ts
2009
2044
  * const output = await midnight.makeRepay({
@@ -2022,13 +2057,15 @@ class MorphoMidnight {
2022
2057
  // Reject the zero account before preparing anything for it.
2023
2058
  (0, validateMidnightAccount_js_1.validateMidnightAccount)("accountAddress", params.accountAddress);
2024
2059
  // Fails before any API read or build when the deadline has already passed.
2025
- (0, deadline_js_1.validateMidnightDeadline)(params.deadline);
2060
+ const now = params.now ?? morpho_ts_1.Time.timestamp();
2061
+ (0, deadline_js_1.validateMidnightDeadline)(params.deadline, now);
2026
2062
  const tree = (0, encodeMidnightCalldata_js_1.wrapMidnightEncoding)("offers", () => midnight_sdk_1.Tree.from(params.offers));
2027
2063
  // Reject the wrong exit intent before calling the mempool validation API.
2028
2064
  (0, validateOfferSides_js_1.validateOfferSides)(tree.offers, true);
2029
2065
  for (const [index, offer] of tree.offers.entries()) {
2030
- if (!offer.reduceOnly)
2066
+ if (!offer.reduceOnly) {
2031
2067
  throw new index_js_4.MidnightOfferNotReduceOnlyError({ index });
2068
+ }
2032
2069
  }
2033
2070
  return this.prepareLendOffers({ ...params, offers: tree });
2034
2071
  }
@@ -2100,7 +2137,7 @@ class MorphoMidnight {
2100
2137
  * @throws {MidnightReplacementGroupCancelledError} when a published group is also cancelled.
2101
2138
  * @throws {InvalidMidnightHexInputError} when a cancellation group is not 32-byte hex.
2102
2139
  * @throws {DuplicateMidnightGroupCancellationError} when a cancelled group appears more than once.
2103
- * @throws {ExpiredDeadlineError} when `params.deadline` is at or before the local clock (`Time.timestamp()`), including zero or negative values.
2140
+ * @throws {ExpiredDeadlineError} when `params.deadline` is at or before the reference timestamp (`params.now`, or the deprecated device-clock fallback), including zero or negative values.
2104
2141
  * @example
2105
2142
  * ```ts
2106
2143
  * const output = await midnight.supplyCollateralMakeBorrow({
@@ -2115,11 +2152,13 @@ class MorphoMidnight {
2115
2152
  * ```
2116
2153
  */
2117
2154
  async supplyCollateralMakeBorrow(params) {
2155
+ const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, params.now);
2118
2156
  (0, index_js_3.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
2119
2157
  // Reject the zero account before preparing anything for it.
2120
2158
  (0, validateMidnightAccount_js_1.validateMidnightAccount)("accountAddress", params.accountAddress);
2121
2159
  // Fails before any API read or build when the deadline has already passed.
2122
- (0, deadline_js_1.validateMidnightDeadline)(params.deadline);
2160
+ const now = params.now ?? morpho_ts_1.Time.timestamp();
2161
+ (0, deadline_js_1.validateMidnightDeadline)(params.deadline, now);
2123
2162
  const market = params.collateral.market instanceof midnight_sdk_1.MarketParams
2124
2163
  ? params.collateral.market
2125
2164
  : midnight_sdk_1.MarketParams.from(params.collateral.market);
@@ -2145,7 +2184,7 @@ class MorphoMidnight {
2145
2184
  nativeAmount: params.nativeAmount,
2146
2185
  cancellations: params.cancellations ?? [],
2147
2186
  deadline: params.deadline,
2148
- metadata: this.client.options.metadata,
2187
+ metadata,
2149
2188
  publication: {
2150
2189
  ratifier: data.ratifier,
2151
2190
  root: data.tree.root,
@@ -2231,14 +2270,16 @@ class MorphoMidnight {
2231
2270
  * ```
2232
2271
  */
2233
2272
  redeem(params) {
2273
+ const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, params.now);
2234
2274
  (0, index_js_3.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
2235
2275
  // Reject a malformed account here; the builder rejects the zero account.
2236
2276
  (0, validateMidnightAccount_js_1.validateMidnightAddress)("accountAddress", params.accountAddress);
2237
2277
  if (!params.positionData) {
2238
2278
  throw new index_js_4.MissingAccrualPositionError();
2239
2279
  }
2240
- if (params.positionData.user != null)
2280
+ if (params.positionData.user != null) {
2241
2281
  (0, validateMidnightAccount_js_1.validateMidnightAddress)("positionData.user", params.positionData.user);
2282
+ }
2242
2283
  if (params.positionData.user == null ||
2243
2284
  !(0, viem_1.isAddressEqual)(params.positionData.user, params.accountAddress)) {
2244
2285
  throw new index_js_4.AccrualPositionUserMismatchError(params.positionData.user ?? "unbound", params.accountAddress);
@@ -2247,13 +2288,15 @@ class MorphoMidnight {
2247
2288
  validateMarketData(market, this.chainId);
2248
2289
  // Apply realized losses and continuous fees the way `Midnight._updatePosition` does before a
2249
2290
  // withdrawal; never accrue to before the snapshot's `lastAccrual`.
2250
- const accrued = params.positionData.accrueInterest(blue_sdk_1.MathLib.max(morpho_ts_1.Time.timestamp(), params.positionData.lastAccrual));
2291
+ const now = params.now ?? morpho_ts_1.Time.timestamp();
2292
+ const accrued = params.positionData.accrueInterest(blue_sdk_1.MathLib.max(now, params.positionData.lastAccrual));
2251
2293
  const units = params.units ?? accrued.faceValue;
2252
2294
  if (params.units !== undefined && units <= 0n) {
2253
2295
  throw new index_js_4.NonPositiveInputError("units", units);
2254
2296
  }
2255
- if (units <= 0n)
2297
+ if (units <= 0n) {
2256
2298
  throw new index_js_4.NoMidnightCreditToRedeemError(market.id);
2299
+ }
2257
2300
  (0, validateMidnightBounds_js_1.validateMidnightUint128Field)("units", units);
2258
2301
  if (accrued.credit < units) {
2259
2302
  throw new index_js_4.MidnightRedeemExceedsCreditError({
@@ -2276,7 +2319,7 @@ class MorphoMidnight {
2276
2319
  units,
2277
2320
  onBehalf: params.accountAddress,
2278
2321
  receiver: params.receiver,
2279
- metadata: this.client.options.metadata,
2322
+ metadata,
2280
2323
  });
2281
2324
  return {
2282
2325
  getRequirements: async () => [],
@@ -2341,7 +2384,7 @@ class MorphoMidnight {
2341
2384
  * during this method call, before the lazy output is returned.
2342
2385
  * @throws {MidnightContractReceiverError} when `collateralReceiver` is a Midnight protocol
2343
2386
  * contract or a market token contract.
2344
- * @throws {ExpiredDeadlineError} when `params.deadline` is at or before the local clock (`Time.timestamp()`), including zero or negative values.
2387
+ * @throws {ExpiredDeadlineError} when `params.deadline` is at or before the reference timestamp (`params.now`, or the deprecated device-clock fallback), including zero or negative values.
2345
2388
  * @example
2346
2389
  * ```ts
2347
2390
  * // Close the position: repay all debt, withdraw all collateral.
@@ -2357,11 +2400,13 @@ class MorphoMidnight {
2357
2400
  * ```
2358
2401
  */
2359
2402
  repayWithdrawCollateral(params) {
2403
+ const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, params.now);
2360
2404
  (0, index_js_3.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
2361
2405
  // Reject the zero account before preparing anything for it.
2362
2406
  (0, validateMidnightAccount_js_1.validateMidnightAccount)("accountAddress", params.accountAddress);
2363
2407
  // Fails before any API read or build when the deadline has already passed.
2364
- (0, deadline_js_1.validateMidnightDeadline)(params.deadline);
2408
+ const now = params.now ?? morpho_ts_1.Time.timestamp();
2409
+ (0, deadline_js_1.validateMidnightDeadline)(params.deadline, now);
2365
2410
  const market = params.marketData.params;
2366
2411
  const tx = (0, index_js_1.midnightRepayWithdrawCollateral)({
2367
2412
  chainId: this.chainId,
@@ -2371,7 +2416,7 @@ class MorphoMidnight {
2371
2416
  collateralWithdrawals: params.collateralWithdrawals ?? [],
2372
2417
  collateralReceiver: params.collateralReceiver ?? params.accountAddress,
2373
2418
  deadline: params.deadline,
2374
- metadata: this.client.options.metadata,
2419
+ metadata,
2375
2420
  });
2376
2421
  validatePositionBinding({
2377
2422
  positionData: params.positionData,
@@ -2379,24 +2424,7 @@ class MorphoMidnight {
2379
2424
  marketData: params.marketData,
2380
2425
  });
2381
2426
  const collateralWithdrawals = params.collateralWithdrawals ?? [];
2382
- const postCollateral = Array.from({ length: market.collateralParams.length }, (_, index) => params.positionData.collateral[index] ?? 0n);
2383
- let withdrawAmount = 0n;
2384
- for (const withdrawal of collateralWithdrawals) {
2385
- // Reject unconfigured collateral indexes before updating postCollateral.
2386
- midnight_sdk_1.MarketUtils.getCollateralByIndex(market, withdrawal.collateralIndex);
2387
- const index = Number(withdrawal.collateralIndex);
2388
- const available = postCollateral[index] ?? 0n;
2389
- const amount = withdrawal.assets === viem_1.maxUint256 ? available : withdrawal.assets;
2390
- if (amount > available) {
2391
- throw new index_js_4.WithdrawExceedsCollateralError({
2392
- withdrawAmount: amount,
2393
- available,
2394
- market: params.marketData.id,
2395
- });
2396
- }
2397
- postCollateral[index] = available - amount;
2398
- withdrawAmount += amount;
2399
- }
2427
+ const { postCollateral, withdrawAmount } = getPostWithdrawalCollateral(params);
2400
2428
  // The bundle must fill the whole assets target and the direct repay is capped at the debt, so a repay above it reverts `OutOfOffers()`.
2401
2429
  if (params.repay.type === "assets" &&
2402
2430
  params.repay.assets > params.positionData.debt) {
@@ -2414,11 +2442,11 @@ class MorphoMidnight {
2414
2442
  limit: params.positionData.debt,
2415
2443
  });
2416
2444
  }
2417
- const postDebt = params.repay.type === "full"
2418
- ? 0n
2419
- : params.positionData.debt > params.repay.assets
2420
- ? params.positionData.debt - params.repay.assets
2421
- : 0n;
2445
+ let postDebt = 0n;
2446
+ if (params.repay.type !== "full" &&
2447
+ params.positionData.debt > params.repay.assets) {
2448
+ postDebt = params.positionData.debt - params.repay.assets;
2449
+ }
2422
2450
  if (collateralWithdrawals.length > 0 && postDebt > 0n) {
2423
2451
  const maxSafeDebt = (0, index_js_3.getMidnightMaxSafeDebt)({
2424
2452
  market: params.marketData,
@@ -2455,42 +2483,37 @@ class MorphoMidnight {
2455
2483
  };
2456
2484
  }
2457
2485
  /**
2458
- * Prepares guarded cancellation of several maker offer groups through `MidnightBundlesV2`.
2486
+ * Prepares a full cancellation of one or more maker offer groups as a direct call to Midnight.
2459
2487
  *
2460
- * The transaction reverts as a whole when any group's consumption exceeds its
2461
- * `maxConsumed` ceiling, so fills landing before execution never leave a partial cancel.
2488
+ * One group builds a bare `setConsumed(group, maxUint128, accountAddress)`. Two or more groups
2489
+ * build one Midnight `multicall` of such calls, in list order, which cancels every group or
2490
+ * none. Only the single-group transaction is matchable by a wallet policy that allows
2491
+ * `setConsumed` by method name; cancel one group per transaction when the signer's policy names
2492
+ * `setConsumed`.
2462
2493
  *
2463
- * @param params - Maker account, groups with consumption ceilings, and deadline.
2464
- * @param params.accountAddress - Maker that must send the transaction. It is used only to check
2465
- * the maker's Midnight authorization of `MidnightBundlesV2`; the bundle cancels the groups of
2466
- * `msg.sender`. A transaction sent from another account that has also authorized
2467
- * `MidnightBundlesV2` does not revert: it cancels that account's groups and leaves the maker's
2468
- * offers fillable.
2469
- * @param params.cancellations - Offer groups and the largest consumption accepted for each.
2470
- * To cancel unconditionally, set `maxConsumed` to `maxUint128`; the contract treats
2471
- * `type(uint128).max` as no limit.
2472
- * Cancellation is per group: it does not unratify roots or revoke
2473
- * `MidnightBundlesV2` or ratifier authorization.
2474
- * @param params.deadline - Bundle execution deadline timestamp.
2475
- * @returns Lazy Midnight authorization requirement for `MidnightBundlesV2` and a synchronous transaction builder.
2476
- * @throws {ChainIdMismatchError} when the client targets another chain, or from
2477
- * `getRequirements()` when the RPC reports another chain via `eth_chainId`.
2478
- * @throws {MidnightCalldataEncodingError} when an input cannot be ABI-encoded, such as an out-of-range integer.
2479
- * @throws {viem.BaseError} from `getRequirements()` when an `eth_chainId` request or requirement
2480
- * RPC read fails.
2481
- * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
2482
- * @throws {UnknownAddressError} when the chain has no `midnightBundlesV2` deployment.
2483
- * @throws {EmptyMidnightGroupCancellationsError} when no groups are provided.
2494
+ * Nothing is required before sending: no Midnight authorization, approval, signature or
2495
+ * deadline. Cancellation is per group and always full; it does not unratify roots or revoke
2496
+ * authorizations, and cancelling a group again succeeds.
2497
+ *
2498
+ * @param params - Maker account and the offer groups to cancel.
2499
+ * @param params.accountAddress - Maker whose groups are cancelled. The maker, or an operator it
2500
+ * authorized on Midnight, must send the transaction; a transaction from any other account
2501
+ * reverts `Unauthorized()` and changes no group.
2502
+ * @param params.groups - Nonempty list of distinct 32-byte offer groups to cancel.
2503
+ * @param params.now - Optional reference timestamp used to resolve `metadata.timestamp: true`.
2504
+ * @returns A handle whose `getRequirements()` resolves to an empty list without reading the
2505
+ * chain, and whose synchronous `buildTx()` returns the transaction targeting `Midnight`.
2506
+ * @throws {ChainIdMismatchError} when the client targets another chain.
2484
2507
  * @throws {MidnightInvalidAccountAddressError} when `accountAddress` is malformed or zero.
2485
- * @throws {InvalidMidnightHexInputError} when a cancellation group is not 32-byte hex.
2508
+ * @throws {EmptyMidnightGroupCancellationsError} when no groups are provided.
2509
+ * @throws {InvalidMidnightHexInputError} when a group is not 32-byte hex.
2486
2510
  * @throws {DuplicateMidnightGroupCancellationError} when a group appears more than once.
2487
- * @throws {NegativeInputError} when a `maxConsumed` ceiling is negative.
2488
- * @throws {InputExceedsMaxError} when `deadline` exceeds `uint256` or a `maxConsumed` ceiling exceeds `uint128`.
2489
- * @throws {ExpiredDeadlineError} when `params.deadline` is at or before the local clock (`Time.timestamp()`), including zero or negative values.
2511
+ * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
2512
+ * @throws {UnknownAddressError} when the chain has no `midnight` deployment.
2490
2513
  * @example
2491
2514
  * ```ts
2492
2515
  * import { morphoViemExtension } from "@morpho-org/morpho-sdk";
2493
- * import { createPublicClient, http, maxUint256, type Address, type Hex } from "viem";
2516
+ * import { createPublicClient, http, type Address, type Hex } from "viem";
2494
2517
  * import { base } from "viem/chains";
2495
2518
  *
2496
2519
  * declare const maker: Address;
@@ -2499,30 +2522,27 @@ class MorphoMidnight {
2499
2522
  * morphoViemExtension(),
2500
2523
  * );
2501
2524
  * const midnight = client.morpho.midnight(base.id);
2502
- * const output = midnight.cancelOffers({
2503
- * accountAddress: maker,
2504
- * cancellations: [{ group, maxConsumed: 0n }],
2505
- * deadline: maxUint256,
2506
- * });
2525
+ * const output = midnight.cancelOffers({ accountAddress: maker, groups: [group] });
2526
+ * await output.getRequirements(); // => []
2527
+ * const tx = output.buildTx(); // calldata: setConsumed(group, maxUint128, maker)
2507
2528
  * ```
2508
2529
  */
2509
2530
  cancelOffers(params) {
2531
+ const metadata = (0, metadata_js_1.resolveTransactionMetadata)(this.client.options.metadata, params.now);
2510
2532
  (0, index_js_3.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
2511
2533
  const { accountAddress } = params;
2512
2534
  if (!(0, viem_1.isAddress)(accountAddress) ||
2513
2535
  (0, viem_1.isAddressEqual)(accountAddress, viem_1.zeroAddress)) {
2514
2536
  throw new index_js_4.MidnightInvalidAccountAddressError(accountAddress);
2515
2537
  }
2516
- // Fails before any API read or build when the deadline has already passed.
2517
- (0, deadline_js_1.validateMidnightDeadline)(params.deadline);
2518
- const tx = (0, index_js_1.midnightCancelAndMake)({
2538
+ const tx = (0, index_js_1.midnightCancelOffers)({
2519
2539
  chainId: this.chainId,
2520
- cancellations: params.cancellations,
2521
- deadline: params.deadline,
2522
- metadata: this.client.options.metadata,
2540
+ groups: params.groups,
2541
+ onBehalf: accountAddress,
2542
+ metadata,
2523
2543
  });
2524
2544
  return {
2525
- getRequirements: () => this.getBundlesV2AuthorizationRequirements(accountAddress),
2545
+ getRequirements: async () => [],
2526
2546
  buildTx: () => tx,
2527
2547
  };
2528
2548
  }