chore(snapshot): sync workspace state
This commit is contained in:
@@ -12,9 +12,8 @@ import {
|
||||
initialize,
|
||||
MarketType,
|
||||
getVariant,
|
||||
isVariant,
|
||||
} from '@drift-labs/sdk';
|
||||
import { RedisClient, RedisClientPrefix } from '@drift/common/clients';
|
||||
|
||||
import { logger, setLogLevel } from './utils/logger';
|
||||
|
||||
import * as http from 'http';
|
||||
@@ -22,6 +21,7 @@ import { handleHealthCheck } from './core/middleware';
|
||||
import { errorHandler, selectMostRecentBySlot, sleep } from './utils/utils';
|
||||
import { setGlobalDispatcher, Agent } from 'undici';
|
||||
import { Metrics } from './core/metricsV2';
|
||||
import { RedisClient, parseRedisClients } from './utils/redisClients';
|
||||
|
||||
setGlobalDispatcher(
|
||||
new Agent({
|
||||
@@ -32,16 +32,7 @@ setGlobalDispatcher(
|
||||
require('dotenv').config();
|
||||
|
||||
// Reading in Redis env vars
|
||||
const envClients = [];
|
||||
const clients = process.env.REDIS_CLIENT?.trim()
|
||||
.replace(/^\[|\]$/g, '')
|
||||
.split(/\s*,\s*/);
|
||||
|
||||
clients?.forEach((client) => envClients.push(RedisClientPrefix[client]));
|
||||
|
||||
const REDIS_CLIENTS = envClients.length
|
||||
? envClients
|
||||
: [RedisClientPrefix.DLOB, RedisClientPrefix.DLOB_HELIUS];
|
||||
const REDIS_CLIENTS = parseRedisClients(process.env.REDIS_CLIENT);
|
||||
console.log('Redis Clients:', REDIS_CLIENTS);
|
||||
|
||||
const driftEnv = (process.env.ENV || 'devnet') as DriftEnv;
|
||||
@@ -203,6 +194,112 @@ const main = async (): Promise<void> => {
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/l2', async (req, res, next) => {
|
||||
try {
|
||||
const { marketName, marketIndex, marketType, depth, includeIndicative } =
|
||||
req.query;
|
||||
|
||||
let normedMarketType: MarketType | undefined;
|
||||
let normedMarketIndex: number | undefined;
|
||||
|
||||
if (marketName != null && String(marketName).trim() !== '') {
|
||||
const name = String(marketName).toUpperCase().trim();
|
||||
const perp = sdkConfig?.PERP_MARKETS?.find((m) => m?.symbol === name);
|
||||
if (perp) {
|
||||
normedMarketType = MarketType.PERP;
|
||||
normedMarketIndex = perp.marketIndex;
|
||||
} else {
|
||||
const spot = sdkConfig?.SPOT_MARKETS?.find((m) => m?.symbol === name);
|
||||
if (spot) {
|
||||
normedMarketType = MarketType.SPOT;
|
||||
normedMarketIndex = spot.marketIndex;
|
||||
}
|
||||
}
|
||||
|
||||
if (normedMarketType == null || normedMarketIndex == null) {
|
||||
res.status(400).send('Bad Request: unrecognized marketName');
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
if (marketIndex == null || marketType == null) {
|
||||
res
|
||||
.status(400)
|
||||
.send(
|
||||
'Bad Request: (marketName) or (marketIndex and marketType) must be supplied'
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const mt = String(marketType).toLowerCase().trim();
|
||||
const idx = parseInt(String(marketIndex), 10);
|
||||
if (!Number.isFinite(idx) || idx < 0) {
|
||||
res.status(400).send('Bad Request: invalid marketIndex');
|
||||
return;
|
||||
}
|
||||
|
||||
if (mt === 'perp') {
|
||||
if (!sdkConfig?.PERP_MARKETS?.[idx]) {
|
||||
res.status(400).send('Bad Request: invalid marketIndex');
|
||||
return;
|
||||
}
|
||||
normedMarketType = MarketType.PERP;
|
||||
normedMarketIndex = idx;
|
||||
} else if (mt === 'spot') {
|
||||
if (!sdkConfig?.SPOT_MARKETS?.[idx]) {
|
||||
res.status(400).send('Bad Request: invalid marketIndex');
|
||||
return;
|
||||
}
|
||||
normedMarketType = MarketType.SPOT;
|
||||
normedMarketIndex = idx;
|
||||
} else {
|
||||
res
|
||||
.status(400)
|
||||
.send('Bad Request: marketType must be either "spot" or "perp"');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const includeIndicativeStr =
|
||||
(includeIndicative as string)?.toLowerCase() === 'true';
|
||||
const depthToUse = Math.min(parseInt(String(depth ?? '100'), 10) || 1, 100);
|
||||
const indicativeSuffix = includeIndicativeStr ? '_indicative' : '';
|
||||
|
||||
const redisL2 = await fetchFromRedis(
|
||||
`last_update_orderbook_${getVariant(normedMarketType)}_${normedMarketIndex}${indicativeSuffix}`,
|
||||
selectMostRecentBySlot
|
||||
);
|
||||
|
||||
if (
|
||||
redisL2 &&
|
||||
slotSubscriber.getSlot() - redisL2['slot'] < SLOT_STALENESS_TOLERANCE
|
||||
) {
|
||||
redisL2['bids'] = redisL2['bids']?.slice(0, depthToUse);
|
||||
redisL2['asks'] = redisL2['asks']?.slice(0, depthToUse);
|
||||
cacheHitCounter.add(1, {
|
||||
miss: false,
|
||||
path: req.baseUrl + req.path,
|
||||
marketIndex: normedMarketIndex,
|
||||
marketType: isVariant(normedMarketType, 'spot') ? 'spot' : 'perp',
|
||||
});
|
||||
res.writeHead(200);
|
||||
res.end(JSON.stringify(redisL2));
|
||||
return;
|
||||
} else {
|
||||
cacheHitCounter.add(1, {
|
||||
miss: true,
|
||||
path: req.baseUrl + req.path,
|
||||
marketIndex: normedMarketIndex,
|
||||
marketType: isVariant(normedMarketType, 'spot') ? 'spot' : 'perp',
|
||||
});
|
||||
res.writeHead(500);
|
||||
res.end(JSON.stringify({ error: 'No cached L2 found' }));
|
||||
return;
|
||||
}
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
server.listen(serverPort, () => {
|
||||
logger.info(
|
||||
`DLOB server lite listening on port http://localhost:${serverPort}`
|
||||
|
||||
Reference in New Issue
Block a user