@morpho-org/midnight-sdk
Version:
Viem-based package for Morpho Midnight that exports protocol utilities, fetch helpers, and Midnight API utilities under a dedicated API subpath.
656 lines (655 loc) • 22.7 kB
TypeScript
import { type BigIntish } from "@morpho-org/morpho-ts";
import type { Address, Hash } from "viem";
/**
* Seven settlement-fee centibip buckets stored on a Midnight market.
*
* @example
* ```ts
* import type { SettlementFeeCbps } from "@morpho-org/midnight-sdk";
*
* const cbps: SettlementFeeCbps = [0, 0, 0, 0, 0, 0, 0];
* console.log(cbps.length);
* ```
*/
export type SettlementFeeCbps = readonly [
number,
number,
number,
number,
number,
number,
number
];
/**
* Plain collateral params input accepted by {@link MarketParams}.
*
* @example
* ```ts
* import type { ICollateralParams } from "@morpho-org/midnight-sdk";
*
* const params: ICollateralParams = {
* token: "0x0000000000000000000000000000000000000001",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000000002",
* };
* ```
*/
export interface ICollateralParams {
/** Collateral token address. */
readonly token: Address;
/** WAD-scaled liquidation loan-to-value. */
readonly lltv: BigIntish;
/** WAD-scaled liquidation cursor used to compute the maximum liquidation incentive factor. */
readonly liquidationCursor: BigIntish;
/** Oracle address for this collateral. */
readonly oracle: Address;
}
/**
* Normalized Midnight collateral params.
*
* @example
* ```ts
* import type { CollateralParams } from "@morpho-org/midnight-sdk";
*
* const params: CollateralParams = {
* token: "0x0000000000000000000000000000000000000001",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000000002",
* };
* console.log(params.lltv);
* ```
*/
export interface CollateralParams {
/** Collateral token address. */
readonly token: Address;
/** WAD-scaled liquidation loan-to-value. */
readonly lltv: bigint;
/** WAD-scaled liquidation cursor used to compute the maximum liquidation incentive factor. */
readonly liquidationCursor: bigint;
/** Oracle address for this collateral. */
readonly oracle: Address;
}
/**
* Plain input accepted by {@link MarketParams}.
*
* @example
* ```ts
* import type { IMarketParams } from "@morpho-org/midnight-sdk";
*
* const params: IMarketParams = {
* chainId: 8453,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000000001",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000000002",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000000003",
* },
* ],
* maturity: 1n,
* rcfThreshold: 0n,
* enterGate: "0x0000000000000000000000000000000000000000",
* liquidatorGate: "0x0000000000000000000000000000000000000000",
* };
* ```
*/
export interface IMarketParams {
/** EIP-155 chain id captured in the market struct. */
readonly chainId: BigIntish;
/** Core Midnight contract address captured in the market struct. */
readonly midnight: Address;
/** Loan token address. */
readonly loanToken: Address;
/** Collateral definitions; `MarketParams` stores them sorted by token and rejects duplicate tokens. */
readonly collateralParams: readonly (ICollateralParams | CollateralParams)[];
/** Market maturity timestamp. */
readonly maturity: BigIntish;
/** Recovery close factor threshold. */
readonly rcfThreshold: BigIntish;
/** Optional entry gate. */
readonly enterGate: Address;
/** Optional liquidation gate. */
readonly liquidatorGate: Address;
}
/**
* Immutable Midnight market configuration.
*
* @example
* ```ts
* import { MarketParams } from "@morpho-org/midnight-sdk";
*
* const params = new MarketParams({
* chainId: 8453,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000000001",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000000002",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000000003",
* },
* ],
* maturity: 1n,
* rcfThreshold: 0n,
* enterGate: "0x0000000000000000000000000000000000000000",
* liquidatorGate: "0x0000000000000000000000000000000000000000",
* });
* console.log(params.loanToken);
* ```
*/
export declare class MarketParams {
/** EIP-155 chain id captured in the market struct. */
readonly chainId: bigint;
/** Core Midnight contract address captured in the market struct. */
readonly midnight: Address;
/** Loan token address. */
readonly loanToken: Address;
/** Collateral definitions sorted by token. */
readonly collateralParams: readonly CollateralParams[];
/** Market maturity timestamp. */
readonly maturity: bigint;
/** Recovery close factor threshold. */
readonly rcfThreshold: bigint;
/** Entry gate address. */
readonly enterGate: Address;
/** Liquidator gate address. */
readonly liquidatorGate: Address;
/**
* Creates normalized market params.
*
* @param params - Market params to normalize.
* @throws {InvalidMarketParameterError} when the chain id is malformed or negative, or when the collateral list is empty, exceeds {@link MAX_COLLATERALS}, contains duplicate tokens, has LLTV outside `[0, WAD]`, has liquidation cursor outside `[0, WAD)`, or computes an invalid maximum liquidation incentive factor.
*/
constructor(params: IMarketParams);
/**
* Returns market params from a params object or hydrated market.
*
* Use at boundaries that accept either a standalone market config or a full
* market object. Existing `MarketParams` instances are returned as-is.
*
* @param market - Market params or hydrated market.
* @returns Market params instance.
* @example
* ```ts
* import { MarketParams } from "@morpho-org/midnight-sdk";
*
* const params = MarketParams.from({
* chainId: 8453,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000007000",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000008000",
* },
* ],
* maturity: 54_000n,
* rcfThreshold: 0n,
* enterGate: "0x0000000000000000000000000000000000000000",
* liquidatorGate: "0x0000000000000000000000000000000000000000",
* });
* console.log(params.loanToken);
* ```
*/
static from(market: MarketInput): MarketParams;
}
/**
* Plain input accepted by {@link Market}.
*
* @example
* ```ts
* import type { IMarket } from "@morpho-org/midnight-sdk";
*
* const market: IMarket = {
* params: {
* chainId: 31337,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000000001",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000000002",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000000003",
* },
* ],
* maturity: 1n,
* rcfThreshold: 0n,
* enterGate: "0x0000000000000000000000000000000000000000",
* liquidatorGate: "0x0000000000000000000000000000000000000000",
* },
* totalUnits: 0n,
* lossFactor: 0n,
* withdrawable: 0n,
* continuousFeeCredit: 0n,
* settlementFeeCbps: [0, 0, 0, 0, 0, 0, 0],
* continuousFee: 0,
* tickSpacing: 4,
* };
* ```
*/
export interface IMarket {
/** Immutable market configuration. */
readonly params: IMarketParams;
/** Total market units. */
readonly totalUnits: BigIntish;
/** Current loss factor. */
readonly lossFactor: BigIntish;
/** Withdrawable assets. */
readonly withdrawable: BigIntish;
/** Continuous-fee credit. */
readonly continuousFeeCredit: BigIntish;
/** Seven settlement-fee cbp buckets. */
readonly settlementFeeCbps: SettlementFeeCbps;
/** Continuous fee per second. */
readonly continuousFee: number;
/** Market tick spacing. */
readonly tickSpacing: number;
}
/**
* Plain market params or hydrated market object accepted by market helpers.
*
* @example
* ```ts
* import type { MarketInput } from "@morpho-org/midnight-sdk";
*
* const market: MarketInput = {
* chainId: 8453,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000007000",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000008000",
* },
* ],
* maturity: 54_000n,
* rcfThreshold: 0n,
* enterGate: "0x0000000000000000000000000000000000000000",
* liquidatorGate: "0x0000000000000000000000000000000000000000",
* };
* console.log(market.loanToken);
* ```
*/
export type MarketInput = IMarketParams | IMarket;
/**
* Hydrated Midnight market configuration plus state.
*
* @example
* ```ts
* import { registerCustomAddresses } from "@morpho-org/morpho-ts";
* import { Market } from "@morpho-org/midnight-sdk";
*
* registerCustomAddresses({
* addresses: {
* 31337: {
* blue: "0x0000000000000000000000000000000000000001",
* adaptiveCurveIrm: "0x0000000000000000000000000000000000000004",
* midnight: "0x0000000000000000000000000000000000001000",
* },
* },
* });
*
* const market = new Market({
* params: {
* chainId: 31337,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000007000",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000008000",
* },
* ],
* maturity: 54_000n,
* rcfThreshold: 0n,
* enterGate: "0x0000000000000000000000000000000000000000",
* liquidatorGate: "0x0000000000000000000000000000000000000000",
* },
* totalUnits: 1_000n,
* lossFactor: 0n,
* withdrawable: 500n,
* continuousFeeCredit: 0n,
* settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
* continuousFee: 10,
* tickSpacing: 4,
* });
* console.log(market.timeToMaturity(0n));
* ```
*/
export declare class Market {
/** Market id. */
readonly id: Hash;
/** EIP-155 chain id captured in the market struct. */
readonly chainId: bigint;
/** Immutable market configuration. */
readonly params: MarketParams;
/** Total market units. */
readonly totalUnits: bigint;
/** Current loss factor. */
readonly lossFactor: bigint;
/** Withdrawable assets. */
readonly withdrawable: bigint;
/** Continuous-fee credit. */
readonly continuousFeeCredit: bigint;
/** Seven settlement-fee cbp buckets. */
readonly settlementFeeCbps: SettlementFeeCbps;
/** Continuous fee per second. */
readonly continuousFee: number;
/** Market tick spacing. */
readonly tickSpacing: number;
constructor(market: IMarket);
/**
* Returns a hydrated market instance from class or plain input.
*
* Existing `Market` instances are returned as-is. Plain hydrated market objects
* are normalized through the constructor so nested params and bigint fields use
* the SDK's canonical shapes.
*
* @param market - Hydrated market class or plain market input.
* @returns Hydrated market instance.
* @throws {InvalidMarketParameterError} when nested market params are invalid.
* @example
* ```ts
* import { Market } from "@morpho-org/midnight-sdk";
*
* const market = Market.from({
* params: {
* chainId: 8453,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000007000",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000008000",
* },
* ],
* maturity: 54_000n,
* rcfThreshold: 0n,
* enterGate: "0x0000000000000000000000000000000000000000",
* liquidatorGate: "0x0000000000000000000000000000000000000000",
* },
* totalUnits: 1_000n,
* lossFactor: 0n,
* withdrawable: 500n,
* continuousFeeCredit: 0n,
* settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
* continuousFee: 10,
* tickSpacing: 4,
* });
* console.log(market.id);
* ```
*/
static from(market: IMarket | Market): Market;
/**
* Returns the non-negative time remaining before maturity.
*
* @param timestamp - Timestamp to compare.
* @returns Seconds until maturity, floored to zero.
* @throws {NegativeValueError} when `timestamp` is negative.
* @example
* ```ts
* import { registerCustomAddresses } from "@morpho-org/morpho-ts";
* import { Market } from "@morpho-org/midnight-sdk";
*
* registerCustomAddresses({
* addresses: {
* 31337: {
* blue: "0x0000000000000000000000000000000000000001",
* adaptiveCurveIrm: "0x0000000000000000000000000000000000000004",
* midnight: "0x0000000000000000000000000000000000001000",
* },
* },
* });
*
* const market = new Market({
* params: {
* chainId: 31337,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000007000",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000008000",
* },
* ],
* maturity: 54_000n,
* rcfThreshold: 0n,
* enterGate: "0x0000000000000000000000000000000000000000",
* liquidatorGate: "0x0000000000000000000000000000000000000000",
* },
* totalUnits: 1_000n,
* lossFactor: 0n,
* withdrawable: 500n,
* continuousFeeCredit: 0n,
* settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
* continuousFee: 10,
* tickSpacing: 4,
* });
* console.log(market.timeToMaturity(0n));
* ```
*/
timeToMaturity(timestamp: BigIntish): bigint;
/**
* Computes the settlement fee for a time to maturity from the hydrated market state.
*
* @param timeToMaturity - Seconds until maturity.
* @returns WAD-scaled settlement fee.
* @throws {NegativeValueError} when `timeToMaturity` is negative.
* @example
* ```ts
* import { registerCustomAddresses } from "@morpho-org/morpho-ts";
* import { Market } from "@morpho-org/midnight-sdk";
*
* registerCustomAddresses({
* addresses: {
* 31337: {
* blue: "0x0000000000000000000000000000000000000001",
* adaptiveCurveIrm: "0x0000000000000000000000000000000000000004",
* midnight: "0x0000000000000000000000000000000000001000",
* },
* },
* });
*
* const market = new Market({
* params: {
* chainId: 31337,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000007000",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000008000",
* },
* ],
* maturity: 54_000n,
* rcfThreshold: 0n,
* enterGate: "0x0000000000000000000000000000000000000000",
* liquidatorGate: "0x0000000000000000000000000000000000000000",
* },
* totalUnits: 1_000n,
* lossFactor: 0n,
* withdrawable: 500n,
* continuousFeeCredit: 0n,
* settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
* continuousFee: 10,
* tickSpacing: 4,
* });
* console.log(market.getSettlementFee(0n));
* ```
*/
getSettlementFee(timeToMaturity: BigIntish): bigint;
/**
* Returns collateral by numeric index.
*
* @param index - Collateral index.
* @returns Collateral entry for the configured index.
* @throws {UnknownCollateralIndexError} when the index is not configured.
* @example
* ```ts
* import { registerCustomAddresses } from "@morpho-org/morpho-ts";
* import { Market } from "@morpho-org/midnight-sdk";
*
* registerCustomAddresses({
* addresses: {
* 31337: {
* blue: "0x0000000000000000000000000000000000000001",
* adaptiveCurveIrm: "0x0000000000000000000000000000000000000004",
* midnight: "0x0000000000000000000000000000000000001000",
* },
* },
* });
*
* const market = new Market({
* params: {
* chainId: 31337,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000007000",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000008000",
* },
* ],
* maturity: 54_000n,
* rcfThreshold: 0n,
* enterGate: "0x0000000000000000000000000000000000000000",
* liquidatorGate: "0x0000000000000000000000000000000000000000",
* },
* totalUnits: 1_000n,
* lossFactor: 0n,
* withdrawable: 500n,
* continuousFeeCredit: 0n,
* settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
* continuousFee: 10,
* tickSpacing: 4,
* });
* const collateral = market.getCollateralByIndex(0);
* console.log(collateral.token);
* ```
*/
getCollateralByIndex(index: BigIntish): CollateralParams;
/**
* Returns the configured collateral index for a token.
*
* @param token - Collateral token address.
* @returns Collateral index, or undefined when the token is not configured.
* @example
* ```ts
* import { registerCustomAddresses } from "@morpho-org/morpho-ts";
* import { Market } from "@morpho-org/midnight-sdk";
*
* registerCustomAddresses({
* addresses: {
* 31337: {
* blue: "0x0000000000000000000000000000000000000001",
* adaptiveCurveIrm: "0x0000000000000000000000000000000000000004",
* midnight: "0x0000000000000000000000000000000000001000",
* },
* },
* });
*
* const collateralToken = "0x0000000000000000000000000000000000007000";
* const market = new Market({
* params: {
* chainId: 31337,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: collateralToken,
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000008000",
* },
* ],
* maturity: 54_000n,
* rcfThreshold: 0n,
* enterGate: "0x0000000000000000000000000000000000000000",
* liquidatorGate: "0x0000000000000000000000000000000000000000",
* },
* totalUnits: 1_000n,
* lossFactor: 0n,
* withdrawable: 500n,
* continuousFeeCredit: 0n,
* settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
* continuousFee: 10,
* tickSpacing: 4,
* });
* const index = market.getCollateralIndexByToken(collateralToken);
* console.log(index);
* ```
*/
getCollateralIndexByToken(token: Address): number | undefined;
/**
* Returns collateral params by token address.
*
* @param token - Collateral token address.
* @returns Collateral params, or undefined when the token is not configured.
* @example
* ```ts
* import { registerCustomAddresses } from "@morpho-org/morpho-ts";
* import { Market } from "@morpho-org/midnight-sdk";
*
* registerCustomAddresses({
* addresses: {
* 31337: {
* blue: "0x0000000000000000000000000000000000000001",
* adaptiveCurveIrm: "0x0000000000000000000000000000000000000004",
* midnight: "0x0000000000000000000000000000000000001000",
* },
* },
* });
*
* const collateralToken = "0x0000000000000000000000000000000000007000";
* const market = new Market({
* params: {
* chainId: 31337,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: collateralToken,
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000008000",
* },
* ],
* maturity: 54_000n,
* rcfThreshold: 0n,
* enterGate: "0x0000000000000000000000000000000000000000",
* liquidatorGate: "0x0000000000000000000000000000000000000000",
* },
* totalUnits: 1_000n,
* lossFactor: 0n,
* withdrawable: 500n,
* continuousFeeCredit: 0n,
* settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
* continuousFee: 10,
* tickSpacing: 4,
* });
* const params = market.getCollateralParamsByToken(collateralToken);
* console.log(params?.lltv);
* ```
*/
getCollateralParamsByToken(token: Address): CollateralParams | undefined;
}