Fix names. Fix sendMax

This commit is contained in:
Gustavo Maximiliano Cortez 2017-06-28 10:21:53 -03:00
commit 7e86153272
No known key found for this signature in database
GPG key ID: 15EDAD8D9F2EB1AF
2 changed files with 28 additions and 13 deletions

View file

@ -29,8 +29,8 @@ angular.module('copayApp.controllers').controller('topUpController', function($s
popupService.showAlert(title, msg); popupService.showAlert(title, msg);
}; };
var satToAlternative = function(sat, cb) { var satToFiat = function(sat, cb) {
txFormatService.formatToCode(sat, $scope.currencyIsoCode, function(value) { txFormatService.toFiat(sat, $scope.currencyIsoCode, function(value) {
return cb(value); return cb(value);
}); });
}; };
@ -61,13 +61,13 @@ angular.module('copayApp.controllers').controller('topUpController', function($s
}; };
var setTotalAmount = function(amountSat, invoiceFeeSat, networkFeeSat) { var setTotalAmount = function(amountSat, invoiceFeeSat, networkFeeSat) {
satToAlternative(amountSat, function(a) { satToFiat(amountSat, function(a) {
$scope.amount = Number(a); $scope.amount = Number(a);
satToAlternative(invoiceFeeSat, function(i) { satToFiat(invoiceFeeSat, function(i) {
$scope.invoiceFee = Number(i); $scope.invoiceFee = Number(i);
satToAlternative(networkFeeSat, function(n) { satToFiat(networkFeeSat, function(n) {
$scope.networkFee = Number(n); $scope.networkFee = Number(n);
$scope.totalAmount = $scope.amount + $scope.invoiceFee + $scope.networkFee; $scope.totalAmount = $scope.amount + $scope.invoiceFee + $scope.networkFee;
$timeout(function() { $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) { if (useSendMax) {
sendMaxService.getInfo(wallet, function(err, values) { sendMaxService.getInfo(wallet, function(err, maxValues) {
if (err) { if (err) {
return cb({ return cb({
title: null, title: null,
message: err 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) { createInvoice({amount: maxAmountBtc, currency: 'BTC'}, function(err, inv) {
if (err) return cb(err); 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 { } 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; return;
} }
// Save TX in memory
createdTx = ctxp; createdTx = ctxp;
$scope.totalAmountStr = txFormatService.formatAmountStr(ctxp.amount); $scope.totalAmountStr = txFormatService.formatAmountStr(ctxp.amount);
@ -278,12 +286,13 @@ angular.module('copayApp.controllers').controller('topUpController', function($s
$scope.onWalletSelect = function(wallet) { $scope.onWalletSelect = function(wallet) {
$scope.wallet = wallet; $scope.wallet = wallet;
ongoingProcess.set('retrievingInputs', true); ongoingProcess.set('retrievingInputs', true);
parseAmount(wallet, function(err, parsedAmount) { calculateAmount(wallet, function(err, a, c) {
ongoingProcess.set('retrievingInputs', false); ongoingProcess.set('retrievingInputs', false);
if (err) { if (err) {
showErrorAndBack(err.title, err.message); showErrorAndBack(err.title, err.message);
return; return;
} }
var parsedAmount = txFormatService.parseAmount(a, c);
initializeTopUp(wallet, parsedAmount); initializeTopUp(wallet, parsedAmount);
}); });
}; };

View file

@ -23,7 +23,7 @@ angular.module('copayApp.services').factory('txFormatService', function($filter,
return root.formatAmount(satoshis) + ' ' + config.unitName; return root.formatAmount(satoshis) + ' ' + config.unitName;
}; };
root.formatToCode = function(satoshis, code, cb) { root.toFiat = function(satoshis, code, cb) {
if (isNaN(satoshis)) return; if (isNaN(satoshis)) return;
var val = function() { var val = function() {
var v1 = rateService.toFiat(satoshis, code); var v1 = rateService.toFiat(satoshis, code);
@ -189,9 +189,15 @@ angular.module('copayApp.services').factory('txFormatService', function($filter,
var alternativeIsoCode = config.alternativeIsoCode; var alternativeIsoCode = config.alternativeIsoCode;
// If fiat currency // If fiat currency
if (currency != 'bits' && currency != 'BTC') { if (currency != 'bits' && currency != 'BTC' && currency != 'sat') {
amountUnitStr = $filter('formatFiatAmount')(amount) + ' ' + currency; amountUnitStr = $filter('formatFiatAmount')(amount) + ' ' + currency;
amountSat = rateService.fromFiat(amount, currency).toFixed(0); 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 { } else {
amountSat = parseInt((amount * unitToSatoshi).toFixed(0)); amountSat = parseInt((amount * unitToSatoshi).toFixed(0));
amountUnitStr = root.formatAmountStr(amountSat); amountUnitStr = root.formatAmountStr(amountSat);