generated from nathanwoodburn/python-webserver-template
Compare commits
2 Commits
f0d233b2f6
...
25506da02c
| Author | SHA1 | Date | |
|---|---|---|---|
|
25506da02c
|
|||
|
2df0001352
|
100
server.py
100
server.py
@@ -110,6 +110,98 @@ def _get_public_base_url() -> str:
|
|||||||
return request.url_root.rstrip("/")
|
return request.url_root.rstrip("/")
|
||||||
|
|
||||||
|
|
||||||
|
def _resolve_namehash(namehash: str) -> str | None:
|
||||||
|
try:
|
||||||
|
db = get_db()
|
||||||
|
cur = db.execute("SELECT name FROM names WHERE namehash = ?", (namehash,))
|
||||||
|
row = cur.fetchone()
|
||||||
|
if row and row["name"]:
|
||||||
|
return row["name"]
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
req = requests.get(f"{HSD_API_BASE}/namehash/{namehash}", timeout=3)
|
||||||
|
if req.status_code == 200:
|
||||||
|
name = req.json().get("result")
|
||||||
|
if name:
|
||||||
|
try:
|
||||||
|
db = get_db()
|
||||||
|
db.execute(
|
||||||
|
"INSERT OR REPLACE INTO names (namehash, name) VALUES (?, ?)",
|
||||||
|
(namehash, name),
|
||||||
|
)
|
||||||
|
db.commit()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
return name
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _summarize_transaction(tx: dict) -> str:
|
||||||
|
outputs = tx.get("outputs", []) if isinstance(tx, dict) else []
|
||||||
|
inputs = tx.get("inputs", []) if isinstance(tx, dict) else []
|
||||||
|
|
||||||
|
if not outputs:
|
||||||
|
return "Transaction details available"
|
||||||
|
|
||||||
|
action_counts: dict[str, int] = {}
|
||||||
|
for output in outputs:
|
||||||
|
covenant = output.get("covenant") or {}
|
||||||
|
action = (covenant.get("action") or "NONE").upper()
|
||||||
|
action_counts[action] = action_counts.get(action, 0) + 1
|
||||||
|
|
||||||
|
finalize_count = action_counts.get("FINALIZE", 0)
|
||||||
|
if finalize_count > 1:
|
||||||
|
return f"Finalized {finalize_count:,} domains"
|
||||||
|
|
||||||
|
# Covenant-aware summary for domain operations (e.g., BID)
|
||||||
|
for output in outputs:
|
||||||
|
covenant = output.get("covenant") or {}
|
||||||
|
action = (covenant.get("action") or "").upper()
|
||||||
|
if action and action != "NONE":
|
||||||
|
value = _safe_hns_value(output.get("value"))
|
||||||
|
items = covenant.get("items") or []
|
||||||
|
name = None
|
||||||
|
if items and isinstance(items[0], str):
|
||||||
|
name = _resolve_namehash(items[0])
|
||||||
|
|
||||||
|
if action == "BID":
|
||||||
|
if name:
|
||||||
|
return f"Bid {value} on {name}"
|
||||||
|
return f"Bid {value} on a domain"
|
||||||
|
|
||||||
|
if name:
|
||||||
|
return f"{action.title()}ed {name} • Value {value}"
|
||||||
|
return f"{action.title()} covenant • Value {value}"
|
||||||
|
|
||||||
|
# Detect coinbase transaction
|
||||||
|
if inputs:
|
||||||
|
prevout = inputs[0].get("prevout") or {}
|
||||||
|
if (
|
||||||
|
prevout.get("hash")
|
||||||
|
== "0000000000000000000000000000000000000000000000000000000000000000"
|
||||||
|
and prevout.get("index") == 4294967295
|
||||||
|
):
|
||||||
|
reward = _safe_hns_value(sum((o.get("value") or 0) for o in outputs))
|
||||||
|
return f"Coinbase reward {reward}"
|
||||||
|
|
||||||
|
total_output_value = sum((o.get("value") or 0) for o in outputs)
|
||||||
|
total_output_hns = _safe_hns_value(total_output_value)
|
||||||
|
recipient_addresses = {
|
||||||
|
o.get("address") for o in outputs if o.get("address") and o.get("value", 0) > 0
|
||||||
|
}
|
||||||
|
recipient_count = len(recipient_addresses)
|
||||||
|
|
||||||
|
if recipient_count <= 1:
|
||||||
|
return f"Sent {total_output_hns}"
|
||||||
|
|
||||||
|
return f"Sent a total of {total_output_hns} to {recipient_count} addresses"
|
||||||
|
|
||||||
|
|
||||||
def _build_og_context(
|
def _build_og_context(
|
||||||
search_type: str | None = None, search_value: str | None = None
|
search_type: str | None = None, search_value: str | None = None
|
||||||
) -> dict:
|
) -> dict:
|
||||||
@@ -177,10 +269,9 @@ def _build_og_context(
|
|||||||
elif search_type == "tx":
|
elif search_type == "tx":
|
||||||
tx, err = _fetch_explorer_json(f"tx/{search_value}")
|
tx, err = _fetch_explorer_json(f"tx/{search_value}")
|
||||||
if not err and tx:
|
if not err and tx:
|
||||||
inputs = len(tx.get("inputs", []))
|
tx_summary = _summarize_transaction(tx)
|
||||||
outputs = len(tx.get("outputs", []))
|
|
||||||
fee = _safe_hns_value(tx.get("fee"))
|
fee = _safe_hns_value(tx.get("fee"))
|
||||||
subtitle = f"{inputs} inputs • {outputs} outputs • Fee {fee}"
|
subtitle = f"{tx_summary} • Fee {fee}"
|
||||||
og["title"] = _truncate("Transaction | Fire Explorer", 100)
|
og["title"] = _truncate("Transaction | Fire Explorer", 100)
|
||||||
og["description"] = _truncate(subtitle, 200)
|
og["description"] = _truncate(subtitle, 200)
|
||||||
og["image_query"]["subtitle"] = subtitle
|
og["image_query"]["subtitle"] = subtitle
|
||||||
@@ -330,6 +421,9 @@ def og_image():
|
|||||||
value_max_length = 30
|
value_max_length = 30
|
||||||
|
|
||||||
display_value = _ellipsize_middle(search_value, value_max_length)
|
display_value = _ellipsize_middle(search_value, value_max_length)
|
||||||
|
# Check if value is a whole number > 100 and format with commas
|
||||||
|
if search_value.isdigit() and int(search_value) > 100:
|
||||||
|
display_value = f"{int(search_value):,}"
|
||||||
value_font_size = "30"
|
value_font_size = "30"
|
||||||
if len(display_value) > 34:
|
if len(display_value) > 34:
|
||||||
value_font_size = "26"
|
value_font_size = "26"
|
||||||
|
|||||||
Reference in New Issue
Block a user