refactor code
This commit is contained in:
parent
621fe1777a
commit
999bcbbe7d
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue