Smart Contract Interactions
Overview
Fox Market uses a hybrid-decentralized architecture that combines off-chain order matching (CLOB) with on-chain settlement. This design provides the speed and efficiency of centralized exchanges while maintaining the security and non-custodial nature of decentralized systems.Architecture Components
Core Smart Contracts
1. CTFExchange (Main Trading Contract)
Based on Polymarket’s CTF Exchange, this is the primary contract for executing trades. Key Features:- Atomic Swaps: Facilitates atomic swaps between ERC1155 outcome tokens and ERC20 collateral
- Hybrid Model: Off-chain matching with on-chain settlement
- EIP-712 Signatures: Secure order signing and verification
- Multiple Signature Types: EOA, POLY_PROXY, POLY_GNOSIS_SAFE
- Fee Management: Symmetric fee collection and distribution
2. ProxyWalletFactory (User Wallet Management)
Factory contract for creating deterministic proxy wallets for users. Key Features:- Deterministic Deployment: Same address for same user across networks
- Gas Optimization: Minimal proxy pattern for efficient deployment
- User Management: Centralized wallet creation and tracking
3. MockUSDC (Collateral Token)
Test implementation of USDC for development and testing. Key Features:- ERC-20 Standard: Compatible with existing DeFi infrastructure
- Minting Capability: For testing and development
- 6 Decimals: Matches real USDC precision
4. Market (Token Management)
Comprehensive contract for managing prediction market tokens (ERC-1155). Key Features:- Market Creation: Dynamic market setup with multiple outcomes
- Token Management: ERC-1155 token minting and burning
- Outcome Resolution: Automated settlement based on oracle data
Integration with CLOB System
Data Flow
Order Processing
1. Order Creation (Off-chain)
2. Order Matching (CLOB)
3. Order Execution (On-chain)
Signature Verification
The system supports three signature types:1. EOA (Externally Owned Account)
2. POLY_PROXY (Proxy Wallet)
3. POLY_GNOSIS_SAFE (Gnosis Safe)
Contract Architecture: CTF-Based Design
Why CTF Exchange Architecture?
The Fox Market system adopts Polymarket’s CTF Exchange architecture for several key reasons:1. Proven Security Model
- Audited Contracts: CTF Exchange has been audited by ChainSecurity
- Battle-Tested: Used in production by Polymarket
- Best Practices: Implements industry-standard security patterns
2. Hybrid Decentralization
- Off-chain Matching: Fast order matching via CLOB
- On-chain Settlement: Secure, non-custodial settlement
- Operator Model: Centralized efficiency with decentralized security
3. Flexible Signature System
- Multiple Wallet Types: EOA, Proxy Wallet, Gnosis Safe
- Deterministic Addresses: Same proxy wallet address across networks
- Gas Optimization: Minimal proxy pattern for efficient deployment
Current Design: CTF-Based Architecture
Pros of CTF Architecture
1. Proven Security
- Audited by ChainSecurity
- Used in production by Polymarket
- Implements industry best practices
2. Hybrid Efficiency
- Fast off-chain matching
- Secure on-chain settlement
- Best of both worlds
3. Flexible Wallet Support
- Multiple signature types
- Deterministic proxy wallets
- Gas-optimized deployment
4. Standard Compliance
- EIP-712 signature standard
- ERC-1155 token standard
- ERC-20 collateral standard
5. Scalable Architecture
- Modular design with mixins
- Easy to extend and customize
- Clear separation of concerns
Cons of CTF Architecture
1. Complexity
- More contracts to manage
- Steeper learning curve
- More complex deployment
2. Gas Costs
- Multiple contract interactions
- Proxy wallet deployment costs
- Cross-contract calls
3. Dependency on External
- Relies on Polymarket’s architecture
- Limited customization options
- Potential version lock-in
Alternative: Simplified Architecture
Pros of Simplified Design
1. Easier to Understand
- Single contract for trading
- Simpler deployment process
- Clearer codebase
2. Lower Gas Costs
- Fewer contract interactions
- No proxy wallet overhead
- Direct token transfers
3. Full Control
- Complete customization
- No external dependencies
- Independent development
Cons of Simplified Design
1. Security Risks
- Untested architecture
- Potential vulnerabilities
- No audit history
2. Limited Features
- Basic functionality only
- No advanced wallet support
- Limited signature types
3. Scalability Issues
- Harder to extend
- Monolithic design
- Difficult to upgrade
Recommendation: CTF-Based Architecture
For the Fox Market system, we recommend adopting the CTF-based architecture because:1. Production Readiness
- Proven in production by Polymarket
- Comprehensive security audit
- Battle-tested codebase
2. Advanced Features
- Multiple wallet support
- Deterministic addresses
- Flexible signature system
3. Industry Standards
- EIP-712 compliance
- ERC-1155 token support
- Hybrid decentralization
4. Future Flexibility
- Modular design
- Easy to extend
- Scalable architecture
Integration Strategy
Phase 1: Core Integration
- Deploy CTFExchange with our customizations
- Implement ProxyWalletFactory for user management
- Create MockUSDC for testing
- Integrate with CLOB for order matching
Phase 2: Advanced Features
- Add Market contract for token management
- Implement Oracle integration for outcome resolution
- Add advanced fee management
- Implement batch operations
Phase 3: Optimization
- Gas optimization for common operations
- Batch processing for multiple orders
- Advanced order types (stop-loss, etc.)
- Cross-chain compatibility
Security Considerations
1. Signature Verification
- All orders must be properly signed
- Multiple signature types supported
- Deterministic address verification
2. Reentrancy Protection
- All external calls protected
- State changes before external calls
- Comprehensive testing
3. Access Control
- Admin-only functions protected
- Proper role management
- Emergency pause functionality
4. Gas Optimization
- Efficient proxy wallet deployment
- Minimal cross-contract calls
- Optimized data structures
Testing Strategy
1. Unit Tests
- Individual contract functions
- Signature verification
- Access control
2. Integration Tests
- End-to-end order flow
- CLOB integration
- Cross-contract interactions
3. Security Tests
- Reentrancy attacks
- Signature forgery
- Access control bypass
4. Gas Tests
- Deployment costs
- Operation costs
- Optimization verification