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.

716 lines (715 loc) • 31.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RateRatifierV1Utils = exports.RateRatifierV1 = void 0; const morpho_ts_1 = require("@morpho-org/morpho-ts"); const viem_1 = require("viem"); const abis_js_1 = require("../abis.js"); const constants_js_1 = require("../constants.js"); const errors_js_1 = require("../errors.js"); const index_js_1 = require("../market/index.js"); const index_js_2 = require("../math/index.js"); const index_js_3 = require("../offers/index.js"); const GroupUtils_js_1 = require("./GroupUtils.js"); const offerStructInternal_js_1 = require("./offerStructInternal.js"); const ratifierV1Internal_js_1 = require("./ratifierV1Internal.js"); const TreeUtils_js_1 = require("./TreeUtils.js"); const treeMathInternal_js_1 = require("./treeMathInternal.js"); const rateRatifierV1DataAbi = [ { name: "root", type: "bytes32" }, { name: "leafIndex", type: "uint256" }, { name: "proof", type: "bytes32[]" }, { name: "rate", type: "uint256" }, { name: "allowedTaker", type: "address" }, ]; const rateRatifierV1LeafHashParams = [ { name: "typehash", type: "bytes32" }, { name: "marketHash", type: "bytes32" }, { name: "buy", type: "bool" }, { name: "maker", type: "address" }, { name: "start", type: "uint256" }, { name: "expiry", type: "uint256" }, { name: "rate", type: "uint256" }, { name: "allowedTaker", type: "address" }, { name: "group", type: "bytes32" }, { name: "callback", type: "address" }, { name: "callbackDataHash", type: "bytes32" }, { name: "receiverIfMakerIsSeller", type: "address" }, { name: "ratifier", type: "address" }, { name: "reduceOnly", type: "bool" }, { name: "maxUnits", type: "uint128" }, { name: "maxAssets", type: "uint128" }, { name: "continuousFeeCap", type: "uint256" }, ]; const isPaddingEntry = (entry) => (0, offerStructInternal_js_1.isEmptyOfferStruct)(entry.offer) && entry.rate === 0n && (0, offerStructInternal_js_1.isZeroAddress)(entry.allowedTaker); /** * RateRatifierV1-specific pure utilities. * * Use this route when makers ratify a Merkle root of rate-bounded offers * onchain. The make-side sequence is: create offers with the RateRatifierV1 * address, build the tree descriptor, ratify the root onchain (directly or * through `setIsRootRatified`), then call `ratify` and pass the returned items * to `Payload.encode`. * * @example * ```ts * import { RateRatifierV1 } from "@morpho-org/midnight-sdk"; * * console.log(typeof RateRatifierV1.ratify); * ``` */ var RateRatifierV1; (function (RateRatifierV1) { /** * Lowest nominal `offer.tick` the router accepts for a RateRatifierV1 leaf * (tick 3372, price >= 0.5 WAD). * * The router prices RateRatifierV1 offers from `rate`, but its gatekeeper * still validates the committed `offer.tick`; ticks below this are rejected * as `min_tick`. * * @example * ```ts * import { RateRatifierV1 } from "@morpho-org/midnight-sdk"; * * console.log(RateRatifierV1.MIN_TICK); // 3372n * ``` */ RateRatifierV1.MIN_TICK = index_js_2.TickLib.priceToTick(morpho_ts_1.MathLib.WAD / 2n, 1n); /** * Computes the RateRatifierV1 EIP-712 leaf hash for one leaf struct. * * This is the SDK port of `HashLib.hashRateRatifierV1Offer`: the offer's * `tick` field is replaced by `rate`, and `allowedTaker` is appended. * * @param leaf - ABI-compatible leaf to hash. * @returns Leaf hash used in the ratified Merkle tree. * @example * ```ts * import { RateRatifierV1, type OfferStruct } from "@morpho-org/midnight-sdk"; * import { zeroAddress } from "viem"; * * function hash(offer: OfferStruct) { * return RateRatifierV1.hashLeaf({ * offer, * rate: 0n, * allowedTaker: zeroAddress, * }); * } * ``` */ function hashLeaf(leaf) { const { offer } = leaf; return (0, viem_1.keccak256)((0, viem_1.encodeAbiParameters)(rateRatifierV1LeafHashParams, [ constants_js_1.RATE_RATIFIER_V1_OFFER_TYPEHASH, index_js_1.MarketUtils.hash(offer.market), offer.buy, offer.maker, offer.start, offer.expiry, leaf.rate, leaf.allowedTaker, offer.group, offer.callback, (0, viem_1.keccak256)(offer.callbackData), offer.receiverIfMakerIsSeller, offer.ratifier, offer.reduceOnly, offer.maxUnits, offer.maxAssets, offer.continuousFeeCap, ])); } RateRatifierV1.hashLeaf = hashLeaf; /** * Zero-group member hash of one leaf, as consumed by `GroupUtils.hashMembers`. * * This is what the router recomputes for its `group_identity` check: the * RateRatifierV1 leaf hash with `group = 0`, so the id commits to `rate` * and `allowedTaker` rather than to `tick`. * * @param leaf - Rate-bounded offer leaf. * @returns Zero-group RateRatifierV1 leaf hash. * @throws {InvalidRateRatifierV1RateError} when `leaf.rate` is negative. * @example * ```ts * import { Offer, RateRatifierV1 } 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: "0x000000000000000000000000000000000000a111", * maxUnits: 100n, * }); * * console.log(RateRatifierV1.memberHash({ offer, rate: 0n })); * ``` */ function memberHash(leaf) { const rate = BigInt(leaf.rate); if (rate < 0n) { throw new errors_js_1.InvalidRateRatifierV1RateError(rate); } return hashLeaf({ offer: index_js_3.OfferUtils.toStruct({ offer: leaf.offer, group: viem_1.zeroHash }), rate, allowedTaker: leaf.allowedTaker ?? viem_1.zeroAddress, }); } RateRatifierV1.memberHash = memberHash; /** * Content-addressed consumption group id for RateRatifierV1 leaves. * * Mirrors `GroupUtils.hash` with the RateRatifierV1 {@link memberHash}, so * it matches the router-derived group. Use it to share one group across * several leaves; single leaves get this id by default in * {@link buildDescriptor}. Leaves sharing a group must satisfy the same * constraints as `Group.create` (one maker, side, and cap mode/value); this * helper only derives the id and does not check them. * * @param leaves - Leaves sharing one consumption group. * @returns Content-addressed group id. * @throws {InvalidOfferGroupError} when `leaves` is empty. * @throws {InvalidRateRatifierV1RateError} when a leaf rate is negative. * @example * ```ts * import { Offer, RateRatifierV1 } 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: "0x000000000000000000000000000000000000a111", * maxUnits: 100n, * }); * const leaves = [ * { offer, rate: 50_000_000_000_000_000n }, * { offer: Offer.from({ ...offer, tick: 5_004n }), rate: 60_000_000_000_000_000n }, * ]; * * const group = RateRatifierV1.groupId(leaves); * const grouped = leaves.map((leaf) => ({ * ...leaf, * offer: Offer.from({ ...leaf.offer, group }), * })); * ``` */ function groupId(leaves) { return GroupUtils_js_1.GroupUtils.hashMembers(Array.from(leaves, memberHash)); } RateRatifierV1.groupId = groupId; /** * Builds a RateRatifierV1 tree descriptor from leaf input. * * Non-power-of-two leaf lists are padded with protocol-zero leaves at the * highest leaf indices. A group matching the generic content id of all * leaves sharing it (for example from `OfferUtils.toStruct` or * `Group.create`) is re-derived with {@link groupId}; other explicit groups * are committed as-is. Every explicit group is first checked with * {@link OfferUtils.validateOfferGroup}. An omitted `group` defaults to the leaf's * content-addressed singleton {@link groupId}, which commits to `rate` and * `allowedTaker` like the router does. * * @param leaves - Rate-bounded offer leaves in leaf order. * @returns RateRatifierV1 tree descriptor. * @throws {InvalidRateRatifierV1RateError} when a leaf rate is negative. * @throws {InvalidRateRatifierV1TickError} when a leaf offer tick is below {@link MIN_TICK}. * @throws {InvalidOfferParameterError} when a sell offer has the zero address as its receiver. * @throws {InvalidOfferGroupError} when an explicitly grouped offer breaks `OfferUtils.validateOfferGroup` rules (members differ in maker, side, loan token, chain, Midnight contract or cap mode/value, an offer has an invalid cap, or a buy offer sets `receiverIfMakerIsSeller`). * @throws {InvalidTreeError} when the leaf list is empty, contains duplicate leaf hashes, or mixes ratifier addresses. * @throws {InvalidTreeHeightError} when the padded tree exceeds supported ratifier typehashes. * @example * ```ts * import { RateRatifierV1, type IOffer } from "@morpho-org/midnight-sdk"; * * function build(offer: IOffer) { * return RateRatifierV1.buildDescriptor([{ offer, rate: 0n }]); * } * ``` */ function buildDescriptor(leaves) { const normalized = leaves.map((leaf) => { const rate = BigInt(leaf.rate); if (rate < 0n) { throw new errors_js_1.InvalidRateRatifierV1RateError(rate); } const allowedTaker = leaf.allowedTaker ?? viem_1.zeroAddress; const input = index_js_3.Offer.from(leaf.offer); if (input.tick < RateRatifierV1.MIN_TICK) { throw new errors_js_1.InvalidRateRatifierV1TickError(input.tick, RateRatifierV1.MIN_TICK); } return { offer: input, rate, allowedTaker }; }); const assignedOffers = (0, ratifierV1Internal_js_1.assignRatifierV1Groups)(normalized, groupId); const offers = []; const structs = normalized.map(({ rate, allowedTaker }, index) => { const offer = (0, treeMathInternal_js_1.requireTreeItem)(assignedOffers, index); offers.push(offer); return { offer: index_js_3.OfferUtils.toStruct({ offer }), rate, allowedTaker }; }); const descriptor = (0, ratifierV1Internal_js_1.buildRatifierV1Descriptor)({ entries: structs, padding: { offer: offerStructInternal_js_1.EMPTY_OFFER_STRUCT, rate: 0n, allowedTaker: viem_1.zeroAddress, }, hashLeaf, ratifierOf: (leafStruct) => leafStruct.offer.ratifier, label: "RateRatifierV1", }); return Object.freeze({ entries: (0, morpho_ts_1.deepFreeze)(descriptor.entries), leaves: (0, morpho_ts_1.deepFreeze)(descriptor.leaves), root: descriptor.root, height: descriptor.height, offers: Object.freeze([...offers]), }); } RateRatifierV1.buildDescriptor = buildDescriptor; const treeHelpers = { buildDescriptor, hashLeaf, isPadding: isPaddingEntry, ratifierOf: (leafStruct) => leafStruct.offer.ratifier, validateEntry: (entry) => { if (entry.rate < 0n) { throw new errors_js_1.InvalidRateRatifierV1RateError(entry.rate); } if (entry.offer.tick < RateRatifierV1.MIN_TICK) { throw new errors_js_1.InvalidRateRatifierV1TickError(entry.offer.tick, RateRatifierV1.MIN_TICK); } }, label: "RateRatifierV1", type: "rateV1", }; /** * Builds a Merkle proof for one RateRatifierV1 leaf. * * @param params.tree - RateRatifierV1 tree descriptor or raw leaf input. * @param params.leafIndex - Leaf index to prove. * @returns Proof descriptor. * @throws {InvalidRateRatifierV1RateError} when a raw leaf carries a negative rate. * @throws {InvalidRateRatifierV1TickError} when a leaf offer tick is below {@link MIN_TICK}, including inside a caller-supplied descriptor. * @throws {InvalidOfferParameterError} when raw tree input contains a sell offer with a zero receiver. * @throws {InvalidOfferGroupError} when an explicitly grouped offer breaks `OfferUtils.validateOfferGroup` rules (members differ in maker, side, loan token, chain, Midnight contract or cap mode/value, an offer has an invalid cap, or a buy offer sets `receiverIfMakerIsSeller`). For raw leaf input only. * @throws {InvalidTreeError} when the tree is invalid or the leaf index is out of range. * @throws {InvalidTreeHeightError} when the tree exceeds the supported height. * @example * ```ts * import { RateRatifierV1, type IOffer } from "@morpho-org/midnight-sdk"; * * function build(offer: IOffer) { * return RateRatifierV1.buildProof({ * tree: RateRatifierV1.buildDescriptor([{ offer, rate: 0n }]), * leafIndex: 0n, * }); * } * ``` */ function buildProof(params) { const tree = (0, ratifierV1Internal_js_1.resolveRatifierV1Tree)(params.tree, treeHelpers); return TreeUtils_js_1.TreeUtils.buildProof({ tree, leafIndex: params.leafIndex }); } RateRatifierV1.buildProof = buildProof; /** * Encodes RateRatifierV1 ratifier data. * * Use only when you already have a root, proof, rate, and allowed taker. * Most maker flows call `ratifierData` for one leaf or `ratify` for every * leaf in the ratified tree. * * @param params.root - Merkle root ratified by the maker's RateRatifierV1. * @param params.leafIndex - Leaf index proven by `params.proof`. * @param params.proof - Merkle proof siblings for the leaf. * @param params.rate - WAD-scaled per-second rate bound carried by the leaf. * @param params.allowedTaker - Taker restriction carried by the leaf. * @returns ABI-encoded ratifier data. * @example * ```ts * import { RateRatifierV1 } from "@morpho-org/midnight-sdk"; * import { zeroAddress, zeroHash } from "viem"; * * const data = RateRatifierV1.encodeRatifierData({ * root: zeroHash, * leafIndex: 0n, * proof: [], * rate: 0n, * allowedTaker: zeroAddress, * }); * console.log(data); * ``` */ function encodeRatifierData(params) { return (0, viem_1.encodeAbiParameters)(rateRatifierV1DataAbi, [ params.root, BigInt(params.leafIndex), params.proof, BigInt(params.rate), params.allowedTaker, ]); } RateRatifierV1.encodeRatifierData = encodeRatifierData; /** * Decodes RateRatifierV1 ratifier data. * * Use on the take-side or in tests after `Payload.decode` to inspect the * proof, rate, and allowed taker attached to a published offer. * * @param data - ABI-encoded ratifier data. * @returns Decoded RateRatifierV1 ratifier data. * @example * ```ts * import { RateRatifierV1 } from "@morpho-org/midnight-sdk"; * import { zeroAddress, zeroHash } from "viem"; * * const decoded = RateRatifierV1.decodeRatifierData( * RateRatifierV1.encodeRatifierData({ * root: zeroHash, * leafIndex: 0n, * proof: [], * rate: 0n, * allowedTaker: zeroAddress, * }), * ); * console.log(decoded.rate); * ``` */ function decodeRatifierData(data) { const [root, leafIndex, proof, rate, allowedTaker] = (0, viem_1.decodeAbiParameters)(rateRatifierV1DataAbi, data); return (0, morpho_ts_1.deepFreeze)({ root, leafIndex, proof: [...proof], rate, allowedTaker, }); } RateRatifierV1.decodeRatifierData = decodeRatifierData; /** * Verifies that RateRatifierV1 ratifier data proves one payload offer * belongs to its root. * * This helper intentionally does not check the onchain * `RateRatifierV1.ratification` state or the rate-implied price bound. * Consumers can query those values at their own block context after local * proof verification. * * This verifies the existing commitment without applying tree-construction checks. * * @param params.offer - Offer carried by the payload item. * @param params.ratifierData - ABI-encoded RateRatifierV1 ratifier data. * @param params.taker - Optional taker to check against the leaf's allowed taker. * @returns Decoded RateRatifierV1 ratifier data after proof verification. * @throws {InvalidTreeError} when the proof does not include `offer` in `root`. * @throws {RatifierV1TakerNotAllowedError} when `taker` is not the leaf's allowed taker. * @example * ```ts * import { RateRatifierV1, type IOffer } from "@morpho-org/midnight-sdk"; * import type { Hex } from "viem"; * * function check(offer: IOffer, ratifierData: Hex) { * return RateRatifierV1.verifyRatifierData({ offer, ratifierData }); * } * ``` */ function verifyRatifierData(params) { const decoded = decodeRatifierData(params.ratifierData); const leaf = hashLeaf({ offer: index_js_3.OfferUtils.toStruct({ offer: index_js_3.Offer.from(params.offer) }), rate: decoded.rate, allowedTaker: decoded.allowedTaker, }); if (!TreeUtils_js_1.TreeUtils.verifyLeafProof({ leaf, root: decoded.root, leafIndex: decoded.leafIndex, proof: decoded.proof, })) { throw new errors_js_1.InvalidTreeError("Ratifier data proof does not include offer."); } (0, ratifierV1Internal_js_1.assertRatifierV1Taker)({ taker: params.taker, allowedTaker: decoded.allowedTaker, }); return decoded; } RateRatifierV1.verifyRatifierData = verifyRatifierData; /** * Builds one ratifier-data value for a tree leaf. * * Use after root ratification when a caller needs data for one offer leaf. * Use `ratify` to produce payload-ready items for the whole tree. * * @param params.tree - RateRatifierV1 tree descriptor or raw leaf input. * @param params.leafIndex - Leaf index to prove. * @returns ABI-encoded RateRatifierV1 data. * @throws {InvalidRateRatifierV1RateError} when a raw leaf carries a negative rate. * @throws {InvalidRateRatifierV1TickError} when a leaf offer tick is below {@link MIN_TICK}, including inside a caller-supplied descriptor. * @throws {InvalidOfferParameterError} when raw tree input contains a sell offer with a zero receiver. * @throws {InvalidOfferGroupError} when an explicitly grouped offer breaks `OfferUtils.validateOfferGroup` rules (members differ in maker, side, loan token, chain, Midnight contract or cap mode/value, an offer has an invalid cap, or a buy offer sets `receiverIfMakerIsSeller`). For raw leaf input only. * @throws {InvalidTreeError} when the tree is invalid, the leaf index is outside the tree, or the tree contains multiple ratifiers. * @throws {InvalidTreeHeightError} when the tree exceeds the supported height. * @example * ```ts * import { RateRatifierV1, type IOffer } from "@morpho-org/midnight-sdk"; * * function build(offer: IOffer) { * return RateRatifierV1.ratifierData({ * tree: [{ offer, rate: 0n }], * leafIndex: 0n, * }); * } * ``` */ function ratifierData(params) { const tree = (0, ratifierV1Internal_js_1.resolveRatifierV1Tree)(params.tree, treeHelpers); const proof = TreeUtils_js_1.TreeUtils.buildProof({ tree, leafIndex: params.leafIndex }); const entry = (0, treeMathInternal_js_1.requireTreeItem)(tree.entries, Number(proof.leafIndex)); return encodeRatifierData({ root: proof.root, leafIndex: proof.leafIndex, proof: proof.proof, rate: entry.rate, allowedTaker: entry.allowedTaker, }); } RateRatifierV1.ratifierData = ratifierData; /** * Returns payload-ready items after a RateRatifierV1 root has been ratified. * * Use after the root ratification transaction has been submitted for every * maker in the tree. 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 - RateRatifierV1 tree descriptor or raw leaf input whose root has already been ratified onchain. * @returns Items containing each non-padding offer and its ratifier data. * @throws {InvalidRateRatifierV1RateError} when a raw leaf carries a negative rate. * @throws {InvalidRateRatifierV1TickError} when a leaf offer tick is below {@link MIN_TICK}, including inside a caller-supplied descriptor. * @throws {InvalidOfferParameterError} when raw tree input contains a sell offer with a zero receiver. * @throws {InvalidOfferGroupError} when an explicitly grouped offer breaks `OfferUtils.validateOfferGroup` rules (members differ in maker, side, loan token, chain, Midnight contract or cap mode/value, an offer has an invalid cap, or a buy offer sets `receiverIfMakerIsSeller`). For raw leaf input only. * @throws {InvalidTreeError} when the tree is invalid or contains multiple ratifiers. * @throws {InvalidTreeHeightError} when the tree exceeds the supported height. * @example * ```ts * import { RateRatifierV1, type IOffer } from "@morpho-org/midnight-sdk"; * * function build(offer: IOffer) { * return RateRatifierV1.ratify({ * tree: [{ offer, rate: 0n }], * }); * } * ``` */ function ratify(params) { const tree = (0, ratifierV1Internal_js_1.resolveRatifierV1Tree)(params.tree, treeHelpers); const proofs = TreeUtils_js_1.TreeUtils.buildProofs({ tree, count: tree.offers.length, }); return tree.offers.map((offer, leafIndex) => { const entry = (0, treeMathInternal_js_1.requireTreeItem)(tree.entries, leafIndex); const proof = (0, treeMathInternal_js_1.requireTreeItem)(proofs, leafIndex); return { offer, ratifierData: encodeRatifierData({ root: proof.root, leafIndex: proof.leafIndex, proof: proof.proof, rate: entry.rate, allowedTaker: entry.allowedTaker, }), }; }); } RateRatifierV1.ratify = ratify; /** * Converts a WAD-scaled per-second rate into the price bound used by * `RateRatifierV1.isRatified`. * * The bound is `WAD * WAD / (WAD + rate * timeToMaturity)`, rounded down * for buy offers and up for sell offers, matching the onchain comparison * direction. * * @param params.rate - WAD-scaled per-second rate. * @param params.timeToMaturity - Seconds remaining until market maturity. * @param params.buy - Whether the maker buys units. * @returns WAD-scaled price bound. * @throws {InvalidRateRatifierV1RateError} when `rate` is negative. * @throws {InvalidRateRatifierV1TimeError} when `timeToMaturity` is negative. * @throws {RateRatifierV1BoundOverflowError} when the bound arithmetic exceeds uint256, matching the contract's checked math. * @example * ```ts * import { RateRatifierV1 } from "@morpho-org/midnight-sdk"; * * const bound = RateRatifierV1.priceBound({ * rate: 0n, * timeToMaturity: 0n, * buy: true, * }); * console.log(bound); * ``` */ function priceBound(params) { const rate = BigInt(params.rate); if (rate < 0n) { throw new errors_js_1.InvalidRateRatifierV1RateError(rate); } const timeToMaturity = BigInt(params.timeToMaturity); if (timeToMaturity < 0n) { throw new errors_js_1.InvalidRateRatifierV1TimeError(timeToMaturity, "timeToMaturity"); } const product = rate * timeToMaturity; if (product > viem_1.maxUint256) { throw new errors_js_1.RateRatifierV1BoundOverflowError(rate, timeToMaturity); } const denominator = morpho_ts_1.MathLib.WAD + product; if (denominator > viem_1.maxUint256) { throw new errors_js_1.RateRatifierV1BoundOverflowError(rate, timeToMaturity); } if (!params.buy && morpho_ts_1.MathLib.WAD * morpho_ts_1.MathLib.WAD + denominator - 1n > viem_1.maxUint256) { throw new errors_js_1.RateRatifierV1BoundOverflowError(rate, timeToMaturity); } return params.buy ? morpho_ts_1.MathLib.mulDivDown(morpho_ts_1.MathLib.WAD, morpho_ts_1.MathLib.WAD, denominator) : morpho_ts_1.MathLib.mulDivUp(morpho_ts_1.MathLib.WAD, morpho_ts_1.MathLib.WAD, denominator); } RateRatifierV1.priceBound = priceBound; /** * Checks whether an offer's tick satisfies its rate bound at a timestamp. * * Mirrors `RateRatifierV1.isRatified`: the offer price must be at or below * the rate-implied bound for buy offers, and at or above it for sell * offers. Time to maturity floors at zero. Bounds that overflow uint256 * are unacceptable, matching the contract's revert. * * @param params.offer - Offer whose `tick`, `buy`, and `market.maturity` are checked. * @param params.rate - WAD-scaled per-second rate bound proven by the leaf. * @param params.timestamp - Timestamp at which acceptance is evaluated. * @returns Whether the offer's tick price satisfies the rate bound. * @throws {InvalidRateRatifierV1RateError} when `rate` is negative. * @throws {InvalidRateRatifierV1TimeError} when `timestamp` is negative. * @throws {NegativeValueError} when `offer.tick` is negative. * @throws {TickOutOfRangeError} when `offer.tick` exceeds `MAX_TICK`. * @example * ```ts * import { RateRatifierV1, type IOffer } from "@morpho-org/midnight-sdk"; * * function check(offer: IOffer) { * return RateRatifierV1.isPriceAcceptable({ * offer, * rate: 0n, * timestamp: 0n, * }); * } * ``` */ function isPriceAcceptable(params) { const timestamp = BigInt(params.timestamp); if (timestamp < 0n) { throw new errors_js_1.InvalidRateRatifierV1TimeError(timestamp, "timestamp"); } const maturity = BigInt("params" in params.offer.market ? params.offer.market.params.maturity : params.offer.market.maturity); const timeToMaturity = maturity - timestamp; let bound; try { bound = priceBound({ rate: params.rate, timeToMaturity: timeToMaturity > 0n ? timeToMaturity : 0n, buy: params.offer.buy, }); } catch (error) { if (error instanceof errors_js_1.RateRatifierV1BoundOverflowError) { return false; } throw error; } const offerPrice = index_js_2.TickLib.tickToPrice(params.offer.tick); return params.offer.buy ? offerPrice <= bound : offerPrice >= bound; } RateRatifierV1.isPriceAcceptable = isPriceAcceptable; /** * Encodes a `setIsRootRatified` call to a RateRatifierV1 contract. * * Onchain, the caller must be the maker or an address authorized by the * maker on the Midnight instance. The returned descriptor is neutral and * never signs or submits anything. * * @param params.ratifier - RateRatifierV1 contract address. * @param params.maker - Maker whose ratification entry is updated. * @param params.root - Merkle root to ratify or unratify. * @param params.isRatified - New ratification flag for the root. * @returns Neutral call descriptor. * @throws {InvalidRatifierV1AddressError} when the ratifier address is the zero address. * @example * ```ts * import { RateRatifierV1 } from "@morpho-org/midnight-sdk"; * import { zeroAddress, zeroHash } from "viem"; * * const call = RateRatifierV1.encodeSetIsRootRatified({ * ratifier: "0x0000000000000000000000000000000000000001", * maker: zeroAddress, * root: zeroHash, * isRatified: true, * }); * console.log(call.to); * ``` */ function encodeSetIsRootRatified(params) { (0, ratifierV1Internal_js_1.assertRatifierV1Address)(params.ratifier); return (0, morpho_ts_1.deepFreeze)({ to: params.ratifier, data: (0, viem_1.encodeFunctionData)({ abi: abis_js_1.rateRatifierV1Abi, functionName: "setIsRootRatified", args: [params.maker, params.root, params.isRatified], }), }); } RateRatifierV1.encodeSetIsRootRatified = encodeSetIsRootRatified; })(RateRatifierV1 || (exports.RateRatifierV1Utils = exports.RateRatifierV1 = RateRatifierV1 = {}));