2014-01-07 11:49:42 -08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
var mongoose = require('mongoose'),
|
2014-01-08 09:50:37 -08:00
|
|
|
Schema = mongoose.Schema,
|
|
|
|
async = require('async');
|
2014-01-07 11:49:42 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
*/
|
|
|
|
var TransactionSchema = new Schema({
|
2014-01-08 09:50:37 -08:00
|
|
|
// For now we keep this as short as possible
|
|
|
|
// More fields will be propably added as we move
|
|
|
|
// forward with the UX
|
2014-01-07 16:03:48 -08:00
|
|
|
txid: {
|
2014-01-07 11:49:42 -08:00
|
|
|
type: String,
|
|
|
|
index: true,
|
|
|
|
unique: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Statics
|
|
|
|
*/
|
|
|
|
|
|
|
|
TransactionSchema.statics.load = function(id, cb) {
|
|
|
|
this.findOne({
|
|
|
|
_id: id
|
|
|
|
}).exec(cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-01-07 16:03:48 -08:00
|
|
|
TransactionSchema.statics.fromID = function(txid, cb) {
|
2014-01-07 11:49:42 -08:00
|
|
|
this.findOne({
|
2014-01-07 16:03:48 -08:00
|
|
|
txid: txid,
|
2014-01-07 11:49:42 -08:00
|
|
|
}).exec(cb);
|
|
|
|
};
|
|
|
|
|
2014-01-08 11:29:39 -08:00
|
|
|
TransactionSchema.statics.fromIDWithInfo = function(txid, cb) {
|
|
|
|
this.fromHash(hash, function(err, tx) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
|
|
|
|
tx.getInfo(function(err) { return cb(err,tx); } );
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-01-08 09:50:37 -08:00
|
|
|
TransactionSchema.statics.createFromArray = function(txs, next) {
|
|
|
|
|
|
|
|
var that = this;
|
|
|
|
|
|
|
|
if (!txs) return next();
|
|
|
|
|
|
|
|
// console.log('exploding ', txs);
|
|
|
|
|
|
|
|
async.forEach( txs,
|
|
|
|
function(tx, callback) {
|
|
|
|
// console.log('procesing TX %s', tx);
|
|
|
|
that.create({ txid: tx }, function(err) {
|
|
|
|
if (err && ! err.toString().match(/E11000/)) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
if (err) {
|
|
|
|
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
return callback();
|
|
|
|
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(err) {
|
|
|
|
if (err) return next(err);
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-01-07 16:03:48 -08:00
|
|
|
|
2014-01-08 11:29:39 -08:00
|
|
|
TransactionSchema.methods.getInfo = function (next) {
|
|
|
|
|
|
|
|
var that = this;
|
|
|
|
var rpc = new RpcClient(config.bitcoind);
|
|
|
|
|
|
|
|
rpc.getRawTransaction(this.txid, function(err, txInfo) {
|
|
|
|
if (err) return next(err);
|
|
|
|
that.info = txInfo.result;
|
|
|
|
|
|
|
|
//console.log("THAT", that);
|
|
|
|
return next(null, that.info);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-01-07 16:03:48 -08:00
|
|
|
|
2014-01-07 11:49:42 -08:00
|
|
|
module.exports = mongoose.model('Transaction', TransactionSchema);
|