A Guide to Events in Solidity

Rahul Ravindran

Events are inheritable members of contracts. When you call them, they cause the arguments to be stored in the transaction’s log — a special data structure in the blockchain. These logs are associated with the address of the contract, are incorporated into the blockchain, and stay there as long as a block is accessible

Solidity defines events with the event keyword. After events are called, their arguments are placed in the blockchain. This way we can query it later. Events can also be subscribed by the front end client application.

All information in the blockchain is public and any actions can be found by looking into the transactions close enough but events are a shortcut to ease development of outside systems in cooperation with smart contracts. They are also indexable, meaning you can search for them. So whenever something happens within your smart contract which some system outside the blockchain should know about you should emit an event and the outside system can listen for such events.

You can add the attribute indexed to up to three parameters which adds them to a special data structure known as “topics” instead of the data part of the log. If you use arrays (including string and bytes) as indexed arguments, its Keccak-256 hash is stored as a topic instead, this is because a topic can only hold a single word (32 bytes).


// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.21 <0.9.0;

contract ClientReceipt {
    event Deposit(
        address indexed _from,
        bytes32 indexed _id,
        uint _value

    function deposit(bytes32 _id) public payable {
        // Events are emitted using `emit`, followed by
        // the name of the event and the arguments
        // (if any) in parentheses. Any such invocation
        // (even deeply nested) can be detected from
        // the JavaScript API by filtering for `Deposit`.
        emit Deposit(msg.sender, _id, msg.value);

on the frontend

var abi = /* abi as generated by the compiler */;
var ClientReceipt = web3.eth.contract(abi);
var clientReceipt = ClientReceipt.at("0x1234...ab67" /* address */);

var depositEvent = clientReceipt.Deposit();

// watch for changes
depositEvent.watch(function(error, result){
    // result contains non-indexed arguments and topics
    // given to the `Deposit` call.
    if (!error)

// Or pass a callback to start watching immediately
var depositEvent = clientReceipt.Deposit(function(error, result) {
    if (!error)

Output of the event:

   "returnValues": {
       "_from": "0x1111…FFFFCCCC",
       "_id": "0x50…sd5adb20",
       "_value": "0x420042"
   "raw": {
       "data": "0x7f…91385",
       "topics": ["0xfd4…b4ead7", "0x7f…1a91385"]


Leave a Reply

More great articles

A Guide to Inheritance in Solidity

Inheritance allows code to be reused by creating a new class from an existing class. The new class that is…

Read Story

Foundry Vs Hardhat

In this article, we will be comparing two smart contract development frameworks. Hardhat, which is the most used framework for…

Read Story

Smart Contract Security Roadmap

In 2021 $1.5 billion were hacked from smart contracts. That’s why smart contract security experts are in high demand and…

Read Story

Never miss a minute

Get great content to your inbox every week. No spam.
[contact-form-7 id="6" title="Footer CTA Subscribe Form"]