import { type IMarket, Market, type MaxBorrowOptions, type MaxPositionCapacities, type MaxWithdrawCollateralOptions } from "../market/index.js"; import type { Address, BigIntish, MarketId } from "../types.js"; /** Plain input shape for a user's Morpho Blue market position. */ export interface IPosition { user: Address; marketId: MarketId; supplyShares: bigint; borrowShares: bigint; collateral: bigint; } /** Represents a user's supply, borrow, and collateral balances on one market. */ export declare class Position implements IPosition { /** * The user holding this position. */ readonly user: Address; /** * The id of the market on which this position is held. */ readonly marketId: MarketId; /** * The amount of supply shares held with this position. */ supplyShares: bigint; /** * The amount of borrow shares held with this position. */ borrowShares: bigint; /** * The amount of collateral assets held with this position. */ collateral: bigint; constructor({ user, marketId, supplyShares, borrowShares, collateral, }: IPosition); } /** Plain input shape for a position paired with market state for accrual math. */ export interface IAccrualPosition extends Omit { } /** Represents a position paired with market state for derived and accrued values. */ export declare class AccrualPosition extends Position implements IAccrualPosition { protected readonly _market: Market; constructor(position: IAccrualPosition, market: IMarket); /** * The market on which this position is held. */ get market(): Market; get supplyAssets(): bigint; get borrowAssets(): bigint; /** * The value of this position's collateral quoted in loan assets. * `undefined` if the market's oracle is undefined or reverts. */ get collateralValue(): bigint | undefined; /** * The maximum amount of loan assets that can be borrowed against this position's collateral. * `undefined` if the market's oracle is undefined or reverts. */ get maxBorrowAssets(): bigint | undefined; /** * The maximum additional amount of assets that can be borrowed against this position's collateral. * `undefined` if the market's oracle is undefined or reverts. */ get maxBorrowableAssets(): bigint | undefined; /** * The maximum amount of collateral that can be seized in exchange for the outstanding debt. * `undefined` if the market's oracle is undefined or reverts. */ get seizableCollateral(): bigint | undefined; /** * The maximum amount of collateral that can be withdrawn. * `undefined` if the market's oracle is undefined or reverts. */ get withdrawableCollateral(): bigint | undefined; /** * Whether this position is healthy. * `undefined` if the market's oracle is undefined or reverts. */ get isHealthy(): boolean | undefined; /** * Whether this position can be liquidated. * `undefined` if the market's oracle is undefined or reverts. */ get isLiquidatable(): boolean | undefined; /** * The price of the collateral quoted in loan assets that would allow this position to be liquidated. * `null` if the position has no borrow. */ get liquidationPrice(): bigint | null; /** * The price variation required for the position to reach its liquidation threshold (scaled by WAD). * Negative when healthy (the price needs to drop x%), positive when unhealthy (the price needs to soar x%). * `undefined` if the market's oracle is undefined or reverts. * `null` if the position is not a borrow. */ get priceVariationToLiquidationPrice(): bigint | null | undefined; /** * This position's Loan-To-Value (debt over collateral power, scaled by WAD). * If the collateral price is 0, LTV is `MaxUint256`. * `undefined` if the market's oracle is undefined or reverts. */ get ltv(): bigint | null | undefined; /** * This position's health factor (collateral power over debt, scaled by WAD). * If the debt is 0, health factor is `MaxUint256`. * `undefined` if the market's oracle is undefined or reverts. */ get healthFactor(): bigint | undefined; /** * The percentage of this position's borrow power currently used (scaled by WAD). * If the collateral price is 0, usage is `MaxUint256`. */ get borrowCapacityUsage(): bigint | undefined; /** * Returns the maximum amount of loan assets that can be withdrawn given a certain supply position * and a balance of loan assets, and the reason for the limit. */ get withdrawCapacityLimit(): import("../utils.js").CapacityLimit; /** * Returns a new position derived from this position, whose interest has been accrued up to the given timestamp. * @param timestamp Optional accrual timestamp. Defaults to the market's `lastUpdate`; past or equal timestamps return an unchanged copy without projecting interest or rewinding the market. * @returns A new position with its accrued market, unchanged for past or equal timestamps. * @throws {UnsupportedMarketIrmError} when projection requires a nonzero unsupported IRM. * @example * ```ts * import { fetchAccrualPosition } from "@morpho-org/blue-sdk-viem"; * import { markets } from "@morpho-org/morpho-test"; * import { createPublicClient, http, zeroAddress } from "viem"; * import { mainnet } from "viem/chains"; * * const client = createPublicClient({ chain: mainnet, transport: http() }); * const position = await fetchAccrualPosition( * zeroAddress, markets[mainnet.id].eth_wstEth.id, client, * ); * const accrued = position.accrueInterest(position.market.lastUpdate - 1n); * // accrued satisfies AccrualPosition; its values equal the original snapshot. * // accrued !== position; accrued.market.lastUpdate === position.market.lastUpdate * ``` */ accrueInterest(timestamp?: BigIntish): AccrualPosition; /** * Projects a market supply into a new accrued position without changing this position. * * @param assets - Supplied loan assets, or `0n` when `shares` specifies the amount. * @param shares - Minted supply shares, or `0n` when `assets` specifies the amount. * @param timestamp - Optional accrual timestamp. Defaults to the market's `lastUpdate`; earlier timestamps skip interest accrual while still applying the operation. * @returns The new accrued position, supplied assets, and minted supply shares after rounding. * @throws {BlueErrors.InconsistentInput} when both `assets` and `shares` are zero or both are nonzero. * @throws {UnsupportedMarketIrmError} when positive debt requires an unsupported IRM projection. * @example * ```ts * import { AccrualPosition, ChainId, ORACLE_PRICE_SCALE } from "@morpho-org/blue-sdk"; * import { markets } from "@morpho-org/morpho-test"; * import { zeroAddress } from "viem"; * * const position = new AccrualPosition( * { user: zeroAddress, supplyShares: 100n, borrowShares: 50n, collateral: 200n }, * { * params: markets[ChainId.EthMainnet].eth_wstEth, * totalSupplyAssets: 400n, * totalBorrowAssets: 200n, * totalSupplyShares: 400n, * totalBorrowShares: 200n, * lastUpdate: 0n, * fee: 0n, * price: ORACLE_PRICE_SCALE, * }, * ); * const result = position.supply(10n, 0n); * // result satisfies { position: AccrualPosition; assets: bigint; shares: bigint } * ``` */ supply(assets: bigint, shares: bigint, timestamp?: BigIntish): { position: AccrualPosition; assets: bigint; shares: bigint; }; /** * Projects a market withdrawal into a new accrued position without changing this position. * * @param assets - Withdrawn loan assets, or `0n` when `shares` specifies the amount. * @param shares - Burned supply shares, or `0n` when `assets` specifies the amount. * @param timestamp - Optional accrual timestamp. Defaults to the market's `lastUpdate`; earlier timestamps skip interest accrual while still applying the operation. * @returns The new accrued position, withdrawn assets, and burned supply shares after rounding. * @throws {BlueErrors.InconsistentInput} when both `assets` and `shares` are zero or both are nonzero. * @throws {BlueErrors.InsufficientLiquidity} when the market lacks enough loan assets. * @throws {BlueErrors.InsufficientPosition} when the withdrawal exceeds the position's supply shares. * @throws {UnsupportedMarketIrmError} when positive debt requires an unsupported IRM projection. * @example * ```ts * import { AccrualPosition, ChainId, ORACLE_PRICE_SCALE } from "@morpho-org/blue-sdk"; * import { markets } from "@morpho-org/morpho-test"; * import { zeroAddress } from "viem"; * * const position = new AccrualPosition( * { user: zeroAddress, supplyShares: 100n, borrowShares: 50n, collateral: 200n }, * { * params: markets[ChainId.EthMainnet].eth_wstEth, * totalSupplyAssets: 400n, * totalBorrowAssets: 200n, * totalSupplyShares: 400n, * totalBorrowShares: 200n, * lastUpdate: 0n, * fee: 0n, * price: ORACLE_PRICE_SCALE, * }, * ); * const result = position.withdraw(10n, 0n); * // result satisfies { position: AccrualPosition; assets: bigint; shares: bigint } * ``` */ withdraw(assets: bigint, shares: bigint, timestamp?: BigIntish): { position: AccrualPosition; assets: bigint; shares: bigint; }; /** * Projects a collateral supply into a new accrued position without changing this position. * * @param assets - Collateral assets to add. * @returns A new accrued position with the increased collateral balance. * @example * ```ts * import { ChainId } from "@morpho-org/blue-sdk"; * import { fetchAccrualPosition } from "@morpho-org/blue-sdk-viem"; * import { markets } from "@morpho-org/morpho-test"; * import { createPublicClient, http, zeroAddress } from "viem"; * import { mainnet } from "viem/chains"; * * const client = createPublicClient({ chain: mainnet, transport: http() }); * const market = markets[ChainId.EthMainnet].eth_wstEth; * const position = await fetchAccrualPosition(zeroAddress, market.id, client); * const projected = position.supplyCollateral(1_000_000_000_000_000_000n); * // projected satisfies AccrualPosition * ``` */ supplyCollateral(assets: bigint): AccrualPosition; /** * Projects a collateral withdrawal into a new accrued position without changing this position. * * @param assets - Collateral assets to withdraw. * @param timestamp - Optional accrual timestamp. Defaults to the market's `lastUpdate`; earlier timestamps skip interest accrual while still applying the operation. * @returns A new accrued position with the reduced collateral balance. * @throws {BlueErrors.UnknownOraclePrice} when the market's oracle price is unavailable. * @throws {BlueErrors.InsufficientPosition} when the withdrawal exceeds the position's collateral. * @throws {BlueErrors.InsufficientCollateral} when the projected position would be unhealthy. * @throws {UnsupportedMarketIrmError} when positive debt requires an unsupported IRM projection. * @example * ```ts * import { AccrualPosition, ChainId, ORACLE_PRICE_SCALE } from "@morpho-org/blue-sdk"; * import { markets } from "@morpho-org/morpho-test"; * import { zeroAddress } from "viem"; * * const position = new AccrualPosition( * { user: zeroAddress, supplyShares: 100n, borrowShares: 50n, collateral: 200n }, * { * params: markets[ChainId.EthMainnet].eth_wstEth, * totalSupplyAssets: 400n, * totalBorrowAssets: 200n, * totalSupplyShares: 400n, * totalBorrowShares: 200n, * lastUpdate: 0n, * fee: 0n, * price: ORACLE_PRICE_SCALE, * }, * ); * const projected = position.withdrawCollateral(10n); * // projected satisfies AccrualPosition * ``` */ withdrawCollateral(assets: bigint, timestamp?: BigIntish): AccrualPosition; /** * Projects a market borrow into a new accrued position without changing this position. * * @param assets - Borrowed loan assets, or `0n` when `shares` specifies the amount. * @param shares - Minted borrow shares, or `0n` when `assets` specifies the amount. * @param timestamp - Optional accrual timestamp. Defaults to the market's `lastUpdate`; earlier timestamps skip interest accrual while still applying the operation. * @returns The new accrued position, borrowed assets, and minted borrow shares after rounding. * @throws {BlueErrors.UnknownOraclePrice} when the market's oracle price is unavailable. * @throws {BlueErrors.InconsistentInput} when both `assets` and `shares` are zero or both are nonzero. * @throws {BlueErrors.InsufficientLiquidity} when the market lacks enough loan assets. * @throws {BlueErrors.InsufficientCollateral} when the projected position would be unhealthy. * @throws {UnsupportedMarketIrmError} when positive debt requires an unsupported IRM projection. * @example * ```ts * import { AccrualPosition, ChainId, ORACLE_PRICE_SCALE } from "@morpho-org/blue-sdk"; * import { markets } from "@morpho-org/morpho-test"; * import { zeroAddress } from "viem"; * * const position = new AccrualPosition( * { user: zeroAddress, supplyShares: 100n, borrowShares: 50n, collateral: 200n }, * { * params: markets[ChainId.EthMainnet].eth_wstEth, * totalSupplyAssets: 400n, * totalBorrowAssets: 200n, * totalSupplyShares: 400n, * totalBorrowShares: 200n, * lastUpdate: 0n, * fee: 0n, * price: ORACLE_PRICE_SCALE, * }, * ); * const result = position.borrow(10n, 0n); * // result satisfies { position: AccrualPosition; assets: bigint; shares: bigint } * ``` */ borrow(assets: bigint, shares: bigint, timestamp?: BigIntish): { position: AccrualPosition; assets: bigint; shares: bigint; }; /** * Projects a market repayment into a new accrued position without changing this position. * * @param assets - Repaid loan assets, or `0n` when `shares` specifies the amount. * @param shares - Burned borrow shares, or `0n` when `assets` specifies the amount. * @param timestamp - Optional accrual timestamp. Defaults to the market's `lastUpdate`; earlier timestamps skip interest accrual while still applying the operation. * @returns The new accrued position, repaid assets, and burned borrow shares after rounding. * @throws {BlueErrors.InconsistentInput} when both `assets` and `shares` are zero or both are nonzero. * @throws {BlueErrors.InsufficientPosition} when the repayment exceeds the position's borrow shares. * @throws {UnsupportedMarketIrmError} when positive debt requires an unsupported IRM projection. * @example * ```ts * import { AccrualPosition, ChainId, ORACLE_PRICE_SCALE } from "@morpho-org/blue-sdk"; * import { markets } from "@morpho-org/morpho-test"; * import { zeroAddress } from "viem"; * * const position = new AccrualPosition( * { user: zeroAddress, supplyShares: 100n, borrowShares: 50n, collateral: 200n }, * { * params: markets[ChainId.EthMainnet].eth_wstEth, * totalSupplyAssets: 400n, * totalBorrowAssets: 200n, * totalSupplyShares: 400n, * totalBorrowShares: 200n, * lastUpdate: 0n, * fee: 0n, * price: ORACLE_PRICE_SCALE, * }, * ); * const result = position.repay(10n, 0n); * // result satisfies { position: AccrualPosition; assets: bigint; shares: bigint } * ``` */ repay(assets: bigint, shares: bigint, timestamp?: BigIntish): { position: AccrualPosition; assets: bigint; shares: bigint; }; getBorrowCapacityLimit(options?: MaxBorrowOptions): import("../utils.js").CapacityLimit | undefined; getWithdrawCollateralCapacityLimit(options?: MaxWithdrawCollateralOptions): import("../utils.js").CapacityLimit | undefined; getRepayCapacityLimit(loanTokenBalance: bigint): import("../utils.js").CapacityLimit; getMaxCapacities(loanTokenBalance: bigint, collateralTokenBalance: bigint, options?: { borrow?: MaxBorrowOptions; withdrawCollateral?: MaxWithdrawCollateralOptions; }): MaxPositionCapacities; }