In Ethereum smart contracts, events are a way to log information on the blockchain. Events can have parameters, and some of these parameters can be marked as indexed. Indexed parameters allow for more efficient filtering and searching of events when querying the blockchain.
What are Indexed Parameters?
Indexed parameters are parameters in an event that are marked with the indexed
keyword in the event declaration. When an event is emitted, the indexed parameters are stored in the transaction log, allowing clients to filter events based on these parameters.
Each event can have up to three indexed parameters. The remaining parameters are non-indexed and can be accessed but not filtered directly.
Example of an Event with Indexed Parameters
pragma solidity ^0.8.0;
contract SimpleStorage {
event ValueChanged(address indexed author, string oldValue, string newValue);
string private _value;
function setValue(string memory newValue) public {
emit ValueChanged(msg.sender, _value, newValue);
_value = newValue;
}
}
In this example, the ValueChanged
event has one indexed parameter: author
(the address of the user who changed the value). The other parameters, oldValue
and newValue
, are non-indexed.
How Indexed Parameters Work in Ethers.js
When using Ethers.js, you can create filters for events based on indexed parameters. This allows you to listen for or query events that match specific criteria.
Creating a Filter for Indexed Parameters
const { ethers } = require('ethers');
// Connect to the Ethereum network
const provider = new ethers.providers.JsonRpcProvider('https://your.ethereum.node');
// Contract ABI and address
const abi = [
"event ValueChanged(address indexed author, string oldValue, string newValue)"
];
const contractAddress = '0xYourContractAddress';
// Create a contract instance
const contract = new ethers.Contract(contractAddress, abi, provider);
// Create a filter for the ValueChanged event for a specific author
const filter = contract.filters.ValueChanged('0xAuthorAddress', null, null);
In this code, we create a filter for the ValueChanged
event, specifying a particular author's address. The null
values indicate that we are not filtering on the oldValue
and newValue
parameters.
Listening for Filtered Events
pragma solidity ^0.8.0;
contract SimpleStorage {
event ValueChanged(address indexed author, string oldValue, string newValue);
string private _value;
function setValue(string memory newValue) public {
emit ValueChanged(msg.sender, _value, newValue);
_value = newValue;
}
}
1
With the filter in place, the listener will only respond to ValueChanged
events emitted by the specified author.
Querying Past Events with Indexed Parameters
You can also query past events using indexed parameters. This is done using the
3 method:
pragma solidity ^0.8.0;
contract SimpleStorage {
event ValueChanged(address indexed author, string oldValue, string newValue);
string private _value;
function setValue(string memory newValue) public {
emit ValueChanged(msg.sender, _value, newValue);
_value = newValue;
}
}
pragma solidity ^0.8.0;
contract SimpleStorage {
event ValueChanged(address indexed author, string oldValue, string newValue);
string private _value;
function setValue(string memory newValue) public {
emit ValueChanged(msg.sender, _value, newValue);
_value = newValue;
}
}
4
Conclusion
Indexed parameters in events are a powerful feature that allows for efficient filtering and querying of events in Ethereum smart contracts. By using indexed parameters, developers can create more responsive applications that only react to relevant events, improving performance and user experience.