-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoteJS.js
425 lines (400 loc) · 13.2 KB
/
voteJS.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//This function connects web3 to the Ropsten Infura node and without it the user will be notified
window.onload = function () {
const web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/v3/f71e6287d414475b8dd8063b0d08c096"));
if (typeof web3 === 'undefined') {
document.getElementById('metamask').innerHTML = 'No web3 detected, so need <a href="https://metamask.io/">MetaMask</a> browser plugin to run this example';
}
}
//This is the ABI from the contract.
let abi =
[
{
"constant": false,
"inputs": [
{
"internalType": "string",
"name": "_fName",
"type": "string"
},
{
"internalType": "string",
"name": "_lName",
"type": "string"
},
{
"internalType": "string",
"name": "_major",
"type": "string"
}
],
"name": "addCandidate",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "apprVoters",
"outputs": [
{
"internalType": "bool",
"name": "allowVote",
"type": "bool"
},
{
"internalType": "bool",
"name": "voted",
"type": "bool"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"internalType": "address",
"name": "_voter",
"type": "address"
}
],
"name": "approverVoter",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "candidates",
"outputs": [
{
"internalType": "uint256",
"name": "id",
"type": "uint256"
},
{
"internalType": "string",
"name": "fName",
"type": "string"
},
{
"internalType": "string",
"name": "lName",
"type": "string"
},
{
"internalType": "string",
"name": "major",
"type": "string"
},
{
"internalType": "uint256",
"name": "voteCount",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "candidatesCount",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "haveVoted",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"internalType": "uint256",
"name": "candidatesCount",
"type": "uint256"
},
{
"internalType": "string",
"name": "_ufName",
"type": "string"
},
{
"internalType": "string",
"name": "_ulName",
"type": "string"
},
{
"internalType": "string",
"name": "_umajor",
"type": "string"
}
],
"name": "updateCandidate",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "voteAprover",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"internalType": "uint256",
"name": "_candidateId",
"type": "uint256"
}
],
"name": "voteFor",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
];
//Contract address which is on the Ropsten Network
const contractaddress = '0xb62811dd541c04E514D9C428eAe18Fb789295D77';
let numOfCandidates = 2;
//This function is to create the supply of tokens which is only allowed by contract creator
function addCandidate() {
try{
//The variable that connects the abi to the contract address.
let contract = web3.eth.contract(abi).at(contractaddress);
//The variables that retrieve the values from the users.
var fName = document.getElementById("fName").value;
var lName = document.getElementById("lName").value;
var major = document.getElementById("major").value;
//Main part of the function. Calls the method from contract and supplies inputs.
contract.addCandidate(fName, lName, major, {from: web3.eth.accounts[0]}, (error, txHash) => {
//If there is no return value display error
if(!txHash){
window.alert("Oops! Something went wrong!");
}
//If there is a return value display success message.
if(txHash){
window.alert("You just added " + fName + " " + lName + " to candidates.");
console.log(txHash);
numOfCandidates++;
}
});
}
catch(err){
window.alert(err);
}
}
//This function is to create the supply of tokens which is only allowed by contract creator
function updateCandidate() {
try{
//The variable that connects the abi to the contract address.
let contract = web3.eth.contract(abi).at(contractaddress);
//The variables that retrieve the values from the users.
var candidateID = document.getElementById("uCandID").value;
var fName = document.getElementById("ufName").value;
var lName = document.getElementById("ulName").value;
var major = document.getElementById("umajor").value;
//Main part of the function. Calls the method from contract and supplies inputs.
contract.updateCandidate(candidateID, fName, lName, major, {from: web3.eth.accounts[0]}, (error, txHash) => {
//If there is no return value display error
if(!txHash){
window.alert("Oops! Something went wrong!");
}
//If there is a return value display success message.
if(txHash){
window.alert("You just updated " + fName + " " + lName + " to the candidates.");
console.log(txHash);
}
});
}
catch(err){
window.alert(err);
}
}
//This function is to transfer tokens from the default account to the specified account.
function approveVoter() {
try{
//The variable that connects the abi to the contract address.
let contract = web3.eth.contract(abi).at(contractaddress);
//The variables that retrieve the values from the users.
let voterAddress = document.getElementById("voterReceiver").value;
//Main part of the function. Calls the method from contract and supplies inputs.
contract.approverVoter(voterAddress, (error, txHash) => {
//If there is no return value display error
if(!txHash){
window.alert("Oops! Something went wrong!");
}
//If there is a return value display success message.
if(txHash){
window.alert("You approved " + voterAddress + " for voting!");
console.log(txHash);
}
});
}
catch(err) {
window.alert(err);
}
}
//This function allows the user to transfer tokens from one specified account to another.
function voteFor() {
try {
//The variable that connects the abi to the contract address.
let contract = web3.eth.contract(abi).at(contractaddress);
//The variables that retrieve the values from the users.
let candidateID = document.getElementById("candidateVoteID").value;
//Main part of the function. Calls the method from contract and supplies inputs.
contract.voteFor(candidateID, function (error, txHash){
//If there is no return value display error
if(!txHash){
window.alert("Oops! Something went wrong!");
}
//If there is a return value display success message.
if(txHash){
window.alert("You voted for candiate number " + candidateID + "! \n Thank you for your vote!");
console.log(txHash);
}
});
}
catch(err) {
window.alert(err);
}
}
//This function function retrieves the balance of the specified user.
function checkCandidates() {
try {
//The variable that connects the abi to the contract address.
let contract = web3.eth.contract(abi).at(contractaddress);
//The variables that retrieve the values from the users.
let candidateID = document.getElementById("candidateID").value;
contract.candidates(candidateID, function (err, candidate) {
//If there is no return value display error
if (!candidate) {
window.alert("Oops! Something went wrong!");
}
//If there is a return value display success message.
if (candidate) {
var id = candidate[0];
var fName = candidate[1];
var lName = candidate[2];
var major = candidate[3];
var votes = candidate[4];
window.alert(fName + " " + lName + " has " + votes + " votes!");
}
});
}
catch(err) {
window.alert(err);
}
}
//This function function retrieves the balance of the specified user.
function showCandidates() {
try {
//The variable that connects the abi to the contract address.
let contract = web3.eth.contract(abi).at(contractaddress);
//The variables that retrieve the values from the users.
document.getElementById('candidateHead').innerHTML = '<tr><th>Name: </th><th>Candidate Id: </th><th>Number of Votes: </th></tr>';
let idVal = 1;
var candidatesResults = document.getElementById('candidateHead');
for(var i = 0; i <= numOfCandidates; i++){
contract.candidates(idVal, function (err, candidate) {
//If there is a return value display success message.
if (candidate) {
var id = candidate[0];
var fName = candidate[1];
var lName = candidate[2];
var votes = candidate[4];
$(candidatesResults).append('<tr><td>' + fName + ' ' + lName + '</td><td>' + id + '</td><td>' + votes + '</td></tr>');
}
});
idVal++;
}
}
catch(err) {
window.alert(err);
}
}
//This function function retrieves the balance of the specified user.
function apprVoters() {
try {
//The variable that connects the abi to the contract address.
let contract = web3.eth.contract(abi).at(contractaddress);
let addressAppr = document.getElementById("addressApproval").value;
//Main part of the function. Calls the method from contract and supplies inputs.
contract.apprVoters(addressAppr, function (err, approval) {
//If there is no return value display error
if (!approval) {
window.alert("Oops! Something went wrong!");
}
//If there is a return value display success message.
if (approval) {
let ableVote = approval[0];
let hasVoted = approval[1];
window.alert(" Allowed to vote: " + ableVote + "!\n" + "Has voted: " + hasVoted + "!" );
}
});
}
catch(err) {
window.alert(err);
}
}
function getInformation() {
window.alert(" Name: CSU-Pueblo Token \n Symbol: WolfieToken \n Version: Token v1.0");
}