From 33053e11e283b5d6ead280b74bde8848c5009bfd Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Mon, 6 May 2019 18:16:30 +0200 Subject: [PATCH] define conversion prices in config --- config.js | 1 + src/components/Account.jsx | 6 +----- src/components/Balance.jsx | 12 ++++------- src/components/Total.jsx | 22 +++++++++---------- src/store/AppProvider.jsx | 44 ++++++++++++++++++++++++++------------ src/util/openUrl.js | 2 +- 6 files changed, 47 insertions(+), 40 deletions(-) diff --git a/config.js b/config.js index 10a9fb2..d3a8d46 100644 --- a/config.js +++ b/config.js @@ -1,5 +1,6 @@ module.exports = { accounts: ['ETH ADDRESS 1', 'ETH ADDRESS 2'], + prices: ['eur', 'usd', 'btc', 'eth'], refreshInterval: '1m', oceanTokenContract: '0x985dd3D42De1e256d09e1c10F112bCCB8015AD41' } diff --git a/src/components/Account.jsx b/src/components/Account.jsx index b416814..3e90dde 100644 --- a/src/components/Account.jsx +++ b/src/components/Account.jsx @@ -7,11 +7,7 @@ export default class Account extends PureComponent { static propTypes = { account: PropTypes.shape({ address: PropTypes.string.isRequired, - balance: PropTypes.shape({ - ocean: PropTypes.number.isRequired, - eur: PropTypes.number.isRequired, - usd: PropTypes.number.isRequired - }).isRequired + balance: PropTypes.object.isRequired }) } diff --git a/src/components/Balance.jsx b/src/components/Balance.jsx index 13b490f..223b384 100644 --- a/src/components/Balance.jsx +++ b/src/components/Balance.jsx @@ -18,8 +18,10 @@ const Balance = ({ balance }) => { Ξ {numberFormatter(eth) || 0} ) : currency === 'eur' ? ( {fiatFormatter('EUR', eur)} - ) : ( + ) : currency === 'usd' ? ( {fiatFormatter('USD', usd)} + ) : ( + {numberFormatter(currency)} ) } @@ -28,13 +30,7 @@ const Balance = ({ balance }) => { } Balance.propTypes = { - balance: PropTypes.shape({ - ocean: PropTypes.number.isRequired, - btc: PropTypes.number.isRequired, - eth: PropTypes.number.isRequired, - eur: PropTypes.number.isRequired, - usd: PropTypes.number.isRequired - }) + balance: PropTypes.object.isRequired } export default Balance diff --git a/src/components/Total.jsx b/src/components/Total.jsx index 2c43464..9433d43 100644 --- a/src/components/Total.jsx +++ b/src/components/Total.jsx @@ -1,6 +1,7 @@ import React from 'react' import { Consumer } from '../store/createContext' import Balance from './Balance' +import { prices } from '../../config' const calculateTotalBalance = (accounts, currency) => { const balanceTotalArray = [] @@ -22,21 +23,18 @@ const Total = () => (
{({ accounts }) => { - const totalOcean = calculateTotalBalance(accounts, 'ocean') - const totalBtc = calculateTotalBalance(accounts, 'btc') - const totalEth = calculateTotalBalance(accounts, 'eth') - const totalEur = calculateTotalBalance(accounts, 'eur') - const totalUsd = calculateTotalBalance(accounts, 'usd') + const conversions = Object.assign( + ...prices.map(key => ({ + [key]: calculateTotalBalance(accounts, key) + })) + ) - const balance = { - ocean: totalOcean, - btc: totalBtc, - eth: totalEth, - eur: totalEur, - usd: totalUsd + const balanceNew = { + ocean: calculateTotalBalance(accounts, 'ocean'), + ...conversions } - return + return }} Total balance diff --git a/src/store/AppProvider.jsx b/src/store/AppProvider.jsx index 916c496..c71cb48 100644 --- a/src/store/AppProvider.jsx +++ b/src/store/AppProvider.jsx @@ -2,7 +2,12 @@ import React, { PureComponent } from 'react' import PropTypes from 'prop-types' import ms from 'ms' import { Provider } from './createContext' -import { accounts, refreshInterval, oceanTokenContract } from '../../config' +import { + accounts, + refreshInterval, + oceanTokenContract, + prices +} from '../../config' export default class AppProvider extends PureComponent { static propTypes = { @@ -12,11 +17,15 @@ export default class AppProvider extends PureComponent { state = { accounts: [], currency: 'ocean', + prices: Object.assign(...prices.map(key => ({ [key]: 0 }))), toggleCurrencies: currency => this.setState({ currency }) } - componentDidMount() { - this.setBalances() + async componentDidMount() { + await this.fetchAndSetPrices() + await this.setBalances() + + setInterval(this.fetchAndSetPrices, ms(refreshInterval)) setInterval(this.setBalances, ms(refreshInterval)) } @@ -54,13 +63,19 @@ export default class AppProvider extends PureComponent { return balance } - fetchPrice = async () => { + fetchAndSetPrices = async () => { + const currencies = prices.join(',') const json = await this.fetch( - 'https://api.coingecko.com/api/v3/simple/price?ids=ocean-protocol&vs_currencies=btc,eth,usd,eur' + `https://api.coingecko.com/api/v3/simple/price?ids=ocean-protocol&vs_currencies=${currencies}` ) - const { btc, eth, usd, eur } = json['ocean-protocol'] - return { btc, eth, usd, eur } + await this.setState({ + prices: Object.assign( + ...prices.map(key => ({ + [key]: json['ocean-protocol'][key] + })) + ) + }) } setBalances = async () => { @@ -68,23 +83,24 @@ export default class AppProvider extends PureComponent { // when they are changed instead of resetting all to 0 here this.clearAccounts() - const { btc, eth, usd, eur } = await this.fetchPrice() - accounts.map(async account => { const oceanBalance = await this.fetchBalance(account) + const conversions = Object.assign( + ...prices.map(key => ({ + [key]: oceanBalance * this.state.prices[key] || 0 + })) + ) + const newAccount = { address: account, balance: { ocean: oceanBalance || 0, - btc: oceanBalance * btc || 0, - eth: oceanBalance * eth || 0, - eur: oceanBalance * eur || 0, - usd: oceanBalance * usd || 0 + ...conversions } } - this.setState(prevState => ({ + await this.setState(prevState => ({ accounts: [...prevState.accounts, newAccount] })) }) diff --git a/src/util/openUrl.js b/src/util/openUrl.js index 27f0b7a..6848cd0 100644 --- a/src/util/openUrl.js +++ b/src/util/openUrl.js @@ -4,4 +4,4 @@ const openUrl = url => { shell.openExternal(url) } -export { openUrl } +module.exports = { openUrl }