Enums are one way to create a user-defined type in Solidity.
Useful to model choice and keep track of state.
They are integers internally.
// 0-> Open state, 1 -> closed state, ...enumLOTTERY_STATE {OPEN, CLOSED, CALCULATING_WINNER}//init lottery_state as type: LOTTERY_STATE (is a class).LOTTERY_STATE public lottery_state; constructor(address_pricefeedaddress) public { ethusd_pricefeed =AggregatorV3Interface(_pricefeedaddress); lottery_state = LOTTERY_STATE.CLOSED; // can also: lottery_state = 1}// init outside connstructor, so its state variable. // then assign within constructor. // if you init + assign within constructor, it wont be state variable.// example usage:functionenter() publicpayable {//is lottery open?require(lottery_state == LOTTERY_STATE.OPEN,"Lottery is not open!");}
create an enum class "LOTTERY_STATE"
init variable lottery_state as type "LOTTERY_STATE"
assign lottery_state LOTTERY_STATE.CLOSED
// SPDX-License-Identifier: MITpragmasolidity ^0.8.10;contract Enum {// Enum representing shipping statusenumStatus { Pending, Shipped, Accepted, Rejected, Canceled }// Default value is the first element listed in// definition of the type, in this case "Pending" Status public status;// Returns uint// Pending - 0// Shipped - 1// Accepted - 2// Rejected - 3// Canceled - 4functionget() publicviewreturns (Status) {return status; }// Update status by passing uint into inputfunctionset(Status_status) public { status = _status; }// You can update to a specific enum like thisfunctioncancel() public { status = Status.Canceled; }// delete resets the enum to its first value, 0functionreset() public {delete status; }}
Declaring and importing Enum
Enums can be declared outside of a contract
File that the enum is declared in:
// SPDX-License-Identifier: MITpragmasolidity ^0.8.10;// This is saved 'EnumDeclaration.sol'enumStatus { Pending, Shipped, Accepted, Rejected, Canceled}
File that imports the enum above:
// SPDX-License-Identifier: MITpragmasolidity ^0.8.10;import"./EnumDeclaration.sol";contract Enum { Status public status;}