Quick Guide To Solidity Access Control
In this article, we will explore the various Solidity access control mechanisms with examples and how they can be used to enforce restricted access to specific parts of a smart contract.
In most contracts, access to the contract is restricted. Unless specified as public, a contract state is read-only by default.
Solidity Access Control / Restricted Access
A contract can be restricted from being modified or being called with the assistance of modifiers.
Here are the steps we will take to create and use multiple modifiers:
- onlyBy : When used on a function, it can only be called by the user mentioned in the call.
- OnlyAfter: This specifies that a function can be called after a certain amount of time has passed.
- Costs: once used on a function, the caller can only call this function if a certain value is supplied.
The following program shows the methods of restricting functions:
Above example contains two functions, get_Product and get_Sum, and two modifiers, onlyBy and onlyAfter.
The SolidityTest contract contains two state variables, owner and time, which are initialized with the address of the contract creator (msg.sender) and the current block timestamp (block.timestamp), respectively.
The onlyBy modifier is defined to restrict access to certain functions to only the contract owner.
The require statement checks whether the user_address parameter passed to the function matches the owner address.
If the address does not match, the function will revert with an error message. The _ symbol is a placeholder that represents the code of the function that the modifier is applied to.
The get_Product function takes two uint parameters, mrx and ample, and returns their product.
The onlyBy(owner) modifier is applied to the function, which means that only the contract owner can call this function.
The onlyAfter modifier is defined to restrict access to certain functions until a certain block timestamp has been reached.
The require statement checks whether the enter_Time parameter passed to the function is equal to or greater than the time state variable plus 1 minute. If the condition is not met, the function will revert with an error message.
The get_Sum function takes two uint parameters, mrx and ample, and returns their sum. The onlyBy(owner) and onlyAfter(time + 1 minutes) modifiers are applied to the function, which means that only the contract owner can call this function and the function can only be called one minute after the contract was deployed.
Here’s another function that shows the process of buyer verification, transfer and time check in order to encrypt the function to the real buyer only:
Solidity function restrictions provides you with the ability to add additional conditions or restrictions to the execution of your smart contracts.
It is possible to make a your code more secure, robust, and efficient by using modifiers.
The type of restrictions that need to be implemented in a Solidity smart contract must be carefully considered and modified accordingly.
This allows you to create smart contracts that are more trustworthy and reliable and better suited to their specific needs.