Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Event filtering in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Event Filtering Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Solidity event filter code?

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?

A"Filtered 3 events"
B"Filtered 0 events"
C"Filtered all events"
D"Filtered 1 event"
Attempts:
2 left
💡 Hint

Look at the return statement inside testFilter().

🧠 Conceptual
intermediate
1:30remaining
Which indexed event parameter allows filtering by that parameter?

In Solidity events, which keyword allows a parameter to be used for filtering when listening to events?

Apublic
Bindexed
Cconstant
Dpayable
Attempts:
2 left
💡 Hint

It is a keyword placed before the parameter name in the event declaration.

❓ Predict Output
advanced
2:30remaining
What error does this event filter code produce?

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?

ANo error, prints number of events
BSyntaxError: unexpected token
CReferenceError: contract is not defined
DTypeError: invalid address
Attempts:
2 left
💡 Hint

Check if the address used is valid and the filter syntax is correct.

❓ Predict Output
advanced
2:00remaining
What is the output of this Solidity event filtering example?

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?

A"Data with id 3: three"
B"Data with id 1: one"
C"Data with id 2: two"
D"No matching data"
Attempts:
2 left
💡 Hint

Look at the return statement in filterEvents().

🧠 Conceptual
expert
3:00remaining
Which option correctly describes event filtering in Ethereum logs?

Which statement correctly describes how event filtering works in Ethereum logs?

AFiltering requires decoding all contract storage to find events.
BFiltering scans all transaction input data to find matching events.
CFiltering is done by scanning block timestamps for event matches.
DFiltering uses indexed event parameters stored in topics to quickly find matching logs.
Attempts:
2 left
💡 Hint

Think about how Ethereum stores event data and how filters use topics.

Practice

(1/5)
1.

What is the main purpose of event filtering in blockchain applications?

easy
A. To encrypt blockchain data
B. To create new blockchain events
C. To mine new blocks faster
D. To listen only to specific events that match certain criteria

Solution

  1. Step 1: Understand event filtering concept

    Event filtering allows you to listen only to events that match specific parameters or conditions.
  2. Step 2: Identify the main purpose

    This helps reduce unnecessary data processing and improves app performance by focusing on relevant events.
  3. Final Answer:

    To listen only to specific events that match certain criteria -> Option D
  4. Quick Check:

    Event filtering = selective listening [OK]
Hint: Event filtering means listening only to events you want [OK]
Common Mistakes:
  • Confusing event filtering with event creation
  • Thinking event filtering speeds up mining
  • Believing event filtering encrypts data
2.

Which of the following is the correct syntax to filter events by a parameter named user in a blockchain event listener?

contract.events.Transfer({ filter: { ??? } })
easy
A. { user: '0x123...' }
B. { 'user' }
C. [ user ]
D. user = '0x123...'

Solution

  1. Step 1: Understand filter syntax

    Filters use an object with key-value pairs where keys are parameter names and values are filter values.
  2. Step 2: Match correct syntax

    The correct syntax is an object with the parameter name as key and the desired value, like { user: '0x123...' }.
  3. Final Answer:

    { user: '0x123...' } -> Option A
  4. Quick Check:

    Filter = object with key-value pairs [OK]
Hint: Filters use {paramName: value} object syntax [OK]
Common Mistakes:
  • Using array or string instead of object for filters
  • Writing filter as assignment instead of object
  • Omitting quotes around string values
3.

Given the following code snippet, what events will be received?

contract.events.Transfer({ filter: { from: '0xabc...' }, fromBlock: 100, toBlock: 200 })

Assuming events with from address '0xabc...' occurred at blocks 90, 150, and 210.

medium
A. Only event at block 150
B. Events at blocks 90, 150, and 210
C. Events at blocks 90 and 150
D. No events

Solution

  1. Step 1: Analyze filter parameters

    The filter listens for events where 'from' equals '0xabc...'. The block range is from 100 to 200.
  2. Step 2: Check event blocks against filter

    Events at blocks 90 and 210 are outside the block range, so only the event at block 150 matches both filter and block range.
  3. Final Answer:

    Only event at block 150 -> Option A
  4. Quick Check:

    Filter + block range = event at block 150 only [OK]
Hint: Events must match filter and block range [OK]
Common Mistakes:
  • Ignoring block range and including all matching events
  • Including events outside the block range
  • Confusing filter parameter with block number
4.

Identify the error in this event filter code snippet:

contract.events.Approval({ filter: { owner: 0xabc123 }, fromBlock: 'latest' })
medium
A. Filter object should be an array
B. The filter value 0xabc123 should be a string
C. fromBlock cannot be 'latest'
D. The event name 'Approval' is incorrect

Solution

  1. Step 1: Check filter parameter types

    Filter values must be strings for addresses, but 0xabc123 is missing quotes, so it's treated as a variable or number.
  2. Step 2: Validate other parts

    The event name and fromBlock usage are valid; fromBlock can be 'latest'. Filter should be an object, not an array.
  3. Final Answer:

    The filter value 0xabc123 should be a string -> Option B
  4. Quick Check:

    Address filter values must be strings [OK]
Hint: Address filters need quotes around them [OK]
Common Mistakes:
  • Omitting quotes around address strings
  • Thinking fromBlock 'latest' is invalid
  • Using array instead of object for filter
5.

You want to listen to Transfer events where the value is greater than 1000 tokens. Which approach correctly filters these events?

// Assume event signature: Transfer(address from, address to, uint256 value)
hard
A. Use filter: { value: '>1000' }
B. Use filter: { value: val => val > 1000 }
C. Filter events after receiving all by checking value > 1000 in code
D. Set fromBlock and toBlock to limit event range

Solution

  1. Step 1: Understand filter limitations

    Event filters only support exact matches or arrays of values, not comparison operators like greater than.
  2. Step 2: Choose correct approach

    To filter by value > 1000, you must receive events first and then check the value in your code logic.
  3. Final Answer:

    Filter events after receiving all by checking value > 1000 in code -> Option C
  4. Quick Check:

    Filters do exact matches; use code for range checks [OK]
Hint: Filters match exact values; use code for greater than checks [OK]
Common Mistakes:
  • Trying to use comparison operators in filter
  • Assuming string '>1000' works as filter
  • Confusing block range with value filtering
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