Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Chaintype type #5772

Merged
merged 9 commits into from
Sep 28, 2024
6 changes: 4 additions & 2 deletions v-next/example-project/viem-scketch-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { HardhatPlugin } from "@ignored/hardhat-vnext/types/plugins";
import { ChainType } from "@ignored/hardhat-vnext/types/config";
import { HookContext } from "@ignored/hardhat-vnext/types/hooks";

import type { NetworkConnection } from "@ignored/hardhat-vnext/types/network";
import type {
ChainType,
NetworkConnection,
} from "@ignored/hardhat-vnext/types/network";

import "@ignored/hardhat-vnext/types/network";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { ChainType } from "@ignored/hardhat-vnext/types/config";
import type {
HookContext,
NetworkHooks,
} from "@ignored/hardhat-vnext/types/hooks";
import type { NetworkConnection } from "@ignored/hardhat-vnext/types/network";
import type {
ChainType,
NetworkConnection,
} from "@ignored/hardhat-vnext/types/network";

import { NetworkHelpers } from "../network-helpers/network-helpers.js";

Expand Down
1 change: 0 additions & 1 deletion v-next/hardhat-network-helpers/src/type-extensions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import "@ignored/hardhat-vnext/types/network";

import type { NetworkHelpers } from "./internal/network-helpers/network-helpers.js";
import type { ChainType } from "@ignored/hardhat-vnext/types/config";

declare module "@ignored/hardhat-vnext/types/network" {
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- the ChainTypeT must be declared in the interface but in this scenario it's not used
Expand Down
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ export class HttpProvider extends EventEmitter implements EthereumProvider {
this.#jsonRpcRequestWrapper = jsonRpcRequestWrapper;
}

// TODO: We should test that the request is actually wrapped
public async request(
requestArguments: RequestArguments,
): Promise<SuccessfulJsonRpcResponse["result"]> {
Expand Down Expand Up @@ -151,7 +150,6 @@ export class HttpProvider extends EventEmitter implements EthereumProvider {
return jsonRpcResponse.result;
}

// TODO: This should be tested
public async close(): Promise<void> {
// See https://github.com/nodejs/undici/discussions/3522#discussioncomment-10498734
await this.#dispatcher.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ChainType, NetworkConfig } from "../../../types/config.js";
import type { NetworkConnection } from "../../../types/network.js";
import type { NetworkConfig } from "../../../types/config.js";
import type { ChainType, NetworkConnection } from "../../../types/network.js";
import type { EthereumProvider } from "../../../types/providers.js";

export type CloseConnectionFunction<ChainTypeT extends ChainType | string> = (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { NetworkConfig } from "../../../types/config.js";
import type { HookManager } from "../../../types/hooks.js";
import type {
ChainType,
DefaultChainType,
NetworkConfig,
} from "../../../types/config.js";
import type { HookManager } from "../../../types/hooks.js";
import type { NetworkConnection } from "../../../types/network.js";
NetworkConnection,
} from "../../../types/network.js";
import type { EthereumProvider } from "../../../types/providers.js";

import { HardhatError } from "@ignored/hardhat-vnext-errors";
Expand Down Expand Up @@ -71,6 +71,7 @@ export class NetworkManagerImplementation {

if (
networkConfigOverride !== undefined &&
"type" in networkConfigOverride &&
networkConfigOverride.type !==
this.#networkConfigs[resolvedNetworkName].type
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,7 @@
import type { ChainType, DefaultChainType } from "../../../../types/network.js";

import "../../../../types/config.js";
declare module "../../../../types/config.js" {
/**
* Represents the possible chain types for the network. The options are:
* - `"unknown"`: Represents the most generic type of network.
* - `"l1"`: Represents Layer 1 networks like Ethereum.
* - `"optimism"`: Represents Layer 2 networks like Optimism.
*/
export type ChainType = "unknown" | "l1" | "optimism";

/**
* Determines the default chain type to use when no chain type is specified.
* The default chain type is `"unknown"` by default. You can customize the
* default chain type by adding a `defaultChainType` property to the
* `ChainTypeConfig` interface with a valid `ChainType` value.
* For example:
* ```ts
* declare module "@ignored/hardhat-vnext/types/config" {
* export interface ChainTypeConfig {
* defaultChainType: "l1";
* }
* }
* ```
*/
export type DefaultChainType = ChainTypeConfig extends {
defaultChainType: infer T;
}
? T extends ChainType
? T
: "unknown"
: "unknown";

/* eslint-disable-next-line @typescript-eslint/no-empty-interface -- Empty
interface to allow the user to change the default chain type. */
export interface ChainTypeConfig {}

export interface HardhatUserConfig {
defaultChainType?: DefaultChainType;
defaultNetwork?: string;
Expand All @@ -48,14 +16,16 @@ declare module "../../../../types/config.js" {

export type NetworkUserConfig = HttpNetworkUserConfig | EdrNetworkUserConfig;

export type GasUserConfig = "auto" | number | bigint;

export interface HttpNetworkUserConfig {
type: "http";
chainId?: number;
chainType?: ChainType;
from?: string;
gas?: "auto" | number | bigint;
gas?: GasUserConfig;
gasMultiplier?: number;
gasPrice?: "auto" | number | bigint;
gasPrice?: GasUserConfig;

// HTTP network specific
url: string;
Expand All @@ -68,23 +38,25 @@ declare module "../../../../types/config.js" {
chainId: number;
chainType?: ChainType;
from?: string;
gas: "auto" | number | bigint;
gas: GasUserConfig;
gasMultiplier: number;
gasPrice: "auto" | number | bigint;
gasPrice: GasUserConfig;

// EDR network specific
}

export type NetworkConfig = HttpNetworkConfig | EdrNetworkConfig;

export type GasConfig = "auto" | bigint;

export interface HttpNetworkConfig {
type: "http";
chainId?: number;
chainType?: ChainType;
from?: string;
gas: "auto" | bigint;
gas: GasConfig;
gasMultiplier: number;
gasPrice: "auto" | bigint;
gasPrice: GasConfig;

// HTTP network specific
url: string;
Expand All @@ -97,9 +69,9 @@ declare module "../../../../types/config.js" {
chainId: number;
chainType?: ChainType;
from: string;
gas: "auto" | bigint;
gas: GasConfig;
gasMultiplier: number;
gasPrice: "auto" | bigint;
gasPrice: GasConfig;

// EDR network specific
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { ChainType } from "../../../../types/config.js";
import type { NetworkConnection } from "../../../../types/network.js";
import type {
ChainType,
NetworkConnection,
} from "../../../../types/network.js";
import type {
JsonRpcRequest,
JsonRpcResponse,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ const httpNetworkUserConfigSchema = z.object({
from: z.optional(z.string()),
gas: z.optional(
unionType(
[z.literal("auto"), z.number().int(), z.bigint()],
"Expected 'auto', number, or bigint",
[z.literal("auto"), z.number().int().safe(), z.bigint()],
"Expected 'auto', a safe int, or bigint",
),
),
gasMultiplier: z.optional(z.number()),
gasPrice: z.optional(
unionType(
[z.literal("auto"), z.number().int(), z.bigint()],
"Expected 'auto', number, or bigint",
[z.literal("auto"), z.number().int().safe(), z.bigint()],
"Expected 'auto', a safe int, or bigint",
),
),

Expand All @@ -47,13 +47,13 @@ const edrNetworkUserConfigSchema = z.object({
chainType: z.optional(chainTypeSchema),
from: z.optional(z.string()),
gas: unionType(
[z.literal("auto"), z.number().int(), z.bigint()],
"Expected 'auto', number, or bigint",
[z.literal("auto"), z.number().int().safe(), z.bigint()],
"Expected 'auto', a safe int, or bigint",
),
gasMultiplier: z.number(),
gasPrice: unionType(
[z.literal("auto"), z.number().int(), z.bigint()],
"Expected 'auto', number, or bigint",
[z.literal("auto"), z.number().int().safe(), z.bigint()],
"Expected 'auto', a safe int, or bigint",
),
});

Expand All @@ -70,7 +70,7 @@ const userConfigSchema = z.object({

const httpNetworkConfigSchema = z.object({
type: z.literal("http"),
chainId: z.number().int().optional(),
chainId: z.optional(z.number().int()),
chainType: z.optional(chainTypeSchema),
from: z.optional(z.string()),
gas: unionType([z.literal("auto"), z.bigint()], "Expected 'auto' or bigint"),
Expand Down
Loading