import { useMemo } from "react"; import starkString from "starkstring"; import { useConvertBaseToQuote } from "../useConvertBaseToQuote"; import { useMarkets } from "../useMarkets"; import { useStepSize } from "../useStepSize"; import { ConvertContext } from "./context"; export const useConvertPrice = () => { if (typeof ConvertContext.context === "undefined") { throw new Error("You must use this hook under the ConvertProvider."); } const convert = useConvertBaseToQuote(); const { marketsSymbols } = useMarkets(); const { fromAsset, toAsset } = ConvertContext.useWatch(); const selectedMarket = useMemo(() => { return marketsSymbols?.find((i) => i.quoteAsset === toAsset); }, [marketsSymbols, toAsset]); const { toTickSize } = useStepSize(selectedMarket?.name, "market"); const { convertedPrice } = useMemo(() => { const fromCurrency = fromAsset?.toUpperCase(); const toCurrency = toAsset?.toUpperCase(); const amountInverse = convert(1, fromCurrency || "", toCurrency || ""); const amount = convert(1, toCurrency || "", fromCurrency || ""); const convertedPrice = { selectedMarket: toCurrency || "--", selectedAsset: fromCurrency || "--", value: starkString(toTickSize(amount, fromCurrency)) .toCurrency() .toString(), reversedValue: starkString(toTickSize(amountInverse, toCurrency)) .scientificNotationToDecimal() .toCurrency() .toString(), }; return { convertedPrice }; }, [convert, fromAsset, toAsset, toTickSize]); return { convertedPrice, }; };