@morpho-org/morpho-sdk 9.8.0 → 10.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (303) hide show
  1. package/MIGRATION-v9-to-v10.md +114 -0
  2. package/README.md +109 -13
  3. package/lib/cjs/actions/blue/common.js +4 -2
  4. package/lib/cjs/actions/blue/withdrawBuyCallback.js +2 -1
  5. package/lib/cjs/actions/bundles/common.js +20 -14
  6. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +3 -4
  7. package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +3 -2
  8. package/lib/cjs/actions/midnight/cancelAndMake.d.ts +2 -1
  9. package/lib/cjs/actions/midnight/cancelAndMake.js +2 -1
  10. package/lib/cjs/actions/midnight/cancelOffers.d.ts +54 -0
  11. package/lib/cjs/actions/midnight/cancelOffers.js +108 -0
  12. package/lib/cjs/actions/midnight/index.d.ts +1 -0
  13. package/lib/cjs/actions/midnight/index.js +1 -0
  14. package/lib/cjs/actions/midnight/supplyCollateralBatch.js +4 -2
  15. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +1 -1
  16. package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +3 -2
  17. package/lib/cjs/actions/midnight/takeBorrow.d.ts +1 -1
  18. package/lib/cjs/actions/midnight/takeBorrow.js +3 -2
  19. package/lib/cjs/actions/midnight/takeLend.d.ts +1 -1
  20. package/lib/cjs/actions/midnight/takeLend.js +3 -2
  21. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.d.ts +1 -1
  22. package/lib/cjs/actions/midnight/takeRepayWithdrawCollateral.js +3 -2
  23. package/lib/cjs/actions/midnight/takeWithdraw.d.ts +1 -1
  24. package/lib/cjs/actions/midnight/takeWithdraw.js +3 -2
  25. package/lib/cjs/actions/midnight/withdrawCollateralBatch.js +4 -2
  26. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +3 -4
  27. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +1 -1
  28. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +4 -6
  29. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +3 -3
  30. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +4 -6
  31. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +3 -3
  32. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +5 -6
  33. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +5 -13
  34. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +3 -5
  35. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +26 -23
  36. package/lib/cjs/actions/requirements/midnight/getMidnightApprovalRequirements.js +2 -1
  37. package/lib/cjs/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +2 -1
  38. package/lib/cjs/actions/vaultV1/inKindRedeem.js +6 -3
  39. package/lib/cjs/actions/vaultV1/migrateToV2.js +1 -1
  40. package/lib/cjs/actions/vaultV2/forceWithdraw.js +4 -2
  41. package/lib/cjs/actions/vaultV2/inKindRedeem.js +4 -2
  42. package/lib/cjs/entities/blue/blue.d.ts +34 -28
  43. package/lib/cjs/entities/blue/blue.js +107 -77
  44. package/lib/cjs/entities/midnight/deadline.d.ts +2 -2
  45. package/lib/cjs/entities/midnight/deadline.js +4 -3
  46. package/lib/cjs/entities/midnight/midnight.d.ts +49 -51
  47. package/lib/cjs/entities/midnight/midnight.js +158 -138
  48. package/lib/cjs/entities/midnight/publication.d.ts +2 -0
  49. package/lib/cjs/entities/midnight/publication.js +193 -138
  50. package/lib/cjs/entities/midnight/types.d.ts +4 -4
  51. package/lib/cjs/entities/requirements/assertContractCode.js +4 -2
  52. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +3 -5
  53. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +24 -28
  54. package/lib/cjs/entities/requirements/getErc2612Permit.js +10 -5
  55. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -4
  56. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +8 -2
  57. package/lib/cjs/entities/requirements/getVaultV1InKindRedeemRequirements.d.ts +21 -0
  58. package/lib/cjs/entities/requirements/getVaultV1InKindRedeemRequirements.js +111 -0
  59. package/lib/cjs/entities/requirements/getVaultV2ForceWithdrawRequirements.d.ts +20 -0
  60. package/lib/cjs/entities/requirements/getVaultV2ForceWithdrawRequirements.js +76 -0
  61. package/lib/cjs/entities/requirements/getVaultV2InKindRedeemRequirements.d.ts +21 -0
  62. package/lib/cjs/entities/requirements/getVaultV2InKindRedeemRequirements.js +93 -0
  63. package/lib/cjs/entities/vaultV1/inKindRedeemQuote.d.ts +25 -0
  64. package/lib/cjs/entities/vaultV1/inKindRedeemQuote.js +85 -0
  65. package/lib/cjs/entities/vaultV1/migrateToV2Quote.d.ts +20 -0
  66. package/lib/cjs/entities/vaultV1/migrateToV2Quote.js +37 -0
  67. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +12 -12
  68. package/lib/cjs/entities/vaultV1/vaultV1.js +90 -194
  69. package/lib/cjs/entities/vaultV2/forceWithdrawQuote.d.ts +25 -0
  70. package/lib/cjs/entities/vaultV2/forceWithdrawQuote.js +189 -0
  71. package/lib/cjs/entities/vaultV2/inKindRedeemQuote.d.ts +28 -0
  72. package/lib/cjs/entities/vaultV2/inKindRedeemQuote.js +110 -0
  73. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +16 -16
  74. package/lib/cjs/entities/vaultV2/vaultV2.js +94 -385
  75. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +14 -0
  76. package/lib/cjs/entities/vaultV2BlueReallocationData.js +1599 -883
  77. package/lib/cjs/errors.d.ts +1 -1
  78. package/lib/cjs/errors.js +2 -1
  79. package/lib/cjs/helpers/deadShares.js +83 -47
  80. package/lib/cjs/helpers/encodeMidnightCalldata.js +4 -2
  81. package/lib/cjs/helpers/metadata.d.ts +14 -2
  82. package/lib/cjs/helpers/metadata.js +49 -8
  83. package/lib/cjs/helpers/previewVaultV2ForceWithdraw.js +44 -27
  84. package/lib/cjs/helpers/previewVaultV2InKindRedeem.js +11 -2
  85. package/lib/cjs/helpers/utilization.js +10 -5
  86. package/lib/cjs/helpers/validate.js +88 -67
  87. package/lib/cjs/helpers/validateMidnightBounds.js +2 -1
  88. package/lib/cjs/helpers/validateMidnightCollateralBatch.js +2 -1
  89. package/lib/cjs/helpers/validateMidnightPublication.js +4 -2
  90. package/lib/cjs/helpers/validateMidnightTakeFeasibility.js +177 -104
  91. package/lib/cjs/helpers/validatePositiveInput.js +2 -1
  92. package/lib/cjs/helpers/validateTakeableOffers.js +2 -1
  93. package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.js +40 -22
  94. package/lib/cjs/index.d.ts +2 -2
  95. package/lib/cjs/index.js +10 -1
  96. package/lib/cjs/transactionPlan/approval.js +12 -1
  97. package/lib/cjs/transactionPlan/deposit.d.ts +22 -0
  98. package/lib/cjs/transactionPlan/deposit.js +139 -0
  99. package/lib/cjs/transactionPlan/error.d.ts +10 -8
  100. package/lib/cjs/transactionPlan/error.js +10 -8
  101. package/lib/cjs/transactionPlan/getTokenPermit.js +2 -1
  102. package/lib/cjs/transactionPlan/index.d.ts +18 -0
  103. package/lib/cjs/transactionPlan/index.js +23 -5
  104. package/lib/cjs/transactionPlan/intent.d.ts +3 -3
  105. package/lib/cjs/transactionPlan/intent.js +14 -7
  106. package/lib/cjs/transactionPlan/permit.d.ts +1 -1
  107. package/lib/cjs/transactionPlan/permit.js +86 -20
  108. package/lib/cjs/transactionPlan/plan.d.ts +16 -6
  109. package/lib/cjs/transactionPlan/plan.js +77 -24
  110. package/lib/cjs/transactionPlan/redeem.d.ts +18 -0
  111. package/lib/cjs/transactionPlan/redeem.js +63 -0
  112. package/lib/cjs/transactionPlan/requirements.d.ts +4 -3
  113. package/lib/cjs/transactionPlan/requirements.js +12 -6
  114. package/lib/cjs/transactionPlan/serde.d.ts +2 -2
  115. package/lib/cjs/transactionPlan/serde.js +45 -19
  116. package/lib/cjs/transactionPlan/shareSpending.d.ts +18 -0
  117. package/lib/cjs/transactionPlan/shareSpending.js +50 -0
  118. package/lib/cjs/transactionPlan/types.d.ts +117 -11
  119. package/lib/cjs/transactionPlan/validateActions.js +11 -1
  120. package/lib/cjs/transactionPlan/validation.js +6 -3
  121. package/lib/cjs/transactionPlan/vaultv1/deposit.d.ts +93 -0
  122. package/lib/cjs/transactionPlan/vaultv1/deposit.js +94 -0
  123. package/lib/cjs/transactionPlan/vaultv1/inKindRedeem.d.ts +96 -0
  124. package/lib/cjs/transactionPlan/vaultv1/inKindRedeem.js +203 -0
  125. package/lib/cjs/transactionPlan/vaultv1/migrateToV2.d.ts +94 -0
  126. package/lib/cjs/transactionPlan/vaultv1/migrateToV2.js +217 -0
  127. package/lib/cjs/transactionPlan/vaultv1/redeem.d.ts +69 -0
  128. package/lib/cjs/transactionPlan/vaultv1/redeem.js +78 -0
  129. package/lib/cjs/transactionPlan/vaultv1/withdraw.d.ts +77 -0
  130. package/lib/cjs/transactionPlan/vaultv1/withdraw.js +88 -0
  131. package/lib/cjs/transactionPlan/vaultv2/deposit.d.ts +18 -33
  132. package/lib/cjs/transactionPlan/vaultv2/deposit.js +19 -143
  133. package/lib/cjs/transactionPlan/vaultv2/forceRedeem.d.ts +74 -0
  134. package/lib/cjs/transactionPlan/vaultv2/forceRedeem.js +155 -0
  135. package/lib/cjs/transactionPlan/vaultv2/forceWithdraw.d.ts +98 -0
  136. package/lib/cjs/transactionPlan/vaultv2/forceWithdraw.js +206 -0
  137. package/lib/cjs/transactionPlan/vaultv2/inKindRedeem.d.ts +113 -0
  138. package/lib/cjs/transactionPlan/vaultv2/inKindRedeem.js +206 -0
  139. package/lib/cjs/transactionPlan/vaultv2/redeem.d.ts +84 -0
  140. package/lib/cjs/transactionPlan/vaultv2/redeem.js +86 -0
  141. package/lib/cjs/transactionPlan/vaultv2/withdraw.d.ts +18 -29
  142. package/lib/cjs/transactionPlan/vaultv2/withdraw.js +21 -126
  143. package/lib/cjs/transactionPlan/version.js +2 -1
  144. package/lib/cjs/transactionPlan/withdraw.d.ts +18 -0
  145. package/lib/cjs/transactionPlan/withdraw.js +94 -0
  146. package/lib/cjs/types/action.d.ts +26 -2
  147. package/lib/cjs/types/action.js +17 -9
  148. package/lib/cjs/types/error.d.ts +8 -1
  149. package/lib/cjs/types/error.js +21 -6
  150. package/lib/cjs/types/metadata.d.ts +12 -2
  151. package/lib/cjs/version.d.ts +1 -1
  152. package/lib/cjs/version.js +1 -1
  153. package/lib/esm/actions/blue/common.js +4 -2
  154. package/lib/esm/actions/blue/withdrawBuyCallback.js +2 -1
  155. package/lib/esm/actions/bundles/common.js +20 -14
  156. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +3 -4
  157. package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +3 -2
  158. package/lib/esm/actions/midnight/cancelAndMake.d.ts +2 -1
  159. package/lib/esm/actions/midnight/cancelAndMake.js +2 -1
  160. package/lib/esm/actions/midnight/cancelOffers.d.ts +54 -0
  161. package/lib/esm/actions/midnight/cancelOffers.js +104 -0
  162. package/lib/esm/actions/midnight/index.d.ts +1 -0
  163. package/lib/esm/actions/midnight/index.js +1 -0
  164. package/lib/esm/actions/midnight/supplyCollateralBatch.js +4 -2
  165. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +1 -1
  166. package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +3 -2
  167. package/lib/esm/actions/midnight/takeBorrow.d.ts +1 -1
  168. package/lib/esm/actions/midnight/takeBorrow.js +3 -2
  169. package/lib/esm/actions/midnight/takeLend.d.ts +1 -1
  170. package/lib/esm/actions/midnight/takeLend.js +3 -2
  171. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.d.ts +1 -1
  172. package/lib/esm/actions/midnight/takeRepayWithdrawCollateral.js +3 -2
  173. package/lib/esm/actions/midnight/takeWithdraw.d.ts +1 -1
  174. package/lib/esm/actions/midnight/takeWithdraw.js +3 -2
  175. package/lib/esm/actions/midnight/withdrawCollateralBatch.js +4 -2
  176. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +3 -4
  177. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +1 -1
  178. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +4 -6
  179. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +3 -3
  180. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +4 -6
  181. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +3 -3
  182. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +5 -6
  183. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +7 -15
  184. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +3 -5
  185. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +26 -23
  186. package/lib/esm/actions/requirements/midnight/getMidnightApprovalRequirements.js +2 -1
  187. package/lib/esm/actions/requirements/midnight/getMidnightAuthorizationRequirement.js +2 -1
  188. package/lib/esm/actions/vaultV1/inKindRedeem.js +6 -3
  189. package/lib/esm/actions/vaultV1/migrateToV2.js +1 -1
  190. package/lib/esm/actions/vaultV2/forceWithdraw.js +4 -2
  191. package/lib/esm/actions/vaultV2/inKindRedeem.js +4 -2
  192. package/lib/esm/entities/blue/blue.d.ts +34 -28
  193. package/lib/esm/entities/blue/blue.js +107 -77
  194. package/lib/esm/entities/midnight/deadline.d.ts +2 -2
  195. package/lib/esm/entities/midnight/deadline.js +4 -3
  196. package/lib/esm/entities/midnight/midnight.d.ts +49 -51
  197. package/lib/esm/entities/midnight/midnight.js +159 -139
  198. package/lib/esm/entities/midnight/publication.d.ts +2 -0
  199. package/lib/esm/entities/midnight/publication.js +193 -138
  200. package/lib/esm/entities/midnight/types.d.ts +4 -4
  201. package/lib/esm/entities/requirements/assertContractCode.js +4 -2
  202. package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +3 -5
  203. package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +25 -29
  204. package/lib/esm/entities/requirements/getErc2612Permit.js +10 -5
  205. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -4
  206. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +8 -2
  207. package/lib/esm/entities/requirements/getVaultV1InKindRedeemRequirements.d.ts +21 -0
  208. package/lib/esm/entities/requirements/getVaultV1InKindRedeemRequirements.js +108 -0
  209. package/lib/esm/entities/requirements/getVaultV2ForceWithdrawRequirements.d.ts +20 -0
  210. package/lib/esm/entities/requirements/getVaultV2ForceWithdrawRequirements.js +73 -0
  211. package/lib/esm/entities/requirements/getVaultV2InKindRedeemRequirements.d.ts +21 -0
  212. package/lib/esm/entities/requirements/getVaultV2InKindRedeemRequirements.js +90 -0
  213. package/lib/esm/entities/vaultV1/inKindRedeemQuote.d.ts +25 -0
  214. package/lib/esm/entities/vaultV1/inKindRedeemQuote.js +82 -0
  215. package/lib/esm/entities/vaultV1/migrateToV2Quote.d.ts +20 -0
  216. package/lib/esm/entities/vaultV1/migrateToV2Quote.js +34 -0
  217. package/lib/esm/entities/vaultV1/vaultV1.d.ts +12 -12
  218. package/lib/esm/entities/vaultV1/vaultV1.js +96 -200
  219. package/lib/esm/entities/vaultV2/forceWithdrawQuote.d.ts +25 -0
  220. package/lib/esm/entities/vaultV2/forceWithdrawQuote.js +186 -0
  221. package/lib/esm/entities/vaultV2/inKindRedeemQuote.d.ts +28 -0
  222. package/lib/esm/entities/vaultV2/inKindRedeemQuote.js +107 -0
  223. package/lib/esm/entities/vaultV2/vaultV2.d.ts +16 -16
  224. package/lib/esm/entities/vaultV2/vaultV2.js +102 -393
  225. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +14 -0
  226. package/lib/esm/entities/vaultV2BlueReallocationData.js +1599 -883
  227. package/lib/esm/errors.d.ts +1 -1
  228. package/lib/esm/errors.js +1 -1
  229. package/lib/esm/helpers/deadShares.js +83 -47
  230. package/lib/esm/helpers/encodeMidnightCalldata.js +4 -2
  231. package/lib/esm/helpers/metadata.d.ts +14 -2
  232. package/lib/esm/helpers/metadata.js +48 -8
  233. package/lib/esm/helpers/previewVaultV2ForceWithdraw.js +44 -27
  234. package/lib/esm/helpers/previewVaultV2InKindRedeem.js +11 -2
  235. package/lib/esm/helpers/utilization.js +10 -5
  236. package/lib/esm/helpers/validate.js +88 -67
  237. package/lib/esm/helpers/validateMidnightBounds.js +2 -1
  238. package/lib/esm/helpers/validateMidnightCollateralBatch.js +2 -1
  239. package/lib/esm/helpers/validateMidnightPublication.js +4 -2
  240. package/lib/esm/helpers/validateMidnightTakeFeasibility.js +176 -103
  241. package/lib/esm/helpers/validatePositiveInput.js +2 -1
  242. package/lib/esm/helpers/validateTakeableOffers.js +2 -1
  243. package/lib/esm/helpers/vaultV2ForceWithdrawPlan.js +40 -22
  244. package/lib/esm/index.d.ts +2 -2
  245. package/lib/esm/index.js +1 -1
  246. package/lib/esm/transactionPlan/approval.js +13 -2
  247. package/lib/esm/transactionPlan/deposit.d.ts +22 -0
  248. package/lib/esm/transactionPlan/deposit.js +135 -0
  249. package/lib/esm/transactionPlan/error.d.ts +10 -8
  250. package/lib/esm/transactionPlan/error.js +10 -8
  251. package/lib/esm/transactionPlan/getTokenPermit.js +2 -1
  252. package/lib/esm/transactionPlan/index.d.ts +18 -0
  253. package/lib/esm/transactionPlan/index.js +9 -0
  254. package/lib/esm/transactionPlan/intent.d.ts +3 -3
  255. package/lib/esm/transactionPlan/intent.js +14 -7
  256. package/lib/esm/transactionPlan/permit.d.ts +1 -1
  257. package/lib/esm/transactionPlan/permit.js +86 -20
  258. package/lib/esm/transactionPlan/plan.d.ts +16 -6
  259. package/lib/esm/transactionPlan/plan.js +73 -20
  260. package/lib/esm/transactionPlan/redeem.d.ts +18 -0
  261. package/lib/esm/transactionPlan/redeem.js +59 -0
  262. package/lib/esm/transactionPlan/requirements.d.ts +4 -3
  263. package/lib/esm/transactionPlan/requirements.js +12 -6
  264. package/lib/esm/transactionPlan/serde.d.ts +2 -2
  265. package/lib/esm/transactionPlan/serde.js +45 -19
  266. package/lib/esm/transactionPlan/shareSpending.d.ts +18 -0
  267. package/lib/esm/transactionPlan/shareSpending.js +47 -0
  268. package/lib/esm/transactionPlan/types.d.ts +117 -11
  269. package/lib/esm/transactionPlan/validateActions.js +11 -1
  270. package/lib/esm/transactionPlan/validation.js +6 -3
  271. package/lib/esm/transactionPlan/vaultv1/deposit.d.ts +93 -0
  272. package/lib/esm/transactionPlan/vaultv1/deposit.js +90 -0
  273. package/lib/esm/transactionPlan/vaultv1/inKindRedeem.d.ts +96 -0
  274. package/lib/esm/transactionPlan/vaultv1/inKindRedeem.js +198 -0
  275. package/lib/esm/transactionPlan/vaultv1/migrateToV2.d.ts +94 -0
  276. package/lib/esm/transactionPlan/vaultv1/migrateToV2.js +212 -0
  277. package/lib/esm/transactionPlan/vaultv1/redeem.d.ts +69 -0
  278. package/lib/esm/transactionPlan/vaultv1/redeem.js +74 -0
  279. package/lib/esm/transactionPlan/vaultv1/withdraw.d.ts +77 -0
  280. package/lib/esm/transactionPlan/vaultv1/withdraw.js +84 -0
  281. package/lib/esm/transactionPlan/vaultv2/deposit.d.ts +18 -33
  282. package/lib/esm/transactionPlan/vaultv2/deposit.js +21 -144
  283. package/lib/esm/transactionPlan/vaultv2/forceRedeem.d.ts +74 -0
  284. package/lib/esm/transactionPlan/vaultv2/forceRedeem.js +150 -0
  285. package/lib/esm/transactionPlan/vaultv2/forceWithdraw.d.ts +98 -0
  286. package/lib/esm/transactionPlan/vaultv2/forceWithdraw.js +201 -0
  287. package/lib/esm/transactionPlan/vaultv2/inKindRedeem.d.ts +113 -0
  288. package/lib/esm/transactionPlan/vaultv2/inKindRedeem.js +201 -0
  289. package/lib/esm/transactionPlan/vaultv2/redeem.d.ts +84 -0
  290. package/lib/esm/transactionPlan/vaultv2/redeem.js +82 -0
  291. package/lib/esm/transactionPlan/vaultv2/withdraw.d.ts +18 -29
  292. package/lib/esm/transactionPlan/vaultv2/withdraw.js +23 -127
  293. package/lib/esm/transactionPlan/version.js +2 -1
  294. package/lib/esm/transactionPlan/withdraw.d.ts +18 -0
  295. package/lib/esm/transactionPlan/withdraw.js +90 -0
  296. package/lib/esm/types/action.d.ts +26 -2
  297. package/lib/esm/types/action.js +17 -9
  298. package/lib/esm/types/error.d.ts +8 -1
  299. package/lib/esm/types/error.js +21 -6
  300. package/lib/esm/types/metadata.d.ts +12 -2
  301. package/lib/esm/version.d.ts +1 -1
  302. package/lib/esm/version.js +1 -1
  303. package/package.json +15 -14
@@ -1,7 +1,7 @@
1
1
  import { type AccrualPosition, type Market, type MarketParams } from "@morpho-org/blue-sdk";
2
2
  import { type Address } from "viem";
3
3
  import type { FetchParameters } from "../../types/data.js";
4
- import { type ActionOutput, type ActionRequirement, type AssetsOrSharesArgs, type BlueBorrowAction, type BlueRefinanceAction, type BlueRepayAction, type BlueRepayWithdrawCollateralAction, type BlueSupplyAction, type BlueSupplyCollateralAction, type BlueSupplyCollateralBorrowAction, type BlueWithdrawAction, type BlueWithdrawBuyCallbackAction, type BlueWithdrawCollateralAction, type MorphoClientType, type RequirementSignature, type VaultV2BluePublicAllocatorOptions, type VaultV2BlueReallocation } from "../../types/index.js";
4
+ import { type ActionOutput, type ActionRequirement, type AssetsOrSharesArgs, type BlueBorrowAction, type BlueRefinanceAction, type BlueRepayAction, type BlueRepayWithdrawCollateralAction, type BlueSupplyAction, type BlueSupplyCollateralAction, type BlueSupplyCollateralBorrowAction, type BlueWithdrawAction, type BlueWithdrawBuyCallbackAction, type BlueWithdrawCollateralAction, type MorphoClientType, type ReferenceTimestampParams, type RequirementSignature, type VaultV2BluePublicAllocatorOptions, type VaultV2BlueReallocation } from "../../types/index.js";
5
5
  import { VaultV2BlueReallocationData } from "../vaultV2BlueReallocationData.js";
6
6
  type VaultV2BlueReallocationsParams = {
7
7
  readonly reallocationData: VaultV2BlueReallocationData;
@@ -138,7 +138,7 @@ export interface BlueActions {
138
138
  readonly deadline: bigint;
139
139
  readonly referralFeePct?: bigint;
140
140
  readonly referralFeeRecipient?: Address;
141
- }) => ActionOutput<BlueSupplyCollateralAction, readonly RequirementSignature[], BlueTokenRequirementsParams>;
141
+ } & ReferenceTimestampParams) => ActionOutput<BlueSupplyCollateralAction, readonly RequirementSignature[], BlueTokenRequirementsParams>;
142
142
  /**
143
143
  * Prepares a direct BlueBundlesV1 loan-asset supply.
144
144
  *
@@ -202,7 +202,8 @@ export interface BlueActions {
202
202
  * userAddress,
203
203
  * marketData: await market.getMarketData(),
204
204
  * assets: 1_000_000n,
205
- * deadline: BigInt(Math.floor(Date.now() / 1_000) + 3_600),
205
+ * now: (await client.getBlock()).timestamp,
206
+ * deadline: (await client.getBlock()).timestamp + 3_600n,
206
207
  * });
207
208
  * const requirements = await action.getRequirements();
208
209
  * const tx = action.buildTx();
@@ -218,7 +219,7 @@ export interface BlueActions {
218
219
  readonly deadline: bigint;
219
220
  readonly referralFeePct?: bigint;
220
221
  readonly referralFeeRecipient?: Address;
221
- }): ActionOutput<BlueSupplyAction, readonly RequirementSignature[], BlueTokenRequirementsParams>;
222
+ } & ReferenceTimestampParams): ActionOutput<BlueSupplyAction, readonly RequirementSignature[], BlueTokenRequirementsParams>;
222
223
  /**
223
224
  * @deprecated Pass `marketData` from `getMarketData()`; the snapshot-free overload is removed in the next major.
224
225
  */
@@ -229,7 +230,7 @@ export interface BlueActions {
229
230
  readonly deadline: bigint;
230
231
  readonly referralFeePct?: bigint;
231
232
  readonly referralFeeRecipient?: Address;
232
- }): ActionOutput<BlueSupplyAction, readonly RequirementSignature[], BlueTokenRequirementsParams>;
233
+ } & ReferenceTimestampParams): ActionOutput<BlueSupplyAction, readonly RequirementSignature[], BlueTokenRequirementsParams>;
233
234
  };
234
235
  /**
235
236
  * Prepares a direct BlueBundlesV1 loan-asset withdrawal by assets or shares.
@@ -297,7 +298,8 @@ export interface BlueActions {
297
298
  * userAddress,
298
299
  * positionData,
299
300
  * shares: 1n,
300
- * deadline: BigInt(Math.floor(Date.now() / 1_000) + 3_600),
301
+ * now: (await client.getBlock()).timestamp,
302
+ * deadline: (await client.getBlock()).timestamp + 3_600n,
301
303
  * });
302
304
  * const requirements = await action.getRequirements();
303
305
  * const tx = action.buildTx();
@@ -311,7 +313,7 @@ export interface BlueActions {
311
313
  readonly deadline: bigint;
312
314
  readonly referralFeePct?: bigint;
313
315
  readonly referralFeeRecipient?: Address;
314
- } & AssetsOrSharesArgs) => ActionOutput<BlueWithdrawAction, readonly RequirementSignature[], undefined>;
316
+ } & ReferenceTimestampParams & AssetsOrSharesArgs) => ActionOutput<BlueWithdrawAction, readonly RequirementSignature[], undefined>;
315
317
  /**
316
318
  * Prepares a direct Morpho Blue withdrawal of a BlueBuyCallback's whole supply position to its
317
319
  * owner.
@@ -381,7 +383,7 @@ export interface BlueActions {
381
383
  readonly userAddress: Address;
382
384
  readonly callbackAddress: Address;
383
385
  readonly positionData: AccrualPosition;
384
- }) => ActionOutput<BlueWithdrawBuyCallbackAction, undefined, undefined>;
386
+ } & ReferenceTimestampParams) => ActionOutput<BlueWithdrawBuyCallbackAction, undefined, undefined>;
385
387
  /**
386
388
  * Prepares a direct BlueBundlesV1 loan-asset borrow.
387
389
  *
@@ -460,7 +462,7 @@ export interface BlueActions {
460
462
  readonly deadline: bigint;
461
463
  readonly referralFeePct?: bigint;
462
464
  readonly referralFeeRecipient?: Address;
463
- }) => ActionOutput<BlueBorrowAction, readonly RequirementSignature[], undefined>;
465
+ } & ReferenceTimestampParams) => ActionOutput<BlueBorrowAction, readonly RequirementSignature[], undefined>;
464
466
  /**
465
467
  * Prepares a direct BlueBundlesV1 loan-asset repayment.
466
468
  *
@@ -545,7 +547,7 @@ export interface BlueActions {
545
547
  readonly deadline: bigint;
546
548
  readonly referralFeePct?: bigint;
547
549
  readonly referralFeeRecipient?: Address;
548
- } & ({
550
+ } & ReferenceTimestampParams & ({
549
551
  readonly repayAssets: bigint;
550
552
  readonly repayShares?: never;
551
553
  } | {
@@ -623,7 +625,7 @@ export interface BlueActions {
623
625
  readonly deadline: bigint;
624
626
  readonly referralFeePct?: bigint;
625
627
  readonly referralFeeRecipient?: Address;
626
- }) => ActionOutput<BlueWithdrawCollateralAction, readonly RequirementSignature[], undefined>;
628
+ } & ReferenceTimestampParams) => ActionOutput<BlueWithdrawCollateralAction, readonly RequirementSignature[], undefined>;
627
629
  /**
628
630
  * Prepares a direct BlueBundlesV1 repay, collateral withdrawal, or atomic combination.
629
631
  *
@@ -706,7 +708,8 @@ export interface BlueActions {
706
708
  * positionData,
707
709
  * repayShares: maxUint256,
708
710
  * collateralAssets: 1n,
709
- * deadline: BigInt(Math.floor(Date.now() / 1_000) + 3_600),
711
+ * now: (await client.getBlock()).timestamp,
712
+ * deadline: (await client.getBlock()).timestamp + 3_600n,
710
713
  * });
711
714
  * const requirements = await action.getRequirements(); // Satisfy these first.
712
715
  * const tx = action.buildTx(); // For a client configured with supportSignature: false.
@@ -721,7 +724,7 @@ export interface BlueActions {
721
724
  readonly deadline: bigint;
722
725
  readonly referralFeePct?: bigint;
723
726
  readonly referralFeeRecipient?: Address;
724
- } & ({
727
+ } & ReferenceTimestampParams & ({
725
728
  readonly repayAssets: bigint;
726
729
  readonly repayShares?: never;
727
730
  } | {
@@ -807,7 +810,8 @@ export interface BlueActions {
807
810
  * positionData,
808
811
  * collateralAssets: 100_000_000n,
809
812
  * borrowAssets: 1_000_000n,
810
- * deadline: BigInt(Math.floor(Date.now() / 1_000) + 3_600),
813
+ * now: (await client.getBlock()).timestamp,
814
+ * deadline: (await client.getBlock()).timestamp + 3_600n,
811
815
  * });
812
816
  * const requirements = await action.getRequirements(); // Satisfy these first.
813
817
  * const tx = action.buildTx(); // For a client configured with supportSignature: false.
@@ -824,7 +828,7 @@ export interface BlueActions {
824
828
  readonly deadline: bigint;
825
829
  readonly referralFeePct?: bigint;
826
830
  readonly referralFeeRecipient?: Address;
827
- }) => ActionOutput<BlueSupplyCollateralBorrowAction, readonly RequirementSignature[], BlueTokenRequirementsParams>;
831
+ } & ReferenceTimestampParams) => ActionOutput<BlueSupplyCollateralBorrowAction, readonly RequirementSignature[], BlueTokenRequirementsParams>;
828
832
  /**
829
833
  * Prepares a direct BlueBundlesV1 full borrow-position migration.
830
834
  *
@@ -903,7 +907,8 @@ export interface BlueActions {
903
907
  * userAddress,
904
908
  * positionData: source,
905
909
  * destination: { marketParams: destinationParams, positionData: destination },
906
- * deadline: BigInt(Math.floor(Date.now() / 1_000) + 3_600),
910
+ * now: (await client.getBlock()).timestamp,
911
+ * deadline: (await client.getBlock()).timestamp + 3_600n,
907
912
  * });
908
913
  * const requirements = await action.getRequirements(); // Satisfy these first.
909
914
  * const tx = action.buildTx(); // For a client configured with supportSignature: false.
@@ -921,7 +926,7 @@ export interface BlueActions {
921
926
  readonly deadline: bigint;
922
927
  readonly referralFeePct?: bigint;
923
928
  readonly referralFeeRecipient?: Address;
924
- }) => ActionOutput<BlueRefinanceAction, readonly RequirementSignature[], undefined>;
929
+ } & ReferenceTimestampParams) => ActionOutput<BlueRefinanceAction, readonly RequirementSignature[], undefined>;
925
930
  /**
926
931
  * Fetches Vault V2 BluePublicAllocator state for this target market.
927
932
  *
@@ -1044,6 +1049,7 @@ export declare class MorphoBlue implements BlueActions {
1044
1049
  private assertWriteTargets;
1045
1050
  private getMaxLtv;
1046
1051
  private getBlueBundlesV1QuoteTimestamp;
1052
+ private quoteRepay;
1047
1053
  private getTokenRequirements;
1048
1054
  private getAuthorizationRequirements;
1049
1055
  getMarketData(parameters?: FetchParameters): Promise<Market>;
@@ -1057,7 +1063,7 @@ export declare class MorphoBlue implements BlueActions {
1057
1063
  readonly deadline: bigint;
1058
1064
  readonly referralFeePct?: bigint;
1059
1065
  readonly referralFeeRecipient?: Address;
1060
- }): ActionOutput<BlueSupplyAction, readonly RequirementSignature[], BlueTokenRequirementsParams>;
1066
+ } & ReferenceTimestampParams): ActionOutput<BlueSupplyAction, readonly RequirementSignature[], BlueTokenRequirementsParams>;
1061
1067
  /**
1062
1068
  * @deprecated Pass `marketData` from `getMarketData()`; the snapshot-free overload is removed in the next major.
1063
1069
  */
@@ -1068,7 +1074,7 @@ export declare class MorphoBlue implements BlueActions {
1068
1074
  readonly deadline: bigint;
1069
1075
  readonly referralFeePct?: bigint;
1070
1076
  readonly referralFeeRecipient?: Address;
1071
- }): ActionOutput<BlueSupplyAction, readonly RequirementSignature[], BlueTokenRequirementsParams>;
1077
+ } & ReferenceTimestampParams): ActionOutput<BlueSupplyAction, readonly RequirementSignature[], BlueTokenRequirementsParams>;
1072
1078
  /** {@inheritDoc BlueActions.withdraw} */
1073
1079
  withdraw(params: {
1074
1080
  readonly userAddress: Address;
@@ -1077,7 +1083,7 @@ export declare class MorphoBlue implements BlueActions {
1077
1083
  readonly deadline: bigint;
1078
1084
  readonly referralFeePct?: bigint;
1079
1085
  readonly referralFeeRecipient?: Address;
1080
- } & AssetsOrSharesArgs): {
1086
+ } & ReferenceTimestampParams & AssetsOrSharesArgs): {
1081
1087
  getRequirements: () => Promise<readonly ActionRequirement[]>;
1082
1088
  buildTx: (signatures?: readonly RequirementSignature[]) => Readonly<import("../../types/action.js").Transaction<BlueWithdrawAction>>;
1083
1089
  };
@@ -1086,7 +1092,7 @@ export declare class MorphoBlue implements BlueActions {
1086
1092
  readonly userAddress: Address;
1087
1093
  readonly callbackAddress: Address;
1088
1094
  readonly positionData: AccrualPosition;
1089
- }): ActionOutput<BlueWithdrawBuyCallbackAction, undefined, undefined>;
1095
+ } & ReferenceTimestampParams): ActionOutput<BlueWithdrawBuyCallbackAction, undefined, undefined>;
1090
1096
  /** {@inheritDoc BlueActions.supplyCollateral} */
1091
1097
  supplyCollateral(params: {
1092
1098
  readonly userAddress: Address;
@@ -1095,7 +1101,7 @@ export declare class MorphoBlue implements BlueActions {
1095
1101
  readonly deadline: bigint;
1096
1102
  readonly referralFeePct?: bigint;
1097
1103
  readonly referralFeeRecipient?: Address;
1098
- }): ActionOutput<BlueSupplyCollateralAction, readonly RequirementSignature[], BlueTokenRequirementsParams>;
1104
+ } & ReferenceTimestampParams): ActionOutput<BlueSupplyCollateralAction, readonly RequirementSignature[], BlueTokenRequirementsParams>;
1099
1105
  /** {@inheritDoc BlueActions.borrow} */
1100
1106
  borrow(params: {
1101
1107
  readonly userAddress: Address;
@@ -1105,7 +1111,7 @@ export declare class MorphoBlue implements BlueActions {
1105
1111
  readonly deadline: bigint;
1106
1112
  readonly referralFeePct?: bigint;
1107
1113
  readonly referralFeeRecipient?: Address;
1108
- }): ActionOutput<BlueBorrowAction, readonly RequirementSignature[], undefined>;
1114
+ } & ReferenceTimestampParams): ActionOutput<BlueBorrowAction, readonly RequirementSignature[], undefined>;
1109
1115
  /** {@inheritDoc BlueActions.repay} */
1110
1116
  repay(params: {
1111
1117
  readonly userAddress: Address;
@@ -1114,7 +1120,7 @@ export declare class MorphoBlue implements BlueActions {
1114
1120
  readonly deadline: bigint;
1115
1121
  readonly referralFeePct?: bigint;
1116
1122
  readonly referralFeeRecipient?: Address;
1117
- } & ({
1123
+ } & ReferenceTimestampParams & ({
1118
1124
  readonly repayAssets: bigint;
1119
1125
  readonly repayShares?: never;
1120
1126
  } | {
@@ -1129,7 +1135,7 @@ export declare class MorphoBlue implements BlueActions {
1129
1135
  readonly deadline: bigint;
1130
1136
  readonly referralFeePct?: bigint;
1131
1137
  readonly referralFeeRecipient?: Address;
1132
- }): ActionOutput<BlueWithdrawCollateralAction, readonly RequirementSignature[], undefined>;
1138
+ } & ReferenceTimestampParams): ActionOutput<BlueWithdrawCollateralAction, readonly RequirementSignature[], undefined>;
1133
1139
  /** {@inheritDoc BlueActions.repayWithdrawCollateral} */
1134
1140
  repayWithdrawCollateral(params: {
1135
1141
  readonly userAddress: Address;
@@ -1139,7 +1145,7 @@ export declare class MorphoBlue implements BlueActions {
1139
1145
  readonly deadline: bigint;
1140
1146
  readonly referralFeePct?: bigint;
1141
1147
  readonly referralFeeRecipient?: Address;
1142
- } & ({
1148
+ } & ReferenceTimestampParams & ({
1143
1149
  readonly repayAssets: bigint;
1144
1150
  readonly repayShares?: never;
1145
1151
  } | {
@@ -1163,7 +1169,7 @@ export declare class MorphoBlue implements BlueActions {
1163
1169
  readonly deadline: bigint;
1164
1170
  readonly referralFeePct?: bigint;
1165
1171
  readonly referralFeeRecipient?: Address;
1166
- }): {
1172
+ } & ReferenceTimestampParams): {
1167
1173
  getRequirements: (requirementsParams?: BlueTokenRequirementsParams) => Promise<readonly ActionRequirement[]>;
1168
1174
  buildTx: (signatures?: readonly RequirementSignature[]) => Readonly<import("../../types/action.js").Transaction<BlueSupplyCollateralBorrowAction>>;
1169
1175
  };
@@ -1179,7 +1185,7 @@ export declare class MorphoBlue implements BlueActions {
1179
1185
  readonly deadline: bigint;
1180
1186
  readonly referralFeePct?: bigint;
1181
1187
  readonly referralFeeRecipient?: Address;
1182
- }): {
1188
+ } & ReferenceTimestampParams): {
1183
1189
  getRequirements: () => Promise<readonly ActionRequirement[]>;
1184
1190
  buildTx: (signatures?: readonly RequirementSignature[]) => Readonly<import("../../types/action.js").Transaction<BlueRefinanceAction>>;
1185
1191
  };
@@ -8,6 +8,7 @@ import { getBlueBundlesV1PenaltyAssets, getBlueBundlesV1PublicAllocations, getBl
8
8
  import { blueBorrow, blueRefinance, blueRepay, blueRepayWithdrawCollateral, blueSupply, blueSupplyCollateral, blueSupplyCollateralBorrow, blueWithdraw, blueWithdrawBuyCallback, blueWithdrawCollateral, getBlueAuthorizationRequirement, } from "../../actions/index.js";
9
9
  import { validateClientChainId } from "../../actions/requirements/validateClientChainId.js";
10
10
  import { DEFAULT_LLTV_BUFFER, validateAccrualPosition, validateChainId, validatePositionHealth, validatePositionHealthAfterWithdraw, validateRepayAmount, validateRepayShares, validateWithdrawAmount, validateWithdrawShares, } from "../../helpers/index.js";
11
+ import { resolveTransactionMetadata } from "../../helpers/metadata.js";
11
12
  import { validateMarketParamsId } from "../../helpers/validate.js";
12
13
  import { BlueBuyCallbackOwnerMismatchError, BlueBuyCallbackUnauthorizedError, EmptyBlueSupplyPositionError, ExpiredDeadlineError, InputExceedsMaxError, InsufficientBlueLiquidityError, MarketIdMismatchError, MaxRepayAssetsBelowRepayAssetsError, MissingAccrualPositionError, MutuallyExclusiveRepayAmountsError, MutuallyExclusiveWithdrawAmountsError, NegativeInputError, NonPositiveInputError, NotBlueBuyCallbackError, ReallocationsRequireBorrowError, RefinanceSameMarketError, RefinanceTokenMismatchError, } from "../../types/index.js";
13
14
  import { assertRegistryTargetsHaveCode } from "../requirements/assertContractCode.js";
@@ -38,9 +39,9 @@ export class MorphoBlue {
38
39
  throw new ExpiredDeadlineError(deadline, now);
39
40
  }
40
41
  }
41
- validateWriteCommon(params) {
42
+ validateWriteCommon(params, now) {
42
43
  validateChainId(this.client.viemClient.chain?.id, this.chainId);
43
- this.validateDeadline(params.deadline);
44
+ this.validateDeadline(params.deadline, now);
44
45
  // Resolve at handle creation so unsupported deployments fail before any RPC prerequisite work.
45
46
  getChainAddress(this.chainId, "bundles.blueBundlesV1");
46
47
  // Validate the referral inputs eagerly; the normalized result is intentionally discarded here
@@ -72,8 +73,43 @@ export class MorphoBlue {
72
73
  getMaxLtv(marketParams = this.marketParams) {
73
74
  return MathLib.zeroFloorSub(marketParams.lltv, DEFAULT_LLTV_BUFFER);
74
75
  }
75
- getBlueBundlesV1QuoteTimestamp(lastUpdate) {
76
- return MathLib.max(Time.timestamp(), lastUpdate) + Time.s.from.h(2n);
76
+ getBlueBundlesV1QuoteTimestamp(lastUpdate, now = Time.timestamp()) {
77
+ return MathLib.max(now, lastUpdate) + Time.s.from.h(2n);
78
+ }
79
+ quoteRepay({ positionData, repayAssets, repayShares, deadline, now, }) {
80
+ const saturatedRepay = repayShares === maxUint256;
81
+ const simulatedRepayShares = saturatedRepay
82
+ ? positionData.borrowShares
83
+ : repayShares;
84
+ if (repayAssets > 0n) {
85
+ validateRepayAmount({
86
+ positionData,
87
+ repayAssets,
88
+ marketId: this.marketParams.id,
89
+ });
90
+ }
91
+ else if (repayShares > 0n && !saturatedRepay) {
92
+ validateRepayShares({
93
+ positionData,
94
+ repayShares,
95
+ marketId: this.marketParams.id,
96
+ });
97
+ }
98
+ const quoteHorizon = this.getBlueBundlesV1QuoteTimestamp(positionData.market.lastUpdate, now);
99
+ if (repayShares > 0n && deadline > quoteHorizon) {
100
+ throw new InputExceedsMaxError({
101
+ field: "deadline",
102
+ value: deadline,
103
+ max: quoteHorizon,
104
+ });
105
+ }
106
+ const accrualTimestamp = repayShares > 0n ? deadline : quoteHorizon;
107
+ const accruedPosition = positionData.accrueInterest(accrualTimestamp);
108
+ let expectedRepayAssets = repayAssets;
109
+ if (repayAssets <= 0n && repayShares > 0n) {
110
+ expectedRepayAssets = accruedPosition.market.toBorrowAssets(simulatedRepayShares, "Up");
111
+ }
112
+ return { simulatedRepayShares, accruedPosition, expectedRepayAssets };
77
113
  }
78
114
  async getTokenRequirements(params) {
79
115
  this.validateDeadline(params.deadline, params.now);
@@ -118,10 +154,14 @@ export class MorphoBlue {
118
154
  });
119
155
  }
120
156
  supply(params) {
157
+ const suppliedNow = params.now;
158
+ const metadata = resolveTransactionMetadata(this.client.options.metadata, suppliedNow);
121
159
  const { userAddress, assets, nativeAmount, deadline, referralFeePct, referralFeeRecipient, } = params;
122
- this.validateWriteCommon(params);
123
- if (assets <= 0n)
160
+ const createdAt = suppliedNow ?? Time.timestamp();
161
+ this.validateWriteCommon(params, createdAt);
162
+ if (assets <= 0n) {
124
163
  throw new NonPositiveInputError("assets", assets);
164
+ }
125
165
  const nativeValue = validateBlueBundlesV1NativeFunding({
126
166
  chainId: this.chainId,
127
167
  token: this.marketParams.loanToken,
@@ -135,11 +175,12 @@ export class MorphoBlue {
135
175
  }
136
176
  return {
137
177
  getRequirements: (requirementsParams) => {
138
- const now = Time.timestamp();
178
+ const now = suppliedNow ?? Time.timestamp();
139
179
  this.validateDeadline(deadline, now);
140
180
  return this.assertWriteTargets().then(() => {
141
- if (nativeValue > 0n)
181
+ if (nativeValue > 0n) {
142
182
  return [];
183
+ }
143
184
  return this.getTokenRequirements({
144
185
  token: this.marketParams.loanToken,
145
186
  amount: assets,
@@ -165,21 +206,26 @@ export class MorphoBlue {
165
206
  referralFeeRecipient,
166
207
  requirementSignature: token,
167
208
  },
168
- metadata: this.client.options.metadata,
209
+ metadata,
169
210
  });
170
211
  },
171
212
  };
172
213
  }
173
214
  /** {@inheritDoc BlueActions.withdraw} */
174
215
  withdraw(params) {
216
+ const suppliedNow = params.now;
217
+ const metadata = resolveTransactionMetadata(this.client.options.metadata, suppliedNow);
175
218
  const { userAddress, positionData, deadline, referralFeePct, referralFeeRecipient, } = params;
176
- this.validateWriteCommon(params);
219
+ const createdAt = suppliedNow ?? Time.timestamp();
220
+ this.validateWriteCommon(params, createdAt);
177
221
  const assets = ("assets" in params ? params.assets : undefined) ?? 0n;
178
222
  const shares = ("shares" in params ? params.shares : undefined) ?? 0n;
179
- if (assets < 0n)
223
+ if (assets < 0n) {
180
224
  throw new NegativeInputError("assets", assets);
181
- if (shares < 0n)
225
+ }
226
+ if (shares < 0n) {
182
227
  throw new NegativeInputError("shares", shares);
228
+ }
183
229
  if (assets > 0n && shares > 0n) {
184
230
  throw new MutuallyExclusiveWithdrawAmountsError(this.marketParams.id);
185
231
  }
@@ -194,20 +240,13 @@ export class MorphoBlue {
194
240
  expectedMarketId: this.marketParams.id,
195
241
  expectedUser: userAddress,
196
242
  });
197
- if (shares > 0n) {
198
- validateWithdrawShares({
199
- positionData,
200
- withdrawShares: shares,
201
- marketId: this.marketParams.id,
202
- });
203
- }
204
- else {
205
- validateWithdrawAmount({
206
- positionData,
207
- withdrawAssets: assets,
208
- marketId: this.marketParams.id,
209
- });
210
- }
243
+ const validateWithdrawal = shares > 0n ? validateWithdrawShares : validateWithdrawAmount;
244
+ validateWithdrawal({
245
+ positionData,
246
+ withdrawShares: shares,
247
+ withdrawAssets: assets,
248
+ marketId: this.marketParams.id,
249
+ });
211
250
  const reallocations = [...(params.reallocations ?? [])];
212
251
  // Validate the exact PublicAllocations mapping before requirement reads.
213
252
  const publicAllocations = getBlueBundlesV1PublicAllocations(reallocations, this.marketParams);
@@ -222,7 +261,7 @@ export class MorphoBlue {
222
261
  }
223
262
  return {
224
263
  getRequirements: async () => {
225
- const now = Time.timestamp();
264
+ const now = suppliedNow ?? Time.timestamp();
226
265
  this.validateDeadline(deadline, now);
227
266
  await this.assertWriteTargets();
228
267
  return this.getAuthorizationRequirements({
@@ -247,13 +286,14 @@ export class MorphoBlue {
247
286
  referralFeeRecipient,
248
287
  authorizationSignature: authorization,
249
288
  },
250
- metadata: this.client.options.metadata,
289
+ metadata,
251
290
  });
252
291
  },
253
292
  };
254
293
  }
255
294
  /** {@inheritDoc BlueActions.withdrawBuyCallback} */
256
295
  withdrawBuyCallback(params) {
296
+ const metadata = resolveTransactionMetadata(this.client.options.metadata, params.now);
257
297
  const { userAddress, callbackAddress, positionData } = params;
258
298
  validateChainId(this.client.viemClient.chain?.id, this.chainId);
259
299
  if (positionData == null) {
@@ -285,7 +325,7 @@ export class MorphoBlue {
285
325
  const tx = blueWithdrawBuyCallback({
286
326
  market: { chainId: this.chainId, marketParams: this.marketParams },
287
327
  args: { callbackAddress, userAddress, shares },
288
- metadata: this.client.options.metadata,
328
+ metadata,
289
329
  });
290
330
  return {
291
331
  getRequirements: async () => {
@@ -338,8 +378,10 @@ export class MorphoBlue {
338
378
  }
339
379
  /** {@inheritDoc BlueActions.supplyCollateral} */
340
380
  supplyCollateral(params) {
381
+ const metadata = resolveTransactionMetadata(this.client.options.metadata, params.now);
341
382
  const { userAddress, collateralAssets, nativeAmount, deadline, referralFeePct, referralFeeRecipient, } = params;
342
383
  const combinedAction = this.supplyCollateralBorrow({
384
+ now: params.now,
343
385
  userAddress,
344
386
  collateralAssets,
345
387
  borrowAssets: 0n,
@@ -365,16 +407,18 @@ export class MorphoBlue {
365
407
  referralFeeRecipient,
366
408
  requirementSignature: token,
367
409
  },
368
- metadata: this.client.options.metadata,
410
+ metadata,
369
411
  });
370
412
  },
371
413
  };
372
414
  }
373
415
  /** {@inheritDoc BlueActions.borrow} */
374
416
  borrow(params) {
417
+ const metadata = resolveTransactionMetadata(this.client.options.metadata, params.now);
375
418
  const { userAddress, borrowAssets, positionData, reallocations, deadline, referralFeePct, referralFeeRecipient, } = params;
376
419
  const normalizedReallocations = [...(reallocations ?? [])];
377
420
  const combinedAction = this.supplyCollateralBorrow({
421
+ now: params.now,
378
422
  userAddress,
379
423
  collateralAssets: 0n,
380
424
  borrowAssets,
@@ -400,13 +444,14 @@ export class MorphoBlue {
400
444
  referralFeeRecipient,
401
445
  authorizationSignature: authorization,
402
446
  },
403
- metadata: this.client.options.metadata,
447
+ metadata,
404
448
  });
405
449
  },
406
450
  };
407
451
  }
408
452
  /** {@inheritDoc BlueActions.repay} */
409
453
  repay(params) {
454
+ const metadata = resolveTransactionMetadata(this.client.options.metadata, params.now);
410
455
  const combinedAction = this.repayWithdrawCollateral({
411
456
  ...params,
412
457
  collateralAssets: 0n,
@@ -432,15 +477,17 @@ export class MorphoBlue {
432
477
  referralFeeRecipient,
433
478
  requirementSignature: token,
434
479
  },
435
- metadata: this.client.options.metadata,
480
+ metadata,
436
481
  });
437
482
  },
438
483
  };
439
484
  }
440
485
  /** {@inheritDoc BlueActions.withdrawCollateral} */
441
486
  withdrawCollateral(params) {
487
+ const metadata = resolveTransactionMetadata(this.client.options.metadata, params.now);
442
488
  const { userAddress, positionData, collateralAssets, deadline, referralFeePct, referralFeeRecipient, } = params;
443
489
  const combinedAction = this.repayWithdrawCollateral({
490
+ now: params.now,
444
491
  userAddress,
445
492
  positionData,
446
493
  repayAssets: 0n,
@@ -466,15 +513,18 @@ export class MorphoBlue {
466
513
  referralFeeRecipient: combinedArgs.referralFeeRecipient,
467
514
  authorizationSignature: authorization,
468
515
  },
469
- metadata: this.client.options.metadata,
516
+ metadata,
470
517
  });
471
518
  },
472
519
  };
473
520
  }
474
521
  /** {@inheritDoc BlueActions.repayWithdrawCollateral} */
475
522
  repayWithdrawCollateral(params) {
523
+ const suppliedNow = params.now;
524
+ const metadata = resolveTransactionMetadata(this.client.options.metadata, suppliedNow);
476
525
  const { userAddress, positionData, collateralAssets, nativeAmount, deadline, referralFeeRecipient, } = params;
477
- this.validateWriteCommon(params);
526
+ const createdAt = suppliedNow ?? Time.timestamp();
527
+ this.validateWriteCommon(params, createdAt);
478
528
  // `validateWriteCommon` returns void, so default an omitted `referralFeePct` to `0n`
479
529
  // here, matching the pure action's normalization.
480
530
  const referralFeePct = params.referralFeePct ?? 0n;
@@ -504,39 +554,13 @@ export class MorphoBlue {
504
554
  expectedMarketId: this.marketParams.id,
505
555
  expectedUser: userAddress,
506
556
  });
507
- const saturatedRepay = repayShares === maxUint256;
508
- const simulatedRepayShares = saturatedRepay
509
- ? positionData.borrowShares
510
- : repayShares;
511
- if (repayAssets > 0n) {
512
- validateRepayAmount({
513
- positionData,
514
- repayAssets,
515
- marketId: this.marketParams.id,
516
- });
517
- }
518
- else if (repayShares > 0n && !saturatedRepay) {
519
- validateRepayShares({
520
- positionData,
521
- repayShares,
522
- marketId: this.marketParams.id,
523
- });
524
- }
525
- const quoteHorizon = this.getBlueBundlesV1QuoteTimestamp(positionData.market.lastUpdate);
526
- if (repayShares > 0n && deadline > quoteHorizon) {
527
- throw new InputExceedsMaxError({
528
- field: "deadline",
529
- value: deadline,
530
- max: quoteHorizon,
531
- });
532
- }
533
- const accrualTimestamp = repayShares > 0n ? deadline : quoteHorizon;
534
- const accruedPosition = positionData.accrueInterest(accrualTimestamp);
535
- const expectedRepayAssets = repayAssets > 0n
536
- ? repayAssets
537
- : hasRepay
538
- ? accruedPosition.market.toBorrowAssets(simulatedRepayShares, "Up")
539
- : 0n;
557
+ const { simulatedRepayShares, accruedPosition, expectedRepayAssets } = this.quoteRepay({
558
+ positionData,
559
+ repayAssets,
560
+ repayShares,
561
+ deadline,
562
+ now: createdAt,
563
+ });
540
564
  const referralFeeAssets = getBlueBundlesV1ReferralFeeAssets(expectedRepayAssets, referralFeePct);
541
565
  const maxRepayAssets = expectedRepayAssets + referralFeeAssets;
542
566
  const nativeValue = validateBlueBundlesV1NativeFunding({
@@ -558,7 +582,7 @@ export class MorphoBlue {
558
582
  }
559
583
  return {
560
584
  getRequirements: async (requirementsParams) => {
561
- const now = Time.timestamp();
585
+ const now = suppliedNow ?? Time.timestamp();
562
586
  this.validateDeadline(deadline, now);
563
587
  await this.assertWriteTargets();
564
588
  const [tokenRequirements, authorizationRequirements] = await Promise.all([
@@ -609,15 +633,18 @@ export class MorphoBlue {
609
633
  requirementSignature: token,
610
634
  authorizationSignature: authorization,
611
635
  },
612
- metadata: this.client.options.metadata,
636
+ metadata,
613
637
  });
614
638
  },
615
639
  };
616
640
  }
617
641
  /** {@inheritDoc BlueActions.supplyCollateralBorrow} */
618
642
  supplyCollateralBorrow(params) {
643
+ const suppliedNow = params.now;
644
+ const metadata = resolveTransactionMetadata(this.client.options.metadata, suppliedNow);
619
645
  const { userAddress, collateralAssets, borrowAssets, positionData, nativeAmount, deadline, referralFeePct, referralFeeRecipient, } = params;
620
- this.validateWriteCommon(params);
646
+ const createdAt = suppliedNow ?? Time.timestamp();
647
+ this.validateWriteCommon(params, createdAt);
621
648
  if (collateralAssets < 0n) {
622
649
  throw new NegativeInputError("collateralAssets", collateralAssets);
623
650
  }
@@ -651,7 +678,7 @@ export class MorphoBlue {
651
678
  expectedUser: userAddress,
652
679
  });
653
680
  validatePositionHealth({
654
- positionData: positionData.accrueInterest(this.getBlueBundlesV1QuoteTimestamp(positionData.market.lastUpdate)),
681
+ positionData: positionData.accrueInterest(this.getBlueBundlesV1QuoteTimestamp(positionData.market.lastUpdate, createdAt)),
655
682
  additionalCollateral: collateralAssets,
656
683
  borrowAmount: borrowAssets,
657
684
  marketId: this.marketParams.id,
@@ -666,7 +693,7 @@ export class MorphoBlue {
666
693
  });
667
694
  return {
668
695
  getRequirements: async (requirementsParams) => {
669
- const now = Time.timestamp();
696
+ const now = suppliedNow ?? Time.timestamp();
670
697
  this.validateDeadline(deadline, now);
671
698
  await this.assertWriteTargets();
672
699
  const [tokenRequirements, authorizationRequirements] = await Promise.all([
@@ -710,15 +737,18 @@ export class MorphoBlue {
710
737
  requirementSignature: token,
711
738
  authorizationSignature: authorization,
712
739
  },
713
- metadata: this.client.options.metadata,
740
+ metadata,
714
741
  });
715
742
  },
716
743
  };
717
744
  }
718
745
  /** {@inheritDoc BlueActions.refinance} */
719
746
  refinance(params) {
747
+ const suppliedNow = params.now;
748
+ const metadata = resolveTransactionMetadata(this.client.options.metadata, suppliedNow);
720
749
  const { userAddress, positionData, destination, deadline, referralFeeRecipient, } = params;
721
- this.validateWriteCommon(params);
750
+ const createdAt = suppliedNow ?? Time.timestamp();
751
+ this.validateWriteCommon(params, createdAt);
722
752
  validateMarketParamsId(destination.marketParams);
723
753
  // `validateWriteCommon` returns void, so default an omitted `referralFeePct` to `0n`
724
754
  // here, matching the pure action's normalization.
@@ -752,7 +782,7 @@ export class MorphoBlue {
752
782
  const reallocations = [...(params.reallocations ?? [])];
753
783
  const publicAllocations = getBlueBundlesV1PublicAllocations(reallocations, destination.marketParams);
754
784
  const penaltyAssets = getBlueBundlesV1PenaltyAssets(publicAllocations);
755
- const accrualTimestamp = this.getBlueBundlesV1QuoteTimestamp(MathLib.max(positionData.market.lastUpdate, destination.positionData.market.lastUpdate));
785
+ const accrualTimestamp = this.getBlueBundlesV1QuoteTimestamp(MathLib.max(positionData.market.lastUpdate, destination.positionData.market.lastUpdate), createdAt);
756
786
  const accruedSourcePosition = positionData.accrueInterest(accrualTimestamp);
757
787
  const accruedDestinationPosition = destination.positionData.accrueInterest(accrualTimestamp);
758
788
  const referralFeeAssets = getBlueBundlesV1ReferralFeeAssets(accruedSourcePosition.borrowAssets, referralFeePct);
@@ -765,7 +795,7 @@ export class MorphoBlue {
765
795
  });
766
796
  return {
767
797
  getRequirements: async () => {
768
- const now = Time.timestamp();
798
+ const now = suppliedNow ?? Time.timestamp();
769
799
  this.validateDeadline(deadline, now);
770
800
  await this.assertWriteTargets();
771
801
  return this.getAuthorizationRequirements({
@@ -793,7 +823,7 @@ export class MorphoBlue {
793
823
  referralFeeRecipient,
794
824
  authorizationSignature: authorization,
795
825
  },
796
- metadata: this.client.options.metadata,
826
+ metadata,
797
827
  });
798
828
  },
799
829
  };