import { useMemo } from "react"; import { useWatch } from "react-hook-form"; import { useAssets } from "../useAssets"; import { useMarketFilters } from "../useMarketFilters"; import { useMarkets } from "../useMarkets"; import { useStepSize } from "../useStepSize"; import { BuySellContext } from "./context"; export const useBuySellMarketConfigs = ({ mode }: { mode: "from" | "to" }) => { const { marketsSymbols } = useMarkets(); const { allAssets } = useAssets(); const { control } = BuySellContext.useFormContext(); const [toAsset, fromAsset] = useWatch({ name: ["toAsset", "fromAsset"], control, }); const { selectedCurrency, isFromFiat } = useMemo(() => { const selectedCurrency = allAssets?.find( ({ symbol }) => symbol === (mode === "from" ? fromAsset : toAsset), ); const isFromFiat = selectedCurrency?.currencyType === "Fiat"; return { selectedCurrency, isFromFiat, }; }, [allAssets, fromAsset, mode, toAsset]); const market = useMemo(() => { return marketsSymbols?.find((i) => isFromFiat ? i.quoteAsset === selectedCurrency?.symbol : i.baseAsset === selectedCurrency?.symbol, ); }, [isFromFiat, marketsSymbols, selectedCurrency?.symbol]); const { filterLotSize, filterPrice } = useMarketFilters({ selectedMarket: market, type: "market", }); const { toTickSize, toStepSize } = useStepSize(market?.name, "market"); const getMaxSize = marketsSymbols?.find( ({ quoteAsset }) => quoteAsset === selectedCurrency?.symbol, ) ? toTickSize : toStepSize; return { filterLotSize, filterPrice, getMaxSize, selectedCurrency, }; };