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