Browse Source

refactor code

activeAddress
Ivan Socolsky 10 years ago
parent
commit
999bcbbe7d
  1. 53
      lib/server.js
  2. 2
      test/integration/server.js

53
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. * Creates a new transaction proposal.
* @param {Object} opts * @param {Object} opts
@ -688,31 +713,11 @@ WalletService.prototype.getBalance = function(opts, cb) {
balance.byAddress = _.values(byAddress); balance.byAddress = _.values(byAddress);
// Compute KB of a tx to send all non-locked funds self._computeKbToSendMax(utxos, balance.totalAmount - balance.lockedAmount, function(err, sizeInKb) {
balance.totalKbToSendMax = 0; if (err) {
self.getWallet({}, function(err, wallet) { log.error('Could not compute fees needed to transfer max amount', err);
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);
}
} }
balance.totalKbToSendMax = sizeInKb || 0;
return cb(null, balance); return cb(null, balance);
}); });
}); });

2
test/integration/server.js

@ -216,7 +216,7 @@ helpers.createAddresses = function(server, wallet, main, change, cb) {
var storage, blockchainExplorer; var storage, blockchainExplorer;
var useMongo = true; var useMongo = false;
function initStorage(cb) { function initStorage(cb) {
function getDb(cb) { function getDb(cb) {

Loading…
Cancel
Save