-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
url-json-rpc-provider.ts
72 lines (55 loc) · 2.39 KB
/
url-json-rpc-provider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"use strict";
import { Network, Networkish } from "@ethersproject/networks";
import { defineReadOnly, getStatic } from "@ethersproject/properties";
import { ConnectionInfo } from "@ethersproject/web";
import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);
import { JsonRpcProvider, JsonRpcSigner } from "./json-rpc-provider";
type getUrlFunc = (network: Network, apiKey: string) => string | ConnectionInfo;
export abstract class UrlJsonRpcProvider extends JsonRpcProvider {
readonly apiKey: any;
constructor(network?: Networkish, apiKey?: any) {
logger.checkAbstract(new.target, UrlJsonRpcProvider);
// Normalize the Network and API Key
network = getStatic<(network: Networkish) => Network>(new.target, "getNetwork")(network);
apiKey = getStatic<(apiKey: string) => string>(new.target, "getApiKey")(apiKey);
const connection = getStatic<getUrlFunc>(new.target, "getUrl")(network, apiKey);
super(connection, network);
if (typeof(apiKey) === "string") {
defineReadOnly(this, "apiKey", apiKey);
} else if (apiKey != null) {
Object.keys(apiKey).forEach((key) => {
defineReadOnly<any, any>(this, key, apiKey[key]);
});
}
}
async detectNetwork(): Promise<Network> {
return this.network;
}
_startPending(): void {
logger.warn("WARNING: API provider does not support pending filters");
}
getSigner(address?: string): JsonRpcSigner {
return logger.throwError(
"API provider does not support signing",
Logger.errors.UNSUPPORTED_OPERATION,
{ operation: "getSigner" }
);
}
listAccounts(): Promise<Array<string>> {
return Promise.resolve([]);
}
// Return a defaultApiKey if null, otherwise validate the API key
static getApiKey(apiKey: any): any {
return apiKey;
}
// Returns the url or connection for the given network and API key. The
// API key will have been sanitized by the getApiKey first, so any validation
// or transformations can be done there.
static getUrl(network: Network, apiKey: any): string | ConnectionInfo {
return logger.throwError("not implemented; sub-classes must override getUrl", Logger.errors.NOT_IMPLEMENTED, {
operation: "getUrl"
});
}
}