diff --git a/src/api/Fees.js b/src/api/Fees.js index 21063c9d..7967f9a7 100644 --- a/src/api/Fees.js +++ b/src/api/Fees.js @@ -1,5 +1,6 @@ // @flow import invariant from 'invariant' +import LRU from 'lru-cache' import type { Currency } from '@ledgerhq/live-common/lib/types' import createCustomErrorClass from 'helpers/createCustomErrorClass' import { blockchainBaseURL } from './Ledger' @@ -11,10 +12,20 @@ export type Fees = { [_: string]: number, } +const cache = LRU({ + maxAge: 5 * 60 * 1000, +}) + export const getEstimatedFees = async (currency: Currency): Promise => { + const key = currency.id + let promise = cache.get(key) + if (promise) return promise.then(r => r.data) const baseURL = blockchainBaseURL(currency) invariant(baseURL, `Fees for ${currency.id} are not supported`) - const { data, status } = await network({ method: 'GET', url: `${baseURL}/fees` }) + promise = network({ method: 'GET', url: `${baseURL}/fees` }) + cache.set(key, promise) + const { data, status } = await promise + if (status < 200 || status >= 300) cache.del(key) if (data) { return data }