asi-calculator/hooks/use-prices.tsx

24 lines
598 B
TypeScript
Raw Normal View History

2024-03-30 19:11:24 +01:00
'use client'
import { tokens } from '@/constants'
import { fetcher } from '@/utils'
import useSWR from 'swr'
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-30 19:11:24 +01:00
`/api/prices/?tokens=${tokens.toString()}`,
fetcher
)
2024-03-31 05:32:11 +02:00
const ocean = data?.[tokens[0]]?.usd || 0
const fet = data?.[tokens[1]]?.usd || 0
const agix = data?.[tokens[2]]?.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
}