mirror of
https://github.com/kremalicious/asi-calculator.git
synced 2024-12-23 01:39:40 +01:00
Matthias Kretschmann
0d08ba807b
* tweak number display * select whole amount upon input focus * handle letter input * number formatting
54 lines
1.3 KiB
TypeScript
54 lines
1.3 KiB
TypeScript
'use client'
|
|
|
|
import { tokens } from '@/constants'
|
|
import { fetcher, getTokenAddressBySymbol } from '@/lib'
|
|
import useSWR from 'swr'
|
|
|
|
const tokenAddresses = tokens.map((token) => token.address).toString()
|
|
|
|
export type PriceCoingecko = {
|
|
usd: number
|
|
usd_24h_change: number
|
|
}
|
|
|
|
export type Prices = {
|
|
ocean: PriceCoingecko
|
|
fet: PriceCoingecko
|
|
agix: PriceCoingecko
|
|
asi: PriceCoingecko
|
|
}
|
|
|
|
export function usePrices(): {
|
|
prices: Prices
|
|
isValidating: boolean
|
|
isLoading: boolean
|
|
} {
|
|
const { data, isValidating, isLoading } = useSWR(
|
|
`/api/prices?tokens=${tokenAddresses}`,
|
|
fetcher
|
|
)
|
|
|
|
const oceanAddress = getTokenAddressBySymbol('OCEAN')
|
|
const fetAddress = getTokenAddressBySymbol('FET')
|
|
const agixAddress = getTokenAddressBySymbol('AGIX')
|
|
|
|
if (!data || !oceanAddress || !fetAddress || !agixAddress)
|
|
return {
|
|
prices: {
|
|
ocean: { usd: 0, usd_24h_change: 0 },
|
|
fet: { usd: 0, usd_24h_change: 0 },
|
|
agix: { usd: 0, usd_24h_change: 0 },
|
|
asi: { usd: 0, usd_24h_change: 0 }
|
|
},
|
|
isValidating,
|
|
isLoading
|
|
}
|
|
|
|
const ocean = data[oceanAddress]
|
|
const fet = data[fetAddress]
|
|
const agix = data[agixAddress]
|
|
const asi = fet
|
|
|
|
return { prices: { ocean, fet, agix, asi }, isValidating, isLoading }
|
|
}
|