Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

347
Visualizações
Can Someone explain ENUM, EMIT, EVENT in solidity for me
 enum Status{ Vacant, Occupied }
    Status currentStatus;

 event Occupy(address _occupant, uint _value);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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).

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda