Architecture Overview

MangroveAI follows a layered architecture with clear separation of concerns. Four foundational patterns are applied consistently across all domains.

Three-Layer Architecture

Routes Layer (HTTP)          Flask-RESTX, request/response
       |
Service Layer (Logic)        Business logic, orchestration
       |
Data Layer (Database)        SQL, database connections

Routes Layer

  • Parses and validates request payloads
  • Maps HTTP inputs to domain request models
  • Calls the service layer
  • Returns standardized responses
  • Does NOT contain business logic or database calls

Service Layer

  • Implements domain logic and orchestration
  • Coordinates between multiple data layers when needed
  • Does NOT contain raw SQL
  • Uses the data layer for persistence

Data Layer (Repository / DAO)

  • Encapsulates SQL and database connections
  • Returns domain entities or simple DTOs
  • Does NOT call external APIs or LLMs

Active Domains

DomainDescription
ai_copilotConversation management and AI workflow
authAuthentication and authorization
backtestingHistorical backtest execution and metrics
batchBatch processing operations
crypto_assetsCrypto asset risk scoring and data
indicators40+ technical indicator classes
managersTrading components and execution
organizationsMulti-tenant org management
positionsPersistence for positions, orders, trades
signals136 trading signals (TRIGGER/FILTER)
strategiesStrategy management and loading

Dependency Injection

Services receive their dependencies (data layers, API clients) through constructor injection. This enables:
  • Unit testing with mocked dependencies
  • Swapping implementations (e.g., local vs external knowledge base)
  • Clear dependency graphs between components

Error Handling

All endpoints return standardized error responses:
{
  "error": "ERROR_CODE",
  "message": "Human-readable description",
  "code": "SPECIFIC_CODE"
}
Error categories:
  • VALIDATION_ERROR (400) — Invalid input
  • UNAUTHORIZED (401) — Missing or invalid auth
  • AUTHORIZATION_ERROR (403) — Insufficient permissions
  • RESOURCE_NOT_FOUND (404) — Entity not found
  • SERVICE_ERROR (500) — Internal failure

External Dependencies

ServicePurpose
PostgreSQL + pgvectorPrimary database with vector search
OpenAI APILLM for AI Copilot and RAG embeddings
CoinAPIHistorical market data for backtesting
CoinGeckoMarket cap, volume, trending data
NansenOn-chain analytics and smart money tracking
Firebase AuthUser authentication (Google OAuth)
Knowledge Base (FastAPI)Trading education content and search