|
|
@ -926,6 +926,10 @@ WalletService.prototype.getTxHistory = function(opts, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
function decorate(txs, addresses, proposals) { |
|
|
|
|
|
|
|
var indexedAddresses = _.indexBy(addresses, 'address'); |
|
|
|
var indexedProposals = _.indexBy(proposals, 'txid'); |
|
|
|
|
|
|
|
function sum(items, isMine, isChange) { |
|
|
|
var filter = {}; |
|
|
|
if (_.isBoolean(isMine)) filter.isMine = isMine; |
|
|
@ -936,48 +940,64 @@ WalletService.prototype.getTxHistory = function(opts, cb) { |
|
|
|
}, 0); |
|
|
|
}; |
|
|
|
|
|
|
|
var indexedAddresses = _.indexBy(addresses, 'address'); |
|
|
|
var indexedProposals = _.indexBy(proposals, 'txid'); |
|
|
|
|
|
|
|
_.each(txs, function(tx) { |
|
|
|
_.each(tx.inputs.concat(tx.outputs), function(item) { |
|
|
|
function classify(items) { |
|
|
|
return _.map(items, function(item) { |
|
|
|
var address = indexedAddresses[item.address]; |
|
|
|
item.isMine = !!address; |
|
|
|
item.isChange = address ? address.isChange : false; |
|
|
|
return { |
|
|
|
address: item.address, |
|
|
|
amount: item.amount, |
|
|
|
isMine: !!address, |
|
|
|
isChange: address ? address.isChange : false, |
|
|
|
} |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
var amountIn = sum(tx.inputs, true); |
|
|
|
var amountOut = sum(tx.outputs, true, false); |
|
|
|
var amountOutChange = sum(tx.outputs, true, true); |
|
|
|
var amount; |
|
|
|
return _.map(txs, function(tx) { |
|
|
|
var inputs = classify(tx.inputs); |
|
|
|
var outputs = classify(tx.outputs); |
|
|
|
|
|
|
|
var amountIn = sum(inputs, true); |
|
|
|
var amountOut = sum(outputs, true, false); |
|
|
|
var amountOutChange = sum(outputs, true, true); |
|
|
|
var amount, action, addressTo; |
|
|
|
if (amountIn == (amountOut + amountOutChange + (amountIn > 0 ? tx.fees : 0))) { |
|
|
|
tx.action = 'moved'; |
|
|
|
amount = amountOut; |
|
|
|
action = 'moved'; |
|
|
|
} else { |
|
|
|
amount = amountIn - amountOut - amountOutChange - (amountIn > 0 ? tx.fees : 0); |
|
|
|
tx.action = amount > 0 ? 'sent' : 'received'; |
|
|
|
action = amount > 0 ? 'sent' : 'received'; |
|
|
|
} |
|
|
|
|
|
|
|
tx.amount = Math.abs(amount); |
|
|
|
if (tx.action == 'sent' || tx.action == 'moved') { |
|
|
|
tx.addressTo = tx.outputs[0].address; |
|
|
|
amount = Math.abs(amount); |
|
|
|
if (action == 'sent' || action == 'moved') { |
|
|
|
addressTo = outputs[0].address; |
|
|
|
}; |
|
|
|
|
|
|
|
delete tx.inputs; |
|
|
|
delete tx.outputs; |
|
|
|
var newTx = { |
|
|
|
txid: tx.txid, |
|
|
|
action: action, |
|
|
|
amount: amount, |
|
|
|
fees: tx.fees, |
|
|
|
time: tx.time, |
|
|
|
addressTo: addressTo, |
|
|
|
confirmations: tx.confirmations, |
|
|
|
}; |
|
|
|
|
|
|
|
var proposal = indexedProposals[tx.txid]; |
|
|
|
if (proposal) { |
|
|
|
tx.proposalId = proposal.id; |
|
|
|
tx.creatorName = proposal.creatorName; |
|
|
|
tx.message = proposal.message; |
|
|
|
tx.actions = _.map(proposal.actions, function(action) { |
|
|
|
newTx.proposalId = proposal.id; |
|
|
|
newTx.creatorName = proposal.creatorName; |
|
|
|
newTx.message = proposal.message; |
|
|
|
newTx.actions = _.map(proposal.actions, function(action) { |
|
|
|
return _.pick(action, ['createdOn', 'type', 'copayerId', 'copayerName', 'comment']); |
|
|
|
}); |
|
|
|
// tx.sentTs = proposal.sentTs;
|
|
|
|
// tx.merchant = proposal.merchant;
|
|
|
|
//tx.paymentAckMemo = proposal.paymentAckMemo;
|
|
|
|
// newTx.sentTs = proposal.sentTs;
|
|
|
|
// newTx.merchant = proposal.merchant;
|
|
|
|
//newTx.paymentAckMemo = proposal.paymentAckMemo;
|
|
|
|
} |
|
|
|
|
|
|
|
return newTx; |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
@ -1024,8 +1044,7 @@ WalletService.prototype.getTxHistory = function(opts, cb) { |
|
|
|
var proposals = res[0]; |
|
|
|
var txs = res[1]; |
|
|
|
|
|
|
|
decorate(txs, addresses, proposals); |
|
|
|
txs = paginate(txs); |
|
|
|
txs = paginate(decorate(txs, addresses, proposals)); |
|
|
|
|
|
|
|
return cb(null, txs); |
|
|
|
}); |
|
|
|