Skip to content

Commit

Permalink
Add perpetual market refresher to roundtable
Browse files Browse the repository at this point in the history
  • Loading branch information
adamfraser committed Nov 19, 2024
1 parent d16654c commit 3a3e3c5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
PerpetualMarketTable,
testConstants,
testMocks,
perpetualMarketRefresher,
} from '@dydxprotocol-indexer/postgres';
import {
OrderbookLevelsCache,
Expand All @@ -17,6 +18,7 @@ describe('cache-orderbook-mid-prices', () => {
beforeEach(async () => {
await redis.deleteAllAsync(redisClient);
await testMocks.seedData();
await perpetualMarketRefresher.updatePerpetualMarkets();
});

afterAll(() => {
Expand All @@ -30,6 +32,7 @@ describe('cache-orderbook-mid-prices', () => {

afterEach(async () => {
await dbHelpers.clearData();
await perpetualMarketRefresher.clear();
});

it('caches mid prices for all markets', async () => {
Expand All @@ -41,36 +44,30 @@ describe('cache-orderbook-mid-prices', () => {
.findByMarketId(
testConstants.defaultMarket2.id,
);
if (!market1) {
throw new Error('Market 1 not found');
}
if (!market2) {
throw new Error('Market 2 not found');
const market3 = await PerpetualMarketTable
.findByMarketId(
testConstants.defaultMarket3.id,
);
if (!market1 || !market2 || !market3) {
throw new Error('Test market not found');
}

jest.spyOn(PerpetualMarketTable, 'findAll')
.mockReturnValueOnce(Promise.resolve([
market1,
// Passing market2 twice so that it will call getOrderbookMidPrice twice and
// cache the last two prices from the mock below
market2,
market2,
] as PerpetualMarketFromDatabase[]));

jest.spyOn(OrderbookLevelsCache, 'getOrderBookMidPrice')
.mockReturnValueOnce(Promise.resolve('200'))
.mockReturnValueOnce(Promise.resolve('300'))
.mockReturnValueOnce(Promise.resolve('400'));

await runTask();
expect(OrderbookLevelsCache.getOrderBookMidPrice).toHaveBeenCalledTimes(5);

const prices = await OrderbookMidPricesCache.getMedianPrices(
redisClient,
[market1.ticker, market2.ticker],
[market1.ticker, market2.ticker, market3.ticker],
);

expect(prices[market1.ticker]).toBe('200');
expect(prices[market2.ticker]).toBe('350');
expect(prices[market2.ticker]).toBe('300');
expect(prices[market3.ticker]).toBe('400');
});

it('handles undefined prices', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import {
logger,
} from '@dydxprotocol-indexer/base';
import {
PerpetualMarketTable,
PerpetualMarketFromDatabase,
perpetualMarketRefresher,
} from '@dydxprotocol-indexer/postgres';
import {
OrderbookMidPricesCache,
Expand All @@ -14,14 +15,10 @@ import { redisClient } from '../helpers/redis';
* Updates OrderbookMidPricesCache with current orderbook mid price for each market
*/
export default async function runTask(): Promise<void> {
const marketTickers: string[] = (await PerpetualMarketTable.findAll(
{},
[],
{ readReplica: true },
)).map((market) => {
return market.ticker;
});

const perpetualMarkets = Object.values(perpetualMarketRefresher.getPerpetualMarketsMap());
const marketTickers = perpetualMarkets.map(
(market: PerpetualMarketFromDatabase) => market.ticker,
);
try {
logger.info({
at: 'cache-orderbook-mid-prices#runTask',
Expand Down

0 comments on commit 3a3e3c5

Please sign in to comment.