DEV Community

Cover image for Enums and Events in Solidity
Scofield Idehen
Scofield Idehen

Posted on • Originally published at blog.learnhub.africa

Enums and Events in Solidity

Enums or Enumerations in Solidity allow developers to create a new user-defined type with a restricted set of possible values. This can make the code more readable and restrict variables to only valid values.

In our example Raffle contract, there is one enum defined called RaffleState with two possible values - OPEN and CALCULATING:

This RaffleState enum represents the current state of the raffle. A raffle can only be in one of two states - it is either open allowing people to enter, or it is calculating the winner.

By using an enum, we restrict the raffle state to only one of these two values rather than using a uint or other type that could have invalid values.

Read: Web3 Scams Targeting Your Crypto in 2024 if you want to learn about the top scams facing web3 today.

Some key points about Enums:

  • Enums restrict variables to one of a predefined set of values, improving code clarity and preventing invalid values.
  • They are useful for representing states, status values, choices, options, etc.
  • Enum values are indexed internally, starting from 0. So, in the RaffleState example, OPEN = 0 and CALCULATING = 1.
  • Enum types cannot have methods or properties. They simply represent a set of constants.

The enum value must be referenced using the enum name like Status.Open.

In the Raffle contract, the RaffleState enum is used for the s_raffleState variable to keep track of the current state:

RaffleState private s_rafflestate;  

In functions, it checks or sets the state:

This makes the code easy to read and understand that the raffle state must be one of two values. Using enums to represent the state in this way is a common Solidity pattern.

Enums can also be used to represent choices or options for users. For example:

This allows a user to pick one of the predefined color options. Enums are a useful way to restrict choices.

Events

Events in Solidity provide a way for smart contracts to communicate that something happened on-chain to external listeners.

They emit log information when called that external applications can detect and react to. This allows signaling to the outside world that certain actions have occurred in the smart contract.

Some key points about events:

  • Emit log information that external apps can detect and use
  • Emitted with the emit keyword
  • Don't cost gas like state changes, only logged externally
  • Can store data related to the specific event

For example, an external website could listen to certain events to update a database or inform users.

Our Raffle contract defines two events - RafflesEntered and PickedWinner:

These events signal that someone has entered the raffle or picked a winner. The indexed keyword allows these logged events to be searchable by the address passed in.

These events are emitted from corresponding functions:

When someone enters the raffle, the RafflesEntered event is emitted with their address. When a winner is picked, the PickedWinner event signals who won.

An external raffle website could listen to these events to update the user interface and database in real-time as they occur on-chain. This communication is enabled through events.

Events, therefore, allow efficient signaling of on-chain activity to off-chain listeners. Our Raffle contract uses events to indicate important state changes that external parties may want to be notified of and track.

An external application could listen for these Raffle events to track raffle entrants and winners in their off-chain database. Events allow efficient signaling of on-chain activity.

Other common use cases for events include:

  • Logging deposits/withdrawals from a bank contract
  • Indicating tokens have been minted or transferred
  • Signaling a game move or important action
  • Tracking votes in a voting contract

Events provide cheap, indexed, log data signaling from smart contracts. This can be incredibly useful for external dapps and services to sync and track on-chain activity.

Frequently Asked Question

Can enums have associated data or properties?

A: No, enums just define constants. To associate data with an enum value, consider creating a struct.

Are events accessible from within the smart contract code?

A: No, events are only logged externally. The internal code cannot read them.

Do enum names have to be capitalized? 

A: It is a convention in Solidity for capitalizing enum names to distinguish them from other variables and functions. But technically, names can be lowercase as well.

What is the indexed keyword for in-event declarations?

A: Indexed makes those event parameters searchable and filterable by the indexed values. It uses a special data structure suitable for search/lookup.

Can I declare an enum variable to equal a specific enum value all in one line?

A: Yes, for example:

enum Status { Open, Closed }  
Status public currentStatus = Status.Open;

So, in summary, Enums restrict values and improve code clarity, while Events signal external listeners that important actions have occurred on-chain. Both are very useful tools in Solidity.

If you find this article thrilling, discover extra thrilling posts like this on Learnhub Blog; we write a lot of tech-related topics from Cloud computing to Frontend Dev, Cybersecurity, AI, and Blockchain. Take a look at How to Build Offline Web Applications. 

Resource

Gift us a cup of coffee if you are so kind.  

Top comments (0)