openbook integration (#208)

This commit is contained in:
frank
2024-08-07 11:47:25 -05:00
committed by GitHub
parent f9fd110ebc
commit bb83b4789b
5 changed files with 94 additions and 3 deletions

View File

@@ -109,6 +109,7 @@ export class DLOBSubscriberIO extends DLOBSubscriber {
updateOnChange: false,
fallbackL2Generators: [
config.spotMarketSubscribers[market.marketIndex].phoenix,
config.spotMarketSubscribers[market.marketIndex].openbook,
].filter((a) => !!a),
});
}

View File

@@ -45,6 +45,7 @@ import {
validateDlobQuery,
getAccountFromId,
getRawAccountFromId,
getOpenbookSubscriber,
} from './utils/utils';
import FEATURE_FLAGS from './utils/featureFlags';
import { getDLOBProviderFromOrderSubscriber } from './dlobProvider';
@@ -144,6 +145,7 @@ const initializeAllMarketSubscribers = async (driftClient: DriftClient) => {
for (const market of sdkConfig.SPOT_MARKETS) {
markets[market.marketIndex] = {
phoenix: undefined,
openbook: undefined,
};
if (market.phoenixMarket) {
@@ -177,6 +179,28 @@ const initializeAllMarketSubscribers = async (driftClient: DriftClient) => {
}
}
}
if (market.openbookMarket) {
const openbookConfigAccount =
await driftClient.getOpenbookV2FulfillmentConfig(market.openbookMarket);
if (isVariant(openbookConfigAccount.status, 'enabled')) {
const openbookSubscriber = getOpenbookSubscriber(
driftClient,
market,
sdkConfig
);
await openbookSubscriber.subscribe();
try {
openbookSubscriber.getL2Asks();
openbookSubscriber.getL2Bids();
markets[market.marketIndex].openbook = openbookSubscriber;
} catch (e) {
logger.info(
`Excluding openbook for ${market.marketIndex}, error: ${e}`
);
}
}
}
}
return markets;
@@ -633,6 +657,7 @@ const main = async (): Promise<void> => {
numVammOrders,
includeVamm,
includePhoenix,
includeOpenbook,
includeOracle,
} = req.query;
@@ -676,7 +701,11 @@ const main = async (): Promise<void> => {
}
}
}
} else if (isSpot && `${includePhoenix}`?.toLowerCase() === 'true') {
} else if (
isSpot &&
`${includePhoenix}`?.toLowerCase() === 'true' &&
`${includeOpenbook}`?.toLowerCase() === 'true'
) {
const redisClient = spotMarketRedisMap.get(normedMarketIndex).client;
const redisL2 = await redisClient.get(
`last_update_orderbook_spot_${normedMarketIndex}`
@@ -720,6 +749,8 @@ const main = async (): Promise<void> => {
? [
`${includePhoenix}`.toLowerCase() === 'true' &&
MARKET_SUBSCRIBERS[normedMarketIndex].phoenix,
`${includeOpenbook}`.toLowerCase() === 'true' &&
MARKET_SUBSCRIBERS[normedMarketIndex].openbook,
].filter((a) => !!a)
: [],
});
@@ -755,6 +786,7 @@ const main = async (): Promise<void> => {
depth,
includeVamm,
includePhoenix,
includeOpenbook,
includeOracle,
} = req.query;
@@ -765,6 +797,7 @@ const main = async (): Promise<void> => {
depth: depth as string | undefined,
includeVamm: includeVamm as string | undefined,
includePhoenix: includePhoenix as string | undefined,
includeOpenbook: includeOpenbook as string | undefined,
includeOracle: includeOracle as string | undefined,
});
@@ -831,7 +864,8 @@ const main = async (): Promise<void> => {
}
} else if (
isSpot &&
normedParam['includePhoenix']?.toLowerCase() === 'true'
normedParam['includePhoenix']?.toLowerCase() === 'true' &&
normedParam['includeOpenbook']?.toLowerCase() === 'true'
) {
const redisClient =
spotMarketRedisMap.get(normedMarketIndex).client;
@@ -880,6 +914,8 @@ const main = async (): Promise<void> => {
? [
`${normedParam['includePhoenix']}`.toLowerCase() === 'true' &&
MARKET_SUBSCRIBERS[normedMarketIndex].phoenix,
`${normedParam['includeOpenbook']}`.toLowerCase() ===
'true' && MARKET_SUBSCRIBERS[normedMarketIndex].openbook,
].filter((a) => !!a)
: [],
});

View File

@@ -22,7 +22,12 @@ import {
import { RedisClient, RedisClientPrefix } from '@drift/common';
import { logger, setLogLevel } from '../utils/logger';
import { SubscriberLookup, parsePositiveIntArray, sleep } from '../utils/utils';
import {
SubscriberLookup,
getOpenbookSubscriber,
parsePositiveIntArray,
sleep,
} from '../utils/utils';
import {
DLOBSubscriberIO,
wsMarketInfo,
@@ -233,6 +238,34 @@ const initializeAllMarketSubscribers = async (driftClient: DriftClient) => {
}
}
}
if (marketConfig.openbookMarket) {
const openbookMarketAccount =
await driftClient.getOpenbookV2FulfillmentConfig(
marketConfig.openbookMarket
);
if (isVariant(openbookMarketAccount.status, 'enabled')) {
logger.info(
`Loading openbook subscriber for spot market ${market.marketIndex}`
);
const openbookSubscriber = getOpenbookSubscriber(
driftClient,
marketConfig,
sdkConfig
);
await openbookSubscriber.subscribe();
try {
openbookSubscriber.getL2Asks();
openbookSubscriber.getL2Bids();
markets[market.marketIndex].openbook = openbookSubscriber;
} catch (e) {
logger.info(
`Excluding openbook for ${market.marketIndex}, error: ${e}`
);
}
}
}
}
return markets;

View File

@@ -212,6 +212,10 @@ const main = async () => {
pubkeysForMarket.push(market.phoenixMarket.toString());
}
if (market.openbookMarket) {
pubkeysForMarket.push(market.openbookMarket.toString());
}
spotMarketPubkeys.push({
marketIndex: market.marketIndex,
pubkeys: pubkeysForMarket,

View File

@@ -4,6 +4,7 @@ import {
L2OrderBook,
L3OrderBook,
MarketType,
OpenbookV2Subscriber,
OraclePriceData,
PerpMarkets,
PhoenixSubscriber,
@@ -436,9 +437,25 @@ export const getSerumSubscriber = (
});
};
export const getOpenbookSubscriber = (
driftClient: DriftClient,
marketConfig: SpotMarketConfig,
sdkConfig
): OpenbookV2Subscriber => {
return new OpenbookV2Subscriber({
connection: driftClient.connection,
programId: new PublicKey(sdkConfig.OPENBOOK),
marketAddress: marketConfig.openbookMarket,
accountSubscription: {
type: 'websocket',
},
});
};
export type SubscriberLookup = {
[marketIndex: number]: {
phoenix?: PhoenixSubscriber;
serum?: SerumSubscriber;
openbook?: OpenbookV2Subscriber;
};
};