Subscribe to get access to this video

and the whole library of videos, sample code, and tutorials.

Pixel bidding and structs

Our goal here is to improve this smart contact so that people can bid on each pixel.

These next few videos really get at the heart of smart contracts because we’re going to be writing a contract that holds its own funds and releases them according to our code.

The behavior we want is to:

  • require that colorPixel 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
  • 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 contact
  • if you are outbid by someone else, your funds should be returned to you, and you can try again.

Right now our pixels array holds 1 million bytes3, but if each pixel can have an owner, and a sold price, we need to keep track of this extra information.

The way we can do that is by using a solidity struct. Instead of a pixel being merely a bytes3, 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.

struct Pixel {
  address owner;
  uint soldPrice;
  bytes3 color;
Pixel[1000][1000] public pixels;

When we want to write a new pixel, we will 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 will update the pixel color, owner, and new soldPrice.


Subscribe to get the full text of this lesson

and the entire library of videos, sample code, and tutorials.