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.

594 lines (593 loc) • 21.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InvalidMidnightApiQuoteTargetError = exports.InvalidMidnightApiResponseError = exports.MidnightApiError = exports.InvalidEcrecoverSignatureVError = exports.InvalidEcrecoverSignatureError = exports.InvalidTypedDataSignatureError = exports.MidnightMempoolValidationError = exports.ChainIdMismatchError = exports.RatifierV1TakerNotAllowedError = exports.InvalidRatifierV1AddressError = exports.InvalidRateRatifierV1TimeError = exports.InvalidRateRatifierV1TickError = exports.RateRatifierV1BoundOverflowError = exports.InvalidRateRatifierV1RateError = exports.InvalidTreeError = exports.InvalidTreeHeightError = exports.PayloadDecodeError = exports.InvalidOfferParameterError = exports.UnknownCollateralIndexError = exports.InvalidMarketParameterError = exports.InvalidPositionAccrualStateError = exports.InvalidPositionLossFactorError = exports.InvalidPositionAccrualTimestampError = exports.SettlementFeeExceedsPriceError = exports.InvalidTickSpacingError = exports.PriceGreaterThanOneError = exports.TickOutOfRangeError = exports.InvalidOfferGroupError = void 0; /** * Thrown when a Midnight offer group violates protocol-level group mechanics. * * @example * ```ts * import { InvalidOfferGroupError } from "@morpho-org/midnight-sdk"; * * throw new InvalidOfferGroupError("All offers in a group must use the same maker."); * ``` */ class InvalidOfferGroupError extends Error { constructor(reason) { super(`Invalid Midnight offer group. ${reason}`); this.name = "InvalidOfferGroupError"; } } exports.InvalidOfferGroupError = InvalidOfferGroupError; /** * Thrown when a tick exceeds the deployed TickLib range. * * @example * ```ts * import { TickOutOfRangeError } from "@morpho-org/midnight-sdk"; * * const error = new TickOutOfRangeError(7000n, 6744n); * console.log(error.message); * ``` */ class TickOutOfRangeError extends Error { constructor(tick, maxTick) { super(`Tick "${tick}" exceeds maximum tick "${maxTick}".`); this.name = "TickOutOfRangeError"; } } exports.TickOutOfRangeError = TickOutOfRangeError; /** * Thrown when a price is above the maximum WAD price accepted by TickLib. * * @example * ```ts * import { PriceGreaterThanOneError } from "@morpho-org/midnight-sdk"; * * throw new PriceGreaterThanOneError(2n * 10n ** 18n); * ``` */ class PriceGreaterThanOneError extends Error { constructor(price) { super(`Price "${price}" is greater than WAD.`); this.name = "PriceGreaterThanOneError"; } } exports.PriceGreaterThanOneError = PriceGreaterThanOneError; /** * Thrown when tick spacing is invalid or a tick is not aligned to it. * * @example * ```ts * import { InvalidTickSpacingError } from "@morpho-org/midnight-sdk"; * * throw new InvalidTickSpacingError(5n, 4n); * ``` */ class InvalidTickSpacingError extends Error { constructor(tickOrSpacing, spacing) { super(spacing == null ? `Tick spacing "${tickOrSpacing}" is invalid. Use a positive spacing that divides the maximum tick.` : `Tick "${tickOrSpacing}" is not aligned to spacing "${spacing}". Use a tick aligned to the market spacing.`); this.name = "InvalidTickSpacingError"; } } exports.InvalidTickSpacingError = InvalidTickSpacingError; /** * Thrown when a buy offer's settlement fee exceeds its tick price. * * @example * ```ts * import { SettlementFeeExceedsPriceError } from "@morpho-org/midnight-sdk"; * * throw new SettlementFeeExceedsPriceError(10n, 9n); * ``` */ class SettlementFeeExceedsPriceError extends Error { constructor(settlementFee, price) { super(`Settlement fee "${settlementFee}" exceeds offer price "${price}". Use a lower fee or a higher tick.`); this.name = "SettlementFeeExceedsPriceError"; } } exports.SettlementFeeExceedsPriceError = SettlementFeeExceedsPriceError; /** * Thrown when a local position accrual is requested for an impossible timestamp. * * @example * ```ts * import { InvalidPositionAccrualTimestampError } from "@morpho-org/midnight-sdk"; * * throw new InvalidPositionAccrualTimestampError(1n, 2n); * ``` */ class InvalidPositionAccrualTimestampError extends Error { constructor(timestamp, lastAccrual) { super(`Position accrual timestamp "${timestamp}" is before last accrual "${lastAccrual}". Use a timestamp greater than or equal to last accrual.`); this.name = "InvalidPositionAccrualTimestampError"; } } exports.InvalidPositionAccrualTimestampError = InvalidPositionAccrualTimestampError; /** * Thrown when a local position accrual sees a market loss factor older than the position. * * @example * ```ts * import { InvalidPositionLossFactorError } from "@morpho-org/midnight-sdk"; * * throw new InvalidPositionLossFactorError(1n, 2n); * ``` */ class InvalidPositionLossFactorError extends Error { constructor(marketLossFactor, positionLossFactor) { super(`Market loss factor "${marketLossFactor}" is below position loss factor "${positionLossFactor}". Fetch a consistent market and position at the same block.`); this.name = "InvalidPositionLossFactorError"; } } exports.InvalidPositionLossFactorError = InvalidPositionLossFactorError; /** * Thrown when local position accrual inputs violate Midnight accounting invariants. * * @example * ```ts * import { InvalidPositionAccrualStateError } from "@morpho-org/midnight-sdk"; * * throw new InvalidPositionAccrualStateError("Pending fee exceeds credit."); * ``` */ class InvalidPositionAccrualStateError extends Error { constructor(reason) { super(`Invalid Midnight position accrual state. ${reason}`); this.name = "InvalidPositionAccrualStateError"; } } exports.InvalidPositionAccrualStateError = InvalidPositionAccrualStateError; /** * Thrown when a market parameter cannot satisfy Midnight protocol rules. * * @example * ```ts * import { InvalidMarketParameterError } from "@morpho-org/midnight-sdk"; * * throw new InvalidMarketParameterError({ * parameter: "collateralParams", * value: 0, * instruction: "Provide at least one collateral.", * }); * ``` * @param params.parameter - Invalid market parameter name. * @param params.value - Invalid market parameter value. * @param params.instruction - Optional instruction appended to the error message. * @param params.cause - Optional wrapped cause. */ class InvalidMarketParameterError extends Error { /** Invalid market parameter name. */ parameter; /** Invalid market parameter value. */ value; constructor(params) { super(`Invalid market parameter "${params.parameter}" with value "${String(params.value)}".${params.instruction == null ? "" : ` ${params.instruction}`}`, params.cause === undefined ? undefined : { cause: params.cause }); this.name = "InvalidMarketParameterError"; this.parameter = params.parameter; this.value = params.value; } } exports.InvalidMarketParameterError = InvalidMarketParameterError; /** * Thrown when a Midnight market collateral index is not configured. * * @example * ```ts * import { UnknownCollateralIndexError } from "@morpho-org/midnight-sdk"; * * throw new UnknownCollateralIndexError({ * market: "0x1111111111111111111111111111111111111111111111111111111111111111", * collateralIndex: 2n, * }); * ``` * @param params.market - Market id whose collateral list was queried. * @param params.collateralIndex - Missing collateral index. */ class UnknownCollateralIndexError extends Error { /** Market id whose collateral list was queried. */ market; /** Missing collateral index. */ collateralIndex; constructor(params) { super(`Midnight market "${params.market}" has no collateral at index "${params.collateralIndex}". Use a configured collateral index.`); this.name = "UnknownCollateralIndexError"; this.market = params.market; this.collateralIndex = params.collateralIndex; } } exports.UnknownCollateralIndexError = UnknownCollateralIndexError; /** * Thrown when an offer builder receives a parameter that cannot satisfy Midnight protocol rules. * * @example * ```ts * import { InvalidOfferParameterError } from "@morpho-org/midnight-sdk"; * * throw new InvalidOfferParameterError({ parameter: "tick", value: 5821n }); * ``` * @param params.parameter - Invalid offer parameter name. * @param params.value - Invalid offer parameter value. * @param params.instruction - Optional instruction appended to the error message. * @param params.cause - Optional wrapped cause. */ class InvalidOfferParameterError extends Error { /** Invalid offer parameter name. */ parameter; /** Invalid offer parameter value. */ value; constructor(params) { super(`Invalid offer parameter "${params.parameter}" with value "${String(params.value)}".${params.instruction == null ? "" : ` ${params.instruction}`}`, params.cause === undefined ? undefined : { cause: params.cause }); this.name = "InvalidOfferParameterError"; this.parameter = params.parameter; this.value = params.value; } } exports.InvalidOfferParameterError = InvalidOfferParameterError; /** * Thrown when Midnight mempool payload bytes cannot be encoded or decoded. * * @example * ```ts * import { PayloadDecodeError } from "@morpho-org/midnight-sdk"; * * throw new PayloadDecodeError("payload too short for header"); * ``` */ class PayloadDecodeError extends Error { /** Machine-readable decode failure reason without the class message prefix. */ reason; constructor(reason, cause) { super(`Failed to decode payload: ${reason}`, cause ? { cause } : undefined); this.name = "PayloadDecodeError"; this.reason = reason; } } exports.PayloadDecodeError = PayloadDecodeError; /** * Thrown when a tree height exceeds the ratifier typehash table. * * @example * ```ts * import { InvalidTreeHeightError } from "@morpho-org/midnight-sdk"; * * throw new InvalidTreeHeightError(21); * ``` */ class InvalidTreeHeightError extends Error { constructor(height) { super(`Tree height "${height}" is not supported.`); this.name = "InvalidTreeHeightError"; } } exports.InvalidTreeHeightError = InvalidTreeHeightError; /** * Thrown when a tree cannot be represented by the ratifiers. * * @example * ```ts * import { InvalidTreeError } from "@morpho-org/midnight-sdk"; * * throw new InvalidTreeError("Tree must not be empty."); * ``` */ class InvalidTreeError extends Error { constructor(message) { super(message); this.name = "InvalidTreeError"; } } exports.InvalidTreeError = InvalidTreeError; /** * Thrown when a RateRatifierV1 leaf receives a negative rate. * * @example * ```ts * import { InvalidRateRatifierV1RateError } from "@morpho-org/midnight-sdk"; * * throw new InvalidRateRatifierV1RateError(-1n); * ``` */ class InvalidRateRatifierV1RateError extends Error { /** Negative rate that was rejected. */ rate; constructor(rate) { super(`Rate "${rate}" is negative. Use a non-negative WAD-scaled rate.`); this.name = "InvalidRateRatifierV1RateError"; this.rate = rate; } } exports.InvalidRateRatifierV1RateError = InvalidRateRatifierV1RateError; /** * Thrown when RateRatifierV1 price-bound arithmetic overflows uint256. * * @example * ```ts * import { RateRatifierV1BoundOverflowError } from "@morpho-org/midnight-sdk"; * * throw new RateRatifierV1BoundOverflowError(2n ** 255n, 10n); * ``` */ class RateRatifierV1BoundOverflowError extends Error { /** Rate whose bound arithmetic overflowed. */ rate; /** Time to maturity used in the overflowing bound. */ timeToMaturity; constructor(rate, timeToMaturity) { super(`Rate bound arithmetic overflows uint256 for rate "${rate}" and timeToMaturity "${timeToMaturity}". Lower the rate or time to maturity.`); this.name = "RateRatifierV1BoundOverflowError"; this.rate = rate; this.timeToMaturity = timeToMaturity; } } exports.RateRatifierV1BoundOverflowError = RateRatifierV1BoundOverflowError; /** * Thrown when a RateRatifierV1 leaf offer commits a nominal tick below the * router's minimum takeable tick. * * @example * ```ts * import { InvalidRateRatifierV1TickError } from "@morpho-org/midnight-sdk"; * * throw new InvalidRateRatifierV1TickError(0n, 3372n); * ``` */ class InvalidRateRatifierV1TickError extends Error { /** Nominal tick that was rejected. */ tick; /** Minimum accepted tick. */ minTick; constructor(tick, minTick) { super(`Offer tick "${tick}" is below the minimum "${minTick}" (price >= 0.5 WAD). RateRatifierV1 prices from \`rate\`, but the router still validates the committed tick; set a nominal tick of at least "${minTick}".`); this.name = "InvalidRateRatifierV1TickError"; this.tick = tick; this.minTick = minTick; } } exports.InvalidRateRatifierV1TickError = InvalidRateRatifierV1TickError; /** * Thrown when a RateRatifierV1 time parameter is negative. * * @example * ```ts * import { InvalidRateRatifierV1TimeError } from "@morpho-org/midnight-sdk"; * * throw new InvalidRateRatifierV1TimeError(-1n, "timestamp"); * ``` */ class InvalidRateRatifierV1TimeError extends Error { /** Negative time value that was rejected. */ value; /** Parameter that carried the negative value. */ field; constructor(value, field) { super(`${field} "${value}" is negative. Use a non-negative number of seconds.`); this.name = "InvalidRateRatifierV1TimeError"; this.value = value; this.field = field; } } exports.InvalidRateRatifierV1TimeError = InvalidRateRatifierV1TimeError; /** * Thrown when a V1 ratifier address is the zero address. * * @example * ```ts * import { InvalidRatifierV1AddressError } from "@morpho-org/midnight-sdk"; * * throw new InvalidRatifierV1AddressError( * "0x0000000000000000000000000000000000000000", * ); * ``` */ class InvalidRatifierV1AddressError extends Error { /** Zero ratifier address that was rejected. */ ratifier; constructor(ratifier) { super(`Ratifier address "${ratifier}" is the zero address. Use a deployed PriceRatifierV1 or RateRatifierV1 address.`); this.name = "InvalidRatifierV1AddressError"; this.ratifier = ratifier; } } exports.InvalidRatifierV1AddressError = InvalidRatifierV1AddressError; /** * Thrown when a taker is not the allowed taker of a V1 ratifier leaf. * * @example * ```ts * import { RatifierV1TakerNotAllowedError } from "@morpho-org/midnight-sdk"; * * throw new RatifierV1TakerNotAllowedError({ * taker: "0x0000000000000000000000000000000000000001", * allowedTaker: "0x0000000000000000000000000000000000000002", * }); * ``` */ class RatifierV1TakerNotAllowedError extends Error { /** Taker that attempted to take the offer. */ taker; /** Taker allowed by the ratified leaf. */ allowedTaker; constructor(params) { super(`Taker "${params.taker}" is not the allowed taker "${params.allowedTaker}". Use the allowed taker or an offer without a taker restriction.`); this.name = "RatifierV1TakerNotAllowedError"; this.taker = params.taker; this.allowedTaker = params.allowedTaker; } } exports.RatifierV1TakerNotAllowedError = RatifierV1TakerNotAllowedError; /** * Thrown when a viem client's chain id does not match the chain id required by a signing flow. * * @example * ```ts * import { ChainIdMismatchError } from "@morpho-org/midnight-sdk"; * * throw new ChainIdMismatchError(1, 8453n); * ``` */ class ChainIdMismatchError extends Error { /** Chain id reported by the viem client, when configured. */ clientChainId; /** Chain id required by the data being signed. */ expectedChainId; constructor(clientChainId, expectedChainId) { super(`Client chain id "${clientChainId}" does not match expected chain id "${expectedChainId}". Switch the viem client to the offer market chain before signing.`); this.name = "ChainIdMismatchError"; this.clientChainId = clientChainId; this.expectedChainId = expectedChainId; } } exports.ChainIdMismatchError = ChainIdMismatchError; /** * Thrown when the Midnight API rejects a tree during mempool policy validation. * * @example * ```ts * import { MidnightMempoolValidationError } from "@morpho-org/midnight-sdk"; * * throw new MidnightMempoolValidationError([{ rule: "tick_spacing" }]); * ``` */ class MidnightMempoolValidationError extends Error { /** Validation issues returned by the Midnight API. */ issues; constructor(issues) { super(`Midnight mempool validation failed with "${issues.length}" issue${issues.length === 1 ? "" : "s"}. Adjust the offer parameters and try again.`); this.name = "MidnightMempoolValidationError"; this.issues = issues; } } exports.MidnightMempoolValidationError = MidnightMempoolValidationError; /** * Thrown when a typed-data signature does not recover to the expected signer. * * @example * ```ts * import { InvalidTypedDataSignatureError } from "@morpho-org/midnight-sdk"; * * throw new InvalidTypedDataSignatureError( * "0x0000000000000000000000000000000000000001", * ); * ``` */ class InvalidTypedDataSignatureError extends Error { /** Signer address the signature was expected to recover. */ signer; constructor(signer, cause) { super(`Typed-data signature does not recover to signer "${signer}". Check the wallet account or signing transport.`, cause === undefined ? undefined : { cause }); this.name = "InvalidTypedDataSignatureError"; this.signer = signer; } } exports.InvalidTypedDataSignatureError = InvalidTypedDataSignatureError; /** * Thrown when an Ecrecover signature cannot be parsed or recovered. * * @example * ```ts * import { InvalidEcrecoverSignatureError } from "@morpho-org/midnight-sdk"; * * const error = new InvalidEcrecoverSignatureError(new TypeError("Invalid recovery input")); * console.log(error.cause); * ``` */ class InvalidEcrecoverSignatureError extends Error { constructor(cause) { super("Ecrecover signature is malformed. Provide a valid ECDSA signature and try again.", { cause }); this.name = "InvalidEcrecoverSignatureError"; } } exports.InvalidEcrecoverSignatureError = InvalidEcrecoverSignatureError; /** * Thrown when decoded Ecrecover ratifier data contains a non-canonical signature `v` value. * * @example * ```ts * import { InvalidEcrecoverSignatureVError } from "@morpho-org/midnight-sdk"; * * throw new InvalidEcrecoverSignatureVError(29); * ``` */ class InvalidEcrecoverSignatureVError extends Error { /** Invalid ECDSA recovery id. */ v; constructor(v) { super(`Ecrecover signature v "${v}" is invalid. Use canonical Ethereum v 27 or 28.`); this.name = "InvalidEcrecoverSignatureVError"; this.v = v; } } exports.InvalidEcrecoverSignatureVError = InvalidEcrecoverSignatureVError; /** * Thrown when the Midnight API returns a non-2xx response. * * @example * ```ts * import { MidnightApiError } from "@morpho-org/midnight-sdk/api"; * * throw new MidnightApiError({ * status: 400, * code: "BAD_REQUEST", * message: "Limit must be greater than 0.", * details: [{ field: "limit", issue: "Limit must be greater than 0." }], * requestId: "req-123", * }); * ``` */ class MidnightApiError extends Error { /** HTTP response status from the API. */ status; /** API error code, when the response body includes one. */ code; /** API error details, when the response body includes them. */ details; /** API request id, when the response body includes one. */ requestId; constructor(params) { super(params.message ?? `Midnight API request failed with HTTP status "${params.status}".`, params.cause === undefined ? undefined : { cause: params.cause }); this.name = "MidnightApiError"; this.status = params.status; this.code = params.code; this.details = params.details; this.requestId = params.requestId; } } exports.MidnightApiError = MidnightApiError; /** * Thrown when the Midnight API returns a malformed successful response. * * @example * ```ts * import { InvalidMidnightApiResponseError } from "@morpho-org/midnight-sdk/api"; * * throw new InvalidMidnightApiResponseError("API response is missing data."); * ``` */ class InvalidMidnightApiResponseError extends Error { constructor(message, options) { super(message, options); this.name = "InvalidMidnightApiResponseError"; } } exports.InvalidMidnightApiResponseError = InvalidMidnightApiResponseError; /** * Thrown when a Midnight API quote request does not target exactly one of `units` or `assets`. * * @example * ```ts * import { InvalidMidnightApiQuoteTargetError } from "@morpho-org/midnight-sdk/api"; * * throw new InvalidMidnightApiQuoteTargetError(); * ``` */ class InvalidMidnightApiQuoteTargetError extends Error { constructor(options) { super("Midnight API quote requires exactly one of `units` or `assets`. Provide a single target amount.", options); this.name = "InvalidMidnightApiQuoteTargetError"; } } exports.InvalidMidnightApiQuoteTargetError = InvalidMidnightApiQuoteTargetError;