feat: coincap forex api fallback
This commit is contained in:
parent
ff474ee507
commit
3af6d3a773
2 changed files with 63 additions and 15 deletions
|
|
@ -1,9 +1,10 @@
|
|||
const _ = require('lodash/fp')
|
||||
const axios = require('axios')
|
||||
const ccxt = require('ccxt')
|
||||
|
||||
const BN = require('../../bn')
|
||||
const { buildMarket, verifyFiatSupport } = require('../common/ccxt')
|
||||
const { getRate } = require('../../../lib/forex')
|
||||
|
||||
const RETRIES = 2
|
||||
|
||||
function ticker (fiatCode, cryptoCode, tickerName) {
|
||||
const ticker = new ccxt[tickerName]({ timeout: 3000 })
|
||||
|
|
@ -11,13 +12,9 @@ function ticker (fiatCode, cryptoCode, tickerName) {
|
|||
return getCurrencyRates(ticker, fiatCode, cryptoCode)
|
||||
}
|
||||
|
||||
return axios.get('https://bitpay.com/rates')
|
||||
.then(response => {
|
||||
return getRate(RETRIES, fiatCode)
|
||||
.then(({ fxRate }) => {
|
||||
try {
|
||||
const fxRates = response.data.data
|
||||
const usdRate = findCurrencyRates(fxRates, 'USD')
|
||||
const fxRate = findCurrencyRates(fxRates, fiatCode).div(usdRate)
|
||||
|
||||
return getCurrencyRates(ticker, 'USD', cryptoCode)
|
||||
.then(res => ({
|
||||
rates: {
|
||||
|
|
@ -49,10 +46,4 @@ function getCurrencyRates (ticker, fiatCode, cryptoCode) {
|
|||
}
|
||||
}
|
||||
|
||||
function findCurrencyRates (fxRates, fiatCode) {
|
||||
const rates = _.find(_.matchesProperty('code', fiatCode), fxRates)
|
||||
if (!rates || !rates.rate) throw new Error(`Unsupported currency: ${fiatCode}`)
|
||||
return new BN(rates.rate.toString())
|
||||
}
|
||||
|
||||
module.exports = { ticker }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue