William O'Beirne a043334685 Node Refactor (#1603)
* Initial work on refactoring node definitions to reduce number of places theyre defined, amount of copy pasting.

* Use makeAutoNodeNAme instead of manually appending _auto

* Add getNetVersion to list of unsupported methods

* PR feedback

* Rework web template node selector to be a network selector. Refactor some types to help with that. Better handle removing custom nodes.

* Remove color dropdown.

* Fix selecting custom networks. Show notification if change network intent fails.

* Use selectors for current node / network instead of intuiting from nodeSelection

* Add id key to all networks, simplify add and remove custom node and network functions.

* Fix a lot of uses of network.name to use network.id instead.

* Dont allow network chainid conflicts

* Fix web3 network by chainid

* Add testnet badge to network selector

* Change nomenclature from change(Node|Network)(Intent)? to change(Node|Network)(Requested|Succeeded)

* tscheck

* Better code for chainid collision

* Remove console logs

* Fix tests

* Network selector becomes self contained component used both by web header and electron nav.

* Dont select node again

* Additional title text

* tscheck

* Custom node behavior in Electron

* Close panel too

* Convert node label data into selector function

* tscheck

* Parens & space
2018-05-29 09:51:42 -05:00

102 lines
3.3 KiB
TypeScript

import { shepherd, redux } from 'mycrypto-shepherd';
import { INode } from '.';
import { tokenBalanceHandler } from './tokenBalanceProxy';
import { IProviderConfig } from 'mycrypto-shepherd/dist/lib/ducks/providerConfigs';
import { NODE_CONFIGS, makeNodeName } from './configs';
import { NodeConfig } from 'types/node';
type DeepPartial<T> = Partial<{ [key in keyof T]: Partial<T[key]> }>;
const { selectors, store } = redux;
const { providerBalancerSelectors: { balancerConfigSelectors } } = selectors;
export const makeProviderConfig = (options: DeepPartial<IProviderConfig> = {}): IProviderConfig => {
const defaultConfig: IProviderConfig = {
concurrency: 2,
network: 'ETH',
requestFailureThreshold: 10,
supportedMethods: {
getNetVersion: true,
ping: true,
sendCallRequest: true,
sendCallRequests: true,
getBalance: true,
estimateGas: true,
getTransactionCount: true,
getCurrentBlock: true,
sendRawTx: true,
getTransactionByHash: true,
getTransactionReceipt: true,
/*web3 methods*/
signMessage: true,
sendTransaction: true
},
timeoutThresholdMs: 10000
};
return {
...defaultConfig,
...options,
supportedMethods: {
...defaultConfig.supportedMethods,
...(options.supportedMethods ? options.supportedMethods : {})
}
};
};
let shepherdProvider: INode;
shepherd
.init({ queueTimeout: 10000 })
.then(
provider => (shepherdProvider = (new Proxy(provider, tokenBalanceHandler) as any) as INode)
);
export const getShepherdManualMode = () => balancerConfigSelectors.getManualMode(store.getState());
export const getShepherdOffline = () => balancerConfigSelectors.isOffline(store.getState());
export const getShepherdNetwork = () => balancerConfigSelectors.getNetwork(store.getState());
export const getShepherdPending = () =>
balancerConfigSelectors.isSwitchingNetworks(store.getState());
const autoNodeSuffix = 'auto';
const web3NodePrefix = 'WEB3_';
export const makeWeb3Network = (network: string) => `${web3NodePrefix}${network}`;
export const stripWeb3Network = (network: string) => network.replace(web3NodePrefix, '');
export const isAutoNode = (nodeName: string) =>
nodeName.endsWith(autoNodeSuffix) || nodeName === 'web3';
export const isAutoNodeConfig = (node: NodeConfig) => !node.isCustom && node.isAuto;
export const makeAutoNodeName = (network: string) => makeNodeName(network, autoNodeSuffix);
/**
* Assemble shepherd providers from node configs. Includes pseudo-configs
*/
const WEB3_NETWORKS = ['ETH', 'Ropsten', 'Kovan', 'Rinkeby', 'ETC'];
Object.entries(NODE_CONFIGS).forEach(([network, nodes]) => {
const nodeProviderConf = makeProviderConfig({ network });
const web3ProviderConf = WEB3_NETWORKS.includes(network)
? makeProviderConfig({
network: makeWeb3Network(network),
supportedMethods: {
sendRawTx: false,
sendTransaction: false,
signMessage: false,
getNetVersion: false
}
})
: null;
nodes.forEach(n => {
shepherd.useProvider(n.type, n.name, nodeProviderConf, n.url);
if (web3ProviderConf) {
shepherd.useProvider(n.type, `web3_${n.name}`, web3ProviderConf, n.url);
}
});
});
export { shepherdProvider, shepherd };
export * from './INode';
export * from './configs';