Added StaticJsonRpcProvider for reducing calls to chainId in certain cases (#901).

This commit is contained in:
Richard Moore 2020-06-29 00:21:59 -04:00
parent 8c1ff4c862
commit c53864de0a
No known key found for this signature in database
GPG Key ID: 665176BE8E9DC651
2 changed files with 23 additions and 6 deletions

@ -26,9 +26,9 @@ import { IpcProvider } from "./ipc-provider";
import { InfuraProvider } from "./infura-provider"; import { InfuraProvider } from "./infura-provider";
import { JsonRpcProvider, JsonRpcSigner } from "./json-rpc-provider"; import { JsonRpcProvider, JsonRpcSigner } from "./json-rpc-provider";
import { NodesmithProvider } from "./nodesmith-provider"; import { NodesmithProvider } from "./nodesmith-provider";
import { StaticJsonRpcProvider, UrlJsonRpcProvider } from "./url-json-rpc-provider";
import { Web3Provider } from "./web3-provider"; import { Web3Provider } from "./web3-provider";
import { WebSocketProvider } from "./websocket-provider"; import { WebSocketProvider } from "./websocket-provider";
import { ExternalProvider, JsonRpcFetchFunc } from "./web3-provider"; import { ExternalProvider, JsonRpcFetchFunc } from "./web3-provider";
import { Formatter } from "./formatter"; import { Formatter } from "./formatter";
@ -93,6 +93,7 @@ export {
Provider, Provider,
BaseProvider, BaseProvider,
UrlJsonRpcProvider,
/////////////////////// ///////////////////////
// Concreate Providers // Concreate Providers
@ -105,6 +106,7 @@ export {
InfuraProvider, InfuraProvider,
JsonRpcProvider, JsonRpcProvider,
NodesmithProvider, NodesmithProvider,
StaticJsonRpcProvider,
Web3Provider, Web3Provider,
WebSocketProvider, WebSocketProvider,

@ -13,7 +13,26 @@ import { JsonRpcProvider, JsonRpcSigner } from "./json-rpc-provider";
type getUrlFunc = (network: Network, apiKey: string) => string | ConnectionInfo; 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<Network> {
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; readonly apiKey: any;
constructor(network?: Networkish, apiKey?: any) { constructor(network?: Networkish, apiKey?: any) {
@ -36,10 +55,6 @@ export abstract class UrlJsonRpcProvider extends JsonRpcProvider {
} }
} }
async detectNetwork(): Promise<Network> {
return this.network;
}
_startPending(): void { _startPending(): void {
logger.warn("WARNING: API provider does not support pending filters"); logger.warn("WARNING: API provider does not support pending filters");
} }