What are events in Solidity?
Solidity events are crucial for smart contract developers because they allow smart contracts to index variables in order to rebuild the storage stage, help to automatically update the user interface, and allow for testing of specific variables. This article will develop your understanding of Solidity events and help you deepen your understanding of the Solidity programming language.
We’ll first introduce Solidity events, then give some of their classifications, and provide examples. By the end of this article, you will be able to create a Solidity event for your next project.
What is an event in Solidity?
In Solidity, events are dispatched signals that smart contracts can fire. When you call events, they cause the arguments to be stored in the transaction’s log, which is a special data structure in the blockchain. Events notify external users, such as a listening frontend website or client application, that something has happened on the blockchain.
What is the difference between events and functions in Solidity?
While both functions and events accept arguments and can be called, functions modify smart contracts directly while events have the role of informing services outside of the blockchain to let users know that something has happened.
Functions in Solidity allow developers to read, write, change, and store data in the smart contract. You can pass arguments or parameters into a function. You can also call a function whenever it is needed in the code.
Events also accept arguments, but these are stored in the transaction’s log, which is inaccessible to smart contracts. Contact data lives in the States trie and event data is stored in the Transaction Receipts trie, meaning contracts cannot read event data.
Like functions, events can be called. However, the emit keyword is used to call/dispatch events. This allows developers to know when an event or a function is being called.
What is the relationship between events and logs?
The Ethereum Log data structure stores the data emitted by events.
The Ethereum Virtual Machine (EVM) has a logging function that is used to write data, including Solidity events, to a structure outside smart contracts.
Logs and events are often referred to synonymously. Events allow you to ‘print’ information to the logging structure in a way that is more gas-efficient since the information is not stored in a storage variable, which takes up more gas. Events, or logs, live in the Transaction Receipts trie, which is inaccessible to smart contracts.
How are events indexed in Solidity?
Solidity events are interfaces with EVM logging functionality. You can add an attribute indexed to up to three parameters. Then they appear in the structure of topics, not the data portion of the log. When parameters do not have the indexed attributes, they are ABI-encoded into the data portion of the log.
Solidity Event Types
There are two types of Solidity events: those which are indexed and those which are not.
When parameters do not have the indexed attribute, they are ABI-encoded into the data portion of the log. These parameters form the byte array of the event. Data is encoded according to its type and can be decoded according to a schema.
Indexed parameters are also known as “topics”, are the searchable parameters in events. The indexed parameters will allow you to search for these events using the indexed parameters as filters. You can add an attribute indexed up to 4 parameters or 3 parameters based on whether the events are anonymous or not, respectively.
How do events work in Solidity?
Solidity events are declared, emitted, and registered.
First, the event type has to be declared with the event keyword in Solidity.
Next, the event has to be emitted with the keyword emit.
Anytime something in the blockchain changes, your program will automatically register this change and trigger the event
Note: Emitting an event after declaration allows you to then ‘listen’ for the event from your application using libraries like Web3.js or Ethers.js.
How do you declare an event in Solidity?
The declaration of an event contains the name of the event and the parameters that you want to save when you trigger the event.
First, you have to declare an event in Solidity. Then, you emit the event with the keyword emit.
The following is an example of how to declare an event:
event moneySent(address_from, address_to, unit_amount);
The event, when triggered, will inform the external application that something on the blockchain has changed.
How are events emitted in Solidity?
After the event is defined, you can trigger the event using the keyword emit. Once an event is emitted, the arguments passed are stored in transaction logs.
The following syntax shows you how to use emit in Solidity:
emit transfer(_from, _to, amount);
How to Listen to Events in Solidity
Once events are emitted, you can listen for them by subscribing to catch these events using ethers.js. Then dApps, or anything connected to an Ethereum JSON-RPC API, can listen to these events and act accordingly.
Solidity Event Example
The following sample code defines and emits an event for transfers. This code is applicable to an Ethereum transfer application, where the event is triggered on a transfer. Additional code can be added to allow the event, upon triggering, to update the user interface to show that a transfer has taken place.
The next example, taken from Solidity by Example, creates an event that has two parameters: the address of the sender and the string message. When triggered, the event logs “Hello World” and “Hello EVM”.
Keep Learning About Solidity
This article introduces you to Solidity events and provides you with explanations and resources to use for your next project. With Solidity events, you will have an easier time creating efficient smart contracts.
If you’re new to the Solidity language and you’re looking forward to building your first smart contract, secure your spot in Alchemy University's free, 7-week online Solidity programming course.
What it is, How it Works, and How to Get Started
Your Guide to Getting Started With Solidity Arrays—Functions, Declaring, and Troubleshooting
An Introduction to Solidity: Web3's Most Popular Programming Language