Million Ether Page Essential Smart Contract
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:03] Let's build the essential contract for the Million Ether page. First, let's make a place for it.
[00:04 - 00:18] Then I'm going to edit contracts, millionetherpage.soul. We're going to be storing our pixels in a 1000x1000 grid.
[00:19 - 00:35] 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.
[00:36 - 00:44] 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.
[00:45 - 00:59] 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.
[01:00 - 01:08] 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.
[01:09 - 01:14] When this function is called, it will update the pixel at the appropriate location. Now, this contract is simple.
[01:15 - 01:20] We're storing a million pixels in our contract with no restrictions. This is just the first version.
[01:21 - 01:35] 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.
[01:36 - 01:52] 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.
[01:53 - 02:01] 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.
[02:02 - 02:06] 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.
[02:07 - 02:11] We'll also need a couple of accounts that have mined some ether locally. Let's start up Geth in the console.
[02:12 - 02:18] You can find this command in the notes. The first thing we need to do is unlock our account.
[02:19 - 02:29] 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.
[02:30 - 02:36] 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.
[02:37 - 02:49] 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.
[02:50 - 02:59] Notice that we have our contract address via the contract key here. Let's save that.
[03:00 - 03:07] 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.
[03:08 - 03:20] 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.
[03:21 - 03:28] Again, copy this, and flipping back over to Geth, we say var ABI equals Jason. parse, and then paste it in.
[03:29 - 03:40] 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.
[03:41 - 04:03] 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.
[04:04 - 04:17] 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.
[04:18 - 04:27] It worked, great. Of course, this contract is a bit silly, anyone can overwrite any pixel essentially for free.
[04:28 - 04:28] Let's work on making this better.