Market Entities
The Market Entities module provides classes for interacting with market-related entities in the Mirage Protocol, including positions, limit orders, and markets.
Market
Represents a Mirage Protocol perpetuals market.
Properties
class Market {
readonly totalLongMargin: BigNumber // Total long margin
readonly totalShortMargin: BigNumber // Total short margin
readonly longOpenInterest: BigNumber // Long open interest in mUSD
readonly shortOpenInterest: BigNumber // Short open interest in mUSD
readonly longFundingAccumulated: BigNumber // Accumulated long funding
readonly shortFundingAccumulated: BigNumber // Accumulated short funding
readonly nextFundingRate: BigNumber // Next funding rate
readonly lastFundingRound: Date // Last funding timestamp
readonly longCloseOnly: boolean // Long positions close-only
readonly shortCloseOnly: boolean // Short positions close-only
readonly objectAddress: string // Market collection address
// Fee Configuration
readonly minTakerFee: number // Minimum taker fee at equal OI
readonly maxTakerFee: number // Maximum taker fee at max OI imbalance
readonly minMakerFee: number // Minimum maker fee at large OI imbalance
readonly maxMakerFee: number // Maximum maker fee at equal OI
// Funding Configuration
readonly minFundingRate: number // Minimum funding rate
readonly maxFundingRate: number // Maximum funding rate
readonly baseFundingRate: number // Base funding rate
readonly fundingInterval: BigNumber // Funding payment interval
// Market Configuration
readonly maxOpenInterest: BigNumber // Maximum total OI allowed
readonly maxOpenInterestImbalance: BigNumber // Maximum OI imbalance allowed
readonly maintenanceMargin: number // Base maintenance margin percentage
readonly maxLeverage: number // Maximum allowed leverage
readonly minOrderSize: BigNumber // Minimum order size in mUSD
readonly maxOrderSize: BigNumber // Maximum order size in mUSD
}Methods
getOpenCloseFee
Calculates the fee for opening or closing a position based on market conditions and position details.
getOpenCloseFee(
side: PositionSide, // Position side (LONG/SHORT)
isClose: boolean, // Whether this is a closing operation
positionSize: BigNumber, // Size of the position
perpPrice: BigNumber, // Current perpetual price
marginPrice: BigNumber // Current margin token price
): BigNumbergetSkew
Calculates the open interest skew for the market, indicating imbalance between long and short positions.
getSkew(
perpPrice: BigNumber, // Current perpetual price
isClose: boolean // Whether this is a closing operation
): BigNumber // Returns skew amount (positive for long skew)calculateFee
Determines the appropriate fee based on market skew and whether the order is taker or maker.
calculateFee(
skew: BigNumber, // Current market skew
is_taker: boolean // Whether the order is a taker order
): BigNumber // Returns calculated fee amountPosition
Represents a position in a Mirage market.
Properties
class Position {
readonly tokenId: bigint // Position token ID
readonly market: Market // Associated market
readonly side: PositionSide // Position side
readonly openingPrice: BigNumber // Opening price
readonly margin: BigNumber // Position margin
readonly positionSize: BigNumber // Position size
readonly fundingAccrued: BigNumber // Accrued funding
readonly maintenanceMargin: BigNumber // Maintenance margin
readonly strategyAddresses: string[] // Strategy addresses
readonly feesPaid: BigNumber // Total fees paid
readonly fundingPaid: BigNumber // Total funding paid
readonly tradePnl: BigNumber // Trade PnL
readonly realizedPnl: BigNumber // Realized PnL
readonly leverage: BigNumber // Position leverage
readonly objectAddress: string // Position object address
}Methods
isOpen
Checks if the position is currently open.
isOpen(): boolean // Returns true if position is opengetLiquidationPrice
Calculates the price at which the position would be liquidated based on current market conditions.
getLiquidationPrice(
perpPrice: BigNumber, // Current perpetual price
marginPrice: BigNumber // Current margin token price
): BigNumber // Returns liquidation pricegetPositionMaintenanceMarginMUSD
Calculates the required maintenance margin in mUSD for the position.
getPositionMaintenanceMarginMUSD(
perpPrice: BigNumber, // Current perpetual price
marginPrice: BigNumber // Current margin token price
): BigNumber // Returns maintenance margin in mUSDstatic getLeverage
Calculates the current leverage of a position.
static getLeverage(
position: Position, // Position to calculate leverage for
perpetualPrice: number, // Current perpetual price
marginPrice: number // Current margin token price
): number // Returns leverage ratio (e.g., 2 for 2x leverage)static estimatePnl
Estimates the current profit/loss for a position in terms of the margin token.
static estimatePnl(
position: Position, // Position to estimate PnL for
perpetualPrice: number, // Current perpetual price
marginPrice: number // Current margin token price
): number // Returns estimated PnL in margin tokensstatic estimatePercentPnl
Calculates the percentage profit/loss for a position.
static estimatePercentPnl(
position: Position, // Position to calculate percent PnL for
perpetualPrice: number, // Current perpetual price
marginPrice: number // Current margin token price
): number // Returns PnL as a percentageLimitOrder
Represents a limit order in the market.
Types
type LimitOrderData = {
is_decrease_only: boolean
position_size: string
is_long: boolean
trigger_price: BigNumber
triggers_above: boolean
max_price_slippage: string
expiration: string
}Properties
class LimitOrder {
readonly side: PositionSide // Order side
readonly isDecreaseOnly: boolean // Decrease-only flag
readonly positionSize: BigNumber // Order size
readonly margin: BigNumber // Order margin
readonly triggerPrice: BigNumber // Trigger price
readonly triggersAbove: boolean // Trigger direction
readonly maxPriceSlippage: BigNumber // Maximum price slippage
readonly expiration: bigint // Order expiration
readonly marketObjectAddress: string // Market object address
readonly positionObjectAddress: string // Position object address
readonly objectAddress: string // Order object address
}Methods
static gtcExpiration
Returns the expiration value for a good-til-cancelled order.
static gtcExpiration(): bigint // Returns U64_MAXisGtc
Checks if the order is good-til-cancelled.
isGtc(): boolean // Returns true if order is GTCstatic percentSlippageToPriceSlippage
Converts a percentage slippage to an absolute price slippage amount.
static percentSlippageToPriceSlippage(
triggerPrice: BigNumber, // Base trigger price
percentSlippage: number // Slippage percentage in basis points
): BigNumber // Returns price slippage amountExample Usage
Creating a Market Instance
// Get market resources
const marketAddr = "0x123..."
const marketResources = await aptosClient.getAccountResources({
accountAddress: marketAddr
})
// Create market instance using client
const market = mirageClient.marketEntities.createMarket(
marketResources,
marketAddr
)Creating a Position Instance
// Get position resources
const positionAddr = "0x456..."
const positionResources = await aptosClient.getAccountResources({
accountAddress: positionAddr
})
// Create position instance using client
const position = mirageClient.marketEntities.createPosition(
positionResources,
market, // Requires previously created market instance
positionAddr
)Creating a Limit Order Instance
// Get limit order resources
const orderAddr = "0x789..."
const limitOrderResources = await aptosClient.getAccountResources({
accountAddress: orderAddr
})
// Create limit order instance using client
const limitOrder = mirageClient.marketEntities.createLimitOrder(
limitOrderResources,
orderAddr
)