From de5772112fe898fd85ac395a6e2c6ac3e4bb348d Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Wed, 22 Oct 2014 16:57:29 -0300 Subject: [PATCH] add tests --- js/controllers/transactions.js | 3 +- js/models/Wallet.js | 14 +-- test/Wallet.js | 150 ++++++++++++++++++++++++++++++++- 3 files changed, 159 insertions(+), 8 deletions(-) diff --git a/js/controllers/transactions.js b/js/controllers/transactions.js index e2f275d1e..ebaa017ae 100644 --- a/js/controllers/transactions.js +++ b/js/controllers/transactions.js @@ -51,6 +51,7 @@ angular.module('copayApp.controllers').controller('TransactionsController', var w = $rootScope.wallet; if (!w) return; + $scope.blockchain_txs = w.cached_txs || []; $scope.loading = true; w.getTransactionHistory(function(err, res) { if (err) throw err; @@ -61,7 +62,7 @@ angular.module('copayApp.controllers').controller('TransactionsController', return; } - $scope.blockchain_txs = res; + $scope.blockchain_txs = w.cached_txs = res; $scope.loading = false; }); }; diff --git a/js/models/Wallet.js b/js/models/Wallet.js index ab87e0cde..1aac2c179 100644 --- a/js/models/Wallet.js +++ b/js/models/Wallet.js @@ -2918,6 +2918,7 @@ Wallet.prototype.getTransactionHistory = function(cb) { var satToUnit = 1 / self.settings.unitToSatoshi; var addresses = _.pluck(self.getAddressesInfo(), 'addressStr'); + if (addresses.length == 0) return cb(); function computeAmountIn(items) { @@ -2932,6 +2933,7 @@ Wallet.prototype.getTransactionHistory = function(cb) { function computeAmountOut(items) { var mine = _.filter(items, function(item) { if (!item.scriptPubKey) return false; + // If classic multisig, ignore if (item.scriptPubKey.addresses.length > 1) return false; return _.contains(addresses, item.scriptPubKey.addresses[0]); @@ -2945,14 +2947,16 @@ Wallet.prototype.getTransactionHistory = function(cb) { var amountIn = computeAmountIn(tx.vin); var amountOut = computeAmountOut(tx.vout); var fees = parseInt((tx.fees * bitcore.util.COIN).toFixed(0)); - var amount = amountIn - amountOut - (amountIn > 0 ? fees : 0); - if (amount == 0) { + var amount; + if (amountIn == (amountOut + (amountIn > 0 ? fees : 0))) { tx.action = 'moved'; - } else if (amount > 0) { - tx.action = 'sent'; + // TODO: subtract amount from change addresses + amount = amountOut; } else { - tx.action = 'received'; + amount = amountIn - amountOut - (amountIn > 0 ? fees : 0); + tx.action = amount > 0 ? 'sent' : 'received'; } + tx.amountSat = Math.abs(amount); tx.amount = tx.amountSat * satToUnit; }; diff --git a/test/Wallet.js b/test/Wallet.js index 5756a5fe8..eda6b2e3c 100644 --- a/test/Wallet.js +++ b/test/Wallet.js @@ -422,11 +422,11 @@ describe('Wallet model', function() { var s = Wallet.decodeSecret('4fp61K187CsYmjoRQC5iAdC5eGmbCRsAAXfwEwetSQgHvZs27eWKaLaNHRoKM'); should.exist(s); - s= Wallet.decodeSecret('4fp61K187CsYmjoRQC5iAdC5eGmbCRsAAXfwEwetSQgHvZs27eWKaLaNHRoK'); + s = Wallet.decodeSecret('4fp61K187CsYmjoRQC5iAdC5eGmbCRsAAXfwEwetSQgHvZs27eWKaLaNHRoK'); s.should.equal(false); - s= Wallet.decodeSecret('123456'); + s = Wallet.decodeSecret('123456'); s.should.equal(false); }); @@ -1937,6 +1937,152 @@ describe('Wallet model', function() { }); }); + describe('#getTransactionHistory', function() { + it('should return list of txs', function(done) { + var w = cachedCreateW2(); + var txs = [{ + vin: [{ + addr: 'addr_in_1', + valueSat: 1000 + }], + vout: [{ + scriptPubKey: { + addresses: ['addr_out_1'], + }, + value: '0.00000900', + }], + fees: 0.00000100 + }, { + vin: [{ + addr: 'addr_in_2', + valueSat: 2000 + }], + vout: [{ + scriptPubKey: { + addresses: ['addr_out_2'], + }, + value: '0.00001900', + }], + fees: 0.00000100 + }, { + 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(function(err, res) { + res.should.exist; + res.length.should.equal(3); + console.log('res', res); + 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); + done(); + }); + }); + it('should compute sent amount correctly', function(done) { + var w = cachedCreateW2(); + var txs = [{ + vin: [{ + addr: 'addr_in_1', + valueSat: 3000 + }, { + addr: 'addr_in_2', + valueSat: 2000 + }], + vout: [{ + scriptPubKey: { + addresses: ['addr_out_1'], + }, + value: '0.00003900', + }, { + scriptPubKey: { + addresses: ['change'], + }, + value: '0.00001000', + }], + fees: 0.00000100 + }]; + + w.blockchain.getTransactions = sinon.stub().yields(null, txs); + w.getAddressesInfo = sinon.stub().returns([{ + addressStr: 'addr_in_1' + }, { + addressStr: 'addr_in_2' + }, { + addressStr: 'change' + }]); + + w.getTransactionHistory(function(err, res) { + res.should.exist; + res[0].action.should.equal('sent'); + res[0].amountSat.should.equal(3900); + done(); + }); + }); + it('should compute moved amount correctly', function(done) { + var w = cachedCreateW2(); + var txs = [{ + vin: [{ + addr: 'addr_1', + valueSat: 3000 + }, { + addr: 'addr_2', + valueSat: 2000 + }], + vout: [{ + scriptPubKey: { + addresses: ['addr_1'], + }, + value: '0.00003900', + }, { + scriptPubKey: { + addresses: ['change'], + }, + value: '0.00001000', + }], + fees: 0.00000100 + }]; + + w.blockchain.getTransactions = sinon.stub().yields(null, txs); + w.getAddressesInfo = sinon.stub().returns([{ + addressStr: 'addr_1' + }, { + addressStr: 'addr_2' + }, { + addressStr: 'change' + }]); + + w.getTransactionHistory(function(err, res) { + res.should.exist; + res[0].action.should.equal('moved'); + res[0].amountSat.should.equal(3900); + done(); + }); + }); + }); + + describe('#read', function() { var storage, network, blockchain;