Confirmations wired up for blocks txs and addr views

This commit is contained in:
tenthirtyone 2017-08-15 04:09:28 -04:00
parent dd0e0bedf0
commit 2b8eee6e03

View File

@ -41,6 +41,17 @@ function getTransactions(params, options, cb) {
module.exports = function transactionAPI(router) { module.exports = function transactionAPI(router) {
router.get('/tx/:txid', (req, res) => { router.get('/tx/:txid', (req, res) => {
getBlock(
{},
{ height: 1 },
1,
(err, block) => {
if (err) {
res.status(501).send();
logger.log('err', err);
}
if (block[0]) {
const height = block[0].height;
request(`http://${config.bcoin_http}:${config.bcoin['http-port']}/tx/${req.params.txid}`, (err, localRes, body) => { request(`http://${config.bcoin_http}:${config.bcoin['http-port']}/tx/${req.params.txid}`, (err, localRes, body) => {
if (err) { if (err) {
logger.log('error', logger.log('error',
@ -60,6 +71,7 @@ module.exports = function transactionAPI(router) {
locktime: body.locktime, locktime: body.locktime,
blockhash: body.block, blockhash: body.block,
fees: body.fee / 1e8, fees: body.fee / 1e8,
confirmations: height - body.height,
valueOut: body.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8, valueOut: body.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8,
vin: body.inputs.map(input => ({ vin: body.inputs.map(input => ({
addr: input.coin ? input.coin.address : '', addr: input.coin ? input.coin.address : '',
@ -74,10 +86,24 @@ module.exports = function transactionAPI(router) {
isCoinbase: body.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000', isCoinbase: body.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000',
}); });
}); });
}
});
}); });
// That callback hell
router.get('/txs', (req, res) => { router.get('/txs', (req, res) => {
if (req.query.block) { if (req.query.block) {
getBlock(
{},
{ height: 1 },
1,
(err, block) => {
if (err) {
res.status(501).send();
logger.log('err', err);
}
if (block[0]) {
const height = block[0].height;
request(`http://${config.bcoin_http}:${config.bcoin['http-port']}/block/${req.query.block}`, (err, localRes, body) => { request(`http://${config.bcoin_http}:${config.bcoin['http-port']}/block/${req.query.block}`, (err, localRes, body) => {
if (err) { if (err) {
logger.log('error', logger.log('error',
@ -94,6 +120,7 @@ module.exports = function transactionAPI(router) {
txs: body.txs.map(tx => ({ txs: body.txs.map(tx => ({
txid: tx.hash, txid: tx.hash,
fees: tx.fee / 1e8, fees: tx.fee / 1e8,
confirmations: height - body.height,
valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8, valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8,
vin: tx.inputs.map(input => ({ vin: tx.inputs.map(input => ({
addr: input.coin ? input.coin.address : '', addr: input.coin ? input.coin.address : '',
@ -109,7 +136,20 @@ module.exports = function transactionAPI(router) {
})), })),
}); });
}); });
}
});
} else if (req.query.address) { } else if (req.query.address) {
getBlock(
{},
{ height: 1 },
1,
(err, block) => {
if (err) {
res.status(501).send();
logger.log('err', err);
}
if (block[0]) {
const height = block[0].height;
request(`http://${config.bcoin_http}:${config.bcoin['http-port']}/tx/address/${req.query.address}`, (err, localRes, body) => { request(`http://${config.bcoin_http}:${config.bcoin['http-port']}/tx/address/${req.query.address}`, (err, localRes, body) => {
if (err) { if (err) {
logger.log('error', logger.log('error',
@ -126,6 +166,7 @@ module.exports = function transactionAPI(router) {
txs: body.map(tx => ({ txs: body.map(tx => ({
txid: tx.hash, txid: tx.hash,
fees: tx.fee / 1e8, fees: tx.fee / 1e8,
confirmations: height - tx.height,
valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8, valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8,
vin: tx.inputs.map(input => ({ vin: tx.inputs.map(input => ({
addr: input.coin ? input.coin.address : '', addr: input.coin ? input.coin.address : '',
@ -141,6 +182,8 @@ module.exports = function transactionAPI(router) {
})), })),
}); });
}); });
}
});
} else { } else {
getTransactions( getTransactions(
{}, {},