Skip to content

Latest commit

Β 

History

14 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Coven Backend

FastAPI service that powers Coven β€” on-chain alpha intelligence for Solana + BNB Chain.

This is the engine behind usecoven.vercel.app. It ingests every swap from tracked smart-money wallets, runs three independent signal algorithms over the stream, executes paper trades on behalf of user-created bots, and dispatches Telegram alerts β€” all in a single live loop.

πŸ† Built for the AVE Γ— CLAW Hackathon. AVE is the primary data source: 11 of 16 documented REST endpoints + the live multi_tx WebSocket stream.

Frontend repo β†’ Β· Live site β†’ Β· Demo video β†’


Table of contents

  1. What this does
  2. Architecture at a glance
  3. Project layout
  4. Setup
  5. Environment variables
  6. Running with Docker
  7. Background jobs
  8. API surface
  9. Event bus
  10. Deployment
  11. Common operations
  12. Troubleshooting

What this does

A continuous detection-to-action loop:

Smart-money wallet swaps a token
   ↓ (Helius / AVE WSS β€” sub-second)
Background jobs ingest + normalize the swap
   ↓
contagion_detector / rank_poller score the move
   ↓
SIGNAL_FIRED β†’ signal_enricher β†’ SIGNAL_SCORED
   ↓
bot_runner opens paper trades for matching user bots
telegram_dispatcher DMs alerts to opted-in users
SSE stream pushes everything to open browser tabs

End-to-end latency: typically under 2 seconds from on-chain confirmation to the user's screen.

Architecture at a glance

flowchart TD
    subgraph FEEDS["External Feeds"]
        AVE["⭐ AVE Data API<br/>11 REST + multi_tx WSS"]
        HELIUS["Helius WSS"]
        BSCSCAN["BSCScan v2"]
        JUPITER["Jupiter (price fallback)"]
    end

    subgraph JOBS["Background Jobs (asyncio)"]
        ws_listener
        helius_stream
        wallet_poller
        price_listener
        rank_poller
        position_monitor
    end

    subgraph SERVICES["Event-bus Services"]
        contagion_detector
        signal_enricher
        bot_runner
        telegram_dispatcher
    end

    MONGO[("MongoDB")]
    REST["REST API"]
    SSE["SSE stream"]
    TGBOT["Telegram poller"]
    USER(("πŸ‘€ User"))

    AVE --> ws_listener & rank_poller & price_listener
    HELIUS --> helius_stream
    BSCSCAN --> wallet_poller
    JUPITER -.fallback.-> bot_runner

    JOBS --> contagion_detector --> signal_enricher
    rank_poller --> signal_enricher
    signal_enricher --> bot_runner & telegram_dispatcher
    price_listener --> position_monitor --> bot_runner

    SERVICES <--> MONGO
    MONGO --> REST
    SERVICES --> SSE
    REST <--> USER
    SSE -.live.-> USER
    telegram_dispatcher --> TGBOT -.alert.-> USER

    style AVE fill:#3CC47B,color:#0b0e14,stroke:#3CC47B,stroke-width:2px
Loading

Project layout

backend/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ main.py                 # FastAPI app + lifespan (boots all jobs)
β”‚   β”œβ”€β”€ config.py               # Settings (env vars via pydantic)
β”‚   β”œβ”€β”€ auth/                   # JWT + cookie deps + password hashing
β”‚   β”œβ”€β”€ db/                     # MongoDB connection (motor async)
β”‚   β”œβ”€β”€ models/                 # User, signal, trade, bot pydantic models
β”‚   β”œβ”€β”€ routers/                # FastAPI route modules
β”‚   β”‚   β”œβ”€β”€ auth.py             # /api/auth/{signup,signin,me,logout}
β”‚   β”‚   β”œβ”€β”€ signals.py          # /api/signals/{live,...}
β”‚   β”‚   β”œβ”€β”€ tokens.py           # /api/tokens/{trending,movers,search,...}
β”‚   β”‚   β”œβ”€β”€ trades.py           # /api/trades/{active,history,pnl,open,close}
β”‚   β”‚   β”œβ”€β”€ bots.py             # /api/bots β€” CRUD for signal/copy bots
β”‚   β”‚   β”œβ”€β”€ telegram.py         # /api/telegram link + prefs
β”‚   β”‚   β”œβ”€β”€ balance.py          # /api/balance β€” paper wallet ops
β”‚   β”‚   β”œβ”€β”€ settings.py         # /api/settings β€” user preferences
β”‚   β”‚   β”œβ”€β”€ stream.py           # /api/stream/signals β€” SSE
β”‚   β”‚   β”œβ”€β”€ system.py           # /api/system/next-scan-at β€” countdown
β”‚   β”‚   β”œβ”€β”€ clusters.py         # /api/clusters
β”‚   β”‚   β”œβ”€β”€ wallets.py          # /api/wallets
β”‚   β”‚   β”œβ”€β”€ backtests.py        # /api/backtests
β”‚   β”‚   └── health.py
β”‚   β”œβ”€β”€ jobs/                   # asyncio background jobs
β”‚   β”‚   β”œβ”€β”€ ws_listener.py      # AVE WSS multi_tx β€” token-level swaps
β”‚   β”‚   β”œβ”€β”€ helius_stream.py    # Helius WSS β€” Solana wallet swaps
β”‚   β”‚   β”œβ”€β”€ wallet_poller.py    # BSCScan REST + SOL fallback
β”‚   β”‚   β”œβ”€β”€ price_listener.py   # Live token prices (AVE WSS)
β”‚   β”‚   β”œβ”€β”€ rank_poller.py      # AVE leaderboard scan every 5 min
β”‚   β”‚   β”œβ”€β”€ position_monitor.py # TP / SL / trailing stop enforcement
β”‚   β”‚   └── telegram_poller.py  # Inbound TG bot updates
β”‚   └── services/               # In-process event-bus consumers
β”‚       β”œβ”€β”€ ave_client.py       # ⭐ AVE Data API wrapper
β”‚       β”œβ”€β”€ event_bus.py        # asyncio pub/sub
β”‚       β”œβ”€β”€ contagion_detector.py # Cluster signal detection
β”‚       β”œβ”€β”€ signal_enricher.py  # Conviction scoring
β”‚       β”œβ”€β”€ bot_runner.py       # Opens paper trades for user bots
β”‚       β”œβ”€β”€ telegram_dispatcher.py # Fans signals out to TG
β”‚       β”œβ”€β”€ execution_engine.py # (legacy auto-trader, disabled)
β”‚       β”œβ”€β”€ exit_monitor.py
β”‚       β”œβ”€β”€ risk_checker.py
β”‚       β”œβ”€β”€ balance_ledger.py   # Paper-balance debit/credit
β”‚       β”œβ”€β”€ graph_builder.py    # Wallet cluster graph builder
β”‚       └── backtester.py
β”œβ”€β”€ scripts/                    # One-off CLI utilities (seed, dedupe, etc.)
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ Dockerfile
└── README.md

Setup

Prerequisites

Tool Version
Python 3.11
MongoDB β‰₯ 6 (local or Atlas)
AVE Data API key ave.ai
Helius API key helius.dev (optional but recommended)
BSCScan API key bscscan.com/apis (optional)
Telegram bot token @BotFather (optional)

Local install

git clone https://github.com/victorjayeoba/coven-backend.git
cd coven-backend

python3 -m venv .venv
source .venv/bin/activate          # Windows: .venv\Scripts\activate
pip install -r requirements.txt

cp .env.example .env               # then fill in your keys
uvicorn app.main:app --reload --port 8000

Boot logs you should see (in order):

[startup] graph index loaded: {'clusters_loaded': N, ...}
[telegram_dispatcher] registered
[ws_listener] starting
[price_listener] starting
[helius_stream] starting
[rank_poller] starting Β· interval=300s
INFO:     Application startup complete.

API is now live at http://localhost:8000 Β· interactive docs at http://localhost:8000/docs.

Environment variables

Variable Required Default Purpose
MONGO_URI βœ… mongodb://localhost:27017 Mongo connection string
MONGO_DB optional contagion Database name
JWT_SECRET βœ… (dev fallback) Signing key for session JWTs β€” change in prod
JWT_EXPIRE_MINUTES optional 1440 Session lifetime
AVE_API_KEY βœ… β€” Primary data feed
AVE_BASE_URL optional https://prod.ave-api.com/v2
AVE_WSS_URL optional wss://wss.ave-api.xyz
HELIUS_API_KEY recommended β€” Solana wallet WSS β€” without this copy bots can't track SOL wallets
HELIUS_BASE_URL optional https://api.helius.xyz
BSCSCAN_API_KEY recommended β€” BSC wallet polling
WALLET_POLL_INTERVAL_SECONDS optional 10 How often wallet_poller ticks
ENABLE_WALLET_POLLER optional false Force REST polling for SOL too (when WSS down)
ENABLE_TX_POLLER optional false Force per-pair tx REST fallback
TELEGRAM_BOT_TOKEN optional β€” Enables outbound TG alerts
TELEGRAM_BOT_USERNAME optional β€” For deep-link generation t.me/<bot>?start=<code>
CORS_ORIGINS optional http://localhost:3000 Comma-separated allowed origins
COOKIE_SECURE optional false Set true in production
COOKIE_SAMESITE optional lax none for cross-domain

Running with Docker

# Build
docker build -t coven-backend .

# Run (assumes Mongo on host)
docker run -d \
  --name coven-backend \
  -p 8084:8000 \
  --env-file .env \
  --add-host=host.docker.internal:host-gateway \
  --restart unless-stopped \
  --ulimit nofile=65536:65536 \
  coven-backend

The --ulimit flag is important β€” without it, the container hits "too many open files" under load (lots of concurrent WebSocket + httpx clients).

When using MONGO_URI=mongodb://host.docker.internal:27017 the --add-host flag lets the container reach a Mongo running on the host machine.

Background jobs

All jobs are started in app/main.py:lifespan and run as long-lived asyncio tasks. They communicate via the in-process event bus (no Redis, no broker β€” direct fanout, sub-millisecond latency).

Job Cadence What it does
helius_stream live (WSS) Subscribes to Helius logsSubscribe for every tracked Solana wallet. Fetches parsed swaps, normalizes to SWAP_EVENT.
ws_listener live (WSS) Subscribes to AVE multi_tx for ~145 trending + pump-phase tokens. Source of cluster signals.
price_listener live (WSS) Subscribes to AVE price/volume/tvl updates for the same token set. Drives live UI ticks + position marking.
wallet_poller every 10s REST fallback for BSC (no WSS equivalent). For Solana, only runs if helius_stream is disabled.
rank_poller every 5 min Polls 10+ AVE leaderboard topics + per-chain trending + pump-phase. Fires rank_stack signals when a token appears on β‰₯2 topics.
position_monitor every 10s Marks every open paper trade with current price (via AVE batch endpoint) and fires TP / SL / trailing-stop exits.
bot_position_monitor every 10s Same as above but for bot-created trades.
telegram_poller live (long-poll) Inbound TG bot updates β€” handles /start <code> linking and inline button callbacks (Buy / View).

API surface

All /api/* routes (except /api/health + /api/system/next-scan-at) require a session JWT in the coven_session HTTP-only cookie.

Auth

  • POST /api/auth/signup
  • POST /api/auth/signin
  • POST /api/auth/logout
  • GET /api/auth/me

Signals + tokens

  • GET /api/signals Β· list with filters (status, conviction, chain)
  • GET /api/signals/live?minutes=60 Β· feed for the dashboard table
  • GET /api/signals/{id} Β· single signal detail
  • GET /api/tokens/movers?chains=solana,bsc Β· merged momentum-ranked feed
  • GET /api/tokens/trending?chain=solana Β· raw AVE passthrough
  • GET /api/tokens/search?q=...&chain=...
  • GET /api/tokens/{token_id} Β· detail + market stats
  • GET /api/tokens/{token_id}/candles?interval=60
  • GET /api/tokens/{token_id}/risk Β· honeypot + ownership checks
  • POST /api/tokens/batch Β· batch token-detail lookup
  • GET /api/tokens/{token_id}/signals Β· all signals + backtests for a token

Trades + bots

  • GET /api/trades Β· everything for the user (signal + bot + manual)
  • GET /api/trades/active Β· open positions
  • GET /api/trades/history Β· closed positions
  • GET /api/trades/pnl Β· aggregate stats (win rate, best/worst, equity)
  • POST /api/trades/open Β· manual paper trade (used by Phantom-style swap)
  • POST /api/trades/{id}/close Β· manual close
  • GET /api/bots Β· user's bots
  • POST /api/bots Β· create signal or copy bot
  • PATCH /api/bots/{id} Β· update settings
  • POST /api/bots/{id}/{start|stop}
  • DELETE /api/bots/{id}
  • GET /api/bots/{id}/trades

Telegram + settings

  • GET /api/telegram/config
  • GET /api/telegram/status
  • POST /api/telegram/start-link Β· returns deep-link code
  • POST /api/telegram/unlink
  • PATCH /api/telegram/prefs
  • POST /api/telegram/test
  • GET /api/settings
  • PATCH /api/settings

Balance + system

  • GET /api/balance Β· paper balances per chain
  • POST /api/balance/deposit
  • POST /api/balance/reset
  • GET /api/system/next-scan-at Β· public β€” countdown timer source

Realtime

  • GET /api/stream/signals Β· SSE feed:
    • signal.fired Β· signal.scored
    • price.update Β· swap
    • bot.trade.opened Β· bot.trade.closed Β· bot.updated

Interactive Swagger docs: http://localhost:8000/docs

Event bus

Internal pub/sub for service decoupling. Located at app/services/event_bus.py. Topics:

Event Published by Subscribers
SWAP_EVENT ws_listener, helius_stream, wallet_poller contagion_detector, bot_runner (copy bots)
SIGNAL_FIRED contagion_detector, rank_poller signal_enricher, SSE stream
SIGNAL_SCORED signal_enricher, rank_poller bot_runner (signal bots), telegram_dispatcher, SSE stream
PRICE_UPDATE price_listener position_monitor, SSE stream
BOT_TRADE_OPENED bot_runner telegram_dispatcher, SSE stream
BOT_TRADE_CLOSED bot_runner telegram_dispatcher, SSE stream
BOT_UPDATED routers/bots.py SSE stream
TRADE_OPENED / TRADE_CLOSED (legacy execution engine, disabled) SSE stream

Deployment

Currently running on a single VPS via Docker:

  • Backend: coven-api.discretliaison.com β†’ Cloudflare β†’ reverse proxy β†’ Docker container on port 8084.
  • MongoDB: separate Docker container on the same host.
  • Frontend: Vercel, calls backend via Next.js rewrite proxy (BACKEND_URL env var).

Update workflow

cd ~/coven-backend && git pull && \
docker build -t coven-backend . && \
docker stop coven-backend && docker rm coven-backend && \
docker run -d --name coven-backend -p 8084:8000 \
  --env-file .env --add-host=host.docker.internal:host-gateway \
  --restart unless-stopped --ulimit nofile=65536:65536 \
  coven-backend && \
docker logs -f coven-backend

Watch for [startup] graph index loaded then Ctrl+C to detach.

Common operations

# Tail live logs
docker logs -f coven-backend

# Drop into the container shell
docker exec -it coven-backend /bin/bash

# Restart without rebuilding
docker restart coven-backend

# Mongo CLI from the host
docker exec -it mongodb mongosh contagion

# Clear all signals (useful after schema changes)
docker exec -it mongodb mongosh contagion --eval "db.signals.deleteMany({})"

# Manually trigger the dedupe cleanup
curl -X POST http://localhost:8084/api/signals/dedupe \
  -H "Cookie: coven_session=..."

Troubleshooting

Symptom Likely cause Fix
[Errno 24] Too many open files Default ulimit too low for the WSS + httpx workload Add --ulimit nofile=65536:65536 to docker run
[rank_poller] DIAG NO OVERLAP AVE leaderboards returning ETH/Base tokens, no SOL/BSC overlap Wait β€” usually resolves on next 5-min poll, or expand TOPIC_HINTS in rank_poller.py
[helius_stream] connection closed Helius free tier rate limit or WSS hiccup Auto-reconnects after 5s β€” safe to ignore unless persistent
401 Unauthorized from frontend Cookie not being sent (cross-domain) Frontend should call /api/* via same-domain proxy (see frontend next.config.js)
Bot trades all close at -100% instantly AVE returns 0 price for unindexed pump.fun tokens, position_monitor fires false stop loss Use a higher liquidity_min on the bot, or skip TP/SL for the first 30s
Signals fire but no Telegram alerts User's conviction_threshold higher than the signal score Lower threshold in /settings (default 70)
[telegram_dispatcher] bot token missing TELEGRAM_BOT_TOKEN env var not set Set it and restart β€” alerts are silently disabled without it

License

MIT β€” built for the AVE Γ— CLAW Hackathon. Credit Coven if you reuse the code.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages