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.

334 lines (333 loc) • 13.6 kB
import { type BigIntish } from "@morpho-org/morpho-ts"; import { type Hash, type Hex } from "viem"; import { type IOffer } from "../offers/index.js"; import type { Payload } from "./Payload.js"; import type { SetterRatifierDataRequest, SetterRatifierRatifyRequest } from "./ratifierRequests.js"; import type { RatifierTreeInput } from "./TreeUtils.js"; import { type TreeProof } from "./TreeUtils.js"; /** * Decoded SetterRatifier ratifier data. * * Use this on the take-side or in diagnostics after `Payload.decode` when you * need to inspect the proof attached to a Setter-ratified offer. * * @example * ```ts * import { SetterRatifierUtils, type DecodedSetterRatifierData } from "@morpho-org/midnight-sdk"; * import { zeroHash } from "viem"; * * const data = SetterRatifierUtils.encodeRatifierData({ * root: zeroHash, * leafIndex: 0n, * proof: [], * }); * const decoded: DecodedSetterRatifierData = * SetterRatifierUtils.decodeRatifierData(data); * console.log(decoded.root); * ``` */ export type DecodedSetterRatifierData = TreeProof; /** * Parameters for one SetterRatifier ratifier-data value. * * Use after the offer maker or delegate has approved the tree root onchain. * Setter approvals are keyed by maker and root, so a mixed-maker tree needs one * approval per maker for the same root. * * @example * ```ts * import { Offer, Tree, type SetterRatifierDataParams } 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: "0x0000000000000000000000000000000000005000", * maxUnits: 100n, * }); * const params: SetterRatifierDataParams = { * tree: Tree.create([offer]), * leafIndex: 0n, * }; * console.log(params.leafIndex); * ``` * @deprecated Use {@link SetterRatifierDataRequest} with {@link SetterRatifier}. */ export interface SetterRatifierDataParams { /** Tree-like input that produced the proof. Existing `Tree` instances reuse cached hashes and proofs. */ readonly tree: RatifierTreeInput; /** Leaf index to prove. */ readonly leafIndex: BigIntish; } /** Parameters for locally verifying Setter ratifier data attached to one payload item. */ export interface SetterRatifierDataVerificationParams { /** Offer carried by the payload item. */ readonly offer: IOffer; /** ABI-encoded Setter ratifier data carried by the payload item. */ readonly ratifierData: Hex; } /** * SetterRatifier-specific pure utilities. * * Use this route for deployed-code makers. The make-side sequence is: create * offers with the Setter ratifier address, build the group/tree, validate the * tree, approve the root onchain for every maker in the tree, call `ratify`, * then pass the returned items to `Payload.encode`. * Ratifier helpers accept tree-like inputs rather than requiring the `Tree` * class. Passing an existing `Tree` remains the optimal path because its * cached offers, leaves, root, and height are reused when proofs are encoded. * * @example * ```ts * import { SetterRatifierUtils } from "@morpho-org/midnight-sdk"; * * console.log(typeof SetterRatifierUtils.encodeRatifierData); * ``` * @deprecated Use {@link SetterRatifier} with a tagged tree. */ export declare namespace SetterRatifierUtils { /** * Encodes SetterRatifier ratifier data. * * Use only when you already have a root and proof. Most maker flows call * `ratifierData` for one leaf or `ratify` for every leaf in the approved * tree. * * @param params.root - Merkle root approved by the maker's Setter ratifier. * @param params.leafIndex - Leaf index proven by `params.proof`. * @param params.proof - Merkle proof siblings for the leaf. * @returns ABI-encoded ratifier data. * @example * ```ts * import { SetterRatifierUtils } from "@morpho-org/midnight-sdk"; * * const data = SetterRatifierUtils.encodeRatifierData({ * root: "0x0000000000000000000000000000000000000000000000000000000000000000", * leafIndex: 0n, * proof: [], * }); * console.log(data); * ``` */ function encodeRatifierData(params: { readonly root: Hash; readonly leafIndex: BigIntish; readonly proof: readonly Hash[]; }): `0x${string}`; /** * Decodes SetterRatifier ratifier data. * * Use on the take-side or in tests after `Payload.decode` to inspect the * proof attached to a published Setter offer. * * @param data - ABI-encoded ratifier data. * @returns Decoded Setter ratifier data. * @example * ```ts * import { SetterRatifierUtils } from "@morpho-org/midnight-sdk"; * import { zeroHash } from "viem"; * * const data = SetterRatifierUtils.encodeRatifierData({ * root: zeroHash, * leafIndex: 0n, * proof: [], * }); * const decoded = SetterRatifierUtils.decodeRatifierData(data); * console.log(decoded.proof); * ``` */ function decodeRatifierData(data: Hex): DecodedSetterRatifierData; /** * Verifies that Setter ratifier data proves one payload offer belongs to its root. * * This helper intentionally does not check `SetterRatifierUtils.isRootRatified` or * `Midnight.isAuthorized` state. Consumers can query those values at their * own block context after local proof verification. * * @param params.offer - Offer carried by the payload item. * @param params.ratifierData - ABI-encoded Setter ratifier data. * @returns Decoded Setter ratifier data after proof verification. * @throws {InvalidTreeError} when the proof does not include `offer` in `root`. * @example * ```ts * import { SetterRatifierUtils } from "@morpho-org/midnight-sdk"; * * const decoded = SetterRatifierUtils.verifyRatifierData({ * offer, * ratifierData, * }); * console.log(decoded.root); * ``` */ function verifyRatifierData(params: SetterRatifierDataVerificationParams): DecodedSetterRatifierData; /** * Builds one ratifier-data value for a tree leaf. * * Use after root approval when a caller needs data for one offer leaf. Use * `ratify` to produce payload-ready items for the whole tree. Setter * approvals are keyed by maker and root, so the maker for this leaf must have * approved the tree root onchain. * * @param params.tree - Setter-ratified offer tree-like input that produced the proof. * @param params.leafIndex - Leaf index to prove. * @returns ABI-encoded SetterRatifier data. * @throws {InvalidOfferParameterError} when raw tree input contains a sell offer with a zero receiver. * @throws {InvalidTreeError} when the leaf index is outside the tree or the tree contains multiple ratifiers. * @throws {InvalidOfferGroupError} when raw standalone offers from one maker repeat an explicit group id. * @example * ```ts * import { Offer, SetterRatifier, Tree } 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: "0x0000000000000000000000000000000000005000", * maxUnits: 100n, * }); * const data = SetterRatifierUtils.ratifierData({ * tree: Tree.create([offer]), * leafIndex: 0n, * }); * console.log(data); * ``` */ function ratifierData(params: SetterRatifierDataParams): Hex; /** * Returns payload-ready items after a Setter root has been approved. * * Use after `Tree.mempoolValidate` and the root approval * transaction has been submitted for every maker in the tree. Setter * approvals are keyed by maker and root, so mixed-maker trees need one * approval per maker for the same root. The returned items can be passed * directly to `Payload.encode`. All offers in the tree must use one ratifier * address; build separate trees per ratifier. * * @param params.tree - Setter-ratified offer tree-like input whose root has already been approved onchain. * @returns Items containing each offer and its ratifier data. * @throws {InvalidOfferParameterError} when raw tree input contains a sell offer with a zero receiver. * @throws {InvalidTreeError} when the tree is invalid or contains multiple ratifiers. * @throws {InvalidOfferGroupError} when raw standalone offers from one maker repeat an explicit group id. * @example * ```ts * import { SetterRatifierUtils, Tree } from "@morpho-org/midnight-sdk"; * import { Offer } 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: "0x0000000000000000000000000000000000005000", * maxUnits: 100n, * }); * * const items = SetterRatifierUtils.ratify({ * tree: Tree.create([offer]), * }); * console.log(items.length); * ``` */ function ratify(params: { readonly tree: RatifierTreeInput; }): readonly Payload.Item[]; } /** * Tagged-tree API for SetterRatifier with validated portable leaf commitments. * Legacy untagged inputs remain available through {@link SetterRatifierUtils}. */ export declare namespace SetterRatifier { /** * Encodes the proof for one tagged Setter leaf. * @param request.tree - Matching tagged tree or portable snapshot; commitments are validated before use. * @param request.leafIndex - Leaf to prove. * @returns ABI-encoded ratifier data for the requested leaf. * @throws {InvalidTreeError} When the tag, commitments, or root are invalid. * @throws {InvalidTreeHeightError} When the descriptor height is unsupported. * @example * ```ts * import { SetterRatifier, type SetterRatifierDataRequest } from "@morpho-org/midnight-sdk"; * function execute(request: SetterRatifierDataRequest) { * return SetterRatifier.ratifierData(request); * } * ``` */ function ratifierData(request: SetterRatifierDataRequest): Hex; /** * Builds payload items for a tagged Setter tree after onchain root approval. * @param request.tree - Matching tagged tree or portable snapshot; commitments are validated before use. * @returns One payload item per non-padding offer, including its ratifier data. * @throws {InvalidTreeError} When the tag, commitments, or root are invalid. * @throws {InvalidTreeHeightError} When the descriptor height is unsupported. * @example * ```ts * import { SetterRatifier, type SetterRatifierRatifyRequest } from "@morpho-org/midnight-sdk"; * function execute(request: SetterRatifierRatifyRequest) { * return SetterRatifier.ratify(request); * } * ``` */ function ratify(request: SetterRatifierRatifyRequest): readonly Payload.Item[]; /** {@inheritDoc SetterRatifierUtils.encodeRatifierData} */ const encodeRatifierData: typeof SetterRatifierUtils.encodeRatifierData; /** {@inheritDoc SetterRatifierUtils.decodeRatifierData} */ const decodeRatifierData: typeof SetterRatifierUtils.decodeRatifierData; /** {@inheritDoc SetterRatifierUtils.verifyRatifierData} */ const verifyRatifierData: typeof SetterRatifierUtils.verifyRatifierData; }