Skip to content

Commit

Permalink
Return undefined from getOrderbookMidPriceMap (backport #2441) (#2486)
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Fraser <[email protected]>
  • Loading branch information
mergify[bot] and adamfraser authored Oct 15, 2024
1 parent 071aa31 commit 7a2200e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 210 deletions.
216 changes: 13 additions & 203 deletions indexer/services/ender/__tests__/lib/candles-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ describe('candleHelper', () => {
id: CandleTable.uuid(currentStartedAt, defaultCandle.ticker, resolution),
startedAt: currentStartedAt,
resolution,
orderbookMidPriceClose: '105000',
orderbookMidPriceOpen: '105000',
orderbookMidPriceClose: null,
orderbookMidPriceOpen: null,
};
},
);
Expand Down Expand Up @@ -187,8 +187,8 @@ describe('candleHelper', () => {
startedAt: currentStartedAt,
resolution,
startingOpenInterest: openInterest,
orderbookMidPriceClose: '80500',
orderbookMidPriceOpen: '80500',
orderbookMidPriceClose: null,
orderbookMidPriceOpen: null,
};
},
);
Expand Down Expand Up @@ -311,8 +311,8 @@ describe('candleHelper', () => {
usdVolume: '0',
trades: 0,
startingOpenInterest: '100',
orderbookMidPriceClose: '1000',
orderbookMidPriceOpen: '1000',
orderbookMidPriceClose: null,
orderbookMidPriceOpen: null,
},
true,
1000,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'),
Expand All @@ -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,
});
});
});
Expand Down
8 changes: 1 addition & 7 deletions indexer/services/ender/src/lib/candles-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ 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';
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';

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 7a2200e

Please sign in to comment.