A FastAPI-based service for tracking Bittensor dividends and sentiment analysis, with Redis caching and Celery task processing.
- Dividend Tracking: Real-time tracking of TAO dividends across all subnets
- Sentiment Analysis: Automated sentiment analysis of Bittensor-related tweets
- Redis Caching: Efficient caching of dividend and sentiment data
- Celery Tasks: Asynchronous processing of dividend updates and sentiment analysis
- REST API: Clean and documented API endpoints for accessing data
- OpenAPI Documentation: Interactive API documentation with Swagger UI
- Authentication: JWT-based authentication with bcrypt password hashing
- Security: Rate limiting and request validation
tao_api_project/
├── app/
│ ├── __init__.py
│ ├── main.py # FastAPI application setup
│ ├── config.py # Configuration and constants
│ ├── clients.py # External service clients
│ ├── utils.py # Utility functions and authentication
│ └── routes/
│ ├── __init__.py
│ ├── auth.py # Authentication endpoints
│ └── dividends.py # Dividend-related endpoints
├── tasks/
│ ├── __init__.py
│ └── worker.py # Celery tasks and workers
├── tests/
│ ├── __init__.py
│ ├── test_dividends.py # Dividend endpoint tests
│ └── test_clients_live.py # Live client tests
├── .env # Environment variables
├── requirements.txt # Python dependencies
├── fake_users_db.json # User authentication database
└── README.md # This file
- Python 3.9+
- Docker and Docker Compose
- Bittensor wallet
- API keys for Datura and LLM services
-
Clone the repository
git clone <repository-url> cd tao_api_project
-
Create virtual environment
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies
pip install -r requirements.txt
-
Configure environment variables Create a
.envfile with:REDIS_URL=redis://127.0.0.1:6379/0 DATABASE_URL=postgresql+asyncpg://tao_user:tao_pass@db:5432/tao_db DATURA_API_KEY=your_datura_api_key CHUTES_API_KEY=your_chutes_api_key BITTENSOR_WALLET_NAME=default BITTENSOR_WALLET_HOTKEY=default BITTENSOR_MNEMONIC="your_wallet_mnemonic" DEFAULT_HOTKEY="your_default_hotkey" DEFAULT_NETUID=18 -
Start Redis
redis-server
-
Start Celery worker
celery -A tasks.worker worker --loglevel=info
-
Run the application
uvicorn app.main:app --reload
-
Docker Compose Setup
# Build and start all services docker compose up --build -d # Check service status docker compose ps # View logs docker compose logs -f
-
Validation Steps
# Check if the API is responding curl http://localhost:8000/api/v1/health # Verify Redis connection curl http://localhost:8000/api/v1/tao-dividends/redis-info # Verify database connection curl http://localhost:8000/api/v1/tao-dividends/status # Test authentication curl -X POST "http://localhost:8000/api/v1/login" \ -H "Content-Type: application/x-www-form-urlencoded" \ -d "username=codefred&password=secret123" # Test dividend endpoint with authentication curl -X GET "http://localhost:8000/api/v1/tao-dividends/all" \ -H "Authorization: Bearer <your_access_token>" # Check Celery worker status docker compose logs worker # Check Celery scheduler status docker compose logs scheduler
-
Environment Configuration
- Set production environment variables in
.env:REDIS_URL=redis://redis:6379/0 DATABASE_URL=postgresql+asyncpg://tao_user:tao_pass@db:5432/tao_db DATURA_API_KEY=your_production_datura_api_key CHUTES_API_KEY=your_production_chutes_api_key BITTENSOR_WALLET_NAME=production BITTENSOR_WALLET_HOTKEY=production BITTENSOR_MNEMONIC="your_production_wallet_mnemonic" DEFAULT_HOTKEY="your_production_hotkey" DEFAULT_NETUID=18
- Set production environment variables in
-
Monitoring and Maintenance
- Check service health:
docker compose ps docker compose logs -f
- Monitor API endpoints:
curl http://localhost:8000/api/v1/health curl http://localhost:8000/api/v1/tao-dividends/status
- Check Celery tasks:
docker compose logs worker docker compose logs scheduler
- Check service health:
-
Data Persistence
- Redis data is persisted in the
redis_datavolume - PostgreSQL data is persisted in the
postgres_datavolume - Bittensor wallet data is mounted from host's
~/.bittensor/wallets
- Redis data is persisted in the
The API uses JWT-based authentication. To authenticate:
-
Get Access Token
curl -X POST "http://localhost:8000/api/v1/login" \ -H "Content-Type: application/x-www-form-urlencoded" \ -d "username=codefred&password=secret123"
-
Use Access Token
curl -X GET "http://localhost:8000/api/v1/tao-dividends/all" \ -H "Authorization: Bearer <your_access_token>"
-
codefred
- Username: codefred
- Password: secret123
-
devqueen
- Username: devqueen
- Password: secret123
POST /api/v1/login- Get JWT token- Form data: username, password
- Returns: access_token, token_type
GET /api/v1/tao-dividends/all- Get all dividendsGET /api/v1/tao-dividends?netuid={netuid}&hotkey={hotkey}- Get specific dividendGET /api/v1/tao-dividends/status- Get update statusGET /api/v1/tao-dividends/redis-info- Get Redis info
GET /api/v1/tao-dividends?netuid={netuid}&hotkey={hotkey}&trade=true- Get dividends with sentiment analysis
-
Run All Tests
pytest -v
-
Live API Tests
python tests/test_clients_live.py
pytest --cov=app --cov=tasks tests/- Cached for 2 minutes (CACHE_TTL)
- Key format:
tao_dividend:{netuid}:{hotkey} - Block hash caching for consistency
- Cached for 2 minutes (CACHE_TTL)
- Key format:
sentiment:{netuid} - Error results also cached
The API documentation is available at /api/v1/docs when running the application. It provides:
- Interactive Swagger UI interface
- Detailed endpoint descriptions
- Request/response schemas
- Authentication requirements
- Example requests
To access the documentation:
- Start the application
- Open your browser and navigate to
http://localhost:8000/api/v1/docs - Explore the available endpoints and try out the API
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
[Your License Here]
For support, please open an issue in the repository or contact [Your Contact Info].