transactions added to db api

This commit is contained in:
tenthirtyone 2017-08-16 00:17:45 -04:00
parent 1d029fcf7f
commit 2eac46fdb5
4 changed files with 75 additions and 26 deletions

View File

@ -1,24 +1,10 @@
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 db = require('../db');
const MAX_TXS = 10;
const API_URL = `http://${config.bcoin_http}:${config.bcoin['http-port']}`; const API_URL = `http://${config.bcoin_http}:${config.bcoin['http-port']}`;
const MAX_TXS = 50;
function getTransactions(params, options, cb) {
const defaultOptions = { _id: 0 };
Object.assign(defaultOptions, options);
Transaction.find(
params,
defaultOptions,
cb)
.sort({ height: 1 })
.limit(MAX_TXS);
}
module.exports = function transactionAPI(router) { module.exports = function transactionAPI(router) {
router.get('/tx/:txid', (req, res) => { router.get('/tx/:txid', (req, res) => {
@ -38,22 +24,21 @@ module.exports = function transactionAPI(router) {
if (error) { if (error) {
logger.log('error', logger.log('error',
`${error}`); `${error}`);
return res.status(404).send();
} }
try { try {
body = JSON.parse(body); body = JSON.parse(body);
} catch (e) { } catch (e) {
logger.log('error', logger.log('error',
`${e}`); `${e}`);
res.status(404).send(); return res.status(404).send();
return;
} }
if (!body || !body.hash) { if (!body || !body.hash) {
logger.log('error', logger.log('error',
'No results found'); 'No results found');
res.status(404).send(); return res.status(404).send();
return;
} }
res.send({ return res.send({
txid: body.hash, txid: body.hash,
version: body.version, version: body.version,
time: body.ps, time: body.ps,
@ -188,14 +173,17 @@ module.exports = function transactionAPI(router) {
}); });
}); });
} else { } else {
getTransactions( db.txs.getTransactions(
{}, {},
{}, {},
50,
(err, txs) => { (err, txs) => {
if (err) { if (err) {
logger.log('err',
`getTransactions: ${err}`);
res.status(404).send(); res.status(404).send();
} }
res.json({ return res.json({
pagesTotal: 1, pagesTotal: 1,
txs: txs.map(tx => ({ txs: txs.map(tx => ({
txid: tx.hash, txid: tx.hash,

View File

@ -3,8 +3,7 @@ const logger = require('../logger');
const config = require('../../config'); const config = require('../../config');
// move to config // move to config
const MAX_BLOCKS = 50; const MAX_BLOCKS = 72; // ~ 12 hours
const blockTemplate = new Block();
function getBlocks(params, options, limit, cb) { function getBlocks(params, options, limit, cb) {
const defaultOptions = { _id: 0 }; const defaultOptions = { _id: 0 };
@ -33,7 +32,7 @@ function getBlocks(params, options, limit, cb) {
return cb(err); return cb(err);
} }
if (!blocks.length > 0) { if (!blocks.length > 0) {
return cb({err: 'Block not found'}); return cb({ err: 'Block not found' });
} }
return cb(null, blocks); return cb(null, blocks);
}) })
@ -49,7 +48,7 @@ function getBlock(params, options, limit, cb) {
return cb(err); return cb(err);
} }
if (!blocks.length > 0) { if (!blocks.length > 0) {
return cb(null, blockTemplate); return cb({ err: 'Block not found' });
} }
return cb(null, blocks[0]); return cb(null, blocks[0]);
}); });

View File

@ -1,6 +1,7 @@
const mongoose = require('mongoose'); const mongoose = require('mongoose');
const logger = require('../logger'); const logger = require('../logger');
const Blocks = require('./blocks'); const Blocks = require('./blocks');
const Txs = require('./transactions');
mongoose.connection.on('error', (err) => { mongoose.connection.on('error', (err) => {
logger.log('error', logger.log('error',
@ -12,4 +13,5 @@ module.exports = {
connect: mongoose.connect, connect: mongoose.connect,
connection: mongoose.connection, connection: mongoose.connection,
blocks: Blocks, blocks: Blocks,
txs: Txs,
}; };

View File

@ -0,0 +1,60 @@
const Transactions = require('../../models/transaction.js');
const logger = require('../logger');
const config = require('../../config');
// move to config
const MAX_TXS = 50;
function getTransactions(params, options, limit, cb) {
const defaultOptions = { _id: 0 };
Object.assign(defaultOptions, options);
if (!Number.isInteger(limit)) {
limit = 1;
}
if (limit > MAX_TXS) {
limit = MAX_TXS;
}
if (limit < 1) {
limit = 1;
}
Transactions.find(
params,
defaultOptions,
(err, txs) => {
if (err) {
logger.log('error',
`getTransactions: ${err}`);
return cb(err);
}
if (!txs.length > 0) {
return cb({ err: 'Tx not found' });
}
return cb(null, txs);
})
.sort({ height: -1 })
.limit(limit);
}
function getTransaction(params, options, limit, cb) {
getTransactions(params, options, limit, (err, tx) => {
if (err) {
logger.log('error',
`getBlock: ${err.err}`);
return cb(err);
}
if (!tx.length > 0) {
return cb({ err: 'Tx not found' });
}
return cb(null, tx[0]);
});
}
module.exports = {
getTransaction,
getTransactions,
};