2022-09-15 22:20:48 -04:00
|
|
|
import {
|
|
|
|
AlchemyProvider,
|
2023-03-20 14:43:20 -04:00
|
|
|
// AnkrProvider,
|
2022-12-30 11:52:54 -05:00
|
|
|
// CloudflareProvider,
|
2022-09-15 22:20:48 -04:00
|
|
|
EtherscanProvider,
|
|
|
|
InfuraProvider,
|
2023-02-02 03:06:15 -05:00
|
|
|
PocketProvider,
|
2022-12-05 22:17:41 -05:00
|
|
|
QuickNodeProvider,
|
2022-09-15 22:20:48 -04:00
|
|
|
|
2022-11-04 18:11:38 -04:00
|
|
|
FallbackProvider,
|
2022-10-20 05:00:49 -04:00
|
|
|
isError,
|
2022-09-15 22:20:48 -04:00
|
|
|
} from "../index.js";
|
|
|
|
|
|
|
|
import type { AbstractProvider } from "../index.js";
|
|
|
|
|
|
|
|
interface ProviderCreator {
|
|
|
|
name: string;
|
|
|
|
networks: Array<string>;
|
|
|
|
create: (network: string) => null | AbstractProvider;
|
|
|
|
};
|
|
|
|
|
2022-10-20 05:00:49 -04:00
|
|
|
const ethNetworks = [ "default", "mainnet", "goerli" ];
|
2022-09-15 22:20:48 -04:00
|
|
|
//const maticNetworks = [ "matic", "maticmum" ];
|
|
|
|
|
|
|
|
const ProviderCreators: Array<ProviderCreator> = [
|
|
|
|
{
|
|
|
|
name: "AlchemyProvider",
|
|
|
|
networks: ethNetworks,
|
|
|
|
create: function(network: string) {
|
|
|
|
return new AlchemyProvider(network, "YrPw6SWb20vJDRFkhWq8aKnTQ8JRNRHM");
|
|
|
|
}
|
|
|
|
},
|
2023-03-20 14:43:20 -04:00
|
|
|
/*
|
2022-09-15 22:20:48 -04:00
|
|
|
{
|
|
|
|
name: "AnkrProvider",
|
|
|
|
networks: ethNetworks.concat([ "matic", "arbitrum" ]),
|
|
|
|
create: function(network: string) {
|
|
|
|
return new AnkrProvider(network);
|
|
|
|
}
|
|
|
|
},
|
2023-03-20 14:43:20 -04:00
|
|
|
*/
|
2022-12-30 11:52:54 -05:00
|
|
|
/*
|
2022-09-15 22:20:48 -04:00
|
|
|
{
|
|
|
|
name: "CloudflareProvider",
|
2022-09-29 21:56:05 -04:00
|
|
|
networks: [ "default", "mainnet" ],
|
2022-09-15 22:20:48 -04:00
|
|
|
create: function(network: string) {
|
|
|
|
return new CloudflareProvider(network);
|
|
|
|
}
|
|
|
|
},
|
2022-12-30 11:52:54 -05:00
|
|
|
*/
|
2022-09-15 22:20:48 -04:00
|
|
|
{
|
|
|
|
name: "EtherscanProvider",
|
|
|
|
networks: ethNetworks,
|
|
|
|
create: function(network: string) {
|
|
|
|
return new EtherscanProvider(network, "FPFGK6JSW2UHJJ2666FG93KP7WC999MNW7");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "InfuraProvider",
|
|
|
|
networks: ethNetworks,
|
|
|
|
create: function(network: string) {
|
|
|
|
return new InfuraProvider(network, "49a0efa3aaee4fd99797bfa94d8ce2f1");
|
|
|
|
}
|
|
|
|
},
|
2022-09-29 21:56:05 -04:00
|
|
|
{
|
|
|
|
name: "InfuraWebsocketProvider",
|
|
|
|
networks: ethNetworks,
|
|
|
|
create: function(network: string) {
|
|
|
|
return InfuraProvider.getWebSocketProvider(network, "49a0efa3aaee4fd99797bfa94d8ce2f1");
|
|
|
|
}
|
|
|
|
},
|
2022-09-15 22:20:48 -04:00
|
|
|
{
|
|
|
|
name: "PocketProvider",
|
|
|
|
networks: ethNetworks,
|
|
|
|
create: function(network: string) {
|
2023-02-02 03:06:15 -05:00
|
|
|
return new PocketProvider(network);
|
2022-09-15 22:20:48 -04:00
|
|
|
}
|
|
|
|
},
|
2022-12-05 22:17:41 -05:00
|
|
|
{
|
|
|
|
name: "QuickNodeProvider",
|
|
|
|
networks: ethNetworks,
|
|
|
|
create: function(network: string) {
|
|
|
|
return new QuickNodeProvider(network);
|
|
|
|
}
|
|
|
|
},
|
2022-09-15 22:20:48 -04:00
|
|
|
{
|
|
|
|
name: "FallbackProvider",
|
|
|
|
networks: ethNetworks,
|
|
|
|
create: function(network: string) {
|
|
|
|
const providers: Array<AbstractProvider> = [];
|
2022-11-04 18:11:38 -04:00
|
|
|
for (const providerName of [ "AlchemyProvider", "AnkrProvider", "EtherscanProvider", "InfuraProvider" ]) {
|
|
|
|
const provider = getProvider(providerName, network);
|
|
|
|
if (provider) { providers.push(provider); }
|
2022-09-15 22:20:48 -04:00
|
|
|
}
|
2022-11-04 18:11:38 -04:00
|
|
|
if (providers.length === 0) { throw new Error("UNSUPPORTED NETWORK"); }
|
2022-09-15 22:20:48 -04:00
|
|
|
return new FallbackProvider(providers);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2022-12-30 16:33:28 -05:00
|
|
|
let setup = false;
|
|
|
|
const cleanup: Array<() => void> = [ ];
|
|
|
|
export function setupProviders(): void {
|
|
|
|
after(function() {
|
|
|
|
for (const func of cleanup) { func(); }
|
|
|
|
});
|
|
|
|
setup = true;
|
|
|
|
}
|
|
|
|
|
2022-09-15 22:20:48 -04:00
|
|
|
export const providerNames = Object.freeze(ProviderCreators.map((c) => (c.name)));
|
|
|
|
|
|
|
|
function getCreator(provider: string): null | ProviderCreator {
|
|
|
|
const creators = ProviderCreators.filter((c) => (c.name === provider));
|
|
|
|
if (creators.length === 1) { return creators[0]; }
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getProviderNetworks(provider: string): Array<string> {
|
|
|
|
const creator = getCreator(provider);
|
|
|
|
if (creator) { return creator.networks; }
|
|
|
|
return [ ];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getProvider(provider: string, network: string): null | AbstractProvider {
|
2022-12-30 16:33:28 -05:00
|
|
|
if (setup == false) { throw new Error("MUST CALL setupProviders in root context"); }
|
|
|
|
|
2022-09-15 22:20:48 -04:00
|
|
|
const creator = getCreator(provider);
|
2022-10-20 05:00:49 -04:00
|
|
|
try {
|
2022-12-30 16:33:28 -05:00
|
|
|
if (creator) {
|
|
|
|
const provider = creator.create(network);
|
|
|
|
if (provider) {
|
|
|
|
cleanup.push(() => { provider.destroy(); });
|
|
|
|
}
|
|
|
|
return provider;
|
|
|
|
}
|
2022-10-20 05:00:49 -04:00
|
|
|
} catch (error) {
|
|
|
|
if (!isError(error, "INVALID_ARGUMENT")) { throw error; }
|
|
|
|
}
|
2022-09-15 22:20:48 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-09-29 21:56:05 -04:00
|
|
|
export function checkProvider(provider: string, network: string): boolean {
|
|
|
|
const creator = getCreator(provider);
|
|
|
|
return (creator != null);
|
|
|
|
}
|
|
|
|
|
2022-09-15 22:20:48 -04:00
|
|
|
export function connect(network: string): AbstractProvider {
|
|
|
|
const provider = getProvider("InfuraProvider", network);
|
|
|
|
if (provider == null) { throw new Error(`could not connect to ${ network }`); }
|
|
|
|
return provider;
|
|
|
|
}
|