We can read an image, so now let’s color each pixel on the blockchain.
What we’re going to do is iterate over each pixel and send a transaction for each pixel we want to color. Of course, this can be a lot of transactions if the image is large. But the idea is that someone is purchasing a scarce pixel, for a long period of time, so it’s not that big of a deal if it takes a while to buy the whole image.
Connecting to Web3
The first thing we need to do is connect to our web3 node:
const Web3 = require('web3'); const web3 = new Web3(); web3.setProvider( new web3.providers.HttpProvider('http://localhost:8545') );
Here I’m using the HttpProvider, but it’s worth looking into the
Getting a handle to the contract
Next we need a handle to our contract. We do this, like we have before, by loading the ABI and looking up the contract’s deployed address.
const abi = require('../src/MillionEtherPage.abi.json'); const mepAddress = '0x23b51f380a0d8cc3ec9dd7b1b7ff13457c8b4360'; const mep = new web3.eth.Contract(abi, mepAddress);
Because we’re sending transactions, we’ll also need the sending account address. Normally, you’d configure this as an environment variable or command-line argument, but we can hardcode it for now.
const fromAccount = '0xa91aaf38a9020d893e2f94e42f8060b7ee70026c';
Now that we have the contract, when we loop over each pixel, instead of logging it, we want to call the
colorPixel method on the contract to send a transaction: