From 7e861532727f9d9f84800c400088382b1fcfc17d Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Wed, 28 Jun 2017 10:21:53 -0300 Subject: [PATCH] Fix names. Fix sendMax --- src/js/controllers/topup.js | 31 +++++++++++++++++++----------- src/js/services/txFormatService.js | 10 ++++++++-- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/js/controllers/topup.js b/src/js/controllers/topup.js index 7cb3236c7..c75672b42 100644 --- a/src/js/controllers/topup.js +++ b/src/js/controllers/topup.js @@ -29,8 +29,8 @@ angular.module('copayApp.controllers').controller('topUpController', function($s popupService.showAlert(title, msg); }; - var satToAlternative = function(sat, cb) { - txFormatService.formatToCode(sat, $scope.currencyIsoCode, function(value) { + var satToFiat = function(sat, cb) { + txFormatService.toFiat(sat, $scope.currencyIsoCode, function(value) { return cb(value); }); }; @@ -61,13 +61,13 @@ angular.module('copayApp.controllers').controller('topUpController', function($s }; var setTotalAmount = function(amountSat, invoiceFeeSat, networkFeeSat) { - satToAlternative(amountSat, function(a) { + satToFiat(amountSat, function(a) { $scope.amount = Number(a); - satToAlternative(invoiceFeeSat, function(i) { + satToFiat(invoiceFeeSat, function(i) { $scope.invoiceFee = Number(i); - satToAlternative(networkFeeSat, function(n) { + satToFiat(networkFeeSat, function(n) { $scope.networkFee = Number(n); $scope.totalAmount = $scope.amount + $scope.invoiceFee + $scope.networkFee; $timeout(function() { @@ -140,25 +140,32 @@ angular.module('copayApp.controllers').controller('topUpController', function($s }); }; - var parseAmount = function(wallet, cb) { + var calculateAmount = function(wallet, cb) { + // Global variables defined beforeEnter + var a = amount; + var c = currency; + if (useSendMax) { - sendMaxService.getInfo(wallet, function(err, values) { + sendMaxService.getInfo(wallet, function(err, maxValues) { if (err) { return cb({ title: null, message: err }) } - var maxAmountBtc = Number((values.amount / 100000000).toFixed(8)); + var maxAmountBtc = Number((maxValues.amount / 100000000).toFixed(8)); createInvoice({amount: maxAmountBtc, currency: 'BTC'}, function(err, inv) { if (err) return cb(err); - return cb(null, txFormatService.parseAmount(maxAmountBtc - inv.buyerPaidBtcMinerFee, 'BTC')); + var invoiceFeeSat = parseInt((inv.buyerPaidBtcMinerFee * 100000000).toFixed()); + var newAmountSat = maxValues.amount - invoiceFeeSat; + + return cb(null, newAmountSat, 'sat'); }); }); } else { - return cb(null, txFormatService.parseAmount(amount, currency)); + return cb(null, a, c); } }; @@ -190,6 +197,7 @@ angular.module('copayApp.controllers').controller('topUpController', function($s return; } + // Save TX in memory createdTx = ctxp; $scope.totalAmountStr = txFormatService.formatAmountStr(ctxp.amount); @@ -278,12 +286,13 @@ angular.module('copayApp.controllers').controller('topUpController', function($s $scope.onWalletSelect = function(wallet) { $scope.wallet = wallet; ongoingProcess.set('retrievingInputs', true); - parseAmount(wallet, function(err, parsedAmount) { + calculateAmount(wallet, function(err, a, c) { ongoingProcess.set('retrievingInputs', false); if (err) { showErrorAndBack(err.title, err.message); return; } + var parsedAmount = txFormatService.parseAmount(a, c); initializeTopUp(wallet, parsedAmount); }); }; diff --git a/src/js/services/txFormatService.js b/src/js/services/txFormatService.js index 269064ac4..0df46fe86 100644 --- a/src/js/services/txFormatService.js +++ b/src/js/services/txFormatService.js @@ -23,7 +23,7 @@ angular.module('copayApp.services').factory('txFormatService', function($filter, return root.formatAmount(satoshis) + ' ' + config.unitName; }; - root.formatToCode = function(satoshis, code, cb) { + root.toFiat = function(satoshis, code, cb) { if (isNaN(satoshis)) return; var val = function() { var v1 = rateService.toFiat(satoshis, code); @@ -189,9 +189,15 @@ angular.module('copayApp.services').factory('txFormatService', function($filter, var alternativeIsoCode = config.alternativeIsoCode; // If fiat currency - if (currency != 'bits' && currency != 'BTC') { + if (currency != 'bits' && currency != 'BTC' && currency != 'sat') { amountUnitStr = $filter('formatFiatAmount')(amount) + ' ' + currency; amountSat = rateService.fromFiat(amount, currency).toFixed(0); + } else if (currency == 'sat') { + amountSat = amount; + amountUnitStr = root.formatAmountStr(amountSat); + // convert sat to BTC + amount = (amountSat * satToBtc).toFixed(8); + currency = 'BTC'; } else { amountSat = parseInt((amount * unitToSatoshi).toFixed(0)); amountUnitStr = root.formatAmountStr(amountSat);