# axisprotocol/axis-trigger architecture

> Architecture diagram of the GitHub repository axisprotocol/axis-trigger, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-11-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/axisprotocol/axis-trigger
- Repository: https://github.com/axisprotocol/axis-trigger

## Overview

##### Project Type and Purpose
- This is a backend microservice (full-stack in the sense of API + scheduler) that runs on Cloudflare Workers.
- It computes Free-Float Adjusted Market Capitalization (FAMC) for configured crypto assets, stores price history in PostgreSQL via Prisma, and exposes TradingView-compatible REST endpoints plus OpenAPI docs.
- It also has cron-backed scheduled jobs (Trigger.dev v4) for backfilling and updating data.

##### High-Level File Structure and Architectural Patterns
- Top-level deployment/config:
- wrangler.toml (Cloudflare Worker config)
- trigger.config.ts / triggers/*.ts (Trigger.dev scheduling)
- .env/.example.env, trigger and cron env variables
- Persistence layer:
- prisma/schema.prisma + migrations/… → PostgreSQL via Prisma Client
- src/db/{client.ts, d1Client.ts, prismaD1.ts, types.ts}
- Data ingestion providers:
- src/providers/bitquery/…
- src/providers/coingecko/…
- src/providers/envAssets.ts (static JSON or env fallback)
- Core business logic:
- src/lib/settlementProcessor.ts
- src/lib/settlementStore.ts
- src/lib/solana.ts
- API routes (REST & TradingView UDF):
- src/routes/api/*.ts
- src/routes/tv/*.ts
- src/routes/index.ts
- Scheduled tasks & scripts:
- src/scheduled/*.ts (Trigger.dev jobs)
- src/scripts/*.ts (manual backfill, migrations, asset updates)
- Server entrypoint: src/server.ts
- Config & utilities: src/config.ts, src/utils/*.ts

a. Infrastructure Layer
- Cloudflare Workers
- Trigger.dev scheduler (cron)
- PostgreSQL database (hosted externally)
b. Application Layer (inside Workers)
- API Gateway (exposes OpenAPI docs & REST endpoints)
- TradingView UDF adapter
- Business Logic Modules (settlementProcessor, settlementStore, solana)
- Data Providers (Coingecko, Bitquery, envAssets)
- Prisma Client for DB interactions
c. Scheduled Jobs
- Trigger.dev Cron trigger → invokes update & backfill code paths
- Scripts for on-demand backfill and migrations
d. External Services
- CoinGecko API
- Bitquery API
- Solana RPC (if used by solana.ts)

##### Relationships and Data Flows
- Scheduler → (cron) → invokes scheduled job code → uses Data Providers → writes/reads via Prisma → PostgreSQL
- API Client → Cloudflare Worker API Layer → routes to business logic → Prisma → PostgreSQL → returns JSON/UDF responses
- Asset configuration path: env or bundled assets.json → loaded at startup
- Manual scripts → run via CLI (pnpm) → update assets.json or backfill DB

##### Architectural Patterns & Design Principles
- Layered architecture: separation of API, business logic, data access, and external integrations
- Dependency Injection via config.ts (env-driven)
- Cron scheduling decoupled using Trigger.dev declarative jobs
- Provider abstraction for external APIs (Coingecko, Bitquery)
- Single entrypoint (server.ts) for both API and Worker compat

##### Essential Elements to Capture
- Cloudflare Workers runtime (wrangler.toml & server.ts)
- Trigger.dev cron jobs (trigger.config.ts, src/scheduled)
- Prisma schema & migrations → PostgreSQL
- External data providers & their abstraction layer
- API routes vs. TV/TradingView UDF endpoints
- Manual scripts for backfill/migrations

## Diagram (Mermaid)

```mermaid
flowchart TD
  %% Infrastructure Layer
  subgraph "Infrastructure Layer"
    direction TB
    CFConfig["wrangler.toml"]:::infra
    CFServer["src/server.ts"]:::infra
    TRConfig["trigger.config.ts"]:::scheduler
    TRSchedule1["src/scheduled/settlementProcessor.ts"]:::scheduler
    TRSchedule2["src/scheduled/update.ts"]:::scheduler
    DBSchema["prisma/schema.prisma"]:::db
    DBM1["migrations/0001_auto_1758105361999.sql"]:::db
    DBM2["migrations/0002_auto_1758159610354.sql"]:::db
    DBM3["migrations/0003_auto_1761658122000.sql"]:::db
    DBClient["src/db/client.ts"]:::db
    DBD1Client["src/db/d1Client.ts"]:::db
    DBPrismaD1["src/db/prismaD1.ts"]:::db
    DBTypes["src/db/types.ts"]:::db
  end

  %% Application Layer
  subgraph "Application Layer"
    direction TB
    API["API Gateway\n(src/routes)"]:::app
    BLProcessor["src/lib/settlementProcessor.ts"]:::app
    BLStore["src/lib/settlementStore.ts"]:::app
    BLSolana["src/lib/solana.ts"]:::app
    DPEnv["src/providers/envAssets.ts"]:::app
    DPBQClient["src/providers/bitquery/client.ts"]:::app
    DPBQHelpers["src/providers/bitquery/helpers.ts"]:::app
    DPBQIndex["src/providers/bitquery/index.ts"]:::app
    DPCGHeaders["src/providers/coingecko/cgHeaders.ts"]:::app
    DPCGMain["src/providers/coingecko/coingecko.ts"]:::app
    DPCGRange["src/providers/coingecko/coingeckoRange.ts"]:::app
    PPClient["Prisma Client\n(src/db)"]:::app
  end

  %% Manual Scripts
  subgraph "Manual Scripts & Tools"
    direction TB
    ScriptBackfill["src/scripts/backfill.ts"]:::scripts
    ScriptMigrate["src/scripts/d1Migrate.ts"]:::scripts
    ScriptAssetsBQ["src/scripts/updateAssetsFromBitquery.ts"]:::scripts
    ScriptAssetsCG["src/scripts/updateAssetsFromCG.ts"]:::scripts
    ConfigFile["src/config.ts"]:::scripts
    TypesFile["src/types.ts"]:::scripts
    AssetsJSON["src/assets.json"]:::scripts
    UtilsFAMC["src/utils/famc.ts"]:::scripts
    UtilsIndexFormula["src/utils/indexFormula.ts"]:::scripts
    UtilsIndexSeries["src/utils/indexSeries.ts"]:::scripts
    UtilsKV["src/utils/kvCache.ts"]:::scripts
    UtilsLiquidity["src/utils/liquidity.ts"]:::scripts
    UtilsMeta["src/utils/meta.ts"]:::scripts
  end

  %% External Services
  subgraph "External Services"
    direction TB
    CG["CoinGecko API"]:::external
    BQ["Bitquery API"]:::external
    SOL["Solana RPC"]:::external
  end

  %% Relationships
  TRConfig -->|cron| TRSchedule1
  TRConfig -->|cron| TRSchedule2
  TRSchedule1 -->|uses| BLProcessor
  TRSchedule2 -->|uses| BLProcessor

  API -->|routes to| BLProcessor
  API --> BLStore
  BLProcessor -->|reads/writes| PPClient
  BLStore -->|reads/writes| PPClient
  BLSolana -->|optional| SOL
  BLProcessor -->|calls| DPEnv
  BLProcessor -->|calls| DPBQClient
  BLProcessor -->|calls| DPCGMain

  PPClient -->|ORM| DBSchema
  DBClient --> DBSchema
  DBD1Client --> DBSchema
  DBPrismaD1 --> DBSchema

  DPBQClient -->|http| BQ
  DPCGMain -->|http| CG

  ScriptBackfill -->|CLI| BLProcessor
  ScriptMigrate -->|CLI| DBD1Client
  ScriptAssetsBQ -->|CLI| DPBQClient
  ScriptAssetsCG -->|CLI| DPCGMain

  %% Click Events
  click CFConfig "https://github.com/axisprotocol/axis-trigger/blob/main/wrangler.toml"
  click CFServer "https://github.com/axisprotocol/axis-trigger/blob/main/src/server.ts"
  click TRConfig "https://github.com/axisprotocol/axis-trigger/blob/main/trigger.config.ts"
  click TRSchedule1 "https://github.com/axisprotocol/axis-trigger/blob/main/src/scheduled/settlementProcessor.ts"
  click TRSchedule2 "https://github.com/axisprotocol/axis-trigger/blob/main/src/scheduled/update.ts"
  click DBSchema "https://github.com/axisprotocol/axis-trigger/blob/main/prisma/schema.prisma"
  click DBM1 "https://github.com/axisprotocol/axis-trigger/blob/main/migrations/0001_auto_1758105361999.sql"
  click DBM2 "https://github.com/axisprotocol/axis-trigger/blob/main/migrations/0002_auto_1758159610354.sql"
  click DBM3 "https://github.com/axisprotocol/axis-trigger/blob/main/migrations/0003_auto_1761658122000.sql"
  click DBClient "https://github.com/axisprotocol/axis-trigger/blob/main/src/db/client.ts"
  click DBD1Client "https://github.com/axisprotocol/axis-trigger/blob/main/src/db/d1Client.ts"
  click DBPrismaD1 "https://github.com/axisprotocol/axis-trigger/blob/main/src/db/prismaD1.ts"
  click DBTypes "https://github.com/axisprotocol/axis-trigger/blob/main/src/db/types.ts"
  click DPBQClient "https://github.com/axisprotocol/axis-trigger/blob/main/src/providers/bitquery/client.ts"
  click DPBQHelpers "https://github.com/axisprotocol/axis-trigger/blob/main/src/providers/bitquery/helpers.ts"
  click DPBQIndex "https://github.com/axisprotocol/axis-trigger/blob/main/src/providers/bitquery/index.ts"
  click DPCGHeaders "https://github.com/axisprotocol/axis-trigger/blob/main/src/providers/coingecko/cgHeaders.ts"
  click DPCGMain "https://github.com/axisprotocol/axis-trigger/blob/main/src/providers/coingecko/coingecko.ts"
  click DPCGRange "https://github.com/axisprotocol/axis-trigger/blob/main/src/providers/coingecko/coingeckoRange.ts"
  click DPEnv "https://github.com/axisprotocol/axis-trigger/blob/main/src/providers/envAssets.ts"
  click BLProcessor "https://github.com/axisprotocol/axis-trigger/blob/main/src/lib/settlementProcessor.ts"
  click BLStore "https://github.com/axisprotocol/axis-trigger/blob/main/src/lib/settlementStore.ts"
  click BLSolana "https://github.com/axisprotocol/axis-trigger/blob/main/src/lib/solana.ts"
  click ScriptBackfill "https://github.com/axisprotocol/axis-trigger/blob/main/src/scripts/backfill.ts"
  click ScriptMigrate "https://github.com/axisprotocol/axis-trigger/blob/main/src/scripts/d1Migrate.ts"
  click ScriptAssetsBQ "https://github.com/axisprotocol/axis-trigger/blob/main/src/scripts/updateAssetsFromBitquery.ts"
  click ScriptAssetsCG "https://github.com/axisprotocol/axis-trigger/blob/main/src/scripts/updateAssetsFromCG.ts"
  click ConfigFile "https://github.com/axisprotocol/axis-trigger/blob/main/src/config.ts"
  click TypesFile "https://github.com/axisprotocol/axis-trigger/blob/main/src/types.ts"
  click AssetsJSON "https://github.com/axisprotocol/axis-trigger/blob/main/src/assets.json"
  click UtilsFAMC "https://github.com/axisprotocol/axis-trigger/blob/main/src/utils/famc.ts"
  click UtilsIndexFormula "https://github.com/axisprotocol/axis-trigger/blob/main/src/utils/indexFormula.ts"
  click UtilsIndexSeries "https://github.com/axisprotocol/axis-trigger/blob/main/src/utils/indexSeries.ts"
  click UtilsKV "https://github.com/axisprotocol/axis-trigger/blob/main/src/utils/kvCache.ts"
  click UtilsLiquidity "https://github.com/axisprotocol/axis-trigger/blob/main/src/utils/liquidity.ts"
  click UtilsMeta "https://github.com/axisprotocol/axis-trigger/blob/main/src/utils/meta.ts"

  %% Styles
  classDef infra fill:#D0E8FF,stroke:#0366d6,color:#0366d6;
  classDef scheduler fill:#FFE5B4,stroke:#FF8C00,color:#FF8C00;
  classDef db fill:#DFFFD6,stroke:#228B22,color:#228B22;
  classDef app fill:#E8F8FF,stroke:#1E90FF,color:#1E90FF;
  classDef scripts fill:#FFF4E5,stroke:#FFA500,color:#FFA500;
  classDef external fill:#FFF9C4,stroke:#FFEB3B,color:#F57F17;
```
