From 8293f7b3810572bd336969df18acd706d5a47938 Mon Sep 17 00:00:00 2001 From: Neal Conner Date: Fri, 20 Apr 2018 18:00:33 +0100 Subject: [PATCH] Use forex conversion for Bitstamp on non-USD/EUR (#111) --- lib/plugins/ticker/bitstamp/bitstamp.js | 39 +++++++++++++++++++++---- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/lib/plugins/ticker/bitstamp/bitstamp.js b/lib/plugins/ticker/bitstamp/bitstamp.js index e8a36a5d..aa35a781 100644 --- a/lib/plugins/ticker/bitstamp/bitstamp.js +++ b/lib/plugins/ticker/bitstamp/bitstamp.js @@ -1,7 +1,40 @@ +const axios = require('axios') +const _ = require('lodash/fp') + const BN = require('../../../bn') const common = require('../../common/bitstamp') -function ticker (account, fiatCode, cryptoCode) { +exports.NAME = 'Bitstamp' +exports.SUPPORTED_MODULES = ['ticker'] + +function findCurrency (fxRates, fiatCode) { + const rates = _.find(_.matchesProperty('code', fiatCode), fxRates) + if (!rates || !rates.rate) throw new Error(`Unsupported currency: ${fiatCode}`) + return BN(rates.rate) +} + +exports.ticker = function ticker (account, fiatCode, cryptoCode) { + if (fiatCode === 'USD' || fiatCode === 'EUR') { + return getCurrencyRates(fiatCode, cryptoCode) + } + + return axios.get('https://bitpay.com/api/rates') + .then(response => { + const fxRates = response.data + const usdRate = findCurrency(fxRates, 'USD') + const fxRate = findCurrency(fxRates, fiatCode).div(usdRate) + + return getCurrencyRates('USD', cryptoCode) + .then(res => ({ + rates: { + ask: res.rates.ask.times(fxRate), + bid: res.rates.bid.times(fxRate) + } + })) + }) +} + +function getCurrencyRates (fiatCode, cryptoCode) { return Promise.resolve() .then(() => { const market = common.buildMarket(fiatCode, cryptoCode) @@ -14,7 +47,3 @@ function ticker (account, fiatCode, cryptoCode) { } })) } - -module.exports = { - ticker -}