
Renamed 'pool_metadata' to 'extra_data' in Pool model, schema, and migrations to avoid conflicts with SQLAlchemy's reserved 'metadata' attribute name. Generated with BackendIM... (backend.im)
Solana Arbitrage Analytics Backend
A FastAPI-based backend for tracking, analyzing, and visualizing arbitrage transactions on the Solana blockchain. This application provides detailed insights into arbitrage opportunities, performance, and patterns across various DEXes and liquidity pools.
Features
- Blockchain Data Fetching: Connects to Solana RPC endpoints to fetch and store blocks and transactions
- Arbitrage Detection: Automatically identifies and analyzes arbitrage transactions
- DEX & Pool Tracking: Maintains data on DEXes and liquidity pools involved in arbitrages
- Comprehensive Analytics: Detailed metrics on arbitrage profitability, volume, and patterns
- REST API: Well-documented API endpoints for integrating with frontends and other services
- Background Workers: Continuously fetches new blockchain data and analyzes it for arbitrages
- SQLite Database: Lightweight database for storing all analytics data
System Architecture
The system consists of several key components:
- Blockchain Fetcher: Retrieves block and transaction data from Solana RPC nodes
- Arbitrage Detector: Analyzes transactions to identify and record arbitrage activities
- Database Models: SQLAlchemy ORM models for storing blockchain and arbitrage data
- API Endpoints: FastAPI routes that provide access to the analytics data
- Background Workers: Services that run continuous fetching and analysis operations
API Endpoints
The API is organized into the following categories:
Blocks
GET /api/v1/blocks
: List blocks with paginationGET /api/v1/blocks/latest
: Get the latest blocksGET /api/v1/blocks/{block_id}
: Get block by IDGET /api/v1/blocks/height/{height}
: Get block by heightGET /api/v1/blocks/hash/{block_hash}
: Get block by hashGET /api/v1/blocks/{block_id}/transactions
: Get transactions in a block
Transactions
GET /api/v1/transactions
: List transactions with paginationGET /api/v1/transactions/{transaction_id}
: Get transaction by IDGET /api/v1/transactions/hash/{tx_hash}
: Get transaction by hashGET /api/v1/transactions/signature/{signature}
: Get transaction by signatureGET /api/v1/transactions/program/{program_id}
: Get transactions involving a programGET /api/v1/transactions/account/{account}
: Get transactions involving an account
Arbitrages
GET /api/v1/arbitrages
: List arbitrages with paginationGET /api/v1/arbitrages/stats
: Get overall arbitrage statisticsGET /api/v1/arbitrages/{arbitrage_id}
: Get detailed arbitrage informationGET /api/v1/arbitrages/initiator/{initiator_address}
: Get arbitrages by initiatorGET /api/v1/arbitrages/token/{token_address}
: Get arbitrages involving a tokenGET /api/v1/arbitrages/profit-range
: Get arbitrages within a profit rangeGET /api/v1/arbitrages/time-range
: Get arbitrages within a time rangeGET /api/v1/arbitrages/dex/{dex_id}
: Get arbitrages involving a DEX
DEXes
GET /api/v1/dexes
: List DEXes with paginationGET /api/v1/dexes/{dex_id}
: Get DEX by IDGET /api/v1/dexes/address/{address}
: Get DEX by addressGET /api/v1/dexes/name/{name}
: Get DEX by nameGET /api/v1/dexes/search
: Search DEXes by nameGET /api/v1/dexes/{dex_id}/pools
: Get pools for a DEX
Pools
GET /api/v1/pools
: List pools with paginationGET /api/v1/pools/active
: Get most active pools by volumeGET /api/v1/pools/{pool_id}
: Get pool by IDGET /api/v1/pools/address/{address}
: Get pool by addressGET /api/v1/pools/token/{token_address}
: Get pools containing a tokenGET /api/v1/pools/pair
: Get pools for a token pairGET /api/v1/pools/tvl-range
: Get pools within a TVL range
Stats
GET /api/v1/stats
: Get overall system statistics
Setup & Installation
Prerequisites
- Python 3.8+
- Access to a Solana RPC endpoint (public or private)
Installation Steps
-
Clone the repository:
git clone <repository-url> cd solana-arbitrage-analytics-backend
-
Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables (optional): Create a
.env
file in the project root:SOLANA_RPC_URL=https://your-solana-rpc-endpoint.com
-
Apply database migrations:
alembic upgrade head
-
Start the application:
uvicorn main:app --reload
-
Access the API documentation at:
http://localhost:8000/docs
Usage
Starting the Background Workers
The background workers need to be started to begin fetching and analyzing blockchain data:
-
Start the blockchain fetcher:
POST /api/v1/stats/worker/start-fetcher
-
Start the arbitrage detector:
POST /api/v1/stats/worker/start-detector
-
To stop the workers:
POST /api/v1/stats/worker/stop
Accessing Analytics
Once data has been collected, you can access various analytics through the API endpoints. The Swagger documentation provides detailed information on each endpoint and its parameters.
Development
Project Structure
├── alembic.ini # Alembic configuration
├── app/ # Application package
│ ├── api/ # API endpoints
│ │ └── v1/ # API version 1
│ ├── core/ # Core configuration
│ ├── crud/ # Database CRUD operations
│ ├── db/ # Database setup
│ ├── models/ # SQLAlchemy models
│ ├── schemas/ # Pydantic schemas
│ ├── services/ # Business logic services
│ └── utils/ # Utility functions
├── main.py # Application entry point
├── migrations/ # Database migrations
│ └── versions/ # Migration versions
└── requirements.txt # Python dependencies
Adding New Features
To add new features or models:
- Create or update SQLAlchemy models in
app/models/
- Create or update Pydantic schemas in
app/schemas/
- Create or update CRUD operations in
app/crud/
- Create or update API endpoints in
app/api/v1/
- Generate a new migration:
alembic revision --autogenerate -m "Description of changes"
- Apply the migration:
alembic upgrade head
License
MIT License
Acknowledgements
- Solana blockchain and its ecosystem
- FastAPI for the web framework
- SQLAlchemy for ORM
- Alembic for database migrations