2018-01-27 12:20:02 -08:00
|
|
|
import { AppState } from 'reducers';
|
|
|
|
import {
|
|
|
|
CustomNetworkConfig,
|
2018-01-29 17:41:59 -08:00
|
|
|
StaticNetworkConfig,
|
2018-01-30 17:04:36 -08:00
|
|
|
StaticNetworkIds,
|
2018-01-27 12:20:02 -08:00
|
|
|
NetworkContract
|
2018-01-29 17:41:59 -08:00
|
|
|
} from 'types/network';
|
2018-02-01 19:49:37 -08:00
|
|
|
const getConfig = (state: AppState) => state.config;
|
2018-01-27 12:20:02 -08:00
|
|
|
|
|
|
|
export const getNetworks = (state: AppState) => getConfig(state).networks;
|
|
|
|
|
2018-01-29 19:52:19 -08:00
|
|
|
export const getNetworkConfigById = (state: AppState, networkId: string) =>
|
2018-01-30 17:04:36 -08:00
|
|
|
isStaticNetworkId(state, networkId)
|
2018-01-29 19:52:19 -08:00
|
|
|
? getStaticNetworkConfigs(state)[networkId]
|
|
|
|
: getCustomNetworkConfigs(state)[networkId];
|
|
|
|
|
2018-01-30 17:04:36 -08:00
|
|
|
export const getStaticNetworkIds = (state: AppState): StaticNetworkIds[] =>
|
|
|
|
Object.keys(getNetworks(state).staticNetworks) as StaticNetworkIds[];
|
2018-01-29 17:41:59 -08:00
|
|
|
|
2018-01-30 17:04:36 -08:00
|
|
|
export const isStaticNetworkId = (
|
2018-01-29 19:52:19 -08:00
|
|
|
state: AppState,
|
2018-01-30 17:04:36 -08:00
|
|
|
networkId: string
|
|
|
|
): networkId is StaticNetworkIds => Object.keys(getStaticNetworkConfigs(state)).includes(networkId);
|
2018-01-29 19:52:19 -08:00
|
|
|
|
2018-01-29 17:41:59 -08:00
|
|
|
export const getStaticNetworkConfig = (state: AppState): StaticNetworkConfig | undefined => {
|
|
|
|
const { staticNetworks, selectedNetwork } = getNetworks(state);
|
2018-01-29 19:52:19 -08:00
|
|
|
|
2018-01-30 17:04:36 -08:00
|
|
|
const defaultNetwork = isStaticNetworkId(state, selectedNetwork)
|
2018-01-29 17:41:59 -08:00
|
|
|
? staticNetworks[selectedNetwork]
|
2018-01-27 12:20:02 -08:00
|
|
|
: undefined;
|
|
|
|
return defaultNetwork;
|
|
|
|
};
|
|
|
|
|
2018-02-02 14:21:41 -08:00
|
|
|
export const getSelectedNetwork = (state: AppState) => getNetworks(state).selectedNetwork;
|
|
|
|
|
2018-01-29 17:41:59 -08:00
|
|
|
export const getCustomNetworkConfig = (state: AppState): CustomNetworkConfig | undefined => {
|
2018-01-27 12:20:02 -08:00
|
|
|
const { customNetworks, selectedNetwork } = getNetworks(state);
|
|
|
|
const customNetwork = customNetworks[selectedNetwork];
|
|
|
|
return customNetwork;
|
|
|
|
};
|
|
|
|
|
2018-01-29 17:41:59 -08:00
|
|
|
export const getNetworkConfig = (state: AppState): StaticNetworkConfig | CustomNetworkConfig => {
|
|
|
|
const config = getStaticNetworkConfig(state) || getCustomNetworkConfig(state);
|
|
|
|
|
|
|
|
if (!config) {
|
|
|
|
const { selectedNetwork } = getNetworks(state);
|
|
|
|
throw Error(
|
|
|
|
`No network config found for ${selectedNetwork} in either static or custom networks`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return config;
|
|
|
|
};
|
2018-01-27 12:20:02 -08:00
|
|
|
|
|
|
|
export const getNetworkContracts = (state: AppState): NetworkContract[] | null => {
|
2018-01-29 17:41:59 -08:00
|
|
|
const network = getStaticNetworkConfig(state);
|
2018-01-27 12:20:02 -08:00
|
|
|
return network ? network.contracts : [];
|
|
|
|
};
|
|
|
|
|
2018-01-29 17:41:59 -08:00
|
|
|
export const getCustomNetworkConfigs = (state: AppState) => getNetworks(state).customNetworks;
|
|
|
|
|
|
|
|
export const getStaticNetworkConfigs = (state: AppState) => getNetworks(state).staticNetworks;
|