From 7a2200ec883892cec3862e536c11298b479b85f9 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 15 Oct 2024 11:24:33 -0400 Subject: [PATCH] Return undefined from getOrderbookMidPriceMap (backport #2441) (#2486) Co-authored-by: Adam Fraser --- .../__tests__/lib/candles-generator.test.ts | 216 ++---------------- .../ender/src/lib/candles-generator.ts | 8 +- 2 files changed, 14 insertions(+), 210 deletions(-) diff --git a/indexer/services/ender/__tests__/lib/candles-generator.test.ts b/indexer/services/ender/__tests__/lib/candles-generator.test.ts index d58ebd0d2a..cd014d3eaf 100644 --- a/indexer/services/ender/__tests__/lib/candles-generator.test.ts +++ b/indexer/services/ender/__tests__/lib/candles-generator.test.ts @@ -137,8 +137,8 @@ describe('candleHelper', () => { id: CandleTable.uuid(currentStartedAt, defaultCandle.ticker, resolution), startedAt: currentStartedAt, resolution, - orderbookMidPriceClose: '105000', - orderbookMidPriceOpen: '105000', + orderbookMidPriceClose: null, + orderbookMidPriceOpen: null, }; }, ); @@ -187,8 +187,8 @@ describe('candleHelper', () => { startedAt: currentStartedAt, resolution, startingOpenInterest: openInterest, - orderbookMidPriceClose: '80500', - orderbookMidPriceOpen: '80500', + orderbookMidPriceClose: null, + orderbookMidPriceOpen: null, }; }, ); @@ -311,8 +311,8 @@ describe('candleHelper', () => { usdVolume: '0', trades: 0, startingOpenInterest: '100', - orderbookMidPriceClose: '1000', - orderbookMidPriceOpen: '1000', + orderbookMidPriceClose: null, + orderbookMidPriceOpen: null, }, true, 1000, @@ -342,8 +342,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 @@ -471,196 +471,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(); - - await OrderbookMidPricesCache.setPrice(redisClient, 'BTC-USD', '10005'); - - 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(); - - await OrderbookMidPricesCache.setPrice(redisClient, 'BTC-USD', '10005'); - - 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 Promise.all([ OrderbookMidPricesCache.setPrice(redisClient, 'BTC-USD', '105000'), @@ -670,11 +480,11 @@ describe('candleHelper', () => { const map = await getOrderbookMidPriceMap(); expect(map).toEqual({ - 'BTC-USD': '105000', - 'ETH-USD': '150000', - 'ISO-USD': '115000', - 'ISO2-USD': null, - 'SHIB-USD': null, + '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 b232a66eb0..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 { OrderbookMidPricesCache } 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 OrderbookMidPricesCache.getMedianPrice( - redisClient, - perpetualMarket.ticker, - ); - return { [perpetualMarket.ticker]: price === undefined ? undefined : price }; + return Promise.resolve({ [perpetualMarket.ticker]: undefined }); }); const pricesArray = await Promise.all(promises);