Skip to content

Deploying a contract

Now that you’ve written and tested your contract, it’s time to deploy it. You’ll use Hardhat Ignition, our official deployment plugin.

The main concept in Ignition is modules: TypeScript files that use a simple API to describe a deployment.

Write an Ignition module that deploys the Counter contract and calls incBy on it. Create a new file at ignition/modules/Counter.ts with the following content:

ignition/modules/Counter.ts
import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";
export default buildModule("CounterModule", (m) => {
const counter = m.contract("Counter");
m.call(counter, "incBy", [5n]);
return { counter };
});

This module deploys an instance of Counter and calls incBy(5) once it’s deployed. These functions don’t execute the deployment transactions or contract calls immediately. They describe what should be done. Ignition then executes the deployment steps in the right order, handling dependencies, recovering from errors, and ensuring everything runs correctly.

Check that the deployment module runs without errors by running it in a locally simulated network, similar to how you ran the TypeScript tests:

Terminal window
npx hardhat ignition deploy ignition/modules/Counter.ts

To deploy your contracts in a persistent local network, use Hardhat’s node task. First, run the node task in one terminal:

Terminal window
npx hardhat node

In another terminal, run the deployment with --network localhost to execute the Ignition module against the local node:

Terminal window
npx hardhat ignition deploy ignition/modules/Counter.ts --network localhost

Ignition remembers the deployment state. If you run the deployment again, nothing will happen the second time because the module was already executed. Try rerunning the previous command to see it in action.

Now you’re ready to deploy your contract to a live network. You’ll use Sepolia for this example. Make sure you have an RPC URL and an account with some Sepolia ETH in it.

Add the Sepolia network to your config by updating hardhat.config.ts:

hardhat.config.ts
import hardhatToolboxViemPlugin from "@nomicfoundation/hardhat-toolbox-viem";
import { defineConfig } from "hardhat/config";
export default defineConfig({
plugins: [hardhatToolboxViemPlugin],
solidity: {
version: "0.8.28",
},
networks: {
sepolia: {
type: "http",
url: "<SEPOLIA_RPC_URL>",
accounts: ["<SEPOLIA_PRIVATE_KEY>"],
},
},
});

Replace <SEPOLIA_RPC_URL> with your Sepolia RPC URL and <SEPOLIA_PRIVATE_KEY> with the private key of an account that has Sepolia ETH.

Now run the Ignition deployment module targeting the Sepolia network:

Terminal window
npx hardhat ignition deploy ignition/modules/Counter.ts --network sepolia

If everything goes well, you’ll see output indicating successful deployment. You can then go to Sepolia Etherscan and validate that the contract is deployed at the address shown in the output.