Skip to content

Commit

Permalink
Allow getDefaultProvider to accept a URL as a network.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Jun 29, 2020
1 parent 987b535 commit 8c1ff4c
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/providers/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down

0 comments on commit 8c1ff4c

Please sign in to comment.