diff --git a/src/services/prices.ts b/src/services/prices.ts index aedac5b..5ec1ea0 100644 --- a/src/services/prices.ts +++ b/src/services/prices.ts @@ -8,7 +8,7 @@ const coingeckoUrl = 'https://api.coingecko.com/api/v3/coins/markets?vs_currency const pricesCache = new Cache('prices', FIVE_MINUTES) const fetchPrices = async (ids: string) => { - const cacheData = await pricesCache.get(cacheKey) || ({} as any) + const cacheData = (await pricesCache.get(cacheKey)) || ({} as any) if (cacheData?.loading) return @@ -17,10 +17,14 @@ const fetchPrices = async (ids: string) => { loading: true }) - const newPrices = await axiosGetRequest(`${coingeckoUrl}&ids=${ids}`) + const newPrices = await axiosGetRequest(`${coingeckoUrl}&ids=${ids}`, { timeout: 5000 }) + + const newData = newPrices + ? { values: newPrices, isCachedData: false, lastUpdate: new Date().getTime() } + : { ...cacheData, isCachedData: true } await pricesCache.set(cacheKey, { - values: newPrices, + ...newData, loading: false }) } @@ -41,5 +45,7 @@ export const getPrices = async (ids: string) => { const cachedData = await pricesCache.get(cacheKey) - return cachedData?.values || [] + const { values, isCachedData, lastUpdate } = cachedData || {} + + return { prices: values || [], isCachedData: isCachedData, lastUpdate: lastUpdate } } diff --git a/src/services/utils.ts b/src/services/utils.ts index 49be695..8c6db82 100644 --- a/src/services/utils.ts +++ b/src/services/utils.ts @@ -6,7 +6,7 @@ import { isEthereumAddress } from '@polkadot/util-crypto' import { SubsocialApi } from "@subsocial/api" import { newLogger, nonEmptyArr } from "@subsocial/utils" import networks from "../connections/networks" -import axios from "axios" +import axios, { AxiosRequestConfig } from "axios" import { RelayChain } from "./crowdloan/types" import registry from "@subsocial/api/utils/registry" import Cache from "../cache" @@ -148,9 +148,9 @@ export async function runQueryOrUndefined( return query(api) } -export const axiosGetRequest = async (url: string) => { +export const axiosGetRequest = async (url: string, config?: AxiosRequestConfig) => { try { - const res = await axios.get(url) + const res = await axios.get(url, config) if (res.status !== 200) { log.error(`Failed request to ${url} with status`, res.status) }