Skip to content

Apostlex0/Xypher

Repository files navigation

Program ID (Devnet): 7TeV1Vdps9eaMv8mfcXJgNsEzWBJwNi4kHfqaVkEu95u

Arcium MPC: Integrated for encrypted balance updates

ZEC Dark Perps - Privacy-Preserving Perpetual Futures on Solana

A Hyperliquid-inspired perpetual futures exchange with privacy-first architecture using Arcium MPC and encrypted storage.

🎯 What Makes This Unique

Traditional Perps Exchanges:

  • ❌ All trades visible on-chain
  • ❌ Position sizes public
  • ❌ Easy to front-run large orders
  • ❌ Whale tracking is trivial

ZEC Dark Perps:

  • βœ… Encrypted Balances via Arcium Multi-Party Computation (MPC)
  • βœ… Private Order Details with AES-256-GCM encryption
  • βœ… Hyperliquid-Style Mechanics (position lifecycle, mark price, margin system)
  • βœ… Zcash Bridge for private collateral deposits (in development)

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    MATCHING ENGINE (Off-Chain)               β”‚
β”‚  β€’ Order Matching & Position Tracking                       β”‚
β”‚  β€’ AES-256-GCM Encrypted Storage (Supabase)                β”‚
β”‚  β€’ Mark Price & Real-Time PnL                               β”‚
β”‚  β€’ Margin Checks (10x Leverage)                             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     β”‚
                     β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  SOLANA PROGRAM (On-Chain)                   β”‚
β”‚  β€’ Encrypted Margin Accounts (Arcium MPC)                   β”‚
β”‚  β€’ Privacy-Preserving Trade Settlement                      β”‚
β”‚  β€’ Zcash Bridge (wZEC Minting)                              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     β”‚
                     β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   ARCIUM MPC CLUSTER                         β”‚
β”‚  β€’ Encrypted Balance Updates                                β”‚
β”‚  β€’ Zero-Knowledge Computations                              β”‚
β”‚  β€’ Decentralized Privacy Layer                              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ” Privacy Model

What's Encrypted (Private)

On-Chain (Arcium MPC):

  • βœ… Margin account balances (encrypted_collateral, encrypted_debt)
  • βœ… Trade settlement values (computed in MPC, re-encrypted)

Off-Chain (AES-256-GCM):

  • βœ… Exact order sizes and prices
  • βœ… User public keys (hashed with SHA-256)
  • βœ… Position details (size, entry price, PnL)
  • βœ… Trade details (buyer/seller, exact amounts)

What's Public (Necessary)

For Trading:

  • Order side (long/short)
  • Size buckets ('0-1', '1-10', '10-100', '100+')
  • Price levels (rounded to nearest $1)

For Safety:

  • Liquidation flags (liquidators need to identify risky accounts)
  • Transaction existence (Solana requirement)

For Transparency:

  • Aggregated metrics (total volume, open interest, trade count)

πŸš€ Quick Start Demo

Prerequisites

# Install dependencies
node --version  # v18+
npm --version   # v9+

# Solana CLI
solana --version  # v1.18+

# Anchor
anchor --version  # v0.30+

1. Start Matching Engine

cd services/matching-engine
npm install
npm run build
npm start

Expected Output:

πŸš€ ZEC Dark Perps Matching Engine
================================
πŸ“‘ Server listening on http://localhost:3001
πŸ“Š Orderbook API: http://localhost:3001/api/orderbook
πŸ“ Submit orders: POST http://localhost:3001/api/orders
================================

βœ… Database connected
πŸ“¦ Restoring orderbook from database...
βœ… Restored 0 orders from database
πŸ”„ Starting matching loop (200ms interval)

2. Place Orders (New Terminal)

# Place a LONG order
curl -X POST http://localhost:3001/api/orders \
  -H "Content-Type: application/json" \
  -d '{
    "userPubkey": "CyViyEZCjHBJPKzCYZ7sSDm2e3YCdgxrjbCj3PLyvT8i",
    "side": "long",
    "size": 0.5,
    "price": 50.0
  }'

# Place a SHORT order (will match!)
curl -X POST http://localhost:3001/api/orders \
  -H "Content-Type: application/json" \
  -d '{
    "userPubkey": "9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin",
    "side": "short",
    "size": 0.5,
    "price": 50.0
  }'

What Happens:

  1. βœ… Orders encrypted and stored in Supabase
  2. βœ… Matching engine finds the match
  3. βœ… Trade created with encrypted details
  4. βœ… Positions updated (weighted average entry price)
  5. βœ… Mark price updated for real-time PnL
  6. βœ… MPC settlement queued to Arcium πŸ”

3. View Encrypted Data

# Check orderbook (bucketed data - public)
curl http://localhost:3001/api/orderbook | jq

# View user positions (decrypted server-side)
curl http://localhost:3001/api/positions/CyViyEZCjHBJPKzCYZ7sSDm2e3YCdgxrjbCj3PLyvT8i | jq

# Check platform metrics
curl http://localhost:3001/api/metrics | jq

# View mark price
curl http://localhost:3001/api/mark-price | jq

# Check margin account
curl http://localhost:3001/api/account/CyViyEZCjHBJPKzCYZ7sSDm2e3YCdgxrjbCj3PLyvT8i | jq

4. Watch MPC Settlement (Matching Engine Terminal)

🎯 Match found!
  Buyer: CyViyEZC... (0.5 @ $50)
  Seller: 9xQeWvG8... (0.5 @ $50)

πŸ’Ύ Trade saved to database (encrypted)
πŸ“Š Mark price updated: $50.00
πŸ“ Position updated for CyViyEZC... (LONG 0.5 @ $50.00)
πŸ“ Position updated for 9xQeWvG8... (SHORT 0.5 @ $50.00)

πŸ” Queuing MPC settlement to Arcium...
   Buyer margin: CyViyEZC...
   Seller margin: 9xQeWvG8...
   Trade value: $25.00

⏳ Waiting for Arcium MPC callback...

🎬 Demo Script (For Video)

Scene 1: Introduction (30 seconds)

# Show the architecture
cat README.md | grep -A 20 "Architecture"

# Explain privacy model
echo "Traditional exchanges expose everything on-chain."
echo "ZEC Dark Perps encrypts balances, orders, and positions."
echo "Using Arcium MPC for zero-knowledge computations."

Scene 2: Start the System (30 seconds)

# Terminal 1: Start matching engine
cd services/matching-engine
npm start

# Show it's connected to Supabase and ready

Scene 3: Place Orders & Show Encryption (2 minutes)

# Terminal 2: Place LONG order
curl -X POST http://localhost:3001/api/orders \
  -H "Content-Type: application/json" \
  -d '{"userPubkey":"CyViyEZCjHBJPKzCYZ7sSDm2e3YCdgxrjbCj3PLyvT8i","side":"long","size":0.5,"price":50.0}'

# Show orderbook (bucketed - privacy preserved)
curl http://localhost:3001/api/orderbook | jq

# Explain: "Notice the size is bucketed as '0-1', not exact 0.5"
# Explain: "User pubkey is hashed, not visible"

# Place SHORT order to trigger match
curl -X POST http://localhost:3001/api/orders \
  -H "Content-Type: application/json" \
  -d '{"userPubkey":"9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin","side":"short","size":0.5,"price":50.0}'

Scene 4: Watch MPC Settlement (1 minute)

# Terminal 1 will show:
# - Match found
# - Trade encrypted and saved
# - Positions updated
# - MPC settlement queued to Arcium
# - Waiting for callback

# Explain: "The matching engine queues encrypted balance updates to Arcium MPC"
# Explain: "Only the MPC cluster can decrypt and update balances"
# Explain: "This prevents anyone (including us) from seeing exact balances"

Scene 5: Show Position Lifecycle (1 minute)

# Check user position
curl http://localhost:3001/api/positions/CyViyEZCjHBJPKzCYZ7sSDm2e3YCdgxrjbCj3PLyvT8i | jq

# Explain the output:
# - Position side: LONG
# - Size: 0.5
# - Entry price: $50.00
# - Current PnL: calculated using mark price
# - Leverage: 10x

# Place another order to show position increase
curl -X POST http://localhost:3001/api/orders \
  -H "Content-Type: application/json" \
  -d '{"userPubkey":"CyViyEZCjHBJPKzCYZ7sSDm2e3YCdgxrjbCj3PLyvT8i","side":"long","size":0.3,"price":52.0}'

curl -X POST http://localhost:3001/api/orders \
  -H "Content-Type: application/json" \
  -d '{"userPubkey":"9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin","side":"short","size":0.3,"price":52.0}'

# Check position again - weighted average entry price
curl http://localhost:3001/api/positions/CyViyEZCjHBJPKzCYZ7sSDm2e3YCdgxrjbCj3PLyvT8i | jq

# Explain: "Position increased from 0.5 to 0.8"
# Explain: "Entry price is weighted average: (50*0.5 + 52*0.3)/0.8 = $50.75"

Scene 6: Show Margin System (1 minute)

# Check account margin
curl http://localhost:3001/api/account/CyViyEZCjHBJPKzCYZ7sSDm2e3YCdgxrjbCj3PLyvT8i | jq

# Explain the output:
# - Margin Balance: $1,000 (mock for demo)
# - Equity: Balance + Unrealized PnL
# - Total Notional: Position size * mark price
# - Leverage: Actual leverage being used
# - Margin Used: Notional / 10
# - Margin Available: What's left for new orders

# Try to place order that exceeds margin
curl -X POST http://localhost:3001/api/orders \
  -H "Content-Type: application/json" \
  -d '{"userPubkey":"CyViyEZCjHBJPKzCYZ7sSDm2e3YCdgxrjbCj3PLyvT8i","side":"long","size":200,"price":50.0}'

# Should get rejected with:
# "Insufficient margin. Required: $X, Available: $Y"

Scene 7: Show Platform Metrics (30 seconds)

# View aggregated metrics
curl http://localhost:3001/api/metrics | jq

# Explain:
# - Total Volume (24h): Sum of all trades
# - Open Interest: Total position notional
# - Total Trades: Number of matches
# - Active Traders: Unique users
# - All calculated from encrypted data!

Scene 8: Database Privacy (1 minute)

# Show Supabase schema
cat services/matching-engine/supabase/schema.sql | grep -A 10 "CREATE TABLE orders"

# Explain:
# - user_hash: SHA-256 hashed pubkey
# - size_bucket: '0-1', '1-10', etc (not exact)
# - price_level: Rounded to $1
# - encrypted_details: AES-256-GCM encrypted exact data

# Show encryption code
cat services/matching-engine/src/crypto.ts | grep -A 15 "export function encrypt"

# Explain:
# - AES-256-GCM authenticated encryption
# - Random IV per encryption
# - Auth tag for integrity

πŸ“Š What's Working (Demo-Ready)

βœ… Core Trading

  • Order placement with encryption
  • Order matching (FIFO, price-time priority)
  • Trade execution with encrypted storage
  • Position lifecycle (open/increase/reduce/close/flip)
  • Mark price tracking (last trade price)
  • Real-time unrealized PnL calculation

βœ… Privacy Layer

  • AES-256-GCM encryption for sensitive data
  • SHA-256 hashing for user identifiers
  • Size bucketing and price rounding
  • Arcium MPC integration (queue_settle_trade)
  • Encrypted margin accounts on-chain

βœ… Risk Management

  • Initial margin checks (10x leverage)
  • Account equity calculation
  • Order rejection when insufficient margin
  • Maintenance margin calculation (for liquidations)

βœ… Analytics

  • Platform metrics (volume, open interest, trades)
  • User position tracking
  • Trade history
  • Mark price API

πŸ”§ Technical Stack

On-Chain:

  • Solana (Devnet)
  • Anchor Framework v0.30
  • Arcium MPC for encrypted computations
  • SPL Token (wZEC)

Off-Chain:

  • Node.js + TypeScript
  • Supabase (PostgreSQL)
  • AES-256-GCM encryption
  • Express.js REST API

Frontend:

  • Next.js 14
  • Solana Wallet Adapter
  • TailwindCSS + shadcn/ui
  • Recharts for analytics

πŸ“ˆ Performance

Matching Engine:

  • Matching interval: 200ms (5 matches/sec)
  • Database latency: ~50-100ms
  • Order encryption: <1ms

On-Chain:

  • MPC settlement: 5-30 seconds (Arcium devnet)
  • Transaction cost: ~0.001 SOL
  • Confirmation time: ~400ms (Solana)

πŸ” Security Features

Encryption:

  • AES-256-GCM for database (authenticated encryption)
  • Arcium MPC for on-chain balances (zero-knowledge)
  • Random IV per encryption (prevents pattern analysis)

Privacy:

  • User pubkeys hashed with salt
  • Order sizes bucketed
  • Prices rounded
  • Exact details only in encrypted fields

Risk Management:

  • Initial margin: 10% (10x leverage)
  • Maintenance margin: 5%
  • Liquidation monitoring (to be implemented)

🚧 Roadmap

Phase 1: Core Trading βœ… (COMPLETE)

  • Order matching engine
  • Position lifecycle
  • Mark price system
  • Margin checks

Phase 2: Privacy Layer βœ… (COMPLETE)

  • Encrypted storage
  • Arcium MPC integration
  • Hashed identifiers

Phase 3: Bridge Integration 🚧 (IN PROGRESS)

  • On-chain bridge instructions
  • Zcash bridge daemon
  • Deposit monitoring
  • Withdrawal processing

Phase 4: Production Ready πŸ“‹ (PLANNED)

  • Signature verification (security)
  • Liquidation bot
  • Oracle integration (Pyth)
  • Funding rates
  • Multiple markets

πŸ“š Documentation


🎯 Key Differentiators

  1. Privacy-First: Only perps exchange with encrypted balances via MPC
  2. Hyperliquid Mechanics: Professional trading with position lifecycle
  3. Zcash Integration: Private collateral deposits (unique in DeFi)
  4. Hybrid Model: Balance privacy with operational transparency

🀝 Contributing

This is a research project exploring privacy-preserving DeFi. Contributions welcome!


⚠️ Disclaimer

This is experimental software on devnet. Not audited. Not for production use. Educational purposes only.


πŸ“ž Contact

Built with ❀️ for privacy-preserving DeFi

Status: MVP - Core trading functional, bridge in development

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published