pass list of markets into DLOBSubscriberIO

This commit is contained in:
wphan
2023-12-24 01:35:17 -08:00
parent 70d9227b6c
commit eb4faa6cf1
2 changed files with 42 additions and 33 deletions

View File

@@ -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<MarketType, Map<number, any>>;
@@ -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,

View File

@@ -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) {