Fix JsonRpcProvider out-of-order super call (#822).

This commit is contained in:
Richard Moore 2020-05-08 15:12:02 -04:00
parent 84d37fda83
commit 963197d70c
No known key found for this signature in database
GPG Key ID: 665176BE8E9DC651

View File

@ -219,34 +219,32 @@ export class JsonRpcProvider extends BaseProvider {
constructor(url?: ConnectionInfo | string, network?: Networkish) { constructor(url?: ConnectionInfo | string, network?: Networkish) {
logger.checkNew(new.target, JsonRpcProvider); logger.checkNew(new.target, JsonRpcProvider);
const getNetwork = getStatic<(network: Networkish) => Network>(new.target, "getNetwork"); let networkOrReady: Networkish | Promise<Network> = network;
// 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;
}
}
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 // The network is unknown, query the JSON-RPC for it
super(this.detectNetwork()); if (networkOrReady == null) {
networkOrReady = new Promise((resolve, reject) => {
setTimeout(() => {
this.detectNetwork().then((network) => {
resolve(network);
}, (error) => {
reject(error);
});
}, 0);
});
} }
super(networkOrReady);
// Default URL // Default URL
if (!url) { url = getStatic<() => string>(this.constructor, "defaultUrl")(); } if (!url) { url = getStatic<() => string>(this.constructor, "defaultUrl")(); }
if (typeof(url) === "string") { if (typeof(url) === "string") {
this.connection = Object.freeze({ defineReadOnly(this, "connection",Object.freeze({
url: url url: url
}); }));
} else { } else {
this.connection = Object.freeze(shallowCopy(url)); defineReadOnly(this, "connection", Object.freeze(shallowCopy(url)));
} }
this._nextId = 42; this._nextId = 42;