Wallet/src/js/services/feeService.js

65 lines
2 KiB
JavaScript
Raw Normal View History

2015-07-24 12:11:07 -03:00
'use strict';
2016-11-25 10:02:56 -03:00
angular.module('copayApp.services').factory('feeService', function($log, $stateParams, bwcService, walletService, configService, gettext, lodash, txFormatService, gettextCatalog) {
2015-07-24 12:11:07 -03:00
var root = {};
// Constant fee options to translate
2015-07-30 15:26:16 -03:00
root.feeOpts = {
2016-06-09 13:41:54 -03:00
priority: gettext('Priority'),
normal: gettext('Normal'),
economy: gettext('Economy'),
superEconomy: gettext('Super Economy')
2015-07-30 15:26:16 -03:00
};
2016-02-22 19:56:53 -03:00
root.getCurrentFeeLevel = function() {
return configService.getSync().wallet.settings.feeLevel || 'normal';
};
2016-11-03 18:27:59 -03:00
root.getCurrentFeeValue = function(network, cb) {
network = network || 'livenet';
2016-02-22 19:56:53 -03:00
var feeLevel = root.getCurrentFeeLevel();
2016-03-04 10:44:13 -03:00
2016-11-03 18:27:59 -03:00
root.getFeeLevels(function(err, levels) {
if (err) return cb(err);
2016-03-04 10:44:13 -03:00
2016-11-03 18:27:59 -03:00
var feeLevelValue = lodash.find(levels[network], {
level: feeLevel
});
2016-11-03 18:27:59 -03:00
if (!feeLevelValue || !feeLevelValue.feePerKB)
return cb({
2016-11-03 18:27:59 -03:00
message: gettextCatalog.getString('Could not get dynamic fee for level: ') + feeLevel
});
2016-03-04 10:44:13 -03:00
2016-03-04 18:36:40 -03:00
var fee = feeLevelValue.feePerKB;
$log.debug('Dynamic fee: ' + feeLevel + ' ' + fee + ' SAT');
2016-11-03 18:27:59 -03:00
return cb(null, fee);
2015-07-24 12:11:07 -03:00
});
};
root.getFeeLevels = function(cb) {
var walletClient = bwcService.getClient();
2016-02-22 19:56:53 -03:00
var unitName = configService.getSync().wallet.settings.unitName;
2015-07-24 12:11:07 -03:00
walletClient.getFeeLevels('livenet', function(errLivenet, levelsLivenet) {
walletClient.getFeeLevels('testnet', function(errTestnet, levelsTestnet) {
2016-11-03 18:27:59 -03:00
if (errLivenet || errTestnet) {
return cb(gettextCatalog.getString('Could not get dynamic fee'));
} else {
2016-06-01 15:01:02 -03:00
for (var i = 0; i < 4; i++) {
2016-08-18 11:45:30 -03:00
levelsLivenet[i]['feePerKBUnit'] = txFormatService.formatAmount(levelsLivenet[i].feePerKB) + ' ' + unitName;
levelsTestnet[i]['feePerKBUnit'] = txFormatService.formatAmount(levelsTestnet[i].feePerKB) + ' ' + unitName;
2015-07-27 12:33:37 -03:00
}
2015-07-24 12:23:30 -03:00
}
2015-07-24 12:11:07 -03:00
2016-11-03 18:27:59 -03:00
return cb(null, {
2015-07-24 12:23:30 -03:00
'livenet': levelsLivenet,
'testnet': levelsTestnet
});
2015-07-24 12:11:07 -03:00
});
});
};
return root;
});