Function Modifiers and how to use them in Solidity.
Function modifiers in solidity helps to restrict access to some functionality of a smart contract to a specific address. This address can be the owner of the smart contract. It allows the owner to be the only one to add value or remove any value in the contract.
If we are to create a Contract with the name "Bank", and we create functions that can allow deposit or withdrawal. This contract without a modifier can be accessed by anyone in the public. To be able to restrict access to only the contract owner, a modifier will be introduced.
In the example above. we created a contract called Bank and functions to deposit and withdraw any value. If this smart contract is deployed, anyone can have access to the smart contract and be able to deposit and withdraw.
This is why the modifiers are introduced, to be able to restrict access, in this case, to deposit or withdraw the value from this smart contract. We can set this contract to be only the person who deploys the smart contract has the ability to make changes whether depositing or withdrawal.
The first thing to do is to create a new variable with the type address and name it as owner according to the example above.
The type address simply refers to an Etheruem address.
Next, we create a constructor function with the name of the contract.
Inside the function, we set the owner variable to msg.sender. What this means is when the contract is deployed it is going to set the owner of that contract to the sender, which is equal to the address that is been used to deploy the contract.
The Next thing to do is to create our function modifier and the way to do that is first write the keyword "Modifier" and then give the modifier a name. In the example below, we gave the name of our modifier as onlyOwner.
require (msg.sender == owner);
This is a way of just saying that if the msg.sender == owner do something if it isn't then an exception will be thrown. If it is true then _;(This symbol means if the condition was true, run the function body where it is used).
HOW TO USE MODIFIERS You use modifiers by placing them on a function that you would want to restrict access to. In our example, we want to restrict access to deposit and withdrawal for just the owner of the contract. We want only the owner of the contract to make any changes regarding deposits and withdrawal. We place the modifier in the function as seen below.
When the contract is deployed, it can only be accessed by the owner required that the address of the sender of the contract is the same as the owner of the contract.
This is basically how you use modifiers to restrict access in your smart contract. If you enjoyed reading this article or learnt something do follow me on Twitter. I'm currently on a #100DaysOfSolidity Challenge so you can follow me for more articles on topics on solidity.
More resources on Function modifiers:
(tutorialspoint.com/solidity/solidity_functi..)
(ethereum.stackexchange.com/questions/48971/..)
and don't forget to always read the Solidity Docs. Solidity Docs