Subscribe to get access to this video

and the whole library of videos, sample code, and tutorials.

Contract Owner and require

Adding an Owner

A common pattern in Solidity contracts is specifying an owner.

The idea is this: when we create the contract, in the contract’s constructor, we’ll store the address of the account that created this contract. That account becomes the owner. When the contract is destroyed, we’ll send the funds back to that owner.

Here’s how we modify our bank to have an owner:

pragma solidity ^0.4.11;

contract Bank {
  // first we'll add an address property to store the owner
  address public owner;

  event DepositMade(
    address sentBy,
    uint amount

  // then we'll add a constructor function. Here we'll set owner to the msg.sender. That is, the address of the account that created this contract.
  function Bank() {
    owner = msg.sender;

  function deposit() payable {
    DepositMade(msg.sender, msg.value);

  // last we'll add a destroy function, and this function will call
  // `selfdestruct`, which is a special function that essentially "shuts down" our
  // contract. The argument to selfdestruct is the address where we want to send
  // all of the funds this contract controls.
  function destroy() {
    // warning -- anyone can call this...

One problem with our destroy function is that anyone can call that function. We don’t have any constraints on who can call destroy and selfdestruct our contract!

Using require

Let’s introduce one more solidity concept: require. We only want the owner of the contract to be able to call destroy. If someone else calls destroy, we don’t want selfdestruct to be called.


Subscribe to get the full text of this lesson

and the entire library of videos, sample code, and tutorials.