Browse Source

cleaner code

activeAddress
Ivan Socolsky 9 years ago
parent
commit
859b1cf042
  1. 33
      lib/server.js
  2. 4
      test/integration/server.js

33
lib/server.js

@ -626,6 +626,9 @@ WalletService.prototype._getBlockchainExplorer = function(network) {
WalletService.prototype.getUtxos = function(cb) { WalletService.prototype.getUtxos = function(cb) {
var self = this; var self = this;
function utxoKey(utxo) {
return utxo.txid + '|' + utxo.vout
};
// Get addresses for this wallet // Get addresses for this wallet
self.storage.fetchAddresses(self.walletId, function(err, addresses) { self.storage.fetchAddresses(self.walletId, function(err, addresses) {
@ -651,24 +654,13 @@ WalletService.prototype.getUtxos = function(cb) {
self.getPendingTxs({}, function(err, txps) { self.getPendingTxs({}, function(err, txps) {
if (err) return cb(err); if (err) return cb(err);
var utxoKey = function(utxo) { var lockedInputs = _.map(_.flatten(_.pluck(txps, 'inputs')), utxoKey);
return utxo.txid + '|' + utxo.vout
};
var inputs = _.chain(txps)
.pluck('inputs')
.flatten()
.map(utxoKey)
.value();
var dictionary = _.reduce(utxos, function(memo, utxo) { var utxoIndex = _.indexBy(utxos, utxoKey);
memo[utxoKey(utxo)] = utxo;
return memo;
}, {});
_.each(inputs, function(input) { _.each(lockedInputs, function(input) {
if (dictionary[input]) { if (utxoIndex[input]) {
dictionary[input].locked = true; utxoIndex[input].locked = true;
} }
}); });
@ -703,9 +695,7 @@ WalletService.prototype._totalizeUtxos = function(utxos) {
WalletService.prototype._computeKbToSendMax = function(utxos, amount, cb) { WalletService.prototype._computeKbToSendMax = function(utxos, amount, cb) {
var self = this; var self = this;
var unlockedUtxos = _.filter(utxos, { var unlockedUtxos = _.reject(utxos, 'locked');
locked: false
});
if (_.isEmpty(unlockedUtxos)) return cb(null, 0); if (_.isEmpty(unlockedUtxos)) return cb(null, 0);
self.getWallet({}, function(err, wallet) { self.getWallet({}, function(err, wallet) {
@ -1500,10 +1490,7 @@ WalletService.prototype.getTxHistory = function(opts, cb) {
var filter = {}; var filter = {};
if (_.isBoolean(isMine)) filter.isMine = isMine; if (_.isBoolean(isMine)) filter.isMine = isMine;
if (_.isBoolean(isChange)) filter.isChange = isChange; if (_.isBoolean(isChange)) filter.isChange = isChange;
return _.reduce(_.where(items, filter), return _.sum(_.filter(items, filter), 'amount');
function(memo, item) {
return memo + item.amount;
}, 0);
}; };
function classify(items) { function classify(items) {

4
test/integration/server.js

@ -288,11 +288,11 @@ helpers.createAddresses = function(server, wallet, main, change, cb) {
var storage, blockchainExplorer; var storage, blockchainExplorer;
var useMongo = false; var useMongoDb = !!process.env.USE_MONGO_DB;
function initStorage(cb) { function initStorage(cb) {
function getDb(cb) { function getDb(cb) {
if (useMongo) { if (useMongoDb) {
var mongodb = require('mongodb'); var mongodb = require('mongodb');
mongodb.MongoClient.connect('mongodb://localhost:27017/bws_test', function(err, db) { mongodb.MongoClient.connect('mongodb://localhost:27017/bws_test', function(err, db) {
if (err) throw err; if (err) throw err;

Loading…
Cancel
Save