-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
161 lines (155 loc) · 4.99 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//Web3 = require('web3');
//web3 = new Web3(new Web3.providers.HttpProvider("http://192.168.91.175: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);
// Colocar o numero de contrato a cada novo processo
//if (typeof web3 !== 'undefined') { console.log('web3 undefined')}
var abi = [
{
"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"
}
]
var contractAdress = '0xc8193239a4cd1225fd7630d68e22cbe3d2ed606a';
var myAbi = web3.eth.contract(abi)
var myFunction = myAbi.at(contractAdress)
candidates = {"Germano": "candidato-1", "Carla": "candidato-2", "Jose": "candidato-3"}
function voteForCandidate() {
/*candidateName = $("#candidate").val();
contractInstance.voteForCandidate(candidateName, {from: web3.eth.accounts[0]}, function() {
let div_id = candidates[candidateName];
$("#" + div_id).html(contractInstance.totalVotesFor.call(candidateName).toString());
});*/ // THIS IS THE OLD WAY, CONNECTING TO GANACHE-CLI
candidateName = $("#candidate").val();
var candidateBytes32 = getBytes32(candidateName);
console.log('votando no candidato ' + candidateName + ' com endereco '+ candidateBytes32);
myFunction.voteForCandidate(candidateBytes32, {from: web3.eth.accounts[0], gas: 4000000}, function(err,
result){
let div_id = candidates[candidateName];
//$("#" + div_id).html(contractInstance.totalVotesFor.call(candidateName).toString());
updateCandidate(candidateBytes32, div_id)
if(err){console.log(err);}
else{console.log(result);}
})
}
$(document).ready(function() {
candidateNames = Object.keys(candidates);
for (var i = 0; i < candidateNames.length; i++) {
let name = candidateNames[i];
var candidateBytes32 = getBytes32(name);
updateCandidate(candidateBytes32, candidates[name]); //endereco e o id
}
});
function updateCandidate(candidateBytes32, htmlID){ //endereco e o id
myFunction.totalVotesFor.call(candidateBytes32,function(err, result){
if(err){console.log(err)}
else{
$('#' + htmlID).html(result.toString());
console.log('colocando o valor ' + result.toString() + ' no '+ htmlID)
}
})
}
function getBytes32(name){
var candidateBytes32;
if(name === 'Germano') candidateBytes32 = '0x63616e6469646174653100000000000000000000000000000000000000000000'
else if(name === 'Carla') candidateBytes32 = '0x6332000000000000000000000000000000000000000000000000000000000000'
else if (name === 'Jose') candidateBytes32 = '0x6333000000000000000000000000000000000000000000000000000000000000'
return candidateBytes32;
}