Skip to content

Repository files navigation

Solana Fair Dice Game – Provably Fair Dice Casino

License: MIT Anchor Solana

A production-ready, provably fair dice casino game built on Solana using the Anchor framework and ORAO VRF (Verifiable Random Function) for cryptographically verifiable randomness. Players can bet SOL or SPL tokens on roll under/over outcomes with transparent, on-chain fairness verification.

🎲 Features

  • Provably Fair: Uses ORAO VRF for cryptographically verifiable randomness - every roll can be independently verified
  • Flexible Betting: Support for both native SOL and SPL tokens
  • Configurable House Edge: Adjustable house edge (default 2-5%) with transparent payout calculations
  • On-Chain Transparency: All bet states, results, and payouts are stored on-chain for full auditability
  • Secure PDAs: Program Derived Addresses (PDAs) ensure secure fund management
  • Event Logging: Comprehensive event emission for off-chain indexing and analytics
  • Timeout Protection: Automatic timeout handling for VRF requests to prevent stuck bets
  • Production Ready: Includes comprehensive test suite, error handling, and security best practices

🛠 Tech Stack

  • Rust: Core program logic
  • Anchor 0.30.1: Solana framework for smart contract development
  • Solana: High-performance blockchain platform
  • ORAO VRF: Fast, cheap verifiable randomness (0.001 SOL per request)
  • SPL Tokens: Support for custom token betting
  • TypeScript/Mocha: Comprehensive test suite

📋 Prerequisites

🚀 Installation & Setup

1. Clone the Repository

git clone <repository-url>
cd Solana-Fair-Dice-Game-1

2. Install Dependencies

# Install Rust dependencies (handled by Anchor)
anchor build

# Install Node.js dependencies
yarn install
# or
npm install

3. Configure Solana CLI

# Set to devnet for testing
solana config set --url devnet

# Or use localnet
solana config set --url localhost

4. Build the Program

anchor build

This will:

  • Compile the Rust program
  • Generate the IDL (Interface Definition Language)
  • Create the program keypair

5. Deploy to Devnet

# Ensure you have SOL for deployment fees
solana airdrop 2 $(solana address)

# Deploy the program
anchor deploy

6. Run Tests

# Run all tests
anchor test

# Or run with local validator
anchor test --skip-local-validator

🎮 Game Mechanics

  1. Choose Your Bet:

    • Target: A number between 1-99
    • Side: Under (roll < target) or Over (roll > target)
    • Amount: Bet amount in SOL or SPL tokens
  2. Payout Calculation:

    • Under bet: Multiplier = (100 / target) × (1 - house_edge)
    • Over bet: Multiplier = (100 / (100 - target)) × (1 - house_edge)
    • Example: Target 50, house edge 2% → ~1.96x payout
  3. Randomness:

    • VRF generates a random number (0-99)
    • Win if: (under && roll < target) || (over && roll > target)
    • Loss otherwise

🔐 Provable Fairness

  1. Commit Phase (place_bet):

    • Player commits bet parameters (target, side, amount)
    • VRF request is created with a unique seed
    • Bet state is stored on-chain with pending status
  2. Reveal Phase (fulfill_bet):

    • ORAO VRF generates cryptographically verifiable randomness
    • Random number is mapped to 0-99 range
    • Win/loss is determined deterministically
    • Payout is calculated and transferred if won
  3. Verification:

    • Anyone can verify the VRF result off-chain using ORAO's verification tools
    • Bet state account contains all necessary data for verification
    • VRF request account stores the cryptographic proof

🏗 Architecture

Program Structure

programs/fair-dice-game/
├── src/
│   ├── lib.rs          # Main program entry point
│   ├── state.rs        # Account structs (GameConfig, BetState)
│   └── error.rs        # Custom error types

Key Accounts

  • GameConfig: Stores house settings, authority, statistics
  • BetState: Per-bet tracking with VRF request ID, status, result
  • Vault PDA: Holds house funds (SOL or SPL tokens)
  • VRF Request: Managed by ORAO program, contains randomness proof

Instructions

  1. initialize: Create game config and set house parameters
  2. place_bet: Transfer bet to vault, request VRF, create bet state
  3. fulfill_bet: Consume VRF result, determine outcome, transfer payout
  4. withdraw_house: House authority withdraws profits
  5. update_config: Update house edge, timeout, fee wallet

📧 Support

About

Casino game: VRF fair dice smart contract on Solana

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages