Skip to main content

Architecture

QsnDEX is a decentralized exchange built on a three-layer architecture designed for performance, reliability, and extensibility across multiple L2 chains.

System Layers

Layer 1: Smart Contracts

  • Language/Tooling: Solidity 0.8.20+, Foundry (build, test, deploy), OpenZeppelin contracts
  • Core contracts: Factory, Pair, Router
  • Periphery contracts: BatchSwap, LimitOrder, Staking, Launchpad, FeeCollector

Layer 2: Backend

  • Runtime: Rust
  • Framework: Axum (async HTTP/WebSocket server)
  • Database: PostgreSQL 16 via SQLx (compile-time checked queries)
  • Cache: Redis 7
  • AI Engine: Groq API integration with three models:
    • Llama 3.3 70B (primary analysis)
    • Llama 3.1 8B (lightweight tasks)
    • DeepSeek R1 70B (reasoning-intensive tasks)

Layer 3: Frontend

  • Framework: Next.js 16 with React 19
  • Language: TypeScript
  • Web3 Stack: wagmi v2, viem, RainbowKit
  • Styling: TailwindCSS v4

System Diagram

                         +---------------------+
| Users (Wallets) |
+----------+----------+
|
v
+---------------------+
| Frontend |
| Next.js 16 / React 19|
+----------+----------+
|
v
+---------------------+
| Backend API |
| Rust / Axum |
+--+-----+-----+---+--+
| | | |
+------------+ +--+--+ | +------------+
| | | | |
v v v v v
+-----------+ +------+-+ +-+--+------+ +------+------+
| Redis 7 | |Postgres| | Groq AI | | Blockchain |
| (Cache) | | 16 | | Engine | | RPCs |
+-----------+ +--------+ +-----------+ +------+------+
|
v
+------+------+
| Smart |
| Contracts |
| on L2 |
+-------------+

Core Contract Architecture

The Factory contract creates Pair contracts using the CREATE2 opcode, enabling deterministic address computation without requiring on-chain lookups. The Router serves as the primary entry point for users, handling swap execution and liquidity management.

Periphery contracts extend the core functionality:

ContractPurpose
BatchSwapExecute multiple swaps in a single transaction
LimitOrderPlace and fill conditional orders
StakingStake QSN and LP tokens for rewards
LaunchpadFair-launch token sales
FeeCollectorAggregate and distribute protocol fees

Indexer Architecture

The backend runs per-chain background indexers that continuously poll on-chain events to keep the database synchronized with contract state.

IndexerPolling IntervalEvents Tracked
Core pair indexer2 secondsSwap, Mint, Burn, Sync
Launchpad indexer15 secondsSale creation, contributions
Staking indexer15 secondsStake, unstake, claims
Limit order keeper15 secondsOrder fills, cancellations
Price alert checker30 secondsToken price thresholds

Each indexer runs as an independent async task, tracking its own last-processed block number in PostgreSQL to ensure no events are missed across restarts.

Caching Strategy

Redis is used as a caching layer with context-appropriate TTLs to balance data freshness against RPC and computation costs.

Cache KeyTTLPurpose
Token prices15 secondsCurrent swap rates
ETH/USD price60 secondsFiat price reference
AI analysis results1 hourToken analysis and assessments
Safety check results5 minutesToken contract safety scores
Autopilot state3 minutesAI-driven trading suggestions