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:
| Contract | Purpose |
|---|---|
| BatchSwap | Execute multiple swaps in a single transaction |
| LimitOrder | Place and fill conditional orders |
| Staking | Stake QSN and LP tokens for rewards |
| Launchpad | Fair-launch token sales |
| FeeCollector | Aggregate 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.
| Indexer | Polling Interval | Events Tracked |
|---|---|---|
| Core pair indexer | 2 seconds | Swap, Mint, Burn, Sync |
| Launchpad indexer | 15 seconds | Sale creation, contributions |
| Staking indexer | 15 seconds | Stake, unstake, claims |
| Limit order keeper | 15 seconds | Order fills, cancellations |
| Price alert checker | 30 seconds | Token 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 Key | TTL | Purpose |
|---|---|---|
| Token prices | 15 seconds | Current swap rates |
| ETH/USD price | 60 seconds | Fiat price reference |
| AI analysis results | 1 hour | Token analysis and assessments |
| Safety check results | 5 minutes | Token contract safety scores |
| Autopilot state | 3 minutes | AI-driven trading suggestions |