From 47b2a2fdcb7f373d77ee954c000675528c7cd42c Mon Sep 17 00:00:00 2001 From: "m.ray" <16125548+MadalinaRaicu@users.noreply.github.com> Date: Thu, 26 Jan 2023 09:21:43 -0500 Subject: [PATCH] fix: volume chart values fixing - position dp (#2749) --- .../rewards/home/__generated__/Rewards.ts | 2 +- libs/candles-chart/src/lib/Candles.graphql | 1 + .../src/lib/__generated__/Candles.ts | 3 ++- libs/candles-chart/src/lib/candles.mock.ts | 1 + libs/candles-chart/src/lib/data-source.ts | 25 ++++++++++++++++--- 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/apps/token/src/routes/rewards/home/__generated__/Rewards.ts b/apps/token/src/routes/rewards/home/__generated__/Rewards.ts index 2f6cd9adf..2f5738696 100644 --- a/apps/token/src/routes/rewards/home/__generated__/Rewards.ts +++ b/apps/token/src/routes/rewards/home/__generated__/Rewards.ts @@ -97,4 +97,4 @@ export function useRewardsLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions; export type RewardsLazyQueryHookResult = ReturnType; -export type RewardsQueryResult = Apollo.QueryResult; +export type RewardsQueryResult = Apollo.QueryResult; \ No newline at end of file diff --git a/libs/candles-chart/src/lib/Candles.graphql b/libs/candles-chart/src/lib/Candles.graphql index 977887add..4279d6a98 100644 --- a/libs/candles-chart/src/lib/Candles.graphql +++ b/libs/candles-chart/src/lib/Candles.graphql @@ -12,6 +12,7 @@ query Candles($marketId: ID!, $interval: Interval!, $since: String!) { market(id: $marketId) { id decimalPlaces + positionDecimalPlaces tradableInstrument { instrument { id diff --git a/libs/candles-chart/src/lib/__generated__/Candles.ts b/libs/candles-chart/src/lib/__generated__/Candles.ts index 5d571db8c..720a57048 100644 --- a/libs/candles-chart/src/lib/__generated__/Candles.ts +++ b/libs/candles-chart/src/lib/__generated__/Candles.ts @@ -12,7 +12,7 @@ export type CandlesQueryVariables = Types.Exact<{ }>; -export type CandlesQuery = { __typename?: 'Query', market?: { __typename?: 'Market', id: string, decimalPlaces: number, tradableInstrument: { __typename?: 'TradableInstrument', instrument: { __typename?: 'Instrument', id: string, name: string, code: string } }, candlesConnection?: { __typename?: 'CandleDataConnection', edges?: Array<{ __typename?: 'CandleEdge', node: { __typename?: 'Candle', periodStart: any, lastUpdateInPeriod: any, high: string, low: string, open: string, close: string, volume: string } } | null> | null } | null } | null }; +export type CandlesQuery = { __typename?: 'Query', market?: { __typename?: 'Market', id: string, decimalPlaces: number, positionDecimalPlaces: number, tradableInstrument: { __typename?: 'TradableInstrument', instrument: { __typename?: 'Instrument', id: string, name: string, code: string } }, candlesConnection?: { __typename?: 'CandleDataConnection', edges?: Array<{ __typename?: 'CandleEdge', node: { __typename?: 'Candle', periodStart: any, lastUpdateInPeriod: any, high: string, low: string, open: string, close: string, volume: string } } | null> | null } | null } | null }; export type CandlesEventsSubscriptionVariables = Types.Exact<{ marketId: Types.Scalars['ID']; @@ -38,6 +38,7 @@ export const CandlesDocument = gql` market(id: $marketId) { id decimalPlaces + positionDecimalPlaces tradableInstrument { instrument { id diff --git a/libs/candles-chart/src/lib/candles.mock.ts b/libs/candles-chart/src/lib/candles.mock.ts index ac7bc0bfd..6726223b8 100644 --- a/libs/candles-chart/src/lib/candles.mock.ts +++ b/libs/candles-chart/src/lib/candles.mock.ts @@ -14,6 +14,7 @@ export const candlesQuery = ( market: { id: 'market-0', decimalPlaces: 5, + positionDecimalPlaces: 0, tradableInstrument: { instrument: { id: '', diff --git a/libs/candles-chart/src/lib/data-source.ts b/libs/candles-chart/src/lib/data-source.ts index a53aa0ff6..121de546b 100644 --- a/libs/candles-chart/src/lib/data-source.ts +++ b/libs/candles-chart/src/lib/data-source.ts @@ -49,6 +49,7 @@ export class VegaDataSource implements DataSource { marketId: string; partyId: null | string; _decimalPlaces = 0; + _positionDecimalPlaces = 0; candlesSub: Subscription | null = null; @@ -60,6 +61,14 @@ export class VegaDataSource implements DataSource { return this._decimalPlaces; } + /** + * Indicates the number of position decimal places that an integer must be shifted by in order to get a correct + * number denominated in the unit size of the Market. + */ + get positionDecimalPlaces(): number { + return this._positionDecimalPlaces; + } + /** * * @param client - An ApolloClient instance. @@ -145,11 +154,14 @@ export class VegaDataSource implements DataSource { if (data?.market?.candlesConnection?.edges) { const decimalPlaces = data.market.decimalPlaces; + const positionDecimalPlaces = data.market.positionDecimalPlaces; const candles = data.market.candlesConnection.edges .map((edge) => edge?.node) .filter((node): node is CandleFieldsFragment => !!node) - .map((node) => parseCandle(node, decimalPlaces)); + .map((node) => + parseCandle(node, decimalPlaces, positionDecimalPlaces) + ); return candles; } else { @@ -180,7 +192,11 @@ export class VegaDataSource implements DataSource { this.candlesSub = res.subscribe(({ data }) => { if (data) { - const candle = parseCandle(data.candles, this.decimalPlaces); + const candle = parseCandle( + data.candles, + this.decimalPlaces, + this.positionDecimalPlaces + ); onSubscriptionData(candle); } @@ -197,7 +213,8 @@ export class VegaDataSource implements DataSource { function parseCandle( candle: CandleFieldsFragment, - decimalPlaces: number + decimalPlaces: number, + positionDecimalPlaces: number ): Candle { return { date: new Date(candle.periodStart), @@ -205,6 +222,6 @@ function parseCandle( low: Number(addDecimal(candle.low, decimalPlaces)), open: Number(addDecimal(candle.open, decimalPlaces)), close: Number(addDecimal(candle.close, decimalPlaces)), - volume: Number(candle.volume), + volume: Number(addDecimal(candle.volume, positionDecimalPlaces)), }; }