@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.
184 lines (183 loc) • 8.33 kB
TypeScript
import { type BigIntish } from "@morpho-org/morpho-ts";
/** One time-bounded offer leg to convert into a Midnight `Offer.create` input. */
export interface FixedRateOfferChainLeg {
/** Spacing-aligned Midnight tick for this offer. */
readonly tick: bigint;
/** First timestamp at which the offer leg is active. */
readonly startTimestamp: bigint;
/** Last timestamp at which the offer leg is active. */
readonly expiryTimestamp: bigint;
}
/** Parameters shared by lend-only and borrow-only fixed-rate offer-chain builders. */
export interface BuildFixedRateOfferChainParams {
/** Target yearly fixed rate as a decimal number, for example `0.05` for 5%. */
readonly targetRate: number;
/** Tick spacing enforced by the market. */
readonly tickSpacing: BigIntish;
/** Market maturity timestamp in seconds. */
readonly maturityTimestamp: BigIntish;
/** First timestamp the chain should cover. */
readonly chainStartTimestamp: BigIntish;
/** Latest timestamp the chain may cover. */
readonly chainEndTimestamp: BigIntish;
}
/**
* Utilities for building time-bounded Midnight offer chains.
*
* A Midnight offer has one fixed price, so its displayed yearly rate changes
* as maturity approaches. The markets app uses these helpers when a maker wants
* to post, for example, a 5% order for several months: the app builds several
* adjacent offers sharing the same reserve, each with a different tick and time
* window, so the order reviews and renders as a stable 5% order across the
* selected window instead of drifting upward as time passes.
*
* @example
* ```ts
* import { OfferChainUtils } from "@morpho-org/midnight-sdk";
*
* const legs = OfferChainUtils.buildLendFixedRateOfferChain({
* targetRate: 0.05,
* tickSpacing: 4n,
* maturityTimestamp: 1_798_761_600n,
* chainStartTimestamp: 1_767_225_600n,
* chainEndTimestamp: 1_791_153_600n,
* });
* console.log(legs[0]?.tick);
* ```
*/
export declare namespace OfferChainUtils {
/**
* Maximum fraction of the initial time-to-maturity covered by a fixed-rate offer chain.
*
* Use this cap before displaying an expiry picker for a fixed-rate maker
* order. The last part of the maturity window is intentionally excluded
* because tiny time changes cause large displayed-rate drift.
*
* @example
* ```ts
* import { OfferChainUtils } from "@morpho-org/midnight-sdk";
*
* console.log(OfferChainUtils.MAX_EXPIRY_TTM_FRACTION);
* ```
*/
const MAX_EXPIRY_TTM_FRACTION = 0.75;
/**
* Builds borrow-side sell-offer legs that approximate one fixed maker rate over time.
*
* Use before a make-borrow or supply-collateral-and-make-borrow flow when a
* borrower wants one displayed fixed borrow rate across a longer window. Map
* the returned legs to `Offer.create({ buy: false, ... })`, then group and
* submit them through the maker flow. Pass the same market tick spacing to
* `Offer.create` so leg validation uses the chain's grid.
*
* Borrow chains read their target rate at each leg's expiry edge. If a leg
* would need to extend past `chainEndTimestamp`, it is dropped rather than
* clamped so the expiry edge remains the recoverable target-rate edge.
*
* @param params - Fixed-rate offer-chain parameters without a side field.
* @param params.targetRate - Target yearly fixed rate expressed as a decimal number.
* @param params.tickSpacing - Tick spacing enforced by the market.
* @param params.maturityTimestamp - Market maturity timestamp in seconds.
* @param params.chainStartTimestamp - First timestamp the chain should cover.
* @param params.chainEndTimestamp - Latest timestamp the chain may cover.
* @returns Borrow-side offer legs that can be mapped to `Offer.create({ buy: false, ... })`.
* @throws {InvalidOfferParameterError} when an input is invalid or the end timestamp exceeds the supported horizon.
* @example
* ```ts
* import { Offer, OfferChainUtils } from "@morpho-org/midnight-sdk";
*
* const legs = OfferChainUtils.buildBorrowFixedRateOfferChain({
* targetRate: 0.08,
* tickSpacing: market.tickSpacing,
* maturityTimestamp: market.params.maturity,
* chainStartTimestamp: now,
* chainEndTimestamp: expiry,
* });
* const offers = legs.map((leg) =>
* Offer.create({
* ...baseOffer,
* buy: false,
* tick: leg.tick,
* tickSpacing: market.tickSpacing,
* start: leg.startTimestamp,
* expiry: leg.expiryTimestamp,
* }),
* );
* ```
*/
function buildBorrowFixedRateOfferChain(params: BuildFixedRateOfferChainParams): readonly FixedRateOfferChainLeg[];
/**
* Builds lend-side buy-offer legs that approximate one fixed maker rate over time.
*
* Use before a make-lend flow when a lender wants one displayed fixed lend
* rate across a longer window. Map the returned legs to
* `Offer.create({ buy: true, ... })`, then group and submit them through the
* maker flow. Pass the same market tick spacing to `Offer.create` so leg
* validation uses the chain's grid.
*
* Lend chains read their target rate at each leg's start edge. The first leg
* may start before `chainStartTimestamp`; clamping that edge would move the
* recoverable display rate below the maker's target.
*
* @param params - Fixed-rate offer-chain parameters without a side field.
* @param params.targetRate - Target yearly fixed rate expressed as a decimal number.
* @param params.tickSpacing - Tick spacing enforced by the market.
* @param params.maturityTimestamp - Market maturity timestamp in seconds.
* @param params.chainStartTimestamp - First timestamp the chain should cover.
* @param params.chainEndTimestamp - Latest timestamp the chain may cover.
* @returns Lend-side offer legs that can be mapped to `Offer.create({ buy: true, ... })`.
* @throws {InvalidOfferParameterError} when an input is invalid or the end timestamp exceeds the supported horizon.
* @example
* ```ts
* import { Offer, OfferChainUtils } from "@morpho-org/midnight-sdk";
*
* const legs = OfferChainUtils.buildLendFixedRateOfferChain({
* targetRate: 0.05,
* tickSpacing: market.tickSpacing,
* maturityTimestamp: market.params.maturity,
* chainStartTimestamp: now,
* chainEndTimestamp: expiry,
* });
* const offers = legs.map((leg) =>
* Offer.create({
* ...baseOffer,
* buy: true,
* tick: leg.tick,
* tickSpacing: market.tickSpacing,
* start: leg.startTimestamp,
* expiry: leg.expiryTimestamp,
* }),
* );
* ```
*/
function buildLendFixedRateOfferChain(params: BuildFixedRateOfferChainParams): readonly FixedRateOfferChainLeg[];
/**
* Returns the latest supported end timestamp for a fixed-rate offer chain.
*
* Chains intentionally stop before the final part of the maturity window
* because rate sensitivity accelerates near maturity and would require too
* many short-lived offers for a stable maker quote.
* Call this when constraining a make-offer expiry selector, then pass the
* selected timestamp to the lend-only or borrow-only chain builder.
*
* @param params - Maturity and chain-start timestamps.
* @param params.maturityTimestamp - Market maturity timestamp in seconds.
* @param params.chainStartTimestamp - First timestamp the chain should cover.
* @returns Latest accepted chain end timestamp.
* @throws {InvalidOfferParameterError} when a timestamp is invalid.
* @example
* ```ts
* import { OfferChainUtils } from "@morpho-org/midnight-sdk";
*
* const maxExpiry = OfferChainUtils.getMaxFixedRateOfferChainEndTimestamp({
* maturityTimestamp: market.params.maturity,
* chainStartTimestamp: now,
* });
* console.log(maxExpiry);
* ```
*/
function getMaxFixedRateOfferChainEndTimestamp(params: {
readonly maturityTimestamp: BigIntish;
readonly chainStartTimestamp: BigIntish;
}): bigint;
}