2019-05-14 18:48:48 -04:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2021-03-07 18:24:04 -05:00
|
|
|
exports.getNetwork = void 0;
|
2019-08-02 02:10:58 -04:00
|
|
|
var logger_1 = require("@ethersproject/logger");
|
|
|
|
var _version_1 = require("./_version");
|
|
|
|
var logger = new logger_1.Logger(_version_1.version);
|
2020-07-16 05:35:32 -04:00
|
|
|
;
|
|
|
|
function isRenetworkable(value) {
|
|
|
|
return (value && typeof (value.renetwork) === "function");
|
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
function ethDefaultProvider(network) {
|
2020-07-16 05:35:32 -04:00
|
|
|
var func = function (providers, options) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (options == null) {
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
var providerList = [];
|
|
|
|
if (providers.InfuraProvider) {
|
|
|
|
try {
|
|
|
|
providerList.push(new providers.InfuraProvider(network, options.infura));
|
|
|
|
}
|
|
|
|
catch (error) { }
|
|
|
|
}
|
|
|
|
if (providers.EtherscanProvider) {
|
|
|
|
try {
|
|
|
|
providerList.push(new providers.EtherscanProvider(network, options.etherscan));
|
|
|
|
}
|
|
|
|
catch (error) { }
|
|
|
|
}
|
|
|
|
if (providers.AlchemyProvider) {
|
|
|
|
try {
|
2021-05-19 00:05:36 -04:00
|
|
|
providerList.push(new providers.AlchemyProvider(network, options.alchemy));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
catch (error) { }
|
|
|
|
}
|
2021-05-19 00:05:36 -04:00
|
|
|
/*
|
2021-02-01 15:56:47 -05:00
|
|
|
if (providers.PocketProvider) {
|
2021-05-19 00:05:36 -04:00
|
|
|
// These networks are currently faulty on Pocket as their
|
2021-03-30 15:22:45 -04:00
|
|
|
// network does not handle the Berlin hardfork, which is
|
|
|
|
// live on these ones.
|
|
|
|
// @TODO: This goes away once Pocket has upgraded their nodes
|
2021-05-19 00:05:36 -04:00
|
|
|
const skip = [ "goerli", "ropsten", "rinkeby" ];
|
2021-02-01 15:56:47 -05:00
|
|
|
try {
|
2021-05-19 00:05:36 -04:00
|
|
|
const provider = new providers.PocketProvider(network);
|
2021-03-30 15:22:45 -04:00
|
|
|
if (provider.network && skip.indexOf(provider.network.name) === -1) {
|
|
|
|
providerList.push(provider);
|
|
|
|
}
|
2021-05-19 00:05:36 -04:00
|
|
|
} catch(error) { }
|
2021-02-01 15:56:47 -05:00
|
|
|
}
|
2021-05-19 00:05:36 -04:00
|
|
|
*/
|
2019-08-21 01:52:13 -04:00
|
|
|
if (providers.CloudflareProvider) {
|
|
|
|
try {
|
|
|
|
providerList.push(new providers.CloudflareProvider(network));
|
|
|
|
}
|
|
|
|
catch (error) { }
|
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
if (providerList.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (providers.FallbackProvider) {
|
2020-02-26 10:06:48 +00:00
|
|
|
var quorum = 1;
|
2019-11-20 18:57:38 +09:00
|
|
|
if (options.quorum != null) {
|
|
|
|
quorum = options.quorum;
|
|
|
|
}
|
2020-02-26 10:06:48 +00:00
|
|
|
else if (network === "homestead") {
|
|
|
|
quorum = 2;
|
2019-11-20 18:57:38 +09:00
|
|
|
}
|
|
|
|
return new providers.FallbackProvider(providerList, quorum);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
return providerList[0];
|
|
|
|
};
|
2020-07-16 05:35:32 -04:00
|
|
|
func.renetwork = function (network) {
|
|
|
|
return ethDefaultProvider(network);
|
|
|
|
};
|
|
|
|
return func;
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
function etcDefaultProvider(url, network) {
|
2020-07-16 05:35:32 -04:00
|
|
|
var func = function (providers, options) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (providers.JsonRpcProvider) {
|
|
|
|
return new providers.JsonRpcProvider(url, network);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
2020-07-16 05:35:32 -04:00
|
|
|
func.renetwork = function (network) {
|
|
|
|
return etcDefaultProvider(url, network);
|
|
|
|
};
|
|
|
|
return func;
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
var homestead = {
|
|
|
|
chainId: 1,
|
2020-01-29 21:43:56 -05:00
|
|
|
ensAddress: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e",
|
2019-05-14 18:48:48 -04:00
|
|
|
name: "homestead",
|
|
|
|
_defaultProvider: ethDefaultProvider("homestead")
|
|
|
|
};
|
|
|
|
var ropsten = {
|
|
|
|
chainId: 3,
|
2020-01-29 21:43:56 -05:00
|
|
|
ensAddress: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e",
|
2019-05-14 18:48:48 -04:00
|
|
|
name: "ropsten",
|
|
|
|
_defaultProvider: ethDefaultProvider("ropsten")
|
|
|
|
};
|
2020-01-09 03:32:50 -05:00
|
|
|
var classicMordor = {
|
|
|
|
chainId: 63,
|
|
|
|
name: "classicMordor",
|
|
|
|
_defaultProvider: etcDefaultProvider("https://www.ethercluster.com/mordor", "classicMordor")
|
|
|
|
};
|
2019-05-14 18:48:48 -04:00
|
|
|
var networks = {
|
2021-05-17 16:19:36 -04:00
|
|
|
unspecified: { chainId: 0, name: "unspecified" },
|
2019-05-14 18:48:48 -04:00
|
|
|
homestead: homestead,
|
|
|
|
mainnet: homestead,
|
2021-05-17 16:19:36 -04:00
|
|
|
morden: { chainId: 2, name: "morden" },
|
2019-05-14 18:48:48 -04:00
|
|
|
ropsten: ropsten,
|
|
|
|
testnet: ropsten,
|
|
|
|
rinkeby: {
|
|
|
|
chainId: 4,
|
2020-01-29 21:43:56 -05:00
|
|
|
ensAddress: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e",
|
2019-05-14 18:48:48 -04:00
|
|
|
name: "rinkeby",
|
|
|
|
_defaultProvider: ethDefaultProvider("rinkeby")
|
|
|
|
},
|
|
|
|
kovan: {
|
|
|
|
chainId: 42,
|
|
|
|
name: "kovan",
|
|
|
|
_defaultProvider: ethDefaultProvider("kovan")
|
|
|
|
},
|
|
|
|
goerli: {
|
|
|
|
chainId: 5,
|
2020-01-29 21:43:56 -05:00
|
|
|
ensAddress: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e",
|
2019-05-14 18:48:48 -04:00
|
|
|
name: "goerli",
|
|
|
|
_defaultProvider: ethDefaultProvider("goerli")
|
|
|
|
},
|
2020-01-09 03:32:50 -05:00
|
|
|
// ETC (See: #351)
|
2019-05-14 18:48:48 -04:00
|
|
|
classic: {
|
|
|
|
chainId: 61,
|
|
|
|
name: "classic",
|
2021-05-17 16:19:36 -04:00
|
|
|
_defaultProvider: etcDefaultProvider("https:/\/www.ethercluster.com/etc", "classic")
|
2020-01-09 03:32:50 -05:00
|
|
|
},
|
2021-05-17 16:19:36 -04:00
|
|
|
classicMorden: { chainId: 62, name: "classicMorden" },
|
2020-01-09 03:32:50 -05:00
|
|
|
classicMordor: classicMordor,
|
|
|
|
classicTestnet: classicMordor,
|
|
|
|
classicKotti: {
|
|
|
|
chainId: 6,
|
|
|
|
name: "classicKotti",
|
2021-05-17 16:19:36 -04:00
|
|
|
_defaultProvider: etcDefaultProvider("https:/\/www.ethercluster.com/kotti", "classicKotti")
|
2020-01-09 03:32:50 -05:00
|
|
|
},
|
2021-05-17 16:19:36 -04:00
|
|
|
xdai: { chainId: 100, name: "xdai" },
|
|
|
|
matic: { chainId: 137, name: "matic" },
|
2021-06-10 18:29:05 -04:00
|
|
|
maticmum: { chainId: 80001, name: "maticmum" },
|
2021-05-17 16:19:36 -04:00
|
|
|
bnb: { chainId: 56, name: "bnb" },
|
|
|
|
bnbt: { chainId: 97, name: "bnbt" },
|
2019-05-14 18:48:48 -04:00
|
|
|
};
|
|
|
|
/**
|
|
|
|
* getNetwork
|
|
|
|
*
|
|
|
|
* Converts a named common networks or chain ID (network ID) to a Network
|
|
|
|
* and verifies a network is a valid Network..
|
|
|
|
*/
|
|
|
|
function getNetwork(network) {
|
|
|
|
// No network (null)
|
|
|
|
if (network == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (typeof (network) === "number") {
|
|
|
|
for (var name_1 in networks) {
|
|
|
|
var standard_1 = networks[name_1];
|
|
|
|
if (standard_1.chainId === network) {
|
|
|
|
return {
|
|
|
|
name: standard_1.name,
|
|
|
|
chainId: standard_1.chainId,
|
|
|
|
ensAddress: (standard_1.ensAddress || null),
|
|
|
|
_defaultProvider: (standard_1._defaultProvider || null)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
chainId: network,
|
|
|
|
name: "unknown"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (typeof (network) === "string") {
|
|
|
|
var standard_2 = networks[network];
|
|
|
|
if (standard_2 == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
name: standard_2.name,
|
|
|
|
chainId: standard_2.chainId,
|
|
|
|
ensAddress: standard_2.ensAddress,
|
|
|
|
_defaultProvider: (standard_2._defaultProvider || null)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
var standard = networks[network.name];
|
|
|
|
// Not a standard network; check that it is a valid network in general
|
|
|
|
if (!standard) {
|
|
|
|
if (typeof (network.chainId) !== "number") {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("invalid network chainId", "network", network);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
return network;
|
|
|
|
}
|
|
|
|
// Make sure the chainId matches the expected network chainId (or is 0; disable EIP-155)
|
|
|
|
if (network.chainId !== 0 && network.chainId !== standard.chainId) {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("network chainId mismatch", "network", network);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-07-16 05:35:32 -04:00
|
|
|
// @TODO: In the next major version add an attach function to a defaultProvider
|
|
|
|
// class and move the _defaultProvider internal to this file (extend Network)
|
|
|
|
var defaultProvider = network._defaultProvider || null;
|
|
|
|
if (defaultProvider == null && standard._defaultProvider) {
|
|
|
|
if (isRenetworkable(standard._defaultProvider)) {
|
|
|
|
defaultProvider = standard._defaultProvider.renetwork(network);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
defaultProvider = standard._defaultProvider;
|
|
|
|
}
|
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
// Standard Network (allow overriding the ENS address)
|
|
|
|
return {
|
|
|
|
name: network.name,
|
|
|
|
chainId: standard.chainId,
|
|
|
|
ensAddress: (network.ensAddress || standard.ensAddress || null),
|
2020-07-16 05:35:32 -04:00
|
|
|
_defaultProvider: defaultProvider
|
2019-05-14 18:48:48 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
exports.getNetwork = getNetwork;
|
2020-07-13 08:03:56 -04:00
|
|
|
//# sourceMappingURL=index.js.map
|