James Bachini

Enums In Solidity

enums Solidity

Enums are one of the most useful and underused data types in Solidity. I think because they aren’t widely used in Javascript and web dev, they often get overlooked in contract development.

In this article I’ll make my case for why enums deserve a place in your tool kit and smart contracts.

Enums in Solidity are a way to create user defined types with a set of predefined values. They are useful for representing states or a set of options.

They can be really helpful in making the code more readable and understandable in 6 months time. Say for example you have a order processing system it’s much more expressive to write if (orderStatus = myEnum.Delivered) than it is to write if (orderStatus = 3)

Here is an example to demonstrate using enums to describe days of the week: (code for this is in the Solidity Snippets github repo)

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Enums {
    enum Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }

    function getNextDay(Day _currentDay) public pure returns (Day) {
        if (_currentDay == Day.Sunday) return Day.Monday;
        return Day(uint(_currentDay) + 1);
    }
}

The Day enum contains values for each day, starting from Monday to Sunday. The contract includes a function getNextDay() that takes a Day enum value as an input, representing the current day, and returns the next day.

If the current day is Sunday, the function returns Monday to complete the weekly cycle. Otherwise, it returns the next day by incrementing the current day’s enum value using typecasting (uint(_currentDay) + 1). This ensures that the days are looped through sequentially within the week.

This contract is deployed to Sepolia testnet if you want to experiment: https://sepolia.etherscan.io/address/0x252B5152F49Bc93BaEB0B14503dCEF6457A4694B#readContract

Enums in Solidity

Here are some obvious use cases for enums in Solidity:

  • Workflow States Track contract stages like Ordered, Shipped, Delivered.
  • Access Control Define user roles such as Admin, Editor, Viewer.
  • Voting Systems Represent voting options like Yes, No, Abstain.
  • Game States Manage game phases like NotStarted, InPlay, GameOver.
  • Days/Months Schedule events using enums like Monday, Tuesday, or January, February.
  • Auction Phases Control auction flow with states like NotStarted, Active, Ended.
  • Transaction Status Track transaction states like Pending, Completed, Failed.

I hope this has convinced you of the vast and glorious benefits of using Enums in your Solidity code 💖


Get The Blockchain Sector Newsletter, binge the YouTube channel and connect with me on Twitter

The Blockchain Sector newsletter goes out a few times a month when there is breaking news or interesting developments to discuss. All the content I produce is free, if you’d like to help please share this content on social media.

Thank you.

James Bachini

Disclaimer: Not a financial advisor, not financial advice. The content I create is to document my journey and for educational and entertainment purposes only. It is not under any circumstances investment advice. I am not an investment or trading professional and am learning myself while still making plenty of mistakes along the way. Any code published is experimental and not production ready to be used for financial transactions. Do your own research and do not play with funds you do not want to lose.