From 2ca3a48884edf6de74337185cb9e257bd0e1ca03 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Tue, 28 Jul 2015 16:03:55 -0400 Subject: [PATCH] Expose estimateFee method. --- integration/regtest.js | 7 +++++++ lib/daemon.js | 4 ++++ src/bitcoindjs.cc | 29 +++++++++++++++++++++++++++++ src/bitcoindjs.h | 1 + 4 files changed, 41 insertions(+) diff --git a/integration/regtest.js b/integration/regtest.js index a4f3f775..d96f2228 100644 --- a/integration/regtest.js +++ b/integration/regtest.js @@ -232,6 +232,13 @@ describe('Daemon Binding Functionality', function() { }); + describe('fee estimation', function() { + it('will estimate fees', function() { + var fees = bitcoind.estimateFee(); + fees.should.equal(-1); + }); + }); + describe('tip updates', function() { it('will get an event when the tip is new', function(done) { this.timeout(4000); diff --git a/lib/daemon.js b/lib/daemon.js index 87a3bd07..26b6dd19 100644 --- a/lib/daemon.js +++ b/lib/daemon.js @@ -296,6 +296,10 @@ Daemon.prototype.getBlockIndex = function(blockHash) { return bitcoindjs.getBlockIndex(blockHash); }; +Daemon.prototype.estimateFee = function(blocks) { + return bitcoindjs.estimateFee(blocks); +}; + Daemon.prototype.sendTransaction = function(transaction, allowAbsurdFees) { return bitcoindjs.sendTransaction(transaction, allowAbsurdFees); }; diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index 91ce4bf3..888835ee 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -1121,6 +1121,33 @@ NAN_METHOD(GetInfo) { NanReturnValue(obj); } +/** + * Estimate Fee + * @blocks {number} - The number of blocks until confirmed + */ + +NAN_METHOD(EstimateFee) { + Isolate* isolate = Isolate::GetCurrent(); + HandleScope scope(isolate); + + int nBlocks = args[0]->NumberValue(); + if (nBlocks < 1) { + nBlocks = 1; + } + + CFeeRate feeRate = mempool.estimateFee(nBlocks); + + if (feeRate == CFeeRate(0)) { + NanReturnValue(NanNew(-1.0)); + return; + } + + CAmount nFee = feeRate.GetFeePerK(); + + NanReturnValue(NanNew(nFee)); + +} + /** * Send Transaction * bitcoindjs.sendTransaction() @@ -1319,6 +1346,8 @@ init(Handle target) { NODE_SET_METHOD(target, "addMempoolUncheckedTransaction", AddMempoolUncheckedTransaction); NODE_SET_METHOD(target, "verifyScript", VerifyScript); NODE_SET_METHOD(target, "sendTransaction", SendTransaction); + NODE_SET_METHOD(target, "estimateFee", EstimateFee); + } NODE_MODULE(bitcoindjs, init) diff --git a/src/bitcoindjs.h b/src/bitcoindjs.h index fdb75372..a05dca82 100644 --- a/src/bitcoindjs.h +++ b/src/bitcoindjs.h @@ -29,3 +29,4 @@ NAN_METHOD(GetMempoolOutputs); NAN_METHOD(AddMempoolUncheckedTransaction); NAN_METHOD(VerifyScript); NAN_METHOD(SendTransaction); +NAN_METHOD(EstimateFee);