0
0
Blockchain / Solidityprogramming~10 mins

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

Choose your learning style9 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.