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

124 lines
3.8 KiB
JavaScript
Raw Normal View History

2022-10-20 12:03:32 +03:00
import { AlchemyProvider, AnkrProvider, CloudflareProvider, EtherscanProvider, InfuraProvider,
2022-09-16 05:58:45 +03:00
// PocketProvider,
2022-09-30 05:57:27 +03:00
// FallbackProvider,
2022-10-20 12:03:32 +03:00
isError, } from "../index.js";
2022-09-16 05:58:45 +03:00
;
2022-10-20 12:03:32 +03:00
const ethNetworks = ["default", "mainnet", "goerli"];
2022-09-16 05:58:45 +03:00
//const maticNetworks = [ "matic", "maticmum" ];
const ProviderCreators = [
{
name: "AlchemyProvider",
networks: ethNetworks,
create: function (network) {
return new AlchemyProvider(network, "YrPw6SWb20vJDRFkhWq8aKnTQ8JRNRHM");
}
},
{
name: "AnkrProvider",
networks: ethNetworks.concat(["matic", "arbitrum"]),
create: function (network) {
return new AnkrProvider(network);
}
},
{
name: "CloudflareProvider",
2022-09-30 05:57:27 +03:00
networks: ["default", "mainnet"],
2022-09-16 05:58:45 +03:00
create: function (network) {
return new CloudflareProvider(network);
}
},
{
name: "EtherscanProvider",
networks: ethNetworks,
create: function (network) {
return new EtherscanProvider(network, "FPFGK6JSW2UHJJ2666FG93KP7WC999MNW7");
}
},
{
name: "InfuraProvider",
networks: ethNetworks,
create: function (network) {
return new InfuraProvider(network, "49a0efa3aaee4fd99797bfa94d8ce2f1");
}
},
2022-09-30 05:57:27 +03:00
{
name: "InfuraWebsocketProvider",
networks: ethNetworks,
create: function (network) {
return InfuraProvider.getWebSocketProvider(network, "49a0efa3aaee4fd99797bfa94d8ce2f1");
}
},
2022-09-16 05:58:45 +03:00
/*
{
name: "PocketProvider",
networks: ethNetworks,
create: function(network: string) {
const apiKeys: Record<string, string> = {
2022-09-30 05:57:27 +03:00
mainnet: "6004bcd10040261633ade990",
2022-09-16 05:58:45 +03:00
ropsten: "6004bd4d0040261633ade991",
rinkeby: "6004bda20040261633ade994",
goerli: "6004bd860040261633ade992",
};
return new PocketProvider(network, apiKeys[network]);
}
},
*/
2022-09-30 05:57:27 +03:00
/*
{
name: "FallbackProvider",
networks: ethNetworks,
create: function(network: string) {
const providers: Array<AbstractProvider> = [];
for (const creator of ProviderCreators) {
if (creator.name === "FallbackProvider") { continue; }
if (creator.networks.indexOf(network) >= 0) {
const provider = creator.create(network);
if (provider) { providers.push(provider); }
2022-09-16 05:58:45 +03:00
}
}
2022-09-30 05:57:27 +03:00
return new FallbackProvider(providers);
2022-09-16 05:58:45 +03:00
}
2022-09-30 05:57:27 +03:00
},
*/
2022-09-16 05:58:45 +03:00
];
export const 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;
}
export function getProviderNetworks(provider) {
const creator = getCreator(provider);
if (creator) {
return creator.networks;
}
return [];
}
export function getProvider(provider, network) {
const creator = getCreator(provider);
2022-10-20 12:03:32 +03:00
try {
if (creator) {
return creator.create(network);
}
}
catch (error) {
if (!isError(error, "INVALID_ARGUMENT")) {
throw error;
}
2022-09-16 05:58:45 +03:00
}
return null;
}
2022-09-30 05:57:27 +03:00
export function checkProvider(provider, network) {
const creator = getCreator(provider);
return (creator != null);
}
2022-09-16 05:58:45 +03:00
export function connect(network) {
const provider = getProvider("InfuraProvider", network);
if (provider == null) {
throw new Error(`could not connect to ${network}`);
}
return provider;
}
//# sourceMappingURL=create-provider.js.map