0

I have a modifier for a few functions that check if a mapping exists, for one function I need the modifier to verify the mapping doesn't exists, is there any way to !modifier as you would a function you wanted a negative response for?

4

You cannot directly negate the result of a modifier, but an alternative way to do this would be to create a function and call that inside two different modifiers, if you want to avoid code duplication. For example:

contract Foo {
  function checkSomething () private pure returns (bool) {
    // Do something here
    return true;
  }
  
  modifier isSomething () {
    require(checkSomething());
    _;
  }
  
  modifier isNotSomething () {
    require(!checkSomething());
    _;
  }
}
3

I voted for Morton's answer. This is just to amplify with some similar patterns to consider. If the modifier is really complicated and you want to avoid repetition, move the logic to a function and then call the function from the modifiers.

modifier things {
  require(isThing());
  _;
}

modifier nothings {
  require(!isThing());
  _;
}

function isThing() ... returns(bool) {
  // is it ?
}

Or, you could pass an arg into the modifier.

modifier isOrNot(bool wantIt) {
  require(something == wantIt);
  _;
}

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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