From f80de9ac5cbd501de8ef8ef8b09cb5e57f87320a Mon Sep 17 00:00:00 2001 From: Jorge Martinez Ortega Date: Tue, 17 Sep 2024 16:14:54 -0600 Subject: [PATCH] remove some redundant code lines --- sauron/sauron.py | 51 +++++++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/sauron/sauron.py b/sauron/sauron.py index c902a0065..0884fc1c9 100755 --- a/sauron/sauron.py +++ b/sauron/sauron.py @@ -51,7 +51,6 @@ def init(plugin, options, **kwargs): feerate_url = "{}/v1/fees/recommended".format(plugin.api_endpoint) feerate_req = fetch(feerate_url) assert feerate_req.status_code == 200 - plugin.is_esplora = False plugin.is_mempoolspace = True except AssertionError as e0: try: @@ -59,10 +58,9 @@ def init(plugin, options, **kwargs): feerate_url = "{}/fee-estimates".format(plugin.api_endpoint) feerate_req = fetch(feerate_url) assert feerate_req.status_code == 200 - plugin.is_esplora = True plugin.is_mempoolspace = False except AssertionError as e1: - raise Exception("Sauron API cannot be reached") from e + raise Exception("Sauron API cannot be reached") from e1 if options["sauron-tor-proxy"]: @@ -185,22 +183,27 @@ def getutxout(plugin, address, txid, vout, **kwargs): # Determine the API endpoint type based on the URL structure if plugin.is_mempoolspace: # MutinyNet API - utxo_url = "{}/address/{}/utxo".format(plugin.api_endpoint, address) + gettx_url = "{}/address/{}/utxo".format(plugin.api_endpoint, address) + else: + # Blockstream API + gettx_url = "{}/tx/{}".format(plugin.api_endpoint, txid) + status_url = "{}/tx/{}/outspend/{}".format(plugin.api_endpoint, txid, vout) - # Fetch the list of UTXOs for the given address - utxo_req = fetch(utxo_url) - if not utxo_req.status_code == 200: - raise SauronError( - "Endpoint at {} returned {} ({}) when trying to get UTXOs.".format( - utxo_url, utxo_req.status_code, utxo_req.text - ) + # Fetch the list of UTXOs for the given address + gettx_req = fetch(gettx_url) + if not gettx_req.status_code == 200: + raise SauronError( + "Endpoint at {} returned {} ({}) when trying to get transaction.".format( + gettx_url, gettx_req.status_code, gettx_req.text ) - + ) + if plugin.is_mempoolspace: + # Building response from MutinyNet API # Parse the UTXO data - utxos = utxo_req.json() + utxos = gettx_req.json() # Find the UTXO with the given txid and vout for utxo in utxos: - if utxo['txid'] == txid and utxo['vout'] == vout: + if utxo['txid'] == txid: return { "amount": utxo["value"], "script": None # MutinyNet API does not provide script information @@ -211,19 +214,8 @@ def getutxout(plugin, address, txid, vout, **kwargs): "amount": None, "script": None } - else: - # Blockstream API - gettx_url = "{}/tx/{}".format(plugin.api_endpoint, txid) - status_url = "{}/tx/{}/outspend/{}".format(plugin.api_endpoint, txid, vout) - - gettx_req = fetch(gettx_url) - if not gettx_req.status_code == 200: - raise SauronError( - "Endpoint at {} returned {} ({}) when trying to get transaction.".format( - gettx_url, gettx_req.status_code, gettx_req.text - ) - ) + # Building response from Blockstream API status_req = fetch(status_url) if not status_req.status_code == 200: raise SauronError( @@ -252,15 +244,12 @@ def estimatefees(plugin, **kwargs): if plugin.is_mempoolspace: # MutinyNet API feerate_url = "{}/v1/fees/recommended".format(plugin.api_endpoint) - plugin.log("estimatefees: plugin.api_endpoint = %s" % plugin.api_endpoint) - plugin.log("estimatefees: feerate_url = %s" % feerate_url) - else: # Blockstream API feerate_url = "{}/fee-estimates".format(plugin.api_endpoint) - plugin.log("estimatefees: plugin.api_endpoint = %s" % plugin.api_endpoint) - plugin.log("estimatefees: feerate_url = %s" % feerate_url) + plugin.log("estimatefees: plugin.api_endpoint = %s" % plugin.api_endpoint) + plugin.log("estimatefees: feerate_url = %s" % feerate_url) feerate_req = fetch(feerate_url) assert feerate_req.status_code == 200 feerates = feerate_req.json()