enum Status{ Vacant, Occupied }
Status currentStatus;
event Occupy(address _occupant, uint _value);

- 12,078
- 3
- 26
- 50
1 Answers
As in some other programming languages, enum
(docs) allows you to aggregate multiple values to one datatype, where only one of the values is active.
In your case, the currentStatus
can be Vacant
(integer value 0), or it can be Occupied
(integer value 1). But never can be "none of these", nor "both", nor "anything else".
pragma solidity ^0.8;
contract MyContract {
enum Status {
Vacant,
Occupied
}
Status currentStatus;
// you can effectively pass `0` or `1` integer value here
function setCurrentStatus(Status _currentStatus) external {
currentStatus = _currentStatus;
}
function getCurrentStatusVerbose() external view returns (string memory) {
if (currentStatus == Status.Vacant) {
return "The current status is Vacant";
} else if (currentStatus == Status.Occupied) {
return "The current status is Occupied";
}
}
}
Events (docs) are readable by off-chain apps, not not readable by other contracts. Usually, the off-chain app listens to these events being emitted to perform an action on its end.
pragma solidity ^0.8;
contract MyContract {
event Occupy(address _occupant, uint _value);
enum Status {
Vacant,
Occupied
}
Status currentStatus;
function setCurrentStatus(Status _currentStatus) external {
currentStatus = _currentStatus;
// emitting the `Occupy` event
emit Occupy(msg.sender, uint(_currentStatus));
}
}
Off-chain app:
myContract.on('Occupy', async (event) => {
updateOccupancyInExternalDB(event);
});
A real-life example is the Transfer()
event defined the ERC-20 token standard. When a token contract emits this event, it means that a token transfer occured. Blockchain explorers (such as Etherscan) listen to these events and update the token balance info in their own databases (subtract from the sender balance and increase the receiver balance in their own DB).

- 40,554
- 8
- 72
- 100