diff --git a/README.md b/README.md index ea04cac33..046f5531d 100644 --- a/README.md +++ b/README.md @@ -371,6 +371,13 @@ Where "xxx" can be: * getBestBlockHash * getLastBlockHash + +### Utility methods +``` + /api/utils/estimatefee[?nbBlocks=2] +``` + + ## Web Socket API The web socket API is served using [socket.io](http://socket.io). diff --git a/app/controllers/utils.js b/app/controllers/utils.js new file mode 100644 index 000000000..b7f0ce769 --- /dev/null +++ b/app/controllers/utils.js @@ -0,0 +1,24 @@ +'use strict'; + +/** + * Module dependencies. + */ + +var Utils = require('../models/Utils'), + common = require('./common'); + +exports.estimateFee = function(req, res) { + + 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); +}; diff --git a/app/models/Utils.js b/app/models/Utils.js new file mode 100644 index 000000000..ab51deea5 --- /dev/null +++ b/app/models/Utils.js @@ -0,0 +1,22 @@ +'use strict'; +//var imports = require('soop').imports(); + +var bitcore = require('bitcore'); +var RpcClient = bitcore.RpcClient; +var config = require('../../config/config'); +var rpc = new RpcClient(config.bitcoind); + +function Utils() {} + +Utils.prototype.estimateFee = function(n, next) { + var that = this; + + rpc.estimateFee(n, function(err, info) { + if (err) return next(err); + + that.feePerKB = info.result; + return next(); + }); +}; + +module.exports = require('soop')(Utils); diff --git a/config/routes.js b/config/routes.js index d4ba9521f..fbb896cc8 100644 --- a/config/routes.js +++ b/config/routes.js @@ -53,6 +53,10 @@ module.exports = function(app) { app.get(apiPrefix + '/sync', st.sync); app.get(apiPrefix + '/peer', st.peer); + // Utils route + var utils = require('../app/controllers/utils'); + app.get(apiPrefix + '/utils/estimatefee', utils.estimateFee); + // Currency var currency = require('../app/controllers/currency'); app.get(apiPrefix + '/currency', currency.index); diff --git a/package.json b/package.json index fff7f526d..b51a0e2e7 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,7 @@ "base58-native": "0.1.2", "bignum": "*", "morgan": "*", - "bitcore": "git://github.com/bitpay/bitcore.git#aa41c70cff2583d810664c073a324376c39c8b36", + "bitcore": "git://github.com/bitpay/bitcore.git#51c53b16ced6bcaf4b78329955d6814579fe4ee9", "bufferput": "git://github.com/bitpay/node-bufferput.git", "buffertools": "*", "commander": "^2.3.0",