ethers.js/packages/networks/lib.esm/index.js

184 lines
5.5 KiB
JavaScript
Raw Permalink Normal View History

2019-05-14 18:48:48 -04:00
"use strict";
import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);
2019-05-14 18:48:48 -04:00
function ethDefaultProvider(network) {
return function (providers, options) {
if (options == null) {
options = {};
}
2019-11-20 18:57:38 +09:00
const providerList = [];
2019-05-14 18:48:48 -04:00
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 {
providerList.push(new providers.AlchemyProvider(network, options.alchemy));
}
catch (error) { }
}
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
let 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];
};
}
function etcDefaultProvider(url, network) {
return function (providers, options) {
if (providers.JsonRpcProvider) {
return new providers.JsonRpcProvider(url, network);
}
return null;
};
}
const homestead = {
2019-05-14 18:48:48 -04:00
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")
};
const ropsten = {
2019-05-14 18:48:48 -04:00
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
const classicMordor = {
chainId: 63,
name: "classicMordor",
_defaultProvider: etcDefaultProvider("https://www.ethercluster.com/mordor", "classicMordor")
};
const networks = {
2019-05-14 18:48:48 -04:00
unspecified: {
chainId: 0,
name: "unspecified"
},
homestead: homestead,
mainnet: homestead,
morden: {
chainId: 2,
name: "morden"
},
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",
2020-01-09 03:32:50 -05:00
_defaultProvider: etcDefaultProvider("https://www.ethercluster.com/etc", "classic")
2019-05-14 18:48:48 -04:00
},
2020-01-09 03:32:50 -05:00
classicMorden: {
2019-05-14 18:48:48 -04:00
chainId: 62,
2020-01-09 03:32:50 -05:00
name: "classicMorden",
},
classicMordor: classicMordor,
classicTestnet: classicMordor,
classicKotti: {
chainId: 6,
name: "classicKotti",
_defaultProvider: etcDefaultProvider("https://www.ethercluster.com/kotti", "classicKotti")
},
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..
*/
export function getNetwork(network) {
2019-05-14 18:48:48 -04:00
// No network (null)
if (network == null) {
return null;
}
if (typeof (network) === "number") {
2019-11-20 18:57:38 +09:00
for (const name in networks) {
const standard = networks[name];
if (standard.chainId === network) {
2019-05-14 18:48:48 -04:00
return {
name: standard.name,
chainId: standard.chainId,
ensAddress: (standard.ensAddress || null),
_defaultProvider: (standard._defaultProvider || null)
2019-05-14 18:48:48 -04:00
};
}
}
return {
chainId: network,
name: "unknown"
};
}
if (typeof (network) === "string") {
2019-11-20 18:57:38 +09:00
const standard = networks[network];
if (standard == null) {
2019-05-14 18:48:48 -04:00
return null;
}
return {
name: standard.name,
chainId: standard.chainId,
ensAddress: standard.ensAddress,
_defaultProvider: (standard._defaultProvider || null)
2019-05-14 18:48:48 -04:00
};
}
2019-11-20 18:57:38 +09:00
const standard = networks[network.name];
2019-05-14 18:48:48 -04:00
// 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
}
// Standard Network (allow overriding the ENS address)
return {
name: network.name,
chainId: standard.chainId,
ensAddress: (network.ensAddress || standard.ensAddress || null),
_defaultProvider: (network._defaultProvider || standard._defaultProvider || null)
};
}