import { type RoundingDirection } from "../math/index.js"; import { VaultToken } from "../token/index.js"; import type { Address, BigIntish, MarketId } from "../types.js"; import { type CapacityLimit } from "../utils.js"; import type { IVaultConfig } from "./VaultConfig.js"; import { type IVaultMarketAllocation, VaultMarketAllocation } from "./VaultMarketAllocation.js"; /** Pending governance value and the timestamp at which it becomes valid. */ export interface Pending { value: T; validAt: bigint; } /** Plain input shape for a MetaMorpho vault. */ export interface IVault extends IVaultConfig { curator: Address; owner: Address; guardian: Address; fee: bigint; feeRecipient: Address; skimRecipient: Address; pendingTimelock: Pending; pendingGuardian: Pending
; pendingOwner: Address; timelock: bigint; supplyQueue: MarketId[]; withdrawQueue: MarketId[]; totalSupply: bigint; totalAssets: bigint; lastTotalAssets: bigint; lostAssets?: bigint; } /** Represents a MetaMorpho vault and its governance, queue, and accounting state. */ export declare class Vault extends VaultToken implements IVault { /** * The vault's share token's name. */ readonly name: string; /** * The vault's share token's symbol. */ readonly symbol: string; /** * The MetaMorpho vault's owner address. */ owner: Address; /** * The MetaMorpho vault's curator address. */ curator: Address; /** * The MetaMorpho vault's guardian address. */ guardian: Address; /** * The MetaMorpho vault's skim recipient address (mostly used to skim reward tokens claimed to the vault). */ skimRecipient: Address; /** * The MetaMorpho vault's fee recipient address. */ feeRecipient: Address; /** * The MetaMorpho vault's timelock (in seconds). */ timelock: bigint; /** * The MetaMorpho vault's fee. */ fee: bigint; /** * The MetaMorpho vault's pending owner address and activation timestamp. */ pendingOwner: Address; /** * The MetaMorpho vault's pending guardian address and activation timestamp. */ pendingGuardian: Pending
; /** * The MetaMorpho vault's pending timelock (in seconds) and activation timestamp. */ pendingTimelock: Pending; /** * The MetaMorpho vault's ordered supply queue. */ supplyQueue: MarketId[]; /** * The MetaMorpho vault's ordered withdraw queue. */ withdrawQueue: MarketId[]; /** * The MetaMorpho vault's last total assets used to calculate performance fees. */ lastTotalAssets: bigint; /** * The MetaMorpho vault's lost assets due to realized bad debt. * Only defined for MetaMorpho V1.1 vaults. */ lostAssets?: bigint; constructor({ curator, owner, guardian, fee, feeRecipient, skimRecipient, pendingTimelock, pendingGuardian, pendingOwner, timelock, supplyQueue, withdrawQueue, totalSupply, totalAssets, lastTotalAssets, lostAssets, ...config }: IVault); /** * Returns interest accrued since the vault's last accounting update, floored at zero. * * @returns The non-negative difference between `totalAssets` and `lastTotalAssets`, in the * underlying asset's smallest unit. * @example * ```ts * import { fetchVault } from "@morpho-org/blue-sdk-viem"; * import { createPublicClient, http } from "viem"; * import { mainnet } from "viem/chains"; * * const client = createPublicClient({ chain: mainnet, transport: http() }); * const vaultAddress = "0x9a8bC3B04b7f3D87cfC09ba407dCED575f2d61D8"; * const vault = await fetchVault(vaultAddress, client); * const interest = vault.totalInterest; * // interest satisfies bigint * ``` */ get totalInterest(): bigint; /** * Converts Vault V1 shares to underlying assets using the current totals and virtual offsets. * * @param shares - Vault shares to convert, in the share token's smallest unit. * @param rounding - Optional rounding direction. Defaults to `"Down"`. * @returns The corresponding underlying assets, rounded in the requested direction. * @example * ```ts * import { fetchVault } from "@morpho-org/blue-sdk-viem"; * import { createPublicClient, http } from "viem"; * import { mainnet } from "viem/chains"; * * const client = createPublicClient({ chain: mainnet, transport: http() }); * const vaultAddress = "0x9a8bC3B04b7f3D87cfC09ba407dCED575f2d61D8"; * const vault = await fetchVault(vaultAddress, client); * const assets = vault.toAssets(1_000_000_000_000_000_000n); * // assets satisfies bigint * ``` */ toAssets(shares: BigIntish, rounding?: RoundingDirection): bigint; /** * Converts underlying assets to Vault V1 shares using the current totals and virtual offsets. * * @param assets - Underlying assets to convert, in the asset token's smallest unit. * @param rounding - Optional rounding direction. Defaults to `"Up"`. * @returns The corresponding vault shares, rounded in the requested direction. * @example * ```ts * import { fetchVault } from "@morpho-org/blue-sdk-viem"; * import { createPublicClient, http } from "viem"; * import { mainnet } from "viem/chains"; * * const client = createPublicClient({ chain: mainnet, transport: http() }); * const vaultAddress = "0x9a8bC3B04b7f3D87cfC09ba407dCED575f2d61D8"; * const vault = await fetchVault(vaultAddress, client); * const shares = vault.toShares(1_000_000n); * // shares satisfies bigint * ``` */ toShares(assets: BigIntish, rounding?: RoundingDirection): bigint; } /** Aggregated vault allocation exposure for one collateral asset. */ export interface CollateralAllocation { address: Address; lltvs: Set; oracles: Set
; markets: Set; } /** Plain input shape for a MetaMorpho vault paired with accrued market allocations. */ export interface IAccrualVault extends Omit { } /** Represents a MetaMorpho vault with accrued market allocation state. */ export declare class AccrualVault extends Vault implements IAccrualVault { /** * @inheritdoc * Reflects the sum of assets of the vault's allocations. * Only includes virtually accrued interest if the vault's allocations include virtually accrued interest. */ totalAssets: bigint; /** * The allocation of the vault on each market enabled. */ readonly allocations: Map; /** * The aggregated allocation exposure (markets, LLTVs, oracles) per collateral asset. * Per-market proportions come from `getAllocationProportion(marketId)`. */ readonly collateralAllocations: Map; constructor(vault: IAccrualVault, /** * The allocation of the vault on each market of the withdraw queue, * in the same order as the withdraw queue. */ allocations: Omit[]); /** * Returns the vault assets immediately withdrawable from its allocated markets. * * @returns The sum of each allocation's position- and market-limited withdraw capacity, in the * underlying asset's smallest unit. * @example * ```ts * import { fetchAccrualVault } from "@morpho-org/blue-sdk-viem"; * import { createPublicClient, http } from "viem"; * import { mainnet } from "viem/chains"; * * const client = createPublicClient({ chain: mainnet, transport: http() }); * const vaultAddress = "0x9a8bC3B04b7f3D87cfC09ba407dCED575f2d61D8"; * const vault = await fetchAccrualVault(vaultAddress, client); * const liquidity = vault.liquidity; * // liquidity satisfies bigint * ``` */ get liquidity(): bigint; /** * Returns the vault's current allocation-weighted experienced APY before its performance fee. * * Use `getApy(timestamp)` to project the APY at a specific timestamp. Markets newer than that * timestamp use their snapshot rates without backward projection. * * @returns The current gross annual yield as a decimal JavaScript number, or `0` for an empty * vault. * @throws {UnsupportedMarketIrmError} when a supplied market with positive debt uses an unsupported IRM. * @example * ```ts * import { fetchAccrualVault } from "@morpho-org/blue-sdk-viem"; * import { createPublicClient, http } from "viem"; * import { mainnet } from "viem/chains"; * * const client = createPublicClient({ chain: mainnet, transport: http() }); * const vaultAddress = "0x9a8bC3B04b7f3D87cfC09ba407dCED575f2d61D8"; * const vault = await fetchAccrualVault(vaultAddress, client); * const apy = vault.apy; * // apy satisfies number * ``` */ get apy(): number; /** * Returns the vault's current allocation-weighted experienced APY after its performance fee. * * Use `getNetApy(timestamp)` to project the net APY at a specific timestamp. Markets newer than * that timestamp use their snapshot rates without backward projection. * * @returns The current net annual yield as a decimal JavaScript number, or `0` for an empty * vault. * @throws {UnsupportedMarketIrmError} when a supplied market with positive debt uses an unsupported IRM. * @example * ```ts * import { fetchAccrualVault } from "@morpho-org/blue-sdk-viem"; * import { createPublicClient, http } from "viem"; * import { mainnet } from "viem/chains"; * * const client = createPublicClient({ chain: mainnet, transport: http() }); * const vaultAddress = "0x9a8bC3B04b7f3D87cfC09ba407dCED575f2d61D8"; * const vault = await fetchAccrualVault(vaultAddress, client); * const netApy = vault.netApy; * // netApy satisfies number * ``` */ get netApy(): number; /** * The MetaMorpho vault's per-second rate at which interest _would_ accrue, * weighted-averaged over its market deposits, before deducting the performance fee, * if interest was to be accrued on each market at the given timestamp. */ private _getAvgRate; /** * Calculates the allocation-weighted experienced APY before fees if each market accrued at a * timestamp. * * @param timestamp - Optional Unix timestamp in seconds. Defaults to the current timestamp; * timestamps at or before each market's `lastUpdate` use its snapshot rate. * @returns The projected gross annual yield as a decimal JavaScript number, or `0` for an empty * vault. * @throws {UnsupportedMarketIrmError} when a supplied market with positive debt uses an unsupported IRM. * @example * ```ts * import { fetchAccrualVault } from "@morpho-org/blue-sdk-viem"; * import { createPublicClient, http } from "viem"; * import { mainnet } from "viem/chains"; * * const client = createPublicClient({ chain: mainnet, transport: http() }); * const vaultAddress = "0x9a8bC3B04b7f3D87cfC09ba407dCED575f2d61D8"; * const vault = await fetchAccrualVault(vaultAddress, client); * const apy = vault.getApy(); * // apy satisfies number * ``` */ getApy(timestamp?: BigIntish): number; /** * Calculates the allocation-weighted experienced APY after fees if each market accrued at a * timestamp. * * @param timestamp - Optional Unix timestamp in seconds. Defaults to the current timestamp; * timestamps at or before each market's `lastUpdate` use its snapshot rate. * @returns The projected net annual yield as a decimal JavaScript number, or `0` for an empty * vault. * @throws {UnsupportedMarketIrmError} when a supplied market with positive debt uses an unsupported IRM. * @example * ```ts * import { fetchAccrualVault } from "@morpho-org/blue-sdk-viem"; * import { createPublicClient, http } from "viem"; * import { mainnet } from "viem/chains"; * * const client = createPublicClient({ chain: mainnet, transport: http() }); * const vaultAddress = "0x9a8bC3B04b7f3D87cfC09ba407dCED575f2d61D8"; * const vault = await fetchAccrualVault(vaultAddress, client); * const netApy = vault.getNetApy(); * // netApy satisfies number * ``` */ getNetApy(timestamp?: BigIntish): number; /** * Returns one market's share of the vault's allocated assets, rounded down. * * @param marketId - Market whose allocation proportion to read. * @returns The WAD-scaled allocation proportion, or `0n` when the vault is empty or the market * is not allocated. * @example * ```ts * import { fetchAccrualVault } from "@morpho-org/blue-sdk-viem"; * import { createPublicClient, http } from "viem"; * import { mainnet } from "viem/chains"; * * const client = createPublicClient({ chain: mainnet, transport: http() }); * const vaultAddress = "0x9a8bC3B04b7f3D87cfC09ba407dCED575f2d61D8"; * const vault = await fetchAccrualVault(vaultAddress, client); * const marketId = vault.withdrawQueue[0]!; * const proportion = vault.getAllocationProportion(marketId); * // proportion satisfies bigint * ``` */ getAllocationProportion(marketId: MarketId): bigint; /** * Returns the requested asset amount depositable through markets in the supply queue. * * @param assets - Maximum underlying asset amount being considered. * @returns A capacity limit containing the depositable asset amount and either the `cap` or * `balance` limiting reason. * @example * ```ts * import { fetchAccrualVault } from "@morpho-org/blue-sdk-viem"; * import { createPublicClient, http } from "viem"; * import { mainnet } from "viem/chains"; * * const client = createPublicClient({ chain: mainnet, transport: http() }); * const vaultAddress = "0x9a8bC3B04b7f3D87cfC09ba407dCED575f2d61D8"; * const vault = await fetchAccrualVault(vaultAddress, client); * const limit = vault.maxDeposit(1_000_000n); * // limit satisfies CapacityLimit * ``` */ maxDeposit(assets: BigIntish): CapacityLimit; /** * Returns the underlying asset amount withdrawable for a requested vault share amount. * * @param shares - Maximum vault share amount being considered. * @returns A capacity limit containing the withdrawable asset amount and either the `liquidity` * or `balance` limiting reason. * @example * ```ts * import { fetchAccrualVault } from "@morpho-org/blue-sdk-viem"; * import { createPublicClient, http } from "viem"; * import { mainnet } from "viem/chains"; * * const client = createPublicClient({ chain: mainnet, transport: http() }); * const vaultAddress = "0x9a8bC3B04b7f3D87cfC09ba407dCED575f2d61D8"; * const vault = await fetchAccrualVault(vaultAddress, client); * const limit = vault.maxWithdraw(1_000_000_000_000_000_000n); * // limit satisfies CapacityLimit * ``` */ maxWithdraw(shares: BigIntish): CapacityLimit; /** * Projects the vault's market interest, loss accounting, and performance fees to a timestamp. * * Returns a new `AccrualVault` and leaves this instance unchanged. * Markets at or beyond the requested timestamp keep their snapshots without backward accrual * or a timestamp error. Zero-share allocations always keep their market snapshots. * Vault loss and fee accounting still reconciles against the resulting allocation balances. * * @param timestamp - Optional accrual timestamp in seconds. Defaults each allocation to its * own `lastUpdate`; earlier timestamps leave that market unchanged. * @returns A new vault whose market positions, realized losses, and fee shares reflect the * projected accounting. * @throws {UnknownMarketAllocationError} when the withdraw queue references a market without an * allocation. * @throws {UnsupportedMarketIrmError} when forward projection of an allocated market with positive * debt requires an unsupported IRM. * @example * ```ts * import { fetchAccrualVault } from "@morpho-org/blue-sdk-viem"; * import { createPublicClient, http } from "viem"; * import { mainnet } from "viem/chains"; * * const client = createPublicClient({ chain: mainnet, transport: http() }); * const vaultAddress = "0x9a8bC3B04b7f3D87cfC09ba407dCED575f2d61D8"; * const block = await client.getBlock(); * const vault = await fetchAccrualVault(vaultAddress, client, { * blockNumber: block.number, * }); * const accrued = vault.accrueInterest(block.timestamp); * // accrued satisfies AccrualVault * ``` */ accrueInterest(timestamp?: BigIntish): AccrualVault; }