Pixel bidding and structs
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:14] Our goal here is to improve the smart contract so that people can bid on a pixel with Ether. These next few videos really get at the heart of smart contracts because we're going to be writing a contract that can hold its own funds and releases them according to our code.
[00:15 - 00:25] The behavior we want is to require that color pixel be sent Ether in order to allow coloration. We want to require that the amount sent is higher than the previous amount paid for that pixel.
[00:26 - 00:33] If you try to buy a pixel at too low of a bid, your funds should be returned to you. If you are the highest bid, then your funds are stored in the contract.
[00:34 - 00:41] If you're outbid by someone else, your funds should be returned to you and you can try again. Right now our pixels array holds 1 million bytes three.
[00:42 - 01:30] But if each pixel can have an owner and a sold price, we need to keep track of that extra information. The way we do that is by using a solidity struct. Instead of a pixel being nearly a bytes three, we'll create a struct called pixel, which stores the address of the owner, the sold price, and the color. In this case, our pixels property becomes a 2D array of pixel structs. When we want to write a new pixel, we'll send Ether along with the transaction to the contract. And if the Ether value is greater than the last sold price of that pixel, then we'll update the pixel color, owner, and new sold price. While the final code won't be that long, there are several new ideas here that require some care. Let's tackle them one at a time, starting with payable contracts.