2020-02-04 08:01:26 -05:00
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
"use strict";
|
|
|
|
|
2019-11-01 23:51:08 +09:00
|
|
|
import { Network, Networkish } from "@ethersproject/networks";
|
2019-08-21 01:45:06 -04:00
|
|
|
import { defineReadOnly, getStatic } from "@ethersproject/properties";
|
2019-11-19 18:00:05 +09:00
|
|
|
import { ConnectionInfo } from "@ethersproject/web";
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-08-01 18:04:06 -04:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
import { JsonRpcProvider, JsonRpcSigner } from "./json-rpc-provider";
|
|
|
|
|
2019-11-19 18:00:05 +09:00
|
|
|
type getUrlFunc = (network: Network, apiKey: string) => string | ConnectionInfo;
|
|
|
|
|
2020-06-29 00:21:59 -04:00
|
|
|
// A StaticJsonRpcProvider is useful when you *know* for certain that
|
|
|
|
// the backend will never change, as it never calls eth_chainId to
|
|
|
|
// verify its backend. However, if the backend does change, the effects
|
|
|
|
// are undefined and may include:
|
|
|
|
// - inconsistent results
|
|
|
|
// - locking up the UI
|
|
|
|
// - block skew warnings
|
|
|
|
// - wrong results
|
2020-07-04 23:06:18 -04:00
|
|
|
// If the network is not explicit (i.e. auto-detection is expected), the
|
|
|
|
// node MUST be running and available to respond to requests BEFORE this
|
|
|
|
// is instantiated.
|
2020-06-29 00:21:59 -04:00
|
|
|
export class StaticJsonRpcProvider extends JsonRpcProvider {
|
|
|
|
async detectNetwork(): Promise<Network> {
|
|
|
|
let network = this.network;
|
|
|
|
if (network == null) {
|
2020-07-04 23:06:18 -04:00
|
|
|
network = await super.detectNetwork();
|
|
|
|
|
|
|
|
if (!network) {
|
|
|
|
logger.throwError("no network detected", Logger.errors.UNKNOWN_ERROR, { });
|
|
|
|
}
|
|
|
|
|
|
|
|
// If still not set, set it
|
|
|
|
if (this._network == null) {
|
|
|
|
// A static network does not support "any"
|
|
|
|
defineReadOnly(this, "_network", network);
|
|
|
|
|
|
|
|
this.emit("network", network, null);
|
|
|
|
}
|
2020-06-29 00:21:59 -04:00
|
|
|
}
|
|
|
|
return network;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export abstract class UrlJsonRpcProvider extends StaticJsonRpcProvider {
|
2020-01-20 19:27:33 -05:00
|
|
|
readonly apiKey: any;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-11-19 18:00:05 +09:00
|
|
|
constructor(network?: Networkish, apiKey?: any) {
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.checkAbstract(new.target, UrlJsonRpcProvider);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-05-23 18:24:31 -04:00
|
|
|
// Normalize the Network and API Key
|
2019-08-21 01:45:06 -04:00
|
|
|
network = getStatic<(network: Networkish) => Network>(new.target, "getNetwork")(network);
|
|
|
|
apiKey = getStatic<(apiKey: string) => string>(new.target, "getApiKey")(apiKey);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-11-19 18:00:05 +09:00
|
|
|
const connection = getStatic<getUrlFunc>(new.target, "getUrl")(network, apiKey);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-11-19 18:00:05 +09:00
|
|
|
super(connection, network);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-11-19 18:00:05 +09:00
|
|
|
if (typeof(apiKey) === "string") {
|
|
|
|
defineReadOnly(this, "apiKey", apiKey);
|
|
|
|
} else if (apiKey != null) {
|
|
|
|
Object.keys(apiKey).forEach((key) => {
|
2020-02-04 08:01:26 -05:00
|
|
|
defineReadOnly<any, any>(this, key, apiKey[key]);
|
2019-11-19 18:00:05 +09:00
|
|
|
});
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_startPending(): void {
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.warn("WARNING: API provider does not support pending filters");
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
getSigner(address?: string): JsonRpcSigner {
|
2019-11-19 18:00:05 +09:00
|
|
|
return logger.throwError(
|
2019-05-14 18:25:46 -04:00
|
|
|
"API provider does not support signing",
|
2019-08-01 18:04:06 -04:00
|
|
|
Logger.errors.UNSUPPORTED_OPERATION,
|
2019-05-14 18:25:46 -04:00
|
|
|
{ operation: "getSigner" }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
listAccounts(): Promise<Array<string>> {
|
|
|
|
return Promise.resolve([]);
|
|
|
|
}
|
2019-11-19 18:00:05 +09:00
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
// Return a defaultApiKey if null, otherwise validate the API key
|
2019-11-19 18:00:05 +09:00
|
|
|
static getApiKey(apiKey: any): any {
|
2019-05-14 18:25:46 -04:00
|
|
|
return apiKey;
|
|
|
|
}
|
|
|
|
|
2019-11-19 18:00:05 +09:00
|
|
|
// 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 {
|
2019-08-01 18:04:06 -04:00
|
|
|
return logger.throwError("not implemented; sub-classes must override getUrl", Logger.errors.NOT_IMPLEMENTED, {
|
2019-05-14 18:25:46 -04:00
|
|
|
operation: "getUrl"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|