fix: resolve explorer transaction search, timestamp formatting, and optimize RPC total_count queries
This commit is contained in:
@@ -418,6 +418,34 @@ async def api_block(height: int):
|
|||||||
return await get_block(height)
|
return await get_block(height)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/api/transactions/{tx_hash}")
|
||||||
|
async def api_transaction(tx_hash: str):
|
||||||
|
"""API endpoint for transaction data, normalized for frontend"""
|
||||||
|
async with httpx.AsyncClient() as client:
|
||||||
|
try:
|
||||||
|
response = await client.get(f"{BLOCKCHAIN_RPC_URL}/tx/{tx_hash}")
|
||||||
|
if response.status_code == 200:
|
||||||
|
tx = response.json()
|
||||||
|
# Normalize for frontend expectations
|
||||||
|
payload = tx.get("payload", {})
|
||||||
|
return {
|
||||||
|
"hash": tx.get("tx_hash"),
|
||||||
|
"block_height": tx.get("block_height"),
|
||||||
|
"from": tx.get("sender"),
|
||||||
|
"to": tx.get("recipient"),
|
||||||
|
"type": payload.get("type", "transfer"),
|
||||||
|
"amount": payload.get("amount", 0),
|
||||||
|
"fee": payload.get("fee", 0),
|
||||||
|
"timestamp": tx.get("created_at")
|
||||||
|
}
|
||||||
|
elif response.status_code == 404:
|
||||||
|
raise HTTPException(status_code=404, detail="Transaction not found")
|
||||||
|
except httpx.RequestError as e:
|
||||||
|
print(f"Error fetching transaction: {e}")
|
||||||
|
raise HTTPException(status_code=500, detail="Internal server error")
|
||||||
|
|
||||||
|
|
||||||
@app.get("/health")
|
@app.get("/health")
|
||||||
async def health():
|
async def health():
|
||||||
"""Health check endpoint"""
|
"""Health check endpoint"""
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
from sqlalchemy import func
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
@@ -200,8 +201,9 @@ async def get_transactions(limit: int = 20, offset: int = 0) -> Dict[str, Any]:
|
|||||||
.limit(limit)
|
.limit(limit)
|
||||||
).all()
|
).all()
|
||||||
|
|
||||||
# Get total count for pagination info
|
# Get total count for pagination info using optimized SQL count
|
||||||
total_count = len(session.exec(select(Transaction)).all())
|
total_count = session.exec(select(func.count()).select_from(Transaction)).one()
|
||||||
|
|
||||||
|
|
||||||
if not transactions:
|
if not transactions:
|
||||||
metrics_registry.increment("rpc_get_transactions_empty_total")
|
metrics_registry.increment("rpc_get_transactions_empty_total")
|
||||||
@@ -350,8 +352,8 @@ async def get_address_details(address: str, limit: int = 20, offset: int = 0) ->
|
|||||||
).all()
|
).all()
|
||||||
|
|
||||||
# Get total counts
|
# Get total counts
|
||||||
total_sent = len(session.exec(select(Transaction).where(Transaction.sender == address)).all())
|
total_sent = session.exec(select(func.count()).select_from(Transaction).where(Transaction.sender == address)).one()
|
||||||
total_received = len(session.exec(select(Transaction).where(Transaction.recipient == address)).all())
|
total_received = session.exec(select(func.count()).select_from(Transaction).where(Transaction.recipient == address)).one()
|
||||||
|
|
||||||
# Serialize transactions
|
# Serialize transactions
|
||||||
serialize_tx = lambda tx: {
|
serialize_tx = lambda tx: {
|
||||||
@@ -419,8 +421,8 @@ async def get_addresses(limit: int = 20, offset: int = 0, min_balance: int = 0)
|
|||||||
address_list = []
|
address_list = []
|
||||||
for addr in addresses:
|
for addr in addresses:
|
||||||
# Get transaction counts
|
# Get transaction counts
|
||||||
sent_count = len(session.exec(select(Transaction).where(Transaction.sender == addr.address)).all())
|
sent_count = session.exec(select(func.count()).select_from(Transaction).where(Transaction.sender == addr.address)).one()
|
||||||
received_count = len(session.exec(select(Transaction).where(Transaction.recipient == addr.address)).all())
|
received_count = session.exec(select(func.count()).select_from(Transaction).where(Transaction.recipient == addr.address)).one()
|
||||||
|
|
||||||
address_list.append({
|
address_list.append({
|
||||||
"address": addr.address,
|
"address": addr.address,
|
||||||
|
|||||||
Reference in New Issue
Block a user