Infer network name from chainid from selecting state

This commit is contained in:
HenryNguyen5 2018-02-08 21:00:02 -05:00
parent c5d63a31af
commit e45989a082
5 changed files with 26 additions and 29 deletions

View File

@ -24,23 +24,6 @@ export function sanitizeHex(hex: string) {
return hex !== '' ? `0x${padLeftEven(hexStr)}` : ''; 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 = ( export const buildEIP681EtherRequest = (
recipientAddr: string, recipientAddr: string,
chainId: number, chainId: number,

View File

@ -1,9 +1,8 @@
import { getTransactionFields, makeTransaction } from 'libs/transaction'; import { getTransactionFields, makeTransaction } from 'libs/transaction';
import { IFullWallet } from '../IWallet'; import { IFullWallet } from '../IWallet';
import { networkIdToName } from 'libs/values';
import { bufferToHex } from 'ethereumjs-util'; import { bufferToHex } from 'ethereumjs-util';
import { configuredStore } from 'store'; import { configuredStore } from 'store';
import { getNodeLib } from 'selectors/config'; import { getNodeLib, getNetworkNameByChainId } from 'selectors/config';
import Web3Node, { isWeb3Node } from 'libs/nodes/web3'; import Web3Node, { isWeb3Node } from 'libs/nodes/web3';
import { INode } from 'libs/nodes/INode'; import { INode } from 'libs/nodes/INode';
@ -70,7 +69,7 @@ export default class Web3Wallet implements IFullWallet {
private async networkCheck(lib: Web3Node) { private async networkCheck(lib: Web3Node) {
const netId = await lib.getNetVersion(); const netId = await lib.getNetVersion();
const netName = networkIdToName(netId as any); const netName = getNetworkNameByChainId(configuredStore.getState(), netId);
if (this.network !== netName) { if (this.network !== netName) {
throw new Error( throw new Error(
`Expected MetaMask / Mist network to be ${ `Expected MetaMask / Mist network to be ${

View File

@ -1,18 +1,19 @@
import { networkIdToName } from 'libs/values';
import { TypeKeys as WalletTypeKeys } from 'actions/wallet/constants'; import { TypeKeys as WalletTypeKeys } from 'actions/wallet/constants';
import { Web3Wallet } from 'libs/wallet'; import { Web3Wallet } from 'libs/wallet';
import { SagaIterator } from 'redux-saga'; import { SagaIterator } from 'redux-saga';
import { select, put, takeEvery, call } from 'redux-saga/effects'; import { select, put, takeEvery, call } from 'redux-saga/effects';
import { changeNodeIntent, TypeKeys, web3SetNode } from 'actions/config'; 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 { setupWeb3Node, Web3Service } from 'libs/nodes/web3';
import { Web3NodeConfig } from 'types/node'; import { Web3NodeConfig } from 'types/node';
export function* initWeb3Node(): SagaIterator { export function* initWeb3Node(): SagaIterator {
const { networkId, lib } = yield call(setupWeb3Node); const { networkId, lib } = yield call(setupWeb3Node);
const network = yield select(getNetworkNameByChainId, networkId);
const config: Web3NodeConfig = { const config: Web3NodeConfig = {
isCustom: false, isCustom: false,
network: networkIdToName(networkId), network,
service: Web3Service, service: Web3Service,
lib, lib,
estimateGas: false, estimateGas: false,

View File

@ -5,6 +5,7 @@ import {
StaticNetworkIds, StaticNetworkIds,
NetworkContract NetworkContract
} from 'types/network'; } from 'types/network';
import { getNodeConfig } from 'selectors/config';
const getConfig = (state: AppState) => state.config; const getConfig = (state: AppState) => state.config;
export const getNetworks = (state: AppState) => getConfig(state).networks; export const getNetworks = (state: AppState) => getConfig(state).networks;
@ -14,6 +15,16 @@ export const getNetworkConfigById = (state: AppState, networkId: string) =>
? getStaticNetworkConfigs(state)[networkId] ? getStaticNetworkConfigs(state)[networkId]
: getCustomNetworkConfigs(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[] => export const getStaticNetworkIds = (state: AppState): StaticNetworkIds[] =>
Object.keys(getNetworks(state).staticNetworks) as StaticNetworkIds[]; Object.keys(getNetworks(state).staticNetworks) as StaticNetworkIds[];
@ -23,7 +34,9 @@ export const isStaticNetworkId = (
): networkId is StaticNetworkIds => Object.keys(getStaticNetworkConfigs(state)).includes(networkId); ): networkId is StaticNetworkIds => Object.keys(getStaticNetworkConfigs(state)).includes(networkId);
export const getStaticNetworkConfig = (state: AppState): StaticNetworkConfig | undefined => { 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) const defaultNetwork = isStaticNetworkId(state, selectedNetwork)
? staticNetworks[selectedNetwork] ? staticNetworks[selectedNetwork]
@ -31,10 +44,11 @@ export const getStaticNetworkConfig = (state: AppState): StaticNetworkConfig | u
return defaultNetwork; 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 => { export const getCustomNetworkConfig = (state: AppState): CustomNetworkConfig | undefined => {
const { customNetworks, selectedNetwork } = getNetworks(state); const selectedNetwork = getSelectedNetwork(state);
const { customNetworks } = getNetworks(state);
const customNetwork = customNetworks[selectedNetwork]; const customNetwork = customNetworks[selectedNetwork];
return customNetwork; return customNetwork;
}; };
@ -43,7 +57,8 @@ export const getNetworkConfig = (state: AppState): StaticNetworkConfig | CustomN
const config = getStaticNetworkConfig(state) || getCustomNetworkConfig(state); const config = getStaticNetworkConfig(state) || getCustomNetworkConfig(state);
if (!config) { if (!config) {
const { selectedNetwork } = getNetworks(state); const selectedNetwork = getSelectedNetwork(state);
throw Error( throw Error(
`No network config found for ${selectedNetwork} in either static or custom networks` `No network config found for ${selectedNetwork} in either static or custom networks`
); );

View File

@ -93,10 +93,9 @@ const expectedInitialState = {
}; };
const web3Id = 'web3'; const web3Id = 'web3';
const web3NetworkId = 1;
const web3Node: Web3NodeConfig = { const web3Node: Web3NodeConfig = {
isCustom: false, isCustom: false,
network: networkIdToName(web3NetworkId), network: 'ETH',
service: Web3Service, service: Web3Service,
lib: jest.fn() as any, lib: jest.fn() as any,
estimateGas: false, estimateGas: false,