
- Create project structure with FastAPI - Add database models for blocks, transactions, arbitrages, pools, and DEXes - Implement Solana RPC client for fetching blockchain data - Create arbitrage detection algorithm - Implement comprehensive API endpoints for analytics - Set up database migrations with Alembic - Add detailed project documentation generated with BackendIM... (backend.im) Co-Authored-By: Claude <noreply@anthropic.com>
65 lines
2.8 KiB
Python
65 lines
2.8 KiB
Python
from typing import List, Optional, Dict, Any, Union
|
|
|
|
from sqlalchemy import desc
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.crud.base import CRUDBase
|
|
from app.models.transaction import Transaction
|
|
from app.schemas.transaction import TransactionCreate, TransactionUpdate
|
|
|
|
|
|
class CRUDTransaction(CRUDBase[Transaction, TransactionCreate, TransactionUpdate]):
|
|
"""CRUD operations for transactions."""
|
|
|
|
def get_by_hash(self, db: Session, *, tx_hash: str) -> Optional[Transaction]:
|
|
"""Get a transaction by its hash."""
|
|
return db.query(Transaction).filter(Transaction.transaction_hash == tx_hash).first()
|
|
|
|
def get_by_signature(self, db: Session, *, signature: str) -> Optional[Transaction]:
|
|
"""Get a transaction by its signature."""
|
|
return db.query(Transaction).filter(Transaction.signature == signature).first()
|
|
|
|
def get_by_block(
|
|
self, db: Session, *, block_id: int, skip: int = 0, limit: int = 100
|
|
) -> List[Transaction]:
|
|
"""Get transactions for a specific block with pagination."""
|
|
return db.query(Transaction).filter(
|
|
Transaction.block_id == block_id
|
|
).offset(skip).limit(limit).all()
|
|
|
|
def get_by_block_count(self, db: Session, *, block_id: int) -> int:
|
|
"""Get count of transactions for a specific block."""
|
|
return db.query(Transaction).filter(Transaction.block_id == block_id).count()
|
|
|
|
def get_successful_by_block(
|
|
self, db: Session, *, block_id: int, skip: int = 0, limit: int = 100
|
|
) -> List[Transaction]:
|
|
"""Get successful transactions for a specific block."""
|
|
return db.query(Transaction).filter(
|
|
Transaction.block_id == block_id,
|
|
Transaction.success == True
|
|
).offset(skip).limit(limit).all()
|
|
|
|
def get_by_program_id(
|
|
self, db: Session, *, program_id: str, skip: int = 0, limit: int = 100
|
|
) -> List[Transaction]:
|
|
"""Get transactions involving a specific program."""
|
|
# Note: This is a simplified version. In a production app, you would
|
|
# want to use a proper JSON query based on your database
|
|
return db.query(Transaction).filter(
|
|
Transaction.program_ids.like(f'%{program_id}%')
|
|
).offset(skip).limit(limit).all()
|
|
|
|
def search_by_account(
|
|
self, db: Session, *, account: str, skip: int = 0, limit: int = 100
|
|
) -> List[Transaction]:
|
|
"""Get transactions involving a specific account."""
|
|
# Note: This is a simplified version. In a production app, you would
|
|
# want to use a proper JSON query based on your database
|
|
return db.query(Transaction).filter(
|
|
Transaction.accounts.like(f'%{account}%')
|
|
).offset(skip).limit(limit).all()
|
|
|
|
|
|
# Create a single instance for use in dependency injection
|
|
transaction = CRUDTransaction(Transaction) |