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:

  1. onlyBy : When used on a function, it can only be called by the user mentioned in the call.
  2. OnlyAfter: This specifies that a function can be called after a certain amount of time has passed.
  3. 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:

Example: 

pragma solidity ^0.8.0;contract SolidityTest { address public owner=msg.sender; uint public time=block.timestamp;modifier onlyBy(address user_address){require(user_address==owner,"Welcome to the program User"); _; }function get_Product(uint mrx,uint ample) public onlyBy(owner) view returns(uint){return mrx*ample; }modifier onlyAfter(uint enter_Time){require(enter_Time >= time ,"Wait for 2 minutes to get this executed"); _; }function get_Sum(uint mrx,uint ample) public onlyBy(owner) onlyAfter(time + 1 minutes) view returns(uint){return mrx+ample; } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Example Explanation

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:

Example: 

pragma solidity ^0.6.0;contract SolidityTest { address public buyer = msg.sender; uint public creation_Time = block.timestamp;modifier onlyBy(address user_account) { require( msg.sender == user_account, "Buyer Matched." ); _; } function modify_Buyer(address new_buyer) public onlyBy(buyer) { buyer =new_buyer; } modifier onlyAfter(uint _time) { require( creation_Time >= _time, "Function called earlier before time" ); _; } function remover_buyer() public onlyBy(buyer) onlyAfter(creation_Time + 2 weeks) { delete buyer; } modifier costs(uint _amount) { require( msg.value >= _amount, "Transferred Successfully." ); _; if (msg.value > _amount) msg.sender.transfer(msg.value -- _amount); } function forceOwnerChange(address new_buyer) public payable costs(440 ether) { buyer =new_buyer; if (uint(buyer) & 0 == 1) return; }}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Conclusion

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.

We value your feedback.
+1
1
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *