From 28ef9a22dcc8aaf0cecca1a069897c18796ff9dc Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Sun, 16 Apr 2017 22:23:20 -0300 Subject: [PATCH] Enable SendMax to top up debit card --- src/js/controllers/amount.js | 22 ++++++++---------- src/js/controllers/topup.js | 27 +++++++++++++++++++++- src/js/routes.js | 3 ++- src/js/services/sendMax.js | 36 ++++++++++++++++++++++++++++++ src/js/services/txFormatService.js | 8 +++++++ 5 files changed, 81 insertions(+), 15 deletions(-) create mode 100644 src/js/services/sendMax.js diff --git a/src/js/controllers/amount.js b/src/js/controllers/amount.js index 9e808d4df..92c5c6219 100644 --- a/src/js/controllers/amount.js +++ b/src/js/controllers/amount.js @@ -22,7 +22,8 @@ angular.module('copayApp.controllers').controller('amountController', function($ $scope.currency = data.stateParams.currency; $scope.forceCurrency = data.stateParams.forceCurrency; - $scope.showMenu = $ionicHistory.backView() && $ionicHistory.backView().stateName == 'tabs.send'; + $scope.showMenu = $ionicHistory.backView() && ($ionicHistory.backView().stateName == 'tabs.send' || + $ionicHistory.backView().stateName == 'tabs.bitpayCard'); $scope.recipientType = data.stateParams.recipientType || null; $scope.toAddress = data.stateParams.toAddress; $scope.toName = data.stateParams.toName; @@ -111,15 +112,8 @@ angular.module('copayApp.controllers').controller('amountController', function($ $scope.sendMax = function() { $scope.showSendMax = false; - $state.transitionTo('tabs.send.confirm', { - recipientType: $scope.recipientType, - toAmount: null, - toAddress: $scope.toAddress, - toName: $scope.toName, - toEmail: $scope.toEmail, - toColor: $scope.toColor, - useSendMax: true, - }); + $scope.useSendMax = true; + $scope.finish(); }; $scope.toggleAlternative = function() { @@ -234,8 +228,9 @@ angular.module('copayApp.controllers').controller('amountController', function($ if ($scope.nextStep) { $state.transitionTo($scope.nextStep, { id: _cardId, - amount: _amount, - currency: $scope.showAlternativeAmount ? $scope.alternativeIsoCode : $scope.unitName + amount: $scope.useSendMax ? null : _amount, + currency: $scope.showAlternativeAmount ? $scope.alternativeIsoCode : $scope.unitName, + useSendMax: $scope.useSendMax }); } else { var amount = $scope.showAlternativeAmount ? fromFiat(_amount) : _amount; @@ -247,11 +242,12 @@ angular.module('copayApp.controllers').controller('amountController', function($ } else { $state.transitionTo('tabs.send.confirm', { recipientType: $scope.recipientType, - toAmount: (amount * unitToSatoshi).toFixed(0), + toAmount: $scope.useSendMax ? null : (amount * unitToSatoshi).toFixed(0), toAddress: $scope.toAddress, toName: $scope.toName, toEmail: $scope.toEmail, toColor: $scope.toColor, + useSendMax: $scope.useSendMax }); } } diff --git a/src/js/controllers/topup.js b/src/js/controllers/topup.js index c7894185d..1a2d2ac03 100644 --- a/src/js/controllers/topup.js +++ b/src/js/controllers/topup.js @@ -1,10 +1,11 @@ 'use strict'; -angular.module('copayApp.controllers').controller('topUpController', function($scope, $log, $state, $timeout, $ionicHistory, lodash, popupService, profileService, ongoingProcess, walletService, configService, platformInfo, bitpayService, bitpayCardService, payproService, bwcError, txFormatService) { +angular.module('copayApp.controllers').controller('topUpController', function($scope, $log, $state, $timeout, $ionicHistory, lodash, popupService, profileService, ongoingProcess, walletService, configService, platformInfo, bitpayService, bitpayCardService, payproService, bwcError, txFormatService, sendMaxService) { var amount; var currency; var cardId; + var sendMax; $scope.isCordova = platformInfo.isCordova; @@ -51,6 +52,7 @@ angular.module('copayApp.controllers').controller('topUpController', function($s $scope.$on("$ionicView.beforeEnter", function(event, data) { cardId = data.stateParams.id; + sendMax = data.stateParams.useSendMax; if (!cardId) { showErrorAndBack('No card selected'); @@ -188,6 +190,29 @@ angular.module('copayApp.controllers').controller('topUpController', function($s $scope.onWalletSelect = function(wallet) { $scope.wallet = wallet; + if (sendMax) { + ongoingProcess.set('retrievingInputs', true); + sendMaxService.getInfo($scope.wallet, function(err, values) { + ongoingProcess.set('retrievingInputs', false); + if (err) { + showErrorAndBack(err); + return; + } + var config = configService.getSync().wallet.settings; + var unitName = config.unitName; + var amountUnit = txFormatService.satToUnit(values.amount); + var parsedAmount = txFormatService.parseAmount( + amountUnit, + unitName); + + amount = parsedAmount.amount; + currency = parsedAmount.currency; + $scope.amountUnitStr = parsedAmount.amountUnitStr; + $timeout(function() { + $scope.$digest(); + }, 100); + }); + } }; $scope.goBackHome = function() { diff --git a/src/js/routes.js b/src/js/routes.js index 45cbc9249..42d2f6ccc 100644 --- a/src/js/routes.js +++ b/src/js/routes.js @@ -1103,7 +1103,8 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr }, params: { id: null, - currency: 'USD' + currency: 'USD', + useSendMax: null } }) .state('tabs.bitpayCard.amount', { diff --git a/src/js/services/sendMax.js b/src/js/services/sendMax.js new file mode 100644 index 000000000..13af6024a --- /dev/null +++ b/src/js/services/sendMax.js @@ -0,0 +1,36 @@ +'use strict'; + +angular.module('copayApp.services').service('sendMaxService', function(feeService, configService, walletService) { + + /** + * Get sendMaxInfo + * + * @param {Obj} Wallet + * @param {Callback} Function (optional) + * + */ + this.getInfo = function(wallet, cb) { + feeService.getCurrentFeeValue(wallet.credentials.network, function(err, feePerKb) { + if (err) return cb(err); + + var config = configService.getSync().wallet; + + walletService.getSendMaxInfo(wallet, { + feePerKb: feePerKb, + excludeUnconfirmedUtxos: !config.spendUnconfirmed, + returnInputs: true, + }, function(err, resp) { + if (err) return cb(err); + + return cb(null, { + sendMax: true, + amount: resp.amount, + inputs: resp.inputs, + fee: resp.fee, + feePerKb: feePerKb, + }); + }); + }); + }; + +}); diff --git a/src/js/services/txFormatService.js b/src/js/services/txFormatService.js index a28079d3f..7524bb548 100644 --- a/src/js/services/txFormatService.js +++ b/src/js/services/txFormatService.js @@ -184,5 +184,13 @@ angular.module('copayApp.services').factory('txFormatService', function($filter, }; }; + root.satToUnit = function(amount) { + var config = configService.getSync().wallet.settings; + var unitToSatoshi = config.unitToSatoshi; + var satToUnit = 1 / unitToSatoshi; + var unitDecimals = config.unitDecimals; + return parseFloat((amount * satToUnit).toFixed(unitDecimals)); + }; + return root; });