Browse Source

fix(ticker): use Coinbase API to fetch fiat ticker

fixes #815
renovate/lint-staged-8.x
losh11 7 years ago
committed by Tom Kirkpatrick
parent
commit
227afbcb88
No known key found for this signature in database GPG Key ID: 72203A8EC5967EA8
  1. 2
      app/components/Activity/Invoice/Invoice.js
  2. 2
      app/components/Activity/Payment/Payment.js
  3. 7
      app/components/Activity/Transaction/Transaction.js
  4. 5
      app/components/Contacts/Network/Network.js
  5. 4
      app/components/Pay/Pay.js
  6. 2
      app/components/Pay/PaySummaryLightning.js
  7. 2
      app/components/Pay/PaySummaryOnChain.js
  8. 4
      app/components/Request/Request.js
  9. 4
      app/components/UI/FiatAmountInput.js
  10. 2
      app/components/UI/Value.js
  11. 2
      app/components/Wallet/Wallet.js
  12. 15
      app/lib/utils/api.js
  13. 4
      app/reducers/contactsform.js
  14. 9
      app/reducers/ticker.js
  15. 8
      internals/webpack/webpack.config.renderer.dev.js
  16. 2
      internals/webpack/webpack.config.renderer.prod.js
  17. 12
      stories/containers/pay.stories.js
  18. 12
      stories/containers/request.stories.js
  19. 12
      test/unit/components/Pay/PaySummaryLightning.spec.js
  20. 12
      test/unit/components/Pay/PaySummaryOnchain.spec.js

2
app/components/Activity/Invoice/Invoice.js

@ -60,7 +60,7 @@ const Invoice = ({ invoice, ticker, currentTicker, showActivityModal, currencyNa
<FormattedNumber <FormattedNumber
currency={ticker.fiatTicker} currency={ticker.fiatTicker}
style="currency" style="currency"
value={btc.convert('sats', 'fiat', invoice.value, currentTicker[ticker.fiatTicker].last)} value={btc.convert('sats', 'fiat', invoice.value, currentTicker[ticker.fiatTicker])}
/> />
</div> </div>
</div> </div>

2
app/components/Activity/Payment/Payment.js

@ -65,7 +65,7 @@ const Payment = ({
<FormattedNumber <FormattedNumber
currency={ticker.fiatTicker} currency={ticker.fiatTicker}
style="currency" style="currency"
value={btc.convert('sats', 'fiat', payment.value, currentTicker[ticker.fiatTicker].last)} value={btc.convert('sats', 'fiat', payment.value, currentTicker[ticker.fiatTicker])}
/> />
</div> </div>
</div> </div>

7
app/components/Activity/Transaction/Transaction.js

@ -61,12 +61,7 @@ const Transaction = ({
<FormattedNumber <FormattedNumber
currency={ticker.fiatTicker} currency={ticker.fiatTicker}
style="currency" style="currency"
value={btc.convert( value={btc.convert('sats', 'fiat', transaction.amount, currentTicker[ticker.fiatTicker])}
'sats',
'fiat',
transaction.amount,
currentTicker[ticker.fiatTicker].last
)}
/> />
</div> </div>
</div> </div>

5
app/components/Contacts/Network/Network.js

@ -158,10 +158,7 @@ class Network extends Component {
return 'online' return 'online'
} }
const fiatAmount = btc.satoshisToFiat( const fiatAmount = btc.satoshisToFiat(balance.channelBalance, currentTicker[ticker.fiatTicker])
balance.channelBalance,
currentTicker[ticker.fiatTicker].last
)
const { refreshing } = this.state const { refreshing } = this.state
return ( return (
<BackgroundTertiary className={styles.network}> <BackgroundTertiary className={styles.network}>

4
app/components/Pay/Pay.js

@ -316,7 +316,7 @@ class Pay extends React.Component {
*/ */
handleAmountCryptoChange = e => { handleAmountCryptoChange = e => {
const { cryptoCurrency, currentTicker, fiatCurrency } = this.props const { cryptoCurrency, currentTicker, fiatCurrency } = this.props
const lastPrice = currentTicker[fiatCurrency].last const lastPrice = currentTicker[fiatCurrency]
const value = convert(cryptoCurrency, 'fiat', e.target.value, lastPrice) const value = convert(cryptoCurrency, 'fiat', e.target.value, lastPrice)
this.formApi.setValue('amountFiat', value) this.formApi.setValue('amountFiat', value)
} }
@ -326,7 +326,7 @@ class Pay extends React.Component {
*/ */
handleAmountFiatChange = e => { handleAmountFiatChange = e => {
const { cryptoCurrency, currentTicker, fiatCurrency } = this.props const { cryptoCurrency, currentTicker, fiatCurrency } = this.props
const lastPrice = currentTicker[fiatCurrency].last const lastPrice = currentTicker[fiatCurrency]
const value = convert('fiat', cryptoCurrency, e.target.value, lastPrice) const value = convert('fiat', cryptoCurrency, e.target.value, lastPrice)
this.formApi.setValue('amountCrypto', value) this.formApi.setValue('amountCrypto', value)
} }

2
app/components/Pay/PaySummaryLightning.js

@ -73,7 +73,7 @@ class PaySummaryLightning extends React.PureComponent {
const { satoshis, payeeNodeKey } = invoice const { satoshis, payeeNodeKey } = invoice
const descriptionTag = invoice.tags.find(tag => tag.tagName === 'description') || {} const descriptionTag = invoice.tags.find(tag => tag.tagName === 'description') || {}
const memo = descriptionTag.data const memo = descriptionTag.data
const fiatAmount = satoshisToFiat(satoshis, currentTicker[fiatCurrency].last) const fiatAmount = satoshisToFiat(satoshis, currentTicker[fiatCurrency])
const nodeAlias = getNodeAlias(payeeNodeKey, nodes) const nodeAlias = getNodeAlias(payeeNodeKey, nodes)
// Select an appropriate fee message... // Select an appropriate fee message...

2
app/components/Pay/PaySummaryOnChain.js

@ -69,7 +69,7 @@ class PaySummaryOnChain extends React.Component {
...rest ...rest
} = this.props } = this.props
const fiatAmount = satoshisToFiat(amount, currentTicker[fiatCurrency].last) const fiatAmount = satoshisToFiat(amount, currentTicker[fiatCurrency])
const fee = get(onchainFees, 'fastestFee', null) const fee = get(onchainFees, 'fastestFee', null)
return ( return (
<Box {...rest}> <Box {...rest}>

4
app/components/Request/Request.js

@ -141,7 +141,7 @@ class Request extends React.Component {
*/ */
handleAmountCryptoChange = e => { handleAmountCryptoChange = e => {
const { cryptoCurrency, currentTicker, fiatCurrency } = this.props const { cryptoCurrency, currentTicker, fiatCurrency } = this.props
const lastPrice = currentTicker[fiatCurrency].last const lastPrice = currentTicker[fiatCurrency]
const value = convert(cryptoCurrency, 'fiat', e.target.value, lastPrice) const value = convert(cryptoCurrency, 'fiat', e.target.value, lastPrice)
this.formApi.setValue('amountFiat', value) this.formApi.setValue('amountFiat', value)
} }
@ -151,7 +151,7 @@ class Request extends React.Component {
*/ */
handleAmountFiatChange = e => { handleAmountFiatChange = e => {
const { cryptoCurrency, currentTicker, fiatCurrency } = this.props const { cryptoCurrency, currentTicker, fiatCurrency } = this.props
const lastPrice = currentTicker[fiatCurrency].last const lastPrice = currentTicker[fiatCurrency]
const value = convert('fiat', cryptoCurrency, e.target.value, lastPrice) const value = convert('fiat', cryptoCurrency, e.target.value, lastPrice)
this.formApi.setValue('amountCrypto', value) this.formApi.setValue('amountCrypto', value)
} }

4
app/components/UI/FiatAmountInput.js

@ -28,8 +28,8 @@ class FiatAmountInput extends React.Component {
if (currency !== prevProps.currency) { if (currency !== prevProps.currency) {
const { fieldApi } = this.props const { fieldApi } = this.props
let value = fieldApi.getValue() let value = fieldApi.getValue()
const lastPriceInOrigCurrency = currentTicker[prevProps.currency].last const lastPriceInOrigCurrency = currentTicker[prevProps.currency]
const lastPriceInNewCurrency = currentTicker[currency].last const lastPriceInNewCurrency = currentTicker[currency]
// Convert to BTC. // Convert to BTC.
const btcValue = convert('fiat', 'btc', value, lastPriceInOrigCurrency) const btcValue = convert('fiat', 'btc', value, lastPriceInOrigCurrency)
// Convert to new currency. // Convert to new currency.

2
app/components/UI/Value.js

@ -8,7 +8,7 @@ const Value = ({ value, currency, currentTicker, fiatTicker }) => {
} }
let price let price
if (currency === 'fiat') { if (currency === 'fiat') {
price = currentTicker[fiatTicker].last price = currentTicker[fiatTicker]
} }
return ( return (
<i> <i>

2
app/components/Wallet/Wallet.js

@ -35,7 +35,7 @@ const Wallet = ({
}) => { }) => {
const fiatAmount = btc.satoshisToFiat( const fiatAmount = btc.satoshisToFiat(
parseInt(balance.walletBalance, 10) + parseInt(balance.channelBalance, 10), parseInt(balance.walletBalance, 10) + parseInt(balance.channelBalance, 10),
currentTicker[ticker.fiatTicker].last currentTicker[ticker.fiatTicker]
) )
return ( return (

15
app/lib/utils/api.js

@ -12,8 +12,8 @@ import axios from 'axios'
// defined on the webpack dev server. // defined on the webpack dev server.
const scheme = process.env.HOT ? '/proxy/' : 'https://' const scheme = process.env.HOT ? '/proxy/' : 'https://'
export function requestTicker() { export function requestTicker(id) {
const BASE_URL = `${scheme}blockchain.info/ticker` const BASE_URL = `${scheme}api.coinbase.com/v2/exchange-rates?currency=${id}`
return axios({ return axios({
method: 'get', method: 'get',
url: BASE_URL url: BASE_URL
@ -21,11 +21,12 @@ export function requestTicker() {
} }
export function requestTickers(ids) { export function requestTickers(ids) {
return axios return axios.all(ids.map(id => requestTicker(id))).then(
.all(ids.map(id => requestTicker(id))) axios.spread((btcTicker, ltcTicker) => ({
.then( btcTicker: btcTicker.data.rates,
axios.spread((btcTicker, ltcTicker) => ({ btcTicker: btcTicker[0], ltcTicker: ltcTicker[0] })) ltcTicker: ltcTicker.data.rates
) }))
)
} }
export function requestSuggestedNodes() { export function requestSuggestedNodes() {

4
app/reducers/contactsform.js

@ -263,11 +263,11 @@ contactFormSelectors.contactFormFiatAmount = createSelector(
tickerSelectors.currentTicker, tickerSelectors.currentTicker,
fiatTickerSelector, fiatTickerSelector,
(amount, currency, currentTicker, fiatTicker) => { (amount, currency, currentTicker, fiatTicker) => {
if (!currentTicker || !currentTicker[fiatTicker].last) { if (!currentTicker || !currentTicker[fiatTicker]) {
return false return false
} }
return btc.convert(currency, 'fiat', amount, currentTicker[fiatTicker].last) return btc.convert(currency, 'fiat', amount, currentTicker[fiatTicker])
} }
) )

9
app/reducers/ticker.js

@ -1,5 +1,5 @@
import { createSelector } from 'reselect' import { createSelector } from 'reselect'
import { requestTicker } from 'lib/utils/api' import { requestTickers } from 'lib/utils/api'
import { currencies, getDefaultCurrency } from 'lib/i18n' import { currencies, getDefaultCurrency } from 'lib/i18n'
import db from 'store/db' import db from 'store/db'
import { infoSelectors } from './info' import { infoSelectors } from './info'
@ -68,9 +68,10 @@ export function recieveTickers({ btcTicker, ltcTicker }) {
export const fetchTicker = () => async dispatch => { export const fetchTicker = () => async dispatch => {
dispatch(getTickers()) dispatch(getTickers())
const btcTicker = await requestTicker() const tickers = await requestTickers(['btc', 'ltc'])
dispatch(recieveTickers({ btcTicker })) dispatch(recieveTickers(tickers))
return btcTicker
return tickers
} }
// Receive IPC event for receiveCryptocurrency // Receive IPC event for receiveCryptocurrency

8
internals/webpack/webpack.config.renderer.dev.js

@ -154,7 +154,7 @@ export default merge.smart(baseConfig, {
"'self'", "'self'",
'http://localhost:*', 'http://localhost:*',
'ws://localhost:*', 'ws://localhost:*',
'https://blockchain.info', 'https://api.coinbase.com/',
'https://bitcoinfees.earn.com', 'https://bitcoinfees.earn.com',
'https://zap.jackmallers.com' 'https://zap.jackmallers.com'
], ],
@ -191,9 +191,9 @@ export default merge.smart(baseConfig, {
pathRewrite: { '^/proxy/zap.jackmallers.com': '' }, pathRewrite: { '^/proxy/zap.jackmallers.com': '' },
changeOrigin: true changeOrigin: true
}, },
'/proxy/blockchain.info': { '/proxy/api.coinbase.com': {
target: 'https://blockchain.info/ticker', target: 'https://api.coinbase.com',
pathRewrite: { '^/proxy/blockchain.info': '' }, pathRewrite: { '^/proxy/api.coinbase.com': '' },
changeOrigin: true changeOrigin: true
} }
}, },

2
internals/webpack/webpack.config.renderer.prod.js

@ -100,7 +100,7 @@ export default merge.smart(baseConfig, {
'object-src': "'none'", 'object-src': "'none'",
'connect-src': [ 'connect-src': [
"'self'", "'self'",
'https://blockchain.info', 'https://api.coinbase.com',
'https://bitcoinfees.earn.com', 'https://bitcoinfees.earn.com',
'https://zap.jackmallers.com' 'https://zap.jackmallers.com'
], ],

12
stories/containers/pay.stories.js

@ -51,15 +51,9 @@ const store = new Store({
routes: [], routes: [],
currentTicker: { currentTicker: {
USD: { USD: 6477.78,
last: 6477.78 EUR: 5656.01,
}, GBP: 5052.73
EUR: {
last: 5656.01
},
GBP: {
last: 5052.73
}
}, },
isProcessing: false isProcessing: false

12
stories/containers/request.stories.js

@ -37,15 +37,9 @@ const store = new Store({
fiatCurrencies: ['USD', 'EUR', 'GBP'], fiatCurrencies: ['USD', 'EUR', 'GBP'],
currentTicker: { currentTicker: {
USD: { USD: 6477.78,
last: 6477.78 EUR: 5656.01,
}, GBP: 5052.73
EUR: {
last: 5656.01
},
GBP: {
last: 5052.73
}
} }
}) })

12
test/unit/components/Pay/PaySummaryLightning.spec.js

@ -5,15 +5,9 @@ import { PaySummaryLightning } from 'components/Pay'
const props = { const props = {
currentTicker: { currentTicker: {
USD: { USD: 6477.78,
last: 6477.78 EUR: 5656.01,
}, GBP: 5052.73
EUR: {
last: 5656.01
},
GBP: {
last: 5052.73
}
}, },
cryptoCurrency: 'btc', cryptoCurrency: 'btc',
cryptoCurrencyTicker: 'BTC', cryptoCurrencyTicker: 'BTC',

12
test/unit/components/Pay/PaySummaryOnchain.spec.js

@ -7,15 +7,9 @@ const props = {
amount: 1000, amount: 1000,
address: 'mmxyr3LNKbnbrf6jdGXZpCE4EDpMSZRf4c', address: 'mmxyr3LNKbnbrf6jdGXZpCE4EDpMSZRf4c',
currentTicker: { currentTicker: {
USD: { USD: 6477.78,
last: 6477.78 EUR: 5656.01,
}, GBP: 5052.73
EUR: {
last: 5656.01
},
GBP: {
last: 5052.73
}
}, },
cryptoCurrency: 'btc', cryptoCurrency: 'btc',
cryptoCurrencyTicker: 'BTC', cryptoCurrencyTicker: 'BTC',

Loading…
Cancel
Save