Merge pull request #186 from drift-labs/master

global cache
This commit is contained in:
moosecat
2024-07-01 08:26:25 -07:00
committed by GitHub
2 changed files with 25 additions and 87 deletions

View File

@@ -248,14 +248,6 @@ export class DLOBSubscriberIO extends DLOBSubscriber {
bids: l2Formatted.bids.slice(0, 100), bids: l2Formatted.bids.slice(0, 100),
asks: l2Formatted.asks.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.publish( this.redisClient.publish(
`${clientPrefix}orderbook_${marketType}_${marketArgs.marketIndex}`, `${clientPrefix}orderbook_${marketType}_${marketArgs.marketIndex}`,
@@ -265,19 +257,6 @@ export class DLOBSubscriberIO extends DLOBSubscriber {
`last_update_orderbook_${marketType}_${marketArgs.marketIndex}`, `last_update_orderbook_${marketType}_${marketArgs.marketIndex}`,
l2Formatted_depth100 l2Formatted_depth100
); );
this.redisClient.set(
`last_update_orderbook_${marketType}_${marketArgs.marketIndex}_depth_100`,
l2Formatted_depth100
);
this.redisClient.set(
`last_update_orderbook_${marketType}_${marketArgs.marketIndex}_depth_20`,
l2Formatted_depth20
);
this.redisClient.set(
`last_update_orderbook_${marketType}_${marketArgs.marketIndex}_depth_5`,
l2Formatted_depth5
);
const oraclePriceData = const oraclePriceData =
marketType === 'spot' marketType === 'spot'
? this.driftClient.getOracleDataForSpotMarket(marketArgs.marketIndex) ? this.driftClient.getOracleDataForSpotMarket(marketArgs.marketIndex)

View File

@@ -681,27 +681,18 @@ const main = async (): Promise<void> => {
let l2Formatted: any; let l2Formatted: any;
if (useRedis) { if (useRedis) {
if (!isSpot && `${includeVamm}`?.toLowerCase() === 'true') { if (!isSpot && `${includeVamm}`?.toLowerCase() === 'true') {
let redisL2: string;
const redisClient = perpMarketRedisMap.get(normedMarketIndex).client; const redisClient = perpMarketRedisMap.get(normedMarketIndex).client;
if (parseInt(adjustedDepth as string) === 5) { const redisL2 = await redisClient.get(`last_update_orderbook_perp_${normedMarketIndex}`);
redisL2 = await redisClient.getRaw( const depth = Math.min(parseInt(adjustedDepth as string) ?? 1, 100);
`last_update_orderbook_perp_${normedMarketIndex}_depth_5` redisL2['bids'] = redisL2['bids'].slice(0, depth);
); redisL2['asks'] = redisL2['asks'].slice(0, depth);
} else if (parseInt(adjustedDepth as string) === 20) {
redisL2 = await redisClient.getRaw(
`last_update_orderbook_perp_${normedMarketIndex}_depth_20`
);
} else if (parseInt(adjustedDepth as string) === 100) {
redisL2 = await redisClient.getRaw(
`last_update_orderbook_perp_${normedMarketIndex}_depth_100`
);
}
if ( if (
redisL2 && redisL2 &&
dlobProvider.getSlot() - parseInt(JSON.parse(redisL2).slot) < dlobProvider.getSlot() - parseInt(redisL2['slot']) <
SLOT_STALENESS_TOLERANCE SLOT_STALENESS_TOLERANCE
) { ) {
l2Formatted = redisL2; l2Formatted = JSON.stringify(redisL2);
} else { } else {
if (redisL2 && redisClients.length > 1) { if (redisL2 && redisClients.length > 1) {
if (canRotate(normedMarketType, normedMarketIndex)) { if (canRotate(normedMarketType, normedMarketIndex)) {
@@ -714,27 +705,17 @@ const main = async (): Promise<void> => {
`${includeSerum}`?.toLowerCase() === 'true' && `${includeSerum}`?.toLowerCase() === 'true' &&
`${includePhoenix}`?.toLowerCase() === 'true' `${includePhoenix}`?.toLowerCase() === 'true'
) { ) {
let redisL2: string;
const redisClient = spotMarketRedisMap.get(normedMarketIndex).client; const redisClient = spotMarketRedisMap.get(normedMarketIndex).client;
if (parseInt(adjustedDepth as string) === 5) { const redisL2 = await redisClient.get(`last_update_orderbook_spot_${normedMarketIndex}`);
redisL2 = await redisClient.getRaw( const depth = Math.min(parseInt(adjustedDepth as string) ?? 1, 100);
`last_update_orderbook_spot_${normedMarketIndex}_depth_5` redisL2['bids'] = redisL2['bids'].slice(0, depth);
); redisL2['asks'] = redisL2['asks'].slice(0, depth);
} else if (parseInt(adjustedDepth as string) === 20) {
redisL2 = await redisClient.getRaw(
`last_update_orderbook_spot_${normedMarketIndex}_depth_20`
);
} else if (parseInt(adjustedDepth as string) === 100) {
redisL2 = await redisClient.getRaw(
`last_update_orderbook_spot_${normedMarketIndex}_depth_100`
);
}
if ( if (
redisL2 && redisL2 &&
dlobProvider.getSlot() - parseInt(JSON.parse(redisL2).slot) < dlobProvider.getSlot() - parseInt(redisL2['slot']) <
SLOT_STALENESS_TOLERANCE SLOT_STALENESS_TOLERANCE
) { ) {
l2Formatted = redisL2; l2Formatted = JSON.stringify(redisL2);
} else { } else {
if (redisL2 && redisClients.length > 1) { if (redisL2 && redisClients.length > 1) {
if (canRotate(normedMarketType, normedMarketIndex)) { if (canRotate(normedMarketType, normedMarketIndex)) {
@@ -853,29 +834,18 @@ const main = async (): Promise<void> => {
!isSpot && !isSpot &&
normedParam['includeVamm']?.toLowerCase() === 'true' normedParam['includeVamm']?.toLowerCase() === 'true'
) { ) {
let redisL2: string;
const redisClient = const redisClient =
perpMarketRedisMap.get(normedMarketIndex).client; perpMarketRedisMap.get(normedMarketIndex).client;
if (parseInt(adjustedDepth as string) === 5) { const redisL2 = await redisClient.get(`last_update_orderbook_perp_${normedMarketIndex}`);
redisL2 = await redisClient.getRaw( const depth = Math.min(parseInt(adjustedDepth as string) ?? 1, 100);
`last_update_orderbook_perp_${normedMarketIndex}_depth_5` redisL2['bids'] = redisL2['bids'].slice(0, depth);
); redisL2['asks'] = redisL2['asks'].slice(0, depth);
} else if (parseInt(adjustedDepth as string) === 20) {
redisL2 = await redisClient.getRaw(
`last_update_orderbook_perp_${normedMarketIndex}_depth_20`
);
} else if (parseInt(adjustedDepth as string) === 100) {
redisL2 = await redisClient.getRaw(
`last_update_orderbook_perp_${normedMarketIndex}_depth_100`
);
}
if (redisL2) { if (redisL2) {
const parsedRedisL2 = JSON.parse(redisL2);
if ( if (
dlobProvider.getSlot() - parseInt(parsedRedisL2.slot) < dlobProvider.getSlot() - parseInt(redisL2['slot']) <
SLOT_STALENESS_TOLERANCE SLOT_STALENESS_TOLERANCE
) { ) {
l2Formatted = parsedRedisL2; l2Formatted = redisL2;
} else { } else {
if (redisClients.length > 1) { if (redisClients.length > 1) {
if (canRotate(normedMarketType, normedMarketIndex)) { if (canRotate(normedMarketType, normedMarketIndex)) {
@@ -889,29 +859,18 @@ const main = async (): Promise<void> => {
normedParam['includePhoenix']?.toLowerCase() === 'true' && normedParam['includePhoenix']?.toLowerCase() === 'true' &&
normedParam['includeSerum']?.toLowerCase() === 'true' normedParam['includeSerum']?.toLowerCase() === 'true'
) { ) {
let redisL2: string;
const redisClient = const redisClient =
spotMarketRedisMap.get(normedMarketIndex).client; spotMarketRedisMap.get(normedMarketIndex).client;
if (parseInt(adjustedDepth as string) === 5) { const redisL2 = await redisClient.get(`last_update_orderbook_spot_${normedMarketIndex}`);
redisL2 = await redisClient.getRaw( const depth = Math.min(parseInt(adjustedDepth as string) ?? 1, 100);
`last_update_orderbook_spot_${normedMarketIndex}_depth_5` redisL2['bids'] = redisL2['bids'].slice(0, depth);
); redisL2['asks'] = redisL2['asks'].slice(0, depth);
} else if (parseInt(adjustedDepth as string) === 20) {
redisL2 = await redisClient.getRaw(
`last_update_orderbook_spot_${normedMarketIndex}_depth_20`
);
} else if (parseInt(adjustedDepth as string) === 100) {
redisL2 = await redisClient.getRaw(
`last_update_orderbook_spot_${normedMarketIndex}_depth_100`
);
}
if (redisL2) { if (redisL2) {
const parsedRedisL2 = JSON.parse(redisL2);
if ( if (
dlobProvider.getSlot() - parseInt(parsedRedisL2.slot) < dlobProvider.getSlot() - parseInt(redisL2['slot']) <
SLOT_STALENESS_TOLERANCE SLOT_STALENESS_TOLERANCE
) { ) {
l2Formatted = parsedRedisL2; l2Formatted = redisL2;
} else { } else {
if (redisClients.length > 1) { if (redisClients.length > 1) {
if (canRotate(normedMarketType, normedMarketIndex)) { if (canRotate(normedMarketType, normedMarketIndex)) {