UNPKG

4.79 kBJavaScriptView Raw
1"use strict";
2import { getStatic } from "@ethersproject/properties";
3import { Logger } from "@ethersproject/logger";
4import { version } from "./_version";
5const logger = new Logger(version);
6import { UrlJsonRpcProvider } from "./url-json-rpc-provider";
7// These are load-balancer-based application IDs
8const defaultApplicationIds = {
9 homestead: "6004bcd10040261633ade990",
10 ropsten: "6004bd4d0040261633ade991",
11 rinkeby: "6004bda20040261633ade994",
12 goerli: "6004bd860040261633ade992",
13};
14export class PocketProvider extends UrlJsonRpcProvider {
15 constructor(network, apiKey) {
16 // We need a bit of creativity in the constructor because
17 // Pocket uses different default API keys based on the network
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 // If there was any issue above, we don't know this network
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 // Most API Providers allow null to get the default configuration, but
41 // Pocket requires the network to decide the default provider, so we
42 // rely on hijacking the constructor to add a sensible default for us
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 // Parse applicationId and applicationSecretKey
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 // Initialize empty headers
102 connection.headers = {};
103 // Apply application secret key
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//# sourceMappingURL=pocket-provider.js.map
\No newline at end of file