add tx paging and protections
This commit is contained in:
parent
6659148d4b
commit
358662fb84
@ -47,25 +47,26 @@ module.exports = function AddressAPI(router) {
|
|||||||
logger.log('error',
|
logger.log('error',
|
||||||
`${err}`);
|
`${err}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const totalReceived = body.reduce((sum, tx) => sum + tx.outputs.reduce((sum, output) => {
|
const totalReceived = body.reduce((sum, tx) => sum + tx.outputs.reduce((sum, output) => {
|
||||||
if (output.address === req.params.addr) {
|
if (output.address === req.params.addr) {
|
||||||
return sum + output.value;
|
return sum + output.value;
|
||||||
}
|
}
|
||||||
return sum;
|
return sum;
|
||||||
}, 0), 0);
|
}, 0), 0) || 0;
|
||||||
|
|
||||||
const totalSpent = body.reduce((sum, tx) => sum + tx.inputs.reduce((sum, input) => {
|
const totalSpent = body.reduce((sum, tx) => sum + tx.inputs.reduce((sum, input) => {
|
||||||
if (input.coin && input.coin.address === req.params.addr) {
|
if (input.coin && input.coin.address === req.params.addr) {
|
||||||
return sum + input.coin.value;
|
return sum + input.coin.value;
|
||||||
}
|
}
|
||||||
return sum;
|
return sum;
|
||||||
}, 0), 0);
|
}, 0), 0) || 0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
addrStr: req.params.addr,
|
addrStr: req.params.addr,
|
||||||
balance: totalReceived - totalSpent,
|
balance: (totalReceived - totalSpent) / 1e8,
|
||||||
balanceSat: totalReceived - totalSpent,
|
balanceSat: totalReceived - totalSpent,
|
||||||
totalReceived: totalReceived / 1e8,
|
totalReceived: totalReceived / 1e8,
|
||||||
totalReceivedSat: totalReceived,
|
totalReceivedSat: totalReceived,
|
||||||
|
|||||||
@ -4,7 +4,7 @@ const logger = require('../logger');
|
|||||||
const request = require('request');
|
const request = require('request');
|
||||||
const config = require('../../config');
|
const config = require('../../config');
|
||||||
|
|
||||||
const MAX_TXS = 20;
|
const MAX_TXS = 10;
|
||||||
const MAX_BLOCKS = 1;
|
const MAX_BLOCKS = 1;
|
||||||
|
|
||||||
// Shoe horned in. Not dry, also in blocks. Make db api later
|
// Shoe horned in. Not dry, also in blocks. Make db api later
|
||||||
@ -100,6 +100,10 @@ module.exports = function transactionAPI(router) {
|
|||||||
|
|
||||||
// That callback hell
|
// That callback hell
|
||||||
router.get('/txs', (req, res) => {
|
router.get('/txs', (req, res) => {
|
||||||
|
const pageNum = parseInt(req.query.pageNum) || 0;
|
||||||
|
const rangeStart = pageNum * MAX_TXS;
|
||||||
|
const rangeEnd = rangeStart + MAX_TXS;
|
||||||
|
|
||||||
if (req.query.block) {
|
if (req.query.block) {
|
||||||
getBlock(
|
getBlock(
|
||||||
{},
|
{},
|
||||||
@ -111,6 +115,7 @@ module.exports = function transactionAPI(router) {
|
|||||||
logger.log('err', err);
|
logger.log('err', err);
|
||||||
}
|
}
|
||||||
if (block[0]) {
|
if (block[0]) {
|
||||||
|
|
||||||
const height = block[0].height;
|
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) {
|
||||||
@ -122,9 +127,18 @@ module.exports = function transactionAPI(router) {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.log('error',
|
logger.log('error',
|
||||||
`${err}`);
|
`${err}`);
|
||||||
|
res.status(501).send();
|
||||||
}
|
}
|
||||||
|
if (!body.txs.length) {
|
||||||
|
logger.log('error',
|
||||||
|
`${'No tx results'}`);
|
||||||
|
res.status(501).send();
|
||||||
|
}
|
||||||
|
const totalPages = Math.ceil(body.txs.length / MAX_TXS);
|
||||||
|
body.txs = body.txs.slice(rangeStart, rangeEnd);
|
||||||
|
|
||||||
res.send({
|
res.send({
|
||||||
pagesTotal: 1,
|
pagesTotal: totalPages,
|
||||||
txs: body.txs.map(tx => ({
|
txs: body.txs.map(tx => ({
|
||||||
txid: tx.hash,
|
txid: tx.hash,
|
||||||
fees: tx.fee / 1e8,
|
fees: tx.fee / 1e8,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user