Newer
Older
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
var hasplugin = 0;
var mainaddr = {
'5445': '0xe5bf7c3e8aa529e42fbd99428137b68db75d85f9',
'47525974938': '0xe3e08934b6fa0b68972c08e0f545cee31ed039c6'
};
window.addEventListener('load', init1 );
async function init1()
{
bfa_setup( init2 );
}
async function init2( err, web3 )
{
if ( web3 == undefined )
{
window.alert( err );
return;
}
if ( err == undefined )
hasplugin = 1;
window.web3 = web3;
console.log( 'window.web3.version.api is ' + window.web3.version.api );
status_start();
show_section('startdiv');
update_create_new_ballot();
}
function update_create_new_ballot()
{
// Find our div
var div = document.getElementById('div_create_ballot');
// We get this from status.js
var netid = netinfo['number'];
// if we do not have the address of the mother contract, we will
// not present the user with a choice to create a new form.
if ( mainaddr[netid] == undefined )
div.setAttribute( 'class', 'hidden' );
else
div.removeAttribute( 'class', 'hidden' );
}
function setup_existing_contract( abi, addr)
{
if ( api_version() == 0 )
{
var MyContract = window.web3.eth.contract( abiNew );
contract = MyContract.at( addr );
return contract;
}
window.alert( 'Do not know how to deal with contracts at this API version (' +window.web3.version.api+ ').' );
return;
}
async function ask_for_ballot_details()
{
show_section('newcontract');
}
async function nueva_votacion()
{
// We get this from status.js
var netid = netinfo['number'];
//
if ( mainaddr[netid] == undefined )
return;
var f = document.forms['form_create_new_ballot'];
var arguments = [];
//
var title = f['title'].value;
if ( !title || title == '' )
throw new Error("No title is set");
arguments.push( f['title'] .value );
//
var voteStarts = new Date( f['voteStarts'].value );
voteStarts = new Date( Date.now() + 300000 );
if ( voteStarts == 'Invalid Date' )
throw new Error( voteStarts );
arguments.push( Math.floor(voteStarts.valueOf() / 1000) );
//
var voteBefore = new Date( f['voteBefore'].value );
voteBefore = new Date( Date.now() + 86700000 );
if ( voteBefore == 'Invalid Date' )
throw new Error( voteBefore );
arguments.push( Math.floor(voteBefore.valueOf() / 1000) );
//
arguments.push( f['percentOfRegisteredVotersReqToBeValid'] .value );
arguments.push( f['percentOfVotesCastToWin'] .value );
//
arguments.push( f['countNonvotesAsBlanks'].value == 'Yes' );
//
arguments.push( f['maxVotesPerVoter'] .value );
arguments.push( f['maxVotesPerProposal'] .value );
var proposals = [];
var p;
for ( var i = 1; p = f['proposal'+i]; i++ )
if ( p.value != '' )
proposals.push( p.value );
popup( 'Trying to access your account.' );
setTimeout( nueva_votacion2, 1000, netid, arguments, proposals );
}
async function nueva_votacion2( netid, arguments, proposals )
{
await access_accounts();
popup( 'Creating new voting contract.' );
var contract = setup_existing_contract( abiNew, mainaddr[netid] );
if ( ! contract )
return;
var rcpt;
if ( api_version() == 0 )
{
console.log( contract );
try
{
contract.newBallot(
arguments[0], arguments[1], arguments[2], arguments[3],
arguments[4], arguments[5], arguments[6], arguments[7],
proposals, { gas: 2111000 }, contract_created
);
}
catch (error)
{
popup(error);
setTimeout( popup, 5000, '' );
}
}
}
async function contract_created(err,rcpt)
{
popup( err ); // the actual error, or undefined
console.log( rcpt );
if ( !rcpt )
return;
if( !rcpt.address )
// The hash of the transaction, which deploys the contract
console.log( rcpt.transactionHash );
// check address on the second call (contract deployed)
else
console.log( rcpt.address ); // the contract address
//ver_votacion( newaddr );
}
async function ver_votacion( addr )
{
if ( addr == "" )
addr =
document
.getElementById('input_address')
.value;
popup( 'Loading contract details, if possible.' );
if ( addr && addr.length == 40 )
addr = '0x' + addr;
window.web3.eth.getCode( addr, function(err,code) {
if ( err )
{
popup( 'Unable to get bytecode of contract: ' + err );
return;
}
ver_votacion2( addr, code );
});
}
async function ver_votacion2( addr, code )
{
popup( undefined );
console.log( code );
if ( code == '0x' )
{
popup( 'There is no contract at that address' );
return;
}
var keccak3 = window.web3.sha3( code, {'encoding':'hex'} );
console.log( keccak3 );
if ( keccak3 != '0xabcd' )
{
popup( 'There is no recognised Ballot contract at that address.' );
return;
}
var contract = setup_existing_contract( abiBallot, addr );
if ( !contract )
return;
//var detailpromises;
// make sure we have just a single verdiv
//var verdiv;
//while ( verdiv = document.getElementById('verdiv'))
// verdiv.parent.removeChild( verdiv );
//verdiv = addchild( rootdiv, 'div', undefined, { 'id': 'verdiv' });
// Get headline
//var proposaldiv = addchild( verdiv, 'p', 'Proposal:')
//contract.methods.proposal().call( {} )
//.then(
// function showproposal(result)
// {
// proposaldiv.innerHTML = 'Proposal: ' + result;
// },
// function failproposal(err)
// {
// proposaldiv.innerHTML = 'Failed to get proposal: ' + err
// }
//);
}
async function de_acuerdo_y_firmar()
{
access_accounts();
if ( ! accExposed )
return;
// web3.eth.sendTransaction({/* ... */});
}