Skip to content

Commit

Permalink
Return undefined from getOrderbookMidPriceMap (backport #2441) (#2442)
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 2, 2024
1 parent f5f1617 commit aad2a2a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 212 deletions.
216 changes: 11 additions & 205 deletions indexer/services/ender/__tests__/lib/candles-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,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 @@ -179,8 +179,8 @@ describe('candleHelper', () => {
startedAt: currentStartedAt,
resolution,
startingOpenInterest: openInterest,
orderbookMidPriceClose: '80500',
orderbookMidPriceOpen: '80500',
orderbookMidPriceClose: null,
orderbookMidPriceOpen: null,
};
},
);
Expand Down Expand Up @@ -303,8 +303,8 @@ describe('candleHelper', () => {
usdVolume: '0',
trades: 0,
startingOpenInterest: '100',
orderbookMidPriceClose: '1000',
orderbookMidPriceOpen: '1000',
orderbookMidPriceClose: null,
orderbookMidPriceOpen: null,
},
true,
1000,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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,
});
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 { OrderbookLevelsCache } 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 OrderbookLevelsCache.getOrderBookMidPrice(
perpetualMarket.ticker,
redisClient,
);
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 aad2a2a

Please sign in to comment.