From 95b99b2ffa353a0701b2363bba82c0dc73dc4183 Mon Sep 17 00:00:00 2001 From: wphan Date: Sun, 24 Dec 2023 01:35:17 -0800 Subject: [PATCH] pass list of markets into DLOBSubscriberIO --- src/dlob-subscriber/DLOBSubscriberIO.ts | 26 ++++++------- src/publishers/dlobPublisher.ts | 49 ++++++++++++++++--------- 2 files changed, 42 insertions(+), 33 deletions(-) diff --git a/src/dlob-subscriber/DLOBSubscriberIO.ts b/src/dlob-subscriber/DLOBSubscriberIO.ts index 7dd331f..8b88c23 100644 --- a/src/dlob-subscriber/DLOBSubscriberIO.ts +++ b/src/dlob-subscriber/DLOBSubscriberIO.ts @@ -2,16 +2,11 @@ import { BN, DLOBSubscriber, DLOBSubscriptionConfig, - DevnetPerpMarkets, - DevnetSpotMarkets, L2OrderBookGenerator, - MainnetPerpMarkets, - MainnetSpotMarkets, MarketType, groupL2, isVariant, } from '@drift-labs/sdk'; -import { driftEnv } from '../publishers/dlobPublisher'; import { RedisClient } from '../utils/redisClient'; import { SubscriberLookup, @@ -33,6 +28,11 @@ type wsMarketL2Args = { updateOnChange?: boolean; }; +export type wsMarketInfo = { + marketIndex: number; + marketName: string; +}; + export class DLOBSubscriberIO extends DLOBSubscriber { public marketL2Args: wsMarketL2Args[] = []; public lastSeenL2Formatted: Map>; @@ -41,6 +41,8 @@ export class DLOBSubscriberIO extends DLOBSubscriber { constructor( config: DLOBSubscriptionConfig & { redisClient: RedisClient; + perpMarketsInfos: wsMarketInfo[]; + spotMarketsInfos: wsMarketInfo[]; spotMarketSubscribers: SubscriberLookup; } ) { @@ -52,17 +54,11 @@ export class DLOBSubscriberIO extends DLOBSubscriber { this.lastSeenL2Formatted.set(MarketType.SPOT, new Map()); this.lastSeenL2Formatted.set(MarketType.PERP, new Map()); - // Add all active markets to the market L2Args - const perpMarkets = - driftEnv === 'devnet' ? DevnetPerpMarkets : MainnetPerpMarkets; - const spotMarkets = - driftEnv === 'devnet' ? DevnetSpotMarkets : MainnetSpotMarkets; - - for (const market of perpMarkets) { + for (const market of config.perpMarketsInfos) { this.marketL2Args.push({ marketIndex: market.marketIndex, marketType: MarketType.PERP, - marketName: market.symbol, + marketName: market.marketName, depth: -1, numVammOrders: 100, includeVamm: true, @@ -70,11 +66,11 @@ export class DLOBSubscriberIO extends DLOBSubscriber { fallbackL2Generators: [], }); } - for (const market of spotMarkets) { + for (const market of config.spotMarketsInfos) { this.marketL2Args.push({ marketIndex: market.marketIndex, marketType: MarketType.SPOT, - marketName: market.symbol, + marketName: market.marketName, depth: -1, includeVamm: false, updateOnChange: true, diff --git a/src/publishers/dlobPublisher.ts b/src/publishers/dlobPublisher.ts index 9858379..f3063d6 100644 --- a/src/publishers/dlobPublisher.ts +++ b/src/publishers/dlobPublisher.ts @@ -25,7 +25,10 @@ import { getSerumSubscriber, sleep, } from '../utils/utils'; -import { DLOBSubscriberIO } from '../dlob-subscriber/DLOBSubscriberIO'; +import { + DLOBSubscriberIO, + wsMarketInfo, +} from '../dlob-subscriber/DLOBSubscriberIO'; import { RedisClient } from '../utils/redisClient'; import { DLOBProvider, @@ -86,20 +89,17 @@ let MARKET_SUBSCRIBERS: SubscriberLookup = {}; const getMarketsAndOraclesToLoad = ( sdkConfig: any ): { - perpMarketIndexes?: number[]; - spotMarketIndexes?: number[]; + perpMarketInfos: wsMarketInfo[]; + spotMarketInfos: wsMarketInfo[]; oracleInfos?: OracleInfo[]; } => { const oracleInfos: OracleInfo[] = []; const oraclesTracked = new Set(); - let perpMarketIndexes: number[] = []; - let spotMarketIndexes: number[] = []; + const perpMarketInfos: wsMarketInfo[] = []; + const spotMarketInfos: wsMarketInfo[] = []; if (PERP_MARKETS_TO_LOAD!.length > 0) { - perpMarketIndexes = PERP_MARKETS_TO_LOAD; - logger.info(`DlobPublisher tracking perp markets: ${perpMarketIndexes}`); - - for (const idx of perpMarketIndexes) { + for (const idx of PERP_MARKETS_TO_LOAD) { const perpMarketConfig = sdkConfig.PERP_MARKETS[idx] as PerpMarketConfig; if (!perpMarketConfig) { throw new Error(`Perp market config for ${idx} not found`); @@ -113,14 +113,18 @@ const getMarketsAndOraclesToLoad = ( }); oraclesTracked.add(oracleKey); } + perpMarketInfos.push({ + marketIndex: perpMarketConfig.marketIndex, + marketName: perpMarketConfig.symbol, + }); } + logger.info( + `DlobPublisher tracking perp markets: ${JSON.stringify(perpMarketInfos)}` + ); } if (SPOT_MARKETS_TO_LOAD!.length > 0) { - spotMarketIndexes = SPOT_MARKETS_TO_LOAD; - logger.info(`DlobPublisher tracking spot markets: ${spotMarketIndexes}`); - - for (const idx of spotMarketIndexes) { + for (const idx of SPOT_MARKETS_TO_LOAD) { const spotMarketConfig = sdkConfig.PERP_MARKETS[idx] as PerpMarketConfig; if (!spotMarketConfig) { throw new Error(`Spot market config for ${idx} not found`); @@ -134,12 +138,19 @@ const getMarketsAndOraclesToLoad = ( }); oraclesTracked.add(oracleKey); } + spotMarketInfos.push({ + marketIndex: spotMarketConfig.marketIndex, + marketName: spotMarketConfig.symbol, + }); } + logger.info( + `DlobPublisher tracking spot markets: ${JSON.stringify(spotMarketInfos)}` + ); } return { - perpMarketIndexes, - spotMarketIndexes, + perpMarketInfos, + spotMarketInfos, oracleInfos, }; }; @@ -259,7 +270,7 @@ const main = async () => { }; } - const { perpMarketIndexes, spotMarketIndexes, oracleInfos } = + const { perpMarketInfos, spotMarketInfos, oracleInfos } = getMarketsAndOraclesToLoad(sdkConfig); driftClient = new DriftClient({ connection, @@ -267,8 +278,8 @@ const main = async () => { programID: clearingHousePublicKey, accountSubscription, env: driftEnv, - perpMarketIndexes, - spotMarketIndexes, + perpMarketIndexes: perpMarketInfos.map((m) => m.marketIndex), + spotMarketIndexes: spotMarketInfos.map((m) => m.marketIndex), oracleInfos, }); @@ -358,6 +369,8 @@ const main = async () => { updateFrequency: ORDERBOOK_UPDATE_INTERVAL, redisClient, spotMarketSubscribers: MARKET_SUBSCRIBERS, + perpMarketsInfos, + spotMarketsInfos, }); await dlobSubscriber.subscribe(); if (useWebsocket && !FEATURE_FLAGS.DISABLE_GPA_REFRESH) {