From c2c16f5910379361d2065c8ab2c65b436742d8dc Mon Sep 17 00:00:00 2001 From: kswim Date: Sun, 30 Jul 2017 07:45:12 -0700 Subject: [PATCH] voting example for veiwing blockchain procedure basis of blockcain and voting web --- Voting.sol | 47 ++++++++++++++++ index.html | 159 +++++++++++++---------------------------------------- index.js | 23 ++++++++ 3 files changed, 107 insertions(+), 122 deletions(-) create mode 100644 Voting.sol create mode 100644 index.js diff --git a/Voting.sol b/Voting.sol new file mode 100644 index 0000000..28b527f --- /dev/null +++ b/Voting.sol @@ -0,0 +1,47 @@ +pragma solidity ^0.4.6; +// 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 + */ + function Voting(bytes32[] candidateNames) { + candidateList = candidateNames; + } + + // This function returns the total votes a candidate has received so far + function totalVotesFor(bytes32 candidate) returns (uint8) { + if (validCandidate(candidate) == false) throw; + return votesReceived[candidate]; + } + + // This function increments the vote count for the specified candidate. This + // is equivalent to casting a vote + function voteForCandidate(bytes32 candidate) { + if (validCandidate(candidate) == false) throw; + votesReceived[candidate] += 1; + } + + function validCandidate(bytes32 candidate) returns (bool) { + for(uint i = 0; i < candidateList.length; i++) { + if (candidateList[i] == candidate) { + return true; + } + } + return false; + } +} diff --git a/index.html b/index.html index b05205f..d3b5ecc 100644 --- a/index.html +++ b/index.html @@ -1,126 +1,41 @@ - - - - COUNTER - - - - - + + + + Hello World DApp + + - -

- //여기 넣어야함 - 사용자명:   - 패스워드:   - -

- - - - - - -
namenumber of counter
-
- -
- + +

A Simple Hello World Voting Application

+
+ + + + + + + + + + + + + + + + + + + + + +
CandidateVotes
Donggil
SSONG
LEE
+
+ + Vote + + + diff --git a/index.js b/index.js new file mode 100644 index 0000000..b324c2c --- /dev/null +++ b/index.js @@ -0,0 +1,23 @@ +web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); +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('0xc0b565f68449f7ecc56fad4bbb5e157844a09898'); +candidates = {"Donggil": "candidate-1", "SSONG": "candidate-2", "LEE": "candidate-3"} + +function voteForCandidate(candidate) { + candidateName = $("#candidate").val(); + contractInstance.voteForCandidate(candidateName, {from: web3.eth.accounts[0]}, function() { + let div_id = candidates[candidateName]; + $("#" + div_id).html(contractInstance.totalVotesFor.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); + } +});