2019-05-15 01:25:46 +03:00
|
|
|
"use strict";
|
|
|
|
|
2020-07-03 08:41:32 +03:00
|
|
|
import { Network, Networkish } from "@ethersproject/networks";
|
2020-10-08 00:28:18 +03:00
|
|
|
import { defineReadOnly } from "@ethersproject/properties";
|
2020-07-14 09:26:45 +03:00
|
|
|
import { ConnectionInfo } from "@ethersproject/web";
|
2020-07-03 08:41:32 +03:00
|
|
|
|
2020-10-08 00:28:18 +03:00
|
|
|
import { CommunityResourcable, showThrottleMessage } from "./formatter";
|
2020-07-03 08:41:32 +03:00
|
|
|
import { WebSocketProvider } from "./websocket-provider";
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-08-02 01:04:06 +03:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
|
2019-05-15 01:25:46 +03: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"
|
|
|
|
|
2020-10-08 00:28:18 +03:00
|
|
|
export class AlchemyWebSocketProvider extends WebSocketProvider implements CommunityResourcable {
|
|
|
|
readonly apiKey: string;
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2020-10-08 00:28:18 +03:00
|
|
|
constructor(network?: Networkish, apiKey?: any) {
|
2020-07-03 08:41:32 +03:00
|
|
|
const provider = new AlchemyProvider(network, apiKey);
|
|
|
|
|
|
|
|
const url = provider.connection.url.replace(/^http/i, "ws")
|
|
|
|
.replace(".alchemyapi.", ".ws.alchemyapi.");
|
|
|
|
|
2020-10-08 00:28:18 +03:00
|
|
|
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);
|
2020-07-03 08:41:32 +03:00
|
|
|
}
|
|
|
|
|
2019-11-19 12:00:05 +03:00
|
|
|
static getApiKey(apiKey: any): any {
|
2019-05-15 01:25:46 +03:00
|
|
|
if (apiKey == null) { return defaultApiKey; }
|
2019-11-19 12:00:05 +03:00
|
|
|
if (apiKey && typeof(apiKey) !== "string") {
|
|
|
|
logger.throwArgumentError("invalid apiKey", "apiKey", apiKey);
|
|
|
|
}
|
2019-05-15 01:25:46 +03:00
|
|
|
return apiKey;
|
|
|
|
}
|
|
|
|
|
2020-07-14 09:26:45 +03:00
|
|
|
static getUrl(network: Network, apiKey: string): ConnectionInfo {
|
2019-05-15 01:25:46 +03:00
|
|
|
let host = null;
|
|
|
|
switch (network.name) {
|
|
|
|
case "homestead":
|
2020-07-03 08:41:32 +03:00
|
|
|
host = "eth-mainnet.alchemyapi.io/v2/";
|
2019-05-15 01:25:46 +03:00
|
|
|
break;
|
|
|
|
case "ropsten":
|
2020-07-03 08:41:32 +03:00
|
|
|
host = "eth-ropsten.alchemyapi.io/v2/";
|
2019-05-15 01:25:46 +03:00
|
|
|
break;
|
|
|
|
case "rinkeby":
|
2020-07-03 08:41:32 +03:00
|
|
|
host = "eth-rinkeby.alchemyapi.io/v2/";
|
2019-05-15 01:25:46 +03:00
|
|
|
break;
|
2020-05-05 05:39:31 +03:00
|
|
|
case "goerli":
|
2020-07-03 08:41:32 +03:00
|
|
|
host = "eth-goerli.alchemyapi.io/v2/";
|
2020-05-05 05:39:31 +03:00
|
|
|
break;
|
2019-05-15 01:25:46 +03:00
|
|
|
case "kovan":
|
2020-07-03 08:41:32 +03:00
|
|
|
host = "eth-kovan.alchemyapi.io/v2/";
|
2019-05-15 01:25:46 +03:00
|
|
|
break;
|
2021-07-23 09:13:35 +03:00
|
|
|
case "matic":
|
|
|
|
host = "polygon-mainnet.g.alchemy.com/v2/";
|
|
|
|
break;
|
|
|
|
case "maticmum":
|
|
|
|
host = "polygon-mumbai.g.alchemy.com/v2/";
|
|
|
|
break;
|
2021-11-30 18:20:26 +03:00
|
|
|
case "arbitrum":
|
|
|
|
host = "arb-mainnet.g.alchemy.com/v2/";
|
|
|
|
break;
|
|
|
|
case "arbitrum-rinkeby":
|
|
|
|
host = "arb-rinkeby.g.alchemy.com/v2/";
|
|
|
|
break;
|
|
|
|
case "optimism":
|
|
|
|
host = "opt-mainnet.g.alchemy.com/v2/";
|
|
|
|
break;
|
|
|
|
case "optimism-kovan":
|
|
|
|
host = "opt-kovan.g.alchemy.com/v2/";
|
|
|
|
break;
|
2019-05-15 01:25:46 +03:00
|
|
|
default:
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.throwArgumentError("unsupported network", "network", arguments[0]);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2020-07-14 09:26:45 +03:00
|
|
|
return {
|
2020-10-08 02:43:20 +03:00
|
|
|
allowGzip: true,
|
2020-07-14 09:26:45 +03:00
|
|
|
url: ("https:/" + "/" + host + apiKey),
|
|
|
|
throttleCallback: (attempt: number, url: string) => {
|
|
|
|
if (apiKey === defaultApiKey) {
|
|
|
|
showThrottleMessage();
|
|
|
|
}
|
|
|
|
return Promise.resolve(true);
|
|
|
|
}
|
|
|
|
};
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
2020-10-08 00:28:18 +03:00
|
|
|
|
|
|
|
isCommunityResource(): boolean {
|
|
|
|
return (this.apiKey === defaultApiKey);
|
|
|
|
}
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|