2023-03-20 22:53:36 +03:00
|
|
|
import { AlchemyProvider,
|
|
|
|
// AnkrProvider,
|
2022-12-30 19:53:40 +03:00
|
|
|
// CloudflareProvider,
|
2023-06-06 05:41:31 +03:00
|
|
|
EtherscanProvider, InfuraProvider,
|
|
|
|
// PocketProvider,
|
|
|
|
QuickNodeProvider, FallbackProvider, 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");
|
|
|
|
}
|
|
|
|
},
|
2023-03-20 22:53:36 +03:00
|
|
|
/*
|
2022-09-16 05:58:45 +03:00
|
|
|
{
|
|
|
|
name: "AnkrProvider",
|
2023-03-20 22:53:36 +03:00
|
|
|
networks: ethNetworks.concat([ "matic", "arbitrum" ]),
|
|
|
|
create: function(network: string) {
|
2022-09-16 05:58:45 +03:00
|
|
|
return new AnkrProvider(network);
|
|
|
|
}
|
|
|
|
},
|
2023-03-20 22:53:36 +03:00
|
|
|
*/
|
2022-12-30 19:53:40 +03:00
|
|
|
/*
|
2022-09-16 05:58:45 +03:00
|
|
|
{
|
|
|
|
name: "CloudflareProvider",
|
2022-12-30 19:53:40 +03:00
|
|
|
networks: [ "default", "mainnet" ],
|
|
|
|
create: function(network: string) {
|
2022-09-16 05:58:45 +03:00
|
|
|
return new CloudflareProvider(network);
|
|
|
|
}
|
|
|
|
},
|
2022-12-30 19:53:40 +03:00
|
|
|
*/
|
2022-09-16 05:58:45 +03:00
|
|
|
{
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
},
|
2023-06-06 05:41:31 +03:00
|
|
|
/*
|
|
|
|
{
|
|
|
|
name: "PocketProvider",
|
|
|
|
networks: ethNetworks,
|
|
|
|
create: function(network: string) {
|
|
|
|
return new PocketProvider(network);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
*/
|
2022-12-06 06:19:14 +03:00
|
|
|
{
|
|
|
|
name: "QuickNodeProvider",
|
|
|
|
networks: ethNetworks,
|
|
|
|
create: function (network) {
|
|
|
|
return new QuickNodeProvider(network);
|
|
|
|
}
|
|
|
|
},
|
2022-11-09 10:57:02 +03: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-16 05:58:45 +03:00
|
|
|
}
|
|
|
|
}
|
2022-11-09 10:57:02 +03:00
|
|
|
if (providers.length === 0) {
|
|
|
|
throw new Error("UNSUPPORTED NETWORK");
|
|
|
|
}
|
|
|
|
return new FallbackProvider(providers);
|
|
|
|
}
|
|
|
|
},
|
2022-09-16 05:58:45 +03:00
|
|
|
];
|
2022-12-31 00:35:04 +03:00
|
|
|
let setup = false;
|
|
|
|
const cleanup = [];
|
|
|
|
export function setupProviders() {
|
|
|
|
after(function () {
|
|
|
|
for (const func of cleanup) {
|
|
|
|
func();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
setup = true;
|
|
|
|
}
|
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) {
|
2022-12-31 00:35:04 +03:00
|
|
|
if (setup == false) {
|
|
|
|
throw new Error("MUST CALL setupProviders in root context");
|
|
|
|
}
|
2022-09-16 05:58:45 +03:00
|
|
|
const creator = getCreator(provider);
|
2022-10-20 12:03:32 +03:00
|
|
|
try {
|
|
|
|
if (creator) {
|
2022-12-31 00:35:04 +03:00
|
|
|
const provider = creator.create(network);
|
|
|
|
if (provider) {
|
|
|
|
cleanup.push(() => { provider.destroy(); });
|
|
|
|
}
|
|
|
|
return provider;
|
2022-10-20 12:03:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|