Program
Overview
The ChessMate program is a Solana program that implements a complete chess game with escrow functionality, built using the Anchor framework and optimized for Ephemeral Rollups.
Program ID
FLZ9jU8NUe9hwdKTKoU7A6HmDqPvFP2dKL1jJgUDw1SmArchitecture
Core Components
- Game Engine - Complete chess rule implementation
- Escrow System - Secure handling of entry fees and prizes
- Time Management - Various time control formats
- Rating System - ELO-based player ratings
- Platform Integration - Multi-platform support with fee sharing
Account Structure
Program Instructions
The program exposes 13 main instructions:
Platform Management
create_platform_config- Initialize platform settings (admin only)update_platform_config- Update platform configuration
Integrator Management
initialize_integrator- Setup integrator configurationupdate_integrator_config- Modify integrator settings
Game Lifecycle
create_game- Create a new chess gamejoin_game- Join an existing gamecancel_game- Cancel game before opponent joins
Gameplay
make_move- Execute a chess moveforfeit_game- Forfeit the current gameoffer_draw- Propose a drawaccept_draw- Accept a draw offerreject_draw- Reject a draw offer
Prize Management
claim_winnings- Claim prize pool after game ends
Security Features
Access Control
- Player Authorization - Only game participants can make moves
- Turn Validation - Enforces proper turn order
- Game State Checks - Prevents actions on finished games
- Signer Verification - All actions require proper signatures
Chess Rule Enforcement
- Move Validation - All moves validated against chess rules
- Check Detection - Prevents moves that leave king in check
- Special Moves - Proper handling of castling, en passant, promotion
- Game End Detection - Automatic checkmate/stalemate detection
Economic Security
- Escrow Protection - Entry fees held securely until game completion
- Fee Validation - Prevents manipulation of fee calculations
- Prize Distribution - Automated and tamper-proof payouts
- Refund Mechanisms - Safe refunds for cancelled games
Data Encoding
Board Representation
The chess board is stored as a 64-byte array with efficient piece encoding:
// Piece encoding: CCTTTTTT
// CC = Color (00=empty, 01=white, 10=black)
// TTTTTT = Type (000001=pawn, 000010=knight, etc.)
0x00 = Empty square
0x11 = White Pawn (0001 0001)
0x12 = White Knight (0001 0010)
0x13 = White Bishop (0001 0011)
0x14 = White Rook (0001 0100)
0x15 = White Queen (0001 0101)
0x16 = White King (0001 0110)
0x21 = Black Pawn (0010 0001)
// ... etcMove History
Moves are stored in a compact format:
pub struct ChessMove {
pub from_square: u8, // Source square (0-63)
pub to_square: u8, // Destination square (0-63)
pub piece_moved: u8, // Piece that moved
pub captured_piece: u8, // Piece captured (if any)
pub promotion_piece: u8, // Promotion piece (if any)
pub special_move: u8, // Special move type
pub timestamp: i64, // Move timestamp
}Performance Optimizations
Ephemeral Rollups Integration
- Fast State Updates - Game state processed on high-speed rollups
- Batch Processing - Multiple moves can be batched
- Reduced Costs - Lower transaction fees for gameplay
- Instant Finality - Immediate move confirmation
Memory Efficiency
- Compact Encoding - Minimal storage for game state
- Zero-Copy Deserialization - Fast account access
- Efficient PDAs - Optimized account derivation
Gas Optimization
- Minimal Compute - Efficient chess algorithms
- Batch Operations - Combined instruction execution
- Account Reuse - Minimize account creation
Error Handling
The program provides comprehensive error codes for all failure scenarios:
#[error_code]
pub enum ChessError {
#[msg("Invalid chess move")]
InvalidMove,
#[msg("Not player's turn")]
NotPlayerTurn,
#[msg("Game is not in progress")]
GameNotInProgress,
#[msg("King would be in check")]
KingInCheck,
// ... more error codes
}Integration Points
Platform Integration
- Multi-Platform Support - Multiple frontends can integrate
- Fee Sharing - Revenue sharing with integrators
- Custom Branding - Platform-specific configurations
- Analytics - Game statistics and metrics
Token Integration
- Multi-Token Support - Any SPL token can be used for entry fees
- Automatic Escrow - Secure token handling
- Fee Distribution - Automated fee collection
- Prize Payouts - Instant winner payouts
Deployment
Network Support
- Mainnet - Production deployment
- Devnet - Development and testing
- Localnet - Local development
Upgrade Path
- Anchor Upgrades - Program can be upgraded by authority
- State Migration - Backward compatibility maintained
- Feature Flags - Gradual feature rollout
Monitoring
Program Logs
- Move Events - All moves logged with details
- Game Events - Game creation, completion, cancellation
- Error Events - Failed operations with reasons
- Performance Metrics - Execution time and resource usage
Account Monitoring
- Game State Changes - Real-time game updates
- Token Movements - Escrow and payout tracking
- Player Actions - Complete audit trail
Next Steps
- Instructions Reference - Detailed instruction documentation
- Account Structures - Complete account schemas
- Error Codes - All error conditions and handling