In this section, we introduce event in Solidity, using transfer events in ERC20 tokens as an example.
Events
The events in solidity are the transaction logs stored on the EVM (Ethereum Virtual Machine). They can be emitted during function calls and are accessible with the contract address. Events have two characteristics:
- Responsive: Applications (e.g.
ether.js) can subscribe and listen to these events throughRPCinterface and respond at frontend. - Economical: It is cheap to store data in events, costing about 2,000
gaseach. In comparison, storing a new variable on-chain takes at least 20,000gas.
Declare events
The events are declared with the event keyword, followed by the event name, and then the type and name of each parameter to be recorded. Let's take the Transfer event from the ERC20 token contract as an example:
Transfer event records three parameters: from,to, and value, which correspond to the address where the tokens are sent, the receiving address, and the number of tokens being transferred. Parameters from and to are marked with indexed keywords, which will be stored in a special data structure known as topics and easily queried by programs.
Emit events
We can emit events in functions. In the following example, each time the _transfer() function is called, Transfer events will be emitted and corresponding parameters will be recorded.
EVM Log
EVM uses Log to store Solidity events. Each log contains two parts: topics and data.

Topics
Topics is used to describe events. Each event contains a maximum of 4 topics. Typically, the first topic is the event hash: the hash of the event signature. The event hash of the Transfer event is calculated as follows:
Besides event hash, topics can include 3 indexed parameters, such as the from and to parameters in the Transfer event. The anonymous event is special: it does not have an event name and can have 4 indexed parameters at maximum.
indexed parameters can be understood as the indexed "key" for events, which can be easily queried by programs. The size of each indexed parameter is 32 bytes. For the parameters larger than 32 bytes, such as array and string, the hash of the underlying data is stored.
Data
Non-indexed parameters will be stored in the data section of the log. They can be interpreted as the "value" of the event and can't be retrieved directly. But they can store data with larger sizes. Therefore, the data section can be used to store complex data structures, such as array and string. Moreover, data consumes less gas compared to topic.
Remix Demo
Let's take Event.sol contract as an example.
-
Deploy the
Eventcontract. -
Call
_transferfunction to emitTransferevent.

- Check transaction details to check the emitted event.

Query event on etherscan
Etherscan is a block explorer that lets you view public data on transactions, smart contracts, and more on the Ethereum blockchain. First, I deployed the contract to an Ethereum testnet (Rinkeby or Goerli). Second, I called the _transfer function to transfer 100 tokens. After that, you can check the transaction details on etherscan:URL
Click Logs button to check the details of the event:

There are 3 elements in Topics: [0] is the hash of the event, [1] and [2] are the indexed parameters defined in the Transfer event (from and to). The element in Data is the non-indexed parameter amount.
Summary
In this lecture, we introduced how to use and query events in solidity. Many on-chain analysis tools are based on solidity events, such as Dune Analytics.