Deployment scripts help you put your smart contracts on the blockchain automatically. They save time and avoid mistakes compared to manual deployment.
Hardhat deployment scripts in Blockchain / Solidity
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Blockchain / Solidity
async function main() { const ContractFactory = await ethers.getContractFactory("ContractName"); const contract = await ContractFactory.deploy(); await contract.deployed(); console.log("Contract deployed to:", contract.address); } main().catch((error) => { console.error(error); process.exitCode = 1; });
The main function is async because deployment takes time.
ethers.getContractFactory prepares the contract for deployment.
Examples
Blockchain / Solidity
async function main() { const Token = await ethers.getContractFactory("MyToken"); const token = await Token.deploy("My Token", "MTK", 1000); await token.deployed(); console.log("Token deployed at", token.address); }
Blockchain / Solidity
async function main() { const ContractA = await ethers.getContractFactory("ContractA"); const a = await ContractA.deploy(); await a.deployed(); const ContractB = await ethers.getContractFactory("ContractB"); const b = await ContractB.deploy(a.address); await b.deployed(); console.log("ContractA at", a.address); console.log("ContractB at", b.address); }
Sample Program
This script deploys a simple Greeter contract with a greeting message.
Blockchain / Solidity
async function main() { const Greeter = await ethers.getContractFactory("Greeter"); const greeter = await Greeter.deploy("Hello, Hardhat!"); await greeter.deployed(); console.log("Greeter deployed to:", greeter.address); } main().catch((error) => { console.error(error); process.exitCode = 1; });
Important Notes
Always handle errors in deployment scripts to catch problems early.
Use await contract.deployed() to wait until the contract is fully deployed.
Deployment addresses will be different each time unless you use deterministic deployment.
Summary
Deployment scripts automate putting contracts on the blockchain.
They use async functions and ethers.getContractFactory to prepare contracts.
You can deploy multiple contracts and pass addresses between them.
Practice
1. What is the main purpose of a Hardhat deployment script?
easy
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 CQuick 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
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 AQuick 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:
What will be printed by
const Token = await ethers.getContractFactory('Token');
const token = await Token.deploy();
await token.deployed();
const Sale = await ethers.getContractFactory('Sale');
const sale = await Sale.deploy(token.address);
await sale.deployed();
console.log(sale.address);What will be printed by
console.log(sale.address)?medium
Solution
Step 1: Understand deployment sequence
The Token contract is deployed first, then its address is passed to Sale contract deployment.Step 2: Identify sale.address value
After deployment, sale.address holds the Sale contract's blockchain address, which is logged.Final Answer:
The deployed address of the Sale contract -> Option DQuick Check:
sale.address = Sale contract address [OK]
Hint: Deployed contract instance has .address property [OK]
Common Mistakes:
- Confusing token.address with sale.address
- Assuming sale.address is undefined before deployment
- Thinking passing token.address causes error
4. Identify the error in this Hardhat deployment script snippet:
const Token = ethers.getContractFactory('Token');
const token = await Token.deploy();
await token.deployed();medium
Solution
Step 1: Check getContractFactory usage
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 AQuick 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
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 BQuick Check:
Deploy dependencies first, then dependent contracts [OK]
Hint: Deploy dependencies first, pass addresses to dependent contracts [OK]
Common Mistakes:
- Deploying dependent contract before dependency
- Not passing required constructor arguments
- Deploying contracts simultaneously ignoring dependencies
