From 227afbcb8856d0390289974b0d859faf64a1eaab Mon Sep 17 00:00:00 2001 From: losh11 Date: Sat, 29 Sep 2018 08:16:30 +0100 Subject: [PATCH] fix(ticker): use Coinbase API to fetch fiat ticker fixes #815 --- app/components/Activity/Invoice/Invoice.js | 2 +- app/components/Activity/Payment/Payment.js | 2 +- .../Activity/Transaction/Transaction.js | 7 +------ app/components/Contacts/Network/Network.js | 5 +---- app/components/Pay/Pay.js | 4 ++-- app/components/Pay/PaySummaryLightning.js | 2 +- app/components/Pay/PaySummaryOnChain.js | 2 +- app/components/Request/Request.js | 4 ++-- app/components/UI/FiatAmountInput.js | 4 ++-- app/components/UI/Value.js | 2 +- app/components/Wallet/Wallet.js | 2 +- app/lib/utils/api.js | 15 ++++++++------- app/reducers/contactsform.js | 4 ++-- app/reducers/ticker.js | 9 +++++---- internals/webpack/webpack.config.renderer.dev.js | 8 ++++---- internals/webpack/webpack.config.renderer.prod.js | 2 +- stories/containers/pay.stories.js | 12 +++--------- stories/containers/request.stories.js | 12 +++--------- .../components/Pay/PaySummaryLightning.spec.js | 12 +++--------- .../unit/components/Pay/PaySummaryOnchain.spec.js | 12 +++--------- 20 files changed, 46 insertions(+), 76 deletions(-) diff --git a/app/components/Activity/Invoice/Invoice.js b/app/components/Activity/Invoice/Invoice.js index de66f2a6..90881607 100644 --- a/app/components/Activity/Invoice/Invoice.js +++ b/app/components/Activity/Invoice/Invoice.js @@ -60,7 +60,7 @@ const Invoice = ({ invoice, ticker, currentTicker, showActivityModal, currencyNa diff --git a/app/components/Activity/Payment/Payment.js b/app/components/Activity/Payment/Payment.js index 9a7a7324..95db2f9c 100644 --- a/app/components/Activity/Payment/Payment.js +++ b/app/components/Activity/Payment/Payment.js @@ -65,7 +65,7 @@ const Payment = ({ diff --git a/app/components/Activity/Transaction/Transaction.js b/app/components/Activity/Transaction/Transaction.js index 4e7d60be..2301199c 100644 --- a/app/components/Activity/Transaction/Transaction.js +++ b/app/components/Activity/Transaction/Transaction.js @@ -61,12 +61,7 @@ const Transaction = ({ diff --git a/app/components/Contacts/Network/Network.js b/app/components/Contacts/Network/Network.js index 9ab17fc5..eba1db29 100644 --- a/app/components/Contacts/Network/Network.js +++ b/app/components/Contacts/Network/Network.js @@ -158,10 +158,7 @@ class Network extends Component { return 'online' } - const fiatAmount = btc.satoshisToFiat( - balance.channelBalance, - currentTicker[ticker.fiatTicker].last - ) + const fiatAmount = btc.satoshisToFiat(balance.channelBalance, currentTicker[ticker.fiatTicker]) const { refreshing } = this.state return ( diff --git a/app/components/Pay/Pay.js b/app/components/Pay/Pay.js index 39bc39d4..71e4bf64 100644 --- a/app/components/Pay/Pay.js +++ b/app/components/Pay/Pay.js @@ -316,7 +316,7 @@ class Pay extends React.Component { */ handleAmountCryptoChange = e => { const { cryptoCurrency, currentTicker, fiatCurrency } = this.props - const lastPrice = currentTicker[fiatCurrency].last + const lastPrice = currentTicker[fiatCurrency] const value = convert(cryptoCurrency, 'fiat', e.target.value, lastPrice) this.formApi.setValue('amountFiat', value) } @@ -326,7 +326,7 @@ class Pay extends React.Component { */ handleAmountFiatChange = e => { const { cryptoCurrency, currentTicker, fiatCurrency } = this.props - const lastPrice = currentTicker[fiatCurrency].last + const lastPrice = currentTicker[fiatCurrency] const value = convert('fiat', cryptoCurrency, e.target.value, lastPrice) this.formApi.setValue('amountCrypto', value) } diff --git a/app/components/Pay/PaySummaryLightning.js b/app/components/Pay/PaySummaryLightning.js index 67b5ba61..4d915c27 100644 --- a/app/components/Pay/PaySummaryLightning.js +++ b/app/components/Pay/PaySummaryLightning.js @@ -73,7 +73,7 @@ class PaySummaryLightning extends React.PureComponent { const { satoshis, payeeNodeKey } = invoice const descriptionTag = invoice.tags.find(tag => tag.tagName === 'description') || {} const memo = descriptionTag.data - const fiatAmount = satoshisToFiat(satoshis, currentTicker[fiatCurrency].last) + const fiatAmount = satoshisToFiat(satoshis, currentTicker[fiatCurrency]) const nodeAlias = getNodeAlias(payeeNodeKey, nodes) // Select an appropriate fee message... diff --git a/app/components/Pay/PaySummaryOnChain.js b/app/components/Pay/PaySummaryOnChain.js index 1f747da4..110cdbe6 100644 --- a/app/components/Pay/PaySummaryOnChain.js +++ b/app/components/Pay/PaySummaryOnChain.js @@ -69,7 +69,7 @@ class PaySummaryOnChain extends React.Component { ...rest } = this.props - const fiatAmount = satoshisToFiat(amount, currentTicker[fiatCurrency].last) + const fiatAmount = satoshisToFiat(amount, currentTicker[fiatCurrency]) const fee = get(onchainFees, 'fastestFee', null) return ( diff --git a/app/components/Request/Request.js b/app/components/Request/Request.js index c2208911..36274fa6 100644 --- a/app/components/Request/Request.js +++ b/app/components/Request/Request.js @@ -141,7 +141,7 @@ class Request extends React.Component { */ handleAmountCryptoChange = e => { const { cryptoCurrency, currentTicker, fiatCurrency } = this.props - const lastPrice = currentTicker[fiatCurrency].last + const lastPrice = currentTicker[fiatCurrency] const value = convert(cryptoCurrency, 'fiat', e.target.value, lastPrice) this.formApi.setValue('amountFiat', value) } @@ -151,7 +151,7 @@ class Request extends React.Component { */ handleAmountFiatChange = e => { const { cryptoCurrency, currentTicker, fiatCurrency } = this.props - const lastPrice = currentTicker[fiatCurrency].last + const lastPrice = currentTicker[fiatCurrency] const value = convert('fiat', cryptoCurrency, e.target.value, lastPrice) this.formApi.setValue('amountCrypto', value) } diff --git a/app/components/UI/FiatAmountInput.js b/app/components/UI/FiatAmountInput.js index d2453cd4..90f54b2d 100644 --- a/app/components/UI/FiatAmountInput.js +++ b/app/components/UI/FiatAmountInput.js @@ -28,8 +28,8 @@ class FiatAmountInput extends React.Component { if (currency !== prevProps.currency) { const { fieldApi } = this.props let value = fieldApi.getValue() - const lastPriceInOrigCurrency = currentTicker[prevProps.currency].last - const lastPriceInNewCurrency = currentTicker[currency].last + const lastPriceInOrigCurrency = currentTicker[prevProps.currency] + const lastPriceInNewCurrency = currentTicker[currency] // Convert to BTC. const btcValue = convert('fiat', 'btc', value, lastPriceInOrigCurrency) // Convert to new currency. diff --git a/app/components/UI/Value.js b/app/components/UI/Value.js index 8398dd98..8f83d9d2 100644 --- a/app/components/UI/Value.js +++ b/app/components/UI/Value.js @@ -8,7 +8,7 @@ const Value = ({ value, currency, currentTicker, fiatTicker }) => { } let price if (currency === 'fiat') { - price = currentTicker[fiatTicker].last + price = currentTicker[fiatTicker] } return ( diff --git a/app/components/Wallet/Wallet.js b/app/components/Wallet/Wallet.js index 14bf7a07..2be5890b 100644 --- a/app/components/Wallet/Wallet.js +++ b/app/components/Wallet/Wallet.js @@ -35,7 +35,7 @@ const Wallet = ({ }) => { const fiatAmount = btc.satoshisToFiat( parseInt(balance.walletBalance, 10) + parseInt(balance.channelBalance, 10), - currentTicker[ticker.fiatTicker].last + currentTicker[ticker.fiatTicker] ) return ( diff --git a/app/lib/utils/api.js b/app/lib/utils/api.js index ed121989..52f24241 100644 --- a/app/lib/utils/api.js +++ b/app/lib/utils/api.js @@ -12,8 +12,8 @@ import axios from 'axios' // defined on the webpack dev server. const scheme = process.env.HOT ? '/proxy/' : 'https://' -export function requestTicker() { - const BASE_URL = `${scheme}blockchain.info/ticker` +export function requestTicker(id) { + const BASE_URL = `${scheme}api.coinbase.com/v2/exchange-rates?currency=${id}` return axios({ method: 'get', url: BASE_URL @@ -21,11 +21,12 @@ export function requestTicker() { } export function requestTickers(ids) { - return axios - .all(ids.map(id => requestTicker(id))) - .then( - axios.spread((btcTicker, ltcTicker) => ({ btcTicker: btcTicker[0], ltcTicker: ltcTicker[0] })) - ) + return axios.all(ids.map(id => requestTicker(id))).then( + axios.spread((btcTicker, ltcTicker) => ({ + btcTicker: btcTicker.data.rates, + ltcTicker: ltcTicker.data.rates + })) + ) } export function requestSuggestedNodes() { diff --git a/app/reducers/contactsform.js b/app/reducers/contactsform.js index 4f732b5b..e2d2b11d 100644 --- a/app/reducers/contactsform.js +++ b/app/reducers/contactsform.js @@ -263,11 +263,11 @@ contactFormSelectors.contactFormFiatAmount = createSelector( tickerSelectors.currentTicker, fiatTickerSelector, (amount, currency, currentTicker, fiatTicker) => { - if (!currentTicker || !currentTicker[fiatTicker].last) { + if (!currentTicker || !currentTicker[fiatTicker]) { return false } - return btc.convert(currency, 'fiat', amount, currentTicker[fiatTicker].last) + return btc.convert(currency, 'fiat', amount, currentTicker[fiatTicker]) } ) diff --git a/app/reducers/ticker.js b/app/reducers/ticker.js index 7346f35a..121c0f00 100644 --- a/app/reducers/ticker.js +++ b/app/reducers/ticker.js @@ -1,5 +1,5 @@ import { createSelector } from 'reselect' -import { requestTicker } from 'lib/utils/api' +import { requestTickers } from 'lib/utils/api' import { currencies, getDefaultCurrency } from 'lib/i18n' import db from 'store/db' import { infoSelectors } from './info' @@ -68,9 +68,10 @@ export function recieveTickers({ btcTicker, ltcTicker }) { export const fetchTicker = () => async dispatch => { dispatch(getTickers()) - const btcTicker = await requestTicker() - dispatch(recieveTickers({ btcTicker })) - return btcTicker + const tickers = await requestTickers(['btc', 'ltc']) + dispatch(recieveTickers(tickers)) + + return tickers } // Receive IPC event for receiveCryptocurrency diff --git a/internals/webpack/webpack.config.renderer.dev.js b/internals/webpack/webpack.config.renderer.dev.js index d5d43d75..dba0416f 100644 --- a/internals/webpack/webpack.config.renderer.dev.js +++ b/internals/webpack/webpack.config.renderer.dev.js @@ -154,7 +154,7 @@ export default merge.smart(baseConfig, { "'self'", 'http://localhost:*', 'ws://localhost:*', - 'https://blockchain.info', + 'https://api.coinbase.com/', 'https://bitcoinfees.earn.com', 'https://zap.jackmallers.com' ], @@ -191,9 +191,9 @@ export default merge.smart(baseConfig, { pathRewrite: { '^/proxy/zap.jackmallers.com': '' }, changeOrigin: true }, - '/proxy/blockchain.info': { - target: 'https://blockchain.info/ticker', - pathRewrite: { '^/proxy/blockchain.info': '' }, + '/proxy/api.coinbase.com': { + target: 'https://api.coinbase.com', + pathRewrite: { '^/proxy/api.coinbase.com': '' }, changeOrigin: true } }, diff --git a/internals/webpack/webpack.config.renderer.prod.js b/internals/webpack/webpack.config.renderer.prod.js index 56886542..2d29cd55 100644 --- a/internals/webpack/webpack.config.renderer.prod.js +++ b/internals/webpack/webpack.config.renderer.prod.js @@ -100,7 +100,7 @@ export default merge.smart(baseConfig, { 'object-src': "'none'", 'connect-src': [ "'self'", - 'https://blockchain.info', + 'https://api.coinbase.com', 'https://bitcoinfees.earn.com', 'https://zap.jackmallers.com' ], diff --git a/stories/containers/pay.stories.js b/stories/containers/pay.stories.js index 03d08bdb..35374cd5 100644 --- a/stories/containers/pay.stories.js +++ b/stories/containers/pay.stories.js @@ -51,15 +51,9 @@ const store = new Store({ routes: [], currentTicker: { - USD: { - last: 6477.78 - }, - EUR: { - last: 5656.01 - }, - GBP: { - last: 5052.73 - } + USD: 6477.78, + EUR: 5656.01, + GBP: 5052.73 }, isProcessing: false diff --git a/stories/containers/request.stories.js b/stories/containers/request.stories.js index 847a14af..171ea72f 100644 --- a/stories/containers/request.stories.js +++ b/stories/containers/request.stories.js @@ -37,15 +37,9 @@ const store = new Store({ fiatCurrencies: ['USD', 'EUR', 'GBP'], currentTicker: { - USD: { - last: 6477.78 - }, - EUR: { - last: 5656.01 - }, - GBP: { - last: 5052.73 - } + USD: 6477.78, + EUR: 5656.01, + GBP: 5052.73 } }) diff --git a/test/unit/components/Pay/PaySummaryLightning.spec.js b/test/unit/components/Pay/PaySummaryLightning.spec.js index 79e3c1b0..03b9e4d1 100644 --- a/test/unit/components/Pay/PaySummaryLightning.spec.js +++ b/test/unit/components/Pay/PaySummaryLightning.spec.js @@ -5,15 +5,9 @@ import { PaySummaryLightning } from 'components/Pay' const props = { currentTicker: { - USD: { - last: 6477.78 - }, - EUR: { - last: 5656.01 - }, - GBP: { - last: 5052.73 - } + USD: 6477.78, + EUR: 5656.01, + GBP: 5052.73 }, cryptoCurrency: 'btc', cryptoCurrencyTicker: 'BTC', diff --git a/test/unit/components/Pay/PaySummaryOnchain.spec.js b/test/unit/components/Pay/PaySummaryOnchain.spec.js index 8ca4bcd6..433997a8 100644 --- a/test/unit/components/Pay/PaySummaryOnchain.spec.js +++ b/test/unit/components/Pay/PaySummaryOnchain.spec.js @@ -7,15 +7,9 @@ const props = { amount: 1000, address: 'mmxyr3LNKbnbrf6jdGXZpCE4EDpMSZRf4c', currentTicker: { - USD: { - last: 6477.78 - }, - EUR: { - last: 5656.01 - }, - GBP: { - last: 5052.73 - } + USD: 6477.78, + EUR: 5656.01, + GBP: 5052.73 }, cryptoCurrency: 'btc', cryptoCurrencyTicker: 'BTC',