
This chapter delves into the heart of Hardhat configuration, explaining the `hardhat.config.js
` (or `hardhat.config.ts
`) report and its critical function in defining your development environment.
Table of Contents
hardhat.config.js
/ hardhat.config.ts
Explained
The `hardhat.config.js
` (or its TypeScript equal, `hardhat.config.ts
`) file, positioned in the root of your Hardhat challenge, is the relevant configuration hub. It’s in which you customize Hardhat’s behavior, specifying things just like the Solidity compiler model, community settings, and different plugins. Think of it as the control panel to your smart agreement development environment.Think of it as the control panel for your smart contract development environment.
A fundamental hardhat.config.js
seems like this:
JavaScript
require("@nomicfoundation/hardhat-toolbox");
/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
solidity: "0.8.19", // Example Solidity version
networks: {
hardhat: { // Default Hardhat network
},
},
};
And its TypeScript equivalent (hardhat.config.ts
):
TypeScript
import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
const config: HardhatUserConfig = {
solidity: "0.8.19", // Example Solidity version
networks: {
hardhat: { // Default Hardhat network
},
},
};
export default config;
Let’s break down the key sections:
require("@nomicfoundation/hardhat-toolbox");
(orimport ...
in TypeScript): This line imports the Hardhat Toolbox plugin, providing essential functionalities like compilation, testing, and network interaction. It’s highly recommended to use this. Older Hardhat versions might use@nomiclabs/hardhat-ethers
.solidity
: This section specifies the Solidity compiler version(s) your project uses. You can specify a single version (as shown above) or an object to support multiple versions and their settings. For example:
JavaScript
solidity: {
compilers: [
{
version: "0.8.19",
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
{
version: "0.7.6", // Example of supporting another version
},
],
};
networks
: This is where you configure the different networks you want to deploy and interact with (e.g., Hardhat Network, Goerli, Sepolia, Polygon, Base).
Configuring Solidity Version
As shown above, the solidity
field is crucial. Using a specific version ensures consistency and avoids compatibility issues. Here’s a more detailed look:
- Single Version:
solidity: "0.8.19"
(or any valid SemVer string). - Multiple Versions: Use an object with a
compilers
array. Each compiler entry can have aversion
and optionalsettings
(like optimizer settings). This allows you to compile your contracts with different Solidity versions if needed. - Optimizer: The
optimizer
setting is important for production deployments. Settingenabled: true
optimizes the bytecode size, reducing gas costs.runs
specifies the number of times the optimizer should run. A higher number of runs generally leads to smaller bytecode but takes longer to compile.
Setting Up Networks (Ethereum, Polygon, Base, etc.)
The networks
section defines the networks Hardhat can connect to. The default hardhat
network is a local development network that Hardhat spins up for you. To interact with other networks, you need to configure them:
JavaScript
networks: {
hardhat: {}, // Default Hardhat network
goerli: {
url: process.env.GOERLI_URL || "", // URL of your Goerli node
accounts: process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [], // Your private key(s)
},
polygon: {
url: process.env.POLYGON_URL || "",
accounts: process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
},
base: {
url: process.env.BASE_URL || "",
accounts: process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
}
},
url
: The URL of the node you need to hook up with. You’ll want to gain this from a node company like Alchemy, Infura, or QuickNode, or via strolling your very own node.accounts
:An array of personal keys. Important: Never commit your personal keys to model manage.. Use environment variables (as shown above) to keep them secure.
Using Environment Variables (dotenv)
Using environment variables is essential for managing sensitive information like API keys and private keys. The dotenv
package makes this easy.
- Installation:
npm install dotenv
(oryarn add dotenv
). - Usage: Create a `
.env
` file in the root of your undertaking and upload your environment variables:
GOERLI_URL=your_goerli_url
PRIVATE_KEY=your_private_key
POLYGON_URL=your_polygon_url
BASE_URL=your_base_url
- Loading: Require
dotenv
at the top of yourhardhat.config.js
:
JavaScript
require("dotenv").config(); // For .js
Or in hardhat.config.ts
:
TypeScript
import * as dotenv from "dotenv";
dotenv.config();
Now you may access your environment variables the use of `process.env
` (as shown within the network configuration instance).
FAQ:
- Q: Why use environment variables for private keys?
- A: To prevent accidentally exposing them in your code repository, which would compromise your funds.
- Q: Should I commit my
.env
file? - A: The
.env
document incorporates sensitive facts and must never be committed to model control. Add.env
in your.gitignore
report. - Q: What if I need exclusive
.env
documents for different environments (development, checking out, manufacturing)? - A: You can use
.env.development
,.env.test
,.env.production
, and load them conditionally the use ofdotenv-cli
or comparable tools. Hardhat also supports specifying different configuration files using the--config
flag. - Q: How can I manage multiple private keys?
- A: You can provide an array of private keys in the
accounts
field, or use a more advanced approach with HD wallets.
This chapter has provided a comprehensive overview of Hardhat configuration. Understanding these concepts is fundamental to building and deploying smart contracts effectively. In the next chapter, we’ll explore contract compilation and testing.