Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Voting.sol
Original file line number Diff line number Diff line change
@@ -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;
}
}
159 changes: 37 additions & 122 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,126 +1,41 @@
<!DOCTYPE html>
<html lang = "ko">
<head><meta charset = "UTF-8">
<title>COUNTER</title>
<script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script type = "text/javascript" src = "./js/bignumber.min.js"></script>
<script type = "text/javascript" src = "./js/crypto-js.js"></script>
<script type = "text/javascript" src = "./js/utf8.js"></script>
<script type = "text/javascript" src = ".js/web3.min.js></script>
<script>
var url = "http://10.4.107.212:8545"
var user_name;
var web3 = new Web3();
var provider = new web3.providers.HttpProvider(url)
we3.setProvider(provider);
web3.eth.defaultAccount = web3.eth.accounts[0];

var ABI = [{"constant":false, "inputs":[],"name":"countUp","outputs":[], "type":"function"},{"constant":true, "inputs":[],"name":"getCounterName", "outputs":[{"name":"name", "type":"bytes32"}],"type":"function"},{"constant":true,"inputs":[],"name":"getNunberOfCounter","outputs":[{"name":"number","type":"uint32"}],"type":"function"},{"uts":[{"name":"name","type":"bytes32"}],"type":"constructor"}];
var masterABI = [{"constant":true,"inputs":[],"name":"getCounterAddressList","outputs":[{"name":"counterAddressList","type":"address[]"}], "type":"function"},{"constant":false,"inputs":[{"name":"name","type":"bytes32"}],"name":"addCounter","outputs"[],"type":"function"}];
var masster = web3.eth.contract(masterABI).at();
//여기 넣어야함
var CounterAddressList = master.getCounterAddressList();
function login(){
user_name = $("#userName").val();
var password = $("#password").val();
var JSONdata = createJSONdata("personal_unlockAccount", [user_name, password, 30]);
executeJsonRpc(url, JSONdata, function success(data){
if(data.error == null){
console.log("login success!");
}
else{
console.log("login error");
}
init();
}, function error(data){
console.log("login error");
});
}
function init(){
web3.eth.defaultAccount = user_name;
var talbe = document.getElementById('list');
for(var i = 0; i < CounterAddressList.length; i++){
var Conter = web3.eth.contract(ABI).at(CounterAddressList[i]);
var row = table.insertRow();
var td = row.insertCell(0);
var radioBuuton1 = document.createElement('input');
radioButton1.type = 'radio';
radioButton1.name = 'CounterAddress';
radioButton1.value = CounterAddressList[i];
td.appendChild(radioButton1);
td = row.insertCell(1);
td.innerHTML = web3.toAscii(Counter.getCounterName());
td = row.insertCell(2);
td.innerHTML = Counter.getNumberOfCounter();
}
}
function refresh(){
web3.eth.defaultAccount = user_name;
var table = document.getElementById('list');
for(var i = CounterAddressList.length; i>0;i--){
table.deleteRow(i);
}
init();
}

function countUp(){
web3.eth.defaultAccount = user_name;
var targetAddress;
var CounterList = document.getElementsByName("CounterAddress");
for(i = 0; i < CounterList.length; i++){
if(CounterList[i].checked){
targetAddress = CounterList[i].value;
}
}
var Counter = web3.eth.contract(ABI).at(targetAddress);
Counter.countUp();
}

function createJSONdata(method, params){
var JSONdata = {
"jsonrpc" : "2.0",
"method" : method,
"params" : params
};
return JSONdata;
}

function executeJsonRpc(url_exec, JSONdata, success, error){
$.ajax({
type : 'post',
url : url_exec,
data : JSON.stringify(JSONdata),
contentType : 'application/JSON',
dataType : 'JSON',
scriptCharset : 'utf-8',
success : function(data){
success(data);
},
eeor : function(data){
error(data);
}
});
}
</script>
<CTYPE html>
<html>
<head>
<title>Hello World DApp</title>
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
<link href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' rel='stylesheet' type='text/css'>
</head>
<body>
<p>
//여기 넣어야함
사용자명:&nbsp;<input type = "text" id = "userName" value = " ">&nbsp;
패스워드:&nbsp;<input type = "text" id = "password" value = "testuser2">&nbsp;
<input type = "button" value = "login" onclick = "login();" />
</p>
<table id = "list" border ="1">
<tr>
<th></th>
<th>name</th>
<th>number of counter</th>
</tr>
</table>
<br />
<input type = "button" value = "countUp" onclick = "countUp();"/>
<br />
<input type = "button" value = "refresh" onclick = "refresh();"/>
<body class="container">
<h1>A Simple Hello World Voting Application</h1>
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>Candidate</th>
<th>Votes</th>
</tr>
</thead>
<tbody>
<tr>
<td>Donggil</td>
<td id="candidate-1"></td>
</tr>
<tr>
<td>SSONG</td>
<td id="candidate-2"></td>
</tr>
<tr>
<td>LEE</td>
<td id="candidate-3"></td>
</tr>
</tbody>
</table>
</div>
<input type="text" id="candidate" />
<a href="#" onclick="voteForCandidate()" class="btn btn-primary">Vote</a>
</body>
<script src="https://cdn.rawgit.com/ethereum/web3.js/develop/dist/web3.js"></script>
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
<script src="./index.js"></script>
</html>

23 changes: 23 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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);
}
});