Skip to content

Commit 7519cc6

Browse files
committed
added back slashes for api calls & comments. Reverted endpoint variable name
1 parent 00341e0 commit 7519cc6

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

sauron/sauron.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class SauronError(Exception):
1919
def fetch(url):
2020
"""Fetch the given {url}, maybe through a pre-defined proxy."""
2121
# FIXME: Maybe try to be smart and renew circuit to broadcast different
22+
# transactions ? Hint: lightningd will agressively send us the same
23+
# transaction a certain amount of times.
2224
session = requests.session()
2325
session.proxies = plugin.sauron_socks_proxies
2426
retry_strategy = Retry(
@@ -36,10 +38,10 @@ def fetch(url):
3638

3739
@plugin.init()
3840
def init(plugin, options, **kwargs):
39-
plugin.sauron_api_endpoint = options.get("sauron-api-endpoint", None)
40-
plugin.log("plugin.sauron_api_endpoint = %s" % plugin.sauron_api_endpoint)
41+
plugin.api_endpoint = options.get("sauron-api-endpoint", None)
42+
plugin.log("plugin.api_endpoint = %s" % plugin.api_endpoint)
4143

42-
if not plugin.sauron_api_endpoint:
44+
if not plugin.api_endpoint:
4345
raise SauronError("You need to specify the sauron-api-endpoint option.")
4446
sys.exit(1)
4547

@@ -57,8 +59,8 @@ def init(plugin, options, **kwargs):
5759

5860
@plugin.method("getchaininfo")
5961
def getchaininfo(plugin, **kwargs):
60-
blockhash_url = "{}block-height/0".format(plugin.sauron_api_endpoint)
61-
blockcount_url = "{}blocks/tip/height".format(plugin.sauron_api_endpoint)
62+
blockhash_url = "{}/block-height/0".format(plugin.api_endpoint)
63+
blockcount_url = "{}/blocks/tip/height".format(plugin.api_endpoint)
6264

6365
chains = {
6466
"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f": "main",
@@ -87,6 +89,8 @@ def getchaininfo(plugin, **kwargs):
8789
raise SauronError("Unsupported network")
8890
plugin.sauron_network = chains[genesis_req.text]
8991

92+
# We wouldn't be able to hit it if its bitcoind wasn't synced, so
93+
# ibd = false and headercount = blockcount
9094
return {
9195
"chain": plugin.sauron_network,
9296
"blockcount": blockcount_req.text,
@@ -97,7 +101,7 @@ def getchaininfo(plugin, **kwargs):
97101
@plugin.method("getrawblockbyheight")
98102
def getrawblock(plugin, height, **kwargs):
99103
# Step 1: Get the block hash by height
100-
blockhash_url = "{}block-height/{}".format(plugin.sauron_api_endpoint, height)
104+
blockhash_url = "{}/block-height/{}".format(plugin.api_endpoint, height)
101105

102106
blockhash_req = fetch(blockhash_url)
103107
if blockhash_req.status_code != 200:
@@ -109,7 +113,7 @@ def getrawblock(plugin, height, **kwargs):
109113
block_hash = blockhash_req.text.strip() # Ensure no extra spaces or newlines
110114

111115
# Step 2: Determine the block URL and fetch the block data
112-
block_url = "{}block/{}/raw".format(plugin.sauron_api_endpoint, block_hash)
116+
block_url = "{}/block/{}/raw".format(plugin.api_endpoint, block_hash)
113117

114118
while True:
115119
block_req = fetch(block_url)
@@ -145,7 +149,7 @@ def getrawblock(plugin, height, **kwargs):
145149

146150
@plugin.method("sendrawtransaction")
147151
def sendrawtx(plugin, tx, **kwargs):
148-
sendtx_url = "{}tx".format(plugin.sauron_api_endpoint)
152+
sendtx_url = "{}/tx".format(plugin.api_endpoint)
149153

150154
sendtx_req = requests.post(sendtx_url, data=tx)
151155
if sendtx_req.status_code != 200:
@@ -163,9 +167,9 @@ def sendrawtx(plugin, tx, **kwargs):
163167
@plugin.method("getutxout")
164168
def getutxout(plugin, address, txid, vout, **kwargs):
165169
# Determine the API endpoint type based on the URL structure
166-
if "mutinynet" in plugin.sauron_api_endpoint:
170+
if "mutinynet" in plugin.api_endpoint:
167171
# MutinyNet API
168-
utxo_url = "{}address/{}/utxo".format(plugin.sauron_api_endpoint, address)
172+
utxo_url = "{}/address/{}/utxo".format(plugin.api_endpoint, address)
169173

170174
# Fetch the list of UTXOs for the given address
171175
utxo_req = fetch(utxo_url)
@@ -194,8 +198,8 @@ def getutxout(plugin, address, txid, vout, **kwargs):
194198

195199
else:
196200
# Blockstream API
197-
gettx_url = "{}/tx/{}".format(plugin.sauron_api_endpoint, txid)
198-
status_url = "{}/tx/{}/outspend/{}".format(plugin.sauron_api_endpoint, txid, vout)
201+
gettx_url = "{}/tx/{}".format(plugin.api_endpoint, txid)
202+
status_url = "{}/tx/{}/outspend/{}".format(plugin.api_endpoint, txid, vout)
199203

200204
gettx_req = fetch(gettx_url)
201205
if not gettx_req.status_code == 200:
@@ -229,16 +233,16 @@ def getutxout(plugin, address, txid, vout, **kwargs):
229233
@plugin.method("estimatefees")
230234
def estimatefees(plugin, **kwargs):
231235
# Define the URL based on the selected API
232-
if "mutinynet" in plugin.sauron_api_endpoint:
236+
if "mutinynet" in plugin.api_endpoint:
233237
# MutinyNet API
234-
feerate_url = "{}v1/fees/recommended".format(plugin.sauron_api_endpoint)
235-
plugin.log("estimatefees: plugin.sauron_api_endpoint = %s" % plugin.sauron_api_endpoint)
238+
feerate_url = "{}/v1/fees/recommended".format(plugin.api_endpoint)
239+
plugin.log("estimatefees: plugin.api_endpoint = %s" % plugin.api_endpoint)
236240
plugin.log("estimatefees: feerate_url = %s" % feerate_url)
237241

238242
else:
239243
# Blockstream API
240-
feerate_url = "{}/fee-estimates".format(plugin.sauron_api_endpoint)
241-
plugin.log("estimatefees: plugin.sauron_api_endpoint = %s" % plugin.sauron_api_endpoint)
244+
feerate_url = "{}/fee-estimates".format(plugin.api_endpoint)
245+
plugin.log("estimatefees: plugin.api_endpoint = %s" % plugin.api_endpoint)
242246
plugin.log("estimatefees: feerate_url = %s" % feerate_url)
243247

244248
feerate_req = fetch(feerate_url)
@@ -255,6 +259,7 @@ def estimatefees(plugin, **kwargs):
255259
slow = normal = urgent = very_urgent = int(feerate * multiply_factor)
256260
else:
257261
# Adjust fee rates based on the specific API
262+
# It returns sat/vB, we want sat/kVB, so multiply everything by 10**3
258263
slow = int(feerates["144"] * multiply_factor)
259264
normal = int(feerates["12"] * multiply_factor)
260265
urgent = int(feerates["6"] * multiply_factor)
@@ -288,6 +293,7 @@ def estimatefees(plugin, **kwargs):
288293
plugin.add_option(
289294
"sauron-api-endpoint",
290295
"",
296+
"The URL of the esplora instance to hit (including '/api').",
291297
"The URL of the mutinynet instance to hit (including '/api').",
292298
)
293299

0 commit comments

Comments
 (0)