@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.
60 lines (59 loc) • 2.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchRatifierInfo = fetchRatifierInfo;
const morpho_ts_1 = require("@morpho-org/morpho-ts");
const actions_1 = require("viem/actions");
const Ratifier_js_1 = require("../signatures/Ratifier.js");
const utils_js_1 = require("./utils.js");
/**
* Fetches maker bytecode and classifies the ratifier route for new offers.
*
* The `bytecode` read is the result of `eth_getCode` for `params.maker` at the
* requested block. It is not returned to callers; it only tells the SDK whether
* the maker is an EOA or EIP-7702 account that can use Ecrecover signatures, or
* a deployed-code account that should use Setter root approval. Put the returned
* `ratifier` address on `Offer.create`, then use the returned `type` to choose
* `EcrecoverRatifier.ratify` or `SetterRatifier.ratify` after the tree
* has been built.
*
* Reads `eth_chainId` only when the viem client has no configured chain id,
* then reads `eth_getCode` for `params.maker`.
*
* @param client - Viem client used for the bytecode read.
* @param params.maker - Maker address whose bytecode determines the ratifier route.
* @param params.blockNumber - Optional block number used for the bytecode read.
* @param params.blockTag - Optional block tag used for the bytecode read.
* @returns Ratifier information.
* @throws {UnsupportedChainIdError} when no address registry exists for the client chain id.
* @throws {UnknownAddressError} when the registry has no configured ratifier address for the client chain id.
* @example
* ```ts
* import { fetchRatifierInfo } from "@morpho-org/midnight-sdk";
*
* const maker = "0x7b093658BE7f90B63D7c359e8f408e503c2D9401";
* const info = await fetchRatifierInfo(client, { maker });
* // Pass info.ratifier to Offer.create(...), then use info.type after
* // Tree.create({ type: info.type, entries: [...] }) to choose the Ecrecover
* // or Setter ratifier flow.
* console.log(info.type, info.ratifier);
* ```
*/
async function fetchRatifierInfo(client, params) {
const chainId = await (0, utils_js_1.resolveChainId)(client);
const ecrecoverRatifier = (0, morpho_ts_1.getChainAddress)(chainId, "ecrecoverRatifier");
const setterRatifier = (0, morpho_ts_1.getChainAddress)(chainId, "setterRatifier");
// eth_getCode is trusted; the returned ratifier is a suggestion the caller commits to on Offer.create. See THREAT_MODEL.md, RPC.
const bytecode = await (0, actions_1.getBytecode)(client, {
...(params.blockNumber != null
? { blockNumber: params.blockNumber }
: params.blockTag != null
? { blockTag: params.blockTag }
: {}),
address: params.maker,
});
return Ratifier_js_1.Ratifier.getRatifierInfo({
bytecode,
ecrecoverRatifier,
setterRatifier,
});
}