Skip to content

Commit

Permalink
feat(api): cache fee estimates that hit rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
nitsujlangston committed Nov 13, 2018
1 parent b2917e6 commit d752027
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/bitcore-node/src/routes/api/fee.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import { Request, Response } from 'express';
import { ChainStateProvider } from '../../providers/chain-state';
const router = require('express').Router({ mergeParams: true });
const feeCache = {};

router.get('/:target', async (req: Request, res: Response) => {
let { target, chain, network } = req.params;
if (network === 'regtest') {
return res.json({ feerate: 0.0002 }); // default 20 sat/byte for regtest
if (target < 0 || target > 100) {
return res.status(400).send('invalid target specified');
}
const cachedFee = feeCache[`${chain}:${network}:${target}`];
if (cachedFee && cachedFee.date > Date.now() - 10 * 1000) {
return res.json(cachedFee.fee);
}
try {
let fee = await ChainStateProvider.getFee({ chain, network, target});
if (!fee) {
return res.status(404).send('not available right now');
}
feeCache[`${chain}:${network}:${target}`] = { fee, date: Date.now() };
return res.json(fee);
} catch (err) {
return res.status(500).send(err);
Expand Down

0 comments on commit d752027

Please sign in to comment.