@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.
223 lines (222 loc) • 7.46 kB
TypeScript
import { type BigIntish } from "@morpho-org/morpho-ts";
/**
* TypeScript port of Midnight `TickLib`.
*
* @example
* ```ts
* import { TickLib } from "@morpho-org/midnight-sdk";
*
* console.log(TickLib.tickToPrice(6744n));
* ```
*/
export declare namespace TickLib {
/**
* WAD-scaled natural log of one plus Midnight's tick delta.
*
* @example
* ```ts
* import { TickLib } from "@morpho-org/midnight-sdk";
*
* console.log(TickLib.LN_ONE_PLUS_DELTA);
* ```
*/
const LN_ONE_PLUS_DELTA = 4987541511039073n;
/**
* WAD-scaled natural log of two.
*
* @example
* ```ts
* import { TickLib } from "@morpho-org/midnight-sdk";
*
* console.log(TickLib.LN_2);
* ```
*/
const LN_2 = 693147180559945309n;
/**
* Exponent offset used by the Midnight exponential approximation.
*
* @example
* ```ts
* import { TickLib } from "@morpho-org/midnight-sdk";
*
* console.log(TickLib.EXP_OFFSET);
* ```
*/
const EXP_OFFSET = 322611214989459870n;
/**
* Divides with half-down rounding without validating the denominator.
*
* @param x - Dividend.
* @param d - Divisor.
* @returns Quotient rounded half down.
* @example
* ```ts
* import { TickLib } from "@morpho-org/midnight-sdk";
*
* const value = TickLib.divHalfDownUnchecked(5n, 2n);
* console.log(value);
* ```
*/
const divHalfDownUnchecked: (x: bigint, d: bigint) => bigint;
/**
* Asserts that a tick is non-negative and within Midnight's deployed range.
*
* @param tick - Tick to validate.
* @returns Tick as a bigint.
* @throws {NegativeValueError} when `tick` is negative.
* @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
* @example
* ```ts
* import { TickLib } from "@morpho-org/midnight-sdk";
*
* const tick = TickLib.assertTickInRange(100n);
* console.log(tick);
* ```
*/
const assertTickInRange: (tick: BigIntish) => bigint;
/**
* Computes the WAD-scaled exponential approximation used by Midnight ticks.
*
* @param x - WAD-scaled exponent.
* @returns WAD-scaled exponential value.
* @example
* ```ts
* import { TickLib } from "@morpho-org/midnight-sdk";
*
* const value = TickLib.wExp(0n);
* console.log(value);
* ```
*/
const wExp: (x: bigint) => bigint;
/**
* Converts a Midnight tick into a WAD price.
*
* @param tick - Tick in the deployed range.
* @returns WAD price rounded to `PRICE_ROUNDING_STEP`.
* @throws {NegativeValueError} when `tick` is negative.
* @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
* @example
* ```ts
* import { TickLib } from "@morpho-org/midnight-sdk";
*
* const price = TickLib.tickToPrice(6744n);
* console.log(price);
* ```
*/
function tickToPrice(tick: BigIntish): bigint;
/**
* Converts a WAD price into the lowest spacing-aligned tick whose price is at least the input.
*
* @param price - WAD price.
* @param spacing - Tick spacing; defaults to `DEFAULT_TICK_SPACING`.
* @returns Lowest aligned tick with price greater than or equal to `price`.
* @throws {NegativeValueError} when `price` is negative.
* @throws {PriceGreaterThanOneError} when price is above WAD.
* @throws {InvalidTickSpacingError} when spacing is invalid.
* @example
* ```ts
* import { TickLib } from "@morpho-org/midnight-sdk";
*
* const tick = TickLib.priceToTick(500000000000000000n);
* console.log(tick);
* ```
*/
function priceToTick(price: BigIntish, spacing?: BigIntish): bigint;
/**
* Snaps a WAD price to the price of the lowest spacing-aligned tick at or above it.
*
* This is an SDK-only convenience around `priceToTick` and `tickToPrice`.
*
* @param price - WAD price.
* @param spacing - Tick spacing.
* @returns Snapped WAD price.
* @throws {NegativeValueError} when `price` is negative.
* @throws {PriceGreaterThanOneError} when price is above WAD.
* @throws {InvalidTickSpacingError} when spacing is invalid.
* @example
* ```ts
* import { TickLib } from "@morpho-org/midnight-sdk";
*
* const price = TickLib.snapPriceToTick(500000000000000000n, 4n);
* console.log(price);
* ```
*/
function snapPriceToTick(price: BigIntish, spacing?: BigIntish): bigint;
/**
* Converts a WAD fixed rate into a WAD zero-coupon price.
*
* This is an SDK-only rate conversion convenience.
*
* @param rate - WAD fixed rate.
* @returns WAD price rounded down.
* @throws {NegativeValueError} when `rate` is negative.
* @example
* ```ts
* import { TickLib } from "@morpho-org/midnight-sdk";
*
* const price = TickLib.rateToPrice(50000000000000000n);
* console.log(price);
* ```
*/
function rateToPrice(rate: BigIntish): bigint;
/**
* Converts a Midnight tick into a WAD fixed rate.
*
* This is an SDK-only rate conversion convenience.
*
* @param tick - Midnight tick.
* @returns WAD fixed rate rounded up.
* @throws {NegativeValueError} when `tick` is negative.
* @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
* @throws {DivisionByZeroError} when the tick price is zero.
* @example
* ```ts
* import { TickLib } from "@morpho-org/midnight-sdk";
*
* const rate = TickLib.tickToRate(6744n);
* console.log(rate);
* ```
*/
function tickToRate(tick: BigIntish): bigint;
/**
* Converts a Midnight tick into a WAD simple annual percentage rate.
*
* This is an SDK-only rate display convenience. It annualizes the fixed
* period rate from {@link tickToRate} over `timeToMaturity` seconds.
*
* @param tick - Midnight tick.
* @param timeToMaturity - Seconds until the market matures.
* @returns WAD simple APR rounded up.
* @throws {NegativeValueError} when `tick` or `timeToMaturity` is negative.
* @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
* @throws {DivisionByZeroError} when the tick price or `timeToMaturity` is zero.
* @example
* ```ts
* import { TickLib } from "@morpho-org/midnight-sdk";
* import { Time } from "@morpho-org/morpho-ts";
*
* const apr = TickLib.tickToApr(6744n, Time.s.from.y(1n));
* console.log(apr);
* ```
*/
function tickToApr(tick: BigIntish, timeToMaturity: BigIntish): bigint;
/**
* Asserts that a tick is aligned to the provided spacing.
*
* This is an SDK-only tick-domain assertion.
*
* @param tick - Tick to validate.
* @param spacing - Market tick spacing.
* @returns Tick as a bigint.
* @throws {NegativeValueError} when `tick` is negative.
* @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
* @throws {InvalidTickSpacingError} when spacing is invalid or the tick is not aligned.
* @example
* ```ts
* import { TickLib } from "@morpho-org/midnight-sdk";
*
* TickLib.assertTickAlignedToSpacing(100n, 4n);
* ```
*/
function assertTickAlignedToSpacing(tick: BigIntish, spacing?: BigIntish): bigint;
}