Skip to content

Commit

Permalink
Add caching to mempool.space requests
Browse files Browse the repository at this point in the history
  • Loading branch information
koirikivi committed Apr 27, 2024
1 parent 2046c50 commit 349b0be
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions bridge_node/bridge/common/btc/fees.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import math
import time

import requests

Expand All @@ -12,7 +13,10 @@
class BitcoinFeeEstimator:
def __init__(self, *, network: BitcoinNetwork, rpc: BitcoinRPC):
self._network = network
self._mempool_space_fee_estimator = MempoolSpaceFeeEstimator(network=network)
self._mempool_space_fee_estimator = MempoolSpaceFeeEstimator(
network=network,
cache_ttl=60,
)
self._rpc_fee_estimator = BitcoinRpcFeeEstimator(network=network, rpc=rpc)

def get_fee_sats_per_vb(self):
Expand All @@ -37,6 +41,7 @@ def __init__(
self,
*,
network: BitcoinNetwork,
cache_ttl: int = 0,
):
if not is_bitcoin_network(network):
raise ValueError(f"Invalid network: {network}")
Expand All @@ -45,8 +50,17 @@ def __init__(
self._mempool_url = "https://mempool.space"
if self._network != "mainnet":
self._mempool_url += f"/{self._network}"
self._cache_ttl = cache_ttl
self._cached_fee_sats_per_vb = None
self._cache_time = 0

def get_fee_sats_per_vb(self):
def get_fee_sats_per_vb(self) -> int:
if self._cached_fee_sats_per_vb is None or self._cache_time + self._cache_ttl < time.time():
self._cached_fee_sats_per_vb = self._get_fee_sats_per_vb()
self._cache_time = time.time()
return self._cached_fee_sats_per_vb

def _get_fee_sats_per_vb(self) -> int:
# Original from bidi fastbtc
# private async fetchFeeSatsPerVBFromMempoolSpace(): Promise<number> {
# let url: string;
Expand Down

0 comments on commit 349b0be

Please sign in to comment.