Enums and Events in Solidity
Tags: web3, smartcontract, blockchain, cryptocurrency
Published: 2023-12-28 16:00:16

<p data-pm-slice="1 1 []">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.</p>
In our example <a class="attrlink" href="https://github.com/Scofield-Idehen/Smart_Lottery/blob/master/src/Raffle.sol" target="_blank" rel="noreferrer nofollow noopener">Raffle contract,</a> there is one enum defined called RaffleState with two possible values - OPEN and CALCULATING:

<img src="https://paper-attachments.dropboxusercontent.com/s_F5C166EA0FF5EC65D3394F1439D869EFC04F06F878A07B2B2B68520F396C17A5_1703776869318_image.png" align="left" />

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: <a class="attrlink" href="https://blog.learnhub.africa/2023/12/28/web3-scams-targeting-your-crypto-in-2024/" target="_blank" rel="noreferrer nofollow noopener">Web3 Scams Targeting Your Crypto in 2024</a> if you want to learn about the top scams facing web3 today.

<strong>Some key points about Enums:</strong>
<ul>
 	<li>Enums restrict variables to one of a predefined set of values, improving code clarity and preventing invalid values.</li>
</ul>
<ul>
 	<li>They are useful for representing states, status values, choices, options, etc.</li>
</ul>
<ul>
 	<li>Enum values are indexed internally, starting from 0. So, in the RaffleState example, <code>OPEN = 0</code> and <code>CALCULATING = 1</code>.</li>
</ul>
<ul>
 	<li>Enum types cannot have methods or properties. They simply represent a set of constants.</li>
</ul>
<img src="https://paper-attachments.dropboxusercontent.com/s_F5C166EA0FF5EC65D3394F1439D869EFC04F06F878A07B2B2B68520F396C17A5_1703776923853_image.png" align="left" />

The enum value must be referenced using the enum name like <a href="https://github.com/Scofield-Idehen/Smart_Lottery/blob/master/src/Raffle.sol" target="_blank" rel="noopener noreferrer nofollow"><code>Status.Open</code></a>.

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

<code>RaffleState private s_rafflestate;  </code>

In functions, it checks or sets the state:

<img src="https://paper-attachments.dropboxusercontent.com/s_F5C166EA0FF5EC65D3394F1439D869EFC04F06F878A07B2B2B68520F396C17A5_1703776985044_image.png" align="left" />

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:

<img src="https://paper-attachments.dropboxusercontent.com/s_F5C166EA0FF5EC65D3394F1439D869EFC04F06F878A07B2B2B68520F396C17A5_1703777093582_image.png" align="left" />

This allows a user to pick one of the predefined color options. Enums are a useful way to restrict choices.
<h2>Events</h2>
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.

<strong>Some key points about events:</strong>
<ul>
 	<li>Emit log information that external apps can detect and use</li>
 	<li>Emitted with the emit keyword</li>
 	<li>Don't cost gas like state changes, only logged externally</li>
 	<li>Can store data related to the specific event</li>
</ul>
For example, an external website could listen to certain events to update a database or inform users.

<img src="https://paper-attachments.dropboxusercontent.com/s_F5C166EA0FF5EC65D3394F1439D869EFC04F06F878A07B2B2B68520F396C17A5_1703777148637_image.png" align="left" />

Our Raffle contract defines two events - RafflesEntered and PickedWinner:

<img src="https://paper-attachments.dropboxusercontent.com/s_F5C166EA0FF5EC65D3394F1439D869EFC04F06F878A07B2B2B68520F396C17A5_1703778176849_image.png" align="left" />

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:

<img src="https://paper-attachments.dropboxusercontent.com/s_F5C166EA0FF5EC65D3394F1439D869EFC04F06F878A07B2B2B68520F396C17A5_1703778235288_image.png" align="left" />

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:
<ul>
 	<li>Logging deposits/withdrawals from a bank contract</li>
 	<li>Indicating tokens have been minted or transferred</li>
 	<li>Signaling a game move or important action</li>
 	<li>Tracking votes in a voting contract</li>
</ul>
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.
<h2>Frequently Asked Question</h2>
<strong>Can enums have associated data or properties?</strong>

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

<strong>Are events accessible from within the smart contract code?</strong>

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

<strong>Do enum names have to be capitalized? </strong>

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.

<strong>What is the indexed keyword for in-event declarations?</strong>

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

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

A: Yes, for example:
<pre><code class="language-solidity">enum Status { Open, Closed }  
Status public currentStatus = Status.Open;</code></pre>
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 <a class="attrlink" href="https://blog.learnhub.africa/" target="_blank" rel="noreferrer nofollow noopener">Learnhub Blog</a>; we write a lot of tech-related topics from <a class="attrlink" href="https://blog.learnhub.africa/category/cloud-computing/" target="_blank" rel="noreferrer nofollow noopener">Cloud computing</a> to <a class="attrlink" href="https://blog.learnhub.africa/category/frontend/" target="_blank" rel="noreferrer nofollow noopener">Frontend Dev</a>, <a class="attrlink" href="https://blog.learnhub.africa/category/security/" target="_blank" rel="noreferrer nofollow noopener">Cybersecurity</a>, <a class="attrlink" href="https://blog.learnhub.africa/category/data-science/" target="_blank" rel="noreferrer nofollow noopener">AI</a>, and <a class="attrlink" href="https://blog.learnhub.africa/category/blockchain/" target="_blank" rel="noreferrer nofollow noopener">Blockchain</a>. Take a look at <a class="attrlink" href="https://blog.learnhub.africa/2023/02/16/how-to-build-offline-web-applications/" target="_blank" rel="noreferrer nofollow noopener">How to Build Offline Web Applications. </a>
<h2>Resource</h2>
<ul>
 	<li><a class="attrlink" href="https://blog.learnhub.africa/2023/12/19/getting-started-with-foundry-for-beginners-in-2024/" target="_blank" rel="noreferrer nofollow noopener">Getting Started with Foundry for Beginners in 2024</a></li>
 	<li><a class="attrlink" href="https://blog.learnhub.africa/2023/09/25/what-are-smart-contracts/" target="_blank" rel="noreferrer nofollow noopener">What are Smart Contracts?</a></li>
 	<li><a class="attrlink" href="https://blog.learnhub.africa/2023/12/27/create-a-raffle-draw-smart-contract-using-foundry-2024-part-1/" target="_blank" rel="noreferrer nofollow noopener">Create a Raffle Draw Smart Contract Using Foundry 2024 (Part 1)</a></li>
</ul>
<a class="attrlink" href="https://www.buymeacoffee.com/Scofield" target="_blank" rel="noreferrer nofollow noopener">Gift us a cup of coffee if you are so kind.  </a>