-
Notifications
You must be signed in to change notification settings - Fork 69
/
ballot.html
318 lines (258 loc) · 16.1 KB
/
ballot.html
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
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/milligram/1.2.3/milligram.min.css">
</head>
<body class="container" style="padding-top: 3%;">
<h2><a href="index.html">ethjs</a></h2>
<h4>Ballot</h4>
<hr />
<div id="ballotCreation">
<h4>Add Proposals</h4>
<div id="inputProposals">
<input type="text" id="proposal_0_value" maxLength="64" placeholder="Proposal Name #0" />
<span id="proposal_0_next"></span> <!-- shim for adding -->
</div>
<button id="addProposal">Add Proposal</button>
<hr />
<button id="createBallot">Create Ballot</button>
<div id="ballotCreationResponse">
</div>
</div>
<div id="ballotActive" style="display: none;">
<div class="row">
<div class="column">
<label>Chairperson</label>
<p><span id="chairpersonAddress">0x</span>...</p>
</div>
<div class="column">
<label>Winning Proposal</label>
<p id="winningProposal">None</p>
</div>
<div class="column">
<label>Your Address</label>
<p><span id="accountAddress">0x</span>...</p>
</div>
<div class="column">
<label>Balance</label>
<p><span id="accountBalance">0</span> ether</p>
</div>
</div>
<hr />
<div class="row">
<div class="column column-50">
<h4>Proposals</h4>
<br />
<div id="proposals"></div>
</div>
<div class="column column-40 column-offset-10">
<h4>Manager</h4>
<br />
<label>Add Voter</label>
<input type="text" id="voterAddress" placeholder="0xcc136826fbd5192c10bba496af42906a7e3b91f8">
<button id="giveRightToVote">Give Right to Vote</button>
<hr />
<label>Delegate</label>
<input type="text" id="delegateAddress" placeholder="0xcc136826fbd5192c10bba496af42906a7e3b91f8">
<button id="delegateVote">Delegate My Vote</button>
</div>
</div>
<br />
<div id="ballotActiveResponse" style="padding: 20px; border: 1px solid #9b4dca;"></div>
</div>
<br /><br />
<script type="text/javascript" src="ethereumjs-testrpc.js"></script>
<script type="text/javascript" src="ethjs.js"></script>
<script type="text/javascript">
var eth = new Eth(TestRPC.provider());
var el = function(id){ return document.querySelector(id); };
/*
// uncomment to enable MetaMask support:
if (typeof window.web3 !== 'undefined' && typeof window.web3.currentProvider !== 'undefined') {
eth.setProvider(window.web3.currentProvider);
} else {
eth.setProvider(provider); // set to TestRPC if not available
}
*/
eth.accounts(function(accountsError, accounts){
el('#accountAddress').innerHTML = accounts[0].substr(0, 15);
// get the current account balance
eth.getBalance(accounts[0]).then(function(balance){
el('#accountBalance').innerHTML = Eth.fromWei(balance, 'ether');
});
// setup the bytecode and ABI for the ballot contract
var BallotBytecode = '6060604052346100005760405161099d38038061099d833981016040528080518201919060200150505b600033600060006101000a81548173ffffffffffffffffffffffffffffffffffffffff02191690836c01000000000000000000000000908102040217905550600160016000600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060000181905550600090505b815181101561019057600280548060010182818154818355818115116101245760020281600202836000526020600020918201910161012391905b8082111561011f57600060008201600090556001820160009055506002016100fc565b5090565b5b505050916000526020600020906002020160005b60406040519081016040528086868151811015610000579060200190602002015181526020016000815260200150909190915060008201518160000155602082015181600101555050505b80806001019150506100c1565b5b50505b6107fb806101a26000396000f360606040523615610086576000357c0100000000000000000000000000000000000000000000000000000000900480630121b93f1461008b578063013cf08b146100a85780632e4176cf146100e45780635c19a95c1461011d578063609ff1bd1461013a5780639e7b8d611461015d578063a3ec138d1461017a578063e2ba53f0146101d8575b610000565b34610000576100a660048080359060200190919050506101ff565b005b34610000576100c360048080359060200190919050506102c9565b60405180836000191681526020018281526020019250505060405180910390f35b34610000576100f16102fa565b604051808273ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34610000576101386004808035906020019091905050610320565b005b3461000057610147610619565b6040518082815260200191505060405180910390f35b3461000057610178600480803590602001909190505061069a565b005b3461000057610195600480803590602001909190505061076d565b6040518085815260200184151581526020018373ffffffffffffffffffffffffffffffffffffffff16815260200182815260200194505050505060405180910390f35b34610000576101e56107ca565b604051808260001916815260200191505060405180910390f35b6000600160003373ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002090508060010160009054906101000a900460ff161561024857610000565b60018160010160006101000a81548160ff02191690837f01000000000000000000000000000000000000000000000000000000000000009081020402179055508181600201819055508060000154600283815481101561000057906000526020600020906002020160005b50600101600082825401925050819055505b5050565b600281815481101561000057906000526020600020906002020160005b915090508060000154908060010154905082565b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60006000600160003373ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002091508160010160009054906101000a900460ff161561036b57610000565b5b600073ffffffffffffffffffffffffffffffffffffffff16600160008573ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060010160019054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff161415801561046d57503373ffffffffffffffffffffffffffffffffffffffff16600160008573ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060010160019054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1614155b156104c657600160008473ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060010160019054906101000a900473ffffffffffffffffffffffffffffffffffffffff16925061036c565b3373ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1614156104ff57610000565b60018260010160006101000a81548160ff02191690837f0100000000000000000000000000000000000000000000000000000000000000908102040217905550828260010160016101000a81548173ffffffffffffffffffffffffffffffffffffffff02191690836c01000000000000000000000000908102040217905550600160008473ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002090508060010160009054906101000a900460ff16156105fc57816000015460028260020154815481101561000057906000526020600020906002020160005b5060010160008282540192505081905550610613565b816000015481600001600082825401925050819055505b5b505050565b60006000600060009150600090505b6002805490508110156106945781600282815481101561000057906000526020600020906002020160005b5060010154111561068657600281815481101561000057906000526020600020906002020160005b506001015491508092505b5b8080600101915050610628565b5b505090565b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614158061072d5750600160008273ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060010160009054906101000a900460ff165b1561073757610000565b6001600160008373ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600001819055505b50565b60016020528060005260406000206000915090508060000154908060010160009054906101000a900460ff16908060010160019054906101000a900473ffffffffffffffffffffffffffffffffffffffff16908060020154905084565b600060026107d6610619565b815481101561000057906000526020600020906002020160005b506000015490505b9056';
var BallotABI = [{"constant":false,"inputs":[{"name":"proposal","type":"uint256"}],"name":"vote","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"proposals","outputs":[{"name":"name","type":"bytes32"},{"name":"voteCount","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"chairperson","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"to","type":"address"}],"name":"delegate","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"winningProposal","outputs":[{"name":"winningProposal","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"voter","type":"address"}],"name":"giveRightToVote","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"voters","outputs":[{"name":"weight","type":"uint256"},{"name":"voted","type":"bool"},{"name":"delegate","type":"address"},{"name":"vote","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"winnerName","outputs":[{"name":"winnerName","type":"bytes32"}],"payable":false,"type":"function"},{"inputs":[{"name":"proposalNames","type":"bytes32[]"}],"payable":false,"type":"constructor"}];
var Ballot = eth.contract(BallotABI, BallotBytecode, { from: accounts[0], gas: 3000000 });
var ballotInstance = null;
var inputProposals = 0;
// the add proposal button
el('#addProposal').addEventListener('click', function(){
inputProposals += 1;
el('#proposal_' + (inputProposals - 1) + '_next')
.innerHTML += '<input type="text" id="proposal_'
+ inputProposals
+ '_value" maxLength="64" placeholder="Proposal Name #'
+ inputProposals + '" />'
+ '<span id="proposal_' + inputProposals + '_next"></span>';
});
// the create ballot button
el('#createBallot').addEventListener('click', function(){
var proposalNames = [];
// draw the proposals submitted
for(var proposalID = 0; proposalID <= inputProposals; proposalID++) {
proposalNames.push(Eth.fromUtf8(el('#proposal_' + proposalID + '_value').value));
el('#proposals').innerHTML += '<h5>#' + proposalID + ' '
+ el('#proposal_' + proposalID + '_value').value
+ ' <button data-id="' + proposalID + '" class="float-right voteForProposal">'
+ 'Vote</button><hr /></h5>';
}
// delegate the vote to another person
el('#delegateVote').addEventListener('click', function(){
el('#ballotActiveResponse').innerHTML = 'Delegating vote..';
ballotInstance.delegate(el('#delegateAddress').value)
.then(function(delegateTxHash){
el('#ballotActiveResponse').innerHTML = 'Delegating vote with TxHash: ' + delegateTxHash;
})
.catch(function(delegateError){
el('#ballotActiveResponse').innerHTML = 'There was an error ' + String(delegateError);
});
});
// give another right to vote
el('#giveRightToVote').addEventListener('click', function(){
el('#ballotActiveResponse').innerHTML = 'Giving right to vote...';
ballotInstance.giveRightToVote(el('#voterAddress').value)
.then(function(rightToVoteTxHash){
el('#ballotActiveResponse').innerHTML = 'Giving right to vote with TxHash: ' + rightToVoteTxHash;
})
.catch(function(rightToVoteError){
el('#ballotActiveResponse').innerHTML = 'There was an error ' + String(rightToVoteError);
});
});
// vote for a specific proposal
document.querySelectorAll(".voteForProposal").forEach(function(elVoteFor){
elVoteFor.addEventListener('click', function(evt){
ballotInstance.vote(evt.target.dataset.id, function(voteError, voteResult){
if (voteError) {
el('#ballotActiveResponse').innerHTML = 'There was an error: ' + String(voteError);
}
el('#ballotActiveResponse').innerHTML = 'Voting with tx hash: ' + String(voteResult);
document.querySelectorAll(".voteForProposal").forEach(function(elVoteFor){
elVoteFor.disabled = 'disabled';
});
});
});
});
// create a new ballot contract
Ballot.new(proposalNames, function(ballotError, ballotTxHash){
if (ballotError) {
el('#ballotCreationResponse').innerHTML = 'Hmm.. there was an error'
+ String(ballotError);
}
// poll for new ballot interval
var ballotInterval = setInterval(function(){
eth.getTransactionReceipt(ballotTxHash).then(function(receipt){
if (receipt) {
clearInterval(ballotInterval);
ballotInstance = Ballot.at(receipt.contractAddress);
el('#ballotCreation').style.display = 'none';
el('#ballotActive').style.display = 'block';
// get the chairperson data
ballotInstance.chairperson().then(function(chairAddress){
el('#chairpersonAddress').innerHTML = chairAddress[0].substr(0, 15);
});
// poll for latest winner name
setInterval(function(){
ballotInstance.winnerName().then(function(proposalName){
el('#winningProposal').innerHTML = Eth.toUtf8(proposalName[0]);
});
}, 400);
}
});
}, 400);
});
});
});
</script>
<pre><code>
pragma solidity ^0.4.4;
contract Ballot {
struct Voter {
uint weight; // weight is accumulated by delegation
bool voted; // if true, that person already voted
address delegate; // person delegated to
uint vote; // index of the voted proposal
}
struct Proposal
{
bytes32 name; // short name (up to 32 bytes)
uint voteCount; // number of accumulated votes
}
address public chairperson;
mapping(address => Voter) public voters;
Proposal[] public proposals;
function Ballot(bytes32[] proposalNames) {
chairperson = msg.sender;
voters[chairperson].weight = 1;
for (uint i = 0; i < proposalNames.length; i++) {
proposals.push(Proposal({
name: proposalNames[i],
voteCount: 0
}));
}
}
function giveRightToVote(address voter) {
if (msg.sender != chairperson || voters[voter].voted) {
throw;
}
voters[voter].weight = 1;
}
function delegate(address to) {
Voter sender = voters[msg.sender];
if (sender.voted)
throw;
while (
voters[to].delegate != address(0) &&
voters[to].delegate != msg.sender
) {
to = voters[to].delegate;
}
if (to == msg.sender) {
throw;
}
sender.voted = true;
sender.delegate = to;
Voter delegate = voters[to];
if (delegate.voted) {
proposals[delegate.vote].voteCount += sender.weight;
} else {
delegate.weight += sender.weight;
}
}
function vote(uint proposal) {
Voter sender = voters[msg.sender];
if (sender.voted)
throw;
sender.voted = true;
sender.vote = proposal;
proposals[proposal].voteCount += sender.weight;
}
function winningProposal() constant
returns (uint winningProposal)
{
uint winningVoteCount = 0;
for (uint p = 0; p < proposals.length; p++) {
if (proposals[p].voteCount > winningVoteCount) {
winningVoteCount = proposals[p].voteCount;
winningProposal = p;
}
}
}
function winnerName() constant
returns (bytes32 winnerName)
{
winnerName = proposals[winningProposal()].name;
}
}
</code></pre>
</body>
</html>