From a2637f0cf1c31d2833abc992f8171dcce1974d4c Mon Sep 17 00:00:00 2001 From: pointbiz Date: Thu, 13 Oct 2022 17:03:18 -0400 Subject: [PATCH] tx api: add mempool data for unconfirmed tx --- routes/apiRouter.js | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/routes/apiRouter.js b/routes/apiRouter.js index 4b9130274..139aad075 100644 --- a/routes/apiRouter.js +++ b/routes/apiRouter.js @@ -144,26 +144,21 @@ router.get("/block/header/:hashOrHeight", asyncHandler(async (req, res, next) => /// TRANSACTIONS -router.get("/tx/:txid", function(req, res, next) { +router.get("/tx/:txid", asyncHandler(async (req, res, next) => { let txid = utils.asHash(req.params.txid); - let promises = []; - let txInputLimit = (res.locals.crawlerBot) ? 3 : -1; - promises.push(coreApi.getRawTransactionsWithInputs([txid], txInputLimit)); - - Promise.all(promises).then(function(results) { - let outJson = results[0].transactions[0]; - let txInputs = results[0].txInputsByTransaction[txid] || {}; + try { + var results = await coreApi.getRawTransactionsWithInputs([txid], txInputLimit); + let outJson = results.transactions[0]; + let txInputs = results.txInputsByTransaction[txid] || {}; let inputBtc = 0; if (txInputs[0]) { for (let key in txInputs) { let item = txInputs[key]; - inputBtc += item["value"] * global.coinConfig.baseCurrencyUnit.multiplier; - outJson.vin[key].scriptSig.address = item.scriptPubKey.address; outJson.vin[key].scriptSig.type = item.scriptPubKey.type; outJson.vin[key].value = item.value; @@ -172,8 +167,7 @@ router.get("/tx/:txid", function(req, res, next) { let outputBtc = 0; for (let key in outJson.vout) { - let item = outJson.vout[key]; - + let item = outJson.vout[key]; outputBtc += item.value * global.coinConfig.baseCurrencyUnit.multiplier; } @@ -182,24 +176,25 @@ router.get("/tx/:txid", function(req, res, next) { "unit": "BTC" }; + if (outJson.confirmations == null) { + outJson.mempool = await coreApi.getMempoolTxDetails(txid, false); + } + if (global.specialTransactions && global.specialTransactions[txid]) { let funInfo = global.specialTransactions[txid]; - outJson.fun = funInfo; } res.json(outJson); - - next(); - - }).catch(function(err) { + + } catch(err) { utils.logError("10328fwgdaqw", err); - res.json({success:false, error:err}); + }; + + next(); - next(); - }); -}); +})); router.get("/tx/volume/24h", function(req, res, next) { try {