0
0
Blockchain / Solidityprogramming~10 mins

Hardhat testing setup in Blockchain / Solidity - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Hardhat testing helpers.

Blockchain / Solidity
const { [1] } = require("hardhat");
Drag options to blanks, or click blank then click option'
Atruffle
Bweb3
Cganache
Dethers
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'web3' instead of 'ethers' causes errors because Hardhat uses ethers by default.
Trying to import 'truffle' or 'ganache' which are separate tools.
2fill in blank
medium

Complete the code to get the list of signers for testing.

Blockchain / Solidity
const [owner, addr1] = await [1].getSigners();
Drag options to blanks, or click blank then click option'
Aethers
Bweb3
Chardhat
Dnetwork
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'web3.getSigners()' which does not exist.
Trying to get signers from 'network' which is unrelated.
3fill in blank
hard

Fix the error in deploying the contract instance.

Blockchain / Solidity
const Contract = await ethers.getContractFactory("MyContract");
const contract = await Contract.[1]();
Drag options to blanks, or click blank then click option'
Adeploy
Battach
Cconnect
Dcall
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'attach()' tries to connect to an existing contract, not deploy a new one.
Using 'connect()' or 'call()' which are not deployment methods.
4fill in blank
hard

Fill both blanks to write a test that checks the contract owner.

Blockchain / Solidity
describe("MyContract", function () {
  it("should set the right owner", async function () {
    const [[1]] = await ethers.getSigners();
    const Contract = await ethers.getContractFactory("MyContract");
    const contract = await Contract.deploy();
    await contract.deployed();
    expect(await contract.owner()).to.equal([2].address);
  });
});
Drag options to blanks, or click blank then click option'
Aowner
Baddr1
Daddr2
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for the signer and the expected address causes test failures.
Using addresses of other signers like 'addr1' or 'addr2' incorrectly.
5fill in blank
hard

Fill all three blanks to write a test that checks a function revert with a custom error message.

Blockchain / Solidity
it("should revert if not owner", async function () {
  const [owner, [1]] = await ethers.getSigners();
  const Contract = await ethers.getContractFactory("MyContract");
  const contract = await Contract.deploy();
  await contract.deployed();
  await expect(contract.connect([2]).restrictedFunction()).to.be.revertedWith([3]);
});
Drag options to blanks, or click blank then click option'
Aaddr1
C"Ownable: caller is not the owner"
D"NotOwnerError"
Attempts:
3 left
💡 Hint
Common Mistakes
Using different signer variables for connecting and assigning.
Using incorrect or incomplete revert messages causing test failures.