Allow getDefaultProvider to accept a URL as a network.

This commit is contained in:
Richard Moore 2020-06-29 00:06:31 -04:00
parent 987b5354cc
commit 8c1ff4c862
No known key found for this signature in database
GPG Key ID: 665176BE8E9DC651

@ -42,6 +42,25 @@ const logger = new Logger(version);
function getDefaultProvider(network?: Network | string, options?: any): BaseProvider {
if (network == null) { network = "homestead"; }
// If passed a URL, figure out the right type of provider based on the scheme
if (typeof(network) === "string") {
// @TODO: Add support for IpcProvider; maybe if it ends in ".ipc"?
// Handle http and ws (and their secure variants)
const match = network.match(/^(ws|http)s?:/i);
if (match) {
switch (match[1]) {
case "http":
return new JsonRpcProvider(network);
case "ws":
return new WebSocketProvider(network);
default:
logger.throwArgumentError("unsupported URL scheme", "network", network);
}
}
}
const n = getNetwork(network);
if (!n || !n._defaultProvider) {
logger.throwError("unsupported getDefaultProvider network", Logger.errors.NETWORK_ERROR, {