Skip to content

Commit

Permalink
fix(tradingview): handle case when initial price scale doesnt need to…
Browse files Browse the repository at this point in the history
… be set (#418)

* tradingview: handle case when initial price scale doesnt need to be set
  • Loading branch information
aforaleka authored Apr 2, 2024
1 parent 20d3fd1 commit d5dbad7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
14 changes: 9 additions & 5 deletions src/hooks/tradingView/useTradingView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,28 @@ export const useTradingView = ({
});

const savedResolution = getSavedResolution({ savedConfig: savedTvChartConfig });
const hasMarkets = marketIds.length > 0;

const [initialPriceScale, setInitialPriceScale] = useState<number | null>(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
);
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 = {
Expand Down Expand Up @@ -113,7 +117,7 @@ export const useTradingView = ({
tvWidgetRef.current = null;
setIsChartReady(false);
};
}, [selectedLocale, selectedNetwork, initialPriceScale, !!marketId]);
}, [selectedLocale, selectedNetwork, !!marketId, hasPriceScaleInfo]);

return { savedResolution };
};
4 changes: 2 additions & 2 deletions src/lib/tradingView/dydxfeed/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const configurationData: DatafeedConfiguration = {
export const getDydxDatafeed = (
store: RootStore,
getCandlesForDatafeed: ReturnType<typeof useDydxClient>['getCandlesForDatafeed'],
initialPriceScale: number
initialPriceScale: number | null
) => ({
onReady: (callback: OnReadyCallback) => {
setTimeout(() => callback(configurationData), 0);
Expand All @@ -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,
Expand Down

0 comments on commit d5dbad7

Please sign in to comment.