|
1 | 1 | import type {
|
| 2 | + ConfigurationVariable, |
| 3 | + GasConfig, |
| 4 | + GasUserConfig, |
| 5 | + HardhatConfig, |
| 6 | + HardhatUserConfig, |
2 | 7 | HttpNetworkConfig,
|
3 | 8 | NetworkConfig,
|
4 | 9 | NetworkUserConfig,
|
| 10 | + ResolvedConfigurationVariable, |
5 | 11 | } from "../../../../types/config.js";
|
6 | 12 | import type { ConfigHooks } from "../../../../types/hooks.js";
|
7 | 13 |
|
8 | 14 | import { validateUserConfig } from "../type-validation.js";
|
9 | 15 |
|
10 |
| -function resolveBigIntOrAuto( |
11 |
| - value: number | bigint | "auto" | undefined, |
12 |
| -): bigint | "auto" { |
13 |
| - if (value === undefined || value === "auto") { |
14 |
| - return "auto"; |
15 |
| - } |
16 |
| - |
17 |
| - // TODO: Validate that it's a valid BigInt |
18 |
| - return BigInt(value); |
19 |
| -} |
20 |
| - |
21 | 16 | export default async (): Promise<Partial<ConfigHooks>> => ({
|
22 |
| - extendUserConfig: async (config, next) => { |
23 |
| - const extendedConfig = await next(config); |
| 17 | + extendUserConfig, |
| 18 | + validateUserConfig, |
| 19 | + resolveUserConfig, |
| 20 | +}); |
24 | 21 |
|
25 |
| - const networks: Record<string, NetworkUserConfig> = |
26 |
| - extendedConfig.networks ?? {}; |
| 22 | +export async function extendUserConfig( |
| 23 | + config: HardhatUserConfig, |
| 24 | + next: (nextConfig: HardhatUserConfig) => Promise<HardhatUserConfig>, |
| 25 | +): Promise<HardhatUserConfig> { |
| 26 | + const extendedConfig = await next(config); |
27 | 27 |
|
28 |
| - return { |
29 |
| - ...extendedConfig, |
30 |
| - networks: { |
31 |
| - ...networks, |
32 |
| - localhost: { |
33 |
| - url: "http://localhost:8545", |
34 |
| - ...networks.localhost, |
35 |
| - type: "http", |
36 |
| - }, |
37 |
| - }, |
38 |
| - }; |
39 |
| - }, |
40 |
| - validateUserConfig, |
41 |
| - resolveUserConfig: async (userConfig, resolveConfigurationVariable, next) => { |
42 |
| - const resolvedConfig = await next(userConfig, resolveConfigurationVariable); |
| 28 | + const networks: Record<string, NetworkUserConfig> = |
| 29 | + extendedConfig.networks ?? {}; |
43 | 30 |
|
44 |
| - const networks: Record<string, NetworkUserConfig> = |
45 |
| - userConfig.networks ?? {}; |
| 31 | + return { |
| 32 | + ...extendedConfig, |
| 33 | + networks: { |
| 34 | + ...networks, |
| 35 | + localhost: { |
| 36 | + url: "http://localhost:8545", |
| 37 | + ...networks.localhost, |
| 38 | + type: "http", |
| 39 | + }, |
| 40 | + }, |
| 41 | + }; |
| 42 | +} |
46 | 43 |
|
47 |
| - const resolvedNetworks: Record<string, NetworkConfig> = {}; |
| 44 | +export async function resolveUserConfig( |
| 45 | + userConfig: HardhatUserConfig, |
| 46 | + resolveConfigurationVariable: ( |
| 47 | + variableOrString: ConfigurationVariable | string, |
| 48 | + ) => ResolvedConfigurationVariable, |
| 49 | + next: ( |
| 50 | + nextUserConfig: HardhatUserConfig, |
| 51 | + nextResolveConfigurationVariable: ( |
| 52 | + variableOrString: ConfigurationVariable | string, |
| 53 | + ) => ResolvedConfigurationVariable, |
| 54 | + ) => Promise<HardhatConfig>, |
| 55 | +): Promise<HardhatConfig> { |
| 56 | + const resolvedConfig = await next(userConfig, resolveConfigurationVariable); |
48 | 57 |
|
49 |
| - for (const [networkName, networkConfig] of Object.entries(networks)) { |
50 |
| - if (networkConfig.type !== "http") { |
51 |
| - // eslint-disable-next-line no-restricted-syntax -- TODO |
52 |
| - throw new Error("Only HTTP network is supported for now"); |
53 |
| - } |
| 58 | + const networks: Record<string, NetworkUserConfig> = userConfig.networks ?? {}; |
54 | 59 |
|
55 |
| - const resolvedNetworkConfig: HttpNetworkConfig = { |
56 |
| - type: "http", |
57 |
| - chainId: networkConfig.chainId, |
58 |
| - chainType: networkConfig.chainType, |
59 |
| - from: networkConfig.from, |
60 |
| - gas: resolveBigIntOrAuto(networkConfig.gas), |
61 |
| - gasMultiplier: networkConfig.gasMultiplier ?? 1, |
62 |
| - gasPrice: resolveBigIntOrAuto(networkConfig.gasPrice), |
63 |
| - url: networkConfig.url, |
64 |
| - timeout: networkConfig.timeout ?? 20_000, |
65 |
| - httpHeaders: networkConfig.httpHeaders ?? {}, |
66 |
| - }; |
| 60 | + const resolvedNetworks: Record<string, NetworkConfig> = {}; |
67 | 61 |
|
68 |
| - resolvedNetworks[networkName] = resolvedNetworkConfig; |
| 62 | + for (const [networkName, networkConfig] of Object.entries(networks)) { |
| 63 | + if (networkConfig.type !== "http") { |
| 64 | + // eslint-disable-next-line no-restricted-syntax -- TODO |
| 65 | + throw new Error("Only HTTP network is supported for now"); |
69 | 66 | }
|
70 | 67 |
|
71 |
| - return { |
72 |
| - ...resolvedConfig, |
73 |
| - defaultNetwork: resolvedConfig.defaultNetwork ?? "localhost", |
74 |
| - defaultChainType: resolvedConfig.defaultChainType ?? "unknown", |
75 |
| - networks: resolvedNetworks, |
| 68 | + const resolvedNetworkConfig: HttpNetworkConfig = { |
| 69 | + type: "http", |
| 70 | + chainId: networkConfig.chainId, |
| 71 | + chainType: networkConfig.chainType, |
| 72 | + from: networkConfig.from, |
| 73 | + gas: resolveGasConfig(networkConfig.gas), |
| 74 | + gasMultiplier: networkConfig.gasMultiplier ?? 1, |
| 75 | + gasPrice: resolveGasConfig(networkConfig.gasPrice), |
| 76 | + url: networkConfig.url, |
| 77 | + timeout: networkConfig.timeout ?? 20_000, |
| 78 | + httpHeaders: networkConfig.httpHeaders ?? {}, |
76 | 79 | };
|
77 |
| - }, |
78 |
| -}); |
| 80 | + |
| 81 | + resolvedNetworks[networkName] = resolvedNetworkConfig; |
| 82 | + } |
| 83 | + |
| 84 | + return { |
| 85 | + ...resolvedConfig, |
| 86 | + defaultChainType: resolvedConfig.defaultChainType ?? "unknown", |
| 87 | + defaultNetwork: resolvedConfig.defaultNetwork ?? "localhost", |
| 88 | + networks: resolvedNetworks, |
| 89 | + }; |
| 90 | +} |
| 91 | + |
| 92 | +function resolveGasConfig(value: GasUserConfig = "auto"): GasConfig { |
| 93 | + return value === "auto" ? value : BigInt(value); |
| 94 | +} |
0 commit comments