# ston-fi/sdk architecture

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

- Interactive diagram: https://gitdiagram.com/ston-fi/sdk
- Repository: https://github.com/ston-fi/sdk

## Overview

##### Main Components

A. SDK Core Layer (packages/sdk):
- Client Module
- Contract Implementations
- DEX Contracts (v1, v2.1, v2.2)
- Farm Contracts (v1, v2, v3)
- pTON Contracts
- Utility Functions
- Test Utils

B. Example Application Layer (examples/next-js-app):
- Frontend Components
- API Integration
- Wallet Integration
- Swap Implementation
- Vault Implementation

**┌─────────────────────────────────────────────┐**
- Client Application             │
- (Next.js Frontend + Application Logic)     │
- ┤
- SDK Interface                 │
- (Client Module + Contract Abstractions) │
- ┤
- Smart Contracts Layer            │
- (DEX, Farm, pTON Contract Implementations) │
- ┤
- Blockchain Layer               │
- (TON Network Interface)           │
- ┘

##### Detailed Component Breakdown

A. Frontend Layer (Blue):
- Components: Swap, Vault, UI Elements
- Hooks and Queries
- State Management
- Wallet Integration

B. SDK Interface (Green):
- Client Module
- Contract Abstractions
- Utility Functions
- Type Definitions

C. Smart Contracts (Orange):
- DEX Contracts (Multiple Versions)
- Farm Contracts
- pTON Contracts
- Router Implementations

##### Key Interactions to Highlight

- Frontend → SDK: Contract calls and transactions
- SDK → Smart Contracts: Method calls and state changes
- Smart Contracts → Blockchain: State updates and events
- Wallet → Frontend: User authentication and signatures

##### Additional Elements

- Version indicators for different contract implementations
- Data flow directions between components
- Integration points with external services
- Error handling and fallback paths

##### Implementation Notes

- Maintain consistent spacing and alignment
- Brief descriptions for complex interactions
- Creating separate detailed views for specific features (swap, vault, farming)

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Application
        Frontend["Frontend Components"]:::frontend
        WalletGuard["Wallet Guard"]:::frontend
        AssetSelect["Asset Select"]:::frontend
        APIClient["API Client"]:::frontend
        
        subgraph Features
            Swap["Swap Implementation"]:::frontend
            Vault["Vault Implementation"]:::frontend
        end
        
        Frontend --> WalletGuard
        Frontend --> AssetSelect
        Frontend --> Features
        Frontend --> APIClient
    end
    
    subgraph SDK
        subgraph ContractLayer["Contract Layer"]
            subgraph DEXContracts["DEX Contracts"]
                DEXv1["DEX v1"]:::contract
                DEXv2_1["DEX v2.1"]:::contract
                DEXv2_2["DEX v2.2"]:::contract
            end
            
            subgraph FarmContracts["Farm Contracts"]
                FarmV1["Farm v1"]:::contract
                FarmV2["Farm v2"]:::contract
                FarmV3["Farm v3"]:::contract
            end
            
            pTON["pTON Contracts"]:::contract
        end
        
        Client["SDK Client"]:::sdk
        Utils["Utility Functions"]:::sdk
        TestUtils["Test Utils"]:::sdk
        
        Client --> ContractLayer
        Utils --> Client
        TestUtils --> Client
    end
    
    Application --> SDK
    SDK --> Blockchain[("TON Network")]:::external
    
    %% Click events for component mapping
    click Frontend "https://github.com/ston-fi/sdk/tree/main/examples/next-js-app/components/"
    click WalletGuard "https://github.com/ston-fi/sdk/blob/main/examples/next-js-app/components/wallet-guard.tsx"
    click AssetSelect "https://github.com/ston-fi/sdk/blob/main/examples/next-js-app/components/asset-select.tsx"
    click APIClient "https://github.com/ston-fi/sdk/blob/main/examples/next-js-app/lib/ston-api-client.ts"
    click Swap "https://github.com/ston-fi/sdk/tree/main/examples/next-js-app/app/swap/"
    click Vault "https://github.com/ston-fi/sdk/tree/main/examples/next-js-app/app/vault/"
    click Client "https://github.com/ston-fi/sdk/blob/main/packages/sdk/src/client/Client.ts"
    click DEXv1 "https://github.com/ston-fi/sdk/tree/main/packages/sdk/src/contracts/dex/v1/"
    click DEXv2_1 "https://github.com/ston-fi/sdk/tree/main/packages/sdk/src/contracts/dex/v2_1/"
    click DEXv2_2 "https://github.com/ston-fi/sdk/tree/main/packages/sdk/src/contracts/dex/v2_2/"
    click FarmV1 "https://github.com/ston-fi/sdk/tree/main/packages/sdk/src/contracts/farm/v1/"
    click FarmV2 "https://github.com/ston-fi/sdk/tree/main/packages/sdk/src/contracts/farm/v2/"
    click FarmV3 "https://github.com/ston-fi/sdk/tree/main/packages/sdk/src/contracts/farm/v3/"
    click pTON "https://github.com/ston-fi/sdk/tree/main/packages/sdk/src/contracts/pTON/"
    click Utils "https://github.com/ston-fi/sdk/tree/main/packages/sdk/src/utils/"
    click TestUtils "https://github.com/ston-fi/sdk/tree/main/packages/sdk/src/test-utils/"
    
    classDef frontend fill:#3498db,stroke:#2980b9,color:white
    classDef sdk fill:#2ecc71,stroke:#27ae60,color:white
    classDef contract fill:#f1c40f,stroke:#f39c12,color:black
    classDef external fill:#95a5a6,stroke:#7f8c8d,color:white
    
    %% Legend
    subgraph Legend
        Frontend_L["Frontend Component"]:::frontend
        SDK_L["SDK Component"]:::sdk
        Contract_L["Contract Component"]:::contract
        External_L["External System"]:::external
    end
```
