added pagination to addrs/txs
This commit is contained in:
parent
275dba3749
commit
6dfaefdcc5
@ -94,8 +94,23 @@ exports.multiutxo = function(req, res, next) {
|
|||||||
|
|
||||||
exports.multitxs = function(req, res, next) {
|
exports.multitxs = function(req, res, next) {
|
||||||
|
|
||||||
function processTxs(txs, cb) {
|
function processTxs(txs, from, to, cb) {
|
||||||
txs = _.uniq(_.flatten(txs));
|
txs = _.uniq(_.flatten(txs), 'txid');
|
||||||
|
|
||||||
|
var nbTxs = txs.length;
|
||||||
|
var paginated = !_.isUndefined(from) || !_.isUndefined(to);
|
||||||
|
|
||||||
|
if (paginated) {
|
||||||
|
txs.sort(function(a, b) {
|
||||||
|
return (b.ts || b.ts) - (a.ts || a.ts);
|
||||||
|
});
|
||||||
|
var start = Math.max(from || 0, 0);
|
||||||
|
var end = Math.min(to || txs.length, txs.length);
|
||||||
|
txs = txs.slice(start, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
txs = _.pluck(txs, 'txid');
|
||||||
|
|
||||||
var transactions = [];
|
var transactions = [];
|
||||||
async.each(txs, function (tx, callback) {
|
async.each(txs, function (tx, callback) {
|
||||||
tDb.fromIdWithInfo(tx, function(err, tx) {
|
tDb.fromIdWithInfo(tx, function(err, tx) {
|
||||||
@ -107,10 +122,22 @@ exports.multitxs = function(req, res, next) {
|
|||||||
});
|
});
|
||||||
}, function (err) {
|
}, function (err) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
|
|
||||||
|
if (paginated) {
|
||||||
|
transactions = {
|
||||||
|
nbItems: nbTxs,
|
||||||
|
from: +from,
|
||||||
|
to: +to,
|
||||||
|
data: transactions,
|
||||||
|
};
|
||||||
|
}
|
||||||
return cb(null, transactions);
|
return cb(null, transactions);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var from = req.query.from;
|
||||||
|
var to = req.query.to;
|
||||||
|
|
||||||
var as = getAddrs(req, res, next);
|
var as = getAddrs(req, res, next);
|
||||||
if (as) {
|
if (as) {
|
||||||
var txs = [];
|
var txs = [];
|
||||||
@ -119,10 +146,10 @@ exports.multitxs = function(req, res, next) {
|
|||||||
if (err) callback(err);
|
if (err) callback(err);
|
||||||
txs = txs.concat(a.transactions);
|
txs = txs.concat(a.transactions);
|
||||||
callback();
|
callback();
|
||||||
}, {ignoreCache: req.param('noCache')});
|
}, {ignoreCache: req.param('noCache'), includeTxInfo: true});
|
||||||
}, function(err) { // finished callback
|
}, function(err) { // finished callback
|
||||||
if (err) return common.handleErrors(err, res);
|
if (err) return common.handleErrors(err, res);
|
||||||
processTxs(txs, function (err, transactions) {
|
processTxs(txs, from, to, function (err, transactions) {
|
||||||
if (err) return common.handleErrors(err, res);
|
if (err) return common.handleErrors(err, res);
|
||||||
res.jsonp(transactions);
|
res.jsonp(transactions);
|
||||||
});
|
});
|
||||||
|
|||||||
@ -93,25 +93,31 @@ Address.prototype.getObj = function() {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
Address.prototype._addTxItem = function(txItem, txList) {
|
Address.prototype._addTxItem = function(txItem, txList, includeInfo) {
|
||||||
|
function addTx(data) {
|
||||||
|
if (!txList) return;
|
||||||
|
if (includeInfo) {
|
||||||
|
txList.push(data);
|
||||||
|
} else {
|
||||||
|
txList.push(data.txid);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
var add=0, addSpend=0;
|
var add=0, addSpend=0;
|
||||||
var v = txItem.value_sat;
|
var v = txItem.value_sat;
|
||||||
var seen = this.seen;
|
var seen = this.seen;
|
||||||
|
|
||||||
// Founding tx
|
// Founding tx
|
||||||
if ( !seen[txItem.txid] ) {
|
if (!seen[txItem.txid]) {
|
||||||
seen[txItem.txid]=1;
|
seen[txItem.txid] = 1;
|
||||||
add=1;
|
add = 1;
|
||||||
|
|
||||||
if (txList)
|
addTx({ txid: txItem.txid, ts: txItem.ts });
|
||||||
txList.push(txItem.txid);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Spent tx
|
// Spent tx
|
||||||
if (txItem.spentTxId && !seen[txItem.spentTxId] ) {
|
if (txItem.spentTxId && !seen[txItem.spentTxId] ) {
|
||||||
if (txList) {
|
addTx({ txid: txItem.spentTxId, ts: txItem.spentTs });
|
||||||
txList.push(txItem.spentTxId);
|
|
||||||
}
|
|
||||||
seen[txItem.spentTxId]=1;
|
seen[txItem.spentTxId]=1;
|
||||||
addSpend=1;
|
addSpend=1;
|
||||||
}
|
}
|
||||||
@ -143,6 +149,7 @@ Address.prototype._addTxItem = function(txItem, txList) {
|
|||||||
// opts are
|
// opts are
|
||||||
// .onlyUnspent
|
// .onlyUnspent
|
||||||
// .txLimit (=0 -> no txs, => -1 no limit)
|
// .txLimit (=0 -> no txs, => -1 no limit)
|
||||||
|
// .includeTxInfo
|
||||||
//
|
//
|
||||||
Address.prototype.update = function(next, opts) {
|
Address.prototype.update = function(next, opts) {
|
||||||
var self = this;
|
var self = this;
|
||||||
@ -188,7 +195,7 @@ Address.prototype.update = function(next, opts) {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
txOut.forEach(function(txItem){
|
txOut.forEach(function(txItem){
|
||||||
self._addTxItem(txItem, txList);
|
self._addTxItem(txItem, txList, opts.includeTxInfo);
|
||||||
});
|
});
|
||||||
if (txList)
|
if (txList)
|
||||||
self.transactions = txList;
|
self.transactions = txList;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user