
- Created Alembic migrations for SQLite database - Set up database initialization on app startup - Fixed linting issues with Ruff - Updated README with comprehensive documentation - Configured startup tasks and health checks
56 lines
1.9 KiB
Python
56 lines
1.9 KiB
Python
from datetime import datetime, timedelta
|
|
from typing import Any, Optional
|
|
from fastapi import APIRouter, Depends, Query
|
|
from sqlalchemy.orm import Session
|
|
from sqlalchemy import desc, func
|
|
|
|
from app.db.session import get_db
|
|
from app.models.arbitrage import Trade
|
|
from app.schemas.arbitrage import TradesList
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("", response_model=TradesList)
|
|
async def get_trades(
|
|
token_address: Optional[str] = Query(None, description="Filter by specific token address"),
|
|
status: Optional[str] = Query(None, description="Filter by trade status (success, failed, pending)"),
|
|
time_period: Optional[int] = Query(24, description="Time period in hours to look back"),
|
|
limit: int = Query(20, ge=1, le=100, description="Number of trades to return"),
|
|
offset: int = Query(0, ge=0, description="Pagination offset"),
|
|
db: Session = Depends(get_db)
|
|
) -> Any:
|
|
"""
|
|
Retrieve trade history with optional filtering.
|
|
"""
|
|
query = db.query(Trade)
|
|
|
|
# Apply filters
|
|
if token_address:
|
|
query = query.filter(Trade.token_address == token_address)
|
|
|
|
if status:
|
|
query = query.filter(Trade.tx_status == status)
|
|
|
|
if time_period is not None:
|
|
start_time = datetime.utcnow() - timedelta(hours=time_period)
|
|
query = query.filter(Trade.created_at >= start_time)
|
|
|
|
# Get total count
|
|
total_count = query.count()
|
|
|
|
# Get paginated results
|
|
trades = query.order_by(desc(Trade.created_at)).offset(offset).limit(limit).all()
|
|
|
|
# Calculate total profit for the filtered trades
|
|
total_profit = db.query(func.sum(Trade.profit_amount_usd)).filter(
|
|
Trade.tx_status == "success",
|
|
Trade.id.in_([trade.id for trade in trades])
|
|
).scalar() or 0.0
|
|
|
|
return {
|
|
"trades": trades,
|
|
"count": total_count,
|
|
"timestamp": datetime.utcnow(),
|
|
"total_profit_usd": total_profit
|
|
} |