@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.
312 lines (311 loc) • 12.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SetterRatifier = exports.SetterRatifierUtils = void 0;
const morpho_ts_1 = require("@morpho-org/morpho-ts");
const viem_1 = require("viem");
const errors_js_1 = require("../errors.js");
const index_js_1 = require("../offers/index.js");
const Ratifier_js_1 = require("./Ratifier.js");
const standardRatifierInternal_js_1 = require("./standardRatifierInternal.js");
const TreeUtils_js_1 = require("./TreeUtils.js");
const setterRatifierDataAbi = [
{ name: "root", type: "bytes32" },
{ name: "leafIndex", type: "uint256" },
{ name: "proof", type: "bytes32[]" },
];
/**
* 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.
*/
var SetterRatifierUtils;
(function (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) {
return (0, viem_1.encodeAbiParameters)(setterRatifierDataAbi, [
params.root,
BigInt(params.leafIndex),
params.proof,
]);
}
SetterRatifierUtils.encodeRatifierData = encodeRatifierData;
/**
* 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) {
const [root, leafIndex, proof] = (0, viem_1.decodeAbiParameters)(setterRatifierDataAbi, data);
return (0, morpho_ts_1.deepFreeze)({ root, leafIndex, proof: [...proof] });
}
SetterRatifierUtils.decodeRatifierData = decodeRatifierData;
/**
* 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) {
const offer = index_js_1.Offer.from(params.offer);
const decoded = decodeRatifierData(params.ratifierData);
if (!TreeUtils_js_1.TreeUtils.verifyProof({
offer,
root: decoded.root,
leafIndex: decoded.leafIndex,
proof: decoded.proof,
})) {
throw new errors_js_1.InvalidTreeError("Ratifier data proof does not include offer.");
}
return decoded;
}
SetterRatifierUtils.verifyRatifierData = verifyRatifierData;
/**
* 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 {InvalidTreeError} when the leaf index is outside the tree or the tree contains multiple ratifiers.
* @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) {
const { tree } = Ratifier_js_1.Ratifier.normalizeRatifierTree({
tree: params.tree,
label: "Setter",
});
const proof = TreeUtils_js_1.TreeUtils.buildProof({ tree, leafIndex: params.leafIndex });
return encodeRatifierData({
root: proof.root,
leafIndex: proof.leafIndex,
proof: proof.proof,
});
}
SetterRatifierUtils.ratifierData = ratifierData;
/**
* 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 {InvalidTreeError} when the tree is invalid or contains multiple ratifiers.
* @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) {
const { tree } = Ratifier_js_1.Ratifier.normalizeRatifierTree({
tree: params.tree,
label: "Setter",
});
const proofs = TreeUtils_js_1.TreeUtils.buildProofs({
tree,
count: tree.offers.length,
});
return tree.offers.map((offer, leafIndex) => ({
offer,
ratifierData: encodeRatifierData(proofs[leafIndex]),
}));
}
SetterRatifierUtils.ratify = ratify;
})(SetterRatifierUtils || (exports.SetterRatifierUtils = SetterRatifierUtils = {}));
/**
* Tagged-tree API for SetterRatifier with validated portable leaf commitments.
* Legacy untagged inputs remain available through {@link SetterRatifierUtils}.
*/
var SetterRatifier;
(function (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) {
const tree = (0, standardRatifierInternal_js_1.parseStandardRatifierTree)(request.tree, "setter");
return SetterRatifierUtils.ratifierData({ ...request, tree });
}
SetterRatifier.ratifierData = ratifierData;
/**
* 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) {
const tree = (0, standardRatifierInternal_js_1.parseStandardRatifierTree)(request.tree, "setter");
return SetterRatifierUtils.ratify({ ...request, tree });
}
SetterRatifier.ratify = ratify;
/** {@inheritDoc SetterRatifierUtils.encodeRatifierData} */
SetterRatifier.encodeRatifierData = SetterRatifierUtils.encodeRatifierData;
/** {@inheritDoc SetterRatifierUtils.decodeRatifierData} */
SetterRatifier.decodeRatifierData = SetterRatifierUtils.decodeRatifierData;
/** {@inheritDoc SetterRatifierUtils.verifyRatifierData} */
SetterRatifier.verifyRatifierData = SetterRatifierUtils.verifyRatifierData;
})(SetterRatifier || (exports.SetterRatifier = SetterRatifier = {}));