Skip to content

Commit

Permalink
Change logic for fetching prices
Browse files Browse the repository at this point in the history
  • Loading branch information
samchuk-vlad committed Nov 28, 2023
1 parent 3ce83a0 commit 8fe0aeb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
14 changes: 10 additions & 4 deletions src/services/prices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const coingeckoUrl = 'https://api.coingecko.com/api/v3/coins/markets?vs_currency
const pricesCache = new Cache<any>('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

Expand All @@ -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
})
}
Expand All @@ -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 }
}
6 changes: 3 additions & 3 deletions src/services/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -148,9 +148,9 @@ export async function runQueryOrUndefined<T extends ApiPromise | SubsocialApi>(
return query(api)
}

export const axiosGetRequest = async (url: string) => {
export const axiosGetRequest = async (url: string, config?: AxiosRequestConfig<any>) => {
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)
}
Expand Down

0 comments on commit 8fe0aeb

Please sign in to comment.