blockchainethereumsolidityremixevm

How to use conditionals with modifiers?


I need to complete this task to let the function work only if the modifiers (that should be correct) work as shown in the image. Basically compPurch has always to be true and also realBuyer OR timeBought have to be true.

    modifier compPurch() {
        require(state == State.Locked, "it's not locked");
        _;
        time = block.timestamp;
    }

    modifier realBuyer() {
        require(msg.sender == buyer, "you're not the buyer");
        _;
    }

    modifier timeBought() {
        require(block.timestamp >= time + 5, "wait 5 mins fro purchase");
        _;
    }
}

I created all modifiers, but I don't know how to use AND & OR conditionals to make them work as intended in the task


Solution

  • Multiple modifiers are always joined with the AND operator.

    So you can use one modifier for the 1st condition, and one modifier for the 2nd condition. The subconditions within the 2nd condition need to be in the same modifier.

    modifier compPurch() {
        // ...
    }
    
    modifier realBuyerOrTimeBought() {
        // explicit OR in the condition
        require(msg.sender == buyer || block.timestamp >= time + 5);
        // ...
    }
    
    // implicitly joined with the AND operator
    function foo() public compPurch realBuyerOrTimeBought {
    }