ethers.js/packages/networks/src.ts/index.ts

310 lines
9.4 KiB
TypeScript
Raw Permalink Normal View History

2019-05-14 18:25:46 -04:00
"use strict";
2019-08-01 18:04:06 -04:00
import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);
2019-05-14 18:25:46 -04:00
import { Network, Networkish } from "./types";
export {
Network,
Networkish
};
type DefaultProviderFunc = (providers: any, options?: any) => any;
interface Renetworkable extends DefaultProviderFunc {
renetwork: (network: Network) => DefaultProviderFunc;
};
function isRenetworkable(value: any): value is Renetworkable {
return (value && typeof(value.renetwork) === "function");
}
function ethDefaultProvider(network: string | Network): Renetworkable {
const func = function(providers: any, options?: any): any {
2019-05-14 18:25:46 -04:00
if (options == null) { options = { }; }
const providerList: Array<any> = [];
2019-05-14 18:25:46 -04:00
if (providers.InfuraProvider && options.infura !== "-") {
2019-05-14 18:25:46 -04:00
try {
providerList.push(new providers.InfuraProvider(network, options.infura));
} catch(error) { }
}
if (providers.EtherscanProvider && options.etherscan !== "-") {
2019-05-14 18:25:46 -04:00
try {
providerList.push(new providers.EtherscanProvider(network, options.etherscan));
2019-05-14 18:25:46 -04:00
} catch(error) { }
}
if (providers.AlchemyProvider && options.alchemy !== "-") {
2019-05-14 18:25:46 -04:00
try {
providerList.push(new providers.AlchemyProvider(network, options.alchemy));
2019-05-14 18:25:46 -04:00
} catch(error) { }
2019-08-21 01:45:06 -04:00
}
if (providers.PocketProvider && options.pocket !== "-") {
// These networks are currently faulty on Pocket as their
2021-03-26 16:16:56 -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
2022-09-13 20:36:43 -04:00
const skip = [ "goerli", "ropsten", "rinkeby", "sepolia" ];
try {
const provider = new providers.PocketProvider(network, options.pocket);
2021-03-26 16:16:56 -04:00
if (provider.network && skip.indexOf(provider.network.name) === -1) {
providerList.push(provider);
}
} catch(error) { }
}
if (providers.CloudflareProvider && options.cloudflare !== "-") {
2019-08-21 01:45:06 -04:00
try {
providerList.push(new providers.CloudflareProvider(network));
} catch(error) { }
2019-05-14 18:25:46 -04:00
}
2022-03-19 02:44:29 -04:00
if (providers.AnkrProvider && options.ankr !== "-") {
try {
const skip = [ "ropsten" ];
const provider = new providers.AnkrProvider(network, options.ankr);
if (provider.network && skip.indexOf(provider.network.name) === -1) {
providerList.push(provider);
}
2022-03-19 02:44:29 -04:00
} catch(error) { }
}
if (providers.QuickNodeProvider && options.quicknode !== "-") {
try {
providerList.push(new providers.QuickNodeProvider(network, options.quicknode));
} catch(error) { }
}
2019-05-14 18:25:46 -04:00
if (providerList.length === 0) { return null; }
if (providers.FallbackProvider) {
let quorum = 1;
if (options.quorum != null) {
quorum = options.quorum;
} else if (network === "homestead") {
quorum = 2;
}
return new providers.FallbackProvider(providerList, quorum);
2019-05-14 18:25:46 -04:00
}
return providerList[0];
};
func.renetwork = function(network: Network) {
return ethDefaultProvider(network);
};
return func;
2019-05-14 18:25:46 -04:00
}
function etcDefaultProvider(url: string, network: string | Network): Renetworkable {
const func = function(providers: any, options?: any): any {
2019-05-14 18:25:46 -04:00
if (providers.JsonRpcProvider) {
return new providers.JsonRpcProvider(url, network);
}
return null;
};
func.renetwork = function(network: Network) {
return etcDefaultProvider(url, network);
};
return func;
2019-05-14 18:25:46 -04:00
}
const homestead: Network = {
chainId: 1,
ensAddress: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e",
2019-05-14 18:25:46 -04:00
name: "homestead",
_defaultProvider: ethDefaultProvider("homestead")
};
const ropsten: Network = {
chainId: 3,
ensAddress: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e",
2019-05-14 18:25:46 -04:00
name: "ropsten",
_defaultProvider: ethDefaultProvider("ropsten")
};
const classicMordor: Network = {
chainId: 63,
name: "classicMordor",
_defaultProvider: etcDefaultProvider("https://www.ethercluster.com/mordor", "classicMordor")
};
// See: https://chainlist.org
2019-05-14 18:25:46 -04:00
const networks: { [name: string]: Network } = {
unspecified: { chainId: 0, name: "unspecified" },
2019-05-14 18:25:46 -04:00
homestead: homestead,
mainnet: homestead,
morden: { chainId: 2, name: "morden" },
2019-05-14 18:25:46 -04:00
ropsten: ropsten,
testnet: ropsten,
rinkeby: {
chainId: 4,
ensAddress: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e",
2019-05-14 18:25:46 -04:00
name: "rinkeby",
_defaultProvider: ethDefaultProvider("rinkeby")
},
kovan: {
chainId: 42,
name: "kovan",
_defaultProvider: ethDefaultProvider("kovan")
},
goerli: {
chainId: 5,
ensAddress: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e",
2019-05-14 18:25:46 -04:00
name: "goerli",
_defaultProvider: ethDefaultProvider("goerli")
2022-01-06 03:37:13 -05:00
},
kintsugi: { chainId: 1337702, name: "kintsugi" },
2019-05-14 18:25:46 -04:00
2022-09-13 20:36:43 -04:00
sepolia: {
chainId: 11155111,
name: "sepolia",
_defaultProvider: ethDefaultProvider("sepolia")
},
holesky: {
chainId: 17000,
name: "holesky",
_defaultProvider: ethDefaultProvider("holesky")
},
// ETC (See: #351)
2019-05-14 18:25:46 -04:00
classic: {
chainId: 61,
name: "classic",
_defaultProvider: etcDefaultProvider("https:/\/www.ethercluster.com/etc", "classic")
2019-05-14 18:25:46 -04:00
},
classicMorden: { chainId: 62, name: "classicMorden" },
classicMordor: classicMordor,
classicTestnet: classicMordor,
classicKotti: {
chainId: 6,
name: "classicKotti",
_defaultProvider: etcDefaultProvider("https:/\/www.ethercluster.com/kotti", "classicKotti")
},
xdai: { chainId: 100, name: "xdai" },
matic: {
chainId: 137,
name: "matic",
_defaultProvider: ethDefaultProvider("matic")
},
maticmum: {
chainId: 80001,
name: "maticmum",
_defaultProvider: ethDefaultProvider("maticmum")
},
optimism: {
chainId: 10,
name: "optimism",
_defaultProvider: ethDefaultProvider("optimism")
},
"optimism-kovan": { chainId: 69, name: "optimism-kovan" },
"optimism-goerli": { chainId: 420, name: "optimism-goerli" },
"optimism-sepolia": { chainId: 11155420, name: "optimism-sepolia" },
arbitrum: { chainId: 42161, name: "arbitrum" },
"arbitrum-rinkeby": { chainId: 421611, name: "arbitrum-rinkeby" },
"arbitrum-goerli": { chainId: 421613, name: "arbitrum-goerli" },
"arbitrum-sepolia": { chainId: 421614, name: "arbitrum-sepolia" },
bnb: { chainId: 56, name: "bnb" },
bnbt: { chainId: 97, name: "bnbt" },
2019-05-14 18:25:46 -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: Networkish): Network {
// No network (null)
if (network == null) { return null; }
if (typeof(network) === "number") {
for (const name in networks) {
const standard = networks[name];
2019-05-14 18:25:46 -04:00
if (standard.chainId === network) {
return {
name: standard.name,
chainId: standard.chainId,
ensAddress: (standard.ensAddress || null),
_defaultProvider: (standard._defaultProvider || null)
};
}
}
return {
chainId: network,
name: "unknown"
};
}
if (typeof(network) === "string") {
const standard = networks[network];
2019-05-14 18:25:46 -04:00
if (standard == null) { return null; }
return {
name: standard.name,
chainId: standard.chainId,
ensAddress: standard.ensAddress,
_defaultProvider: (standard._defaultProvider || null)
};
}
const standard = networks[network.name];
2019-05-14 18:25:46 -04:00
// Not a standard network; check that it is a valid network in general
if (!standard) {
if (typeof(network.chainId) !== "number") {
2019-08-01 18:04:06 -04:00
logger.throwArgumentError("invalid network chainId", "network", network);
2019-05-14 18:25:46 -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-01 18:04:06 -04:00
logger.throwArgumentError("network chainId mismatch", "network", network);
2019-05-14 18:25:46 -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)
let defaultProvider: DefaultProviderFunc = 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:25:46 -04:00
// Standard Network (allow overriding the ENS address)
return {
name: network.name,
chainId: standard.chainId,
ensAddress: (network.ensAddress || standard.ensAddress || null),
_defaultProvider: defaultProvider
2019-05-14 18:25:46 -04:00
};
}