Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Testing with ethers.js in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ethers.js Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple ethers.js contract call
What is the output of this ethers.js code snippet when calling a contract's getValue function that returns 42?
Blockchain / Solidity
const { ethers } = require('ethers');

async function test() {
  const provider = new ethers.providers.JsonRpcProvider();
  const contractAddress = '0x1234567890123456789012345678901234567890';
  const abi = [
    'function getValue() view returns (uint256)'
  ];
  const contract = new ethers.Contract(contractAddress, abi, provider);
  const value = await contract.getValue();
  console.log(value.toString());
}
test();
A42
Bundefined
CError: contract method not found
D0
Attempts:
2 left
💡 Hint
Remember that ethers.js returns BigNumber objects for uint256 values, so you need to convert them to string or number.
❓ Predict Output
intermediate
2:00remaining
Result of sending a transaction with ethers.js signer
What will be the value of txResponse.hash after sending this transaction?
Blockchain / Solidity
const { ethers } = require('ethers');

async function sendTx() {
  const provider = new ethers.providers.JsonRpcProvider();
  const signer = provider.getSigner();
  const txResponse = await signer.sendTransaction({
    to: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd',
    value: ethers.utils.parseEther('0.01')
  });
  console.log(txResponse.hash);
}
sendTx();
AA 66-character hexadecimal string starting with '0x'
BThe number 0.01
Cundefined
DError: signer not connected
Attempts:
2 left
💡 Hint
Transaction hashes are always 32 bytes represented as 66 hex characters including '0x'.
🔧 Debug
advanced
2:00remaining
Identify the error in this ethers.js test setup
What error will this ethers.js test code produce when run?
Blockchain / Solidity
const { ethers } = require('ethers');

describe('MyContract', () => {
  let contract;
  beforeEach(async () => {
    const factory = await ethers.getContractFactory('MyContract');
    contract = await factory.deploy();
    await contract.deployed();
  });

  it('should return correct value', async () => {
    const value = await contract.getValue();
    expect(value).to.equal(42);
  });
});
ANo error, test passes
BTypeError: ethers.getContractFactory is not a function
CSyntaxError: Unexpected token 'await'
DReferenceError: expect is not defined
Attempts:
2 left
💡 Hint
Check if the testing assertion library is imported or available.
📝 Syntax
advanced
2:00remaining
Find the syntax error in this ethers.js async function
Which option correctly fixes the syntax error in this code?
Blockchain / Solidity
async function test() {
  const provider = new ethers.providers.JsonRpcProvider();
  const balance = provider.getBalance('0xabc123...');
  console.log(balance.toString());
}
test();
AChange function to non-async: function test() { ... }
BAdd await before provider.getBalance: const balance = await provider.getBalance('0xabc123...');
CAdd .then() to getBalance: provider.getBalance('0xabc123...').then(balance => console.log(balance.toString()));
DNo fix needed, code runs fine
Attempts:
2 left
💡 Hint
Remember that getBalance returns a Promise and must be awaited inside async functions.
🚀 Application
expert
3:00remaining
How to test event emission with ethers.js and Mocha
You want to test that your contract emits an event ValueChanged with the new value 100 after calling setValue(100). Which code snippet correctly tests this using ethers.js and Mocha?
A
contract.setValue(100).then(() => {
  console.log('Event emitted');
});
B
const tx = await contract.setValue(100);
await tx.wait();
const events = await contract.queryFilter('ValueChanged');
assert(events[0].args.value === 100);
Cawait expect(contract.setValue(100)).to.emit(contract, 'ValueChanged').withArgs(100);
D
const event = contract.filters.ValueChanged(100);
expect(event).to.exist;
Attempts:
2 left
💡 Hint
Use the Chai matchers from ethereum-waffle or hardhat for event testing.

Practice

(1/5)
1. What is the main purpose of using ethers.js in testing smart contracts?
easy
A. To create new blockchain networks
B. To design the user interface of a blockchain app
C. To mine new blocks on the blockchain
D. To interact with smart contracts and verify their behavior programmatically

Solution

  1. Step 1: Understand ethers.js role

    ethers.js is a library that helps developers interact with Ethereum smart contracts in code.
  2. Step 2: Identify testing purpose

    In testing, ethers.js lets you call contract functions and check if they behave as expected.
  3. Final Answer:

    To interact with smart contracts and verify their behavior programmatically -> Option D
  4. Quick Check:

    ethers.js tests contract behavior = A [OK]
Hint: Remember: ethers.js tests contracts by calling their functions [OK]
Common Mistakes:
  • Confusing ethers.js with UI design tools
  • Thinking ethers.js mines blocks
  • Assuming ethers.js creates blockchains
2. Which of the following is the correct way to get a signer from a provider in ethers.js?
easy
A. const signer = provider.signer();
B. const signer = provider.getSigner();
C. const signer = provider.getsigner();
D. const signer = provider.sign();

Solution

  1. Step 1: Recall ethers.js signer method

    The correct method to get a signer from a provider is getSigner() with capital S.
  2. Step 2: Check method spelling and casing

    Options B, C, and D have incorrect method names or casing.
  3. Final Answer:

    const signer = provider.getSigner(); -> Option B
  4. Quick Check:

    Correct method is getSigner() = A [OK]
Hint: Method names are case-sensitive; use getSigner() exactly [OK]
Common Mistakes:
  • Using wrong casing like signer() instead of getSigner()
  • Misspelling method names
  • Confusing signer with sign
3. Consider this ethers.js test code snippet:
const tx = await contract.setValue(42);
await tx.wait();
const value = await contract.getValue();
console.log(value);
What will be printed to the console?
medium
A. 42
B. undefined
C. Transaction hash
D. Error: contract.getValue is not a function

Solution

  1. Step 1: Understand transaction and wait

    The code calls setValue(42) on the contract, then waits for the transaction to confirm with tx.wait().
  2. Step 2: Retrieve stored value

    After confirmation, it calls getValue() which returns the stored value 42.
  3. Final Answer:

    42 -> Option A
  4. Quick Check:

    Confirmed setValue(42) then getValue() = 42 [OK]
Hint: Always wait for tx confirmation before reading updated state [OK]
Common Mistakes:
  • Not awaiting tx.wait() before reading value
  • Expecting transaction hash instead of value
  • Assuming getValue() is undefined
4. You wrote this test code but it throws an error:
const tx = contract.setValue(10);
await tx.wait();
What is the likely cause of the error?
medium
A. Missing await before contract.setValue(10)
B. tx.wait() is not a function
C. contract.setValue does not exist
D. Provider is not connected

Solution

  1. Step 1: Check async call to contract method

    contract.setValue(10) returns a Promise, so it must be awaited to get the transaction object.
  2. Step 2: Identify missing await issue

    Without await, tx is a Promise, which does not have a wait() method, causing the error.
  3. Final Answer:

    Missing await before contract.setValue(10) -> Option A
  4. Quick Check:

    Await async call before tx.wait() = C [OK]
Hint: Always await contract calls returning transactions before wait() [OK]
Common Mistakes:
  • Forgetting await before async contract calls
  • Assuming tx is transaction without awaiting
  • Not checking provider connection first
5. You want to test a smart contract function transfer(address to, uint amount) that emits an event Transfer. Which ethers.js testing approach correctly waits for the transaction and verifies the event was emitted?
hard
A. await contract.transfer(addr, 100); expect(tx.events[0].event).to.equal('Transfer');
B. const receipt = await contract.transfer(addr, 100); expect(receipt.events[0].event).to.equal('Transfer');
C. const tx = await contract.transfer(addr, 100); const receipt = await tx.wait(); expect(receipt.events[0].event).to.equal('Transfer');
D. const tx = contract.transfer(addr, 100); expect(tx.events[0].event).to.equal('Transfer');

Solution

  1. Step 1: Await the transaction call

    Use await to get the transaction object from contract.transfer.
  2. Step 2: Wait for transaction confirmation and get receipt

    Call tx.wait() to get the receipt which contains event logs.
  3. Step 3: Verify event in receipt

    Check receipt.events[0].event equals 'Transfer' to confirm event emitted.
  4. Final Answer:

    const tx = await contract.transfer(addr, 100); const receipt = await tx.wait(); expect(receipt.events[0].event).to.equal('Transfer'); -> Option C
  5. Quick Check:

    Await tx, wait receipt, check event = D [OK]
Hint: Always await tx, then wait for receipt to check events [OK]
Common Mistakes:
  • Not awaiting transaction before wait()
  • Trying to access events on tx instead of receipt
  • Ignoring event verification after transaction
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard