What are Events on Ethereum and How to Use Them

Events and logs are essential components of Ethereum, as they enable communication between smart contracts and their user interfaces, facilitate the return of values from smart contracts to the user interface, trigger asynchronous processes with data, and provide a cost-effective form of storage. In this article, we'll discuss both topics and data, as they each have their own advantages and disadvantages. For instance, topics are searchable while data is not. On the other hand, including data in event logs is much more cost-efficient than topics.

Smart contracts have the capability to emit events during their execution. These events are also known as registrations in Ethereum. The result of the events is stored in the receipts of the transactions in a records section. Events have been widely used in Ethereum smart contracts to record when significant action has taken place, particularly in symbolic contracts (e.g., ERC-20) to indicate that a token transfer has occurred.

In these cases, you can read blockchain events and subscribe to them so that you receive real-time notifications as the transaction is mined. An event is an effective way to record something that has happened in the contract. The events that were broadcast remain on the blockchain along with the other contract data and are available for future audits. This is an improvement over the previous example, since events are used to provide a history of donations from the charity.

The application would connect to the Ethereum node via JSON-RPC and would watch (wait) for the event to occur or read all past events to synchronize the internal state of the application with the Ethereum blockchain. Note that the event signature hash is the first topic by default (unless declared anonymous) and each event is limited to a total of three indexed topics. The yellow paper introduced the terminology of events, but an event is a way in which DApps can obtain information at a specific point during the execution of the code of a smart contract (EVM). The syntax of the event signature is essentially similar to that of a function, but it doesn't have a body itself; however, it can take parameters that will be saved in the event data.

The real power of events lies in their ability to be found and receive notifications about them based on their topics. An event allows a contract to record a change of state on the blockchain in a specific format so that the Ethereum virtual machine can easily retrieve and filter it; moreover, an event can carry with it data about the change in state. To invoke or activate an event, you must include it in the body of the function that you want to cause to generate it.