From c6e4d92b5a29d3766459973b9b881d4dd59f2c27 Mon Sep 17 00:00:00 2001 From: Adam Fraser Date: Fri, 2 Aug 2024 12:52:14 -0400 Subject: [PATCH] Adam/ct 1059 address issue with orderbookmidprice calculation (#2012) (cherry picked from commit 9385c5a6775ebdfd9e39256d3dc3ab991acd117c) --- .../caches/orderbook-levels-cache.test.ts | 45 ++++++++++++++++++- .../src/caches/orderbook-levels-cache.ts | 19 ++------ .../ender/src/lib/candles-generator.ts | 2 +- 3 files changed, 49 insertions(+), 17 deletions(-) diff --git a/indexer/packages/redis/__tests__/caches/orderbook-levels-cache.test.ts b/indexer/packages/redis/__tests__/caches/orderbook-levels-cache.test.ts index 40c180a3b0..9e118f22ee 100644 --- a/indexer/packages/redis/__tests__/caches/orderbook-levels-cache.test.ts +++ b/indexer/packages/redis/__tests__/caches/orderbook-levels-cache.test.ts @@ -743,9 +743,52 @@ describe('orderbookLevelsCache', () => { ]); const midPrice = await getOrderBookMidPrice(ticker, client); - expect(midPrice).toEqual(45350); + expect(midPrice).toEqual('45350'); }); + }); + it('returns the correct mid price for very small numbers', async () => { + await Promise.all([ + updatePriceLevel({ + ticker, + side: OrderSide.SELL, + humanPrice: '0.000000002346', + sizeDeltaInQuantums: '2000', + client, + }), + updatePriceLevel({ + ticker, + side: OrderSide.BUY, + humanPrice: '0.000000002344', + sizeDeltaInQuantums: '2000', + client, + }), + ]); + + const midPrice = await getOrderBookMidPrice(ticker, client); + expect(midPrice).toEqual('0.000000002345'); + }); + + it('returns the approprite amount of decimal precision', async () => { + await Promise.all([ + updatePriceLevel({ + ticker, + side: OrderSide.SELL, + humanPrice: '1.02', + sizeDeltaInQuantums: '2000', + client, + }), + updatePriceLevel({ + ticker, + side: OrderSide.BUY, + humanPrice: '1.01', + sizeDeltaInQuantums: '2000', + client, + }), + ]); + + const midPrice = await getOrderBookMidPrice(ticker, client); + expect(midPrice).toEqual('1.015'); }); it('returns undefined if there are no bids or asks', async () => { diff --git a/indexer/packages/redis/src/caches/orderbook-levels-cache.ts b/indexer/packages/redis/src/caches/orderbook-levels-cache.ts index 6f30854cb7..6d766719a4 100644 --- a/indexer/packages/redis/src/caches/orderbook-levels-cache.ts +++ b/indexer/packages/redis/src/caches/orderbook-levels-cache.ts @@ -533,7 +533,7 @@ function convertToPriceLevels( export async function getOrderBookMidPrice( ticker: string, client: RedisClient, -): Promise { +): Promise { const levels = await getOrderBookLevels(ticker, client, { removeZeros: true, sortSides: true, @@ -542,25 +542,14 @@ export async function getOrderBookMidPrice( }); if (levels.bids.length === 0 || levels.asks.length === 0) { - const message: string = `Orderbook bid length: ${levels.bids.length}, ask length: ${levels.asks.length}. Expected > 0`; - logger.error({ - at: 'orderbook-levels-cache#getOrderBookMidPrice', - message, - }); return undefined; } - const bestAsk = Number(levels.asks[0].humanPrice); - const bestBid = Number(levels.bids[0].humanPrice); + const bestAsk = Big(levels.asks[0].humanPrice); + const bestBid = Big(levels.bids[0].humanPrice); if (bestAsk === undefined || bestBid === undefined) { - const message: string = `Orderbook bid or ask failed to parse to Number, bid: ${levels.bids[0]}, ask: ${levels.asks[0]}`; - logger.error({ - at: 'orderbook-levels-cache#getOrderBookMidPrice', - message, - }); return undefined; } - - return bestBid + (bestAsk - bestBid) / 2; + return bestBid.plus(bestAsk).div(2).toFixed(); } diff --git a/indexer/services/ender/src/lib/candles-generator.ts b/indexer/services/ender/src/lib/candles-generator.ts index 034095e378..00fd1ab598 100644 --- a/indexer/services/ender/src/lib/candles-generator.ts +++ b/indexer/services/ender/src/lib/candles-generator.ts @@ -541,7 +541,7 @@ export async function getOrderbookMidPriceMap(): Promise<{ [ticker: string]: Ord perpetualMarket.ticker, redisClient, ); - return { [perpetualMarket.ticker]: price === undefined ? undefined : String(price) }; + return { [perpetualMarket.ticker]: price === undefined ? undefined : price }; }); const pricesArray = await Promise.all(promises);