Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

Hardhat deployment scripts in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

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
Concept Flow - Hardhat deployment scripts
Start script
Load Hardhat Runtime Environment
Get contract factory
Deploy contract
Wait for deployment
Log deployed address
End script
The deployment script loads Hardhat, prepares the contract, deploys it, waits for confirmation, then logs the address.
Execution Sample
Blockchain / Solidity
async function main() {
  const hre = require('hardhat');
  const Contract = await hre.ethers.getContractFactory("MyContract");
  const contract = await Contract.deploy();
  await contract.deployed();
  console.log("Deployed to:", contract.address);
}
main();
This script deploys 'MyContract' to the blockchain and prints its address.
Execution Table
StepActionEvaluationResult
1Load Hardhat Runtime Environment (hre)hre.ethers availableReady to interact with contracts
2Get contract factory for 'MyContract'hre.ethers.getContractFactory('MyContract')Contract factory object obtained
3Deploy contractContract.deploy()Deployment transaction sent
4Wait for deployment to completecontract.deployed()Contract deployed and address assigned
5Log deployed contract addressconsole.log(contract.address)Address printed to console
6Script endsNo further actionsDeployment complete
💡 Script ends after logging deployed contract address
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
hreundefinedHardhat Runtime Environment objectSameSameSame
ContractundefinedContract factory for MyContractSameSameSame
contractundefinedundefinedDeployment transaction objectDeployed contract instance with addressSame
Key Moments - 3 Insights
Why do we use 'await' before deploy() and deployed()?
Because deploy() sends a transaction that takes time to confirm, and deployed() waits until the contract is fully deployed. See execution_table steps 3 and 4.
What is the contract factory and why do we need it?
The contract factory is like a blueprint to create new contract instances. We get it in step 2 to prepare deployment.
Why do we log contract.address after deployment?
Because the address is only assigned after deployment completes (step 4), so logging it before would be undefined.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'contract' after step 3?
ADeployment transaction object
BContract factory object
CDeployed contract instance with address
DUndefined
💡 Hint
Check variable_tracker row for 'contract' after Step 3
At which step does the script wait for the contract to be fully deployed?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
See execution_table action 'Wait for deployment to complete'
If we remove 'await' before contract.deployed(), what happens?
AScript waits correctly for deployment
BDeployment will fail
Ccontract.address will be undefined when logged
DScript will throw a syntax error
💡 Hint
Refer to key_moments about why 'await' is needed before deployed()
Concept Snapshot
Hardhat deployment script steps:
1. Load Hardhat Runtime Environment (hre)
2. Get contract factory with hre.ethers.getContractFactory('ContractName')
3. Deploy contract with await deploy()
4. Wait for deployment with await deployed()
5. Log deployed contract address
Use async/await to handle asynchronous blockchain operations.
Full Transcript
This visual execution shows how a Hardhat deployment script runs step-by-step. First, the script loads the Hardhat Runtime Environment (hre) to access blockchain tools. Then it gets a contract factory for the contract named 'MyContract'. Next, it deploys the contract by sending a transaction and waits for the deployment to complete. After deployment, the contract address is available and logged to the console. Variables like 'contract' change from undefined to a deployment transaction object, then to a deployed contract instance with an address. Key points include using 'await' to wait for asynchronous steps and logging the address only after deployment finishes. The quiz tests understanding of these steps and variable states.

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

  1. Step 1: Understand deployment scripts

    Deployment scripts are used to automate the process of putting smart contracts on the blockchain.
  2. Step 2: Differentiate from other tasks

    Writing contract logic, testing, and UI creation are separate tasks from deployment.
  3. Final Answer:

    To automate deploying smart contracts to the blockchain -> Option C
  4. 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

  1. Step 1: Recall ethers.js method

    The correct method to prepare a contract for deployment is ethers.getContractFactory with await.
  2. Step 2: Check syntax correctness

    Options B, C, and D use incorrect method names or miss await keyword.
  3. Final Answer:

    const Contract = await ethers.getContractFactory('MyContract'); -> Option A
  4. 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:
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
A. The deployed address of the Token contract
B. An error because token.address cannot be passed
C. Undefined, because sale.address is not set
D. The deployed address of the Sale contract

Solution

  1. Step 1: Understand deployment sequence

    The Token contract is deployed first, then its address is passed to Sale contract deployment.
  2. Step 2: Identify sale.address value

    After deployment, sale.address holds the Sale contract's blockchain address, which is logged.
  3. Final Answer:

    The deployed address of the Sale contract -> Option D
  4. Quick 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
A. Missing await before ethers.getContractFactory
B. Missing await before Token.deploy()
C. Missing await before token.deployed()
D. No error, the code is correct

Solution

  1. Step 1: Check getContractFactory usage

    ethers.getContractFactory returns a promise, so it needs await.
  2. Step 2: Verify other awaits

    Token.deploy() and token.deployed() correctly use await.
  3. Final Answer:

    Missing await before ethers.getContractFactory -> Option A
  4. 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

  1. Step 1: Understand constructor dependency

    Marketplace requires Token's address, so Token must be deployed first to get its address.
  2. Step 2: Deploy in correct order

    Deploy Token, then deploy Marketplace passing token.address to its constructor.
  3. Step 3: Reject incorrect options

    Deploying Marketplace first or simultaneously won't provide Token's address; omitting constructor args breaks dependency.
  4. Final Answer:

    Deploy Token first, then deploy Marketplace passing token.address -> Option B
  5. Quick 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