From e45989a08288dbc9a50c7d758d936559a15ee4e1 Mon Sep 17 00:00:00 2001 From: HenryNguyen5 Date: Thu, 8 Feb 2018 21:00:02 -0500 Subject: [PATCH] Infer network name from chainid from selecting state --- common/libs/values.ts | 17 -------------- common/libs/wallet/non-deterministic/web3.ts | 5 ++-- common/sagas/config/web3.ts | 7 +++--- common/selectors/config/networks.ts | 23 +++++++++++++++---- .../reducers/config/nodes/staticNodes.spec.ts | 3 +-- 5 files changed, 26 insertions(+), 29 deletions(-) diff --git a/common/libs/values.ts b/common/libs/values.ts index 34123527..3d386843 100644 --- a/common/libs/values.ts +++ b/common/libs/values.ts @@ -24,23 +24,6 @@ export function sanitizeHex(hex: string) { return hex !== '' ? `0x${padLeftEven(hexStr)}` : ''; } -export function networkIdToName( - networkId: 1 | 3 | 4 | 42 | '1' | '3' | '4' | '42' -): StaticNetworkIds { - switch (networkId.toString()) { - case '1': - return 'ETH'; - case '3': - return 'Ropsten'; - case '4': - return 'Rinkeby'; - case '42': - return 'Kovan'; - default: - throw new Error(`Network ${networkId} is unsupported.`); - } -} - export const buildEIP681EtherRequest = ( recipientAddr: string, chainId: number, diff --git a/common/libs/wallet/non-deterministic/web3.ts b/common/libs/wallet/non-deterministic/web3.ts index ec4f9918..60929823 100644 --- a/common/libs/wallet/non-deterministic/web3.ts +++ b/common/libs/wallet/non-deterministic/web3.ts @@ -1,9 +1,8 @@ import { getTransactionFields, makeTransaction } from 'libs/transaction'; import { IFullWallet } from '../IWallet'; -import { networkIdToName } from 'libs/values'; import { bufferToHex } from 'ethereumjs-util'; import { configuredStore } from 'store'; -import { getNodeLib } from 'selectors/config'; +import { getNodeLib, getNetworkNameByChainId } from 'selectors/config'; import Web3Node, { isWeb3Node } from 'libs/nodes/web3'; import { INode } from 'libs/nodes/INode'; @@ -70,7 +69,7 @@ export default class Web3Wallet implements IFullWallet { private async networkCheck(lib: Web3Node) { const netId = await lib.getNetVersion(); - const netName = networkIdToName(netId as any); + const netName = getNetworkNameByChainId(configuredStore.getState(), netId); if (this.network !== netName) { throw new Error( `Expected MetaMask / Mist network to be ${ diff --git a/common/sagas/config/web3.ts b/common/sagas/config/web3.ts index 39c8351e..7853c412 100644 --- a/common/sagas/config/web3.ts +++ b/common/sagas/config/web3.ts @@ -1,18 +1,19 @@ -import { networkIdToName } from 'libs/values'; import { TypeKeys as WalletTypeKeys } from 'actions/wallet/constants'; import { Web3Wallet } from 'libs/wallet'; import { SagaIterator } from 'redux-saga'; import { select, put, takeEvery, call } from 'redux-saga/effects'; import { changeNodeIntent, TypeKeys, web3SetNode } from 'actions/config'; -import { getNodeId, getStaticAltNodeIdToWeb3 } from 'selectors/config'; +import { getNodeId, getStaticAltNodeIdToWeb3, getNetworkNameByChainId } from 'selectors/config'; import { setupWeb3Node, Web3Service } from 'libs/nodes/web3'; import { Web3NodeConfig } from 'types/node'; export function* initWeb3Node(): SagaIterator { const { networkId, lib } = yield call(setupWeb3Node); + const network = yield select(getNetworkNameByChainId, networkId); + const config: Web3NodeConfig = { isCustom: false, - network: networkIdToName(networkId), + network, service: Web3Service, lib, estimateGas: false, diff --git a/common/selectors/config/networks.ts b/common/selectors/config/networks.ts index f57081c2..d9d355f6 100644 --- a/common/selectors/config/networks.ts +++ b/common/selectors/config/networks.ts @@ -5,6 +5,7 @@ import { StaticNetworkIds, NetworkContract } from 'types/network'; +import { getNodeConfig } from 'selectors/config'; const getConfig = (state: AppState) => state.config; export const getNetworks = (state: AppState) => getConfig(state).networks; @@ -14,6 +15,16 @@ export const getNetworkConfigById = (state: AppState, networkId: string) => ? getStaticNetworkConfigs(state)[networkId] : getCustomNetworkConfigs(state)[networkId]; +export const getNetworkNameByChainId = (state: AppState, chainId: number | string) => { + const network = + Object.values(getStaticNetworkConfigs(state)).find(n => +n.chainId === +chainId) || + Object.values(getCustomNetworkConfigs(state)).find(n => +n.chainId === +chainId); + if (!network) { + return null; + } + return network.name; +}; + export const getStaticNetworkIds = (state: AppState): StaticNetworkIds[] => Object.keys(getNetworks(state).staticNetworks) as StaticNetworkIds[]; @@ -23,7 +34,9 @@ export const isStaticNetworkId = ( ): networkId is StaticNetworkIds => Object.keys(getStaticNetworkConfigs(state)).includes(networkId); export const getStaticNetworkConfig = (state: AppState): StaticNetworkConfig | undefined => { - const { staticNetworks, selectedNetwork } = getNetworks(state); + const selectedNetwork = getSelectedNetwork(state); + + const { staticNetworks } = getNetworks(state); const defaultNetwork = isStaticNetworkId(state, selectedNetwork) ? staticNetworks[selectedNetwork] @@ -31,10 +44,11 @@ export const getStaticNetworkConfig = (state: AppState): StaticNetworkConfig | u return defaultNetwork; }; -export const getSelectedNetwork = (state: AppState) => getNetworks(state).selectedNetwork; +export const getSelectedNetwork = (state: AppState) => getNodeConfig(state).network; export const getCustomNetworkConfig = (state: AppState): CustomNetworkConfig | undefined => { - const { customNetworks, selectedNetwork } = getNetworks(state); + const selectedNetwork = getSelectedNetwork(state); + const { customNetworks } = getNetworks(state); const customNetwork = customNetworks[selectedNetwork]; return customNetwork; }; @@ -43,7 +57,8 @@ export const getNetworkConfig = (state: AppState): StaticNetworkConfig | CustomN const config = getStaticNetworkConfig(state) || getCustomNetworkConfig(state); if (!config) { - const { selectedNetwork } = getNetworks(state); + const selectedNetwork = getSelectedNetwork(state); + throw Error( `No network config found for ${selectedNetwork} in either static or custom networks` ); diff --git a/spec/reducers/config/nodes/staticNodes.spec.ts b/spec/reducers/config/nodes/staticNodes.spec.ts index 3bc81413..74dfdaed 100644 --- a/spec/reducers/config/nodes/staticNodes.spec.ts +++ b/spec/reducers/config/nodes/staticNodes.spec.ts @@ -93,10 +93,9 @@ const expectedInitialState = { }; const web3Id = 'web3'; -const web3NetworkId = 1; const web3Node: Web3NodeConfig = { isCustom: false, - network: networkIdToName(web3NetworkId), + network: 'ETH', service: Web3Service, lib: jest.fn() as any, estimateGas: false,