Wallet/src/js/controllers/preferencesFee.js

114 lines
3.2 KiB
JavaScript
Raw Normal View History

'use strict';
angular.module('copayApp.controllers').controller('preferencesFeeController', function($scope, $timeout, $ionicHistory, lodash, gettextCatalog, configService, feeService, ongoingProcess, popupService) {
2016-06-10 18:41:52 -03:00
$scope.save = function(newFee) {
2017-07-07 10:26:31 -03:00
2017-06-21 17:09:33 -03:00
$scope.currentFeeLevel = newFee;
2017-07-07 10:26:31 -03:00
if ($scope.currentFeeLevel != 'custom') updateCurrentValues();
else showCustomFeePrompt();
if ($scope.noSave) return;
2016-06-10 18:41:52 -03:00
var opts = {
wallet: {
settings: {
2017-01-10 10:23:01 -03:00
feeLevel: newFee
2016-06-10 18:41:52 -03:00
}
}
};
2016-06-10 18:41:52 -03:00
configService.set(opts, function(err) {
if (err) $log.debug(err);
$timeout(function() {
$scope.$apply();
});
2016-06-10 18:41:52 -03:00
});
};
2017-01-10 10:23:01 -03:00
$scope.$on("$ionicView.beforeEnter", function(event, data) {
$scope.init();
});
$scope.init = function() {
2017-06-21 17:09:33 -03:00
$scope.network = $scope.network || 'livenet';
$scope.feeOpts = feeService.feeOpts;
2017-06-21 17:09:33 -03:00
$scope.currentFeeLevel = $scope.feeLevel || feeService.getCurrentFeeLevel();
2017-01-10 10:23:01 -03:00
$scope.loadingFee = true;
feeService.getFeeLevels(function(err, levels) {
$scope.loadingFee = false;
if (err) {
2017-01-16 16:00:09 -03:00
//Error is already formatted
popupService.showAlert(err);
2017-01-10 10:23:01 -03:00
return;
}
$scope.feeLevels = levels;
updateCurrentValues();
2017-06-23 10:24:14 -03:00
$timeout(function() {
$scope.$apply();
});
2017-01-10 10:23:01 -03:00
});
};
2017-01-10 10:23:01 -03:00
var updateCurrentValues = function() {
2017-01-10 10:31:53 -03:00
if (lodash.isEmpty($scope.feeLevels) || lodash.isEmpty($scope.currentFeeLevel)) return;
2017-06-21 17:09:33 -03:00
var value = lodash.find($scope.feeLevels[$scope.network], {
2017-01-10 10:23:01 -03:00
level: $scope.currentFeeLevel
});
2017-06-21 17:09:33 -03:00
if (lodash.isEmpty(value)) {
2017-07-07 10:26:31 -03:00
$scope.feePerSatByte = $scope.currentFeeLevel == 'custom' ? $scope.feePerSatByte : null;
$scope.avgConfirmationTime = null;
2017-07-07 10:26:31 -03:00
setMinWarning();
setMaxWarning();
2017-03-06 16:38:16 -03:00
return;
}
2017-06-21 17:09:33 -03:00
$scope.feePerSatByte = (value.feePerKB / 1000).toFixed();
$scope.avgConfirmationTime = value.nbBlocks * 10;
2017-07-07 10:26:31 -03:00
$scope.invalidCustomFeeEntered = false;
setMinWarning();
setMaxWarning();
2017-01-10 10:23:01 -03:00
};
$scope.chooseNewFee = function() {
2017-07-07 10:26:31 -03:00
$scope.hideModal($scope.currentFeeLevel, $scope.customFeePerKB);
};
var showCustomFeePrompt = function() {
$scope.invalidCustomFeeEntered = true;
$scope.showMaxWarning = false;
popupService.showPrompt(null, gettextCatalog.getString('Custom Fee'), null, function(text) {
if (!text || !parseInt(text)) return;
$scope.feePerSatByte = parseInt(text);
$scope.customFeePerKB = ($scope.feePerSatByte * 1000).toFixed();
setMaxWarning();
setMinWarning();
});
};
$scope.getMinimumRecommeded = function() {
var value = lodash.find($scope.feeLevels[$scope.network], {
level: 'superEconomy'
});
return parseInt((value.feePerKB / 1000).toFixed());
};
var setMinWarning = function() {
if (parseInt($scope.feePerSatByte) < $scope.getMinimumRecommeded()) $scope.showMinWarning = true;
else $scope.showMinWarning = false;
};
2017-07-07 10:26:31 -03:00
var setMaxWarning = function() {
if (parseInt($scope.feePerSatByte) > 1000) {
$scope.showMaxWarning = true;
$scope.invalidCustomFeeEntered = true;
} else {
$scope.showMaxWarning = false;
$scope.invalidCustomFeeEntered = false;
}
};
2016-06-10 18:41:52 -03:00
});