Payable color pixel function
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:13] To color a pixel, we're going to require that Ether is sent to buy the pixel. We have our pixel struct already, and we also want our pixel changed event to emit the new owner and sold price, along with the x, y, and color as before.
[00:14 - 00:18] This way our UI can keep track of how much each pixel costs. Here's how we change color pixel.
[00:19 - 00:25] First, our color pixel function needs to be marked "Payable". We want to read the current pixel from our contract storage.
[00:26 - 00:37] Here we declare the type "pixel", and we designate this variable as coming from "storage". This is important because variables in storage cost a different amount of gas than variables in memory, and we don't want to copy data unnecessarily.
[00:38 - 00:53] This syntax means that our pixel object will reference the same pixel in storage, so any changes to this struct will be written to storage as well. We want to require that the message.value, that is, the amount of Ether sent along, is greater than the last sale price for this pixel.
[00:54 - 01:02] Pixels that haven't been set will have all values initialized to zero. So in the case of an unclaimed pixel, you just need to send a positive amount of Ether.
[01:03 - 01:16] If not enough Ether was sent to win the bid, then "require" will revert all state changes and exit from this function. The next line will only be run if the bid is highest, so we update the pixel's owner to the message.center.
[01:17 - 01:24] We set the sold price to be the current value of this transaction, and then we set the new color. Lastly, we'll omit the event that says all of this happened.
[01:25 - 01:32] Let's try it out. First we'll create the contract.
[01:33 - 01:46] Next, we'll try to create a pixel without sending any value. It didn't work.
[01:47 - 01:56] That's because the "require" statement triggered, so let's add some value. It looks like it worked.
[01:57 - 02:01] Let's grab that pixel. Nice.
[02:02 - 02:10] Notice that it shows not only the color, but also our address is the owner and the price that it's sold for. Let's try to buy that same pixel at a lower price.
[02:11 - 02:20] Again, it doesn't work. But if we increase our bid...
[02:21 - 02:27] It does. Now, there's one problem with our setup.
[02:28 - 02:32] The contract keeps all the money, and we have no way to get it out. What we want to do is this.
[02:33 - 02:36] If someone is outbid, they should receive a refund for their previous bid.