Skip to content

Commit

Permalink
Cast oracle price to bigint and then numeric to get proper values (#2689
Browse files Browse the repository at this point in the history
)

Signed-off-by: Shrenuj Bansal <[email protected]>
  • Loading branch information
shrenujb authored Jan 16, 2025
1 parent c0d7369 commit 56cc48e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion indexer/packages/postgres/__tests__/helpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { DydxIndexerSubtypes, MarketPriceUpdateEventMessage } from '../../../src
import {
defaultHeight,
defaultMarketPriceUpdate,
defaultMarketPriceUpdate3,
defaultPreviousHeight,
defaultTime,
defaultTxHash,
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions indexer/services/ender/__tests__/helpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down

0 comments on commit 56cc48e

Please sign in to comment.