@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.
499 lines (498 loc) • 17.3 kB
JavaScript
import { _try, MathLib } from "@morpho-org/morpho-ts";
import { InvalidMarketParameterError, UnknownCollateralIndexError, } from "../errors.js";
import { Market } from "../market/Market.js";
import { PositionUtils } from "./PositionUtils.js";
/**
* Midnight user position as stored by the core contract.
*
* @example
* ```ts
* import { Position } from "@morpho-org/midnight-sdk";
*
* const position = new Position({
* user: "0x0000000000000000000000000000000000009000",
* marketId: "0x0000000000000000000000000000000000000000000000000000000000000001",
* credit: 0n,
* pendingFee: 0n,
* lastLossFactor: 0n,
* lastAccrual: 0n,
* debt: 0n,
* collateralBitmap: 0n,
* collateral: [],
* });
* console.log(position.debt);
* ```
*/
export class Position {
/** User holding this position. */
user;
/** Id of the market on which this position is held. */
marketId;
/** User credit. */
credit;
/** Pending continuous fee. */
pendingFee;
/** Last loss factor seen by the position. */
lastLossFactor;
/** Last accrual timestamp. */
lastAccrual;
/** User debt. */
debt;
/** Collateral bitmap. */
collateralBitmap;
/** Collateral balances by index. */
collateral;
constructor(position) {
this.user = position.user;
this.marketId = position.marketId;
this.credit = BigInt(position.credit);
this.pendingFee = BigInt(position.pendingFee);
this.lastLossFactor = BigInt(position.lastLossFactor);
this.lastAccrual = BigInt(position.lastAccrual);
this.debt = BigInt(position.debt);
this.collateralBitmap = BigInt(position.collateralBitmap);
this.collateral = position.collateral.map((assets) => BigInt(assets));
}
/**
* Returns credit net of pending fee, floored to zero for invalid raw inputs.
*
* @returns Credit minus pending fee.
* @example
* ```ts
* import { Position } from "@morpho-org/midnight-sdk";
*
* const position = new Position({
* user: "0x0000000000000000000000000000000000009000",
* marketId: "0x0000000000000000000000000000000000000000000000000000000000000001",
* credit: 1_000n,
* pendingFee: 100n,
* lastLossFactor: 0n,
* lastAccrual: 0n,
* debt: 0n,
* collateralBitmap: 1n,
* collateral: [50n],
* });
* console.log(position.faceValue);
* ```
*/
get faceValue() {
return MathLib.zeroFloorSub(this.credit, this.pendingFee);
}
}
/**
* Midnight position paired with its hydrated market.
*
* @example
* ```ts
* import { registerCustomAddresses } from "@morpho-org/morpho-ts";
* import { AccrualPosition } from "@morpho-org/midnight-sdk";
*
* registerCustomAddresses({
* addresses: {
* 31337: {
* blue: "0x0000000000000000000000000000000000000001",
* adaptiveCurveIrm: "0x0000000000000000000000000000000000000004",
* midnight: "0x0000000000000000000000000000000000001000",
* },
* },
* });
*
* const position = new AccrualPosition(
* {
* user: "0x0000000000000000000000000000000000009000",
* credit: 1_000n,
* pendingFee: 100n,
* lastLossFactor: 0n,
* lastAccrual: 1_000n,
* debt: 0n,
* collateralBitmap: 1n,
* collateral: [50n],
* },
* {
* params: {
* chainId: 31337,
* 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,
* },
* );
* console.log(position.market.id);
* ```
*/
export class AccrualPosition extends Position {
_market;
/**
* @throws {InvalidMarketParameterError} when `position` carries a `marketId` that differs from `market.id`.
*/
constructor(position, market) {
const _market = Market.from(market);
const carriedMarketId = "marketId" in position ? position.marketId : undefined;
if (carriedMarketId !== undefined &&
(typeof carriedMarketId !== "string" ||
carriedMarketId.toLowerCase() !== _market.id.toLowerCase())) {
throw new InvalidMarketParameterError({
parameter: "marketId",
value: carriedMarketId,
instruction: "Pair the position with the market it was read from.",
});
}
super({ ...position, marketId: _market.id });
this._market = _market;
}
/** Hydrated market for this position. */
get market() {
return this._market;
}
/**
* Returns the maximum credit units currently withdrawable from this position.
*
* The protocol capacity is limited by both the accrued position credit and
* the market's currently withdrawable liquidity. Default redemption flows may
* still choose the position face value.
*
* @returns Maximum credit units currently withdrawable.
* @example
* ```ts
* import { AccrualPosition } from "@morpho-org/midnight-sdk";
*
* const position = new AccrualPosition(
* {
* user: "0x0000000000000000000000000000000000009000",
* credit: 1_000n,
* pendingFee: 100n,
* lastLossFactor: 0n,
* lastAccrual: 1_000n,
* debt: 0n,
* collateralBitmap: 1n,
* collateral: [50n],
* },
* {
* 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: 950n,
* continuousFeeCredit: 0n,
* settlementFeeCbps: [1, 2, 3, 4, 5, 6, 7],
* continuousFee: 10,
* tickSpacing: 4,
* },
* );
* console.log(position.withdrawable); // 950n
* ```
*/
get withdrawable() {
return PositionUtils.getWithdrawable({
position: this,
market: this.market,
});
}
/**
* Returns the position collateral balance at an index configured by the market.
*
* @param index - Collateral index.
* @returns Collateral balance, or undefined when the index is not configured.
* @example
* ```ts
* import { registerCustomAddresses } from "@morpho-org/morpho-ts";
* import { AccrualPosition } from "@morpho-org/midnight-sdk";
*
* registerCustomAddresses({
* addresses: {
* 31337: {
* blue: "0x0000000000000000000000000000000000000001",
* adaptiveCurveIrm: "0x0000000000000000000000000000000000000004",
* midnight: "0x0000000000000000000000000000000000001000",
* },
* },
* });
*
* const position = new AccrualPosition(
* {
* user: "0x0000000000000000000000000000000000009000",
* credit: 1_000n,
* pendingFee: 100n,
* lastLossFactor: 0n,
* lastAccrual: 1_000n,
* debt: 0n,
* collateralBitmap: 1n,
* collateral: [50n],
* },
* {
* params: {
* chainId: 31337,
* 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,
* },
* );
* const balance = position.getCollateralBalanceByIndex(0);
* console.log(balance);
* ```
*/
getCollateralBalanceByIndex(index) {
const normalizedIndex = BigInt(index);
if (_try(() => this.market.getCollateralByIndex(normalizedIndex), UnknownCollateralIndexError) == null) {
return undefined;
}
return this.collateral[Number(normalizedIndex)] ?? 0n;
}
/**
* Returns the position collateral balance for a configured token.
*
* @param token - Collateral token address.
* @returns Collateral balance, or undefined when the token is not configured.
* @example
* ```ts
* import { registerCustomAddresses } from "@morpho-org/morpho-ts";
* import { AccrualPosition } from "@morpho-org/midnight-sdk";
*
* registerCustomAddresses({
* addresses: {
* 31337: {
* blue: "0x0000000000000000000000000000000000000001",
* adaptiveCurveIrm: "0x0000000000000000000000000000000000000004",
* midnight: "0x0000000000000000000000000000000000001000",
* },
* },
* });
*
* const collateralToken = "0x0000000000000000000000000000000000007000";
* const position = new AccrualPosition(
* {
* user: "0x0000000000000000000000000000000000009000",
* credit: 1_000n,
* pendingFee: 100n,
* lastLossFactor: 0n,
* lastAccrual: 1_000n,
* debt: 0n,
* collateralBitmap: 1n,
* collateral: [50n],
* },
* {
* params: {
* chainId: 31337,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: collateralToken,
* 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,
* },
* );
* const balance = position.getCollateralBalanceByToken(collateralToken);
* console.log(balance);
* ```
*/
getCollateralBalanceByToken(token) {
const index = this.market.getCollateralIndexByToken(token);
return index == null ? undefined : this.getCollateralBalanceByIndex(index);
}
/**
* Returns the market settlement fee at a timestamp.
*
* @param timestamp - Timestamp used to compute time to maturity.
* @returns WAD-scaled settlement fee.
* @example
* ```ts
* import { registerCustomAddresses } from "@morpho-org/morpho-ts";
* import { AccrualPosition } from "@morpho-org/midnight-sdk";
*
* registerCustomAddresses({
* addresses: {
* 31337: {
* blue: "0x0000000000000000000000000000000000000001",
* adaptiveCurveIrm: "0x0000000000000000000000000000000000000004",
* midnight: "0x0000000000000000000000000000000000001000",
* },
* },
* });
*
* const position = new AccrualPosition(
* {
* user: "0x0000000000000000000000000000000000009000",
* credit: 1_000n,
* pendingFee: 100n,
* lastLossFactor: 0n,
* lastAccrual: 1_000n,
* debt: 0n,
* collateralBitmap: 1n,
* collateral: [50n],
* },
* {
* params: {
* chainId: 31337,
* 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,
* },
* );
* const fee = position.getSettlementFee(0n);
* console.log(fee);
* ```
*/
getSettlementFee(timestamp) {
return this.market.getSettlementFee(this.market.timeToMaturity(timestamp));
}
/**
* Returns a new position locally accrued like Midnight `updatePositionView`.
*
* @param timestamp - Timestamp at which to accrue. Must be greater than or equal to `lastAccrual`.
* @returns New accrual position 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 { registerCustomAddresses } from "@morpho-org/morpho-ts";
* import { AccrualPosition } from "@morpho-org/midnight-sdk";
*
* registerCustomAddresses({
* addresses: {
* 31337: {
* blue: "0x0000000000000000000000000000000000000001",
* adaptiveCurveIrm: "0x0000000000000000000000000000000000000004",
* midnight: "0x0000000000000000000000000000000000001000",
* },
* },
* });
*
* const position = new AccrualPosition(
* {
* user: "0x0000000000000000000000000000000000009000",
* credit: 1_000n,
* pendingFee: 100n,
* lastLossFactor: 0n,
* lastAccrual: 1_000n,
* debt: 0n,
* collateralBitmap: 1n,
* collateral: [50n],
* },
* {
* params: {
* chainId: 31337,
* 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,
* },
* );
* const accrued = position.accrueInterest(1_500n);
* console.log(accrued.credit);
* ```
*/
accrueInterest(timestamp) {
const { position, market } = PositionUtils.accrueInterest({
position: this,
market: this.market,
timestamp,
});
return new AccrualPosition(position, market);
}
}