Modifier with enums

When an enum variable is declared globally, its value can be verified using a modifier.

Syntax:

enum variablename {inst1, inst2, inst3, …}

variablename v;

modifier modifiername(variablename _v){

require(v==_v);

_;

}

Below is the solidity program to implement enums:

Solidity




//SPDX-License-Identifier:GPL-3.0
pragma solidity >=0.4.22 <0.7.0;
contract modifierWithEnum {
  enum Status{Fresher, Trained, Experienced}
    struct employee
  {
    uint emp_id;
    string emp_name;
    Status s;
   }
   Status s1;
       modifier isValid(Status _entry)
    {
      require(s1==_entry);
      _;
    }
    
  employee e;
 function enterDetails (uint _eno, string memory _ename, Status _s) public isValid(_s)  {
   e.emp_id=_eno;
   e.emp_name=_ename;
   e.s=_s;
 }
}


Output:

Modifier with enum

Modifier code examples

This section focuses on discussing a few applications of modifiers:

1. Data Validation: In this example, the input data is validated based on its type.

modifier triggerIfEqualToZero(uint _num)
{
   if(_num == 0) throw;
   _;
}

Similarly, the other type of data can also be verified.

2. Refund Ether send by accident: For every transaction made in the blockchain, some amount of Ether needs to be paid. Also, a facility is there for transferring some amount of Ether to other users. Sometimes, accidentally few transactions may take place. 

modifier refundEther {
   if(msg.value >0) throw;
   _;
}

3. Charge a fee: Modifiers can also be used for verifying whether the user has paid the required fees as shown below:

modifier payFee (uint _fee) {
  if(msg.value >= _fee) throw;
  _;

}

4. Send the change back: Suppose a user wants to send back the extra amount paid by a person, modifiers can be applied to it. The implementation for the same is given below:

modifier sendBackChange(unit _balance)
{
    _;
    if (msg.value > _balance)   
   {
      msg.sender.transfer(msg.value – _balance)
   }
}



Solidity – Function Modifiers

Function behavior can be changed using function modifiers. Function modifier can be used to automatically check the condition prior to executing the function. These can be created for many different use cases. Function modifier can be executed before or after the function executes its code.

  • The modifiers can be used when there is a need to verify the condition automatically before executing a particular function.
  • If the given condition is not satisfied, then the function will not get executed.

How to Create and Use Modifiers?

Function modifiers are created by defining them and invoking the same in the required function. 

Syntax:

modifier  modifier_name 
{
   // action to be taken
}

There are two variations of a function modifier: 

1. Function modifier with an argument:

modifier  modifier_name(unit arg)
{
   // action to be taken
}

2. Function modifier without argument:

modifier  modifier_name()
{
   // action to be taken
}

If the modifier does not have an argument then parentheses () can be omitted.

What is Merge Wildcard?

Consider the below function modifier:

modifier isAdmin
{
   require(msg.sender == admin);
   _;
}

The _; symbol is known as Merge Wildcard and this is replaced by the function definition during execution. 

  • In other words, after this wildcard has been used, the control is moved to the location where the appropriate function definition is located. 
  • This symbol is mandatory for all modifiers.
  • The modifier may contain this wildcard anywhere. 
  • When the wildcard is placed at the end of the modifier, the condition is verified and the appropriate function is executed if it is satisfied. 
  • When it is placed at the beginning, the appropriate function is executed first followed by the condition verification.

Similar Reads

Modifiers without Argument

A modifier may not have arguments. If the modifier does not have an argument then parentheses () can be omitted....

Modifier with Arguments

...

Multiple Modifiers to Function

Modifiers may accept an argument and it is included after the modifier name within parentheses ()....

Modifier Overriding

...

Modifier with enums

Multiple modifiers may be present in a function, and each of these conditions must be met in order for the function to be successfully executed.  To verify whether only the administrator with 5 years of experience is editing, two modifiers namely isAdmin and isExperienced are introduced. The function enterDetails will execute only if the user has administrator authorization and has a minimum of 5 years of experience....

Contact Us