diff --git a/.gitignore b/.gitignore index 9561f1a..4f3f4a4 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,7 @@ .coverage coverage.xml htmlcov +build +dist +*.egg-info +*.swp diff --git a/obelisk/mempool_api.py b/obelisk/mempool_api.py index e30df1f..db845f2 100644 --- a/obelisk/mempool_api.py +++ b/obelisk/mempool_api.py @@ -28,3 +28,14 @@ def get_mempool_fee_estimates(): return None return json.load(res) + + +def get_fee_histogram(): + conn = HTTPSConnection("mempool.space") + conn.request("GET", "/api/mempool") + res = conn.getresponse() + + if res.status != 200: + return None + + return json.load(res)["fee_histogram"] diff --git a/obelisk/protocol.py b/obelisk/protocol.py index 0d3089c..c939620 100644 --- a/obelisk/protocol.py +++ b/obelisk/protocol.py @@ -25,7 +25,7 @@ from obelisk.errors_jsonrpc import JsonRPCError from obelisk.errors_libbitcoin import ZMQError -from obelisk.mempool_api import get_mempool_fee_estimates +from obelisk.mempool_api import get_mempool_fee_estimates, get_fee_histogram from obelisk.merkle import merkle_branch, merkle_branch_and_root from obelisk.util import ( bh2u, @@ -698,8 +698,16 @@ async def get_fee_histogram(self, writer, query): # pylint: disable=W0613 Return a histogram of the fee rates paid by transactions in the memory pool, weighted by transaction size. """ - # TODO: Help wanted - return {"result": [[0, 0]]} + # NOTE: This solution is using the mempool.space API. + # Let's try to eventually solve it with some internal logic. + if self.chain == "testnet": + return {"result": [[0, 0]]} + + fee_hist = get_fee_histogram() + if not fee_dict: + return {"result": [[0, 0]]} + + return {"result": fee_hist} async def add_peer(self, writer, query): # pylint: disable=W0613 """Method: server.add_peer