2018-06-13 22:39:39 +03:00
|
|
|
'use strict';
|
|
|
|
|
2018-09-24 22:55:17 +03:00
|
|
|
import * as errors from '../errors';
|
2018-06-13 22:39:39 +03:00
|
|
|
|
|
|
|
|
2018-07-31 01:59:52 +03:00
|
|
|
export type Network = {
|
|
|
|
name: string,
|
|
|
|
chainId: number,
|
|
|
|
ensAddress?: string,
|
2018-11-20 23:41:12 +03:00
|
|
|
_defaultProvider?: (providers: any) => any
|
2018-07-31 01:59:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export type Networkish = Network | string | number;
|
|
|
|
|
2018-11-20 23:41:12 +03:00
|
|
|
function ethDefaultProvider(network: string): (providers: any) => any {
|
|
|
|
return function(providers: any): any {
|
|
|
|
let providerList: Array<any> = [];
|
|
|
|
|
|
|
|
if (providers.InfuraProvider) {
|
|
|
|
providerList.push(new providers.InfuraProvider(network));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (providers.EtherscanProvider) {
|
|
|
|
providerList.push(new providers.EtherscanProvider(network));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (providerList.length === 0) { return null; }
|
|
|
|
|
|
|
|
if (providers.FallbackProvider) {
|
|
|
|
return new providers.FallbackProvider(providerList);;
|
|
|
|
}
|
|
|
|
|
|
|
|
return providerList[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function etcDefaultProvider(url: string, network: string): (providers: any) => any {
|
|
|
|
return function(providers: any): any {
|
|
|
|
if (providers.JsonRpcProvider) {
|
|
|
|
return new providers.JsonRpcProvider(url, network);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2018-07-31 01:59:52 +03:00
|
|
|
|
|
|
|
const homestead: Network = {
|
2018-06-18 12:42:41 +03:00
|
|
|
chainId: 1,
|
|
|
|
ensAddress: "0x314159265dd8dbb310642f98f50c066173c1259b",
|
2018-11-20 23:41:12 +03:00
|
|
|
name: "homestead",
|
|
|
|
_defaultProvider: ethDefaultProvider('homestead')
|
2018-06-18 12:42:41 +03:00
|
|
|
};
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2018-07-31 01:59:52 +03:00
|
|
|
const ropsten: Network = {
|
2018-06-18 12:42:41 +03:00
|
|
|
chainId: 3,
|
|
|
|
ensAddress: "0x112234455c3a32fd11230c42e7bccd4a84e02010",
|
2018-11-20 23:41:12 +03:00
|
|
|
name: "ropsten",
|
|
|
|
_defaultProvider: ethDefaultProvider('ropsten')
|
2018-06-18 12:42:41 +03:00
|
|
|
};
|
|
|
|
|
2018-11-20 23:41:12 +03:00
|
|
|
const networks: { [name: string]: Network } = {
|
2018-06-18 12:42:41 +03:00
|
|
|
unspecified: {
|
2018-11-20 23:41:12 +03:00
|
|
|
chainId: 0,
|
|
|
|
name: 'unspecified'
|
2018-06-13 22:39:39 +03:00
|
|
|
},
|
2018-06-18 12:42:41 +03:00
|
|
|
|
|
|
|
homestead: homestead,
|
|
|
|
mainnet: homestead,
|
|
|
|
|
|
|
|
morden: {
|
2018-11-20 23:41:12 +03:00
|
|
|
chainId: 2,
|
|
|
|
name: 'morden'
|
2018-06-13 22:39:39 +03:00
|
|
|
},
|
|
|
|
|
2018-06-18 12:42:41 +03:00
|
|
|
ropsten: ropsten,
|
|
|
|
testnet: ropsten,
|
|
|
|
|
|
|
|
rinkeby: {
|
|
|
|
chainId: 4,
|
2018-11-20 23:41:12 +03:00
|
|
|
ensAddress: "0xe7410170f87102DF0055eB195163A03B7F2Bff4A",
|
|
|
|
name: 'rinkeby',
|
|
|
|
_defaultProvider: ethDefaultProvider('rinkeby')
|
2018-06-13 22:39:39 +03:00
|
|
|
},
|
|
|
|
|
2018-06-18 12:42:41 +03:00
|
|
|
kovan: {
|
2018-11-20 23:41:12 +03:00
|
|
|
chainId: 42,
|
|
|
|
name: 'kovan',
|
|
|
|
_defaultProvider: ethDefaultProvider('kovan')
|
2018-06-13 22:39:39 +03:00
|
|
|
},
|
|
|
|
|
2018-06-18 12:42:41 +03:00
|
|
|
classic: {
|
2018-11-20 23:41:12 +03:00
|
|
|
chainId: 61,
|
|
|
|
name: 'classic',
|
|
|
|
_defaultProvider: etcDefaultProvider('https://web3.gastracker.io', 'classic')
|
2018-09-21 22:34:56 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
classicTestnet: {
|
2018-11-20 23:41:12 +03:00
|
|
|
chainId: 62,
|
|
|
|
name: 'classicTestnet',
|
|
|
|
_defaultProvider: etcDefaultProvider('https://web3.gastracker.io/morden', 'classicTestnet')
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getNetwork
|
|
|
|
*
|
2018-06-19 09:12:57 +03:00
|
|
|
* Converts a named common networks or chain ID (network ID) to a Network
|
|
|
|
* and verifies a network is a valid Network..
|
2018-06-13 22:39:39 +03:00
|
|
|
*/
|
2018-06-18 12:42:41 +03:00
|
|
|
export function getNetwork(network: Networkish): Network {
|
2018-11-20 23:41:12 +03:00
|
|
|
// No network (null)
|
|
|
|
if (network == null) { return null; }
|
2018-06-13 22:39:39 +03:00
|
|
|
|
|
|
|
if (typeof(network) === 'number') {
|
2018-11-20 23:41:12 +03:00
|
|
|
for (let name in networks) {
|
2018-06-18 12:42:41 +03:00
|
|
|
let n = networks[name];
|
2018-06-13 22:39:39 +03:00
|
|
|
if (n.chainId === network) {
|
2018-06-18 12:42:41 +03:00
|
|
|
return {
|
2018-11-20 23:41:12 +03:00
|
|
|
name: n.name,
|
2018-06-18 12:42:41 +03:00
|
|
|
chainId: n.chainId,
|
2018-11-20 23:41:12 +03:00
|
|
|
ensAddress: (n.ensAddress || null),
|
|
|
|
_defaultProvider: (n._defaultProvider || null)
|
2018-06-18 12:42:41 +03:00
|
|
|
};
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-18 12:42:41 +03:00
|
|
|
return {
|
|
|
|
chainId: network,
|
|
|
|
name: 'unknown'
|
|
|
|
};
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(network) === 'string') {
|
2018-06-18 12:42:41 +03:00
|
|
|
let n = networks[network];
|
|
|
|
if (n == null) { return null; }
|
|
|
|
return {
|
2018-11-20 23:41:12 +03:00
|
|
|
name: n.name,
|
2018-06-18 12:42:41 +03:00
|
|
|
chainId: n.chainId,
|
2018-11-20 23:41:12 +03:00
|
|
|
ensAddress: n.ensAddress,
|
|
|
|
_defaultProvider: (n._defaultProvider || null)
|
2018-06-18 12:42:41 +03:00
|
|
|
};
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
2018-06-23 03:30:50 +03:00
|
|
|
let n = networks[network.name];
|
2018-06-13 22:39:39 +03:00
|
|
|
|
|
|
|
// Not a standard network; check that it is a valid network in general
|
2018-06-18 12:42:41 +03:00
|
|
|
if (!n) {
|
2018-08-13 18:50:47 +03:00
|
|
|
if (typeof(network.chainId) !== 'number') {
|
2018-06-25 01:41:28 +03:00
|
|
|
errors.throwError('invalid network chainId', errors.INVALID_ARGUMENT, { arg: 'network', value: network });
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
return network;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the chainId matches the expected network chainId (or is 0; disable EIP-155)
|
2018-06-18 12:42:41 +03:00
|
|
|
if (network.chainId !== 0 && network.chainId !== n.chainId) {
|
2018-06-25 01:41:28 +03:00
|
|
|
errors.throwError('network chainId mismatch', errors.INVALID_ARGUMENT, { arg: 'network', value: network });
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
2018-11-20 23:41:12 +03:00
|
|
|
// Standard Network (allow overriding the ENS address)
|
2018-06-18 12:42:41 +03:00
|
|
|
return {
|
|
|
|
name: network.name,
|
|
|
|
chainId: n.chainId,
|
2018-11-20 23:41:12 +03:00
|
|
|
ensAddress: (network.ensAddress || n.ensAddress || null),
|
|
|
|
_defaultProvider: (network._defaultProvider || n._defaultProvider || null)
|
2018-06-18 12:42:41 +03:00
|
|
|
};
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|