diff --git a/js/controllers/send.js b/js/controllers/send.js index 53e742f1f..67871c33f 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; @@ -87,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 @@ -425,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; @@ -447,7 +445,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 * satToUnit).toFixed(w.settings.unitDecimals)); } }); }; @@ -465,6 +463,8 @@ angular.module('copayApp.controllers').controller('SendController', return newUri; }; + var w = $rootScope.wallet; + var satToUnit = 1 / w.settings.unitToSatoshi; var form = $scope.sendForm; uri = sanitizeUri(uri); @@ -481,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) * satToUnit : 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 abf95dada..ca9436c26 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,32 @@ 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; + 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; + })); }); describe("Unit: Version Controller", function() {