Events in Solidity

What are Events in Solidity?

As Solidity is similar to languages like Python, let's take a look at what Events are in Python and co-relate them with Events in Solidity.

Events in Python are basically a class that notifies other classes when a specific event takes place which might be of interest to the other classes. In simple language, an Event is basically raising a flag and alerting others that something worth nothing has taken place.

Events in Python are generally used when there is some UI interaction involved and to help alert the users of the occurrence of an event. A real-life example could be a store is being robbed and the employees pressing the Emergency button to notify the police and the shop owner about the theft. The pressing of the button leads to the triggering of an event where the system then needs to send an alert to the police and the shop owner about the robbery taking place. This chain of events is how events in Python work as well.

Events in Solidity work on the same logic as well but the only difference is that they are on the blockchain and thus work a bit differently. Let's take a look at how Events work in Solidity.

As said above, Events are used to interact with the UI to make sure that the user is alerted about any events which the user should be notified about, for example, a transaction passing or failing, deposits failing, etc. Events notify a user about the events happening on the Blockchain to ensure ease of use when someone uses dApps. If the user has to scan the Blockchain themselves to make sure whether their transaction has passed or failed it would have been very inconvenient for the user, instead of this Events do the job of notifying the user whether something has passed or failed by showing it on the UI itself. There are two types of Events in Solidity, one of them is where we can interact with the UI and one of them is called by using the keyword "emit", which means that the Event will not show up on the UI but will just be stored on the blockchain and will stay there as long as the Smart Contract is up and running on the Blockchain. Events were added in a recent update to Solidity when hackers managed to steal around 3.6m$ from a DAO resulting in a hard fork of the Solidity languages and changes being made to it. Out of these changes, the addition of Events was the most prominent one as they were added to prevent further hacks using the same method.

Let's see how to create an Event in Solidity:

event deposit(
address indexed user
uint256 time
uint256 etherAmount
);
//output: will display the user address, the time of the transaction and the amount of money sent

Now if we emit the Deposit event let's see what happens:

function deposit() payable {
event deposit(
address indexed user
uint256 time
uint256 etherAmount
);
emit deposit(msg.sender, block.timestamp, msg.value);
};
//output: -

Here, 'msg. sender' is the account address sending the Ether, 'block. timestamp' is the time when the Ether was sent, 'msg. value' is the amount of Ether sent. All of these are global variables and will only be emitted here as they are being told to emit the deposit event.

If you have any more questions you can find me on my Twitter and LinkedIn