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

104 lines
2.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.
*/
2014-01-07 20:47:20 -08:00
var Transaction = require('../models/Transaction');
var Block = require('../models/Block');
var Address = require('../models/Address');
var async = require('async');
2014-01-07 20:47:20 -08:00
/**
* Find block by hash ...
*/
exports.transaction = function(req, res, next, txid) {
Transaction.fromIdWithInfo(txid, function(err, tx) {
if (err) {
console.log(err);
res.status(404).send('Not found');
return next();
}
2014-01-07 20:47:20 -08:00
if (!tx) return next(new Error('Failed to load TX ' + txid));
req.transaction = tx.info;
2014-01-07 20:47:20 -08:00
next();
});
};
/**
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) {
Transaction.fromIdWithInfo(txid, function(err, tx) {
if (err) {
console.log(err);
return cb(err);
}
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;
var aId = req.query.address;
2014-01-15 07:41:59 -08:00
var limit = req.query.limit || 1000;
2014-01-15 05:22:07 -08:00
if (bId) {
Block.fromHashWithInfo(bId, function(err, block) {
if (err && !block) {
console.log(err);
res.status(404).send('Not found');
return next();
}
async.mapSeries(block.info.tx, getTransaction,
function(err, results) {
res.jsonp(results);
});
});
}
2014-01-15 07:41:59 -08:00
else if (aId) {
2014-01-15 05:22:07 -08:00
var a = Address.new(aId);
a.update(function(err) {
if (err && !a.totalReceivedSat) {
console.log(err);
res.status(404).send('Invalid address');
return next();
}
async.mapSeries(a.transactions, getTransaction,
function(err, results) {
res.jsonp(results);
});
});
2014-01-15 07:41:59 -08:00
}
else {
Transaction
.find()
.limit(limit)
2014-01-15 07:50:55 -08:00
.sort('-time')
2014-01-15 07:41:59 -08:00
.exec(function(err, txs) {
if (err) {
res.status(500).send(err);
} else {
res.jsonp(txs);
}
});
2014-01-15 05:22:07 -08:00
}
};