Merge pull request #1834 from RafaelTaranto/fix/forex-on-eur-based-exchanges
LAM-1406 fix: forex on eur based exchanges
This commit is contained in:
commit
65f8ddbecf
2 changed files with 7 additions and 19 deletions
24
lib/forex.js
24
lib/forex.js
|
|
@ -7,27 +7,15 @@ 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 API_QUEUE = [
|
const API_QUEUE = [
|
||||||
{ api: getBitPayFxRate, name: 'bitpay', fiatCodeProperty: 'code', rateProperty: 'rate' },
|
{ api: getBitPayFxRate, name: 'bitpay', fiatCodeProperty: 'code', rateProperty: 'rate' }
|
||||||
{ api: getCoinCapFxRate, name: 'coincap', fiatCodeProperty: 'symbol', rateProperty: 'rateUsd' }
|
|
||||||
]
|
]
|
||||||
|
|
||||||
function getBitPayFxRate (fiatCode, fiatCodeProperty, rateProperty) {
|
function getBitPayFxRate (fiatCode, defaultFiatMarket, fiatCodeProperty, rateProperty) {
|
||||||
return axios.get('https://bitpay.com/rates')
|
return axios.get('https://bitpay.com/rates')
|
||||||
.then(response => {
|
.then(response => {
|
||||||
const fxRates = response.data.data
|
const fxRates = response.data.data
|
||||||
const usdRate = findCurrencyRates(fxRates, 'USD', fiatCodeProperty, rateProperty)
|
const defaultFiatRate = findCurrencyRates(fxRates, defaultFiatMarket, fiatCodeProperty, rateProperty)
|
||||||
const fxRate = findCurrencyRates(fxRates, fiatCode, fiatCodeProperty, rateProperty).div(usdRate)
|
const fxRate = findCurrencyRates(fxRates, fiatCode, fiatCodeProperty, rateProperty).div(defaultFiatRate)
|
||||||
return {
|
|
||||||
fxRate
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
function getCoinCapFxRate (fiatCode, fiatCodeProperty, rateProperty) {
|
|
||||||
return axios.get('https://api.coincap.io/v2/rates')
|
|
||||||
.then(response => {
|
|
||||||
const fxRates = response.data.data
|
|
||||||
const fxRate = new BN(1).div(findCurrencyRates(fxRates, fiatCode, fiatCodeProperty, rateProperty))
|
|
||||||
return {
|
return {
|
||||||
fxRate
|
fxRate
|
||||||
}
|
}
|
||||||
|
|
@ -40,7 +28,7 @@ function findCurrencyRates (fxRates, fiatCode, fiatCodeProperty, rateProperty) {
|
||||||
return new BN(rates[rateProperty].toString())
|
return new BN(rates[rateProperty].toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
const getRate = (retries = 1, fiatCode) => {
|
const getRate = (retries = 1, fiatCode, defaultFiatMarket) => {
|
||||||
const selected = _.first(API_QUEUE).name
|
const selected = _.first(API_QUEUE).name
|
||||||
const activeAPI = _.first(API_QUEUE).api
|
const activeAPI = _.first(API_QUEUE).api
|
||||||
const fiatCodeProperty = _.first(API_QUEUE).fiatCodeProperty
|
const fiatCodeProperty = _.first(API_QUEUE).fiatCodeProperty
|
||||||
|
|
@ -48,7 +36,7 @@ const getRate = (retries = 1, fiatCode) => {
|
||||||
|
|
||||||
if (!activeAPI) throw new Error(`FOREX api ${selected} does not exist.`)
|
if (!activeAPI) throw new Error(`FOREX api ${selected} does not exist.`)
|
||||||
|
|
||||||
return activeAPI(fiatCode, fiatCodeProperty, rateProperty)
|
return activeAPI(fiatCode, defaultFiatMarket, fiatCodeProperty, rateProperty)
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
// Switch service
|
// Switch service
|
||||||
const erroredService = API_QUEUE.shift()
|
const erroredService = API_QUEUE.shift()
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ function ticker (fiatCode, cryptoCode, tickerName) {
|
||||||
return getCurrencyRates(ticker, fiatCode, cryptoCode)
|
return getCurrencyRates(ticker, fiatCode, cryptoCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
return getRate(RETRIES, fiatCode)
|
return getRate(RETRIES, tickerName, defaultFiatMarket(tickerName))
|
||||||
.then(({ fxRate }) => {
|
.then(({ fxRate }) => {
|
||||||
try {
|
try {
|
||||||
return getCurrencyRates(ticker, defaultFiatMarket(tickerName), cryptoCode)
|
return getCurrencyRates(ticker, defaultFiatMarket(tickerName), cryptoCode)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue