Skip to content

Instantly share code, notes, and snippets.

@maheshmurthy
Last active December 7, 2021 21:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save maheshmurthy/f6e96d6b3fff4cd4fa7f892de8a1a1b4 to your computer and use it in GitHub Desktop.
Save maheshmurthy/f6e96d6b3fff4cd4fa7f892de8a1a1b4 to your computer and use it in GitHub Desktop.
Html file to cast and display votes
<!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.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.37/dist/web3.min.js"></script>
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
<script src="./index.js"></script>
</html>
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"))
var account;
web3.eth.getAccounts().then((f) => {
account = f[0];
})
abi = JSON.parse('[{"constant":true,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"totalVotesFor","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"validCandidate","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"bytes32"}],"name":"votesReceived","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"candidateList","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"voteForCandidate","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"candidateNames","type":"bytes32[]"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]')
contract = new web3.eth.Contract(abi);
contract.options.address = "0x71789831d83d4C8325b324eA9B5fFB27525480b5";
// update this contract address with your contract address
candidates = {"Rama": "candidate-1", "Nick": "candidate-2", "Jose": "candidate-3"}
function voteForCandidate(candidate) {
candidateName = $("#candidate").val();
console.log(candidateName);
contract.methods.voteForCandidate(web3.utils.asciiToHex(candidateName)).send({from: account}).then((f) => {
let div_id = candidates[candidateName];
contract.methods.totalVotesFor(web3.utils.asciiToHex(candidateName)).call().then((f) => {
$("#" + div_id).html(f);
})
})
}
$(document).ready(function() {
candidateNames = Object.keys(candidates);
for(var i=0; i<candidateNames.length; i++) {
let name = candidateNames[i];
contract.methods.totalVotesFor(web3.utils.asciiToHex(name)).call().then((f) => {
$("#" + candidates[name]).html(f);
})
}
});
@nandosb
Copy link

nandosb commented Aug 22, 2017

In the code you have:
candidates = {"Rama": "candidate-1", "Nick": "candidate-2", "Jose": "candidate-3"}

I was wondering what would be a way to fetch the list of candidates directly from the blockchain.

Thanks

@maheshmurthy
Copy link
Author

You can see part 3, we do that there :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment