GoVertical presents

Blockchain Startup Ideation Workshop

Hosted by TiE Seattle & Madrona Venture Labs

Ethereum Voting App

Goals

  1. Create a Smart Contract with the Solidity IDE
  2. Deploy the contract to a private blockchain network
  3. Build a client side app that interacts with the contract using the Web3 library

Create Your Smart Contract

You are going to build a voting app inspired by Mahesh Murthy’s excellent blog post Full Stack Hello World Voting Ethereum Dapp.

The app is super simple. It initializes a set of candidates, lets anyone vote for the candidates and displays the vote totals received by each candidate.

We are going to use the Solidity IDE to write and compile our Ethereum contract. If this is your first time using Solidity I recommend you first read the post on Getting Started with Ethereum.

  1. Open Solidity and create a new file called Voting.sol

  2. Paste in the following code:

The Voting contract has three main functions. There is a function to get the list of candidates, a function to vote for a candidate, and a function that returns the number of votes a candidate has received.

Deploy Your Smart Contract

To deploy the contract to your local blockchain make sure you are running a node with the correct RPC flags.

  1. Start a node with the correct flags in the Geth CLI

    geth --datadir="$ethereum_home/chain1" --ipcdisable --port 30301 --rpc --rpcport 8545 --rpccorsdomain "*" --networkid 15 console 2>> console.log

  2. Unlock an account in the Geth CLI

    personal.unlockAccount(eth.accounts[0])

  3. In Solidity under the Run tab change the “Environment” to “Web3 Provider”. A prompt will ask you for the Web3 Provider endpoint. Type http://localhost:8545.  Note that this matches the geth parameter (--rpcport 8545) from above.

  4. We are now ready to deploy the Smart Contract to the blockchain in the form of a transaction.  In the input field next to the Create button, enter an array of candidate names like ["Rama", "Nick", "Jose"] and click Create.  You should see a message in Solidity that the transaction is waiting to be mined.

  5. To mine the transaction go to the Geth CLI and type:

    miner.start()

  6. In Solidity wait until you see the message change from “transaction is waiting to be mined” to a record of the transaction. You have now deployed the Smart Contract. The deployed contract has an address (location on blockchain) and an interface, which allows you to interact with it.

  7. Copy the interface and the address to a text file. You will need them later.
    To copy the contract interface go to the Compile tab and click Details. This will open a popup, which will have a copy button for the interface.

    To copy the contract address click on the copy button next to the contract name.

  8. You can now stop mining. Go to the Geth CLI and type:

    miner.stop()

Interact With Your Smart Contract

Now that the Smart Contract has been deployed to your blockchain, you can interact with it either using the Solidity IDE, the Geth command line, or a web app using the Web3 library. You are going to interact with your Smart Contract using the Web3 library.

  1. In order to enable your node to communicate with the Web3 library, you need to exit your node and restart it using the correct Web3 rpcapi flags:

    geth --datadir="$ethereum_home/chain1" --ipcdisable --port 30301 --rpc --rpcport 8545 --rpccorsdomain "*" --rpcapi eth,web3,personal --networkid 15 console 2>> console.log

  2. Create an html file. The file has a table, which we will populate with candidate names and a form to submit votes for candidates. Copy the following code:

  3. Create a JS file where we will invoke the voting commands. Copy the following code:

  4. Update the index.js file with the your contract’s interface and address, which you copied earlier, and change the password that unlocks your account.

  5. Open index.html. You should see the following:

  6. If you can enter the candidate name in the text box and vote and see the vote count increment, you have successfully created your first application!

You can find all the code for the app here: https://github.com/madrona-labs/voting

Questions? Send us a note!

Ⓒ 2017 TiE Seattle and Madrona Venture Labs. Contact us