diff --git a/server/lib/api/transaction.js b/server/lib/api/transaction.js index d7dc1e3..1c92b25 100644 --- a/server/lib/api/transaction.js +++ b/server/lib/api/transaction.js @@ -21,7 +21,7 @@ module.exports = function transactionAPI(router) { `getTxById: ${err}`); return res.status(400).send(); } - + console.log(transaction); const tx = transaction; return res.send({ txid: tx.hash, @@ -34,8 +34,8 @@ module.exports = function transactionAPI(router) { confirmations: (height - tx.height) + 1, valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8, vin: tx.inputs.map(input => ({ - addr: input.coin ? input.coin.address : '', - value: input.coin ? input.coin.value / 1e8 : 0, + addr: input.address, + value: input.value / 1e8, })), vout: tx.outputs.map(output => ({ scriptPubKey: { diff --git a/server/lib/db/transactions.js b/server/lib/db/transactions.js index 9b7ec2c..0bbfbd7 100644 --- a/server/lib/db/transactions.js +++ b/server/lib/db/transactions.js @@ -54,7 +54,6 @@ function getTransaction(params, options, limit, cb) { }); } - function getTxById(txid, cb) { getTransaction( { hash: txid }, @@ -66,13 +65,31 @@ function getTxById(txid, cb) { `/rawblock/:blockHash: ${err}`); return cb(err); } - console.log(transaction); return cb(null, transaction); }); } +function updateInput(txid, inputid, value, address) { + Transactions.findOneAndUpdate( + { _id: txid, 'inputs._id': inputid }, + { + $set: { + 'inputs.$.value': value, + 'inputs.$.address': address, + }, + }, + (err, tx) => { + if (err) { + logger.log('err', + `updateInput: ${err}`); + } + }, + ); +} + module.exports = { getTransaction, getTransactions, getTxById, + updateInput, }; diff --git a/server/lib/parser/block.js b/server/lib/parser/block.js index 34b3d3d..7ea44aa 100644 --- a/server/lib/parser/block.js +++ b/server/lib/parser/block.js @@ -73,13 +73,6 @@ function parse(entry, block) { } }); } -// Fill in behind blocks and update tx inputs -function updateInputs(txid, address) { - // Use txid and output address to get value - // Get addr / value from prev out - // update input - -} module.exports = { parse, diff --git a/server/lib/parser/transaction.js b/server/lib/parser/transaction.js index b727417..993e825 100644 --- a/server/lib/parser/transaction.js +++ b/server/lib/parser/transaction.js @@ -4,6 +4,7 @@ const OutputModel = require('../../models/output'); const config = require('../../config'); const util = require('../../lib/util'); const logger = require('../logger'); +const db = require('../db'); function parse(entry, txs) { txs.forEach((tx) => { @@ -46,10 +47,28 @@ function parse(entry, txs) { chain: config.bcoin.network, }); + t.save((err) => { if (err) { logger.log('error', err.message); } + + t.inputs.forEach((input) => { + const txid = input.prevout.hash; + const idx = input.prevout.index; + const addr = input.address; + if (txid !== '0000000000000000000000000000000000000000000000000000000000000000') { + db.txs.getTxById(txid, (err, tx) => { + if (err) { + logger.log('err', + `Tx Parser inputs.ForEach: ${err}`); + return; + } + + db.txs.updateInput(t._id, input._id, tx.outputs[idx].value, tx.outputs[idx].address); + }); + } + }); }); }); }