Payable color pixel function

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

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. This way our UI can keep track of how much each pixel costs. Here's how we change color pixel. First, our color pixel function needs to be marked "Payable". We want to read the current pixel from our contract storage. 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. 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. 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. 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. 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. Let's try it out. First we'll create the contract. Next, we'll try to create a pixel without sending any value. It didn't work. That's because the "require" statement triggered, so let's add some value. It looks like it worked. Let's grab that pixel. Nice. 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. Again, it doesn't work. But if we increase our bid... It does. Now, there's one problem with our setup. The contract keeps all the money, and we have no way to get it out. What we want to do is this. If someone is outbid, they should receive a refund for their previous bid.