From ea50319a69aa6b43b37796f65323bfd2ab13136f Mon Sep 17 00:00:00 2001 From: Nour Alharithi Date: Wed, 26 Jun 2024 21:24:58 -0700 Subject: [PATCH] global cache --- src/dlob-subscriber/DLOBSubscriberIO.ts | 21 ------ src/index.ts | 91 +++++++------------------ 2 files changed, 25 insertions(+), 87 deletions(-) diff --git a/src/dlob-subscriber/DLOBSubscriberIO.ts b/src/dlob-subscriber/DLOBSubscriberIO.ts index 92506a4..008d8a3 100644 --- a/src/dlob-subscriber/DLOBSubscriberIO.ts +++ b/src/dlob-subscriber/DLOBSubscriberIO.ts @@ -248,14 +248,6 @@ export class DLOBSubscriberIO extends DLOBSubscriber { 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.publish( `${clientPrefix}orderbook_${marketType}_${marketArgs.marketIndex}`, @@ -265,19 +257,6 @@ export class DLOBSubscriberIO extends DLOBSubscriber { `last_update_orderbook_${marketType}_${marketArgs.marketIndex}`, 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 = marketType === 'spot' ? this.driftClient.getOracleDataForSpotMarket(marketArgs.marketIndex) diff --git a/src/index.ts b/src/index.ts index badf414..7df554b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -681,27 +681,18 @@ const main = async (): Promise => { let l2Formatted: any; if (useRedis) { if (!isSpot && `${includeVamm}`?.toLowerCase() === 'true') { - let redisL2: string; const redisClient = perpMarketRedisMap.get(normedMarketIndex).client; - if (parseInt(adjustedDepth as string) === 5) { - redisL2 = await redisClient.getRaw( - `last_update_orderbook_perp_${normedMarketIndex}_depth_5` - ); - } 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` - ); - } + const redisL2 = await redisClient.get(`last_update_orderbook_perp_${normedMarketIndex}`); + const depth = Math.min(parseInt(adjustedDepth as string) ?? 1, 100); + redisL2['bids'] = redisL2['bids'].slice(0, depth); + redisL2['asks'] = redisL2['asks'].slice(0, depth); + if ( redisL2 && - dlobProvider.getSlot() - parseInt(JSON.parse(redisL2).slot) < + dlobProvider.getSlot() - parseInt(redisL2['slot']) < SLOT_STALENESS_TOLERANCE ) { - l2Formatted = redisL2; + l2Formatted = JSON.stringify(redisL2); } else { if (redisL2 && redisClients.length > 1) { if (canRotate(normedMarketType, normedMarketIndex)) { @@ -714,27 +705,17 @@ const main = async (): Promise => { `${includeSerum}`?.toLowerCase() === 'true' && `${includePhoenix}`?.toLowerCase() === 'true' ) { - let redisL2: string; const redisClient = spotMarketRedisMap.get(normedMarketIndex).client; - if (parseInt(adjustedDepth as string) === 5) { - redisL2 = await redisClient.getRaw( - `last_update_orderbook_spot_${normedMarketIndex}_depth_5` - ); - } 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` - ); - } + const redisL2 = await redisClient.get(`last_update_orderbook_spot_${normedMarketIndex}`); + const depth = Math.min(parseInt(adjustedDepth as string) ?? 1, 100); + redisL2['bids'] = redisL2['bids'].slice(0, depth); + redisL2['asks'] = redisL2['asks'].slice(0, depth); if ( redisL2 && - dlobProvider.getSlot() - parseInt(JSON.parse(redisL2).slot) < + dlobProvider.getSlot() - parseInt(redisL2['slot']) < SLOT_STALENESS_TOLERANCE ) { - l2Formatted = redisL2; + l2Formatted = JSON.stringify(redisL2); } else { if (redisL2 && redisClients.length > 1) { if (canRotate(normedMarketType, normedMarketIndex)) { @@ -853,29 +834,18 @@ const main = async (): Promise => { !isSpot && normedParam['includeVamm']?.toLowerCase() === 'true' ) { - let redisL2: string; const redisClient = perpMarketRedisMap.get(normedMarketIndex).client; - if (parseInt(adjustedDepth as string) === 5) { - redisL2 = await redisClient.getRaw( - `last_update_orderbook_perp_${normedMarketIndex}_depth_5` - ); - } 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` - ); - } + const redisL2 = await redisClient.get(`last_update_orderbook_perp_${normedMarketIndex}`); + const depth = Math.min(parseInt(adjustedDepth as string) ?? 1, 100); + redisL2['bids'] = redisL2['bids'].slice(0, depth); + redisL2['asks'] = redisL2['asks'].slice(0, depth); if (redisL2) { - const parsedRedisL2 = JSON.parse(redisL2); if ( - dlobProvider.getSlot() - parseInt(parsedRedisL2.slot) < + dlobProvider.getSlot() - parseInt(redisL2['slot']) < SLOT_STALENESS_TOLERANCE ) { - l2Formatted = parsedRedisL2; + l2Formatted = redisL2; } else { if (redisClients.length > 1) { if (canRotate(normedMarketType, normedMarketIndex)) { @@ -889,29 +859,18 @@ const main = async (): Promise => { normedParam['includePhoenix']?.toLowerCase() === 'true' && normedParam['includeSerum']?.toLowerCase() === 'true' ) { - let redisL2: string; const redisClient = spotMarketRedisMap.get(normedMarketIndex).client; - if (parseInt(adjustedDepth as string) === 5) { - redisL2 = await redisClient.getRaw( - `last_update_orderbook_spot_${normedMarketIndex}_depth_5` - ); - } 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` - ); - } + const redisL2 = await redisClient.get(`last_update_orderbook_spot_${normedMarketIndex}`); + const depth = Math.min(parseInt(adjustedDepth as string) ?? 1, 100); + redisL2['bids'] = redisL2['bids'].slice(0, depth); + redisL2['asks'] = redisL2['asks'].slice(0, depth); if (redisL2) { - const parsedRedisL2 = JSON.parse(redisL2); if ( - dlobProvider.getSlot() - parseInt(parsedRedisL2.slot) < + dlobProvider.getSlot() - parseInt(redisL2['slot']) < SLOT_STALENESS_TOLERANCE ) { - l2Formatted = parsedRedisL2; + l2Formatted = redisL2; } else { if (redisClients.length > 1) { if (canRotate(normedMarketType, normedMarketIndex)) {