Events in Solidity programming language help inform when something has happened in the smart contract. Parties like a web frontend, data gathering tools, and more can listen to specific events and react accordingly. In this post, we will dig deeper into Events and how to use them when developing smart contracts on Ethereum-like blockchains.
We need to start with the event keyword to define an event.
An event can have additional parameters. It means that it is a struct that can hold data. Data types are the same that we are using in Solidity language.
Event struct can hold multiple parameters. We can name these parameters, but that is optional, and we can provide just the data type.
Suppose we want to filter the logs when listening to the specific events. We need to specify which of the parameters should be indexed. There is a limit that only up to 3 parameters can be indexed. Keep in mind that indexing parameters are cost-efficient and will take more gas fees for the transaction to be mined.
When a specific action has happened, we can release the event with the emit keyword followed by the event name.
If an event does have any parameters, we can specify those in the parenthesis in the same order as to how we defined them.
To see emitted events can use Remix IDE, Etherscan block explorer, or any library to interact with Ethereum like chains like Ethers.js, web3.js or any other. How to do that is out of the scope of this blog post.
Events in Solidity programming language are a great way to inform other parties that something has happened. We can't just rely on the transaction status. An event can hold data, and others can filter events and react upon that, for instance, in the web app.