Function modifiers in Solidity smart contracts

Function modifiers are used to change or restrict the behavior of a function in a smart contract. You can use a modifier to automatically check a condition prior to executing the function. For example before executing a function you can check the balance of an account, verify the sender is the owner, require access to an account, etc. Modifiers are reusable blocks of code that can be attached to one or many function in your contract.

As an example the picture below details the basic structure of a Solidity smart contract with a function modifier. The modifier is referenced in the first and third function to illustrates the reusability of modifiers in your code.

Function modifiers in Solidity smart contracts

Function modifiers can be created for many different use cases. These modifiers can be executed before or after the function executes its code.

_;” is a special code only used in the function modifier. It instructs solidity to run the code in the function. It’s a merge wildcard that indicates where “the rest” of the function code should go by merging the function source with the modifier code at the “_;” wild card. In simpler terms you can:

  • execute the function modifier and then the function

or

  • execute the function and then the function modifier

Basic structure / syntax of a function modifier:

Basic syntax Function modifiers in Solidity smart contracts

Solidity Function Modifier example

One example is the use of a modifier to check if the caller of the function is the owner of the contract. This is a very common use case to restrict access to a function. You can write a simple modifier that indicates only the owner of the contract can do something. First you need to write the function modifier then you need to associate it to a function.

In the example below, the contract does not use the modifier, only defines it

//the modifier "onlyOwner" can be used in a function
//if it is called it will evaluate if the entity executing the function is the owner of the contract
//If not the function will not run and "Not Owner" will print
//if it is the owner then the function will execute
modifier onlyOwner() {
         require(msg.sender == owner, “Not Owner’);
         _;

In the example below, the contract defines the function modifier onlyOwner and uses it in a function. If the caller of the function is not the owner then the function does not execute. The message “Not Owner” is printed to the log. Try this in Remix

//define which compiler to use
pragma solidity ^0.7.0;

//contract name is MyFirstContract
contract MyFirstContract {

//create two variables.  A sting and an address

    address owner;
    string private name;

//constructor sets the creator of the contract to the owner variable
   constructor() {
      owner = msg.sender;
   }
    

//modifier checks that the caller of the function is the owner
    modifier onlyOwner() {
         require(msg.sender == owner, 'Not Owner');
         _;
    }

//set name.  Only the owner of the contract can call because a modifier is specified
    function setName(string memory newName) public onlyOwner{
        name = newName;
    }

//get the name
    function getName () public view returns (string memory) {
        return name;
    }
    

}

Try it in Remix

Function modifiers can also have parameters past to them to enhance their capabilities. In the example below the modifier uses the address passed in a function as a parameter. It then checks to determine if the address is valid and if it is not valid then prints an error message in the log. The important thing to note is that function modifiers can evaluate function parameters.

Example with parameter:

//this modifier can be reused and called in a function
//this modifier allows the address parameter to be passed to it
Modifier validAddress(address _addr) {
         Require(_addr != address(0), “Not a valid address”);
         _;

For more information read the docs

This code is for learning and entertainment purposes only. The code has not been audited and use at your own risk. Remember smart contracts are experimental and could contain bugs.

Click here for more information about how to use the Ethereum test network and how to obtain test ETH.

Next Review – Gas in Solidity smart contracts

Leave a Reply