Diffusal

Architecture

Five-server model and data flow patterns

The Diffusal backend consists of five distinct server components, each with specific responsibilities and security profiles.

Implementation Status: Currently, only the Indexer and API Server are implemented. The RFQ Server, Operator Server, and Keeper Server are planned for future development.


Five-Server Model

╔═══════════════════════════════════════════════════════════════════╗
║                       EXTERNAL (PUBLIC)                           ║
║  ┌────────────────────┐  ┌────────────────────┐  ┌────────────┐   ║
║  │ Indexer (Ponder)   │  │ API Server (Elysia)│  │ Frontend / │   ║
║  │ • GraphQL          │──│ • REST API         │◀─│ External   │   ║
║  │ • SQL-HTTP         │  │ • WebSocket        │  │ Clients    │   ║
║  │ • Events           │  │ • No keys          │  └────────────┘   ║
║  └─────────┬──────────┘  └────────────────────┘                   ║
╚════════════╪══════════════════════════════════════════════════════╝
             │ Events

┌───────────────────────────────────────────────────────────────────┐
│                           BLOCKCHAIN                              │
│  CollateralVault • OrderBook • RFQ • SettlementEngine • etc.      │
└───────────────────────────────────────────────────────────────────┘
             ▲                    ▲                    ▲
             │ Transactions       │ Transactions       │ Transactions
╔════════════╪════════════════════╪════════════════════╪════════════╗
║            │                    │                    │            ║
║  ┌─────────┴──────────┐ ┌───────┴───────┐ ┌─────────┴─────────┐   ║
║  │ RFQ Server         │ │ Operator Srv  │ │ Keeper Server     │   ║
║  │ • MMM keys         │ │ • Admin ops   │ │ • Snapshots       │   ║
║  │ • Quotes/Fills     │ │ • Oracle      │ │ • Liquidate       │   ║
║  └────────────────────┘ └───────────────┘ └───────────────────┘   ║
║                       INTERNAL (PRIVATE)                          ║
╚═══════════════════════════════════════════════════════════════════╝

Server Descriptions

1. Indexer (Ponder)

Role: Blockchain event indexer and data store

AspectDetails
TechnologyPonder framework, PostgreSQL
Port42069 (GraphQL + SQL-over-HTTP)
Database Port54322
SecurityPublic read-only, no keys

Responsibilities:

  • Index all protocol events (trades, positions, deposits, etc.)
  • Maintain queryable database of on-chain state
  • Provide GraphQL and SQL-over-HTTP APIs
  • Serve historical data (trade history, position changes)

Events Indexed:

ContractEvents
CollateralVaultDepositedToPortfolio, WithdrawnFromPortfolio, TransferredOut, TransferredIn, DebitedFromPortfolio, CreditedToPortfolio
PositionManagerPositionUpdated, OptionsBought, OptionsSold
SeriesRegistryOpenInterestUpdated, PairCapsSet, SeriesCreated, SeriesSettled
OrderBookOrderRegistered, OrderCancelled, Trade, MatchSettled, FeesCollected, MinOrderNonceIncremented
RFQRfqQuoteFilled
LiquidationEnginePortfolioLiquidated, PositionLiquidated
SettlementEnginePositionSettled, InsuranceProrated
OracleVolatilityUpdated, PremiumUpdated, RiskFreeRateUpdated
PriceHistoryPriceSnapshotRecorded

2. API Server (Elysia)

Role: Public API gateway for frontend clients

AspectDetails
TechnologyElysia (Bun), Drizzle ORM
Port8080 (REST + WebSocket)
Database Port54323
SecurityPublic, no keys, SIWE auth for user data

Responsibilities:

  • Aggregate data from Indexer and on-chain sources
  • Calculate mark prices and Greeks
  • Serve REST API and WebSocket streams
  • Cache expensive computations
  • Handle user authentication (SIWE)

Data Sources:

DataSourceMethod
Positions, balancesIndexerSQL-over-HTTP
Margin calculationsComputed@diffusal/algorithms
Mark prices, GreeksComputed@diffusal/algorithms
Oracle dataIndexerSQL-over-HTTP (cached from chain)

Note: The API server has no direct RPC calls to the blockchain. All on-chain data is consumed via the Indexer's SQL-over-HTTP interface.

3. RFQ Server

Status: Not yet implemented

Role: Main Market Maker quote generation

AspectDetails
SecurityInternal only, holds MMM keys
AccessNot exposed to public internet

Planned Responsibilities:

  • Generate RFQ quotes for users
  • Sign quotes with MMM private key
  • Manage MMM inventory and risk
  • Execute quote fills on-chain

Key Characteristics:

  • Holds private keys (critical security)
  • Internal network only
  • Monitored for health/solvency

4. Operator Server

Status: Not yet implemented

Role: Administrative operations

AspectDetails
SecurityInternal only, holds operator keys
AccessRestricted to authorized personnel

Planned Responsibilities:

  • Set oracle parameters (volatility, rates)
  • Update protocol configuration
  • Emergency operations
  • Native price overrides (when needed)

5. Keeper Server

Status: Not yet implemented

Role: Automated protocol maintenance

AspectDetails
SecurityInternal only, holds keeper keys
AccessAutomated, monitored

Planned Responsibilities:

  • Take price snapshots (30-second intervals)
  • Trigger liquidations when users become undercollateralized
  • Settle expired series (TWAP calculation)
  • Settlement readiness liquidations

Data Flow Patterns

Read Flow (User → API)

User Request


┌─────────────────────────────────────────────────────────────┐
│                        API Server                           │
├─────────────────────────────────────────────────────────────┤
│  1. Auth?     ─── If protected, verify JWT ───►             │
│       │                                                     │
│       ▼                                                     │
│  2. Cache?    ─── Check if cached data is fresh ───►        │
│       │                                                     │
│       ▼                                                     │
│  3. Query     ─── Fetch from Indexer (SQL-over-HTTP) ───►   │
│       │                                                     │
│       ▼                                                     │
│  4. Compute   ─── Calculate Greeks, margins ───►            │
│       │                                                     │
│       ▼                                                     │
│  5. Return    ─── JSON response ───►                        │
└─────────────────────────────────────────────────────────────┘

Write Flow (User → Chain)

User Action (e.g., deposit)


┌──────────────────────────────────────┐
│              FRONTEND                │
├──────────────────────────────────────┤
│  1. Build tx  ── helper endpoints    │
│       │                              │
│       ▼                              │
│  2. Sign      ── wallet signature    │
│       │                              │
│       ▼                              │
│  3. Submit    ── blockchain RPC      │
└──────────┬───────────────────────────┘


┌──────────────────────────────────────┐
│           BLOCKCHAIN                 │
│        Transaction executes          │
└──────────┬───────────────────────────┘
           │ Events

┌──────────────────────────────────────┐
│            INDEXER                   │
│        Indexes new events            │
└──────────┬───────────────────────────┘


┌──────────────────────────────────────┐
│          API SERVER                  │
│     WebSocket broadcasts update      │
└──────────┬───────────────────────────┘


┌──────────────────────────────────────┐
│           FRONTEND                   │
│       UI updates in real-time        │
└──────────────────────────────────────┘

Real-Time Update Flow

Blockchain Event (e.g., Trade)


┌──────────────────────────────────────┐
│            INDEXER                   │
│        Persists to database          │
└──────────┬───────────────────────────┘
           │ Subscription

┌──────────────────────────────────────┐
│          API SERVER                  │
├──────────────────────────────────────┤
│  1. Receive   ── from Indexer stream │
│       │                              │
│       ▼                              │
│  2. Process   ── update caches       │
│       │                              │
│       ▼                              │
│  3. Broadcast ── push to WebSocket   │
└──────────┬───────────────────────────┘


┌──────────────────────────────────────┐
│            CLIENTS                   │
│      Receive real-time updates       │
└──────────────────────────────────────┘

Security Model

Public Servers (No Keys)

ServerKeysRisk if Compromised
IndexerNoneData leak only, no fund loss
API ServerNoneData leak only, no fund loss

Mitigations:

  • Rate limiting
  • Input validation
  • CORS whitelisting
  • DDoS protection

Internal Servers (With Keys)

ServerKeysRisk if Compromised
RFQ ServerMMM keysFund loss possible
Operator ServerOperator keysProtocol manipulation
Keeper ServerKeeper keysLimited (can trigger liquidations)

Mitigations:

  • Network isolation (no public access)
  • Key management (HSM, secure enclaves)
  • Multi-sig for high-value operations
  • Monitoring and alerting
  • Transaction limits

Deployment Topology

Development

┌───────────────────────────────────────────────────────────┐
│                       localhost                           │
├───────────────────────────────────────────────────────────┤
│                                                           │
│  ┌─────────────────────────────────────────────────────┐  │
│  │        Anvil (8545) - Local blockchain              │  │
│  └─────────────────────────────────────────────────────┘  │
│                                                           │
│  ┌──────────────────────┐  ┌──────────────────────────┐  │
│  │ Indexer Postgres     │  │ API Postgres             │  │
│  │ (54322)              │  │ (54323)                  │  │
│  └──────────┬───────────┘  └─────────────┬────────────┘  │
│             │                            │               │
│  ┌──────────▼───────────┐  ┌─────────────▼────────────┐  │
│  │ Indexer (42069)      │  │ API Server (8080)        │  │
│  │ GraphQL + SQL-HTTP   │  │ REST + WebSocket         │  │
│  └──────────────────────┘  └──────────────────────────┘  │
│                                                           │
└───────────────────────────────────────────────────────────┘

Production

┌─────────────────────────────────────────────────────────┐
│                    PUBLIC ZONE                          │
├─────────────────────────────────────────────────────────┤
│                                                         │
│              ┌───────────────────────┐                  │
│              │    Load Balancer      │                  │
│              └───────────┬───────────┘                  │
│                    ┌─────┴─────┐                        │
│                    │           │                        │
│             ┌──────▼─────┐ ┌───▼──────────────┐        │
│             │ API Server │ │ Indexer          │        │
│             │ (replicated)│ │ (replicated,    │        │
│             └────────────┘ │  read-only)      │        │
│                            └──────────────────┘        │
└─────────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────────┐
│              PRIVATE ZONE (VPN/VPC only)                │
├─────────────────────────────────────────────────────────┤
│                                                         │
│  ┌────────────┐  ┌─────────────┐  ┌────────────────┐    │
│  │ RFQ Server │  │ Operator    │  │ Keeper Server  │    │
│  │            │  │ Server      │  │                │    │
│  └────────────┘  └─────────────┘  └────────────────┘    │
│                                                         │
│              ┌──────────────────────────┐               │
│              │  Databases (PostgreSQL)  │               │
│              └──────────────────────────┘               │
│                                                         │
└─────────────────────────────────────────────────────────┘

On this page