Enum Types - Solidity Part 2.7

When diving into Solidity, one of the key features you’ll encounter is the use of enum types. Enums can be a powerful tool in your smart contract development arsenal, allowing you to define and work with a set of named values, which makes your code more readable and maintainable. Let’s explore what enums are, how to use them, and some practical examples.

Source Code: https://github.com/scaihai/enkwadore-blog-blockchain-demos/tree/main/solidity/contracts/2.7

What are Enums?

Enums, short for “enumerations,” are a user-defined data type in Solidity. They allow you to create a set of named constants that can be assigned to a variable. Enums are particularly useful for representing a collection of related values that don’t change, like states in a state machine.

Defining Enums

To define an enum in Solidity, you use the enum keyword followed by the name of the enum and a list of its possible values enclosed in curly braces.

Shipping.sol

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

contract Shipping {
    enum Status { Pending, Shipped, Delivered, Canceled }

    Status public status;
}

Here’s an example:

In this example, we’ve defined an enum called Status with four possible values: Pending, Shipped, Delivered, and Canceled.

Using Enums

Enums can be used as the type for state variables, function parameters, and local variables. Here’s how you can interact with enums:

Shipping2.sol

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

contract Shipping2 {
    enum Status { Pending, Shipped, Delivered, Canceled }

    Status public status;

    function setStatus(Status _status) public {
        status = _status;
    }

    function getStatus() public view returns (Status) {
        return status;
    }

    function cancel() public {
        status = Status.Canceled;
    }
}

In this contract:

  • The status state variable holds the current status of the shipping process.
  • The setStatus function allows you to set the status by passing in one of the enum values.
  • The getStatus function returns the current status.
  • The cancel function sets the status to Canceled.

Enum Defaults and Indexing

Enums in Solidity start with an index of 0. If you don’t explicitly set an initial value, the first value in the enum definition (index 0) will be the default. In our Shipping example, if we don’t set a status, it will default to Pending.

Practical Example: A Voting System

Let’s look at a practical example of using enums in a voting system:

Voting.sol

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

contract Voting {
    enum Stage { Init, Reg, Vote, Done }
    Stage public stage = Stage.Init;

    function advanceStage() public {
        require(stage != Stage.Done, "Voting has ended");
        stage = Stage(uint(stage) + 1);
    }

    function getStage() public view returns (Stage) {
        return stage;
    }
}

In this contract:

  • We have an enum Stage with four values: Init, Reg, Vote, and Done.
  • The stage state variable holds the current stage of the voting process and is initialized to Init.
  • The advanceStage function advances the voting stage, ensuring it doesn’t proceed past Done.
  • The getStage function returns the current stage.

Benefits of Using Enums

  1. Readability: Enums make your code more readable by replacing numeric constants with meaningful names.
  2. Maintenance: It’s easier to manage and update a set of related constants.
  3. Safety: Enums reduce the risk of invalid values being assigned, as only the defined constants can be used.

Conclusion

Enums are a simple yet powerful feature in Solidity that can significantly improve the readability and maintainability of your smart contracts. By using enums, you can make your code more intuitive and less error-prone. So, next time you need to represent a set of related constants, consider using enums!

Share:
spacer