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,106 +41,149 @@ 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) => {
request(`http://${config.bcoin_http}:${config.bcoin['http-port']}/tx/${req.params.txid}`, (err, localRes, body) => { getBlock(
if (err) { {},
logger.log('error', { height: 1 },
`${err}`); 1,
} (err, block) => {
try { if (err) {
body = JSON.parse(body); res.status(501).send();
} catch (e) { logger.log('err', err);
logger.log('error', }
`${err}`); if (block[0]) {
} const height = block[0].height;
res.send({ request(`http://${config.bcoin_http}:${config.bcoin['http-port']}/tx/${req.params.txid}`, (err, localRes, body) => {
txid: body.hash, if (err) {
version: body.version, logger.log('error',
time: body.ps, `${err}`);
blocktime: body.ps, }
locktime: body.locktime, try {
blockhash: body.block, body = JSON.parse(body);
fees: body.fee / 1e8, } catch (e) {
valueOut: body.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8, logger.log('error',
vin: body.inputs.map(input => ({ `${err}`);
addr: input.coin ? input.coin.address : '', }
value: input.coin ? input.coin.value / 1e8 : 0, res.send({
})), txid: body.hash,
vout: body.outputs.map(output => ({ version: body.version,
scriptPubKey: { time: body.ps,
addresses: [output.address], blocktime: body.ps,
}, locktime: body.locktime,
value: output.value / 1e8, blockhash: body.block,
})), fees: body.fee / 1e8,
isCoinbase: body.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000', confirmations: height - body.height,
valueOut: body.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8,
vin: body.inputs.map(input => ({
addr: input.coin ? input.coin.address : '',
value: input.coin ? input.coin.value / 1e8 : 0,
})),
vout: body.outputs.map(output => ({
scriptPubKey: {
addresses: [output.address],
},
value: output.value / 1e8,
})),
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) {
request(`http://${config.bcoin_http}:${config.bcoin['http-port']}/block/${req.query.block}`, (err, localRes, body) => { getBlock(
if (err) { {},
logger.log('error', { height: 1 },
`${err}`); 1,
} (err, block) => {
try { if (err) {
body = JSON.parse(body); res.status(501).send();
} catch (e) { logger.log('err', err);
logger.log('error', }
`${err}`); if (block[0]) {
} const height = block[0].height;
res.send({ request(`http://${config.bcoin_http}:${config.bcoin['http-port']}/block/${req.query.block}`, (err, localRes, body) => {
pagesTotal: 1, if (err) {
txs: body.txs.map(tx => ({ logger.log('error',
txid: tx.hash, `${err}`);
fees: tx.fee / 1e8, }
valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8, try {
vin: tx.inputs.map(input => ({ body = JSON.parse(body);
addr: input.coin ? input.coin.address : '', } catch (e) {
value: input.coin ? input.coin.value / 1e8 : 0, logger.log('error',
})), `${err}`);
vout: tx.outputs.map(output => ({ }
scriptPubKey: { res.send({
addresses: [output.address], pagesTotal: 1,
}, txs: body.txs.map(tx => ({
value: output.value / 1e8, txid: tx.hash,
})), fees: tx.fee / 1e8,
output: tx.outputs, confirmations: height - body.height,
})), 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,
})),
vout: tx.outputs.map(output => ({
scriptPubKey: {
addresses: [output.address],
},
value: output.value / 1e8,
})),
output: tx.outputs,
})),
});
});
}
}); });
});
} else if (req.query.address) { } else if (req.query.address) {
request(`http://${config.bcoin_http}:${config.bcoin['http-port']}/tx/address/${req.query.address}`, (err, localRes, body) => { getBlock(
if (err) { {},
logger.log('error', { height: 1 },
`${err}`); 1,
} (err, block) => {
try { if (err) {
body = JSON.parse(body); res.status(501).send();
} catch (e) { logger.log('err', err);
logger.log('error', }
`${err}`); if (block[0]) {
} const height = block[0].height;
res.send({ request(`http://${config.bcoin_http}:${config.bcoin['http-port']}/tx/address/${req.query.address}`, (err, localRes, body) => {
pagesTotal: 1, if (err) {
txs: body.map(tx => ({ logger.log('error',
txid: tx.hash, `${err}`);
fees: tx.fee / 1e8, }
valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8, try {
vin: tx.inputs.map(input => ({ body = JSON.parse(body);
addr: input.coin ? input.coin.address : '', } catch (e) {
value: input.coin ? input.coin.value / 1e8 : 0, logger.log('error',
})), `${err}`);
vout: tx.outputs.map(output => ({ }
scriptPubKey: { res.send({
addresses: [output.address], pagesTotal: 1,
}, txs: body.map(tx => ({
value: output.value / 1e8, txid: tx.hash,
})), fees: tx.fee / 1e8,
output: tx.outputs, confirmations: height - tx.height,
})), 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,
})),
vout: tx.outputs.map(output => ({
scriptPubKey: {
addresses: [output.address],
},
value: output.value / 1e8,
})),
output: tx.outputs,
})),
});
});
}
}); });
});
} else { } else {
getTransactions( getTransactions(
{}, {},