mirror of
https://github.com/kremalicious/blowfish.git
synced 2024-11-22 09:47:00 +01:00
refactor
This commit is contained in:
parent
33532c33d3
commit
46ab69905d
@ -2,20 +2,9 @@ import React, { useContext, useState, useEffect } from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
import ms from 'ms'
|
||||
// import { ipcRenderer } from 'electron'
|
||||
import Store from 'electron-store'
|
||||
import unit from 'ethjs-unit'
|
||||
import { AppContext, PriceContext } from './createContext'
|
||||
import { fetchData } from '../../utils'
|
||||
import { refreshInterval, conversions, oceanTokenContract } from '../../config'
|
||||
|
||||
async function getBalance(account) {
|
||||
const json = await fetchData(
|
||||
`https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress=${oceanTokenContract}&address=${account}&tag=latest&apikey=${process.env.ETHERSCAN_API_KEY}`
|
||||
)
|
||||
|
||||
const balance = unit.fromWei(`${json.result}`, 'ether')
|
||||
return balance
|
||||
}
|
||||
import { refreshInterval, conversions } from '../../config'
|
||||
import { getAccounts, getBalance } from './helpers'
|
||||
|
||||
export default function AppProvider({ children }) {
|
||||
const { prices } = useContext(PriceContext)
|
||||
@ -59,24 +48,10 @@ export default function AppProvider({ children }) {
|
||||
}
|
||||
}, [prices])
|
||||
|
||||
function getAccounts() {
|
||||
let accountsPref
|
||||
const store = process.env.NODE_ENV === 'test' ? new Store() : global.store
|
||||
|
||||
if (store.has('accounts')) {
|
||||
accountsPref = store.get('accounts')
|
||||
!accountsPref.length ? setNeedsConfig(true) : setNeedsConfig(false)
|
||||
} else {
|
||||
accountsPref = []
|
||||
setNeedsConfig(true)
|
||||
}
|
||||
|
||||
return accountsPref
|
||||
}
|
||||
|
||||
async function setBalances() {
|
||||
let newAccounts = []
|
||||
const accountsPref = await getAccounts()
|
||||
const { needsConfig, accountsPref } = await getAccounts()
|
||||
setNeedsConfig(needsConfig)
|
||||
|
||||
for (const account of accountsPref) {
|
||||
const oceanBalance = await getBalance(account)
|
||||
|
@ -2,8 +2,8 @@ import React, { useEffect, useState } from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
import ms from 'ms'
|
||||
import { PriceContext } from './createContext'
|
||||
import { fetchData } from '../../utils'
|
||||
import { refreshInterval, conversions } from '../../config'
|
||||
import { fetchAndSetPrices } from './helpers'
|
||||
|
||||
export default function PriceProvider({ children }) {
|
||||
// construct initial prices Map to get consistent
|
||||
@ -21,28 +21,10 @@ export default function PriceProvider({ children }) {
|
||||
)
|
||||
)
|
||||
|
||||
async function fetchAndSetPrices() {
|
||||
const currencies = conversions.join(',')
|
||||
const json = await fetchData(
|
||||
`https://api.coingecko.com/api/v3/simple/price?ids=ocean-protocol&vs_currencies=${currencies}&include_24hr_change=true`
|
||||
)
|
||||
|
||||
let newPrices = new Map(prices) // make a shallow copy of the Map
|
||||
conversions.map(key => newPrices.set(key, json['ocean-protocol'][key])) // modify the copy
|
||||
|
||||
const newPriceChanges = await Object.assign(
|
||||
...conversions.map(key => ({
|
||||
[key]: json['ocean-protocol'][key + '_24h_change']
|
||||
}))
|
||||
)
|
||||
|
||||
return { newPrices, newPriceChanges }
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
async function init() {
|
||||
try {
|
||||
const { newPrices, newPriceChanges } = await fetchAndSetPrices()
|
||||
const { newPrices, newPriceChanges } = await fetchAndSetPrices(prices)
|
||||
setPrices(newPrices)
|
||||
setPriceChanges(newPriceChanges)
|
||||
global.ipcRenderer.send('prices-updated', Array.from(newPrices)) // convert Map to array, ipc messages seem to kill it
|
||||
|
47
src/renderer/store/helpers.js
Normal file
47
src/renderer/store/helpers.js
Normal file
@ -0,0 +1,47 @@
|
||||
import Store from 'electron-store'
|
||||
import unit from 'ethjs-unit'
|
||||
import { fetchData } from '../../utils'
|
||||
import { oceanTokenContract, conversions } from '../../config'
|
||||
|
||||
export async function fetchAndSetPrices(prices) {
|
||||
const currencies = conversions.join(',')
|
||||
const json = await fetchData(
|
||||
`https://api.coingecko.com/api/v3/simple/price?ids=ocean-protocol&vs_currencies=${currencies}&include_24hr_change=true`
|
||||
)
|
||||
|
||||
let newPrices = new Map(prices) // make a shallow copy of the Map
|
||||
conversions.map(key => newPrices.set(key, json['ocean-protocol'][key])) // modify the copy
|
||||
|
||||
const newPriceChanges = await Object.assign(
|
||||
...conversions.map(key => ({
|
||||
[key]: json['ocean-protocol'][key + '_24h_change']
|
||||
}))
|
||||
)
|
||||
|
||||
return { newPrices, newPriceChanges }
|
||||
}
|
||||
|
||||
export async function getBalance(account) {
|
||||
const json = await fetchData(
|
||||
`https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress=${oceanTokenContract}&address=${account}&tag=latest&apikey=${process.env.ETHERSCAN_API_KEY}`
|
||||
)
|
||||
|
||||
const balance = unit.fromWei(`${json.result}`, 'ether')
|
||||
return balance
|
||||
}
|
||||
|
||||
export async function getAccounts() {
|
||||
let needsConfig
|
||||
let accountsPref
|
||||
const store = process.env.NODE_ENV === 'test' ? new Store() : global.store
|
||||
|
||||
if (store.has('accounts')) {
|
||||
accountsPref = store.get('accounts')
|
||||
needsConfig = !accountsPref.length
|
||||
} else {
|
||||
accountsPref = []
|
||||
needsConfig = true
|
||||
}
|
||||
|
||||
return { needsConfig, accountsPref }
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
module.exports = {
|
||||
rootDir: '../',
|
||||
transform: {
|
||||
'^.+\\.jsx?$': 'babel-jest'
|
||||
'^.+\\.[t|j]sx?$': 'babel-jest'
|
||||
},
|
||||
moduleNameMapper: {
|
||||
'.+\\.(css|styl|less|sass|scss)$': 'identity-obj-proxy',
|
||||
|
Loading…
Reference in New Issue
Block a user