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 logger = require('../logger');
|
||||||
const request = require('request');
|
const request = require('request');
|
||||||
const config = require('../../config');
|
const config = require('../../config');
|
||||||
const db = require('../db');
|
|
||||||
|
|
||||||
console.log(db);
|
const API_URL = `http://${config.bcoin_http}:${config.bcoin['http-port']}`;
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = function AddressAPI(router) {
|
module.exports = function AddressAPI(router) {
|
||||||
router.get('/addr/:addr', (req, res) => {
|
router.get('/addr/:addr', (req, res) => {
|
||||||
getBlock(
|
const addr = req.params.addr || '';
|
||||||
{},
|
|
||||||
{ height: 1 },
|
request(`${API_URL}/tx/address/${addr}`,
|
||||||
1,
|
(error, bcoinRes, txs) => {
|
||||||
(err, block) => {
|
if (error) {
|
||||||
if (err) {
|
logger.log('error',
|
||||||
res.status(501).send();
|
`${error}`);
|
||||||
logger.log('err', err);
|
|
||||||
}
|
}
|
||||||
if (block[0]) {
|
try {
|
||||||
const height = block[0].height;
|
txs = JSON.parse(txs);
|
||||||
request(`http://${config.bcoin_http}:${config.bcoin['http-port']}/tx/address/${req.params.addr}`, (err, localRes, body) => {
|
} catch (e) {
|
||||||
if (err) {
|
logger.log('error',
|
||||||
logger.log('error',
|
`${e}`);
|
||||||
`${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,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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) => {
|
router.get('/addr/:addr/utxo', (req, res) => {
|
||||||
res.send('1');
|
res.send('1');
|
||||||
});
|
});
|
||||||
@ -114,58 +84,10 @@ module.exports = function AddressAPI(router) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
router.get('/addrs/:addrs/txs', (req, res) => {
|
router.get('/addrs/:addrs/txs', (req, res) => {
|
||||||
getBlock(
|
res.send('8');
|
||||||
{
|
|
||||||
$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();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post('/addrs/txs', (req, res) => {
|
router.post('/addrs/txs', (req, res) => {
|
||||||
res.send('post stub');
|
res.send('9');
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@ -11,5 +11,5 @@ mongoose.connection.on('error', (err) => {
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
connect: mongoose.connect,
|
connect: mongoose.connect,
|
||||||
connection: mongoose.connection,
|
connection: mongoose.connection,
|
||||||
Blocks,
|
blocks: Blocks,
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user