0
0
Blockchain / Solidityprogramming~5 mins

Indexed parameters in Blockchain / Solidity

Choose your learning style9 modes available
Introduction

Indexed parameters help you find specific events quickly in blockchain logs. They make searching easier and faster.

When you want to track transfers of tokens between addresses.
When you need to find all events related to a specific user.
When you want to filter events by a certain value, like an ID or address.
When building apps that show transaction history for a user.
When debugging smart contracts and you want to find specific event data.
Syntax
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.

Examples
This event tracks token transfers. The 'from' and 'to' addresses are indexed for easy searching.
Blockchain / Solidity
event Transfer(address indexed from, address indexed to, uint256 value);
This event logs when an owner approves a spender to use tokens. Both addresses are indexed.
Blockchain / Solidity
event Approval(address indexed owner, address indexed spender, uint256 value);
Only the 'account' address is indexed here, so you can search deposits by account.
Blockchain / Solidity
event Deposit(address indexed account, uint256 amount);
Sample Program

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.

Blockchain / Solidity
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);
    }
}
OutputSuccess
Important Notes

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.

Summary

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.