Hardhat Ignition is Hardhat's declarative deployment system, providing a more powerful and maintainable deployment approach:
Core Concepts:
-
Modular Deployment
- Use modules to define deployment logic
- Support dependencies between modules
- Declarative configuration instead of imperative scripts
-
Deployment State Management
- Automatically track deployment state
- Support incremental deployment
- Avoid duplicate deployments
Basic Usage:
Create deployment module:
javascriptconst { buildModule } = require("@nomicfoundation/hardhat-ignition/modules"); module.exports = buildModule("TokenModule", (m) => { const token = m.contract("MyToken", ["MyToken", "MTK", 18]); return { token }; });
Advanced Features:
- Parameterized Deployment
javascriptmodule.exports = buildModule("TokenModule", (m) => { const name = m.getParameter("name", "MyToken"); const symbol = m.getParameter("symbol", "MTK"); const token = m.contract("MyToken", [name, symbol, 18]); return { token }; });
- Dependency Management
javascriptmodule.exports = buildModule("DAppModule", (m) => { const token = m.contract("MyToken"); const sale = m.contract("TokenSale", [token]); // Call token contract function m.call(token, "transferOwnership", [sale]); return { token, sale }; });
- Using Existing Contracts
javascriptmodule.exports = buildModule("Module", (m) => { const existingContract = m.contractAt( "ExistingContract", "0x1234..." ); return { existingContract }; });
Deployment Commands:
bash# Deploy to local network npx hardhat ignition deploy ./ignition/modules/Module.js # Deploy to testnet npx hardhat ignition deploy ./ignition/modules/Module.js --network sepolia # Deploy with parameters npx hardhat ignition deploy ./ignition/modules/Module.js --parameters name:CustomToken,symbol:CTK # Verify deployment npx hardhat ignition deploy ./ignition/modules/Module.js --verify
Deployment Plan:
Ignition generates a deployment plan showing the operations to be performed:
bashnpx hardhat ignition plan ./ignition/modules/Module.js
Advantages:
- Declarative configuration is easier to understand
- Automatically handle deployment dependencies
- Support deployment verification
- Better error handling
- Suitable for complex multi-contract deployments
- Facilitates team collaboration