Hardhat's forking feature allows developers to create a local copy of the Ethereum blockchain at a specific block height. This enables them to test and develop their smart contracts in an environment that closely mimics the mainnet or any other Ethereum network, complete with real-world data and state. Forking is particularly useful for testing how contracts will behave with existing on-chain data without the risk of affecting the actual blockchain.
Why Use Forking?
- Testing with Real Data: Developers can interact with existing smart contracts and data, allowing for more realistic testing scenarios.
- Safe Experimentation: You can experiment with different strategies, upgrades, or changes to your contracts without risking real funds or affecting the mainnet.
- Debugging: Forking allows developers to replicate issues that occur on the mainnet, making it easier to debug and resolve problems.
How to Use the Forking Feature in Hardhat
To use the forking feature, you'll need to follow these steps:
- Set Up Your Hardhat Project: If you haven't already created a Hardhat project, you can do so by running the following commands:
mkdir my-hardhat-project
cd my-hardhat-project
npx hardhat
Follow the prompts to create a sample project.
1. Install Dependencies
To use the forking feature, you need to install the @nomiclabs/hardhat-ethers
plugin, which provides the necessary tools for interacting with the Ethereum network:
npm install --save-dev @nomiclabs/hardhat-ethers ethers
2. Configure Forking in Hardhat
Next, you need to configure Hardhat to enable forking. Open the hardhat.config.js
file and add the following configuration:
require("@nomiclabs/hardhat-ethers");
module.exports = {
solidity: "0.8.0",
networks: {
hardhat: {
fork: {
url: "https://eth-mainnet.alchemyapi.io/v2/YOUR_ALCHEMY_API_KEY",
blockNumber: 12345678 // Optional: Specify a block number to fork from
}
}
}
};
In this configuration:
- Replace
YOUR_ALCHEMY_API_KEY
with your actual Alchemy API key or any other Ethereum node provider URL. - You can optionally specify a
blockNumber
from which to fork. If omitted, it will fork from the latest block.
3. Running the Forked Network
To start the Hardhat node with forking enabled, run the following command:
npx hardhat node
This will start a local Ethereum node that is a fork of the mainnet at the specified block number. You can now interact with this forked network just like you would with a regular Hardhat network.
4. Interacting with the Forked Network
To interact with the forked network, you can run scripts or use the Hardhat console. For example, you can create a script to interact with existing contracts:
// scripts/interact.js
async function main() {
const provider = new ethers.providers.JsonRpcProvider("http://localhost:8545");
const contractAddress = "0x..."; // Replace with the address of an existing contract
const abi = [ /* ABI of the contract */ ];
const contract = new ethers.Contract(contractAddress, abi, provider);
const value = await contract.someFunction(); // Replace with a function from the contract
console.log("Value from contract:", value.toString());
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Run the script with:
npx hardhat run scripts/interact.js --network localhost
Conclusion
Hardhat's forking feature is an invaluable tool for developers looking to test and develop smart contracts in a realistic environment.