Skip to content

Commit

Permalink
Use OrderbookMidPrice cache in candles generator
Browse files Browse the repository at this point in the history
  • Loading branch information
adamfraser committed Sep 24, 2024
1 parent da16dc7 commit 843aea3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 30 deletions.
53 changes: 26 additions & 27 deletions indexer/services/ender/__tests__/lib/candles-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
testMocks,
Transaction,
helpers,
OrderSide,
} from '@dydxprotocol-indexer/postgres';
import { CandleMessage, CandleMessage_Resolution } from '@dydxprotocol-indexer/v4-protos';
import Big from 'big.js';
Expand All @@ -32,9 +31,11 @@ import { KafkaPublisher } from '../../src/lib/kafka-publisher';
import { ConsolidatedKafkaEvent } from '../../src/lib/types';
import { defaultTradeContent, defaultTradeKafkaEvent } from '../helpers/constants';
import { contentToSingleTradeMessage, createConsolidatedKafkaEventFromTrade } from '../helpers/kafka-publisher-helpers';
import { updatePriceLevel } from '../helpers/redis-helpers';
import { redisClient } from '../../src/helpers/redis/redis-controller';
import { redis } from '@dydxprotocol-indexer/redis';
import {
redis,
OrderbookMidPricesCache,
} from '@dydxprotocol-indexer/redis';

describe('candleHelper', () => {
beforeAll(async () => {
Expand Down Expand Up @@ -113,9 +114,12 @@ describe('candleHelper', () => {
defaultTradeKafkaEvent2,
]);

// Create Orderbook levels to set orderbookMidPrice open & close
await updatePriceLevel('BTC-USD', '100000', OrderSide.BUY);
await updatePriceLevel('BTC-USD', '110000', OrderSide.SELL);
const ticker = 'BTC-USD';
await Promise.all([
OrderbookMidPricesCache.setPrice(redisClient, ticker, '100000'),
OrderbookMidPricesCache.setPrice(redisClient, ticker, '105000'),
OrderbookMidPricesCache.setPrice(redisClient, ticker, '110000'),
]);

await runUpdateCandles(publisher);

Expand Down Expand Up @@ -155,8 +159,12 @@ describe('candleHelper', () => {
defaultTradeKafkaEvent2,
]);

await updatePriceLevel('BTC-USD', '80000', OrderSide.BUY);
await updatePriceLevel('BTC-USD', '81000', OrderSide.SELL);
const ticker = 'BTC-USD';
await Promise.all([
OrderbookMidPricesCache.setPrice(redisClient, ticker, '80000'),
OrderbookMidPricesCache.setPrice(redisClient, ticker, '81000'),
OrderbookMidPricesCache.setPrice(redisClient, ticker, '80500'),
]);

// Create Perpetual Position to set open position
const openInterest: string = '100';
Expand Down Expand Up @@ -427,9 +435,7 @@ describe('candleHelper', () => {
containsKafkaMessages: boolean = true,
orderbookMidPrice: number,
) => {
const midPriceSpread = 10;
await updatePriceLevel('BTC-USD', String(orderbookMidPrice + midPriceSpread), OrderSide.SELL);
await updatePriceLevel('BTC-USD', String(orderbookMidPrice - midPriceSpread), OrderSide.BUY);
await OrderbookMidPricesCache.setPrice(redisClient, 'BTC-USD', orderbookMidPrice.toFixed());

if (initialCandle !== undefined) {
await CandleTable.create(initialCandle);
Expand Down Expand Up @@ -494,9 +500,7 @@ describe('candleHelper', () => {
);
await startCandleCache();

// Update Orderbook levels
await updatePriceLevel('BTC-USD', '10010', OrderSide.SELL);
await updatePriceLevel('BTC-USD', '10000', OrderSide.BUY);
await OrderbookMidPricesCache.setPrice(redisClient, 'BTC-USD', '10005');

const publisher: KafkaPublisher = new KafkaPublisher();
publisher.addEvents([
Expand Down Expand Up @@ -594,9 +598,7 @@ describe('candleHelper', () => {
);
await startCandleCache();

// Update Orderbook levels
await updatePriceLevel('BTC-USD', '10010', OrderSide.SELL);
await updatePriceLevel('BTC-USD', '10000', OrderSide.BUY);
await OrderbookMidPricesCache.setPrice(redisClient, 'BTC-USD', '10005');

const publisher: KafkaPublisher = new KafkaPublisher();
publisher.addEvents([]);
Expand Down Expand Up @@ -660,22 +662,19 @@ describe('candleHelper', () => {
});

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);

await updatePriceLevel('ISO-USD', '110000', OrderSide.BUY);
await updatePriceLevel('ISO-USD', '120000', OrderSide.SELL);

await updatePriceLevel('ETH-USD', '100000', OrderSide.BUY);
await updatePriceLevel('ETH-USD', '200000', OrderSide.SELL);
await Promise.all([
OrderbookMidPricesCache.setPrice(redisClient, 'BTC-USD', '105000'),
OrderbookMidPricesCache.setPrice(redisClient, 'ISO-USD', '115000'),
OrderbookMidPricesCache.setPrice(redisClient, 'ETH-USD', '150000'),
]);

const map = await getOrderbookMidPriceMap();
expect(map).toEqual({
'BTC-USD': '105000',
'ETH-USD': '150000',
'ISO-USD': '115000',
'ISO2-USD': undefined,
'SHIB-USD': undefined,
'ISO2-USD': null,
'SHIB-USD': null,
});
});
});
Expand Down
6 changes: 3 additions & 3 deletions indexer/services/ender/src/lib/candles-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
TradeMessageContents,
helpers,
} from '@dydxprotocol-indexer/postgres';
import { OrderbookLevelsCache } from '@dydxprotocol-indexer/redis';
import { OrderbookMidPricesCache } from '@dydxprotocol-indexer/redis';
import { CandleMessage } from '@dydxprotocol-indexer/v4-protos';
import Big from 'big.js';
import _ from 'lodash';
Expand Down Expand Up @@ -538,9 +538,9 @@ 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,
const price = await OrderbookMidPricesCache.getMedianPrice(
redisClient,
perpetualMarket.ticker,
);
return { [perpetualMarket.ticker]: price === undefined ? undefined : price };
});
Expand Down

0 comments on commit 843aea3

Please sign in to comment.