This document outlines the planning and architecture for the AMM/DEX trading system implemented in Canopy. The system provides a flexible trading module that adapts to different contexts (standalone Trade page, Chain detail pages, and Liquidity pages).
The main wrapper component that orchestrates all trading functionality.
Props:
variant:'trade' | 'chain' | 'liquidity'- Determines which tabs to showchainData: Chain data object (required for 'chain' variant)defaultTokenPair:{ from: string, to: string }- Initial token selectiondefaultTab: Starting active tabisPreview: Boolean for preview mode
Variants:
- trade: Shows Swap / Liquidity / Convert tabs
- chain: Shows Buy / Sell / Convert tabs
- liquidity: Shows Liquidity / Swap / Convert tabs
Modal dialog for searching and selecting tokens.
Features:
- Search by name, symbol, or address
- Recent token selections (stored in localStorage)
- Token exclusion (prevent selecting same token twice)
- Displays token price and 24h change
Token-to-token swapping interface.
Features:
- Select any two tokens
- "Select token" state when no token chosen
- Automatic price calculation based on token prices
- Exchange rate display
Add/remove liquidity to pools.
Features:
- Deposit mode: Select two tokens to add to pool
- Withdrawal mode: Select pool to withdraw from
- Toggle button (↓/↑) to switch between deposit/withdraw
- Auto-calculation of paired amounts based on pool ratios
- Pool statistics (APR, TVL, 24h volume)
Simplified buy/sell for chain tokens.
Features:
- Buy: CNPY → Chain token
- Sell: Chain token → CNPY
- Price conversion display
- "Use max" button
Cross-chain token conversion.
Features:
- Select source and destination tokens
- Token selection cards
- Simplified UI for basic conversions
Route: /trade/:tokenPair?
Layout:
- Sidebar navigation
- Sticky header bar with page title and Share button
- Centered trading module (max-width: 480px)
Features:
- Clean navigation header
- Share functionality
- Focused trading interface
Route: /liquidity/:tokenPair?
Layout:
- Sidebar navigation
- Sticky header bar with page title and Share button
- Centered trading module (max-width: 480px)
Features:
- Clean navigation header
- Share functionality
- Focused liquidity management interface
Contains metadata for all tradeable tokens:
- CNPY (native token)
- All chain tokens (OENS, MGC, SOCN, DEFI, SVLT)
Fields:
symbol: Token tickername: Full nameaddress: Contract addressdecimals: Token decimals (18)brandColor: UI colorcurrentPrice: Price in USDpriceChange24h: 24h price change percentagevolume24h: 24h trading volumemarketCap: Market capitalization
Defines available liquidity pools:
Fields:
id: Pool identifiertokenA,tokenB: Pool token pairtokenAReserve,tokenBReserve: Token reservestotalLiquidity: Total value locked (TVL)apr: Annual percentage ratevolume24h: 24h trading volumefees24h: 24h fees earned
New routes added to /src/App.jsx:
/trade/:tokenPair- Trade page/liquidity/:tokenPair- Liquidity page
Token Pair Format:
cnpy-oens- Trade CNPY for OENScnpy-select- Start with CNPY selected, prompting user to select second token
Updated /src/components/main-sidebar.jsx:
- Trade button navigates to
/trade/cnpy-select - Active state detection for
/trade/*and/liquidity/*routes - Both compact and default sidebar variants updated
Updated /src/pages/chain-detail/index.jsx:
- Replaced
TradingPanelwithTradingModule - Uses
variant="chain"for Buy/Sell/Convert tabs - Maintains existing functionality with new modular system
- User clicks "Trade" in sidebar
- Navigates to
/trade - Default state:
- First token (selling): "Select token" state (null)
- Second token (receiving): CNPY pre-selected
- User clicks "Select token" to choose selling token
- Token selection dialog opens
- User searches/selects token
- Swap interface shows with both tokens selected
- User enters amount in large input field
- User connects wallet to execute trade
- User navigates to Liquidity page or clicks pool from Trade page
- Default tab: "Liquidity" (deposit mode)
- Deposit mode:
- Select first token
- Select second token
- Enter amount for first token (second auto-calculates based on pool ratio)
- Click "Add Liquidity"
- Withdraw mode (toggle ↑):
- Select pool
- Enter LP token amount to withdraw
- See preview of tokens to receive
- Click "Remove Liquidity"
- User views chain detail page
- Trading panel shows Buy/Sell/Convert tabs
- Buy tab: CNPY → Chain token (default)
- Sell tab: Chain token → CNPY
- Convert tab: Any token → Any token
- User enters amount and connects wallet
All trades must be CNPY paired with another token. This architectural decision:
- No direct token-to-token trading: You cannot trade OENS directly for MGC
- All pairs go through CNPY: OENS → CNPY or CNPY → MGC
- Simplifies liquidity pools: All pools are CNPY-based pairs
- Automatic correction: System automatically ensures one side is always CNPY
Valid Trading Pairs:
- CNPY ↔ OENS ✓
- CNPY ↔ MGC ✓
- CNPY ↔ SOCN ✓
- CNPY ↔ Any Token ✓
Invalid Trading Pairs (Auto-corrected):
- OENS ↔ MGC ✗ (becomes OENS → CNPY or CNPY → MGC)
- Token A ↔ Token B ✗ (one side becomes CNPY)
The TradingModule uses a variant prop rather than separate components for each context. This:
- Reduces code duplication
- Ensures consistent behavior across contexts
- Makes maintenance easier
- Allows easy addition of new variants
Using :tokenPair in URLs allows:
- Deep linking to specific trading pairs
- Bookmarking favorite pairs
- Sharing specific swap configurations
- Format:
cnpy-oens(always CNPY paired with another token)
In liquidity pools, when user enters amount for one token, the other auto-calculates:
- Ensures correct pool ratios
- Prevents invalid deposits
- Improves UX
Token selection dialog stores recent selections:
- Quick access to frequently used tokens
- Persists across sessions
- User-specific preferences
Single toggle button (instead of separate tabs):
- Cleaner UI
- Less visual clutter
- Clear state transition
- Matches common DEX patterns
- Token selection state managed in TradingModule
- Recent tokens stored in localStorage
- No global state management needed (kept simple)
- Swap: Uses token prices from tokens.json
- Liquidity: Uses pool reserves for ratios
- All calculations done client-side (mock data)
The system is designed for easy extension:
- Add new variants by updating
getTabsConfig() - Add new tab components in
/src/components/trading-module/ - Add new pools to
liquidity-pools.json - Add new tokens to
tokens.json
Slippage Settings: Allow users to set max slippage tolerance✅ Implemented- Price Impact Warning: Show price impact for large trades
- Chart Integration: Add price charts to trade page
- Transaction History: Show user's past trades
- LP Position Tracking: Dashboard for liquidity provider positions
- Multi-hop Swaps: Route through multiple pools for best price
- Limit Orders: Set target prices for automatic execution
- Real-time Price Updates: WebSocket integration for live prices
- Pool Creation: Allow users to create new liquidity pools
- Rewards Dashboard: Track trading fees earned as LP
- Real blockchain integration (replace mock data)
- Smart contract interaction
- Wallet balance checking
- Transaction signing and submission
- Gas estimation
- Error handling and retry logic
- Loading states and optimistic updates
- Transaction confirmation tracking
- Trade page loads with default state (CNPY selected)
- Token selection dialog opens and closes
- Token search filters correctly
- Recent tokens persist in localStorage
- Can select tokens for swap
- Price calculations update correctly
- Liquidity deposit mode calculates ratios
- Toggle button switches deposit/withdraw modes
- Liquidity withdraw mode shows correct preview
- Chain page Buy/Sell tabs work
- Navigation between pages preserves state appropriately
- Sidebar highlights active route
- Responsive layout works on mobile
- All tabs render without errors
- Exchange rates display correctly
All components use existing UI components from /src/components/ui/:
- Button, Card, Input, Dialog
- No new external dependencies added
- Leverages existing design system