Skip to content

MrBlackGhostt/orderBook

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

27 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

OpenDEX - Permissionless Order Book DEX

A fully on-chain order book DEX on Solana that allows anyone to create trading pairs without permission. Built with Anchor framework featuring limit orders, order matching, and a modern Next.js frontend with one-click token creation.

Solana Anchor Next.js

πŸš€ Try it now! No CLI required - create tokens and markets directly in the browser.

✨ Features

Core Trading Features

  • Permissionless Market Creation - Create trading pairs in 2 clicks (no CLI required!)
  • One-Click Token Creation - Auto-generate BASE and QUOTE tokens with mint authority
  • Limit Order Book - Place limit buy/sell orders with custom prices
  • Automated Order Matching - Permissionless cranking mechanism with economic incentives
  • Fee System - Split fees between traders, crankers (10%), and market creators (90%)

Modern UX Features

  • Token Balance Display - Real-time balance tracking in header
  • One-Click Token Airdrop - Mint test tokens instantly (if you're mint authority)
  • Step-by-Step Onboarding - Clear visual flow for new users
  • Network Warnings - Prominent DEVNET badges to prevent confusion
  • Professional UI - Tailwind CSS with dark mode, responsive design

Developer Features

  • Full Documentation - Comprehensive guides for all features
  • Helper Scripts - Automated token minting and setup scripts
  • Social Integration - Links to developer Twitter and GitHub
  • Solscan Integration - Direct links to view tokens and transactions

🎯 Quick Start for Users

No CLI knowledge required! Try the DEX in 3 clicks:

  1. Connect Wallet β†’ Use Phantom or Solflare
  2. Create Market β†’ Click "Create Token Pair" β†’ Auto-fills form β†’ Create Market
  3. Trade β†’ Click "Get Test Tokens" β†’ Start trading!

Total time: ~2 minutes ⏱️

πŸ› οΈ Tech Stack

  • Blockchain: Solana (Anchor Framework 0.30+)
  • Frontend: Next.js 15, React 18, TypeScript
  • Styling: Tailwind CSS
  • Wallet: Solana Wallet Adapter
  • State Management: React Hooks
  • Icons: Lucide React

πŸ“¦ Deployment Info

Program ID (Devnet): Gvv7atyen9oY1TccNVDb76btjUKwXi6bgmhQZDnaryGg

View on Solscan: Program Explorer

πŸš€ Getting Started (Developers)

Prerequisites

# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# Install Solana CLI
sh -c "$(curl -sSfL https://release.solana.com/stable/install)"

# Install Anchor (0.30+)
cargo install --git https://github.com/coral-xyz/anchor avm --locked --force
avm install latest
avm use latest

# Install Node.js (v18+)
# Use nvm or download from nodejs.org

Build & Test Smart Contract

# Build the program
anchor build

# Run tests
anchor test

# Deploy to devnet
solana config set --url devnet
anchor deploy

Run Frontend Locally

cd app

# Install dependencies
npm install

# Start development server
npm run dev

Visit http://localhost:3000 to see the app!

Mint Tokens to a Specific Wallet

# Use the helper script
./scripts/mint-to-wallet.sh YOUR_WALLET_ADDRESS 1000 10000

# Or manually
spl-token mint BASE_MINT 1000 YOUR_WALLET --fund-recipient
spl-token mint QUOTE_MINT 10000 YOUR_WALLET --fund-recipient

πŸ“ Project Structure

orderbook-dex/
β”œβ”€β”€ programs/
β”‚   └── orderbook-dex/
β”‚       └── src/
β”‚           β”œβ”€β”€ lib.rs                    # Program entry point
β”‚           β”œβ”€β”€ states.rs                 # Account structures
β”‚           β”œβ”€β”€ errors.rs                 # Custom errors
β”‚           └── instructions/
β”‚               β”œβ”€β”€ create_market.rs      # Market creation
β”‚               β”œβ”€β”€ place_order.rs        # Order placement
β”‚               β”œβ”€β”€ cancel_order.rs       # Order cancellation
β”‚               └── match_orders.rs       # Matching engine
β”œβ”€β”€ tests/
β”‚   └── orderbook-dex.ts                  # Integration tests
β”œβ”€β”€ app/                                  # Next.js frontend
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ page.tsx                      # Market list
β”‚   β”‚   β”œβ”€β”€ create/page.tsx               # Create market (with token creation!)
β”‚   β”‚   └── trade/[marketId]/page.tsx     # Trading interface
β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”œβ”€β”€ TokenBalances.tsx             # Balance display
β”‚   β”‚   β”œβ”€β”€ AirdropButton.tsx             # One-click token minting
β”‚   β”‚   β”œβ”€β”€ TradePanel.tsx                # Order placement
β”‚   β”‚   β”œβ”€β”€ OrderBook.tsx                 # Order book display
β”‚   β”‚   β”œβ”€β”€ Footer.tsx                    # Site footer
β”‚   β”‚   └── NetworkBanner.tsx             # Devnet warning
β”‚   β”œβ”€β”€ hooks/
β”‚   β”‚   β”œβ”€β”€ useProgram.ts                 # Anchor program hook
β”‚   β”‚   β”œβ”€β”€ useMarket.ts                  # Market data hook
β”‚   β”‚   └── useOrderBook.ts               # Order book hook
β”‚   └── lib/
β”‚       β”œβ”€β”€ tokenCreation.ts              # Token creation utilities
β”‚       β”œβ”€β”€ constants.ts                  # Config and helpers
β”‚       └── idl.ts                        # Program IDL
β”œβ”€β”€ scripts/
β”‚   β”œβ”€β”€ mint-to-wallet.sh                 # Batch mint tokens
β”‚   └── airdrop-tokens.ts                 # Airdrop helper
β”œβ”€β”€ Anchor.toml                           # Anchor configuration
└── Cargo.toml                            # Rust workspace

🎨 Architecture Overview

Smart Contract (Anchor/Rust)

Core Instructions:

  • create_market - Initialize a new trading pair
  • place_order - Place limit buy/sell orders
  • cancel_order - Cancel pending orders with refunds
  • match_orders - Permissionless crank to execute matching trades

Key Features:

  • Price-priority matching (bids: highest first, asks: lowest first)
  • Partial and full fill support
  • Dynamic account resolution using remaining accounts pattern
  • PDA-based vault system for secure asset custody
  • Cranker incentives (10% of fees)
  • Market creator revenue (90% of fees)

Frontend Features

Pages:

  • Market List - Browse all created markets
  • Create Market - Two-step token + market creation
  • Trading - Full order book with trade panel

Components:

  • Token balance display (compact header mode)
  • One-click token airdrop button
  • Order book visualization
  • Trade panel (buy/sell)
  • My orders management
  • Crank button for matching

πŸ”‘ Key Technical Patterns

Solana/Anchor Patterns

  • PDA Architecture - Program Derived Addresses for markets and order books
  • Remaining Accounts Pattern - Dynamic account resolution for trader ATAs
  • Token Program CPIs - Cross-program invocations for token transfers
  • PDA Signing - Using PDAs as vault authority for secure transfers
  • Partial Transaction Signing - Multi-signature transactions for token creation

DeFi/Trading Patterns

  • Order Book Data Structure - Efficient Vec-based order storage
  • Price-Time Priority - Proper order sorting and matching
  • Fee Distribution - Economic incentives for decentralized cranking
  • Partial Fill Handling - Support for partial order execution

Frontend Patterns

  • Wallet Adapter Integration - Seamless wallet connection
  • Real-time Balance Updates - Auto-refresh every 10 seconds
  • Transaction State Management - Loading, success, error states
  • Responsive Design - Mobile-friendly layout

πŸ§ͺ Testing

Comprehensive test coverage including:

  • Market creation and initialization
  • Order placement (bid and ask)
  • Stress testing (50 orders per side)
  • Order cancellation with refund verification
  • Order matching (full fills, partial fills, multiple matches)
  • Fee calculation and distribution

Run tests:

anchor test

πŸ” Security Considerations

  • Signer Validation - All critical operations require proper signer accounts
  • PDA Authority - Vaults controlled by PDAs to prevent unauthorized access
  • Account Validation - Proper ATA and account ownership checks
  • Integer Math - Careful handling of token decimals to prevent overflow
  • Devnet Only - Clear warnings that this is a test environment

πŸ“š Documentation

⚠️ Known Limitations

  • Maximum 50 orders per side (configurable via MAX_ORDERS const)
  • Match orders must be called manually (permissionless but not automatic)
  • No advanced order types (only limit orders currently)
  • No order modification (must cancel and re-place)
  • Devnet only (not production-ready for mainnet)

🚧 Future Enhancements

Trading Features:

  • Automated cranking bot
  • Stop-loss and take-profit orders
  • Order modification without cancel
  • Market orders (instant execution)

UI/UX Features:

  • Historical price charts (TradingView integration)
  • Trade history and analytics
  • Order book depth visualization
  • Mobile app (React Native)

Infrastructure:

  • Backend faucet service
  • WebSocket real-time updates
  • Mainnet deployment
  • Token metadata support

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“„ License

MIT License - see LICENSE file for details

πŸ‘¨β€πŸ’» Developer

Built by @HKsoldev

πŸ™ Acknowledgments

Built as part of a 40-day Solana developer sprint, targeting roles at:

  • Helius, Anza, Jito, Ellipsis Labs, Drift Protocol

Special thanks to:

  • Solana Foundation for excellent documentation
  • Anchor framework team for the amazing developer experience
  • Solana community for support and resources

⚑ Built with Solana | πŸ¦€ Powered by Rust | βš›οΈ UI with React

For questions, feedback, or collaboration opportunities, reach out via GitHub Issues or Twitter.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published