refactor: tx listener and superblock date selection fields (#6)

This commit is contained in:
Cofresi 2016-11-29 21:27:36 -04:00 committed by snogcel
parent 6cae0e6ec5
commit c28f342ec3

View file

@ -27,96 +27,10 @@
$("#time").val(Math.floor((new Date).getTime() / 1000));
var opts = { dateFormat: $.datepicker.ISO_8601 }; // TODO remove
prepareTime();
$("#progresstxt").text(progresstxt);
$.getJSON(apiserversocket + "insight-api-dash/status?q=getinfo", function( data ) {
var timefield = $("#time");
timefield.val(Math.floor((new Date).getTime() / 1000));
console.log('time: ' + timefield.val());
blockheight = data.info.blocks;
console.log('network: ' + gov.network);
var budgetPaymentCycleBlocks = getBudgetPaymentCycleBlocks();
var nextsuperblock = Math.round(blockheight/budgetPaymentCycleBlocks) * budgetPaymentCycleBlocks;
var selectblock = nextsuperblock;
console.log('budgetPaymentCycleBlocks: ' + budgetPaymentCycleBlocks);
console.log('blockheight: ' + blockheight);
console.log('next superblock ' + nextsuperblock);
var optionsStart = [];
var optionsEnd = [];
var numOfSuperBlockDates = 12;
// Clear the options first
var start_epoch = $("#start_epoch");
var end_epoch = $("#end_epoch");
start_epoch.find("option").each(function(index, option) {
$(option).remove();
});
end_epoch.find("option").each(function(index, option) {
$(option).remove();
});
var blocksleft = nextsuperblock - blockheight;
for (i = 0; i < numOfSuperBlockDates; i ++)
{
var timeleft;
var blockdiff = nextsuperblock - blockheight;
var nextsuperblocktimestamp = $.now() + blockdiff * (2.6 * 60 * 1000);
var d = $.datepicker.formatDate('yy-mm-dd', new Date(nextsuperblocktimestamp));
if (i == 0) {
var daysleft = Math.round( blocksleft/554 * 10) / 10; // day rounded to one decimal
if (daysleft >= 2) {
timeleft = daysleft + ' days'
}
else {
timeleft = daysleft + ' day'
}
optionsStart.push("<option value='" + nextsuperblock + "'>block " + nextsuperblock + " in " + timeleft + " (" + d + ")</option>");
}
else {
if (i == 1) {
timeleft = '1 month';
}
else {
timeleft = i + ' months';
}
optionsStart.push("<option value='" + nextsuperblock + "'>block " + nextsuperblock + " in " + timeleft + " (" + d + ")</option>");
if (i == numOfSuperBlockDates - 1) {
timeleft = numOfSuperBlockDates + ' month';
nextsuperblock += budgetPaymentCycleBlocks;
}
}
nextsuperblock += budgetPaymentCycleBlocks;
}
start_epoch.append(optionsStart.join(""));
//populate budget cycle dropdown
for (i = 1; i < 100; i ++)
{
if (i == 1) {
optionsEnd.push("<option value='" + i + "'>one-time payment</option>");
}
else {
optionsEnd.push("<option value='" + i + "'>payment once a month for " + i + " months</option>");
}
}
end_epoch.append(optionsEnd.join(""));
//if next superblock is less than 3 days (60*24*3/2.6) away, select next month's superblock by default so proposal has more time to be voted upon
if (selectblock - blockheight < (60*24*bufferdays/2.6)) {
selectblock += budgetPaymentCycleBlocks;
start_epoch.val(selectblock);
nextsuperblocktimestamp = nextsuperblocktimestamp + budgetPaymentCycleBlocks * (2.6 * 60 * 1000);
}
});
$('#prepareProposal').click(function() {
copyToClipboard($(this).attr('id'));
});
@ -149,228 +63,7 @@
proposal.walletCommands();
$('#feeTxid').on('input', function() {
if ($(this).val().length > 0) {
var submitCommand = "gobject submit " + $('#parentHash').val() + " " + $('#revision').val() + " " + $('#time').val() + " " + proposal.gov.serialize() + " " + $(this).val();
console.log(submitCommand);
var txidfield = $('#feeTxid');
console.log('value entered: ' + txidfield.val());
$('textarea#submitProposal').val(submitCommand);
txidfield.change(function() {
// Check input( $( this ).val() ) for validity here
console.log('there is something wrong with your transaction ID: ' + $('#feeTxid').val() + ' Please copy and paste it here .');
txidfield.val('')
});
//some checks if feeTxid seems valid before we check the api
if (txidfield.val().length == 64) {
if (isAlphaNumeric(txidfield.val())) {
txidfield.unbind( "change" );
console.log('feeTxid seems good: ' + txidfield.val());
console.log("wait while we check the api!");
// first check if transactionid is already in api
$.getJSON(apiserversocket + 'insight-api-dash/tx/' + txidfield.val(), function(data) {
txidfield.attr("disabled", true);
$('.walletCommands#walletCommandsProgress').removeClass('hidden');
var txid = data.tx;
var confirmations = data.confirmations;
var conftxt;
var conftxt2;
var progbarval;
console.log('Transaction has ' + confirmations + ' confirmation(s)');
progbarval = 100/6*confirmations;
$("#progressbar").progressbar({value: progbarval})
.children('.ui-progressbar-value')
.html(progbarval.toPrecision(3) + '%')
.css("display", "block");
if (confirmations != 'undefined' && $.isNumeric(confirmations)) {
var socket = io(apiserversocket);
if (confirmations == 0) {
// we have to count the blocks and wait for 6 confirmations
console.log("we have to count the blocks and wait for 6 confirmations");
eventToListenTo = 'block';
room = 'inv';
socket.on('connect', function() {
// Join the room.
socket.emit('subscribe', room);
console.log("listening for '" + eventToListenTo + "' in '" + room + "'");
});
socket.on(eventToListenTo, function(data) {
console.log("New block received: " + data + " time: " + data.time);
blockhash = data;
// let's check if transaction is really in this block or not
if (confirmations == 0) {
$.getJSON(apiserversocket + 'insight-api-dash/txs/?block=' + blockhash, function(data) {
var txs = data.txs;
var found;
var numOfTxs = txs.length;
for (var i = 0; i < numOfTxs; i++) {
console.log('txs' + i + ': ' + txs[i].txid);
if (txs[i].txid == txidfield.val()) {
console.log('found tx!');
found = true;
}
}
if (found) {
console.log('all good. Count up confirmations.');
confirmations = confirmations + 1;
progbarval = 100/6*confirmations;
$("#progressbar").progressbar({value: progbarval})
.children('.ui-progressbar-value')
.html(progbarval.toPrecision(3) + '%')
.css("display", "block");
if (confirmations == 1) {
conftxt = 'confirmation';
conftxt2 = 'confirmations';
}
else if (confirmations == 5) {
conftxt = 'confirmations';
conftxt2 = 'confirmation';
}
else {
conftxt = 'confirmations';
conftxt2 = 'confirmations';
}
$("#progresstxt").text("Your transaction has " + confirmations + " " + conftxt + ". Waiting for " + (6 - confirmations) + " more " + conftxt2 + "...");
console.log('we have ' + confirmations + ' confirmations...');
}
else {
console.log('txid not in new block');
}
}).fail(function(jqXHR) {
if (jqXHR.status == 400) {
// there seems to be a problem with your feeTxid because txid is not found in api
console.log('block hash not found in api!');
} else {
console.log('There seems to be a problem with the api connection. Maybe endpoint resyncing?');
}
});
}
else {
// for the time being just count up the confirmations without confirming everytime if the transaction is still inside the previous blocks
confirmations = confirmations + 1;
progbarval = 100/6*confirmations;
if (confirmations == 1) {
conftxt = 'confirmation';
conftxt2 = 'confirmations';
}
else if (confirmations == 5) {
conftxt = 'confirmations';
conftxt2 = 'confirmation';
}
else {
conftxt = 'confirmations';
conftxt2 = 'confirmations';
}
$("#progresstxt").text("Your transaction has " + confirmations + " " + conftxt + ". Waiting for " + (6 - confirmations) + " more " + conftxt2 + "...");
console.log('we have ' + confirmations + ' confirmations...');
}
if (confirmations >= 6) {
progbarval = 100;
$("#progresstxt").text("Your transaction has " + confirmations + " confirmations. You can now submit the proposal.");
$('.walletCommands#walletCommandsSubmit').removeClass('hidden');
}
$("#progressbar").progressbar({value: progbarval})
.children('.ui-progressbar-value')
.html(progbarval.toPrecision(3) + '%')
.css("display", "block");
});
}
else if (confirmations > 0 && confirmations <= 5) {
// we have to count the blocks and wait for outstanding confirmations
console.log("we have to count the blocks and wait for outstanding confirmations");
eventToListenTo = 'block';
room = 'inv';
socket.on('connect', function() {
// Join the room.
socket.emit('subscribe', room);
console.log("listening for '" + eventToListenTo + "' in '" + room + "'");
});
socket.on(eventToListenTo, function(data) {
console.log("New block received: " + data + " time: " + data.time);
// for the time being just count up the confirmations without confirming everytime if the transaction is still inside the previous blocks
confirmations = confirmations + 1;
progbarval = 100/6*confirmations;
$("#progressbar").progressbar({value: progbarval})
.children('.ui-progressbar-value')
.html(progbarval.toPrecision(3) + '%')
.css("display", "block");
if (confirmations = 1) {
conftxt = 'confirmation';
conftxt2 = 'confirmations';
}
else if (confirmations = 5) {
conftxt = 'confirmations';
conftxt2 = 'confirmation';
}
else {
conftxt = 'confirmations';
conftxt2 = 'confirmations';
}
$("#progresstxt").text("Your transaction has " + confirmations + " " + conftxt + ". Waiting for " + (6 - confirmations) + " more " + conftxt2 + "...");
console.log('we have ' + confirmations + ' confirmations...');
if (confirmations >= 6) {
progbarval = 100;
$("#progressbar").progressbar({value: progbarval})
.children('.ui-progressbar-value')
.html(progbarval + '%')
.css("display", "block");
$('.walletCommands#walletCommandsSubmit').removeClass('hidden');
}
});
}
else {
// already reached 6 or more confirmations, so we can proceed
progbarval = 100;
$("#progressbar").progressbar({value: progbarval})
.children('.ui-progressbar-value')
.html(progbarval + '%')
.css("display", "block");
$("#progresstxt").text("Your transaction has " + confirmations + " confirmations. You can now submit the proposal.");
console.log("already reached 6 or more confirmations, so we can proceed");
$('.walletCommands#walletCommandsSubmit').removeClass('hidden');
}
}
else {
console.log('Something went terribly wrong. Faulty api data?');
txidfield.attr("disabled", false);
}
}).fail(function(jqXHR) {
if (jqXHR.status == 400) {
// there seems to be a problem with your feeTxid because txid is not found in api
console.log('problem with feeTxid! Ask for new input!');
alert("Check again and please enter your correct TxID!");
txidfield.attr("disabled", false);
} else {
txidfield.attr("disabled", false);
console.log('There seems to be a problem with the api connection');
}
});
}
else {
$('#feeTxid').addClass('validationError');
$('#feeTxid').val('Your transacton ID is invalid. It must be alphanumeric. Please just copy & paste from console.');
console.log("there is something wrong with your transaction ID. It must be alphanumeric!")
}
}
else {
$('#feeTxid').addClass('validationError');
$('#feeTxid').val('Your transacton ID is invalid. Please just copy & paste from console.');
console.log("there is something wrong with your transaction ID. It must be exactly 64 characters!")
}
}
else {
$('textarea#submitProposal').val('');
$('.walletCommands#walletCommandsSubmit').addClass('hidden');
}
});
proposal.transactionListener(proposal);
$('#btnEdit').click(function() {
proposal.createProposal();
@ -394,7 +87,7 @@
this.gov.payment_address = $('#payment_address').val();
this.gov.payment_amount = parseFloat($('#payment_amount').val());
// format dates for gobject serialization
// format dates for govobject serialization
var start_epoch = $('#start_epoch');
var startdate = formattedDateForSuperblockHeight(start_epoch.val());
@ -512,6 +205,319 @@
setFormEditable(true);
};
ProposalGenerator.prototype.transactionListener = function(proposal) {
$('#feeTxid').on('input', function() {
if ($(this).val().length > 0) {
var submitCommand = "gobject submit " + $('#parentHash').val() + " " + $('#revision').val() + " " + $('#time').val() + " " + proposal.gov.serialize() + " " + $(this).val();
console.log(submitCommand);
var txidfield = $('#feeTxid');
console.log('value entered: ' + txidfield.val());
$('textarea#submitProposal').val(submitCommand);
txidfield.change(function() {
// Check input( $( this ).val() ) for validity here
console.log('there is something wrong with your transaction ID: ' + $('#feeTxid').val() + ' Please copy and paste it here .');
txidfield.val('')
});
//some checks if feeTxid seems valid before we check the api
if (txidfield.val().length == 64) {
if (isAlphaNumeric(txidfield.val())) {
txidfield.unbind( "change" );
console.log('feeTxid seems good: ' + txidfield.val());
console.log("wait while we check the api!");
// first check if transactionid is already in api
$.getJSON(apiserversocket + 'insight-api-dash/tx/' + txidfield.val(), function(data) {
txidfield.attr("disabled", true);
$('.walletCommands#walletCommandsProgress').removeClass('hidden');
var txid = data.tx;
var confirmations = data.confirmations;
var conftxt;
var conftxt2;
var progbarval;
console.log('Transaction has ' + confirmations + ' confirmation(s)');
progbarval = 100/6*confirmations;
$("#progressbar").progressbar({value: progbarval})
.children('.ui-progressbar-value')
.html(progbarval.toPrecision(3) + '%')
.css("display", "block");
if (confirmations != 'undefined' && $.isNumeric(confirmations)) {
var socket = io(apiserversocket);
if (confirmations == 0) {
// we have to count the blocks and wait for 6 confirmations
console.log("we have to count the blocks and wait for 6 confirmations");
eventToListenTo = 'block';
room = 'inv';
socket.on('connect', function() {
// Join the room.
socket.emit('subscribe', room);
console.log("listening for '" + eventToListenTo + "' in '" + room + "'");
});
socket.on(eventToListenTo, function(data) {
console.log("New block received: " + data + " time: " + data.time);
blockhash = data;
// let's check if transaction is really in this block or not
if (confirmations == 0) {
$.getJSON(apiserversocket + 'insight-api-dash/txs/?block=' + blockhash, function(data) {
var txs = data.txs;
var found;
var numOfTxs = txs.length;
for (var i = 0; i < numOfTxs; i++) {
console.log('txs' + i + ': ' + txs[i].txid);
if (txs[i].txid == txidfield.val()) {
console.log('found tx!');
found = true;
}
}
if (found) {
console.log('all good. Count up confirmations.');
confirmations = confirmations + 1;
progbarval = 100/6*confirmations;
$("#progressbar").progressbar({value: progbarval})
.children('.ui-progressbar-value')
.html(progbarval.toPrecision(3) + '%')
.css("display", "block");
if (confirmations == 1) {
conftxt = 'confirmation';
conftxt2 = 'confirmations';
}
else if (confirmations == 5) {
conftxt = 'confirmations';
conftxt2 = 'confirmation';
}
else {
conftxt = 'confirmations';
conftxt2 = 'confirmations';
}
$("#progresstxt").text("Your transaction has " + confirmations + " " + conftxt + ". Waiting for " + (6 - confirmations) + " more " + conftxt2 + "...");
console.log('we have ' + confirmations + ' confirmations...');
}
else {
console.log('txid not in new block');
}
}).fail(function(jqXHR) {
if (jqXHR.status == 400) {
// there seems to be a problem with your feeTxid because txid is not found in api
console.log('block hash not found in api!');
} else {
console.log('There seems to be a problem with the api connection. Maybe endpoint resyncing?');
}
});
}
else {
// for the time being just count up the confirmations without confirming everytime if the transaction is still inside the previous blocks
confirmations = confirmations + 1;
progbarval = 100/6*confirmations;
if (confirmations == 1) {
conftxt = 'confirmation';
conftxt2 = 'confirmations';
}
else if (confirmations == 5) {
conftxt = 'confirmations';
conftxt2 = 'confirmation';
}
else {
conftxt = 'confirmations';
conftxt2 = 'confirmations';
}
$("#progresstxt").text("Your transaction has " + confirmations + " " + conftxt + ". Waiting for " + (6 - confirmations) + " more " + conftxt2 + "...");
console.log('we have ' + confirmations + ' confirmations...');
}
if (confirmations >= 6) {
progbarval = 100;
$("#progresstxt").text("Your transaction has " + confirmations + " confirmations. You can now submit the proposal.");
$('.walletCommands#walletCommandsSubmit').removeClass('hidden');
}
$("#progressbar").progressbar({value: progbarval})
.children('.ui-progressbar-value')
.html(progbarval.toPrecision(3) + '%')
.css("display", "block");
});
}
else if (confirmations > 0 && confirmations <= 5) {
// we have to count the blocks and wait for outstanding confirmations
console.log("we have to count the blocks and wait for outstanding confirmations");
eventToListenTo = 'block';
room = 'inv';
socket.on('connect', function() {
// Join the room.
socket.emit('subscribe', room);
console.log("listening for '" + eventToListenTo + "' in '" + room + "'");
});
socket.on(eventToListenTo, function(data) {
console.log("New block received: " + data + " time: " + data.time);
// for the time being just count up the confirmations without confirming everytime if the transaction is still inside the previous blocks
confirmations = confirmations + 1;
progbarval = 100/6*confirmations;
$("#progressbar").progressbar({value: progbarval})
.children('.ui-progressbar-value')
.html(progbarval.toPrecision(3) + '%')
.css("display", "block");
if (confirmations = 1) {
conftxt = 'confirmation';
conftxt2 = 'confirmations';
}
else if (confirmations = 5) {
conftxt = 'confirmations';
conftxt2 = 'confirmation';
}
else {
conftxt = 'confirmations';
conftxt2 = 'confirmations';
}
$("#progresstxt").text("Your transaction has " + confirmations + " " + conftxt + ". Waiting for " + (6 - confirmations) + " more " + conftxt2 + "...");
console.log('we have ' + confirmations + ' confirmations...');
if (confirmations >= 6) {
progbarval = 100;
$("#progressbar").progressbar({value: progbarval})
.children('.ui-progressbar-value')
.html(progbarval + '%')
.css("display", "block");
$('.walletCommands#walletCommandsSubmit').removeClass('hidden');
}
});
}
else {
// already reached 6 or more confirmations, so we can proceed
progbarval = 100;
$("#progressbar").progressbar({value: progbarval})
.children('.ui-progressbar-value')
.html(progbarval + '%')
.css("display", "block");
$("#progresstxt").text("Your transaction has " + confirmations + " confirmations. You can now submit the proposal.");
console.log("already reached 6 or more confirmations, so we can proceed");
$('.walletCommands#walletCommandsSubmit').removeClass('hidden');
}
}
else {
console.log('Something went terribly wrong. Faulty api data?');
txidfield.attr("disabled", false);
}
}).fail(function(jqXHR) {
if (jqXHR.status == 400) {
// there seems to be a problem with your feeTxid because txid is not found in api
console.log('problem with feeTxid! Ask for new input!');
alert("Check again and please enter your correct TxID!");
txidfield.attr("disabled", false);
} else {
txidfield.attr("disabled", false);
console.log('There seems to be a problem with the api connection');
}
});
}
else {
$('#feeTxid').addClass('validationError');
$('#feeTxid').val('Your transacton ID is invalid. It must be alphanumeric. Please just copy & paste from console.');
console.log("there is something wrong with your transaction ID. It must be alphanumeric!")
}
}
else {
$('#feeTxid').addClass('validationError');
$('#feeTxid').val('Your transacton ID is invalid. Please just copy & paste from console.');
console.log("there is something wrong with your transaction ID. It must be exactly 64 characters!")
}
}
else {
$('textarea#submitProposal').val('');
$('.walletCommands#walletCommandsSubmit').addClass('hidden');
}
});
};
prepareTime = function() {
$.getJSON(apiserversocket + "insight-api-dash/status?q=getinfo", function( data ) {
var timefield = $("#time");
timefield.val(Math.floor((new Date).getTime() / 1000));
console.log('time: ' + timefield.val());
blockheight = data.info.blocks;
console.log('network: ' + gov.network);
var budgetPaymentCycleBlocks = getBudgetPaymentCycleBlocks();
var nextsuperblock = Math.round(blockheight/budgetPaymentCycleBlocks) * budgetPaymentCycleBlocks;
var selectblock = nextsuperblock;
console.log('budgetPaymentCycleBlocks: ' + budgetPaymentCycleBlocks);
console.log('blockheight: ' + blockheight);
console.log('next superblock ' + nextsuperblock);
var optionsStart = [];
var optionsEnd = [];
var numOfSuperBlockDates = 12;
// Clear the options first
var start_epoch = $("#start_epoch");
var end_epoch = $("#end_epoch");
start_epoch.find("option").each(function(index, option) {
$(option).remove();
});
end_epoch.find("option").each(function(index, option) {
$(option).remove();
});
var blocksleft = nextsuperblock - blockheight;
for (i = 0; i < numOfSuperBlockDates; i ++)
{
var timeleft;
var blockdiff = nextsuperblock - blockheight;
var nextsuperblocktimestamp = $.now() + blockdiff * (2.6 * 60 * 1000);
var d = $.datepicker.formatDate('yy-mm-dd', new Date(nextsuperblocktimestamp));
if (i == 0) {
var daysleft = Math.round( blocksleft/554 * 10) / 10; // day rounded to one decimal
if (daysleft >= 2) {
timeleft = daysleft + ' days'
}
else {
timeleft = daysleft + ' day'
}
optionsStart.push("<option value='" + nextsuperblock + "'>block " + nextsuperblock + " in " + timeleft + " (" + d + ")</option>");
}
else {
if (i == 1) {
timeleft = '1 month';
}
else {
timeleft = i + ' months';
}
optionsStart.push("<option value='" + nextsuperblock + "'>block " + nextsuperblock + " in " + timeleft + " (" + d + ")</option>");
if (i == numOfSuperBlockDates - 1) {
timeleft = numOfSuperBlockDates + ' month';
nextsuperblock += budgetPaymentCycleBlocks;
}
}
nextsuperblock += budgetPaymentCycleBlocks;
}
start_epoch.append(optionsStart.join(""));
//populate budget cycle dropdown
for (i = 1; i < 100; i ++)
{
if (i == 1) {
optionsEnd.push("<option value='" + i + "'>one-time payment</option>");
}
else {
optionsEnd.push("<option value='" + i + "'>payment once a month for " + i + " months</option>");
}
}
end_epoch.append(optionsEnd.join(""));
//if next superblock is less than n (bufferdays) days (60*24*bufferdays/2.6) away, select next month's superblock by default so proposal has more time to be voted upon
if (selectblock - blockheight < (60*24*bufferdays/2.6)) {
selectblock += budgetPaymentCycleBlocks;
start_epoch.val(selectblock);
}
});
};
function setFormEditable(edit) {
$('.createProposal input').each(function() {
$(this).attr("disabled", edit);