# mrb1nary/solana-trading-bot architecture

> Architecture diagram of the GitHub repository mrb1nary/solana-trading-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/mrb1nary/solana-trading-bot
- Repository: https://github.com/mrb1nary/solana-trading-bot

## Overview

##### Main Components
- Environment & User Configuration:
- Configuration layer where the user provides necessary parameters (Twitter usernames, Solana private keys, API keys, etc.). Indicate the use of an .env file (copied from .env.example).
- Tweet Scraping Module:
- This component fetches and monitors tweets from designated Twitter accounts (e.g., implemented in src/fetchTweet.ts).
- AI Analysis Module:
- This module uses a language model (LLM) to analyze tweet content and extract token addresses, token symbols, and market sentiment (implemented in src/fetchLLM.ts).
- Token Resolution / Swap Module:
- Logic that checks if a token address is directly found or if additional resolution is needed (switching between direct trade and symbol-based resolution). This includes functionality likely found in both swapToken and parts of the fetching modules.
- Trade Execution Module:
- A dedicated component to interface with the Raydium API to query token details and execute trades (as indicated in the “Trade Execution” in the README). This is integrated into the overall logic flow.
- API & External Integration Layer:
- External services like Twitter (for tweet scraping), the LLM service (for analysis), and Raydium’s API (for trading operations).

##### Relationships & Data Flow
- The process begins with User Configuration feeding into the Tweet Scraping Module.
- The AI Analysis Module then performs token detection:
- On a “Yes” branch (direct token address found), it proceeds directly to trade execution.
- On a “No” branch (address not found), it goes through a symbol extraction and token resolution process before proceeding.
- Finally, after address resolution, the data flows into the Trade Execution Module, which interacts with the Raydium API.
- Additionally, a feedback loop or confirmation might be present for trade status reporting.

##### Architectural Patterns & Design Principles
- Modularity is key – each major functionality is in its own module (e.g., tweet fetching, AI analysis, token resolution, trade execution) making it easier to isolate concerns and extend functionalities.
- Clear Separation of Concerns: Dividing the system into configuration, data acquisition, processing/analysis, decision-making, and execution layers.
- Use of asynchronous operations likely given external API calls (Twitter API, Raydium API, LLM calls).

##### Relevant Technologies & Frameworks
- Node.js & TypeScript (noted by tsconfig.json, src folder).
- External API integrations for Twitter and Raydium.
- AI integration leveraging a Language Learning Model (LLM) which might be built-in or use an external service.

- Configuration Management (.env)
- Tweet Scraper (src/fetchTweet.ts)
- AI Analysis Module (src/fetchLLM.ts)
- Token Resolution / Swap Module (src/swapToken.ts)
- Trade Executor (integrated logic in src/index.ts or within the swap module)
- From configuration, to tweet scraping, passing tweet data to the AI module.
- From AI module, branch conditions leading either directly to the trade execution or to a token resolution process then to trade execution.
- Optionally, include the external services as separate nodes outside of the main system to show integration (e.g., Twitter API, Raydium API, LLM service).
- Use annotations or brief notes to indicate where critical design decisions or error handling should be implemented (like secure handling of private keys).

## Diagram (Mermaid)

```mermaid
graph TD
    %% Configuration Layer
    subgraph "Configuration Layer"
        CONFIG[".env Configuration"]:::config
    end

    %% Backend Modules
    subgraph "Backend Modules"
        TS["Tweet Scraper (src/fetchTweet.ts)"]:::internal
        LLM["AI Analysis Module (src/fetchLLM.ts)"]:::internal
        TOKEN["Token Resolution / Swap Module (src/swapToken.ts)"]:::internal
        TRADE["Trade Executor (src/index.ts)"]:::internal
    end

    %% External APIs
    subgraph "External APIs"
        TWITTER["Twitter API"]:::external
        LLM_SVC["LLM Service"]:::external
        RAYDIUM["Raydium API"]:::external
    end

    %% Data Flow
    CONFIG -->|"UserConfig"| TS
    TS -->|"TweetData"| LLM
    TS -.->|"FetchTweets"| TWITTER
    LLM -->|"LLMCall"| LLM_SVC
    LLM -->|"DirectToken?"| TRADE
    LLM -->|"ResolutionNeeded?"| TOKEN
    TOKEN -->|"ResolvedToken"| TRADE
    TRADE -->|"TradeQuery"| RAYDIUM
    TRADE -->|"TradeConfirmation"| TWITTER
    TRADE -->|"Feedback"| CONFIG

    %% Styles
    classDef config fill:#ffcccc,stroke:#cc0000,stroke-width:2px;
    classDef internal fill:#ccffcc,stroke:#008800,stroke-width:2px;
    classDef external fill:#cce5ff,stroke:#0055cc,stroke-width:2px;

    %% Click Events
    click CONFIG "https://github.com/mrb1nary/solana-trading-bot/blob/main/.env.example"
    click TS "https://github.com/mrb1nary/solana-trading-bot/blob/main/src/fetchTweet.ts"
    click LLM "https://github.com/mrb1nary/solana-trading-bot/blob/main/src/fetchLLM.ts"
    click TOKEN "https://github.com/mrb1nary/solana-trading-bot/blob/main/src/swapToken.ts"
    click TRADE "https://github.com/mrb1nary/solana-trading-bot/blob/main/src/index.ts"
```
