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