Browse Source

treat single address in output as change

feat/estimateFee-limit
Ivan Socolsky 8 years ago
parent
commit
75c8e28e73
No known key found for this signature in database GPG Key ID: FAECE6A05FAA4F56
  1. 89
      lib/server.js
  2. 65
      test/integration/server.js

89
lib/server.js

@ -2660,8 +2660,7 @@ WalletService.prototype.getTxHistory = function(opts, cb) {
if (opts.limit > Defaults.HISTORY_LIMIT) if (opts.limit > Defaults.HISTORY_LIMIT)
return cb(Errors.HISTORY_LIMIT_EXCEEDED); return cb(Errors.HISTORY_LIMIT_EXCEEDED);
function decorate(txs, addresses, proposals, notes) { function decorate(wallet, txs, addresses, proposals, notes) {
var indexedAddresses = _.indexBy(addresses, 'address'); var indexedAddresses = _.indexBy(addresses, 'address');
var indexedProposals = _.indexBy(proposals, 'txid'); var indexedProposals = _.indexBy(proposals, 'txid');
var indexedNotes = _.indexBy(notes, 'txid'); var indexedNotes = _.indexBy(notes, 'txid');
@ -2680,7 +2679,7 @@ WalletService.prototype.getTxHistory = function(opts, cb) {
address: item.address, address: item.address,
amount: item.amount, amount: item.amount,
isMine: !!address, isMine: !!address,
isChange: address ? address.isChange : false, isChange: address ? (address.isChange || wallet.singleAddress) : false,
} }
}); });
}; };
@ -2858,55 +2857,59 @@ WalletService.prototype.getTxHistory = function(opts, cb) {
}); });
}; };
// Get addresses for this wallet self.getWallet({}, function(err, wallet) {
self.storage.fetchAddresses(self.walletId, function(err, addresses) {
if (err) return cb(err); if (err) return cb(err);
if (addresses.length == 0) return cb(null, []);
var from = opts.skip || 0; // Get addresses for this wallet
var to = from + opts.limit; self.storage.fetchAddresses(self.walletId, function(err, addresses) {
if (err) return cb(err);
if (addresses.length == 0) return cb(null, []);
var from = opts.skip || 0;
var to = from + opts.limit;
async.waterfall([ async.waterfall([
function(next) { function(next) {
getNormalizedTxs(addresses, from, to, next); getNormalizedTxs(addresses, from, to, next);
}, },
function(txs, next) { function(txs, next) {
// Fetch all proposals in [t - 7 days, t + 1 day] // Fetch all proposals in [t - 7 days, t + 1 day]
var minTs = _.min(txs.items, 'time').time - 7 * 24 * 3600; var minTs = _.min(txs.items, 'time').time - 7 * 24 * 3600;
var maxTs = _.max(txs.items, 'time').time + 1 * 24 * 3600; var maxTs = _.max(txs.items, 'time').time + 1 * 24 * 3600;
async.parallel([ async.parallel([
function(done) { function(done) {
self.storage.fetchTxs(self.walletId, { self.storage.fetchTxs(self.walletId, {
minTs: minTs, minTs: minTs,
maxTs: maxTs maxTs: maxTs
}, done); }, done);
}, },
function(done) { function(done) {
self.storage.fetchTxNotes(self.walletId, { self.storage.fetchTxNotes(self.walletId, {
minTs: minTs minTs: minTs
}, done); }, done);
}, },
], function(err, res) { ], function(err, res) {
return next(err, { return next(err, {
txs: txs, txs: txs,
txps: res[0], txps: res[0],
notes: res[1] notes: res[1]
});
}); });
}); },
}, ], function(err, res) {
], function(err, res) { if (err) return cb(err);
if (err) return cb(err);
var finalTxs = decorate(res.txs.items, addresses, res.txps, res.notes); var finalTxs = decorate(wallet, res.txs.items, addresses, res.txps, res.notes);
if (res.txs.fromCache) if (res.txs.fromCache)
log.debug("History from cache for:", self.walletId, from, to); log.debug("History from cache for:", self.walletId, from, to);
return cb(null, finalTxs, !!res.txs.fromCache); return cb(null, finalTxs, !!res.txs.fromCache);
});
}); });
}); });
}; };

65
test/integration/server.js

@ -4245,41 +4245,42 @@ describe('Wallet service', function() {
}); });
}); });
}); });
it.only('should correctly handle change in tx history', function(done) { it('should correctly handle change in tx history', function(done) {
server._normalizeTxHistory = sinon.stub().returnsArg(0); server._normalizeTxHistory = sinon.stub().returnsArg(0);
var txs = [{ helpers.stubUtxos(server, wallet, 2, function() {
txid: '1', var txs = [{
confirmations: 1, txid: '1',
fees: 150, confirmations: 1,
time: Date.now() / 1000, fees: 150,
inputs: [{ time: Date.now() / 1000,
address: firstAddress, inputs: [{
amount: 550, address: firstAddress.address,
}], amount: 550,
outputs: [{ }],
address: firstAddress, outputs: [{
amount: 100, address: firstAddress.address,
}, { amount: 100,
address: 'external', }, {
amount: 300, address: 'external',
}], amount: 300,
}]; }],
helpers.stubHistory(txs); }];
server.getTxHistory({}, function(err, txs) { helpers.stubHistory(txs);
should.not.exist(err); server.getTxHistory({}, function(err, txs) {
should.exist(txs); should.not.exist(err);
txs.length.should.equal(1); should.exist(txs);
var tx = txs[0]; txs.length.should.equal(1);
tx.action.should.equal('sent'); var tx = txs[0];
tx.amount.should.equal(300); tx.action.should.equal('sent');
tx.fees.should.equal(150); tx.amount.should.equal(300);
tx.outputs.length.should.equal(1); tx.fees.should.equal(150);
tx.outputs[0].address.should.equal('external'); tx.outputs.length.should.equal(1);
tx.outputs[0].amount.should.equal(300); tx.outputs[0].address.should.equal('external');
done(); tx.outputs[0].amount.should.equal(300);
done();
});
}); });
}); });
}); });

Loading…
Cancel
Save