Merge pull request #2368 from cmgustavo/bug/amount-tosatoshi-01

WIP: Fix rounding error in amount field
This commit is contained in:
Matias Alejo Garcia 2015-01-30 12:44:10 -03:00
commit 6feb67fe10
2 changed files with 33 additions and 7 deletions

View File

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

View File

@ -273,7 +273,7 @@ describe("Unit: Controllers", function() {
var element2 = angular.element(
'<form name="form2">' +
'<input type="text" id="address" name="address" ng-model="_address" valid-address required>' +
'<input type="number" id="amount" name="amount" ng-model="_amount" min="1" max="10000000000" required>' +
'<input type="number" id="amount" name="amount" ng-model="_amount" min="0.00000001" max="10000000000" valid-amount required>' +
'<input type="number" id="alternative" name="alternative" ng-model="_alternative">' +
'<textarea id="comment" name="comment" ng-model="commentText" ng-maxlength="100"></textarea>' +
'</form>'
@ -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() {