Skip to content

Commit

Permalink
Merge pull request #889 from pinheadmz/clientfee1
Browse files Browse the repository at this point in the history
bcoin-cli: expose estimateFee
  • Loading branch information
braydonf committed Nov 22, 2019
2 parents 3bb6691 + a139b62 commit d5eaf37
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 12 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ take multiple hours (e.g. 8 hours) depending on hardware and the
index. Please take the potential downtime in re-indexing into account
before upgrading.


### Client API changes

- Client module `/lib/client` was created from the external dependency `bclient`.
- `NodeClient` method `estimateFee` now returns JSON instead of a number.
- Added `fee` command to `bcoin-cli`.
- Added `getBlockHeader` to `NodeClient` and corresponding `header` command to CLI.
- Added `getFilter` to `NodeClient` and corresponding `filter` command to CLI.

### Wallet API changes

#### HTTP
Expand Down
25 changes: 24 additions & 1 deletion bin/bcoin-cli
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

const Config = require('bcfg');
const NodeClient = require('../lib/client/node');
const Amount = require('../lib/btc/amount');

const ports = {
main: 8332,
Expand Down Expand Up @@ -127,6 +128,24 @@ class CLI {
this.log(filter);
}

async estimateFee() {
const blocks = this.config.uint(0, 1);

const json = await this.client.estimateFee(blocks);

if (!json) {
this.log('Fee not found.');
return;
}

if (!Number.isInteger(json.rate)) {
this.log('Fee is not an integer.');
return;
}

this.log(Amount.btc(json.rate));
}

async getCoin() {
const hash = this.config.str(0, '');
const index = this.config.uint(1);
Expand Down Expand Up @@ -226,6 +245,9 @@ class CLI {
case 'filter':
await this.getFilter();
break;
case 'fee':
await this.estimateFee();
break;
case 'reset':
await this.reset();
break;
Expand All @@ -242,7 +264,8 @@ class CLI {
this.log(' $ coin [hash+index/address]: View coins.');
this.log(' $ block [hash/height]: View block.');
this.log(' $ header [hash/height]: View block header.');
this.log(' $ filter [hash/height]: View filter');
this.log(' $ filter [hash/height]: View filter.');
this.log(' $ fee [target]: Estimate smart fee.');
this.log(' $ reset [height/hash]: Reset chain to desired block.');
this.log(' $ rpc [command] [args]: Execute RPC command.' +
' (`bcoin-cli rpc help` for more)');
Expand Down
7 changes: 5 additions & 2 deletions lib/client/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,17 @@ class NodeClient extends Client {
}

/**
* Esimate smart fee.
* Estimate smart fee.
* @param {Number?} blocks
* @returns {Promise}
*/

estimateFee(blocks) {
assert(blocks == null || typeof blocks === 'number');
return this.call('estimate fee', blocks);
let query = '/fee';
if (blocks != null)
query += `?blocks=${blocks}`;
return this.get(query);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/node/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ class HTTP extends Server {
// Estimate fee
this.get('/fee', async (req, res) => {
const valid = Validator.fromRequest(req);
const blocks = valid.u32('blocks');
const blocks = valid.u32('blocks', 1);

if (!this.fees) {
res.json(200, { rate: this.network.feeRate });
Expand Down
7 changes: 4 additions & 3 deletions lib/wallet/nodeclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,17 @@ class NodeClient extends AsyncEmitter {
}

/**
* Esimate smart fee.
* Estimate smart fee.
* @param {Number?} blocks
* @returns {Promise}
*/

async estimateFee(blocks) {
if (!this.node.fees)
return this.network.feeRate;
return {rate: this.network.feeRate};

return this.node.fees.estimateFee(blocks);
const fee = this.node.fees.estimateFee(blocks);
return {rate: fee};
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/wallet/nullclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class NullClient extends EventEmitter {
*/

async estimateFee(blocks) {
return this.network.feeRate;
return {rate: this.network.feeRate};
}

/**
Expand Down
14 changes: 10 additions & 4 deletions lib/wallet/walletdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,15 +503,21 @@ class WalletDB extends EventEmitter {
if (this.feeRate > 0)
return this.feeRate;

const rate = await this.client.estimateFee(blocks);
const json = await this.client.estimateFee(blocks);

if (rate < this.network.feeRate)
if (!json)
throw new Error('Fee not found.');

if (!Number.isInteger(json.rate))
throw new Error('Fee is not an integer.');

if (json.rate < this.network.feeRate)
return this.network.feeRate;

if (rate > this.network.maxFeeRate)
if (json.rate > this.network.maxFeeRate)
return this.network.maxFeeRate;

return rate;
return json.rate;
}

/**
Expand Down

0 comments on commit d5eaf37

Please sign in to comment.