Indexed parameters help you find specific events quickly in blockchain logs. They make searching easier and faster.
Indexed parameters in Blockchain / Solidity
event EventName(type1 indexed param1, type2 param2, type3 indexed param3);
Only up to 3 parameters can be indexed in an event.
Indexed parameters are stored as topics in the blockchain logs.
event Transfer(address indexed from, address indexed to, uint256 value);event Approval(address indexed owner, address indexed spender, uint256 value);
event Deposit(address indexed account, uint256 amount);
This smart contract defines a Transfer event with two indexed parameters: 'from' and 'to'. When the transfer function is called, it emits the event so external apps can track token movements efficiently.
pragma solidity ^0.8.0; contract Token { event Transfer(address indexed from, address indexed to, uint256 value); function transfer(address to, uint256 value) public { // In a real contract, you'd update balances here emit Transfer(msg.sender, to, value); } }
Indexed parameters help external apps filter events without reading all data.
Non-indexed parameters are stored in the event data but can't be searched by topic.
Use indexed parameters wisely to keep event logs efficient and searchable.
Indexed parameters make event searching faster on the blockchain.
You can index up to 3 parameters per event.
Use indexed parameters for important values like addresses or IDs.
Practice
What is the main purpose of indexed parameters in blockchain events?
Solution
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.Step 2: Compare with other options
Encryption, size limits, or automatic execution are not related to indexed parameters.Final Answer:
To tag event data for fast searching on the blockchain -> Option CQuick Check:
Indexed parameters = fast search tags [OK]
- Thinking indexed encrypts data
- Confusing indexing with size limits
- Assuming indexed triggers functions
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);Solution
Step 1: Recall Solidity event syntax
The keyword to mark a parameter as indexed is exactlyindexed.Step 2: Check other options
Other words likeindex,indexing, orindexedParamare invalid in Solidity event declarations.Final Answer:
indexed -> Option AQuick Check:
Use 'indexed' keyword for parameters [OK]
- Using 'index' instead of 'indexed'
- Adding extra words like 'indexedParam'
- Omitting the keyword entirely
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?
Solution
Step 1: Identify indexed parameters in the event
The event declaresownerandspenderas indexed parameters.Step 2: Understand filtering capability
Only indexed parameters can be filtered in event logs, soownerandspenderare filterable.Final Answer:
owner and spender -> Option AQuick Check:
Indexed parameters = owner, spender [OK]
- Assuming all parameters are indexed
- Including non-indexed 'value' as filterable
- Confusing parameter order with indexing
What is wrong with this Solidity event declaration?
event Deposit(address indexed from, uint indexed amount, uint indexed message, uint indexed timestamp);
Solution
Step 1: Count indexed parameters
The event has four indexed parameters:from,amount,message, andtimestamp.Step 2: Recall indexing limit
Solidity allows a maximum of 3 indexed parameters per event. This declaration exceeds that limit.Final Answer:
More than 3 parameters are indexed -> Option DQuick Check:
4 indexed > max 3 [OK]
- Thinking 4 indexed parameters are allowed
- Ignoring the 3 indexed parameter limit
- Confusing event name rules
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?
Solution
Step 1: Identify which parameters need indexing
Sender and receiver addresses must be indexed for filtering.Step 2: Check indexing limits and types
Only 3 parameters can be indexed. Amount can be indexed but note (string) cannot be indexed.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.Final Answer:
event Transfer(address indexed sender, address indexed receiver, uint amount, string note); -> Option BQuick Check:
Index sender and receiver only; string can't be indexed [OK]
- Indexing string parameters
- Indexing more than 3 parameters
- Not indexing all filterable parameters
Ready to prove it?
Pass the test and this pattern moves unknown
