diff --git a/app/controllers/utils.js b/app/controllers/utils.js index b7f0ce769..63ca715db 100644 --- a/app/controllers/utils.js +++ b/app/controllers/utils.js @@ -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); + }); }; diff --git a/app/models/Utils.js b/app/models/Utils.js index ab51deea5..9f9e88467 100644 --- a/app/models/Utils.js +++ b/app/models/Utils.js @@ -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; @@ -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)); }); };