refactor wallet seletors in buy/sell controllers

This commit is contained in:
Matias Alejo Garcia 2016-06-11 23:55:28 -03:00
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 <div
ng-if="index.coinbaseToken" ng-if="index.coinbaseToken"
ng-init="buy.init(index.coinbaseTestnet)" ng-init="buy.init(index.coinbaseTestnet)"
ng-click="openWalletsModal(buy.otherWallets)"> ng-click="openWalletsModal(buy.allWallets)">
<label>Copay Wallet</label> <label>Copay Wallet</label>
<div class="input"> <div class="input">
<input type="text" id="address" name="address" ng-disabled="buy.selectedWalletId" <input type="text" id="address" name="address" ng-disabled="buy.selectedWalletId"

View file

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

View file

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

View file

@ -41,7 +41,7 @@
<div ng-if="index.glideraToken" <div ng-if="index.glideraToken"
ng-init="sell.init(index.glideraTestnet)" ng-init="sell.init(index.glideraTestnet)"
ng-click="openWalletsModal(sell.otherWallets)"> ng-click="openWalletsModal(sell.allWallets)">
<label>Wallet</label> <label>Wallet</label>
<div class="input"> <div class="input">
<input type="text" id="address" name="address" ng-disabled="sell.selectedWalletId" <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 self = this;
var fc; 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) { this.init = function(testnet) {
self.otherWallets = otherWallets(testnet); self.allWallets = profileService.getWallets(testnet ? 'testnet' : 'livenet', 1)
// Choose focused wallet
try { client = profileService.focusedClient;
var currentWalletId = profileService.focusedClient.credentials.walletId; if (client) {
lodash.find(self.otherWallets, function(w) { $timeout(function() {
if (w.id == currentWalletId) { self.selectedWalletId = client.credentials.walletId;
$timeout(function() { self.selectedWalletName = client.credentials.walletName;
self.selectedWalletId = w.id; $scope.$apply();
self.selectedWalletName = w.name; }, 100);
fc = profileService.getClient(w.id); }
$scope.$apply();
}, 100);
}
});
} catch (e) {
$log.debug(e);
};
}; };
this.getPaymentMethods = function(token) { this.getPaymentMethods = function(token) {

View file

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

View file

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