Browse Source

compute kbs needed to send max in getBalance

activeAddress
Ivan Socolsky 10 years ago
parent
commit
931923f585
  1. 4
      lib/model/txproposal.js
  2. 35
      lib/server.js

4
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;

35
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();
}

Loading…
Cancel
Save