Payable Contracts, payable Functions

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

Alright, a contract can own funds. Remember how in the Geth console we check the balance of our accounts? What's happening here? That hash is the address of our account. And the Ethereum blockchain stores the number of Ether assigned to that account . We can do the exact same thing with contracts. Remember how our contract is deployed at a particular address? That address is also the contract's account address. And that account can hold a balance controlled by the contract. To explore this idea, let's create a simple bank contract. In this bank, we can deposit funds. When funds are deposited, we'll create a new event called deposit made, which has the address that the deposit was sent by and the amount. Our deposit function has a new modifier called payable. Payable functions are functions that can accept Ether value when called. That is, when we send the transaction, we send not only the function arguments, but also an additional amount of Ether from our account to the contracts account. And we can use that value in our function. When we call contract functions, there are a number of global or context variables that we can access. This is the first time we've talked about these, and there are two that we're going to use. Message.center and message.value. Message.center is the address of the incoming transaction. So if we call this message from our account, message.center would be our account address. Message.value is the amount of Ether that was sent with the call to this function from the incoming transaction. You can think of these as invisible arguments. Let's deploy this contract and try it. When a deposit is made with this function, we'll emit the deposit made event and pass the message sender and the message value as the arguments. If we hit the deposit button, you'll see a new event is created, but the value that was sent was zero. To send a value in remix, we'll just add on to this value field, and you can do the same thing in Geth. Let's add three Ether. Now, let's copy the address of this contract and check the balance in our Geth console. We have value. The contract now has a balance of Ether of its own. The contract owns these Ether, not us. And let's say we want our money back. Well, we can't get it. We didn't code any way to get the funds out of this contract. Oops. But there is one common way to get Ether out of a contract, and that is to set it up to give us the funds back when the contract self-destructs. The Ether in this contract is lost, so let's improve this code and deploy a new one.