From 931923f585d12fad4f36d9a784479eb4b5262583 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Thu, 18 Jun 2015 13:20:19 -0300 Subject: [PATCH] compute kbs needed to send max in getBalance --- lib/model/txproposal.js | 4 ++++ lib/server.js | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/lib/model/txproposal.js b/lib/model/txproposal.js index 97ae8fb..ef120d3 100644 --- a/lib/model/txproposal.js +++ b/lib/model/txproposal.js @@ -74,6 +74,10 @@ TxProposal.fromObj = function(obj) { return x; }; +TxProposal.prototype.setInputs = function(inputs) { + this.inputs = inputs; + this.inputPaths = _.pluck(inputs, 'path'); +}; TxProposal.prototype._updateStatus = function() { if (this.status != 'pending') return; diff --git a/lib/server.js b/lib/server.js index 740107d..627d0c7 100644 --- a/lib/server.js +++ b/lib/server.js @@ -602,7 +602,9 @@ WalletService.prototype._getUtxos = function(cb) { return cb(new ClientError('BLOCKCHAINERROR', 'Could not fetch unspent outputs')); } var utxos = _.map(inutxos, function(utxo) { - return _.pick(utxo, ['txid', 'vout', 'address', 'scriptPubKey', 'amount', 'satoshis']); + var u = _.pick(utxo, ['txid', 'vout', 'address', 'scriptPubKey', 'amount', 'satoshis']); + u.locked = false; + return u; }); self.getPendingTxs({}, function(err, txps) { if (err) return cb(err); @@ -686,7 +688,33 @@ WalletService.prototype.getBalance = function(opts, cb) { balance.byAddress = _.values(byAddress); - return cb(null, balance); + // 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); + } + } + + return cb(null, balance); + }); }); }; @@ -720,13 +748,12 @@ WalletService.prototype._selectTxInputs = function(txp, cb) { if (total >= txp.amount) { try { - txp.inputs = selected; + txp.setInputs(selected); bitcoreTx = txp.getBitcoreTx(); bitcoreError = bitcoreTx.getSerializationError({ disableIsFullySigned: true, }); if (!bitcoreError) { - txp.inputPaths = _.pluck(txp.inputs, 'path'); txp.fee = bitcoreTx.getFee(); return cb(); }