Browse Source

Merge pull request #149 from isocolsky/fix/pagination

Fix/pagination
activeAddress
Matias Alejo Garcia 10 years ago
parent
commit
4092847ff5
  1. 14
      README.md
  2. 18
      lib/server.js
  3. 2
      package.json
  4. 34
      test/integration/server.js

14
README.md

@ -47,10 +47,18 @@ Returns:
`/v1/txhistory/`: Get Wallet's transaction history
Optional Arguments:
* skip: Records to skip from the result (defaults to 0)
* limit: Total number of records to return (return all available records if not specified).
Returns:
* History of incomming and outgoing transactions of the wallet. The list is returned complete (Pagination and filter are [ToDos](https://github.com/bitpay/bitcore-wallet-service/issues/121))
Each item has the following fields:
https://github.com/bitpay/bitcore-wallet-service/issues/121
* History of incomming and outgoing transactions of the wallet. The list is paginated using the `skip` & `limit` params. Each item has the following fields:
* action ('sent', 'received', 'moved')
* amount
* fees
* time
* addressTo
* confirmations
* proposalId
* creatorName
* message

18
lib/server.js

@ -913,12 +913,11 @@ WalletService.prototype._normalizeTxHistory = function(txs) {
};
/**
* Retrieves all transactions (incoming & outgoing) in the range (maxTs-minTs)
* Retrieves all transactions (incoming & outgoing)
* Times are in UNIX EPOCH
*
* @param {Object} opts
* @param {Number} opts.minTs (defaults to 0)
* @param {Number} opts.maxTs (defaults to now)
* @param {Number} opts.skip (defaults to 0)
* @param {Number} opts.limit
* @returns {TxProposal[]} Transaction proposals, first newer
*/
@ -1003,17 +1002,14 @@ WalletService.prototype.getTxHistory = function(opts, cb) {
};
function paginate(txs) {
var limited = opts.limit && opts.limit != -1;
var minTs = opts.minTs || 0;
var maxTs = opts.maxTs || Math.ceil(Date.now() / 1000);
var skip = opts.skip || 0;
var limited = _.isNumber(opts.limit) && opts.limit != -1;
var filtered = _.sortBy(_.filter(txs, function(tx) {
return tx.time >= minTs && tx.time <= maxTs;
}), function(tx) {
var sliced = _.slice(_.sortBy(txs, function(tx) {
return -tx.time;
});
}), skip);
return limited ? _.take(filtered, opts.limit) : filtered;
return limited ? _.take(sliced, opts.limit) : sliced;
};
// Get addresses for this wallet

2
package.json

@ -2,7 +2,7 @@
"name": "bitcore-wallet-service",
"description": "A service for Mutisig HD Bitcoin Wallets",
"author": "BitPay Inc",
"version": "0.0.10",
"version": "0.0.11",
"keywords": [
"bitcoin",
"copay",

34
test/integration/server.js

@ -2347,34 +2347,54 @@ describe('Copay server', function() {
});
it('should get various paginated tx history', function(done) {
var testCases = [{
opts: {},
expected: [50, 40, 30, 20, 10],
}, {
opts: {
minTs: 15,
maxTs: 45,
skip: 1,
limit: 3,
},
expected: [40, 30, 20],
}, {
opts: {
minTs: 15,
maxTs: 45,
skip: 1,
limit: 2,
},
expected: [40, 30],
}, {
opts: {
maxTs: 35,
skip: 2,
},
expected: [30, 20, 10],
}, {
opts: {
minTs: 15,
limit: 4,
},
expected: [50, 40, 30, 20],
}, {
opts: {
minTs: 15,
skip: 0,
limit: 3,
},
expected: [50, 40, 30],
}, {
opts: {
skip: 0,
limit: 0,
},
expected: [],
}, {
opts: {
skip: 4,
limit: 20,
},
expected: [10],
}, {
opts: {
skip: 20,
limit: 1,
},
expected: [],
}];
server._normalizeTxHistory = sinon.stub().returnsArg(0);

Loading…
Cancel
Save