refactor wallet seletors in buy/sell controllers

This commit is contained in:
Matias Alejo Garcia 2016-06-11 23:55:28 -03:00
parent 27e925b84f
commit 4b0abd77a2
No known key found for this signature in database
GPG Key ID: 02470DB551277AB3
9 changed files with 73 additions and 119 deletions

View File

@ -84,7 +84,7 @@
<div
ng-if="index.coinbaseToken"
ng-init="buy.init(index.coinbaseTestnet)"
ng-click="openWalletsModal(buy.otherWallets)">
ng-click="openWalletsModal(buy.allWallets)">
<label>Copay Wallet</label>
<div class="input">
<input type="text" id="address" name="address" ng-disabled="buy.selectedWalletId"

View File

@ -42,7 +42,7 @@
<div ng-if="index.glideraToken"
ng-init="buy.init(index.glideraTestnet)"
ng-click="openWalletsModal(buy.otherWallets)">
ng-click="openWalletsModal(buy.allWallets)">
<label>Wallet</label>
<div class="input">
<input type="text" id="address" name="address" ng-disabled="buy.selectedWalletId"

View File

@ -38,7 +38,7 @@
<div
ng-if="index.coinbaseToken"
ng-init="sell.init(index.coinbaseTestnet)"
ng-click="openWalletsModal(sell.otherWallets)">
ng-click="openWalletsModal(sell.allWallets)">
<label>Copay Wallet</label>
<div class="input">
<input type="text" id="address" name="address" ng-disabled="sell.selectedWalletId"

View File

@ -41,7 +41,7 @@
<div ng-if="index.glideraToken"
ng-init="sell.init(index.glideraTestnet)"
ng-click="openWalletsModal(sell.otherWallets)">
ng-click="openWalletsModal(sell.allWallets)">
<label>Wallet</label>
<div class="input">
<input type="text" id="address" name="address" ng-disabled="sell.selectedWalletId"

View File

@ -5,31 +5,17 @@ angular.module('copayApp.controllers').controller('buyCoinbaseController',
var self = this;
var fc;
var otherWallets = function(testnet) {
var network = testnet ? 'testnet' : 'livenet';
return lodash.filter(profileService.getWallets(network), function(w) {
return w.network == network;
});
};
this.init = function(testnet) {
self.otherWallets = otherWallets(testnet);
// Choose focused wallet
try {
var currentWalletId = profileService.focusedClient.credentials.walletId;
lodash.find(self.otherWallets, function(w) {
if (w.id == currentWalletId) {
$timeout(function() {
self.selectedWalletId = w.id;
self.selectedWalletName = w.name;
fc = profileService.getClient(w.id);
$scope.$apply();
}, 100);
}
});
} catch (e) {
$log.debug(e);
};
self.allWallets = profileService.getWallets(testnet ? 'testnet' : 'livenet', 1)
client = profileService.focusedClient;
if (client) {
$timeout(function() {
self.selectedWalletId = client.credentials.walletId;
self.selectedWalletName = client.credentials.walletName;
$scope.$apply();
}, 100);
}
};
this.getPaymentMethods = function(token) {

View File

@ -8,30 +8,18 @@ angular.module('copayApp.controllers').controller('buyGlideraController',
this.error = null;
this.success = null;
this.loading = null;
var otherWallets = function(testnet) {
var network = testnet ? 'testnet' : 'livenet';
return lodash.filter(profileService.getWallets(network), function(w) {
return w.network == network;
});
};
this.init = function(testnet) {
self.otherWallets = otherWallets(testnet);
// Choose focused wallet
try {
var currentWalletId = profileService.focusedClient.credentials.walletId;
lodash.find(self.otherWallets, function(w) {
if (w.id == currentWalletId) {
$timeout(function() {
self.selectedWalletId = w.id;
self.selectedWalletName = w.name;
$scope.$apply();
}, 100);
}
});
} catch (e) {
$log.debug(e);
};
self.allWallets = profileService.getWallets(testnet ? 'testnet' : 'livenet', 1)
client = profileService.focusedClient;
if (client) {
$timeout(function() {
self.selectedWalletId = client.credentials.walletId;
self.selectedWalletName = client.credentials.walletName;
$scope.$apply();
}, 100);
}
};
$scope.openWalletsModal = function(wallets) {

View File

@ -4,7 +4,7 @@ angular.module('copayApp.controllers').controller('sellCoinbaseController',
function($rootScope, $scope, $modal, $log, $timeout, $ionicModal, lodash, profileService, coinbaseService, bwsError, configService, walletService, fingerprintService) {
var self = this;
var fc;
var client;
$scope.priceSensitivity = [
{
@ -30,13 +30,6 @@ angular.module('copayApp.controllers').controller('sellCoinbaseController',
];
$scope.selectedPriceSensitivity = $scope.priceSensitivity[1];
var otherWallets = function(testnet) {
var network = testnet ? 'testnet' : 'livenet';
return lodash.filter(profileService.getWallets(network), function(w) {
return w.network == network && w.m == 1;
});
};
var handleEncryptedWallet = function(client, cb) {
if (!walletService.isEncrypted(client)) return cb();
$rootScope.$emit('Local/NeedsPassword', false, function(err, password) {
@ -46,23 +39,16 @@ angular.module('copayApp.controllers').controller('sellCoinbaseController',
};
this.init = function(testnet) {
self.otherWallets = otherWallets(testnet);
// Choose focused wallet
try {
var currentWalletId = profileService.focusedClient.credentials.walletId;
lodash.find(self.otherWallets, function(w) {
if (w.id == currentWalletId) {
$timeout(function() {
self.selectedWalletId = w.id;
self.selectedWalletName = w.name;
fc = profileService.getClient(w.id);
$scope.$apply();
}, 100);
}
});
} catch (e) {
$log.debug(e);
};
self.allWallets = profileService.getWallets(testnet ? 'testnet' : 'livenet', 1);
client = profileService.focusedClient;
if (client) {
$timeout(function() {
self.selectedWalletId = client.credentials.walletId;
self.selectedWalletName = client.credentials.walletName;
$scope.$apply();
}, 100);
}
};
this.getPaymentMethods = function(token) {
@ -198,7 +184,7 @@ angular.module('copayApp.controllers').controller('sellCoinbaseController',
feeLevel: walletSettings.feeLevel || 'normal'
};
walletService.createTx(fc, txp, function(err, createdTxp) {
walletService.createTx(client, txp, function(err, createdTxp) {
if (err) {
$log.debug(err);
self.loading = null;
@ -238,7 +224,7 @@ angular.module('copayApp.controllers').controller('sellCoinbaseController',
ctx['price_sensitivity'] = $scope.selectedPriceSensitivity;
ctx['sell_price_amount'] = self.sellPrice.amount;
ctx['sell_price_currency'] = self.sellPrice.currency;
ctx['description'] = 'Copay Wallet: ' + fc.credentials.walletName;
ctx['description'] = 'Copay Wallet: ' + client.credentials.walletName;
coinbaseService.savePendingTransaction(ctx, null, function(err) {
if (err) $log.debug(err);
self.sendInfo = ctx;
@ -261,20 +247,20 @@ angular.module('copayApp.controllers').controller('sellCoinbaseController',
this.confirmTx = function(txp, cb) {
fingerprintService.check(fc, function(err) {
fingerprintService.check(client, function(err) {
if (err) {
$log.debug(err);
return cb(err);
}
handleEncryptedWallet(fc, function(err) {
handleEncryptedWallet(client, function(err) {
if (err) {
$log.debug(err);
return cb(err);
}
self.loading = 'Sending bitcoin to Coinbase...';
walletService.publishTx(fc, txp, function(err, publishedTxp) {
walletService.publishTx(client, txp, function(err, publishedTxp) {
if (err) {
self.loading = null;
$log.debug(err);
@ -285,12 +271,12 @@ angular.module('copayApp.controllers').controller('sellCoinbaseController',
});
}
walletService.signTx(fc, publishedTxp, function(err, signedTxp) {
walletService.lock(fc);
walletService.signTx(client, publishedTxp, function(err, signedTxp) {
walletService.lock(client);
if (err) {
self.loading = null;
$log.debug(err);
walletService.removeTx(fc, signedTxp, function(err) {
walletService.removeTx(client, signedTxp, function(err) {
if (err) $log.debug(err);
});
return cb({
@ -300,11 +286,11 @@ angular.module('copayApp.controllers').controller('sellCoinbaseController',
});
}
walletService.broadcastTx(fc, signedTxp, function(err, broadcastedTxp) {
walletService.broadcastTx(client, signedTxp, function(err, broadcastedTxp) {
if (err) {
self.loading = null;
$log.debug(err);
walletService.removeTx(fc, broadcastedTxp, function(err) {
walletService.removeTx(client, broadcastedTxp, function(err) {
if (err) $log.debug(err);
});
return cb({

View File

@ -10,14 +10,7 @@ angular.module('copayApp.controllers').controller('sellGlideraController',
this.success = null;
this.error = null;
this.loading = null;
var fc;
var otherWallets = function(testnet) {
var network = testnet ? 'testnet' : 'livenet';
return lodash.filter(profileService.getWallets(network), function(w) {
return w.network == network && w.m == 1;
});
};
var client;
var handleEncryptedWallet = function(client, cb) {
if (!walletService.isEncrypted(client)) return cb();
@ -28,25 +21,20 @@ angular.module('copayApp.controllers').controller('sellGlideraController',
};
this.init = function(testnet) {
self.otherWallets = otherWallets(testnet);
// Choose focused wallet
try {
var currentWalletId = profileService.focusedClient.credentials.walletId;
lodash.find(self.otherWallets, function(w) {
if (w.id == currentWalletId) {
$timeout(function() {
self.selectedWalletId = w.id;
self.selectedWalletName = w.name;
fc = profileService.getClient(w.id);
$scope.$apply();
}, 100);
}
});
} catch (e) {
$log.debug(e);
};
self.allWallets = profileService.getWallets(testnet ? 'testnet' : 'livenet', 1)
client = profileService.focusedClient;
if (client) {
$timeout(function() {
self.selectedWalletId = client.credentials.walletId;
self.selectedWalletName = client.credentials.walletName;
$scope.$apply();
}, 100);
}
};
$scope.openWalletsModal = function(wallets) {
self.error = null;
self.selectedWalletId = null;
@ -106,7 +94,7 @@ angular.module('copayApp.controllers').controller('sellGlideraController',
var configWallet = config.wallet;
var walletSettings = configWallet.settings;
addressService.getAddress(fc.credentials.walletId, null, function(err, refundAddress) {
addressService.getAddress(client.credentials.walletId, null, function(err, refundAddress) {
if (!refundAddress) {
self.loading = null;
self.error = bwsError.msg(err, 'Could not create address');
@ -141,7 +129,7 @@ angular.module('copayApp.controllers').controller('sellGlideraController',
};
self.loading = 'Creating transaction...';
walletService.createTx(fc, txp, function(err, createdTxp) {
walletService.createTx(client, txp, function(err, createdTxp) {
self.loading = null;
if (err) {
self.error = err.message ||  bwsError.msg(err);
@ -149,13 +137,13 @@ angular.module('copayApp.controllers').controller('sellGlideraController',
}
$scope.$emit('Local/NeedsConfirmation', createdTxp, function(accept) {
if (accept) {
fingerprintService.check(fc, function(err) {
fingerprintService.check(client, function(err) {
if (err) {
self.error = err.message ||  bwsError.msg(err);
return;
}
handleEncryptedWallet(fc, function(err) {
handleEncryptedWallet(client, function(err) {
if (err) {
self.error = err.message ||  bwsError.msg(err);
return;
@ -163,15 +151,15 @@ angular.module('copayApp.controllers').controller('sellGlideraController',
self.loading = 'Signing transaction...';
walletService.publishTx(fc, createdTxp, function(err, publishedTxp) {
walletService.publishTx(client, createdTxp, function(err, publishedTxp) {
if (err) {
self.loading = null;
self.error = err.message ||  bwsError.msg(err);
}
walletService.signTx(fc, publishedTxp, function(err, signedTxp) {
walletService.lock(fc);
walletService.removeTx(fc, signedTxp, function(err) {
walletService.signTx(client, publishedTxp, function(err, signedTxp) {
walletService.lock(client);
walletService.removeTx(client, signedTxp, function(err) {
if (err) $log.debug(err);
});
if (err) {

View File

@ -699,7 +699,7 @@ angular.module('copayApp.services')
});
};
root.getWallets = function(network) {
root.getWallets = function(network, n) {
if (!root.profile) return [];
var config = configService.getSync();
@ -721,6 +721,12 @@ angular.module('copayApp.services')
return (w.network == network);
});
}
if (n) {
ret = lodash.filter(ret, function(w) {
return (w.n == n);
});
}
return lodash.sortBy(ret, 'name');
};