This commit is contained in:
Nour Alharithi
2024-04-17 09:22:40 -07:00
parent 202cb26b4a
commit 0bf8966e11

View File

@@ -7,11 +7,9 @@ import morgan from 'morgan';
import { Commitment, Connection, Keypair, PublicKey } from '@solana/web3.js'; import { Commitment, Connection, Keypair, PublicKey } from '@solana/web3.js';
import { import {
BulkAccountLoader,
DLOBNode, DLOBNode,
DLOBSubscriber, DLOBSubscriber,
DriftClient, DriftClient,
DriftClientSubscriptionConfig,
DriftEnv, DriftEnv,
SlotSubscriber, SlotSubscriber,
Wallet, Wallet,
@@ -92,7 +90,7 @@ const serverPort = process.env.PORT || 6969;
export const ORDERBOOK_UPDATE_INTERVAL = 1000; export const ORDERBOOK_UPDATE_INTERVAL = 1000;
const WS_FALLBACK_FETCH_INTERVAL = ORDERBOOK_UPDATE_INTERVAL * 10; const WS_FALLBACK_FETCH_INTERVAL = ORDERBOOK_UPDATE_INTERVAL * 10;
const SLOT_STALENESS_TOLERANCE = const SLOT_STALENESS_TOLERANCE =
parseInt(process.env.SLOT_STALENESS_TOLERANCE) || 20; parseInt(process.env.SLOT_STALENESS_TOLERANCE) || 35;
const ROTATION_COOLDOWN = parseInt(process.env.ROTATION_COOLDOWN) || 5000; const ROTATION_COOLDOWN = parseInt(process.env.ROTATION_COOLDOWN) || 5000;
const useWebsocket = process.env.USE_WEBSOCKET?.toLowerCase() === 'true'; const useWebsocket = process.env.USE_WEBSOCKET?.toLowerCase() === 'true';
const useRedis = process.env.USE_REDIS?.toLowerCase() === 'true'; const useRedis = process.env.USE_REDIS?.toLowerCase() === 'true';
@@ -226,63 +224,32 @@ const main = async (): Promise<void> => {
commitment: stateCommitment, commitment: stateCommitment,
}); });
// only set when polling const slotSubscriber = new SlotSubscriber(connection, {
let bulkAccountLoader: BulkAccountLoader | undefined;
// only set when using websockets
let slotSubscriber: SlotSubscriber | undefined;
let accountSubscription: DriftClientSubscriptionConfig;
if (!useWebsocket) {
bulkAccountLoader = new BulkAccountLoader(
connection,
stateCommitment,
ORDERBOOK_UPDATE_INTERVAL
);
accountSubscription = {
type: 'polling',
accountLoader: bulkAccountLoader,
};
} else {
accountSubscription = {
type: 'websocket',
commitment: stateCommitment,
};
slotSubscriber = new SlotSubscriber(connection, {
resubTimeoutMs: 5000, resubTimeoutMs: 5000,
}); });
await slotSubscriber.subscribe(); await slotSubscriber.subscribe();
}
driftClient = new DriftClient({ driftClient = new DriftClient({
connection, connection,
wallet, wallet,
programID: clearingHousePublicKey, programID: clearingHousePublicKey,
accountSubscription, accountSubscription: {
env: driftEnv,
});
let subscriptionConfig;
if (useWebsocket) {
subscriptionConfig = {
type: 'websocket', type: 'websocket',
commitment: stateCommitment, commitment: stateCommitment,
resyncIntervalMs: WS_FALLBACK_FETCH_INTERVAL, resubTimeoutMs: 60_000,
}; },
} else { env: driftEnv,
subscriptionConfig = { });
type: 'polling',
frequency: ORDERBOOK_UPDATE_INTERVAL,
commitment: stateCommitment,
};
}
let updatesReceivedTotal = 0; let updatesReceivedTotal = 0;
const orderSubscriber = new OrderSubscriber({ const orderSubscriber = new OrderSubscriber({
driftClient, driftClient,
subscriptionConfig, subscriptionConfig: {
type: 'websocket',
commitment: stateCommitment,
resubTimeoutMs: 10_000,
resyncIntervalMs: WS_FALLBACK_FETCH_INTERVAL,
},
}); });
orderSubscriber.eventEmitter.on( orderSubscriber.eventEmitter.on(
'updateReceived', 'updateReceived',
@@ -690,11 +657,7 @@ const main = async (): Promise<void> => {
let l2Formatted: any; let l2Formatted: any;
if (useRedis) { if (useRedis) {
if ( if (!isSpot && `${includeVamm}`?.toLowerCase() === 'true') {
!isSpot &&
`${includeVamm}`?.toLowerCase() === 'true' &&
`${includeOracle}`?.toLowerCase() === 'true'
) {
let redisL2: string; let redisL2: string;
const redisClient = perpMarketRedisMap.get(normedMarketIndex).client; const redisClient = perpMarketRedisMap.get(normedMarketIndex).client;
if (parseInt(adjustedDepth as string) === 5) { if (parseInt(adjustedDepth as string) === 5) {
@@ -726,8 +689,7 @@ const main = async (): Promise<void> => {
} else if ( } else if (
isSpot && isSpot &&
`${includeSerum}`?.toLowerCase() === 'true' && `${includeSerum}`?.toLowerCase() === 'true' &&
`${includePhoenix}`?.toLowerCase() === 'true' && `${includePhoenix}`?.toLowerCase() === 'true'
`${includeOracle}`?.toLowerCase() === 'true'
) { ) {
let redisL2: string; let redisL2: string;
const redisClient = spotMarketRedisMap.get(normedMarketIndex).client; const redisClient = spotMarketRedisMap.get(normedMarketIndex).client;
@@ -866,8 +828,7 @@ const main = async (): Promise<void> => {
if (useRedis) { if (useRedis) {
if ( if (
!isSpot && !isSpot &&
normedParam['includeVamm']?.toLowerCase() === 'true' && normedParam['includeVamm']?.toLowerCase() === 'true'
normedParam['includeOracle']?.toLowerCase() === 'true'
) { ) {
let redisL2: string; let redisL2: string;
const redisClient = const redisClient =