blowfish/src/renderer/store/AppProvider.jsx

151 lines
4.1 KiB
React
Raw Normal View History

2019-05-05 13:34:21 +02:00
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import ms from 'ms'
// import { ipcRenderer } from 'electron'
2019-05-08 01:02:02 +02:00
import Store from 'electron-store'
2019-05-08 21:41:24 +02:00
import { AppContext } from './createContext'
import { fetchData } from '../../utils'
import { refreshInterval, conversions, oceanTokenContract } from '../../config'
2019-05-25 02:33:54 +02:00
// construct initial prices Map to get consistent
// order for Ticker and Touchbar
let pricesMap = new Map()
pricesMap.set('ocean', 1)
conversions.map(key => pricesMap.set(key, 0))
2019-05-05 13:34:21 +02:00
export default class AppProvider extends PureComponent {
static propTypes = {
children: PropTypes.any.isRequired
}
store = process.env.NODE_ENV === 'test' ? new Store() : global.store
2019-05-08 01:02:02 +02:00
2019-05-05 13:34:21 +02:00
state = {
isLoading: true,
2019-05-06 00:10:28 +02:00
accounts: [],
currency: 'ocean',
2019-05-08 01:02:02 +02:00
needsConfig: false,
2019-05-25 02:33:54 +02:00
prices: pricesMap,
2019-06-03 23:15:04 +02:00
priceChanges: Object.assign(
...conversions.map(key => ({
[key]: 0
}))
),
2019-05-20 03:35:19 +02:00
toggleCurrencies: currency => this.toggleCurrencies(currency),
setBalances: () => this.setBalances(),
2019-05-29 01:46:41 +02:00
accentColor: '#f6388a'
2019-05-05 13:34:21 +02:00
}
2019-05-06 18:16:30 +02:00
async componentDidMount() {
2019-05-20 03:35:19 +02:00
// listener for accent color
global.ipcRenderer.on('accent-color', (event, accentColor) => {
this.setState({ accentColor })
})
2019-05-20 03:35:19 +02:00
// listener for touchbar
global.ipcRenderer.on('setCurrency', (evt, currency) =>
2019-05-20 03:35:19 +02:00
this.state.toggleCurrencies(currency)
)
const newPrizes = await this.fetchAndSetPrices()
this.setState({ prices: newPrizes })
await this.setBalances()
2019-05-06 18:16:30 +02:00
setInterval(this.fetchAndSetPrices, ms(refreshInterval))
setInterval(this.setBalances, ms(refreshInterval))
2019-05-07 00:25:31 +02:00
this.setState({ isLoading: false })
2019-05-05 13:34:21 +02:00
}
2019-05-08 01:02:02 +02:00
getAccounts() {
let accountsPref
if (this.store.has('accounts')) {
accountsPref = this.store.get('accounts')
!accountsPref.length
? this.setState({ needsConfig: true })
: this.setState({ needsConfig: false })
} else {
accountsPref = []
2019-05-10 00:37:59 +02:00
this.setState({ needsConfig: true })
2019-05-08 01:02:02 +02:00
}
return accountsPref
2019-05-05 13:34:21 +02:00
}
async getBalance(account) {
const json = await fetchData(
2019-05-06 00:10:28 +02:00
`https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress=${oceanTokenContract}&address=${account}&tag=latest`
)
2019-06-03 23:15:04 +02:00
const balance = json.result / 1e18 // Convert from vodka 10^18
2019-05-06 00:10:28 +02:00
return balance
}
fetchAndSetPrices = async () => {
2019-05-25 02:33:54 +02:00
const currencies = conversions.join(',')
const json = await fetchData(
2019-06-03 23:15:04 +02:00
`https://api.coingecko.com/api/v3/simple/price?ids=ocean-protocol&vs_currencies=${currencies}&include_24hr_change=true`
2019-05-06 00:10:28 +02:00
)
2019-05-25 02:33:54 +02:00
let newPrices = new Map(this.state.prices) // make a shallow copy of the Map
conversions.map(key => newPrices.set(key, json['ocean-protocol'][key])) // modify the copy
2019-06-03 23:15:04 +02:00
const newPriceChanges = await Object.assign(
...conversions.map(key => ({
[key]: json['ocean-protocol'][key + '_24h_change']
}))
)
global.ipcRenderer.send('prices-updated', Array.from(newPrices)) // convert Map to array, ipc messages seem to kill it
2019-06-03 23:15:04 +02:00
this.setState({ prices: newPrices, priceChanges: newPriceChanges })
2019-05-25 02:33:54 +02:00
return newPrices
2019-05-06 00:10:28 +02:00
}
setBalances = async () => {
const accountsPref = await this.getAccounts()
2019-05-05 13:34:21 +02:00
let newAccounts = []
2019-05-08 01:02:02 +02:00
for (const account of accountsPref) {
const oceanBalance = await this.getBalance(account)
2019-05-05 13:34:21 +02:00
2019-05-25 02:33:54 +02:00
const conversionsBalance = Object.assign(
...conversions.map(key => ({
[key]: oceanBalance * this.state.prices.get(key) || 0
2019-05-06 18:16:30 +02:00
}))
)
2019-05-05 13:34:21 +02:00
const newAccount = {
address: account,
balance: {
ocean: oceanBalance,
2019-05-25 02:33:54 +02:00
...conversionsBalance
2019-05-05 13:34:21 +02:00
}
}
newAccounts.push(newAccount)
}
if (newAccounts !== this.state.accounts) {
this.setState({ accounts: newAccounts })
}
2019-05-05 13:34:21 +02:00
}
2019-05-20 03:35:19 +02:00
toggleCurrencies(currency) {
2019-05-25 02:33:54 +02:00
const pricesNew = Array.from(this.state.prices)
global.ipcRenderer.send('currency-updated', pricesNew, currency)
2019-05-20 03:35:19 +02:00
this.setState({ currency })
}
2019-05-05 13:34:21 +02:00
render() {
2019-05-08 21:41:24 +02:00
return (
<AppContext.Provider value={this.state}>
{this.props.children}
</AppContext.Provider>
)
2019-05-05 13:34:21 +02:00
}
}