UNPKG

3.68 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 "arbitrum-goerli":
70 host = "arb-goerli.g.alchemy.com/v2/";
71 break;
72 case "optimism":
73 host = "opt-mainnet.g.alchemy.com/v2/";
74 break;
75 case "optimism-kovan":
76 host = "opt-kovan.g.alchemy.com/v2/";
77 break;
78 case "optimism-goerli":
79 host = "opt-goerli.g.alchemy.com/v2/";
80 break;
81 default:
82 logger.throwArgumentError("unsupported network", "network", arguments[0]);
83 }
84 return {
85 allowGzip: true,
86 url: ("https:/" + "/" + host + apiKey),
87 throttleCallback: (attempt, url) => {
88 if (apiKey === defaultApiKey) {
89 showThrottleMessage();
90 }
91 return Promise.resolve(true);
92 }
93 };
94 }
95 isCommunityResource() {
96 return (this.apiKey === defaultApiKey);
97 }
98}
99//# sourceMappingURL=alchemy-provider.js.map
\No newline at end of file