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

106 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');
2014-01-13 12:21:42 -08:00
var BitcoreAddress = require('bitcore/Address').class();
var BitcoreUtil = require('bitcore/util/util');
2014-02-03 23:06:03 -08:00
var TransactionDb = require('../../lib/TransactionDb').class();
2014-01-13 12:21:42 -08:00
function Address(addrStr) {
2014-02-03 23:06:03 -08:00
this.tdb = new TransactionDb();
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) {
2014-01-29 10:10:36 -08:00
var self = this;
async.series([
2014-01-29 10:10:36 -08:00
/* function (cb) {
TransactionIn.find({addr:self.addrStr}).exec(function(err,txIn){
2014-01-13 12:21:42 -08:00
if (err) return cb(err);
2014-01-29 10:10:36 -08:00
txIn.forEach(function(txItem){
2014-01-29 10:10:36 -08:00
self.balanceSat += txItem.value_sat;
self.totalReceivedSat += txItem.value_sat;
});
return cb();
});
},
*/
function (cb) {
2014-02-03 23:06:03 -08:00
self.tdb.fromAddr(self.addrStr, function(err,txOut){
2014-01-29 10:10:36 -08:00
if (err) return cb(err);
2014-01-29 10:10:36 -08:00
txOut.forEach(function(txItem){
self.totalReceivedSat += txItem.value_sat;
self.transactions.push(txItem.txid);
2014-02-03 23:06:03 -08:00
if (! txItem.spendTxId) {
2014-01-29 10:10:36 -08:00
// unspent
self.balanceSat += txItem.value_sat;
self.txApperances +=1;
}
else {
// spent
self.totalSentSat += txItem.value_sat;
self.transactions.push(txItem.spendTxid);
self.txApperances +=2;
}
2014-01-13 12:21:42 -08:00
});
return cb();
});
2014-01-29 10:10:36 -08:00
},
], function (err) {
return next(err);
});
2014-01-13 12:21:42 -08:00
};
return Address;
}
module.defineClass(spec);