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

Common pattern in Solidity Contracts is specifying an owner. The idea is this. 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. 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. Here's how we modify our bank contract to have an owner. First, we'll add an address property to store the owner. 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. 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. 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. 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. 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. 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 self-destruct to be called. Do you remember how we check who is calling a function? We use message.center. So we could have a simple if statement that checks that the message.center is the owner. This would work. And if someone other than the owner tried to call destroy, it would fail silently. But Solidity has a mechanism that's sort of like raising an exception. It's called the require operator. We can add the require like this. Require message.center = owner. Require checks the condition. And if the condition is false, it will revert all state changes up to that point. We use require for input validation. And we will be using require a lot. So let's try out self-destruct. First, we'll create the contract. 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. Then we'll deposit three ether to the bank. Now let's check our contracts balance. Looks good. And our account balance. Now let's destroy our contract. But first, let's try it from our first account. That is the account that isn't the owner. It says callback contains no result, gas required exceeds limit. 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. Great, it looks like it worked. Let's check our balances again. Good, our account has three more ether. And our contract has a balance of zero. Now we know the basics of payable contracts, ownership, and using require. Let's use this to color pixels. Now let's check our balance. Let's check our balances again. Good, our account has three more ether. And our contract has a balance of zero. Now we know the basics of payable contracts, ownership, and using require. Let's use this to color pixels. ownership and using Require. Let's use this to color pixels.