Consider the following code snippet that listens to a blockchain event using ethers.js. What will be logged to the console when the event is emitted with value 42?
const { ethers } = require('ethers');
const provider = new ethers.providers.JsonRpcProvider();
const contractAddress = '0x1234567890abcdef1234567890abcdef12345678';
const abi = [
'event ValueChanged(uint256 newValue)'
];
const contract = new ethers.Contract(contractAddress, abi, provider);
contract.on('ValueChanged', (newValue) => {
console.log(`New value is: ${newValue.toString()}`);
});
// Assume event ValueChanged(42) is emitted by the contractRemember that ethers.js BigNumber objects have a toString() method to convert to readable strings.
The event listener receives a BigNumber object representing the value 42. Calling toString() converts it to the string '42'.
Given the following code, what will be the output after emitting the event twice?
const { ethers } = require('ethers');
const provider = new ethers.providers.JsonRpcProvider();
const contractAddress = '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd';
const abi = [
'event DataUpdated(string data)'
];
const contract = new ethers.Contract(contractAddress, abi, provider);
function onDataUpdated(data) {
console.log(`Data updated: ${data}`);
}
contract.on('DataUpdated', onDataUpdated);
// Event DataUpdated('First') emitted
// Event DataUpdated('Second') emittedThink about how many times the listener is attached and if it is removed.
The listener is attached once and listens to both events, so it logs both updates once each.
Look at the code below. The event Transfer is emitted on the contract, but the console never logs anything. What is the reason?
const { ethers } = require('ethers');
const provider = new ethers.providers.JsonRpcProvider();
const contractAddress = '0xfeedfeedfeedfeedfeedfeedfeedfeedfeedfeed';
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) => {
console.log(`Transfer from ${from} to ${to} of ${value.toString()}`);
});Check the event name spelling and capitalization.
Event names in ethers.js are case-sensitive. Using 'transfer' instead of 'Transfer' means the listener never matches the event.
You want to listen to both Deposit and Withdrawal events from a contract and log their details with one function. Which code snippet is correct?
Think about how to attach multiple listeners properly.
Option D attaches separate listeners for each event. Option D is invalid syntax. Option D uses logical OR incorrectly. Option D has invalid syntax.
You have a React app that listens to a blockchain event BalanceChanged. You want to update the displayed balance whenever the event fires. Which approach correctly updates the React state?
import React, { useEffect, useState } from 'react'; import { ethers } from 'ethers'; const contractAddress = '0xabcabcabcabcabcabcabcabcabcabcabcabcabca'; const abi = [ 'event BalanceChanged(address indexed user, uint256 newBalance)' ]; export default function Balance() { const [balance, setBalance] = useState('0'); useEffect(() => { const provider = new ethers.providers.Web3Provider(window.ethereum); const contract = new ethers.Contract(contractAddress, abi, provider); // Fill in event listener here return () => { // Clean up listener }; }, []); return <div>Balance: {balance}</div>; }
Remember to use the React state setter and convert BigNumber to string.
Option A correctly uses setBalance with newBalance.toString() and cleans up the listener with contract.off. Option A tries to assign state variable directly which won't update UI. Option A passes BigNumber directly without conversion. Option A uses parseInt which may lose precision.