UNPKG

2 kBPlain TextView Raw
1
2import { Network } from "@ethersproject/networks";
3
4import { showThrottleMessage } from "./formatter";
5import { UrlJsonRpcProvider } from "./url-json-rpc-provider";
6
7import type { ConnectionInfo } from "@ethersproject/web";
8
9import { Logger } from "@ethersproject/logger";
10import { version } from "./_version";
11const logger = new Logger(version);
12
13
14const defaultApiKey = "9f7d929b018cdffb338517efa06f58359e86ff1ffd350bc889738523659e7972";
15
16function 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
36export 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}