From f9992727697abddfaa58b4f07d4458bf52ec0e63 Mon Sep 17 00:00:00 2001 From: HenryNguyen5 Date: Fri, 2 Feb 2018 17:34:38 -0500 Subject: [PATCH] Get web3 working + cleanup sagas --- .../Header/components/CustomNodeModal.tsx | 23 ++++++----- common/sagas/config/index.ts | 9 ++++ common/sagas/config/network.ts | 23 +++++++++++ common/sagas/{config.ts => config/node.ts} | 41 +++++-------------- common/sagas/{ => config}/web3.ts | 3 +- common/sagas/wallet/wallet.ts | 4 +- common/utils/network.ts | 5 --- 7 files changed, 58 insertions(+), 50 deletions(-) create mode 100644 common/sagas/config/index.ts create mode 100644 common/sagas/config/network.ts rename common/sagas/{config.ts => config/node.ts} (83%) rename common/sagas/{ => config}/web3.ts (94%) delete mode 100644 common/utils/network.ts diff --git a/common/components/Header/components/CustomNodeModal.tsx b/common/components/Header/components/CustomNodeModal.tsx index dc46b9c8..74a51b8f 100644 --- a/common/components/Header/components/CustomNodeModal.tsx +++ b/common/components/Header/components/CustomNodeModal.tsx @@ -2,7 +2,6 @@ import React from 'react'; import classnames from 'classnames'; import Modal, { IButton } from 'components/ui/Modal'; import translate from 'translations'; -import { makeCustomNetworkId } from 'utils/network'; import { CustomNetworkConfig } from 'types/network'; import { CustomNodeConfig } from 'types/node'; import { TAddCustomNetwork, addCustomNetwork, AddCustomNodeAction } from 'actions/config'; @@ -131,14 +130,11 @@ class CustomNodeModal extends React.Component { {net} ))} - {Object.values(customNetworks).map(net => { - const id = makeCustomNetworkId(net); - return ( - - ); - })} + {Object.entries(customNetworks).map(([id, net]) => ( + + ))} @@ -336,8 +332,11 @@ class CustomNodeModal extends React.Component { private makeCustomNodeConfigFromState(): CustomNodeConfig { const { network } = this.state; + const networkId = - network === CUSTOM ? makeCustomNetworkId(this.makeCustomNetworkConfigFromState()) : network; + network === CUSTOM + ? this.makeCustomNetworkId(this.makeCustomNetworkConfigFromState()) + : network; const port = parseInt(this.state.port, 10); const url = this.state.url.trim(); @@ -392,6 +391,10 @@ class CustomNodeModal extends React.Component { this.props.addCustomNode({ config: node, id: node.id }); }; + + private makeCustomNetworkId(config: CustomNetworkConfig): string { + return config.chainId ? `${config.chainId}` : `${config.name}:${config.unit}`; + } } const mapStateToProps = (state: AppState): StateProps => ({ diff --git a/common/sagas/config/index.ts b/common/sagas/config/index.ts new file mode 100644 index 00000000..2f9a93d5 --- /dev/null +++ b/common/sagas/config/index.ts @@ -0,0 +1,9 @@ +import { network } from './network'; +import { node } from './node'; +import { web3 } from './web3'; +import { all } from 'redux-saga/effects'; +import { SagaIterator } from 'redux-saga'; + +export default function*(): SagaIterator { + yield all([...network, ...node, ...web3]); +} diff --git a/common/sagas/config/network.ts b/common/sagas/config/network.ts new file mode 100644 index 00000000..2f11a833 --- /dev/null +++ b/common/sagas/config/network.ts @@ -0,0 +1,23 @@ +import { select, takeEvery, put } from 'redux-saga/effects'; +import { getCustomNodeConfigs, getCustomNetworkConfigs } from 'selectors/config'; +import { removeCustomNetwork, TypeKeys } from 'actions/config'; +import { SagaIterator } from 'redux-saga'; +import { AppState } from 'reducers'; + +// If there are any orphaned custom networks, purge them +export function* cleanCustomNetworks(): SagaIterator { + const customNodes: AppState['config']['nodes']['customNodes'] = yield select( + getCustomNodeConfigs + ); + const customNetworks: AppState['config']['networks']['customNetworks'] = yield select( + getCustomNetworkConfigs + ); + + Object.values(customNodes).forEach(function*(n) { + if (!customNetworks[n.network]) { + yield put(removeCustomNetwork({ id: n.network })); + } + }); +} + +export const network = [takeEvery(TypeKeys.CONFIG_REMOVE_CUSTOM_NODE, cleanCustomNetworks)]; diff --git a/common/sagas/config.ts b/common/sagas/config/node.ts similarity index 83% rename from common/sagas/config.ts rename to common/sagas/config/node.ts index cc7f3c5c..1976e3ae 100644 --- a/common/sagas/config.ts +++ b/common/sagas/config/node.ts @@ -5,18 +5,15 @@ import { fork, put, take, - takeLatest, takeEvery, select, race, - apply + apply, + takeLatest } from 'redux-saga/effects'; -import { makeCustomNetworkId } from 'utils/network'; import { getNodeId, getNodeConfig, - getCustomNodeConfigs, - getCustomNetworkConfigs, getOffline, isStaticNodeId, getCustomNodeFromId, @@ -29,7 +26,6 @@ import { changeNode, changeNodeIntent, setLatestBlock, - removeCustomNetwork, AddCustomNodeAction, ChangeNodeIntentAction } from 'actions/config'; @@ -42,7 +38,7 @@ import { Web3Service } from 'libs/nodes/web3'; let hasCheckedOnline = false; export function* pollOfflineStatus(): SagaIterator { while (true) { - const node: StaticNodeConfig = yield select(getNodeConfig); + const nodeConfig: StaticNodeConfig = yield select(getNodeConfig); const isOffline: boolean = yield select(getOffline); // If our offline state disagrees with the browser, run a check @@ -50,7 +46,7 @@ export function* pollOfflineStatus(): SagaIterator { const shouldPing = !hasCheckedOnline || navigator.onLine === isOffline; if (shouldPing && !document.hidden) { const { pingSucceeded } = yield race({ - pingSucceeded: call(node.lib.ping.bind(node.lib)), + pingSucceeded: call(nodeConfig.lib.ping.bind(nodeConfig.lib)), timeout: call(delay, 5000) }); @@ -190,26 +186,9 @@ export function* switchToNewNode(action: AddCustomNodeAction): SagaIterator { yield put(changeNodeIntent(action.payload.id)); } -// If there are any orphaned custom networks, purge them -export function* cleanCustomNetworks(): SagaIterator { - const customNodes = yield select(getCustomNodeConfigs); - const customNetworks = yield select(getCustomNetworkConfigs); - const networksInUse = customNodes.reduce((prev, conf) => { - prev[conf.network] = true; - return prev; - }, {}); - - for (const net of customNetworks) { - if (!networksInUse[makeCustomNetworkId(net)]) { - yield put(removeCustomNetwork(net)); - } - } -} - -export default function* configSaga(): SagaIterator { - yield takeLatest(TypeKeys.CONFIG_POLL_OFFLINE_STATUS, handlePollOfflineStatus); - yield takeEvery(TypeKeys.CONFIG_NODE_CHANGE_INTENT, handleNodeChangeIntent); - yield takeEvery(TypeKeys.CONFIG_LANGUAGE_CHANGE, reload); - yield takeEvery(TypeKeys.CONFIG_ADD_CUSTOM_NODE, switchToNewNode); - yield takeEvery(TypeKeys.CONFIG_REMOVE_CUSTOM_NODE, cleanCustomNetworks); -} +export const node = [ + takeEvery(TypeKeys.CONFIG_NODE_CHANGE_INTENT, handleNodeChangeIntent), + takeLatest(TypeKeys.CONFIG_POLL_OFFLINE_STATUS, handlePollOfflineStatus), + takeEvery(TypeKeys.CONFIG_LANGUAGE_CHANGE, reload), + takeEvery(TypeKeys.CONFIG_ADD_CUSTOM_NODE, switchToNewNode) +]; diff --git a/common/sagas/web3.ts b/common/sagas/config/web3.ts similarity index 94% rename from common/sagas/web3.ts rename to common/sagas/config/web3.ts index 47ee6de2..21d7590d 100644 --- a/common/sagas/web3.ts +++ b/common/sagas/config/web3.ts @@ -6,7 +6,7 @@ import { select, put, takeEvery, call } from 'redux-saga/effects'; import { changeNodeIntent, TypeKeys, web3SetNode } from 'actions/config'; import { getNodeId, getStaticAltNodeToWeb3 } from 'selectors/config'; import { setupWeb3Node, Web3Service } from 'libs/nodes/web3'; -import { Web3NodeConfig } from '../../shared/types/node'; +import { Web3NodeConfig } from 'types/node'; export function* initWeb3Node(): SagaIterator { const { networkId, lib } = yield call(setupWeb3Node); @@ -50,7 +50,6 @@ export function* unsetWeb3Node(): SagaIterator { } export const web3 = [ - takeEvery(TypeKeys.CONFIG_NODE_WEB3_SET, initWeb3Node), takeEvery(TypeKeys.CONFIG_NODE_WEB3_UNSET, unsetWeb3Node), takeEvery(WalletTypeKeys.WALLET_SET, unsetWeb3NodeOnWalletEvent), takeEvery(WalletTypeKeys.WALLET_RESET, unsetWeb3NodeOnWalletEvent) diff --git a/common/sagas/wallet/wallet.ts b/common/sagas/wallet/wallet.ts index c86daf84..5f86ba41 100644 --- a/common/sagas/wallet/wallet.ts +++ b/common/sagas/wallet/wallet.ts @@ -52,7 +52,7 @@ import { loadWalletConfig, saveWalletConfig } from 'utils/localStorage'; import { getTokenBalances, filterScannedTokenBalances } from './helpers'; import { Token } from 'types/network'; import { Web3NodeConfig } from '../../../shared/types/node'; -import { initWeb3Node } from 'sagas/web3'; +import { initWeb3Node } from 'sagas/config/web3'; export interface TokenBalanceLookup { [symbol: string]: TokenBalance; @@ -264,7 +264,7 @@ export function* unlockWeb3(): SagaIterator { yield put(changeNodeIntent('web3')); yield take( action => - action.type === ConfigTypeKeys.CONFIG_NODE_CHANGE && action.payload.nodeSelection === 'web3' + action.type === ConfigTypeKeys.CONFIG_NODE_CHANGE && action.payload.nodeId === 'web3' ); const web3Node: Web3NodeConfig | null = yield select(getWeb3Node); diff --git a/common/utils/network.ts b/common/utils/network.ts deleted file mode 100644 index f38ffb6d..00000000 --- a/common/utils/network.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { CustomNetworkConfig } from 'types/network'; - -export function makeCustomNetworkId(config: CustomNetworkConfig): string { - return config.chainId ? `${config.chainId}` : `${config.name}:${config.unit}`; -}