-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# DAVE | ||
Decentralized Autonomous Voting Engine | ||
Decentralized Autonomous Voting Engine/Environment |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
pragma solidity ^0.4.18; | ||
// specify compiler version | ||
|
||
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) public { | ||
candidateList = candidateNames; | ||
} | ||
|
||
// This function returns the total votes a candidate has received so far | ||
function totalVotesFor(bytes32 candidate) view public returns (uint8) { | ||
require(validCandidate(candidate)); | ||
return votesReceived[candidate]; | ||
} | ||
|
||
// This function increments the vote count for the specified candidate. This | ||
// is equivalent to casting a vote | ||
function voteForCandidate(bytes32 candidate) public { | ||
require(validCandidate(candidate)); | ||
votesReceived[candidate] += 1; | ||
} | ||
|
||
function validCandidate(bytes32 candidate) view public returns (bool) { | ||
for(uint i = 0; i < candidateList.length; i++) { | ||
if (candidateList[i] == candidate) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<!DOCTYPE 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 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>Rama</td> | ||
<td id="candidate-1"></td> | ||
</tr> | ||
<tr> | ||
<td>Nick</td> | ||
<td id="candidate-2"></td> | ||
</tr> | ||
<tr> | ||
<td>Jose</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> |
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('0xd08b893984b42966581ce311f65128e252bf1e3a'); | ||
candidates = {"Rama": "candidate-1", "Nick": "candidate-2", "Jose": "candidate-3"} | ||
|
||
function voteForCandidate() { | ||
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); | ||
} | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.