ethers.js/utils/networks.js

152 lines
4.6 KiB
JavaScript
Raw Permalink Normal View History

2018-06-13 15:39:39 -04:00
'use strict';
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
2018-09-24 16:07:14 -04:00
var errors = __importStar(require("../errors"));
2018-11-20 15:45:47 -05:00
function ethDefaultProvider(network) {
return function (providers) {
var providerList = [];
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, network) {
return function (providers) {
if (providers.JsonRpcProvider) {
return new providers.JsonRpcProvider(url, network);
}
return null;
};
}
2018-06-18 05:42:41 -04:00
var homestead = {
chainId: 1,
2020-01-29 22:14:34 -05:00
ensAddress: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e",
2018-11-20 15:45:47 -05:00
name: "homestead",
_defaultProvider: ethDefaultProvider('homestead')
2018-06-18 05:42:41 -04:00
};
var ropsten = {
chainId: 3,
2020-01-29 22:14:34 -05:00
ensAddress: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e",
2018-11-20 15:45:47 -05:00
name: "ropsten",
_defaultProvider: ethDefaultProvider('ropsten')
2018-06-18 05:42:41 -04:00
};
var networks = {
unspecified: {
2018-11-20 15:45:47 -05:00
chainId: 0,
name: 'unspecified'
2018-06-13 15:39:39 -04:00
},
2018-06-18 05:42:41 -04:00
homestead: homestead,
mainnet: homestead,
morden: {
2018-11-20 15:45:47 -05:00
chainId: 2,
name: 'morden'
2018-06-13 15:39:39 -04:00
},
2018-06-18 05:42:41 -04:00
ropsten: ropsten,
testnet: ropsten,
rinkeby: {
chainId: 4,
2020-01-29 22:14:34 -05:00
ensAddress: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e",
2018-11-20 15:45:47 -05:00
name: 'rinkeby',
_defaultProvider: ethDefaultProvider('rinkeby')
2018-06-13 15:39:39 -04:00
},
2019-02-08 19:58:41 -05:00
goerli: {
chainId: 5,
2020-01-29 22:14:34 -05:00
ensAddress: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e",
2019-02-08 19:58:41 -05:00
name: "goerli",
2019-03-08 14:29:59 -05:00
_defaultProvider: ethDefaultProvider('goerli')
2019-02-08 19:58:41 -05:00
},
2018-06-18 05:42:41 -04:00
kovan: {
2018-11-20 15:45:47 -05:00
chainId: 42,
name: 'kovan',
_defaultProvider: ethDefaultProvider('kovan')
2018-06-13 15:39:39 -04:00
},
2018-06-18 05:42:41 -04:00
classic: {
2018-11-20 15:45:47 -05:00
chainId: 61,
name: 'classic',
_defaultProvider: etcDefaultProvider('https://web3.gastracker.io', 'classic')
2018-09-24 16:07:14 -04:00
},
classicTestnet: {
2018-11-20 15:45:47 -05:00
chainId: 62,
name: 'classicTestnet',
_defaultProvider: etcDefaultProvider('https://web3.gastracker.io/morden', 'classicTestnet')
2018-06-13 15:39:39 -04:00
}
};
/**
* getNetwork
*
2018-06-19 02:12:57 -04: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 15:39:39 -04:00
*/
function getNetwork(network) {
2018-11-20 15:45:47 -05:00
// No network (null)
if (network == null) {
2018-06-13 15:39:39 -04:00
return null;
}
if (typeof (network) === 'number') {
2018-11-20 15:45:47 -05:00
for (var name_1 in networks) {
var n_1 = networks[name_1];
2018-06-18 05:42:41 -04:00
if (n_1.chainId === network) {
return {
2018-11-20 15:45:47 -05:00
name: n_1.name,
2018-06-18 05:42:41 -04:00
chainId: n_1.chainId,
2018-11-20 15:45:47 -05:00
ensAddress: (n_1.ensAddress || null),
_defaultProvider: (n_1._defaultProvider || null)
2018-06-18 05:42:41 -04:00
};
2018-06-13 15:39:39 -04:00
}
}
2018-06-18 05:42:41 -04:00
return {
chainId: network,
name: 'unknown'
};
2018-06-13 15:39:39 -04:00
}
if (typeof (network) === 'string') {
2018-06-18 05:42:41 -04:00
var n_2 = networks[network];
if (n_2 == null) {
return null;
}
return {
2018-11-20 15:45:47 -05:00
name: n_2.name,
2018-06-18 05:42:41 -04:00
chainId: n_2.chainId,
2018-11-20 15:45:47 -05:00
ensAddress: n_2.ensAddress,
_defaultProvider: (n_2._defaultProvider || null)
2018-06-18 05:42:41 -04:00
};
2018-06-13 15:39:39 -04:00
}
2018-06-18 05:42:41 -04:00
var n = networks[network.name];
2018-06-13 15:39:39 -04:00
// Not a standard network; check that it is a valid network in general
2018-06-18 05:42:41 -04:00
if (!n) {
2018-08-13 17:01:44 +01:00
if (typeof (network.chainId) !== 'number') {
errors.throwError('invalid network chainId', errors.INVALID_ARGUMENT, { arg: 'network', value: network });
2018-06-13 15:39:39 -04:00
}
return network;
}
// Make sure the chainId matches the expected network chainId (or is 0; disable EIP-155)
2018-06-18 05:42:41 -04:00
if (network.chainId !== 0 && network.chainId !== n.chainId) {
errors.throwError('network chainId mismatch', errors.INVALID_ARGUMENT, { arg: 'network', value: network });
2018-06-13 15:39:39 -04:00
}
2018-11-20 15:45:47 -05:00
// Standard Network (allow overriding the ENS address)
2018-06-18 05:42:41 -04:00
return {
name: network.name,
chainId: n.chainId,
2018-11-20 15:45:47 -05:00
ensAddress: (network.ensAddress || n.ensAddress || null),
_defaultProvider: (network._defaultProvider || n._defaultProvider || null)
2018-06-18 05:42:41 -04:00
};
2018-06-13 15:39:39 -04:00
}
exports.getNetwork = getNetwork;