UNPKG

@d8x/perpetuals-sdk

Version:

Node TypeScript SDK for D8X Perpetual Futures

179 lines (178 loc) 7.81 kB
import MarketData from "./marketData"; import type { ExchangeInfo, MarginAccount, OrderStruct, PerpetualState, SmartContractOrder, TradeEvent } from "./nodeSDKTypes"; /** * This class handles events and stores relevant variables * as member variables. The events change the state of the member variables: * mktData : MarketData relevant market data with current state (e.g. index price) * ordersInPerpetual: Map<number, OrderStruct> all open orders for the given trader * positionInPerpetual: Map<number, MarginAccount> all open positions for the given trader * * * Get data: * - getPerpetualData(perp id (string) or symbol) : PerpetualState. This is a reference! * - getExchangeInfo() : ExchangeInfo. This is a reference! * - getCurrentPositionRisk(perp id (string) or symbol) : MarginAccount. This is a reference! * - getOrdersInPerpetualMap : Map<number, OrderStruct>. This is a reference! * - getpositionInPerpetualMap : Map<number, MarginAccount>. This is a reference! * * Construct with a trader address and a marketData object * Initialize to gather all the relevant data. * Send event variables to event handler "on<EventName>" - this updates members * - [x] onUpdateMarkPrice : emitted on proxy; updates markprice and index price data * - [x] onUpdateUpdateFundingRate : emitted on proxy; sets funding rate * - [x] onExecutionFailed : emitted on order book; removes an open order * - [x] onPerpetualLimitOrderCancelled : emitted on order book; removes an open order * - [x] onPerpetualLimitOrderCreated : emitted on order book; adds an open order to the data * - [x] async onUpdateMarginAccount : emitted on proxy; updates position data and open interest * - [x] onTrade : emitted on proxy; returns TradeEvent to be displayed */ export default class PerpetualEventHandler { private mktData; private traderAddr; private ordersInPerpetual; private positionInPerpetual; private poolIndexForPerpetual; private exchangeInfo; constructor(mktData: MarketData, traderAddr: string); /** * Call this async function to initialize the * market data */ initialize(): Promise<void>; /** * Get the current exchange info * @returns exchange info */ getExchangeInfo(): ExchangeInfo | undefined; /** * getOrdersInPerpetualMap * @returns this.ordersInPerpetual */ getOrdersInPerpetualMap(): Map<number, OrderStruct>; /** * getpositionInPerpetualMap * @returns this.positionInPerpetual */ getpositionInPerpetualMap(): Map<number, MarginAccount>; /** * Get the data for a perpetual with a given index * @param perpetualIdOrSymbol perpetual idx as string or symbol for which we want the data * @returns perpetual data for this idx */ getPerpetualData(perpetualIdOrSymbol: string): PerpetualState | undefined; /** * Get the trader's current position risk (margin account data) * @param perpetualIdOrSymbol perpetual id as string ('100003') or symbol ('BTC-USD-MATIC') * @returns undefined if no position or margin account (='position risk') */ getCurrentPositionRisk(perpetualIdOrSymbol: string): MarginAccount | undefined; /** * Update the following prices: * - index price * - collateral price * - mid-price * @param perpetualIdOrSymbol perpetual id as string ('100003') or symbol ('BTC-USD-MATIC') */ updatePrices(perpetualIdOrSymbol: string): Promise<void>; /** * Handle the event UpdateMarkPrice and update relevant * data * @param perpetualId perpetual Id * @param fMarkPricePremium premium rate in ABDK format * @param fSpotIndexPrice spot index price in ABDK format * @returns void */ onUpdateMarkPrice(perpetualId: number, fMidPricePremium: bigint, fMarkPricePremium: bigint, fSpotIndexPrice: bigint): void; /** * Handle the event UpdateFundingRate and update relevant * data * UpdateFundingRate(uint24 indexed perpetualId, int128 fFundingRate) * @param fFundingRate funding rate in ABDK format */ onUpdateUpdateFundingRate(perpetualId: number, fFundingRate: bigint): void; /** * event ExecutionFailed( uint24 indexed perpetualId, address indexed trader, bytes32 digest, string reason ); * @param perpetualId id of the perpetual * @param trader address of the trader * @param digest digest of the order/cancel order * @param reason reason why the execution failed */ onExecutionFailed(perpetualId: number, trader: string, digest: string, reason: string): void; /** * Event emitted by perpetual proxy * event PerpetualLimitOrderCancelled(bytes32 indexed orderHash); * @param orderId string order id/digest */ onPerpetualLimitOrderCancelled(orderId: string): void; /** * event PerpetualLimitOrderCreated( * uint24 indexed perpetualId, * address indexed trader, * address executorAddr, * address brokerAddr, * Order order, * bytes32 digest *) * @param perpetualId id of the perpetual * @param trader address of the trader * @param executorAddr address of the executor * @param brokerAddr address of the broker * @param Order order struct * @param digest order id */ onPerpetualLimitOrderCreated(perpetualId: number, trader: string, _executorAddr: string, _brokerAddr: string, Order: SmartContractOrder, digest: string): void; /** * This function is async -> queries the margin account * @param perpetualId id of the perpetual * @param trader trader address * @param positionId position id * @param fPositionBC position size in base currency * @param fCashCC margin collateral in margin account * @param fLockedInValueQC pos*average opening price * @param fFundingPaymentCC funding payment made * @param fOpenInterestBC open interest */ onUpdateMarginAccount(perpetualId: number, trader: string, _positionId: string, _fPositionBC: bigint, _fCashCC: bigint, _fLockedInValueQC: bigint, _fFundingPaymentCC: bigint, fOpenInterestBC: bigint): Promise<void>; /** * * @param perpetualId perpetual id * @param trader trader address * @param positionId position id * @param order order struct * @param orderDigest order id * @param newPositionSizeBC new pos size in base currency ABDK * @param price price in ABDK format * @returns trade event data in regular number format */ onTrade(perpetualId: number, _trader: string, positionId: string, _order: SmartContractOrder, orderDigest: string, newPositionSizeBC: bigint, _price: bigint): TradeEvent; /** * static function to find the number of the OrderStruct with given orderId * @param orderId id/digest of order * @param orderMap mapping for perpetualId->OrderStruct * @returns id of perpetual that contains order with id = orderId or undefined */ private static findOrderForId; /** * Delete the order with given id from the class member data * @param orderStructs array of order struct as stored for the trader and a given perpetual * @param orderId digest/order id * @returns void */ private static deleteOrder; /** * UpdateMarkPrice( * uint24 indexed perpetualId, * int128 fMarkPricePremium, * int128 fSpotIndexPrice * ) * @param fMarkPricePremium premium rate in ABDK format * @param fSpotIndexPrice spot index price in ABDK format * @returns midPrice, markPricePremium, indexPrice in float */ private static ConvertUpdateMarkPrice; }