Solidity events for logging

x1xx1x
4 min read

What are Events in Solidity?

  • In solidity, events are used as a mechanism for logging important information on the blockchain. When a smart contract performs a transaction or triggers a specific action, events allow external consumers (such as user interfaces, off-chain applications, or other contracts) to listen for these changes. Events provide a way to store data that can be retrieved later, but they are different from state variables in that events do not alter the state of the blockchain directly.

Here are a few key points about events in Solidity:

  1. Event Declaration: Events are declared in the contract using the event keyword. For example:

     event Transfer(address indexed from, address indexed to, uint amount);
    
  • Indexed Parameters: Events can have up to 3 indexed parameters, allowing them to be more efficiently searched for. Indexed parameters are particularly useful for filtering events in transaction logs.
// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

contract IndexEvents {

    event NewTrade(
        uint256 indexed date,
        address from,
        address indexed to,
        uint256 indexed amount
    );

    function trade(address to, uint256 amount) external  {
        emit NewTrade(block.timestamp, msg.sender, to,amount);
    }
}

from above;

  • Event Definition:

    • The NewTrade event is defined with the following parameters:

      • date: The timestamp when the trade occurred (uint256 indexed date). The indexed keyword makes this parameter searchable in the logs, which is useful for filtering events by date.

      • from: The sender of the trade, represented by an address.

      • to: The recipient of the trade, represented by an address. This is also indexed, making it easy to search for events involving specific addresses.

      • amount: The amount being traded, represented by uint256.

    • The indexed keyword on date, from, and to allows for efficient searching and filtering of events by these fields on the blockchain.

  • Logging Mechanism: Events are logged on the blockchain, making it possible to query past events. However, they are more gas-efficient compared to using state variables because they don’t need to be stored on the blockchain's permanent state but rather in the transaction log.

  • Emitting Events: To trigger an event in Solidity, the emit keyword is used:

      emit Transfer(msg.sender, recipient, amount);
    
  • External Use: External applications or listeners (such as DApps) can listen for these events using web3.js, ethers.js, or similar libraries. They can use the events to react to blockchain changes and update the user interface accordingly.

Mastering Custom Errors in Solidity

Custom errors allow developers to define their own error types that can be thrown in smart contracts, providing more control and clarity over error handling. Before custom errors were introduced, Solidity only had the ability to throw built-in errors using the revert keyword with a string message or predefined error types like require and assert. With custom errors, developers can define structured error messages and pass relevant data to help identify the root cause of a failure.

Defining Custom Errors

In Solidity, custom errors are defined using the error keyword followed by a unique name, and can include specific details in parentheses. This improves the clarity and specificity of error messages in smart contracts.

error NumberTooHigh(uint256 from, uint256 number);

Or we can omit the parameters and define a custom error with them.

error Unauthorized();

Using Custom Error

The revert statement triggers custom errors, improving code readability and error diagnosis. Errors with parameters use revert followed by the error name and parameters, while errors without parameters use revert with just the error name, treating it like a function call.

We can use the' revert' keyword to utilize errors that don't require parameters. Then, we specify the desired custom error name and call it a regular function.

revert Unauthorized();

To use a custom error with parameters, we must pass the error name and parameters when reverting, similar to how we would pass them in a function.

revert NumberToLow(from, number);

Consuming custom errors

When utilizing a smart contract that has custom errors, it can provide users with more specific explanations of what went wrong. By utilizing libraries such as ethers.js or web3.js, developers have a more effective means of managing and presenting information to users.

Custom errors in Solidity can improve the transparency and effectiveness of smart contract code, providing more insightful feedback for developers and smart contract consumers, for instance, using a Web3 library. Adding them to your toolkit can be highly beneficial and reduce gas costs.

Debugging Solidity Using Remix

A great resource.

https://youtu.be/7z52hP26MFs

0
Subscribe to my newsletter

Read articles from x1x directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

x1x
x1x

Everyday learner