@morpho-org/morpho-sdk 6.0.0-next.2 → 6.0.0-next.4

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 (277) hide show
  1. package/MIGRATION-v5-to-v6.md +88 -32
  2. package/README.md +9 -7
  3. package/lib/cjs/abis.d.ts +7 -2844
  4. package/lib/cjs/abis.js +3 -1951
  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 +1 -9
  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 +2 -2
  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 +6 -2
  25. package/lib/cjs/actions/bundles/common.js +31 -5
  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 +6 -8
  29. package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +11 -16
  30. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +5 -6
  31. package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +4 -5
  32. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +4 -4
  33. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +4 -5
  34. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +5 -5
  35. package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +6 -11
  36. package/lib/cjs/actions/requirements/encode/index.d.ts +0 -1
  37. package/lib/cjs/actions/requirements/encode/index.js +0 -1
  38. package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +5 -6
  39. package/lib/cjs/actions/requirements/getRequirementsApproval.js +5 -6
  40. package/lib/cjs/actions/requirements/index.d.ts +0 -1
  41. package/lib/cjs/actions/requirements/index.js +0 -1
  42. package/lib/cjs/actions/vaultV1/deposit.d.ts +1 -2
  43. package/lib/cjs/actions/vaultV1/deposit.js +1 -2
  44. package/lib/cjs/actions/vaultV1/inKindRedeem.d.ts +7 -5
  45. package/lib/cjs/actions/vaultV1/inKindRedeem.js +11 -7
  46. package/lib/cjs/actions/vaultV2/deposit.d.ts +1 -2
  47. package/lib/cjs/actions/vaultV2/deposit.js +1 -2
  48. package/lib/cjs/actions/vaultV2/forceWithdraw.d.ts +1 -1
  49. package/lib/cjs/actions/vaultV2/forceWithdraw.js +3 -3
  50. package/lib/cjs/actions/vaultV2/inKindRedeem.d.ts +7 -5
  51. package/lib/cjs/actions/vaultV2/inKindRedeem.js +11 -7
  52. package/lib/cjs/augment/AccrualPosition.d.ts +1 -0
  53. package/lib/cjs/augment.d.ts +0 -1
  54. package/lib/cjs/augment.js +0 -1
  55. package/lib/cjs/blue/abis.d.ts +5 -5
  56. package/lib/cjs/blue/abis.js +3 -7
  57. package/lib/cjs/blue/entities.d.ts +10 -8
  58. package/lib/cjs/blue/entities.js +3 -3
  59. package/lib/cjs/blue/errors.d.ts +4 -2
  60. package/lib/cjs/blue/errors.js +3 -1
  61. package/lib/cjs/blue/fetch.d.ts +5 -3
  62. package/lib/cjs/blue/fetch.js +3 -3
  63. package/lib/cjs/blue/types.d.ts +2 -4
  64. package/lib/cjs/blue/utils.d.ts +6 -4
  65. package/lib/cjs/blue/utils.js +3 -3
  66. package/lib/cjs/client/morphoViemExtension.d.ts +1 -1
  67. package/lib/cjs/client/morphoViemExtension.js +1 -1
  68. package/lib/cjs/constants.d.ts +3 -37
  69. package/lib/cjs/constants.js +1 -39
  70. package/lib/cjs/entities/blue/blue.d.ts +30 -265
  71. package/lib/cjs/entities/blue/blue.js +1 -205
  72. package/lib/cjs/entities/index.d.ts +11 -66
  73. package/lib/cjs/entities/index.js +3 -21
  74. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -2
  75. package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +8 -3
  76. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +13 -5
  77. package/lib/cjs/entities/vaultV1/vaultV1.js +16 -10
  78. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +40 -19
  79. package/lib/cjs/entities/vaultV2/vaultV2.js +57 -26
  80. package/lib/cjs/entities/vaultV2BlueReallocationData.js +4 -21
  81. package/lib/cjs/errors.d.ts +5 -47
  82. package/lib/cjs/errors.js +4 -46
  83. package/lib/cjs/fetch.d.ts +5 -17
  84. package/lib/cjs/fetch.js +3 -17
  85. package/lib/cjs/helpers/constant.d.ts +0 -2
  86. package/lib/cjs/helpers/constant.js +0 -2
  87. package/lib/cjs/helpers/index.d.ts +2 -3
  88. package/lib/cjs/helpers/index.js +1 -9
  89. package/lib/cjs/helpers/previewVaultV2ForceWithdraw.js +11 -5
  90. package/lib/cjs/helpers/slippage.d.ts +1 -103
  91. package/lib/cjs/helpers/slippage.js +0 -150
  92. package/lib/cjs/helpers/utilization.d.ts +12 -10
  93. package/lib/cjs/helpers/utilization.js +27 -12
  94. package/lib/cjs/helpers/validate.d.ts +1 -24
  95. package/lib/cjs/helpers/validate.js +2 -49
  96. package/lib/cjs/helpers/validateRequirementSpender.d.ts +1 -1
  97. package/lib/cjs/helpers/validateRequirementSpender.js +1 -3
  98. package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.d.ts +48 -15
  99. package/lib/cjs/helpers/vaultV2ForceWithdrawPlan.js +48 -15
  100. package/lib/cjs/midnight/errors.d.ts +1 -1
  101. package/lib/cjs/midnight/errors.js +2 -1
  102. package/lib/cjs/types/action.d.ts +17 -65
  103. package/lib/cjs/types/action.js +11 -4
  104. package/lib/cjs/types/error.d.ts +50 -381
  105. package/lib/cjs/types/error.js +64 -395
  106. package/lib/cjs/types/sharedLiquidity.d.ts +1 -117
  107. package/lib/cjs/types.d.ts +2 -18
  108. package/lib/cjs/utils.d.ts +10 -35
  109. package/lib/cjs/utils.js +4 -36
  110. package/lib/esm/abis.d.ts +7 -2844
  111. package/lib/esm/abis.js +7 -1950
  112. package/lib/esm/actions/blue/borrow.d.ts +3 -2
  113. package/lib/esm/actions/blue/borrow.js +3 -2
  114. package/lib/esm/actions/blue/common.js +1 -9
  115. package/lib/esm/actions/blue/refinance.d.ts +2 -2
  116. package/lib/esm/actions/blue/refinance.js +2 -2
  117. package/lib/esm/actions/blue/repay.d.ts +1 -1
  118. package/lib/esm/actions/blue/repay.js +1 -1
  119. package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +2 -2
  120. package/lib/esm/actions/blue/repayWithdrawCollateral.js +2 -2
  121. package/lib/esm/actions/blue/supply.d.ts +2 -2
  122. package/lib/esm/actions/blue/supply.js +2 -2
  123. package/lib/esm/actions/blue/supplyCollateral.d.ts +2 -2
  124. package/lib/esm/actions/blue/supplyCollateral.js +2 -2
  125. package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +3 -2
  126. package/lib/esm/actions/blue/supplyCollateralBorrow.js +3 -2
  127. package/lib/esm/actions/blue/withdraw.d.ts +3 -3
  128. package/lib/esm/actions/blue/withdraw.js +3 -3
  129. package/lib/esm/actions/blue/withdrawCollateral.d.ts +2 -2
  130. package/lib/esm/actions/blue/withdrawCollateral.js +2 -2
  131. package/lib/esm/actions/bundles/common.d.ts +6 -2
  132. package/lib/esm/actions/bundles/common.js +32 -6
  133. package/lib/esm/actions/index.d.ts +0 -1
  134. package/lib/esm/actions/index.js +0 -1
  135. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +6 -8
  136. package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +12 -17
  137. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +5 -6
  138. package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +4 -5
  139. package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +4 -4
  140. package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +4 -5
  141. package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +5 -5
  142. package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +6 -11
  143. package/lib/esm/actions/requirements/encode/index.d.ts +0 -1
  144. package/lib/esm/actions/requirements/encode/index.js +0 -1
  145. package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +5 -6
  146. package/lib/esm/actions/requirements/getRequirementsApproval.js +5 -6
  147. package/lib/esm/actions/requirements/index.d.ts +0 -1
  148. package/lib/esm/actions/requirements/index.js +0 -1
  149. package/lib/esm/actions/vaultV1/deposit.d.ts +1 -2
  150. package/lib/esm/actions/vaultV1/deposit.js +1 -2
  151. package/lib/esm/actions/vaultV1/inKindRedeem.d.ts +7 -5
  152. package/lib/esm/actions/vaultV1/inKindRedeem.js +11 -7
  153. package/lib/esm/actions/vaultV2/deposit.d.ts +1 -2
  154. package/lib/esm/actions/vaultV2/deposit.js +1 -2
  155. package/lib/esm/actions/vaultV2/forceWithdraw.d.ts +1 -1
  156. package/lib/esm/actions/vaultV2/forceWithdraw.js +3 -3
  157. package/lib/esm/actions/vaultV2/inKindRedeem.d.ts +7 -5
  158. package/lib/esm/actions/vaultV2/inKindRedeem.js +11 -7
  159. package/lib/esm/augment/AccrualPosition.d.ts +1 -0
  160. package/lib/esm/augment.d.ts +0 -1
  161. package/lib/esm/augment.js +0 -1
  162. package/lib/esm/blue/abis.d.ts +5 -5
  163. package/lib/esm/blue/abis.js +5 -5
  164. package/lib/esm/blue/entities.d.ts +10 -8
  165. package/lib/esm/blue/entities.js +5 -3
  166. package/lib/esm/blue/errors.d.ts +4 -2
  167. package/lib/esm/blue/errors.js +4 -2
  168. package/lib/esm/blue/fetch.d.ts +5 -3
  169. package/lib/esm/blue/fetch.js +5 -3
  170. package/lib/esm/blue/types.d.ts +2 -4
  171. package/lib/esm/blue/utils.d.ts +6 -4
  172. package/lib/esm/blue/utils.js +6 -4
  173. package/lib/esm/client/morphoViemExtension.d.ts +1 -1
  174. package/lib/esm/client/morphoViemExtension.js +1 -1
  175. package/lib/esm/constants.d.ts +3 -37
  176. package/lib/esm/constants.js +3 -37
  177. package/lib/esm/entities/blue/blue.d.ts +30 -265
  178. package/lib/esm/entities/blue/blue.js +4 -208
  179. package/lib/esm/entities/index.d.ts +11 -66
  180. package/lib/esm/entities/index.js +5 -18
  181. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +5 -2
  182. package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +8 -3
  183. package/lib/esm/entities/vaultV1/vaultV1.d.ts +13 -5
  184. package/lib/esm/entities/vaultV1/vaultV1.js +19 -13
  185. package/lib/esm/entities/vaultV2/vaultV2.d.ts +40 -19
  186. package/lib/esm/entities/vaultV2/vaultV2.js +61 -30
  187. package/lib/esm/entities/vaultV2BlueReallocationData.js +2 -19
  188. package/lib/esm/errors.d.ts +5 -47
  189. package/lib/esm/errors.js +5 -47
  190. package/lib/esm/fetch.d.ts +5 -17
  191. package/lib/esm/fetch.js +5 -17
  192. package/lib/esm/helpers/constant.d.ts +0 -2
  193. package/lib/esm/helpers/constant.js +0 -2
  194. package/lib/esm/helpers/index.d.ts +2 -3
  195. package/lib/esm/helpers/index.js +2 -3
  196. package/lib/esm/helpers/previewVaultV2ForceWithdraw.js +11 -5
  197. package/lib/esm/helpers/slippage.d.ts +1 -103
  198. package/lib/esm/helpers/slippage.js +1 -147
  199. package/lib/esm/helpers/utilization.d.ts +12 -10
  200. package/lib/esm/helpers/utilization.js +26 -11
  201. package/lib/esm/helpers/validate.d.ts +1 -24
  202. package/lib/esm/helpers/validate.js +2 -48
  203. package/lib/esm/helpers/validateRequirementSpender.d.ts +1 -1
  204. package/lib/esm/helpers/validateRequirementSpender.js +1 -3
  205. package/lib/esm/helpers/vaultV2ForceWithdrawPlan.d.ts +48 -15
  206. package/lib/esm/helpers/vaultV2ForceWithdrawPlan.js +48 -15
  207. package/lib/esm/midnight/errors.d.ts +1 -1
  208. package/lib/esm/midnight/errors.js +1 -1
  209. package/lib/esm/types/action.d.ts +17 -65
  210. package/lib/esm/types/action.js +12 -5
  211. package/lib/esm/types/error.d.ts +50 -381
  212. package/lib/esm/types/error.js +58 -377
  213. package/lib/esm/types/sharedLiquidity.d.ts +1 -117
  214. package/lib/esm/types.d.ts +2 -18
  215. package/lib/esm/utils.d.ts +10 -35
  216. package/lib/esm/utils.js +9 -34
  217. package/package.json +6 -17
  218. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2Approve.d.ts +0 -44
  219. package/lib/cjs/actions/requirements/encode/encodeErc20Permit2Approve.js +0 -88
  220. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirements.d.ts +0 -88
  221. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirements.js +0 -152
  222. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +0 -40
  223. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.js +0 -53
  224. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.d.ts +0 -57
  225. package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.js +0 -74
  226. package/lib/cjs/actions/requirements/generalAdapter/index.d.ts +0 -3
  227. package/lib/cjs/actions/requirements/generalAdapter/index.js +0 -19
  228. package/lib/cjs/actions/signatures/getBlueAuthorizationAction.d.ts +0 -29
  229. package/lib/cjs/actions/signatures/getBlueAuthorizationAction.js +0 -48
  230. package/lib/cjs/actions/signatures/getTokenRequirementActions.d.ts +0 -58
  231. package/lib/cjs/actions/signatures/getTokenRequirementActions.js +0 -119
  232. package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +0 -50
  233. package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.js +0 -45
  234. package/lib/cjs/actions/signatures/index.d.ts +0 -3
  235. package/lib/cjs/actions/signatures/index.js +0 -19
  236. package/lib/cjs/augment/VaultMarketPublicAllocatorConfig.d.ts +0 -11
  237. package/lib/cjs/augment/VaultMarketPublicAllocatorConfig.js +0 -8
  238. package/lib/cjs/bundler/actions.d.ts +0 -797
  239. package/lib/cjs/bundler/actions.js +0 -1371
  240. package/lib/cjs/bundler/index.d.ts +0 -2
  241. package/lib/cjs/bundler/index.js +0 -18
  242. package/lib/cjs/bundler/types.d.ts +0 -235
  243. package/lib/cjs/bundler/types.js +0 -2
  244. package/lib/cjs/entities/vaultV1ReallocationData.d.ts +0 -332
  245. package/lib/cjs/entities/vaultV1ReallocationData.js +0 -557
  246. package/lib/cjs/helpers/computeVaultV1Reallocations.d.ts +0 -80
  247. package/lib/cjs/helpers/computeVaultV1Reallocations.js +0 -245
  248. package/lib/esm/actions/requirements/encode/encodeErc20Permit2Approve.d.ts +0 -44
  249. package/lib/esm/actions/requirements/encode/encodeErc20Permit2Approve.js +0 -84
  250. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirements.d.ts +0 -88
  251. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirements.js +0 -148
  252. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +0 -40
  253. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.js +0 -49
  254. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.d.ts +0 -57
  255. package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit2.js +0 -70
  256. package/lib/esm/actions/requirements/generalAdapter/index.d.ts +0 -3
  257. package/lib/esm/actions/requirements/generalAdapter/index.js +0 -3
  258. package/lib/esm/actions/signatures/getBlueAuthorizationAction.d.ts +0 -29
  259. package/lib/esm/actions/signatures/getBlueAuthorizationAction.js +0 -44
  260. package/lib/esm/actions/signatures/getTokenRequirementActions.d.ts +0 -58
  261. package/lib/esm/actions/signatures/getTokenRequirementActions.js +0 -115
  262. package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +0 -50
  263. package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.js +0 -41
  264. package/lib/esm/actions/signatures/index.d.ts +0 -3
  265. package/lib/esm/actions/signatures/index.js +0 -3
  266. package/lib/esm/augment/VaultMarketPublicAllocatorConfig.d.ts +0 -11
  267. package/lib/esm/augment/VaultMarketPublicAllocatorConfig.js +0 -7
  268. package/lib/esm/bundler/actions.d.ts +0 -797
  269. package/lib/esm/bundler/actions.js +0 -1368
  270. package/lib/esm/bundler/index.d.ts +0 -2
  271. package/lib/esm/bundler/index.js +0 -2
  272. package/lib/esm/bundler/types.d.ts +0 -235
  273. package/lib/esm/bundler/types.js +0 -1
  274. package/lib/esm/entities/vaultV1ReallocationData.d.ts +0 -332
  275. package/lib/esm/entities/vaultV1ReallocationData.js +0 -559
  276. package/lib/esm/helpers/computeVaultV1Reallocations.d.ts +0 -80
  277. package/lib/esm/helpers/computeVaultV1Reallocations.js +0 -241
@@ -1,241 +0,0 @@
1
- import { MarketUtils, MathLib } from "@morpho-org/blue-sdk";
2
- import { InsufficientSharedLiquidityError, MissingPublicAllocatorConfigError, ReallocationWithdrawExceedsMarketSupplyError, } from "../types/index.js";
3
- import { getSupplyTargetUtilization } from "./utilization.js";
4
- import { compareMarketIds } from "./validate.js";
5
- const groupWithdrawalsByVault = (withdrawals) => {
6
- const groups = [];
7
- const groupsByVault = new Map();
8
- for (const [index, { vault, id, assets }] of withdrawals.entries()) {
9
- if (assets <= 0n)
10
- continue;
11
- let group = groupsByVault.get(vault);
12
- if (group == null) {
13
- group = {
14
- vault,
15
- firstIndex: index,
16
- totalAssets: 0n,
17
- withdrawals: [],
18
- };
19
- groupsByVault.set(vault, group);
20
- groups.push(group);
21
- }
22
- group.totalAssets += assets;
23
- group.withdrawals.push({ id, assets });
24
- }
25
- return groups.sort((a, b) => {
26
- if (a.totalAssets > b.totalAssets)
27
- return -1;
28
- if (a.totalAssets < b.totalAssets)
29
- return 1;
30
- return a.firstIndex - b.firstIndex;
31
- });
32
- };
33
- const capVaultWithdrawals = (withdrawals, assets) => {
34
- const cappedWithdrawals = [];
35
- let remainingAssets = assets;
36
- for (const withdrawal of withdrawals) {
37
- const cappedAssets = MathLib.min(withdrawal.assets, remainingAssets);
38
- /* v8 ignore next: grouped withdrawals and positive reallocation cap keep this defensive guard unreachable. */
39
- if (cappedAssets <= 0n)
40
- continue;
41
- const existing = cappedWithdrawals.find((item) => item.id === withdrawal.id);
42
- if (existing != null) {
43
- existing.assets += cappedAssets;
44
- }
45
- else {
46
- cappedWithdrawals.push({
47
- id: withdrawal.id,
48
- assets: cappedAssets,
49
- });
50
- }
51
- remainingAssets -= cappedAssets;
52
- if (remainingAssets === 0n)
53
- break;
54
- }
55
- return cappedWithdrawals;
56
- };
57
- /**
58
- * Computes vault reallocations for a `borrow` or `withdraw` on a target market.
59
- *
60
- * First attempts "friendly" reallocations respecting withdrawal utilization
61
- * targets, then falls back to aggressive reallocations (100% withdrawal
62
- * utilization) if liquidity is still insufficient.
63
- *
64
- * Algebra branches on `operation`:
65
- * - `"borrow"`: `S' = S`, `B' = B + amount` (additional borrow demand).
66
- * - `"withdraw"`: `S' = S − amount`, `B' = B` (supply-side shrinkage).
67
- *
68
- * In both cases reallocated assets are added on the supply side; `requiredAssets`
69
- * and `absoluteShortfall` are derived from the operation-specific post-state.
70
- *
71
- * @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.
72
- * @param params.reallocationData - The local state containing market, vault, and position data.
73
- * @param params.marketId - The target market to reallocate liquidity into.
74
- * @param params.operation - The operation driving the reallocation (`"borrow"` or `"withdraw"`).
75
- * @param params.amount - The borrow or withdraw amount used to compute the post-state utilization.
76
- * @param params.options - Optional reallocation computation options.
77
- * @returns Array of vault reallocations, sorted with withdrawals in ascending market id order.
78
- * @throws {UnsupportedBlueMarketIrmError} when a market with positive debt uses an unsupported IRM.
79
- * @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.
80
- * @throws {ReallocationWithdrawExceedsMarketSupplyError} when `operation === "withdraw"` and `amount` exceeds the target market's `totalSupplyAssets` — the on-chain call would revert regardless of reallocations.
81
- * @throws {MissingPublicAllocatorConfigError} when a selected vault is missing its public allocator config.
82
- * @deprecated Vault V1 shared-liquidity planning will be removed in the next major. Use
83
- * `VaultV2BlueReallocationData.computeVaultV2BlueReallocations`.
84
- * @example
85
- * ```ts
86
- * import { createPublicClient, http, parseUnits } from "viem";
87
- * import { mainnet } from "viem/chains";
88
- * import { markets, vaults } from "@morpho-org/morpho-test";
89
- * import {
90
- * computeVaultV1Reallocations,
91
- * morphoViemExtension,
92
- * } from "@morpho-org/morpho-sdk";
93
- *
94
- * const client = createPublicClient({
95
- * chain: mainnet,
96
- * transport: http(),
97
- * }).extend(morphoViemExtension());
98
- *
99
- * const marketParams = markets[mainnet.id].usdc_wbtc;
100
- * const market = client.morpho.blue(marketParams, mainnet.id);
101
- * const block = await client.getBlock();
102
- * const reallocationData = await market.getVaultV1ReallocationData({
103
- * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
104
- * block: { number: block.number, timestamp: block.timestamp },
105
- * });
106
- * const borrowAmount = parseUnits("1000", 6);
107
- * const reallocations = computeVaultV1Reallocations({
108
- * reallocationData,
109
- * marketId: marketParams.id,
110
- * operation: "borrow",
111
- * amount: borrowAmount,
112
- * options: { timestamp: block.timestamp },
113
- * });
114
- * // Encode `reallocations` explicitly with
115
- * // BundlerAction.publicAllocatorReallocateTo(...) when composing a low-level bundle.
116
- * // High-level Blue writes accept Vault V2 reallocations only.
117
- * ```
118
- */
119
- export const computeVaultV1Reallocations = ({ reallocationData: data, marketId, operation, amount, options, }) => {
120
- if (options?.enabled === false)
121
- return [];
122
- const normalizedOptions = {
123
- ...options,
124
- reallocatableVaults: options?.reallocatableVaults == null
125
- ? undefined
126
- : [...options.reallocatableVaults],
127
- };
128
- // VaultV1ReallocationData does not retain the fetch block; pass that block timestamp
129
- // to compute against the same accrued state, otherwise Market defaults to lastUpdate.
130
- const market = data
131
- .getMarket(marketId)
132
- .accrueInterest(normalizedOptions.timestamp);
133
- // Reject unreachable withdraws before any utilization math: a negative
134
- // post-supply yields a negative utilization that short-circuits the
135
- // utilization gate below and silently returns `[]` — masking a sure revert.
136
- if (operation === "withdraw" && amount > market.totalSupplyAssets) {
137
- throw new ReallocationWithdrawExceedsMarketSupplyError({
138
- marketId,
139
- withdrawAmount: amount,
140
- totalSupplyAssets: market.totalSupplyAssets,
141
- });
142
- }
143
- // Post-state utilization is operation-dependent.
144
- const newTotalBorrowAssets = operation === "borrow"
145
- ? market.totalBorrowAssets + amount
146
- : market.totalBorrowAssets;
147
- const newTotalSupplyAssets = operation === "withdraw"
148
- ? market.totalSupplyAssets - amount
149
- : market.totalSupplyAssets;
150
- const supplyTargetUtilization = getSupplyTargetUtilization(market.params.id, normalizedOptions);
151
- if (MarketUtils.getUtilization({
152
- totalSupplyAssets: newTotalSupplyAssets,
153
- totalBorrowAssets: newTotalBorrowAssets,
154
- }) <= supplyTargetUtilization)
155
- return [];
156
- // Solve: newTotalBorrowAssets / (newTotalSupplyAssets + reallocatedAssets) = supplyTargetUtilization
157
- let requiredAssets = supplyTargetUtilization === 0n
158
- ? MathLib.MAX_UINT_160
159
- : MathLib.wDivDown(newTotalBorrowAssets, supplyTargetUtilization) -
160
- newTotalSupplyAssets;
161
- // Phase 1: "friendly" reallocations respecting withdrawal utilization targets.
162
- const { withdrawals: friendlyWithdrawals, data: friendlyReallocationData } = data.computeVaultV1Reallocations(market.id, normalizedOptions);
163
- const withdrawals = [...friendlyWithdrawals];
164
- const friendlyReallocationMarket = friendlyReallocationData.getMarket(market.id);
165
- // Operation-specific post-friendly check: would the on-chain call still revert?
166
- const friendlyBorrow = operation === "borrow"
167
- ? friendlyReallocationMarket.totalBorrowAssets + amount
168
- : friendlyReallocationMarket.totalBorrowAssets;
169
- const friendlySupply = operation === "withdraw"
170
- ? friendlyReallocationMarket.totalSupplyAssets - amount
171
- : friendlyReallocationMarket.totalSupplyAssets;
172
- if (friendlyBorrow > friendlySupply) {
173
- // Phase 2: "aggressive" — fully withdraw from every market (100% utilization).
174
- requiredAssets = newTotalBorrowAssets - newTotalSupplyAssets;
175
- withdrawals.push(...friendlyReallocationData.computeVaultV1Reallocations(market.id, {
176
- ...normalizedOptions,
177
- defaultMaxWithdrawalUtilization: MathLib.WAD,
178
- maxWithdrawalUtilization: {},
179
- }).withdrawals);
180
- }
181
- if (requiredAssets <= 0n)
182
- return [];
183
- // Liquidity strictly required for the on-chain call to succeed.
184
- const absoluteShortfall = newTotalBorrowAssets > newTotalSupplyAssets
185
- ? newTotalBorrowAssets - newTotalSupplyAssets
186
- : 0n;
187
- const reallocations = [];
188
- let totalReallocated = 0n;
189
- let remainingRequiredAssets = requiredAssets;
190
- for (const group of groupWithdrawalsByVault(withdrawals)) {
191
- const reallocatedAssets = MathLib.min(group.totalAssets, remainingRequiredAssets);
192
- /* v8 ignore next: groups with non-positive assets are filtered before this point. */
193
- if (reallocatedAssets <= 0n)
194
- continue;
195
- reallocations.push({
196
- vault: group.vault,
197
- withdrawals: capVaultWithdrawals(group.withdrawals, reallocatedAssets),
198
- });
199
- remainingRequiredAssets -= reallocatedAssets;
200
- totalReallocated += reallocatedAssets;
201
- if (remainingRequiredAssets === 0n)
202
- break;
203
- }
204
- // Refuse fee-bearing partial plans for an unreachable operation.
205
- if (totalReallocated < absoluteShortfall) {
206
- throw new InsufficientSharedLiquidityError({
207
- marketId,
208
- shortfall: absoluteShortfall,
209
- available: totalReallocated,
210
- });
211
- }
212
- // Transform into VaultV1Reallocation[] format.
213
- return reallocations
214
- .filter(({ withdrawals: vaultWithdrawals }) => vaultWithdrawals.length > 0)
215
- .map(({ vault, withdrawals: vaultWithdrawals }) => ({
216
- vault,
217
- fee: (() => {
218
- const config = data.getVault(vault).publicAllocatorConfig;
219
- if (config == null) {
220
- throw new MissingPublicAllocatorConfigError(vault);
221
- }
222
- return config.fee;
223
- })(),
224
- withdrawals: vaultWithdrawals
225
- // Reallocation withdrawals must be sorted by market id in ascending
226
- // order. Normalize first because `MarketId` can be mixed-case hex.
227
- .sort(({ id: idA }, { id: idB }) => compareMarketIds(idA, idB))
228
- .map(({ id, assets }) => ({
229
- marketParams: data.getMarket(id).params,
230
- amount: assets,
231
- })),
232
- }));
233
- };
234
- /**
235
- * Deprecated name for the Vault V1 amount-aware reallocation planner.
236
- *
237
- * @throws {UnsupportedBlueMarketIrmError} when a market with positive debt uses an unsupported IRM.
238
- * @deprecated Vault V1 shared-liquidity planning will be removed in the next major. Use
239
- * `VaultV2BlueReallocationData.computeVaultV2BlueReallocations`.
240
- */
241
- export const computeReallocations = computeVaultV1Reallocations;