Skip to content

Commit

Permalink
Fixed ENS overrides for the default provider (#959).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Jul 16, 2020
1 parent b116621 commit 63dd3d4
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions packages/networks/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,18 @@ export {
Networkish
};

function ethDefaultProvider(network: string): (providers: any) => any {
return function(providers: any, options?: any): any {
type DefaultProviderFunc = (providers: any, options?: any) => any;

interface Renetworkable extends DefaultProviderFunc {
renetwork: (network: Network) => DefaultProviderFunc;
};

function isRenetworkable(value: any): value is Renetworkable {
return (value && typeof(value.renetwork) === "function");
}

function ethDefaultProvider(network: string | Network): Renetworkable {
const func = function(providers: any, options?: any): any {
if (options == null) { options = { }; }
const providerList: Array<any> = [];

Expand Down Expand Up @@ -53,17 +63,29 @@ function ethDefaultProvider(network: string): (providers: any) => any {
}

return providerList[0];
}
};

func.renetwork = function(network: Network) {
return ethDefaultProvider(network);
};

return func;
}

function etcDefaultProvider(url: string, network: string): (providers: any) => any {
return function(providers: any, options?: any): any {
function etcDefaultProvider(url: string, network: string | Network): Renetworkable {
const func = function(providers: any, options?: any): any {
if (providers.JsonRpcProvider) {
return new providers.JsonRpcProvider(url, network);
}

return null;
}
};

func.renetwork = function(network: Network) {
return etcDefaultProvider(url, network);
};

return func;
}

const homestead: Network = {
Expand Down Expand Up @@ -201,11 +223,22 @@ export function getNetwork(network: Networkish): Network {
logger.throwArgumentError("network chainId mismatch", "network", network);
}

// @TODO: In the next major version add an attach function to a defaultProvider
// class and move the _defaultProvider internal to this file (extend Network)
let defaultProvider: DefaultProviderFunc = network._defaultProvider || null;
if (defaultProvider == null && standard._defaultProvider) {
if (isRenetworkable(standard._defaultProvider)) {
defaultProvider = standard._defaultProvider.renetwork(network);
} else {
defaultProvider = standard._defaultProvider;
}
}

// Standard Network (allow overriding the ENS address)
return {
name: network.name,
chainId: standard.chainId,
ensAddress: (network.ensAddress || standard.ensAddress || null),
_defaultProvider: (network._defaultProvider || standard._defaultProvider || null)
_defaultProvider: defaultProvider
};
}

0 comments on commit 63dd3d4

Please sign in to comment.