txdb: balance object.
This commit is contained in:
parent
c4f16f8d72
commit
4b9753d3c3
@ -993,13 +993,9 @@ HTTPServer.prototype._initIO = function _initIO() {
|
||||
});
|
||||
|
||||
this.walletdb.on('balance', function(id, balance) {
|
||||
balance = {
|
||||
confirmed: utils.btc(balance.confirmed),
|
||||
unconfirmed: utils.btc(balance.unconfirmed),
|
||||
total: utils.btc(balance.total)
|
||||
};
|
||||
self.server.io.to(id).emit('wallet balance', balance);
|
||||
self.server.io.to('!all').emit('wallet balance', id, balance);
|
||||
var json = balance.toJSON();
|
||||
self.server.io.to(id).emit('wallet balance', json);
|
||||
self.server.io.to('!all').emit('wallet balance', id, json);
|
||||
});
|
||||
|
||||
this.walletdb.on('address', function(id, receive) {
|
||||
|
||||
@ -51,6 +51,7 @@ function TXDB(wallet) {
|
||||
this.locker = new bcoin.locker(this);
|
||||
this.current = null;
|
||||
this.coinCache = new bcoin.lru(10000, 1);
|
||||
this.balance = new Balance();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -426,6 +427,9 @@ TXDB.prototype._resolveOrphans = function _resolveOrphans(tx, index, callback) {
|
||||
if (err)
|
||||
return callback(err);
|
||||
|
||||
// Just going to be added again outside.
|
||||
self.balance.sub(coin);
|
||||
|
||||
return callback(null, false);
|
||||
});
|
||||
});
|
||||
@ -519,6 +523,7 @@ TXDB.prototype.add = function add(tx, info, callback) {
|
||||
self.del('c/' + key);
|
||||
self.del('C/' + path.account + '/' + key);
|
||||
self.put('d/' + hash + '/' + i, input.coin.toRaw());
|
||||
self.balance.sub(input.coin);
|
||||
|
||||
self.coinCache.remove(key);
|
||||
|
||||
@ -548,7 +553,9 @@ TXDB.prototype.add = function add(tx, info, callback) {
|
||||
if (orphans)
|
||||
return next();
|
||||
|
||||
coin = bcoin.coin.fromTX(tx, i).toRaw();
|
||||
coin = bcoin.coin.fromTX(tx, i);
|
||||
self.balance.add(coin);
|
||||
coin = coin.toRaw();
|
||||
|
||||
self.put('c/' + key, coin);
|
||||
self.put('C/' + path.account + '/' + key, DUMMY);
|
||||
@ -792,6 +799,8 @@ TXDB.prototype._confirm = function _confirm(tx, info, callback) {
|
||||
return next();
|
||||
}
|
||||
|
||||
self.balance.confirm(coin.value);
|
||||
|
||||
coin.height = tx.height;
|
||||
coin = coin.toRaw();
|
||||
|
||||
@ -915,6 +924,8 @@ TXDB.prototype._remove = function remove(tx, info, callback) {
|
||||
if (!path)
|
||||
continue;
|
||||
|
||||
self.balance.add(input.coin);
|
||||
|
||||
coin = input.coin.toRaw();
|
||||
|
||||
self.put('c/' + key, coin);
|
||||
@ -936,6 +947,10 @@ TXDB.prototype._remove = function remove(tx, info, callback) {
|
||||
if (!path)
|
||||
continue;
|
||||
|
||||
coin = bcoin.coin.fromTX(tx, i);
|
||||
|
||||
self.balance.sub(coin);
|
||||
|
||||
self.del('c/' + key);
|
||||
self.del('C/' + path.account + '/' + key);
|
||||
|
||||
@ -1038,6 +1053,7 @@ TXDB.prototype._unconfirm = function unconfirm(tx, info, callback, force) {
|
||||
return next();
|
||||
}
|
||||
|
||||
self.balance.unconfirm(coin.value);
|
||||
coin.height = tx.height;
|
||||
coin = coin.toRaw();
|
||||
|
||||
@ -1679,8 +1695,7 @@ TXDB.prototype.hasCoin = function hasCoin(hash, index, callback) {
|
||||
|
||||
TXDB.prototype.getBalance = function getBalance(account, callback) {
|
||||
var self = this;
|
||||
var confirmed = 0;
|
||||
var unconfirmed = 0;
|
||||
var balance = new Balance();
|
||||
|
||||
if (typeof account === 'function') {
|
||||
callback = account;
|
||||
@ -1706,10 +1721,12 @@ TXDB.prototype.getBalance = function getBalance(account, callback) {
|
||||
|
||||
assert(data.length >= 16);
|
||||
|
||||
balance.total += value;
|
||||
|
||||
if (height === 0x7fffffff)
|
||||
unconfirmed += value;
|
||||
balance.unconfirmed += value;
|
||||
else
|
||||
confirmed += value;
|
||||
balance.confirmed += value;
|
||||
|
||||
key = hash + '/' + index;
|
||||
|
||||
@ -1719,11 +1736,7 @@ TXDB.prototype.getBalance = function getBalance(account, callback) {
|
||||
if (err)
|
||||
return callback(err);
|
||||
|
||||
return callback(null, {
|
||||
confirmed: confirmed,
|
||||
unconfirmed: unconfirmed,
|
||||
total: confirmed + unconfirmed
|
||||
});
|
||||
return callback(null, balance);
|
||||
});
|
||||
};
|
||||
|
||||
@ -1735,8 +1748,7 @@ TXDB.prototype.getBalance = function getBalance(account, callback) {
|
||||
|
||||
TXDB.prototype.getAccountBalance = function getBalance(account, callback) {
|
||||
var self = this;
|
||||
var confirmed = 0;
|
||||
var unconfirmed = 0;
|
||||
var balance = new Balance();
|
||||
var key, coin;
|
||||
|
||||
function parse(data) {
|
||||
@ -1745,10 +1757,12 @@ TXDB.prototype.getAccountBalance = function getBalance(account, callback) {
|
||||
|
||||
assert(data.length >= 16);
|
||||
|
||||
balance.total += value;
|
||||
|
||||
if (height === 0x7fffffff)
|
||||
unconfirmed += value;
|
||||
balance.unconfirmed += value;
|
||||
else
|
||||
confirmed += value;
|
||||
balance.confirmed += value;
|
||||
}
|
||||
|
||||
this.getCoinHashes(account, function(err, hashes) {
|
||||
@ -1789,11 +1803,7 @@ TXDB.prototype.getAccountBalance = function getBalance(account, callback) {
|
||||
if (err)
|
||||
return callback(err);
|
||||
|
||||
return callback(null, {
|
||||
confirmed: confirmed,
|
||||
unconfirmed: unconfirmed,
|
||||
total: confirmed + unconfirmed
|
||||
});
|
||||
return callback(null, balance);
|
||||
});
|
||||
});
|
||||
};
|
||||
@ -1960,6 +1970,50 @@ DetailsMember.prototype.toJSON = function toJSON() {
|
||||
};
|
||||
};
|
||||
|
||||
/*
|
||||
* Balance
|
||||
*/
|
||||
|
||||
function Balance() {
|
||||
this.unconfirmed = 0;
|
||||
this.confirmed = 0;
|
||||
this.total = 0;
|
||||
}
|
||||
|
||||
Balance.prototype.add = function add(coin) {
|
||||
this.total += coin.value;
|
||||
if (coin.height === -1)
|
||||
this.unconfirmed += coin.value;
|
||||
else
|
||||
this.confirmed += coin.value;
|
||||
};
|
||||
|
||||
Balance.prototype.sub = function sub(coin) {
|
||||
this.total -= coin.value;
|
||||
if (coin.height === -1)
|
||||
this.unconfirmed -= coin.value;
|
||||
else
|
||||
this.confirmed -= coin.value;
|
||||
};
|
||||
|
||||
Balance.prototype.confirm = function confirm(value) {
|
||||
this.unconfirmed -= value;
|
||||
this.confirmed += value;
|
||||
};
|
||||
|
||||
Balance.prototype.unconfirm = function unconfirm(value) {
|
||||
this.unconfirmed += value;
|
||||
this.confirmed -= value;
|
||||
};
|
||||
|
||||
Balance.prototype.toJSON = function toJSON() {
|
||||
return {
|
||||
unconfirmed: utils.btc(this.unconfirmed),
|
||||
confirmed: utils.btc(this.confirmed),
|
||||
total: utils.btc(this.total)
|
||||
};
|
||||
};
|
||||
|
||||
/*
|
||||
* Helpers
|
||||
*/
|
||||
|
||||
Loading…
Reference in New Issue
Block a user