2024-10-02 20:03:30 +00:00
|
|
|
import { SUPPORTED_CURRENCIES } from '@/lib/currency'
|
|
|
|
import { cachedFetcher } from '@/lib/fetch'
|
2023-01-27 23:20:33 +00:00
|
|
|
|
2024-10-09 00:26:29 +00:00
|
|
|
const getPrice = cachedFetcher(async function fetchPrice (fiat = 'USD') {
|
2024-10-02 20:03:30 +00:00
|
|
|
const url = `https://api.coinbase.com/v2/prices/BTC-${fiat}/spot`
|
|
|
|
try {
|
|
|
|
const res = await fetch(url)
|
|
|
|
const body = await res.json()
|
|
|
|
return parseFloat(body.data.amount)
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
return -1
|
2023-05-07 15:44:57 +00:00
|
|
|
}
|
2024-10-02 20:03:30 +00:00
|
|
|
}, {
|
|
|
|
maxSize: SUPPORTED_CURRENCIES.length,
|
|
|
|
cacheExpiry: 60 * 1000, // 1 minute
|
2024-10-06 22:58:46 +00:00
|
|
|
forceRefreshThreshold: 0, // never force refresh
|
|
|
|
keyGenerator: (fiat = 'USD') => fiat
|
2024-10-02 20:03:30 +00:00
|
|
|
})
|
2023-05-07 15:44:57 +00:00
|
|
|
|
2023-01-27 23:20:33 +00:00
|
|
|
export default {
|
|
|
|
Query: {
|
|
|
|
price: async (parent, { fiatCurrency }, ctx) => {
|
2024-10-02 20:03:30 +00:00
|
|
|
return await getPrice(fiatCurrency) || -1
|
2023-01-28 00:08:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|