-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
86 lines (72 loc) · 3.17 KB
/
app.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
// Import the page's CSS. Webpack will know what to do with it.
import "../stylesheets/app.css";
// Import libraries we need.
import { default as Web3} from 'web3';
import { default as contract } from 'truffle-contract'
// Import our contract artifacts and turn them into usable abstractions.
import voting_artifacts from '../../build/contracts/Voting.json'
// Voting is our usable abstraction, which we'll use through the code below.
var Voting = contract(voting_artifacts);
// The following code is simple to show off interacting with your contracts.
// As your needs grow you will likely need to change its form and structure.
// For application bootstrapping, check out window.addEventListener below.
var accounts;
var account;
var candidates = {"Rama": "candidate-1", "Nick": "candidate-2", "Jose": "candidate-3"}
window.App = {
start: function() {
var self = this;
// Bootstrap the MetaCoin abstraction for Use.
Voting.setProvider(web3.currentProvider);
// Get the initial account balance so it can be displayed.
web3.eth.getAccounts(function(err, accs) {
if (err != null) {
alert("There was an error fetching your accounts.");
return;
}
if (accs.length == 0) {
alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly.");
return;
}
accounts = accs;
account = accounts[0];
});
// Load all candidates and their votes
// Vote for a candidate
self.loadCandidatesAndVotes();
},
loadCandidatesAndVotes: function() {
var candidateNames = object.keys(candidates);
for(var i=0; i<candidateNames.length; i++) {
let name = candidateNames[i];
Voting.deployed().then(function(f) {
f.totalVotesFor.call(name).then(function(f) {
$("#" + candidates[name]).html(f.toNumber());
})
})
}
},
voteForCondidate: function() {
var candidateName = $("#candidate").val();
Voting.deployed().then(function(i) {
i.voteForCondidate(candidateName, {from: web3.eth.accounts[0]}).then(function(f) {
i.totalVotesFor.call(candidateName).then(function(f){
$("#" + candidates[candidateName]).html(f.toNumber());
})
});
})
}
};
window.addEventListener('load', function() {
// Checking if Web3 has been injected by the browser (Mist/MetaMask)
if (typeof web3 !== 'undefined') {
console.warn("Using web3 detected from external source. If you find that your accounts don't appear or you have 0 MetaCoin, ensure you've configured that source properly. If using MetaMask, see the following link. Feel free to delete this warning. :) http://truffleframework.com/tutorials/truffle-and-metamask")
// Use Mist/MetaMask's provider
window.web3 = new Web3(web3.currentProvider);
} else {
console.warn("No web3 detected. Falling back to http://127.0.0.1:9545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask");
// fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
window.web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545"));
}
App.start();
});