Skip to content

Commit

Permalink
added back slashes for api calls & comments. Reverted endpoint variab…
Browse files Browse the repository at this point in the history
…le name
  • Loading branch information
Victor Guzman authored and Victor Guzman committed Sep 12, 2024
1 parent 00341e0 commit f343745
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions sauron/sauron.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class SauronError(Exception):
def fetch(url):
"""Fetch the given {url}, maybe through a pre-defined proxy."""
# FIXME: Maybe try to be smart and renew circuit to broadcast different
# transactions ? Hint: lightningd will agressively send us the same
# transaction a certain amount of times.
session = requests.session()
session.proxies = plugin.sauron_socks_proxies
retry_strategy = Retry(
Expand All @@ -36,10 +38,10 @@ def fetch(url):

@plugin.init()
def init(plugin, options, **kwargs):
plugin.sauron_api_endpoint = options.get("sauron-api-endpoint", None)
plugin.log("plugin.sauron_api_endpoint = %s" % plugin.sauron_api_endpoint)
plugin.api_endpoint = options.get("sauron-api-endpoint", None)
plugin.log("plugin.api_endpoint = %s" % plugin.api_endpoint)

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

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

@plugin.method("getchaininfo")
def getchaininfo(plugin, **kwargs):
blockhash_url = "{}block-height/0".format(plugin.sauron_api_endpoint)
blockcount_url = "{}blocks/tip/height".format(plugin.sauron_api_endpoint)
blockhash_url = "{}/block-height/0".format(plugin.api_endpoint)
blockcount_url = "{}/blocks/tip/height".format(plugin.api_endpoint)

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

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

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

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

while True:
block_req = fetch(block_url)
Expand Down Expand Up @@ -145,7 +149,7 @@ def getrawblock(plugin, height, **kwargs):

@plugin.method("sendrawtransaction")
def sendrawtx(plugin, tx, **kwargs):
sendtx_url = "{}tx".format(plugin.sauron_api_endpoint)
sendtx_url = "{}/tx".format(plugin.api_endpoint)

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

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

else:
# Blockstream API
gettx_url = "{}/tx/{}".format(plugin.sauron_api_endpoint, txid)
status_url = "{}/tx/{}/outspend/{}".format(plugin.sauron_api_endpoint, txid, vout)
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:
Expand Down Expand Up @@ -229,16 +233,16 @@ def getutxout(plugin, address, txid, vout, **kwargs):
@plugin.method("estimatefees")
def estimatefees(plugin, **kwargs):
# Define the URL based on the selected API
if "mutinynet" in plugin.sauron_api_endpoint:
if "mutinynet" in plugin.api_endpoint:
# MutinyNet API
feerate_url = "{}v1/fees/recommended".format(plugin.sauron_api_endpoint)
plugin.log("estimatefees: plugin.sauron_api_endpoint = %s" % plugin.sauron_api_endpoint)
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.sauron_api_endpoint)
plugin.log("estimatefees: plugin.sauron_api_endpoint = %s" % plugin.sauron_api_endpoint)
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)

feerate_req = fetch(feerate_url)
Expand All @@ -255,6 +259,7 @@ def estimatefees(plugin, **kwargs):
slow = normal = urgent = very_urgent = int(feerate * multiply_factor)
else:
# Adjust fee rates based on the specific API
# It returns sat/vB, we want sat/kVB, so multiply everything by 10**3
slow = int(feerates["144"] * multiply_factor)
normal = int(feerates["12"] * multiply_factor)
urgent = int(feerates["6"] * multiply_factor)
Expand Down Expand Up @@ -288,6 +293,7 @@ def estimatefees(plugin, **kwargs):
plugin.add_option(
"sauron-api-endpoint",
"",
"The URL of the esplora instance to hit (including '/api').",
"The URL of the mutinynet instance to hit (including '/api').",
)

Expand Down

0 comments on commit f343745

Please sign in to comment.