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