From 17057cb199b24b4d1dfb464fd020cf4041c7b814 Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Fri, 30 Jan 2015 12:25:54 -0300 Subject: [PATCH] Fix variables and toFixed --- js/controllers/send.js | 9 ++++++--- test/unit/controllers/controllersSpec.js | 5 +++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/js/controllers/send.js b/js/controllers/send.js index 98887129f..67871c33f 100644 --- a/js/controllers/send.js +++ b/js/controllers/send.js @@ -83,6 +83,7 @@ angular.module('copayApp.controllers').controller('SendController', $scope.setInputs = function() { var w = $rootScope.wallet; var unitToSat = w.settings.unitToSatoshi; + var satToUnit = 1 / unitToSat; /** * Setting the two related amounts as properties prevents an infinite * recursion for watches while preserving the original angular updates @@ -97,7 +98,7 @@ angular.module('copayApp.controllers').controller('SendController', this.__alternative = newValue; if (typeof(newValue) === 'number' && $scope.isRateAvailable) { this._amount = parseFloat( - (rateService.fromFiat(newValue, $scope.alternativeIsoCode) * 1 / w.settings.unitToSatoshi).toFixed(w.settings.unitDecimals), 10); + (rateService.fromFiat(newValue, $scope.alternativeIsoCode) * satToUnit).toFixed(w.settings.unitDecimals), 10); } else { this._amount = 0; } @@ -421,6 +422,7 @@ angular.module('copayApp.controllers').controller('SendController', } var w = $rootScope.wallet; + var satToUnit = 1 / w.settings.unitToSatoshi; $scope.fetchingURL = uri; $scope.loading = true; @@ -443,7 +445,7 @@ angular.module('copayApp.controllers').controller('SendController', } else { $scope._merchantData = merchantData; $scope._domain = merchantData.domain; - $scope.setForm(null, merchantData.total * 1 / w.settings.unitToSatoshi); + $scope.setForm(null, (merchantData.total * satToUnit).toFixed(w.settings.unitDecimals)); } }); }; @@ -462,6 +464,7 @@ angular.module('copayApp.controllers').controller('SendController', }; var w = $rootScope.wallet; + var satToUnit = 1 / w.settings.unitToSatoshi; var form = $scope.sendForm; uri = sanitizeUri(uri); @@ -478,7 +481,7 @@ angular.module('copayApp.controllers').controller('SendController', return $scope.setFromPayPro(parsed.data.merchant); var amount = (parsed.data && parsed.data.amount) ? - (parsed.data.amount * 100000000).toFixed(0) * 1 / w.settings.unitToSatoshi: 0; + ((parsed.data.amount * 100000000).toFixed(0) * satToUnit).toFixed(w.settings.unitDecimals): 0; $scope.setForm(addr, amount, parsed.data.message, true); return addr; diff --git a/test/unit/controllers/controllersSpec.js b/test/unit/controllers/controllersSpec.js index b002832fe..ca9436c26 100644 --- a/test/unit/controllers/controllersSpec.js +++ b/test/unit/controllers/controllersSpec.js @@ -402,14 +402,19 @@ describe("Unit: Controllers", function() { it('receive from uri using BTC', inject(function($rootScope) { var old = $rootScope.wallet.settings.unitToSatoshi; + var old_decimals = $rootScope.wallet.settings.unitDecimals; $rootScope.wallet.settings.unitToSatoshi = 100000000; + $rootScope.wallet.settings.unitDecimals = 8; sendForm.address.$setViewValue('bitcoin:mxf5psDyA8EQVzb2MZ7MkDWiXuAuWWCRMB?amount=1.018085'); expect(sendForm.amount.$modelValue).to.equal(1.018085); sendForm.address.$setViewValue('bitcoin:mxf5psDyA8EQVzb2MZ7MkDWiXuAuWWCRMB?amount=1.01808500'); expect(sendForm.amount.$modelValue).to.equal(1.018085); sendForm.address.$setViewValue('bitcoin:mxf5psDyA8EQVzb2MZ7MkDWiXuAuWWCRMB?amount=0.29133585'); expect(sendForm.amount.$modelValue).to.equal(0.29133585); + sendForm.address.$setViewValue('bitcoin:mxf5psDyA8EQVzb2MZ7MkDWiXuAuWWCRMB?amount=0.1'); + expect(sendForm.amount.$modelValue).to.equal(0.1); $rootScope.wallet.settings.unitToSatoshi = old; + $rootScope.wallet.settings.unitDecimals = old_decimals; })); });