diff --git a/packages/providers/src.ts/json-rpc-provider.ts b/packages/providers/src.ts/json-rpc-provider.ts index ed4bb3397c..cee81148de 100644 --- a/packages/providers/src.ts/json-rpc-provider.ts +++ b/packages/providers/src.ts/json-rpc-provider.ts @@ -219,34 +219,32 @@ export class JsonRpcProvider extends BaseProvider { constructor(url?: ConnectionInfo | string, network?: Networkish) { logger.checkNew(new.target, JsonRpcProvider); - const getNetwork = getStatic<(network: Networkish) => Network>(new.target, "getNetwork"); - - // One parameter, but it is a network name, so swap it with the URL - if (typeof(url) === "string") { - if (network === null) { - const checkNetwork = getNetwork(url); - network = checkNetwork; - url = null; - } + let networkOrReady: Networkish | Promise = network; + + // The network is unknown, query the JSON-RPC for it + if (networkOrReady == null) { + networkOrReady = new Promise((resolve, reject) => { + setTimeout(() => { + this.detectNetwork().then((network) => { + resolve(network); + }, (error) => { + reject(error); + }); + }, 0); + }); } - if (network) { - // The network has been specified explicitly, we can use it - super(network); - } else { - // The network is unknown, query the JSON-RPC for it - super(this.detectNetwork()); - } + super(networkOrReady); // Default URL if (!url) { url = getStatic<() => string>(this.constructor, "defaultUrl")(); } if (typeof(url) === "string") { - this.connection = Object.freeze({ + defineReadOnly(this, "connection",Object.freeze({ url: url - }); + })); } else { - this.connection = Object.freeze(shallowCopy(url)); + defineReadOnly(this, "connection", Object.freeze(shallowCopy(url))); } this._nextId = 42;