So the problem is I have defined a hardhat config like this
import { HardhatUserConfig, task } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
import * as dotenv from "dotenv";
dotenv.config();
task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
const accounts = await hre.ethers.getSigners();
for (const account of accounts) {
console.log(account.address);
}
})
const config: HardhatUserConfig = {
solidity: "0.8.17",
networks: {
// defined defaultNetwork as hardhat so it would use it for testing.
defaultNetwork: {
url: 'hardhat',
},
hardhat: {},
goerli: {
url: process.env.STAGING_QUICKNODE_KEY,
accounts: [process.env.PRIVATE_KEY!]
}
}
};
export default config;
when I run this command on my local machine npx hardhat test
it works fine because I guess maybe I have a .env
file, but if I remove this .env
file or run this command on GitHub workflow
it would give me this error.
Error HH8: There are one or more errors in your config file:
* Invalid value undefined for HardhatConfig.networks.goerli.url - Expected a value of type string.
* Invalid account: #0 for the network: goerli - Expected string, received undefined
now the problem is I don't want to upload my .env
file on GitHub because it contains my secret key information. I have seen this answer as well and tried it also How do I use different config for testing vs. deployment hardhat solidity?
so I tried to run this command npx hardhat test --network hardhat
but it would the same error as above.
I want to know if there is any way to change this config
object at runtime in *.spec.ts
files, then I can try that way or if you know of any other hacks, please let me know.
The errors mentioned in your question are related to the fact that the config values are invalid. Hardhat expects the accounts
items to be of type string
(and in the format of "0x
followed by 64 hex characters") but they are undefined
.
I'm not sure about the proper solution (anyone else feel free to provide a better answer), but you can use a dummy key as a workaround.
goerli: {
url: process.env.STAGING_QUICKNODE_KEY || "https://dummy.url.com",
accounts: [process.env.PRIVATE_KEY || "0x1234567890123456789012345678901234567890123456789012345678901234"]
}
This validation happens before running the tests. So even if you use the hardhat
network for tests, it won't allow you to run them if another part of the config is invalid.
npx hardhat test
and npx hardhat test --network hardhat
is effectively the same command, as hardhat
is the default network.