pragma solidity ^0.4.25; // We have to specify what version of compiler this code will compile with
contract Voting { /* mapping field below is equivalent to an associative array or hash. The key of the mapping is candidate name stored as type bytes32 and value is an unsigned integer to store the vote count */
mapping (bytes32 => uint8) public votesReceived;
/* Solidity doesn't let you pass in an array of strings in the constructor (yet). We will use an array of bytes32 instead to store the list of candidates */
bytes32[] public candidateList;
/* This is the constructor which will be called once when you deploy the contract to the blockchain. When we deploy the contract, we will pass an array of candidates who will be contesting in the election */ constructor(bytes32[] candidateNames) public { candidateList = candidateNames; }
// This function returns the total votes a candidate has received so far functiontotalVotesFor(bytes32 candidate) viewpublicreturns (uint8) { require(validCandidate(candidate)); return votesReceived[candidate]; }
// This function increments the vote count for the specified candidate. This // is equivalent to casting a vote functionvoteForCandidate(bytes32 candidate) public{ require(validCandidate(candidate)); votesReceived[candidate] += 1; }
functionvalidCandidate(bytes32 candidate) viewpublicreturns (bool) { for(uint i = 0; i < candidateList.length; i++) { if (candidateList[i] == candidate) { returntrue; } } returnfalse; } }
Web3 = require('web3'); web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); abi = JSON.parse('[{"constant":true,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"totalVotesFor","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"validCandidate","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"bytes32"}],"name":"votesReceived","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"candidateList","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"voteForCandidate","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"candidateNames","type":"bytes32[]"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]') VotingContract = web3.eth.contract(abi); // In your nodejs console, execute contractInstance.address to get the address at which the contract is deployed and change the line below to use your deployed address contractInstance = VotingContract.at('0xd018d4ed72e54fc3d66f254dfead8965577403dd'); candidates = {"Rama": "candidate-1", "Nick": "candidate-2", "Jose": "candidate-3"} var account;
$(document).ready(function() { web3.eth.getAccounts(function (err, accs) { if (err != null) { alert('There was an error fetching your accounts.') return }
if (accs.length === 0) { alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly.") return }
account = accs[0]
})
candidateNames = Object.keys(candidates); for (var i = 0; i < candidateNames.length; i++) { let name = candidateNames[i]; let val = contractInstance.totalVotesFor.call(name).toString() $("#" + candidates[name]).html(val); } });