🔮
Ethereum
  • General
    • What is Ethereum
      • Ethereum & Bitcoin General Comparison
      • Singleton State
      • The Ethereum Virtual Machine (EVM)
      • Opcodes (operation codes) EVM
      • Ethereum Client
      • Forks
      • Gas (wei)
        • EIP-1559
        • Table Conversion (wei)
      • Proof of Stake (PoS)
      • Proof of Authority (PoA)
      • The Beacon Chain
      • Networks
        • Ethereum mainnet
        • Goerli
      • Account-based model & UTXO-based model
      • Externally Owned Account (EOA)
      • Node Clients
        • Geth
        • Nethermind
      • Contract Account
      • Smart Contract Upgradeability
      • Ultrasound Money
      • Merkle Trees
        • Patricia Merkle Tree
      • Tries
        • State Trie
        • Storage Trie
        • Transactions Trie
        • Receipts Trie
      • Transactions
        • Ethereum Transaction Architecture
      • World State
        • Chain of States
        • Chain of Blocks
        • Stack of Transactions / Mempool
      • Contract Creation
      • Message Call Transaction
      • P2P Network
      • Web3.js
      • Ether.js
        • Smart Contract Interaction Example
      • Web3.js vs Ether.js
      • Node Providers
      • ENS (Ethereum Name Service)
      • Web3 dapp
      • Escrow
      • Multi-signature
      • ERC-20 tokens
        • Send ERC20s to Contracts
      • NFTs
        • ERC-721 and ERC-1155
      • Solidity
        • State Variables
        • Data Location
        • Numbers
        • Modifiers
        • View & Pure Modifiers
        • Data Types
          • Modifiers
          • Modifiers (Functions)
          • Address & Address Payable
        • Hardhat
        • Payable Functions
        • Receive Function
        • Fallback Function
        • Global Variables
        • Self Destruct
        • Create2 Function
        • Revert function
        • Require function
        • Assert Function
        • Calldata
        • Interface
        • Mapping
        • Array
        • Struct
        • Inheritance
          • Virtual & Overwrite
          • Multiple inheritance
          • Hierarchical Inheritance
        • Events
          • Indexed (keyword)
          • LOG0 - LOG4
        • Multi-signature Example
        • Smart Contracts
          • Context
      • Application Binary Interface (ABI )
  • Extras
    • Terminology
      • Bytecode
      • Keccak-256
      • Turing complete
Powered by GitBook
On this page
  1. General
  2. What is Ethereum
  3. Solidity

Modifiers

Modifiers in Solidity are a way to define a function that can be applied to other functions, to add additional functionality or restrictions to the original function. Modifiers are used to simplify the code and make it more readable and easier to maintain.

Modifiers are defined using the modifier keyword, followed by the name of the modifier, and the code block that defines the modifier. A function can have multiple modifiers, which are executed in the order they are specified in the function declaration.

Modifiers are commonly used to restrict access to a function, by checking that the caller has the required permissions. They can also be used to check that certain conditions are met before executing a function, or to modify the input or output parameters of a function.

Modifiers are usually combined with the require statement, which is used to check that certain conditions are met before executing the rest of the function code. If the condition is not met, the function is reverted and the transaction is aborted.

Here's an example of a modifier that restricts access to a function:

pragma solidity ^0.8.0;

contract MyContract {
    address owner;

    modifier onlyOwner() {
        require(msg.sender == owner, "Only the contract owner can call this function");
        _;
    }

    function myFunction() public onlyOwner {
        // Function code here
    }
}

In this example, the onlyOwner modifier checks that the caller of the function is the contract owner, as specified by the owner variable. If the condition is not met, the function is reverted and an error message is returned to the user.

PreviousNumbersNextView & Pure Modifiers

Last updated 2 years ago

Search — Solidity 0.8.19 documentation
Logo