UNPKG

3.45 kBJavaScriptView Raw
1"use strict";
2import { defineReadOnly } from "@ethersproject/properties";
3import { showThrottleMessage } from "./formatter";
4import { WebSocketProvider } from "./websocket-provider";
5import { Logger } from "@ethersproject/logger";
6import { version } from "./_version";
7const logger = new Logger(version);
8import { UrlJsonRpcProvider } from "./url-json-rpc-provider";
9// This key was provided to ethers.js by Alchemy to be used by the
10// default provider, but it is recommended that for your own
11// production environments, that you acquire your own API key at:
12// https://dashboard.alchemyapi.io
13const defaultApiKey = "_gg7wSSi0KMBsdKnGVfHDueq6xMB9EkC";
14export class AlchemyWebSocketProvider extends WebSocketProvider {
15 constructor(network, apiKey) {
16 const provider = new AlchemyProvider(network, apiKey);
17 const url = provider.connection.url.replace(/^http/i, "ws")
18 .replace(".alchemyapi.", ".ws.alchemyapi.");
19 super(url, provider.network);
20 defineReadOnly(this, "apiKey", provider.apiKey);
21 }
22 isCommunityResource() {
23 return (this.apiKey === defaultApiKey);
24 }
25}
26export class AlchemyProvider extends UrlJsonRpcProvider {
27 static getWebSocketProvider(network, apiKey) {
28 return new AlchemyWebSocketProvider(network, apiKey);
29 }
30 static getApiKey(apiKey) {
31 if (apiKey == null) {
32 return defaultApiKey;
33 }
34 if (apiKey && typeof (apiKey) !== "string") {
35 logger.throwArgumentError("invalid apiKey", "apiKey", apiKey);
36 }
37 return apiKey;
38 }
39 static getUrl(network, apiKey) {
40 let host = null;
41 switch (network.name) {
42 case "homestead":
43 host = "eth-mainnet.alchemyapi.io/v2/";
44 break;
45 case "ropsten":
46 host = "eth-ropsten.alchemyapi.io/v2/";
47 break;
48 case "rinkeby":
49 host = "eth-rinkeby.alchemyapi.io/v2/";
50 break;
51 case "goerli":
52 host = "eth-goerli.alchemyapi.io/v2/";
53 break;
54 case "kovan":
55 host = "eth-kovan.alchemyapi.io/v2/";
56 break;
57 case "matic":
58 host = "polygon-mainnet.g.alchemy.com/v2/";
59 break;
60 case "maticmum":
61 host = "polygon-mumbai.g.alchemy.com/v2/";
62 break;
63 case "arbitrum":
64 host = "arb-mainnet.g.alchemy.com/v2/";
65 break;
66 case "arbitrum-rinkeby":
67 host = "arb-rinkeby.g.alchemy.com/v2/";
68 break;
69 case "optimism":
70 host = "opt-mainnet.g.alchemy.com/v2/";
71 break;
72 case "optimism-kovan":
73 host = "opt-kovan.g.alchemy.com/v2/";
74 break;
75 default:
76 logger.throwArgumentError("unsupported network", "network", arguments[0]);
77 }
78 return {
79 allowGzip: true,
80 url: ("https:/" + "/" + host + apiKey),
81 throttleCallback: (attempt, url) => {
82 if (apiKey === defaultApiKey) {
83 showThrottleMessage();
84 }
85 return Promise.resolve(true);
86 }
87 };
88 }
89 isCommunityResource() {
90 return (this.apiKey === defaultApiKey);
91 }
92}
93//# sourceMappingURL=alchemy-provider.js.map
\No newline at end of file