A complete guide to Events in Solidity

In this tutorial, we are going to learn what Events are in Solidity, how to create a type of event and how to emit them. This will teach you how to create logs in Solidity.

In this tutorial, we are going to learn what Events are in Solidity, how to create a type of event and how to emit them. This will teach you how to create logs in Solidity.

Solidity events allow you to create logs with the data you want whenever you want in your smart contract functions. These logs will then be saved in the transaction data in the blockchain.

The use cases of Solidity logs are:

  • Emitting events that front-end applications will listen to and update their interfaces accordingly
  • Storing data about what happened. It's a cheap form of storage.
  • Create subgraphs (or allow other people to do it) for reading data from your smart contract faster
  • Know what happened at specific moments

How to create events and logs in Solidity

First, you have to define a new type of events and then in your functions you can emit these events which will create logs.

Here is an example:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

contract Messaging {
    // Create 2 new event types
    event Message(address indexed sender, address indexed recipient, string message);
    event MessageDelivered();

    function sendMessage(address _recipient) public {
        emit Message(msg.sender, _recipient, "Hello World!");
        emit MessageDelivered();
    }
}

So to create an event, we use the event keyword, followed by the name of the event and the parameters you can pass when emitting the event.

Parameters are pieces of data about the event that you can set when emitting the event.

You can choose to index a parameter or not using the indexed parameter which will allow other applications to filter the logs based on the value that this parameters has when getting the logs your smart contract created.

If a parameter is not an indexed parameter, other apps can't filter the logs of your smart contract based on the value of that parameter.

An event can only have up to 3 indexed parameters.

And in total, just like functions, an event can only have up to 17 parameters. If a parameter is an array, it will count as 2 parameters.

So if your event has 6 array parameters and 6 other types of parameters, it will count as 18 parameters and it exceeds the stack so it won't work.

You can see a real example of logs on ERC-20 tokens smart contracts. These contracts have a Transfer event that is emitted when a token is transferred from an address to another.

So when sending ERC-20 tokens, it generates a log of type Transfer and you can see that log on Etherscan.

Here is an example of a transaction that sends LINK tokens:

https://goerli.etherscan.io/tx/0xe1e8699d445d10918b5770463b87a4147dea88e098a8c8a36a99c0abf005153a#eventlog

If you go to the Etherscan page of a transaction and the transaction created logs, there will be a Logs tab at the top, next to Overview.

If you go to that tab, you can get information about every log that this transaction created.

For each log, you'll see:

  • Address: the address of the smart contract that created this log
  • Name: the name of the event corresponding to that log.
    It also shows the parameters that can be passed to that event.
  • Topics: The topics of this event (more on this below)
  • Data: The non-indexed parameters of the event as an hexadecimal string.
    If the smart contract is verified on Etherscan, the ABI is available so we can view the decoded value of the parameters in the “Dec” mode. If you select “Hex” it will be it's raw hexadecimal value.

Topics are what an application can use to get and filter logs.

A log can only have up to 4 topics. It will always at least 1 topic which is the hash of that log.
The next 3 topics it can have are the values of the indexed parameters of the event.

Just like the for the non-indexed parameters in the Data section, if the smart contract that created the log is verified on Etherscan, you can see the decoded value of the parameter by selecting “Dec” next to the parameter.

You can also see the raw hexadecimal value by selecting “Hex”. That raw value is the only value you would be able to see if the smart contract is not verified on Etherscan.

Lastly, you can see the whole list of logs that the contract created by going to the Etherscan page of that smart contract and selecting the "Events" tab.

https://etherscan.io/address/0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e#events

How to get logs from a smart contract

You can get the logs of a smart contract and filter them by type and indexed parameters by calling the eth_getLogs method of Ethereum's JSON-RPC API.

Check out the documentation to learn more about that method.

If you're using Brownie, check out this StackOverflow answer.

If you're using Ethers JS (in a JavaScript app or in Hardhat), check out our full tutorial on how to get logs using Ethers JS here:

How to listen for smart contract events and get logs using Ethers JS
In this tutorial, we are going to learn how to get all the logs of a smart contract and how to filter them and how to listen for smart contract events using Ethers JS and JavaScript.

In Hardhat, it's going to be a bit different because we don't create the Contract instances the same way but the rest is similar.

If you're using Wagmi in a React application, check out our turorial here:

How to listen for contract events and get logs using Wagmi
In this tutorial, we are going to learn how to listen for smart contract events and get logs and filter them in your React app using Wagmi hooks and Ethers JS.

And lastly, if you're using Web3.js, check out our tutorial here:

How to listen to smart contract events using Web3JS
In this tutorial, we are going to learn how to listen to smart contract events and run code when the event is triggered and a log is added using Web3JS and JavaScript.

And that's it 🎉

Thank you for reading this article!