Non-custodial decentralized exchange built on the Lunes blockchain. Spot trading, copy trading, margin, staking, governance, and AI agent API — all in one protocol.
Lunex is a full-stack DEX protocol on the Lunes Network (Substrate/Polkadot). It provides:
- Spot Trading — On-chain order book with sr25519 signature-based authentication
- Copy Trading — Follow top traders' vaults with performance fee logic
- Margin Trading — Leveraged positions with automated liquidation
- AI Agents API — Programmatic trading via API keys with staking-based limits
- Social Trading — Leaderboards, trade ideas, leader profiles
- Affiliate System — Multi-level commission distribution (up to 5 levels)
- Token Listing — Permissioned listing with on-chain liquidity locks
- Node.js ≥ 20
- PostgreSQL ≥ 15
- Redis ≥ 7
- Docker (optional, for full stack)
git clone <repo-url>
cd Lunex
yarn install# Backend API
cp spot-api/.env.example spot-api/.env
# Edit: DATABASE_URL, REDIS_URL, ADMIN_SECRET
# Frontend
cp lunes-dex-main/.env.example lunes-dex-main/.env
# Edit: VITE_API_URL, VITE_WS_URLcd spot-api
npx prisma migrate dev
npx prisma generate# Backend API (port 4000) + WebSocket (port 4001)
cd spot-api && yarn dev
# Frontend (port 3000)
cd lunes-dex-main && yarn devdocker-compose -f docker-compose.dev.yml up┌─────────────────┐ ┌─────────────────┐ ┌──────────────────┐
│ lunes-dex-main │────▶│ spot-api │────▶│ PostgreSQL │
│ (React/Vite) │ │ (Express 5) │ │ (Prisma ORM) │
└─────────────────┘ │ Port 4000 │ └──────────────────┘
│ WS: 4001 │ ┌──────────────────┐
└────────┬────────┘────▶│ Redis │
│ │ (Nonces/Cache) │
┌────────▼────────┐ └──────────────────┘
│ Lunes Node │ ┌──────────────────┐
│ (Substrate RPC)│────▶│ SubQuery │
└─────────────────┘ │ (Block Indexer) │
└──────────────────┘
For detailed architecture, see docs/ARCHITECTURE.md.
Lunex/
├── spot-api/ # Backend REST API + WebSocket server
│ ├── prisma/ # Database schema & migrations
│ ├── src/
│ │ ├── routes/ # 15 API route modules
│ │ ├── services/ # 24 business logic services
│ │ ├── middleware/ # Auth, error, pagination, metrics
│ │ └── utils/ # Logger, Redis, Metrics, Signing
├── lunes-dex-main/ # React frontend (Vite)
│ ├── src/
│ │ ├── components/ # UI components
│ │ ├── context/ # SpotContext, SDKContext
│ │ └── services/ # API client layer
├── contracts/ # ink! smart contracts (Rust)
│ ├── factory/ # AMM pair factory
│ ├── pair/ # Liquidity pair
│ └── marketplace/ # Order book contract
├── sdk/ # TypeScript SDK for external integrations
├── subquery-node/ # SubQuery indexer for Lunes events
├── mcp/ # Model Context Protocol server
├── docs/ # Documentation
└── scripts/ # Deployment & utility scripts
Base URL: http://localhost:4000/api/v1
| Module | Prefix | Description |
|---|---|---|
| Pairs | /pairs |
Trading pairs, ticker, sync |
| Orders | /orders |
Create, cancel, list orders |
| Trades | /trades |
Trade history, user trades |
| Orderbook | /orderbook |
Order book snapshot |
| Candles | /candles |
OHLCV candlestick data |
| Social | /social |
Leaders, ideas, follow, vaults |
| Copy Trade | /copytrade |
Copy vault positions & signals |
| Margin | /margin |
Leveraged positions |
| Affiliate | /affiliate |
Referrals, commissions, payouts |
| Agents | /agents |
AI agent registration & API keys |
| Smart Router | /route |
Best-route quotes & execution |
| Asymmetric | /asymmetric |
Asymmetric order matching |
| Listing | /listing |
Token listing & lock management |
| Governance | /governance |
Vote check, record, history |
Full API docs: docs/API.md
Connect to ws://localhost:4001 and subscribe to channels:
// Subscribe
ws.send(JSON.stringify({ type: 'subscribe', channel: 'orderbook:LUNES/LUSDT' }))
ws.send(JSON.stringify({ type: 'subscribe', channel: 'trades:LUNES/LUSDT' }))
ws.send(JSON.stringify({ type: 'subscribe', channel: 'ticker:LUNES/LUSDT' }))
ws.send(JSON.stringify({ type: 'subscribe', channel: 'user:<walletAddress>' }))See spot-api/.env.example for the full list. Critical variables:
| Variable | Description | Required |
|---|---|---|
DATABASE_URL |
PostgreSQL connection string | ✅ |
REDIS_URL |
Redis connection string | ✅ |
ADMIN_SECRET |
Bearer token for admin routes | ✅ prod |
RELAYER_SEED |
Relayer wallet seed for settlements | ✅ prod |
LUNES_WS_URL |
Lunes node WebSocket RPC | ✅ |
FACTORY_CONTRACT_ADDRESS |
Deployed factory contract | ✅ |
SPOT_CONTRACT_ADDRESS |
Deployed spot contract | ✅ |
SUBQUERY_ENDPOINT |
SubQuery indexer URL | Optional |
LOG_LEVEL |
Logging level (info/debug/warn) | Optional |
yarn add @lunex/sdkimport { LunexSDK } from '@lunex/sdk'
const sdk = new LunexSDK({ apiUrl: 'https://api.lunex.io' })
const pairs = await sdk.spot.getPairs()See SDK documentation for full SDK reference.
Contracts are written in ink! 4.x (Rust) and deployed on the Lunes Network.
| Contract | Purpose |
|---|---|
factory |
Deploys and tracks AMM pair contracts |
pair |
PSP22-compatible AMM liquidity pair |
marketplace |
On-chain order book |
Build:
cargo contract build --release# Unit & integration tests
cd spot-api && yarn test
# Specific test
yarn test -- --testPathPattern=affiliate
# E2E tests
yarn test:e2eTest files in spot-api/__tests__/:
agent.e2e.test.ts— Agent registration flowaffiliate.test.ts— Commission logic & referral chainbotSandbox.test.ts— Bot execution sandboxgovernance.e2e.test.ts— Vote cooldown enforcementtradeApi.e2e.test.ts— Trade API E2E
See docs/DEPLOYMENT.md for production deployment guide.
Quick production checklist:
- Set
NODE_ENV=production - Configure
ADMIN_SECRET(useopenssl rand -base64 32) - Point
DATABASE_URLto production PostgreSQL - Configure
REDIS_URLto production Redis cluster - Set
CORS_ALLOWED_ORIGINSto production frontend domain - Set
TRUST_PROXY=trueif behind nginx/load balancer
See LICENSE.md and NOTICE.md.