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');
|
2014-01-14 15:34:01 -08:00
|
|
|
var Block = require('../models/Block');
|
|
|
|
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-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-01-08 12:15:46 -08:00
|
|
|
Transaction.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) {
|
|
|
|
Transaction.fromIdWithInfo(txid, function(err, tx) {
|
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
return cb(err);
|
|
|
|
}
|
2014-01-23 12:33:12 -08:00
|
|
|
|
2014-01-23 12:49:20 -08:00
|
|
|
if (!tx || !tx.info) {
|
2014-01-23 12:38:30 -08:00
|
|
|
console.log('[transactions.js.48]:: TXid %d not found in RPC. CHECK THIS.', txid); //TODO
|
|
|
|
// not check this. no
|
|
|
|
tx.info = {
|
|
|
|
txid: txid,
|
|
|
|
};
|
|
|
|
}
|
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-21 12:58:29 -08:00
|
|
|
var limit = req.query.limit || 5;
|
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) {
|
|
|
|
Block.fromHashWithInfo(bId, function(err, block) {
|
|
|
|
if (err && !block) {
|
|
|
|
console.log(err);
|
|
|
|
res.status(404).send('Not found');
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
Transaction
|
|
|
|
.find()
|
|
|
|
.limit(limit)
|
|
|
|
.sort('-time')
|
|
|
|
.exec(function(err, txs) {
|
|
|
|
if (err) {
|
|
|
|
res.status(500).send(err);
|
|
|
|
} else {
|
|
|
|
var txids = [];
|
|
|
|
for(var i=0;i<txs.length;i++) {
|
|
|
|
txids.push(txs[i].txid);
|
|
|
|
}
|
|
|
|
|
|
|
|
async.mapSeries(txids, getTransaction, function(err, alltxs) {
|
2014-01-23 12:33:12 -08:00
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
res.status(404).send('TX not found');
|
|
|
|
}
|
|
|
|
|
2014-01-21 12:58:29 -08:00
|
|
|
res.jsonp({
|
|
|
|
txs: alltxs,
|
|
|
|
length: alltxs.length
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-01-21 07:55:38 -08:00
|
|
|
|
2014-01-14 15:34:01 -08:00
|
|
|
};
|