Merge pull request #32 from matiu/feature/02addr

Feature/02addr -- good!!
This commit is contained in:
Gustavo Maximiliano Cortez 2014-01-13 15:39:59 -08:00
commit 2ab49e10b1
3 changed files with 53 additions and 31 deletions

View File

@ -28,13 +28,21 @@ function spec() {
Address.prototype.__defineGetter__('balance', function(){ Address.prototype.__defineGetter__('balance', function(){
return parseFloat(this.balanceSat) / parseFloat(BitcoreUtil.COIN);
console.log('#################### '+this.balanceSat);
return this.balanceSat / BitcoreUtil.COIN;
}); });
Address.prototype.__defineGetter__('totalReceived', function(){
return parseFloat(this.totalReceivedSat) / parseFloat(BitcoreUtil.COIN);
});
Address.prototype.__defineGetter__('totalSent', function(){
return parseFloat(this.totalSentSat) / parseFloat(BitcoreUtil.COIN);
});
Address.prototype.update = function(next) { Address.prototype.update = function(next) {
if (! this.addrStr) { if (! this.addrStr) {
@ -46,21 +54,21 @@ console.log('#################### '+this.balanceSat);
// TODO TXout! // TODO TXout!
//T //T
function (cb) { function (cb) {
TransactionItem.find({addr:that.addrStr}, function(err,txItems){ TransactionItem.find({addr:that.addrStr}).sort({ts:1}).exec(function(err,txItems){
if (err) return cb(err); if (err) return cb(err);
txItems.forEach(function(txItem){ txItems.forEach(function(txItem){
// console.log(txItem.txid + ' : ' + txItem.value_sat); // console.log(txItem.txid + ':' + txItem.ts+ ' : ' + (txItem.value_sat/parseFloat(BitcoreUtil.COIN) ) );
that.txApperances +=1; that.txApperances +=1;
that.balanceSat += txItem.value_sat; that.balanceSat += txItem.value_sat;
that.transactions.push(txItem.txid); that.transactions.push(txItem.txid);
if (txItem.value_sat > 0) if (txItem.value_sat > 0)
that.totalSentSat += txItem.value_sat; that.totalReceivedSat += txItem.value_sat;
else else
that.totalReceivedSat += Math.abs(txItem.value_sat); that.totalSentSat += Math.abs(txItem.value_sat);
}); });
return cb(); return cb();
}); });

View File

@ -10,7 +10,7 @@ var
mongoose= require('mongoose'), mongoose= require('mongoose'),
addrValid = JSON.parse(fs.readFileSync('test/model/addr.json')); addrValid = JSON.parse(fs.readFileSync('test/model/addr.json'));
describe('Address update', function(){ describe('Address balances', function(){
before(function(done) { before(function(done) {
mongoose.connect(config.db); mongoose.connect(config.db);
@ -27,21 +27,23 @@ describe('Address update', function(){
console.log(v.addr + " => disabled in JSON"); console.log(v.addr + " => disabled in JSON");
} }
else { else {
it('should retrieve the correct info for:' + v.addr, function(done) { it('Info for:' + v.addr, function(done) {
this.timeout(5000); this.timeout(5000);
var a = new Address(v.addr); var a = new Address(v.addr);
a.update(function(err) { a.update(function(err) {
if (err) done(err); if (err) done(err);
assert.equal(v.addr, a.addrStr); assert.equal(v.addr, a.addrStr);
console.log("TX count:" + a.transactions.length);
if (v.balance) assert.equal(v.balance, a.balance); if (v.balance) assert.equal(v.balance, a.balance);
if (v.totalReceived) assert.equal(v.totalReceived, a.totalReceived); if (v.totalReceived) assert.equal(v.totalReceived, a.totalReceived);
if (v.totalSent) assert.equal(v.totalSent, a.totalSent); if (v.totalSent) assert.equal(v.totalSent, a.totalSent);
if (v.transactions) { if (v.transactions) {
v.transactions.forEach( function(tx) { v.transactions.forEach( function(tx) {
assert(tx in a.inTransactions); assert(a.transactions.indexOf(tx)>-1);
}); });
} }
done(); done();

View File

@ -1,38 +1,51 @@
[ [
{ {
"addr": "mjRmkmYzvZN3cA3aBKJgYJ65epn3WCG84H" "addr": "mgqvRGJMwR9JU5VhJ3x9uX9MTkzTsmmDgQ",
"balance": 43.1
}, },
{ {
"addr": "mp3Rzxx9s1A21SY3sjJ3CQoa2Xjph7e5eS", "addr": "mp3Rzxx9s1A21SY3sjJ3CQoa2Xjph7e5eS",
"balance": 0, "balance": 0,
"totalReceived": 50, "totalReceived": 50,
"totalSent": 50.0 "totalSent": 50.0
}
,
{
"addr": "mgqvRGJMwR9JU5VhJ3x9uX9MTkzTsmmDgQ",
"balance": 43.1
}, },
{ {
"disabled":1, "addr": "muyg1K5WsHkfMVCkUXU2y7Xp5ZD6RGzCeH",
"addr": "mzW2hdZN2um7WBvTDerdahKqRgj3md9C29", "balance": 0.38571339,
"balance": 910.39522682, "totalReceived": 0.38571339,
"totalReceived": 910.39522682,
"totalSent": 0 "totalSent": 0
} },
, {
"addr": "mhPEfAmeKVwT7arwMYbhwnL2TfwuWbP4r4",
"balance": 1065,
"totalReceived": 1069,
"totalSent": 4
},
{
"addr": "n47CfqnKWdNwqY1UWxTmNJAqYutFxdH3zY",
"balance": 0,
"totalReceived":26.4245,
"totalSent": 26.4245
},
{
"addr": "mzSyyXgofoBxpr6gYcU3cV345G8hJpixRd",
"balance": 0,
"totalReceived":3.9775,
"totalSent": 3.9775
},
{
"addr": "mzW2hdZN2um7WBvTDerdahKqRgj3md9C29",
"balance": 910.3952268,
"totalReceived": 910.3952268,
"totalSent": 0
},
{ {
"disabled":1,
"addr": "mjRmkmYzvZN3cA3aBKJgYJ65epn3WCG84H", "addr": "mjRmkmYzvZN3cA3aBKJgYJ65epn3WCG84H",
"balance": 46413.0, "balance": 46413.0,
"totalReceived": 357130.17644359, "totalReceived": 357130.17644359,
"totalSent": 310717.17644359 "totalSent": 310717.17644359
}, },
{ {
"disabled":1,
"addr": "mgKY35SXqxFpcKK3Dq9mW9919N7wYXvcFM", "addr": "mgKY35SXqxFpcKK3Dq9mW9919N7wYXvcFM",
"balance": 0.01979459, "balance": 0.01979459,
"totalReceived": 0.01979459, "totalReceived": 0.01979459,
@ -40,7 +53,6 @@
"transactions": [ "91800d80bb4c69b238c9bfd94eb5155ab821e6b25cae5c79903d12853bbb4ed5" ] "transactions": [ "91800d80bb4c69b238c9bfd94eb5155ab821e6b25cae5c79903d12853bbb4ed5" ]
}, },
{ {
"disabled":1,
"addr": "mmvP3mTe53qxHdPqXEvdu8WdC7GfQ2vmx5", "addr": "mmvP3mTe53qxHdPqXEvdu8WdC7GfQ2vmx5",
"balance": 10580.50027254, "balance": 10580.50027254,
"totalReceived": 12157.65075053, "totalReceived": 12157.65075053,