-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5762 from NomicFoundation/network-manager-m1-cleanup
Network Manager M1 remaining tasks
- Loading branch information
Showing
11 changed files
with
1,286 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 74 additions & 58 deletions
132
v-next/hardhat/src/internal/builtin-plugins/network-manager/hook-handlers/config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,94 @@ | ||
import type { | ||
ConfigurationVariable, | ||
GasConfig, | ||
GasUserConfig, | ||
HardhatConfig, | ||
HardhatUserConfig, | ||
HttpNetworkConfig, | ||
NetworkConfig, | ||
NetworkUserConfig, | ||
ResolvedConfigurationVariable, | ||
} from "../../../../types/config.js"; | ||
import type { ConfigHooks } from "../../../../types/hooks.js"; | ||
|
||
import { validateUserConfig } from "../type-validation.js"; | ||
|
||
function resolveBigIntOrAuto( | ||
value: number | bigint | "auto" | undefined, | ||
): bigint | "auto" { | ||
if (value === undefined || value === "auto") { | ||
return "auto"; | ||
} | ||
|
||
// TODO: Validate that it's a valid BigInt | ||
return BigInt(value); | ||
} | ||
|
||
export default async (): Promise<Partial<ConfigHooks>> => ({ | ||
extendUserConfig: async (config, next) => { | ||
const extendedConfig = await next(config); | ||
extendUserConfig, | ||
validateUserConfig, | ||
resolveUserConfig, | ||
}); | ||
|
||
const networks: Record<string, NetworkUserConfig> = | ||
extendedConfig.networks ?? {}; | ||
export async function extendUserConfig( | ||
config: HardhatUserConfig, | ||
next: (nextConfig: HardhatUserConfig) => Promise<HardhatUserConfig>, | ||
): Promise<HardhatUserConfig> { | ||
const extendedConfig = await next(config); | ||
|
||
return { | ||
...extendedConfig, | ||
networks: { | ||
...networks, | ||
localhost: { | ||
url: "http://localhost:8545", | ||
...networks.localhost, | ||
type: "http", | ||
}, | ||
}, | ||
}; | ||
}, | ||
validateUserConfig, | ||
resolveUserConfig: async (userConfig, resolveConfigurationVariable, next) => { | ||
const resolvedConfig = await next(userConfig, resolveConfigurationVariable); | ||
const networks: Record<string, NetworkUserConfig> = | ||
extendedConfig.networks ?? {}; | ||
|
||
const networks: Record<string, NetworkUserConfig> = | ||
userConfig.networks ?? {}; | ||
return { | ||
...extendedConfig, | ||
networks: { | ||
...networks, | ||
localhost: { | ||
url: "http://localhost:8545", | ||
...networks.localhost, | ||
type: "http", | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
const resolvedNetworks: Record<string, NetworkConfig> = {}; | ||
export async function resolveUserConfig( | ||
userConfig: HardhatUserConfig, | ||
resolveConfigurationVariable: ( | ||
variableOrString: ConfigurationVariable | string, | ||
) => ResolvedConfigurationVariable, | ||
next: ( | ||
nextUserConfig: HardhatUserConfig, | ||
nextResolveConfigurationVariable: ( | ||
variableOrString: ConfigurationVariable | string, | ||
) => ResolvedConfigurationVariable, | ||
) => Promise<HardhatConfig>, | ||
): Promise<HardhatConfig> { | ||
const resolvedConfig = await next(userConfig, resolveConfigurationVariable); | ||
|
||
for (const [networkName, networkConfig] of Object.entries(networks)) { | ||
if (networkConfig.type !== "http") { | ||
// eslint-disable-next-line no-restricted-syntax -- TODO | ||
throw new Error("Only HTTP network is supported for now"); | ||
} | ||
const networks: Record<string, NetworkUserConfig> = userConfig.networks ?? {}; | ||
|
||
const resolvedNetworkConfig: HttpNetworkConfig = { | ||
type: "http", | ||
chainId: networkConfig.chainId, | ||
chainType: networkConfig.chainType, | ||
from: networkConfig.from, | ||
gas: resolveBigIntOrAuto(networkConfig.gas), | ||
gasMultiplier: networkConfig.gasMultiplier ?? 1, | ||
gasPrice: resolveBigIntOrAuto(networkConfig.gasPrice), | ||
url: networkConfig.url, | ||
timeout: networkConfig.timeout ?? 20_000, | ||
httpHeaders: networkConfig.httpHeaders ?? {}, | ||
}; | ||
const resolvedNetworks: Record<string, NetworkConfig> = {}; | ||
|
||
resolvedNetworks[networkName] = resolvedNetworkConfig; | ||
for (const [networkName, networkConfig] of Object.entries(networks)) { | ||
if (networkConfig.type !== "http") { | ||
// eslint-disable-next-line no-restricted-syntax -- TODO | ||
throw new Error("Only HTTP network is supported for now"); | ||
} | ||
|
||
return { | ||
...resolvedConfig, | ||
defaultNetwork: resolvedConfig.defaultNetwork ?? "localhost", | ||
defaultChainType: resolvedConfig.defaultChainType ?? "unknown", | ||
networks: resolvedNetworks, | ||
const resolvedNetworkConfig: HttpNetworkConfig = { | ||
type: "http", | ||
chainId: networkConfig.chainId, | ||
chainType: networkConfig.chainType, | ||
from: networkConfig.from, | ||
gas: resolveGasConfig(networkConfig.gas), | ||
gasMultiplier: networkConfig.gasMultiplier ?? 1, | ||
gasPrice: resolveGasConfig(networkConfig.gasPrice), | ||
url: networkConfig.url, | ||
timeout: networkConfig.timeout ?? 20_000, | ||
httpHeaders: networkConfig.httpHeaders ?? {}, | ||
}; | ||
}, | ||
}); | ||
|
||
resolvedNetworks[networkName] = resolvedNetworkConfig; | ||
} | ||
|
||
return { | ||
...resolvedConfig, | ||
defaultChainType: resolvedConfig.defaultChainType ?? "unknown", | ||
defaultNetwork: resolvedConfig.defaultNetwork ?? "localhost", | ||
networks: resolvedNetworks, | ||
}; | ||
} | ||
|
||
function resolveGasConfig(value: GasUserConfig = "auto"): GasConfig { | ||
return value === "auto" ? value : BigInt(value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.