1 | "use strict";
|
2 | import { getStatic } from "@ethersproject/properties";
|
3 | import { Logger } from "@ethersproject/logger";
|
4 | import { version } from "./_version";
|
5 | const logger = new Logger(version);
|
6 | import { UrlJsonRpcProvider } from "./url-json-rpc-provider";
|
7 |
|
8 | const defaultApplicationIds = {
|
9 | homestead: "6004bcd10040261633ade990",
|
10 | ropsten: "6004bd4d0040261633ade991",
|
11 | rinkeby: "6004bda20040261633ade994",
|
12 | goerli: "6004bd860040261633ade992",
|
13 | };
|
14 | export class PocketProvider extends UrlJsonRpcProvider {
|
15 | constructor(network, apiKey) {
|
16 |
|
17 |
|
18 | if (apiKey == null) {
|
19 | const n = getStatic(new.target, "getNetwork")(network);
|
20 | if (n) {
|
21 | const applicationId = defaultApplicationIds[n.name];
|
22 | if (applicationId) {
|
23 | apiKey = {
|
24 | applicationId: applicationId,
|
25 | loadBalancer: true
|
26 | };
|
27 | }
|
28 | }
|
29 |
|
30 | if (apiKey == null) {
|
31 | logger.throwError("unsupported network", Logger.errors.INVALID_ARGUMENT, {
|
32 | argument: "network",
|
33 | value: network
|
34 | });
|
35 | }
|
36 | }
|
37 | super(network, apiKey);
|
38 | }
|
39 | static getApiKey(apiKey) {
|
40 |
|
41 |
|
42 |
|
43 | if (apiKey == null) {
|
44 | logger.throwArgumentError("PocketProvider.getApiKey does not support null apiKey", "apiKey", apiKey);
|
45 | }
|
46 | const apiKeyObj = {
|
47 | applicationId: null,
|
48 | loadBalancer: false,
|
49 | applicationSecretKey: null
|
50 | };
|
51 |
|
52 | if (typeof (apiKey) === "string") {
|
53 | apiKeyObj.applicationId = apiKey;
|
54 | }
|
55 | else if (apiKey.applicationSecretKey != null) {
|
56 | logger.assertArgument((typeof (apiKey.applicationId) === "string"), "applicationSecretKey requires an applicationId", "applicationId", apiKey.applicationId);
|
57 | logger.assertArgument((typeof (apiKey.applicationSecretKey) === "string"), "invalid applicationSecretKey", "applicationSecretKey", "[REDACTED]");
|
58 | apiKeyObj.applicationId = apiKey.applicationId;
|
59 | apiKeyObj.applicationSecretKey = apiKey.applicationSecretKey;
|
60 | apiKeyObj.loadBalancer = !!apiKey.loadBalancer;
|
61 | }
|
62 | else if (apiKey.applicationId) {
|
63 | logger.assertArgument((typeof (apiKey.applicationId) === "string"), "apiKey.applicationId must be a string", "apiKey.applicationId", apiKey.applicationId);
|
64 | apiKeyObj.applicationId = apiKey.applicationId;
|
65 | apiKeyObj.loadBalancer = !!apiKey.loadBalancer;
|
66 | }
|
67 | else {
|
68 | logger.throwArgumentError("unsupported PocketProvider apiKey", "apiKey", apiKey);
|
69 | }
|
70 | return apiKeyObj;
|
71 | }
|
72 | static getUrl(network, apiKey) {
|
73 | let host = null;
|
74 | switch (network ? network.name : "unknown") {
|
75 | case "homestead":
|
76 | host = "eth-mainnet.gateway.pokt.network";
|
77 | break;
|
78 | case "ropsten":
|
79 | host = "eth-ropsten.gateway.pokt.network";
|
80 | break;
|
81 | case "rinkeby":
|
82 | host = "eth-rinkeby.gateway.pokt.network";
|
83 | break;
|
84 | case "goerli":
|
85 | host = "eth-goerli.gateway.pokt.network";
|
86 | break;
|
87 | default:
|
88 | logger.throwError("unsupported network", Logger.errors.INVALID_ARGUMENT, {
|
89 | argument: "network",
|
90 | value: network
|
91 | });
|
92 | }
|
93 | let url = null;
|
94 | if (apiKey.loadBalancer) {
|
95 | url = `https:/\/${host}/v1/lb/${apiKey.applicationId}`;
|
96 | }
|
97 | else {
|
98 | url = `https:/\/${host}/v1/${apiKey.applicationId}`;
|
99 | }
|
100 | const connection = { url };
|
101 |
|
102 | connection.headers = {};
|
103 |
|
104 | if (apiKey.applicationSecretKey != null) {
|
105 | connection.user = "";
|
106 | connection.password = apiKey.applicationSecretKey;
|
107 | }
|
108 | return connection;
|
109 | }
|
110 | isCommunityResource() {
|
111 | return (this.applicationId === defaultApplicationIds[this.network.name]);
|
112 | }
|
113 | }
|
114 |
|
\ | No newline at end of file |