MyCrypto/common/selectors/config/networks.ts

62 lines
2.2 KiB
TypeScript
Raw Normal View History

2018-01-27 12:20:02 -08:00
import { AppState } from 'reducers';
import {
CustomNetworkConfig,
2018-01-29 17:41:59 -08:00
StaticNetworkConfig,
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;
export const getNetworkConfigById = (state: AppState, networkId: string) =>
isStaticNetworkId(state, networkId)
? getStaticNetworkConfigs(state)[networkId]
: getCustomNetworkConfigs(state)[networkId];
export const getStaticNetworkIds = (state: AppState): StaticNetworkIds[] =>
Object.keys(getNetworks(state).staticNetworks) as StaticNetworkIds[];
2018-01-29 17:41:59 -08:00
export const isStaticNetworkId = (
state: AppState,
networkId: string
): networkId is StaticNetworkIds => Object.keys(getStaticNetworkConfigs(state)).includes(networkId);
2018-01-29 17:41:59 -08:00
export const getStaticNetworkConfig = (state: AppState): StaticNetworkConfig | undefined => {
const { staticNetworks, selectedNetwork } = getNetworks(state);
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;