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

96 lines
2.4 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-02-05 09:25:52 -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-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;
if (!self.addrStr) return next();
2014-02-05 09:25:52 -08:00
var db = new TransactionDb();
async.series([
2014-01-29 10:10:36 -08:00
function (cb) {
2014-02-05 09:25:52 -08:00
db.fromAddr(self.addrStr, function(err,txOut){
2014-01-29 10:10:36 -08:00
if (err) return cb(err);
txOut.forEach(function(txItem){
2014-02-04 22:34:46 -08:00
2014-02-06 05:20:49 -08:00
if (txItem.isConfirmed) {
var v = txItem.value_sat;
self.totalReceivedSat += v;
self.transactions.push(txItem.txid);
if (! txItem.spendTxId || !txItem.spendIsConfirmed) {
// unspent
self.balanceSat += v;
self.txApperances +=1;
}
else {
// spent
self.totalSentSat += v;
self.transactions.push(txItem.spendTxId);
self.txApperances +=2;
}
2014-02-08 05:57:37 -08:00
}
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);