add tests
This commit is contained in:
parent
797da92d90
commit
de5772112f
3 changed files with 159 additions and 8 deletions
|
|
@ -51,6 +51,7 @@ angular.module('copayApp.controllers').controller('TransactionsController',
|
||||||
var w = $rootScope.wallet;
|
var w = $rootScope.wallet;
|
||||||
if (!w) return;
|
if (!w) return;
|
||||||
|
|
||||||
|
$scope.blockchain_txs = w.cached_txs || [];
|
||||||
$scope.loading = true;
|
$scope.loading = true;
|
||||||
w.getTransactionHistory(function(err, res) {
|
w.getTransactionHistory(function(err, res) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
@ -61,7 +62,7 @@ angular.module('copayApp.controllers').controller('TransactionsController',
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$scope.blockchain_txs = res;
|
$scope.blockchain_txs = w.cached_txs = res;
|
||||||
$scope.loading = false;
|
$scope.loading = false;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -2918,6 +2918,7 @@ Wallet.prototype.getTransactionHistory = function(cb) {
|
||||||
|
|
||||||
var satToUnit = 1 / self.settings.unitToSatoshi;
|
var satToUnit = 1 / self.settings.unitToSatoshi;
|
||||||
var addresses = _.pluck(self.getAddressesInfo(), 'addressStr');
|
var addresses = _.pluck(self.getAddressesInfo(), 'addressStr');
|
||||||
|
|
||||||
if (addresses.length == 0) return cb();
|
if (addresses.length == 0) return cb();
|
||||||
|
|
||||||
function computeAmountIn(items) {
|
function computeAmountIn(items) {
|
||||||
|
|
@ -2932,6 +2933,7 @@ Wallet.prototype.getTransactionHistory = function(cb) {
|
||||||
function computeAmountOut(items) {
|
function computeAmountOut(items) {
|
||||||
var mine = _.filter(items, function(item) {
|
var mine = _.filter(items, function(item) {
|
||||||
if (!item.scriptPubKey) return false;
|
if (!item.scriptPubKey) return false;
|
||||||
|
|
||||||
// If classic multisig, ignore
|
// If classic multisig, ignore
|
||||||
if (item.scriptPubKey.addresses.length > 1) return false;
|
if (item.scriptPubKey.addresses.length > 1) return false;
|
||||||
return _.contains(addresses, item.scriptPubKey.addresses[0]);
|
return _.contains(addresses, item.scriptPubKey.addresses[0]);
|
||||||
|
|
@ -2945,14 +2947,16 @@ Wallet.prototype.getTransactionHistory = function(cb) {
|
||||||
var amountIn = computeAmountIn(tx.vin);
|
var amountIn = computeAmountIn(tx.vin);
|
||||||
var amountOut = computeAmountOut(tx.vout);
|
var amountOut = computeAmountOut(tx.vout);
|
||||||
var fees = parseInt((tx.fees * bitcore.util.COIN).toFixed(0));
|
var fees = parseInt((tx.fees * bitcore.util.COIN).toFixed(0));
|
||||||
var amount = amountIn - amountOut - (amountIn > 0 ? fees : 0);
|
var amount;
|
||||||
if (amount == 0) {
|
if (amountIn == (amountOut + (amountIn > 0 ? fees : 0))) {
|
||||||
tx.action = 'moved';
|
tx.action = 'moved';
|
||||||
} else if (amount > 0) {
|
// TODO: subtract amount from change addresses
|
||||||
tx.action = 'sent';
|
amount = amountOut;
|
||||||
} else {
|
} else {
|
||||||
tx.action = 'received';
|
amount = amountIn - amountOut - (amountIn > 0 ? fees : 0);
|
||||||
|
tx.action = amount > 0 ? 'sent' : 'received';
|
||||||
}
|
}
|
||||||
|
|
||||||
tx.amountSat = Math.abs(amount);
|
tx.amountSat = Math.abs(amount);
|
||||||
tx.amount = tx.amountSat * satToUnit;
|
tx.amount = tx.amountSat * satToUnit;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
146
test/Wallet.js
146
test/Wallet.js
|
|
@ -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() {
|
describe('#read', function() {
|
||||||
var storage, network, blockchain;
|
var storage, network, blockchain;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue