cleaner code

This commit is contained in:
Ivan Socolsky 2015-07-20 13:44:39 -03:00
parent 9a5daa5bf4
commit 859b1cf042
2 changed files with 12 additions and 25 deletions

View File

@ -626,6 +626,9 @@ WalletService.prototype._getBlockchainExplorer = function(network) {
WalletService.prototype.getUtxos = function(cb) {
var self = this;
function utxoKey(utxo) {
return utxo.txid + '|' + utxo.vout
};
// Get addresses for this wallet
self.storage.fetchAddresses(self.walletId, function(err, addresses) {
@ -651,24 +654,13 @@ WalletService.prototype.getUtxos = function(cb) {
self.getPendingTxs({}, function(err, txps) {
if (err) return cb(err);
var utxoKey = function(utxo) {
return utxo.txid + '|' + utxo.vout
};
var lockedInputs = _.map(_.flatten(_.pluck(txps, 'inputs')), utxoKey);
var inputs = _.chain(txps)
.pluck('inputs')
.flatten()
.map(utxoKey)
.value();
var utxoIndex = _.indexBy(utxos, utxoKey);
var dictionary = _.reduce(utxos, function(memo, utxo) {
memo[utxoKey(utxo)] = utxo;
return memo;
}, {});
_.each(inputs, function(input) {
if (dictionary[input]) {
dictionary[input].locked = true;
_.each(lockedInputs, function(input) {
if (utxoIndex[input]) {
utxoIndex[input].locked = true;
}
});
@ -703,9 +695,7 @@ WalletService.prototype._totalizeUtxos = function(utxos) {
WalletService.prototype._computeKbToSendMax = function(utxos, amount, cb) {
var self = this;
var unlockedUtxos = _.filter(utxos, {
locked: false
});
var unlockedUtxos = _.reject(utxos, 'locked');
if (_.isEmpty(unlockedUtxos)) return cb(null, 0);
self.getWallet({}, function(err, wallet) {
@ -1500,10 +1490,7 @@ WalletService.prototype.getTxHistory = function(opts, cb) {
var filter = {};
if (_.isBoolean(isMine)) filter.isMine = isMine;
if (_.isBoolean(isChange)) filter.isChange = isChange;
return _.reduce(_.where(items, filter),
function(memo, item) {
return memo + item.amount;
}, 0);
return _.sum(_.filter(items, filter), 'amount');
};
function classify(items) {

View File

@ -288,11 +288,11 @@ helpers.createAddresses = function(server, wallet, main, change, cb) {
var storage, blockchainExplorer;
var useMongo = false;
var useMongoDb = !!process.env.USE_MONGO_DB;
function initStorage(cb) {
function getDb(cb) {
if (useMongo) {
if (useMongoDb) {
var mongodb = require('mongodb');
mongodb.MongoClient.connect('mongodb://localhost:27017/bws_test', function(err, db) {
if (err) throw err;