|
1 | 1 | const { CoinModel } = require('../../../models/coin');
|
2 | 2 | const {Transform} = require('stream');
|
3 | 3 | const util = require('util');
|
4 |
| -const _ = require('underscore'); |
5 | 4 |
|
6 | 5 | function ListTransactionsStream(wallet) {
|
7 | 6 | this.wallet = wallet;
|
8 |
| - Transform.call(this, {objectMode: true}); |
| 7 | + Transform.call(this, { objectMode: true }); |
9 | 8 | }
|
10 |
| - |
11 | 9 | util.inherits(ListTransactionsStream, Transform);
|
12 |
| - |
13 |
| -ListTransactionsStream.prototype._transform = async function(transaction, enc, done) { |
| 10 | +ListTransactionsStream.prototype._transform = function (transaction, enc, done) { |
14 | 11 | var self = this;
|
15 |
| - transaction.inputs = await CoinModel.collection.find({ |
16 |
| - chain: transaction.chain, |
17 |
| - network: transaction.network, |
18 |
| - spentTxid: transaction.txid |
19 |
| - }, { batchSize: 100 }).toArray(); |
20 |
| - transaction.outputs = await CoinModel.collection.find({ |
21 |
| - chain: transaction.chain, |
22 |
| - network: transaction.network, |
23 |
| - mintTxid: transaction.txid |
24 |
| - }, { batchSize: 100 }).toArray(); |
| 12 | + return Promise.all([ |
| 13 | + CoinModel.collection.find({ |
| 14 | + chain: transaction.chain, |
| 15 | + network: transaction.network, |
| 16 | + spentTxid: transaction.txid |
| 17 | + }, { batchSize: 100 }).toArray(), |
| 18 | + CoinModel.collection.find({ |
| 19 | + chain: transaction.chain, |
| 20 | + network: transaction.network, |
| 21 | + mintTxid: transaction.txid |
| 22 | + }, { batchSize: 100 }).toArray()]) |
| 23 | + .then(([inputs, outputs]) => { |
25 | 24 |
|
26 |
| - var wallet = this.wallet._id.toString(); |
27 |
| - var totalInputs = transaction.inputs.reduce((total, input) => { return total + input.value; }, 0); |
28 |
| - var totalOutputs = transaction.outputs.reduce((total, output) => { return total + output.value; }, 0); |
29 |
| - var fee = totalInputs - totalOutputs; |
30 |
| - var sending = _.some(transaction.inputs, function(input) { |
31 |
| - var contains = false; |
32 |
| - _.each(input.wallets, function(inputWallet) { |
33 |
| - if(inputWallet.equals(wallet)) { |
34 |
| - contains = true; |
35 |
| - } |
36 |
| - }); |
37 |
| - return contains; |
38 |
| - }); |
| 25 | + transaction.inputs = inputs; |
| 26 | + transaction.outputs = outputs; |
| 27 | + var wallet = this.wallet._id; |
| 28 | + var totalInputs = transaction.inputs.reduce((total, input) => { return total + input.value; }, 0); |
| 29 | + var totalOutputs = transaction.outputs.reduce((total, output) => { return total + output.value; }, 0); |
| 30 | + var fee = totalInputs - totalOutputs; |
| 31 | + |
| 32 | + for(let input of transaction.inputs) { |
| 33 | + if (input.wallets.includes(wallet)) { |
| 34 | + self.push(JSON.stringify({ |
| 35 | + txid: transaction.txid, |
| 36 | + category: 'send', |
| 37 | + satoshis: -input.value, |
| 38 | + height: transaction.blockHeight, |
| 39 | + address: input.address, |
| 40 | + outputIndex: input.vout, |
| 41 | + blockTime: transaction.blockTimeNormalized |
| 42 | + }) + '\n'); |
39 | 43 |
|
40 |
| - if(sending) { |
41 |
| - _.each(transaction.outputs, function(output) { |
42 |
| - var contains = false; |
43 |
| - _.each(output.wallets, function(outputWallet) { |
44 |
| - if(outputWallet.equals(wallet)) { |
45 |
| - contains = true; |
| 44 | + if (fee > 0) { |
| 45 | + self.push(JSON.stringify({ |
| 46 | + txid: transaction.txid, |
| 47 | + category: 'fee', |
| 48 | + satoshis: -fee, |
| 49 | + height: transaction.blockHeight, |
| 50 | + blockTime: transaction.blockTimeNormalized |
| 51 | + }) + '\n'); |
| 52 | + } |
46 | 53 | }
|
47 |
| - }); |
48 |
| - if(!contains) { |
49 |
| - self.push(JSON.stringify({ |
50 |
| - txid: transaction.txid, |
51 |
| - category: 'send', |
52 |
| - satoshis: -output.value, |
53 |
| - height: transaction.blockHeight, |
54 |
| - address: output.address, |
55 |
| - outputIndex: output.vout, |
56 |
| - blockTime: transaction.blockTimeNormalized |
57 |
| - }) + '\n'); |
58 | 54 | }
|
59 |
| - }); |
60 |
| - if(fee > 0) { |
61 |
| - self.push(JSON.stringify({ |
62 |
| - txid: transaction.txid, |
63 |
| - category: 'fee', |
64 |
| - satoshis: -fee, |
65 |
| - height: transaction.blockHeight, |
66 |
| - blockTime: transaction.blockTimeNormalized |
67 |
| - }) + '\n'); |
68 |
| - } |
69 |
| - return done(); |
70 |
| - } |
71 |
| - |
72 |
| - _.each(transaction.outputs, function(output) { |
73 |
| - var contains = false; |
74 |
| - _.each(output.wallets, function(outputWallet) { |
75 |
| - if(outputWallet.equals(wallet)) { |
76 |
| - contains = true; |
| 55 | + for(let output of transaction.outputs) { |
| 56 | + if (output.wallets.includes(output)) { |
| 57 | + self.push(JSON.stringify({ |
| 58 | + txid: transaction.txid, |
| 59 | + category: 'receive', |
| 60 | + satoshis: output.value, |
| 61 | + height: transaction.blockHeight, |
| 62 | + address: output.address, |
| 63 | + outputIndex: output.vout, |
| 64 | + blockTime: transaction.blockTimeNormalized |
| 65 | + }) + '\n'); |
| 66 | + } |
77 | 67 | }
|
78 |
| - }); |
79 |
| - if(contains) { |
80 |
| - self.push(JSON.stringify({ |
81 |
| - txid: transaction.txid, |
82 |
| - category: 'receive', |
83 |
| - satoshis: output.value, |
84 |
| - height: transaction.blockHeight, |
85 |
| - address: output.address, |
86 |
| - outputIndex: output.vout, |
87 |
| - blockTime: transaction.blockTimeNormalized |
88 |
| - }) + '\n'); |
89 |
| - } |
90 |
| - }); |
91 |
| - done(); |
| 68 | + }) |
| 69 | + .then(() => done()) |
| 70 | + .catch((err) => done(err)); |
92 | 71 | };
|
93 |
| - |
94 | 72 | module.exports = ListTransactionsStream;
|
0 commit comments