From d5dbad78e087369428a41980b404e39fc47135b5 Mon Sep 17 00:00:00 2001 From: aleka Date: Tue, 2 Apr 2024 12:53:23 -0400 Subject: [PATCH] fix(tradingview): handle case when initial price scale doesnt need to be set (#418) * tradingview: handle case when initial price scale doesnt need to be set --- src/hooks/tradingView/useTradingView.ts | 14 +++++++++----- src/lib/tradingView/dydxfeed/index.ts | 4 ++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/hooks/tradingView/useTradingView.ts b/src/hooks/tradingView/useTradingView.ts index fc839b9b6..1a49a88e6 100644 --- a/src/hooks/tradingView/useTradingView.ts +++ b/src/hooks/tradingView/useTradingView.ts @@ -51,13 +51,17 @@ export const useTradingView = ({ }); const savedResolution = getSavedResolution({ savedConfig: savedTvChartConfig }); - const hasMarkets = marketIds.length > 0; const [initialPriceScale, setInitialPriceScale] = useState(null); + const hasMarkets = marketIds.length > 0; + const hasPriceScaleInfo = initialPriceScale !== null || hasMarkets; + useEffect(() => { + // we only need tick size from current market for the price scale settings + // if markets haven't been loaded via abacus, get the current market info from indexer (async () => { - if (marketId && !hasMarkets) { + if (marketId && !hasPriceScaleInfo) { const marketTickSize = await getMarketTickSize(marketId); const priceScale = BigNumber(10).exponentiatedBy( BigNumber(marketTickSize).decimalPlaces() ?? 2 @@ -65,10 +69,10 @@ export const useTradingView = ({ setInitialPriceScale(priceScale.toNumber()); } })(); - }, [marketId!!, hasMarkets]); + }, [marketId, hasPriceScaleInfo]); useEffect(() => { - if (marketId && initialPriceScale !== null) { + if (marketId && hasPriceScaleInfo) { const widgetOptions = getWidgetOptions(); const widgetOverrides = getWidgetOverrides({ appTheme, appColorMode }); const options = { @@ -113,7 +117,7 @@ export const useTradingView = ({ tvWidgetRef.current = null; setIsChartReady(false); }; - }, [selectedLocale, selectedNetwork, initialPriceScale, !!marketId]); + }, [selectedLocale, selectedNetwork, !!marketId, hasPriceScaleInfo]); return { savedResolution }; }; diff --git a/src/lib/tradingView/dydxfeed/index.ts b/src/lib/tradingView/dydxfeed/index.ts index 7ce45c30a..8295d9ba8 100644 --- a/src/lib/tradingView/dydxfeed/index.ts +++ b/src/lib/tradingView/dydxfeed/index.ts @@ -47,7 +47,7 @@ const configurationData: DatafeedConfiguration = { export const getDydxDatafeed = ( store: RootStore, getCandlesForDatafeed: ReturnType['getCandlesForDatafeed'], - initialPriceScale: number + initialPriceScale: number | null ) => ({ onReady: (callback: OnReadyCallback) => { setTimeout(() => callback(configurationData), 0); @@ -70,7 +70,7 @@ export const getDydxDatafeed = ( const symbolItem = getSymbol(symbolName || DEFAULT_MARKETID); const { tickSizeDecimals } = getMarketConfig(symbolItem.symbol)(store.getState()) || {}; - const pricescale = tickSizeDecimals ? 10 ** tickSizeDecimals : initialPriceScale; + const pricescale = tickSizeDecimals ? 10 ** tickSizeDecimals : initialPriceScale ?? 100; const symbolInfo: LibrarySymbolInfo = { ticker: symbolItem.full_name,