Commit eb7dce85 authored by Miguel Braidot's avatar Miguel Braidot

Formulario de alta de votaciones ahora genera una instancia del SC Ballot

parent 09adac96
......@@ -5,6 +5,7 @@ var mainaddr = {
'47525974938': '0xe3e08934b6fa0b68972c08e0f545cee31ed039c6',
'5777': '0xC4d36AaA89b75357bd68f2E526f27B277Bd43c23'
};
var ballot_keccak3 = '0xca753fe72f9141d87c626c29a766381187dbf6fd350319cd6ba8c62b00040154';
var event;
window.addEventListener('load', init1 );
......@@ -191,16 +192,15 @@ async function ver_votacion( addr )
async function ver_votacion2( addr, code )
{
popup( undefined );
console.log( code );
console.log( 'Contract code: ', 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' )
if ( keccak3 != '0xca753fe72f9141d87c626c29a766381187dbf6fd350319cd6ba8c62b00040154' )
console.log( 'keccak3: ', keccak3 );
if ( keccak3 != ballot_keccak3 )
{
popup( 'There is no recognised Ballot contract at that address.' );
return;
......
......@@ -47,7 +47,7 @@
<div class="form-group form-group-lg col-sm-12">
<label for="input_address" class="form-required">Dirección de la votación</label>
<input class="form-control" type="text" id="input_address" name="input_address" value="" size="40"
minlength="40" maxlength="40" required="required" aria-required="true" placeholder="40 caracteres hex (puede empezar con 0x)">
minlength="42" maxlength="42" required="required" aria-required="true" placeholder="42 caracteres hex (debe empezar con 0x)">
</div>
<div class="form-group col-sm-6 col-sm-push-6">
<button class="btn-primary btn btn-block" type="submit" id="submit">Ver votación</button>
......@@ -117,15 +117,15 @@
messages: {
input_address: {
required: "Ingresá una dirección válida",
minlength: "La dirección debe tener 40 caractéres incluidos el 0x.",
maxlength: "La dirección debe tener 40 caractéres incluidos el 0x."
minlength: "La dirección debe tener 42 caractéres incluidos el 0x.",
maxlength: "La dirección debe tener 42 caractéres incluidos el 0x."
}
},
rules: {
input_address: {
required: true,
minlength: 40,
maxlength: 40
minlength: 42,
maxlength: 42
}
}
......
......@@ -292,55 +292,62 @@ var abiBallot = [
// $ solc --abi src/Ballot.sol | tail -1 | jq
var abiNew = [
{
"constant": false,
"inputs": [
{
"name": "title",
"type": "string"
},
{
"name": "voteStarts",
"type": "uint256"
},
{
"name": "voteBefore",
"type": "uint256"
},
{
"name": "percentOfRegisteredVotersReqToBeValid",
"type": "uint256"
},
{
"name": "percentOfVotesCastToWin",
"type": "uint256"
},
{
"name": "countNonvotesAsBlanks",
"type": "bool"
},
{
"name": "maxVotesPerVoter",
"type": "uint256"
},
{
"name": "maxVotesPerProposal",
"type": "uint256"
},
{
"name": "proposalNames",
"type": "bytes32[]"
}
],
"name": "newBallot",
"outputs": [
{
"name": "newaddr",
"type": "address"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
{
"constant": false,
"inputs": [
{
"name": "title",
"type": "string"
},
{
"name": "voteStarts",
"type": "uint256"
},
{
"name": "voteBefore",
"type": "uint256"
},
{
"name": "percentOfRegisteredVotersReqToBeValid",
"type": "uint256"
},
{
"name": "percentOfVotesCastToWin",
"type": "uint256"
},
{
"name": "countNonvotesAsBlanks",
"type": "bool"
},
{
"name": "maxVotesPerVoter",
"type": "uint256"
},
{
"name": "maxVotesPerProposal",
"type": "uint256"
},
{
"name": "proposalNames",
"type": "bytes32[]"
}
],
"name": "newBallot",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"name": "contrato",
"type": "address"
}
],
"name": "contratocreado",
"type": "event"
}
];
......@@ -14,12 +14,16 @@ var netnames = {
"4": 'Rinkeby test',
'42': 'Kovan test',
'5445': 'BFA test',
'47525974938': 'Blockchain Federal Argentina'
'47525974938': 'Blockchain Federal Argentina',
'5777': 'Ganache - Local'
};
var mainaddr = {
'5445': '0xe5bf7c3e8aa529e42fbd99428137b68db75d85f9',
'47525974938': '0xe3e08934b6fa0b68972c08e0f545cee31ed039c6'
'47525974938': '0xe3e08934b6fa0b68972c08e0f545cee31ed039c6',
'5777': '0x68bc96dd287Fd7AdAD480b487B16ac5AE7D59B3A'
};
var ballot_keccak3 = '0xfd956d3ac857d157367d1a1bfe0e0fddee572dd24aebe34df32c430543377bb0';
var contract_event;
window.addEventListener('load', page_loaded);
......@@ -105,6 +109,13 @@ async function page_loaded() {
ask_about_contract_address();
//addchild( rootdiv, 'hr', undefined, {'width':'20%'} );
web3.eth.getAccounts(function (error, accounts) {
if (error) return console.error(error)
console.log("Accounts: " + accounts);
window.web3.eth.defaultAccount = accounts[0];
});
addchild(rootdiv, 'div', undefined, {
'id': 'div_create_ballot',
'class': 'hidden'
......@@ -371,32 +382,42 @@ async function ask_for_ballot_details() {
});
}
async function nueva_votacion() {
async function nueva_votacion(arguments,proposals) {
if (mainaddr[netid] == undefined)
return;
var f = document.forms['form_create_new_ballot'];
var arguments = [];
arguments.push(f['title'].value);
arguments.push(f['voteStarts'].value);
arguments.push(f['voteBefore'].value);
arguments.push(f['percentOfRegisteredVotersReqToBeValid'].value);
arguments.push(f['percentOfVotesCastToWin'].value);
arguments.push(f['countNonvotesAsBlanks'].value);
arguments.push(f['maxVotesPerVoter'].value);
arguments.push(f['maxVotesPerProposal'].value);
//var f = document.forms['form_create_new_ballot'];
/*var arguments = [];
arguments.push(document.getElementById('_title').value);
arguments.push(document.getElementById('start_date').value);
arguments.push(document.getElementById('voteBefore').value);
arguments.push(document.getElementById('percentOfRegisteredVotersReqToBeValid').value);
arguments.push(document.getElementById('percentOfVotesCastToWin').value);
arguments.push(document.getElementById('countNonvotesAsBlanks').value);
arguments.push(document.getElementById('maxVotesPerVoter').value);
arguments.push(document.getElementById('maxVotesPerProposal').value);
var p;
var proposals = [];
for (var i = 1; p = f['proposal' + i]; i++)
proposals.push(p.value);
var newdiv = document.getElementById('newcontract');
var localstatus = addchild(newdiv, 'p', 'Trying to access your account.');
access_accounts();
localstatus.innerHTML = 'Creating new voting contract.';
for (var i = 1; p = document.getElementById('proposal' + i); i++){
proposals.push(web3.fromAscii(p.value));
}*/
for (var i = 0; i < proposals.length; i++){
proposals[i] = web3.fromAscii(proposals[i]);
}
//var newdiv = document.getElementById('newcontract');
//var localstatus = addchild(newdiv, 'p', 'Trying to access your account.');
alertarLoading('Trying to access your account.');
await access_accounts();
//localstatus.innerHTML = 'Creating new voting contract.';
alertarLoading('Creating new voting contract.');
var contract = setup_existing_contract(abiNew, mainaddr[netid]);
if (!contract)
return;
var rcpt;
/*var rcpt;
if (api_version() == 0)
contract.newBallot.call(
arguments[0],
......@@ -411,13 +432,35 @@ async function nueva_votacion() {
gas: 2111000
},
contract_created
);*/
console.log(arguments,proposals);
try
{
contract_event = contract.contratocreado();
contract_event.watch(function(error, result){
if (!error){
console.log(result);
alertarSuccess('Ballot was successfuly created. Here, its address: ' + result.args.contrato); // the contract address
}else{
alertar(error);
}
});
contract.newBallot.sendTransaction(
arguments[0], arguments[1], arguments[2], arguments[3],
arguments[4], arguments[5], arguments[6], arguments[7],
proposals, { gas: 2111000 }, contract_created
);
}
catch (error)
{
alertar(error);
}
}
async function contract_created(err, rcpt) {
console.log(rcpt);
if (!rcpt)
return;
/*if (!rcpt)
return;*/
//ver_votacion( newaddr );
}
......@@ -439,7 +482,7 @@ async function ver_votacion(addr) {
});
}
async function ver_votacion2(addr, code) {
console.log(code);
console.log('Contract code', code);
if (code == '0x') {
alertar('No existe un contrato con esa dirección');
return;
......@@ -447,8 +490,8 @@ async function ver_votacion2(addr, code) {
var keccak3 = window.web3.sha3(code, {
'encoding': 'hex'
});
console.log(keccak3);
if (keccak3 != '0xabcd') {
console.log('keccak3: ', keccak3);
if (keccak3 != ballot_keccak3) {
alertar('No hay una votación en esa dirección.');
return;
}
......
......@@ -33,7 +33,7 @@
<div id="voterow" class="row hide">
<section>
<h1 class="page-header">Nueva Votación</h1>
<form action="">
<form action="" id="form_create_new_ballot">
<div class="alert alert-danger" role="alert" id="errorForm">
<p>Comprobá los siguientes errores del formulario:</p>
<ul></ul>
......@@ -272,11 +272,12 @@
});
function returnDate(d, t) {
return new Date(d + " " + t);
var date = new Date(d + " " + t);
return date.getTime();
}
function sendResults() {
alertarSuccess('La votación ha sido creada en la dirección: 0x4f6as4df65a6536523464f6as');
//alertarSuccess('La votación ha sido creada en la dirección: 0x4f6as4df65a6536523464f6as');
$("#voterow :input").attr("readonly", true);
$("#voterow :input").attr("disabled", true);
$("#voterow .btn").hide();
......@@ -284,8 +285,8 @@
var $title = $("#_title").val();
var $voteStarts = returnDate($("#start_date").val(), $("#start_time").val());
var $voteBefore = returnDate($("#end_date").val(), $("#end_time").val());
var $percentOfRegisteredVotersReqToBeValid = $("#_percentOfRegisteredVotersReqToBeValid").val();
var $percentOfVotesCastToWin = $("#_percentOfVotesCastToWin").val();
var $percentOfRegisteredVotersReqToBeValid = $("#_percentOfRegisteredVotersReqToBeValid").val() * 1000000;
var $percentOfVotesCastToWin = $("#_percentOfVotesCastToWin").val() * 1000000;
var $countNonvotesAsBlanks = ($("input[name='_countNonvotesAsBlanks']:checked").val() == 'true');
var $maxVotesPerVoter = $("#_maxVotesPerVoter").val();
var $maxVotesPerProposal = $("#_maxVotesPerProposal").val();
......@@ -301,8 +302,8 @@
voters.push($(this).val());
});
console.log(arguments,proposalNames,voters);
//console.log(arguments,proposalNames,voters);
nueva_votacion(arguments,proposalNames);
}
$("form").validate({
......@@ -328,6 +329,10 @@
}
},
submitHandler: function (form) {
var body = $("html, body");
body.stop().animate({
scrollTop: 0
}, 200);
// do other things for a valid form
//form.submit();
sendResults();
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment