Creating Ownable Contracts in Solidity
A Smart Contract (or crypto contract) is a computer program that directly and automatically controls the transfer of digital assets between the parties under certain conditions. Once deployed, a smart contract code cannot be changed. It is also publicly visible to everyone.
Solidity is a language used for creating smart contracts which is then compiled to a byte code which in turn is deployed on the Ethereum network. Even though, the code is publicly visible, the calling of functions can be restricted using modifiers.
Solidity provides some access modifiers by default:
- Public- Accessible by anyone
- Private- Accessible by only contracts where they are defined and not by inheriting contracts.
- Protected- Accessible by only contracts where they are defined and by contracts that inherit them.
- Internal- Accessible by only other functions of the contract and not by other people or contracts.
- External- Accessible by only other people or contracts and not by functions.
Need of Ownable Contracts:
Some functions are necessary for only configuration purposes or one-time calculations. Such functions if exposed publicly can be used maliciously or mistakenly to exhaust gas. To prevent misuse or reduce gas cost, such functions must be restricted to only selected external addresses (for simplicity, owner). To solve this, ownable contracts can be used.
Following is the code for an ownable contract. It can be inherited by other contracts which intend to have ownable modifiers for selected functions.
Below is the Solidity program for the above approach:
Solidity
pragma solidity >=0.7.0;
contract Ownable
{
address private _owner;
constructor()
{
_owner = msg.sender;
}
function owner() public view returns(address)
{
return _owner;
}
modifier onlyOwner()
{
require(isOwner(),
"Function accessible only by the owner !!" );
_;
}
function isOwner() public view returns( bool )
{
return msg.sender == _owner;
}
}
contract OwnableDemo is Ownable
{
uint sum = 0;
uint[] numbers;
function addNumber(uint number) public
{
numbers.push(number);
}
function getSum() public view returns(uint)
{
return sum;
}
function calculateSum() onlyOwner public
{
sum = 0;
for (uint i = 0;
i < numbers.length; i++)
sum += numbers[i];
}
}
|
Output:
Adding numbers from any address (owner or not doesn’t matter):
The function was called thrice with arguments: 5, 6, and 2 respectively
Attempt to call calculateSum() from a non-owner address (Unsuccessful):
Calling calculateSum() as owner (Successful):
Last Updated :
11 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...