1
0
mirror of https://github.com/kremalicious/blowfish.git synced 2024-12-30 16:47:52 +01:00

define conversion prices in config

This commit is contained in:
Matthias Kretschmann 2019-05-06 18:16:30 +02:00
parent 79e1ad5042
commit 33053e11e2
Signed by: m
GPG Key ID: 606EEEF3C479A91F
6 changed files with 47 additions and 40 deletions

View File

@ -1,5 +1,6 @@
module.exports = { module.exports = {
accounts: ['ETH ADDRESS 1', 'ETH ADDRESS 2'], accounts: ['ETH ADDRESS 1', 'ETH ADDRESS 2'],
prices: ['eur', 'usd', 'btc', 'eth'],
refreshInterval: '1m', refreshInterval: '1m',
oceanTokenContract: '0x985dd3D42De1e256d09e1c10F112bCCB8015AD41' oceanTokenContract: '0x985dd3D42De1e256d09e1c10F112bCCB8015AD41'
} }

View File

@ -7,11 +7,7 @@ export default class Account extends PureComponent {
static propTypes = { static propTypes = {
account: PropTypes.shape({ account: PropTypes.shape({
address: PropTypes.string.isRequired, address: PropTypes.string.isRequired,
balance: PropTypes.shape({ balance: PropTypes.object.isRequired
ocean: PropTypes.number.isRequired,
eur: PropTypes.number.isRequired,
usd: PropTypes.number.isRequired
}).isRequired
}) })
} }

View File

@ -18,8 +18,10 @@ const Balance = ({ balance }) => {
<span className="balance">Ξ {numberFormatter(eth) || 0}</span> <span className="balance">Ξ {numberFormatter(eth) || 0}</span>
) : currency === 'eur' ? ( ) : currency === 'eur' ? (
<span className="balance">{fiatFormatter('EUR', eur)}</span> <span className="balance">{fiatFormatter('EUR', eur)}</span>
) : ( ) : currency === 'usd' ? (
<span className="balance">{fiatFormatter('USD', usd)}</span> <span className="balance">{fiatFormatter('USD', usd)}</span>
) : (
<span className="balance">{numberFormatter(currency)}</span>
) )
} }
</Consumer> </Consumer>
@ -28,13 +30,7 @@ const Balance = ({ balance }) => {
} }
Balance.propTypes = { Balance.propTypes = {
balance: PropTypes.shape({ balance: PropTypes.object.isRequired
ocean: PropTypes.number.isRequired,
btc: PropTypes.number.isRequired,
eth: PropTypes.number.isRequired,
eur: PropTypes.number.isRequired,
usd: PropTypes.number.isRequired
})
} }
export default Balance export default Balance

View File

@ -1,6 +1,7 @@
import React from 'react' import React from 'react'
import { Consumer } from '../store/createContext' import { Consumer } from '../store/createContext'
import Balance from './Balance' import Balance from './Balance'
import { prices } from '../../config'
const calculateTotalBalance = (accounts, currency) => { const calculateTotalBalance = (accounts, currency) => {
const balanceTotalArray = [] const balanceTotalArray = []
@ -22,21 +23,18 @@ const Total = () => (
<div className="number-unit number-unit--main"> <div className="number-unit number-unit--main">
<Consumer> <Consumer>
{({ accounts }) => { {({ accounts }) => {
const totalOcean = calculateTotalBalance(accounts, 'ocean') const conversions = Object.assign(
const totalBtc = calculateTotalBalance(accounts, 'btc') ...prices.map(key => ({
const totalEth = calculateTotalBalance(accounts, 'eth') [key]: calculateTotalBalance(accounts, key)
const totalEur = calculateTotalBalance(accounts, 'eur') }))
const totalUsd = calculateTotalBalance(accounts, 'usd') )
const balance = { const balanceNew = {
ocean: totalOcean, ocean: calculateTotalBalance(accounts, 'ocean'),
btc: totalBtc, ...conversions
eth: totalEth,
eur: totalEur,
usd: totalUsd
} }
return <Balance balance={balance} /> return <Balance balance={balanceNew} />
}} }}
</Consumer> </Consumer>
<span className="label">Total balance</span> <span className="label">Total balance</span>

View File

@ -2,7 +2,12 @@ import React, { PureComponent } from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import ms from 'ms' import ms from 'ms'
import { Provider } from './createContext' import { Provider } from './createContext'
import { accounts, refreshInterval, oceanTokenContract } from '../../config' import {
accounts,
refreshInterval,
oceanTokenContract,
prices
} from '../../config'
export default class AppProvider extends PureComponent { export default class AppProvider extends PureComponent {
static propTypes = { static propTypes = {
@ -12,11 +17,15 @@ export default class AppProvider extends PureComponent {
state = { state = {
accounts: [], accounts: [],
currency: 'ocean', currency: 'ocean',
prices: Object.assign(...prices.map(key => ({ [key]: 0 }))),
toggleCurrencies: currency => this.setState({ currency }) toggleCurrencies: currency => this.setState({ currency })
} }
componentDidMount() { async componentDidMount() {
this.setBalances() await this.fetchAndSetPrices()
await this.setBalances()
setInterval(this.fetchAndSetPrices, ms(refreshInterval))
setInterval(this.setBalances, ms(refreshInterval)) setInterval(this.setBalances, ms(refreshInterval))
} }
@ -54,13 +63,19 @@ export default class AppProvider extends PureComponent {
return balance return balance
} }
fetchPrice = async () => { fetchAndSetPrices = async () => {
const currencies = prices.join(',')
const json = await this.fetch( 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'] await this.setState({
return { btc, eth, usd, eur } prices: Object.assign(
...prices.map(key => ({
[key]: json['ocean-protocol'][key]
}))
)
})
} }
setBalances = async () => { setBalances = async () => {
@ -68,23 +83,24 @@ export default class AppProvider extends PureComponent {
// when they are changed instead of resetting all to 0 here // when they are changed instead of resetting all to 0 here
this.clearAccounts() this.clearAccounts()
const { btc, eth, usd, eur } = await this.fetchPrice()
accounts.map(async account => { accounts.map(async account => {
const oceanBalance = await this.fetchBalance(account) const oceanBalance = await this.fetchBalance(account)
const conversions = Object.assign(
...prices.map(key => ({
[key]: oceanBalance * this.state.prices[key] || 0
}))
)
const newAccount = { const newAccount = {
address: account, address: account,
balance: { balance: {
ocean: oceanBalance || 0, ocean: oceanBalance || 0,
btc: oceanBalance * btc || 0, ...conversions
eth: oceanBalance * eth || 0,
eur: oceanBalance * eur || 0,
usd: oceanBalance * usd || 0
} }
} }
this.setState(prevState => ({ await this.setState(prevState => ({
accounts: [...prevState.accounts, newAccount] accounts: [...prevState.accounts, newAccount]
})) }))
}) })

View File

@ -4,4 +4,4 @@ const openUrl = url => {
shell.openExternal(url) shell.openExternal(url)
} }
export { openUrl } module.exports = { openUrl }