December 31, 2022

Ethereum Logs and Events – What are Event Logs on the Ethereum Network?

Table of Contents

In traditional programming practices, applications, websites, and other programs use logs to capture and constitute occurrences/events at certain moments. Logs are also important for developers as devs can use them for debugging apps, notifying log viewers of what is going on, detecting events, etc. However, not only are logs essential in conventional Web2 development, but they play an equally important role within the decentralized web. If you want to examine logs and events further, read on as we explore the intricacies of Ethereum event logs, how to use them, and why they are helpful!

To kickstart this tutorial, this article dives straight into Ethereum event logs, providing a comprehensible overview of what they entail. In doing so, you will learn that they consist of two main components: topics and data. From there, we will explain where event logs are stored and then explore three potential use cases. Lastly, to top things off, the final section provides an Ethereum logs example where you will learn how to query any contract’s logs using Moralis

Moralis is the top choice among various blockchain infrastructure companies, supplying enterprise-grade development tools like Web3 APIs. Among these tools, you will, for instance, find the Web3 Steams API. With this application programming interface, you can seamlessly stream on-chain data into the backend of all your Web3 projects through Moralis webhooks

Furthermore, with Moralis and the provider’s Web3 APIs, you can fully leverage the power of blockchain to build dapps faster and smarter. What’s more, you can sign up with Moralis entirely free, and it only takes a couple of seconds! 

Nevertheless, let us move further into the next section by answering the question, ”what are logs and events on Ethereum?”.

What are Logs and Events on Ethereum? 

In the conventional Web2 development space, most applications commonly utilize logs to describe and capture occurrences at certain moments. Developers often use logs like these to detect particular events, notify the log viewer of circumstances, debug applications, etc. Additionally, event logs play an equally important role within Web3 when writing or interacting with Ethereum smart contracts! 

Exploring the Ethereum network and Ethereum event logs

All Ethereum smart contracts generally have the ability to emit events when they execute. When this happens, the contracts additionally generate logs and provide insight into the events, which we can find on transaction receipts. Consequently, whenever a transaction is “mined”, it becomes possible to view event logs related to the particular transaction by querying the blockchain network. Moreover, note that within the Ethereum ecosystem, it is not uncommon to use the terms “events” and “logs” synonymously. 

Ethereum Virtual Machine (EVM) has five opcodes – or “operation codes” – for emitting Ethereum event logs: LOG0, LOG1, LOG2, LOG3, and LOG4. Web3 developers can use these opcodes to create log records describing events within smart contracts. This can, for instance, be Ethereum events, such as when NFTs change ownership or token transfers occurs. 

Furthermore, every Ethereum log consists of two main components: topics and data. Log topics are 32-byte ”words” describing the inner workings of an event. The various opcodes are required to describe the number of topics included in log records. To exemplify, let us briefly compare the differences between LOG1 and LOG4. In short, LOG1 includes only one topic; meanwhile, LOG4 includes four. This further means that the maximum number of topics we can include in a log record is four.

Nevertheless, to make this more straightforward, we will take the following two sub-sections to dive deeper into log topics and log data! 

Ethereum Logs Records: Topics 

The first part of all Ethereum logs consists of an array of various topics describing its associated event. The initial topic is usually the signature or hash of the event name, including its parameters. However, there are exceptions, and an example is anonymous transactions, where we deliberately exclude this signature.

circle with Ethereum logs and events surrounded by Web3 components

Moreover, due to the 32-byte size limitation of topics, they can not reliably be represented by data types like strings or arrays. If you try introducing a topic exceeding the size limitation, it is hashed automatically, which is problematic for several reasons. However, one clear example is that you can only reverse the value if you know the original input.

So, what does this mean? Well, this suggests that topics can only be used for data narrowing down search queries such as a crypto address, for example. We can, therefore, compare Ethereum event log topics to indexing keys referencing an event. So, with a more profound understanding of the first Ethereum event logs component, let us dive straight into the second one: data!

Ethereum Logs Records: Data 

The other component of Ethereum event logs is data, which we will briefly explore in this section. Both topics and data work best in unison since they have their own pros and cons. For instance, topics are searchable; meanwhile, the data is not. On the other hand, including data in event logs is much cheaper than topics. 

sequence of Ethereum events as users fetch data and monitor it via Ethereum event logs

Furthermore, while the event log topics are restricted by their 32-byte size limitations, no such bounds are put on the data. Consequently, it is possible to include more complex and more significant types, such as strings and arrays, in the data of the Ethereum event logs. 

In conclusion, this means that the data of the Ethereum event logs can be seen as the corresponding values of the topic keys. 

Where is an Ethereum Log Stored? 

To understand the location/storage of Ethereum logs, we must briefly return to the intricacies of smart contracts. Ethereum smart contracts store information in one of two distinct ways: account storage and event logs. The account storage contains information and data in relation to the smart contract’s state. Moreover, this is data that smart contracts themselves can access. 

On the other side of the spectrum, we find logs storing information that the contract does not have access to. However, this information can significantly benefit other off-chain sources like applications and UIs. What’s more, it is worth noting that storing data in event logs is more economically friendly than the account storage alternative, but more on this in the next section. Nevertheless, where exactly are Ethereum event logs stored?

receipt/log of an Ethereum event

Whenever a smart contract emits an event, the associated log data is written and recorded in a transaction receipt. All transactions have a particulate receipt containing transaction outcomes, which includes status and event logs. Consequently, in addition to the logs, a transaction receipt also contains data like the gas amounts used, log blooms, etc. 

In sum, transaction receipts, along with other pieces of data, store Ethereum event logs. Also, “trie” structures organize them. Nevertheless, with a more profound understanding of Ethereum event logs, let us look closely at three prominent use cases! 

Ethereum Logs Use Cases 

With an overview of the ins and outs of Ethereum event logs, this section will explore some of their most prominent use cases. Specifically, we will focus on three examples: 

  1. Asynchronous Triggers with Data – When building dapps and other Web3 platforms, you generally set up your projects so that they monitor relevant Ethereum smart contract events. Consequently, Ethereum event logs act as a form of asynchronous triggers with data, allowing smart contracts to interact with a dapp’s fronted. In turn, dapps can utilize the information relayed by the event logs to display a message, perform certain actions, or any other associated tasks. 
  1. Cheaper Storage – Ethereum event logs were initially designed to be a cheaper data storage method requiring significantly less gas than account/contract storage. Logs require eight gas per byte; meanwhile, account storage demand 20,000 gas per 32 bytes. Accordingly, Ethereum event logs present a significantly more economically friendly method for storing data. However, it is essential to note that logs are not accessible to any smart contracts. 
  1. Smart Contract Return Values – The third and most straightforward Ethereum event logs use case is to pass along the return value of a contract to a dapp’s frontend. Whenever a transaction invoking a smart contract executes, it triggers a callback. As such, this allows the frontend of a dapp to query the return value from the contract.

You now know three potential ways in which you can use Ethereum event logs. However, you might still need help implementing and using logs in your Web3 projects. If this is the case, join us in the following section, where we provide a brief Ethereum event logs example. This shows the most accessible way of querying this data from the various blockchains and using it in your dapps! 

Ethereum Logs Example 

Now, you might wonder, ”how can I easily query Ethereum event logs?”. Well, the answer to this question is the best Web3 infrastructure provider, Moralis. With the various Web3 APIs offered by Moralis, you can seamlessly build any Web3 projects smarter and more efficiently. These tools bridge the development gap between Web2 and Web3, enabling you to leverage the power of blockchain technology to its fullest! 

unlock the power of blockchain with Moralis

Among the Web3 APIs from Moralis, you can find the EVM API acting as an “events API”. With this development tool, you can easily get the logs of a contract through a few lines of code. All you essentially need to do is make an API call to the ”getContractLogs” endpoint, and it can look something like this: 

import Moralis  from 'moralis';
import { EvmChain } from '@moralisweb3/common-evm-utils';

try {
    const address = '0x1234567890123456789012345678901234567890';

    const chain = EvmChain.ETHEREUM;

    await Moralis.start({
        apiKey: 'YOUR_API_KEY',
        // ...and any other configuration
    });

    const response = await Moralis.EvmApi.events.getContractLogs({
        address,
        chain,
    });

    console.log(response?.result);
} catch (e) {
    console.error(e);
}

In order to run the code above, all you need to do is install the required dependencies, add your Moralis API key, and pass the address of the smart contract your want to query. Moreover, you can also try it directly in your browser by visiting Moralis’ get logs by contract documentation page. It should look something like this: 

complete documentation page showing information on Ethereum event logs and code structures

To try it out, simply input a contract address and click on the ”Try It!” button:

showing the path parameter on the documentation page

Doing so should return a response similar to the one shown down below: 

{
  "total": 290974,
  "page_size": 100,
  "page": 1,
  "cursor": "eyJhbGci…,
  "result": [
    {
      "transaction_hash": "0x0b5d2cfc76ec7d603fd0f766533c9e90140141680446ed09bc000fe81e7dc528",
      "address": "0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb",
      "block_timestamp": "2022-12-29T10:10:11.000Z",
      "block_number": "16289728",
      "block_hash": "0x94f79f3d8dd6dfc38bec76d776c1d0b2b9be5a3307f3f2cccc0686fca9a106fb",
      "data": "0x000000000000000000000000000000000000000000000006735166905fbb0000",
      "topic0": "0x3c7b682d5da98001a9b8cbda6c647d2c63d698a4184fd1d55e2ce7b66f5d21eb",
      "topic1": "0x000000000000000000000000000000000000000000000000000000000000038b",
      "topic2": "0x0000000000000000000000000000000000000000000000000000000000000000",
      "topic3": null,
      "transaction_index": 111,
      "log_index": 198
    },

If you want to see a practical implementation of the events API and the ”getContractLogs” endpoint, check out the YouTube video below illustrating how to create an application for fetching Ethereum logs of any contract: 

Would you like to compare Moralis with other methods for querying blockchain data? If so, check out the article on how to listen to Ethereum smart contract events using ethers.js. With this guide to ethers.js events, you can seamlessly learn how to create an ethers.js dapp in no time! 

Summary – What is an Ethereum Log?

Ethereum smart contracts can emit events when they execute. In combination with performing an action, they can also generate event logs. These logs provide insight into the events themselves, and you can find them in a contract’s transaction receipt. 

Ethereum event logs consist of two core elements: topics and data. Each event log has an array of topics describing the event they relate to. Furthermore, topics have a 32-byte size limit, suggesting that data types like strings and arrays cannot reliably represent them. However, the log’s data has no such restrictions, enabling the storage of more complex and more extensive data types. 

In this article, we presented three potential use cases for Ethereum event logs: 

  1. Asynchronous Triggers with Data
  2. Cheaper Storage 
  3. Smart Contract Return Values

Finally, in the last section of this article, we dove deeper into Moralis and the EVM API (a.k.a. “events API”). This interface presents the easiest way to query Ethereum event logs. In fact, thanks to the accessibility of Moralis, all it requires is a single API call to the ”getContractLogs” endpoint. 

If you found this article helpful, consider checking out more Web3-based development content here on the Web3 blog. For instance, read our article answering, ”what is danksharding?” or learn how to use a Web3 JS call contract function

Moralis Academy

Moreover, consider enrolling in Moralis Academy if you want to take your Web3 development skills to the next level. The academy supplies industry-leading development courses for all developers. It does not matter if you are a seasoned programmer or have no prior development skills. For instance, if you are new to the space, check out the following course: ”Blockchain & Bitcoin Fundamentals”. 

What’s more, do not forget to sign up with Moralis! Creating an account is free of charge, and you will not regret it! 

Moralis Money
Stay ahead of the markets with real-time, on-chain data insights. Inform your trades with true market alpha!
Moralis Money
Related Articles
February 15, 2024

Comparing the Industry’s Leading Web3 API Providers – Moralis vs. Alchemy vs. QuickNode

December 29, 2022

Web3 Wallet Tracker – How to Create a Firebase Web3 Wallet Tracker

September 24, 2022

Web3 APIs – Exploring the Top 5 Blockchain APIs

August 16, 2022

NodeJS SDK for Web3 – Exploring Moralis’ NodeJS SDK

October 31, 2023

Complete Blockchain API Tutorial with API Examples

March 6, 2023

Web3 Marketplace Development – Build a Web3 Business in 3 Steps

December 19, 2022

Ultimate Guide to Ethers.js Events and How to Use Them

January 2, 2023

Transaction Details by Hash – How to Get Ethereum Transaction Details

September 23, 2022

How to Create a Decentralized App in Just 3 Steps