Challenge - 5 Problems
Event Listener Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output when listening to a smart contract event?
Consider the following code snippet that listens to a smart contract event using ethers.js. What will be logged to the console when the event is emitted?
Blockchain / Solidity
const { ethers } = require('ethers');
const provider = new ethers.providers.JsonRpcProvider('https://mainnet.infura.io/v3/YOUR_PROJECT_ID');
const contractAddress = '0x1234567890abcdef1234567890abcdef12345678';
const abi = [
'event Transfer(address indexed from, address indexed to, uint256 value)'
];
const contract = new ethers.Contract(contractAddress, abi, provider);
contract.on('Transfer', (from, to, value, event) => {
console.log(`Transfer from ${from} to ${to} of ${value.toString()} tokens`);
});
// Assume the Transfer event is emitted with from='0xabc...', to='0xdef...', value=1000Attempts:
2 left
💡 Hint
Remember that ethers.js contract objects have an 'on' method to listen to events.
✗ Incorrect
The 'on' method listens to the 'Transfer' event and receives the event parameters. The console.log prints the event details as expected.
🧠 Conceptual
intermediate1:30remaining
Which statement about event listeners in blockchain frontend is true?
Select the correct statement about listening to blockchain events in a frontend application.
Attempts:
2 left
💡 Hint
Think about what information is needed to decode and listen to events.
✗ Incorrect
To listen to events, the frontend needs the contract ABI to decode event data and the provider to connect to the blockchain. Past events require special queries, not just listeners.
🔧 Debug
advanced2:00remaining
Why does this event listener code fail to log events?
The following code is intended to listen to a smart contract event but does not log anything when the event is emitted. What is the most likely cause?
Blockchain / Solidity
const contract = new ethers.Contract(contractAddress, abi, provider); contract.once('MyEvent', (data) => { console.log('Event data:', data); }); // Event 'MyEvent' is emitted multiple times after this code runs
Attempts:
2 left
💡 Hint
Check the difference between 'on' and 'once' methods.
✗ Incorrect
'once' listens only for the first occurrence of the event and then removes the listener. If multiple events are emitted, only the first triggers the callback.
📝 Syntax
advanced1:30remaining
Identify the syntax error in this event listener code
Which option contains the correct syntax to listen to a smart contract event named 'Approval' using ethers.js?
Attempts:
2 left
💡 Hint
Check the parentheses and brackets usage in function calls.
✗ Incorrect
Option A uses correct syntax: method call with event name string and arrow function callback. Other options have missing commas, wrong brackets, or incorrect function syntax.
🚀 Application
expert2:30remaining
How to efficiently update UI on multiple event emissions?
You have a frontend app listening to a 'NewBid' event emitted frequently by a smart contract. To avoid UI lag, which approach is best to update the UI efficiently?
Attempts:
2 left
💡 Hint
Think about how to reduce frequent UI re-renders while keeping data fresh.
✗ Incorrect
Batching events and updating the UI periodically reduces rendering overhead and improves performance when events are frequent.