Update elerium_ordinals_daemon.py

Added fee estimation via get_fee_rate function which calls bitcoin-cli estimatesmartfee with a conf_target of 3 blocks
This commit is contained in:
nick 2023-12-01 12:23:41 +00:00
parent 676d349ca3
commit 275287bedc

View File

@ -15,6 +15,9 @@ STORE_ID = "BLt2tAvgDHaYL6eCpbcNTybaPEUS6cYBCoXZLMBw8APg"
API_KEY = "8daa71e669ca230acd5fb437fee6cfdd659c92f6" API_KEY = "8daa71e669ca230acd5fb437fee6cfdd659c92f6"
DB_PATH = "elerium_orders.db" DB_PATH = "elerium_orders.db"
CHECK_INTERVAL = 60 # in seconds CHECK_INTERVAL = 60 # in seconds
# Maximum weight units for a safe transaction
MAX_INSCRIPTION_SIZE = 390000 # Maximum size in weight units
# Set up logging # Set up logging
logging.basicConfig(filename='elerium_daemon.log', level=logging.INFO, logging.basicConfig(filename='elerium_daemon.log', level=logging.INFO,
@ -30,13 +33,6 @@ cursor.execute('''CREATE TABLE IF NOT EXISTS orders
conn.commit() conn.commit()
#Bitcoin node checks
#check ord is able to run correctly. If ord can check transactions, that means bitcoind is running fine on the machine & ord is set up correctly
#ord --cookie-file=/media/n/backup/bitcoin-core/bitcoin-25.0/data/testnet3/.cookie --testnet wallet transactionse --testnet wallet transactions
#If that works, continue with program, else, throw error and shut down gracefully
# Headers for BTCPay Server API requests # Headers for BTCPay Server API requests
headers = { headers = {
@ -46,7 +42,43 @@ headers = {
def execute_inscription(metadata):
def get_fee_rate():
# Assuming bitcoin-cli is located in the same directory
command = [
"/media/n/backup/bitcoin-core/bitcoin-25.0/bin/bitcoin-cli",
"-datadir=/media/n/backup/bitcoin-core/bitcoin-25.0/data",
"-rpcport=18332",
"estimatesmartfee",
"3", # conf_target for 3 blocks
"conservative" # estimate_mode
]
try:
result = subprocess.run(command, capture_output=True, text=True, check=True)
output = result.stdout
data = json.loads(output)
# Convert BTC/kvB to sat/B and round to nearest integer
btc_per_kvB = data.get("feerate", 0)
sat_per_B = int(btc_per_kvB * 100000000 / 1000)
return sat_per_B
except subprocess.CalledProcessError as e:
error_msg = e.stderr
logging.error(f"Fee rate estimation failed: {error_msg}")
return 1 # Default fee rate or handle error accordingly
def inscribe(metadata):
metadata_size = len(json.dumps(metadata))
if metadata_size > MAX_INSCRIPTION_SIZE:
logging.error(f"Metadata size {metadata_size} exceeds maximum limit.")
return None
fee_rate = get_fee_rate()
print("FEE RATE: "+ str(fee_rate))
with tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix='.json') as temp_file: with tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix='.json') as temp_file:
# Write the metadata to the temporary file # Write the metadata to the temporary file
json.dump(metadata, temp_file) json.dump(metadata, temp_file)
@ -54,16 +86,24 @@ def execute_inscription(metadata):
command = [ command = [
"ord", "ord",
#"--cookie-file=/media/n/backup/bitcoin-core/bitcoin-25.0/data/testnet3/.cookie", "--cookie-file=/media/n/backup/bitcoin-core/bitcoin-25.0/data/testnet3/.cookie",
#"--testnet", "--testnet",
"wallet", "wallet",
"inscribe", "inscribe",
"--fee-rate", "--fee-rate",
"1", str(fee_rate), # Use dynamic fee rate
"--file", "--file",
temp_file_path temp_file_path
] ]
# Print the command
print("Command to be executed:", ' '.join(command))
# Ask user for confirmation
user_input = input("Do you want to continue with the inscription? (Y/N): ")
if user_input.lower() != 'y':
print("Inscription cancelled.")
exit()
return None
try: try:
result = subprocess.run(command, capture_output=True, text=True, check=True) result = subprocess.run(command, capture_output=True, text=True, check=True)
output = result.stdout output = result.stdout
@ -105,7 +145,9 @@ def is_inscribed(invoice):
metadata = invoice.get('metadata', {}) metadata = invoice.get('metadata', {})
ordinals_id = metadata.get('ordinalsId', '') ordinals_id = metadata.get('ordinalsId', '')
is_inscribed = len(ordinals_id) > 1 # Check if ordinals_id is not None and has a length greater than 1
is_inscribed = ordinals_id is not None and len(ordinals_id) > 1
# Debugging print statement # Debugging print statement
print(f"Invoice ID: {invoice['id']}, Ordinals ID: {ordinals_id}, Is inscribed: {is_inscribed}") print(f"Invoice ID: {invoice['id']}, Ordinals ID: {ordinals_id}, Is inscribed: {is_inscribed}")
@ -125,7 +167,7 @@ def process_invoices(invoices):
print("Found non-inscribed order: " + str(invoice)) print("Found non-inscribed order: " + str(invoice))
# 1. Inscribing the order # 1. Inscribing the order
metadata = invoice.get('metadata', {}) metadata = invoice.get('metadata', {})
ordinalsid = execute_inscription(metadata=metadata) ordinalsid = inscribe(metadata=metadata)
# 2. Prepare the invoice data for update # 2. Prepare the invoice data for update
update_data = invoice.copy() update_data = invoice.copy()