Skip to content

Latest commit

 

History

History
66 lines (49 loc) · 1.8 KB

File metadata and controls

66 lines (49 loc) · 1.8 KB

30 Days Of Solidity: Function Modifiers

Twitter Follow

Author: Vedant Chainani
June, 2022

<< Day 18 | Day 20 >>

Cover


📔 Day 19

Modifiers assist in the execution of a function’s behavior. The behavior of a function can be changed using a function modifier, they can also be called before or after a function is executed.

Solidity function modifiers help in the following:

  • To access restrictions
  • Input accuracy checks
  • Hacks protection

Example:

contract Owner {
   modifier onlyOwner {
      require(msg.sender == owner);
      _;
   }
}

The function body is inserted where the special symbol "_;" appears in the definition of a modifier. So if condition of modifier is satisfied while calling this function, the function is executed and otherwise, an exception is thrown.

Example:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

contract Mint {
    constructor()  {
        owner = msg.sender;
    }
    modifier onlyOwner {
      require(msg.sender == owner);
      _;

    function _mint(address to , uint tokenId) onlyOwner {
      mint(to, tokenId);
    }
}

<< Day 18 | Day 20 >>