diff --git a/lib/utils.js b/lib/utils.js index 54320b4..1028fb5 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,5 +1,8 @@ 'use strict'; + var _ = require('lodash'); +var async = require('async'); +var common = require('./common'); function UtilsController(node) { this.node = node; @@ -10,14 +13,22 @@ UtilsController.prototype.estimateFee = function(req, res) { var args = req.query.nbBlocks || '2'; var nbBlocks = args.split(','); - var result = nbBlocks.map(function(n) { + async.map(nbBlocks, function(n, next) { var num = parseInt(n); // Insight and Bitcoin JSON-RPC return bitcoin for this value (instead of satoshis). - var fee = self.node.services.bitcoind.estimateFee(num) / 1e8; - return [num, fee]; + self.node.services.bitcoind.estimateFee(num, function(err, fee) { + if (err) { + return next(err); + } + next(null, [num, fee]); + }); + }, function(err, result) { + if (err) { + return common.handleErrors(err, res); + } + res.jsonp(_.zipObject(result)); }); - res.jsonp(_.zipObject(result)); }; module.exports = UtilsController; diff --git a/test/utils.js b/test/utils.js index 4fda011..41c84a9 100644 --- a/test/utils.js +++ b/test/utils.js @@ -10,12 +10,12 @@ describe('Utils', function() { var node = { services: { bitcoind: { - estimateFee: function(blocks) { + estimateFee: function(blocks, callback) { switch(blocks) { - case 1: - return 1000; - case 3: - return 3000; + case 1: + return callback(null, 1000 / 1e8); + case 3: + return callback(null, 3000 / 1e8); } } }