UNPKG

@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.

590 lines (589 loc) • 22.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Market = exports.MarketParams = void 0; const morpho_ts_1 = require("@morpho-org/morpho-ts"); const constants_js_1 = require("../constants.js"); const errors_js_1 = require("../errors.js"); const MarketUtils_js_1 = require("./MarketUtils.js"); /** * 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); * ``` */ class MarketParams { /** EIP-155 chain id captured in the market struct. */ chainId; /** Core Midnight contract address captured in the market struct. */ midnight; /** Loan token address. */ loanToken; /** Collateral definitions sorted by token. */ collateralParams; /** Market maturity timestamp. */ maturity; /** Recovery close factor threshold. */ rcfThreshold; /** Entry gate address. */ enterGate; /** Liquidator gate address. */ liquidatorGate; /** * 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) { try { this.chainId = BigInt(params.chainId); } catch (cause) { throw new errors_js_1.InvalidMarketParameterError({ parameter: "chainId", value: params.chainId, instruction: "Provide a bigint-compatible EIP-155 chain id.", cause, }); } this.midnight = params.midnight; this.loanToken = params.loanToken; const collateralParams = params.collateralParams.map(MarketUtils_js_1.MarketUtils.toCollateralParams); if (this.chainId < 0n) { throw new errors_js_1.InvalidMarketParameterError({ parameter: "chainId", value: this.chainId, instruction: "Use a non-negative EIP-155 chain id.", }); } if (collateralParams.length === 0) { throw new errors_js_1.InvalidMarketParameterError({ parameter: "collateralParams", value: params.collateralParams.length, instruction: "Provide at least one collateral.", }); } if (collateralParams.length > Number(constants_js_1.MAX_COLLATERALS)) { throw new errors_js_1.InvalidMarketParameterError({ parameter: "collateralParams", value: collateralParams.length, instruction: `Provide at most ${constants_js_1.MAX_COLLATERALS} collaterals.`, }); } const seenCollateralTokens = new Set(); for (const collateral of collateralParams) { if (collateral.lltv < 0n || collateral.lltv > morpho_ts_1.MathLib.WAD) { throw new errors_js_1.InvalidMarketParameterError({ parameter: "collateralParams.lltv", value: collateral.lltv, instruction: "Use an LLTV between 0 and WAD.", }); } if (collateral.liquidationCursor < 0n || collateral.liquidationCursor >= morpho_ts_1.MathLib.WAD) { throw new errors_js_1.InvalidMarketParameterError({ parameter: "collateralParams.liquidationCursor", value: collateral.liquidationCursor, instruction: "Use a liquidation cursor between 0 and WAD, exclusive of WAD.", }); } const maxLif = morpho_ts_1.MathLib.mulDivDown(morpho_ts_1.MathLib.WAD, morpho_ts_1.MathLib.WAD, morpho_ts_1.MathLib.WAD - morpho_ts_1.MathLib.mulDivDown(collateral.liquidationCursor, morpho_ts_1.MathLib.WAD - collateral.lltv, morpho_ts_1.MathLib.WAD)); if (maxLif > 2n * morpho_ts_1.MathLib.WAD) { throw new errors_js_1.InvalidMarketParameterError({ parameter: "collateralParams.liquidationCursor", value: collateral.liquidationCursor, instruction: "Use a liquidation cursor whose computed maximum LIF is at most 2 WAD.", }); } if (collateral.lltv !== morpho_ts_1.MathLib.WAD && collateral.lltv * maxLif > 999000000000000000n * morpho_ts_1.MathLib.WAD) { throw new errors_js_1.InvalidMarketParameterError({ parameter: "collateralParams.liquidationCursor", value: collateral.liquidationCursor, instruction: "Use an LLTV and liquidation cursor whose computed maximum LIF product satisfies the protocol bound.", }); } const token = collateral.token.toLowerCase(); if (seenCollateralTokens.has(token)) { throw new errors_js_1.InvalidMarketParameterError({ parameter: "collateralParams", value: collateral.token, instruction: "Use each collateral token at most once.", }); } seenCollateralTokens.add(token); } this.collateralParams = collateralParams.sort(MarketUtils_js_1.MarketUtils.compareCollateralTokens); this.maturity = BigInt(params.maturity); this.rcfThreshold = BigInt(params.rcfThreshold); this.enterGate = params.enterGate; this.liquidatorGate = params.liquidatorGate; } /** * 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) { if ("params" in market) { return MarketParams.from(market.params); } return market instanceof MarketParams ? market : new MarketParams(market); } } exports.MarketParams = MarketParams; /** * 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)); * ``` */ class Market { /** Market id. */ id; /** EIP-155 chain id captured in the market struct. */ chainId; /** Immutable market configuration. */ params; /** Total market units. */ totalUnits; /** Current loss factor. */ lossFactor; /** Withdrawable assets. */ withdrawable; /** Continuous-fee credit. */ continuousFeeCredit; /** Seven settlement-fee cbp buckets. */ settlementFeeCbps; /** Continuous fee per second. */ continuousFee; /** Market tick spacing. */ tickSpacing; constructor(market) { this.params = MarketParams.from(market.params); this.chainId = this.params.chainId; this.id = MarketUtils_js_1.MarketUtils.toId(this.params); this.totalUnits = BigInt(market.totalUnits); this.lossFactor = BigInt(market.lossFactor); this.withdrawable = BigInt(market.withdrawable); this.continuousFeeCredit = BigInt(market.continuousFeeCredit); this.settlementFeeCbps = [...market.settlementFeeCbps]; this.continuousFee = market.continuousFee; this.tickSpacing = market.tickSpacing; } /** * 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) { return market instanceof Market ? market : new 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) { const normalizedTimestamp = BigInt(timestamp); (0, morpho_ts_1.assertNonNegative)("timestamp", normalizedTimestamp); return normalizedTimestamp >= this.params.maturity ? 0n : this.params.maturity - normalizedTimestamp; } /** * 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) { return MarketUtils_js_1.MarketUtils.getSettlementFee({ settlementFeeCbps: this.settlementFeeCbps, timeToMaturity, }); } /** * 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) { return MarketUtils_js_1.MarketUtils.getCollateralByIndex(this, index); } /** * 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) { const expectedToken = token.toLowerCase(); const index = this.params.collateralParams.findIndex((params) => params.token.toLowerCase() === expectedToken); return index === -1 ? undefined : index; } /** * 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) { const index = this.getCollateralIndexByToken(token); return index == null ? undefined : this.params.collateralParams[index]; } } exports.Market = Market;