diff --git a/packages/providers/src.ts/websocket-provider.ts b/packages/providers/src.ts/websocket-provider.ts index 772884636c..32ae2af657 100644 --- a/packages/providers/src.ts/websocket-provider.ts +++ b/packages/providers/src.ts/websocket-provider.ts @@ -3,7 +3,7 @@ import WebSocket from "ws"; import { BigNumber } from "@ethersproject/bignumber"; -import { Networkish } from "@ethersproject/networks"; +import { Network, Networkish } from "@ethersproject/networks"; import { defineReadOnly } from "@ethersproject/properties"; import { Event } from "./base-provider"; @@ -47,6 +47,7 @@ export type Subscription = { export class WebSocketProvider extends JsonRpcProvider { readonly _websocket: any; readonly _requests: { [ name: string ]: InflightRequest }; + readonly _detectNetwork: Promise; // Maps event tag to subscription ID (we dedupe identical events) readonly _subIds: { [ tag: string ]: Promise }; @@ -67,13 +68,15 @@ export class WebSocketProvider extends JsonRpcProvider { super(url, network); this._pollingInterval = -1; + this._wsReady = false; + defineReadOnly(this, "_websocket", new WebSocket(this.connection.url)); defineReadOnly(this, "_requests", { }); defineReadOnly(this, "_subs", { }); defineReadOnly(this, "_subIds", { }); + defineReadOnly(this, "_detectNetwork", super.detectNetwork()); // Stall sending requests until the socket is open... - this._wsReady = false; this._websocket.onopen = () => { this._wsReady = true; Object.keys(this._requests).forEach((id) => { @@ -125,6 +128,10 @@ export class WebSocketProvider extends JsonRpcProvider { if (fauxPoll.unref) { fauxPoll.unref(); } } + detectNetwork(): Promise { + return this._detectNetwork; + } + get pollingInterval(): number { return 0; }