Skip to content
Snippets Groups Projects
Commit eb7dce85 authored by Miguel Braidot's avatar Miguel Braidot
Browse files

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

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