Fix variables and toFixed

This commit is contained in:
Gustavo Maximiliano Cortez 2015-01-30 12:25:54 -03:00
parent 6586a4b696
commit 17057cb199
2 changed files with 11 additions and 3 deletions

View File

@ -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;

View File

@ -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;
}));
});