Merge pull request #1836 from siiky/feat/lam-1410/cache-forex-response
LAM-1410 feat: cache Forex responses
This commit is contained in:
commit
5e2394e816
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 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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue