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