Skip to content

Commit

Permalink
Document and clean up the provider construction module
Browse files Browse the repository at this point in the history
  • Loading branch information
alcuadrado committed Sep 14, 2020
1 parent c5227c1 commit 29822f8
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions packages/buidler-core/src/internal/core/providers/construction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ export function isHDAccountsConfig(
return accounts !== undefined && Object.keys(accounts).includes("mnemonic");
}

function isHttpNetworkConfig(
netConfig: NetworkConfig
): netConfig is HttpNetworkConfig {
return "url" in netConfig;
}

// This function is let's you import a provider dynamically in a pretty
// type-safe way.
// `ProviderNameT` and `name` must be the same literal string. TS enforces it.
// `ModuleT` and `filePath` must also be the same, but this is not enforced.
function importProvider<ModuleT, ProviderNameT extends keyof ModuleT>(
filePath: string,
name: ProviderNameT
Expand Down Expand Up @@ -133,11 +143,8 @@ export function applyProviderWrappers(
"ChainIdValidatorProvider"
>("./chainId", "ChainIdValidatorProvider");

const isHttpNetworkConfig = "url" in netConfig;

if (isHttpNetworkConfig) {
const httpNetConfig = netConfig as Partial<HttpNetworkConfig>;
const accounts = httpNetConfig.accounts;
if (isHttpNetworkConfig(netConfig)) {
const accounts = netConfig.accounts;

if (Array.isArray(accounts)) {
provider = new LocalAccountsProvider(provider, accounts);
Expand All @@ -153,7 +160,7 @@ export function applyProviderWrappers(

// TODO: Add some extension mechanism for account plugins here

if (typeof httpNetConfig.gas !== "number") {
if (typeof netConfig.gas !== "number") {
provider = new GanacheGasMultiplierProvider(provider);
}
}
Expand All @@ -176,7 +183,7 @@ export function applyProviderWrappers(
provider = new FixedGasPriceProvider(provider, netConfig.gasPrice);
}

if (isHttpNetworkConfig && netConfig.chainId !== undefined) {
if (isHttpNetworkConfig(netConfig) && netConfig.chainId !== undefined) {
provider = new ChainIdValidatorProvider(provider, netConfig.chainId);
}

Expand Down

0 comments on commit 29822f8

Please sign in to comment.