Million Ether Page Essential Smart Contract

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

Let's build the essential contract for the Million Ether page. First, let's make a place for it. Then I'm going to edit contracts, millionetherpage.soul. We're going to be storing our pixels in a 1000x1000 grid. The 00 point is the top left, with x increasing to the right, and y increasing towards the bottom. Remember that we're storing each pixel as three bytes. The data type for this insularity is "bytes 3". In our contract, we'll represent this grid initially as a two-dimensional array of bytes 3. This array is going to be stored in our contracts storage, that is, every node on the Ethereum network will hold a copy of these pixels. We'll mark these pixels public, which means that Solidity will automatically create a public getter function to read these values. To set a pixel color, we'll use the function "color pixel". It accepts an unsigned integer for the x and y location, and a bytes 3 for color. When this function is called, it will update the pixel at the appropriate location. Now, this contract is simple. We're storing a million pixels in our contract with no restrictions. This is just the first version. Over the next few videos, we're going to talk about how to bid for and buy pixels, how to emit events when pixels change, and how to give refunds when someone is outbid. While these ideas might sound simple, there are a lot of tricky details that we 're going to work through along the way. But for now, this contract gives us the foundation to test our DAP end-to-end. As a sanity check, let's deploy this contract to our local network and try reading and writing pixels. We'll compile and deploy this on the command line, and then we'll look at some tools to make it easier in the next video. Let's compile our contract using Solk.js and generate the ABI. If you watched the earlier course, we created our own private testnet and Geth. I'm going to be using that testnet for this video too. If you don't have a private testnet, go back and watch it. We'll also need a couple of accounts that have mined some ether locally. Let's start up Geth in the console. You can find this command in the notes. The first thing we need to do is unlock our account. Something you should know is that you can set a time limit for the unlock by using the third parameter. I'm going to unlock my account for 24 hours. If we restart Geth, you'll have to unlock your account again. Now let's copy our contract bin. You can select it to copy and paste, or I'm on a max, so I'll use PB copy. And then flipping back over to the Geth console, we'll type var contract code equals, and then paste the byte code of our contract. And we submit the transaction to create an instance of the contract on the blockchain. Notice that we have our contract address via the contract key here. Let's save that. And remember that your contract address will be different than mine. Of course, before our contract can actually be used, we need to mine the transaction. We'll do that by typing miner.start, wait for it to finish, and then type miner.stop. We also need our contract's ABI, the interface description of our contracts code. Again, copy this, and flipping back over to Geth, we say var ABI equals Jason. parse, and then paste it in. Take a look. Next, we create a JavaScript class for our contract, and then we create an instance of that contract at a particular address. Now we can check the value of a particular pixel, let's say pixel one, two, and this is zero, just like we'd expect. Let's try changing a pixel by sending a transaction, I'll type map dot color pixel dot send transaction, then we put the coordinates one, two, and then a hex code for the bytes three, which will represent the color. Now, this won't mutate our contract state until the transaction has been mined, so let's mine this transaction. Let's stop the miner, and let's check the value of pixel one, two again. It worked, great. Of course, this contract is a bit silly, anyone can overwrite any pixel essentially for free. Let's work on making this better.