Issues (615)

lib/unspent_output_finder.js (2 issues)

1
var _ = require('lodash');
2
var q = require('q');
3
var async = require('async');
4
5
/**
6
 * @param bitcoinDataClient
7
 * @param options
8
 * @constructor
9
 */
10
var UnspentOutputFinder = function(bitcoinDataClient, options) {
11
    this.defaultSettings = {
12
        logging: false,
13
        batchChunkSize: 200
14
    };
15
    this.settings = _.merge({}, this.defaultSettings, options);
16
    this.dataClient = bitcoinDataClient;
17
};
18
19
/**
20
 * get unspent outputs for an array of addresses
21
 *
22
 * @param addresses         an array of addresses to find unspent output for
23
 * @returns {q.Promise}     resolves with an object (associative array) of unspent outputs for each address with a spendable balance
24
 */
25
UnspentOutputFinder.prototype.getUTXOs = function(addresses) {
26
    var self = this;
27
    var results = {};
28
29
    var deferred = q.defer();
30
31
    //do batch if the bitcoin service supports it...
32
    async.eachSeries(_.chunk(addresses, self.settings.batchChunkSize), function(addressBatch, done) {
33
        if (self.settings.logging) {
34
            console.log("checking batch of " + addressBatch.length + " addresses for UTXOs", addressBatch.join(","));
0 ignored issues
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
35
        }
36
37
        //get the utxos for this address
38
        self.dataClient.getBatchUnspentOutputs(addressBatch).done(function(batchResults) {
39
            _.each(batchResults, function(utxos, address) {
40
                //add the found utxos to the final result
41
                if (utxos.length > 0) {
42
                    results[address] = utxos;
43
                }
44
            });
45
            //this iteration is complete
46
            done();
47
        }, function(err) {
48
            done(err);
49
        });
50
51
    }, function(err) {
52
        //callback
53
        if (err) {
54
            //perhaps we should also reject the promise, and stop everything?
55
            console.log("error encountered", err);
0 ignored issues
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
56
        }
57
58
        //resolve the promise
59
        deferred.resolve(results);
60
    });
61
62
    return deferred.promise;
63
};
64
65
module.exports = UnspentOutputFinder;
66