Skip to content

Repository files navigation

Solana DeFi Price Prediction Agent

A proof-of-concept AI agent that fetches real-time SOL/USDC price data, runs ML inference for short-term price prediction, and uses LLM reasoning to output simulated trade recommendations.

Project Structure

predict_agent/
├── backend/           # FastAPI server
├── cli/               # Command-line interface
├── core/              # Core business logic
│   ├── agent/         # Orchestrator, Grok client, RAG, guardrails
│   ├── data/          # CoinGecko client, price buffer, Solana RPC
│   ├── knowledge/     # Solana knowledge base
│   ├── ml/            # ML predictor, preprocessor
│   ├── simulation/    # Transaction builder, logger
│   └── utils/         # Config loading
├── frontend/          # React dashboard
├── config.yaml
└── pyproject.toml

Prerequisites

  • Python 3.10+
  • Poetry for dependency management
  • Node.js 18+ (for web frontend only)

Installation

# Clone and navigate to project
cd predict_agent

# Install Python dependencies
poetry install

# Activate virtual environment
poetry shell

# (Optional) For web frontend
cd frontend && npm install && cd ..

# Configure environment
cp .env.example .env
# Edit .env and add your API keys

Running the Application

Method 1: CLI (Command Line Interface)

The simplest way to run the agent.

# Activate environment first
poetry shell

# Run with default settings (3 cycles, 60s interval)
predict-agent

# Or without activating shell
poetry run predict-agent

CLI Options:

Option Default Description
--cycles N 3 Number of cycles (0 = indefinite)
--interval N 60 Seconds between cycles
--config PATH config.yaml Custom config file
--skip-historical false Skip historical data fetch
--quiet false Reduce output verbosity

Examples:

# Run 10 cycles with 30-second intervals
predict-agent --cycles 10 --interval 30

# Run indefinitely until Ctrl+C
predict-agent --cycles 0

# Quick test without historical data
predict-agent --cycles 1 --skip-historical

# Quiet mode with custom config
predict-agent --cycles 5 --quiet --config /path/to/config.yaml

Method 2: Web Dashboard (Frontend + Backend)

A visual interface with real-time updates via WebSocket.

+------------------+            +------------------+
|  React Frontend  |<-HTTP/WS-->|  FastAPI Backend |
|   (Port 5173)    |            |   (Port 8000)    |
+------------------+            +------------------+

Step 1: Start Backend API

poetry shell
uvicorn backend.api:app --reload --port 8000

Step 2: Start Frontend (in a new terminal)

cd frontend
npm run dev

Step 3: Open Browser

Navigate to http://localhost:5173

Dashboard Features:

  • Real-time price and prediction display
  • Live log streaming via WebSocket
  • Cycle history with charts
  • Manual cycle trigger / Start-Stop controls
  • Session export and review

WebSocket Status Indicator:

  • Live - Connected and receiving updates
  • Reconnecting... - Connection lost, auto-reconnecting

Configuration

Edit config.yaml to customize behavior:

data:
  primary_source: "coingecko"
  price_buffer_length: 120
  rate_limit_delay: 2.1

ml:
  model: "amazon/chronos-t5-small"
  prediction_length: 10
  device: "cpu"
  num_samples: 20
  min_prediction_points: 30

agent:
  grok_model: "grok-2-latest"
  grok_base_url: "https://api.x.ai/v1"
  rag_top_k: 3
  loop_interval: 60

guardrails:
  volatility_threshold_warn: 0.05
  volatility_threshold_block: 0.10
  confidence_threshold: 0.3
  max_position_pct: 0.10
  enable_rpc_check: true

simulation:
  default_portfolio_usd: 10000
  slippage_estimate: 0.005

Environment Variables

Variable Required Description
GROK_API_KEY Yes* Grok LLM API key
COINGECKO_API_KEY No Higher rate limits
SOLANA_RPC_URL No Custom RPC endpoint

*Without GROK_API_KEY, the agent uses fallback rule-based recommendations.

Setup:

# Create .env file
cat > .env << EOF
GROK_API_KEY=your_grok_api_key_here
COINGECKO_API_KEY=your_coingecko_key_here
SOLANA_RPC_URL=https://api.mainnet-beta.solana.com
EOF

Sample Output

CLI Output:

============================================================
STARTING AGENT LOOP
============================================================
Running 3 cycles...

[Cycle 1] Price: $142.35 | Predicted: $143.12 (+0.54%) | Action: HOLD
[Cycle 2] Price: $142.50 | Predicted: $143.25 (+0.53%) | Action: HOLD
[Cycle 3] Price: $142.80 | Predicted: $144.10 (+0.91%) | Action: BUY

Action Summary:
  BUY: 1
  HOLD: 2

JSON Log Entry:

{
  "timestamp": "2026-01-26T14:30:00Z",
  "cycle_id": 1,
  "data": {
    "current_price": 142.35,
    "source": "coingecko"
  },
  "prediction": {
    "predicted_price": 143.12,
    "predicted_change_pct": 0.54,
    "confidence_score": 0.72
  },
  "reasoning": {
    "recommendation": "HOLD",
    "confidence": "MEDIUM",
    "explanation": "Modest upward prediction with moderate confidence..."
  },
  "guardrails": {
    "final_result": "PASS",
    "should_trade": true
  },
  "simulation": {
    "action": "HOLD"
  }
}

AWS EC2 Deployment

Quick Start (Recommended Instance: t3.medium or larger)

Step 1: Launch EC2 Instance

Step 2: Connect and Setup

Step 3: Deploy Application

# Configure environment
cp .env.example .env
nano .env  # Add your GROK_API_KEY

# Start application
./deploy/deploy.sh start

Step 4: Access Application

  • API: http://YOUR_EC2_PUBLIC_IP
  • API Docs: http://YOUR_EC2_PUBLIC_IP/docs
  • Health Check: http://YOUR_EC2_PUBLIC_IP/api/status

Deployment Commands

# Start/restart application
./deploy/deploy.sh start

# Stop application
./deploy/deploy.sh stop

# View logs
./deploy/deploy.sh logs

# Check status
./deploy/deploy.sh status

# Update to latest version
./deploy/deploy.sh update

# Clean up everything
./deploy/deploy.sh clean

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages