# sammchardy/python-binance architecture

> Architecture diagram of the GitHub repository sammchardy/python-binance, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sammchardy/python-binance
- Repository: https://github.com/sammchardy/python-binance

## Overview

##### Project Type and Core Components

a. Core Client Layer
- Synchronous Client
- Asynchronous Client
- Base Client (shared functionality)

b. WebSocket Layer
- WebSocket Manager
- Depth Cache Manager
- Threaded implementations

c. API Integration Layer
- REST API endpoints
- WebSocket API endpoints
- Authentication handling

**[Client Applications]**
- ↓
**[Python-Binance Library]**
- ↙     ↓     ↘
**[REST] [WebSocket] [Depth Cache]**
- ↘     ↓     ↙
**[Binance Exchange API]**

##### Detailed Component Breakdown

- Blue: Core client components
- Green: WebSocket-related components
- Yellow: API integration layers
- Gray: External systems (Binance API)

##### Specific Components

a. Client Layer:
**[Base Client]**
- ↙     ↘
**[Client] [AsyncClient]**

b. WebSocket Layer:
**[WebSocket Manager]**
- ↙     ↘
**[ThreadedWebSocketManager] [BinanceSocketManager]**

c. Depth Cache:
**[DepthCache Manager]**
- ↙     ↘
**[ThreadedDepthCache] [AsyncDepthCache]**

##### Important Features to Highlight

- Market Data Operations
- Account Management
- Order Operations
- WebSocket Streams
- Authentication Methods

##### Technical Details

Annotations for:
- Supported Python versions
- Key dependencies
- Authentication methods (API keys, RSA, EDDSA)
- Reconnection handling
- Thread management

##### Integration Points

External integration points:
- REST API endpoints
- WebSocket connections
- Different Binance domains (US, JP, etc.)
- Testnet support

##### Data Flow Patterns

Typical data flows:
- Market data streaming
- Order creation/management
- Depth cache updates
- Authentication flow

##### Implementation Notes

Notes regarding:
- Async/await support
- Thread safety
- Error handling
- Rate limiting
- Proxy support

This system design should effectively communicate how the library provides a clean interface to interact with Binance's various APIs while handling the complexities of authentication, websocket management, and data streaming.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client_Applications
        CA[Client Applications]
    end

    subgraph Python_Binance_Library
        subgraph Core_Clients
            BC["Base Client"]:::core
            SC["Synchronous Client"]:::core
            AC["Async Client"]:::core
            BC --> SC
            BC --> AC
        end

        subgraph WebSocket_Layer
            WSM["WebSocket Manager"]:::ws
            TWS["Threaded WebSocket"]:::ws
            BSM["Binance Socket Manager"]:::ws
            WSM --> TWS
            WSM --> BSM
            
            subgraph WebSocket_Components
                WS["WebSocket Connection"]:::ws
                KA["Keep-Alive Manager"]:::ws
                ST["Stream Handler"]:::ws
            end
        end

        subgraph Cache_Management
            DCM["Depth Cache Manager"]:::cache
            TDC["Threaded Depth Cache"]:::cache
            ADC["Async Depth Cache"]:::cache
            DCM --> TDC
            DCM --> ADC
        end

        subgraph Support_Components
            EH["Exception Handler"]:::support
            HF["Helper Functions"]:::support
            CN["Constants & Enums"]:::support
        end
    end

    subgraph Binance_API
        REST["REST API"]:::external
        WSA["WebSocket API"]:::external
    end

    CA --> SC
    CA --> AC
    SC --> REST
    AC --> REST
    WSM --> WSA
    DCM --> WSA

    %% Click events for component mapping
    click BC "https://github.com/sammchardy/python-binance/blob/master/binance/base_client.py"
    click SC "https://github.com/sammchardy/python-binance/blob/master/binance/client.py"
    click AC "https://github.com/sammchardy/python-binance/blob/master/binance/async_client.py"
    click WSM "https://github.com/sammchardy/python-binance/blob/master/binance/ws/websocket_api.py"
    click DCM "https://github.com/sammchardy/python-binance/blob/master/binance/ws/depthcache.py"
    click TWS "https://github.com/sammchardy/python-binance/blob/master/binance/ws/threaded_stream.py"
    click WS "https://github.com/sammchardy/python-binance/blob/master/binance/ws/reconnecting_websocket.py"
    click KA "https://github.com/sammchardy/python-binance/blob/master/binance/ws/keepalive_websocket.py"
    click ST "https://github.com/sammchardy/python-binance/blob/master/binance/ws/streams.py"
    click CN "https://github.com/sammchardy/python-binance/blob/master/binance/enums.py"
    click EH "https://github.com/sammchardy/python-binance/blob/master/binance/exceptions.py"
    click HF "https://github.com/sammchardy/python-binance/blob/master/binance/helpers.py"

    classDef core fill:#2374ab,stroke:#000,stroke-width:2px;
    classDef ws fill:#48a9a6,stroke:#000,stroke-width:2px;
    classDef cache fill:#e4572e,stroke:#000,stroke-width:2px;
    classDef support fill:#c17767,stroke:#000,stroke-width:2px;
    classDef external fill:#d8d8d8,stroke:#000,stroke-width:2px;

    subgraph Legend
        L1["Core Components"]:::core
        L2["WebSocket Components"]:::ws
        L3["Cache Components"]:::cache
        L4["Support Components"]:::support
        L5["External Systems"]:::external
    end
```
