Wallet/src/js/services/feeService.js

85 lines
2.3 KiB
JavaScript
Raw Normal View History

2015-07-24 12:11:07 -03:00
'use strict';
2017-06-20 12:14:21 -03:00
angular.module('copayApp.services').factory('feeService', function($log, $timeout, $stateParams, bwcService, walletService, configService, gettext, lodash, txFormatService, gettextCatalog) {
2015-07-24 12:11:07 -03:00
var root = {};
2017-06-20 12:14:21 -03:00
var CACHE_TIME_TS = 60; // 1 min
// Constant fee options to translate
2015-07-30 15:26:16 -03:00
root.feeOpts = {
2017-03-06 16:38:16 -03:00
urgent: gettext('Urgent'),
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
};
2017-06-20 12:14:21 -03:00
var cache = {
updateTs: 0,
};
2016-02-22 19:56:53 -03:00
root.getCurrentFeeLevel = function() {
return configService.getSync().wallet.settings.feeLevel || 'normal';
};
2017-06-20 12:14:21 -03:00
root.getFeeRate = function(network, feeLevel, cb) {
2016-11-03 18:27:59 -03:00
network = network || 'livenet';
2016-03-04 10:44:13 -03:00
2017-06-21 13:03:48 -03:00
root.getFeeLevels(function(err, levels, fromCache) {
2016-11-03 18:27:59 -03:00
if (err) return cb(err);
2016-03-04 10:44:13 -03:00
2017-06-20 12:14:21 -03:00
var feeLevelRate = lodash.find(levels[network], {
level: feeLevel
});
2016-11-03 18:27:59 -03:00
2017-06-20 12:14:21 -03:00
if (!feeLevelRate || !feeLevelRate.feePerKB) {
return cb({
2016-11-29 14:28:43 -03:00
message: gettextCatalog.getString("Could not get dynamic fee for level: {{feeLevel}}", {
feeLevel: feeLevel
})
});
2016-11-29 14:28:43 -03:00
}
2016-03-04 10:44:13 -03:00
2017-06-20 12:14:21 -03:00
var feeRate = feeLevelRate.feePerKB;
2017-06-21 13:03:48 -03:00
if (!fromCache) $log.debug('Dynamic fee: ' + feeLevel + '/' + network +' ' + (feeLevelRate.feePerKB / 1000).toFixed() + ' SAT/B');
2016-11-03 18:27:59 -03:00
2017-06-20 12:14:21 -03:00
return cb(null, feeRate);
2015-07-24 12:11:07 -03:00
});
};
2017-06-20 12:14:21 -03:00
root.getCurrentFeeRate = function(network, cb) {
return root.getFeeRate(network, root.getCurrentFeeLevel(), cb);
};
root.getFeeLevels = function(cb) {
2017-06-20 12:14:21 -03:00
if (cache.updateTs > Date.now() - CACHE_TIME_TS * 1000 ) {
$timeout( function() {
2017-06-21 13:03:48 -03:00
return cb(null, cache.data, true);
2017-06-20 12:14:21 -03:00
}, 1);
}
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'));
2015-07-24 12:23:30 -03:00
}
2017-06-20 12:14:21 -03:00
cache.updateTs =Date.now();
cache.data = {
2015-07-24 12:23:30 -03:00
'livenet': levelsLivenet,
'testnet': levelsTestnet
2017-06-20 12:14:21 -03:00
};
return cb(null, cache.data);
2015-07-24 12:11:07 -03:00
});
});
};
return root;
});