In a Hardhat project, obtaining the chain ID can be done in a few ways, depending on whether you want to retrieve it programmatically within a script or check it during your development process. Here’s a step-by-step guide on how to get the chain ID in different scenarios:
1. Getting the Chain ID Programmatically
You can write a script to get the chain ID of the network you are connected to. Here’s a simple example using Hardhat’s ethers.js
plugin:
Step-by-Step Guide
-
Set up your Hardhat environment:
Make sure you have a Hardhat project set up. If not, you can create one using:
npx hardhat
Choose “Create an empty hardhat.config.js” or any of the other options as needed.
-
Install necessary dependencies:
Ensure that you have
ethers
installed in your project, as it’s commonly used with Hardhat for interacting with Ethereum networks:npm install --save-dev @nomiclabs/hardhat-ethers ethers
-
Create a script to get the chain ID:
Create a new script file in your project, e.g.,
scripts/getChainId.js
, and add the following code:const hre = require("hardhat"); async function main() { // Get the network provider const network = await hre.ethers.provider.getNetwork(); // Log the chain ID console.log(`Connected to network: ${network.name}`); console.log(`Chain ID: ${network.chainId}`); } main() .then(() => process.exit(0)) .catch((error) => { console.error(error); process.exit(1); });
-
Run the script:
Execute the script using Hardhat:
npx hardhat run scripts/getChainId.js --network <network-name>
Replace
<network-name>
with the name of the network you’re targeting (e.g.,localhost
,rinkeby
,mainnet
, etc.). This will output the chain ID of the specified network.
2. Checking the Chain ID in Hardhat Config
If you want to check the chain ID configured for a specific network in your Hardhat setup, you can do so in the hardhat.config.js
file. Here’s how:
-
Open
hardhat.config.js
:Find the configuration for the network you are interested in. For example:
module.exports = { solidity: "0.8.18", networks: { hardhat: { chainId: 31337, // Default chain ID for Hardhat Network }, localhost: { url: "http://127.0.0.1:8545", chainId: 1337, // Chain ID for your local network }, rinkeby: { url: process.env.RINKEBY_URL || "", accounts: process.env.PRIVATE_KEY ? [process.env.PRIVATE_KEY] : [], chainId: 4, // Chain ID for Rinkeby Testnet }, // Add other network configurations as needed }, };
-
Modify or View Chain ID:
You can modify or view the chain ID for each network here. This is useful for ensuring that your scripts and tests are connecting to the correct network.
3. Using the Console
Hardhat also provides an interactive console, which can be used to fetch the chain ID on the fly:
-
Open the Hardhat Console:
Start the Hardhat console using:
npx hardhat console --network <network-name>
-
Run a Command to Get the Chain ID:
In the console, run the following command to fetch the chain ID:
const network = await ethers.provider.getNetwork(); console.log(network.chainId);
Conclusion
Using these methods, you can easily retrieve the chain ID of the network you’re working with in a Hardhat environment. This can help ensure your scripts and contracts are deployed and tested on the correct network. Whether programmatically or via configuration checks, knowing the chain ID is vital for deploying to and interacting with different blockchain networks correctly.