tx by address wired up

This commit is contained in:
tenthirtyone 2017-08-19 04:07:09 -04:00
parent 697388f1ce
commit 46842c9cc5

View File

@ -9,67 +9,45 @@ const TTL = config.api.request_ttl;
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 || '';
/*
db.txs.getTxByAddress(addr, 0, 999999999, (error, txs) => { db.txs.getTxByAddress(addr, 0, 999999999, (error, txs) => {
if (error) { if (error) {
logger.log('err', logger.log('error',
`getTxByBlock ${error}`); `getTxByBlock ${error}`);
return res.status(404).send(); return res.status(404).send();
} }
console.log(txs.count()); // Sum the matching outputs for every tx
const totalReceived = txs.reduce((total, tx) => total + tx.outputs.reduce((sum, output) => {
}); if (output.address === req.params.addr) {
*/ return sum + output.value;
// Get Bcoin data
return request(`${API_URL}/tx/address/${addr}`,
{ timeout: TTL },
(error, bcoinRes, bcoinTxs) => {
if (error) {
logger.log('error',
`${error}`);
return res.status(404).send({});
} }
let txs = {}; return sum;
try { }, 0), 0) || 0;
txs = JSON.parse(bcoinTxs);
} catch (e) { // Sum the matching inputs for every tx
logger.log('error', const totalSpent = txs.reduce((total, tx) => total + tx.inputs.reduce((sum, input) => {
`${e}`); if (input.coin && input.coin.address === req.params.addr) {
return res.status(404).send({}); return sum + input.coin.value;
} }
return sum;
}, 0), 0) || 0;
// Sum the matching outputs for every tx // Match Insight API
const totalReceived = txs.reduce((total, tx) => total + tx.outputs.reduce((sum, output) => { return res.json({
if (output.address === req.params.addr) { addrStr: req.params.addr,
return sum + output.value; balance: (totalReceived - totalSpent) / 1e8,
} balanceSat: totalReceived - totalSpent,
return sum; totalReceived: totalReceived / 1e8,
}, 0), 0) || 0; totalReceivedSat: totalReceived,
totalSent: totalSpent / 1e8,
// Sum the matching inputs for every tx totalSentSat: totalSpent,
const totalSpent = txs.reduce((total, tx) => total + tx.inputs.reduce((sum, input) => { unconfirmedBalance: 0,
if (input.coin && input.coin.address === req.params.addr) { unconfirmedBalanceSat: 0,
return sum + input.coin.value; unconfirmedTxApperances: 0,
} txApperances: txs.length,
return sum;
}, 0), 0) || 0;
// Match Insight API
return 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 // Stubbed by # to help with tasking