Skip to content

Commit

Permalink
Inital Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedalaahagag committed Dec 19, 2017
0 parents commit 1f43782
Show file tree
Hide file tree
Showing 7 changed files with 371 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .idea/dapp-elections.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

241 changes: 241 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions README.ME
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## Decentralized elections app

using the etherum block chain, smart contract , solidity , web3 a simple decentralized voting app

## Dependencies

* ethereumjs-testrpc
* web3
* solc

Install missing dependencies with [npm](https://www.npmjs.com/).

## Usage

Run the following commands to open the node console then deploy your contract to the test chain

```
siraj:~/hello_world_voting$ node
> Web3 = require('web3')
> web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:3000"));
> code = fs.readFileSync('voting.sol').toString()
> solc = require('solc')
> compiled = solc.compile(code)
> abiDefinition = JSON.parse(compiled.contracts[':Voting'].interface)
> VotingContract = web3.eth.contract(abiDefinition)
> byteCode = compiledCode.contracts[':Voting'].bytecode
> deployedContract = VotingContract.new(['Trump','Hilary'],{data: byteCode, from: web3.eth.accounts[0], gas: 4700000})
> deployedContract.address
> contractInstance = VotingContract.at(deployedContract.address)
36 changes: 36 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!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>Elections Application</h1>
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>Candidate</th>
<th>Votes</th>
</tr>
</thead>
<tbody>
<tr>
<td>Trump</td>
<td id="candidate-1"></td>
</tr>
<tr>
<td>Hilary</td>
<td id="candidate-2"></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>
24 changes: 24 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
25 changes: 25 additions & 0 deletions voting.sol
Original file line number Diff line number Diff line change
@@ -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<candidatesList.length;i++){
if(candidates[i]==candidate){
return true;
}
}
return false;
}
}

0 comments on commit 1f43782

Please sign in to comment.