chore: server code formatting
This commit is contained in:
parent
aedabcbdee
commit
68517170e2
234 changed files with 9824 additions and 6195 deletions
|
|
@ -7,34 +7,52 @@ 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: () => ''
|
||||
cacheKey: () => '',
|
||||
})
|
||||
|
||||
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) {
|
||||
return getFiatRates()
|
||||
.then(({ data: fxRates }) => {
|
||||
const defaultFiatRate = findCurrencyRates(fxRates, defaultFiatMarket, fiatCodeProperty, rateProperty)
|
||||
const fxRate = findCurrencyRates(fxRates, fiatCode, fiatCodeProperty, rateProperty).div(defaultFiatRate)
|
||||
return {
|
||||
fxRate
|
||||
}
|
||||
})
|
||||
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) {
|
||||
function findCurrencyRates(fxRates, fiatCode, fiatCodeProperty, rateProperty) {
|
||||
const rates = _.find(_.matchesProperty(fiatCodeProperty, fiatCode), fxRates)
|
||||
if (!rates || !rates[rateProperty]) throw new Error(`Unsupported currency: ${fiatCode}`)
|
||||
if (!rates || !rates[rateProperty])
|
||||
throw new Error(`Unsupported currency: ${fiatCode}`)
|
||||
return new BN(rates[rateProperty].toString())
|
||||
}
|
||||
|
||||
|
|
@ -46,15 +64,20 @@ const getRate = (retries = 1, fiatCode, defaultFiatMarket) => {
|
|||
|
||||
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 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)
|
||||
})
|
||||
return getRate(++retries, fiatCode)
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = { getFiatRates, getRate }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue