From 8892624fc09141642bcd8d434dc0de766376fed1 Mon Sep 17 00:00:00 2001 From: Adam Fraser Date: Wed, 2 Oct 2024 16:38:02 -0400 Subject: [PATCH] Return undefined instead of hitting redis cache --- .../__tests__/lib/candles-generator.test.ts | 216 +----------------- .../ender/src/lib/candles-generator.ts | 8 +- 2 files changed, 12 insertions(+), 212 deletions(-) diff --git a/indexer/services/ender/__tests__/lib/candles-generator.test.ts b/indexer/services/ender/__tests__/lib/candles-generator.test.ts index d1c257b80b..87a6c65434 100644 --- a/indexer/services/ender/__tests__/lib/candles-generator.test.ts +++ b/indexer/services/ender/__tests__/lib/candles-generator.test.ts @@ -133,8 +133,8 @@ describe('candleHelper', () => { id: CandleTable.uuid(currentStartedAt, defaultCandle.ticker, resolution), startedAt: currentStartedAt, resolution, - orderbookMidPriceClose: '105000', - orderbookMidPriceOpen: '105000', + orderbookMidPriceClose: null, + orderbookMidPriceOpen: null, }; }, ); @@ -179,8 +179,8 @@ describe('candleHelper', () => { startedAt: currentStartedAt, resolution, startingOpenInterest: openInterest, - orderbookMidPriceClose: '80500', - orderbookMidPriceOpen: '80500', + orderbookMidPriceClose: null, + orderbookMidPriceOpen: null, }; }, ); @@ -303,8 +303,8 @@ describe('candleHelper', () => { usdVolume: '0', trades: 0, startingOpenInterest: '100', - orderbookMidPriceClose: '1000', - orderbookMidPriceOpen: '1000', + orderbookMidPriceClose: null, + orderbookMidPriceOpen: null, }, true, 1000, @@ -334,8 +334,8 @@ describe('candleHelper', () => { startedAt, resolution: CandleResolution.ONE_MINUTE, startingOpenInterest: '100', - orderbookMidPriceClose: '1000', - orderbookMidPriceOpen: '1000', + orderbookMidPriceClose: null, + orderbookMidPriceOpen: null, }, true, // contains kafka messages 1000, // orderbook mid price @@ -465,200 +465,6 @@ describe('candleHelper', () => { expectTimingStats(); }); - it('Updates previous candle orderBookMidPriceClose if startTime is past candle resolution', async () => { - // Create existing candles - const existingPrice: string = '7000'; - const startingOpenInterest: string = '200'; - const baseTokenVolume: string = '10'; - const usdVolume: string = Big(existingPrice).times(baseTokenVolume).toString(); - const orderbookMidPriceClose = '7500'; - const orderbookMidPriceOpen = '8000'; - await Promise.all( - _.map(Object.values(CandleResolution), (resolution: CandleResolution) => { - return CandleTable.create({ - startedAt: previousStartedAt, - ticker: testConstants.defaultPerpetualMarket.ticker, - resolution, - low: existingPrice, - high: existingPrice, - open: existingPrice, - close: existingPrice, - baseTokenVolume, - usdVolume, - trades: existingTrades, - startingOpenInterest, - orderbookMidPriceClose, - orderbookMidPriceOpen, - }); - }), - ); - await startCandleCache(); - - // Update Orderbook levels - await updatePriceLevel('BTC-USD', '10010', OrderSide.SELL); - await updatePriceLevel('BTC-USD', '10000', OrderSide.BUY); - - const publisher: KafkaPublisher = new KafkaPublisher(); - publisher.addEvents([ - defaultTradeKafkaEvent, - defaultTradeKafkaEvent2, - ]); - - // Create new candles, with trades - await runUpdateCandles(publisher).then(async () => { - - // Verify previous candles have orderbookMidPriceClose updated - const previousExpectedCandles: CandleFromDatabase[] = _.map( - Object.values(CandleResolution), - (resolution: CandleResolution) => { - return { - id: CandleTable.uuid(previousStartedAt, defaultCandle.ticker, resolution), - startedAt: previousStartedAt, - ticker: defaultCandle.ticker, - resolution, - low: existingPrice, - high: existingPrice, - open: existingPrice, - close: existingPrice, - baseTokenVolume, - usdVolume, - trades: existingTrades, - startingOpenInterest, - orderbookMidPriceClose: '10005', - orderbookMidPriceOpen, - }; - }, - ); - await verifyCandlesInPostgres(previousExpectedCandles); - }); - - // Verify new candles were created - const expectedCandles: CandleFromDatabase[] = _.map( - Object.values(CandleResolution), - (resolution: CandleResolution) => { - const currentStartedAt: IsoString = helpers.calculateNormalizedCandleStartTime( - testConstants.createdDateTime, - resolution, - ).toISO(); - - return { - id: CandleTable.uuid(currentStartedAt, defaultCandle.ticker, resolution), - startedAt: currentStartedAt, - ticker: defaultCandle.ticker, - resolution, - low: '10000', - high: defaultPrice2, - open: '10000', - close: defaultPrice2, - baseTokenVolume: '20', - usdVolume: '250000', - trades: 2, - startingOpenInterest: '0', - orderbookMidPriceClose: '10005', - orderbookMidPriceOpen: '10005', - }; - }, - ); - await verifyCandlesInPostgres(expectedCandles); - await validateCandlesCache(); - expectTimingStats(); - }); - - it('creates an empty candle and updates the previous candle orderBookMidPriceClose if startTime is past candle resolution', async () => { - // Create existing candles - const existingPrice: string = '7000'; - const startingOpenInterest: string = '200'; - const baseTokenVolume: string = '10'; - const usdVolume: string = Big(existingPrice).times(baseTokenVolume).toString(); - const orderbookMidPriceClose = '7500'; - const orderbookMidPriceOpen = '8000'; - - await Promise.all( - _.map(Object.values(CandleResolution), (resolution: CandleResolution) => { - return CandleTable.create({ - startedAt: previousStartedAt, - ticker: testConstants.defaultPerpetualMarket.ticker, - resolution, - low: existingPrice, - high: existingPrice, - open: existingPrice, - close: existingPrice, - baseTokenVolume, - usdVolume, - trades: existingTrades, - startingOpenInterest, - orderbookMidPriceClose, - orderbookMidPriceOpen, - }); - }), - ); - await startCandleCache(); - - // Update Orderbook levels - await updatePriceLevel('BTC-USD', '10010', OrderSide.SELL); - await updatePriceLevel('BTC-USD', '10000', OrderSide.BUY); - - const publisher: KafkaPublisher = new KafkaPublisher(); - publisher.addEvents([]); - - // Create new candles, without trades - await runUpdateCandles(publisher); - - // Verify previous candles have orderbookMidPriceClose updated - const previousExpectedCandles: CandleFromDatabase[] = _.map( - Object.values(CandleResolution), - (resolution: CandleResolution) => { - return { - id: CandleTable.uuid(previousStartedAt, defaultCandle.ticker, resolution), - startedAt: previousStartedAt, - ticker: defaultCandle.ticker, - resolution, - low: existingPrice, - high: existingPrice, - open: existingPrice, - close: existingPrice, - baseTokenVolume, - usdVolume, - trades: existingTrades, - startingOpenInterest, - orderbookMidPriceClose: '10005', - orderbookMidPriceOpen, - }; - }, - ); - await verifyCandlesInPostgres(previousExpectedCandles); - - // Verify new empty candle was created - const expectedCandles: CandleFromDatabase[] = _.map( - Object.values(CandleResolution), - (resolution: CandleResolution) => { - const currentStartedAt: IsoString = helpers.calculateNormalizedCandleStartTime( - testConstants.createdDateTime, - resolution, - ).toISO(); - - return { - id: CandleTable.uuid(currentStartedAt, defaultCandle.ticker, resolution), - startedAt: currentStartedAt, - ticker: defaultCandle.ticker, - resolution, - low: existingPrice, - high: existingPrice, - open: existingPrice, - close: existingPrice, - baseTokenVolume: '0', - usdVolume: '0', - trades: 0, - startingOpenInterest: '0', - orderbookMidPriceClose: '10005', - orderbookMidPriceOpen: '10005', - }; - }, - ); - await verifyCandlesInPostgres(expectedCandles); - - }); - it('successfully creates an orderbook price map for each market', async () => { await updatePriceLevel('BTC-USD', '100000', OrderSide.BUY); await updatePriceLevel('BTC-USD', '110000', OrderSide.SELL); @@ -671,9 +477,9 @@ describe('candleHelper', () => { const map = await getOrderbookMidPriceMap(); expect(map).toEqual({ - 'BTC-USD': '105000', - 'ETH-USD': '150000', - 'ISO-USD': '115000', + 'BTC-USD': undefined, + 'ETH-USD': undefined, + 'ISO-USD': undefined, 'ISO2-USD': undefined, 'SHIB-USD': undefined, }); diff --git a/indexer/services/ender/src/lib/candles-generator.ts b/indexer/services/ender/src/lib/candles-generator.ts index d7dd7bba34..f1daa75f06 100644 --- a/indexer/services/ender/src/lib/candles-generator.ts +++ b/indexer/services/ender/src/lib/candles-generator.ts @@ -20,7 +20,6 @@ import { TradeMessageContents, helpers, } from '@dydxprotocol-indexer/postgres'; -import { OrderbookLevelsCache } from '@dydxprotocol-indexer/redis'; import { CandleMessage } from '@dydxprotocol-indexer/v4-protos'; import Big from 'big.js'; import _ from 'lodash'; @@ -28,7 +27,6 @@ import { DateTime } from 'luxon'; import { getCandle } from '../caches/candle-cache'; import config from '../config'; -import { redisClient } from '../helpers/redis/redis-controller'; import { KafkaPublisher } from './kafka-publisher'; import { ConsolidatedKafkaEvent, SingleTradeMessage } from './types'; @@ -538,11 +536,7 @@ export async function getOrderbookMidPriceMap(): Promise<{ [ticker: string]: Ord const perpetualMarkets = Object.values(perpetualMarketRefresher.getPerpetualMarketsMap()); const promises = perpetualMarkets.map(async (perpetualMarket: PerpetualMarketFromDatabase) => { - const price = await OrderbookLevelsCache.getOrderBookMidPrice( - perpetualMarket.ticker, - redisClient, - ); - return { [perpetualMarket.ticker]: price === undefined ? undefined : price }; + return Promise.resolve({ [perpetualMarket.ticker]: undefined }); }); const pricesArray = await Promise.all(promises);