1 |
|
2 | import { Network } from "@ethersproject/networks";
|
3 |
|
4 | import { showThrottleMessage } from "./formatter";
|
5 | import { UrlJsonRpcProvider } from "./url-json-rpc-provider";
|
6 |
|
7 | import type { ConnectionInfo } from "@ethersproject/web";
|
8 |
|
9 | import { Logger } from "@ethersproject/logger";
|
10 | import { version } from "./_version";
|
11 | const logger = new Logger(version);
|
12 |
|
13 |
|
14 | const defaultApiKey = "9f7d929b018cdffb338517efa06f58359e86ff1ffd350bc889738523659e7972";
|
15 |
|
16 | function getHost(name: string): string {
|
17 | switch (name) {
|
18 | case "homestead":
|
19 | return "rpc.ankr.com/eth/";
|
20 | case "ropsten":
|
21 | return "rpc.ankr.com/eth_ropsten/";
|
22 | case "rinkeby":
|
23 | return "rpc.ankr.com/eth_rinkeby/";
|
24 | case "goerli":
|
25 | return "rpc.ankr.com/eth_goerli/";
|
26 |
|
27 | case "matic":
|
28 | return "rpc.ankr.com/polygon/";
|
29 |
|
30 | case "arbitrum":
|
31 | return "rpc.ankr.com/arbitrum/";
|
32 | }
|
33 | return logger.throwArgumentError("unsupported network", "name", name);
|
34 | }
|
35 |
|
36 | export class AnkrProvider extends UrlJsonRpcProvider {
|
37 | readonly apiKey: string;
|
38 |
|
39 | isCommunityResource(): boolean {
|
40 | return (this.apiKey === defaultApiKey);
|
41 | }
|
42 |
|
43 | static getApiKey(apiKey: any): any {
|
44 | if (apiKey == null) { return defaultApiKey; }
|
45 | return apiKey;
|
46 | }
|
47 |
|
48 | static getUrl(network: Network, apiKey: any): ConnectionInfo {
|
49 | if (apiKey == null) { apiKey = defaultApiKey; }
|
50 | const connection: ConnectionInfo = {
|
51 | allowGzip: true,
|
52 | url: ("https:/\/" + getHost(network.name) + apiKey),
|
53 | throttleCallback: (attempt: number, url: string) => {
|
54 | if (apiKey.apiKey === defaultApiKey) {
|
55 | showThrottleMessage();
|
56 | }
|
57 | return Promise.resolve(true);
|
58 | }
|
59 | };
|
60 |
|
61 | if (apiKey.projectSecret != null) {
|
62 | connection.user = "";
|
63 | connection.password = apiKey.projectSecret
|
64 | }
|
65 |
|
66 | return connection;
|
67 | }
|
68 | }
|