-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
37 lines (29 loc) · 2.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"))
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 = "0x7A7f7Fa2A5cB17d3a8dB350273A5B7757A3390BD";
// update this contract address with your contract address
candidates = {"BJP": "candidate-1", "Congress": "candidate-2", "NOTA": "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);
})
}
});