Contract Owner and require

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the Million Ether Homepage course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to Million Ether Homepage, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Million Ether Homepage
  • [00:00 - 00:03] Common pattern in Solidity Contracts is specifying an owner. The idea is this.

    [00:04 - 00:13] When we create a contract, in the Contracts constructor, we'll store the address of the account that created this contract. That account becomes the owner.

    [00:14 - 00:25] And in our code, we can grant special privileges to the owner by checking that the message sender is the person who created the contract. In this case, when the contract is destroyed, we'll send the funds back to that owner.

    [00:26 - 00:30] Here's how we modify our bank contract to have an owner. First, we'll add an address property to store the owner.

    [00:31 - 00:40] And then we'll add a constructor function. Here we'll set the owner to the message.center, that is, the address of the account that created this contract.

    [00:41 - 00:49] Last, we'll add a destroy function. And this function we'll call self-destruct, which is a special function that essentially shuts down our contract.

    [00:50 - 01:02] The argument to self-destruct is the address where we want to send all of the funds that this contract controls. Notice here that the owner property is not a magic variable in Solidity.

    [01:03 - 01:13] That is, it's a normal address that we're keeping in storage, like any other variable we keep in storage. It's only the logic that we have in our code that gives it special privileges.

    [01:14 - 01:21] 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 self-destruct our contract.

    [01:22 - 01:28] Let's introduce one more Solidity concept, require. We only want the owner of the contract to be able to call destroy.

    [01:29 - 01:35] If someone else calls destroy, we don't want self-destruct to be called. Do you remember how we check who is calling a function?

    [01:36 - 01:41] We use message.center. So we could have a simple if statement that checks that the message.center is the owner.

    [01:42 - 01:45] This would work. And if someone other than the owner tried to call destroy, it would fail silently.

    [01:46 - 01:51] But Solidity has a mechanism that's sort of like raising an exception. It's called the require operator.

    [01:52 - 01:56] We can add the require like this. Require message.center = owner.

    [01:57 - 02:03] Require checks the condition. And if the condition is false, it will revert all state changes up to that point.

    [02:04 - 02:07] We use require for input validation. And we will be using require a lot.

    [02:08 - 02:11] So let's try out self-destruct. First, we'll create the contract.

    [02:12 - 02:23] And I'm going to select my second account here because the first account is where my mining rewards go. And you can see here that the owner address is our account address.

    [02:24 - 02:30] Then we'll deposit three ether to the bank. Now let's check our contracts balance.

    [02:31 - 02:39] Looks good. And our account balance.

    [02:40 - 02:47] Now let's destroy our contract. But first, let's try it from our first account.

    [02:48 - 02:54] That is the account that isn't the owner. It says callback contains no result, gas required exceeds limit.

    [02:55 - 03:01] That error message isn't exactly clear, but what's happening here is that it did not accept our destroy transaction, which is what we want. Let's try it from the owner account.

    [03:02 - 03:08] Great, it looks like it worked. Let's check our balances again.

    [03:09 - 03:11] Good, our account has three more ether. And our contract has a balance of zero.

    [03:12 - 03:15] Now we know the basics of payable contracts, ownership, and using require. Let's use this to color pixels.

    [03:16 - 03:17] Now let's check our balance. Let's check our balances again.

    [03:18 - 03:26] Good, our account has three more ether. And our contract has a balance of zero.

    [03:27 - 03:30] Now we know the basics of payable contracts, ownership, and using require. Let's use this to color pixels.

    [03:31 - 03:34] ownership and using Require. Let's use this to color pixels.