Solidity Steps
  • Learning Solidity
  • Step 1
    • 1: Introduction
    • 2: Data Types
    • 3: Functions
    • 4: Control Structures
    • 5: State Variable
    • 6: Local Variables
    • 7: Global Variables
    • 8: View Keyword
    • 9: Pure Keyword
  • STEP 2
    • 10: Immunable Keyword
    • 11: Events
    • 12: Condition
    • 13: While Loop
    • 14: Do While Loop
    • 15: For Loop
    • 16: Required
    • 17: Assert
    • 18: Revert
    • 19: Modifier
  • STEP 3
    • 20: Constructor
    • 21: Mapping
    • 22: Array
    • 23: Enum
    • 24: Structs
    • 25: Data Location
    • 26: Inheritance
    • 27: The Shadowing Effect
    • 28: Super Keyword
    • 29: Visibility
  • STEP 4
    • 30: Interface
    • 31: Abstract Contract
    • 32: Payable
    • 33: Using type()
    • 34: Sending Ether
    • 35: Receive
    • 36: Fallback
    • 37: Call
    • 38: DelegateCall
    • 39: Calling Other Contracts
  • STEP 5
    • 40: Factory Contract
    • 41: Proxy Contract
    • 42: Create2
    • 43: Try and Catch
    • 44: Solidity Library
    • 45: ABI Encoded
    • 46: ABI Decoded
    • 47: Keccak256
    • 48: Function Signature Hash
  • TIPS
    • Tips: Solidity by "Immunable"
    • Tips: Truffle Tutorial
    • Tips: Microblog Dapp
    • Tips: Reentrancy
    • Tips: Slither Tutorial
    • Tips: Remix Tutorial
    • Tips: Hardhat Tutorial
  • CAREER
    • 💲Cover Letter
    • 💲Resume
  • ABOUT
    • Contact me
Powered by GitBook
On this page
  1. STEP 2

19: Modifier

A modifier is a keyword used to change the behavior of a function or a contract. Modifiers allow you to define a set of conditions that must be met before the function is executed. Modifiers can be used to restrict access to certain functions or to enforce business logic. Here are some examples of how modifiers can be used.

  • Example 1: Restrict Access In this example, we will create a modifier to restrict access to a function. This modifier will require the caller of the function to have a certain role.

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

function doSomething() onlyAdmin {  // Do something }

In this example, the onlyAdmin modifier will check to see if the sender of the function call has the admin role. If the sender does not have the admin role, the function call will be prevented from executing.

  • Example 2: Enforce Business Logic In this example, we will create a modifier to enforce a business logic rule. This modifier will check to see if the value of a parameter is within a certain range.

modifier withinRange(uint min, uint max) {  
    require(min <= max);  
    require(min <= myValue && myValue <= max);  
    _;
} 

function doSomething(uint myValue) withinRange(100, 200) {// Do something}

In this example, the withinRange modifier will check to see if the myValue parameter is between 100 and 200. If myValue is outside of this range, the function call will be prevented from executing.

Modifiers are a powerful tool that can be used to restrict access to certain functions and enforce business logic rules. They can help make your contracts more secure and reliable.

That's it for the lesson 19! In the next lesson, Constructor

Previous18: RevertNext20: Constructor

Last updated 1 year ago