Skip to content

Commit

Permalink
Added StaticJsonRpcProvider for reducing calls to chainId in certain …
Browse files Browse the repository at this point in the history
…cases (#901).
  • Loading branch information
ricmoo committed Jun 29, 2020
1 parent 8c1ff4c commit c53864d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
4 changes: 3 additions & 1 deletion packages/providers/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -93,6 +93,7 @@ export {
Provider,
BaseProvider,

UrlJsonRpcProvider,

///////////////////////
// Concreate Providers
Expand All @@ -105,6 +106,7 @@ export {
InfuraProvider,
JsonRpcProvider,
NodesmithProvider,
StaticJsonRpcProvider,
Web3Provider,
WebSocketProvider,

Expand Down
25 changes: 20 additions & 5 deletions packages/providers/src.ts/url-json-rpc-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<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;

constructor(network?: Networkish, apiKey?: any) {
Expand All @@ -36,10 +55,6 @@ export abstract class UrlJsonRpcProvider extends JsonRpcProvider {
}
}

async detectNetwork(): Promise<Network> {
return this.network;
}

_startPending(): void {
logger.warn("WARNING: API provider does not support pending filters");
}
Expand Down

0 comments on commit c53864d

Please sign in to comment.