mirror of https://github.com/BTCPrivate/copay.git
add tests
This commit is contained in:
parent
797da92d90
commit
de5772112f
|
@ -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;
|
||||
});
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
150
test/Wallet.js
150
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;
|
||||
|
||||
|
|
Loading…
Reference in New Issue