Topics are indexed parameters to an event.
topic[0]
always refers to the hash of the hash of the event itself, and can have up to 3 indexed arguments, which will each be reflected in the topics.
EVM uses low-level primitives called logs to map them to high-level Solidity construct called Event. Logs may contain different topics that are indexed arguments.
Consider Event:
event PersonCreated(uint indexed age, uint height);
And you fire it the foobar
function of MyContract
:
function foobar() {
emit PersonCreated(26, 176);
}
This will create a low-level EVM log entry with topics
0x6be15e8568869b1e100750dd5079151b32637268ec08d199b318b793181b8a7d (Keccak-256 hash of PersonCreated(uint256,uint256)
)
0x36383cc9cfbf1dc87c78c2529ae2fcd4e3fc4e575e154b357ae3a8b2739113cf (Keccak-256 hash of age
), value 26
You'll notice that height will not be a topic, but it will be included in the data section of the event.
Internally, your Ethereum node (Geth / Parity) will index arguments to build on indexable search indexes, so that you can easily do look ups by value later. Because creating indexes takes additional disk space, indexed parameters in events have additional gas cost. However, indexed are required to any meaningful look up in scale of events by value later.
Now in the web3 client you want to watch for creation events of all persons that are age
of 26, you can simply do:
var createdEvent = myContract.PersonCreated({age: 26});
createdEvent.watch(function(err, result) {
if (err) {
console.log(err)
return;
}
console.log("Found ", result);
})
Or you could filter all past events in similar fashion.
More information here
From the experience, the issue of using the filter is quite complicated, since the use of callback and synchronization with the blockchain make this type of utilities difficult.
To begin with, I tell you that the ideal would be to filter through the exact block that is the only one that gives me resultant feedback and does not give me failures. The fastest would be to use an external log to save the transactions and addresses.
I also recommend that you see this API, so you know a little how to work with these topics.
Tea I leave a pretty useful link with a case similar to what you try. Link
Best Answer
In a non-anonymous event,
topics[0]
will be the signature of the event name. In an anonymous event, the signature of the event name is not a topic.If you have access to the ABI or the source code of the contract, here is what you could do:
topics[0]
is equal to this signature. If not, you have an anonymous event.If you don't have access to the ABI or the source code, I don't think that it is possible because there is no way to know if
topics[0]
is the event name signature or abytes32
indexed argument.On a side-note, the JSON ABI would indicate if an event is anonymous (see the docs).