Consider the following Solidity code snippet that filters events by an indexed parameter:
pragma solidity ^0.8.0;
contract EventFilter {
event Transfer(address indexed from, address indexed to, uint amount);
function testFilter() public pure returns (string memory) {
// Imagine filtering Transfer events where from == 0x123...
// This is a conceptual example, not actual filtering code
return "Filtered 3 events";
}
}What would be the output of testFilter() assuming it returns the count of filtered events?
Look at the return statement inside testFilter().
The function testFilter() returns the string "Filtered 3 events" directly, so that is the output.
In Solidity events, which keyword allows a parameter to be used for filtering when listening to events?
It is a keyword placed before the parameter name in the event declaration.
The indexed keyword marks event parameters that can be filtered on when listening to events.
Given this JavaScript code using ethers.js to filter events:
const filter = contract.filters.Transfer(null, '0x0000000000000000000000000000000000000000'); const events = await contract.queryFilter(filter); console.log(events.length);
What error will this code produce?
Check if the address used is valid and the filter syntax is correct.
The address '0x000...000' is a valid zero address, and the filter syntax is correct, so no error occurs. The code prints the number of matching events.
Consider this Solidity event and filter example:
pragma solidity ^0.8.0;
contract Example {
event Data(uint indexed id, string value);
function emitEvents() public {
emit Data(1, "one");
emit Data(2, "two");
emit Data(3, "three");
}
// Conceptual filter: get events where id == 2
function filterEvents() public pure returns (string memory) {
return "Data with id 2: two";
}
}What does filterEvents() return?
Look at the return statement in filterEvents().
The function returns the string "Data with id 2: two" directly, representing the filtered event.
Which statement correctly describes how event filtering works in Ethereum logs?
Think about how Ethereum stores event data and how filters use topics.
Ethereum stores indexed event parameters in topics, which are used to filter logs efficiently. Non-indexed parameters are stored in data and cannot be filtered directly.