William O'Beirne a043334685 Node Refactor (#1603)
* Initial work on refactoring node definitions to reduce number of places theyre defined, amount of copy pasting.

* Use makeAutoNodeNAme instead of manually appending _auto

* Add getNetVersion to list of unsupported methods

* PR feedback

* Rework web template node selector to be a network selector. Refactor some types to help with that. Better handle removing custom nodes.

* Remove color dropdown.

* Fix selecting custom networks. Show notification if change network intent fails.

* Use selectors for current node / network instead of intuiting from nodeSelection

* Add id key to all networks, simplify add and remove custom node and network functions.

* Fix a lot of uses of network.name to use network.id instead.

* Dont allow network chainid conflicts

* Fix web3 network by chainid

* Add testnet badge to network selector

* Change nomenclature from change(Node|Network)(Intent)? to change(Node|Network)(Requested|Succeeded)

* tscheck

* Better code for chainid collision

* Remove console logs

* Fix tests

* Network selector becomes self contained component used both by web header and electron nav.

* Dont select node again

* Additional title text

* tscheck

* Custom node behavior in Electron

* Close panel too

* Convert node label data into selector function

* tscheck

* Parens & space
2018-05-29 09:51:42 -05:00

93 lines
3.1 KiB
TypeScript

import { AppState } from 'reducers';
import {
CustomNetworkConfig,
StaticNetworkConfig,
StaticNetworkIds,
NetworkContract
} from 'types/network';
import { getNodeConfig } from 'selectors/config';
import { stripWeb3Network } from 'libs/nodes';
const getConfig = (state: AppState) => state.config;
export const getNetworks = (state: AppState) => getConfig(state).networks;
export const getNetworkConfigById = (state: AppState, networkId: string) =>
isStaticNetworkId(state, networkId)
? getStaticNetworkConfigs(state)[networkId]
: getCustomNetworkConfigs(state)[networkId];
export const getNetworkByChainId = (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;
};
export const getStaticNetworkIds = (state: AppState): StaticNetworkIds[] =>
Object.keys(getNetworks(state).staticNetworks) as StaticNetworkIds[];
export const isStaticNetworkId = (
state: AppState,
networkId: string
): networkId is StaticNetworkIds =>
Object.keys(getStaticNetworkConfigs(state)).includes(stripWeb3Network(networkId));
export const getStaticNetworkConfig = (state: AppState): StaticNetworkConfig | undefined => {
const selectedNetwork = getSelectedNetwork(state);
const { staticNetworks } = getNetworks(state);
const defaultNetwork = isStaticNetworkId(state, selectedNetwork)
? staticNetworks[selectedNetwork]
: undefined;
return defaultNetwork;
};
export const getSelectedNetwork = (state: AppState) =>
stripWeb3Network(getNodeConfig(state).network);
export const getCustomNetworkConfig = (state: AppState): CustomNetworkConfig | undefined => {
const selectedNetwork = getSelectedNetwork(state);
const { customNetworks } = getNetworks(state);
const customNetwork = customNetworks[selectedNetwork];
return customNetwork;
};
export const getNetworkConfig = (state: AppState): StaticNetworkConfig | CustomNetworkConfig => {
const config = getStaticNetworkConfig(state) || getCustomNetworkConfig(state);
if (!config) {
const selectedNetwork = getSelectedNetwork(state);
throw Error(
`No network config found for ${selectedNetwork} in either static or custom networks`
);
}
return config;
};
export const getNetworkUnit = (state: AppState): string => {
return getNetworkConfig(state).unit;
};
export const getNetworkContracts = (state: AppState): NetworkContract[] | null => {
const network = getStaticNetworkConfig(state);
return network ? network.contracts : [];
};
export const getCustomNetworkConfigs = (state: AppState) => getNetworks(state).customNetworks;
export const getStaticNetworkConfigs = (state: AppState) => getNetworks(state).staticNetworks;
export const getAllNetworkConfigs = (state: AppState) => ({
...getStaticNetworkConfigs(state),
...getCustomNetworkConfigs(state)
});
export const isNetworkUnit = (state: AppState, unit: string) => {
return unit === getNetworkUnit(state);
};