Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Hardhat Deployment Scripts
📖 Scenario: You are building a simple Ethereum smart contract deployment project using Hardhat. Hardhat is a tool that helps developers deploy smart contracts to Ethereum networks easily.In this project, you will create a deployment script step-by-step to deploy a basic smart contract called SimpleStorage that stores a number.
🎯 Goal: Build a Hardhat deployment script that deploys the SimpleStorage contract to a local Ethereum network.
📋 What You'll Learn
Create a deployment script file named deploy.js inside the scripts folder.
Use Hardhat Runtime Environment (hre) to deploy the contract.
Deploy the SimpleStorage contract and wait for deployment to finish.
Print the deployed contract address to the console.
💡 Why This Matters
🌍 Real World
Deploying smart contracts is a key step in launching blockchain applications like tokens, games, or finance apps.
💼 Career
Blockchain developers must write deployment scripts to automate contract deployment and testing in professional projects.
Progress0 / 4 steps
1
Create the deployment script file and import Hardhat
Create a file named deploy.js inside the scripts folder. Inside it, write const hre = require("hardhat"); to import Hardhat Runtime Environment.
Blockchain / Solidity
Hint
Use require("hardhat") to import Hardhat Runtime Environment as hre.
2
Write an async function to deploy the SimpleStorage contract
Write an async function named main. Inside it, get the contract factory for SimpleStorage using hre.ethers.getContractFactory("SimpleStorage") and store it in a variable called SimpleStorage. Then deploy the contract by calling SimpleStorage.deploy() and store the deployed contract in a variable called simpleStorage. Use await to wait for deployment to finish by calling simpleStorage.deployed().
Blockchain / Solidity
Hint
Use hre.ethers.getContractFactory to get the contract factory, then deploy and wait for deployment.
3
Call the main function and handle errors
After the main function, call it and add .then(() => process.exit(0)) to exit successfully. Add .catch((error) => { console.error(error); process.exit(1); }) to catch and print errors and exit with failure.
Blockchain / Solidity
Hint
Use main().then(...).catch(...) to run the async function and handle errors.
4
Print the deployed contract address
Inside the main function, after await simpleStorage.deployed(), add a console.log statement to print "SimpleStorage deployed to:" followed by simpleStorage.address.
Blockchain / Solidity
Hint
Use console.log("SimpleStorage deployed to:", simpleStorage.address) to print the address.
Practice
(1/5)
1. What is the main purpose of a Hardhat deployment script?
easy
A. To test smart contracts locally
B. To write smart contract logic
C. To automate deploying smart contracts to the blockchain
D. To create user interfaces for contracts
Solution
Step 1: Understand deployment scripts
Deployment scripts are used to automate the process of putting smart contracts on the blockchain.
Step 2: Differentiate from other tasks
Writing contract logic, testing, and UI creation are separate tasks from deployment.
Final Answer:
To automate deploying smart contracts to the blockchain -> Option C
Quick Check:
Deployment script = automate deployment [OK]
Hint: Deployment scripts automate contract deployment fast [OK]
Common Mistakes:
Confusing deployment with contract coding
Thinking deployment scripts test contracts
Assuming deployment scripts build UI
2. Which of the following is the correct way to get a contract factory in a Hardhat deployment script?
easy
A. const Contract = await ethers.getContractFactory('MyContract');
B. const Contract = ethers.getContract('MyContract');
C. const Contract = await ethers.deployContract('MyContract');
D. const Contract = ethers.createContractFactory('MyContract');
Solution
Step 1: Recall ethers.js method
The correct method to prepare a contract for deployment is ethers.getContractFactory with await.
Step 2: Check syntax correctness
Options B, C, and D use incorrect method names or miss await keyword.
Final Answer:
const Contract = await ethers.getContractFactory('MyContract'); -> Option A
Quick Check:
Use getContractFactory with await [OK]
Hint: Use await ethers.getContractFactory('Name') to prepare contract [OK]
Common Mistakes:
Omitting await before getContractFactory
Using wrong method names like getContract or deployContract
Confusing contract factory with contract instance
3. Consider this Hardhat deployment script snippet:
ethers.getContractFactory returns a promise, so it needs await.
Step 2: Verify other awaits
Token.deploy() and token.deployed() correctly use await.
Final Answer:
Missing await before ethers.getContractFactory -> Option A
Quick Check:
Always await getContractFactory [OK]
Hint: Always await getContractFactory call [OK]
Common Mistakes:
Forgetting await on getContractFactory
Confusing which calls need await
Assuming deploy() is synchronous
5. You want to deploy two contracts, Token and Marketplace, where Marketplace needs the Token address in its constructor. Which is the correct way to write the deployment script?
hard
A. Deploy Marketplace first, then deploy Token passing marketplace.address
B. Deploy Token first, then deploy Marketplace passing token.address
C. Deploy both contracts simultaneously without passing addresses
D. Deploy Token and Marketplace separately without constructor arguments
Solution
Step 1: Understand constructor dependency
Marketplace requires Token's address, so Token must be deployed first to get its address.
Step 2: Deploy in correct order
Deploy Token, then deploy Marketplace passing token.address to its constructor.
Step 3: Reject incorrect options
Deploying Marketplace first or simultaneously won't provide Token's address; omitting constructor args breaks dependency.
Final Answer:
Deploy Token first, then deploy Marketplace passing token.address -> Option B
Quick Check:
Deploy dependencies first, then dependent contracts [OK]
Hint: Deploy dependencies first, pass addresses to dependent contracts [OK]