0
0
Blockchain / Solidityprogramming~20 mins

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

Choose your learning style9 modes available
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.