feat: cache Forex responses
This commit is contained in:
parent
482b606177
commit
a477fa2393
1 changed files with 14 additions and 4 deletions
18
lib/forex.js
18
lib/forex.js
|
|
@ -1,19 +1,29 @@
|
||||||
const _ = require('lodash/fp')
|
const _ = require('lodash/fp')
|
||||||
const axios = require('axios')
|
const axios = require('axios')
|
||||||
|
const mem = require('mem')
|
||||||
|
|
||||||
const BN = require('./bn')
|
const BN = require('./bn')
|
||||||
|
const T = require('./time')
|
||||||
|
|
||||||
const MAX_ROTATIONS = 5
|
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 = [
|
const API_QUEUE = [
|
||||||
{ api: getBitPayFxRate, name: 'bitpay', fiatCodeProperty: 'code', rateProperty: 'rate' }
|
{ api: getBitPayFxRate, name: 'bitpay', fiatCodeProperty: 'code', rateProperty: 'rate' }
|
||||||
]
|
]
|
||||||
|
|
||||||
function getBitPayFxRate (fiatCode, defaultFiatMarket, fiatCodeProperty, rateProperty) {
|
function getBitPayFxRate (fiatCode, defaultFiatMarket, fiatCodeProperty, rateProperty) {
|
||||||
return axios.get('https://bitpay.com/rates')
|
return getFiatRates()
|
||||||
.then(response => {
|
.then(({ data: fxRates }) => {
|
||||||
const fxRates = response.data.data
|
|
||||||
const defaultFiatRate = findCurrencyRates(fxRates, defaultFiatMarket, fiatCodeProperty, rateProperty)
|
const defaultFiatRate = findCurrencyRates(fxRates, defaultFiatMarket, fiatCodeProperty, rateProperty)
|
||||||
const fxRate = findCurrencyRates(fxRates, fiatCode, fiatCodeProperty, rateProperty).div(defaultFiatRate)
|
const fxRate = findCurrencyRates(fxRates, fiatCode, fiatCodeProperty, rateProperty).div(defaultFiatRate)
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue