Created
May 15, 2018 09:00
-
-
Save toanalien/90a2e017a5d3603e7de007623f3d226d to your computer and use it in GitHub Desktop.
web interaction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
contractInstance = VotingContract.at('0x329f5c190380ebcf640a90d06eb1db2d68503a53'); | |
candidates = {"Rama": "candidate-1", "Nick": "candidate-2", "Jose": "candidate-3"} | |
function voteForCandidate(candidate) { | |
candidateName = $("#candidate").val(); | |
try { | |
contractInstance.voteForCandidate(candidateName, {from: web3.eth.accounts[0]}, function() { | |
let div_id = candidates[candidateName]; | |
$("#"+div_id).html(contractInstance.totalVotesFor.call(candidateName).toString()); | |
}); | |
} catch (err) { | |
} | |
} | |
$(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); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment