Adds three levels of dynamic fees

This commit is contained in:
Gustavo Maximiliano Cortez 2015-07-24 12:11:07 -03:00
commit 5742dee340
10 changed files with 241 additions and 110 deletions

View file

@ -1,6 +1,6 @@
'use strict';
angular.module('copayApp.controllers').controller('indexController', function($rootScope, $scope, $log, $filter, $timeout, lodash, go, profileService, configService, isCordova, rateService, storageService, addressService, gettextCatalog, gettext, amMoment, nodeWebkit, addonManager) {
angular.module('copayApp.controllers').controller('indexController', function($rootScope, $scope, $log, $filter, $timeout, lodash, go, profileService, configService, isCordova, rateService, storageService, addressService, gettextCatalog, gettext, amMoment, nodeWebkit, addonManager, feeService) {
var self = this;
self.isCordova = isCordova;
self.onGoingProcess = {};
@ -278,6 +278,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
}
$log.debug('Wallet Status:', walletStatus);
self.setPendingTxps(walletStatus.pendingTxps);
self.setFees();
// Status Shortcuts
self.walletName = walletStatus.wallet.name;
@ -305,6 +306,22 @@ angular.module('copayApp.controllers').controller('indexController', function($r
});
};
self.setCurrentFeeLevel = function(level) {
self.currentFeeLevel = level || configService.getSync().wallet.settings.feeLevel || 'priority';
};
self.setFees = function() {
var fc = profileService.focusedClient;
if (!fc) return;
$timeout(function() {
feeService.getFeeLevels(function(levels) {
self.feeLevels = levels;
self.setCurrentFeeLevel();
$rootScope.$apply();
});
});
};
self.updateBalance = function() {
var fc = profileService.focusedClient;
$timeout(function() {
@ -801,6 +818,10 @@ angular.module('copayApp.controllers').controller('indexController', function($r
});
});
$rootScope.$on('Local/FeeLevelUpdated', function(event, level) {
self.setCurrentFeeLevel(level);
});
$rootScope.$on('Local/ProfileBound', function() {
storageService.getRemotePrefsStoredFlag(function(err, val) {
if (err || val) return;

View file

@ -1,38 +0,0 @@
'use strict';
angular.module('copayApp.controllers').controller('preferencesFeeController',
function($rootScope, $scope, configService, go, gettext) {
var config = configService.getSync();
this.feeName = config.wallet.settings.feeName || 'Priority';
this.feeOpts = [{
name: gettext('Priority'),
value: 100,
}, {
name: gettext('Normal'),
value: 50,
}, {
name: gettext('Economy'),
value: 10,
}, {
name: gettext('Emergency'),
red: true,
value: 500,
}, ];
this.save = function(newFee) {
var opts = {
wallet: {
settings: {
feeName: newFee.name,
feeValue: newFee.value * 100,
}
}
};
this.feeName = newFee.name;
configService.set(opts, function(err) {
if (err) console.log(err);
});
};
});

View file

@ -1,6 +1,6 @@
'use strict';
angular.module('copayApp.controllers').controller('walletHomeController', function($scope, $rootScope, $timeout, $filter, $modal, $log, notification, txStatus, isCordova, profileService, lodash, configService, rateService, storageService, bitcore, isChromeApp, gettext, gettextCatalog, nodeWebkit, addressService) {
angular.module('copayApp.controllers').controller('walletHomeController', function($scope, $rootScope, $timeout, $filter, $modal, $log, notification, txStatus, isCordova, profileService, lodash, configService, rateService, storageService, bitcore, isChromeApp, gettext, gettextCatalog, nodeWebkit, addressService, feeService) {
var self = this;
$rootScope.hideMenuBar = false;
@ -526,7 +526,7 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi
});
};
// Send
// Send
this.canShowAlternative = function() {
return $scope.showAlternative;
@ -724,40 +724,47 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi
address = form.address.$modelValue;
amount = parseInt((form.amount.$modelValue * unitToSat).toFixed(0));
fc.sendTxProposal({
toAddress: address,
amount: amount,
message: comment,
payProUrl: paypro ? paypro.url : null,
feePerKb: config.feeValue || 10000,
}, function(err, txp) {
feeService.getCurrentFeeValue(function(err, feePerKb) {
if (err) {
self.setOngoingProcess();
profileService.lockFC();
return self.setSendError(err);
}
if (!fc.canSign()) {
$log.info('No signing proposal: No private key')
self.setOngoingProcess();
self.resetForm();
txStatus.notify(txp, function() {
return $scope.$emit('Local/TxProposalAction');
});
return;
}
self.signAndBroadcast(txp, function(err) {
self.setOngoingProcess();
profileService.lockFC();
self.resetForm();
fc.sendTxProposal({
toAddress: address,
amount: amount,
message: comment,
payProUrl: paypro ? paypro.url : null,
feePerKb: feePerKb,
}, function(err, txp) {
if (err) {
self.error = err.message ? err.message : gettext('The payment was created but could not be completed. Please try again from home screen');
$scope.$emit('Local/TxProposalAction');
$timeout(function() {
$scope.$digest();
}, 1);
self.setOngoingProcess();
profileService.lockFC();
return self.setSendError(err);
}
if (!fc.canSign()) {
$log.info('No signing proposal: No private key')
self.setOngoingProcess();
self.resetForm();
txStatus.notify(txp, function() {
return $scope.$emit('Local/TxProposalAction');
});
return;
}
self.signAndBroadcast(txp, function(err) {
self.setOngoingProcess();
profileService.lockFC();
self.resetForm();
if (err) {
self.error = err.message ? err.message : gettext('The payment was created but could not be completed. Please try again from home screen');
$scope.$emit('Local/TxProposalAction');
$timeout(function() {
$scope.$digest();
}, 1);
}
});
});
});
}, 100);
@ -970,6 +977,47 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi
}
};
this.openFeeModal = function(feeLevels, currentFeeLevel) {
var fc = profileService.focusedClient;
var ModalInstanceCtrl = function($scope, $modalInstance) {
$scope.feeLevels = feeLevels;
$scope.currentFeeLevel = currentFeeLevel
$scope.network = fc.credentials.network;
$scope.color = fc.backgroundColor;
$scope.save = function(level) {
var opts = {
wallet: {
settings: {
feeLevel: level
}
}
};
$scope.currentFeeLevel = level;
$rootScope.$emit('Local/FeeLevelUpdated', level);
configService.set(opts, function(err) {
if (err) $log.debug(err);
});
};
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
};
var modalInstance = $modal.open({
templateUrl: 'views/modals/fee.html',
windowClass: 'full animated slideInUp',
controller: ModalInstanceCtrl
});
modalInstance.result.finally(function() {
var m = angular.element(document.getElementsByClassName('reveal-modal'));
m.addClass('slideOutDown');
});
};
// History

View file

@ -252,17 +252,6 @@ angular
},
}
})
.state('preferencesFee', {
url: '/preferencesFee',
templateUrl: 'views/preferencesFee.html',
walletShouldBeComplete: true,
needProfile: true,
views: {
'main': {
templateUrl: 'views/preferencesFee.html'
},
}
})
.state('preferencesAdvanced', {
url: '/preferencesAdvanced',
@ -469,7 +458,6 @@ angular
delete: 13,
preferencesLanguage: 12,
preferencesUnit: 12,
preferencesFee: 12,
preferencesAltCurrency: 12,
preferencesBwsUrl: 12,
preferencesAlias: 12,

View file

@ -0,0 +1,88 @@
'use strict';
angular.module('copayApp.services').factory('feeService', function($log, lodash, profileService, configService, gettext) {
var root = {};
root.feeStaticOpts = [{
name: gettext('Priority'),
level: 'priority',
feePerKB: 10000,
nbBlocks: 1
}, {
name: gettext('Normal'),
level: 'normal',
feePerKB: 5000,
nbBlocks: 4
}, {
name: gettext('Economy'),
level: 'economy',
feePerKB: 1000,
nbBlocks: 12
}];
root.getCurrentFeeValue = function(cb) {
var fc = profileService.focusedClient;
var config = configService.getSync().wallet.settings;
var feeLevel = config.feeLevel || 'priority';
// static fee
var fee = 10000;
fc.getFeeLevels(fc.credentials.network, function(err, levels) {
if (err) {
return cb({message: 'Error getting dynamic fee'})
}
else {
for (var i = 0; i < 3; i++) {
if (levels[i].level == feeLevel) {
fee = levels[i].feePerKB;
}
}
$log.debug('Dynamic fee for ' + feeLevel + ': ' + fee);
return cb(null, fee);
}
});
};
var checkCompatibility = function(config) {
if (config.feeName && !config.feeLevel) {
// Migrate to new dynamic fee values
var level = config.feeName.toLowerCase();
if (level == 'emergency') level = 'priority';
var opts = {
wallet: {
settings: {
feeLevel: level
}
}
};
configService.set(opts, function(err) {
if (err) $log.debug(err);
});
}
};
root.getFeeLevels = function(cb) {
var fc = profileService.focusedClient;
var config = configService.getSync().wallet.settings;
var unitName = config.unitName;
checkCompatibility(config);
fc.getFeeLevels('livenet', function(errLivenet, levelsLivenet) {
fc.getFeeLevels('testnet', function(errTestnet, levelsTestnet) {
if (errLivenet || errTestnet) $log.error('Error getting dynamic fee');
for (var i = 0; i < 3; i++) {
levelsLivenet[i]['feePerKBUnit'] = profileService.formatAmount(levelsLivenet[i].feePerKB) + ' ' + unitName;
levelsTestnet[i]['feePerKBUnit'] = profileService.formatAmount(levelsTestnet[i].feePerKB) + ' ' + unitName;
}
return cb({
'livenet': levelsLivenet,
'testnet': levelsTestnet
});
});
});
};
return root;
});