listener for blocks and 6 confirmations

This commit is contained in:
Cofresi 2016-11-22 01:10:22 -04:00
parent b4e0a9b589
commit 3cb084cdd1

View file

@ -152,41 +152,47 @@
console.log("wait while we check the api!"); console.log("wait while we check the api!");
// first check if transactionid is already in api // first check if transactionid is already in api
$.getJSON(apiserversocket + 'insight-api-dash/tx/' + txidfield.val(), function(data) { $.getJSON(apiserversocket + 'insight-api-dash/tx/' + txidfield.val(), function(data) {
txidfield.attr("disabled", true); txidfield.attr("disabled", true);
var txid = data.tx; var txid = data.tx;
var confirmations = data.confirmations; var confirmations = data.confirmations;
console.log('Transaction has ' + confirmations + ' confirmation(s)'); console.log('Transaction has ' + confirmations + ' confirmation(s)');
if (confirmations != 'undefined' && $.isNumeric(confirmations)) {
var socket = io(apiserversocket); if (confirmations != 'undefined' && $.isNumeric(confirmations)) {
if (confirmations == 0) { var socket = io(apiserversocket);
// we have to count the blocks and wait for 6 confirmations if (confirmations == 0) {
console.log("we have to count the blocks and wait for 6 confirmations"); // we have to count the blocks and wait for 6 confirmations
eventToListenTo = 'block'; console.log("we have to count the blocks and wait for 6 confirmations");
room = 'inv'; eventToListenTo = 'block';
socket.on('connect', function() { room = 'inv';
// Join the room. socket.on('connect', function() {
socket.emit('subscribe', room); // Join the room.
console.log("listening for '" + eventToListenTo + "' in '" + 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); socket.on(eventToListenTo, function(data) {
var blockhash = data; console.log("New block received: " + data + " time: " + data.time);
// let's check if transaction is really in this block or not var 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) { $.getJSON(apiserversocket + 'insight-api-dash/txs/?block=' + blockhash, function(data) {
var txs = data.txs; var txs = data.txs;
var found;
var numOfTxs = txs.length; var numOfTxs = txs.length;
for (var i = 0; i < numOfTxs; i++) { for (var i = 0; i < numOfTxs; i++) {
console.log('txs' + i + ': ' + txs[i].txid); console.log('txs' + i + ': ' + txs[i].txid);
if (txs[i].txid == blockhash) { if (txs[i].txid == txidfield.val()) {
console.log('all good. Count up confirmations.'); console.log('found tx!');
confirmations = confirmations + 1; found = true;
console.log(confirmations + " confirmation(s)");
}
else {
console.log('txid not in new block. Waiting for some more blocks.');
} }
} }
if (found) {
console.log('all good. Count up confirmations.');
confirmations = confirmations + 1;
}
else {
console.log('txid not in new block');
}
}).fail(function(jqXHR) { }).fail(function(jqXHR) {
if (jqXHR.status == 400) { if (jqXHR.status == 400) {
// there seems to be a problem with your feeTxid because txid is not found in api // there seems to be a problem with your feeTxid because txid is not found in api
@ -195,104 +201,85 @@
console.log('There seems to be a problem with the api connection. Maybe endpoint resyncing?'); console.log('There seems to be a problem with the api connection. Maybe endpoint resyncing?');
} }
}); });
if (confirmations >= 6) { }
$('.walletCommands#walletCommandsSubmit').removeClass('hidden'); 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;
} }
else if (confirmations > 0 && confirmations <= 5) {
// we have to count the blocks and wait for outstanding confirmations if (confirmations >= 6) {
console.log("we have to count the blocks and wait for outstanding confirmations"); $('.walletCommands#walletCommandsSubmit').removeClass('hidden');
eventToListenTo = 'block'; }
room = 'inv'; });
socket.on('connect', function() { }
// Join the room. else if (confirmations > 0 && confirmations <= 5) {
socket.emit('subscribe', room); // we have to count the blocks and wait for outstanding confirmations
console.log("listening for '" + eventToListenTo + "' in '" + room + "'"); console.log("we have to count the blocks and wait for outstanding confirmations");
}); eventToListenTo = 'block';
socket.on(eventToListenTo, function(data) { room = 'inv';
console.log("New block received: " + data + " time: " + data.time); socket.on('connect', function() {
var blockhash = data; // Join the room.
// let's check if transaction is really in this block or not socket.emit('subscribe', room);
$.getJSON(apiserversocket + 'insight-api-dash/txs/?block=' + blockhash, function(data) { console.log("listening for '" + eventToListenTo + "' in '" + room + "'");
var txs = data.txs; });
var numOfTxs = txs.length; socket.on(eventToListenTo, function(data) {
for (var i = 0; i < numOfTxs; i++) { console.log("New block received: " + data + " time: " + data.time);
console.log('txs' + i + ': ' + txs[i].txid); var blockhash = data;
if (txs[i].txid == blockhash) { // for the time being just count up the confirmations without confirming everytime if the transaction is still inside the previous blocks
console.log('all good. Count up confirmations.'); confirmations = confirmations + 1;
confirmations = confirmations + 1; if (confirmations >= 6) {
console.log(confirmations + " confirmation(s)"); $('.walletCommands#walletCommandsSubmit').removeClass('hidden');
} }
else { });
console.log('txid not in new block. Waiting for some more blocks...');
}
}
}).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?');
}
});
if (confirmations >= 6) {
$('.walletCommands#walletCommandsSubmit').removeClass('hidden');
}
});
}
else {
// already reached 6 or more confirmations, so we can proceed
alert("already reached 6 or more confirmations, so we can proceed");
$('.walletCommands#walletCommandsSubmit').removeClass('hidden');
}
} }
else { else {
console.log('Something went terribly wrong. Faulty api data?'); // already reached 6 or more confirmations, so we can proceed
txidfield.attr("disabled", false); alert("already reached 6 or more confirmations, so we can proceed");
$('.walletCommands#walletCommandsSubmit').removeClass('hidden');
} }
}).fail(function(jqXHR) { }
if (jqXHR.status == 400) { else {
// there seems to be a problem with your feeTxid because txid is not found in api console.log('Something went terribly wrong. Faulty api data?');
console.log('problem with feeTxid! Ask for new input!'); txidfield.attr("disabled", false);
alert("Check again and please enter your correct TxID!"); }
txidfield.attr("disabled", false); }).fail(function(jqXHR) {
} else { if (jqXHR.status == 400) {
txidfield.attr("disabled", false); // there seems to be a problem with your feeTxid because txid is not found in api
console.log('There seems to be a problem with the api connection'); 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 { });
alert("there is something wrong with your transaction ID. It must be alphanumeric!") }
} else {
alert("there is something wrong with your transaction ID. It must be alphanumeric!")
} }
} else {
$('textarea#submitProposal').val('');
$('.walletCommands#walletCommandsSubmit').addClass('hidden');
} }
});
$('#btnEdit').click(function() {
proposal.createProposal();
}); } else {
$('textarea#submitProposal').val('');
$('.walletCommands#walletCommandsSubmit').addClass('hidden');
}
});
$('#btnNew').click(function() { $('#btnEdit').click(function() {
proposal.resetProposal();
}); proposal.createProposal();
}
}); });
$('#btnNew').click(function() {
proposal.resetProposal();
});
}
}); });
});
var ProposalGenerator = function(gov) { var ProposalGenerator = function(gov) {
this._mode = 'proposal'; this._mode = 'proposal';
@ -476,6 +463,62 @@
return true; return true;
} }
function existsTransactionInBlock(blockhash, txid) {
$.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 === txid) {
console.log('found tx!');
found = true;
}
}
if (found) {
console.log('all good. ');
return true;
}
else {
console.log('txid not in new block');
return 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('block hash not found in api!');
return false;
} else {
console.log('There seems to be a problem with the api connection. Maybe endpoint resyncing?');
return false;
}
});
}
function getConfirmationsForTxid(txid) {
var jqxhr = $.ajax({
url: apiserversocket + 'insight-api-dash/tx/' + txid,
async: 'false'
}).done(function() {
alert( "success" );
})
.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!');
return false;
} else {
console.log('There seems to be a problem with the api connection. Maybe endpoint resyncing?');
return false;
}
})
}
</script> </script>