-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
30 lines (26 loc) · 1.04 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
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
asciiToHex = Web3.utils.asciiToHex;
contractInstance = new web3.eth.Contract(ABI_DEFINITION, CONTRACT_ADDRESS);
candidates = {"Rama": "candidate-1", "Nick": "candidate-2", "Jose": "candidate-3"}
function voteForCandidate() {
candidateName = $("#candidate").val();
web3.eth.getAccounts()
.then((accounts) => {
return contractInstance.methods.voteForCandidate(asciiToHex(candidateName)).send({from: accounts[0]});
})
.then(() => {
return contractInstance.methods.totalVotesSoFar(asciiToHex(candidateName)).call();
})
.then((voteCount) => {
const div_id = candidates[candidateName];
$("#" + div_id).html(voteCount);
});
}
$(document).ready(function() {
Object.keys(candidates).forEach((name) => {
contractInstance.methods.totalVotesSoFar(asciiToHex(name)).call()
.then((val) => {
$("#" + candidates[name]).html(val);
});
});
});