Update elerium_ordinals_daemon.py

In our updated script, we added a check to ensure that ordinalsid is not None before proceeding with updating the invoice. This prevents the script from trying to update invoices with incomplete or invalid data.
This commit is contained in:
nick 2023-12-05 09:36:49 +00:00
parent 76809d536a
commit 46058a453d

View File

@ -137,10 +137,9 @@ def inscribe(metadata):
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
error_msg = e.stderr error_msg = e.stderr
if "error: failed to connect to Bitcoin Core RPC" in error_msg: # Log the error and return None to continue the script
raise ConnectionError("Failed to connect to Bitcoin Core RPC") logging.error(f"Error during inscribe operation: {error_msg}")
else: return None
raise RuntimeError(f"Command execution failed: {error_msg}")
finally: finally:
# Clean up the temporary file # Clean up the temporary file
@ -177,39 +176,43 @@ def process_invoices(invoices):
for invoice in invoices: for invoice in invoices:
if invoice['status'] != 'Settled': if invoice['status'] != 'Settled':
continue # Skip non-settled invoices continue # Skip non-settled invoices
invoice_id = invoice['id'] invoice_id = invoice['id']
cursor.execute("SELECT status FROM orders WHERE invoice_id = ?", (invoice_id,)) cursor.execute("SELECT status FROM orders WHERE invoice_id = ?", (invoice_id,))
if not is_inscribed(invoice): if not is_inscribed(invoice):
print("Found non-inscribed order: " + str(invoice)) print("Found non-inscribed order: " + str(invoice))
# 1. Inscribing the order
metadata = invoice.get('metadata', {}) metadata = invoice.get('metadata', {})
ordinalsid = inscribe(metadata=metadata) ordinalsid = inscribe(metadata=metadata)
# 2. Prepare the invoice data for update # Only proceed if inscribe was successful
update_data = invoice.copy() if ordinalsid is not None:
update_data['metadata'] = metadata.copy() update_data = invoice.copy()
update_data['metadata']['ordinalsId'] = ordinalsid update_data['metadata'] = metadata.copy()
update_data['metadata']['ordinalsId'] = ordinalsid
# 3. Update the invoice on BTCPay Server update_endpoint = f"{BTCPAY_INSTANCE}/api/v1/stores/{STORE_ID}/invoices/{invoice_id}"
update_endpoint = f"{BTCPAY_INSTANCE}/api/v1/stores/{STORE_ID}/invoices/{invoice_id}" response = requests.put(update_endpoint, json=update_data, headers=headers)
response = requests.put(update_endpoint, json=update_data, headers=headers)
if response.status_code == 200:
logging.info(f"Updated invoice {invoice_id} with ordinalsId "+ ordinalsid)
print(f"Updated invoice {invoice_id} with ordinalsId.")
# Update the local database as well if response.status_code == 200:
cursor.execute("INSERT OR REPLACE INTO orders (invoice_id, status) VALUES (?, ?)", logging.info(f"Updated invoice {invoice_id} with ordinalsId {ordinalsid}")
(invoice_id, 'inscribed')) print(f"Updated invoice {invoice_id} with ordinalsId.")
conn.commit()
cursor.execute("INSERT OR REPLACE INTO orders (invoice_id, status) VALUES (?, ?)",
(invoice_id, 'inscribed'))
conn.commit()
else:
logging.error(f"Failed to update invoice {invoice_id}: {response.text}")
print(f"Failed to update invoice {invoice_id}: {response.text}")
else: else:
logging.error(f"Failed to update invoice {invoice_id}: {response.text}") logging.error(f"Inscribe operation failed for invoice {invoice_id}. Skipping update.")
print(f"Failed to update invoice {invoice_id}: {response.text}") continue # Skip updating this invoice
else: else:
print("Already inscribed order: " + str(invoice)) print("Already inscribed order: " + str(invoice))
def main_loop(): def main_loop():
while True: while True:
invoices = fetch_invoices() invoices = fetch_invoices()