From cf8670c64f7248c67d08ad3407e2f2218e7358df Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Thu, 29 Jan 2015 17:48:28 -0300 Subject: [PATCH 1/3] Fix rounding error in amount field --- js/controllers/send.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/js/controllers/send.js b/js/controllers/send.js index 53e742f1f..7ebb4147e 100644 --- a/js/controllers/send.js +++ b/js/controllers/send.js @@ -447,7 +447,7 @@ angular.module('copayApp.controllers').controller('SendController', } else { $scope._merchantData = merchantData; $scope._domain = merchantData.domain; - $scope.setForm(null, merchantData.total * satToUnit); + $scope.setForm(null, merchantData.total * 1 / w.settings.unitToSatoshi); } }); }; @@ -465,6 +465,7 @@ angular.module('copayApp.controllers').controller('SendController', return newUri; }; + var w = $rootScope.wallet; var form = $scope.sendForm; uri = sanitizeUri(uri); @@ -481,7 +482,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) * satToUnit : 0; + (parsed.data.amount * 100000000).toFixed(0) * 1 / w.settings.unitToSatoshi: 0; $scope.setForm(addr, amount, parsed.data.message, true); return addr; From 6586a4b69650b4d381a944509a90b8bf4e29aba7 Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Fri, 30 Jan 2015 11:09:07 -0300 Subject: [PATCH 2/3] Remove unnused variable. Added tests to check amount validation --- js/controllers/send.js | 6 +----- test/unit/controllers/controllersSpec.js | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/js/controllers/send.js b/js/controllers/send.js index 7ebb4147e..98887129f 100644 --- a/js/controllers/send.js +++ b/js/controllers/send.js @@ -5,8 +5,6 @@ var preconditions = require('preconditions').singleton(); angular.module('copayApp.controllers').controller('SendController', function($scope, $rootScope, $window, $timeout, $modal, $filter, notification, isMobile, rateService, txStatus, isCordova) { - var satToUnit; - $scope.init = function() { var w = $rootScope.wallet; preconditions.checkState(w); @@ -23,8 +21,6 @@ angular.module('copayApp.controllers').controller('SendController', $scope.loading = false; $scope.error = $scope.success = null; - satToUnit = 1 / w.settings.unitToSatoshi; - $scope.alternativeName = w.settings.alternativeName; $scope.alternativeIsoCode = w.settings.alternativeIsoCode; @@ -101,7 +97,7 @@ angular.module('copayApp.controllers').controller('SendController', this.__alternative = newValue; if (typeof(newValue) === 'number' && $scope.isRateAvailable) { this._amount = parseFloat( - (rateService.fromFiat(newValue, $scope.alternativeIsoCode) * satToUnit).toFixed(w.settings.unitDecimals), 10); + (rateService.fromFiat(newValue, $scope.alternativeIsoCode) * 1 / w.settings.unitToSatoshi).toFixed(w.settings.unitDecimals), 10); } else { this._amount = 0; } diff --git a/test/unit/controllers/controllersSpec.js b/test/unit/controllers/controllersSpec.js index abf95dada..b002832fe 100644 --- a/test/unit/controllers/controllersSpec.js +++ b/test/unit/controllers/controllersSpec.js @@ -273,7 +273,7 @@ describe("Unit: Controllers", function() { var element2 = angular.element( '
' + '' + - '' + + '' + '' + '' + '
' @@ -390,6 +390,27 @@ describe("Unit: Controllers", function() { done(); }); }); + + it('receive from uri using bits', inject(function() { + sendForm.address.$setViewValue('bitcoin:mxf5psDyA8EQVzb2MZ7MkDWiXuAuWWCRMB?amount=1.018085'); + expect(sendForm.amount.$modelValue).to.equal(1018085); + sendForm.address.$setViewValue('bitcoin:mxf5psDyA8EQVzb2MZ7MkDWiXuAuWWCRMB?amount=1.01808500'); + expect(sendForm.amount.$modelValue).to.equal(1018085); + sendForm.address.$setViewValue('bitcoin:mxf5psDyA8EQVzb2MZ7MkDWiXuAuWWCRMB?amount=0.29133585'); + expect(sendForm.amount.$modelValue).to.equal(291335.85); + })); + + it('receive from uri using BTC', inject(function($rootScope) { + var old = $rootScope.wallet.settings.unitToSatoshi; + $rootScope.wallet.settings.unitToSatoshi = 100000000; + 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); + $rootScope.wallet.settings.unitToSatoshi = old; + })); }); describe("Unit: Version Controller", function() { From 17057cb199b24b4d1dfb464fd020cf4041c7b814 Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Fri, 30 Jan 2015 12:25:54 -0300 Subject: [PATCH 3/3] 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; })); });