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

@ -157,6 +157,7 @@
var txid = data.tx;
var confirmations = data.confirmations;
console.log('Transaction has ' + confirmations + ' confirmation(s)');
if (confirmations != 'undefined' && $.isNumeric(confirmations)) {
var socket = io(apiserversocket);
if (confirmations == 0) {
@ -173,19 +174,24 @@
console.log("New block received: " + data + " time: " + data.time);
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) {
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 == blockhash) {
if (txs[i].txid == txidfield.val()) {
console.log('found tx!');
found = true;
}
}
if (found) {
console.log('all good. Count up confirmations.');
confirmations = confirmations + 1;
console.log(confirmations + " confirmation(s)");
}
else {
console.log('txid not in new block. Waiting for some more blocks.');
}
console.log('txid not in new block');
}
}).fail(function(jqXHR) {
if (jqXHR.status == 400) {
@ -195,6 +201,12 @@
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;
}
if (confirmations >= 6) {
$('.walletCommands#walletCommandsSubmit').removeClass('hidden');
}
@ -213,29 +225,8 @@
socket.on(eventToListenTo, function(data) {
console.log("New block received: " + data + " time: " + data.time);
var blockhash = data;
// let's check if transaction is really in this block or not
$.getJSON(apiserversocket + 'insight-api-dash/txs/?block=' + blockhash, function(data) {
var txs = data.txs;
var numOfTxs = txs.length;
for (var i = 0; i < numOfTxs; i++) {
console.log('txs' + i + ': ' + txs[i].txid);
if (txs[i].txid == blockhash) {
console.log('all good. Count up confirmations.');
// for the time being just count up the confirmations without confirming everytime if the transaction is still inside the previous blocks
confirmations = confirmations + 1;
console.log(confirmations + " confirmation(s)");
}
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');
}
@ -262,9 +253,6 @@
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!")
@ -274,7 +262,6 @@
} else {
$('textarea#submitProposal').val('');
$('.walletCommands#walletCommandsSubmit').addClass('hidden');
@ -476,6 +463,62 @@
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>