2018-02-12 20:43:07 +00:00
|
|
|
import { AppState } from 'reducers';
|
|
|
|
import {
|
|
|
|
getStaticNetworkConfigs,
|
|
|
|
getCustomNetworkConfigs,
|
|
|
|
isStaticNetworkId
|
|
|
|
} from 'selectors/config';
|
2018-04-06 20:52:48 +00:00
|
|
|
import { CustomNodeConfig, StaticNodeConfig, StaticNodeId } from 'types/node';
|
|
|
|
import { StaticNetworkIds } from 'types/network';
|
2018-02-12 20:43:07 +00:00
|
|
|
const getConfig = (state: AppState) => state.config;
|
2018-04-06 20:52:48 +00:00
|
|
|
import { shepherdProvider, INode, stripWeb3Network } from 'libs/nodes';
|
2018-02-12 20:43:07 +00:00
|
|
|
|
|
|
|
export const getNodes = (state: AppState) => getConfig(state).nodes;
|
|
|
|
|
|
|
|
export function isNodeCustom(state: AppState, nodeId: string): CustomNodeConfig | undefined {
|
|
|
|
return getCustomNodeConfigs(state)[nodeId];
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getCustomNodeFromId = (
|
|
|
|
state: AppState,
|
|
|
|
nodeId: string
|
|
|
|
): CustomNodeConfig | undefined => getCustomNodeConfigs(state)[nodeId];
|
|
|
|
|
|
|
|
export const getStaticNodeFromId = (state: AppState, nodeId: StaticNodeId) =>
|
|
|
|
getStaticNodeConfigs(state)[nodeId];
|
|
|
|
|
2018-04-06 20:52:48 +00:00
|
|
|
export const isStaticNodeId = (state: AppState, nodeId: string): nodeId is StaticNodeId =>
|
2018-02-12 20:43:07 +00:00
|
|
|
Object.keys(getStaticNodeConfigs(state)).includes(nodeId);
|
|
|
|
|
|
|
|
const getStaticNodeConfigs = (state: AppState) => getNodes(state).staticNodes;
|
|
|
|
|
|
|
|
export const getStaticNodeConfig = (state: AppState) => {
|
|
|
|
const { staticNodes, selectedNode: { nodeId } } = getNodes(state);
|
|
|
|
|
|
|
|
const defaultNetwork = isStaticNodeId(state, nodeId) ? staticNodes[nodeId] : undefined;
|
|
|
|
return defaultNetwork;
|
|
|
|
};
|
|
|
|
|
2018-04-06 20:52:48 +00:00
|
|
|
export const getWeb3Node = (state: AppState): StaticNodeConfig | null => {
|
|
|
|
const isWeb3Node = (nodeId: string) => nodeId === 'web3';
|
2018-02-12 20:43:07 +00:00
|
|
|
const currNode = getStaticNodeConfig(state);
|
|
|
|
const currNodeId = getNodeId(state);
|
2018-04-06 20:52:48 +00:00
|
|
|
if (currNode && currNodeId && isWeb3Node(currNodeId)) {
|
2018-02-12 20:43:07 +00:00
|
|
|
return currNode;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getCustomNodeConfig = (state: AppState): CustomNodeConfig | undefined => {
|
|
|
|
const { customNodes, selectedNode: { nodeId } } = getNodes(state);
|
|
|
|
|
|
|
|
const customNode = customNodes[nodeId];
|
|
|
|
return customNode;
|
|
|
|
};
|
|
|
|
|
|
|
|
export function getCustomNodeConfigs(state: AppState) {
|
|
|
|
return getNodes(state).customNodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getStaticNodes(state: AppState) {
|
|
|
|
return getNodes(state).staticNodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getSelectedNode(state: AppState) {
|
|
|
|
return getNodes(state).selectedNode;
|
|
|
|
}
|
|
|
|
|
2018-04-09 17:23:37 +00:00
|
|
|
export function getPreviouslySelectedNode(state: AppState) {
|
|
|
|
return getSelectedNode(state).prevNode;
|
|
|
|
}
|
|
|
|
|
2018-03-07 23:36:05 +00:00
|
|
|
export function isNodeChanging(state: AppState): boolean {
|
2018-02-12 20:43:07 +00:00
|
|
|
return getSelectedNode(state).pending;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getNodeId(state: AppState): string {
|
|
|
|
return getSelectedNode(state).nodeId;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getIsWeb3Node(state: AppState): boolean {
|
|
|
|
return getNodeId(state) === 'web3';
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getNodeConfig(state: AppState): StaticNodeConfig | CustomNodeConfig {
|
|
|
|
const config = getStaticNodeConfig(state) || getCustomNodeConfig(state);
|
|
|
|
|
|
|
|
if (!config) {
|
|
|
|
const { selectedNode } = getNodes(state);
|
|
|
|
throw Error(`No node config found for ${selectedNode.nodeId} in either static or custom nodes`);
|
|
|
|
}
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2018-04-06 20:52:48 +00:00
|
|
|
export function getNodeLib(_: AppState): INode {
|
|
|
|
return shepherdProvider;
|
2018-02-12 20:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface NodeOption {
|
|
|
|
isCustom: false;
|
|
|
|
value: string;
|
2018-02-24 18:02:07 +00:00
|
|
|
label: { network: string; service: string };
|
2018-02-12 20:43:07 +00:00
|
|
|
color?: string;
|
|
|
|
hidden?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getStaticNodeOptions(state: AppState): NodeOption[] {
|
|
|
|
const staticNetworkConfigs = getStaticNetworkConfigs(state);
|
|
|
|
return Object.entries(getStaticNodes(state)).map(([nodeId, node]: [string, StaticNodeConfig]) => {
|
2018-04-06 20:52:48 +00:00
|
|
|
const associatedNetwork =
|
|
|
|
staticNetworkConfigs[stripWeb3Network(node.network) as StaticNetworkIds];
|
2018-02-12 20:43:07 +00:00
|
|
|
const opt: NodeOption = {
|
|
|
|
isCustom: node.isCustom,
|
|
|
|
value: nodeId,
|
2018-02-24 18:02:07 +00:00
|
|
|
label: {
|
|
|
|
network: node.network,
|
|
|
|
service: node.service
|
|
|
|
},
|
2018-02-12 20:43:07 +00:00
|
|
|
color: associatedNetwork.color,
|
|
|
|
hidden: node.hidden
|
|
|
|
};
|
|
|
|
return opt;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface CustomNodeOption {
|
|
|
|
isCustom: true;
|
|
|
|
id: string;
|
|
|
|
value: string;
|
2018-02-24 18:02:07 +00:00
|
|
|
label: {
|
|
|
|
network: string;
|
|
|
|
nodeName: string;
|
|
|
|
};
|
2018-02-12 20:43:07 +00:00
|
|
|
color?: string;
|
|
|
|
hidden?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getCustomNodeOptions(state: AppState): CustomNodeOption[] {
|
|
|
|
const staticNetworkConfigs = getStaticNetworkConfigs(state);
|
|
|
|
const customNetworkConfigs = getCustomNetworkConfigs(state);
|
|
|
|
return Object.entries(getCustomNodeConfigs(state)).map(
|
2018-02-24 18:02:07 +00:00
|
|
|
([_, node]: [string, CustomNodeConfig]) => {
|
|
|
|
const chainId = node.network;
|
|
|
|
const associatedNetwork = isStaticNetworkId(state, chainId)
|
|
|
|
? staticNetworkConfigs[chainId]
|
|
|
|
: customNetworkConfigs[chainId];
|
2018-02-12 20:43:07 +00:00
|
|
|
const opt: CustomNodeOption = {
|
|
|
|
isCustom: node.isCustom,
|
|
|
|
value: node.id,
|
2018-02-24 18:02:07 +00:00
|
|
|
label: {
|
|
|
|
network: associatedNetwork.unit,
|
|
|
|
nodeName: node.name
|
|
|
|
},
|
2018-02-12 20:43:07 +00:00
|
|
|
color: associatedNetwork.isCustom ? undefined : associatedNetwork.color,
|
|
|
|
hidden: false,
|
|
|
|
id: node.id
|
|
|
|
};
|
|
|
|
return opt;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getNodeOptions(state: AppState) {
|
|
|
|
return [...getStaticNodeOptions(state), ...getCustomNodeOptions(state)];
|
|
|
|
}
|