transactions added to db api
This commit is contained in:
parent
1d029fcf7f
commit
2eac46fdb5
@ -1,24 +1,10 @@
|
||||
const Transaction = require('../../models/transaction');
|
||||
const logger = require('../logger');
|
||||
const request = require('request');
|
||||
const config = require('../../config');
|
||||
const db = require('../db');
|
||||
|
||||
const MAX_TXS = 10;
|
||||
const API_URL = `http://${config.bcoin_http}:${config.bcoin['http-port']}`;
|
||||
|
||||
function getTransactions(params, options, cb) {
|
||||
const defaultOptions = { _id: 0 };
|
||||
|
||||
Object.assign(defaultOptions, options);
|
||||
|
||||
Transaction.find(
|
||||
params,
|
||||
defaultOptions,
|
||||
cb)
|
||||
.sort({ height: 1 })
|
||||
.limit(MAX_TXS);
|
||||
}
|
||||
const MAX_TXS = 50;
|
||||
|
||||
module.exports = function transactionAPI(router) {
|
||||
router.get('/tx/:txid', (req, res) => {
|
||||
@ -38,22 +24,21 @@ module.exports = function transactionAPI(router) {
|
||||
if (error) {
|
||||
logger.log('error',
|
||||
`${error}`);
|
||||
return res.status(404).send();
|
||||
}
|
||||
try {
|
||||
body = JSON.parse(body);
|
||||
} catch (e) {
|
||||
logger.log('error',
|
||||
`${e}`);
|
||||
res.status(404).send();
|
||||
return;
|
||||
return res.status(404).send();
|
||||
}
|
||||
if (!body || !body.hash) {
|
||||
logger.log('error',
|
||||
'No results found');
|
||||
res.status(404).send();
|
||||
return;
|
||||
return res.status(404).send();
|
||||
}
|
||||
res.send({
|
||||
return res.send({
|
||||
txid: body.hash,
|
||||
version: body.version,
|
||||
time: body.ps,
|
||||
@ -188,14 +173,17 @@ module.exports = function transactionAPI(router) {
|
||||
});
|
||||
});
|
||||
} else {
|
||||
getTransactions(
|
||||
db.txs.getTransactions(
|
||||
{},
|
||||
{},
|
||||
50,
|
||||
(err, txs) => {
|
||||
if (err) {
|
||||
logger.log('err',
|
||||
`getTransactions: ${err}`);
|
||||
res.status(404).send();
|
||||
}
|
||||
res.json({
|
||||
return res.json({
|
||||
pagesTotal: 1,
|
||||
txs: txs.map(tx => ({
|
||||
txid: tx.hash,
|
||||
|
||||
@ -3,8 +3,7 @@ const logger = require('../logger');
|
||||
const config = require('../../config');
|
||||
|
||||
// move to config
|
||||
const MAX_BLOCKS = 50;
|
||||
const blockTemplate = new Block();
|
||||
const MAX_BLOCKS = 72; // ~ 12 hours
|
||||
|
||||
function getBlocks(params, options, limit, cb) {
|
||||
const defaultOptions = { _id: 0 };
|
||||
@ -33,7 +32,7 @@ function getBlocks(params, options, limit, cb) {
|
||||
return cb(err);
|
||||
}
|
||||
if (!blocks.length > 0) {
|
||||
return cb({err: 'Block not found'});
|
||||
return cb({ err: 'Block not found' });
|
||||
}
|
||||
return cb(null, blocks);
|
||||
})
|
||||
@ -49,7 +48,7 @@ function getBlock(params, options, limit, cb) {
|
||||
return cb(err);
|
||||
}
|
||||
if (!blocks.length > 0) {
|
||||
return cb(null, blockTemplate);
|
||||
return cb({ err: 'Block not found' });
|
||||
}
|
||||
return cb(null, blocks[0]);
|
||||
});
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
const mongoose = require('mongoose');
|
||||
const logger = require('../logger');
|
||||
const Blocks = require('./blocks');
|
||||
const Txs = require('./transactions');
|
||||
|
||||
mongoose.connection.on('error', (err) => {
|
||||
logger.log('error',
|
||||
@ -12,4 +13,5 @@ module.exports = {
|
||||
connect: mongoose.connect,
|
||||
connection: mongoose.connection,
|
||||
blocks: Blocks,
|
||||
txs: Txs,
|
||||
};
|
||||
|
||||
60
server/lib/db/transactions.js
Normal file
60
server/lib/db/transactions.js
Normal 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,
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user