blowfish/src/renderer/store/AppProvider.jsx

129 lines
3.3 KiB
React
Raw Normal View History

2020-02-25 04:10:06 +01:00
import React, { useContext, useState, useEffect } from 'react'
2019-05-05 13:34:21 +02:00
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'
2020-02-25 04:10:06 +01:00
import unit from 'ethjs-unit'
import { AppContext, PriceContext } from './createContext'
import { fetchData } from '../../utils'
import { refreshInterval, conversions, oceanTokenContract } from '../../config'
2019-05-25 02:33:54 +02:00
2020-02-25 04:10:06 +01:00
async function getBalance(account) {
const json = await fetchData(
2020-02-25 15:16:44 +01:00
`https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress=${oceanTokenContract}&address=${account}&tag=latest&apikey=${process.env.ETHERSCAN_API_KEY}`
2020-02-25 04:10:06 +01:00
)
2019-05-05 13:34:21 +02:00
2020-02-25 04:10:06 +01:00
const balance = unit.fromWei(`${json.result}`, 'ether')
return balance
}
2019-05-05 13:34:21 +02:00
2020-02-25 04:10:06 +01:00
export default function AppProvider({ children }) {
const { prices } = useContext(PriceContext)
const [isLoading, setIsLoading] = useState(true)
const [accounts, setAccounts] = useState([])
const [needsConfig, setNeedsConfig] = useState(false)
const [currency, setCurrency] = useState('ocean')
const [accentColor, setAccentColor] = useState('#f6388a')
2020-02-25 15:16:44 +01:00
const [error, setError] = useState()
2019-05-05 13:34:21 +02:00
2020-02-25 04:10:06 +01:00
useEffect(() => {
2019-05-20 03:35:19 +02:00
// listener for accent color
2020-02-25 15:16:44 +01:00
if (process.env.NODE_ENV !== 'test') {
global.ipcRenderer.on('accent-color', (evt, accentColor) => {
setAccentColor(accentColor)
})
}
2020-02-25 04:10:06 +01:00
}, [])
2020-02-25 04:10:06 +01:00
useEffect(() => {
async function init() {
2020-02-25 15:16:44 +01:00
try {
await setBalances()
setIsLoading(false)
} catch (error) {
console.error(error.message)
setError(error.message)
}
2019-05-20 03:35:19 +02:00
2020-02-25 04:10:06 +01:00
// listener for touchbar
global.ipcRenderer.on('setCurrency', (evt, currency) =>
toggleCurrencies(currency)
)
}
2019-05-06 18:16:30 +02:00
2020-02-25 04:10:06 +01:00
init()
setInterval(init, ms(refreshInterval))
2019-05-07 00:25:31 +02:00
2020-02-25 04:10:06 +01:00
return () => {
clearInterval(init)
}
}, [prices])
2019-05-05 13:34:21 +02:00
2020-02-25 04:10:06 +01:00
function getAccounts() {
2019-05-08 01:02:02 +02:00
let accountsPref
2020-02-25 04:10:06 +01:00
const store = process.env.NODE_ENV === 'test' ? new Store() : global.store
2019-05-08 01:02:02 +02:00
2020-02-25 04:10:06 +01:00
if (store.has('accounts')) {
accountsPref = store.get('accounts')
!accountsPref.length ? setNeedsConfig(true) : setNeedsConfig(false)
2019-05-08 01:02:02 +02:00
} else {
accountsPref = []
2020-02-25 04:10:06 +01:00
setNeedsConfig(true)
2019-05-08 01:02:02 +02:00
}
return accountsPref
2019-05-05 13:34:21 +02:00
}
2020-02-25 04:10:06 +01:00
async function setBalances() {
let newAccounts = []
2020-02-25 04:10:06 +01:00
const accountsPref = await getAccounts()
2019-05-08 01:02:02 +02:00
for (const account of accountsPref) {
2020-02-25 04:10:06 +01:00
const oceanBalance = await 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 => ({
2020-02-25 04:10:06 +01:00
[key]: oceanBalance * 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)
}
2020-02-25 04:10:06 +01:00
if (newAccounts !== accounts) {
setAccounts(newAccounts)
}
2019-05-05 13:34:21 +02:00
}
2020-02-25 04:10:06 +01:00
function toggleCurrencies(currency) {
2020-02-25 15:16:44 +01:00
setCurrency(currency)
2020-02-25 04:10:06 +01:00
const pricesNew = Array.from(prices)
global.ipcRenderer.send('currency-updated', pricesNew, currency)
2019-05-20 03:35:19 +02:00
}
2020-02-25 04:10:06 +01:00
const context = {
isLoading,
accounts,
currency,
needsConfig,
accentColor,
2020-02-25 15:16:44 +01:00
error,
toggleCurrencies,
setBalances
2019-05-05 13:34:21 +02:00
}
2020-02-25 04:10:06 +01:00
return <AppContext.Provider value={context}>{children}</AppContext.Provider>
}
AppProvider.propTypes = {
children: PropTypes.any.isRequired
2019-05-05 13:34:21 +02:00
}