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.
Open Solidity and create a new file called Voting.sol
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.
To deploy the contract to your local blockchain make sure you are running a node with the correct RPC flags.
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
Unlock an account in the Geth CLI
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.
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.
To mine the transaction go to the Geth CLI and type:
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.
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.
You can now stop mining. Go to the Geth CLI and type:
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.
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
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:
Create a JS file where we will invoke the voting commands. Copy the following code:
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.
Open index.html. You should see the following:
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
Ⓒ 2017 TiE Seattle and Madrona Venture Labs. Contact us