make a modifier in solidity

Modifying in Solidity

Solidity is a programming language used for developing smart contracts on the Ethereum blockchain. Modifiers in Solidity are used to modify the behavior of a function by adding some conditions or restrictions to it.

Creating a Modifier

To create a modifier, we use the keyword "modifier" followed by the name of the modifier. Here is an example:

modifier onlyAdmin() {
    require(admin == msg.sender, "Only admin can execute this function");

This modifier is called onlyAdmin and it checks if the sender of the transaction is the admin. If it is, then the function will be executed, otherwise, it will revert with an error message.

Using a Modifier

To use a modifier, we simply add the modifier's name before the function declaration. Here is an example:

function changeAdmin() public onlyAdmin {
    // Code to change admin

This function can only be executed by the admin because we added the onlyAdmin modifier to it.

Multiple Modifiers

You can also use multiple modifiers on a function by separating them with commas. Here is an example:

modifier onlyAdminAndOwner() {
    require(admin == msg.sender || owner == msg.sender, "Only admin or owner can execute this function");

function changeAdminAndOwner() public onlyAdminAndOwner {
    // Code to change admin and owner

This function can only be executed by the admin or the owner because we added the onlyAdminAndOwner modifier to it.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]