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