Enum wrong value

Viewed 152

I'm new to solidity and I'm working through a course and I've set myself a project but I can't work out what I'm doing wrong as the data I'm logging is showing me the values are what's expected based on the enum positions.

I'm using compiler version ^0.6.6

I first set up my enum

enum VOTE_STATUS {CLOSED,OPEN}
VOTE_STATUS public voting_status = VOTE_STATUS.CLOSED;

I then have a voting function which I'm calling require in.

 function addVote(uint _vote) public{
        // Make sure voting is open
        require(voting_status == VOTE_STATUS.CLOSED,"Voting hasn't opened yet");
   }

In remix when I click the voting_status button on the left-hand side it returns the correct option, 0 = CLOSED, 1 = OPEN.

The issue is when I run the require statement above it seems to think the enum is always set to OPEN when the logging and solidity say differently.

I've confirmed this by setting the require statement to check for OPEN when the status is closed and this gives me the result I'm looking for

voting_status = VOTE_STATUS.CLOSED;
require(voting_status == VOTE_STATUS.OPEN,"Voting hasn't opened yet");

Any help with this would be massively appreciated Thanks

1 Answers

I think that you may have a conceptual misunderstanding. A require statement works as follows:

require('condition', 'error msg');

What the require statement does is to check if the condition is met. If the condition is NOT met, it reverts the transaction and returns the error msg. So what you want to do is a require, which checks the voting_status to be OPEN, and if it is not, then it will throw the error "Voting hasn't opened yet".

So just to be clearer, your current require statement:

require(voting_status == VOTE_STATUS.CLOSED,"Voting hasn't opened yet");

Is actually checking if the voting_status is set to CLOSED. And if it is not, then it throws the msg error "Voting hasn't opened yet".

So maybe what you are looking for is:

require(voting_status == VOTE_STATUS.OPEN,"Voting hasn't opened yet");

Hope you find this information useful :)

Related