@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.
789 lines (788 loc) • 32.3 kB
TypeScript
import { type BigIntish } from "@morpho-org/morpho-ts";
import { type Address, type Hash } from "viem";
import { type BuildOfferParams, type IOffer, Offer, type OfferStruct } from "./Offer.js";
/**
* Object-compatible helpers for Midnight offer construction, grouping, and
* take-side encoding.
*
* Make-side apps usually call {@link Offer.create}, then `Group.create` or
* `Tree.create`. Use these helpers when you need the same validation or ABI
* conversion without depending on class instances, or when converting API data
* into the structs consumed by payload and take encoders.
*
* @example
* ```ts
* import { OfferUtils } from "@morpho-org/midnight-sdk";
*
* console.log(typeof OfferUtils.getOfferExpiry);
* ```
*/
export declare namespace OfferUtils {
/**
* Converts an offer into the tuple object expected by viem ABI encoders.
*
* This is the bridge from SDK/domain objects
* into Merkle leaf hashing, payload items, and take calldata encoding.
*
* The struct carries a materialized group (the generic default when omitted);
* V1 tree builders re-derive that generic default under their scheme.
*
* @param params.offer - Offer class or plain offer input to encode.
* @param params.group - Optional protocol group id override encoded into the ABI offer.
* @returns ABI-compatible offer.
* @example
* ```ts
* import { Offer, OfferUtils } from "@morpho-org/midnight-sdk";
* import { zeroAddress } from "viem";
*
* const offer = Offer.create({
* market: {
* chainId: 8453,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000007000",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000008000",
* },
* ],
* maturity: 54_000n,
* rcfThreshold: 0n,
* enterGate: zeroAddress,
* liquidatorGate: zeroAddress,
* },
* buy: true,
* maker: "0x0000000000000000000000000000000000009000",
* tick: 5_000n,
* expiry: 3_600n,
* ratifier: "0x0000000000000000000000000000000000004000",
* maxUnits: 100n,
* });
* const struct = OfferUtils.toStruct({ offer });
* console.log(struct.tick);
* ```
*/
function toStruct(params: {
readonly offer: IOffer;
readonly group?: Hash;
}): OfferStruct;
/**
* Computes the canonical protocol offer hash for an ABI-compatible offer.
*
* Use when you already have an `OfferStruct`, for example from
* `GroupUtils.toStructs`, `TreeUtils.buildDescriptor`, or decoded payload
* bytes. Use `hash` when you still have a grouped `Offer` or grouped plain
* offer input, and `groupHash` when deriving a group id from the zero-group
* hash.
*
* @param offerStruct - ABI-compatible offer to hash.
* @returns Offer hash.
* @example
* ```ts
* import { Offer, OfferUtils } from "@morpho-org/midnight-sdk";
* import { zeroAddress, zeroHash } from "viem";
*
* const offer = Offer.create({
* market: {
* chainId: 8453,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000007000",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000008000",
* },
* ],
* maturity: 54_000n,
* rcfThreshold: 0n,
* enterGate: zeroAddress,
* liquidatorGate: zeroAddress,
* },
* buy: true,
* maker: "0x0000000000000000000000000000000000009000",
* tick: 5_000n,
* expiry: 3_600n,
* ratifier: "0x0000000000000000000000000000000000004000",
* maxUnits: 100n,
* });
* const hash = OfferUtils.hashStruct(
* OfferUtils.toStruct({ offer, group: zeroHash }),
* );
* console.log(hash);
* ```
*/
function hashStruct(offerStruct: OfferStruct): Hash;
/**
* Computes the canonical protocol offer hash from the offer's group id.
*
* Use this for final tree leaves and payload offers after a group has been
* assigned. Use `groupHash` for the zero-group hash that derives a group id.
*
* @param offer - Offer with the protocol group id encoded into the hash.
* @returns Offer hash.
* @example
* ```ts
* import { Offer, OfferUtils } from "@morpho-org/midnight-sdk";
* import { zeroAddress, zeroHash } from "viem";
*
* const offer = Offer.create({
* market: {
* chainId: 8453,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000007000",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000008000",
* },
* ],
* maturity: 54_000n,
* rcfThreshold: 0n,
* enterGate: zeroAddress,
* liquidatorGate: zeroAddress,
* },
* buy: true,
* maker: "0x0000000000000000000000000000000000009000",
* tick: 5_000n,
* group: zeroHash,
* expiry: 3_600n,
* ratifier: "0x0000000000000000000000000000000000004000",
* maxUnits: 100n,
* });
* const hash = OfferUtils.hash(offer);
* console.log(hash);
* ```
*/
function hash(offer: IOffer & Required<Pick<IOffer, "group">>): Hash;
/**
* Computes the on-chain offer identifier from the supplied offer struct.
*
* This is `keccak256(abi.encode(offer))`, the hash emitted by Midnight's
* `Take` event and used to identify offers on-chain. It is distinct from
* `hashStruct` and `hash`, which produce the typehash-prefixed ratifier hash.
* Collaterals are hashed in the supplied order; Midnight rejects unsorted
* collateral params with `CollateralParamsNotSorted`, so pass a sorted struct,
* for example one returned by `toStruct`.
* It matches a `Take` event only for the offer exactly as published. For
* RateRatifierV1 and PriceRatifierV1, hash the offers in the descriptor's
* `offers`: `buildDescriptor` replaces both the default group and a
* `Group.create` group.
*
* @param offerStruct - ABI-compatible offer to hash without normalization.
* @returns The offer hash emitted by Midnight's `Take` event.
* @throws {viem.InvalidAddressError} when an address has an invalid mixed-case checksum.
* @throws {viem.IntegerOutOfRangeError} when a uint field overflows its ABI size.
* @example
* ```ts
* import { Offer, OfferUtils } from "@morpho-org/midnight-sdk";
* import { zeroAddress } from "viem";
*
* const offer = Offer.create({
* market: {
* chainId: 8453,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000007000",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000008000",
* },
* ],
* maturity: 54_000n,
* rcfThreshold: 0n,
* enterGate: zeroAddress,
* liquidatorGate: zeroAddress,
* },
* buy: true,
* maker: "0x0000000000000000000000000000000000009000",
* tick: 5_000n,
* group: "0x1111111111111111111111111111111111111111111111111111111111111111",
* expiry: 3_600n,
* ratifier: "0x0000000000000000000000000000000000004000",
* maxUnits: 100n,
* });
* const offerHash = OfferUtils.onchainHashStruct(
* OfferUtils.toStruct({ offer }),
* );
* console.log(offerHash);
* ```
*/
function onchainHashStruct(offerStruct: OfferStruct): Hash;
/**
* Computes the on-chain offer identifier from an offer with a group id.
*
* This is `keccak256(abi.encode(offer))`, the hash emitted by Midnight's
* `Take` event and used to identify offers on-chain. It is distinct from
* `hash`, which returns the typehash-prefixed ratifier hash. `toStruct`
* normalizes the offer and sorts collateral params as required by Midnight.
* It matches a `Take` event only for the offer exactly as published. For
* RateRatifierV1 and PriceRatifierV1, hash the offers in the descriptor's
* `offers`: `buildDescriptor` replaces both the default group and a
* `Group.create` group.
*
* @param offer - Offer with the protocol group id encoded in its struct.
* @returns The offer hash emitted by Midnight's `Take` event.
* @throws {viem.InvalidAddressError} when an address has an invalid mixed-case checksum.
* @throws {viem.IntegerOutOfRangeError} when a uint field overflows its ABI size.
* @example
* ```ts
* import { Offer, OfferUtils } from "@morpho-org/midnight-sdk";
* import { zeroAddress } from "viem";
*
* const offer = Offer.create({
* market: {
* chainId: 8453,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000007000",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000008000",
* },
* ],
* maturity: 54_000n,
* rcfThreshold: 0n,
* enterGate: zeroAddress,
* liquidatorGate: zeroAddress,
* },
* buy: true,
* maker: "0x0000000000000000000000000000000000009000",
* tick: 5_000n,
* group: "0x1111111111111111111111111111111111111111111111111111111111111111",
* expiry: 3_600n,
* ratifier: "0x0000000000000000000000000000000000004000",
* maxUnits: 100n,
* });
* const offerHash = OfferUtils.onchainHash(offer);
* console.log(offerHash);
* ```
*/
function onchainHash(offer: IOffer & Required<Pick<IOffer, "group">>): Hash;
/**
* Computes the offer hash used to derive a content-addressed group id.
*
* The encoded group is always the protocol zero hash. Use this only for
* standalone or grouped-offer id derivation, not for final tree leaves or
* payload offers.
*
* @param offer - Offer to hash without a group id.
* @returns Offer hash encoded with the protocol zero group id.
* @example
* ```ts
* import { Offer, OfferUtils } from "@morpho-org/midnight-sdk";
* import { zeroAddress } from "viem";
*
* const offer = Offer.create({
* market: {
* chainId: 8453,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000007000",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000008000",
* },
* ],
* maturity: 54_000n,
* rcfThreshold: 0n,
* enterGate: zeroAddress,
* liquidatorGate: zeroAddress,
* },
* buy: true,
* maker: "0x0000000000000000000000000000000000009000",
* tick: 5_000n,
* expiry: 3_600n,
* ratifier: "0x0000000000000000000000000000000000004000",
* maxUnits: 100n,
* });
* const hash = OfferUtils.groupHash(offer);
* console.log(hash);
* ```
*/
function groupHash(offer: Omit<IOffer, "group">): Hash;
/**
* Validates and normalizes a Midnight offer tick and spacing.
*
* Use this for form-level validation before `Offer.create` when you want to
* surface a tick-specific issue. `Offer.create` calls it internally.
*
* @param params.tick - Offer tick to validate.
* @param params.tickSpacing - Optional market tick spacing; defaults to `DEFAULT_TICK_SPACING`.
* @returns Normalized tick and tick spacing.
* @throws {InvalidOfferParameterError} when the tick or spacing cannot be accepted by Midnight.
* @example
* ```ts
* import { OfferUtils } from "@morpho-org/midnight-sdk";
*
* const { tick } = OfferUtils.validateOfferTick({ tick: 4n });
* console.log(tick);
* ```
*/
function validateOfferTick(params: {
readonly tick: BigIntish;
readonly tickSpacing?: BigIntish;
}): {
tick: bigint;
tickSpacing: bigint;
};
/**
* Validates and normalizes an offer start/expiry range.
*
* Use this before `Offer.create` when a UI edits dates separately from other
* offer fields. `Offer.create` calls it internally.
*
* @param params.start - Optional offer start timestamp; defaults to zero.
* @param params.expiry - Offer expiry timestamp.
* @returns Normalized start and expiry timestamps.
* @throws {InvalidOfferParameterError} when the range is negative or expiry is before start.
* @example
* ```ts
* import { OfferUtils } from "@morpho-org/midnight-sdk";
*
* const range = OfferUtils.validateOfferTimeRange({ start: 0n, expiry: 1n });
* console.log(range.expiry);
* ```
*/
function validateOfferTimeRange(params: {
readonly start?: BigIntish;
readonly expiry: BigIntish;
}): {
start: bigint;
expiry: bigint;
};
/**
* Validates and normalizes mutually exclusive offer caps.
*
* Use this before `Offer.create` when a UI lets makers choose between a unit
* cap and an asset cap. `Offer.create` calls it internally.
*
* @param params.maxUnits - Optional unit cap; defaults to zero.
* @param params.maxAssets - Optional buyer or seller asset cap; defaults to zero.
* @returns Normalized unit and asset caps.
* @throws {InvalidOfferParameterError} when caps are negative, both zero, or both non-zero.
* @example
* ```ts
* import { OfferUtils } from "@morpho-org/midnight-sdk";
*
* const caps = OfferUtils.validateOfferCaps({ maxAssets: 100n });
* console.log(caps.maxAssets);
* ```
*/
function validateOfferCaps(params: {
readonly maxUnits?: BigIntish;
readonly maxAssets?: BigIntish;
}): {
maxUnits: bigint;
maxAssets: bigint;
};
/**
* Validates and normalizes the maximum continuous fee an offer accepts.
*
* Use this before `Offer.create` when a maker UI exposes fee tolerance.
* `Offer.create` calls it internally and defaults omitted values to zero so
* fresh offers fail closed unless the maker explicitly accepts a fee cap.
*
* @param params.continuousFeeCap - Optional maximum market continuous fee accepted by this offer.
* @returns Normalized continuous fee cap.
* @throws {InvalidOfferParameterError} when the cap is negative.
* @example
* ```ts
* import { OfferUtils } from "@morpho-org/midnight-sdk";
*
* const cap = OfferUtils.validateContinuousFeeCap({});
* console.log(cap.continuousFeeCap);
* ```
*/
function validateContinuousFeeCap(params: {
readonly continuousFeeCap?: BigIntish;
}): {
continuousFeeCap: bigint;
};
/**
* Resolves and validates the maker-seller receiver field for an offer side.
*
* Use this before `Offer.create` when side-specific receiver defaults must be
* displayed to a maker. `Offer.create` calls it internally.
*
* @param params.buy - Whether the maker buys loan assets.
* @param params.maker - Maker address used as the default sell-side receiver.
* @param params.receiverIfMakerIsSeller - Optional receiver used when maker is seller.
* @returns Receiver address to put on the offer.
* @throws {InvalidOfferParameterError} when a buy offer sets a non-zero maker-seller receiver.
* @example
* ```ts
* import { OfferUtils } from "@morpho-org/midnight-sdk";
*
* const receiver = OfferUtils.resolveReceiverIfMakerIsSeller({
* buy: true,
* maker: "0x0000000000000000000000000000000000000001",
* });
* console.log(receiver);
* ```
*/
function resolveReceiverIfMakerIsSeller(params: {
readonly buy: boolean;
readonly maker: Address;
readonly receiverIfMakerIsSeller?: Address;
}): `0x${string}`;
/**
* Validates deterministic make-offer parameters without constructing an offer.
*
* Use when an app needs normalized maker input or field-level errors before
* instantiating an `Offer`. `Offer.create` uses the same validation and then
* constructs the class instance for grouping and trees.
*
* @param params.buy - Whether the maker buys loan assets.
* @param params.maker - Maker address.
* @param params.tick - Offer tick.
* @param params.tickSpacing - Optional market tick spacing; defaults to `DEFAULT_TICK_SPACING`.
* @param params.maxUnits - Optional unit cap; defaults to zero.
* @param params.maxAssets - Optional buyer or seller asset cap; defaults to zero.
* @param params.continuousFeeCap - Optional maximum market continuous fee accepted by this offer; defaults to zero.
* @param params.start - Optional offer start timestamp; defaults to zero.
* @param params.expiry - Offer expiry timestamp.
* @param params.receiverIfMakerIsSeller - Optional receiver used when maker is seller.
* @returns Normalized deterministic offer parameters.
* @throws {InvalidOfferParameterError} when a deterministic offer parameter cannot satisfy protocol rules.
* @example
* ```ts
* import { OfferUtils } from "@morpho-org/midnight-sdk";
*
* const params = OfferUtils.validateOfferParams({
* buy: true,
* maker: "0x0000000000000000000000000000000000009000",
* tick: 5_000n,
* expiry: 3_600n,
* maxUnits: 100n,
* });
* console.log(params.maxAssets);
* ```
*/
function validateOfferParams(params: Pick<BuildOfferParams, "buy" | "maker" | "tick" | "tickSpacing" | "maxUnits" | "maxAssets" | "continuousFeeCap" | "start" | "expiry" | "receiverIfMakerIsSeller">): {
readonly tick: bigint;
readonly tickSpacing: bigint;
readonly start: bigint;
readonly expiry: bigint;
readonly maxUnits: bigint;
readonly maxAssets: bigint;
readonly continuousFeeCap: bigint;
readonly receiverIfMakerIsSeller: Address;
};
/**
* Validates protocol-level mechanics for one Midnight offer consumption group.
*
* Use before deriving a content-addressed group id. Grouped offers must share
* maker, side, loan token, cap mode, and cap value because Midnight tracks one
* consumed scalar per maker and group. `Group.create` calls this automatically;
* call it directly only when you need the normalized offers without
* constructing a `Group`.
*
* @param params.offers - Offers to validate as one protocol consumption group.
* @returns Normalized offers in the same order as the input entries.
* @throws {InvalidOfferGroupError} when the group violates protocol mechanics.
* @example
* ```ts
* import { Offer, OfferUtils } from "@morpho-org/midnight-sdk";
* import { zeroAddress } from "viem";
*
* const offer = Offer.create({
* market: {
* chainId: 8453,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000007000",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000008000",
* },
* ],
* maturity: 54_000n,
* rcfThreshold: 0n,
* enterGate: zeroAddress,
* liquidatorGate: zeroAddress,
* },
* buy: true,
* maker: "0x0000000000000000000000000000000000009000",
* tick: 5_000n,
* expiry: 3_600n,
* ratifier: "0x0000000000000000000000000000000000004000",
* maxUnits: 100n,
* });
* const offers = OfferUtils.validateOfferGroup({ offers: [offer] });
* console.log(offers.length);
* ```
*/
function validateOfferGroup(params: {
readonly offers: Iterable<IOffer>;
}): readonly Offer[];
/**
* Converts an offer tick into its WAD zero-coupon price.
*
* Use for display or local quote checks when accepting any plain object
* matching `IOffer` or an `Offer` instance.
*
* @param offer - Offer to inspect.
* @returns WAD price rounded to the protocol price quantum.
* @throws {NegativeValueError} when `offer.tick` is negative.
* @throws {TickOutOfRangeError} when `offer.tick` exceeds `MAX_TICK`.
* @example
* ```ts
* import { OfferUtils } from "@morpho-org/midnight-sdk";
*
* const price = OfferUtils.getPrice({ tick: 5_000n });
* console.log(price);
* ```
*/
function getPrice(offer: Pick<IOffer, "tick">): bigint;
/**
* Converts an offer tick into a WAD per-second simple rate at a timestamp.
*
* The rate is the offer's fixed period rate divided by the market's remaining
* time to maturity at `timestamp`, rounded up.
*
* @param params.offer - Offer to inspect.
* @param params.timestamp - Timestamp at which the rate is calculated.
* @returns WAD per-second simple rate rounded up.
* @throws {NegativeValueError} when `market.maturity`, `timestamp`, or `tick` is negative.
* @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
* @throws {DivisionByZeroError} when the tick price is zero or `timestamp` is at or after maturity.
* @example
* ```ts
* import { Offer, OfferUtils } from "@morpho-org/midnight-sdk";
*
* const offer = Offer.from({
* market: {
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000007000",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000008000",
* },
* ],
* maturity: 54_000n,
* rcfThreshold: 0n,
* enterGate: "0x0000000000000000000000000000000000000000",
* liquidatorGate: "0x0000000000000000000000000000000000000000",
* },
* buy: true,
* maker: "0x0000000000000000000000000000000000009000",
* start: 0n,
* expiry: 3_600n,
* tick: 5_000n,
* callback: "0x0000000000000000000000000000000000000000",
* callbackData: "0x",
* receiverIfMakerIsSeller: "0x0000000000000000000000000000000000000000",
* ratifier: "0x0000000000000000000000000000000000004000",
* reduceOnly: false,
* maxUnits: 100n,
* maxAssets: 0n,
* continuousFeeCap: 317097919n,
* });
* const rate = OfferUtils.getRate({ offer, timestamp: 1_000n });
* console.log(rate);
* ```
*/
function getRate(params: {
readonly offer: Pick<IOffer, "market" | "tick">;
readonly timestamp: BigIntish;
}): bigint;
/**
* Converts an offer tick into a WAD simple annual percentage rate at a timestamp.
*
* It computes time to maturity from the offer market and delegates the final
* tick annualization to `TickLib.tickToApr`.
*
* @param params.offer - Offer to inspect.
* @param params.timestamp - Timestamp at which the APR is calculated.
* @returns WAD simple APR rounded up.
* @throws {NegativeValueError} when `market.maturity`, `timestamp`, or `tick` is negative.
* @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
* @throws {DivisionByZeroError} when the tick price is zero or `timestamp` is at or after maturity.
* @example
* ```ts
* import { Offer, OfferUtils } from "@morpho-org/midnight-sdk";
*
* const offer = Offer.from({
* market: {
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000007000",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000008000",
* },
* ],
* maturity: 54_000n,
* rcfThreshold: 0n,
* enterGate: "0x0000000000000000000000000000000000000000",
* liquidatorGate: "0x0000000000000000000000000000000000000000",
* },
* buy: true,
* maker: "0x0000000000000000000000000000000000009000",
* start: 0n,
* expiry: 3_600n,
* tick: 5_000n,
* callback: "0x0000000000000000000000000000000000000000",
* callbackData: "0x",
* receiverIfMakerIsSeller: "0x0000000000000000000000000000000000000000",
* ratifier: "0x0000000000000000000000000000000000004000",
* reduceOnly: false,
* maxUnits: 100n,
* maxAssets: 0n,
* continuousFeeCap: 317097919n,
* });
* const apr = OfferUtils.getApr({ offer, timestamp: 1_000n });
* console.log(apr);
* ```
*/
function getApr(params: {
readonly offer: Pick<IOffer, "market" | "tick">;
readonly timestamp: BigIntish;
}): bigint;
/**
* Returns remaining units accepted by an offer's cap at a timestamp.
*
* The offer must carry a hydrated {@link Market} so the current market
* continuous fee and settlement-fee buckets are available locally. Fetch the
* current `consumed(maker, group)` value separately and pass it as input.
*
* Returns `0n` when Midnight `take` rejects the offer regardless of the
* amount: outside its `start`/`expiry` window, above its `continuousFeeCap`,
* on a tick off the market's tick spacing (the default spacing on an
* untouched market), or on a market whose loss factor is maxed out. Taker-dependent checks (`SelfTake`, ratification) and the
* seller's post-maturity debt check are not mirrored.
*
* @param params.offer - Offer to inspect. Its market must be hydrated.
* @param params.consumed - Amount already consumed from the offer group.
* @param params.timestamp - Timestamp used to compute market time to maturity.
* @returns Remaining consumable units accepted by Midnight `take`.
* @throws {InvalidOfferParameterError} when the offer does not carry hydrated market state or has invalid caps.
* @throws {NegativeValueError} when `consumed`, `timestamp`, offer limits, or delegated math inputs are negative.
* @throws {TickOutOfRangeError} when `tick` exceeds `MAX_TICK`.
* @throws {SettlementFeeExceedsPriceError} when settlement fee exceeds a buy-offer price.
* @example
* ```ts
* import { OfferUtils, midnightAbi } from "@morpho-org/midnight-sdk";
* import { createPublicClient, http } from "viem";
* import { base } from "viem/chains";
* import { readContract } from "viem/actions";
*
* const client = createPublicClient({ chain: base, transport: http() });
* const offer = {
* market: {
* 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,
* },
* buy: true,
* maker: "0x0000000000000000000000000000000000009000",
* start: 0n,
* expiry: 3_600n,
* tick: 5_000n,
* callback: "0x0000000000000000000000000000000000000000",
* callbackData: "0x",
* receiverIfMakerIsSeller: "0x0000000000000000000000000000000000000000",
* ratifier: "0x0000000000000000000000000000000000004000",
* reduceOnly: false,
* maxUnits: 100n,
* maxAssets: 0n,
* continuousFeeCap: 317097919n,
* } as const;
* const consumed = await readContract(client, {
* address: "0x0000000000000000000000000000000000001000",
* abi: midnightAbi,
* functionName: "consumed",
* args: [offer.maker, "0x1111111111111111111111111111111111111111111111111111111111111111"],
* });
* const units = OfferUtils.getConsumableUnits({ offer, consumed, timestamp: 1_000n });
* console.log(units);
* ```
*/
function getConsumableUnits(params: {
readonly offer: Pick<IOffer, "market" | "buy" | "start" | "expiry" | "tick" | "maxUnits" | "maxAssets" | "continuousFeeCap">;
readonly consumed: BigIntish;
readonly timestamp: BigIntish;
}): bigint;
/**
* Returns an offer expiry timestamp.
*
* Use in maker UIs, API response mappers, or quote filters when accepting
* any plain object matching `IOffer` or an `Offer` instance. This does not validate
* whether the offer is still live at the current block time.
*
* @param offer - Offer to inspect.
* @returns Offer expiry.
* @example
* ```ts
* import { Offer, OfferUtils } from "@morpho-org/midnight-sdk";
* import { zeroAddress } from "viem";
*
* const offer = Offer.create({
* market: {
* chainId: 8453,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000007000",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000008000",
* },
* ],
* maturity: 54_000n,
* rcfThreshold: 0n,
* enterGate: zeroAddress,
* liquidatorGate: zeroAddress,
* },
* buy: true,
* maker: "0x0000000000000000000000000000000000009000",
* tick: 5_000n,
* expiry: 3_600n,
* ratifier: "0x0000000000000000000000000000000000004000",
* maxUnits: 100n,
* });
* const expiry = OfferUtils.getOfferExpiry(offer);
* console.log(expiry);
* ```
*/
function getOfferExpiry(offer: Pick<IOffer, "expiry">): bigint;
}