From 999bcbbe7dbe7b2a763014c548c0f1137b2d1279 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Thu, 18 Jun 2015 13:31:53 -0300 Subject: [PATCH] refactor code --- lib/server.js | 53 +++++++++++++++++++++----------------- test/integration/server.js | 2 +- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/lib/server.js b/lib/server.js index 627d0c7..dd1d114 100644 --- a/lib/server.js +++ b/lib/server.js @@ -659,6 +659,31 @@ WalletService.prototype._totalizeUtxos = function(utxos) { }; +WalletService.prototype._computeKbToSendMax = function(utxos, amount, cb) { + var self = this; + + var unlockedUtxos = _.filter(utxos, { + locked: false + }); + if (_.isEmpty(unlockedUtxos)) return cb(null, 0); + + self.getWallet({}, function(err, wallet) { + if (err) return cb(err); + + var t = WalletUtils.newBitcoreTransaction(); + try { + _.each(unlockedUtxos, function(i) { + t.from(i, i.publicKeys, wallet.m); + }); + t.to(utxos[0].address, amount); + var sizeInKb = Math.ceil(t._estimateSize() / 1000); + return cb(null, sizeInKb); + } catch (ex) { + return cb(ex); + } + }); +}; + /** * Creates a new transaction proposal. * @param {Object} opts @@ -688,31 +713,11 @@ WalletService.prototype.getBalance = function(opts, cb) { balance.byAddress = _.values(byAddress); - // Compute KB of a tx to send all non-locked funds - balance.totalKbToSendMax = 0; - self.getWallet({}, function(err, wallet) { - if (err) return cb(err); - - var unlockedUtxos = _.filter(utxos, { - locked: false - }); - if (unlockedUtxos.length > 0) { - var t = WalletUtils.newBitcoreTransaction(); - - try { - _.each(unlockedUtxos, function(i) { - t.from(i, i.publicKeys, wallet.m); - }); - - var address = utxos[0].address; - t.to(address, balance.totalAmount - balance.lockedAmount); - - balance.totalKbToSendMax = Math.ceil(t._estimateSize() / 1000); - } catch (ex) { - log.error('Could not compute fees needed to transfer max amount', ex); - } + self._computeKbToSendMax(utxos, balance.totalAmount - balance.lockedAmount, function(err, sizeInKb) { + if (err) { + log.error('Could not compute fees needed to transfer max amount', err); } - + balance.totalKbToSendMax = sizeInKb || 0; return cb(null, balance); }); }); diff --git a/test/integration/server.js b/test/integration/server.js index debae21..2266865 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -216,7 +216,7 @@ helpers.createAddresses = function(server, wallet, main, change, cb) { var storage, blockchainExplorer; -var useMongo = true; +var useMongo = false; function initStorage(cb) { function getDb(cb) {