insight-ui-zcash/app/controllers/transactions.js

154 lines
3.0 KiB
JavaScript

'use strict';
/**
* Module dependencies.
*/
var Address = require('../models/Address');
var async = require('async');
var common = require('./common');
var TransactionDb = require('../../lib/TransactionDb').class();
var BlockDb = require('../../lib/BlockDb').class();
var bdb = new BlockDb();
/**
* Find transaction by hash ...
*/
exports.transaction = function(req, res, next, txid) {
var tDb = new TransactionDb();
tDb.fromIdWithInfo(txid, function(err, tx) {
if (err || ! tx)
return common.handleErrors(err, res);
else {
req.transaction = tx.info;
return next();
}
});
};
/**
* Show transaction
*/
exports.show = function(req, res) {
if (req.transaction) {
res.jsonp(req.transaction);
}
};
var getTransaction = function(txid, cb) {
var tDb = new TransactionDb();
tDb.fromIdWithInfo(txid, function(err, tx) {
if (err) {
console.log(err);
}
if (!tx || !tx.info) {
console.log('[transactions.js.48]:: TXid %s not found in RPC. CHECK THIS.', txid); //TODO
// not check this. no
tx = {
info: {
txid: txid
}
};
}
return cb(null, tx.info);
});
};
/**
* List of transaction
*/
exports.list = function(req, res, next) {
var bId = req.query.block;
var addrStr = req.query.address;
var page = req.query.pageNum;
var pageLength = 20;
var pagesTotal = 1;
var txLength;
var txs;
if (bId) {
bdb.fromHashWithInfo(bId, function(err, block) {
if (err) {
console.log(err);
return res.status(500).send('Internal Server Error');
}
if (! block) {
return res.status(404).send('Not found');
}
txLength = block.info.tx.length;
if (page) {
var spliceInit = page * pageLength;
txs = block.info.tx.splice(spliceInit, pageLength);
pagesTotal = Math.ceil(txLength / pageLength);
}
else {
txs = block.info.tx;
}
async.mapSeries(txs, getTransaction, function(err, results) {
if (err) {
console.log(err);
res.status(404).send('TX not found');
}
res.jsonp({
pagesTotal: pagesTotal,
txs: results
});
});
});
}
else if (addrStr) {
var a = Address.new(addrStr);
a.update(function(err) {
if (err && !a.totalReceivedSat) {
console.log(err);
res.status(404).send('Invalid address');
return next();
}
txLength = a.transactions.length;
if (page) {
var spliceInit = page * pageLength;
txs = a.transactions.splice(spliceInit, pageLength);
pagesTotal = Math.ceil(txLength / pageLength);
}
else {
txs = a.transactions;
}
async.mapSeries(txs, getTransaction, function(err, results) {
if (err) {
console.log(err);
res.status(404).send('TX not found');
}
res.jsonp({
pagesTotal: pagesTotal,
txs: results
});
});
});
}
else {
res.jsonp({
txs: []
});
}
};