mirror of
https://github.com/status-im/MyCrypto.git
synced 2025-02-05 15:53:26 +00:00
04eaa08d6c
* initial mvp * First functioning pass * Add token balance shim * Add working web3 implementation * Fix tests * Fix tsc errors * Implement token batch splitting * Undo logger change * Fix linting errors * Revert makeconfig change * Add typing to token proxy + use string interpolation * Remove useless parameter * Remove logging * Use type coercion to fix proxied methods * Update shepherd * Update to typescript 2.8.1 * Fix merged typings * Address PR comments * replace myc-shepherd with mycrypto-shepherd
88 lines
2.9 KiB
TypeScript
88 lines
2.9 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 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
|
|
): 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 isNetworkUnit = (state: AppState, unit: string) => {
|
|
return unit === getNetworkUnit(state);
|
|
};
|