@morpho-org/morpho-sdk 5.4.1 → 5.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/README.md +23 -7
  2. package/lib/cjs/abis.d.ts +314 -1
  3. package/lib/cjs/abis.js +169 -1
  4. package/lib/cjs/actions/blue/borrow.d.ts +21 -15
  5. package/lib/cjs/actions/blue/borrow.js +25 -18
  6. package/lib/cjs/actions/blue/buildAssetFundingActions.d.ts +1 -1
  7. package/lib/cjs/actions/blue/buildAssetFundingActions.js +1 -1
  8. package/lib/cjs/actions/blue/buildReallocationActions.d.ts +62 -19
  9. package/lib/cjs/actions/blue/buildReallocationActions.js +123 -27
  10. package/lib/cjs/actions/blue/refinance.d.ts +14 -7
  11. package/lib/cjs/actions/blue/refinance.js +18 -10
  12. package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +28 -19
  13. package/lib/cjs/actions/blue/supplyCollateralBorrow.js +39 -23
  14. package/lib/cjs/actions/blue/withdraw.d.ts +20 -13
  15. package/lib/cjs/actions/blue/withdraw.js +23 -18
  16. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +1 -1
  17. package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +9 -2
  18. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +64 -0
  19. package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +131 -0
  20. package/lib/cjs/actions/requirements/encode/index.d.ts +1 -0
  21. package/lib/cjs/actions/requirements/encode/index.js +1 -0
  22. package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +3 -2
  23. package/lib/cjs/actions/requirements/getRequirementsApproval.js +5 -3
  24. package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +52 -0
  25. package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.js +91 -0
  26. package/lib/cjs/actions/signatures/index.d.ts +1 -0
  27. package/lib/cjs/actions/signatures/index.js +1 -0
  28. package/lib/cjs/actions/vaultV1/inKindRedeem.d.ts +54 -0
  29. package/lib/cjs/actions/vaultV1/inKindRedeem.js +96 -0
  30. package/lib/cjs/actions/vaultV1/index.d.ts +1 -0
  31. package/lib/cjs/actions/vaultV1/index.js +1 -0
  32. package/lib/cjs/actions/vaultV2/inKindRedeem.d.ts +56 -0
  33. package/lib/cjs/actions/vaultV2/inKindRedeem.js +97 -0
  34. package/lib/cjs/actions/vaultV2/index.d.ts +1 -0
  35. package/lib/cjs/actions/vaultV2/index.js +1 -0
  36. package/lib/cjs/bundler/actions.d.ts +118 -2
  37. package/lib/cjs/bundler/actions.js +212 -6
  38. package/lib/cjs/bundler/types.d.ts +20 -0
  39. package/lib/cjs/constants.d.ts +1 -1
  40. package/lib/cjs/constants.js +3 -1
  41. package/lib/cjs/entities/blue/blue.d.ts +365 -76
  42. package/lib/cjs/entities/blue/blue.js +285 -52
  43. package/lib/cjs/entities/index.d.ts +2 -1
  44. package/lib/cjs/entities/index.js +6 -3
  45. package/lib/cjs/entities/vaultV1/vaultV1.d.ts +82 -4
  46. package/lib/cjs/entities/vaultV1/vaultV1.js +164 -0
  47. package/lib/cjs/entities/{reallocationData.d.ts → vaultV1ReallocationData.d.ts} +41 -17
  48. package/lib/cjs/entities/{reallocationData.js → vaultV1ReallocationData.js} +30 -18
  49. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +89 -3
  50. package/lib/cjs/entities/vaultV2/vaultV2.js +181 -0
  51. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +511 -0
  52. package/lib/cjs/entities/vaultV2BlueReallocationData.js +1311 -0
  53. package/lib/{esm/helpers/computeReallocations.d.ts → cjs/helpers/computeVaultV1Reallocations.d.ts} +14 -8
  54. package/lib/cjs/helpers/{computeReallocations.js → computeVaultV1Reallocations.js} +27 -13
  55. package/lib/cjs/helpers/constant.d.ts +5 -3
  56. package/lib/cjs/helpers/constant.js +6 -4
  57. package/lib/cjs/helpers/index.d.ts +3 -2
  58. package/lib/cjs/helpers/index.js +8 -3
  59. package/lib/cjs/helpers/previewVaultV2InKindRedeem.d.ts +49 -0
  60. package/lib/cjs/helpers/previewVaultV2InKindRedeem.js +73 -0
  61. package/lib/cjs/helpers/validate.d.ts +31 -15
  62. package/lib/cjs/helpers/validate.js +140 -27
  63. package/lib/cjs/helpers/validateRequirementSpender.d.ts +1 -1
  64. package/lib/cjs/helpers/validateRequirementSpender.js +2 -1
  65. package/lib/cjs/types/action.d.ts +33 -1
  66. package/lib/cjs/types/error.d.ts +363 -5
  67. package/lib/cjs/types/error.js +455 -6
  68. package/lib/cjs/types/sharedLiquidity.d.ts +72 -8
  69. package/lib/cjs/utils.d.ts +2 -2
  70. package/lib/cjs/utils.js +6 -4
  71. package/lib/esm/abis.d.ts +314 -1
  72. package/lib/esm/abis.js +167 -1
  73. package/lib/esm/actions/blue/borrow.d.ts +21 -15
  74. package/lib/esm/actions/blue/borrow.js +26 -19
  75. package/lib/esm/actions/blue/buildAssetFundingActions.d.ts +1 -1
  76. package/lib/esm/actions/blue/buildAssetFundingActions.js +1 -1
  77. package/lib/esm/actions/blue/buildReallocationActions.d.ts +62 -19
  78. package/lib/esm/actions/blue/buildReallocationActions.js +119 -25
  79. package/lib/esm/actions/blue/refinance.d.ts +14 -7
  80. package/lib/esm/actions/blue/refinance.js +19 -11
  81. package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +28 -19
  82. package/lib/esm/actions/blue/supplyCollateralBorrow.js +40 -24
  83. package/lib/esm/actions/blue/withdraw.d.ts +20 -13
  84. package/lib/esm/actions/blue/withdraw.js +24 -19
  85. package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +1 -1
  86. package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +9 -2
  87. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +64 -0
  88. package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +127 -0
  89. package/lib/esm/actions/requirements/encode/index.d.ts +1 -0
  90. package/lib/esm/actions/requirements/encode/index.js +1 -0
  91. package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +3 -2
  92. package/lib/esm/actions/requirements/getRequirementsApproval.js +5 -3
  93. package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +52 -0
  94. package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.js +87 -0
  95. package/lib/esm/actions/signatures/index.d.ts +1 -0
  96. package/lib/esm/actions/signatures/index.js +1 -0
  97. package/lib/esm/actions/vaultV1/inKindRedeem.d.ts +54 -0
  98. package/lib/esm/actions/vaultV1/inKindRedeem.js +92 -0
  99. package/lib/esm/actions/vaultV1/index.d.ts +1 -0
  100. package/lib/esm/actions/vaultV1/index.js +1 -0
  101. package/lib/esm/actions/vaultV2/inKindRedeem.d.ts +56 -0
  102. package/lib/esm/actions/vaultV2/inKindRedeem.js +93 -0
  103. package/lib/esm/actions/vaultV2/index.d.ts +1 -0
  104. package/lib/esm/actions/vaultV2/index.js +1 -0
  105. package/lib/esm/bundler/actions.d.ts +118 -2
  106. package/lib/esm/bundler/actions.js +215 -9
  107. package/lib/esm/bundler/types.d.ts +20 -0
  108. package/lib/esm/constants.d.ts +1 -1
  109. package/lib/esm/constants.js +1 -1
  110. package/lib/esm/entities/blue/blue.d.ts +362 -73
  111. package/lib/esm/entities/blue/blue.js +288 -55
  112. package/lib/esm/entities/index.d.ts +2 -1
  113. package/lib/esm/entities/index.js +2 -1
  114. package/lib/esm/entities/vaultV1/vaultV1.d.ts +80 -2
  115. package/lib/esm/entities/vaultV1/vaultV1.js +170 -6
  116. package/lib/esm/entities/{reallocationData.d.ts → vaultV1ReallocationData.d.ts} +41 -17
  117. package/lib/esm/entities/{reallocationData.js → vaultV1ReallocationData.js} +33 -16
  118. package/lib/esm/entities/vaultV2/vaultV2.d.ts +88 -2
  119. package/lib/esm/entities/vaultV2/vaultV2.js +187 -6
  120. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +511 -0
  121. package/lib/esm/entities/vaultV2BlueReallocationData.js +1307 -0
  122. package/lib/{cjs/helpers/computeReallocations.d.ts → esm/helpers/computeVaultV1Reallocations.d.ts} +14 -8
  123. package/lib/esm/helpers/{computeReallocations.js → computeVaultV1Reallocations.js} +25 -11
  124. package/lib/esm/helpers/constant.d.ts +5 -3
  125. package/lib/esm/helpers/constant.js +5 -3
  126. package/lib/esm/helpers/index.d.ts +3 -2
  127. package/lib/esm/helpers/index.js +3 -2
  128. package/lib/esm/helpers/previewVaultV2InKindRedeem.d.ts +49 -0
  129. package/lib/esm/helpers/previewVaultV2InKindRedeem.js +70 -0
  130. package/lib/esm/helpers/validate.d.ts +31 -15
  131. package/lib/esm/helpers/validate.js +141 -30
  132. package/lib/esm/helpers/validateRequirementSpender.d.ts +1 -1
  133. package/lib/esm/helpers/validateRequirementSpender.js +2 -1
  134. package/lib/esm/types/action.d.ts +33 -1
  135. package/lib/esm/types/error.d.ts +363 -5
  136. package/lib/esm/types/error.js +428 -4
  137. package/lib/esm/types/sharedLiquidity.d.ts +72 -8
  138. package/lib/esm/utils.d.ts +2 -2
  139. package/lib/esm/utils.js +2 -2
  140. package/package.json +9 -11
@@ -7,8 +7,10 @@ const morpho_ts_1 = require("@morpho-org/morpho-ts");
7
7
  const viem_1 = require("viem");
8
8
  const index_js_1 = require("../../actions/index.js");
9
9
  const index_js_2 = require("../../helpers/index.js");
10
+ const validate_js_1 = require("../../helpers/validate.js");
10
11
  const index_js_3 = require("../../types/index.js");
11
- const reallocationData_js_1 = require("../reallocationData.js");
12
+ const vaultV1ReallocationData_js_1 = require("../vaultV1ReallocationData.js");
13
+ const vaultV2BlueReallocationData_js_1 = require("../vaultV2BlueReallocationData.js");
12
14
  class MorphoBlue {
13
15
  client;
14
16
  marketParams;
@@ -19,6 +21,22 @@ class MorphoBlue {
19
21
  this.marketParams = marketParams;
20
22
  this.chainId = chainId;
21
23
  }
24
+ getReallocationPenaltyRequirements(userAddress, reallocationPlan) {
25
+ if (reallocationPlan.type === "vaultV1")
26
+ return [];
27
+ const { reallocations } = reallocationPlan;
28
+ const amount = reallocations.reduce((total, reallocation) => total +
29
+ blue_sdk_1.VaultV2BluePublicAllocatorConfigUtils.getPenaltyAssets(reallocation, reallocation.assets), 0n);
30
+ // Separate-token penalty funding uses a classic GeneralAdapter1 allowance so a collateral
31
+ // permit and a loan-token penalty can coexist in one bundle. The shared-token path aggregates
32
+ // both amounts into the collateral requirement instead.
33
+ return (0, index_js_1.getGeneralAdapterRequirements)(this.client.viemClient, {
34
+ address: this.marketParams.loanToken,
35
+ chainId: this.chainId,
36
+ supportSignature: false,
37
+ args: { amount, from: userAddress },
38
+ });
39
+ }
22
40
  async getMarketData(parameters) {
23
41
  (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
24
42
  return (0, blue_sdk_viem_1.fetchMarket)(this.marketParams.id, this.client.viemClient, {
@@ -92,6 +110,12 @@ class MorphoBlue {
92
110
  withdraw(params) {
93
111
  (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
94
112
  const { userAddress, receiver = userAddress, positionData, slippageTolerance = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, reallocations, } = params;
113
+ const reallocationPlan = (0, validate_js_1.validateAndNormalizeReallocations)({
114
+ reallocations,
115
+ targetMarketId: this.marketParams.id,
116
+ chainId: this.chainId,
117
+ });
118
+ const reallocationList = reallocationPlan.reallocations;
95
119
  // Mode normalization: a missing or undefined `assets`/`shares` key collapses to `0n`
96
120
  // so the mutual-exclusion and positivity checks below are pure value comparisons.
97
121
  const assets = ("assets" in params ? params.assets : undefined) ?? 0n;
@@ -113,9 +137,6 @@ class MorphoBlue {
113
137
  throw new index_js_3.NonPositiveInputError("assets or shares", 0n);
114
138
  }
115
139
  (0, index_js_2.validateSlippageTolerance)(slippageTolerance);
116
- if (reallocations) {
117
- (0, index_js_2.validateReallocations)(reallocations, this.marketParams.id);
118
- }
119
140
  if (!positionData) {
120
141
  throw new index_js_3.MissingAccrualPositionError(this.marketParams.id);
121
142
  }
@@ -146,13 +167,16 @@ class MorphoBlue {
146
167
  });
147
168
  return {
148
169
  getRequirements: async () => {
149
- const authTx = await (0, index_js_1.getBlueAuthorizationRequirement)({
150
- viemClient: this.client.viemClient,
151
- chainId: this.chainId,
152
- userAddress,
153
- supportSignature: this.client.options.supportSignature,
154
- });
155
- return authTx ? [authTx] : [];
170
+ const [penaltyRequirements, authTx] = await Promise.all([
171
+ this.getReallocationPenaltyRequirements(userAddress, reallocationPlan),
172
+ (0, index_js_1.getBlueAuthorizationRequirement)({
173
+ viemClient: this.client.viemClient,
174
+ chainId: this.chainId,
175
+ userAddress,
176
+ supportSignature: this.client.options.supportSignature,
177
+ }),
178
+ ]);
179
+ return [...penaltyRequirements, ...(authTx ? [authTx] : [])];
156
180
  },
157
181
  buildTx: (signatures) => {
158
182
  const { authorization } = (0, index_js_3.selectRequirementSignatures)(signatures, {
@@ -165,7 +189,7 @@ class MorphoBlue {
165
189
  shares,
166
190
  receiver,
167
191
  minSharePrice,
168
- reallocations,
192
+ reallocations: reallocationList,
169
193
  authorizationSignature: authorization,
170
194
  },
171
195
  metadata: this.client.options.metadata,
@@ -219,13 +243,16 @@ class MorphoBlue {
219
243
  }
220
244
  borrow({ amount, userAddress, positionData, slippageTolerance = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, reallocations, }) {
221
245
  (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
246
+ const reallocationPlan = (0, validate_js_1.validateAndNormalizeReallocations)({
247
+ reallocations,
248
+ targetMarketId: this.marketParams.id,
249
+ chainId: this.chainId,
250
+ });
251
+ const reallocationList = reallocationPlan.reallocations;
222
252
  if (amount <= 0n) {
223
253
  throw new index_js_3.NonPositiveInputError("amount", amount);
224
254
  }
225
255
  (0, index_js_2.validateSlippageTolerance)(slippageTolerance);
226
- if (reallocations) {
227
- (0, index_js_2.validateReallocations)(reallocations, this.marketParams.id);
228
- }
229
256
  if (!positionData) {
230
257
  throw new index_js_3.MissingAccrualPositionError(this.marketParams.id);
231
258
  }
@@ -248,13 +275,16 @@ class MorphoBlue {
248
275
  });
249
276
  return {
250
277
  getRequirements: async () => {
251
- const authTx = await (0, index_js_1.getBlueAuthorizationRequirement)({
252
- viemClient: this.client.viemClient,
253
- chainId: this.chainId,
254
- userAddress,
255
- supportSignature: this.client.options.supportSignature,
256
- });
257
- return authTx ? [authTx] : [];
278
+ const [penaltyRequirements, authTx] = await Promise.all([
279
+ this.getReallocationPenaltyRequirements(userAddress, reallocationPlan),
280
+ (0, index_js_1.getBlueAuthorizationRequirement)({
281
+ viemClient: this.client.viemClient,
282
+ chainId: this.chainId,
283
+ userAddress,
284
+ supportSignature: this.client.options.supportSignature,
285
+ }),
286
+ ]);
287
+ return [...penaltyRequirements, ...(authTx ? [authTx] : [])];
258
288
  },
259
289
  buildTx: (signatures) => {
260
290
  const { authorization } = (0, index_js_3.selectRequirementSignatures)(signatures, {
@@ -269,7 +299,7 @@ class MorphoBlue {
269
299
  amount,
270
300
  receiver: userAddress,
271
301
  minSharePrice,
272
- reallocations,
302
+ reallocations: reallocationList,
273
303
  authorizationSignature: authorization,
274
304
  },
275
305
  metadata: this.client.options.metadata,
@@ -606,6 +636,12 @@ class MorphoBlue {
606
636
  }
607
637
  supplyCollateralBorrow({ amount = 0n, userAddress, positionData, borrowAmount, nativeAmount, slippageTolerance = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, reallocations, }) {
608
638
  (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
639
+ const reallocationPlan = (0, validate_js_1.validateAndNormalizeReallocations)({
640
+ reallocations,
641
+ targetMarketId: this.marketParams.id,
642
+ chainId: this.chainId,
643
+ });
644
+ const reallocationList = reallocationPlan.reallocations;
609
645
  if (amount < 0n) {
610
646
  throw new index_js_3.NegativeInputError("amount", amount);
611
647
  }
@@ -620,9 +656,6 @@ class MorphoBlue {
620
656
  throw new index_js_3.NonPositiveInputError("totalCollateral", totalCollateral);
621
657
  }
622
658
  (0, index_js_2.validateSlippageTolerance)(slippageTolerance);
623
- if (reallocations) {
624
- (0, index_js_2.validateReallocations)(reallocations, this.marketParams.id);
625
- }
626
659
  if (!positionData) {
627
660
  throw new index_js_3.MissingAccrualPositionError(this.marketParams.id);
628
661
  }
@@ -648,15 +681,26 @@ class MorphoBlue {
648
681
  });
649
682
  return {
650
683
  getRequirements: async (params) => {
651
- const [erc20Requirements, authTx] = await Promise.all([
684
+ const penaltyAssets = reallocationPlan.type === "vaultV2Blue"
685
+ ? reallocationPlan.reallocations.reduce((total, reallocation) => total +
686
+ blue_sdk_1.VaultV2BluePublicAllocatorConfigUtils.getPenaltyAssets(reallocation, reallocation.assets), 0n)
687
+ : 0n;
688
+ const usesSharedFundingToken = (0, viem_1.isAddressEqual)(this.marketParams.collateralToken, this.marketParams.loanToken);
689
+ const [erc20Requirements, penaltyRequirements, authTx] = await Promise.all([
652
690
  (0, index_js_1.getGeneralAdapterRequirements)(this.client.viemClient, {
653
691
  address: this.marketParams.collateralToken,
654
692
  chainId: this.chainId,
655
693
  supportSignature: this.client.options.supportSignature,
656
694
  supportDeployless: this.client.options.supportDeployless,
657
695
  useSimplePermit: params?.useSimplePermit,
658
- args: { amount, from: userAddress },
696
+ args: {
697
+ amount: amount + (usesSharedFundingToken ? penaltyAssets : 0n),
698
+ from: userAddress,
699
+ },
659
700
  }),
701
+ usesSharedFundingToken
702
+ ? []
703
+ : this.getReallocationPenaltyRequirements(userAddress, reallocationPlan),
660
704
  (0, index_js_1.getBlueAuthorizationRequirement)({
661
705
  viemClient: this.client.viemClient,
662
706
  chainId: this.chainId,
@@ -664,7 +708,11 @@ class MorphoBlue {
664
708
  supportSignature: this.client.options.supportSignature,
665
709
  }),
666
710
  ]);
667
- return [...erc20Requirements, ...(authTx ? [authTx] : [])];
711
+ return [
712
+ ...erc20Requirements,
713
+ ...penaltyRequirements,
714
+ ...(authTx ? [authTx] : []),
715
+ ];
668
716
  },
669
717
  buildTx: (signatures) => {
670
718
  const { permit, authorization } = (0, index_js_3.selectRequirementSignatures)(signatures, { permit: true, authorization: true });
@@ -682,7 +730,7 @@ class MorphoBlue {
682
730
  minSharePrice,
683
731
  requirementSignature: permit,
684
732
  authorizationSignature: authorization,
685
- reallocations,
733
+ reallocations: reallocationList,
686
734
  },
687
735
  metadata: this.client.options.metadata,
688
736
  });
@@ -692,6 +740,12 @@ class MorphoBlue {
692
740
  refinance({ userAddress, positionData, target, collateralAmount, borrowAssets, borrowShares, slippageTolerance = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, targetReallocations, }) {
693
741
  (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
694
742
  (0, index_js_2.validateSlippageTolerance)(slippageTolerance);
743
+ const targetReallocationPlan = (0, validate_js_1.validateAndNormalizeReallocations)({
744
+ reallocations: targetReallocations,
745
+ targetMarketId: target.marketParams.id,
746
+ chainId: this.chainId,
747
+ });
748
+ const targetReallocationList = targetReallocationPlan.reallocations;
695
749
  if (collateralAmount <= 0n) {
696
750
  throw new index_js_3.NonPositiveInputError("collateralAmount", collateralAmount);
697
751
  }
@@ -706,9 +760,6 @@ class MorphoBlue {
706
760
  if (requestedAssets > 0n && requestedShares > 0n) {
707
761
  throw new index_js_3.BorrowAmountAndSharesExclusiveError(this.marketParams.id);
708
762
  }
709
- if (targetReallocations) {
710
- (0, index_js_2.validateReallocations)(targetReallocations, target.marketParams.id);
711
- }
712
763
  if (!positionData) {
713
764
  throw new index_js_3.MissingAccrualPositionError(this.marketParams.id);
714
765
  }
@@ -822,13 +873,16 @@ class MorphoBlue {
822
873
  : 0n;
823
874
  return {
824
875
  getRequirements: async () => {
825
- const authTx = await (0, index_js_1.getBlueAuthorizationRequirement)({
826
- viemClient: this.client.viemClient,
827
- chainId: this.chainId,
828
- userAddress,
829
- supportSignature: this.client.options.supportSignature,
830
- });
831
- return authTx ? [authTx] : [];
876
+ const [penaltyRequirements, authTx] = await Promise.all([
877
+ this.getReallocationPenaltyRequirements(userAddress, targetReallocationPlan),
878
+ (0, index_js_1.getBlueAuthorizationRequirement)({
879
+ viemClient: this.client.viemClient,
880
+ chainId: this.chainId,
881
+ userAddress,
882
+ supportSignature: this.client.options.supportSignature,
883
+ }),
884
+ ]);
885
+ return [...penaltyRequirements, ...(authTx ? [authTx] : [])];
832
886
  },
833
887
  buildTx: (signatures) => {
834
888
  const { authorization } = (0, index_js_3.selectRequirementSignatures)(signatures, {
@@ -847,7 +901,7 @@ class MorphoBlue {
847
901
  borrowShares: requestedShares,
848
902
  minBorrowSharePrice,
849
903
  maxRepaySharePrice,
850
- targetReallocations,
904
+ targetReallocations: targetReallocationList,
851
905
  authorizationSignature: authorization,
852
906
  },
853
907
  metadata: this.client.options.metadata,
@@ -858,13 +912,30 @@ class MorphoBlue {
858
912
  /**
859
913
  * Fetches all on-chain inputs needed to compute public allocator reallocations.
860
914
  *
861
- * @param params - Reallocation data fetch parameters.
862
915
  * @param params.vaultAddresses - Vaults to inspect for source-market liquidity.
863
- * @param params.block - Block number and timestamp used for consistent RPC reads.
864
- * @returns Reallocation data ready for {@link getReallocations}.
916
+ * @param params.block.number - Block number used for every RPC read.
917
+ * @param params.block.timestamp - Timestamp corresponding to the fetched block.
918
+ * @returns Reallocation data ready for {@link getVaultV1Reallocations}.
865
919
  * @throws {ChainIdMismatchError} when the client chain does not match this market.
920
+ * @example
921
+ * ```ts
922
+ * import { markets, vaults } from "@morpho-org/morpho-test";
923
+ * import { createPublicClient, http } from "viem";
924
+ * import { mainnet } from "viem/chains";
925
+ * import { morphoViemExtension } from "@morpho-org/morpho-sdk";
926
+ * import type { VaultV1ReallocationData } from "@morpho-org/morpho-sdk/entities";
927
+ *
928
+ * const client = createPublicClient({ chain: mainnet, transport: http() })
929
+ * .extend(morphoViemExtension());
930
+ * const market = client.morpho.blue(markets[mainnet.id].usdc_wbtc, mainnet.id);
931
+ * const block = await client.getBlock();
932
+ * const data: VaultV1ReallocationData = await market.getVaultV1ReallocationData({
933
+ * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
934
+ * block,
935
+ * });
936
+ * ```
866
937
  */
867
- async getReallocationData({ vaultAddresses, block, }) {
938
+ async getVaultV1ReallocationData({ vaultAddresses, block, }) {
868
939
  (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
869
940
  const client = this.client.viemClient;
870
941
  const fetchParams = {
@@ -904,7 +975,7 @@ class MorphoBlue {
904
975
  position,
905
976
  })))),
906
977
  ]);
907
- // Assemble records for ReallocationData.
978
+ // Assemble records for VaultV1ReallocationData.
908
979
  const marketsRecord = {
909
980
  [targetMarketId]: targetMarket,
910
981
  };
@@ -923,7 +994,7 @@ class MorphoBlue {
923
994
  for (const { vault, mid, position } of positions) {
924
995
  (positionsRecord[vault] ??= {})[mid] = position;
925
996
  }
926
- return new reallocationData_js_1.ReallocationData({
997
+ return new vaultV1ReallocationData_js_1.VaultV1ReallocationData({
927
998
  chainId: this.chainId,
928
999
  markets: marketsRecord,
929
1000
  vaults: vaultsRecord,
@@ -932,13 +1003,109 @@ class MorphoBlue {
932
1003
  });
933
1004
  }
934
1005
  /**
935
- * Computes public allocator reallocations for a borrow or withdraw on this market.
1006
+ * Fetches Vault V1 PublicAllocator state using the deprecated unversioned name.
1007
+ *
1008
+ * @param params.vaultAddresses - Addresses of MetaMorpho vaults that allocate to this market.
1009
+ * @param params.block.number - Block number used for every RPC read.
1010
+ * @param params.block.timestamp - Timestamp corresponding to the fetched block.
1011
+ * @returns A `VaultV1ReallocationData` snapshot populated from one block.
1012
+ * @throws {ChainIdMismatchError} when the client chain does not match this market.
1013
+ * @deprecated Use {@link getVaultV1ReallocationData} instead.
1014
+ * @example
1015
+ * ```ts
1016
+ * const data = await market.getReallocationData({ vaultAddresses, block });
1017
+ * // Equivalent to market.getVaultV1ReallocationData({ vaultAddresses, block }).
1018
+ * ```
1019
+ */
1020
+ getReallocationData(params) {
1021
+ return this.getVaultV1ReallocationData(params);
1022
+ }
1023
+ /**
1024
+ * Fetches Vault V2 BluePublicAllocator state for this target market.
1025
+ *
1026
+ * Reads the target Morpho Blue market, each Vault V2 accrual tree, and each
1027
+ * vault's BluePublicAllocator permissions and allocation caps at one block.
1028
+ *
1029
+ * @param params.vaultAddresses - Vault V2 addresses to inspect for market or idle liquidity.
1030
+ * @param params.block.number - Block number used for every RPC read.
1031
+ * @param params.block.timestamp - Timestamp corresponding to the fetched block.
1032
+ * @returns A `VaultV2BlueReallocationData` snapshot ready for {@link getVaultV2BlueReallocations}.
1033
+ * @throws {ChainIdMismatchError} when the client chain does not match this market.
1034
+ * @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
1035
+ * @throws {UnknownAddressError} when the chain has no BluePublicAllocator deployment.
1036
+ * @throws {UnknownFactory} when the chain has no Vault V2 factory.
1037
+ * @throws {UnknownOfFactory} when a requested address is not a Vault V2 from that factory.
1038
+ * @throws {UnsupportedVaultV2AdapterError} when a vault contains an unsupported adapter.
1039
+ * @throws {viem.BaseError} when an RPC or contract read fails with no fallback left.
1040
+ * @example
1041
+ * ```ts
1042
+ * import { markets } from "@morpho-org/morpho-test";
1043
+ * import { createPublicClient, http } from "viem";
1044
+ * import { mainnet } from "viem/chains";
1045
+ * import { morphoViemExtension } from "@morpho-org/morpho-sdk";
1046
+ * import type { VaultV2BlueReallocationData } from "@morpho-org/morpho-sdk/entities";
1047
+ *
1048
+ * const client = createPublicClient({ chain: mainnet, transport: http() })
1049
+ * .extend(morphoViemExtension());
1050
+ * const market = client.morpho.blue(markets[mainnet.id].usdc_wbtc, mainnet.id);
1051
+ * const block = await client.getBlock();
1052
+ * const keyrockUsdcVaultV2 = "0x04422053aDDbc9bB2759b248B574e3FCA76Bc145";
1053
+ * const data: VaultV2BlueReallocationData =
1054
+ * await market.getVaultV2BlueReallocationData({
1055
+ * vaultAddresses: [keyrockUsdcVaultV2],
1056
+ * block,
1057
+ * });
1058
+ * ```
1059
+ */
1060
+ async getVaultV2BlueReallocationData({ vaultAddresses, block, }) {
1061
+ (0, index_js_2.validateChainId)(this.client.viemClient.chain?.id, this.chainId);
1062
+ const client = this.client.viemClient;
1063
+ const fetchParams = {
1064
+ blockNumber: block.number,
1065
+ chainId: this.chainId,
1066
+ deployless: this.client.options.supportDeployless,
1067
+ };
1068
+ const [targetMarket, vaultEntries] = await Promise.all([
1069
+ (0, blue_sdk_viem_1.fetchMarket)(this.marketParams.id, client, fetchParams),
1070
+ Promise.all(vaultAddresses.map(async (address) => {
1071
+ const vault = await (0, blue_sdk_viem_1.fetchAccrualVaultV2)(address, client, fetchParams);
1072
+ const publicAllocatorData = await (0, blue_sdk_viem_1.fetchVaultV2BluePublicAllocatorData)(vault, client, {
1073
+ ...fetchParams,
1074
+ targetMarketParams: this.marketParams,
1075
+ });
1076
+ return { publicAllocatorData, vault };
1077
+ })),
1078
+ ]);
1079
+ return new vaultV2BlueReallocationData_js_1.VaultV2BlueReallocationData({
1080
+ chainId: this.chainId,
1081
+ markets: { [targetMarket.id]: targetMarket },
1082
+ vaults: Object.fromEntries(vaultEntries.map(({ vault }) => [vault.address, vault])),
1083
+ allocations: Object.fromEntries(vaultEntries.map(({ publicAllocatorData, vault }) => [
1084
+ vault.address,
1085
+ publicAllocatorData.allocations,
1086
+ ])),
1087
+ publicAllocatorConfigs: Object.fromEntries(vaultEntries.map(({ publicAllocatorData, vault }) => [
1088
+ vault.address,
1089
+ publicAllocatorData.publicAllocatorConfig,
1090
+ ])),
1091
+ activeAdapters: Object.fromEntries(vaultEntries.map(({ publicAllocatorData, vault }) => [
1092
+ vault.address,
1093
+ publicAllocatorData.activeAdapters,
1094
+ ])),
1095
+ marketPublicAllocatorConfigs: Object.fromEntries(vaultEntries.map(({ publicAllocatorData, vault }) => [
1096
+ vault.address,
1097
+ publicAllocatorData.marketPublicAllocatorConfigs,
1098
+ ])),
1099
+ });
1100
+ }
1101
+ /**
1102
+ * Computes Vault V1 PublicAllocator reallocations for this market.
936
1103
  *
937
1104
  * Pass `{ borrowAmount }` for a borrow (legacy alias, equivalent to `{ operation: "borrow", amount }`)
938
1105
  * or `{ operation, amount }` for a borrow or loan-asset withdraw.
939
1106
  *
940
1107
  * @param params - Reallocation computation parameters.
941
- * @param params.reallocationData - State returned by {@link getReallocationData}.
1108
+ * @param params.reallocationData - State returned by {@link getVaultV1ReallocationData}.
942
1109
  * @param params.operation - The operation driving the reallocation (`"borrow"` or `"withdraw"`).
943
1110
  * @param params.amount - The borrow or withdraw amount used to compute the post-state utilization.
944
1111
  * @param params.borrowAmount - {@deprecated Pass `{ operation: "borrow", amount }` instead.}
@@ -949,13 +1116,21 @@ class MorphoBlue {
949
1116
  * @throws {ReallocationWithdrawExceedsMarketSupplyError} when `operation === "withdraw"` and `amount` exceeds the target market's `totalSupplyAssets`.
950
1117
  * @throws {MissingPublicAllocatorConfigError} when a selected vault is missing its public allocator config.
951
1118
  * @throws {UnknownReallocationMarketError} when the target market is absent from the reallocation data.
1119
+ * @example
1120
+ * ```ts
1121
+ * const reallocations = market.getVaultV1Reallocations({
1122
+ * reallocationData,
1123
+ * operation: "borrow",
1124
+ * amount: 1_000_000n,
1125
+ * });
1126
+ * ```
952
1127
  */
953
- getReallocations(params) {
1128
+ getVaultV1Reallocations(params) {
954
1129
  (0, index_js_2.validateChainId)(params.reallocationData.chainId, this.chainId);
955
1130
  const marketId = this.marketParams.id;
956
1131
  const options = { enabled: true, ...params.options };
957
1132
  if (params.borrowAmount !== undefined) {
958
- return (0, index_js_2.computeReallocations)({
1133
+ return (0, index_js_2.computeVaultV1Reallocations)({
959
1134
  reallocationData: params.reallocationData,
960
1135
  marketId,
961
1136
  operation: "borrow",
@@ -963,7 +1138,7 @@ class MorphoBlue {
963
1138
  options,
964
1139
  });
965
1140
  }
966
- return (0, index_js_2.computeReallocations)({
1141
+ return (0, index_js_2.computeVaultV1Reallocations)({
967
1142
  reallocationData: params.reallocationData,
968
1143
  marketId,
969
1144
  operation: params.operation,
@@ -971,5 +1146,63 @@ class MorphoBlue {
971
1146
  options,
972
1147
  });
973
1148
  }
1149
+ /**
1150
+ * Computes Vault V1 PublicAllocator reallocations using the deprecated unversioned name.
1151
+ *
1152
+ * @param params.reallocationData - State returned by {@link getVaultV1ReallocationData}.
1153
+ * @param params.operation - The operation driving the reallocation (`"borrow"` or `"withdraw"`).
1154
+ * @param params.amount - The borrow or withdraw amount used to compute post-state utilization.
1155
+ * @param params.borrowAmount - Deprecated borrow amount alias.
1156
+ * @param params.options - Optional allocator and utilization options.
1157
+ * @returns Vault V1 reallocations ready for a Blue action.
1158
+ * @throws {ChainIdMismatchError} when `reallocationData` belongs to another chain.
1159
+ * @throws {InsufficientSharedLiquidityError} when shared liquidity cannot cover the operation.
1160
+ * @throws {ReallocationWithdrawExceedsMarketSupplyError} when a withdrawal exceeds market supply.
1161
+ * @throws {MissingPublicAllocatorConfigError} when a selected vault lacks allocator state.
1162
+ * @throws {UnknownReallocationMarketError} when the target market is absent.
1163
+ * @deprecated Use {@link getVaultV1Reallocations} instead.
1164
+ * @example
1165
+ * ```ts
1166
+ * const reallocations = market.getReallocations({
1167
+ * reallocationData,
1168
+ * operation: "borrow",
1169
+ * amount: 1_000_000n,
1170
+ * });
1171
+ * ```
1172
+ */
1173
+ getReallocations(params) {
1174
+ return this.getVaultV1Reallocations(params);
1175
+ }
1176
+ /**
1177
+ * Computes Vault V2 BluePublicAllocator reallocations for this market.
1178
+ *
1179
+ * @param params.reallocationData - State returned by {@link getVaultV2BlueReallocationData}.
1180
+ * @param params.options - Optional allocator discovery controls and operation to support.
1181
+ * @returns Action-ready reallocations and their post-simulation state.
1182
+ * @throws {ChainIdMismatchError} when `reallocationData` belongs to another chain.
1183
+ * @throws {NegativeInputError} when a utilization or penalty limit is negative.
1184
+ * @throws {InputExceedsMaxError} when a utilization or penalty limit exceeds WAD.
1185
+ * @throws {NonPositiveInputError} when an enabled operation amount is not positive.
1186
+ * @throws {UnknownReallocationMarketError} when a required market is absent.
1187
+ * @throws {UnknownReallocationVaultError} when configured vault state is absent.
1188
+ * @throws {UnknownReallocationPublicAllocatorConfigError} when allocator authorization state is absent.
1189
+ * @throws {UnknownReallocationActiveAdaptersError} when active-adapter state is absent.
1190
+ * @throws {UnknownReallocationMarketPublicAllocatorConfigError} when an adapter-market allocator configuration is absent.
1191
+ * @throws {UnknownReallocationAllocationError} when required allocation state is absent.
1192
+ * @throws {ReallocationAllocationUnderflowError} when an inconsistent allocation snapshot underflows during the final transition.
1193
+ * @throws {InsufficientSharedLiquidityError} when selected liquidity cannot cover the shortfall.
1194
+ * @throws {ReallocationWithdrawExceedsMarketSupplyError} when a withdrawal exceeds market supply.
1195
+ * @example
1196
+ * ```ts
1197
+ * const result = market.getVaultV2BlueReallocations({
1198
+ * reallocationData,
1199
+ * options: { operation: { type: "borrow", amount: 1_000_000n } },
1200
+ * });
1201
+ * ```
1202
+ */
1203
+ getVaultV2BlueReallocations({ reallocationData, options, }) {
1204
+ (0, index_js_2.validateChainId)(reallocationData.chainId, this.chainId);
1205
+ return reallocationData.computeVaultV2BlueReallocations(this.marketParams.id, options);
1206
+ }
974
1207
  }
975
1208
  exports.MorphoBlue = MorphoBlue;
@@ -2,6 +2,7 @@ export type { IAccrualPosition, IAccrualVault, IAccrualVaultV2, IAccrualVaultV2A
2
2
  export { AccrualPosition, AccrualVault, AccrualVaultV2, AccrualVaultV2MorphoMarketV1Adapter, AccrualVaultV2MorphoMarketV1AdapterV2, AccrualVaultV2MorphoVaultV1Adapter, AssetBalances, ConstantWrappedToken, Eip5267Domain, ExchangeRateWrappedToken, Holding, Market, MarketParams, Position, PreLiquidationParams, PreLiquidationPosition, Token, User, Vault, VaultConfig, VaultMarketAllocation, VaultMarketConfig, VaultMarketPublicAllocatorConfig, VaultToken, VaultUser, VaultV2, VaultV2Adapter, VaultV2MorphoMarketV1Adapter, VaultV2MorphoMarketV1AdapterV2, VaultV2MorphoVaultV1Adapter, WrappedToken, } from "@morpho-org/blue-sdk";
3
3
  export { MorphoBlue } from "./blue/index.js";
4
4
  export * from "./midnight/index.js";
5
- export { type InputReallocationData, ReallocationData, } from "./reallocationData.js";
6
5
  export { MorphoVaultV1 } from "./vaultV1/index.js";
6
+ export { type InputReallocationData, type InputVaultV1ReallocationData, ReallocationData, VaultV1ReallocationData, } from "./vaultV1ReallocationData.js";
7
7
  export { MorphoVaultV2 } from "./vaultV2/index.js";
8
+ export { type InputVaultV2BlueReallocationData, VaultV2BlueReallocationData, } from "./vaultV2BlueReallocationData.js";
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.MorphoVaultV2 = exports.MorphoVaultV1 = exports.ReallocationData = exports.MorphoBlue = exports.WrappedToken = exports.VaultV2MorphoVaultV1Adapter = exports.VaultV2MorphoMarketV1AdapterV2 = exports.VaultV2MorphoMarketV1Adapter = exports.VaultV2Adapter = exports.VaultV2 = exports.VaultUser = exports.VaultToken = exports.VaultMarketPublicAllocatorConfig = exports.VaultMarketConfig = exports.VaultMarketAllocation = exports.VaultConfig = exports.Vault = exports.User = exports.Token = exports.PreLiquidationPosition = exports.PreLiquidationParams = exports.Position = exports.MarketParams = exports.Market = exports.Holding = exports.ExchangeRateWrappedToken = exports.Eip5267Domain = exports.ConstantWrappedToken = exports.AssetBalances = exports.AccrualVaultV2MorphoVaultV1Adapter = exports.AccrualVaultV2MorphoMarketV1AdapterV2 = exports.AccrualVaultV2MorphoMarketV1Adapter = exports.AccrualVaultV2 = exports.AccrualVault = exports.AccrualPosition = void 0;
17
+ exports.VaultV2BlueReallocationData = exports.MorphoVaultV2 = exports.VaultV1ReallocationData = exports.ReallocationData = exports.MorphoVaultV1 = exports.MorphoBlue = exports.WrappedToken = exports.VaultV2MorphoVaultV1Adapter = exports.VaultV2MorphoMarketV1AdapterV2 = exports.VaultV2MorphoMarketV1Adapter = exports.VaultV2Adapter = exports.VaultV2 = exports.VaultUser = exports.VaultToken = exports.VaultMarketPublicAllocatorConfig = exports.VaultMarketConfig = exports.VaultMarketAllocation = exports.VaultConfig = exports.Vault = exports.User = exports.Token = exports.PreLiquidationPosition = exports.PreLiquidationParams = exports.Position = exports.MarketParams = exports.Market = exports.Holding = exports.ExchangeRateWrappedToken = exports.Eip5267Domain = exports.ConstantWrappedToken = exports.AssetBalances = exports.AccrualVaultV2MorphoVaultV1Adapter = exports.AccrualVaultV2MorphoMarketV1AdapterV2 = exports.AccrualVaultV2MorphoMarketV1Adapter = exports.AccrualVaultV2 = exports.AccrualVault = exports.AccrualPosition = void 0;
18
18
  var blue_sdk_1 = require("@morpho-org/blue-sdk");
19
19
  Object.defineProperty(exports, "AccrualPosition", { enumerable: true, get: function () { return blue_sdk_1.AccrualPosition; } });
20
20
  Object.defineProperty(exports, "AccrualVault", { enumerable: true, get: function () { return blue_sdk_1.AccrualVault; } });
@@ -50,9 +50,12 @@ Object.defineProperty(exports, "WrappedToken", { enumerable: true, get: function
50
50
  var index_js_1 = require("./blue/index.js");
51
51
  Object.defineProperty(exports, "MorphoBlue", { enumerable: true, get: function () { return index_js_1.MorphoBlue; } });
52
52
  __exportStar(require("./midnight/index.js"), exports);
53
- var reallocationData_js_1 = require("./reallocationData.js");
54
- Object.defineProperty(exports, "ReallocationData", { enumerable: true, get: function () { return reallocationData_js_1.ReallocationData; } });
55
53
  var index_js_2 = require("./vaultV1/index.js");
56
54
  Object.defineProperty(exports, "MorphoVaultV1", { enumerable: true, get: function () { return index_js_2.MorphoVaultV1; } });
55
+ var vaultV1ReallocationData_js_1 = require("./vaultV1ReallocationData.js");
56
+ Object.defineProperty(exports, "ReallocationData", { enumerable: true, get: function () { return vaultV1ReallocationData_js_1.ReallocationData; } });
57
+ Object.defineProperty(exports, "VaultV1ReallocationData", { enumerable: true, get: function () { return vaultV1ReallocationData_js_1.VaultV1ReallocationData; } });
57
58
  var index_js_3 = require("./vaultV2/index.js");
58
59
  Object.defineProperty(exports, "MorphoVaultV2", { enumerable: true, get: function () { return index_js_3.MorphoVaultV2; } });
60
+ var vaultV2BlueReallocationData_js_1 = require("./vaultV2BlueReallocationData.js");
61
+ Object.defineProperty(exports, "VaultV2BlueReallocationData", { enumerable: true, get: function () { return vaultV2BlueReallocationData_js_1.VaultV2BlueReallocationData; } });