Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Hardhat deployment scripts in Blockchain / Solidity - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Hardhat Deployment Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a basic Hardhat deployment script
What will be the output printed to the console when running this Hardhat deployment script?
Blockchain / Solidity
async function main() {
  const [deployer] = await ethers.getSigners();
  console.log('Deploying contracts with the account:', deployer.address);
  const Token = await ethers.getContractFactory('Token');
  const token = await Token.deploy('MyToken', 'MTK', 18, 1000);
  await token.deployed();
  console.log('Token deployed to:', token.address);
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});
ASyntaxError: Unexpected token 'await'
BDeploying contracts with the account: 0x... (deployer address)\nToken deployed to: 0x... (contract address)
CDeploying contracts with the account: undefined\nToken deployed to: undefined
DError: Token contract not found
Attempts:
2 left
💡 Hint
Remember that ethers.getSigners() returns an array of accounts and deployer.address is a valid address string.
🧠 Conceptual
intermediate
1:30remaining
Understanding deployment script parameters
In a Hardhat deployment script, what is the purpose of the line `const Token = await ethers.getContractFactory('Token');`?
AIt compiles the Token contract and prepares it for deployment.
BIt fetches the deployed Token contract address from the network.
CIt deploys the Token contract immediately to the blockchain.
DIt imports the Token contract source code as a string.
Attempts:
2 left
💡 Hint
Think about what a contract factory does in ethers.js.
🔧 Debug
advanced
2:00remaining
Identify the error in this deployment script
What error will this Hardhat deployment script produce when run?
Blockchain / Solidity
async function main() {
  const Token = await ethers.getContractFactory('Token')
  const token = Token.deploy('MyToken', 'MTK', 18, 1000)
  await token.deployed()
  console.log('Token deployed to:', token.address)
}

main().catch(console.error);
AReferenceError: ethers is not defined
BSyntaxError: missing semicolon
CTypeError: token.deployed is not a function
DNo error, script runs successfully
Attempts:
2 left
💡 Hint
Check if the deploy function is awaited properly.
📝 Syntax
advanced
1:30remaining
Correct syntax for deploying with constructor arguments
Which option correctly deploys a contract named 'Crowdsale' with constructor arguments `rate` and `wallet`?
A
const Crowdsale = await ethers.getContractFactory('Crowdsale');
const crowdsale = await Crowdsale.deploy([rate, wallet]);
B
const Crowdsale = ethers.getContractFactory('Crowdsale');
const crowdsale = Crowdsale.deploy(rate, wallet);
C
const Crowdsale = await ethers.getContractFactory('Crowdsale');
const crowdsale = Crowdsale.deploy([rate, wallet]);
D
const Crowdsale = await ethers.getContractFactory('Crowdsale');
const crowdsale = await Crowdsale.deploy(rate, wallet);
Attempts:
2 left
💡 Hint
Remember how to pass multiple arguments to a contract constructor in deploy().
🚀 Application
expert
2:30remaining
Determine the number of deployed contracts
Given this Hardhat deployment script, how many contracts will be deployed to the blockchain after running it?
Blockchain / Solidity
async function main() {
  const [deployer] = await ethers.getSigners();

  const Token = await ethers.getContractFactory('Token');
  const token = await Token.deploy('MyToken', 'MTK', 18, 1000);
  await token.deployed();

  const Crowdsale = await ethers.getContractFactory('Crowdsale');
  const crowdsale = await Crowdsale.deploy(1, deployer.address, token.address);
  await crowdsale.deployed();

  console.log('Deployment complete');
}

main().catch(console.error);
A2
B1
C3
D0
Attempts:
2 left
💡 Hint
Count each deploy() call that is awaited and completed.

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