0
0
Blockchain / Solidityprogramming~20 mins

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

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