ethers.js/packages/providers/src.ts/alchemy-provider.ts

102 lines
3.4 KiB
TypeScript
Raw Permalink Normal View History

2019-05-14 18:25:46 -04:00
"use strict";
import { Network, Networkish } from "@ethersproject/networks";
import { defineReadOnly } from "@ethersproject/properties";
import { ConnectionInfo } from "@ethersproject/web";
import { CommunityResourcable, showThrottleMessage } from "./formatter";
import { WebSocketProvider } from "./websocket-provider";
2019-05-14 18:25:46 -04:00
2019-08-01 18:04:06 -04:00
import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);
2019-05-14 18:25:46 -04:00
import { UrlJsonRpcProvider } from "./url-json-rpc-provider";
// This key was provided to ethers.js by Alchemy to be used by the
// default provider, but it is recommended that for your own
// production environments, that you acquire your own API key at:
// https://dashboard.alchemyapi.io
const defaultApiKey = "_gg7wSSi0KMBsdKnGVfHDueq6xMB9EkC"
export class AlchemyWebSocketProvider extends WebSocketProvider implements CommunityResourcable {
readonly apiKey: string;
2019-05-14 18:25:46 -04:00
constructor(network?: Networkish, apiKey?: any) {
const provider = new AlchemyProvider(network, apiKey);
const url = provider.connection.url.replace(/^http/i, "ws")
.replace(".alchemyapi.", ".ws.alchemyapi.");
super(url, provider.network);
defineReadOnly(this, "apiKey", provider.apiKey);
}
isCommunityResource(): boolean {
return (this.apiKey === defaultApiKey);
}
}
export class AlchemyProvider extends UrlJsonRpcProvider {
static getWebSocketProvider(network?: Networkish, apiKey?: any): AlchemyWebSocketProvider {
return new AlchemyWebSocketProvider(network, apiKey);
}
static getApiKey(apiKey: any): any {
2019-05-14 18:25:46 -04:00
if (apiKey == null) { return defaultApiKey; }
if (apiKey && typeof(apiKey) !== "string") {
logger.throwArgumentError("invalid apiKey", "apiKey", apiKey);
}
2019-05-14 18:25:46 -04:00
return apiKey;
}
static getUrl(network: Network, apiKey: string): ConnectionInfo {
2019-05-14 18:25:46 -04:00
let host = null;
switch (network.name) {
case "homestead":
host = "eth-mainnet.alchemyapi.io/v2/";
2019-05-14 18:25:46 -04:00
break;
2020-05-04 22:39:31 -04:00
case "goerli":
host = "eth-goerli.g.alchemy.com/v2/";
2019-05-14 18:25:46 -04:00
break;
case "matic":
host = "polygon-mainnet.g.alchemy.com/v2/";
break;
case "maticmum":
host = "polygon-mumbai.g.alchemy.com/v2/";
break;
case "arbitrum":
host = "arb-mainnet.g.alchemy.com/v2/";
break;
case "arbitrum-goerli":
host = "arb-goerli.g.alchemy.com/v2/";
break;
case "optimism":
host = "opt-mainnet.g.alchemy.com/v2/";
break;
case "optimism-goerli":
host = "opt-goerli.g.alchemy.com/v2/"
break;
2019-05-14 18:25:46 -04:00
default:
2019-08-01 18:04:06 -04:00
logger.throwArgumentError("unsupported network", "network", arguments[0]);
2019-05-14 18:25:46 -04:00
}
return {
allowGzip: true,
url: ("https:/" + "/" + host + apiKey),
throttleCallback: (attempt: number, url: string) => {
if (apiKey === defaultApiKey) {
showThrottleMessage();
}
return Promise.resolve(true);
}
};
2019-05-14 18:25:46 -04:00
}
isCommunityResource(): boolean {
return (this.apiKey === defaultApiKey);
}
2019-05-14 18:25:46 -04:00
}