Lesson 3 of 4 Deploying Smart Contracts

Deploying with Remix and Hardhat

This lesson shows the two main ways to deploy: interactively with Remix and a browser wallet, and with Hardhat Ignition modules and configuration variables for repeatable deployments, including resuming and inspecting deployments.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe the deployment options of Remix.
  • Describe how Hardhat reads network secrets with configuration variables.
  • Explain how Hardhat Ignition modules deploy and resume.
  • Compare Ignition with scripted deployments.

Deploying with Remix

Remix's Deploy & Run Transactions panel deploys the selected compiled contract. Its main options are described below.

Environment
Where to deploy: a Remix VM (an in-browser simulated chain), Injected Provider - MetaMask (the browser wallet's network), or an external node by URL.
Account
The sending account; with MetaMask, the wallet's selected account.
Gas limit and Value
The gas limit for the transaction and any ether sent to a payable constructor or function.
Deploy
Sends the creation transaction; constructor arguments are typed into the field beside it.
At Address
Loads an already-deployed contract at an address, using the selected contract's ABI.

With an injected provider, MetaMask shows the transaction for approval, including the estimated fee. After deployment, the contract appears under Deployed Contracts, where each function can be called.

Scenario questions

Remix environments

Exam skills 4.2.2

Choose the right Remix environment for each deployment.

Log in to try this activity Interactive activities are free with an account.

Configuring Hardhat for a network

A Hardhat 3 configuration defines each network with a URL and accounts. Secrets are read with configVariable, which resolves them at run time from the Hardhat keystore or environment variables.

import hardhatToolboxMochaEthers from "@nomicfoundation/hardhat-toolbox-mocha-ethers";
import { configVariable, defineConfig } from "hardhat/config";

export default defineConfig({
  plugins: [hardhatToolboxMochaEthers],
  solidity: "0.8.34",
  networks: {
    sepolia: {
      type: "http",
      url: configVariable("SEPOLIA_RPC_URL"),
      accounts: [configVariable("SEPOLIA_PRIVATE_KEY")],
    },
  },
});
npx hardhat keystore set SEPOLIA_RPC_URL
npx hardhat keystore set SEPOLIA_PRIVATE_KEY

Each keystore set prompts for the value and for the keystore password; the values are stored encrypted outside the project.

Select all that apply

A safe network configuration

Exam skills 4.2.3 · 4.3.2

Identify sound practices for configuring a Hardhat 3 network.

Log in to try this activity Interactive activities are free with an account.

Hardhat Ignition modules

Hardhat Ignition deploys from declarative modules that describe what should exist, not the step-by-step process.

import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";

export default buildModule("ClubModule", (m) => {
  const owner = m.getAccount(0);
  const token = m.contract("ClubToken", [owner]);
  m.call(token, "mint", [owner, 1000n * 10n ** 18n]);
  return { token };
});
npx hardhat ignition deploy ignition/modules/ClubModule.js --network sepolia

Ignition works out the order of operations from dependencies, sends the transactions, waits for confirmations and records everything in ignition/deployments/chain-11155111/, including deployed_addresses.json. If the deployment is interrupted, running the same command resumes it; running it again after success changes nothing. --network localhost deploys to a running Hardhat node, and omitting --network deploys to a temporary in-process network, which is a quick way to test the module.

Scenario questions

Working with Ignition

Exam skills 4.2.3

Predict how Hardhat Ignition behaves.

Log in to try this activity Interactive activities are free with an account.

Scripted deployments

Deployments can also be written as scripts and run with npx hardhat run scripts/deploy.js --network sepolia, using ethers.js (await ethers.deployContract("ClubToken", [owner])) or web3.js. Scripts are flexible but must handle failures, avoid redeploying on reruns and record addresses themselves, which Ignition does automatically. Truffle's numbered migration scripts, found in older material, served the same purpose.

Sort into groups

Ignition or a script?

Exam skills 4.2.3

Decide which approach each statement describes.

Log in to try this activity Interactive activities are free with an account.
Worked example

An interrupted deployment

An Ignition module deploys a token, a staking contract and then calls grantRole. The connection drops after the staking contract is deployed. What happens when the developer runs the same deploy command again?

Ignition reads its journal in ignition/deployments/chain-<id>, sees that the token and staking contract already exist, and executes only the remaining grantRole call. A naive script would have deployed both contracts again, wasting gas and creating duplicate addresses.

Worked example

Where does this deploy?

A developer runs npx hardhat ignition deploy ignition/modules/ClubModule.js without --network, sees "successfully deployed", but cannot find the contract from a web3.js script connected to http://127.0.0.1:8545. Why?

Without --network, Hardhat uses a temporary in-process network that disappears when the command ends. To deploy to the running node the script connects to, use --network localhost.

Check yourself

What does Hardhat Ignition do if a deployment command is run again after it was interrupted?

Show answer

It reads the deployment journal for that chain, skips the steps already completed and executes only the remaining ones.

Exam Tips

Remember for the exam

  • Remix: choose Environment carefully; Injected Provider deploys to MetaMask's network; At Address loads existing contracts.
  • Hardhat reads secrets with configVariable from the keystore or environment variables.
  • Ignition modules are declarative, record deployments per chain and resume after interruption.
  • Without --network, Hardhat deploys to a temporary in-process network.