2018-02-12 20:43:07 +00:00
|
|
|
import { AppState } from 'reducers';
|
|
|
|
import {
|
|
|
|
CustomNetworkConfig,
|
|
|
|
StaticNetworkConfig,
|
|
|
|
StaticNetworkIds,
|
|
|
|
NetworkContract
|
|
|
|
} from 'types/network';
|
|
|
|
import { getNodeConfig } from 'selectors/config';
|
2018-04-06 20:52:48 +00:00
|
|
|
import { stripWeb3Network } from 'libs/nodes';
|
2018-02-12 20:43:07 +00:00
|
|
|
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 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[];
|
|
|
|
|
|
|
|
export const isStaticNetworkId = (
|
|
|
|
state: AppState,
|
|
|
|
networkId: string
|
2018-04-06 20:52:48 +00:00
|
|
|
): networkId is StaticNetworkIds =>
|
|
|
|
Object.keys(getStaticNetworkConfigs(state)).includes(stripWeb3Network(networkId));
|
2018-02-12 20:43:07 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2018-04-06 20:52:48 +00:00
|
|
|
export const getSelectedNetwork = (state: AppState) =>
|
|
|
|
stripWeb3Network(getNodeConfig(state).network);
|
2018-02-12 20:43:07 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2018-03-02 01:24:14 +00:00
|
|
|
export const getNetworkUnit = (state: AppState): string => {
|
|
|
|
return getNetworkConfig(state).unit;
|
|
|
|
};
|
|
|
|
|
2018-02-12 20:43:07 +00:00
|
|
|
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;
|
2018-03-02 01:24:14 +00:00
|
|
|
|
|
|
|
export const isNetworkUnit = (state: AppState, unit: string) => {
|
|
|
|
return unit === getNetworkUnit(state);
|
|
|
|
};
|