
Hardhat: Testing and Deploying Smart Contracts
Testing and deploying smart contracts with Hardhat: scenario tests, fixtures, local and forked networks, coverage, fuzzing, staged deployments, and source verification.
Quick answer
With Hardhat, first test the contract on a local network with scenario-based tests: the happy path, failures, unauthorized access, and edge cases. Use fixtures for speed and repeatability, fork mainnet to interact with real protocols, add coverage and fuzz tests, then deploy with a repeatable script or module such as Hardhat Ignition, to a testnet first and mainnet after, and verify the source on the block explorer. The deployment private key must never be in the repository.
Write scenario-driven tests
Write tests that cover real user scenarios, not just individual functions. For every important function test at least four cases: the happy path, failure with the right error, unauthorized access, and edge cases such as zero, the maximum value, or a repeated call.
Besides return values, check emitted events and changes in account balances; many bugs hide in exactly those details.
Fixtures, the local network, and forking
Hardhat's local network mines transactions instantly and lets you control time and accounts. With fixtures, the initial state, such as deploying contracts and distributing tokens, is built once and restored for every test, so tests stay fast and independent.
If your contract works with existing protocols such as a DEX or an oracle, fork mainnet so tests run against the real state of those contracts.
Beyond unit tests
A contract that holds funds needs more layers of assurance:
- Code coverage reports to find untested paths
- Fuzz tests with random inputs to uncover unexpected states
- Invariant tests for rules that must always hold, such as total balances adding up
- Static analysis, for example with Slither, before every release
Repeatable, staged deployments
Do not deploy by hand, one command at a time. A script or a declarative module such as Hardhat Ignition records deployment order, parameters, and final addresses, and resumes from the same point if something fails midway.
The safe path is always the same: the local network, then a testnet such as Sepolia for Ethereum or the matching layer-2 testnet, and finally mainnet. Keep each network's addresses and parameters in the repository so everyone knows which version is deployed where.
Key security and after deployment
A few simple rules that prevent the most common disasters:
- Never put a private key in code or committed files; use an environment variable or an encrypted keystore
- Keep the deployer wallet separate from the wallet that holds assets
- After deployment, transfer contract ownership to a multisig wallet
- Verify the contract source on the explorer and monitor important events
Frequently asked questions
Hardhat or Foundry?
Both are excellent. Foundry is known for very fast Solidity-native tests and fuzzing, and Hardhat for its JavaScript and TypeScript ecosystem and many plugins. Many teams use both side by side.
Which testnet should we use?
Usually Sepolia for Ethereum apps. If you deploy on a layer 2 such as Arbitrum or Base, use that layer 2's own testnet so behavior and costs are more realistic.
Why verify the contract source?
Source verification lets anyone read the contract's real code on the explorer and interact with it. It increases transparency and user trust, and it is essential for audits and support.
