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

142 lines
3.3 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);
2014-01-20 10:51:23 -08:00
a.validate();
this.addrStr = addrStr;
2014-01-13 12:21:42 -08:00
2014-01-13 14:57:13 -08:00
2014-01-15 05:13:46 -08:00
Object.defineProperty(this, 'totalSent', {
2014-01-14 07:58:45 -08:00
get: function() {
return parseFloat(this.totalSentSat) / parseFloat(BitcoreUtil.COIN);
},
set: function(i) {
2014-01-15 05:13:46 -08:00
this.totalSentSat = i * BitcoreUtil.COIN;
2014-01-14 07:58:45 -08:00
},
enumerable: 1,
});
2014-01-15 05:13:46 -08:00
Object.defineProperty(this, 'balance', {
2014-01-14 07:58:45 -08:00
get: function() {
return parseFloat(this.balanceSat) / parseFloat(BitcoreUtil.COIN);
},
set: function(i) {
2014-01-15 05:13:46 -08:00
this.balance = i * BitcoreUtil.COIN;
2014-01-14 07:58:45 -08:00
},
enumerable: 1,
});
2014-01-15 05:13:46 -08:00
Object.defineProperty(this, 'totalReceived', {
2014-01-14 07:58:45 -08:00
get: function() {
return parseFloat(this.totalReceivedSat) / parseFloat(BitcoreUtil.COIN);
},
set: function(i) {
2014-01-15 05:13:46 -08:00
this.totalReceived = i * BitcoreUtil.COIN;
2014-01-14 07:58:45 -08:00
},
enumerable: 1,
});
}
2014-01-13 14:57:13 -08:00
Address.prototype.update = function(next) {
var that = this;
async.series([
// TODO TXout!
//T
function (cb) {
2014-01-21 10:49:25 -08:00
TransactionItem.find({addr:that.addrStr}).sort({ts:-1}).exec(function(err,txItems){
2014-01-13 12:21:42 -08:00
if (err) return cb(err);
2014-01-13 12:21:42 -08:00
txItems.forEach(function(txItem){
2014-01-13 15:38:51 -08:00
// console.log(txItem.txid + ':' + txItem.ts+ ' : ' + (txItem.value_sat/parseFloat(BitcoreUtil.COIN) ) );
2014-01-13 12:21:42 -08:00
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)
2014-01-13 14:57:13 -08:00
that.totalReceivedSat += txItem.value_sat;
2014-01-13 12:21:42 -08:00
else
2014-01-13 14:57:13 -08:00
that.totalSentSat += Math.abs(txItem.value_sat);
2014-01-13 12:21:42 -08:00
});
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);
*/