diff --git a/lib/forex.js b/lib/forex.js index aeb4f514..6c5ea781 100644 --- a/lib/forex.js +++ b/lib/forex.js @@ -1,19 +1,29 @@ const _ = require('lodash/fp') const axios = require('axios') +const mem = require('mem') + const BN = require('./bn') +const T = require('./time') const MAX_ROTATIONS = 5 -const getFiatRates = () => axios.get('https://bitpay.com/api/rates').then(response => response.data) +const _getFiatRates = () => ( + axios.get('https://bitpay.com/api/rates') + .then(response => response.data) +) + +const getFiatRates = mem(_getFiatRates, { + maxAge: 6 * T.hours, + cacheKey: () => '' +}) const API_QUEUE = [ { api: getBitPayFxRate, name: 'bitpay', fiatCodeProperty: 'code', rateProperty: 'rate' } ] function getBitPayFxRate (fiatCode, defaultFiatMarket, fiatCodeProperty, rateProperty) { - return axios.get('https://bitpay.com/rates') - .then(response => { - const fxRates = response.data.data + return getFiatRates() + .then(({ data: fxRates }) => { const defaultFiatRate = findCurrencyRates(fxRates, defaultFiatMarket, fiatCodeProperty, rateProperty) const fxRate = findCurrencyRates(fxRates, fiatCode, fiatCodeProperty, rateProperty).div(defaultFiatRate) return {