What does a modifier do in Solidity?
Asked Answered
F

4

6

Reading the docs it says to "add semantics to a function in a declarative ways"

Can I understand it as an "interface" in Java?

Figurative answered 8/7, 2017 at 4:18 Comment(0)
D
13

Modifiers let you wrap additional functionality to a method, so they're kind of like the decorator pattern in OOP.

Modifiers are typically used in smart contracts to make sure that certain conditions are met before proceeding to executing the rest of the body of code in the method.

For example, isOwner is often used to make sure that the caller of the method is the owner of the contract:

modifier isOwner() {
   if (msg.sender != owner) {
        throw;
    }

    _; // continue executing rest of method body
}

doSomething() isOwner {
  // will first check if caller is owner

  // code
}

You can also stack multiple modifiers to streamline your procedures:

enum State { Created, Locked, Inactive }

modifier isState(State _state) {
    require(state == _state);

    _; // run rest of code
}

modifier cleanUp() {
    _; // finish running rest of method body

    // clean up code
}

doSomething() isOwner isState(State.Created) cleanUp {
  // code
}

Modifiers express what actions are occurring in a declarative and readable manner.

Donnettedonni answered 10/7, 2017 at 5:23 Comment(0)
S
1

It's just a standard to secure that a certain property is met when somebody calls a particular function.

You can make your own modifier :)

Example 1:

   modifier owner() {
      if (msg.sender != owner) {
           throw;
       }

       _; // continue executing rest of method body

   }

   doSomething() isOwner {
     // will first check if caller is owner

     // code

   }

Example 2:

    uint256 thisNumber = 4;

    modifier HigherThan2(uint256 _x) {

      if (_x < 2) {
           throw;
       }

       _; // continue executing rest of method body
    }

    doMathStuff() HigherThan2(thisNumber) {

     // will check if thisNumber is higher than 2

     // then, more code

    }

On the other hand, you may use predefined modifiers, such as payable.

function ThisSuperICO() payable {
  require (msg.value > 0);

  // more code

}

Basically is a standard to handle conditions, throwing the call when these conditions are not met.

Hope this helps!

Cheers.

Smallsword answered 21/6, 2018 at 16:25 Comment(0)
O
0

Modifier is a piece of code which consist some validation kind of rules or logic defined by the developer. This piece of code is for the reusability purpose. It is pass in while defining the function in solidity, the purpose of it is to do some validation before running that respective function, now if the validation is failed then the respective function doesn't execute.

Analogy with node.js/express-framework:- Think of it as middleware function.

Analogy with html form validation function:- When submit is triggered with validation function call and if validation fails then the submit is aborted.

Outcross answered 21/6, 2018 at 14:15 Comment(0)
O
0

Similar to how we implement role based routes, modifiers are used to protect the functions. Caller can call the function if it passes some conditions.

Smart contracts have 24kb size limit. if its size is greater, you cannot compile the contract. modifiers allow us to reduce the contract size. Imagine you have a large contract and you have 10 protected functions. Implementing if condition inside each function would take up too much space.

Opulence answered 5/12, 2022 at 1:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.