2014-01-07 20:47:20 -08:00
|
|
|
'use strict';
|
|
|
|
|
2014-01-15 07:41:59 -08:00
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
2014-01-14 15:34:01 -08:00
|
|
|
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();
|
2014-02-05 05:16:40 -08:00
|
|
|
var BlockDb = require('../../lib/BlockDb').class();
|
|
|
|
|
2014-02-15 14:51:35 -08:00
|
|
|
var tDb = new TransactionDb();
|
2014-02-05 05:16:40 -08:00
|
|
|
var bdb = new BlockDb();
|
|
|
|
|
2014-01-07 20:47:20 -08:00
|
|
|
|
|
|
|
/**
|
2014-01-20 10:37:44 -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
|
|
|
|
|
|
|
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;
|
2014-01-09 12:30:29 -08:00
|
|
|
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) {
|
2014-01-09 12:30:29 -08:00
|
|
|
|
|
|
|
if (req.transaction) {
|
|
|
|
res.jsonp(req.transaction);
|
|
|
|
}
|
2014-01-07 20:47:20 -08:00
|
|
|
};
|
|
|
|
|
2014-01-15 07:41:59 -08:00
|
|
|
|
2014-01-14 15:34:01 -08:00
|
|
|
var getTransaction = function(txid, cb) {
|
2014-02-05 09:25:52 -08:00
|
|
|
|
|
|
|
tDb.fromIdWithInfo(txid, function(err, tx) {
|
2014-02-15 14:51:35 -08:00
|
|
|
if (err) console.log(err);
|
2014-01-23 12:33:12 -08:00
|
|
|
|
2014-01-23 12:49:20 -08:00
|
|
|
if (!tx || !tx.info) {
|
2014-02-24 20:44:19 -08:00
|
|
|
console.log('[transactions.js.48]:: TXid %s not found in RPC. CHECK THIS.', txid);
|
2014-02-15 14:51:35 -08:00
|
|
|
return ({ txid: txid });
|
2014-01-23 12:38:30 -08:00
|
|
|
}
|
2014-01-23 12:33:12 -08:00
|
|
|
|
2014-01-14 15:34:01 -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;
|
2014-02-15 14:54:17 -08:00
|
|
|
var pageLength = 10;
|
2014-01-21 07:55:38 -08:00
|
|
|
var pagesTotal = 1;
|
|
|
|
var txLength;
|
|
|
|
var txs;
|
2014-01-15 05:22:07 -08:00
|
|
|
|
|
|
|
if (bId) {
|
2014-02-05 05:16:40 -08:00
|
|
|
bdb.fromHashWithInfo(bId, function(err, block) {
|
2014-01-24 13:04:08 -08:00
|
|
|
if (err) {
|
2014-01-15 05:22:07 -08:00
|
|
|
console.log(err);
|
2014-01-24 13:04:08 -08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-01-23 12:33:12 -08:00
|
|
|
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-23 12:33:12 -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;
|
|
|
|
}
|
|
|
|
|
2014-01-23 12:33:12 -08:00
|
|
|
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-23 12:33:12 -08:00
|
|
|
});
|
2014-01-15 05:22:07 -08:00
|
|
|
});
|
2014-01-15 07:41:59 -08:00
|
|
|
}
|
2014-01-21 12:58:29 -08:00
|
|
|
else {
|
2014-01-27 08:10:59 -08:00
|
|
|
res.jsonp({
|
2014-01-29 12:58:45 -08:00
|
|
|
txs: []
|
2014-01-27 08:10:59 -08:00
|
|
|
});
|
2014-01-21 12:58:29 -08:00
|
|
|
}
|
2014-01-14 15:34:01 -08:00
|
|
|
};
|