From af6777d35971eac5210b637689bb3d647ec6d095 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Fri, 7 Nov 2014 16:19:49 -0300 Subject: [PATCH 1/5] removed duplicate code --- js/controllers/send.js | 30 +----------------------------- 1 file changed, 1 insertion(+), 29 deletions(-) diff --git a/js/controllers/send.js b/js/controllers/send.js index 7461609a6..7b2e76b15 100644 --- a/js/controllers/send.js +++ b/js/controllers/send.js @@ -73,7 +73,7 @@ angular.module('copayApp.controllers').controller('SendController', $scope.loadTxs = function() { controllerUtils.updateTxs(); - + setTimeout(function() { $scope.loading = false; $rootScope.$digest(); @@ -625,32 +625,4 @@ angular.module('copayApp.controllers').controller('SendController', '.' + ' Message: ' + merchantData.pr.pd.memo); }); }; - - controllerUtils.redirIfNotComplete(); - - var w = $rootScope.wallet; - preconditions.checkState(w); - preconditions.checkState(w.settings.unitToSatoshi); - - $rootScope.title = 'Send'; - $scope.loading = false; - var satToUnit = 1 / w.settings.unitToSatoshi; - $scope.defaultFee = bitcore.TransactionBuilder.FEE_PER_1000B_SAT * satToUnit; - $scope.unitToBtc = w.settings.unitToSatoshi / bitcore.util.COIN; - $scope.unitToSatoshi = w.settings.unitToSatoshi; - - $scope.alternativeName = w.settings.alternativeName; - $scope.alternativeIsoCode = w.settings.alternativeIsoCode; - - $scope.isRateAvailable = false; - $scope.rateService = rateService; - $scope.availableBalance = $scope.getAvailableAmount(); - - rateService.whenAvailable(function() { - $scope.isRateAvailable = true; - $scope.$digest(); - }); - - - }); From 04a0bff7061f65479fc9c3f1045af107aab0328f Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 10 Nov 2014 10:40:03 -0300 Subject: [PATCH 2/5] added pagination --- js/controllers/history.js | 39 +++++++++++++++++++++++++++++++------- js/models/Wallet.js | 40 ++++++++++++++++++++++++++++++++++++--- views/history.html | 2 ++ 3 files changed, 71 insertions(+), 10 deletions(-) diff --git a/js/controllers/history.js b/js/controllers/history.js index 72a1e5b93..ff8f41ebc 100644 --- a/js/controllers/history.js +++ b/js/controllers/history.js @@ -11,8 +11,9 @@ angular.module('copayApp.controllers').controller('HistoryController', $scope.loading = false; $scope.lastShowed = false; - $scope.txpCurrentPage = 1; - $scope.txpItemsPerPage = 4; + $scope.currentPage = 1; + $scope.itemsPerPage = 10; + $scope.nbPages = 0; $scope.blockchain_txs = []; $scope.alternativeCurrency = []; @@ -24,19 +25,38 @@ angular.module('copayApp.controllers').controller('HistoryController', $scope.loading = true; setTimeout(function() { $scope.update(); - }, 10); + }, 1); }; + $scope.nextPage = function() { + $scope.currentPage++; + $scope.update(); + }; + + $scope.previousPage = function() { + $scope.currentPage--; + $scope.update(); + }; + + $scope.hasNextPage = function() { + return $scope.currentPage < $scope.nbPages; + }; + + $scope.hasPreviousPage = function() { + return $scope.currentPage > 1; + }; $scope.getTransactions = function() { - var w = $rootScope.wallet; if (!w) return; $scope.blockchain_txs = w.cached_txs || []; $scope.loading = true; - w.getTransactionHistory(function(err, res) { + w.getTransactionHistory({ + currentPage: $scope.currentPage, + itemsPerPage: $scope.itemsPerPage, + }, function(err, res) { if (err) throw err; if (!res) { @@ -45,10 +65,15 @@ angular.module('copayApp.controllers').controller('HistoryController', return; } - _.each(res, function(r) { + var items = res.items; + + _.each(items, function(r) { r.ts = r.minedTs || r.sentTs; }); - $scope.blockchain_txs = w.cached_txs = res; + $scope.blockchain_txs = w.cached_txs = items; + $scope.nbPages = res.nbPages; + + $scope.loading = false; setTimeout(function() { $scope.$digest(); diff --git a/js/models/Wallet.js b/js/models/Wallet.js index 38ace1a9a..24ceb468b 100644 --- a/js/models/Wallet.js +++ b/js/models/Wallet.js @@ -1289,7 +1289,7 @@ Wallet.prototype._getActionList = function(actions) { var peers = Object.keys(actions).map(function(i) { return { cId: i, - actions: actions[i] + actions: actions[i] } }); @@ -2836,9 +2836,22 @@ Wallet.request = function(options, callback) { }; -Wallet.prototype.getTransactionHistory = function(cb) { +/** + * @desc Return a list of past transactions + * + * @param {number} opts.currentPage - the desired page in the dataset + * @param {number} opts.itemsPerPage - number of items per page + * @return {Object} the list of transactions + */ +Wallet.prototype.getTransactionHistory = function(opts, cb) { var self = this; + if (_.isFunction(opts)) { + cb = opts; + opts = {}; + } + opts = opts || {}; + var addresses = self.getAddressesInfo(); var proposals = self.getTxProposals(); var satToUnit = 1 / self.settings.unitToSatoshi; @@ -2948,6 +2961,23 @@ Wallet.prototype.getTransactionHistory = function(cb) { } }; + function paginate(list, currentPage, itemsPerPage) { + if (list.length == 0) return list; + + var res = { + itemsPerPage: itemsPerPage || list.length, + currentPage: currentPage || 1, + nbItems: list.length, + }; + res.nbPages = res.itemsPerPage != 0 ? Math.ceil(res.nbItems / res.itemsPerPage) : 1; + + var from = (res.currentPage - 1) * res.itemsPerPage; + var to = Math.min(from + res.itemsPerPage, res.nbItems); + res.items = list.slice(from, to); + + return res; + }; + if (addresses.length > 0) { var addressesStr = _.pluck(addresses, 'addressStr'); self.blockchain.getTransactions(addressesStr, function(err, txs) { @@ -2957,7 +2987,11 @@ Wallet.prototype.getTransactionHistory = function(cb) { decorateTx(tx); return tx; }); - return cb(null, history); + history.sort(function(a, b) { + return (b.sentTs || b.minedTs) - (a.sentTs || a.minedTs); + }); + + return cb(null, paginate(history, opts.currentPage, opts.itemsPerPage)); }); } }; diff --git a/views/history.html b/views/history.html index 750292d6f..deac8c694 100644 --- a/views/history.html +++ b/views/history.html @@ -100,6 +100,8 @@ + + From dff11dbd70699c5e9122d02e45d3ec8a16eae794 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 10 Nov 2014 10:45:29 -0300 Subject: [PATCH 3/5] fixed tests --- test/Wallet.js | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/test/Wallet.js b/test/Wallet.js index 0a4f25890..fec6cf0ba 100644 --- a/test/Wallet.js +++ b/test/Wallet.js @@ -1976,13 +1976,15 @@ describe('Wallet model', function() { w.getTransactionHistory(function(err, res) { res.should.exist; - res.length.should.equal(3); - res[0].action.should.equal('sent'); - res[0].amountSat.should.equal(900); - res[1].action.should.equal('received'); - res[1].amountSat.should.equal(1900); - res[2].action.should.equal('moved'); - res[2].amountSat.should.equal(2900); + res.items.should.exist; + var items = res.items; + items.length.should.equal(3); + items[0].action.should.equal('sent'); + items[0].amountSat.should.equal(900); + items[1].action.should.equal('received'); + items[1].amountSat.should.equal(1900); + items[2].action.should.equal('moved'); + items[2].amountSat.should.equal(2900); done(); }); }); @@ -2022,8 +2024,10 @@ describe('Wallet model', function() { w.getTransactionHistory(function(err, res) { res.should.exist; - res[0].action.should.equal('sent'); - res[0].amountSat.should.equal(3900); + res.items.should.exist; + var items = res.items; + items[0].action.should.equal('sent'); + items[0].amountSat.should.equal(3900); done(); }); }); @@ -2063,8 +2067,10 @@ describe('Wallet model', function() { w.getTransactionHistory(function(err, res) { res.should.exist; - res[0].action.should.equal('moved'); - res[0].amountSat.should.equal(3900); + res.items.should.exist; + var items = res.items; + items[0].action.should.equal('moved'); + items[0].amountSat.should.equal(3900); done(); }); }); @@ -2110,7 +2116,8 @@ describe('Wallet model', function() { w.getTransactionHistory(function(err, res) { res.should.exist; - res[0].labelTo.should.equal('Address out one'); + res.items.should.exist; + res.items[0].labelTo.should.equal('Address out one'); done(); }); }); @@ -2158,7 +2165,8 @@ describe('Wallet model', function() { }]); w.getTransactionHistory(function(err, res) { res.should.exist; - res[0].comment.should.equal('Another comment'); + res.items.should.exist; + res.items[0].comment.should.equal('Another comment'); done(); }); }); From abae8d43e42e041e148566029894c50b825ddad8 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 10 Nov 2014 11:15:58 -0300 Subject: [PATCH 4/5] fix case for empty tx list --- js/models/Wallet.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/js/models/Wallet.js b/js/models/Wallet.js index 24ceb468b..53f8a1b94 100644 --- a/js/models/Wallet.js +++ b/js/models/Wallet.js @@ -2962,8 +2962,6 @@ Wallet.prototype.getTransactionHistory = function(opts, cb) { }; function paginate(list, currentPage, itemsPerPage) { - if (list.length == 0) return list; - var res = { itemsPerPage: itemsPerPage || list.length, currentPage: currentPage || 1, From 5d3a86057246c335afabc389da156e3a3532b681 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 10 Nov 2014 11:25:31 -0300 Subject: [PATCH 5/5] added tests for pagination --- test/Wallet.js | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/test/Wallet.js b/test/Wallet.js index fec6cf0ba..201f37abe 100644 --- a/test/Wallet.js +++ b/test/Wallet.js @@ -1988,6 +1988,93 @@ describe('Wallet model', function() { done(); }); }); + it('should return paginated list of txs', function(done) { + var w = cachedCreateW2(); + var txs = [{ + txid: 'id1', + vin: [{ + addr: 'addr_in_1', + valueSat: 1000 + }], + vout: [{ + scriptPubKey: { + addresses: ['addr_out_1'], + }, + value: '0.00000900', + }], + fees: 0.00000100 + }, { + txid: 'id2', + vin: [{ + addr: 'addr_in_2', + valueSat: 2000 + }], + vout: [{ + scriptPubKey: { + addresses: ['addr_out_2'], + }, + value: '0.00001900', + }], + fees: 0.00000100 + }, { + txid: 'id3', + vin: [{ + addr: 'addr_in_1', + valueSat: 3000 + + }], + vout: [{ + scriptPubKey: { + addresses: ['addr_out_2'], + }, + value: '0.00002900', + + }], + fees: 0.00000100 + }]; + + w.blockchain.getTransactions = sinon.stub().yields(null, txs); + w.getAddressesInfo = sinon.stub().returns([{ + addressStr: 'addr_in_1' + }, { + addressStr: 'addr_out_2' + }]); + + w.getTransactionHistory({ + currentPage: 2, + itemsPerPage: 2 + }, function(err, res) { + res.should.exist; + res.nbItems.should.equal(3); + res.nbPages.should.equal(2); + res.currentPage.should.equal(2); + res.items.should.exist; + res.items.length.should.equal(1); + res.items[0].txid.should.equal('id3'); + done(); + }); + }); + it('should paginate empty list', function(done) { + var w = cachedCreateW2(); + w.blockchain.getTransactions = sinon.stub().yields(null, []); + w.getAddressesInfo = sinon.stub().returns([{ + addressStr: 'addr_in_1' + }, { + addressStr: 'addr_out_2' + }]); + + w.getTransactionHistory({ + currentPage: 2, + itemsPerPage: 2 + }, function(err, res) { + res.should.exist; + res.nbItems.should.equal(0); + res.nbPages.should.equal(0); + res.items.should.exist; + res.items.length.should.equal(0); + done(); + }); + }); it('should compute sent amount correctly', function(done) { var w = cachedCreateW2(); var txs = [{