asi-calculator/features/prices/hooks/use-prices.tsx

42 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-03-30 19:11:24 +01:00
import { tokens } from '@/constants'
2024-04-01 04:25:28 +02:00
import { fetcher, getTokenAddressBySymbol } from '@/lib/utils'
2024-03-30 19:11:24 +01:00
import useSWR from 'swr'
2024-03-31 16:48:31 +02:00
const tokenAddresses = tokens.map((token) => token.address).toString()
2024-04-01 14:42:08 +02:00
export type Prices = {
ocean: number
fet: number
agix: number
asi: number
}
2024-03-31 05:32:11 +02:00
export function usePrices(): {
prices: { ocean: number; fet: number; agix: number; asi: number }
isValidating: boolean
2024-03-31 15:24:07 +02:00
isLoading: boolean
2024-03-31 05:32:11 +02:00
} {
2024-03-31 15:24:07 +02:00
const { data, isValidating, isLoading } = useSWR(
2024-03-31 16:48:31 +02:00
`/api/prices/?tokens=${tokenAddresses}`,
2024-03-30 19:11:24 +01:00
fetcher
)
2024-03-31 16:48:31 +02:00
const oceanAddress = getTokenAddressBySymbol('OCEAN')
const fetAddress = getTokenAddressBySymbol('FET')
const agixAddress = getTokenAddressBySymbol('AGIX')
if (!oceanAddress || !fetAddress || !agixAddress)
return {
prices: { ocean: 0, fet: 0, agix: 0, asi: 0 },
isValidating,
isLoading
}
const ocean = data?.[oceanAddress]?.usd || 0
const fet = data?.[fetAddress]?.usd || 0
const agix = data?.[agixAddress]?.usd || 0
2024-03-30 19:11:24 +01:00
const asi = fet
2024-03-31 15:24:07 +02:00
return { prices: { ocean, fet, agix, asi }, isValidating, isLoading }
2024-03-30 19:11:24 +01:00
}