import React, { createContext, useContext, useMemo } from 'react'; import { FixBigNumber, NetworkId } from '@jarvisnetwork/core-sdk'; import { useWeb3React } from '@web3-react/core'; import { useGetPrices } from '../../hooks/useGetPrices'; type PriceFeed = { [key in string]: FixBigNumber; }; export type NetworkPricesFeedMap = { [chainId in NetworkId]?: string[]; }; const PriceFeedContext = createContext({}); export const usePricesFeed = () => useContext(PriceFeedContext); export const usePriceFeed = (pair?: string): FixBigNumber | undefined => { const prices = useContext(PriceFeedContext); if (!pair) return undefined; return prices[pair]; }; export const PriceFeedProvider = ({ children, NetworkPricesFeed, }: { children: JSX.Element | JSX.Element[]; NetworkPricesFeed: NetworkPricesFeedMap; }) => { const { chainId } = useWeb3React(); const prices = useGetPrices(NetworkPricesFeed[chainId as NetworkId]); const priceFeeds = useMemo(() => { if (!prices.value) return {}; const result: { [key in string]: FixBigNumber; } = {}; for (const price of prices.value) { if (!price.pair || !price.price) continue; result[price.pair] = price.price; } return result; }, [prices.value]); return ( {children} ); };