@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.
24 lines (22 loc) • 782 B
text/typescript
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, // 每分钟更新一次价格
staleTime: 60_000,
})
return {
price,
isLoading,
}
}