From 56cc48eaa2dae71b53eace7da93ae94b03e78aa1 Mon Sep 17 00:00:00 2001 From: shrenujb <98204323+shrenujb@users.noreply.github.com> Date: Wed, 15 Jan 2025 23:27:55 -0500 Subject: [PATCH] Cast oracle price to bigint and then numeric to get proper values (#2689) Signed-off-by: Shrenuj Bansal --- .../postgres/__tests__/helpers/constants.ts | 2 +- .../market-price-update-handler.test.ts | 34 +++++++++++++++++++ .../ender/__tests__/helpers/constants.ts | 7 ++++ .../dydx_market_price_update_handler.sql | 6 ++-- 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/indexer/packages/postgres/__tests__/helpers/constants.ts b/indexer/packages/postgres/__tests__/helpers/constants.ts index aeaedeae3c9..496a3a12e76 100644 --- a/indexer/packages/postgres/__tests__/helpers/constants.ts +++ b/indexer/packages/postgres/__tests__/helpers/constants.ts @@ -746,7 +746,7 @@ export const isolatedMarket: MarketCreateObject = { export const isolatedMarket2: MarketCreateObject = { id: 4, pair: 'ISO2-USD', - exponent: -12, + exponent: -20, minPriceChangePpm: 50, oraclePrice: '0.000000085', }; diff --git a/indexer/services/ender/__tests__/handlers/markets/market-price-update-handler.test.ts b/indexer/services/ender/__tests__/handlers/markets/market-price-update-handler.test.ts index 6c858450327..28b23c1be5a 100644 --- a/indexer/services/ender/__tests__/handlers/markets/market-price-update-handler.test.ts +++ b/indexer/services/ender/__tests__/handlers/markets/market-price-update-handler.test.ts @@ -15,6 +15,7 @@ import { DydxIndexerSubtypes, MarketPriceUpdateEventMessage } from '../../../src import { defaultHeight, defaultMarketPriceUpdate, + defaultMarketPriceUpdate3, defaultPreviousHeight, defaultTime, defaultTxHash, @@ -154,6 +155,39 @@ describe('marketPriceUpdateHandler', () => { }); }); + it('successfully inserts new oracle price for market with very low exponent', async () => { + const transactionIndex: number = 0; + + const kafkaMessage: KafkaMessage = createKafkaMessageFromMarketEvent({ + marketEvents: [defaultMarketPriceUpdate3], + transactionIndex, + height: defaultHeight, + time: defaultTime, + txHash: defaultTxHash, + }); + + await onMessage(kafkaMessage); + + const { market, oraclePrice } = await getDbState(defaultMarketPriceUpdate3); + + expectOraclePriceMatchesEvent( + defaultMarketPriceUpdate3 as MarketPriceUpdateEventMessage, + oraclePrice, + market, + defaultHeight, + ); + + const contents: MarketMessageContents = generateOraclePriceContents( + oraclePrice, + market.pair, + ); + + expectMarketKafkaMessage({ + producerSendMock, + contents: JSON.stringify(contents), + }); + }); + it('successfully inserts new oracle price for market created in same block', async () => { const transactionIndex: number = 0; const newMarketId: number = 3000; diff --git a/indexer/services/ender/__tests__/helpers/constants.ts b/indexer/services/ender/__tests__/helpers/constants.ts index c871f589bad..a90a03c0abc 100644 --- a/indexer/services/ender/__tests__/helpers/constants.ts +++ b/indexer/services/ender/__tests__/helpers/constants.ts @@ -67,6 +67,13 @@ export const defaultMarketPriceUpdate2: MarketEventV1 = { }, }; +export const defaultMarketPriceUpdate3: MarketEventV1 = { + marketId: 4, + priceUpdate: { + priceWithExponent: Long.fromValue(100000000, true), + }, +}; + export const defaultFundingUpdateSampleEvent: FundingEventMessage = { type: FundingEventV1_Type.TYPE_PREMIUM_SAMPLE, updates: [ diff --git a/indexer/services/ender/src/scripts/handlers/dydx_market_price_update_handler.sql b/indexer/services/ender/src/scripts/handlers/dydx_market_price_update_handler.sql index 8203860c24a..a40562b717b 100644 --- a/indexer/services/ender/src/scripts/handlers/dydx_market_price_update_handler.sql +++ b/indexer/services/ender/src/scripts/handlers/dydx_market_price_update_handler.sql @@ -11,6 +11,8 @@ CREATE OR REPLACE FUNCTION dydx_market_price_update_handler(block_height int, bl (Note that no text should exist before the function declaration to ensure that exception line numbers are correct.) */ + + DECLARE market_record_id integer; market_record markets%ROWTYPE; @@ -25,8 +27,8 @@ BEGIN END IF; oracle_price = dydx_trim_scale( - dydx_from_jsonlib_long(event_data->'priceUpdate'->'priceWithExponent') * - power(10, market_record.exponent::numeric)); + (dydx_from_jsonlib_long(event_data->'priceUpdate'->'priceWithExponent') * + power(10, market_record.exponent::bigint))::numeric); market_record."oraclePrice" = oracle_price;