|
| 1 | +# Architecture Overview |
| 2 | + |
| 3 | +Lens is a unified price aggregation engine for the Stellar network. It bridges the gap between Stellar's Classic Order Book (SDEX) and Liquidity Pools (AMM) by providing a single source of truth for asset pricing, VWAP calculations, and optimal trade routing. |
| 4 | + |
| 5 | +## Data Flow |
| 6 | + |
| 7 | +The following diagram illustrates how data flows from the Stellar network through our ingesters, into the database, and finally out through our API layers. |
| 8 | + |
| 9 | +```mermaid |
| 10 | +graph TD |
| 11 | + subgraph Stellar Network |
| 12 | + SDEX[SDEX Trades] |
| 13 | + AMM[AMM Pools] |
| 14 | + end |
| 15 | +
|
| 16 | + subgraph Ingestion Layer |
| 17 | + SI[SDEX Ingester] |
| 18 | + AI[AMM Ingester] |
| 19 | + end |
| 20 | +
|
| 21 | + subgraph Storage Layer |
| 22 | + DB[(PostgreSQL / TimescaleDB)] |
| 23 | + PP[price_points table] |
| 24 | + PS[pool_snapshots table] |
| 25 | + end |
| 26 | +
|
| 27 | + subgraph Aggregation Layer |
| 28 | + BR[bestRoute Logic] |
| 29 | + end |
| 30 | +
|
| 31 | + subgraph API Layer |
| 32 | + REST[REST API /price] |
| 33 | + GQL[GraphQL API] |
| 34 | + X402{x402 Payment Gate} |
| 35 | + end |
| 36 | +
|
| 37 | + SDEX -->|Horizon Stream| SI |
| 38 | + AMM -->|Polling| AI |
| 39 | +
|
| 40 | + SI --> PP |
| 41 | + AI --> PS |
| 42 | + AI --> PP |
| 43 | +
|
| 44 | + PP --> BR |
| 45 | + PS --> BR |
| 46 | +
|
| 47 | + BR --> X402 |
| 48 | + X402 --> REST |
| 49 | + X402 --> GQL |
| 50 | +``` |
| 51 | + |
| 52 | +## System Components |
| 53 | + |
| 54 | +### 1. Ingestion Layer |
| 55 | +Lens utilizes two distinct ingestion strategies to maintain up-to-date pricing data: |
| 56 | + |
| 57 | +* **SDEX Ingester**: Streams trades directly from Stellar Horizon for watched asset pairs. It processes individual trade events in real-time, extracting price and volume data which is then persisted to the `price_points` table. |
| 58 | +* **AMM Ingester**: Polls Horizon for liquidity pool snapshots and recent AMM trades. It stores pool reserves (Asset A/B amounts) in `pool_snapshots` and individual trades in `price_points`. This allows Lens to calculate spot prices based on reserve ratios even when no trades have occurred recently. |
| 59 | + |
| 60 | +### 2. Storage Layer |
| 61 | +Data is stored in a PostgreSQL database optimized with TimescaleDB for time-series efficiency. |
| 62 | +* **`price_points`**: A unified table for all trade events across both SDEX and AMM. This enables high-performance VWAP (Volume Weighted Average Price) calculations over various time windows (1m, 5m, 1h, 24h). |
| 63 | +* **`pool_snapshots`**: Stores the reserve state of AMM liquidity pools, which is critical for slippage estimation and constant-product price calculations. |
| 64 | + |
| 65 | +### 3. Aggregation & Routing (`bestRoute`) |
| 66 | +When a price is requested, the `bestRoute` engine compares the available liquidity on both SDEX and AMM: |
| 67 | +* **SDEX Pricing**: Fetches real-time path payment quotes from Horizon to determine the effective rate for a specific trade size. |
| 68 | +* **AMM Pricing**: Uses the constant-product formula (`x * y = k`) against the latest pool snapshots to estimate the output and slippage. |
| 69 | +* **Optimal Routing**: Compares the rates and recommends the best execution path (SDEX, AMM, or a SPLIT for large orders) to minimize slippage for the user. |
| 70 | + |
| 71 | +### 4. API Layer & x402 Payment Gate |
| 72 | +The system exposes both REST and GraphQL interfaces. To monetize the high-fidelity data, Lens implements the **x402 protocol**. |
| 73 | + |
| 74 | +The **x402 Payment Gate** is a middleware that intercepts requests to premium endpoints (like `/price`, `/pools`, and `/candles`). If a valid `x-payment` header containing a signed Stellar transaction is not present, the server returns a `402 Payment Required` status along with the payment requirements (price, destination address, and network). This ensures that every high-value API call is backed by a micropayment, enabling a "pay-per-query" business model. |
0 commit comments