Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

Indexed parameters in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Indexed parameters
Define event with indexed params
↓
Emit event with values
↓
Blockchain stores event
↓
Indexed params stored as topics
↓
Listeners filter events by topics
↓
Retrieve matching events efficiently
Indexed parameters in blockchain events allow quick filtering by storing key values as topics, making event search efficient.
Execution Sample
Blockchain / Solidity
event Transfer(address indexed from, address indexed to, uint256 value);

function send(address to, uint256 amount) public {
    emit Transfer(msg.sender, to, amount);
}
This code defines a Transfer event with two indexed parameters and emits it when sending tokens.
Execution Table
StepActionIndexed Params (topics)Non-indexed Params (data)Result
1Define event Transfer with indexed 'from' and 'to'from, tovalueEvent signature and topics set
2Call send() with to=0xABC, amount=100Function executes
3Emit Transfer eventmsg.sender, toamountEvent logged with topics and data
4Blockchain stores eventTopics: from, to addressesData: value=100Event stored in logs
5Listener filters by 'from' addressMatches topic 'from'Event retrieved efficiently
6Listener filters by 'value'Cannot filter efficientlyFull scan needed
7EndEvent emission and indexing complete
💡 Event emitted and indexed parameters stored as topics for efficient filtering
Variable Tracker
VariableStartAfter emitFinal
from (indexed)undefinedmsg.sendermsg.sender
to (indexed)undefinedtoto
value (non-indexed)undefinedamountamount
Key Moments - 3 Insights
Why are some parameters marked as indexed in events?
Indexed parameters are stored as topics, allowing fast filtering by listeners, as shown in execution_table step 5.
Can non-indexed parameters be used for efficient event filtering?
No, non-indexed parameters are stored in data and require scanning all events, as shown in execution_table step 6.
How many parameters can be indexed in an event?
Up to three parameters can be indexed per event, enabling multiple filters as in the example with two indexed parameters.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what are the indexed parameters stored as?
ATopics
BData
CFunction arguments
DTransaction inputs
💡 Hint
Check the 'Indexed Params (topics)' column at step 3 in execution_table
At which step does the listener efficiently filter events by an indexed parameter?
AStep 2
BStep 6
CStep 5
DStep 7
💡 Hint
Look at the 'Result' column describing filtering in execution_table
If the 'value' parameter was indexed, how would filtering change at step 6?
AFiltering would still require full scan
BFiltering would be efficient using topics
CFiltering would be impossible
DFiltering would be slower
💡 Hint
Compare step 6 filtering with indexed vs non-indexed parameters in execution_table
Concept Snapshot
Indexed parameters in blockchain events:
- Marked with 'indexed' keyword
- Stored as topics (up to 3)
- Enable fast event filtering
- Non-indexed stored in data, slower to filter
- Used in event definitions and emissions
Full Transcript
Indexed parameters in blockchain events are special parameters marked with the 'indexed' keyword. When an event is emitted, these indexed parameters are stored as topics, which are like tags that help listeners quickly find events matching certain criteria. For example, in a Transfer event with 'from' and 'to' addresses indexed, listeners can filter events by these addresses efficiently. Non-indexed parameters like 'value' are stored in the event data and require scanning all events to filter. Up to three parameters can be indexed per event. This makes event searching faster and more efficient on the blockchain.

Practice

(1/5)
1.

What is the main purpose of indexed parameters in blockchain events?

easy
A. To limit the size of event data
B. To encrypt event data for security
C. To tag event data for fast searching on the blockchain
D. To automatically execute functions on event trigger

Solution

  1. Step 1: Understand the role of indexed parameters

    Indexed parameters are used to tag event data so it can be searched quickly on the blockchain.
  2. Step 2: Compare with other options

    Encryption, size limits, or automatic execution are not related to indexed parameters.
  3. Final Answer:

    To tag event data for fast searching on the blockchain -> Option C
  4. Quick Check:

    Indexed parameters = fast search tags [OK]
Hint: Indexed means fast searchable tags in events [OK]
Common Mistakes:
  • Thinking indexed encrypts data
  • Confusing indexing with size limits
  • Assuming indexed triggers functions
2.

Which of the following is the correct syntax to declare an indexed parameter in a Solidity event?

event Transfer(address ? from, address to, uint amount);
easy
A. indexed
B. index
C. indexing
D. indexedParam

Solution

  1. Step 1: Recall Solidity event syntax

    The keyword to mark a parameter as indexed is exactly indexed.
  2. Step 2: Check other options

    Other words like index, indexing, or indexedParam are invalid in Solidity event declarations.
  3. Final Answer:

    indexed -> Option A
  4. Quick Check:

    Use 'indexed' keyword for parameters [OK]
Hint: Use exact keyword 'indexed' for event parameters [OK]
Common Mistakes:
  • Using 'index' instead of 'indexed'
  • Adding extra words like 'indexedParam'
  • Omitting the keyword entirely
3.

Given the Solidity event declaration:

event Approval(address indexed owner, address indexed spender, uint256 value);

Which parameters are indexed and can be filtered in event logs?

medium
A. owner and spender
B. owner and value
C. spender and value
D. owner, spender, and value

Solution

  1. Step 1: Identify indexed parameters in the event

    The event declares owner and spender as indexed parameters.
  2. Step 2: Understand filtering capability

    Only indexed parameters can be filtered in event logs, so owner and spender are filterable.
  3. Final Answer:

    owner and spender -> Option A
  4. Quick Check:

    Indexed parameters = owner, spender [OK]
Hint: Only parameters with 'indexed' keyword are filterable [OK]
Common Mistakes:
  • Assuming all parameters are indexed
  • Including non-indexed 'value' as filterable
  • Confusing parameter order with indexing
4.

What is wrong with this Solidity event declaration?

event Deposit(address indexed from, uint indexed amount, uint indexed message, uint indexed timestamp);
medium
A. Indexed keyword is missing
B. Parameters must be public
C. Event name is invalid
D. More than 3 parameters are indexed

Solution

  1. Step 1: Count indexed parameters

    The event has four indexed parameters: from, amount, message, and timestamp.
  2. Step 2: Recall indexing limit

    Solidity allows a maximum of 3 indexed parameters per event. This declaration exceeds that limit.
  3. Final Answer:

    More than 3 parameters are indexed -> Option D
  4. Quick Check:

    4 indexed > max 3 [OK]
Hint: Max 3 indexed params per event [OK]
Common Mistakes:
  • Thinking 4 indexed parameters are allowed
  • Ignoring the 3 indexed parameter limit
  • Confusing event name rules
5.

You want to create an event to track token transfers with these details: sender address, receiver address, amount, and a note message. You want to filter events by sender and receiver. Which event declaration is correct?

hard
A. event Transfer(address indexed sender, address indexed receiver, uint indexed amount, string indexed note);
B. event Transfer(address indexed sender, address indexed receiver, uint amount, string note);
C. event Transfer(address indexed sender, address receiver, uint indexed amount, string note);
D. event Transfer(address sender, address receiver, uint indexed amount, string indexed note);

Solution

  1. Step 1: Identify which parameters need indexing

    Sender and receiver addresses must be indexed for filtering.
  2. Step 2: Check indexing limits and types

    Only 3 parameters can be indexed. Amount can be indexed but note (string) cannot be indexed.
  3. Step 3: Evaluate each option

    event Transfer(address indexed sender, address indexed receiver, uint amount, string note); indexes sender and receiver only, which is correct. event Transfer(address sender, address receiver, uint indexed amount, string indexed note); indexes amount and note incorrectly. event Transfer(address indexed sender, address receiver, uint indexed amount, string note); indexes amount but not receiver. event Transfer(address indexed sender, address indexed receiver, uint indexed amount, string indexed note); indexes 4 parameters which is invalid and string cannot be indexed.
  4. Final Answer:

    event Transfer(address indexed sender, address indexed receiver, uint amount, string note); -> Option B
  5. Quick Check:

    Index sender and receiver only; string can't be indexed [OK]
Hint: Index only filterable params; strings cannot be indexed [OK]
Common Mistakes:
  • Indexing string parameters
  • Indexing more than 3 parameters
  • Not indexing all filterable parameters
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