Merge pull request #1836 from siiky/feat/lam-1410/cache-forex-response

LAM-1410 feat: cache Forex responses
This commit is contained in:
Rafael Taranto 2025-05-12 08:36:04 +01:00 committed by GitHub
commit 5e2394e816

View file

@ -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 {