insight-ui-zcash/app/models/Address.js

128 lines
2.7 KiB
JavaScript
Raw Normal View History

2014-01-09 15:24:14 -08:00
'use strict';
require('classtool');
function spec() {
var async = require('async');
var TransactionItem = require('./TransactionItem');
2014-01-13 12:21:42 -08:00
var BitcoreAddress = require('bitcore/Address').class();
var BitcoreUtil = require('bitcore/util/util');
2014-01-13 12:21:42 -08:00
function Address(addrStr) {
this.balanceSat = 0;
this.totalReceivedSat = 0;
this.totalSentSat = 0;
this.txApperances = 0;
// TODO store only txids? +index? +all?
this.transactions = [];
2014-01-13 12:21:42 -08:00
var a = new BitcoreAddress(addrStr);
try {
a.validate();
this.addrStr = addrStr;
} catch(e){
}
}
2014-01-13 12:21:42 -08:00
Address.prototype.__defineGetter__('balance', function(){
console.log('#################### '+this.balanceSat);
return this.balanceSat / BitcoreUtil.COIN;
});
Address.prototype.update = function(next) {
2014-01-13 12:21:42 -08:00
if (! this.addrStr) {
return next(new Error('Invalid or undefined address string'));
}
var that = this;
async.series([
// TODO TXout!
//T
function (cb) {
2014-01-13 12:21:42 -08:00
TransactionItem.find({addr:that.addrStr}, function(err,txItems){
if (err) return cb(err);
2014-01-13 12:21:42 -08:00
txItems.forEach(function(txItem){
2014-01-13 12:21:42 -08:00
// console.log(txItem.txid + ' : ' + txItem.value_sat);
that.txApperances +=1;
that.balanceSat += txItem.value_sat;
2014-01-13 12:21:42 -08:00
that.transactions.push(txItem.txid);
2014-01-13 12:21:42 -08:00
if (txItem.value_sat > 0)
that.totalSentSat += txItem.value_sat;
else
that.totalReceivedSat += Math.abs(txItem.value_sat);
});
return cb();
});
2014-01-13 12:21:42 -08:00
}
], function (err) {
return next(err);
});
2014-01-13 12:21:42 -08:00
};
return Address;
}
module.defineClass(spec);
2014-01-09 15:24:14 -08:00
/**
* Addr Schema Idea for moogose. Not used now.
*
2014-01-09 15:24:14 -08:00
var AddressSchema = new Schema({
// For now we keep this as short as possible
// More fields will be propably added as we move
// forward with the UX
addr: {
type: String,
index: true,
unique: true,
},
2014-01-10 16:42:39 -08:00
inputs: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'TransactionItem' //Edit: I'd put the schema. Silly me.
}],
output: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'TransactionItem' //Edit: I'd put the schema. Silly me.
}],
2014-01-09 15:24:14 -08:00
});
AddressSchema.statics.load = function(id, cb) {
this.findOne({
_id: id
}).exec(cb);
};
AddressSchema.statics.fromAddr = function(hash, cb) {
this.findOne({
hash: hash,
}).exec(cb);
};
AddressSchema.statics.fromAddrWithInfo = function(hash, cb) {
2014-01-10 16:42:39 -08:00
this.fromHash(hash, function(err, addr) {
2014-01-09 15:24:14 -08:00
if (err) return cb(err);
2014-01-10 16:42:39 -08:00
if (!addr) { return cb(new Error('Addr not found')); }
// TODO
// addr.getInfo(function(err) { return cb(err,addr); } );
2014-01-09 15:24:14 -08:00
});
};
module.exports = mongoose.model('Address', AddressSchema);
*/