asi-calculator/features/prices/hooks/use-prices.tsx
Matthias Kretschmann 436babf4ba
add CUDOS calculations (#40)
* add CUDOS calculations

* content updates

* token order switch

* uniswap quote fix

* list & link community proposals

* migration tool scenario simplification

* rename Uniswap label

* ratio harmonization

* buy ratio fix

* copy updates

* copy updates

* default to 1000 CUDOS
2024-09-19 21:01:39 +01:00

58 lines
1.4 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
cudos: 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')
const cudosAddress = getTokenAddressBySymbol('CUDOS')
if (!data || !oceanAddress || !fetAddress || !agixAddress || !cudosAddress)
return {
prices: {
ocean: { usd: 0, usd_24h_change: 0 },
fet: { usd: 0, usd_24h_change: 0 },
agix: { usd: 0, usd_24h_change: 0 },
cudos: { 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 cudos = data[cudosAddress]
const asi = fet
return { prices: { ocean, fet, agix, cudos, asi }, isValidating, isLoading }
}