Make your Solidity Smart Contract Ownable

2020-05-10

A good practice during Dapp development is to make your contracts ownable. This will allow you to add modifiers to prevent certain functions form being used. Be careful though, because this can remove the 'decentralized' aspects of the contract.

pragma solidity ^0.4.23;

contract Ownable {
  address public owner;

  constructor() public {
    owner = msg.sender;
  }

  modifier onlyOwner() {
    require(msg.sender == owner);
    _;
  }
}

contract Example is Ownable {
  function exampleFunction() public view onlyOwner {
    // code here
  }
}

Now only the account that deployed this contract can call exampleFunction. To see a better example and many more examples, check out OpenZepplin.

GoTea - KoalaTea