@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.
224 lines (223 loc) • 9.19 kB
JavaScript
import { MAX_TREE_HEIGHT } from "../constants.js";
import { InvalidTreeError, InvalidTreeHeightError } from "../errors.js";
import { OfferUtils } from "../offers/index.js";
import { isEmptyOfferStruct } from "./offerStructInternal.js";
import { TreeUtils } from "./TreeUtils.js";
import { nextPowerOfTwo } from "./treeMathInternal.js";
function isTreeLike(tree) {
return (!Array.isArray(tree) &&
"offers" in tree &&
"paddedOffers" in tree &&
"leaves" in tree &&
"root" in tree &&
"height" in tree);
}
function normalizeTree(tree) {
if (isTreeLike(tree)) {
if (!Number.isInteger(tree.height) ||
tree.height < 0 ||
tree.height > MAX_TREE_HEIGHT) {
throw new InvalidTreeHeightError(tree.height);
}
const expectedLength = 2 ** tree.height;
if (tree.paddedOffers.length !== expectedLength ||
tree.leaves.length !== expectedLength ||
expectedLength !== nextPowerOfTwo(tree.offers.length)) {
throw new InvalidTreeError("Tree offers, leaves, and height describe different trees.");
}
const computedLeaves = tree.paddedOffers.map(OfferUtils.hashStruct);
const seen = new Set();
for (const [index, offer] of tree.offers.entries()) {
if (isEmptyOfferStruct(tree.paddedOffers[index], {
allowDefaultGroup: true,
})) {
throw new InvalidTreeError("Visible offers must not contain tree padding.");
}
const hash = OfferUtils.hashStruct(OfferUtils.toStruct({ offer }));
if (hash.toLowerCase() !== computedLeaves[index].toLowerCase() ||
seen.has(hash.toLowerCase())) {
throw new InvalidTreeError("Visible offers do not match the tree leaves.");
}
seen.add(hash.toLowerCase());
}
if (tree.paddedOffers
.slice(tree.offers.length)
.some((offer) => !isEmptyOfferStruct(offer, { allowDefaultGroup: true }))) {
throw new InvalidTreeError("Tree padding contains offers hidden from the visible offer list.");
}
if (computedLeaves.some((leaf, index) => leaf.toLowerCase() !== tree.leaves[index].toLowerCase())) {
throw new InvalidTreeError("Tree leaves do not match its padded offers.");
}
let level = computedLeaves;
while (level.length > 1) {
const next = [];
for (let index = 0; index < level.length; index += 2) {
next.push(TreeUtils.hashNode(level[index], level[index + 1]));
}
level = next;
}
if (level[0]?.toLowerCase() !== tree.root.toLowerCase()) {
throw new InvalidTreeError("Tree root does not match its leaves.");
}
return tree;
}
const entries = Array.isArray(tree) ? tree : [tree];
const offers = TreeUtils.normalizeEntries(entries);
const descriptor = TreeUtils.buildDescriptor(entries);
return {
offers,
paddedOffers: descriptor.offers,
leaves: descriptor.leaves,
root: descriptor.root,
height: descriptor.height,
};
}
function assertRatifierTree(params) {
const firstOffer = params.tree.offers[0];
if (firstOffer == null) {
throw new InvalidTreeError("Tree must contain at least one offer.");
}
const ratifier = firstOffer.ratifier;
const comparableRatifier = ratifier.toLowerCase();
for (const offer of params.tree.offers.slice(1)) {
if (offer.ratifier.toLowerCase() !== comparableRatifier) {
throw new InvalidTreeError(`All offers in a ${params.label} tree must use one ratifier; expected "${ratifier}", got "${offer.ratifier}". Build separate trees per ratifier.`);
}
}
return ratifier;
}
/**
* Utilities for selecting Midnight ratifier routes.
*
* Call these during make-side preparation, before `Offer.create`, when an app
* must decide whether a maker can sign an Ecrecover root or must approve a
* Setter root onchain. Fetch helpers read bytecode for you; this namespace is
* the pure classification logic.
*
* @example
* ```ts
* import { Ratifier } from "@morpho-org/midnight-sdk";
*
* console.log(Ratifier.isEip7702Designator("0xef0100"));
* ```
*/
export var Ratifier;
(function (Ratifier) {
/**
* Normalizes a ratifier tree input and asserts it uses one ratifier address.
*
* Pass an existing `Tree` or `TreeLike` object to reuse cached offers,
* leaves, root, and height. Pass raw offer/group input when the caller has
* not materialized a tree yet.
*
* @param params.tree - Tree-like object or raw offer/group input.
* @param params.label - Ratifier label used in validation errors.
* @returns Normalized tree-like data and its shared ratifier.
* @throws {InvalidTreeError} when the tree fields disagree, the tree is empty, or it contains multiple ratifiers.
* @throws {InvalidTreeHeightError} when the tree height is unsupported.
* @example
* ```ts
* import { Offer, Ratifier } 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 { tree, ratifier } = Ratifier.normalizeRatifierTree({
* tree: [offer],
* label: "Ecrecover",
* });
* console.log(tree.root, ratifier);
* ```
*/
function normalizeRatifierTree(params) {
if ("type" in params.tree &&
params.tree.type != null &&
params.tree.type !== params.label.toLowerCase()) {
throw new InvalidTreeError("Ratifier route does not match the tree route.");
}
const tree = normalizeTree(params.tree);
const ratifier = assertRatifierTree({ tree, label: params.label });
return { tree, ratifier };
}
Ratifier.normalizeRatifierTree = normalizeRatifierTree;
/**
* Checks whether bytecode is an EIP-7702 designator.
*
* EIP-7702 accounts have code-like bytecode but still sign as the owning EOA,
* so they use the Ecrecover ratifier route instead of the Setter route.
*
* @param bytecode - Account bytecode.
* @returns Whether the bytecode starts with `0xef0100`.
* @example
* ```ts
* import { Ratifier } from "@morpho-org/midnight-sdk";
*
* console.log(Ratifier.isEip7702Designator("0xef0100"));
* ```
*/
function isEip7702Designator(bytecode) {
return bytecode.toLowerCase().startsWith("0xef0100");
}
Ratifier.isEip7702Designator = isEip7702Designator;
/**
* Selects Ecrecover for EOAs/EIP-7702 accounts and Setter for deployed-code
* accounts.
*
* Use the returned `ratifier` address in `Offer.create`. Later, use
* `EcrecoverRatifier.ratify` when `type` is `ecrecover`, or approve the
* root and call `SetterRatifier.ratify` when `type` is `setter`.
*
* @param params.bytecode - Maker bytecode returned by `eth_getCode`.
* @param params.ecrecoverRatifier - Ratifier address used for EOAs and EIP-7702 accounts.
* @param params.setterRatifier - Ratifier address used for deployed-code accounts.
* @returns Ratifier information for the maker.
* @example
* ```ts
* import { Ratifier } from "@morpho-org/midnight-sdk";
*
* const info = Ratifier.getRatifierInfo({
* bytecode: "0x",
* ecrecoverRatifier: "0x0000000000000000000000000000000000000001",
* setterRatifier: "0x0000000000000000000000000000000000000002",
* });
* console.log(info.type);
* ```
*/
function getRatifierInfo(params) {
const ecrecoverRatifier = params.ecrecoverRatifier;
const setterRatifier = params.setterRatifier;
const bytecode = params.bytecode;
if (bytecode == null ||
bytecode === "0x" ||
isEip7702Designator(bytecode)) {
return { type: "ecrecover", ratifier: ecrecoverRatifier };
}
return { type: "setter", ratifier: setterRatifier };
}
Ratifier.getRatifierInfo = getRatifierInfo;
})(Ratifier || (Ratifier = {}));
/** @deprecated Use {@link Ratifier}. Retained for compatibility. */
export { Ratifier as RatifierUtils };