Enums in Solidity


Enums are one way to create a user-defined data type in Solidity. Enums increase the code readability.

Enums in solidity are one way to create a user-defined data type. Enums increase the code readability. Let’s understand them with an example.

You can declare an enum data type with the keyword enum followed by a custom data type name which is loanStatus in our case. The values( Applied, InProgress, Verified, Approved) in this enumerated list are called enums and are constant. 

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

contract Loan {
    enum loanStatus {Applied, Inprogress, Verified, Approved}
    loanStatus AnniLoanStatus;

    function applyLoan() public returns(uint, loanStatus) {
        AnniLoanStatus = loanStatus.Applied;
        return (block.timestamp, AnniLoanStatus);

    function IsLoanApplied() public view returns(bool) {
        require(AnniLoanStatus == loanStatus.Applied);
        return true;


They are used to provide names for integer constants which makes the contract easier to maintain and read. In our Loan contract above, we have created a simple enum in line number 5 that helps track the status of the loan application. This enum will be called ‘loanStatus’ and it will contain only the following values:

  • Applied (0)
  • InProgress (1)
  • Verified (2)
  • Approved (3)

Basically, Applied is set to constant value 0, Inprogress to 1, Verified to 2, and Approved to 3. Now imagine if you would have used normal variables to track the loan Application status you had to add a lot of documentation to explain ‘0’ means Applied, ‘1’ means Inprogress, etc. Having a defined list of 0-3 will reduce the chances of a bug in your code by eliminating the possibility of a 5, 10, 15 being passed in.

loanStatus acts as a data type just like string and unit. So, you can declare a variable “AnniLoanStatus” of type loanStatus. When Anni applies for the loan, AnniLoanStatus variable will be set to Applied which is constant number 0. Pay attention to the return type in applyLoan() function where the second return type is loanStatus.

Enums make our code much cleaner and increase the code readability. When IsLoanApplied() function is called you can simply compare the value of AnniLoanStatus variable against “loanStatus.Applied” to make the decision.

You can also convert enum custom datatype to unit and from unit to custom data type. Below is an example to do just that. newvar will have value 3 after conversion because Approved is the last value in the enum list. 

Similarly, you can convert an integer value to loanStatus but it cannot be more than 3 because we have defined only 4 values in our enum list from 0-3.

					uint newvar = uint(loanStatus.Approved);
loanStatus newvar1 = loanStatus(3);

What you can not do with an enum in Solidity

  • Implicit conversion is not allowed
  • Numbers or booleans can not be used as members of an enum
  • Enums can not be used as a key type in a mapping
  • You can not define enums in an interface if your Solidity compiler version is less than 0.5.0.

Leave a Reply



We connect you to a world of houseplants and urban gardening tailored to your home


Login to your account