ethers.js/lib.commonjs/_tests/create-provider.js

147 lines
4.4 KiB
JavaScript
Raw Permalink Normal View History

2022-09-15 22:58:45 -04:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
2022-12-30 16:35:04 -05:00
exports.connect = exports.checkProvider = exports.getProvider = exports.getProviderNetworks = exports.providerNames = exports.setupProviders = void 0;
2022-09-15 22:58:45 -04:00
const index_js_1 = require("../index.js");
;
2022-10-20 05:03:32 -04:00
const ethNetworks = ["default", "mainnet", "goerli"];
2022-09-15 22:58:45 -04:00
//const maticNetworks = [ "matic", "maticmum" ];
const ProviderCreators = [
{
name: "AlchemyProvider",
networks: ethNetworks,
create: function (network) {
return new index_js_1.AlchemyProvider(network, "YrPw6SWb20vJDRFkhWq8aKnTQ8JRNRHM");
}
},
{
name: "AnkrProvider",
networks: ethNetworks.concat(["matic", "arbitrum"]),
create: function (network) {
return new index_js_1.AnkrProvider(network);
}
},
2022-12-30 11:53:40 -05:00
/*
2022-09-15 22:58:45 -04:00
{
name: "CloudflareProvider",
2022-12-30 11:53:40 -05:00
networks: [ "default", "mainnet" ],
create: function(network: string) {
return new CloudflareProvider(network);
2022-09-15 22:58:45 -04:00
}
},
2022-12-30 11:53:40 -05:00
*/
2022-09-15 22:58:45 -04:00
{
name: "EtherscanProvider",
networks: ethNetworks,
create: function (network) {
return new index_js_1.EtherscanProvider(network, "FPFGK6JSW2UHJJ2666FG93KP7WC999MNW7");
}
},
{
name: "InfuraProvider",
networks: ethNetworks,
create: function (network) {
return new index_js_1.InfuraProvider(network, "49a0efa3aaee4fd99797bfa94d8ce2f1");
}
},
2022-09-29 22:57:27 -04:00
{
name: "InfuraWebsocketProvider",
networks: ethNetworks,
create: function (network) {
return index_js_1.InfuraProvider.getWebSocketProvider(network, "49a0efa3aaee4fd99797bfa94d8ce2f1");
}
},
2022-09-15 22:58:45 -04:00
{
name: "PocketProvider",
networks: ethNetworks,
2023-02-02 04:05:47 -05:00
create: function (network) {
return new index_js_1.PocketProvider(network);
2022-09-15 22:58:45 -04:00
}
},
2022-12-05 22:19:14 -05:00
{
name: "QuickNodeProvider",
networks: ethNetworks,
create: function (network) {
return new index_js_1.QuickNodeProvider(network);
}
},
2022-11-09 02:57:02 -05:00
{
name: "FallbackProvider",
networks: ethNetworks,
create: function (network) {
const providers = [];
for (const providerName of ["AlchemyProvider", "AnkrProvider", "EtherscanProvider", "InfuraProvider"]) {
const provider = getProvider(providerName, network);
if (provider) {
providers.push(provider);
2022-09-15 22:58:45 -04:00
}
}
2022-11-09 02:57:02 -05:00
if (providers.length === 0) {
throw new Error("UNSUPPORTED NETWORK");
}
return new index_js_1.FallbackProvider(providers);
}
},
2022-09-15 22:58:45 -04:00
];
2022-12-30 16:35:04 -05:00
let setup = false;
const cleanup = [];
function setupProviders() {
after(function () {
for (const func of cleanup) {
func();
}
});
setup = true;
}
exports.setupProviders = setupProviders;
2022-09-15 22:58:45 -04:00
exports.providerNames = Object.freeze(ProviderCreators.map((c) => (c.name)));
function getCreator(provider) {
const creators = ProviderCreators.filter((c) => (c.name === provider));
if (creators.length === 1) {
return creators[0];
}
return null;
}
function getProviderNetworks(provider) {
const creator = getCreator(provider);
if (creator) {
return creator.networks;
}
return [];
}
exports.getProviderNetworks = getProviderNetworks;
function getProvider(provider, network) {
2022-12-30 16:35:04 -05:00
if (setup == false) {
throw new Error("MUST CALL setupProviders in root context");
}
2022-09-15 22:58:45 -04:00
const creator = getCreator(provider);
2022-10-20 05:03:32 -04:00
try {
if (creator) {
2022-12-30 16:35:04 -05:00
const provider = creator.create(network);
if (provider) {
cleanup.push(() => { provider.destroy(); });
}
return provider;
2022-10-20 05:03:32 -04:00
}
}
catch (error) {
if (!(0, index_js_1.isError)(error, "INVALID_ARGUMENT")) {
throw error;
}
2022-09-15 22:58:45 -04:00
}
return null;
}
exports.getProvider = getProvider;
2022-09-29 22:57:27 -04:00
function checkProvider(provider, network) {
const creator = getCreator(provider);
return (creator != null);
}
exports.checkProvider = checkProvider;
2022-09-15 22:58:45 -04:00
function connect(network) {
const provider = getProvider("InfuraProvider", network);
if (provider == null) {
throw new Error(`could not connect to ${network}`);
}
return provider;
}
exports.connect = connect;
//# sourceMappingURL=create-provider.js.map