What are Topics in Event Logs and How to Use Them?

Log topics are 32-byte words that describe the inner workings of an event. Different operating codes are needed to determine the number of topics included in the records. To illustrate, let's compare the differences between LOG1 and LOG4.In short, LOG1 includes one topic, while LOG4 includes four. The topics are what help us filter the events in the logs.

With the right topics, we can subscribe only to a particular event instead of all events. For example, in an Ethereum blockchain, when an event is emitted in the smart contract, the event is stored in the transaction logs and the logs are stored on the blockchain. Therefore, we can retrieve events from block 0 by adding a fromBlock parameter to the event. Using this approach, we can conveniently query events by name and automatically decode log data.

This can save us a lot of time if performance is a priority. Of course, everything described here is already implemented in web3.