[Ethereum] how to use multiple modofier to form combinations of boolean conditions

modifierssolidity

I have read this article and this SO. And I understand the functionality of modifier. When modifiers are placed on a function with a spacing they will be evaluated in the order of placing thus creating an And condition. However, I would to understand how multiple modifiers can be placed so that combination of other Boolean conditions such as OR be evaluated?

For Instance,

modifier modA {
  // verify something
  _;
  // verify something
  _;
}
modifier modB {
  // verify something
  _;
  // verify something
  _;
}
function Fun() modA modB {
  // Do something
}

In this case, modA AND modB will be the execution. But is it possible to use them to form OR condition?

Best Answer

There is no direct way in solidity. I'll explain you why. Modifier is like a macro function while compiling it will replace your modifier with its actuval implementation. Here _; replace with your function implementation.

contract ModiferTesting {
   uint public a;
   uint public b;
   uint public c;

   modifier modA() {
    a = a + 1;
    _;
   }

   modifier modB() {
    b = b + 1;
    _;
    b = b + 1;
    _;
    b = b + 1;
    _;
  }

  function test() public modA modB {
    c = c + 1;
  }
}

If you debug, above code you can find a=1, b=3, c=3

Try to write an other modifier that check's or condition.

Related Topic