From d2f68fefe9e9e568c8abf693a9ef20c87ebaaebc Mon Sep 17 00:00:00 2001 From: tenthirtyone Date: Thu, 17 Aug 2017 17:41:07 -0400 Subject: [PATCH] need new mongo models from another branch --- server/config/index.js | 3 ++- server/lib/api/transaction.js | 14 ++++++++++++-- server/lib/db/transactions.js | 32 +++++++++++++++++++++++++++++--- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/server/config/index.js b/server/config/index.js index 4c45558..50638e3 100644 --- a/server/config/index.js +++ b/server/config/index.js @@ -28,7 +28,8 @@ const config = { ticker_url: 'https://www.bitstamp.net/api/ticker/', ticker_prop: 'bitstamp', max_blocks: 72, - max_txs: 10, + max_txs: 50, + max_page_txs: 10, request_ttl: 100000, }, }; diff --git a/server/lib/api/transaction.js b/server/lib/api/transaction.js index d0f742b..15c79d1 100644 --- a/server/lib/api/transaction.js +++ b/server/lib/api/transaction.js @@ -86,7 +86,7 @@ module.exports = function transactionAPI(router) { return res.status(400).send({ error: 'Invalid block hash', }); - } + } const height = db.blocks.bestHeight(); // Get Bcoin data return request(`${API_URL}/block/${req.query.block}`, @@ -204,7 +204,17 @@ module.exports = function transactionAPI(router) { }); } // Get last n txs - return res.status(404).send({ error: 'Block hash or address expected' }); + console.log('GETTING N TXS'); + db.txs.getTopTransactions((err, txs) => { + if (err) { + logger.log('err', + `/txs getTopTransactions ${err}`); + return res.status(404).send(err); + } + return res.json(txs); + }); + + // return res.status(404).send({ error: 'Block hash or address expected' }); }); router.get('/rawtx/:txid', (req, res) => { diff --git a/server/lib/db/transactions.js b/server/lib/db/transactions.js index bfd90c3..68d93df 100644 --- a/server/lib/db/transactions.js +++ b/server/lib/db/transactions.js @@ -4,9 +4,12 @@ const config = require('../../config'); // For now, blocks handles these calls. // These will be replaced with more advanced mongo +// No optimization yet. -const MAX_TXS = config.api.max_txs; +const MAX_TXS = config.api.max_page_txs; +const MAX_PAGE_TXS = config.api.max_page_txs; +// For Paging function getTransactions(params, options, limit, cb) { // Do not return mongo ids const defaultOptions = { _id: 0 }; @@ -17,8 +20,8 @@ function getTransactions(params, options, limit, cb) { limit = 1; } - if (limit > MAX_TXS) { - limit = MAX_TXS; + if (limit > MAX_PAGE_TXS) { + limit = MAX_PAGE_TXS; } if (limit < 1) { @@ -57,7 +60,30 @@ function getTransaction(params, options, limit, cb) { }); } +// Req Change, refactor above +function getTopTransactions(cb) { + // Do not return mongo ids + const defaultOptions = { _id: 0 }; + // Query mongo + Transactions.find( + {}, + (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(MAX_TXS); +} + module.exports = { getTransaction, getTransactions, + getTopTransactions, };