Browse Source

Merge pull request #323 from isocolsky/ref/fee_levels

Refactor fee level sampling
activeAddress
Matias Alejo Garcia 10 years ago
parent
commit
a31617446f
  1. 2
      lib/blockchainexplorers/insight.js
  2. 25
      lib/server.js
  3. 7
      test/integration/server.js

2
lib/blockchainexplorers/insight.js

@ -108,7 +108,7 @@ Insight.prototype.getAddressActivity = function(addresses, cb) {
Insight.prototype.estimateFee = function(nbBlocks, cb) { Insight.prototype.estimateFee = function(nbBlocks, cb) {
var url = this.url + '/api/utils/estimatefee'; var url = this.url + '/api/utils/estimatefee';
if (nbBlocks) { if (nbBlocks) {
url += '?nbBlocks=' + nbBlocks; url += '?nbBlocks=' + [].concat(nbBlocks).join(',');
} }
var args = { var args = {

25
lib/server.js

@ -752,23 +752,22 @@ WalletService.prototype.getBalance = function(opts, cb) {
WalletService.prototype._sampleFeeLevels = function(network, points, cb) { WalletService.prototype._sampleFeeLevels = function(network, points, cb) {
var self = this; var self = this;
// TODO: cache blockexplorer data
var bc = self._getBlockchainExplorer(network); var bc = self._getBlockchainExplorer(network);
async.map(points, function(p, next) { bc.estimateFee(points, function(err, result) {
bc.estimateFee(p, function(err, result) { if (err) {
if (err) { log.error('Error estimating fee', err);
log.error('Error estimating fee', err); return cb(err);
return next(err); }
}
var feePerKB = _.isObject(result) ? +(result.feePerKB) : -1; var levels = _.zipObject(_.map(points, function(p) {
var feePerKB = _.isObject(result) ? +result[p] : -1;
if (feePerKB < 0) { if (feePerKB < 0) {
log.warn('Could not compute fee estimation (nbBlocks=' + p + ')'); log.warn('Could not compute fee estimation (nbBlocks=' + p + ')');
} }
return next(null, [p, Utils.strip(feePerKB * 1e8)]); return [p, Utils.strip(feePerKB * 1e8)];
}); }));
}, function(err, results) {
if (err) return cb(err); return cb(null, levels);
return cb(null, _.zipObject(results));
}); });
}; };

7
test/integration/server.js

@ -196,9 +196,10 @@ helpers.stubHistory = function(txs) {
helpers.stubFeeLevels = function(levels) { helpers.stubFeeLevels = function(levels) {
blockchainExplorer.estimateFee = function(nbBlocks, cb) { blockchainExplorer.estimateFee = function(nbBlocks, cb) {
return cb(null, { var result = _.zipObject(_.map(_.pick(levels, nbBlocks), function(fee, n) {
feePerKB: levels[nbBlocks] / 1e8 return [+n, fee > 0 ? fee / 1e8 : fee];
}); }));
return cb(null, result);
}; };
}; };

Loading…
Cancel
Save