@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.
224 lines (223 loc) • 8.54 kB
TypeScript
import { type BigIntish } from "@morpho-org/morpho-ts";
import { type Address } from "viem";
import { type CollateralParams, type ICollateralParams, type MarketInput, MarketParams, type SettlementFeeCbps } from "./Market.js";
type CollateralParamsInput = ICollateralParams | CollateralParams;
/**
* Domain helpers for Midnight markets.
*
* @example
* ```ts
* import { MarketUtils } from "@morpho-org/midnight-sdk";
*
* console.log(typeof MarketUtils.hash);
* ```
*/
export declare namespace MarketUtils {
/**
* @internal Orders collaterals by token address, case-insensitively, matching the protocol's
* canonical `collateralIndex` order used by `MarketParams` and `MarketUtils.hash`.
*
* @param a - First collateral.
* @param b - Second collateral.
* @returns Negative when `a` sorts before `b`, positive when after, `0` when tokens are equal.
*/
function compareCollateralTokens(a: {
readonly token: Address;
}, b: {
readonly token: Address;
}): -1 | 0 | 1;
/**
* Converts collateral params from a plain input or ABI tuple.
*
* @param params.token - Collateral token address.
* @param params.lltv - WAD-scaled liquidation loan-to-value.
* @param params.liquidationCursor - WAD-scaled liquidation cursor.
* @param params.oracle - Oracle address used to price this collateral.
* @returns Collateral params with bigint fields.
* @example
* ```ts
* import { MarketUtils } from "@morpho-org/midnight-sdk";
*
* const collateral = MarketUtils.toCollateralParams({
* token: "0x0000000000000000000000000000000000000001",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000000002",
* });
* console.log(collateral.liquidationCursor);
* ```
*/
function toCollateralParams(params: CollateralParamsInput): CollateralParams;
/**
* Converts market params into the tuple object expected by viem ABI encoders.
*
* @param market - Market params or hydrated market.
* @returns ABI-compatible market params.
* @example
* ```ts
* import { MarketUtils } from "@morpho-org/midnight-sdk";
*
* const struct = MarketUtils.toStruct({
* 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(struct.maturity);
* ```
*/
function toStruct(market: MarketInput): MarketParams;
/**
* Returns configured collateral by index.
*
* @param market - Market params or hydrated market.
* @param index - Collateral index to look up.
* @returns Collateral entry for the configured index.
* @throws {UnknownCollateralIndexError} when the index is not configured.
* @example
* ```ts
* import { MarketUtils } from "@morpho-org/midnight-sdk";
*
* const collateral = MarketUtils.getCollateralByIndex({
* chainId: 8453,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000000001",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000000002",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000000003",
* },
* ],
* maturity: 1n,
* rcfThreshold: 0n,
* enterGate: "0x0000000000000000000000000000000000000000",
* liquidatorGate: "0x0000000000000000000000000000000000000000",
* }, 0n);
* console.log(collateral.token);
* ```
*/
function getCollateralByIndex(market: MarketInput, index: BigIntish): CollateralParams;
/**
* Returns the liquidation incentive factor for an LLTV and liquidation cursor.
*
* @param input - WAD-scaled LLTV or collateral params carrying an LLTV.
* @param cursor - WAD-scaled liquidation cursor.
* @returns WAD-scaled liquidation incentive factor.
* @example
* ```ts
* import { MarketUtils } from "@morpho-org/midnight-sdk";
*
* const liquidationIncentiveFactor = MarketUtils.getLiquidationIncentiveFactor(
* 770000000000000000n,
* 250000000000000000n,
* );
* console.log(liquidationIncentiveFactor);
* ```
*/
function getLiquidationIncentiveFactor(input: BigIntish | Pick<CollateralParamsInput, "lltv">, cursor: BigIntish): bigint;
/**
* Computes the Midnight settlement fee from market cbp buckets and time to maturity.
*
* This mirrors Midnight `settlementFee`: cbp buckets are scaled by {@link CBP},
* values are linearly interpolated between {@link SETTLEMENT_FEE_BREAKPOINTS},
* and any time to maturity at or above 360 days uses the last bucket.
*
* @param params.settlementFeeCbps - Seven settlement-fee centibip buckets from the market state.
* @param params.timeToMaturity - Seconds until market maturity.
* @returns WAD-scaled settlement fee.
* @throws {NegativeValueError} when `timeToMaturity` is negative.
* @example
* ```ts
* import { MarketUtils } from "@morpho-org/midnight-sdk";
*
* const fee = MarketUtils.getSettlementFee({
* settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
* timeToMaturity: 12n * 60n * 60n,
* });
* console.log(fee);
* ```
*/
function getSettlementFee(params: {
readonly settlementFeeCbps: SettlementFeeCbps;
readonly timeToMaturity: BigIntish;
}): bigint;
/**
* Computes the HashLib market params struct hash.
*
* Hashing canonicalizes non-empty collateral params by token order and
* preserves the protocol-zero empty market used for tree padding. It does
* not validate every market creation rule. Use {@link MarketParams} when
* constructing user-facing market params.
*
* @param market - Market to hash.
* @returns EIP-712 market struct hash.
* @example
* ```ts
* import { MarketUtils } from "@morpho-org/midnight-sdk";
*
* const hash = MarketUtils.hash({
* 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(hash);
* ```
*/
function hash(market: MarketInput): `0x${string}`;
/**
* Computes the Midnight id for a market using `IdLib.toId`.
*
* @param market - Market to hash.
* @returns Market id.
* @example
* ```ts
* import { MarketUtils } from "@morpho-org/midnight-sdk";
*
* const id = MarketUtils.toId({
* 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(id);
* ```
*/
function toId(market: MarketInput): `0x${string}`;
}
export {};