Diffusal

Overview

DEX-native API architecture for the Diffusal protocol

The Diffusal backend follows a DEX-native philosophy: the API server is a read-only data provider, while all writes go directly to smart contracts via user wallets.

Implementation Status: The Indexer and API Server are currently implemented. Internal servers (RFQ, Operator, Keeper) are planned for future development.


Three-Tier Interaction Model

┌─────────────────────────────────────────────────────────┐
│                        CLIENT                           │
│                    Frontend / Client                    │
└─────────────┬───────────────────────────────┬───────────┘
              │                               │
              ▼                               ▼
┌─────────────────────────────┐ ┌─────────────────────────┐
│          READS              │ │         WRITES          │
├─────────────────────────────┤ ├─────────────────────────┤
│  API Server (read-only)     │ │  Direct Transactions    │
│        │                    │ │  (via wallet)           │
│        ▼                    │ │                         │
│  Indexer (Ponder)           │ │                         │
└─────────────┬───────────────┘ └────────────┬────────────┘
              │                              │
              └──────────────┬───────────────┘


              ┌─────────────────────────────┐
              │         BLOCKCHAIN          │
              │       Smart Contracts       │
              └─────────────────────────────┘

Read Operations (API Server)

The API server aggregates data from multiple sources and serves it to clients:

Data TypeSourceProcessing
Positions, balancesIndexer (PostgreSQL)SQL-over-HTTP queries
Trade historyIndexerAggregation, pagination
Order bookIndexer + CacheReal-time aggregation
Mark prices, GreeksComputedBlack-Scholes via @diffusal/algorithms
Margin requirementsComputed@diffusal/algorithms (TypeScript)
Oracle dataIndexerCached from indexed chain events
24h statisticsComputedRolling window calculations

Write Operations (Direct to Contracts)

All state-changing operations go directly to smart contracts:

OperationContractFunction
Deposit collateralCollateralVaultdepositToPortfolio()
Withdraw collateralCollateralVaultwithdrawFromPortfolio()
Register orderOrderBookregisterOrder()
Cancel orderOrderBookcancelOrder()
Fill RFQ quoteRFQfillQuote()
Create portfolioPortfolioManagercreatePortfolio()
Transfer positionsPortfolioManagertransferPositionBetweenPortfolios()

Quick Reference

Endpoint Categories

CategoryBase PathAuth RequiredDescription
Auth/auth/api/*NoSIWE authentication
Markets/markets, /orderbook, /tickerNoPublic market data
Account/account/*YesUser positions, balances
Helpers/helpers/*NoTransaction parameter computation
WebSocket/wsOptionalReal-time data streams

Port Reference (Local Development)

ServicePortProtocolDescription
Anvil8545HTTPLocal blockchain RPC
Indexer PostgreSQL54322TCPPonder database
Indexer (Ponder)42069HTTPGraphQL + SQL-over-HTTP
API PostgreSQL54323TCPAPI server database
API Server (Elysia)8080HTTP/WSREST API + WebSocket

Key Design Principles

1. No Private Keys

The API server holds zero private keys. It cannot:

  • Sign transactions
  • Execute trades on behalf of users
  • Move user funds

This eliminates entire classes of security vulnerabilities.

2. Trustless Reads

All data served by the API can be independently verified:

  • Position data comes from indexed blockchain events
  • Margin calculations can be verified via on-chain view calls
  • Mark prices use transparent Black-Scholes formulas

3. Real-Time Updates

The API maintains WebSocket connections for:

  • Order book updates
  • Trade notifications
  • Position changes
  • Oracle price updates

Architecture Deep Dive

On this page