From c53864de0af55dd8ec8ca5681e78da380d85250a Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Mon, 29 Jun 2020 00:21:59 -0400 Subject: [PATCH] Added StaticJsonRpcProvider for reducing calls to chainId in certain cases (#901). --- packages/providers/src.ts/index.ts | 4 ++- .../providers/src.ts/url-json-rpc-provider.ts | 25 +++++++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/packages/providers/src.ts/index.ts b/packages/providers/src.ts/index.ts index 21ba25d4f6..3c4c9142b2 100644 --- a/packages/providers/src.ts/index.ts +++ b/packages/providers/src.ts/index.ts @@ -26,9 +26,9 @@ import { IpcProvider } from "./ipc-provider"; import { InfuraProvider } from "./infura-provider"; import { JsonRpcProvider, JsonRpcSigner } from "./json-rpc-provider"; import { NodesmithProvider } from "./nodesmith-provider"; +import { StaticJsonRpcProvider, UrlJsonRpcProvider } from "./url-json-rpc-provider"; import { Web3Provider } from "./web3-provider"; import { WebSocketProvider } from "./websocket-provider"; - import { ExternalProvider, JsonRpcFetchFunc } from "./web3-provider"; import { Formatter } from "./formatter"; @@ -93,6 +93,7 @@ export { Provider, BaseProvider, + UrlJsonRpcProvider, /////////////////////// // Concreate Providers @@ -105,6 +106,7 @@ export { InfuraProvider, JsonRpcProvider, NodesmithProvider, + StaticJsonRpcProvider, Web3Provider, WebSocketProvider, diff --git a/packages/providers/src.ts/url-json-rpc-provider.ts b/packages/providers/src.ts/url-json-rpc-provider.ts index 2eea5fc773..03d96dd4f6 100644 --- a/packages/providers/src.ts/url-json-rpc-provider.ts +++ b/packages/providers/src.ts/url-json-rpc-provider.ts @@ -13,7 +13,26 @@ import { JsonRpcProvider, JsonRpcSigner } from "./json-rpc-provider"; type getUrlFunc = (network: Network, apiKey: string) => string | ConnectionInfo; -export abstract class UrlJsonRpcProvider extends JsonRpcProvider { +// 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 +export class StaticJsonRpcProvider extends JsonRpcProvider { + async detectNetwork(): Promise { + let network = this.network; + if (network == null) { + // After this call completes, network is defined + network = await super._ready(); + } + return network; + } +} + +export abstract class UrlJsonRpcProvider extends StaticJsonRpcProvider { readonly apiKey: any; constructor(network?: Networkish, apiKey?: any) { @@ -36,10 +55,6 @@ export abstract class UrlJsonRpcProvider extends JsonRpcProvider { } } - async detectNetwork(): Promise { - return this.network; - } - _startPending(): void { logger.warn("WARNING: API provider does not support pending filters"); }