@morpho-org/morpho-sdk 4.0.0 → 4.1.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.
@@ -1,6 +1,6 @@
1
1
  import { AccrualPosition, Market, type MarketId, Position, Vault, VaultMarketConfig } from "@morpho-org/blue-sdk";
2
2
  import type { Address } from "viem";
3
- import type { PublicAllocatorOptions, PublicReallocation } from "../types/index.js";
3
+ import type { PublicAllocatorOptions, PublicReallocation, ReallocationComputeOptions } from "../types/index.js";
4
4
  /**
5
5
  * Input state required to construct {@link ReallocationData}.
6
6
  */
@@ -159,6 +159,100 @@ export declare class ReallocationData implements InputReallocationData {
159
159
  readonly withdrawals: readonly PublicReallocation[];
160
160
  data: ReallocationData;
161
161
  };
162
+ /**
163
+ * Sums the public-allocator liquidity reallocatable into `marketId` from
164
+ * sibling markets.
165
+ *
166
+ * Read-only metric — never throws on insufficiency (returns `0n`). Bounded by
167
+ * each source market's withdrawal utilization cap and the target market's
168
+ * vault supply-cap headroom. Pass `options.defaultMaxWithdrawalUtilization` to
169
+ * widen the source ceiling (e.g. `MathLib.WAD` for the full drain).
170
+ *
171
+ * @param marketId - Target market that would receive the liquidity.
172
+ * @param options - Optional allocator discovery options.
173
+ * @returns Total reallocatable assets in loan-token units; `0n` when none is available.
174
+ * @throws {@link UnknownReallocationMarketError} when the target market is absent.
175
+ * @example
176
+ * ```ts
177
+ * import { createPublicClient, http } from "viem";
178
+ * import { mainnet } from "viem/chains";
179
+ * import { markets, vaults } from "@morpho-org/morpho-test";
180
+ * import { morphoViemExtension } from "@morpho-org/morpho-sdk";
181
+ *
182
+ * const client = createPublicClient({
183
+ * chain: mainnet,
184
+ * transport: http(),
185
+ * }).extend(morphoViemExtension());
186
+ *
187
+ * const marketParams = markets[mainnet.id].usdc_wbtc;
188
+ * const market = client.morpho.blue(marketParams, mainnet.id);
189
+ * const block = await client.getBlock();
190
+ * const reallocationData = await market.getReallocationData({
191
+ * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
192
+ * block: { number: block.number, timestamp: block.timestamp },
193
+ * });
194
+ *
195
+ * const liquidity: bigint = reallocationData.getPublicReallocationLiquidity(
196
+ * marketParams.id,
197
+ * { timestamp: block.timestamp },
198
+ * );
199
+ * ```
200
+ */
201
+ getPublicReallocationLiquidity(marketId: MarketId, options?: PublicAllocatorOptions): bigint;
202
+ /**
203
+ * Computes the liquidity available to bring `marketId` to `targetUtilization`,
204
+ * counting the public-allocator liquidity reallocatable into it.
205
+ *
206
+ * Returns the max borrow `x` keeping post-borrow utilization
207
+ * `(borrow + x) / (supply + L) ≤ targetUtilization`, where `L` is the
208
+ * reallocatable liquidity added to the market's supply — equivalently
209
+ * `getBorrowToUtilization({ supply + L, borrow }, targetUtilization)`. Below
210
+ * the target this is the market's own borrow headroom plus `targetUtilization · L`;
211
+ * reallocated supply also raises the supply denominator, so only that scaled
212
+ * share backs further borrow.
213
+ *
214
+ * Read-only metric — never throws on insufficiency:
215
+ * - returns only the market's own borrow headroom when
216
+ * `supplyTargetUtilization > targetUtilization` (reallocation would not
217
+ * trigger at that target);
218
+ * - returns `0n` when the market is already at or above the target and `L` is
219
+ * too small to bring it back under.
220
+ *
221
+ * @param marketId - Target market to borrow from.
222
+ * @param targetUtilization - Utilization to bring the market to, scaled by WAD. Defaults to {@link DEFAULT_SUPPLY_TARGET_UTILIZATION}.
223
+ * @param options - Optional reallocation options (supply target utilization trigger, timestamp, withdrawal caps).
224
+ * @returns Available liquidity to the target utilization in loan-token units; `0n` when none is available.
225
+ * @throws {@link UnknownReallocationMarketError} when the target market is absent.
226
+ * @example
227
+ * ```ts
228
+ * import { createPublicClient, http, parseEther } from "viem";
229
+ * import { mainnet } from "viem/chains";
230
+ * import { markets, vaults } from "@morpho-org/morpho-test";
231
+ * import { morphoViemExtension } from "@morpho-org/morpho-sdk";
232
+ *
233
+ * const client = createPublicClient({
234
+ * chain: mainnet,
235
+ * transport: http(),
236
+ * }).extend(morphoViemExtension());
237
+ *
238
+ * const marketParams = markets[mainnet.id].usdc_wbtc;
239
+ * const market = client.morpho.blue(marketParams, mainnet.id);
240
+ * const block = await client.getBlock();
241
+ * const reallocationData = await market.getReallocationData({
242
+ * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
243
+ * block: { number: block.number, timestamp: block.timestamp },
244
+ * });
245
+ *
246
+ * // Max borrow keeping utilization at or below 90%, counting shared liquidity.
247
+ * const available: bigint =
248
+ * reallocationData.getAvailableLiquidityToTargetUtilization(
249
+ * marketParams.id,
250
+ * parseEther("0.9"),
251
+ * { timestamp: block.timestamp },
252
+ * );
253
+ * ```
254
+ */
255
+ getAvailableLiquidityToTargetUtilization(marketId: MarketId, targetUtilization?: bigint, options?: ReallocationComputeOptions): bigint;
162
256
  /**
163
257
  * Gets the largest currently valid source-market withdrawal for a vault.
164
258
  *
@@ -4,6 +4,7 @@ exports.ReallocationData = void 0;
4
4
  const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
5
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
6
  const constant_js_1 = require("../helpers/constant.js");
7
+ const utilization_js_1 = require("../helpers/utilization.js");
7
8
  const index_js_1 = require("../types/index.js");
8
9
  /**
9
10
  * Clones a market so simulated interest and liquidity changes never mutate caller input.
@@ -303,6 +304,116 @@ class ReallocationData {
303
304
  });
304
305
  }
305
306
  }
307
+ /**
308
+ * Sums the public-allocator liquidity reallocatable into `marketId` from
309
+ * sibling markets.
310
+ *
311
+ * Read-only metric — never throws on insufficiency (returns `0n`). Bounded by
312
+ * each source market's withdrawal utilization cap and the target market's
313
+ * vault supply-cap headroom. Pass `options.defaultMaxWithdrawalUtilization` to
314
+ * widen the source ceiling (e.g. `MathLib.WAD` for the full drain).
315
+ *
316
+ * @param marketId - Target market that would receive the liquidity.
317
+ * @param options - Optional allocator discovery options.
318
+ * @returns Total reallocatable assets in loan-token units; `0n` when none is available.
319
+ * @throws {@link UnknownReallocationMarketError} when the target market is absent.
320
+ * @example
321
+ * ```ts
322
+ * import { createPublicClient, http } from "viem";
323
+ * import { mainnet } from "viem/chains";
324
+ * import { markets, vaults } from "@morpho-org/morpho-test";
325
+ * import { morphoViemExtension } from "@morpho-org/morpho-sdk";
326
+ *
327
+ * const client = createPublicClient({
328
+ * chain: mainnet,
329
+ * transport: http(),
330
+ * }).extend(morphoViemExtension());
331
+ *
332
+ * const marketParams = markets[mainnet.id].usdc_wbtc;
333
+ * const market = client.morpho.blue(marketParams, mainnet.id);
334
+ * const block = await client.getBlock();
335
+ * const reallocationData = await market.getReallocationData({
336
+ * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
337
+ * block: { number: block.number, timestamp: block.timestamp },
338
+ * });
339
+ *
340
+ * const liquidity: bigint = reallocationData.getPublicReallocationLiquidity(
341
+ * marketParams.id,
342
+ * { timestamp: block.timestamp },
343
+ * );
344
+ * ```
345
+ */
346
+ getPublicReallocationLiquidity(marketId, options) {
347
+ const { withdrawals } = this.getMarketPublicReallocations(marketId, options);
348
+ return withdrawals.reduce((total, { assets }) => total + assets, 0n);
349
+ }
350
+ /**
351
+ * Computes the liquidity available to bring `marketId` to `targetUtilization`,
352
+ * counting the public-allocator liquidity reallocatable into it.
353
+ *
354
+ * Returns the max borrow `x` keeping post-borrow utilization
355
+ * `(borrow + x) / (supply + L) ≤ targetUtilization`, where `L` is the
356
+ * reallocatable liquidity added to the market's supply — equivalently
357
+ * `getBorrowToUtilization({ supply + L, borrow }, targetUtilization)`. Below
358
+ * the target this is the market's own borrow headroom plus `targetUtilization · L`;
359
+ * reallocated supply also raises the supply denominator, so only that scaled
360
+ * share backs further borrow.
361
+ *
362
+ * Read-only metric — never throws on insufficiency:
363
+ * - returns only the market's own borrow headroom when
364
+ * `supplyTargetUtilization > targetUtilization` (reallocation would not
365
+ * trigger at that target);
366
+ * - returns `0n` when the market is already at or above the target and `L` is
367
+ * too small to bring it back under.
368
+ *
369
+ * @param marketId - Target market to borrow from.
370
+ * @param targetUtilization - Utilization to bring the market to, scaled by WAD. Defaults to {@link DEFAULT_SUPPLY_TARGET_UTILIZATION}.
371
+ * @param options - Optional reallocation options (supply target utilization trigger, timestamp, withdrawal caps).
372
+ * @returns Available liquidity to the target utilization in loan-token units; `0n` when none is available.
373
+ * @throws {@link UnknownReallocationMarketError} when the target market is absent.
374
+ * @example
375
+ * ```ts
376
+ * import { createPublicClient, http, parseEther } from "viem";
377
+ * import { mainnet } from "viem/chains";
378
+ * import { markets, vaults } from "@morpho-org/morpho-test";
379
+ * import { morphoViemExtension } from "@morpho-org/morpho-sdk";
380
+ *
381
+ * const client = createPublicClient({
382
+ * chain: mainnet,
383
+ * transport: http(),
384
+ * }).extend(morphoViemExtension());
385
+ *
386
+ * const marketParams = markets[mainnet.id].usdc_wbtc;
387
+ * const market = client.morpho.blue(marketParams, mainnet.id);
388
+ * const block = await client.getBlock();
389
+ * const reallocationData = await market.getReallocationData({
390
+ * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
391
+ * block: { number: block.number, timestamp: block.timestamp },
392
+ * });
393
+ *
394
+ * // Max borrow keeping utilization at or below 90%, counting shared liquidity.
395
+ * const available: bigint =
396
+ * reallocationData.getAvailableLiquidityToTargetUtilization(
397
+ * marketParams.id,
398
+ * parseEther("0.9"),
399
+ * { timestamp: block.timestamp },
400
+ * );
401
+ * ```
402
+ */
403
+ // biome-ignore lint/complexity/useMaxParams: (marketId, targetUtilization, options) is the metric's public API
404
+ getAvailableLiquidityToTargetUtilization(marketId, targetUtilization = constant_js_1.DEFAULT_SUPPLY_TARGET_UTILIZATION, options) {
405
+ const market = this.getMarket(marketId).accrueInterest(options?.timestamp);
406
+ // Below the allocator's trigger, no reallocation happens: own headroom only.
407
+ const supplyTargetUtilization = (0, utilization_js_1.getSupplyTargetUtilization)(marketId, options);
408
+ if (supplyTargetUtilization > targetUtilization)
409
+ return market.getBorrowToUtilization(targetUtilization);
410
+ // Borrow-to-target on the post-reallocation supply; clamps to 0n above target.
411
+ const availableLiquidity = this.getPublicReallocationLiquidity(marketId, options);
412
+ return blue_sdk_1.MarketUtils.getBorrowToUtilization({
413
+ totalSupplyAssets: market.totalSupplyAssets + availableLiquidity,
414
+ totalBorrowAssets: market.totalBorrowAssets,
415
+ }, targetUtilization);
416
+ }
306
417
  /**
307
418
  * Gets the largest currently valid source-market withdrawal for a vault.
308
419
  *
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.computeReallocations = void 0;
4
4
  const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
5
  const index_js_1 = require("../types/index.js");
6
- const constant_js_1 = require("./constant.js");
6
+ const utilization_js_1 = require("./utilization.js");
7
7
  const validate_js_1 = require("./validate.js");
8
8
  const groupWithdrawalsByVault = (withdrawals) => {
9
9
  const groups = [];
@@ -145,9 +145,7 @@ const computeReallocations = ({ reallocationData: data, marketId, operation, amo
145
145
  const newTotalSupplyAssets = operation === "withdraw"
146
146
  ? market.totalSupplyAssets - amount
147
147
  : market.totalSupplyAssets;
148
- const supplyTargetUtilization = options?.supplyTargetUtilization?.[market.params.id] ??
149
- options?.defaultSupplyTargetUtilization ??
150
- constant_js_1.DEFAULT_SUPPLY_TARGET_UTILIZATION;
148
+ const supplyTargetUtilization = (0, utilization_js_1.getSupplyTargetUtilization)(market.params.id, options);
151
149
  if (blue_sdk_1.MarketUtils.getUtilization({
152
150
  totalSupplyAssets: newTotalSupplyAssets,
153
151
  totalBorrowAssets: newTotalBorrowAssets,
@@ -0,0 +1,12 @@
1
+ import type { MarketId } from "@morpho-org/blue-sdk";
2
+ import type { ReallocationComputeOptions } from "../types/index.js";
3
+ /**
4
+ * Resolves the effective supply target utilization for a market: the per-market
5
+ * override, then the global default override, then
6
+ * {@link DEFAULT_SUPPLY_TARGET_UTILIZATION}.
7
+ *
8
+ * @param marketId - The market whose supply target utilization to resolve.
9
+ * @param options - Reallocation options carrying the per-market and default overrides.
10
+ * @returns The supply target utilization, scaled by WAD.
11
+ */
12
+ export declare const getSupplyTargetUtilization: (marketId: MarketId, options?: ReallocationComputeOptions) => bigint;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getSupplyTargetUtilization = void 0;
4
+ const constant_js_1 = require("./constant.js");
5
+ /**
6
+ * Resolves the effective supply target utilization for a market: the per-market
7
+ * override, then the global default override, then
8
+ * {@link DEFAULT_SUPPLY_TARGET_UTILIZATION}.
9
+ *
10
+ * @param marketId - The market whose supply target utilization to resolve.
11
+ * @param options - Reallocation options carrying the per-market and default overrides.
12
+ * @returns The supply target utilization, scaled by WAD.
13
+ */
14
+ const getSupplyTargetUtilization = (marketId, options) => options?.supplyTargetUtilization?.[marketId] ??
15
+ options?.defaultSupplyTargetUtilization ??
16
+ constant_js_1.DEFAULT_SUPPLY_TARGET_UTILIZATION;
17
+ exports.getSupplyTargetUtilization = getSupplyTargetUtilization;
@@ -1,6 +1,6 @@
1
1
  import { AccrualPosition, Market, type MarketId, Position, Vault, VaultMarketConfig } from "@morpho-org/blue-sdk";
2
2
  import type { Address } from "viem";
3
- import type { PublicAllocatorOptions, PublicReallocation } from "../types/index.js";
3
+ import type { PublicAllocatorOptions, PublicReallocation, ReallocationComputeOptions } from "../types/index.js";
4
4
  /**
5
5
  * Input state required to construct {@link ReallocationData}.
6
6
  */
@@ -159,6 +159,100 @@ export declare class ReallocationData implements InputReallocationData {
159
159
  readonly withdrawals: readonly PublicReallocation[];
160
160
  data: ReallocationData;
161
161
  };
162
+ /**
163
+ * Sums the public-allocator liquidity reallocatable into `marketId` from
164
+ * sibling markets.
165
+ *
166
+ * Read-only metric — never throws on insufficiency (returns `0n`). Bounded by
167
+ * each source market's withdrawal utilization cap and the target market's
168
+ * vault supply-cap headroom. Pass `options.defaultMaxWithdrawalUtilization` to
169
+ * widen the source ceiling (e.g. `MathLib.WAD` for the full drain).
170
+ *
171
+ * @param marketId - Target market that would receive the liquidity.
172
+ * @param options - Optional allocator discovery options.
173
+ * @returns Total reallocatable assets in loan-token units; `0n` when none is available.
174
+ * @throws {@link UnknownReallocationMarketError} when the target market is absent.
175
+ * @example
176
+ * ```ts
177
+ * import { createPublicClient, http } from "viem";
178
+ * import { mainnet } from "viem/chains";
179
+ * import { markets, vaults } from "@morpho-org/morpho-test";
180
+ * import { morphoViemExtension } from "@morpho-org/morpho-sdk";
181
+ *
182
+ * const client = createPublicClient({
183
+ * chain: mainnet,
184
+ * transport: http(),
185
+ * }).extend(morphoViemExtension());
186
+ *
187
+ * const marketParams = markets[mainnet.id].usdc_wbtc;
188
+ * const market = client.morpho.blue(marketParams, mainnet.id);
189
+ * const block = await client.getBlock();
190
+ * const reallocationData = await market.getReallocationData({
191
+ * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
192
+ * block: { number: block.number, timestamp: block.timestamp },
193
+ * });
194
+ *
195
+ * const liquidity: bigint = reallocationData.getPublicReallocationLiquidity(
196
+ * marketParams.id,
197
+ * { timestamp: block.timestamp },
198
+ * );
199
+ * ```
200
+ */
201
+ getPublicReallocationLiquidity(marketId: MarketId, options?: PublicAllocatorOptions): bigint;
202
+ /**
203
+ * Computes the liquidity available to bring `marketId` to `targetUtilization`,
204
+ * counting the public-allocator liquidity reallocatable into it.
205
+ *
206
+ * Returns the max borrow `x` keeping post-borrow utilization
207
+ * `(borrow + x) / (supply + L) ≤ targetUtilization`, where `L` is the
208
+ * reallocatable liquidity added to the market's supply — equivalently
209
+ * `getBorrowToUtilization({ supply + L, borrow }, targetUtilization)`. Below
210
+ * the target this is the market's own borrow headroom plus `targetUtilization · L`;
211
+ * reallocated supply also raises the supply denominator, so only that scaled
212
+ * share backs further borrow.
213
+ *
214
+ * Read-only metric — never throws on insufficiency:
215
+ * - returns only the market's own borrow headroom when
216
+ * `supplyTargetUtilization > targetUtilization` (reallocation would not
217
+ * trigger at that target);
218
+ * - returns `0n` when the market is already at or above the target and `L` is
219
+ * too small to bring it back under.
220
+ *
221
+ * @param marketId - Target market to borrow from.
222
+ * @param targetUtilization - Utilization to bring the market to, scaled by WAD. Defaults to {@link DEFAULT_SUPPLY_TARGET_UTILIZATION}.
223
+ * @param options - Optional reallocation options (supply target utilization trigger, timestamp, withdrawal caps).
224
+ * @returns Available liquidity to the target utilization in loan-token units; `0n` when none is available.
225
+ * @throws {@link UnknownReallocationMarketError} when the target market is absent.
226
+ * @example
227
+ * ```ts
228
+ * import { createPublicClient, http, parseEther } from "viem";
229
+ * import { mainnet } from "viem/chains";
230
+ * import { markets, vaults } from "@morpho-org/morpho-test";
231
+ * import { morphoViemExtension } from "@morpho-org/morpho-sdk";
232
+ *
233
+ * const client = createPublicClient({
234
+ * chain: mainnet,
235
+ * transport: http(),
236
+ * }).extend(morphoViemExtension());
237
+ *
238
+ * const marketParams = markets[mainnet.id].usdc_wbtc;
239
+ * const market = client.morpho.blue(marketParams, mainnet.id);
240
+ * const block = await client.getBlock();
241
+ * const reallocationData = await market.getReallocationData({
242
+ * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
243
+ * block: { number: block.number, timestamp: block.timestamp },
244
+ * });
245
+ *
246
+ * // Max borrow keeping utilization at or below 90%, counting shared liquidity.
247
+ * const available: bigint =
248
+ * reallocationData.getAvailableLiquidityToTargetUtilization(
249
+ * marketParams.id,
250
+ * parseEther("0.9"),
251
+ * { timestamp: block.timestamp },
252
+ * );
253
+ * ```
254
+ */
255
+ getAvailableLiquidityToTargetUtilization(marketId: MarketId, targetUtilization?: bigint, options?: ReallocationComputeOptions): bigint;
162
256
  /**
163
257
  * Gets the largest currently valid source-market withdrawal for a vault.
164
258
  *
@@ -1,6 +1,7 @@
1
- import { _try, AccrualPosition, Market, MathLib, Position, UnknownDataError, Vault, VaultMarketConfig, VaultMarketPublicAllocatorConfig, } from "@morpho-org/blue-sdk";
1
+ import { _try, AccrualPosition, Market, MarketUtils, MathLib, Position, UnknownDataError, Vault, VaultMarketConfig, VaultMarketPublicAllocatorConfig, } from "@morpho-org/blue-sdk";
2
2
  import { bigIntComparator } from "@morpho-org/morpho-ts";
3
- import { DEFAULT_WITHDRAWAL_TARGET_UTILIZATION } from "../helpers/constant.js";
3
+ import { DEFAULT_SUPPLY_TARGET_UTILIZATION, DEFAULT_WITHDRAWAL_TARGET_UTILIZATION, } from "../helpers/constant.js";
4
+ import { getSupplyTargetUtilization } from "../helpers/utilization.js";
4
5
  import { DisabledReallocationMarketError, MissingPublicAllocatorConfigError, UnknownReallocationMarketError, UnknownReallocationPositionError, UnknownReallocationVaultError, UnknownReallocationVaultMarketConfigError, } from "../types/index.js";
5
6
  /**
6
7
  * Clones a market so simulated interest and liquidity changes never mutate caller input.
@@ -300,6 +301,116 @@ export class ReallocationData {
300
301
  });
301
302
  }
302
303
  }
304
+ /**
305
+ * Sums the public-allocator liquidity reallocatable into `marketId` from
306
+ * sibling markets.
307
+ *
308
+ * Read-only metric — never throws on insufficiency (returns `0n`). Bounded by
309
+ * each source market's withdrawal utilization cap and the target market's
310
+ * vault supply-cap headroom. Pass `options.defaultMaxWithdrawalUtilization` to
311
+ * widen the source ceiling (e.g. `MathLib.WAD` for the full drain).
312
+ *
313
+ * @param marketId - Target market that would receive the liquidity.
314
+ * @param options - Optional allocator discovery options.
315
+ * @returns Total reallocatable assets in loan-token units; `0n` when none is available.
316
+ * @throws {@link UnknownReallocationMarketError} when the target market is absent.
317
+ * @example
318
+ * ```ts
319
+ * import { createPublicClient, http } from "viem";
320
+ * import { mainnet } from "viem/chains";
321
+ * import { markets, vaults } from "@morpho-org/morpho-test";
322
+ * import { morphoViemExtension } from "@morpho-org/morpho-sdk";
323
+ *
324
+ * const client = createPublicClient({
325
+ * chain: mainnet,
326
+ * transport: http(),
327
+ * }).extend(morphoViemExtension());
328
+ *
329
+ * const marketParams = markets[mainnet.id].usdc_wbtc;
330
+ * const market = client.morpho.blue(marketParams, mainnet.id);
331
+ * const block = await client.getBlock();
332
+ * const reallocationData = await market.getReallocationData({
333
+ * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
334
+ * block: { number: block.number, timestamp: block.timestamp },
335
+ * });
336
+ *
337
+ * const liquidity: bigint = reallocationData.getPublicReallocationLiquidity(
338
+ * marketParams.id,
339
+ * { timestamp: block.timestamp },
340
+ * );
341
+ * ```
342
+ */
343
+ getPublicReallocationLiquidity(marketId, options) {
344
+ const { withdrawals } = this.getMarketPublicReallocations(marketId, options);
345
+ return withdrawals.reduce((total, { assets }) => total + assets, 0n);
346
+ }
347
+ /**
348
+ * Computes the liquidity available to bring `marketId` to `targetUtilization`,
349
+ * counting the public-allocator liquidity reallocatable into it.
350
+ *
351
+ * Returns the max borrow `x` keeping post-borrow utilization
352
+ * `(borrow + x) / (supply + L) ≤ targetUtilization`, where `L` is the
353
+ * reallocatable liquidity added to the market's supply — equivalently
354
+ * `getBorrowToUtilization({ supply + L, borrow }, targetUtilization)`. Below
355
+ * the target this is the market's own borrow headroom plus `targetUtilization · L`;
356
+ * reallocated supply also raises the supply denominator, so only that scaled
357
+ * share backs further borrow.
358
+ *
359
+ * Read-only metric — never throws on insufficiency:
360
+ * - returns only the market's own borrow headroom when
361
+ * `supplyTargetUtilization > targetUtilization` (reallocation would not
362
+ * trigger at that target);
363
+ * - returns `0n` when the market is already at or above the target and `L` is
364
+ * too small to bring it back under.
365
+ *
366
+ * @param marketId - Target market to borrow from.
367
+ * @param targetUtilization - Utilization to bring the market to, scaled by WAD. Defaults to {@link DEFAULT_SUPPLY_TARGET_UTILIZATION}.
368
+ * @param options - Optional reallocation options (supply target utilization trigger, timestamp, withdrawal caps).
369
+ * @returns Available liquidity to the target utilization in loan-token units; `0n` when none is available.
370
+ * @throws {@link UnknownReallocationMarketError} when the target market is absent.
371
+ * @example
372
+ * ```ts
373
+ * import { createPublicClient, http, parseEther } from "viem";
374
+ * import { mainnet } from "viem/chains";
375
+ * import { markets, vaults } from "@morpho-org/morpho-test";
376
+ * import { morphoViemExtension } from "@morpho-org/morpho-sdk";
377
+ *
378
+ * const client = createPublicClient({
379
+ * chain: mainnet,
380
+ * transport: http(),
381
+ * }).extend(morphoViemExtension());
382
+ *
383
+ * const marketParams = markets[mainnet.id].usdc_wbtc;
384
+ * const market = client.morpho.blue(marketParams, mainnet.id);
385
+ * const block = await client.getBlock();
386
+ * const reallocationData = await market.getReallocationData({
387
+ * vaultAddresses: [vaults[mainnet.id].steakUsdc.address],
388
+ * block: { number: block.number, timestamp: block.timestamp },
389
+ * });
390
+ *
391
+ * // Max borrow keeping utilization at or below 90%, counting shared liquidity.
392
+ * const available: bigint =
393
+ * reallocationData.getAvailableLiquidityToTargetUtilization(
394
+ * marketParams.id,
395
+ * parseEther("0.9"),
396
+ * { timestamp: block.timestamp },
397
+ * );
398
+ * ```
399
+ */
400
+ // biome-ignore lint/complexity/useMaxParams: (marketId, targetUtilization, options) is the metric's public API
401
+ getAvailableLiquidityToTargetUtilization(marketId, targetUtilization = DEFAULT_SUPPLY_TARGET_UTILIZATION, options) {
402
+ const market = this.getMarket(marketId).accrueInterest(options?.timestamp);
403
+ // Below the allocator's trigger, no reallocation happens: own headroom only.
404
+ const supplyTargetUtilization = getSupplyTargetUtilization(marketId, options);
405
+ if (supplyTargetUtilization > targetUtilization)
406
+ return market.getBorrowToUtilization(targetUtilization);
407
+ // Borrow-to-target on the post-reallocation supply; clamps to 0n above target.
408
+ const availableLiquidity = this.getPublicReallocationLiquidity(marketId, options);
409
+ return MarketUtils.getBorrowToUtilization({
410
+ totalSupplyAssets: market.totalSupplyAssets + availableLiquidity,
411
+ totalBorrowAssets: market.totalBorrowAssets,
412
+ }, targetUtilization);
413
+ }
303
414
  /**
304
415
  * Gets the largest currently valid source-market withdrawal for a vault.
305
416
  *
@@ -1,6 +1,6 @@
1
1
  import { MarketUtils, MathLib } from "@morpho-org/blue-sdk";
2
2
  import { InsufficientSharedLiquidityError, MissingPublicAllocatorConfigError, ReallocationWithdrawExceedsMarketSupplyError, } from "../types/index.js";
3
- import { DEFAULT_SUPPLY_TARGET_UTILIZATION } from "./constant.js";
3
+ import { getSupplyTargetUtilization } from "./utilization.js";
4
4
  import { compareMarketIds } from "./validate.js";
5
5
  const groupWithdrawalsByVault = (withdrawals) => {
6
6
  const groups = [];
@@ -142,9 +142,7 @@ export const computeReallocations = ({ reallocationData: data, marketId, operati
142
142
  const newTotalSupplyAssets = operation === "withdraw"
143
143
  ? market.totalSupplyAssets - amount
144
144
  : market.totalSupplyAssets;
145
- const supplyTargetUtilization = options?.supplyTargetUtilization?.[market.params.id] ??
146
- options?.defaultSupplyTargetUtilization ??
147
- DEFAULT_SUPPLY_TARGET_UTILIZATION;
145
+ const supplyTargetUtilization = getSupplyTargetUtilization(market.params.id, options);
148
146
  if (MarketUtils.getUtilization({
149
147
  totalSupplyAssets: newTotalSupplyAssets,
150
148
  totalBorrowAssets: newTotalBorrowAssets,
@@ -0,0 +1,12 @@
1
+ import type { MarketId } from "@morpho-org/blue-sdk";
2
+ import type { ReallocationComputeOptions } from "../types/index.js";
3
+ /**
4
+ * Resolves the effective supply target utilization for a market: the per-market
5
+ * override, then the global default override, then
6
+ * {@link DEFAULT_SUPPLY_TARGET_UTILIZATION}.
7
+ *
8
+ * @param marketId - The market whose supply target utilization to resolve.
9
+ * @param options - Reallocation options carrying the per-market and default overrides.
10
+ * @returns The supply target utilization, scaled by WAD.
11
+ */
12
+ export declare const getSupplyTargetUtilization: (marketId: MarketId, options?: ReallocationComputeOptions) => bigint;
@@ -0,0 +1,13 @@
1
+ import { DEFAULT_SUPPLY_TARGET_UTILIZATION } from "./constant.js";
2
+ /**
3
+ * Resolves the effective supply target utilization for a market: the per-market
4
+ * override, then the global default override, then
5
+ * {@link DEFAULT_SUPPLY_TARGET_UTILIZATION}.
6
+ *
7
+ * @param marketId - The market whose supply target utilization to resolve.
8
+ * @param options - Reallocation options carrying the per-market and default overrides.
9
+ * @returns The supply target utilization, scaled by WAD.
10
+ */
11
+ export const getSupplyTargetUtilization = (marketId, options) => options?.supplyTargetUtilization?.[marketId] ??
12
+ options?.defaultSupplyTargetUtilization ??
13
+ DEFAULT_SUPPLY_TARGET_UTILIZATION;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@morpho-org/morpho-sdk",
3
3
  "description": "Abstraction layer for Morpho's complexity.",
4
- "version": "4.0.0",
4
+ "version": "4.1.0",
5
5
  "author": "Morpho Association <contact@morpho.org>",
6
6
  "contributors": [
7
7
  "Foulks-Plb <https://x.com/FoulkPlb>"
@@ -138,8 +138,8 @@
138
138
  "dependencies": {
139
139
  "zod": "^4.4.3",
140
140
  "@morpho-org/blue-sdk": "^6.2.0",
141
- "@morpho-org/blue-sdk-viem": "^5.1.1",
142
- "@morpho-org/morpho-ts": "^2.6.0"
141
+ "@morpho-org/morpho-ts": "^2.6.0",
142
+ "@morpho-org/blue-sdk-viem": "^5.1.1"
143
143
  },
144
144
  "peerDependencies": {
145
145
  "viem": "^2.0.0"
@@ -150,8 +150,8 @@
150
150
  "typescript": "^6.0.3",
151
151
  "viem": "2.52.2",
152
152
  "vitest": "^4.1.8",
153
- "@morpho-org/simulation-sdk": "^4.0.3",
154
153
  "@morpho-org/bundler-sdk-viem": "^5.0.3",
154
+ "@morpho-org/simulation-sdk": "^4.0.3",
155
155
  "@morpho-org/test": "^2.8.1"
156
156
  },
157
157
  "scripts": {