diff --git a/lib/storage.js b/lib/storage.js index 25f571b..9e47949 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -931,18 +931,26 @@ Storage.prototype.updateHasBalance = function(addresses, hasBalance, cb) { Storage.prototype.fetchAddressesWithBalance = function(walletId, cb) { var self = this; - this.db.collection(collections.ADDRESSES).find({ - walletId: walletId, - $or: [{ + function getResult(cb) { + self.db.collection(collections.ADDRESSES).find({ + walletId: walletId, hasBalance: true, - }, { - hasBalance: { - $exists: false - }, - }] - }).toArray(function(err, result) { + }).toArray(function(err, result) { + if (err) return cb(err); + if (!_.isEmpty(result)) return cb(null, result); + self.db.collection(collections.ADDRESSES).find({ + walletId: walletId, + hasBalance: { + $exists: false + }, + }).toArray(cb); + }); + }; + + getResult(function(err, result) { if (err) return cb(err); if (!result) return cb(); + var addresses = _.map(result, function(address) { return Model.Address.fromObj(address); });