Making your Smart Contracts Ownable

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 you only the account whoe deployed this contract can call exampleFunction. To see a better example and many more examples, check out OpenZepplin.

Removing Your Smart Contract from a Testnet with Solidity’s Self Destruct

When developing Dapps, you will eventually need to deploy your app the a testnet like Rospen. When doing this, you have an option to self destruct – something you can’t do on the main net. This is helpful to keep the test net clean. Here is how we would implement this.

pragma solidity ^0.4.23;

contract Mortal {
    address owner;

    function Mortal() public {
      owner = msg.sender;
    }

    function kill() public {
      if (msg.sender == owner) selfdestruct(owner);
    }
}


contract Example is Mortal {
}

And that’s it! now you can call kill on your Example contract to remove it from the testnet.