diff --git a/.idea/dapp-elections.iml b/.idea/dapp-elections.iml new file mode 100644 index 0000000..c956989 --- /dev/null +++ b/.idea/dapp-elections.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..ae8627a --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..8423510 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,241 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + +Vote + + + + + diff --git a/index.js b/index.js new file mode 100644 index 0000000..2dcde2a --- /dev/null +++ b/index.js @@ -0,0 +1,24 @@ +web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:3000")); +abi = JSON.parse('[{"constant":false,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"totalVotesFor","outputs":[{"name":"","type":"uint8"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"validCandidate","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"bytes32"}],"name":"votesReceived","outputs":[{"name":"","type":"uint8"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"x","type":"bytes32"}],"name":"bytes32ToString","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"candidateList","outputs":[{"name":"","type":"bytes32"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"voteForCandidate","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"contractOwner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"inputs":[{"name":"candidateNames","type":"bytes32[]"}],"payable":false,"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('0x4a9c1d265d06d47e8f7b03ffa234a918ccf622'); +candidates = {"Trump": "candidate-1", "Hilary": "candidate-2"}; + +function voteForCandidate() { + candidateName = $("#candidate").val(); + contractInstance.voteForCandidate(candidateName, {from: web3.eth.accounts[0]}, function() { + let div_id = candidates[candidateName]; + $("#" + div_id).html(contractInstance.totalVotesForCandidate.call(candidateName).toString()); + }); +} + +$(document).ready(function() { + 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); + } +}); diff --git a/voting.sol b/voting.sol new file mode 100644 index 0000000..edc479b --- /dev/null +++ b/voting.sol @@ -0,0 +1,25 @@ +pragma solidity ^0.4.11; + +contract Voting{ + mapping (bytes32 => uint8) public totalVotes; + bytes32[] public candidatesList; + + function votingList(bytes32[] candidatesNames){ + candidatesList=candidatesNames; + } + function totalVotesForCandidate(bytes32 candidate) returns (uint8){ + return totalVotes[candidate]; + } + function hilary(bytes32 candidate){ + if(validCandidate(candidate)==false) throw; + totalVotes[candidate]+=1; + } + function validCandidate(bytes32 candidate) returns (boolen){ + for(uint i =0;i