Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tidy-bananas-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"hardhat": patch
---

Updated forking configurations to support number and bigint.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ declare module "../../../../types/config.js" {
export interface EdrNetworkForkingUserConfig {
enabled?: boolean;
url: SensitiveString;
blockNumber?: number;
blockNumber?: number | bigint;
httpHeaders?: Record<string, string>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ const nonnegativeIntSchema = z.number().int().nonnegative();
const nonnegativeBigIntSchema = z.bigint().nonnegative();

const blockNumberSchema = nonnegativeIntSchema;
const forkingBlockNumberSchema = unionType(
[z.number().int().nonnegative().safe(), z.bigint().nonnegative()],
"Expected a nonnegative safe int or a nonnegative bigint",
);
const chainIdSchema = nonnegativeIntSchema;

const chainTypeUserConfigSchema = unionType(
Expand Down Expand Up @@ -267,7 +271,7 @@ const edrNetworkAccountsUserConfigSchema = conditionalUnionType(
const edrNetworkForkingUserConfigSchema = z.object({
enabled: z.optional(z.boolean()),
url: sensitiveUrlSchema,
blockNumber: z.optional(blockNumberSchema),
blockNumber: z.optional(forkingBlockNumberSchema),
httpHeaders: z.optional(z.record(z.string())),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
conditionalUnionType,
sensitiveStringSchema,
sensitiveUrlSchema,
unionType,
validateUserConfigZodType,
} from "@nomicfoundation/hardhat-zod-utils";
import { z } from "zod";
Expand Down Expand Up @@ -66,7 +67,12 @@ const solidityTestUserConfigType = z.object({
forking: z
.object({
url: z.optional(sensitiveUrlSchema),
blockNumber: z.bigint().optional(),
blockNumber: z.optional(
unionType(
[z.number().int().nonnegative().safe(), z.bigint().nonnegative()],
"Expected a nonnegative safe int or a nonnegative bigint",
),
),
rpcEndpoints: z.record(sensitiveStringSchema).optional(),
})
.optional(),
Expand Down Expand Up @@ -122,6 +128,10 @@ export function resolveSolidityTestForkingConfig(

return {
...forkingUserConfig,
blockNumber:
forkingUserConfig.blockNumber !== undefined
? BigInt(forkingUserConfig.blockNumber)
: undefined,
url:
forkingUserConfig.url !== undefined
? resolveConfigurationVariable(forkingUserConfig.url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ declare module "../../../types/test.js" {

export interface SolidityTestForkingUserConfig {
url?: SensitiveString;
blockNumber?: bigint;
blockNumber?: number | bigint;
rpcEndpoints?: Record<string, SensitiveString>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,26 @@ describe("config-resolution", () => {
);
});

it("should accept a bigint blockNumber and resolve it correctly", async () => {
const userForkingConfig = {
enabled: true,
url: "http://localhost:8545",
blockNumber: 1234n,
};
const cacheDir = "cache-dir";
const forkingConfig = resolveForkingConfig(
userForkingConfig,
cacheDir,
configVarResolver,
);

assert.ok(
isEdrNetworkForkingConfig(forkingConfig),
"forkingConfig is not an EdrNetworkForkingConfig",
);
assert.equal(forkingConfig.blockNumber, 1234n);
});

it("should use the default values for the optional fields if they are not provided", async () => {
const userForkingConfig = {
url: "http://localhost:8545",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2607,6 +2607,17 @@ describe("NetworkManagerImplementation", () => {
);

assertValidationErrors(validationErrors, []);

const validationErrorsBigint = await validateNetworkUserConfig(
edrConfig({
forking: {
url: "https://someurl.com",
blockNumber: 123n,
},
}),
);

assertValidationErrors(validationErrorsBigint, []);
});

it("should not validate an invalid network config", async () => {
Expand Down Expand Up @@ -2649,7 +2660,8 @@ describe("NetworkManagerImplementation", () => {
assertValidationErrors(validationErrors, [
{
path: ["networks", "hardhat", "forking", "blockNumber"],
message: "Expected number, received string",
message:
"Expected a nonnegative safe int or a nonnegative bigint",
Comment thread
ChristopherDedominici marked this conversation as resolved.
},
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,29 @@ describe("solidityTestConfigToSolidityTestRunnerConfigArgs", () => {
assert.deepEqual(args.rpcEndpoints, { a: "b" });
});

it("accepts a number blockNumber in forking config and converts it to bigint", async () => {
const userForkingConfig = {
url: "an_url",
blockNumber: 123,
rpcEndpoints: { a: "b" },
};

const resolvedForkingConfig = resolveSolidityTestForkingConfig(
userForkingConfig,
configVarResolver,
);

const args = await solidityTestConfigToSolidityTestRunnerConfigArgs({
chainType: GENERIC_CHAIN_TYPE,
projectRoot: process.cwd(),
config: { fuzz: { seed: "0x1234" }, forking: resolvedForkingConfig },
verbosity: 1,
generateGasReport: false,
});

assert.equal(args.forkBlockNumber, 123n);
});

it("sets generateGasReport to true", async () => {
const args = await solidityTestConfigToSolidityTestRunnerConfigArgs({
chainType: GENERIC_CHAIN_TYPE,
Expand Down
Loading