Skip to main content

Overview

Autonome uses PostgreSQL with Drizzle ORM to manage trading data, model invocations, and portfolio history. The schema is designed with these critical principles:
  • Quoted identifiers: Table names use PascalCase (e.g., "Models", "Orders")
  • TEXT for money: Store monetary values as TEXT, cast to NUMERIC for calculations
  • UUID generation: Primary keys use crypto.randomUUID() for consistency
  • Orders table is single source of truth: OPEN = active positions, CLOSED = completed trades

Tables

Models

Stores AI model configurations and runtime statistics.
Key Fields:
  • id: UUID primary key
  • name: Model display name (e.g., “GPT-4”, “Claude”)
  • variant: Trading strategy variant (Apex, Trendsurfer, Contrarian, Sovereign)
  • lighterApiKey: Account index for Lighter exchange API
  • invocationCount: Total number of AI invocations
  • totalMinutes: Cumulative runtime across all invocations
Unique Constraint: Each model can have up to 5 variants (one per strategy). The unique index on (name, variant) enforces this.

Invocations

Records each AI model invocation with the response and payload.
Key Fields:
  • id: UUID generated via randomUUID()
  • modelId: Foreign key to Models table
  • response: Raw AI response text
  • responsePayload: JSONB containing full AI SDK response structure

ToolCalls

Tracks tool invocations made by the AI during trading decisions.
Tool Call Types:
  • CREATE_POSITION: AI opened a new trade
  • CLOSE_POSITION: AI closed an existing position
  • HOLDING: AI decided to hold (no action)

PortfolioSize

Time-series data tracking portfolio net asset value (NAV) over time.
Critical Rules:
  • Store as NUMERIC in DB for precision
  • Use TEXT in application code, cast to NUMERIC for calculations
  • Multiple indexes support efficient time-range queries and aggregations

Orders (Single Source of Truth)

The Orders table is the single source of truth for all positions and trades.
Status Enum:
Key Concepts:
  1. OPEN vs CLOSED:
    • OPEN: Active position (appears in Positions tab)
    • CLOSED: Completed trade (appears in Trades tab)
  2. Derived Values (NOT stored):
    • entryNotional = quantity * entryPrice
    • exitNotional = quantity * exitPrice
    • Unrealized P&L calculated live from current market price
  3. Exit Plan Structure:
  4. Real SL/TP Orders:
    • slOrderIndex / tpOrderIndex: Exchange order IDs for real stop-loss/take-profit
    • slTriggerPrice / tpTriggerPrice: Actual trigger prices set on exchange
    • Used for live trading mode (not simulator)

Enums

Variant Enum

Trading strategy variants (derived from SSOT in @/core/shared/variants):

Relations

Drizzle ORM relations enable easy joins:

Type Exports

Generate TypeScript types from schema:

Critical Schema Rules

1. Quoted Identifiers

PostgreSQL requires quoting for capitalized identifiers:

2. TEXT for Money

Store money as TEXT in application code, cast to NUMERIC for DB operations:
Why? JavaScript number precision issues. TEXT preserves exact decimal values.

3. UUID Generation

Use crypto.randomUUID() consistently:

4. Timestamps

All tables with user data have createdAt and updatedAt:
Manually set updatedAt in update queries:

Next Steps