@morpho-org/morpho-sdk 5.5.0 → 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 (90) hide show
  1. package/README.md +8 -6
  2. package/lib/cjs/abis.d.ts +1 -1
  3. package/lib/cjs/abis.js +3 -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/getRequirementsApproval.js +2 -1
  17. package/lib/cjs/bundler/actions.d.ts +117 -0
  18. package/lib/cjs/bundler/actions.js +211 -4
  19. package/lib/cjs/bundler/types.d.ts +20 -0
  20. package/lib/cjs/constants.d.ts +1 -1
  21. package/lib/cjs/constants.js +3 -1
  22. package/lib/cjs/entities/blue/blue.d.ts +360 -71
  23. package/lib/cjs/entities/blue/blue.js +285 -52
  24. package/lib/cjs/entities/index.d.ts +2 -1
  25. package/lib/cjs/entities/index.js +6 -3
  26. package/lib/cjs/entities/{reallocationData.d.ts → vaultV1ReallocationData.d.ts} +40 -15
  27. package/lib/cjs/entities/{reallocationData.js → vaultV1ReallocationData.js} +29 -16
  28. package/lib/cjs/entities/vaultV2/vaultV2.d.ts +1 -1
  29. package/lib/cjs/entities/vaultV2/vaultV2.js +1 -1
  30. package/lib/cjs/entities/vaultV2BlueReallocationData.d.ts +511 -0
  31. package/lib/cjs/entities/vaultV2BlueReallocationData.js +1311 -0
  32. package/lib/{esm/helpers/computeReallocations.d.ts → cjs/helpers/computeVaultV1Reallocations.d.ts} +14 -8
  33. package/lib/cjs/helpers/{computeReallocations.js → computeVaultV1Reallocations.js} +27 -13
  34. package/lib/cjs/helpers/constant.d.ts +5 -3
  35. package/lib/cjs/helpers/constant.js +6 -4
  36. package/lib/cjs/helpers/index.d.ts +2 -2
  37. package/lib/cjs/helpers/index.js +6 -3
  38. package/lib/cjs/helpers/validate.d.ts +30 -14
  39. package/lib/cjs/helpers/validate.js +140 -27
  40. package/lib/cjs/types/action.d.ts +12 -0
  41. package/lib/cjs/types/error.d.ts +224 -5
  42. package/lib/cjs/types/error.js +269 -7
  43. package/lib/cjs/types/sharedLiquidity.d.ts +72 -8
  44. package/lib/cjs/utils.d.ts +2 -2
  45. package/lib/cjs/utils.js +6 -4
  46. package/lib/esm/abis.d.ts +1 -1
  47. package/lib/esm/abis.js +1 -1
  48. package/lib/esm/actions/blue/borrow.d.ts +21 -15
  49. package/lib/esm/actions/blue/borrow.js +26 -19
  50. package/lib/esm/actions/blue/buildAssetFundingActions.d.ts +1 -1
  51. package/lib/esm/actions/blue/buildAssetFundingActions.js +1 -1
  52. package/lib/esm/actions/blue/buildReallocationActions.d.ts +62 -19
  53. package/lib/esm/actions/blue/buildReallocationActions.js +119 -25
  54. package/lib/esm/actions/blue/refinance.d.ts +14 -7
  55. package/lib/esm/actions/blue/refinance.js +19 -11
  56. package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +28 -19
  57. package/lib/esm/actions/blue/supplyCollateralBorrow.js +40 -24
  58. package/lib/esm/actions/blue/withdraw.d.ts +20 -13
  59. package/lib/esm/actions/blue/withdraw.js +24 -19
  60. package/lib/esm/actions/requirements/getRequirementsApproval.js +2 -1
  61. package/lib/esm/bundler/actions.d.ts +117 -0
  62. package/lib/esm/bundler/actions.js +214 -7
  63. package/lib/esm/bundler/types.d.ts +20 -0
  64. package/lib/esm/constants.d.ts +1 -1
  65. package/lib/esm/constants.js +1 -1
  66. package/lib/esm/entities/blue/blue.d.ts +360 -71
  67. package/lib/esm/entities/blue/blue.js +288 -55
  68. package/lib/esm/entities/index.d.ts +2 -1
  69. package/lib/esm/entities/index.js +2 -1
  70. package/lib/esm/entities/{reallocationData.d.ts → vaultV1ReallocationData.d.ts} +40 -15
  71. package/lib/esm/entities/{reallocationData.js → vaultV1ReallocationData.js} +32 -14
  72. package/lib/esm/entities/vaultV2/vaultV2.d.ts +1 -1
  73. package/lib/esm/entities/vaultV2/vaultV2.js +1 -1
  74. package/lib/esm/entities/vaultV2BlueReallocationData.d.ts +511 -0
  75. package/lib/esm/entities/vaultV2BlueReallocationData.js +1307 -0
  76. package/lib/{cjs/helpers/computeReallocations.d.ts → esm/helpers/computeVaultV1Reallocations.d.ts} +14 -8
  77. package/lib/esm/helpers/{computeReallocations.js → computeVaultV1Reallocations.js} +25 -11
  78. package/lib/esm/helpers/constant.d.ts +5 -3
  79. package/lib/esm/helpers/constant.js +5 -3
  80. package/lib/esm/helpers/index.d.ts +2 -2
  81. package/lib/esm/helpers/index.js +2 -2
  82. package/lib/esm/helpers/validate.d.ts +30 -14
  83. package/lib/esm/helpers/validate.js +141 -30
  84. package/lib/esm/types/action.d.ts +12 -0
  85. package/lib/esm/types/error.d.ts +224 -5
  86. package/lib/esm/types/error.js +253 -4
  87. package/lib/esm/types/sharedLiquidity.d.ts +72 -8
  88. package/lib/esm/utils.d.ts +2 -2
  89. package/lib/esm/utils.js +2 -2
  90. package/package.json +5 -5
@@ -1,6 +1,6 @@
1
1
  import { type MarketId } from "@morpho-org/blue-sdk";
2
- import type { ReallocationData } from "../entities/reallocationData.js";
3
- import { type ReallocationComputeOptions, type VaultReallocation } from "../types/index.js";
2
+ import type { VaultV1ReallocationData } from "../entities/vaultV1ReallocationData.js";
3
+ import { type ReallocationComputeOptions, type VaultV1Reallocation } from "../types/index.js";
4
4
  /**
5
5
  * Computes vault reallocations for a `borrow` or `withdraw` on a target market.
6
6
  *
@@ -31,7 +31,7 @@ import { type ReallocationComputeOptions, type VaultReallocation } from "../type
31
31
  * import { mainnet } from "viem/chains";
32
32
  * import { markets, vaults } from "@morpho-org/morpho-test";
33
33
  * import {
34
- * computeReallocations,
34
+ * computeVaultV1Reallocations,
35
35
  * morphoViemExtension,
36
36
  * } from "@morpho-org/morpho-sdk";
37
37
  *
@@ -44,12 +44,12 @@ import { type ReallocationComputeOptions, type VaultReallocation } from "../type
44
44
  * const marketParams = markets[mainnet.id].usdc_wbtc;
45
45
  * const market = client.morpho.blue(marketParams, mainnet.id);
46
46
  * const block = await client.getBlock();
47
- * const reallocationData = await market.getReallocationData({
47
+ * const reallocationData = await market.getVaultV1ReallocationData({
48
48
  * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
49
49
  * block: { number: block.number, timestamp: block.timestamp },
50
50
  * });
51
51
  * const borrowAmount = parseUnits("1000", 6);
52
- * const reallocations = computeReallocations({
52
+ * const reallocations = computeVaultV1Reallocations({
53
53
  * reallocationData,
54
54
  * marketId: marketParams.id,
55
55
  * operation: "borrow",
@@ -66,10 +66,16 @@ import { type ReallocationComputeOptions, type VaultReallocation } from "../type
66
66
  * // borrow.buildTx() includes any required PublicAllocator reallocations.
67
67
  * ```
68
68
  */
69
- export declare const computeReallocations: ({ reallocationData: data, marketId, operation, amount, options, }: {
70
- readonly reallocationData: ReallocationData;
69
+ export declare const computeVaultV1Reallocations: ({ reallocationData: data, marketId, operation, amount, options, }: {
70
+ readonly reallocationData: VaultV1ReallocationData;
71
71
  readonly marketId: MarketId;
72
72
  readonly operation: "borrow" | "withdraw";
73
73
  readonly amount: bigint;
74
74
  readonly options?: ReallocationComputeOptions;
75
- }) => readonly VaultReallocation[];
75
+ }) => readonly VaultV1Reallocation[];
76
+ /**
77
+ * Deprecated name for the Vault V1 amount-aware reallocation planner.
78
+ *
79
+ * @deprecated Use {@link computeVaultV1Reallocations} instead.
80
+ */
81
+ export declare const computeReallocations: typeof computeVaultV1Reallocations;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.computeReallocations = void 0;
3
+ exports.computeReallocations = exports.computeVaultV1Reallocations = void 0;
4
4
  const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
5
  const index_js_1 = require("../types/index.js");
6
6
  const utilization_js_1 = require("./utilization.js");
@@ -87,7 +87,7 @@ const capVaultWithdrawals = (withdrawals, assets) => {
87
87
  * import { mainnet } from "viem/chains";
88
88
  * import { markets, vaults } from "@morpho-org/morpho-test";
89
89
  * import {
90
- * computeReallocations,
90
+ * computeVaultV1Reallocations,
91
91
  * morphoViemExtension,
92
92
  * } from "@morpho-org/morpho-sdk";
93
93
  *
@@ -100,12 +100,12 @@ const capVaultWithdrawals = (withdrawals, assets) => {
100
100
  * const marketParams = markets[mainnet.id].usdc_wbtc;
101
101
  * const market = client.morpho.blue(marketParams, mainnet.id);
102
102
  * const block = await client.getBlock();
103
- * const reallocationData = await market.getReallocationData({
103
+ * const reallocationData = await market.getVaultV1ReallocationData({
104
104
  * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
105
105
  * block: { number: block.number, timestamp: block.timestamp },
106
106
  * });
107
107
  * const borrowAmount = parseUnits("1000", 6);
108
- * const reallocations = computeReallocations({
108
+ * const reallocations = computeVaultV1Reallocations({
109
109
  * reallocationData,
110
110
  * marketId: marketParams.id,
111
111
  * operation: "borrow",
@@ -122,12 +122,20 @@ const capVaultWithdrawals = (withdrawals, assets) => {
122
122
  * // borrow.buildTx() includes any required PublicAllocator reallocations.
123
123
  * ```
124
124
  */
125
- const computeReallocations = ({ reallocationData: data, marketId, operation, amount, options, }) => {
125
+ const computeVaultV1Reallocations = ({ reallocationData: data, marketId, operation, amount, options, }) => {
126
126
  if (options?.enabled === false)
127
127
  return [];
128
- // ReallocationData does not retain the fetch block; pass that block timestamp
128
+ const normalizedOptions = {
129
+ ...options,
130
+ reallocatableVaults: options?.reallocatableVaults == null
131
+ ? undefined
132
+ : [...options.reallocatableVaults],
133
+ };
134
+ // VaultV1ReallocationData does not retain the fetch block; pass that block timestamp
129
135
  // to compute against the same accrued state, otherwise Market defaults to lastUpdate.
130
- const market = data.getMarket(marketId).accrueInterest(options?.timestamp);
136
+ const market = data
137
+ .getMarket(marketId)
138
+ .accrueInterest(normalizedOptions.timestamp);
131
139
  // Reject unreachable withdraws before any utilization math: a negative
132
140
  // post-supply yields a negative utilization that short-circuits the
133
141
  // utilization gate below and silently returns `[]` — masking a sure revert.
@@ -145,7 +153,7 @@ const computeReallocations = ({ reallocationData: data, marketId, operation, amo
145
153
  const newTotalSupplyAssets = operation === "withdraw"
146
154
  ? market.totalSupplyAssets - amount
147
155
  : market.totalSupplyAssets;
148
- const supplyTargetUtilization = (0, utilization_js_1.getSupplyTargetUtilization)(market.params.id, options);
156
+ const supplyTargetUtilization = (0, utilization_js_1.getSupplyTargetUtilization)(market.params.id, normalizedOptions);
149
157
  if (blue_sdk_1.MarketUtils.getUtilization({
150
158
  totalSupplyAssets: newTotalSupplyAssets,
151
159
  totalBorrowAssets: newTotalBorrowAssets,
@@ -157,7 +165,7 @@ const computeReallocations = ({ reallocationData: data, marketId, operation, amo
157
165
  : blue_sdk_1.MathLib.wDivDown(newTotalBorrowAssets, supplyTargetUtilization) -
158
166
  newTotalSupplyAssets;
159
167
  // Phase 1: "friendly" reallocations respecting withdrawal utilization targets.
160
- const { withdrawals: friendlyWithdrawals, data: friendlyReallocationData } = data.getMarketPublicReallocations(market.id, options);
168
+ const { withdrawals: friendlyWithdrawals, data: friendlyReallocationData } = data.computeVaultV1Reallocations(market.id, normalizedOptions);
161
169
  const withdrawals = [...friendlyWithdrawals];
162
170
  const friendlyReallocationMarket = friendlyReallocationData.getMarket(market.id);
163
171
  // Operation-specific post-friendly check: would the on-chain call still revert?
@@ -170,8 +178,8 @@ const computeReallocations = ({ reallocationData: data, marketId, operation, amo
170
178
  if (friendlyBorrow > friendlySupply) {
171
179
  // Phase 2: "aggressive" — fully withdraw from every market (100% utilization).
172
180
  requiredAssets = newTotalBorrowAssets - newTotalSupplyAssets;
173
- withdrawals.push(...friendlyReallocationData.getMarketPublicReallocations(market.id, {
174
- ...options,
181
+ withdrawals.push(...friendlyReallocationData.computeVaultV1Reallocations(market.id, {
182
+ ...normalizedOptions,
175
183
  defaultMaxWithdrawalUtilization: blue_sdk_1.MathLib.WAD,
176
184
  maxWithdrawalUtilization: {},
177
185
  }).withdrawals);
@@ -207,7 +215,7 @@ const computeReallocations = ({ reallocationData: data, marketId, operation, amo
207
215
  available: totalReallocated,
208
216
  });
209
217
  }
210
- // Transform into VaultReallocation[] format.
218
+ // Transform into VaultV1Reallocation[] format.
211
219
  return reallocations
212
220
  .filter(({ withdrawals: vaultWithdrawals }) => vaultWithdrawals.length > 0)
213
221
  .map(({ vault, withdrawals: vaultWithdrawals }) => ({
@@ -229,4 +237,10 @@ const computeReallocations = ({ reallocationData: data, marketId, operation, amo
229
237
  })),
230
238
  }));
231
239
  };
232
- exports.computeReallocations = computeReallocations;
240
+ exports.computeVaultV1Reallocations = computeVaultV1Reallocations;
241
+ /**
242
+ * Deprecated name for the Vault V1 amount-aware reallocation planner.
243
+ *
244
+ * @deprecated Use {@link computeVaultV1Reallocations} instead.
245
+ */
246
+ exports.computeReallocations = exports.computeVaultV1Reallocations;
@@ -1,15 +1,17 @@
1
1
  import { type Address } from "viem";
2
2
  /** Maximum slippage tolerance: 10% */
3
3
  export declare const MAX_SLIPPAGE_TOLERANCE: bigint;
4
+ /** Default maximum Vault V2 reallocation penalty: zero. */
5
+ export declare const DEFAULT_MAX_REALLOCATION_PENALTY = 0n;
6
+ /** Maximum Vault V2 reallocation penalty: 100%. */
7
+ export declare const MAX_REALLOCATION_PENALTY = 1000000000000000000n;
4
8
  /** Default LLTV buffer: 0.5% below LLTV. Prevents instant liquidation on new positions. */
5
9
  export declare const DEFAULT_LLTV_BUFFER: bigint;
6
10
  /** Maximum absolute share price cap (100 RAY). Prevents absurd maxSharePrice values in repay. */
7
11
  export declare const MAX_ABSOLUTE_SHARE_PRICE: bigint;
8
12
  /**
9
13
  * The default maximum utilization a source market may reach when withdrawing
10
- * shared liquidity, scaled by WAD. Still overridable through the deprecated
11
- * `maxWithdrawalUtilization` / `defaultMaxWithdrawalUtilization` options until
12
- * the next major.
14
+ * shared liquidity, scaled by WAD.
13
15
  */
14
16
  export declare const DEFAULT_WITHDRAWAL_TARGET_UTILIZATION = 900000000000000000n;
15
17
  /**
@@ -1,19 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MAX_TOKEN_APPROVALS = exports.APPROVE_ONLY_ONCE_TOKENS = exports.DEFAULT_SUPPLY_TARGET_UTILIZATION = exports.DEFAULT_WITHDRAWAL_TARGET_UTILIZATION = exports.MAX_ABSOLUTE_SHARE_PRICE = exports.DEFAULT_LLTV_BUFFER = exports.MAX_SLIPPAGE_TOLERANCE = void 0;
3
+ exports.MAX_TOKEN_APPROVALS = exports.APPROVE_ONLY_ONCE_TOKENS = exports.DEFAULT_SUPPLY_TARGET_UTILIZATION = exports.DEFAULT_WITHDRAWAL_TARGET_UTILIZATION = exports.MAX_ABSOLUTE_SHARE_PRICE = exports.DEFAULT_LLTV_BUFFER = exports.MAX_REALLOCATION_PENALTY = exports.DEFAULT_MAX_REALLOCATION_PENALTY = exports.MAX_SLIPPAGE_TOLERANCE = void 0;
4
4
  const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
5
  const viem_1 = require("viem");
6
6
  /** Maximum slippage tolerance: 10% */
7
7
  exports.MAX_SLIPPAGE_TOLERANCE = blue_sdk_1.MathLib.WAD / 10n;
8
+ /** Default maximum Vault V2 reallocation penalty: zero. */
9
+ exports.DEFAULT_MAX_REALLOCATION_PENALTY = 0n;
10
+ /** Maximum Vault V2 reallocation penalty: 100%. */
11
+ exports.MAX_REALLOCATION_PENALTY = blue_sdk_1.MathLib.WAD;
8
12
  /** Default LLTV buffer: 0.5% below LLTV. Prevents instant liquidation on new positions. */
9
13
  exports.DEFAULT_LLTV_BUFFER = blue_sdk_1.MathLib.WAD / 200n;
10
14
  /** Maximum absolute share price cap (100 RAY). Prevents absurd maxSharePrice values in repay. */
11
15
  exports.MAX_ABSOLUTE_SHARE_PRICE = 100n * blue_sdk_1.MathLib.RAY;
12
16
  /**
13
17
  * The default maximum utilization a source market may reach when withdrawing
14
- * shared liquidity, scaled by WAD. Still overridable through the deprecated
15
- * `maxWithdrawalUtilization` / `defaultMaxWithdrawalUtilization` options until
16
- * the next major.
18
+ * shared liquidity, scaled by WAD.
17
19
  */
18
20
  exports.DEFAULT_WITHDRAWAL_TARGET_UTILIZATION = 900000000000000000n;
19
21
  /**
@@ -1,5 +1,5 @@
1
- export { computeReallocations } from "./computeReallocations.js";
2
- export { APPROVE_ONLY_ONCE_TOKENS, DEFAULT_LLTV_BUFFER, DEFAULT_SUPPLY_TARGET_UTILIZATION, DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, MAX_ABSOLUTE_SHARE_PRICE, MAX_SLIPPAGE_TOLERANCE, MAX_TOKEN_APPROVALS, } from "./constant.js";
1
+ export { computeReallocations, computeVaultV1Reallocations, } from "./computeVaultV1Reallocations.js";
2
+ export { APPROVE_ONLY_ONCE_TOKENS, DEFAULT_LLTV_BUFFER, DEFAULT_MAX_REALLOCATION_PENALTY, DEFAULT_SUPPLY_TARGET_UTILIZATION, DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, MAX_ABSOLUTE_SHARE_PRICE, MAX_REALLOCATION_PENALTY, MAX_SLIPPAGE_TOLERANCE, MAX_TOKEN_APPROVALS, } from "./constant.js";
3
3
  export { addTransactionMetadata } from "./metadata.js";
4
4
  export { type PreviewVaultV2InKindRedeemParams, previewVaultV2InKindRedeem, type VaultV2InKindRedeemMarketPreview, } from "./previewVaultV2InKindRedeem.js";
5
5
  export { signAndVerifyTypedData } from "./signAndVerifyTypedData.js";
@@ -1,14 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.validateRequirementSpender = exports.validateOfferSides = exports.validateWithdrawShares = exports.validateWithdrawAmount = exports.validateUserAddress = exports.validateSlippageTolerance = exports.validateRepayShares = exports.validateRepayAmount = exports.validateReallocations = exports.validatePositionHealthAfterWithdraw = exports.validatePositionHealth = exports.validateNativeAsset = exports.validateMidnightMarketChainId = exports.validateChainId = exports.validateAccrualPosition = exports.computeMinWithdrawSharePrice = exports.computeMinBorrowSharePrice = exports.computeMaxSupplySharePrice = exports.computeMaxRepaySharePrice = exports.signAndVerifyTypedData = exports.previewVaultV2InKindRedeem = exports.addTransactionMetadata = exports.MAX_TOKEN_APPROVALS = exports.MAX_SLIPPAGE_TOLERANCE = exports.MAX_ABSOLUTE_SHARE_PRICE = exports.DEFAULT_WITHDRAWAL_TARGET_UTILIZATION = exports.DEFAULT_SUPPLY_TARGET_UTILIZATION = exports.DEFAULT_LLTV_BUFFER = exports.APPROVE_ONLY_ONCE_TOKENS = exports.computeReallocations = void 0;
4
- var computeReallocations_js_1 = require("./computeReallocations.js");
5
- Object.defineProperty(exports, "computeReallocations", { enumerable: true, get: function () { return computeReallocations_js_1.computeReallocations; } });
3
+ exports.validateRequirementSpender = exports.validateOfferSides = exports.validateWithdrawShares = exports.validateWithdrawAmount = exports.validateUserAddress = exports.validateSlippageTolerance = exports.validateRepayShares = exports.validateRepayAmount = exports.validateReallocations = exports.validatePositionHealthAfterWithdraw = exports.validatePositionHealth = exports.validateNativeAsset = exports.validateMidnightMarketChainId = exports.validateChainId = exports.validateAccrualPosition = exports.computeMinWithdrawSharePrice = exports.computeMinBorrowSharePrice = exports.computeMaxSupplySharePrice = exports.computeMaxRepaySharePrice = exports.signAndVerifyTypedData = exports.previewVaultV2InKindRedeem = exports.addTransactionMetadata = exports.MAX_TOKEN_APPROVALS = exports.MAX_SLIPPAGE_TOLERANCE = exports.MAX_REALLOCATION_PENALTY = exports.MAX_ABSOLUTE_SHARE_PRICE = exports.DEFAULT_WITHDRAWAL_TARGET_UTILIZATION = exports.DEFAULT_SUPPLY_TARGET_UTILIZATION = exports.DEFAULT_MAX_REALLOCATION_PENALTY = exports.DEFAULT_LLTV_BUFFER = exports.APPROVE_ONLY_ONCE_TOKENS = exports.computeVaultV1Reallocations = exports.computeReallocations = void 0;
4
+ var computeVaultV1Reallocations_js_1 = require("./computeVaultV1Reallocations.js");
5
+ Object.defineProperty(exports, "computeReallocations", { enumerable: true, get: function () { return computeVaultV1Reallocations_js_1.computeReallocations; } });
6
+ Object.defineProperty(exports, "computeVaultV1Reallocations", { enumerable: true, get: function () { return computeVaultV1Reallocations_js_1.computeVaultV1Reallocations; } });
6
7
  var constant_js_1 = require("./constant.js");
7
8
  Object.defineProperty(exports, "APPROVE_ONLY_ONCE_TOKENS", { enumerable: true, get: function () { return constant_js_1.APPROVE_ONLY_ONCE_TOKENS; } });
8
9
  Object.defineProperty(exports, "DEFAULT_LLTV_BUFFER", { enumerable: true, get: function () { return constant_js_1.DEFAULT_LLTV_BUFFER; } });
10
+ Object.defineProperty(exports, "DEFAULT_MAX_REALLOCATION_PENALTY", { enumerable: true, get: function () { return constant_js_1.DEFAULT_MAX_REALLOCATION_PENALTY; } });
9
11
  Object.defineProperty(exports, "DEFAULT_SUPPLY_TARGET_UTILIZATION", { enumerable: true, get: function () { return constant_js_1.DEFAULT_SUPPLY_TARGET_UTILIZATION; } });
10
12
  Object.defineProperty(exports, "DEFAULT_WITHDRAWAL_TARGET_UTILIZATION", { enumerable: true, get: function () { return constant_js_1.DEFAULT_WITHDRAWAL_TARGET_UTILIZATION; } });
11
13
  Object.defineProperty(exports, "MAX_ABSOLUTE_SHARE_PRICE", { enumerable: true, get: function () { return constant_js_1.MAX_ABSOLUTE_SHARE_PRICE; } });
14
+ Object.defineProperty(exports, "MAX_REALLOCATION_PENALTY", { enumerable: true, get: function () { return constant_js_1.MAX_REALLOCATION_PENALTY; } });
12
15
  Object.defineProperty(exports, "MAX_SLIPPAGE_TOLERANCE", { enumerable: true, get: function () { return constant_js_1.MAX_SLIPPAGE_TOLERANCE; } });
13
16
  Object.defineProperty(exports, "MAX_TOKEN_APPROVALS", { enumerable: true, get: function () { return constant_js_1.MAX_TOKEN_APPROVALS; } });
14
17
  var metadata_js_1 = require("./metadata.js");
@@ -1,7 +1,7 @@
1
1
  import { type AccrualPosition, type MarketId } from "@morpho-org/blue-sdk";
2
2
  import type { MarketInput as MidnightMarketInput } from "@morpho-org/midnight-sdk";
3
3
  import { type Address } from "viem";
4
- import { type VaultReallocation } from "../types/index.js";
4
+ import { type BlueReallocationPlan, type VaultV1Reallocation, type VaultV2BlueReallocation } from "../types/index.js";
5
5
  /** @internal */
6
6
  export declare const compareMarketIds: (idA: MarketId, idB: MarketId) => -1 | 0 | 1;
7
7
  /**
@@ -138,18 +138,10 @@ export declare const validateRepayShares: (params: {
138
138
  marketId: MarketId;
139
139
  }) => void;
140
140
  /**
141
- * Validates that vault reallocations are well-formed.
142
- *
143
- * Enforces the following invariants for each {@link VaultReallocation}:
144
- * - `fee` must be non-negative.
145
- * - `withdrawals` must be non-empty.
146
- * - Every withdrawal `amount` must be strictly positive.
147
- * - No withdrawal may target `targetMarketId` (the operation's target market — the market being
148
- * borrowed from for `borrow`, or being withdrawn from for `withdraw`).
149
- * - Withdrawal market IDs must be strictly ascending (required by `PublicAllocator.reallocateTo`).
150
- *
151
- * @param reallocations - The reallocations to validate.
152
- * @param targetMarketId - The ID of the operation's target market. No withdrawal may reference this market.
141
+ * Validates that Vault V1 PublicAllocator reallocations are well-formed.
142
+ *
143
+ * @param reallocations - Vault V1 reallocations to validate.
144
+ * @param targetMarketId - The operation's target market ID.
153
145
  * @returns Nothing when every reallocation is valid.
154
146
  * @throws {NegativeInputError} when a reallocation fee is negative.
155
147
  * @throws {EmptyReallocationWithdrawalsError} when a reallocation has no withdrawals.
@@ -165,7 +157,31 @@ export declare const validateRepayShares: (params: {
165
157
  * const result: void = validateReallocations([], zeroHash as MarketId);
166
158
  * ```
167
159
  */
168
- export declare const validateReallocations: (reallocations: readonly VaultReallocation[], targetMarketId: MarketId) => void;
160
+ export declare const validateReallocations: (reallocations: Iterable<VaultV1Reallocation>, targetMarketId: MarketId) => void;
161
+ /** @internal */
162
+ export declare const validateVaultV2BlueReallocations: (reallocations: Iterable<VaultV2BlueReallocation>, targetMarketId: MarketId) => void;
163
+ /**
164
+ * Validates and normalizes a homogeneous Blue reallocation plan.
165
+ *
166
+ * @param params - Validation parameters.
167
+ * @param params.reallocations - Optional Vault V1 or Vault V2 reallocation plan.
168
+ * @param params.targetMarketId - Morpho Blue market receiving the liquidity.
169
+ * @param params.chainId - Chain whose allocator deployment is required for a V2 plan.
170
+ * @returns The validated plan tagged with its allocator version.
171
+ * @throws {BundlerErrors.UnexpectedAction} when a V2 plan is unsupported on the chain.
172
+ * @internal
173
+ */
174
+ export declare const validateAndNormalizeReallocations: ({ reallocations, targetMarketId, chainId, }: {
175
+ readonly reallocations: BlueReallocationPlan | undefined;
176
+ readonly targetMarketId: MarketId;
177
+ readonly chainId: number;
178
+ }) => {
179
+ type: "vaultV2Blue";
180
+ reallocations: VaultV2BlueReallocation[];
181
+ } | {
182
+ type: "vaultV1";
183
+ reallocations: VaultV1Reallocation[];
184
+ };
169
185
  /**
170
186
  * Validates that a slippage tolerance is within an acceptable range.
171
187
  *
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.validateWithdrawShares = exports.validateWithdrawAmount = exports.validateSlippageTolerance = exports.validateReallocations = exports.validateRepayShares = exports.validateRepayAmount = exports.validatePositionHealthAfterWithdraw = exports.validateNativeAsset = exports.validateChainId = exports.validatePositionHealth = exports.validateAccrualPosition = exports.validateMidnightMarketChainId = exports.compareMarketIds = void 0;
3
+ exports.validateWithdrawShares = exports.validateWithdrawAmount = exports.validateSlippageTolerance = exports.validateAndNormalizeReallocations = exports.validateVaultV2BlueReallocations = exports.validateReallocations = exports.validateRepayShares = exports.validateRepayAmount = exports.validatePositionHealthAfterWithdraw = exports.validateNativeAsset = exports.validateChainId = exports.validatePositionHealth = exports.validateAccrualPosition = exports.validateMidnightMarketChainId = exports.compareMarketIds = void 0;
4
4
  exports.validateUserAddress = validateUserAddress;
5
5
  const blue_sdk_1 = require("@morpho-org/blue-sdk");
6
6
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
@@ -231,18 +231,10 @@ const validateRepayShares = (params) => {
231
231
  };
232
232
  exports.validateRepayShares = validateRepayShares;
233
233
  /**
234
- * Validates that vault reallocations are well-formed.
234
+ * Validates that Vault V1 PublicAllocator reallocations are well-formed.
235
235
  *
236
- * Enforces the following invariants for each {@link VaultReallocation}:
237
- * - `fee` must be non-negative.
238
- * - `withdrawals` must be non-empty.
239
- * - Every withdrawal `amount` must be strictly positive.
240
- * - No withdrawal may target `targetMarketId` (the operation's target market — the market being
241
- * borrowed from for `borrow`, or being withdrawn from for `withdraw`).
242
- * - Withdrawal market IDs must be strictly ascending (required by `PublicAllocator.reallocateTo`).
243
- *
244
- * @param reallocations - The reallocations to validate.
245
- * @param targetMarketId - The ID of the operation's target market. No withdrawal may reference this market.
236
+ * @param reallocations - Vault V1 reallocations to validate.
237
+ * @param targetMarketId - The operation's target market ID.
246
238
  * @returns Nothing when every reallocation is valid.
247
239
  * @throws {NegativeInputError} when a reallocation fee is negative.
248
240
  * @throws {EmptyReallocationWithdrawalsError} when a reallocation has no withdrawals.
@@ -259,30 +251,151 @@ exports.validateRepayShares = validateRepayShares;
259
251
  * ```
260
252
  */
261
253
  const validateReallocations = (reallocations, targetMarketId) => {
262
- for (const r of reallocations) {
263
- if (r.fee < 0n) {
264
- throw new index_js_1.NegativeInputError("reallocation.fee", r.fee);
254
+ for (const reallocation of reallocations) {
255
+ if (reallocation.fee < 0n) {
256
+ throw new index_js_1.NegativeInputError("reallocation.fee", reallocation.fee);
265
257
  }
266
- if (r.withdrawals.length === 0) {
267
- throw new index_js_1.EmptyReallocationWithdrawalsError(r.vault);
258
+ if (reallocation.withdrawals.length === 0) {
259
+ throw new index_js_1.EmptyReallocationWithdrawalsError(reallocation.vault);
268
260
  }
269
- let prevId;
270
- for (const w of r.withdrawals) {
271
- if (w.amount <= 0n) {
272
- throw new index_js_1.NonPositiveInputError(`reallocation.withdrawals[${w.marketParams.id}].amount`, w.amount);
261
+ let previousMarketId;
262
+ for (const withdrawal of reallocation.withdrawals) {
263
+ if (withdrawal.amount <= 0n) {
264
+ throw new index_js_1.NonPositiveInputError(`reallocation.withdrawals[${withdrawal.marketParams.id}].amount`, withdrawal.amount);
273
265
  }
274
- if (w.marketParams.id === targetMarketId) {
275
- throw new index_js_1.ReallocationWithdrawalOnTargetMarketError(r.vault, w.marketParams.id);
266
+ if (withdrawal.marketParams.id === targetMarketId) {
267
+ throw new index_js_1.ReallocationWithdrawalOnTargetMarketError(reallocation.vault, withdrawal.marketParams.id);
276
268
  }
277
- if (prevId !== undefined &&
278
- (0, exports.compareMarketIds)(w.marketParams.id, prevId) <= 0) {
279
- throw new index_js_1.UnsortedReallocationWithdrawalsError(r.vault, w.marketParams.id);
269
+ if (previousMarketId !== undefined &&
270
+ (0, exports.compareMarketIds)(withdrawal.marketParams.id, previousMarketId) <= 0) {
271
+ throw new index_js_1.UnsortedReallocationWithdrawalsError(reallocation.vault, withdrawal.marketParams.id);
280
272
  }
281
- prevId = w.marketParams.id;
273
+ previousMarketId = withdrawal.marketParams.id;
282
274
  }
283
275
  }
284
276
  };
285
277
  exports.validateReallocations = validateReallocations;
278
+ /** @internal */
279
+ const validateVaultV2BlueReallocations = (reallocations, targetMarketId) => {
280
+ const penaltyByVault = new Map();
281
+ for (const reallocation of reallocations) {
282
+ if (typeof reallocation.vault !== "string" ||
283
+ !(0, viem_1.isAddress)(reallocation.vault)) {
284
+ throw new index_js_1.InvalidReallocationAddressError("vault");
285
+ }
286
+ if (reallocation.to == null ||
287
+ typeof reallocation.to.adapter !== "string" ||
288
+ !(0, viem_1.isAddress)(reallocation.to.adapter)) {
289
+ throw new index_js_1.InvalidReallocationAddressError("to.adapter");
290
+ }
291
+ const source = reallocation.from;
292
+ if (source == null) {
293
+ throw new index_js_1.InvalidReallocationSourceTypeError(undefined);
294
+ }
295
+ const sourceType = source.type;
296
+ if (sourceType !== "market" && sourceType !== "idle") {
297
+ throw new index_js_1.InvalidReallocationSourceTypeError(sourceType);
298
+ }
299
+ let sourceMarketId;
300
+ if (source.type === "market") {
301
+ if (typeof source.adapter !== "string" || !(0, viem_1.isAddress)(source.adapter)) {
302
+ throw new index_js_1.InvalidReallocationAddressError("from.adapter");
303
+ }
304
+ if (source.marketParams == null ||
305
+ !(0, viem_1.isAddress)(source.marketParams.loanToken) ||
306
+ !(0, viem_1.isAddress)(source.marketParams.collateralToken) ||
307
+ !(0, viem_1.isAddress)(source.marketParams.oracle) ||
308
+ !(0, viem_1.isAddress)(source.marketParams.irm) ||
309
+ typeof source.marketParams.lltv !== "bigint") {
310
+ throw new index_js_1.InvalidReallocationSourceTypeError("market", "marketParams");
311
+ }
312
+ sourceMarketId = blue_sdk_1.MarketUtils.getMarketId(source.marketParams);
313
+ }
314
+ if (reallocation.penalty < 0n) {
315
+ throw new index_js_1.NegativeInputError("reallocation.penalty", reallocation.penalty);
316
+ }
317
+ if (reallocation.penalty > constant_js_1.MAX_REALLOCATION_PENALTY) {
318
+ throw new index_js_1.InputExceedsMaxError({
319
+ field: "reallocation.penalty",
320
+ value: reallocation.penalty,
321
+ max: constant_js_1.MAX_REALLOCATION_PENALTY,
322
+ });
323
+ }
324
+ if (reallocation.assets <= 0n) {
325
+ throw new index_js_1.NonPositiveInputError("reallocation.assets", reallocation.assets);
326
+ }
327
+ if (reallocation.assets > viem_1.maxUint128) {
328
+ throw new index_js_1.InputExceedsMaxError({
329
+ field: "reallocation.assets",
330
+ value: reallocation.assets,
331
+ max: viem_1.maxUint128,
332
+ });
333
+ }
334
+ const penaltyKey = reallocation.vault.toLowerCase();
335
+ const expectedPenalty = penaltyByVault.get(penaltyKey);
336
+ if (expectedPenalty !== undefined &&
337
+ expectedPenalty !== reallocation.penalty) {
338
+ throw new index_js_1.InconsistentReallocationPenaltyError({
339
+ vault: reallocation.vault,
340
+ expected: expectedPenalty,
341
+ actual: reallocation.penalty,
342
+ });
343
+ }
344
+ penaltyByVault.set(penaltyKey, reallocation.penalty);
345
+ if (sourceMarketId !== undefined &&
346
+ (0, exports.compareMarketIds)(sourceMarketId, targetMarketId) === 0) {
347
+ throw new index_js_1.ReallocationWithdrawalOnTargetMarketError(reallocation.vault, sourceMarketId);
348
+ }
349
+ }
350
+ };
351
+ exports.validateVaultV2BlueReallocations = validateVaultV2BlueReallocations;
352
+ /**
353
+ * Validates and normalizes a homogeneous Blue reallocation plan.
354
+ *
355
+ * @param params - Validation parameters.
356
+ * @param params.reallocations - Optional Vault V1 or Vault V2 reallocation plan.
357
+ * @param params.targetMarketId - Morpho Blue market receiving the liquidity.
358
+ * @param params.chainId - Chain whose allocator deployment is required for a V2 plan.
359
+ * @returns The validated plan tagged with its allocator version.
360
+ * @throws {BundlerErrors.UnexpectedAction} when a V2 plan is unsupported on the chain.
361
+ * @internal
362
+ */
363
+ const validateAndNormalizeReallocations = ({ reallocations, targetMarketId, chainId, }) => {
364
+ const vaultV1Reallocations = [];
365
+ const vaultV2Reallocations = [];
366
+ for (const reallocation of reallocations ?? []) {
367
+ if (typeof reallocation !== "object" || reallocation === null) {
368
+ throw new index_js_1.InvalidReallocationShapeError();
369
+ }
370
+ if ("from" in reallocation === "withdrawals" in reallocation) {
371
+ throw new index_js_1.InvalidReallocationShapeError();
372
+ }
373
+ if ("withdrawals" in reallocation) {
374
+ vaultV1Reallocations.push(reallocation);
375
+ }
376
+ else {
377
+ vaultV2Reallocations.push(reallocation);
378
+ }
379
+ }
380
+ if (vaultV1Reallocations.length > 0 && vaultV2Reallocations.length > 0) {
381
+ throw new index_js_1.MixedReallocationVersionsError();
382
+ }
383
+ if (vaultV2Reallocations.length > 0) {
384
+ (0, exports.validateVaultV2BlueReallocations)(vaultV2Reallocations, targetMarketId);
385
+ if ((0, blue_sdk_1.getChainAddresses)(chainId).vaultV2BluePublicAllocator == null) {
386
+ throw new index_js_1.BundlerErrors.UnexpectedAction(vaultV2Reallocations[0]?.from.type === "market"
387
+ ? "vaultV2BluePublicAllocatorReallocate"
388
+ : "vaultV2BluePublicAllocatorAllocateFromIdle", chainId);
389
+ }
390
+ return {
391
+ type: "vaultV2Blue",
392
+ reallocations: vaultV2Reallocations,
393
+ };
394
+ }
395
+ (0, exports.validateReallocations)(vaultV1Reallocations, targetMarketId);
396
+ return { type: "vaultV1", reallocations: vaultV1Reallocations };
397
+ };
398
+ exports.validateAndNormalizeReallocations = validateAndNormalizeReallocations;
286
399
  /**
287
400
  * Validates that a slippage tolerance is within an acceptable range.
288
401
  *
@@ -115,7 +115,10 @@ export interface BlueWithdrawAction extends BaseAction<"blueWithdraw", {
115
115
  shares: bigint;
116
116
  receiver: Address;
117
117
  minSharePrice: bigint;
118
+ /** Native-token fees paid to PublicAllocator V1. */
118
119
  reallocationFee: bigint;
120
+ /** Loan-token assets donated as BluePublicAllocator V2 penalties. */
121
+ readonly reallocationPenaltyAssets: bigint;
119
122
  }> {
120
123
  }
121
124
  export interface BlueSupplyCollateralAction extends BaseAction<"blueSupplyCollateral", {
@@ -130,7 +133,10 @@ export interface BlueBorrowAction extends BaseAction<"blueBorrow", {
130
133
  amount: bigint;
131
134
  receiver: Address;
132
135
  minSharePrice: bigint;
136
+ /** Native-token fees paid to PublicAllocator V1. */
133
137
  reallocationFee: bigint;
138
+ /** Loan-token assets donated as BluePublicAllocator V2 penalties. */
139
+ readonly reallocationPenaltyAssets: bigint;
134
140
  }> {
135
141
  }
136
142
  export interface BlueSupplyCollateralBorrowAction extends BaseAction<"blueSupplyCollateralBorrow", {
@@ -141,7 +147,10 @@ export interface BlueSupplyCollateralBorrowAction extends BaseAction<"blueSupply
141
147
  onBehalf: Address;
142
148
  receiver: Address;
143
149
  nativeAmount?: bigint;
150
+ /** Native-token fees paid to PublicAllocator V1. */
144
151
  reallocationFee: bigint;
152
+ /** Loan-token assets donated as BluePublicAllocator V2 penalties. */
153
+ readonly reallocationPenaltyAssets: bigint;
145
154
  }> {
146
155
  }
147
156
  export interface BlueRepayAction extends BaseAction<"blueRepay", {
@@ -185,7 +194,10 @@ export interface BlueRefinanceAction extends BaseAction<"blueRefinance", {
185
194
  readonly minBorrowSharePrice: bigint;
186
195
  readonly maxRepaySharePrice: bigint;
187
196
  readonly user: Address;
197
+ /** Native-token fees paid to PublicAllocator V1. */
188
198
  readonly reallocationFee: bigint;
199
+ /** Loan-token assets donated as BluePublicAllocator V2 penalties. */
200
+ readonly reallocationPenaltyAssets: bigint;
189
201
  }> {
190
202
  }
191
203
  /**