getBlocks completely in db api
This commit is contained in:
parent
b0f95d93ab
commit
1d029fcf7f
@ -7,18 +7,20 @@ const API_URL = `http://${config.bcoin_http}:${config.bcoin['http-port']}`;
|
|||||||
module.exports = function AddressAPI(router) {
|
module.exports = function AddressAPI(router) {
|
||||||
router.get('/addr/:addr', (req, res) => {
|
router.get('/addr/:addr', (req, res) => {
|
||||||
const addr = req.params.addr || '';
|
const addr = req.params.addr || '';
|
||||||
|
// Get Bcoin data
|
||||||
request(`${API_URL}/tx/address/${addr}`,
|
request(`${API_URL}/tx/address/${addr}`,
|
||||||
(error, bcoinRes, txs) => {
|
(error, bcoinRes, txs) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
logger.log('error',
|
logger.log('error',
|
||||||
`${error}`);
|
`${error}`);
|
||||||
|
return res.status(404).send({});
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
txs = JSON.parse(txs);
|
txs = JSON.parse(txs);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.log('error',
|
logger.log('error',
|
||||||
`${e}`);
|
`${e}`);
|
||||||
|
return res.status(404).send({});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sum the matching outputs for every tx
|
// Sum the matching outputs for every tx
|
||||||
@ -38,7 +40,7 @@ module.exports = function AddressAPI(router) {
|
|||||||
}, 0), 0) || 0;
|
}, 0), 0) || 0;
|
||||||
|
|
||||||
// Match Insight API
|
// Match Insight API
|
||||||
res.json({
|
return res.json({
|
||||||
addrStr: req.params.addr,
|
addrStr: req.params.addr,
|
||||||
balance: (totalReceived - totalSpent) / 1e8,
|
balance: (totalReceived - totalSpent) / 1e8,
|
||||||
balanceSat: totalReceived - totalSpent,
|
balanceSat: totalReceived - totalSpent,
|
||||||
|
|||||||
@ -14,6 +14,17 @@ app.use(bodyParser.json());
|
|||||||
|
|
||||||
// Serve insight ui front end from root dir public folder
|
// Serve insight ui front end from root dir public folder
|
||||||
app.use(express.static('./public'));
|
app.use(express.static('./public'));
|
||||||
|
app.use('/:stuff', express.static('./public'));
|
||||||
|
app.use('/blocks', express.static('./public'));
|
||||||
|
app.use('/blocks/:blockhash', express.static('./public'));
|
||||||
|
app.use('/block-index', express.static('./public'));
|
||||||
|
app.use('/block-index/:height', express.static('./public'));
|
||||||
|
app.use('/blocks-date/:date', express.static('./public'));
|
||||||
|
app.use('/block/:blockhash', express.static('./public'));
|
||||||
|
app.use('/tx/:txid', express.static('./public'));
|
||||||
|
app.use('/address/:addr', express.static('./public'));
|
||||||
|
app.use('/status', express.static('./public'));
|
||||||
|
app.use('/status/:stuff', express.static('./public'));
|
||||||
|
|
||||||
app.set('json spaces', config.api.json_spaces);
|
app.set('json spaces', config.api.json_spaces);
|
||||||
|
|
||||||
|
|||||||
@ -46,7 +46,7 @@ module.exports = function statusAPI(router) {
|
|||||||
} else {
|
} else {
|
||||||
getStatus((err, status) => {
|
getStatus((err, status) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.log('err'
|
logger.log('err',
|
||||||
`/status getStatus: ${err}`);
|
`/status getStatus: ${err}`);
|
||||||
return res.status(404).send(err);
|
return res.status(404).send(err);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,30 +1,11 @@
|
|||||||
const Block = require('../../models/block.js');
|
|
||||||
const Transaction = require('../../models/transaction');
|
const Transaction = require('../../models/transaction');
|
||||||
const logger = require('../logger');
|
const logger = require('../logger');
|
||||||
const request = require('request');
|
const request = require('request');
|
||||||
const config = require('../../config');
|
const config = require('../../config');
|
||||||
|
const db = require('../db');
|
||||||
|
|
||||||
const MAX_TXS = 10;
|
const MAX_TXS = 10;
|
||||||
const MAX_BLOCKS = 1;
|
const API_URL = `http://${config.bcoin_http}:${config.bcoin['http-port']}`;
|
||||||
|
|
||||||
// Shoe horned in. Not dry, also in blocks. Make db api later
|
|
||||||
function getBlock(params, options, limit, cb) {
|
|
||||||
const defaultOptions = { _id: 0 };
|
|
||||||
|
|
||||||
if (!Number.isInteger(limit)) {
|
|
||||||
limit = MAX_BLOCKS;
|
|
||||||
}
|
|
||||||
|
|
||||||
Object.assign(defaultOptions, options);
|
|
||||||
|
|
||||||
Block.find(
|
|
||||||
params,
|
|
||||||
defaultOptions,
|
|
||||||
cb)
|
|
||||||
.sort({ height: -1 })
|
|
||||||
.limit(limit);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function getTransactions(params, options, cb) {
|
function getTransactions(params, options, cb) {
|
||||||
const defaultOptions = { _id: 0 };
|
const defaultOptions = { _id: 0 };
|
||||||
@ -41,61 +22,60 @@ 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(
|
db.blocks.getBlock(
|
||||||
{},
|
{},
|
||||||
{ height: 1 },
|
{ height: 1 },
|
||||||
1,
|
1,
|
||||||
(err, block) => {
|
(err, block) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
res.status(404).send();
|
|
||||||
logger.log('err', err);
|
logger.log('err', err);
|
||||||
|
return res.status(404).send();
|
||||||
}
|
}
|
||||||
if (block[0]) {
|
|
||||||
const height = block[0].height;
|
|
||||||
|
|
||||||
request(`http://${config.bcoin_http}:${config.bcoin['http-port']}/tx/${req.params.txid}`, (err, localRes, body) => {
|
const height = block.height;
|
||||||
if (err) {
|
|
||||||
logger.log('error',
|
request(`${API_URL}/tx/${req.params.txid}`, (error, localRes, body) => {
|
||||||
`${err}`);
|
if (error) {
|
||||||
}
|
logger.log('error',
|
||||||
try {
|
`${error}`);
|
||||||
body = JSON.parse(body);
|
}
|
||||||
} catch (e) {
|
try {
|
||||||
logger.log('error',
|
body = JSON.parse(body);
|
||||||
`${err}`);
|
} catch (e) {
|
||||||
res.status(404).send();
|
logger.log('error',
|
||||||
return;
|
`${e}`);
|
||||||
}
|
res.status(404).send();
|
||||||
if (!body || !body.hash) {
|
return;
|
||||||
logger.log('error',
|
}
|
||||||
'No results found');
|
if (!body || !body.hash) {
|
||||||
res.status(404).send();
|
logger.log('error',
|
||||||
return;
|
'No results found');
|
||||||
}
|
res.status(404).send();
|
||||||
res.send({
|
return;
|
||||||
txid: body.hash,
|
}
|
||||||
version: body.version,
|
res.send({
|
||||||
time: body.ps,
|
txid: body.hash,
|
||||||
blocktime: body.ps,
|
version: body.version,
|
||||||
locktime: body.locktime,
|
time: body.ps,
|
||||||
blockhash: body.block,
|
blocktime: body.ps,
|
||||||
fees: body.fee / 1e8,
|
locktime: body.locktime,
|
||||||
confirmations: height - body.height + 1,
|
blockhash: body.block,
|
||||||
valueOut: body.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8,
|
fees: body.fee / 1e8,
|
||||||
vin: body.inputs.map(input => ({
|
confirmations: height - body.height + 1,
|
||||||
addr: input.coin ? input.coin.address : '',
|
valueOut: body.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8,
|
||||||
value: input.coin ? input.coin.value / 1e8 : 0,
|
vin: body.inputs.map(input => ({
|
||||||
})),
|
addr: input.coin ? input.coin.address : '',
|
||||||
vout: body.outputs.map(output => ({
|
value: input.coin ? input.coin.value / 1e8 : 0,
|
||||||
scriptPubKey: {
|
})),
|
||||||
addresses: [output.address],
|
vout: body.outputs.map(output => ({
|
||||||
},
|
scriptPubKey: {
|
||||||
value: output.value / 1e8,
|
addresses: [output.address],
|
||||||
})),
|
},
|
||||||
isCoinbase: body.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000',
|
value: output.value / 1e8,
|
||||||
});
|
})),
|
||||||
|
isCoinbase: body.inputs[0].prevout.hash === '0000000000000000000000000000000000000000000000000000000000000000',
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -106,105 +86,106 @@ module.exports = function transactionAPI(router) {
|
|||||||
const rangeEnd = rangeStart + MAX_TXS;
|
const rangeEnd = rangeStart + MAX_TXS;
|
||||||
|
|
||||||
if (req.query.block) {
|
if (req.query.block) {
|
||||||
getBlock(
|
db.blocks.getBlock(
|
||||||
{},
|
{},
|
||||||
{ height: 1 },
|
{ height: 1 },
|
||||||
1,
|
1,
|
||||||
(err, block) => {
|
(err, block) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
res.status(404).send();
|
|
||||||
logger.log('err', err);
|
logger.log('err', err);
|
||||||
|
return res.status(404).send();
|
||||||
}
|
}
|
||||||
if (block[0]) {
|
const height = block.height;
|
||||||
const height = block[0].height;
|
request(`${API_URL}/block/${req.query.block}`, (error, localRes, block) => {
|
||||||
request(`http://${config.bcoin_http}:${config.bcoin['http-port']}/block/${req.query.block}`, (err, localRes, body) => {
|
if (error) {
|
||||||
if (err) {
|
logger.log('error',
|
||||||
logger.log('error',
|
`${error}`);
|
||||||
`${err}`);
|
}
|
||||||
}
|
try {
|
||||||
try {
|
block = JSON.parse(block);
|
||||||
body = JSON.parse(body);
|
} catch (e) {
|
||||||
} catch (e) {
|
logger.log('error',
|
||||||
logger.log('error',
|
`${e}`);
|
||||||
`${err}`);
|
return res.status(404).send();
|
||||||
res.status(404).send();
|
}
|
||||||
}
|
if (!block.txs.length) {
|
||||||
if (!body.txs.length) {
|
logger.log('error',
|
||||||
logger.log('error',
|
`${'No tx results'}`);
|
||||||
`${'No tx results'}`);
|
res.status(404).send();
|
||||||
res.status(404).send();
|
}
|
||||||
}
|
const totalPages = Math.ceil(block.txs.length / MAX_TXS);
|
||||||
const totalPages = Math.ceil(body.txs.length / MAX_TXS);
|
block.txs = block.txs.slice(rangeStart, rangeEnd);
|
||||||
body.txs = body.txs.slice(rangeStart, rangeEnd);
|
|
||||||
|
|
||||||
res.send({
|
return res.send({
|
||||||
pagesTotal: totalPages,
|
pagesTotal: totalPages,
|
||||||
txs: body.txs.map(tx => ({
|
txs: block.txs.map(tx => ({
|
||||||
txid: tx.hash,
|
txid: tx.hash,
|
||||||
fees: tx.fee / 1e8,
|
fees: tx.fee / 1e8,
|
||||||
confirmations: height - body.height + 1,
|
confirmations: height - block.height + 1,
|
||||||
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 : '',
|
||||||
value: input.coin ? input.coin.value / 1e8 : 0,
|
value: input.coin ? input.coin.value / 1e8 : 0,
|
||||||
})),
|
|
||||||
vout: tx.outputs.map(output => ({
|
|
||||||
scriptPubKey: {
|
|
||||||
addresses: [output.address],
|
|
||||||
},
|
|
||||||
value: output.value / 1e8,
|
|
||||||
})),
|
|
||||||
output: tx.outputs,
|
|
||||||
})),
|
})),
|
||||||
});
|
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) {
|
||||||
getBlock(
|
db.blocks.getBlock(
|
||||||
{},
|
{},
|
||||||
{ height: 1 },
|
{ height: 1 },
|
||||||
1,
|
1,
|
||||||
(err, block) => {
|
(err, block) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
res.status(404).send();
|
|
||||||
logger.log('err', err);
|
logger.log('err', err);
|
||||||
|
return res.status(404).send();
|
||||||
}
|
}
|
||||||
if (block[0]) {
|
|
||||||
const height = block[0].height;
|
const height = block.height;
|
||||||
request(`http://${config.bcoin_http}:${config.bcoin['http-port']}/tx/address/${req.query.address}`, (err, localRes, body) => {
|
const addr = req.query.address || '';
|
||||||
if (err) {
|
|
||||||
logger.log('error',
|
request(`${API_URL}/tx/address/${req.query.address}`, (error, localRes, txs) => {
|
||||||
`${err}`);
|
if (error) {
|
||||||
}
|
logger.log('error',
|
||||||
try {
|
`${error}`);
|
||||||
body = JSON.parse(body);
|
return res.status(404).send();
|
||||||
} catch (e) {
|
}
|
||||||
logger.log('error',
|
try {
|
||||||
`${err}`);
|
txs = JSON.parse(txs);
|
||||||
}
|
} catch (e) {
|
||||||
res.send({
|
logger.log('error',
|
||||||
pagesTotal: 1,
|
`${e}`);
|
||||||
txs: body.map(tx => ({
|
return res.status(404).send();
|
||||||
txid: tx.hash,
|
}
|
||||||
fees: tx.fee / 1e8,
|
return res.send({
|
||||||
confirmations: height - tx.height + 1,
|
pagesTotal: 1,
|
||||||
valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8,
|
txs: txs.map(tx => ({
|
||||||
vin: tx.inputs.map(input => ({
|
txid: tx.hash,
|
||||||
addr: input.coin ? input.coin.address : '',
|
fees: tx.fee / 1e8,
|
||||||
value: input.coin ? input.coin.value / 1e8 : 0,
|
confirmations: height - tx.height + 1,
|
||||||
})),
|
valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8,
|
||||||
vout: tx.outputs.map(output => ({
|
vin: tx.inputs.map(input => ({
|
||||||
scriptPubKey: {
|
addr: input.coin ? input.coin.address : '',
|
||||||
addresses: [output.address],
|
value: input.coin ? input.coin.value / 1e8 : 0,
|
||||||
},
|
|
||||||
value: output.value / 1e8,
|
|
||||||
})),
|
|
||||||
output: tx.outputs,
|
|
||||||
})),
|
})),
|
||||||
});
|
vout: tx.outputs.map(output => ({
|
||||||
|
scriptPubKey: {
|
||||||
|
addresses: [output.address],
|
||||||
|
},
|
||||||
|
value: output.value / 1e8,
|
||||||
|
})),
|
||||||
|
output: tx.outputs,
|
||||||
|
})),
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
getTransactions(
|
getTransactions(
|
||||||
@ -253,7 +234,7 @@ module.exports = function transactionAPI(router) {
|
|||||||
const rawtx = req.body.rawtx || '';
|
const rawtx = req.body.rawtx || '';
|
||||||
request.post({
|
request.post({
|
||||||
url: `http://${config.bcoin_http}:${config.bcoin['http-port']}/broadcast`,
|
url: `http://${config.bcoin_http}:${config.bcoin['http-port']}/broadcast`,
|
||||||
body: {"tx": rawtx },
|
body: { tx: rawtx },
|
||||||
json: true,
|
json: true,
|
||||||
}, (err, localRes, body) => {
|
}, (err, localRes, body) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
|||||||
@ -45,13 +45,13 @@ function getBlock(params, options, limit, cb) {
|
|||||||
getBlocks(params, options, limit, (err, blocks) => {
|
getBlocks(params, options, limit, (err, blocks) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.log('error',
|
logger.log('error',
|
||||||
`getBlock: ${err}`);
|
`getBlock: ${err.err}`);
|
||||||
return cb(err);
|
return cb(err);
|
||||||
}
|
}
|
||||||
if (blocks.length > 0) {
|
if (!blocks.length > 0) {
|
||||||
return cb(null, blocks[0]);
|
return cb(null, blockTemplate);
|
||||||
}
|
}
|
||||||
return cb(null, blockTemplate);
|
return cb(null, blocks[0]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user