Network
Launch Date
Consensus
Note
Sepolia
Oct 2021
PoW
Like-for-like representation of Ethereum
Görli
Jan 2019
PoA
Proof-of-Authority
Kiln
Mar 2022
PoS
Post-Merge (for ETH2), shadow fork of the mainnet
Kintsugi
Dec 2021
PoS
DEPRECATED, use Kiln; post-Merge (for ETH2)
Ropsten
Nov 2016
PoW
DEPRECATED, use Sepolia; the Merge to happen on Jun 8, 2022
Rinkeby
Apr 2017
PoA
DEPRECATED, use Görli and Görli Faucet
Kovan
Mar 2017
PoA
DEPRECATED, use Sepolia or Görli
List of active and deprecated Ethereum testnets, including Kintsugi.
Features
Optimistic rollup 
ZK-rollup 
Proof
Uses fraud proofs to prove transaction validity. 
Uses validity (zero-knowledge) proofs to prove transaction validity. 
Capital efficiency
Requires waiting through a 1-week delay (dispute period) before withdrawing funds. 
Users can withdraw funds immediately because validity proofs provide incontrovertible evidence of the authenticity of off-chain transactions. 
Data compression
Publishes full transaction data as calldata to Ethereum Mainnet, which increases rollup costs. 
Doesn't need to publish transaction data on Ethereum because ZK-SNARKs and ZK-STARKs already guarantee the accuracy of the rollup state. 
EVM compatibility
Uses a simulation of the Ethereum Virtual Machine (EVM), which allows it to run arbitrary logic and support smart contracts. 
Doesn't widely support EVM computation, although a few EVM-compatible ZK-rollups have appeared. 
Rollup costs
Reduces costs since it publishes minimal data on Ethereum and doesn't have to post proofs for transactions, except in special circumstances. 
Faces higher overhead from costs involved in generating and verifying proofs for every transaction block. ZK proofs require specialized, expensive hardware to create and have high on-chain verification costs. 
Trust assumptions
Doesn't require a trusted setup. 
Requires a trusted setup to work. 
Liveness requirements
Verifiers are needed to keep tabs on the actual rollup state and the one referenced in the state root to detect fraud. 
Users don't need someone to watch the L2 chain to detect fraud. 
Security properties 
Relies on cryptoeconomic incentives to assure users of rollup security. 
Relies on cryptographic guarantees for security. 
Start building
on Alchemy.
Sign up for free
Start building on Optimism.
Sign up for free
Start building on Arbitrum.
Sign up for free
Start building on Ethereum.
Sign up for free
Start building on Polygon.
Sign up for free
Start building on Starknet.
Sign up for free
Start building on Flow.
Sign up for free
kiln faucet
Get free Kiln ETH.
Start building today
Goerli faucet
Get free Goerli ETH.
Start building today
SEPOLIA FAUCET
Get free Sepolia ETH.
Start Building Today
mumbai faucet
Get free Mumbai Matic.
Start building today
rinkeby faucet
Get free Rinkeby
ETH.
Start building today
Start building on Ethereum.
Get started for free
Start building on Ethereum.
Get started for free
Start building on Flow.
Get started for free
Start building on Polygon.
Get started for free
Start building on Starknet.
Get started for free
Start building on Optimism.
Get started for free
Start building on Solana.
Get started for free
Start building on Solana.
Sign up for beta access
Start building on Solana.
Join the waitlist
Arbitrum logo
Start building on Arbitrum.
Get started for free
Build with Alchemy's
Gas Manager & Bundler APIs
Learn
Solidity at
Alchemy
University
Get started today
Build with Alchemy's
Gas Manager & Bundler APIs
curl 
https://release.solana.com/v1.10.32/solana-install-init-x86_64-pc-windows-msvc.exe 
--output 
C:\solana-install-tmp\solana-install-init.exe 
--create-dirs
Learn Solidity
SOLIDITY MODIFIER OVERVIEW

Learn Solidity: What is a Solidity modifier?

What Solidity Modifiers Do, How They Work, and When to Use Them
Last Updated:
October 4, 2022
Table of Contents
Table of Contents
Table of Contents

{{learn-solidity}}

Solidity is an object-oriented, high-level language for implementing smart contracts. While learning Solidity, you will come across modifiers, which is a special type of function that modifies the behavior of other functions

In this article, we’ll explain what a Solidity modifier does, introduce the types of modifiers and show you how to use them. By the end of this article, you will be able to recognize Solidity modifiers and understand how to use them.

What does a Solidity modifier do?

A modifier is a special type of Solidity function that is used to modify the behavior of other functions. For example, developers can use a modifier to check that a certain condition is met before allowing the function to execute. 

Modifiers are similar to functions, in that they can take arguments and have a return type. Modifiers can also be chained together, meaning that you can have multiple modifiers on a single function. 

However, modifiers can only modify contract logic, and they cannot modify a contract’s storage, which includes structs. Modifiers reduce the amount of boilerplate code that developers have to write, and can make your Solidity code more readable.

Can you have multiple modifiers on Solidity functions?

Yes, developers can use multiple modifiers on a single Solidity function. Multiple modifiers are separated by commas. One important thing to note about using multiple modifiers is that the order of modifiers matters. The first modifier in the list will be executed first, the second modifier will be applied second, and so on. 

For example, if you have a modifier that checks if a user is authenticated and another modifier that checks if a user is authorized to view a certain resource, then the order in which those modifiers are applied will determine whether or not a user is able to view the resource. 

What are the different types of Solidity modifiers?

There are four broad categories of Solidity modifiers: gate checks, prerequisites, filters, and reentrancy attack prevention.

1. Gate Checks

A "gate check" is a modifier that checks if a certain condition is true before allowing a function to execute. 

For example, you might have a function that allows a user to withdraw money from their account, but before the function executes, a developer might want to check if the user has enough money in their account to make the withdrawal. That check is considered a gate check modifier. 

Another example of a gate check is a function that checks if a user is authenticated before allowing them to view a certain resource. 

2. Prerequisites

A "prerequisite" is a modifier that sets up the environment for a function to execute, rather than checking if a certain condition is true. 

For example, a Solidity developer might use a function that requires a certain amount of Ether to be sent along with it in order to execute. In that case, the prerequisite would be the function that sets up the Ether balance. 

3. Filters

A "filter" is a modifier that checks if a certain condition is true, and, if it is, allows the function to execute. If the condition is not true, then the function will not execute. 

Unlike a gate check, which will not automatically allow the function to execute even if the condition is true, a filter will allow the function to execute if the condition is true. 

4. Reentrancy Attack Prevention

A reentrancy attack is a type of attack where a malicious actor tries to execute a function multiple times, via a recursive call, in order to exploit it.

For example, imagine that you have a function that allows a user to withdraw money from their account. A reentrancy attacker might try to call that function multiple times to withdraw more money than they actually have in their account. 

To prevent reentrancy attacks, you can use a modifier that checks if the function is being called recursively. If it is, then the function will not execute.

What is the relationship between require and Solidity modifiers?

Require is often used interchangeably with modifiers, since they both allow you to check if a certain condition is true before allowing a function to execute. If the specified condition is not true, then the compiler with throw an error.

For instance, the following statement uses the require keyword so that only an owner can interact with a function:



require(msg.sender == owner)

Using a modifier can achieve the exact same result, which would look like this: 

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

There are some important differences between require and Solidity modifiers:

  1. Modifiers can be used to set up the environment for a function to execute (as in the case of prerequisites)
  2. Require can only be used to check if a certain condition is true
  3. Modifiers can be overridden
  4. Require cannot be overridden

How does Solidity handle modifiers compared to Vyper?

Vyper is a pythonic language for Ethereum smart contract development that makes certain tradeoffs to increase security, including not using modifiers. Instead, developers are meant to use inline checks and asserts in the function, and if modifying smart contracts, to again make changes explicitly as part of the function.

Vyper's choice to remove the ability to use modifiers increases smart contract auditability because the reader doesn’t have to mentally place the modifier around the function to see what it will do.

What is modifier overriding?

The keyword "virtual" can be used to indicate that a modifier can be overridden in a derived contract. For example, imagine that you have a contract "Base Contract" with a modifier "myModifier". You also have a contract "Derived Contract" that inherits from "Base Contract". 

If "Base Contract" is marked as "virtual", then it can be overridden in the derived contract. This is often used in the context of libraries, where a contract allows for customization. 

How do modifiers work with inheritance?

Inheritance lets you extend a contract's attributes and properties, and in the context of modifiers, inheritance allows you to add new modifiers, or override existing ones. This can be done with the keyword virtual which was discussed in the previous section. 

The simple implementation below demonstrates how inheritance and modifiers work together:



contract A {
modifier X virtual {
}
}

contract B is A {
modifier X override {
}
}

In this example, contract B inherits from contract A. Both contracts have a modifier called "X". However, in contract B, the modifier is marked as "override", which indicates that it overrides the modifier in contract A.

How to Use a Modifier in Solidity

When using a modifier, you first need to define the modifier function in a contract. Modifiers use a special symbol “_;” where the function body is inserted only if the condition of the modifier is satisfied.

The contract below demonstrates how to use a modifier:



contract Owner {
   modifier onlyOwner {
      require(msg.sender == owner);
      _;
   }
   modifier costs(uint price) {
      if (msg.value >= price) {
         _;
      }
   }
}

In the example above, the contract has two modifiers: “onlyOwner” and “costs”.

The first modifier checks that the msg.sender is the owner of the contract, and the second modifier checks that the msg.value is greater than or equal to a certain price. 

Both of these modifiers can be used on any function in the contract. 

Continue Learning About Solidity Modifiers

Modifiers are functions that are used to modify the behavior of other functions. By using modifiers, you can reduce the amount of boilerplate code that you have to write, and can make your Solidity code more readable.

To continue learning about Solidity modifiers and how to become a Solidity developer, secure your spot in Alchemy University's 7-week Ethereum Developer Bootcamp. Originally created by the ChainShot team, this FREE, Solidity crash course is the best way to learn Solidity. If developers are new to development in general, Alchemy University's 3-week JavaScript crash course is a great prerequisite before starting an Ethereum bootcamp.

ALCHEMY SUPERNODE - ETHEREUM NODE API

Scale to any size, without any errors

Alchemy Supernode finally makes it possible to scale blockchain applications without all the headaches. Plus, our legendary support will guide you every step of the way.

Get started for free
Supernode footer
Learn Solidity
SOLIDITY MODIFIER OVERVIEW

What is a modifier in Solidity?

What Solidity Modifiers Do, How They Work, and When to Use Them
Last Updated:
October 4, 2022
Last Updated:
March 14, 2023
Don't miss an update
Sign up for our newsletter to get alpha, key insights, and killer resources.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Table of Contents

Talk to an Expert

Learn how Alchemy's blockchain developer tools can help your business succeed in web3!
Valid number
Thank you! An Alchemy expert will be in touch with you shortly!
Oops! Something went wrong while submitting the form.

{{learn-solidity}}

Table of Contents

Solidity is an object-oriented, high-level language for implementing smart contracts. While learning Solidity, you will come across modifiers, which is a special type of function that modifies the behavior of other functions

In this article, we’ll explain what a Solidity modifier does, introduce the types of modifiers and show you how to use them. By the end of this article, you will be able to recognize Solidity modifiers and understand how to use them.

What does a Solidity modifier do?

A modifier is a special type of Solidity function that is used to modify the behavior of other functions. For example, developers can use a modifier to check that a certain condition is met before allowing the function to execute. 

Modifiers are similar to functions, in that they can take arguments and have a return type. Modifiers can also be chained together, meaning that you can have multiple modifiers on a single function. 

However, modifiers can only modify contract logic, and they cannot modify a contract’s storage, which includes structs. Modifiers reduce the amount of boilerplate code that developers have to write, and can make your Solidity code more readable.

Can you have multiple modifiers on Solidity functions?

Yes, developers can use multiple modifiers on a single Solidity function. Multiple modifiers are separated by commas. One important thing to note about using multiple modifiers is that the order of modifiers matters. The first modifier in the list will be executed first, the second modifier will be applied second, and so on. 

For example, if you have a modifier that checks if a user is authenticated and another modifier that checks if a user is authorized to view a certain resource, then the order in which those modifiers are applied will determine whether or not a user is able to view the resource. 

What are the different types of Solidity modifiers?

There are four broad categories of Solidity modifiers: gate checks, prerequisites, filters, and reentrancy attack prevention.

1. Gate Checks

A "gate check" is a modifier that checks if a certain condition is true before allowing a function to execute. 

For example, you might have a function that allows a user to withdraw money from their account, but before the function executes, a developer might want to check if the user has enough money in their account to make the withdrawal. That check is considered a gate check modifier. 

Another example of a gate check is a function that checks if a user is authenticated before allowing them to view a certain resource. 

2. Prerequisites

A "prerequisite" is a modifier that sets up the environment for a function to execute, rather than checking if a certain condition is true. 

For example, a Solidity developer might use a function that requires a certain amount of Ether to be sent along with it in order to execute. In that case, the prerequisite would be the function that sets up the Ether balance. 

3. Filters

A "filter" is a modifier that checks if a certain condition is true, and, if it is, allows the function to execute. If the condition is not true, then the function will not execute. 

Unlike a gate check, which will not automatically allow the function to execute even if the condition is true, a filter will allow the function to execute if the condition is true. 

4. Reentrancy Attack Prevention

A reentrancy attack is a type of attack where a malicious actor tries to execute a function multiple times, via a recursive call, in order to exploit it.

For example, imagine that you have a function that allows a user to withdraw money from their account. A reentrancy attacker might try to call that function multiple times to withdraw more money than they actually have in their account. 

To prevent reentrancy attacks, you can use a modifier that checks if the function is being called recursively. If it is, then the function will not execute.

What is the relationship between require and Solidity modifiers?

Require is often used interchangeably with modifiers, since they both allow you to check if a certain condition is true before allowing a function to execute. If the specified condition is not true, then the compiler with throw an error.

For instance, the following statement uses the require keyword so that only an owner can interact with a function:



require(msg.sender == owner)

Using a modifier can achieve the exact same result, which would look like this: 

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

There are some important differences between require and Solidity modifiers:

  1. Modifiers can be used to set up the environment for a function to execute (as in the case of prerequisites)
  2. Require can only be used to check if a certain condition is true
  3. Modifiers can be overridden
  4. Require cannot be overridden

How does Solidity handle modifiers compared to Vyper?

Vyper is a pythonic language for Ethereum smart contract development that makes certain tradeoffs to increase security, including not using modifiers. Instead, developers are meant to use inline checks and asserts in the function, and if modifying smart contracts, to again make changes explicitly as part of the function.

Vyper's choice to remove the ability to use modifiers increases smart contract auditability because the reader doesn’t have to mentally place the modifier around the function to see what it will do.

What is modifier overriding?

The keyword "virtual" can be used to indicate that a modifier can be overridden in a derived contract. For example, imagine that you have a contract "Base Contract" with a modifier "myModifier". You also have a contract "Derived Contract" that inherits from "Base Contract". 

If "Base Contract" is marked as "virtual", then it can be overridden in the derived contract. This is often used in the context of libraries, where a contract allows for customization. 

How do modifiers work with inheritance?

Inheritance lets you extend a contract's attributes and properties, and in the context of modifiers, inheritance allows you to add new modifiers, or override existing ones. This can be done with the keyword virtual which was discussed in the previous section. 

The simple implementation below demonstrates how inheritance and modifiers work together:



contract A {
modifier X virtual {
}
}

contract B is A {
modifier X override {
}
}

In this example, contract B inherits from contract A. Both contracts have a modifier called "X". However, in contract B, the modifier is marked as "override", which indicates that it overrides the modifier in contract A.

How to Use a Modifier in Solidity

When using a modifier, you first need to define the modifier function in a contract. Modifiers use a special symbol “_;” where the function body is inserted only if the condition of the modifier is satisfied.

The contract below demonstrates how to use a modifier:



contract Owner {
   modifier onlyOwner {
      require(msg.sender == owner);
      _;
   }
   modifier costs(uint price) {
      if (msg.value >= price) {
         _;
      }
   }
}

In the example above, the contract has two modifiers: “onlyOwner” and “costs”.

The first modifier checks that the msg.sender is the owner of the contract, and the second modifier checks that the msg.value is greater than or equal to a certain price. 

Both of these modifiers can be used on any function in the contract. 

Continue Learning About Solidity Modifiers

Modifiers are functions that are used to modify the behavior of other functions. By using modifiers, you can reduce the amount of boilerplate code that you have to write, and can make your Solidity code more readable.

To continue learning about Solidity modifiers and how to become a Solidity developer, secure your spot in Alchemy University's 7-week Ethereum Developer Bootcamp. Originally created by the ChainShot team, this FREE, Solidity crash course is the best way to learn Solidity. If developers are new to development in general, Alchemy University's 3-week JavaScript crash course is a great prerequisite before starting an Ethereum bootcamp.

Solidity is an object-oriented, high-level language for implementing smart contracts. While learning Solidity, you will come across modifiers, which is a special type of function that modifies the behavior of other functions

In this article, we’ll explain what a Solidity modifier does, introduce the types of modifiers and show you how to use them. By the end of this article, you will be able to recognize Solidity modifiers and understand how to use them.

What does a Solidity modifier do?

A modifier is a special type of Solidity function that is used to modify the behavior of other functions. For example, developers can use a modifier to check that a certain condition is met before allowing the function to execute. 

Modifiers are similar to functions, in that they can take arguments and have a return type. Modifiers can also be chained together, meaning that you can have multiple modifiers on a single function. 

However, modifiers can only modify contract logic, and they cannot modify a contract’s storage, which includes structs. Modifiers reduce the amount of boilerplate code that developers have to write, and can make your Solidity code more readable.

Can you have multiple modifiers on Solidity functions?

Yes, developers can use multiple modifiers on a single Solidity function. Multiple modifiers are separated by commas. One important thing to note about using multiple modifiers is that the order of modifiers matters. The first modifier in the list will be executed first, the second modifier will be applied second, and so on. 

For example, if you have a modifier that checks if a user is authenticated and another modifier that checks if a user is authorized to view a certain resource, then the order in which those modifiers are applied will determine whether or not a user is able to view the resource. 

What are the different types of Solidity modifiers?

There are four broad categories of Solidity modifiers: gate checks, prerequisites, filters, and reentrancy attack prevention.

1. Gate Checks

A "gate check" is a modifier that checks if a certain condition is true before allowing a function to execute. 

For example, you might have a function that allows a user to withdraw money from their account, but before the function executes, a developer might want to check if the user has enough money in their account to make the withdrawal. That check is considered a gate check modifier. 

Another example of a gate check is a function that checks if a user is authenticated before allowing them to view a certain resource. 

2. Prerequisites

A "prerequisite" is a modifier that sets up the environment for a function to execute, rather than checking if a certain condition is true. 

For example, a Solidity developer might use a function that requires a certain amount of Ether to be sent along with it in order to execute. In that case, the prerequisite would be the function that sets up the Ether balance. 

3. Filters

A "filter" is a modifier that checks if a certain condition is true, and, if it is, allows the function to execute. If the condition is not true, then the function will not execute. 

Unlike a gate check, which will not automatically allow the function to execute even if the condition is true, a filter will allow the function to execute if the condition is true. 

4. Reentrancy Attack Prevention

A reentrancy attack is a type of attack where a malicious actor tries to execute a function multiple times, via a recursive call, in order to exploit it.

For example, imagine that you have a function that allows a user to withdraw money from their account. A reentrancy attacker might try to call that function multiple times to withdraw more money than they actually have in their account. 

To prevent reentrancy attacks, you can use a modifier that checks if the function is being called recursively. If it is, then the function will not execute.

What is the relationship between require and Solidity modifiers?

Require is often used interchangeably with modifiers, since they both allow you to check if a certain condition is true before allowing a function to execute. If the specified condition is not true, then the compiler with throw an error.

For instance, the following statement uses the require keyword so that only an owner can interact with a function:



require(msg.sender == owner)

Using a modifier can achieve the exact same result, which would look like this: 

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

There are some important differences between require and Solidity modifiers:

  1. Modifiers can be used to set up the environment for a function to execute (as in the case of prerequisites)
  2. Require can only be used to check if a certain condition is true
  3. Modifiers can be overridden
  4. Require cannot be overridden

How does Solidity handle modifiers compared to Vyper?

Vyper is a pythonic language for Ethereum smart contract development that makes certain tradeoffs to increase security, including not using modifiers. Instead, developers are meant to use inline checks and asserts in the function, and if modifying smart contracts, to again make changes explicitly as part of the function.

Vyper's choice to remove the ability to use modifiers increases smart contract auditability because the reader doesn’t have to mentally place the modifier around the function to see what it will do.

What is modifier overriding?

The keyword "virtual" can be used to indicate that a modifier can be overridden in a derived contract. For example, imagine that you have a contract "Base Contract" with a modifier "myModifier". You also have a contract "Derived Contract" that inherits from "Base Contract". 

If "Base Contract" is marked as "virtual", then it can be overridden in the derived contract. This is often used in the context of libraries, where a contract allows for customization. 

How do modifiers work with inheritance?

Inheritance lets you extend a contract's attributes and properties, and in the context of modifiers, inheritance allows you to add new modifiers, or override existing ones. This can be done with the keyword virtual which was discussed in the previous section. 

The simple implementation below demonstrates how inheritance and modifiers work together:



contract A {
modifier X virtual {
}
}

contract B is A {
modifier X override {
}
}

In this example, contract B inherits from contract A. Both contracts have a modifier called "X". However, in contract B, the modifier is marked as "override", which indicates that it overrides the modifier in contract A.

How to Use a Modifier in Solidity

When using a modifier, you first need to define the modifier function in a contract. Modifiers use a special symbol “_;” where the function body is inserted only if the condition of the modifier is satisfied.

The contract below demonstrates how to use a modifier:



contract Owner {
   modifier onlyOwner {
      require(msg.sender == owner);
      _;
   }
   modifier costs(uint price) {
      if (msg.value >= price) {
         _;
      }
   }
}

In the example above, the contract has two modifiers: “onlyOwner” and “costs”.

The first modifier checks that the msg.sender is the owner of the contract, and the second modifier checks that the msg.value is greater than or equal to a certain price. 

Both of these modifiers can be used on any function in the contract. 

Continue Learning About Solidity Modifiers

Modifiers are functions that are used to modify the behavior of other functions. By using modifiers, you can reduce the amount of boilerplate code that you have to write, and can make your Solidity code more readable.

To continue learning about Solidity modifiers and how to become a Solidity developer, secure your spot in Alchemy University's 7-week Ethereum Developer Bootcamp. Originally created by the ChainShot team, this FREE, Solidity crash course is the best way to learn Solidity. If developers are new to development in general, Alchemy University's 3-week JavaScript crash course is a great prerequisite before starting an Ethereum bootcamp.

{{learn-solidity}}

Contact Us

Talk to an expert at Alchemy to answer all of your product questions.
Valid number
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Build blockchain magic with Alchemy

Alchemy combines the most powerful web3 developer products and tools with resources, community and legendary support.

Get started for free