from typing import Any, List, Optional from fastapi import APIRouter, Depends, HTTPException, Query from sqlalchemy.orm import Session from app import crud from app.api import deps from app.schemas.transaction import Transaction, TransactionDetail, TransactionList router = APIRouter() @router.get("/", response_model=TransactionList) def read_transactions( db: Session = Depends(deps.get_db), skip: int = 0, limit: int = 100 ) -> Any: """ Retrieve transactions with pagination. """ transactions = crud.transaction.get_multi(db, skip=skip, limit=limit) total = crud.transaction.get_count(db) return {"transactions": transactions, "total": total} @router.get("/{transaction_id}", response_model=TransactionDetail) def read_transaction( transaction_id: int, db: Session = Depends(deps.get_db), ) -> Any: """ Get a specific transaction by id. """ transaction = crud.transaction.get(db, id=transaction_id) if not transaction: raise HTTPException(status_code=404, detail="Transaction not found") return transaction @router.get("/hash/{tx_hash}", response_model=TransactionDetail) def read_transaction_by_hash( tx_hash: str, db: Session = Depends(deps.get_db), ) -> Any: """ Get a specific transaction by hash. """ transaction = crud.transaction.get_by_hash(db, tx_hash=tx_hash) if not transaction: raise HTTPException(status_code=404, detail="Transaction not found") return transaction @router.get("/signature/{signature}", response_model=TransactionDetail) def read_transaction_by_signature( signature: str, db: Session = Depends(deps.get_db), ) -> Any: """ Get a specific transaction by signature. """ transaction = crud.transaction.get_by_signature(db, signature=signature) if not transaction: raise HTTPException(status_code=404, detail="Transaction not found") return transaction @router.get("/program/{program_id}", response_model=TransactionList) def read_transactions_by_program( program_id: str, db: Session = Depends(deps.get_db), skip: int = 0, limit: int = 100 ) -> Any: """ Get transactions involving a specific program. """ transactions = crud.transaction.get_by_program_id( db, program_id=program_id, skip=skip, limit=limit ) # This is not an accurate count but serves as a simplified implementation total = len(transactions) return {"transactions": transactions, "total": total} @router.get("/account/{account}", response_model=TransactionList) def read_transactions_by_account( account: str, db: Session = Depends(deps.get_db), skip: int = 0, limit: int = 100 ) -> Any: """ Get transactions involving a specific account. """ transactions = crud.transaction.search_by_account( db, account=account, skip=skip, limit=limit ) # This is not an accurate count but serves as a simplified implementation total = len(transactions) return {"transactions": transactions, "total": total}