refactor transaction listener

This commit is contained in:
snogcel 2016-12-06 21:28:17 -07:00
parent 3b4ef0ee62
commit 4f249309bb
2 changed files with 114 additions and 6 deletions

View file

@ -19,22 +19,30 @@
<link rel="stylesheet" href="css/master.css"> <link rel="stylesheet" href="css/master.css">
<script src="js/proposalGenerator.js"></script> <script src="js/proposalGenerator.js"></script>
<script src="js/paymentCycle.js"></script> <script src="js/paymentCycle.js"></script>
<script src="js/txListener.js"></script>
<script src="js/transactionListener.js"></script> <script src="js/transactionListener.js"></script>
<script src="js/formHandler.js"></script> <script src="js/formHandler.js"></script>
<script src="https://dev-test.dash.org/socket.io/socket.io.js"></script> <script src="https://dev-test.dash.org/socket.io/socket.io.js"></script>
<script type="text/javascript"> <script type="text/javascript">
var Bitcore = require('bitcore-lib-dash'); var Bitcore = require('bitcore-lib-dash');
var provider = 'https://dev-test.dash.org:3001/'; var provider = 'https://dev-test.dash.org/';
var socket = io(provider);
socket.on('connect', function() {
socket.emit('subscribe', 'inv');
console.log("socket.io initialized...");
});
$(document).ready(function() { $(document).ready(function() {
var gov = new Bitcore.GovObject.Proposal(); var gov = new Bitcore.GovObject.Proposal();
gov.network = 'testnet'; gov.network = 'testnet';
var paymentCycle = new PaymentCycle(gov); var paymentCycle = new PaymentCycle(gov);
paymentCycle.updateDropdowns(); // update dropdown menus based on network and current time paymentCycle.updateDropdowns(); // update dropdown menus based on network and current time
$("#time").val(Math.floor((new Date).getTime() / 1000)); $("#time").val(Math.floor((new Date).getTime() / 1000));
@ -71,8 +79,6 @@
proposal.walletCommands(); proposal.walletCommands();
transactionListener(proposal);
$('#btnEdit').click(function() { $('#btnEdit').click(function() {
proposal.createProposal(); proposal.createProposal();
}); });
@ -82,6 +88,25 @@
}); });
} }
}); });
$('#feeTxid').on('input', function() {
var transaction = $(this).val();
var txListener = new TXListener(socket, provider, transaction);
// check if tx exists in insight
txListener.getTx(function(err, res) {
if(err) console.log(err);
if(res) {
console.log(res);
txListener.blockheight = res.blockheight;
txListener.confirmations = res.confirmations;
txListener.initSocket();
}
});
});
}); });
</script> </script>
@ -232,7 +257,7 @@
</div> </div>
<div class="row walletCommands hidden" id="walletCommandsTx"> <div class="row walletCommands" id="walletCommandsTx">
<div class="col-xs-12"> <div class="col-xs-12">
@ -246,7 +271,7 @@
</div> </div>
<div class="row walletCommands hidden" id="walletCommandsProgress"> <div class="row walletCommands" id="walletCommandsProgress">
<div class="col-xs-12"> <div class="col-xs-12">

83
js/txListener.js Executable file
View file

@ -0,0 +1,83 @@
function TXListener(socket, provider, transaction) {
this.socket = socket;
this.provider = provider;
this.transaction = transaction;
this.blockheight = null;
this.confirmations = null;
}
TXListener.prototype.initSocket = function() {
var self = this;
var socket = this.socket;
socket.on('block', function(data) {
console.log('block: '+ data);
self.getBlock(data, function(err, res) {
if (err) console.log("error fetching block: " + data);
self.confirmations = (res.height - self.blockheight) + 1; // compare blockHeight against transaction blockHeight
console.log('confirmations: ' + self.confirmations);
});
});
};
TXListener.prototype.getTx = function(cb) {
var txid = this.transaction;
var opts = {
type: "GET",
route: "insight-api-dash/tx/"+txid,
data: {
format: "json"
}
};
this._fetch(opts, cb);
};
TXListener.prototype.getBlock = function(hash, cb) {
var opts = {
type: "GET",
route: "insight-api-dash/block/"+hash,
data: {
format: "json"
}
};
this._fetch(opts, cb);
};
TXListener.prototype._fetch = function(opts,cb) {
var self = this;
var provider = opts.provider || self.provider;
if(opts.type && opts.route && opts.data) {
jQuery.ajax({
type: opts.type,
url: provider + opts.route,
data: JSON.stringify(opts.data),
contentType: "application/json; charset=utf-8",
crossDomain: true,
dataType: "json",
success: function (data, status, jqXHR) {
cb(null, data);
},
error: function (jqXHR, status, error) {
var err = eval("(" + jqXHR.responseText + ")");
cb(err, null);
}
});
} else {
cb('missing parameter',null);
}
};