From d6caa0397cf560b30118242a0b4cd6c6f886069e Mon Sep 17 00:00:00 2001 From: Yuri Tkachenko Date: Tue, 14 Jan 2025 15:54:21 +0000 Subject: [PATCH] fix: refactor hardhat config file --- .env.example | 2 +- CONTRIBUTING.md | 2 +- hardhat.config.ts | 47 +++--------------------------------- hardhat.helpers.ts | 32 ++++++++++++++++++++++++ tasks/index.ts | 1 + tasks/solidity-get-source.ts | 18 ++++++++++++++ 6 files changed, 57 insertions(+), 45 deletions(-) create mode 100644 hardhat.helpers.ts create mode 100644 tasks/solidity-get-source.ts diff --git a/.env.example b/.env.example index b654199fd..f21c1c63a 100644 --- a/.env.example +++ b/.env.example @@ -28,7 +28,7 @@ MAINNET_RPC_URL=http://localhost:8545 # RPC URL for Hardhat Network forking, required for running tests on mainnet fork with tracing (Infura, Alchemy, etc.) # https://hardhat.org/hardhat-network/docs/guides/forking-other-networks#forking-other-networks -HARDHAT_FORKING_URL= +HARDHAT_FORKING_URL=https://eth.drpc.org # https://docs.lido.fi/deployed-contracts MAINNET_LOCATOR_ADDRESS=0xC1d0b3DE6792Bf6b4b37EccdcC24e45978Cfd2Eb diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b95f36970..a93d16690 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -328,7 +328,7 @@ mainnet environment, allowing you to run integration tests with trace logging. > [!NOTE] > Ensure that `HARDHAT_FORKING_URL` is set to Ethereum Mainnet RPC and `MAINNET_*` environment variables are set in the -> `.env` file (refer to `.env.example` for guidance). +> `.env` file (refer to `.env.example` for guidance). Otherwise, the tests will run against the Scratch deployment. ```bash # Run all integration tests diff --git a/hardhat.config.ts b/hardhat.config.ts index 76ccd1b46..e046e8fdb 100644 --- a/hardhat.config.ts +++ b/hardhat.config.ts @@ -1,5 +1,4 @@ -import { existsSync, readFileSync } from "node:fs"; -import path from "node:path"; +import * as process from "node:process"; import "@nomicfoundation/hardhat-chai-matchers"; import "@nomicfoundation/hardhat-toolbox"; @@ -12,41 +11,15 @@ import "hardhat-tracer"; import "hardhat-watcher"; import "hardhat-ignore-warnings"; import "hardhat-contract-sizer"; -import { globSync } from "glob"; -import { TASK_COMPILE_SOLIDITY_GET_SOURCE_PATHS } from "hardhat/builtin-tasks/task-names"; -import { HardhatUserConfig, subtask } from "hardhat/config"; +import { HardhatUserConfig } from "hardhat/config"; import { mochaRootHooks } from "test/hooks"; import "./tasks"; -const RPC_URL: string = process.env.RPC_URL || ""; -const ACCOUNTS_PATH = "./accounts.json"; - -const HARDHAT_FORKING_URL = process.env.HARDHAT_FORKING_URL || ""; - -const INTEGRATION_WITH_SCRATCH_DEPLOY = process.env.INTEGRATION_WITH_SCRATCH_DEPLOY || "off"; - -/* Determines the forking configuration for Hardhat */ -function getHardhatForkingConfig() { - if (INTEGRATION_WITH_SCRATCH_DEPLOY === "on" || !HARDHAT_FORKING_URL) { - return undefined; - } - return { url: HARDHAT_FORKING_URL }; -} +import { getHardhatForkingConfig, loadAccounts } from "./hardhat.helpers"; -function loadAccounts(networkName: string) { - // TODO: this plaintext accounts.json private keys management is a subject - // of rework to a solution with the keys stored encrypted - if (!existsSync(ACCOUNTS_PATH)) { - return []; - } - const content = JSON.parse(readFileSync(ACCOUNTS_PATH, "utf-8")); - if (!content.eth) { - return []; - } - return content.eth[networkName] || []; -} +const RPC_URL: string = process.env.RPC_URL || ""; const config: HardhatUserConfig = { defaultNetwork: "hardhat", @@ -185,16 +158,4 @@ const config: HardhatUserConfig = { }, }; -// a workaround for having an additional source directory for compilation -// see, https://github.com/NomicFoundation/hardhat/issues/776#issuecomment-1713584386 -subtask(TASK_COMPILE_SOLIDITY_GET_SOURCE_PATHS).setAction(async (_, hre, runSuper) => { - const paths = await runSuper(); - - const otherDirectoryGlob = path.join(hre.config.paths.root, "test", "**", "*.sol"); - // Don't need to compile test, helper and script files that are not part of the contracts for Hardhat. - const otherPaths = globSync(otherDirectoryGlob).filter((x) => !/\.([ths]\.sol)$/.test(x)); - - return [...paths, ...otherPaths]; -}); - export default config; diff --git a/hardhat.helpers.ts b/hardhat.helpers.ts new file mode 100644 index 000000000..47f6533b8 --- /dev/null +++ b/hardhat.helpers.ts @@ -0,0 +1,32 @@ +import { existsSync, readFileSync } from "node:fs"; + +/* Determines the forking configuration for Hardhat */ +export function getHardhatForkingConfig() { + const forkingUrl = process.env.HARDHAT_FORKING_URL || ""; + + if (!forkingUrl) { + // Scratch deploy, need to disable CSM + process.env.INTEGRATION_ON_SCRATCH = "on"; + process.env.INTEGRATION_WITH_CSM = "off"; + return undefined; + } + + return { url: forkingUrl }; +} + +// TODO: this plaintext accounts.json private keys management is a subject +// of rework to a solution with the keys stored encrypted +export function loadAccounts(networkName: string) { + const accountsPath = "./accounts.json"; + + if (!existsSync(accountsPath)) { + return []; + } + + const content = JSON.parse(readFileSync(accountsPath, "utf-8")); + if (!content.eth) { + return []; + } + + return content.eth[networkName] || []; +} diff --git a/tasks/index.ts b/tasks/index.ts index ada28baca..04b17d7c9 100644 --- a/tasks/index.ts +++ b/tasks/index.ts @@ -1,2 +1,3 @@ export * from "./verify-contracts"; export * from "./extract-abis"; +export * from "./solidity-get-source"; diff --git a/tasks/solidity-get-source.ts b/tasks/solidity-get-source.ts new file mode 100644 index 000000000..522a4af59 --- /dev/null +++ b/tasks/solidity-get-source.ts @@ -0,0 +1,18 @@ +import path from "node:path"; + +import { globSync } from "glob"; +import { TASK_COMPILE_SOLIDITY_GET_SOURCE_PATHS } from "hardhat/builtin-tasks/task-names"; +import { subtask } from "hardhat/config"; + +// a workaround for having an additional source directory for compilation +// see, https://github.com/NomicFoundation/hardhat/issues/776#issuecomment-1713584386 + +subtask(TASK_COMPILE_SOLIDITY_GET_SOURCE_PATHS).setAction(async (_, hre, runSuper) => { + const paths = await runSuper(); + + const otherDirectoryGlob = path.join(hre.config.paths.root, "test", "**", "*.sol"); + // Don't need to compile test, helper and script files that are not part of the contracts for Hardhat. + const otherPaths = globSync(otherDirectoryGlob).filter((x) => !/\.([ths]\.sol)$/.test(x)); + + return [...paths, ...otherPaths]; +});