Address API cleaned up. Unnecessary db calls removed
This commit is contained in:
parent
d278dbdb7c
commit
ecee54c10b
@ -1,90 +1,60 @@
|
||||
const Block = require('../../models/block.js');
|
||||
const logger = require('../logger');
|
||||
const request = require('request');
|
||||
const config = require('../../config');
|
||||
const db = require('../db');
|
||||
|
||||
console.log(db);
|
||||
|
||||
const MAX_BLOCKS = 200;
|
||||
|
||||
// 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);
|
||||
}
|
||||
const API_URL = `http://${config.bcoin_http}:${config.bcoin['http-port']}`;
|
||||
|
||||
module.exports = function AddressAPI(router) {
|
||||
router.get('/addr/:addr', (req, res) => {
|
||||
getBlock(
|
||||
{},
|
||||
{ height: 1 },
|
||||
1,
|
||||
(err, block) => {
|
||||
if (err) {
|
||||
res.status(501).send();
|
||||
logger.log('err', err);
|
||||
const addr = req.params.addr || '';
|
||||
|
||||
request(`${API_URL}/tx/address/${addr}`,
|
||||
(error, bcoinRes, txs) => {
|
||||
if (error) {
|
||||
logger.log('error',
|
||||
`${error}`);
|
||||
}
|
||||
if (block[0]) {
|
||||
const height = block[0].height;
|
||||
request(`http://${config.bcoin_http}:${config.bcoin['http-port']}/tx/address/${req.params.addr}`, (err, localRes, body) => {
|
||||
if (err) {
|
||||
logger.log('error',
|
||||
`${err}`);
|
||||
}
|
||||
try {
|
||||
body = JSON.parse(body);
|
||||
} catch (e) {
|
||||
logger.log('error',
|
||||
`${err}`);
|
||||
}
|
||||
|
||||
const totalReceived = body.reduce((sum, tx) => sum + tx.outputs.reduce((sum, output) => {
|
||||
if (output.address === req.params.addr) {
|
||||
return sum + output.value;
|
||||
}
|
||||
return sum;
|
||||
}, 0), 0) || 0;
|
||||
|
||||
const totalSpent = body.reduce((sum, tx) => sum + tx.inputs.reduce((sum, input) => {
|
||||
if (input.coin && input.coin.address === req.params.addr) {
|
||||
return sum + input.coin.value;
|
||||
}
|
||||
return sum;
|
||||
}, 0), 0) || 0;
|
||||
|
||||
|
||||
|
||||
res.json({
|
||||
addrStr: req.params.addr,
|
||||
balance: (totalReceived - totalSpent) / 1e8,
|
||||
balanceSat: totalReceived - totalSpent,
|
||||
totalReceived: totalReceived / 1e8,
|
||||
totalReceivedSat: totalReceived,
|
||||
totalSent: totalSpent / 1e8,
|
||||
totalSentSat: totalSpent,
|
||||
unconfirmedBalance: 0,
|
||||
unconfirmedBalanceSat: 0,
|
||||
unconfirmedTxApperances: 0,
|
||||
txApperances: body.length,
|
||||
});
|
||||
});
|
||||
try {
|
||||
txs = JSON.parse(txs);
|
||||
} catch (e) {
|
||||
logger.log('error',
|
||||
`${e}`);
|
||||
}
|
||||
|
||||
// Sum the matching outputs for every tx
|
||||
const totalReceived = txs.reduce((sum, tx) => sum + tx.outputs.reduce((sum, output) => {
|
||||
if (output.address === req.params.addr) {
|
||||
return sum + output.value;
|
||||
}
|
||||
return sum;
|
||||
}, 0), 0) || 0;
|
||||
|
||||
// Sum the matching inputs for every tx
|
||||
const totalSpent = txs.reduce((sum, tx) => sum + tx.inputs.reduce((sum, input) => {
|
||||
if (input.coin && input.coin.address === req.params.addr) {
|
||||
return sum + input.coin.value;
|
||||
}
|
||||
return sum;
|
||||
}, 0), 0) || 0;
|
||||
|
||||
// Match Insight API
|
||||
res.json({
|
||||
addrStr: req.params.addr,
|
||||
balance: (totalReceived - totalSpent) / 1e8,
|
||||
balanceSat: totalReceived - totalSpent,
|
||||
totalReceived: totalReceived / 1e8,
|
||||
totalReceivedSat: totalReceived,
|
||||
totalSent: totalSpent / 1e8,
|
||||
totalSentSat: totalSpent,
|
||||
unconfirmedBalance: 0,
|
||||
unconfirmedBalanceSat: 0,
|
||||
unconfirmedTxApperances: 0,
|
||||
txApperances: txs.length,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// Stubbed by # to help with tasking
|
||||
router.get('/addr/:addr/utxo', (req, res) => {
|
||||
res.send('1');
|
||||
});
|
||||
@ -114,58 +84,10 @@ module.exports = function AddressAPI(router) {
|
||||
});
|
||||
|
||||
router.get('/addrs/:addrs/txs', (req, res) => {
|
||||
getBlock(
|
||||
{
|
||||
$or:
|
||||
[
|
||||
{ 'txs.outputs.address': req.params.addr },
|
||||
{ 'txs.inputs.prevout.hash': req.params.addr },
|
||||
],
|
||||
},
|
||||
{ rawBlock: 0 },
|
||||
MAX_BLOCKS,
|
||||
(err, block) => {
|
||||
if (err) {
|
||||
res.status(501).send();
|
||||
logger.log('err', err);
|
||||
}
|
||||
|
||||
if (block[0]) {
|
||||
const b = block[0];
|
||||
res.json({
|
||||
pagesTotal: 1,
|
||||
txs: b.txs.map(tx => ({
|
||||
txid: tx.hash,
|
||||
version: tx.version,
|
||||
locktime: tx.locktime,
|
||||
vin: tx.inputs.map(input => ({
|
||||
coinbase: input.script,
|
||||
sequence: input.sequence,
|
||||
n: 0,
|
||||
addr: input.address,
|
||||
})),
|
||||
vout: tx.outputs.map(output => ({
|
||||
value: output.value / 1e8,
|
||||
n: 0,
|
||||
scriptPubKey: {
|
||||
hex: '',
|
||||
asm: '',
|
||||
addresses: [output.address],
|
||||
type: output.type,
|
||||
},
|
||||
spentTxid: '',
|
||||
spentIndex: 0,
|
||||
spentHeight: 0,
|
||||
})),
|
||||
})),
|
||||
});
|
||||
} else {
|
||||
res.send();
|
||||
}
|
||||
});
|
||||
res.send('8');
|
||||
});
|
||||
|
||||
router.post('/addrs/txs', (req, res) => {
|
||||
res.send('post stub');
|
||||
res.send('9');
|
||||
});
|
||||
};
|
||||
|
||||
@ -11,5 +11,5 @@ mongoose.connection.on('error', (err) => {
|
||||
module.exports = {
|
||||
connect: mongoose.connect,
|
||||
connection: mongoose.connection,
|
||||
Blocks,
|
||||
blocks: Blocks,
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user