chore: use monorepo organization
This commit is contained in:
parent
deaf7d6ecc
commit
a687827f7e
1099 changed files with 8184 additions and 11535 deletions
60
packages/server/lib/forex.js
Normal file
60
packages/server/lib/forex.js
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
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 = 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 getFiatRates()
|
||||
.then(({ data: fxRates }) => {
|
||||
const defaultFiatRate = findCurrencyRates(fxRates, defaultFiatMarket, fiatCodeProperty, rateProperty)
|
||||
const fxRate = findCurrencyRates(fxRates, fiatCode, fiatCodeProperty, rateProperty).div(defaultFiatRate)
|
||||
return {
|
||||
fxRate
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function findCurrencyRates (fxRates, fiatCode, fiatCodeProperty, rateProperty) {
|
||||
const rates = _.find(_.matchesProperty(fiatCodeProperty, fiatCode), fxRates)
|
||||
if (!rates || !rates[rateProperty]) throw new Error(`Unsupported currency: ${fiatCode}`)
|
||||
return new BN(rates[rateProperty].toString())
|
||||
}
|
||||
|
||||
const getRate = (retries = 1, fiatCode, defaultFiatMarket) => {
|
||||
const selected = _.first(API_QUEUE).name
|
||||
const activeAPI = _.first(API_QUEUE).api
|
||||
const fiatCodeProperty = _.first(API_QUEUE).fiatCodeProperty
|
||||
const rateProperty = _.first(API_QUEUE).rateProperty
|
||||
|
||||
if (!activeAPI) throw new Error(`FOREX api ${selected} does not exist.`)
|
||||
|
||||
return activeAPI(fiatCode, defaultFiatMarket, fiatCodeProperty, rateProperty)
|
||||
.catch(() => {
|
||||
// Switch service
|
||||
const erroredService = API_QUEUE.shift()
|
||||
API_QUEUE.push(erroredService)
|
||||
if (retries >= MAX_ROTATIONS) throw new Error(`FOREX API error from ${erroredService.name}`)
|
||||
|
||||
return getRate(++retries, fiatCode)
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = { getFiatRates, getRate }
|
||||
Loading…
Add table
Add a link
Reference in a new issue