2022-12-06 06:19:14 +03:00
|
|
|
import { assert } from "../utils/index.js";
|
2022-09-05 23:57:11 +03:00
|
|
|
import { AnkrProvider } from "./provider-ankr.js";
|
|
|
|
import { AlchemyProvider } from "./provider-alchemy.js";
|
|
|
|
import { CloudflareProvider } from "./provider-cloudflare.js";
|
|
|
|
import { EtherscanProvider } from "./provider-etherscan.js";
|
|
|
|
import { InfuraProvider } from "./provider-infura.js";
|
|
|
|
//import { PocketProvider } from "./provider-pocket.js";
|
2022-12-06 06:19:14 +03:00
|
|
|
import { QuickNodeProvider } from "./provider-quicknode.js";
|
2022-09-05 23:57:11 +03:00
|
|
|
import { FallbackProvider } from "./provider-fallback.js";
|
|
|
|
import { JsonRpcProvider } from "./provider-jsonrpc.js";
|
2023-07-29 09:37:03 +03:00
|
|
|
import { Network } from "./network.js";
|
2022-09-05 23:57:11 +03:00
|
|
|
import { WebSocketProvider } from "./provider-websocket.js";
|
|
|
|
function isWebSocketLike(value) {
|
|
|
|
return (value && typeof (value.send) === "function" &&
|
|
|
|
typeof (value.close) === "function");
|
|
|
|
}
|
2023-07-29 09:37:03 +03:00
|
|
|
const Testnets = "goerli kovan sepolia classicKotti optimism-goerli arbitrum-goerli matic-mumbai bnbt".split(" ");
|
2022-09-05 23:57:11 +03:00
|
|
|
export function getDefaultProvider(network, options) {
|
|
|
|
if (options == null) {
|
|
|
|
options = {};
|
|
|
|
}
|
2023-07-29 09:37:03 +03:00
|
|
|
const allowService = (name) => {
|
|
|
|
if (options[name] === "-") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (typeof (options.exclusive) === "string") {
|
|
|
|
return (name === options.exclusive);
|
|
|
|
}
|
|
|
|
if (Array.isArray(options.exclusive)) {
|
|
|
|
return (options.exclusive.indexOf(name) !== -1);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2022-09-05 23:57:11 +03:00
|
|
|
if (typeof (network) === "string" && network.match(/^https?:/)) {
|
|
|
|
return new JsonRpcProvider(network);
|
|
|
|
}
|
|
|
|
if (typeof (network) === "string" && network.match(/^wss?:/) || isWebSocketLike(network)) {
|
|
|
|
return new WebSocketProvider(network);
|
|
|
|
}
|
2023-07-29 09:37:03 +03:00
|
|
|
// Get the network and name, if possible
|
|
|
|
let staticNetwork = null;
|
|
|
|
try {
|
|
|
|
staticNetwork = Network.from(network);
|
|
|
|
}
|
|
|
|
catch (error) { }
|
2022-09-05 23:57:11 +03:00
|
|
|
const providers = [];
|
2023-07-29 09:37:03 +03:00
|
|
|
if (allowService("publicPolygon") && staticNetwork) {
|
|
|
|
if (staticNetwork.name === "matic") {
|
|
|
|
providers.push(new JsonRpcProvider("https:/\/polygon-rpc.com/", staticNetwork, { staticNetwork }));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (allowService("alchemy")) {
|
2022-09-05 23:57:11 +03:00
|
|
|
try {
|
|
|
|
providers.push(new AlchemyProvider(network, options.alchemy));
|
|
|
|
}
|
2023-05-19 00:26:59 +03:00
|
|
|
catch (error) { }
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
2023-07-29 09:37:03 +03:00
|
|
|
if (allowService("ankr") && options.ankr != null) {
|
2022-09-05 23:57:11 +03:00
|
|
|
try {
|
|
|
|
providers.push(new AnkrProvider(network, options.ankr));
|
|
|
|
}
|
2023-05-19 00:26:59 +03:00
|
|
|
catch (error) { }
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
2023-07-29 09:37:03 +03:00
|
|
|
if (allowService("cloudflare")) {
|
2022-09-05 23:57:11 +03:00
|
|
|
try {
|
|
|
|
providers.push(new CloudflareProvider(network));
|
|
|
|
}
|
2023-05-19 00:26:59 +03:00
|
|
|
catch (error) { }
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
2023-07-29 09:37:03 +03:00
|
|
|
if (allowService("etherscan")) {
|
2022-09-05 23:57:11 +03:00
|
|
|
try {
|
|
|
|
providers.push(new EtherscanProvider(network, options.etherscan));
|
|
|
|
}
|
2023-05-19 00:26:59 +03:00
|
|
|
catch (error) { }
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
2023-07-29 09:37:03 +03:00
|
|
|
if (allowService("infura")) {
|
2022-09-05 23:57:11 +03:00
|
|
|
try {
|
|
|
|
let projectId = options.infura;
|
|
|
|
let projectSecret = undefined;
|
|
|
|
if (typeof (projectId) === "object") {
|
|
|
|
projectSecret = projectId.projectSecret;
|
|
|
|
projectId = projectId.projectId;
|
|
|
|
}
|
|
|
|
providers.push(new InfuraProvider(network, projectId, projectSecret));
|
|
|
|
}
|
2023-05-19 00:26:59 +03:00
|
|
|
catch (error) { }
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
if (options.pocket !== "-") {
|
|
|
|
try {
|
|
|
|
let appId = options.pocket;
|
|
|
|
let secretKey: undefined | string = undefined;
|
|
|
|
let loadBalancer: undefined | boolean = undefined;
|
|
|
|
if (typeof(appId) === "object") {
|
|
|
|
loadBalancer = !!appId.loadBalancer;
|
|
|
|
secretKey = appId.secretKey;
|
|
|
|
appId = appId.appId;
|
|
|
|
}
|
|
|
|
providers.push(new PocketProvider(network, appId, secretKey, loadBalancer));
|
|
|
|
} catch (error) { console.log(error); }
|
|
|
|
}
|
|
|
|
*/
|
2023-07-29 09:37:03 +03:00
|
|
|
if (allowService("quicknode")) {
|
2022-12-06 06:19:14 +03:00
|
|
|
try {
|
2023-02-19 06:18:42 +03:00
|
|
|
let token = options.quicknode;
|
2022-12-06 06:19:14 +03:00
|
|
|
providers.push(new QuickNodeProvider(network, token));
|
|
|
|
}
|
2023-05-19 00:26:59 +03:00
|
|
|
catch (error) { }
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
2022-12-06 06:19:14 +03:00
|
|
|
assert(providers.length, "unsupported default network", "UNSUPPORTED_OPERATION", {
|
|
|
|
operation: "getDefaultProvider"
|
|
|
|
});
|
2023-07-29 09:37:03 +03:00
|
|
|
// No need for a FallbackProvider
|
2022-09-05 23:57:11 +03:00
|
|
|
if (providers.length === 1) {
|
|
|
|
return providers[0];
|
|
|
|
}
|
2023-07-29 09:37:03 +03:00
|
|
|
// We use the floor because public third-party providers can be unreliable,
|
|
|
|
// so a low number of providers with a large quorum will fail too often
|
|
|
|
let quorum = Math.floor(providers.length / 2);
|
|
|
|
if (quorum > 2) {
|
|
|
|
quorum = 2;
|
|
|
|
}
|
|
|
|
// Testnets don't need as strong a security gaurantee and speed is
|
|
|
|
// more useful during testing
|
|
|
|
if (staticNetwork && Testnets.indexOf(staticNetwork.name) !== -1) {
|
|
|
|
quorum = 1;
|
|
|
|
}
|
|
|
|
// Provided override qorum takes priority
|
|
|
|
if (options && options.quorum) {
|
|
|
|
quorum = options.quorum;
|
|
|
|
}
|
|
|
|
return new FallbackProvider(providers, undefined, { quorum });
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
//# sourceMappingURL=default-provider.js.map
|