@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.
143 lines (142 loc) • 5.3 kB
TypeScript
import { type BigIntish } from "@morpho-org/morpho-ts";
import type { IMarket } from "../market/Market.js";
import type { IPosition } from "./Position.js";
/**
* Domain helpers for Midnight positions.
*
* @example
* ```ts
* import { PositionUtils } from "@morpho-org/midnight-sdk";
*
* const accrued = PositionUtils.accrueInterest({
* position: {
* user: "0x0000000000000000000000000000000000009000",
* marketId: "0x0000000000000000000000000000000000000000000000000000000000000001",
* credit: 1_000n,
* pendingFee: 100n,
* lastLossFactor: 0n,
* lastAccrual: 1_000n,
* debt: 0n,
* collateralBitmap: 1n,
* collateral: [50n],
* },
* market: {
* params: {
* chainId: 8453,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000007000",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000008000",
* },
* ],
* maturity: 54_000n,
* rcfThreshold: 0n,
* enterGate: "0x0000000000000000000000000000000000000000",
* liquidatorGate: "0x0000000000000000000000000000000000000000",
* },
* totalUnits: 1_000n,
* lossFactor: 0n,
* withdrawable: 500n,
* continuousFeeCredit: 0n,
* settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
* continuousFee: 10,
* tickSpacing: 4,
* },
* timestamp: 1_500n,
* });
* console.log(accrued.position.credit);
* ```
*/
export declare namespace PositionUtils {
/**
* Returns the maximum credit units currently withdrawable by a position.
*
* @param params.position - Position whose credit limits the withdrawal.
* @param params.market - Market whose available liquidity limits the withdrawal.
* @returns The lower of the position credit and market withdrawable liquidity.
* @example
* ```ts
* import { PositionUtils } from "@morpho-org/midnight-sdk";
*
* const withdrawable = PositionUtils.getWithdrawable({
* position: { credit: 1_000n },
* market: { withdrawable: 500n },
* });
* console.log(withdrawable); // 500n
* ```
*/
function getWithdrawable(params: {
readonly position: Pick<IPosition, "credit">;
readonly market: Pick<IMarket, "withdrawable">;
}): bigint;
/**
* Returns plain Midnight position and market objects accrued like Midnight `updatePositionView`.
*
* @param params.position - Position state to accrue.
* @param params.market - Hydrated market state used for loss factor and continuous-fee accrual.
* @param params.timestamp - Timestamp at which to accrue.
* @returns New plain position and market objects with updated credit, pending fee, last loss factor, last accrual, and market continuous-fee credit.
* @throws {InvalidPositionAccrualTimestampError} when timestamp is before `lastAccrual`.
* @throws {InvalidPositionLossFactorError} when the market loss factor is older than the position loss factor.
* @throws {InvalidPositionAccrualStateError} when raw inputs violate Midnight accounting invariants.
* @example
* ```ts
* import { PositionUtils } from "@morpho-org/midnight-sdk";
*
* const { position, market } = PositionUtils.accrueInterest({
* position: {
* user: "0x0000000000000000000000000000000000009000",
* marketId: "0x0000000000000000000000000000000000000000000000000000000000000001",
* credit: 1_000n,
* pendingFee: 100n,
* lastLossFactor: 0n,
* lastAccrual: 1_000n,
* debt: 0n,
* collateralBitmap: 1n,
* collateral: [50n],
* },
* market: {
* params: {
* chainId: 8453,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000007000",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000008000",
* },
* ],
* maturity: 54_000n,
* rcfThreshold: 0n,
* enterGate: "0x0000000000000000000000000000000000000000",
* liquidatorGate: "0x0000000000000000000000000000000000000000",
* },
* totalUnits: 1_000n,
* lossFactor: 0n,
* withdrawable: 500n,
* continuousFeeCredit: 0n,
* settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
* continuousFee: 10,
* tickSpacing: 4,
* },
* timestamp: 1_500n,
* });
* console.log(position.lastAccrual, market.continuousFeeCredit);
* ```
*/
function accrueInterest(params: {
readonly position: IPosition;
readonly market: IMarket;
readonly timestamp: BigIntish;
}): {
readonly position: IPosition;
readonly market: IMarket;
readonly accruedFee: bigint;
};
}