What are Events in solidity and how do use it?
Hello Developers ?,
Hope you all are doing well, in this article we will discuss the Events in solidity. You might be familiar with events in python if you know the python programming language. The concept of events in solidity is no different than events in python, as the motive of the event is to raise a flag to alert others that something of interest has happened.
What are Events in Solidity?
Let’s first understand what are events in general. Events are like notifications that give you alerts when something of interest happens or an important event takes place.
For example, when you subscribe to a YouTube channel, you do not need to go every time and check if a creator has uploaded a new video. Instead, you will get notified when he will upload a new video. And that’s what the events do.
Now let’s see how events work in Solidity . . .
EVM(Ethereum Virtual Machine) has a functionality called a logging functionality which logs the important messages on the blockchain.
And we can read these logs from blockchain nodes that we run. Events allow us to write these logs to the blockchain.
Events are very important from smart contract developers’ perspective as events allow them to emit some important alerts to the front end or user interface. In short, events facilitate communication between smart contracts and their user interfaces. The events consume less gas and are cheaper as they are not accessed by smart contracts.
Why Use Events?
Now we know what are events, but why do we need them, or why do we use events?
Events play a vital role in blockchain application building. In blockchain applications sometimes transactions take time to process and sometimes the next task or operation to be performed is determined by the result of the processing transaction. So it is very difficult for the user or client to get to know if the transaction is completed or failed or still processing as you can not communicate with smart contracts directly.
And that’s where Events comes to the rescue. Events allow us to send an update of the ongoing processes to the frontend or user interface, which significantly helps users to know what’s happening under the hood so they can wait or cancel the transaction.
How to use events in solidity?
Declaring an Event
To define an event you start with the keyword “event” followed by the name of the event and parameters wrapped inside ().
event AccountSwitched(address indexed from, bytes32 indexed to
Emitting an Event
You emit an event in the respective function by writing a emit keyword followed by the name of the event and the specified parameters wrapped inside the ().
Parameters passed to the events
There are two types of parameters you can pass to an event
- Indexed Parameters – Indexed parameters are searchable parameters and help to query events. They are also called “topics”.
- Non-Indexed Parameters – Non-Indexed parameters are regular parameters passed to an event that is not searchable and are only used to log the messages to the blockchain.
The concept of events in solidity is the same as in other programming languages but they work uniquely as they get logged into the blockchain. Events costs cheaper as smart contracts can not access the events within the contract. Events are integral for smart contract developers as they help build good blockchain applications for users to communicate with the blockchain.
Unlimited access to all platform courses
100+ practice projects included
ChatGPT Based Instant AI Help
Structured Web 3 Roadmap To Get A Job
Exclusive community for events, workshops
Sharing is caring
Did you like what Shashank Wangkar wrote? Thank them for their work by sharing it on social media.
No comments so far
- Smart Contract Developer