System Architecture & Data Pipeline

  1. Core Components - Blockchain Indexer (Ingestion Layer):

  • Technology: A Rust-based service using the native solana-client and solana-program SDKs for maximum performance and compatibility.

  • Function:

    • Subscribes to program account changes via a WebSocket connection to a Solana RPC endpoint.

    • Monitors specific Program Derived Addresses (PDAs) and transaction signatures related to the prediction market protocol.

    • Filters transactions based on the target Program ID.

  • Mechanism:

    • Implements slot-based confirmation (waiting for 32 slots, ~10-15 seconds, for transaction finality) to guard against forks.

    • Uses getSignaturesForAddress and getTransaction for historical data backfilling.

    • Decodes instruction data using the protocol's IDL (Interface Description Language) and identifies relevant PDAs.

2. Message Queue (Buffer):

  • Technology: Redis Streams or Apache Kafka.

  • Function:

    • Decouples the ingestion layer from the processing layer to handle Solana's high TPS potential.

    • Provides a buffer against RPC rate limiting and network congestion.

    • Enables replay capabilities from a specific slot for error recovery and data integrity.

3. Event Processor (Transformation Layer):

  • Technology: A scalable service in Rust (preferred) or Node.js with @solana/web3.js.

  • Function: This is the core logic engine that decodes raw instruction data and transforms it into enriched application-level entities.

  • Key Processing Logic:

    • Market Creation: Processes the InitializeMarket instruction, extracts market parameters from the newly created account data, and initializes the market document.

    • Position Minting/Trading: Processes PlaceOrder or AddLiquidity instructions, updates the market's liquidity pool, and recalculates outcome prices based on the AMM state.

    • Market Resolution: Processes the ResolveMarket instruction, finalizes the market state, and triggers the PnL calculation cascade for all participating wallets.

    • PnL Calculation: Trades are tracked and Realized/Unrealized PnL is computed using a FIFO (First-In, First-Out) accounting method across a user's positions in a market, referencing SPL token transfers.

4. Database (Persistence Layer):

  • Technology: PostgreSQL with TimescaleDB extension for time-series data, combined with Redis for caching frequently accessed on-chain state.

  • Schema Highlights:

    • markets table: Stores static and dynamic market data.

    • market_timeseries table (TimescaleDB HyperTable): Stores price, volume, and liquidity at fixed intervals, using slot_number as the primary timestamp.

    • user_positions table: A ledger of all user actions (mint, trade, redeem), linked to transaction signatures.

    • user_analytics table: Pre-aggregated roll-ups of user performance (total PnL, accuracy, volume) updated incrementally.

5. GraphQL API (Query Layer):

  • Technology: Hasura or a custom Apollo GraphQL server.

  • Function: Provides a flexible, strongly-typed query interface for the frontend. Allows the client to request exactly the data it needs in a single request, combining both indexed database data and real-time on-chain state.

  • Example Query:

6. Frontend Client (Presentation Layer):

  • Technology: React with a state management library like Zustand.

  • Data Flow:

    • Integrates the Solana Wallet Adapter for secure wallet connection and transaction signing.

    • Uses React Query (TanStack Query) or SWR for efficient server-state management, caching, and background refetching from the GraphQL API.

    • Connects to the GraphQL endpoint via a WebSocket link for real-time subscription data (e.g., price updates).

    • Directly interacts with Solana RPC for reading the latest on-chain state and submitting transactions.

Solana-Specific Technical Considerations

1. Account-Based Data Model

The architecture shifts from an event-logging model to an account-state model. The indexer primarily watches for changes in critical PDAs that represent market and user position states.

2. PDA (Program Derived Address) Management

  • Market accounts are derived from the creator's wallet and a unique seed.

  • User position accounts are derived from the user's wallet and the market PDA.

  • This allows for efficient and deterministic account lookup without relying on complex event filtering.

3. Slot-Based Time Tracking

  • Slot numbers are used as the primary timestamp for all time-series data, providing a native measure of blockchain time.

  • Slots are converted to human-readable timestamps using estimated slot times, accounting for network congestion.

4. RPC Optimization & Management

  • Utilizes multiple RPC providers (e.g., Helius, Triton, public RPCs) for redundancy, load balancing, and rate limit avoidance.

  • Implements strategic rate limiting and connection pooling to maintain stable RPC connections.

  • Employs geographically distributed RPC endpoints to minimize latency for a global user base.

5. Transaction Confirmation Strategy

The frontend and indexer use a robust confirmation strategy:

Performance Optimizations for Solana

  • Batch Account Updates: Fetches and processes multiple account states in a single RPC call where possible.

  • Parallel Processing: Handles data for multiple markets and users concurrently to leverage Solana's high throughput.

  • Efficient Cursor Management: Persists the last processed slot to ensure no data is missed on service restart.

  • Memory Pool (Mempool) Monitoring: (Optional) Subscribes to pending transactions for near-instant user feedback before confirmation.

Enhanced Features for the Solana Ecosystem

  • SPL Token Integration: Natively supports markets denominated in various SPL tokens (e.g., USDC).

  • Cross-Program Invocation (CPI) Tracking: Can track interactions and liquidity flows between the prediction market and other Solana DeFi protocols (e.g., AMMs, lending markets).

  • Priority Fee Analysis: Tracks and visualizes transaction priority fees, helping users understand market congestion and cost-to-transact.

  • Validator Health Dashboard: Monitors the performance and health of connected RPC providers and the broader Solana network.

Last updated