UNPKG

4.06 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11import { defineReadOnly, getStatic } from "@ethersproject/properties";
12import { Logger } from "@ethersproject/logger";
13import { version } from "./_version";
14const logger = new Logger(version);
15import { JsonRpcProvider } from "./json-rpc-provider";
16// A StaticJsonRpcProvider is useful when you *know* for certain that
17// the backend will never change, as it never calls eth_chainId to
18// verify its backend. However, if the backend does change, the effects
19// are undefined and may include:
20// - inconsistent results
21// - locking up the UI
22// - block skew warnings
23// - wrong results
24// If the network is not explicit (i.e. auto-detection is expected), the
25// node MUST be running and available to respond to requests BEFORE this
26// is instantiated.
27export class StaticJsonRpcProvider extends JsonRpcProvider {
28 detectNetwork() {
29 const _super = Object.create(null, {
30 detectNetwork: { get: () => super.detectNetwork }
31 });
32 return __awaiter(this, void 0, void 0, function* () {
33 let network = this.network;
34 if (network == null) {
35 network = yield _super.detectNetwork.call(this);
36 if (!network) {
37 logger.throwError("no network detected", Logger.errors.UNKNOWN_ERROR, {});
38 }
39 // If still not set, set it
40 if (this._network == null) {
41 // A static network does not support "any"
42 defineReadOnly(this, "_network", network);
43 this.emit("network", network, null);
44 }
45 }
46 return network;
47 });
48 }
49}
50export class UrlJsonRpcProvider extends StaticJsonRpcProvider {
51 constructor(network, apiKey) {
52 logger.checkAbstract(new.target, UrlJsonRpcProvider);
53 // Normalize the Network and API Key
54 network = getStatic(new.target, "getNetwork")(network);
55 apiKey = getStatic(new.target, "getApiKey")(apiKey);
56 const connection = getStatic(new.target, "getUrl")(network, apiKey);
57 super(connection, network);
58 if (typeof (apiKey) === "string") {
59 defineReadOnly(this, "apiKey", apiKey);
60 }
61 else if (apiKey != null) {
62 Object.keys(apiKey).forEach((key) => {
63 defineReadOnly(this, key, apiKey[key]);
64 });
65 }
66 }
67 _startPending() {
68 logger.warn("WARNING: API provider does not support pending filters");
69 }
70 isCommunityResource() {
71 return false;
72 }
73 getSigner(address) {
74 return logger.throwError("API provider does not support signing", Logger.errors.UNSUPPORTED_OPERATION, { operation: "getSigner" });
75 }
76 listAccounts() {
77 return Promise.resolve([]);
78 }
79 // Return a defaultApiKey if null, otherwise validate the API key
80 static getApiKey(apiKey) {
81 return apiKey;
82 }
83 // Returns the url or connection for the given network and API key. The
84 // API key will have been sanitized by the getApiKey first, so any validation
85 // or transformations can be done there.
86 static getUrl(network, apiKey) {
87 return logger.throwError("not implemented; sub-classes must override getUrl", Logger.errors.NOT_IMPLEMENTED, {
88 operation: "getUrl"
89 });
90 }
91}
92//# sourceMappingURL=url-json-rpc-provider.js.map
\No newline at end of file