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.

152 lines (151 loc) • 6.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fetchMarketParams = fetchMarketParams; exports.fetchMarket = fetchMarket; const morpho_ts_1 = require("@morpho-org/morpho-ts"); const actions_1 = require("viem/actions"); const abis_js_1 = require("../abis.js"); const errors_js_1 = require("../errors.js"); const index_js_1 = require("../market/index.js"); const utils_js_1 = require("./utils.js"); /** * Fetches immutable market params by id. * * Reads `eth_chainId` only when the viem client has no configured chain id, * then reads `Midnight.toMarket(marketId)`. * * @param client - Viem client used for the read. * @param params.marketId - Market id whose immutable params to read. * @param params.account - Optional account used as the `from` field for the read. * @param params.blockNumber - Optional block number used for the read. * @param params.blockTag - Optional block tag used for the read. * @param params.stateOverride - Optional state override set used for the read. * @returns Market params instance. * @throws {UnsupportedChainIdError} when no address registry exists for the client chain id. * @throws {UnknownAddressError} when the registry has no Midnight address for the client chain id. * @example * ```ts * import { fetchMarketParams } from "@morpho-org/midnight-sdk"; * import { createPublicClient, http } from "viem"; * import { base } from "viem/chains"; * * const client = createPublicClient({ chain: base, transport: http() }); * const params = await fetchMarketParams(client, { * marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67", * }); * console.log(params.loanToken); * ``` */ async function fetchMarketParams(client, params) { const chainId = await (0, utils_js_1.resolveChainId)(client); const midnight = (0, morpho_ts_1.getChainAddress)(chainId, "midnight"); const market = await (0, actions_1.readContract)(client, { ...(0, utils_js_1.callParameters)(params), address: midnight, abi: abis_js_1.midnightAbi, functionName: "toMarket", args: [params.marketId], }); return normalizeFetchedMarketParams(market, { marketId: params.marketId, chainId, midnight, }); } /** * Fetches a hydrated market by id. * * Reads `eth_chainId` only when the viem client has no configured chain id, * then reads `Midnight.toMarket(marketId)` and `Midnight.marketState(marketId)`. * * @param client - Viem client used for the reads. * @param params.marketId - Market id whose hydrated market state to read. * @param params.account - Optional account used as the `from` field for the reads. * @param params.blockNumber - Optional block number used for the reads. * @param params.blockTag - Optional block tag used for the reads. * @param params.stateOverride - Optional state override set used for the reads. * @returns Market instance. * @throws {UnsupportedChainIdError} when no address registry exists for the client chain id. * @throws {UnknownAddressError} when the registry has no Midnight address for the client chain id. * @example * ```ts * import { fetchMarket } from "@morpho-org/midnight-sdk"; * import { createPublicClient, http } from "viem"; * import { base } from "viem/chains"; * * const client = createPublicClient({ chain: base, transport: http() }); * const market = await fetchMarket(client, { * marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67", * }); * console.log(market.params.loanToken); * ``` */ async function fetchMarket(client, params) { const chainId = await (0, utils_js_1.resolveChainId)(client); const midnight = (0, morpho_ts_1.getChainAddress)(chainId, "midnight"); const [market, state] = await Promise.all([ (0, actions_1.readContract)(client, { ...(0, utils_js_1.callParameters)(params), address: midnight, abi: abis_js_1.midnightAbi, functionName: "toMarket", args: [params.marketId], }), (0, actions_1.readContract)(client, { ...(0, utils_js_1.callParameters)(params), address: midnight, abi: abis_js_1.midnightAbi, functionName: "marketState", args: [params.marketId], }), ]); const [totalUnits, lossFactor, withdrawable, continuousFeeCredit, settlementFeeCbps0, settlementFeeCbps1, settlementFeeCbps2, settlementFeeCbps3, settlementFeeCbps4, settlementFeeCbps5, settlementFeeCbps6, continuousFee, tickSpacing,] = state; return new index_js_1.Market({ params: normalizeFetchedMarketParams(market, { marketId: params.marketId, chainId, midnight, }), totalUnits, lossFactor, withdrawable, continuousFeeCredit, settlementFeeCbps: [ settlementFeeCbps0, settlementFeeCbps1, settlementFeeCbps2, settlementFeeCbps3, settlementFeeCbps4, settlementFeeCbps5, settlementFeeCbps6, ], continuousFee, tickSpacing, }); } function normalizeFetchedMarketParams(market, expected) { const marketParams = new index_js_1.MarketParams(market); if (marketParams.chainId !== BigInt(expected.chainId)) { throw new errors_js_1.InvalidMarketParameterError({ parameter: "chainId", value: marketParams.chainId, instruction: `Fetched market must use client chain id "${expected.chainId}".`, }); } if (marketParams.midnight.toLowerCase() !== expected.midnight.toLowerCase()) { throw new errors_js_1.InvalidMarketParameterError({ parameter: "midnight", value: marketParams.midnight, instruction: `Fetched market must use Midnight contract "${expected.midnight}".`, }); } const actualMarketId = index_js_1.MarketUtils.toId(marketParams); if (actualMarketId.toLowerCase() !== expected.marketId.toLowerCase()) { throw new errors_js_1.InvalidMarketParameterError({ parameter: "marketId", value: actualMarketId, instruction: `Fetched market must match requested market id "${expected.marketId}".`, }); } return marketParams; }