UNPKG

@openocean.finance/widget

Version:

Openocean Widget for cross-chain bridging and swapping. It will drive your multi-chain strategy and attract new users from everywhere.

27 lines (24 loc) 797 B
import { useQuery } from '@tanstack/react-query' import { OpenOceanService } from '../services/OpenOceanService.js' import type { TokenAmount } from '../types/token.js' export const useTokenPrice = (chainId?: number, token?: TokenAmount) => { const { data: price, isLoading } = useQuery({ queryKey: ['token-price', chainId, token?.address], queryFn: async () => { if (!chainId || !token?.address) { return undefined } const prices = await OpenOceanService.getTokensPrice(chainId.toString(), [ token.address, ]) return prices[token.address.toLowerCase()] || 0 }, enabled: !!chainId && !!token?.address, refetchInterval: 60_000, // Update price every minute staleTime: 60_000, }) return { price, isLoading, } }