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

154 lines
3.0 KiB
JavaScript
Raw Normal View History

2014-01-07 20:47:20 -08:00
'use strict';
2014-01-15 07:41:59 -08:00
/**
* Module dependencies.
*/
var Address = require('../models/Address');
var async = require('async');
2014-01-20 10:51:23 -08:00
var common = require('./common');
2014-01-07 20:47:20 -08:00
2014-02-05 09:25:52 -08:00
var TransactionDb = require('../../lib/TransactionDb').class();
var BlockDb = require('../../lib/BlockDb').class();
var bdb = new BlockDb();
2014-01-07 20:47:20 -08:00
/**
* Find transaction by hash ...
2014-01-07 20:47:20 -08:00
*/
exports.transaction = function(req, res, next, txid) {
2014-02-05 09:25:52 -08:00
var tDb = new TransactionDb();
tDb.fromIdWithInfo(txid, function(err, tx) {
2014-01-20 11:40:20 -08:00
if (err || ! tx)
return common.handleErrors(err, res);
else {
req.transaction = tx.info;
return next();
}
2014-01-07 20:47:20 -08:00
});
};
/**
2014-01-15 07:41:59 -08:00
* Show transaction
2014-01-07 20:47:20 -08:00
*/
exports.show = function(req, res) {
if (req.transaction) {
res.jsonp(req.transaction);
}
2014-01-07 20:47:20 -08:00
};
2014-01-15 07:41:59 -08:00
var getTransaction = function(txid, cb) {
2014-02-05 09:25:52 -08:00
var tDb = new TransactionDb();
tDb.fromIdWithInfo(txid, function(err, tx) {
if (err) {
console.log(err);
}
2014-01-23 12:49:20 -08:00
if (!tx || !tx.info) {
console.log('[transactions.js.48]:: TXid %s not found in RPC. CHECK THIS.', txid); //TODO
2014-01-23 12:38:30 -08:00
// not check this. no
tx = {
info: {
txid: txid
}
2014-01-23 12:38:30 -08:00
};
}
return cb(null, tx.info);
});
};
2014-01-15 07:41:59 -08:00
/**
* List of transaction
*/
exports.list = function(req, res, next) {
2014-01-15 05:22:07 -08:00
var bId = req.query.block;
2014-01-21 07:55:38 -08:00
var addrStr = req.query.address;
var page = req.query.pageNum;
var pageLength = 20;
var pagesTotal = 1;
var txLength;
var txs;
2014-01-15 05:22:07 -08:00
if (bId) {
bdb.fromHashWithInfo(bId, function(err, block) {
if (err) {
2014-01-15 05:22:07 -08:00
console.log(err);
return res.status(500).send('Internal Server Error');
}
if (! block) {
return res.status(404).send('Not found');
2014-01-15 05:22:07 -08:00
}
2014-01-21 07:55:38 -08:00
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
2014-01-15 05:22:07 -08:00
});
});
2014-01-15 05:22:07 -08:00
});
}
2014-01-21 07:55:38 -08:00
else if (addrStr) {
var a = Address.new(addrStr);
2014-01-15 05:22:07 -08:00
a.update(function(err) {
if (err && !a.totalReceivedSat) {
console.log(err);
res.status(404).send('Invalid address');
return next();
}
2014-01-21 07:55:38 -08:00
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
2014-01-15 05:22:07 -08:00
});
});
2014-01-15 05:22:07 -08:00
});
2014-01-15 07:41:59 -08:00
}
else {
res.jsonp({
2014-01-29 12:58:45 -08:00
txs: []
});
}
};