Changing fns to use _streamAddressSummary

Functions updated:
- getAddressHistory
- getAddressSummary

(old fns are kept as it is and renamed to __getAddressHistory and __getAddressSummary respectively)
This commit is contained in:
sairajzero 2023-01-27 17:38:37 +05:30
parent e13bd5e3e6
commit 4472ed8394

View File

@ -67,8 +67,8 @@ AddressService.dependencies = [
// then I would pass back [tx1, tx2] in that order
//
// Instead of passing addresses, with from>0, options.cacheKey can be used to define the address set.
//
AddressService.prototype.getAddressHistory = function(addresses, options, callback) {
//(old one: non-optimized for large data)
AddressService.prototype.__getAddressHistory = function(addresses, options, callback) {
var self = this;
var cacheUsed = false;
@ -173,8 +173,62 @@ AddressService.prototype.getAddressHistory = function(addresses, options, callba
};
AddressService.prototype.getAddressHistory = function(addresses, options, streamer, callback) {
var self = this;
options = options || {};
options.from = options.from || 0;
options.to = options.to || 0xffffffff;
if(!callback){ //if only 3 args, then streamer is callback
callback = streamer;
streamer = () => null; //NULL fn
}
if (_.isUndefined(options.queryMempool)) {
options.queryMempool = true;
}
if (_.isString(addresses)) {
addresses = [addresses];
}
var results = {
totalCount: 0,
items: [],
}
async.eachLimit(addresses, 4, function(address, next) {
self._streamAddressSummary(address, options, function(err, tx){
results.totalCount++;
if(err)
return log.error(err);
if(!options.txNotNeeded && results.items.length < MAX_TX_QUERY_LIMIT)
results.items.push(tx);
streamer(null, tx);
}, next);
}, function(err) {
if (err) {
return callback(err);
}
//TODO: sorting of tx list (results.items)
callback(null, results);
})
}
// this is basically the same as _getAddressHistory apart from the summary
AddressService.prototype.getAddressSummary = function(address, options, callback) {
//(old one: non-optimized for large data)
AddressService.prototype.__getAddressSummary = function(address, options, callback) {
var self = this;
@ -218,6 +272,67 @@ AddressService.prototype.getAddressSummary = function(address, options, callback
};
AddressService.prototype.getAddressSummary = function(address, options, streamer, callback) {
var self = this;
options = options || {};
options.from = options.from || 0;
options.to = options.to || 0xffffffff;
options.txNotNeeded = true; //no need to store tx details in result
if (_.isUndefined(options.queryMempool)) {
options.queryMempool = true;
}
if(!callback){ //if only 3 args, then streamer is callback
callback = streamer;
streamer = () => null; //NULL fn
}
var result = {
addrStr: address,
balance: 0,
balanceSat: 0,
totalReceived: 0,
totalReceivedSat: 0,
totalSent: 0,
totalSentSat: 0,
unconfirmedBalance: 0,
unconfirmedBalanceSat: 0,
unconfirmedTxApperances: 0,
txApperances: 0,
};
self._aggregateAddressSummaryResult(tx, address, result);
self.getAddressHistory(address, options, function(err, tx) {
if(err)
return log.error(err);
if(tx)
self._aggregateAddressSummaryResult(tx, address, result);
streamer(null, tx);
}, function(err) {
if (err) {
return callback(err);
}
result.balance = Unit.fromSatoshis(result.balanceSat).toBTC();
result.totalReceived = Unit.fromSatoshis(result.totalReceivedSat).toBTC();
result.totalSent = Unit.fromSatoshis(result.totalSentSat).toBTC();
result.unconfirmedBalance = Unit.fromSatoshis(result.unconfirmedBalanceSat).toBTC();
callback(null, result);
});
}
AddressService.prototype._setOutputResults = function(tx, address, result) {
for(var j = 0; j < tx.outputs.length; j++) {
@ -260,14 +375,10 @@ AddressService.prototype._setInputResults = function(tx, address, result) {
}
};
AddressService.prototype._getAddressSummaryResult = function(txs, address, result, options) {
AddressService.prototype._aggregateAddressSummaryResult = function (tx, address, result, options){
var self = this;
for(var i = 0; i < txs.length; i++) {
var tx = txs[i];
self._setOutputResults(tx, address, result);
self._setInputResults(tx, address, result);
@ -278,6 +389,15 @@ AddressService.prototype._getAddressSummaryResult = function(txs, address, resul
result.transactions.push(tx.txid());
}
}
AddressService.prototype._getAddressSummaryResult = function(txs, address, result, options) {
var self = this;
for(var i = 0; i < txs.length; i++) {
var tx = txs[i];
self._aggregateAddressSummaryResult(tx, address, result, options);
}
return result;