# bottoproject/sr-auction-bot architecture

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

- Interactive diagram: https://gitdiagram.com/bottoproject/sr-auction-bot
- Repository: https://github.com/bottoproject/sr-auction-bot

## Overview

##### Main Components
- Serverless Backend: Represent the three Supabase Edge Functions – “fetch-events”, “process-events”, and “send-discord”. Each function plays a different role:
- fetch-events: Polls the SuperRare GraphQL API for new auction events.
- process-events: Processes and transforms raw events stored in the database.
- send-discord: Dispatches notifications to Discord.
- Database Layer: Include the Supabase PostgreSQL database that stores event data along with tables from the migrations (e.g., auction events, CRON logs).
- Scheduler: Represent the CRON jobs (configured via SQL scripts) that trigger the edge functions on a scheduled interval (every minute).
- External Services:
- SuperRare GraphQL API: External API providing auction event data.
- Discord: Represent both the Discord Bot (using the bot token) and/or Discord Webhooks for sending messages.

##### Interactions and Flow
- The “fetch-events” function makes an HTTP request to the SuperRare GraphQL API, retrieves auction events, and then writes those events into the PostgreSQL database.
- The “process-events” function is triggered (either via CRON or chained internally) to read from the database, process or format the events, and prepare notifications.
- The “send-discord” function is subsequently triggered; it picks up the processed data and sends notifications to Discord via the Discord Bot or Webhook.

##### Architectural Patterns & Design Principles
- Serverless Architecture: Emphasize the use of Supabase Edge Functions for scalability.
- Separation of Concerns: Each edge function has a clear, singular responsibility.
- Event-Driven Processing: The scheduler triggers functions based on events (time-based CRON schedule) ensuring reactive behavior.
- Secure Handling of Secrets: Note how sensitive information (like Discord tokens and API keys) is passed as environment variables and through a secured setup process.

##### Technologies
- Node.js environment for scripts (init, deploy, secret setup).
- Supabase (for both edge functions and PostgreSQL).
- CRON jobs, likely implemented using pg_cron for scheduling in Supabase.
- Discord platform (via Bot tokens or Webhooks).
- GraphQL communication with the SuperRare API.

##### Additional Details
- That each component is clearly labeled with its purpose (e.g., “Data Fetching”, “Event Processing”, “Notification Dispatch”).
- Use annotations or callouts to explain where configuration files enter the process (e.g., .env files, config.toml).
- For clarity, include notes about the secure handling of secrets, routing of environment variables, and the role of CRON-based scheduling.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Scheduler Subgraph
    subgraph "Scheduler"
        S1["SQL Scheduler Template"]:::scheduler
        S2["Scheduler Config"]:::scheduler
        SCH["CRON Scheduler"]:::scheduler
    end

    %% Edge Functions Subgraph
    subgraph "Supabase Edge Functions"
        FE["fetch-events (Polls GraphQL API)"]:::backend
        PE["process-events (Processes Events)"]:::backend
        SD["send-discord (Sends Notifications)"]:::backend
    end

    %% Database Layer
    subgraph "Database"
        DB["PostgreSQL Database"]:::database
    end

    %% External Services Subgraph
    subgraph "External Services"
        SR["SuperRare GraphQL API"]:::external
        DIS["Discord (Bot/Webhook)"]:::external
    end

    %% Utility Scripts Subgraph
    subgraph "Utility Scripts"
        DS["Deployment Script"]:::utility
        IS["Initialization Script"]:::utility
        DTS["Discord Setup Script"]:::utility
    end

    %% Connections
    S1 --> SCH
    S2 --> SCH
    SCH --> FE
    FE -->|"HTTP_Request"| SR
    FE -->|"Store_Event"| DB
    DB -->|"Read_Event"| PE
    PE -->|"Trigger_Notification"| SD
    SD -->|"Send_Message"| DIS

    %% Click Events for Supabase Edge Functions
    click FE "https://github.com/bottoproject/sr-auction-bot/blob/main/supabase/functions/fetch-events/index.ts"
    click PE "https://github.com/bottoproject/sr-auction-bot/blob/main/supabase/functions/process-events/index.ts"
    click SD "https://github.com/bottoproject/sr-auction-bot/blob/main/supabase/functions/send-discord/index.ts"

    %% Click Events for Scheduler/CRON Jobs Configuration
    click S1 "https://github.com/bottoproject/sr-auction-bot/blob/main/scripts/setup-scheduler.sql.template"
    click S2 "https://github.com/bottoproject/sr-auction-bot/blob/main/supabase/functions/scheduler.json"

    %% Click Event for Database Migration
    click DB "https://github.com/bottoproject/sr-auction-bot/blob/main/supabase/migrations/20240314_initial_schema.sql"

    %% Click Events for Deployment and Setup Scripts
    click DS "https://github.com/bottoproject/sr-auction-bot/blob/main/scripts/deploy.js"
    click IS "https://github.com/bottoproject/sr-auction-bot/blob/main/scripts/init.js"
    click DTS "https://github.com/bottoproject/sr-auction-bot/blob/main/scripts/setup-discord.js"

    %% Styles
    classDef backend fill:#BBDEFB,stroke:#0D47A1,stroke-width:2px;
    classDef database fill:#C8E6C9,stroke:#2E7D32,stroke-width:2px;
    classDef external fill:#FFCC80,stroke:#E65100,stroke-width:2px;
    classDef scheduler fill:#E1BEE7,stroke:#6A1B9A,stroke-width:2px;
    classDef utility fill:#F0F4C3,stroke:#8BC34A,stroke-width:2px;
```
