# 0xoucan/0xxoc-aip2pmxnexchange architecture

> Architecture diagram of the GitHub repository 0xoucan/0xxoc-aip2pmxnexchange, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/0xoucan/0xxoc-aip2pmxnexchange
- Repository: https://github.com/0xoucan/0xxoc-aip2pmxnexchange

## Overview

##### Project type and purpose
- This is a full-stack P2P token marketplace (0xXOC) connecting Mexican Peso payments (via OXXO Spin QR codes) to crypto trading on the Base network.
- Frontend: React + Vite, user-facing UI for creating/filling buy & sell orders.
- Backend: Node/TypeScript Express API, relay services, action providers to orchestrate token buy/sell flows and escrow.

##### Key backend folders (filtered to buying/selling flows)
- src/services/
- token-buying-order-relay.ts
- token-buying-order-filler-relay.ts
- token-selling-order-relay.ts
- token-selling-order-filler-relay.ts
- src/action-providers/
- basic-atomic-swaps/ (handles on-chain token transfers used by buy/sell relays)
- src/api-server.ts (REST endpoints for /buying-orders and /selling-orders)

##### Core frontend modules
- src/services/marketplaceService.ts (calls buy/sell API endpoints)
- src/components/marketplace/ (CreateBuyingOrder, CreateSellingOrder, BuyingOrderDetail, SellingOrderDetail, OrdersDisplay)
- WalletConnect + TransactionMonitor (sign & send blockchain transactions)

##### Main components
A. Frontend (React UI)
- Marketplace pages & forms
- MarketplaceService (HTTP client)
- WalletAdapter (MetaMask/Rabby) + TransactionMonitor
B. API Server (Express)
- BuyingOrdersController → token-buying-order-relay
- SellingOrdersController → token-selling-order-relay
- FillOrderControllers → token-buying-order-filler-relay & token-selling-order-filler-relay
C. Relay Services (Order Relays & Fillers)
- Receive validated request, invoke basic-atomic-swaps action provider to build & sign transactions or interact with escrow wallet
D. Action Provider (basic-atomic-swaps)
- Encapsulates on-chain token transfer logic used by both relays & fillers
E. External Systems
- Base network blockchain (token contracts, escrow)
- OXXO Spin QR code storage & encryption (file-storage utility)
- Browser wallet extensions for signing

##### Relationships & Data Flow
1. User creates a buy/sell order in Web UI → MarketplaceService
2. HTTP POST → API Server → appropriate Relay (buying-order-relay or selling-order-relay)
3. Relay validates data → calls basic-atomic-swaps Action Provider to prepare transaction or escrow call
4. API returns unsigned tx data to Frontend
5. Frontend’s TransactionMonitor / WalletAdapter prompts user to sign & submit on Base network
6. On “fill order”, similar flow via filler-relay modules; OXXO QR code data flows into backend (encrypted/stored) and triggers token release once payment confirmed

##### Architectural patterns & design principles
- Separation of concerns: Controllers → Relay services → Action providers
- Relay/Provider pattern isolates order logic from on-chain logic
- Stateless API server; state persisted on-chain and via file-storage for QR codes
- Event-driven transaction monitoring on frontend

##### Technologies & libraries
- Backend: Node.js, Express, TypeScript, viem (blockchain client), file-storage, encryption-utils
- Frontend: React, TypeScript, Vite, Tailwind CSS, Privy (auth), viem, browser extension wallets

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Frontend Layer
    subgraph "Frontend (UI Layer)"
        direction TB
        CreateBuyingOrder["CreateBuyingOrder"]:::ui
        CreateSellingOrder["CreateSellingOrder"]:::ui
        BuyingOrderDetail["BuyingOrderDetail"]:::ui
        SellingOrderDetail["SellingOrderDetail"]:::ui
        OrdersDisplay["OrdersDisplay"]:::ui
        MarketplacePage["MarketplacePage"]:::ui
        MarketplaceService["MarketplaceService"]:::api
        WalletConnect["WalletConnect"]:::onchain
        TransactionMonitor["TransactionMonitor"]:::onchain
    end

    %% Backend Layer
    subgraph "Backend (API & Services)"
        direction TB
        APIServer["API Server\n(src/api-server.ts)"]:::api

        subgraph "Relay Services"
            direction TB
            BuyingRelay["token-buying-order-relay"]:::service
            SellingRelay["token-selling-order-relay"]:::service
            BuyingFillerRelay["token-buying-order-filler-relay"]:::service
            SellingFillerRelay["token-selling-order-filler-relay"]:::service
        end

        subgraph "Action Provider\n(basic-atomic-swaps)"
            direction TB
            BasicSwap["basicAtomicSwapActionProvider"]:::onchain
        end

        QRStorage[(QR Code Storage)]:::external
    end

    %% External Systems
    subgraph "External Systems"
        direction TB
        BaseNetwork[(Base Network Blockchain)]:::external
        OXXOStorage[(OXXO Spin QR Storage)]:::external
        BrowserWallet[(Browser Wallet Extensions)]:::external
    end

    %% Connections
    CreateBuyingOrder -->|uses| MarketplaceService
    CreateSellingOrder -->|uses| MarketplaceService
    BuyingOrderDetail -->|uses| MarketplaceService
    SellingOrderDetail -->|uses| MarketplaceService
    OrdersDisplay -->|uses| MarketplaceService
    MarketplacePage -->|renders| CreateBuyingOrder
    MarketplacePage -->|renders| CreateSellingOrder
    MarketplacePage -->|renders| BuyingOrderDetail
    MarketplacePage -->|renders| SellingOrderDetail
    MarketplacePage -->|renders| OrdersDisplay

    MarketplaceService -->|POST /buying-orders| APIServer
    MarketplaceService -->|POST /selling-orders| APIServer

    APIServer -->|handles call| BuyingRelay
    APIServer -->|handles call| SellingRelay
    APIServer -->|handles call| BuyingFillerRelay
    APIServer -->|handles call| SellingFillerRelay

    BuyingRelay -->|invoke| BasicSwap
    SellingRelay -->|invoke| BasicSwap
    BuyingFillerRelay -->|invoke| BasicSwap
    SellingFillerRelay -->|invoke| BasicSwap

    APIServer -->|store QR| QRStorage
    QRStorage -->|retrieve encrypted QR| BuyingFillerRelay
    QRStorage -->|retrieve encrypted QR| SellingFillerRelay

    APIServer -->|returns unsigned TX| MarketplaceService
    MarketplaceService -->|passes tx| TransactionMonitor
    TransactionMonitor -->|sign & send| BaseNetwork
    WalletConnect -->|sign request| TransactionMonitor
    BrowserWallet -->|provides key| WalletConnect

    OXXOStorage -->|payment data| BuyingFillerRelay
    OXXOStorage -->|payment data| SellingFillerRelay

    %% Click Events
    click CreateBuyingOrder "https://github.com/0xoucan/0xxoc-aip2pmxnexchange/blob/main/0xXOC-Frontend/src/components/marketplace/CreateBuyingOrder.tsx"
    click CreateSellingOrder "https://github.com/0xoucan/0xxoc-aip2pmxnexchange/blob/main/0xXOC-Frontend/src/components/marketplace/CreateSellingOrder.tsx"
    click BuyingOrderDetail "https://github.com/0xoucan/0xxoc-aip2pmxnexchange/blob/main/0xXOC-Frontend/src/components/marketplace/BuyingOrderDetail.tsx"
    click SellingOrderDetail "https://github.com/0xoucan/0xxoc-aip2pmxnexchange/blob/main/0xXOC-Frontend/src/components/marketplace/SellingOrderDetail.tsx"
    click OrdersDisplay "https://github.com/0xoucan/0xxoc-aip2pmxnexchange/blob/main/0xXOC-Frontend/src/components/marketplace/OrdersDisplay.tsx"
    click MarketplacePage "https://github.com/0xoucan/0xxoc-aip2pmxnexchange/blob/main/0xXOC-Frontend/src/components/marketplace/MarketplacePage.tsx"
    click MarketplaceService "https://github.com/0xoucan/0xxoc-aip2pmxnexchange/blob/main/0xXOC-Frontend/src/services/marketplaceService.ts"
    click WalletConnect "https://github.com/0xoucan/0xxoc-aip2pmxnexchange/blob/main/0xXOC-Frontend/src/components/WalletConnect.tsx"
    click TransactionMonitor "https://github.com/0xoucan/0xxoc-aip2pmxnexchange/blob/main/0xXOC-Frontend/src/components/TransactionMonitor.tsx"
    click APIServer "https://github.com/0xoucan/0xxoc-aip2pmxnexchange/blob/main/0xXOC-Backend/src/api-server.ts"
    click BuyingRelay "https://github.com/0xoucan/0xxoc-aip2pmxnexchange/blob/main/0xXOC-Backend/src/services/token-buying-order-relay.ts"
    click SellingRelay "https://github.com/0xoucan/0xxoc-aip2pmxnexchange/blob/main/0xXOC-Backend/src/services/token-selling-order-relay.ts"
    click BuyingFillerRelay "https://github.com/0xoucan/0xxoc-aip2pmxnexchange/blob/main/0xXOC-Backend/src/services/token-buying-order-filler-relay.ts"
    click SellingFillerRelay "https://github.com/0xoucan/0xxoc-aip2pmxnexchange/blob/main/0xXOC-Backend/src/services/token-selling-order-filler-relay.ts"
    click BasicSwap "https://github.com/0xoucan/0xxoc-aip2pmxnexchange/blob/main/0xXOC-Backend/src/action-providers/basic-atomic-swaps/basicAtomicSwapActionProvider.ts"
    click QRStorage "https://github.com/0xoucan/0xxoc-aip2pmxnexchange/blob/main/0xXOC-Backend/src/utils/file-storage.ts"

    %% Styles
    classDef ui fill:#e0f7fa,stroke:#006064,color:#006064
    classDef api fill:#e8f5e9,stroke:#1b5e20,color:#1b5e20
    classDef service fill:#c8e6c9,stroke:#2e7d32,color:#2e7d32
    classDef onchain fill:#fff3e0,stroke:#ef6c00,color:#e65100
    classDef external fill:#eceff1,stroke:#607d8b,color:#455a64
```
