fetch from redis
This commit is contained in:
@@ -62,8 +62,8 @@ export class DLOBSubscriberIO extends DLOBSubscriber {
|
||||
marketType: MarketType.PERP,
|
||||
marketName: market.symbol,
|
||||
depth: -1,
|
||||
includeVamm: true,
|
||||
numVammOrders: 100,
|
||||
includeVamm: true,
|
||||
updateOnChange: true,
|
||||
fallbackL2Generators: [],
|
||||
});
|
||||
@@ -124,13 +124,39 @@ export class DLOBSubscriberIO extends DLOBSubscriber {
|
||||
l2Args.marketType,
|
||||
l2Args.marketIndex
|
||||
);
|
||||
|
||||
const l2Formatted_depth100 = Object.assign({}, l2Formatted, {
|
||||
bids: l2Formatted.bids.slice(0, 100),
|
||||
asks: l2Formatted.asks.slice(0, 100),
|
||||
});
|
||||
const l2Formatted_depth20 = Object.assign({}, l2Formatted, {
|
||||
bids: l2Formatted.bids.slice(0, 20),
|
||||
asks: l2Formatted.asks.slice(0, 20),
|
||||
});
|
||||
const l2Formatted_depth5 = Object.assign({}, l2Formatted, {
|
||||
bids: l2Formatted.bids.slice(0, 5),
|
||||
asks: l2Formatted.asks.slice(0, 5),
|
||||
});
|
||||
|
||||
this.redisClient.client.publish(
|
||||
`orderbook_${marketType}_${l2Args.marketIndex}`,
|
||||
JSON.stringify(l2Formatted)
|
||||
);
|
||||
this.redisClient.client.set(
|
||||
`last_update_orderbook_${marketType}_${l2Args.marketIndex}`,
|
||||
JSON.stringify(l2Formatted)
|
||||
JSON.stringify(l2Formatted_depth100)
|
||||
);
|
||||
this.redisClient.client.set(
|
||||
`last_update_orderbook_${marketType}_${l2Args.marketIndex}_depth_100`,
|
||||
JSON.stringify(l2Formatted_depth100)
|
||||
);
|
||||
this.redisClient.client.set(
|
||||
`last_update_orderbook_${marketType}_${l2Args.marketIndex}_depth_20`,
|
||||
JSON.stringify(l2Formatted_depth20)
|
||||
);
|
||||
this.redisClient.client.set(
|
||||
`last_update_orderbook_${marketType}_${l2Args.marketIndex}_depth_5`,
|
||||
JSON.stringify(l2Formatted_depth5)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
123
src/index.ts
123
src/index.ts
@@ -28,8 +28,6 @@ import {
|
||||
initialize,
|
||||
isVariant,
|
||||
OrderSubscriber,
|
||||
UserAccount,
|
||||
Order,
|
||||
} from '@drift-labs/sdk';
|
||||
|
||||
import { logger, setLogLevel } from './utils/logger';
|
||||
@@ -59,8 +57,14 @@ import {
|
||||
getDLOBProviderFromOrderSubscriber,
|
||||
getDLOBProviderFromUserMap,
|
||||
} from './dlobProvider';
|
||||
import { RedisClient } from './utils/redisClient';
|
||||
|
||||
require('dotenv').config();
|
||||
|
||||
const REDIS_HOST = process.env.REDIS_HOST || 'localhost';
|
||||
const REDIS_PORT = process.env.REDIS_PORT || '6379';
|
||||
const REDIS_PASSWORD = process.env.REDIS_PASSWORD;
|
||||
|
||||
const driftEnv = (process.env.ENV || 'devnet') as DriftEnv;
|
||||
const commitHash = process.env.COMMIT;
|
||||
//@ts-ignore
|
||||
@@ -343,6 +347,10 @@ const main = async () => {
|
||||
`DLOBSubscriber initialized in ${Date.now() - initDlobSubscriberStart} ms`
|
||||
);
|
||||
|
||||
logger.info('Connecting to redis');
|
||||
const redisClient = new RedisClient(REDIS_HOST, REDIS_PORT, REDIS_PASSWORD);
|
||||
await redisClient.connect();
|
||||
|
||||
logger.info(`Initializing all market subscribers...`);
|
||||
const initAllMarketSubscribersStart = Date.now();
|
||||
MARKET_SUBSCRIBERS = await initializeAllMarketSubscribers(driftClient);
|
||||
@@ -742,6 +750,55 @@ const main = async () => {
|
||||
adjustedDepth = '-1';
|
||||
}
|
||||
|
||||
let l2Formatted: any;
|
||||
if (
|
||||
!isSpot &&
|
||||
`${includeVamm}`.toLowerCase() === 'true' &&
|
||||
`${includeOracle}`.toLowerCase().toLowerCase() === 'true' &&
|
||||
!grouping
|
||||
) {
|
||||
if (parseInt(adjustedDepth as string) === 5) {
|
||||
l2Formatted = await redisClient.client.get(
|
||||
`last_update_orderbook_perp_${normedMarketIndex}_depth_5`
|
||||
);
|
||||
} else if (parseInt(adjustedDepth as string) === 20) {
|
||||
l2Formatted = await redisClient.client.get(
|
||||
`last_update_orderbook_perp_${normedMarketIndex}_depth_20`
|
||||
);
|
||||
} else if (parseInt(adjustedDepth as string) === 100) {
|
||||
l2Formatted = await redisClient.client.get(
|
||||
`last_update_orderbook_perp_${normedMarketIndex}_depth_100`
|
||||
);
|
||||
}
|
||||
} else if (
|
||||
isSpot &&
|
||||
`${includeSerum}`.toLowerCase() === 'true' &&
|
||||
`${includePhoenix}`.toLowerCase() === 'true' &&
|
||||
`${includeOracle}`.toLowerCase() === 'true' &&
|
||||
!grouping
|
||||
) {
|
||||
if (parseInt(adjustedDepth as string) === 5) {
|
||||
l2Formatted = await redisClient.client.get(
|
||||
`last_update_orderbook_spot_${normedMarketIndex}_depth_5`
|
||||
);
|
||||
} else if (parseInt(adjustedDepth as string) === 20) {
|
||||
l2Formatted = await redisClient.client.get(
|
||||
`last_update_orderbook_spot_${normedMarketIndex}_depth_20`
|
||||
);
|
||||
} else if (parseInt(adjustedDepth as string) === 100) {
|
||||
console.log('100');
|
||||
l2Formatted = await redisClient.client.get(
|
||||
`last_update_orderbook_spot_${normedMarketIndex}_depth_100`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (l2Formatted) {
|
||||
res.writeHead(200);
|
||||
res.end(JSON.stringify(l2Formatted));
|
||||
return;
|
||||
}
|
||||
|
||||
const l2 = dlobSubscriber.getL2({
|
||||
marketIndex: normedMarketIndex,
|
||||
marketType: normedMarketType,
|
||||
@@ -783,7 +840,7 @@ const main = async () => {
|
||||
res.end(JSON.stringify(l2Formatted));
|
||||
} else {
|
||||
// make the BNs into strings
|
||||
const l2Formatted = l2WithBNToStrings(l2);
|
||||
l2Formatted = l2WithBNToStrings(l2);
|
||||
if (`${includeOracle}`.toLowerCase() === 'true') {
|
||||
addOracletoResponse(
|
||||
l2Formatted,
|
||||
@@ -792,10 +849,10 @@ const main = async () => {
|
||||
normedMarketIndex
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
res.writeHead(200);
|
||||
res.end(JSON.stringify(l2Formatted));
|
||||
}
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
@@ -836,7 +893,8 @@ const main = async () => {
|
||||
return;
|
||||
}
|
||||
|
||||
const l2s = normedParams.map((normedParam) => {
|
||||
const l2s = await Promise.all(
|
||||
normedParams.map(async (normedParam) => {
|
||||
const { normedMarketType, normedMarketIndex, error } =
|
||||
validateDlobQuery(
|
||||
driftClient,
|
||||
@@ -859,6 +917,51 @@ const main = async () => {
|
||||
adjustedDepth = '-1';
|
||||
}
|
||||
|
||||
let l2Formatted: any;
|
||||
if (
|
||||
marketType === 'perp' &&
|
||||
normedParam['includeVamm'].toLowerCase() === 'true' &&
|
||||
normedParam['includeOracle'].toLowerCase() === 'true' &&
|
||||
!normedParam['grouping']
|
||||
) {
|
||||
if (parseInt(adjustedDepth as string) === 5) {
|
||||
l2Formatted = await redisClient.client.get(
|
||||
`last_update_orderbook_perp_${normedMarketIndex}_depth_5`
|
||||
);
|
||||
} else if (parseInt(adjustedDepth as string) === 20) {
|
||||
l2Formatted = await redisClient.client.get(
|
||||
`last_update_orderbook_perp_${normedMarketIndex}_depth_20`
|
||||
);
|
||||
} else if (parseInt(adjustedDepth as string) === 100) {
|
||||
l2Formatted = await redisClient.client.get(
|
||||
`last_update_orderbook_perp_${normedMarketIndex}_depth_100`
|
||||
);
|
||||
}
|
||||
} else if (
|
||||
marketType === 'spot' &&
|
||||
normedParam['includePhoenix'].toLowerCase() === 'true' &&
|
||||
normedParam['includeSerum'].toLowerCase() === 'true' &&
|
||||
!normedParam['grouping']
|
||||
) {
|
||||
if (parseInt(adjustedDepth as string) === 5) {
|
||||
l2Formatted = await redisClient.client.get(
|
||||
`last_update_orderbook_spot_${normedMarketIndex}_depth_5`
|
||||
);
|
||||
} else if (parseInt(adjustedDepth as string) === 20) {
|
||||
l2Formatted = await redisClient.client.get(
|
||||
`last_update_orderbook_spot_${normedMarketIndex}_depth_20`
|
||||
);
|
||||
} else if (parseInt(adjustedDepth as string) === 100) {
|
||||
l2Formatted = await redisClient.client.get(
|
||||
`last_update_orderbook_spot_${normedMarketIndex}_depth_100`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (l2Formatted) {
|
||||
return l2Formatted;
|
||||
}
|
||||
|
||||
const l2 = dlobSubscriber.getL2({
|
||||
marketIndex: normedMarketIndex,
|
||||
marketType: normedMarketType,
|
||||
@@ -890,7 +993,7 @@ const main = async () => {
|
||||
parseInt(normedParam['grouping'] as string)
|
||||
);
|
||||
|
||||
const l2Formatted = l2WithBNToStrings(
|
||||
l2Formatted = l2WithBNToStrings(
|
||||
groupL2(l2, groupingBN, finalDepth)
|
||||
);
|
||||
if (`${normedParam['includeOracle']}`.toLowerCase() === 'true') {
|
||||
@@ -901,10 +1004,9 @@ const main = async () => {
|
||||
normedMarketIndex
|
||||
);
|
||||
}
|
||||
return l2Formatted;
|
||||
} else {
|
||||
// make the BNs into strings
|
||||
const l2Formatted = l2WithBNToStrings(l2);
|
||||
l2Formatted = l2WithBNToStrings(l2);
|
||||
if (`${normedParam['includeOracle']}`.toLowerCase() === 'true') {
|
||||
addOracletoResponse(
|
||||
l2Formatted,
|
||||
@@ -913,9 +1015,10 @@ const main = async () => {
|
||||
normedMarketIndex
|
||||
);
|
||||
}
|
||||
return l2Formatted;
|
||||
}
|
||||
});
|
||||
return l2Formatted;
|
||||
})
|
||||
);
|
||||
|
||||
res.writeHead(200);
|
||||
res.end(JSON.stringify({ l2s }));
|
||||
|
||||
@@ -36,8 +36,6 @@ import { GeyserOrderSubscriber } from '../grpc/OrderSubscriberGRPC';
|
||||
|
||||
require('dotenv').config();
|
||||
const stateCommitment: Commitment = 'processed';
|
||||
const ORDERBOOK_UPDATE_INTERVAL = 400;
|
||||
const WS_FALLBACK_FETCH_INTERVAL = ORDERBOOK_UPDATE_INTERVAL * 10;
|
||||
const driftEnv = (process.env.ENV || 'devnet') as DriftEnv;
|
||||
const commitHash = process.env.COMMIT;
|
||||
const REDIS_HOST = process.env.REDIS_HOST || 'localhost';
|
||||
@@ -62,6 +60,9 @@ const useOrderSubscriber =
|
||||
const useGrpc = process.env.USE_GRPC?.toLowerCase() === 'true';
|
||||
const useWebsocket = process.env.USE_WEBSOCKET?.toLowerCase() === 'true';
|
||||
|
||||
const ORDERBOOK_UPDATE_INTERVAL = useGrpc ? 500 : 1000;
|
||||
const WS_FALLBACK_FETCH_INTERVAL = 10_000;
|
||||
|
||||
logger.info(`RPC endpoint: ${endpoint}`);
|
||||
logger.info(`WS endpoint: ${wsEndpoint}`);
|
||||
logger.info(`DriftEnv: ${driftEnv}`);
|
||||
|
||||
Reference in New Issue
Block a user