From 46ab69905deb2bdbea0889df688f74ecbc28c123 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Tue, 25 Feb 2020 16:46:04 +0100 Subject: [PATCH] refactor --- src/renderer/store/AppProvider.jsx | 33 +++---------------- src/renderer/store/PriceProvider.jsx | 22 ++----------- src/renderer/store/helpers.js | 47 ++++++++++++++++++++++++++++ tests/jest.config.js | 2 +- 4 files changed, 54 insertions(+), 50 deletions(-) create mode 100644 src/renderer/store/helpers.js diff --git a/src/renderer/store/AppProvider.jsx b/src/renderer/store/AppProvider.jsx index 02ab36d..a611748 100644 --- a/src/renderer/store/AppProvider.jsx +++ b/src/renderer/store/AppProvider.jsx @@ -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) diff --git a/src/renderer/store/PriceProvider.jsx b/src/renderer/store/PriceProvider.jsx index 3e867ab..fc2ed0b 100644 --- a/src/renderer/store/PriceProvider.jsx +++ b/src/renderer/store/PriceProvider.jsx @@ -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 diff --git a/src/renderer/store/helpers.js b/src/renderer/store/helpers.js new file mode 100644 index 0000000..5071405 --- /dev/null +++ b/src/renderer/store/helpers.js @@ -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 } +} diff --git a/tests/jest.config.js b/tests/jest.config.js index 5867dcb..86325c2 100644 --- a/tests/jest.config.js +++ b/tests/jest.config.js @@ -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',