Merge pull request #278 from isocolsky/ref/history_pagination
Remote pagination of tx history
This commit is contained in:
commit
348eb0e48a
|
@ -1470,17 +1470,6 @@ WalletService.prototype.getTxHistory = function(opts, cb) {
|
|||
});
|
||||
};
|
||||
|
||||
function paginate(txs) {
|
||||
var skip = opts.skip || 0;
|
||||
var limited = _.isNumber(opts.limit) && opts.limit != -1;
|
||||
|
||||
var sliced = _.slice(_.sortBy(txs, function(tx) {
|
||||
return -tx.time;
|
||||
}), skip);
|
||||
|
||||
return limited ? _.take(sliced, opts.limit) : sliced;
|
||||
};
|
||||
|
||||
// Get addresses for this wallet
|
||||
self.storage.fetchAddresses(self.walletId, function(err, addresses) {
|
||||
if (err) return cb(err);
|
||||
|
@ -1499,7 +1488,9 @@ WalletService.prototype.getTxHistory = function(opts, cb) {
|
|||
});
|
||||
},
|
||||
function(next) {
|
||||
bc.getTransactions(addressStrs, null, null, function(err, txs) {
|
||||
var from = opts.skip || 0;
|
||||
var to = from + (_.isUndefined(opts.limit) ? 100 : opts.limit);
|
||||
bc.getTransactions(addressStrs, from, to, function(err, txs) {
|
||||
if (err) {
|
||||
log.error('Could not fetch transactions', err);
|
||||
return next(new ClientError('BLOCKCHAINERROR', 'Could not fetch transactions'));
|
||||
|
@ -1513,7 +1504,7 @@ WalletService.prototype.getTxHistory = function(opts, cb) {
|
|||
var proposals = res[0];
|
||||
var txs = res[1];
|
||||
|
||||
txs = paginate(decorate(txs, addresses, proposals));
|
||||
txs = decorate(txs, addresses, proposals);
|
||||
|
||||
return cb(null, txs);
|
||||
});
|
||||
|
|
|
@ -173,7 +173,28 @@ helpers.stubBroadcastFail = function() {
|
|||
};
|
||||
|
||||
helpers.stubHistory = function(txs) {
|
||||
blockchainExplorer.getTransactions = sinon.stub().callsArgWith(3, null, txs);
|
||||
blockchainExplorer.getTransactions = function(addresses, from, to, cb) {
|
||||
var MAX_BATCH_SIZE = 100;
|
||||
var nbTxs = txs.length;
|
||||
|
||||
if (_.isUndefined(from) && _.isUndefined(to)) {
|
||||
from = 0;
|
||||
to = MAX_BATCH_SIZE;
|
||||
}
|
||||
if (!_.isUndefined(from) && _.isUndefined(to))
|
||||
to = from + MAX_BATCH_SIZE;
|
||||
|
||||
if (!_.isUndefined(from) && !_.isUndefined(to) && to - from > MAX_BATCH_SIZE)
|
||||
to = from + MAX_BATCH_SIZE;
|
||||
|
||||
if (from < 0) from = 0;
|
||||
if (to < 0) to = 0;
|
||||
if (from > nbTxs) from = nbTxs;
|
||||
if (to > nbTxs) to = nbTxs;
|
||||
|
||||
var page = txs.slice(from, to);
|
||||
return cb(null, page);
|
||||
};
|
||||
};
|
||||
|
||||
helpers.stubAddressActivity = function(activeAddresses) {
|
||||
|
@ -227,8 +248,7 @@ helpers.createProposalOpts = function(type, outputs, message, signingKey, feePer
|
|||
opts.amount = outputs[0].amount;
|
||||
hash = WalletUtils.getProposalHash(opts.toAddress, opts.amount,
|
||||
opts.message, opts.payProUrl);
|
||||
}
|
||||
else if (type == Model.TxProposal.Types.MULTIPLEOUTPUTS) {
|
||||
} else if (type == Model.TxProposal.Types.MULTIPLEOUTPUTS) {
|
||||
opts.outputs = outputs;
|
||||
var header = {
|
||||
outputs: outputs,
|
||||
|
@ -3536,7 +3556,7 @@ describe('Wallet service', function() {
|
|||
}];
|
||||
|
||||
server._normalizeTxHistory = sinon.stub().returnsArg(0);
|
||||
var timestamps = [10, 50, 30, 40, 20];
|
||||
var timestamps = [50, 40, 30, 20, 10];
|
||||
var txs = _.map(timestamps, function(ts, idx) {
|
||||
return {
|
||||
txid: (idx + 1).toString(),
|
||||
|
|
Loading…
Reference in New Issue