【问题标题】:How to use conditionals with modifiers?如何使用带修饰符的条件?
【发布时间】:2022-05-28 19:39:05
【问题描述】:

我需要完成此任务以使该功能仅在修饰符(即应该是正确的)如图所示工作。基本上 compPurch 必须始终为真,而 realBuyer 或 timeBought 也必须为真。

    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

【问题讨论】:

    标签: blockchain ethereum solidity remix evm


    【解决方案1】:

    多个修饰符始终与 AND 运算符连接。

    所以你可以对第一个条件使用一个修饰符,对第二个条件使用一个修饰符。第二个条件中的子条件需要在同一个修饰符中。

    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 {
    }
    

    【讨论】:

      猜你喜欢
      • 2017-03-13
      • 1970-01-01
      • 2021-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-13
      • 1970-01-01
      • 2013-11-30
      相关资源
      最近更新 更多