0
contract MyIfElse {
    uint256 public revealState = 0;
    bool public isNowRevealed = false;

    function addToRevealState () private {
        revealState++;
    }

    function isRevealed () public {
        if (revealState >= 10){
            isNowRevealed = true;
            addToRevealState();
        }else{
            addToRevealState();
        }
    }
}

When the revealState() is called, addToRevealstate() gets triggered as intended because the if else statement hasn't been met.

The problems occurs when the revealState = 10. The isNowReveal bool does not update the state to true. Only after revealState = 11 will the state be updated to true.

Is there an indexing issue I'm not taking into account?

5
  • Are you saying that when revealState is 10, calling isRevealed does not set isNowRevealed to true? Apr 2, 2022 at 19:10
  • Correct. I have to call it again, which means realState is now 11 before isNowRevealed turns true.
    – sltdev
    Apr 2, 2022 at 19:42
  • 1
    Hmmm, I think you're mixing up the ordering here. revealState will turn 11 when isNowRevealed turns true, but it is 10 when the transaction that flips isNowRevealed to true begins! So you have a state like this, where each new state is the result of a call to isRevealed (0, false) -> ... -> (9, false) -> (10, false) -> (11, true). The true only shows up when revealState is 11 because you always increment revealState after setting isNowRevealed to true. But the flip happens when revealState is 10. Apr 2, 2022 at 20:19
  • 1
    Brilliant. Thanks mate. As you probably guess I'm rather new. Thank you so much for taking some time to help me. So sometimes is a simple ordering problem.
    – sltdev
    Apr 2, 2022 at 20:38
  • 1
    No worries! This behaviour definitely can feel confusing sometimes! I'll throw my comment in an answer so that this question doesn't show up as "unanswered" :) Apr 3, 2022 at 21:02

1 Answer 1

0

I think you're mixing up the ordering here. revealState will turn 11 when isNowRevealed turns true, but it is 10 when the transaction that flips isNowRevealed to true begins!

So you have a state like this, where each new state is the result of a call to isRevealed:

(0, false) -> ... -> (9, false) -> (10, false) -> (11, true)

The true only shows up when revealState is 11 because you always increment revealState after setting isNowRevealed to true. But the flip happens when revealState is 10.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.