@morpho-org/morpho-sdk 6.0.0-next.1 → 6.0.0-next.3

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 (305) hide show
  1. package/MIGRATION-v5-to-v6.md +199 -34
  2. package/README.md +23 -15
  3. package/lib/cjs/abis.d.ts +3 -2829
  4. package/lib/cjs/abis.js +1 -1938
  5. package/lib/cjs/actions/blue/borrow.d.ts +3 -2
  6. package/lib/cjs/actions/blue/borrow.js +3 -2
  7. package/lib/cjs/actions/blue/common.js +6 -35
  8. package/lib/cjs/actions/blue/refinance.d.ts +2 -2
  9. package/lib/cjs/actions/blue/refinance.js +2 -2
  10. package/lib/cjs/actions/blue/repay.d.ts +1 -1
  11. package/lib/cjs/actions/blue/repay.js +1 -1
  12. package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +2 -2
  13. package/lib/cjs/actions/blue/repayWithdrawCollateral.js +2 -2
  14. package/lib/cjs/actions/blue/supply.d.ts +2 -2
  15. package/lib/cjs/actions/blue/supply.js +4 -9
  16. package/lib/cjs/actions/blue/supplyCollateral.d.ts +2 -2
  17. package/lib/cjs/actions/blue/supplyCollateral.js +2 -2
  18. package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +3 -2
  19. package/lib/cjs/actions/blue/supplyCollateralBorrow.js +3 -2
  20. package/lib/cjs/actions/blue/withdraw.d.ts +3 -3
  21. package/lib/cjs/actions/blue/withdraw.js +3 -3
  22. package/lib/cjs/actions/blue/withdrawCollateral.d.ts +2 -2
  23. package/lib/cjs/actions/blue/withdrawCollateral.js +2 -2
  24. package/lib/cjs/actions/bundles/common.d.ts +38 -4
  25. package/lib/cjs/actions/bundles/common.js +91 -35
  26. package/lib/cjs/actions/index.d.ts +0 -1
  27. package/lib/cjs/actions/index.js +0 -1
  28. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +9 -8
  29. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +17 -26
  30. package/lib/cjs/actions/requirements/blue/index.d.ts +0 -1
  31. package/lib/cjs/actions/requirements/blue/index.js +0 -1
  32. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +20 -14
  33. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +24 -25
  34. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +4 -4
  35. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +4 -5
  36. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +21 -12
  37. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +39 -38
  38. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +4 -2
  39. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +14 -9
  40. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +9 -6
  41. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +22 -17
  42. package/lib/cjs/actions/requirements/encode/index.d.ts +0 -1
  43. package/lib/cjs/actions/requirements/encode/index.js +0 -1
  44. package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +5 -6
  45. package/lib/cjs/actions/requirements/getRequirementsApproval.js +5 -6
  46. package/lib/cjs/actions/requirements/index.d.ts +0 -1
  47. package/lib/cjs/actions/requirements/index.js +0 -1
  48. package/lib/cjs/actions/vaultV1/deposit.d.ts +1 -2
  49. package/lib/cjs/actions/vaultV1/deposit.js +1 -2
  50. package/lib/cjs/actions/vaultV1/inKindRedeem.d.ts +6 -4
  51. package/lib/cjs/actions/vaultV1/inKindRedeem.js +12 -8
  52. package/lib/cjs/actions/vaultV2/deposit.d.ts +1 -2
  53. package/lib/cjs/actions/vaultV2/deposit.js +1 -2
  54. package/lib/cjs/actions/vaultV2/forceWithdraw.d.ts +58 -39
  55. package/lib/cjs/actions/vaultV2/forceWithdraw.js +87 -60
  56. package/lib/cjs/actions/vaultV2/inKindRedeem.d.ts +6 -4
  57. package/lib/cjs/actions/vaultV2/inKindRedeem.js +12 -8
  58. package/lib/cjs/augment.d.ts +0 -1
  59. package/lib/cjs/augment.js +0 -1
  60. package/lib/cjs/blue/abis.d.ts +1 -1
  61. package/lib/cjs/blue/abis.js +1 -5
  62. package/lib/cjs/blue/entities.d.ts +2 -2
  63. package/lib/cjs/blue/entities.js +1 -2
  64. package/lib/cjs/blue/errors.d.ts +1 -1
  65. package/lib/cjs/blue/errors.js +2 -1
  66. package/lib/cjs/blue/fetch.d.ts +1 -1
  67. package/lib/cjs/blue/fetch.js +1 -2
  68. package/lib/cjs/blue/types.d.ts +2 -4
  69. package/lib/cjs/blue/utils.d.ts +1 -3
  70. package/lib/cjs/blue/utils.js +1 -3
  71. package/lib/cjs/client/morphoViemExtension.d.ts +1 -1
  72. package/lib/cjs/client/morphoViemExtension.js +1 -1
  73. package/lib/cjs/constants.d.ts +3 -37
  74. package/lib/cjs/constants.js +1 -39
  75. package/lib/cjs/entities/blue/blue.d.ts +38 -264
  76. package/lib/cjs/entities/blue/blue.js +2 -203
  77. package/lib/cjs/entities/index.d.ts +3 -60
  78. package/lib/cjs/entities/index.js +1 -20
  79. package/lib/cjs/entities/midnight/midnight.js +20 -24
  80. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +7 -6
  81. package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +19 -14
  82. package/lib/cjs/{actions/requirements/blue → entities/requirements}/getUnusedPermit2Nonce.js +3 -3
  83. package/lib/cjs/entities/requirements/index.d.ts +1 -0
  84. package/lib/cjs/entities/requirements/index.js +1 -0
  85. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +18 -6
  86. package/lib/cjs/entities/vaultV1/vaultV1.js +25 -8
  87. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +154 -35
  88. package/lib/cjs/entities/vaultV2/vaultV2.js +249 -23
  89. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +8 -1
  90. package/lib/cjs/entities/vaultV2BlueReallocationData.js +50 -34
  91. package/lib/cjs/errors.d.ts +2 -46
  92. package/lib/cjs/errors.js +2 -46
  93. package/lib/cjs/fetch.d.ts +1 -15
  94. package/lib/cjs/fetch.js +1 -16
  95. package/lib/cjs/helpers/constant.d.ts +0 -2
  96. package/lib/cjs/helpers/constant.js +0 -2
  97. package/lib/cjs/helpers/index.d.ts +4 -3
  98. package/lib/cjs/helpers/index.js +10 -9
  99. package/lib/cjs/helpers/metadata.d.ts +3 -5
  100. package/lib/cjs/helpers/metadata.js +4 -6
  101. package/lib/cjs/helpers/previewVaultV2ForceWithdraw.d.ts +107 -0
  102. package/lib/cjs/helpers/previewVaultV2ForceWithdraw.js +164 -0
  103. package/lib/cjs/helpers/previewVaultV2InKindRedeem.d.ts +1 -0
  104. package/lib/cjs/helpers/previewVaultV2InKindRedeem.js +2 -0
  105. package/lib/cjs/helpers/slippage.d.ts +34 -90
  106. package/lib/cjs/helpers/slippage.js +52 -137
  107. package/lib/cjs/helpers/utilization.d.ts +12 -10
  108. package/lib/cjs/helpers/utilization.js +27 -12
  109. package/lib/cjs/helpers/validate.d.ts +66 -32
  110. package/lib/cjs/helpers/validate.js +84 -57
  111. package/lib/cjs/helpers/validateRequirementSpender.d.ts +1 -1
  112. package/lib/cjs/helpers/validateRequirementSpender.js +1 -3
  113. package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.d.ts +274 -0
  114. package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.js +369 -0
  115. package/lib/cjs/types/action.d.ts +102 -68
  116. package/lib/cjs/types/action.js +11 -4
  117. package/lib/cjs/types/error.d.ts +332 -385
  118. package/lib/cjs/types/error.js +369 -412
  119. package/lib/cjs/types/sharedLiquidity.d.ts +1 -117
  120. package/lib/cjs/types.d.ts +2 -18
  121. package/lib/cjs/utils.d.ts +8 -35
  122. package/lib/cjs/utils.js +11 -36
  123. package/lib/esm/abis.d.ts +3 -2829
  124. package/lib/esm/abis.js +3 -1935
  125. package/lib/esm/actions/blue/borrow.d.ts +3 -2
  126. package/lib/esm/actions/blue/borrow.js +3 -2
  127. package/lib/esm/actions/blue/common.js +8 -37
  128. package/lib/esm/actions/blue/refinance.d.ts +2 -2
  129. package/lib/esm/actions/blue/refinance.js +2 -2
  130. package/lib/esm/actions/blue/repay.d.ts +1 -1
  131. package/lib/esm/actions/blue/repay.js +1 -1
  132. package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +2 -2
  133. package/lib/esm/actions/blue/repayWithdrawCollateral.js +2 -2
  134. package/lib/esm/actions/blue/supply.d.ts +2 -2
  135. package/lib/esm/actions/blue/supply.js +6 -11
  136. package/lib/esm/actions/blue/supplyCollateral.d.ts +2 -2
  137. package/lib/esm/actions/blue/supplyCollateral.js +2 -2
  138. package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +3 -2
  139. package/lib/esm/actions/blue/supplyCollateralBorrow.js +3 -2
  140. package/lib/esm/actions/blue/withdraw.d.ts +3 -3
  141. package/lib/esm/actions/blue/withdraw.js +3 -3
  142. package/lib/esm/actions/blue/withdrawCollateral.d.ts +2 -2
  143. package/lib/esm/actions/blue/withdrawCollateral.js +2 -2
  144. package/lib/esm/actions/bundles/common.d.ts +38 -4
  145. package/lib/esm/actions/bundles/common.js +91 -36
  146. package/lib/esm/actions/index.d.ts +0 -1
  147. package/lib/esm/actions/index.js +0 -1
  148. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +9 -8
  149. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +20 -29
  150. package/lib/esm/actions/requirements/blue/index.d.ts +0 -1
  151. package/lib/esm/actions/requirements/blue/index.js +0 -1
  152. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +20 -14
  153. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +25 -26
  154. package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +4 -4
  155. package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +4 -5
  156. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +21 -12
  157. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +40 -39
  158. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +4 -2
  159. package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +14 -9
  160. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +9 -6
  161. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +22 -17
  162. package/lib/esm/actions/requirements/encode/index.d.ts +0 -1
  163. package/lib/esm/actions/requirements/encode/index.js +0 -1
  164. package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +5 -6
  165. package/lib/esm/actions/requirements/getRequirementsApproval.js +5 -6
  166. package/lib/esm/actions/requirements/index.d.ts +0 -1
  167. package/lib/esm/actions/requirements/index.js +0 -1
  168. package/lib/esm/actions/vaultV1/deposit.d.ts +1 -2
  169. package/lib/esm/actions/vaultV1/deposit.js +1 -2
  170. package/lib/esm/actions/vaultV1/inKindRedeem.d.ts +6 -4
  171. package/lib/esm/actions/vaultV1/inKindRedeem.js +12 -8
  172. package/lib/esm/actions/vaultV2/deposit.d.ts +1 -2
  173. package/lib/esm/actions/vaultV2/deposit.js +1 -2
  174. package/lib/esm/actions/vaultV2/forceWithdraw.d.ts +58 -39
  175. package/lib/esm/actions/vaultV2/forceWithdraw.js +89 -62
  176. package/lib/esm/actions/vaultV2/inKindRedeem.d.ts +6 -4
  177. package/lib/esm/actions/vaultV2/inKindRedeem.js +12 -8
  178. package/lib/esm/augment.d.ts +0 -1
  179. package/lib/esm/augment.js +0 -1
  180. package/lib/esm/blue/abis.d.ts +1 -1
  181. package/lib/esm/blue/abis.js +1 -1
  182. package/lib/esm/blue/entities.d.ts +2 -2
  183. package/lib/esm/blue/entities.js +1 -1
  184. package/lib/esm/blue/errors.d.ts +1 -1
  185. package/lib/esm/blue/errors.js +1 -1
  186. package/lib/esm/blue/fetch.d.ts +1 -1
  187. package/lib/esm/blue/fetch.js +1 -1
  188. package/lib/esm/blue/types.d.ts +2 -4
  189. package/lib/esm/blue/utils.d.ts +1 -3
  190. package/lib/esm/blue/utils.js +1 -3
  191. package/lib/esm/client/morphoViemExtension.d.ts +1 -1
  192. package/lib/esm/client/morphoViemExtension.js +1 -1
  193. package/lib/esm/constants.d.ts +3 -37
  194. package/lib/esm/constants.js +3 -37
  195. package/lib/esm/entities/blue/blue.d.ts +38 -264
  196. package/lib/esm/entities/blue/blue.js +5 -206
  197. package/lib/esm/entities/index.d.ts +3 -60
  198. package/lib/esm/entities/index.js +1 -16
  199. package/lib/esm/entities/midnight/midnight.js +20 -24
  200. package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +7 -6
  201. package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +20 -15
  202. package/lib/esm/{actions/requirements/blue → entities/requirements}/getUnusedPermit2Nonce.js +3 -3
  203. package/lib/esm/entities/requirements/index.d.ts +1 -0
  204. package/lib/esm/entities/requirements/index.js +1 -0
  205. package/lib/esm/entities/vaultV1/vaultV1.d.ts +18 -6
  206. package/lib/esm/entities/vaultV1/vaultV1.js +29 -12
  207. package/lib/esm/entities/vaultV2/vaultV2.d.ts +154 -35
  208. package/lib/esm/entities/vaultV2/vaultV2.js +256 -30
  209. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +8 -1
  210. package/lib/esm/entities/vaultV2BlueReallocationData.js +48 -32
  211. package/lib/esm/errors.d.ts +2 -46
  212. package/lib/esm/errors.js +2 -46
  213. package/lib/esm/fetch.d.ts +1 -15
  214. package/lib/esm/fetch.js +1 -15
  215. package/lib/esm/helpers/constant.d.ts +0 -2
  216. package/lib/esm/helpers/constant.js +0 -2
  217. package/lib/esm/helpers/index.d.ts +4 -3
  218. package/lib/esm/helpers/index.js +4 -3
  219. package/lib/esm/helpers/metadata.d.ts +3 -5
  220. package/lib/esm/helpers/metadata.js +4 -6
  221. package/lib/esm/helpers/previewVaultV2ForceWithdraw.d.ts +107 -0
  222. package/lib/esm/helpers/previewVaultV2ForceWithdraw.js +161 -0
  223. package/lib/esm/helpers/previewVaultV2InKindRedeem.d.ts +1 -0
  224. package/lib/esm/helpers/previewVaultV2InKindRedeem.js +2 -0
  225. package/lib/esm/helpers/slippage.d.ts +34 -90
  226. package/lib/esm/helpers/slippage.js +52 -134
  227. package/lib/esm/helpers/utilization.d.ts +12 -10
  228. package/lib/esm/helpers/utilization.js +26 -11
  229. package/lib/esm/helpers/validate.d.ts +66 -32
  230. package/lib/esm/helpers/validate.js +83 -57
  231. package/lib/esm/helpers/validateRequirementSpender.d.ts +1 -1
  232. package/lib/esm/helpers/validateRequirementSpender.js +1 -3
  233. package/lib/esm/helpers/vaultV2ForceWithdrawPlan.d.ts +274 -0
  234. package/lib/esm/helpers/vaultV2ForceWithdrawPlan.js +362 -0
  235. package/lib/esm/types/action.d.ts +102 -68
  236. package/lib/esm/types/action.js +12 -5
  237. package/lib/esm/types/error.d.ts +332 -385
  238. package/lib/esm/types/error.js +355 -392
  239. package/lib/esm/types/sharedLiquidity.d.ts +1 -117
  240. package/lib/esm/types.d.ts +2 -18
  241. package/lib/esm/utils.d.ts +8 -35
  242. package/lib/esm/utils.js +7 -34
  243. package/package.json +7 -15
  244. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2Approve.d.ts +0 -42
  245. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2Approve.js +0 -83
  246. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirements.d.ts +0 -88
  247. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirements.js +0 -151
  248. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +0 -37
  249. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.js +0 -50
  250. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.d.ts +0 -57
  251. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.js +0 -74
  252. package/lib/cjs/actions/requirements/generalAdapter/index.d.ts +0 -3
  253. package/lib/cjs/actions/requirements/generalAdapter/index.js +0 -19
  254. package/lib/cjs/actions/signatures/getBlueAuthorizationAction.d.ts +0 -29
  255. package/lib/cjs/actions/signatures/getBlueAuthorizationAction.js +0 -48
  256. package/lib/cjs/actions/signatures/getTokenRequirementActions.d.ts +0 -58
  257. package/lib/cjs/actions/signatures/getTokenRequirementActions.js +0 -119
  258. package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +0 -44
  259. package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.js +0 -43
  260. package/lib/cjs/actions/signatures/index.d.ts +0 -3
  261. package/lib/cjs/actions/signatures/index.js +0 -19
  262. package/lib/cjs/augment/VaultMarketPublicAllocatorConfig.d.ts +0 -8
  263. package/lib/cjs/augment/VaultMarketPublicAllocatorConfig.js +0 -8
  264. package/lib/cjs/bundler/actions.d.ts +0 -797
  265. package/lib/cjs/bundler/actions.js +0 -1371
  266. package/lib/cjs/bundler/index.d.ts +0 -2
  267. package/lib/cjs/bundler/index.js +0 -18
  268. package/lib/cjs/bundler/types.d.ts +0 -235
  269. package/lib/cjs/bundler/types.js +0 -2
  270. package/lib/cjs/entities/vaultV1ReallocationData.d.ts +0 -326
  271. package/lib/cjs/entities/vaultV1ReallocationData.js +0 -542
  272. package/lib/cjs/helpers/computeVaultV1Reallocations.d.ts +0 -78
  273. package/lib/cjs/helpers/computeVaultV1Reallocations.js +0 -243
  274. package/lib/esm/actions/requirements/encode/encodeErc20Permit2Approve.d.ts +0 -42
  275. package/lib/esm/actions/requirements/encode/encodeErc20Permit2Approve.js +0 -79
  276. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirements.d.ts +0 -88
  277. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirements.js +0 -147
  278. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +0 -37
  279. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.js +0 -46
  280. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.d.ts +0 -57
  281. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.js +0 -70
  282. package/lib/esm/actions/requirements/generalAdapter/index.d.ts +0 -3
  283. package/lib/esm/actions/requirements/generalAdapter/index.js +0 -3
  284. package/lib/esm/actions/signatures/getBlueAuthorizationAction.d.ts +0 -29
  285. package/lib/esm/actions/signatures/getBlueAuthorizationAction.js +0 -44
  286. package/lib/esm/actions/signatures/getTokenRequirementActions.d.ts +0 -58
  287. package/lib/esm/actions/signatures/getTokenRequirementActions.js +0 -115
  288. package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +0 -44
  289. package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.js +0 -39
  290. package/lib/esm/actions/signatures/index.d.ts +0 -3
  291. package/lib/esm/actions/signatures/index.js +0 -3
  292. package/lib/esm/augment/VaultMarketPublicAllocatorConfig.d.ts +0 -8
  293. package/lib/esm/augment/VaultMarketPublicAllocatorConfig.js +0 -5
  294. package/lib/esm/bundler/actions.d.ts +0 -797
  295. package/lib/esm/bundler/actions.js +0 -1368
  296. package/lib/esm/bundler/index.d.ts +0 -2
  297. package/lib/esm/bundler/index.js +0 -2
  298. package/lib/esm/bundler/types.d.ts +0 -235
  299. package/lib/esm/bundler/types.js +0 -1
  300. package/lib/esm/entities/vaultV1ReallocationData.d.ts +0 -326
  301. package/lib/esm/entities/vaultV1ReallocationData.js +0 -544
  302. package/lib/esm/helpers/computeVaultV1Reallocations.d.ts +0 -78
  303. package/lib/esm/helpers/computeVaultV1Reallocations.js +0 -239
  304. /package/lib/cjs/{actions/requirements/blue → entities/requirements}/getUnusedPermit2Nonce.d.ts +0 -0
  305. /package/lib/esm/{actions/requirements/blue → entities/requirements}/getUnusedPermit2Nonce.d.ts +0 -0
@@ -1,544 +0,0 @@
1
- import { AccrualPosition, Market, MarketUtils, MathLib, Position, UnknownDataError, Vault, VaultMarketConfig, VaultMarketPublicAllocatorConfig, } from "@morpho-org/blue-sdk";
2
- import { _try, bigIntComparator } from "@morpho-org/morpho-ts";
3
- import { DEFAULT_SUPPLY_TARGET_UTILIZATION, DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, } from "../helpers/constant.js";
4
- import { getSupplyTargetUtilization } from "../helpers/utilization.js";
5
- import { DisabledReallocationMarketError, MissingPublicAllocatorConfigError, UnknownReallocationMarketError, UnknownReallocationPositionError, UnknownReallocationVaultError, UnknownReallocationVaultMarketConfigError, } from "../types/index.js";
6
- /**
7
- * Clones a market so simulated interest and liquidity changes never mutate caller input.
8
- *
9
- * @internal
10
- */
11
- const cloneMarket = (market) => new Market({ ...market });
12
- /**
13
- * Clones a market position before simulated supply or withdrawal changes are applied.
14
- *
15
- * @internal
16
- */
17
- const clonePosition = (position) => new Position({ ...position });
18
- /**
19
- * Clones vault state, including queue arrays and public allocator accounting.
20
- *
21
- * @internal
22
- */
23
- const cloneVault = (vault) => new Vault({
24
- ...vault,
25
- pendingTimelock: { ...vault.pendingTimelock },
26
- pendingGuardian: { ...vault.pendingGuardian },
27
- supplyQueue: [...vault.supplyQueue],
28
- withdrawQueue: [...vault.withdrawQueue],
29
- publicAllocatorConfig: vault.publicAllocatorConfig == null
30
- ? undefined
31
- : { ...vault.publicAllocatorConfig },
32
- });
33
- /**
34
- * Clones a vault-market config, including nested public allocator limits.
35
- *
36
- * @internal
37
- */
38
- const cloneVaultMarketConfig = (config) => new VaultMarketConfig({
39
- ...config,
40
- pendingCap: { ...config.pendingCap },
41
- publicAllocatorConfig: config.publicAllocatorConfig == null
42
- ? undefined
43
- : new VaultMarketPublicAllocatorConfig({
44
- ...config.publicAllocatorConfig,
45
- }),
46
- });
47
- /**
48
- * Narrow state container for computing public allocator reallocations.
49
- *
50
- * @remarks
51
- * The class owns only the market, vault, position, vault-market-config,
52
- * and chain data needed by the shared-liquidity algorithm. Constructor inputs
53
- * are cloned, and simulation steps return cloned `VaultV1ReallocationData` instances
54
- * so fetched caller inputs are not mutated.
55
- *
56
- * Public records are exposed for inspection and snapshotting only. Treat
57
- * `markets`, `vaults`, `positions`, and `vaultMarketConfigs` as a read
58
- * contract keyed by market id or address; use the getters for typed absence
59
- * errors and use simulation methods to produce updated state.
60
- *
61
- * @deprecated Vault V1 shared-liquidity planning will be removed in the next major. Use
62
- * `VaultV2BlueReallocationData`.
63
- */
64
- export class VaultV1ReallocationData {
65
- /** Chain id associated with the fetched reallocation data. */
66
- chainId;
67
- /** Markets indexed by market id. */
68
- markets;
69
- /** Vaults indexed by vault address. */
70
- vaults;
71
- /** Positions indexed by user or vault address, then by market id. */
72
- positions;
73
- /** Vault market configs indexed by vault address, then by market id. */
74
- vaultMarketConfigs;
75
- /**
76
- * Creates a cloned reallocation state from fetched market, vault, position,
77
- * and vault-market-config data.
78
- *
79
- * @param input - Reallocation input data fetched at a consistent chain state.
80
- */
81
- constructor(input) {
82
- const { chainId, markets, vaults, positions, vaultMarketConfigs } = input;
83
- this.chainId = chainId;
84
- this.markets = {};
85
- this.vaults = {};
86
- this.positions = {};
87
- this.vaultMarketConfigs = {};
88
- for (const [id, market] of Object.entries(markets ?? {})) {
89
- this.markets[id] = market == null ? undefined : cloneMarket(market);
90
- }
91
- for (const [address, vault] of Object.entries(vaults ?? {})) {
92
- this.vaults[address] = vault == null ? undefined : cloneVault(vault);
93
- }
94
- for (const [user, byMarket] of Object.entries(positions ?? {})) {
95
- this.positions[user] = {};
96
- for (const [id, position] of Object.entries(byMarket)) {
97
- this.positions[user][id] =
98
- position == null ? undefined : clonePosition(position);
99
- }
100
- }
101
- for (const [vault, byMarket] of Object.entries(vaultMarketConfigs ?? {})) {
102
- this.vaultMarketConfigs[vault] = {};
103
- for (const [id, config] of Object.entries(byMarket)) {
104
- this.vaultMarketConfigs[vault][id] =
105
- config == null ? undefined : cloneVaultMarketConfig(config);
106
- }
107
- }
108
- }
109
- /**
110
- * Creates a deep clone of this reallocation state.
111
- *
112
- * @returns A new `VaultV1ReallocationData` instance with cloned entity objects.
113
- */
114
- clone() {
115
- return new VaultV1ReallocationData(this);
116
- }
117
- forkAliasedState() {
118
- const data = new VaultV1ReallocationData({ chainId: this.chainId });
119
- Object.assign(data.markets, this.markets);
120
- Object.assign(data.vaults, this.vaults);
121
- for (const [user, positions] of Object.entries(this.positions)) {
122
- data.positions[user] = positions;
123
- }
124
- for (const [vault, configs] of Object.entries(this.vaultMarketConfigs)) {
125
- data.vaultMarketConfigs[vault] = configs;
126
- }
127
- return data;
128
- }
129
- /**
130
- * Gets a market by id.
131
- *
132
- * @param marketId - Market id to read.
133
- * @returns The cloned market data.
134
- * @throws {@link UnknownReallocationMarketError} when the market is absent.
135
- */
136
- getMarket(marketId) {
137
- const market = this.markets[marketId];
138
- if (market == null)
139
- throw new UnknownReallocationMarketError(marketId);
140
- return market;
141
- }
142
- /**
143
- * Gets a vault by address.
144
- *
145
- * @param vault - Vault address to read.
146
- * @returns The cloned vault data.
147
- * @throws {@link UnknownReallocationVaultError} when the vault is absent.
148
- */
149
- getVault(vault) {
150
- const data = this.vaults[vault];
151
- if (data == null)
152
- throw new UnknownReallocationVaultError(vault);
153
- return data;
154
- }
155
- /**
156
- * Gets a raw market position.
157
- *
158
- * @param user - Position owner address, usually a MetaMorpho vault.
159
- * @param marketId - Market id for the position.
160
- * @returns The cloned position data.
161
- * @throws {@link UnknownReallocationPositionError} when the position is absent.
162
- */
163
- getPosition(user, marketId) {
164
- const position = this.positions[user]?.[marketId];
165
- if (position == null)
166
- throw new UnknownReallocationPositionError(user, marketId);
167
- return position;
168
- }
169
- /**
170
- * Gets a position wrapped with its market accrual helpers.
171
- *
172
- * @param user - Position owner address, usually a MetaMorpho vault.
173
- * @param marketId - Market id for the position.
174
- * @returns Accrual-aware position data.
175
- * @throws {@link UnknownReallocationPositionError} when the position is absent.
176
- * @throws {@link UnknownReallocationMarketError} when the market is absent.
177
- */
178
- getAccrualPosition(user, marketId) {
179
- return new AccrualPosition(this.getPosition(user, marketId), this.getMarket(marketId));
180
- }
181
- /**
182
- * Gets a vault-market config.
183
- *
184
- * @param vault - Vault address.
185
- * @param marketId - Market id configured by the vault.
186
- * @returns The cloned vault-market config.
187
- * @throws {@link UnknownReallocationVaultMarketConfigError} when the config is absent.
188
- */
189
- getVaultMarketConfig(vault, marketId) {
190
- const config = this.vaultMarketConfigs[vault]?.[marketId];
191
- if (config == null)
192
- throw new UnknownReallocationVaultMarketConfigError(vault, marketId);
193
- return config;
194
- }
195
- /**
196
- * Calculates public reallocations that can supply liquidity to `marketId`.
197
- *
198
- * @remarks
199
- * The algorithm repeatedly chooses the single largest currently available
200
- * source-market withdrawal across reallocatable vaults, applies that
201
- * withdrawal to a cloned state, and repeats until no valid withdrawal remains.
202
- *
203
- * Pass `options.timestamp` to evaluate market interest and pending public
204
- * allocator caps at the same block as the fetched reallocation data.
205
- * This method does not add an implicit delay margin before measuring
206
- * target-market vault headroom. If a transaction may land later than the
207
- * fetched block, pass a future `timestamp` or reserve your own headroom so
208
- * interest accrued before inclusion does not make `reallocateTo` exceed the
209
- * target market cap.
210
- * Returned `data` normalizes `vault.publicAllocatorConfig.accruedFee` to the
211
- * on-chain `reallocateTo` fee semantics: one fee charge per vault with at
212
- * least one computed withdrawal.
213
- *
214
- * @param marketId - Target market to supply with shared liquidity.
215
- * @param options - Optional allocator discovery options.
216
- * @returns Computed source-market withdrawals and the post-reallocation state.
217
- * @throws {@link UnknownReallocationMarketError} when the target market is absent.
218
- * @deprecated Vault V1 shared-liquidity planning will be removed in the next major. Use
219
- * `VaultV2BlueReallocationData.computeVaultV2BlueReallocations`.
220
- * @example
221
- * ```ts
222
- * import { createPublicClient, http } from "viem";
223
- * import { mainnet } from "viem/chains";
224
- * import { markets, vaults } from "@morpho-org/morpho-test";
225
- * import {
226
- * morphoViemExtension,
227
- * type PublicReallocation,
228
- * } from "@morpho-org/morpho-sdk";
229
- * import type { VaultV1ReallocationData } from "@morpho-org/morpho-sdk/entities";
230
- *
231
- * const client = createPublicClient({
232
- * chain: mainnet,
233
- * transport: http(),
234
- * }).extend(morphoViemExtension());
235
- *
236
- * const marketParams = markets[mainnet.id].usdc_wbtc;
237
- * const market = client.morpho.blue(marketParams, mainnet.id);
238
- * const block = await client.getBlock();
239
- * const reallocationData = await market.getVaultV1ReallocationData({
240
- * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
241
- * block: { number: block.number, timestamp: block.timestamp },
242
- * });
243
- *
244
- * const result: {
245
- * withdrawals: readonly PublicReallocation[];
246
- * data: VaultV1ReallocationData;
247
- * } = reallocationData.computeVaultV1Reallocations(marketParams.id, {
248
- * timestamp: block.timestamp,
249
- * });
250
- * ```
251
- */
252
- computeVaultV1Reallocations(marketId, options = {}) {
253
- const { enabled = true, timestamp, reallocatableVaults, defaultMaxWithdrawalUtilization = DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, maxWithdrawalUtilization = {}, } = options;
254
- if (!enabled)
255
- return { withdrawals: [], data: this };
256
- const accrualTimestamp = BigInt(timestamp ?? this.getMarket(marketId).lastUpdate);
257
- const configuredVaults = Object.keys(this.vaultMarketConfigs);
258
- const vaultKeyByLower = new Map(configuredVaults.map((vault) => [vault.toLowerCase(), vault]));
259
- const vaults = Array.from(new Set([...(reallocatableVaults ?? configuredVaults)]
260
- .map((vault) => vaultKeyByLower.get(vault.toLowerCase()))
261
- .filter((vault) => vault != null))).filter((vault) => {
262
- const vaultMarketConfig = this.vaultMarketConfigs[vault]?.[marketId];
263
- return (vaultMarketConfig?.enabled === true &&
264
- this.vaults[vault]?.publicAllocatorConfig != null);
265
- });
266
- let data = this.clone();
267
- const withdrawals = [];
268
- while (true) {
269
- const vaultWithdrawals = vaults
270
- .map((vaultAddress) => data.getLargestVaultWithdrawal({
271
- vault: vaultAddress,
272
- marketId,
273
- withdrawals,
274
- defaultMaxWithdrawalUtilization,
275
- maxWithdrawalUtilization,
276
- timestamp: accrualTimestamp,
277
- }))
278
- .filter((withdrawal) => withdrawal?.largestWithdrawal != null)
279
- .sort(bigIntComparator((withdrawal) => withdrawal.largestWithdrawal.assets, "desc"));
280
- const largestVaultWithdrawal = vaultWithdrawals[0];
281
- if (largestVaultWithdrawal?.largestWithdrawal == null) {
282
- const withdrawalCountsByVault = new Map();
283
- for (const { vault } of withdrawals) {
284
- withdrawalCountsByVault.set(vault, (withdrawalCountsByVault.get(vault) ?? 0) + 1);
285
- }
286
- for (const [vault, count] of withdrawalCountsByVault) {
287
- if (count <= 1)
288
- continue;
289
- const vaultPublicAllocatorConfig = data.getVault(vault).publicAllocatorConfig;
290
- /* v8 ignore next: repeated withdrawals require a configured public allocator earlier in the flow. */
291
- if (vaultPublicAllocatorConfig == null)
292
- throw new MissingPublicAllocatorConfigError(vault);
293
- vaultPublicAllocatorConfig.accruedFee -=
294
- BigInt(count - 1) * vaultPublicAllocatorConfig.fee;
295
- }
296
- return { withdrawals, data };
297
- }
298
- const { vault, largestWithdrawal } = largestVaultWithdrawal;
299
- withdrawals.push({ ...largestWithdrawal, vault });
300
- data = data.applyPublicReallocation({
301
- vault,
302
- supplyMarketId: marketId,
303
- withdrawal: largestWithdrawal,
304
- timestamp: accrualTimestamp,
305
- });
306
- }
307
- }
308
- /**
309
- * Calculates Vault V1 public reallocations that can supply liquidity to `marketId`.
310
- *
311
- * @param marketId - Target market to supply with shared liquidity.
312
- * @param options - Optional allocator discovery options.
313
- * @returns Computed source-market withdrawals and the post-reallocation state.
314
- * @throws {@link UnknownReallocationMarketError} when the target market is absent.
315
- * @deprecated Vault V1 shared-liquidity planning will be removed in the next major. Use
316
- * `VaultV2BlueReallocationData.computeVaultV2BlueReallocations`.
317
- */
318
- getMarketPublicReallocations(marketId, options = {}) {
319
- return this.computeVaultV1Reallocations(marketId, options);
320
- }
321
- /**
322
- * Sums the public-allocator liquidity reallocatable into `marketId` from
323
- * sibling markets.
324
- *
325
- * Read-only metric — never throws on insufficiency (returns `0n`). Bounded by
326
- * each source market's withdrawal utilization cap and the target market's
327
- * vault supply-cap headroom. Pass `options.defaultMaxWithdrawalUtilization` to
328
- * widen the source ceiling (e.g. `MathLib.WAD` for the full drain).
329
- *
330
- * @param marketId - Target market that would receive the liquidity.
331
- * @param options - Optional allocator discovery options.
332
- * @returns Total reallocatable assets in loan-token units; `0n` when none is available.
333
- * @throws {@link UnknownReallocationMarketError} when the target market is absent.
334
- * @deprecated Vault V1 shared-liquidity metrics will be removed in the next major. Use
335
- * `VaultV2BlueReallocationData.getPublicReallocationLiquidity`.
336
- * @example
337
- * ```ts
338
- * import { createPublicClient, http } from "viem";
339
- * import { mainnet } from "viem/chains";
340
- * import { markets, vaults } from "@morpho-org/morpho-test";
341
- * import { morphoViemExtension } from "@morpho-org/morpho-sdk";
342
- *
343
- * const client = createPublicClient({
344
- * chain: mainnet,
345
- * transport: http(),
346
- * }).extend(morphoViemExtension());
347
- *
348
- * const marketParams = markets[mainnet.id].usdc_wbtc;
349
- * const market = client.morpho.blue(marketParams, mainnet.id);
350
- * const block = await client.getBlock();
351
- * const reallocationData = await market.getVaultV1ReallocationData({
352
- * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
353
- * block: { number: block.number, timestamp: block.timestamp },
354
- * });
355
- *
356
- * const liquidity: bigint = reallocationData.getPublicReallocationLiquidity(
357
- * marketParams.id,
358
- * { timestamp: block.timestamp },
359
- * );
360
- * ```
361
- */
362
- getPublicReallocationLiquidity(marketId, options) {
363
- const { withdrawals } = this.computeVaultV1Reallocations(marketId, options);
364
- return withdrawals.reduce((total, { assets }) => total + assets, 0n);
365
- }
366
- /**
367
- * Computes the liquidity available to bring `marketId` to `utilization`,
368
- * counting the public-allocator liquidity reallocatable into it.
369
- *
370
- * Returns the max borrow `x` keeping post-borrow utilization
371
- * `(borrow + x) / (supply + L) ≤ utilization`, where `L` is the
372
- * reallocatable liquidity added to the market's supply — equivalently
373
- * `getBorrowToUtilization({ supply + L, borrow }, utilization)`. Below
374
- * `utilization` this is the market's own borrow headroom plus `utilization · L`;
375
- * reallocated supply also raises the supply denominator, so only that scaled
376
- * share backs further borrow.
377
- *
378
- * Read-only metric — never throws on insufficiency:
379
- * - returns only the market's own borrow headroom when
380
- * `supplyTargetUtilization > utilization` (reallocation would not
381
- * trigger at that utilization);
382
- * - returns `0n` when the market is already at or above `utilization` and `L`
383
- * is too small to bring it back under.
384
- *
385
- * @param marketId - Target market to borrow from.
386
- * @param utilization - Utilization to bring the market to, scaled by WAD. Defaults to {@link DEFAULT_SUPPLY_TARGET_UTILIZATION}.
387
- * @param options - Optional reallocation options (supply target utilization trigger, timestamp, withdrawal caps).
388
- * @returns Available liquidity to the given utilization in loan-token units; `0n` when none is available.
389
- * @throws {@link UnknownReallocationMarketError} when the target market is absent.
390
- * @deprecated Vault V1 shared-liquidity metrics will be removed in the next major. Use
391
- * `VaultV2BlueReallocationData.getAvailableLiquidityToUtilization`.
392
- * @example
393
- * ```ts
394
- * import { createPublicClient, http, parseEther } from "viem";
395
- * import { mainnet } from "viem/chains";
396
- * import { markets, vaults } from "@morpho-org/morpho-test";
397
- * import { morphoViemExtension } from "@morpho-org/morpho-sdk";
398
- *
399
- * const client = createPublicClient({
400
- * chain: mainnet,
401
- * transport: http(),
402
- * }).extend(morphoViemExtension());
403
- *
404
- * const marketParams = markets[mainnet.id].usdc_wbtc;
405
- * const market = client.morpho.blue(marketParams, mainnet.id);
406
- * const block = await client.getBlock();
407
- * const reallocationData = await market.getVaultV1ReallocationData({
408
- * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
409
- * block: { number: block.number, timestamp: block.timestamp },
410
- * });
411
- *
412
- * // Max borrow keeping utilization at or below 90%, counting shared liquidity.
413
- * const available: bigint =
414
- * reallocationData.getAvailableLiquidityToUtilization(
415
- * marketParams.id,
416
- * parseEther("0.9"),
417
- * { timestamp: block.timestamp },
418
- * );
419
- * ```
420
- */
421
- // biome-ignore lint/complexity/useMaxParams: (marketId, utilization, options) is the metric's public API
422
- getAvailableLiquidityToUtilization(marketId, utilization = DEFAULT_SUPPLY_TARGET_UTILIZATION, options) {
423
- const market = this.getMarket(marketId).accrueInterest(options?.timestamp);
424
- // Below the allocator's trigger, no reallocation happens: own headroom only.
425
- const supplyTargetUtilization = getSupplyTargetUtilization(marketId, options);
426
- if (supplyTargetUtilization > utilization)
427
- return market.getBorrowToUtilization(utilization);
428
- // Borrow-to-target on the post-reallocation supply; clamps to 0n above target.
429
- const availableLiquidity = this.getPublicReallocationLiquidity(marketId, options);
430
- return MarketUtils.getBorrowToUtilization({
431
- totalSupplyAssets: market.totalSupplyAssets + availableLiquidity,
432
- totalBorrowAssets: market.totalBorrowAssets,
433
- }, utilization);
434
- }
435
- /**
436
- * Gets the largest currently valid source-market withdrawal for a vault.
437
- *
438
- * @param params - Candidate vault, target market, prior withdrawals, and allocator limits.
439
- * @returns The largest withdrawal candidate, or `undefined` if required data is missing.
440
- *
441
- * @internal
442
- */
443
- getLargestVaultWithdrawal({ vault, marketId, withdrawals, defaultMaxWithdrawalUtilization, maxWithdrawalUtilization, timestamp, }) {
444
- return _try(() => {
445
- const { cap, pendingCap, publicAllocatorConfig } = this.getVaultMarketConfig(vault, marketId);
446
- const validCap = pendingCap.validAt >= timestamp
447
- ? MathLib.min(pendingCap.value, cap)
448
- : cap;
449
- const suppliable = MathLib.zeroFloorSub(validCap, this.getAccrualPosition(vault, marketId).accrueInterest(timestamp)
450
- .supplyAssets);
451
- const marketWithdrawals = this.getVault(vault)
452
- .withdrawQueue.filter((srcMarketId) => srcMarketId !== marketId &&
453
- !withdrawals.some((withdrawal) => withdrawal.id === srcMarketId && withdrawal.vault === vault))
454
- .map((srcMarketId) => {
455
- const withdrawal = _try(() => {
456
- const srcPosition = this.getAccrualPosition(vault, srcMarketId).accrueInterest(timestamp);
457
- const targetUtilizationLiquidity = srcPosition.market.getWithdrawToUtilization(maxWithdrawalUtilization[srcMarketId] ??
458
- defaultMaxWithdrawalUtilization);
459
- const srcConfig = this.getVaultMarketConfig(vault, srcMarketId);
460
- if (!srcConfig.enabled)
461
- return { id: srcMarketId, assets: 0n };
462
- return {
463
- id: srcMarketId,
464
- assets: MathLib.min(srcPosition.supplyAssets, targetUtilizationLiquidity, suppliable, publicAllocatorConfig?.maxIn ?? 0n, srcConfig.publicAllocatorConfig?.maxOut ?? 0n),
465
- };
466
- }, UnknownDataError);
467
- return withdrawal ?? { id: srcMarketId, assets: 0n };
468
- })
469
- .filter(({ assets }) => assets > 0n)
470
- .sort(bigIntComparator(({ assets }) => assets, "desc"));
471
- return { vault, largestWithdrawal: marketWithdrawals[0] };
472
- }, UnknownDataError);
473
- }
474
- /**
475
- * Applies one public allocator withdrawal and corresponding target-market supply.
476
- *
477
- * @param vault - Vault performing the public reallocation.
478
- * @param supplyMarketId - Target market that receives the withdrawn assets.
479
- * @param withdrawal - Source market id and asset amount to withdraw.
480
- * @param timestamp - Timestamp used to accrue source and target positions.
481
- * @returns A cloned state after applying the simulated reallocation.
482
- * @throws {@link MissingPublicAllocatorConfigError} when the vault allocator config is absent.
483
- * @throws {@link UnknownReallocationVaultMarketConfigError} when source or target allocator limits are absent.
484
- *
485
- * @internal
486
- */
487
- applyPublicReallocation({ vault, supplyMarketId, withdrawal, timestamp, }) {
488
- const data = this.forkAliasedState();
489
- data.vaults[vault] = cloneVault(this.getVault(vault));
490
- /* v8 ignore next: forkAliasedState keeps vault position buckets initialized for known vaults. */
491
- data.positions[vault] = { ...(this.positions[vault] ?? {}) };
492
- /* v8 ignore next: forkAliasedState keeps vault market config buckets initialized for known vaults. */
493
- data.vaultMarketConfigs[vault] = {
494
- ...(this.vaultMarketConfigs[vault] ?? {}),
495
- };
496
- const vaultPublicAllocatorConfig = data.getVault(vault).publicAllocatorConfig;
497
- if (vaultPublicAllocatorConfig == null)
498
- throw new MissingPublicAllocatorConfigError(vault);
499
- vaultPublicAllocatorConfig.accruedFee += vaultPublicAllocatorConfig.fee;
500
- const sourceConfig = cloneVaultMarketConfig(this.getVaultMarketConfig(vault, withdrawal.id));
501
- data.vaultMarketConfigs[vault][withdrawal.id] = sourceConfig;
502
- if (!sourceConfig.enabled)
503
- throw new DisabledReallocationMarketError(vault, withdrawal.id);
504
- const sourcePublicAllocatorConfig = sourceConfig.publicAllocatorConfig;
505
- if (sourcePublicAllocatorConfig == null)
506
- throw new UnknownReallocationVaultMarketConfigError(vault, withdrawal.id);
507
- const supplyConfig = supplyMarketId === withdrawal.id
508
- ? sourceConfig
509
- : cloneVaultMarketConfig(this.getVaultMarketConfig(vault, supplyMarketId));
510
- data.vaultMarketConfigs[vault][supplyMarketId] = supplyConfig;
511
- if (!supplyConfig.enabled)
512
- throw new DisabledReallocationMarketError(vault, supplyMarketId);
513
- const supplyPublicAllocatorConfig = supplyConfig.publicAllocatorConfig;
514
- if (supplyPublicAllocatorConfig == null)
515
- throw new UnknownReallocationVaultMarketConfigError(vault, supplyMarketId);
516
- sourcePublicAllocatorConfig.maxIn += withdrawal.assets;
517
- sourcePublicAllocatorConfig.maxOut -= withdrawal.assets;
518
- supplyPublicAllocatorConfig.maxIn -= withdrawal.assets;
519
- supplyPublicAllocatorConfig.maxOut += withdrawal.assets;
520
- const { position: sourcePosition } = data
521
- .getAccrualPosition(vault, withdrawal.id)
522
- .withdraw(withdrawal.assets, 0n, timestamp);
523
- data.markets[withdrawal.id] = cloneMarket(sourcePosition.market);
524
- data.positions[vault] ??= {};
525
- data.positions[vault][withdrawal.id] = clonePosition(sourcePosition);
526
- const { position: supplyPosition } = data
527
- .getAccrualPosition(vault, supplyMarketId)
528
- .supply(withdrawal.assets, 0n, timestamp);
529
- data.markets[supplyMarketId] = cloneMarket(supplyPosition.market);
530
- data.positions[vault][supplyMarketId] = clonePosition(supplyPosition);
531
- void _try(() => {
532
- const vaultData = data.getVault(vault);
533
- vaultData.totalAssets = vaultData.withdrawQueue.reduce((total, marketId) => total + data.getAccrualPosition(vault, marketId).supplyAssets, 0n);
534
- }, UnknownDataError);
535
- return data;
536
- }
537
- }
538
- /**
539
- * Deprecated class name for Vault V1 reallocation data.
540
- *
541
- * @deprecated Vault V1 shared-liquidity planning will be removed in the next major. Use
542
- * `VaultV2BlueReallocationData`.
543
- */
544
- export { VaultV1ReallocationData as ReallocationData };
@@ -1,78 +0,0 @@
1
- import { type MarketId } from "@morpho-org/blue-sdk";
2
- import type { VaultV1ReallocationData } from "../entities/vaultV1ReallocationData.js";
3
- import { type ReallocationComputeOptions, type VaultV1Reallocation } from "../types/index.js";
4
- /**
5
- * Computes vault reallocations for a `borrow` or `withdraw` on a target market.
6
- *
7
- * First attempts "friendly" reallocations respecting withdrawal utilization
8
- * targets, then falls back to aggressive reallocations (100% withdrawal
9
- * utilization) if liquidity is still insufficient.
10
- *
11
- * Algebra branches on `operation`:
12
- * - `"borrow"`: `S' = S`, `B' = B + amount` (additional borrow demand).
13
- * - `"withdraw"`: `S' = S − amount`, `B' = B` (supply-side shrinkage).
14
- *
15
- * In both cases reallocated assets are added on the supply side; `requiredAssets`
16
- * and `absoluteShortfall` are derived from the operation-specific post-state.
17
- *
18
- * @remarks Pass `options.timestamp` from the same block used to fetch `reallocationData`; when omitted, market accrual falls back to the target market's `lastUpdate`, which can diverge from the source rows' fetch block. Per-market `maxWithdrawalUtilization` overrides apply only to phase 1; phase 2 forces 100% utilization on every source market.
19
- * @param params.reallocationData - The local state containing market, vault, and position data.
20
- * @param params.marketId - The target market to reallocate liquidity into.
21
- * @param params.operation - The operation driving the reallocation (`"borrow"` or `"withdraw"`).
22
- * @param params.amount - The borrow or withdraw amount used to compute the post-state utilization.
23
- * @param params.options - Optional reallocation computation options.
24
- * @returns Array of vault reallocations, sorted with withdrawals in ascending market id order.
25
- * @throws {InsufficientSharedLiquidityError} when shared liquidity cannot cover the operation's absolute shortfall on the target market — preventing fee-bearing reallocations from being attached to a call that would still revert onchain.
26
- * @throws {ReallocationWithdrawExceedsMarketSupplyError} when `operation === "withdraw"` and `amount` exceeds the target market's `totalSupplyAssets` — the on-chain call would revert regardless of reallocations.
27
- * @throws {MissingPublicAllocatorConfigError} when a selected vault is missing its public allocator config.
28
- * @deprecated Vault V1 shared-liquidity planning will be removed in the next major. Use
29
- * `VaultV2BlueReallocationData.computeVaultV2BlueReallocations`.
30
- * @example
31
- * ```ts
32
- * import { createPublicClient, http, parseUnits } from "viem";
33
- * import { mainnet } from "viem/chains";
34
- * import { markets, vaults } from "@morpho-org/morpho-test";
35
- * import {
36
- * computeVaultV1Reallocations,
37
- * morphoViemExtension,
38
- * } from "@morpho-org/morpho-sdk";
39
- *
40
- * const client = createPublicClient({
41
- * chain: mainnet,
42
- * transport: http(),
43
- * }).extend(morphoViemExtension());
44
- *
45
- * const marketParams = markets[mainnet.id].usdc_wbtc;
46
- * const market = client.morpho.blue(marketParams, mainnet.id);
47
- * const block = await client.getBlock();
48
- * const reallocationData = await market.getVaultV1ReallocationData({
49
- * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
50
- * block: { number: block.number, timestamp: block.timestamp },
51
- * });
52
- * const borrowAmount = parseUnits("1000", 6);
53
- * const reallocations = computeVaultV1Reallocations({
54
- * reallocationData,
55
- * marketId: marketParams.id,
56
- * operation: "borrow",
57
- * amount: borrowAmount,
58
- * options: { timestamp: block.timestamp },
59
- * });
60
- * // Encode `reallocations` explicitly with
61
- * // BundlerAction.publicAllocatorReallocateTo(...) when composing a low-level bundle.
62
- * // High-level Blue writes accept Vault V2 reallocations only.
63
- * ```
64
- */
65
- export declare const computeVaultV1Reallocations: ({ reallocationData: data, marketId, operation, amount, options, }: {
66
- readonly reallocationData: VaultV1ReallocationData;
67
- readonly marketId: MarketId;
68
- readonly operation: "borrow" | "withdraw";
69
- readonly amount: bigint;
70
- readonly options?: ReallocationComputeOptions;
71
- }) => readonly VaultV1Reallocation[];
72
- /**
73
- * Deprecated name for the Vault V1 amount-aware reallocation planner.
74
- *
75
- * @deprecated Vault V1 shared-liquidity planning will be removed in the next major. Use
76
- * `VaultV2BlueReallocationData.computeVaultV2BlueReallocations`.
77
- */
78
- export declare const computeReallocations: typeof computeVaultV1Reallocations;