Payable Contracts, payable Functions
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo 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.
Get unlimited access to Million Ether Homepage, plus 70+ \newline books, guides and courses with the \newline Pro subscription.
[00:00 - 00:05] Alright, a contract can own funds. Remember how in the Geth console we check the balance of our accounts?
[00:06 - 00:08] What's happening here? That hash is the address of our account.
[00:09 - 00:16] And the Ethereum blockchain stores the number of Ether assigned to that account . We can do the exact same thing with contracts.
[00:17 - 00:23] Remember how our contract is deployed at a particular address? That address is also the contract's account address.
[00:24 - 00:32] And that account can hold a balance controlled by the contract. To explore this idea, let's create a simple bank contract.
[00:33 - 00:43] 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.
[00:44 - 00:50] Our deposit function has a new modifier called payable. Payable functions are functions that can accept Ether value when called.
[00:51 - 01:04] 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.
[01:05 - 01:15] 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.
[01:16 - 01:22] Message.center and message.value. Message.center is the address of the incoming transaction.
[01:23 - 01:34] 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.
[01:35 - 01:42] You can think of these as invisible arguments. Let's deploy this contract and try it.
[01:43 - 02:00] 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.
[02:01 - 02:09] 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.
[02:10 - 02:21] Now, let's copy the address of this contract and check the balance in our Geth console. We have value.
[02:22 - 02:28] The contract now has a balance of Ether of its own. The contract owns these Ether, not us.
[02:29 - 02:32] And let's say we want our money back. Well, we can't get it.
[02:33 - 02:36] We didn't code any way to get the funds out of this contract. Oops.
[02:37 - 02:47] 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.