Skip to content

Commit

Permalink
Merge pull request #346 from isocolsky/estimate_multi_fee
Browse files Browse the repository at this point in the history
Get multiple values for estimatefee on single request
  • Loading branch information
matiu committed Aug 13, 2015
2 parents 74c27b2 + c1210ce commit 483dde1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
23 changes: 10 additions & 13 deletions app/controllers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,18 @@
* Module dependencies.
*/

var Utils = require('../models/Utils'),
common = require('./common');
var _ = require('lodash');

var Utils = require('../models/Utils');
var common = require('./common');

exports.estimateFee = function(req, res) {
var args = req.query.nbBlocks || '2';
var nbBlocks = args.split(',');

var nbBlocks = +req.query.nbBlocks || 2;
var utilsObject = new Utils();

var returnJsonp = function(err) {
if (err || !utilsObject)
return common.handleErrors(err, res);
else {
res.jsonp(utilsObject);
}
};

utilsObject.estimateFee(nbBlocks, returnJsonp);
utilsObject.estimateFee(nbBlocks, function(err, fees) {
if (err) return common.handleErrors(err, res);
res.jsonp(fees);
});
};
16 changes: 10 additions & 6 deletions app/models/Utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';
//var imports = require('soop').imports();
var _ = require('lodash');
var async = require('async');

var bitcore = require('bitcore');
var RpcClient = bitcore.RpcClient;
Expand All @@ -8,14 +10,16 @@ var rpc = new RpcClient(config.bitcoind);

function Utils() {}

Utils.prototype.estimateFee = function(n, next) {
Utils.prototype.estimateFee = function(nbBlocks, cb) {
var that = this;

rpc.estimateFee(n, function(err, info) {
if (err) return next(err);

that.feePerKB = info.result;
return next();
async.map([].concat(nbBlocks), function(n, next) {
rpc.estimateFee(+n, function(err, info) {
return next(err, [n, info.result]);
});
}, function(err, result) {
if (err) return cb(err);
return cb(null, _.zipObject(result));
});
};

Expand Down

0 comments on commit 483dde1

Please sign in to comment.