import { AccrualPosition, Market, type MarketId, Position, Vault, VaultMarketConfig } from "@morpho-org/blue-sdk"; import type { Address } from "viem"; import type { PublicAllocatorOptions, PublicReallocation, ReallocationComputeOptions } from "../types/index.js"; /** * Input state required to construct {@link VaultV1ReallocationData}. * * @deprecated Vault V1 shared-liquidity planning will be removed in the next major. Use * `InputVaultV2BlueReallocationData`. */ export interface InputVaultV1ReallocationData { /** Chain id associated with the fetched state. */ readonly chainId: number; /** Markets indexed by market id. */ readonly markets?: Readonly>; /** Vaults indexed by vault address. */ readonly vaults?: Readonly>; /** Positions indexed by user or vault address, then by market id. */ readonly positions?: Readonly>>>; /** Vault market configs indexed by vault address, then by market id. */ readonly vaultMarketConfigs?: Readonly>>>; } /** * Deprecated input name for Vault V1 reallocation data. * * @deprecated Vault V1 shared-liquidity planning will be removed in the next major. Use * `InputVaultV2BlueReallocationData`. */ export type InputReallocationData = InputVaultV1ReallocationData; /** * Narrow state container for computing public allocator reallocations. * * @remarks * The class owns only the market, vault, position, vault-market-config, * and chain data needed by the shared-liquidity algorithm. Constructor inputs * are cloned, and simulation steps return cloned `VaultV1ReallocationData` instances * so fetched caller inputs are not mutated. * * Public records are exposed for inspection and snapshotting only. Treat * `markets`, `vaults`, `positions`, and `vaultMarketConfigs` as a read * contract keyed by market id or address; use the getters for typed absence * errors and use simulation methods to produce updated state. * * @deprecated Vault V1 shared-liquidity planning will be removed in the next major. Use * `VaultV2BlueReallocationData`. */ export declare class VaultV1ReallocationData implements InputVaultV1ReallocationData { /** Chain id associated with the fetched reallocation data. */ readonly chainId: number; /** Markets indexed by market id. */ readonly markets: Record; /** Vaults indexed by vault address. */ readonly vaults: Record; /** Positions indexed by user or vault address, then by market id. */ readonly positions: Record>; /** Vault market configs indexed by vault address, then by market id. */ readonly vaultMarketConfigs: Record>; /** * Creates a cloned reallocation state from fetched market, vault, position, * and vault-market-config data. * * @param input - Reallocation input data fetched at a consistent chain state. */ constructor(input: InputVaultV1ReallocationData); /** * Creates a deep clone of this reallocation state. * * @returns A new `VaultV1ReallocationData` instance with cloned entity objects. */ clone(): VaultV1ReallocationData; private forkAliasedState; /** * Gets a market by id. * * @param marketId - Market id to read. * @returns The cloned market data. * @throws {@link UnknownReallocationMarketError} when the market is absent. */ getMarket(marketId: MarketId): Market; /** * Gets a vault by address. * * @param vault - Vault address to read. * @returns The cloned vault data. * @throws {@link UnknownReallocationVaultError} when the vault is absent. */ getVault(vault: Address): Vault; /** * Gets a raw market position. * * @param user - Position owner address, usually a MetaMorpho vault. * @param marketId - Market id for the position. * @returns The cloned position data. * @throws {@link UnknownReallocationPositionError} when the position is absent. */ getPosition(user: Address, marketId: MarketId): Position; /** * Gets a position wrapped with its market accrual helpers. * * @param user - Position owner address, usually a MetaMorpho vault. * @param marketId - Market id for the position. * @returns Accrual-aware position data. * @throws {@link UnknownReallocationPositionError} when the position is absent. * @throws {@link UnknownReallocationMarketError} when the market is absent. */ getAccrualPosition(user: Address, marketId: MarketId): AccrualPosition; /** * Gets a vault-market config. * * @param vault - Vault address. * @param marketId - Market id configured by the vault. * @returns The cloned vault-market config. * @throws {@link UnknownReallocationVaultMarketConfigError} when the config is absent. */ getVaultMarketConfig(vault: Address, marketId: MarketId): VaultMarketConfig; /** * Calculates public reallocations that can supply liquidity to `marketId`. * * @remarks * The algorithm repeatedly chooses the single largest currently available * source-market withdrawal across reallocatable vaults, applies that * withdrawal to a cloned state, and repeats until no valid withdrawal remains. * * Pass `options.timestamp` to evaluate market interest and pending public * allocator caps at the same block as the fetched reallocation data. * This method does not add an implicit delay margin before measuring * target-market vault headroom. If a transaction may land later than the * fetched block, pass a future `timestamp` or reserve your own headroom so * interest accrued before inclusion does not make `reallocateTo` exceed the * target market cap. * Returned `data` normalizes `vault.publicAllocatorConfig.accruedFee` to the * on-chain `reallocateTo` fee semantics: one fee charge per vault with at * least one computed withdrawal. * Sources with zero allocator withdrawal capacity and destinations with no deposit capacity * are skipped before projecting source interest. * * @param marketId - Target market to supply with shared liquidity. * @param options - Optional allocator discovery options. * @returns Computed source-market withdrawals and the post-reallocation state. * @throws {UnsupportedBlueMarketIrmError} when a market with positive debt uses an unsupported IRM. * @throws {@link UnknownReallocationMarketError} when the target market is absent. * @deprecated Vault V1 shared-liquidity planning will be removed in the next major. Use * `VaultV2BlueReallocationData.computeVaultV2BlueReallocations`. * @example * ```ts * import { createPublicClient, http } from "viem"; * import { mainnet } from "viem/chains"; * import { markets, vaults } from "@morpho-org/morpho-test"; * import { * morphoViemExtension, * type PublicReallocation, * } from "@morpho-org/morpho-sdk"; * import type { VaultV1ReallocationData } from "@morpho-org/morpho-sdk/entities"; * * const client = createPublicClient({ * chain: mainnet, * transport: http(), * }).extend(morphoViemExtension()); * * const marketParams = markets[mainnet.id].usdc_wbtc; * const market = client.morpho.blue(marketParams, mainnet.id); * const block = await client.getBlock(); * const reallocationData = await market.getVaultV1ReallocationData({ * vaultAddresses: [vaults[mainnet.id].steakUsdc.address], * block: { number: block.number, timestamp: block.timestamp }, * }); * * const result: { * withdrawals: readonly PublicReallocation[]; * data: VaultV1ReallocationData; * } = reallocationData.computeVaultV1Reallocations(marketParams.id, { * timestamp: block.timestamp, * }); * ``` */ computeVaultV1Reallocations(marketId: MarketId, options?: PublicAllocatorOptions): { readonly withdrawals: readonly PublicReallocation[]; data: VaultV1ReallocationData; }; /** * Calculates Vault V1 public reallocations that can supply liquidity to `marketId`. * * @param marketId - Target market to supply with shared liquidity. * @param options - Optional allocator discovery options. * @returns Computed source-market withdrawals and the post-reallocation state. * @throws {UnsupportedBlueMarketIrmError} when a market with positive debt uses an unsupported IRM. * @throws {@link UnknownReallocationMarketError} when the target market is absent. * @deprecated Vault V1 shared-liquidity planning will be removed in the next major. Use * `VaultV2BlueReallocationData.computeVaultV2BlueReallocations`. */ getMarketPublicReallocations(marketId: MarketId, options?: PublicAllocatorOptions): { readonly withdrawals: readonly PublicReallocation[]; data: VaultV1ReallocationData; }; /** * Sums the public-allocator liquidity reallocatable into `marketId` from * sibling markets. * * Read-only metric — never throws on insufficiency (returns `0n`). Bounded by * each source market's withdrawal utilization cap and the target market's * vault supply-cap headroom. Pass `options.defaultMaxWithdrawalUtilization` to * widen the source ceiling (e.g. `MathLib.WAD` for the full drain). * * @param marketId - Target market that would receive the liquidity. * @param options - Optional allocator discovery options. * @returns Total reallocatable assets in loan-token units; `0n` when none is available. * @throws {UnsupportedBlueMarketIrmError} when a market with positive debt uses an unsupported IRM. * @throws {@link UnknownReallocationMarketError} when the target market is absent. * @deprecated Vault V1 shared-liquidity metrics will be removed in the next major. Use * `VaultV2BlueReallocationData.getPublicReallocationLiquidity`. * @example * ```ts * import { createPublicClient, http } from "viem"; * import { mainnet } from "viem/chains"; * import { markets, vaults } from "@morpho-org/morpho-test"; * import { morphoViemExtension } from "@morpho-org/morpho-sdk"; * * const client = createPublicClient({ * chain: mainnet, * transport: http(), * }).extend(morphoViemExtension()); * * const marketParams = markets[mainnet.id].usdc_wbtc; * const market = client.morpho.blue(marketParams, mainnet.id); * const block = await client.getBlock(); * const reallocationData = await market.getVaultV1ReallocationData({ * vaultAddresses: [vaults[mainnet.id].steakUsdc.address], * block: { number: block.number, timestamp: block.timestamp }, * }); * * const liquidity: bigint = reallocationData.getPublicReallocationLiquidity( * marketParams.id, * { timestamp: block.timestamp }, * ); * ``` */ getPublicReallocationLiquidity(marketId: MarketId, options?: PublicAllocatorOptions): bigint; /** * Computes the liquidity available to bring `marketId` to `utilization`, * counting the public-allocator liquidity reallocatable into it. * * Returns the max borrow `x` keeping post-borrow utilization * `(borrow + x) / (supply + L) ≤ utilization`, where `L` is the * reallocatable liquidity added to the market's supply — equivalently * `getBorrowToUtilization({ supply + L, borrow }, utilization)`. Below * `utilization` this is the market's own borrow headroom plus `utilization · L`; * reallocated supply also raises the supply denominator, so only that scaled * share backs further borrow. * * Read-only metric — never throws on insufficiency: * - returns only the market's own borrow headroom when * `supplyTargetUtilization > utilization` (reallocation would not * trigger at that utilization); * - returns `0n` when the market is already at or above `utilization` and `L` * is too small to bring it back under. * * @param marketId - Target market to borrow from. * @param utilization - Utilization to bring the market to, scaled by WAD. Defaults to {@link DEFAULT_SUPPLY_TARGET_UTILIZATION}. * @param options - Optional reallocation options (supply target utilization trigger, timestamp, withdrawal caps). * @returns Available liquidity to the given utilization in loan-token units; `0n` when none is available. * @throws {UnsupportedBlueMarketIrmError} when a market with positive debt uses an unsupported IRM. * @throws {@link UnknownReallocationMarketError} when the target market is absent. * @deprecated Vault V1 shared-liquidity metrics will be removed in the next major. Use * `VaultV2BlueReallocationData.getAvailableLiquidityToUtilization`. * @example * ```ts * import { createPublicClient, http, parseEther } from "viem"; * import { mainnet } from "viem/chains"; * import { markets, vaults } from "@morpho-org/morpho-test"; * import { morphoViemExtension } from "@morpho-org/morpho-sdk"; * * const client = createPublicClient({ * chain: mainnet, * transport: http(), * }).extend(morphoViemExtension()); * * const marketParams = markets[mainnet.id].usdc_wbtc; * const market = client.morpho.blue(marketParams, mainnet.id); * const block = await client.getBlock(); * const reallocationData = await market.getVaultV1ReallocationData({ * vaultAddresses: [vaults[mainnet.id].steakUsdc.address], * block: { number: block.number, timestamp: block.timestamp }, * }); * * // Max borrow keeping utilization at or below 90%, counting shared liquidity. * const available: bigint = * reallocationData.getAvailableLiquidityToUtilization( * marketParams.id, * parseEther("0.9"), * { timestamp: block.timestamp }, * ); * ``` */ getAvailableLiquidityToUtilization(marketId: MarketId, utilization?: bigint, options?: ReallocationComputeOptions): bigint; /** * Gets the largest currently valid source-market withdrawal for a vault. * * @param params - Candidate vault, target market, prior withdrawals, and allocator limits. * @returns The largest withdrawal candidate, or `undefined` if required data is missing. * * @internal */ private getLargestVaultWithdrawal; /** * Applies one public allocator withdrawal and corresponding target-market supply. * * @param vault - Vault performing the public reallocation. * @param supplyMarketId - Target market that receives the withdrawn assets. * @param withdrawal - Source market id and asset amount to withdraw. * @param timestamp - Timestamp used to accrue source and target positions. * @returns A cloned state after applying the simulated reallocation. * @throws {@link MissingPublicAllocatorConfigError} when the vault allocator config is absent. * @throws {@link UnknownReallocationVaultMarketConfigError} when source or target allocator limits are absent. * * @internal */ protected applyPublicReallocation({ vault, supplyMarketId, withdrawal, timestamp, }: { readonly vault: Address; readonly supplyMarketId: MarketId; readonly withdrawal: { readonly id: MarketId; readonly assets: bigint; }; readonly timestamp: bigint; }): VaultV1ReallocationData; } /** * Deprecated class name for Vault V1 reallocation data. * * @deprecated Vault V1 shared-liquidity planning will be removed in the next major. Use * `VaultV2BlueReallocationData`. */ export { VaultV1ReallocationData as ReallocationData };