Skip to content

Commit b8ca105

Browse files
committed
test if backend is Esplora or Mempool space
remove some redundant code lines
1 parent e151735 commit b8ca105

File tree

1 file changed

+39
-30
lines changed

1 file changed

+39
-30
lines changed

sauron/sauron.py

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ def init(plugin, options, **kwargs):
4545
raise SauronError("You need to specify the sauron-api-endpoint option.")
4646
sys.exit(1)
4747

48+
# Test for Esplora or mempoolspace
49+
try:
50+
# MutinyNet API
51+
feerate_url = "{}/v1/fees/recommended".format(plugin.api_endpoint)
52+
feerate_req = fetch(feerate_url)
53+
assert feerate_req.status_code == 200
54+
plugin.is_mempoolspace = True
55+
except AssertionError as e0:
56+
try:
57+
# Blockstream API
58+
feerate_url = "{}/fee-estimates".format(plugin.api_endpoint)
59+
feerate_req = fetch(feerate_url)
60+
assert feerate_req.status_code == 200
61+
plugin.is_mempoolspace = False
62+
except AssertionError as e1:
63+
raise Exception("Sauron API cannot be reached") from e1
64+
65+
4866
if options["sauron-tor-proxy"]:
4967
address, port = options["sauron-tor-proxy"].split(":")
5068
socks5_proxy = "socks5h://{}:{}".format(address, port)
@@ -163,24 +181,29 @@ def sendrawtx(plugin, tx, **kwargs):
163181
@plugin.method("getutxout")
164182
def getutxout(plugin, address, txid, vout, **kwargs):
165183
# Determine the API endpoint type based on the URL structure
166-
if "mutinynet" in plugin.api_endpoint:
184+
if plugin.is_mempoolspace:
167185
# MutinyNet API
168-
utxo_url = "{}/address/{}/utxo".format(plugin.api_endpoint, address)
186+
gettx_url = "{}/address/{}/utxo".format(plugin.api_endpoint, address)
187+
else:
188+
# Blockstream API
189+
gettx_url = "{}/tx/{}".format(plugin.api_endpoint, txid)
190+
status_url = "{}/tx/{}/outspend/{}".format(plugin.api_endpoint, txid, vout)
169191

170-
# Fetch the list of UTXOs for the given address
171-
utxo_req = fetch(utxo_url)
172-
if not utxo_req.status_code == 200:
173-
raise SauronError(
174-
"Endpoint at {} returned {} ({}) when trying to get UTXOs.".format(
175-
utxo_url, utxo_req.status_code, utxo_req.text
176-
)
192+
# Fetch the list of UTXOs for the given address
193+
gettx_req = fetch(gettx_url)
194+
if not gettx_req.status_code == 200:
195+
raise SauronError(
196+
"Endpoint at {} returned {} ({}) when trying to get transaction.".format(
197+
gettx_url, gettx_req.status_code, gettx_req.text
177198
)
178-
199+
)
200+
if plugin.is_mempoolspace:
201+
# Building response from MutinyNet API
179202
# Parse the UTXO data
180-
utxos = utxo_req.json()
203+
utxos = gettx_req.json()
181204
# Find the UTXO with the given txid and vout
182205
for utxo in utxos:
183-
if utxo['txid'] == txid and utxo['vout'] == vout:
206+
if utxo['txid'] == txid:
184207
return {
185208
"amount": utxo["value"],
186209
"script": None # MutinyNet API does not provide script information
@@ -191,19 +214,8 @@ def getutxout(plugin, address, txid, vout, **kwargs):
191214
"amount": None,
192215
"script": None
193216
}
194-
195217
else:
196-
# Blockstream API
197-
gettx_url = "{}/tx/{}".format(plugin.api_endpoint, txid)
198-
status_url = "{}/tx/{}/outspend/{}".format(plugin.api_endpoint, txid, vout)
199-
200-
gettx_req = fetch(gettx_url)
201-
if not gettx_req.status_code == 200:
202-
raise SauronError(
203-
"Endpoint at {} returned {} ({}) when trying to get transaction.".format(
204-
gettx_url, gettx_req.status_code, gettx_req.text
205-
)
206-
)
218+
# Building response from Blockstream API
207219
status_req = fetch(status_url)
208220
if not status_req.status_code == 200:
209221
raise SauronError(
@@ -229,18 +241,15 @@ def getutxout(plugin, address, txid, vout, **kwargs):
229241
@plugin.method("estimatefees")
230242
def estimatefees(plugin, **kwargs):
231243
# Define the URL based on the selected API
232-
if "mutinynet" in plugin.api_endpoint:
244+
if plugin.is_mempoolspace:
233245
# MutinyNet API
234246
feerate_url = "{}/v1/fees/recommended".format(plugin.api_endpoint)
235-
plugin.log("estimatefees: plugin.api_endpoint = %s" % plugin.api_endpoint)
236-
plugin.log("estimatefees: feerate_url = %s" % feerate_url)
237-
238247
else:
239248
# Blockstream API
240249
feerate_url = "{}/fee-estimates".format(plugin.api_endpoint)
241-
plugin.log("estimatefees: plugin.api_endpoint = %s" % plugin.api_endpoint)
242-
plugin.log("estimatefees: feerate_url = %s" % feerate_url)
243250

251+
plugin.log("estimatefees: plugin.api_endpoint = %s" % plugin.api_endpoint)
252+
plugin.log("estimatefees: feerate_url = %s" % feerate_url)
244253
feerate_req = fetch(feerate_url)
245254
assert feerate_req.status_code == 200
246255
feerates = feerate_req.json()

0 commit comments

Comments
 (0)