Enums
An enum defines a type with a fixed list of named values. Enums are perfect for state machines, which are very common in contracts: an order is Created, then Paid, then Shipped; an auction is Open and later Closed.
enum Status { Pending, Active, Closed }Under the hood an enum is stored as a small unsigned integer (uint8 for up to 256 members). The first member is 0, the next 1, and so on. The first member is also the default value, so choose it carefully: it is the value every new variable starts with.
Using an enum
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Main {
enum Status { Pending, Active, Closed }
Status public status; // starts as Status.Pending (0)
function main() external {
console.log("initial:", uint256(status));
status = Status.Active;
console.log("after activate:", uint256(status));
console.log("is active?", status == Status.Active);
console.log("first member:", uint256(type(Status).min));
console.log("last member :", uint256(type(Status).max));
}
}initial: 0
after activate: 1
is active? true
first member: 0
last member : 2
console.log cannot print an enum directly, so convert it with uint256(...). When a contract's ABI exposes an enum, it appears as a uint8.
A state machine
Enums shine when combined with checks: each function only works in the right state, and moves the contract to the next one.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Delivery {
enum Stage { Ordered, Shipped, Delivered }
Stage public stage;
function ship() external {
require(stage == Stage.Ordered, "not in Ordered stage");
stage = Stage.Shipped;
}
function confirm() external {
require(stage == Stage.Shipped, "not shipped yet");
stage = Stage.Delivered;
}
function stageName() external view returns (string memory) {
if (stage == Stage.Ordered) return "Ordered";
if (stage == Stage.Shipped) return "Shipped";
return "Delivered";
}
}
contract Main {
function main() external {
Delivery parcel = new Delivery();
console.log(parcel.stageName());
parcel.ship();
console.log(parcel.stageName());
parcel.confirm();
console.log(parcel.stageName());
parcel.ship(); // invalid: already delivered
}
}Ordered
Shipped
Delivered
main() failed: reverted with reason: not in Ordered stage
require(condition, "message") stops the call with that message when the condition is false. You'll study it properly in the Errors lesson.
Converting integers to enums
You can convert an integer to an enum explicitly, for example when it comes from user input. If the number is outside the enum's range, the conversion reverts with a panic (code 0x21), so invalid states can never be created this way:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Main {
enum Size { Small, Medium, Large }
function toSize(uint256 n) public pure returns (Size) {
return Size(n);
}
function main() external pure {
console.log("Size(2) ->", uint256(toSize(2)));
console.log("Size(3) -> ...");
toSize(3);
}
}Size(2) -> 2
Size(3) -> ...
main() failed: panic 0x21 (invalid enum value)
Enums in other contracts
An enum declared inside a contract is referenced from outside with the contract's name as prefix, as in Delivery.Stage.Shipped. Enums (and structs) can also be declared at file level, outside any contract, so several contracts can share them:
enum Vote { Abstain, Yes, No }
contract Ballot {
mapping(address => Vote) public votes;
}Tips
- Put a "nothing yet" member first (
None,Pending), because that is the default. - Don't reorder members of a deployed contract's enum in an upgrade: stored values are numbers, so their meaning would change.
- Prefer enums over magic numbers or several booleans:
Status.Closedis self-documenting, and impossible combinations (open and closed) can't happen.
Test yourself
What is the default value of a variable of type enum Color { Red, Green, Blue }?
Color.BlueColor.Red- It must be initialized
null
What happens when you convert uint256(5) to an enum with three members?
- It wraps around to member 2
- It becomes the first member
- The call reverts with a panic
- It compiles to an invalid value
How is an enum represented in a contract's ABI?
- As a string
- As
uint8 - As
bytes32 - Enums cannot be used in external functions