2019-05-05 13:34:21 +02:00
|
|
|
import React, { PureComponent } from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import ms from 'ms'
|
2020-02-09 17:16:10 +01:00
|
|
|
// 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'
|
2020-02-09 17:16:10 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-02-09 17:16:10 +01:00
|
|
|
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 = {
|
2019-05-06 23:39:59 +02:00
|
|
|
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),
|
2019-05-19 23:06:34 +02:00
|
|
|
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
|
2020-02-09 17:16:10 +01:00
|
|
|
global.ipcRenderer.on('accent-color', (event, accentColor) => {
|
2019-05-19 23:06:34 +02:00
|
|
|
this.setState({ accentColor })
|
|
|
|
})
|
|
|
|
|
2019-05-20 03:35:19 +02:00
|
|
|
// listener for touchbar
|
2020-02-09 17:16:10 +01:00
|
|
|
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 })
|
|
|
|
|
2019-05-09 23:28:58 +02:00
|
|
|
await this.setBalances()
|
2019-05-06 18:16:30 +02:00
|
|
|
|
2019-05-09 23:28:58 +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
|
|
|
}
|
|
|
|
|
2019-05-09 23:28:58 +02:00
|
|
|
return accountsPref
|
2019-05-05 13:34:21 +02:00
|
|
|
}
|
|
|
|
|
2019-05-09 23:28:58 +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
|
|
|
|
}
|
|
|
|
|
2019-05-09 23:28:58 +02:00
|
|
|
fetchAndSetPrices = async () => {
|
2019-05-25 02:33:54 +02:00
|
|
|
const currencies = conversions.join(',')
|
2019-05-09 23:28:58 +02:00
|
|
|
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-05-09 23:28:58 +02:00
|
|
|
|
2019-06-03 23:15:04 +02:00
|
|
|
const newPriceChanges = await Object.assign(
|
|
|
|
...conversions.map(key => ({
|
|
|
|
[key]: json['ocean-protocol'][key + '_24h_change']
|
|
|
|
}))
|
|
|
|
)
|
|
|
|
|
2020-02-09 17:16:10 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-05-09 23:28:58 +02:00
|
|
|
setBalances = async () => {
|
|
|
|
const accountsPref = await this.getAccounts()
|
2019-05-05 13:34:21 +02:00
|
|
|
|
2019-05-09 23:28:58 +02:00
|
|
|
let newAccounts = []
|
2019-05-08 01:02:02 +02:00
|
|
|
|
2019-05-09 23:28:58 +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: {
|
2019-05-09 23:28:58 +02:00
|
|
|
ocean: oceanBalance,
|
2019-05-25 02:33:54 +02:00
|
|
|
...conversionsBalance
|
2019-05-05 13:34:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-09 23:28:58 +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)
|
2020-02-09 17:16:10 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|