Merge pull request #913 from josepfo/feat/coincap-api-forex

feat: coincap forex api fallback
This commit is contained in:
Rafael Taranto 2021-11-14 23:38:31 +00:00 committed by GitHub
commit a35c086efb
2 changed files with 63 additions and 15 deletions

View file

@ -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 }