@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.
86 lines (85 loc) • 4.38 kB
TypeScript
import type { Address, Client, Hash } from "viem";
import { AccrualPosition, Position } from "../position/index.js";
import type { MidnightFetchParams } from "./types.js";
/**
* Fetches a Midnight position by id and user.
*
* The Solidity storage getter does not return the fixed collateral array, so
* this helper reads each collateral slot before returning the position.
* The deployless path is used by default. If deployless is disabled or
* unavailable, the direct fallback can fan out to 130 RPC calls unless the viem
* client has `batch.multicall` enabled.
*
* Reads `eth_chainId` only when the viem client has no configured chain id. In
* deployless mode, reads the deployless `GetPosition.query(midnight, marketId,
* user)` helper. If deployless is disabled or falls back, reads
* `Midnight.position(marketId, user)` and each
* `Midnight.collateral(marketId, user, index)` slot for `index` 0 through 127.
*
* @param client - Viem client used for the reads.
* @param params.marketId - Market id whose position to read.
* @param params.user - Position owner address.
* @param params.deployless - Optional deployless mode; defaults to `true` with fallback, or `"force"` without fallback.
* @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 Normalized position object.
* @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 { fetchPosition } from "@morpho-org/midnight-sdk";
* import { createPublicClient, http } from "viem";
* import { base } from "viem/chains";
*
* const client = createPublicClient({ chain: base, transport: http() });
* const position = await fetchPosition(client, {
* marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
* user: "0x0000000000000000000000000000000000009000",
* });
* console.log(position.debt);
* ```
*/
export declare function fetchPosition(client: Client, params: MidnightFetchParams & {
readonly marketId: Hash;
readonly user: Address;
}): Promise<Position>;
/**
* Fetches a Midnight position paired with its hydrated market.
*
* Reads the full inventory of `fetchPosition` and `fetchMarket` at the same
* call parameters: `eth_chainId` only when needed,
* `Midnight.toMarket(marketId)`, `Midnight.marketState(marketId)`, and either
* deployless `GetPosition.query(midnight, marketId, user)` or direct
* `Midnight.position(marketId, user)` plus all collateral slots.
*
* @param client - Viem client used for the reads.
* @param params.marketId - Market id whose position and market state to read.
* @param params.user - Position owner address.
* @param params.deployless - Optional deployless mode for the position read; defaults to `true` with fallback, or `"force"` without fallback.
* @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 Accrual position 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 { fetchAccrualPosition } from "@morpho-org/midnight-sdk";
* import { createPublicClient, http } from "viem";
* import { base } from "viem/chains";
*
* const client = createPublicClient({ chain: base, transport: http() });
* const position = await fetchAccrualPosition(client, {
* marketId: "0x12590ae1aee324a005be565f3bcdd16dbf8daf7969b26c181c8b8f467dad9f67",
* user: "0x0000000000000000000000000000000000009000",
* });
* console.log(position.market.id);
* ```
*/
export declare function fetchAccrualPosition(client: Client, params: MidnightFetchParams & {
readonly marketId: Hash;
readonly user: Address;
}): Promise<AccrualPosition>;