Added balance by address.
This commit is contained in:
parent
05ddd43dfc
commit
3f558f6ace
@ -87,6 +87,7 @@ WalletService.prototype._endpointGetBalance= function() {
|
||||
return function(req, res) {
|
||||
var walletId = req.params.walletId;
|
||||
var queryMempool = req.query.queryMempool === false ? false : true;
|
||||
var byAddress = req.query.byAddress;
|
||||
|
||||
//var tip = self.node.bitcoind.tip;
|
||||
// TODO: get the height of the tip
|
||||
@ -94,17 +95,15 @@ WalletService.prototype._endpointGetBalance= function() {
|
||||
var height = null;
|
||||
|
||||
var options = {
|
||||
queryMempool: queryMempool
|
||||
queryMempool: queryMempool,
|
||||
byAddress: byAddress
|
||||
};
|
||||
|
||||
self._getBalance(walletId, height, options, function(err, balance) {
|
||||
self._getBalance(walletId, height, options, function(err, result) {
|
||||
if(err) {
|
||||
return utils.sendError(err);
|
||||
}
|
||||
res.status(200).jsonp({
|
||||
balance: balance,
|
||||
height: height
|
||||
});
|
||||
res.status(200).jsonp(result);
|
||||
});
|
||||
};
|
||||
};
|
||||
@ -227,20 +226,44 @@ WalletService.prototype._getBalance = function(walletId, height, options, callba
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
|
||||
self.node.services.bitcoind.getAddressUnspentOutputs(addresses, options, function(err, utxos) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
if (options.byAddress) {
|
||||
var result = self._getBalanceByAddress(addresses, utxos);
|
||||
return callback(null, {
|
||||
addresses: result,
|
||||
height: null
|
||||
});
|
||||
}
|
||||
var balance = 0;
|
||||
utxos.forEach(function(utxo) {
|
||||
balance += utxo.satoshis;
|
||||
});
|
||||
callback(null, balance);
|
||||
callback(null, {
|
||||
balance: balance,
|
||||
height: null
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
WalletService.prototype._getBalanceByAddress = function(addresses, utxos) {
|
||||
var res = {};
|
||||
utxos.forEach(function(utxo) {
|
||||
if (res[utxo.address]) {
|
||||
res[utxo.address] += utxo.satoshis;
|
||||
} else {
|
||||
res[utxo.address] = utxo.satoshis;
|
||||
}
|
||||
});
|
||||
addresses.forEach(function(address) {
|
||||
res[address] = res[address] || 0;
|
||||
});
|
||||
return res;
|
||||
};
|
||||
|
||||
WalletService.prototype._chunkAdresses = function(addresses) {
|
||||
var maxLength = this.node.services.bitcoind.maxAddressesQuery;
|
||||
var groups = [];
|
||||
|
||||
Loading…
Reference in New Issue
Block a user