Skip to content

roswelly/casino-coinflip-game

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Solana Coinflip Casino Game

A decentralized coinflip casino game built on the Solana . Players can bet SOL on coin flips (heads or tails) with a 50/50 chance to win double their bet amount.

Solana Next.js TypeScript Rust Anchor image

Contract Address

  • BX51pEVZAgWqxvgrPfX9yDiiU4WTKs4PZT34M5MjCRuc

Features

  • Simple Coinflip Game: Bet on heads or tails with a 50/50 chance
  • SOL Betting: Bet using Solana's native SOL token
  • Modern UI: Beautiful, responsive interface with dark/light mode
  • Secure: Built on Solana blockchain with smart contract security
  • Game History: View recent games and transaction history
  • Instant Rewards: Win 2x your bet amount (plus fee refund)
  • Fair Play: Transparent random number generation using blockchain data

Project Structure

casino-coinflip-game/
β”œβ”€β”€ fe/                    # Frontend (Next.js + TypeScript)
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ components/    # React components
β”‚   β”‚   β”œβ”€β”€ contexts/      # Blockchain interactions & utilities
β”‚   β”‚   β”œβ”€β”€ pages/         # Next.js pages
β”‚   β”‚   β”œβ”€β”€ styles/        # SCSS stylesheets
β”‚   β”‚   └── idl/           # Anchor IDL files
β”‚   └── package.json
β”‚
β”œβ”€β”€ sc/                    # Smart Contract (Anchor + Rust)
β”‚   β”œβ”€β”€ programs/
β”‚   β”‚   └── coinflip/      # Main Solana program
β”‚   β”œβ”€β”€ tests/             # Test files
β”‚   β”œβ”€β”€ migrations/        # Deployment scripts
β”‚   └── Anchor.toml        # Anchor configuration
β”‚
└── README.md

Tech Stack

Frontend

  • Framework: Next.js 12.1.0
  • Language: TypeScript
  • UI: Material-UI, Custom SCSS
  • Wallet: Solana Wallet Adapter
  • Blockchain: @solana/web3.js, @project-serum/anchor

Smart Contract

  • Framework: Anchor 0.31.1
  • Language: Rust
  • Network: Solana Devnet/Mainnet

Prerequisites

Before you begin, ensure you have the following installed:

  • Node.js (v16 or higher)
  • Yarn or npm
  • Rust (latest stable version)
  • Solana CLI (v1.18.0 or higher)
  • Anchor CLI (v0.31.1)
  • Git

Installing Prerequisites

Solana CLI

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

Anchor CLI

cargo install --git https://github.com/coral-xyz/anchor avm --locked --force
avm install latest
avm use latest

Quick Start

1. Clone the Repository

git clone <repository-url>
cd casino-coinflip-game

2. Smart Contract Setup

cd sc

# Install dependencies
yarn install

# Build the program
anchor build

# Run tests
anchor test

# Deploy to devnet (make sure your wallet is configured)
anchor deploy

Note: Update Anchor.toml with your program ID and wallet path before deploying.

3. Frontend Setup

cd fe

# Install dependencies
npm install

# Start development server
npm run dev

The frontend will be available at http://localhost:3000

4. Configure Program ID

After deploying the smart contract, update the program ID in:

  • fe/src/contexts/type.ts - Update PROGRAM_ID
  • fe/src/idl/coinflip.json - Update with new IDL (copy from sc/target/idl/coinflip.json)

Configuration

Smart Contract Configuration

Edit sc/Anchor.toml:

[programs.devnet]
coinflip = "YOUR_PROGRAM_ID"

[provider]
cluster = "devnet"  # or "mainnet"
wallet = "~/.config/solana/id.json"

Frontend Configuration

Edit fe/src/contexts/type.ts:

export const PROGRAM_ID = new PublicKey("YOUR_PROGRAM_ID");
export const NETWORK = "devnet";  // or "mainnet"

How to Play

  1. Connect Wallet: Click the wallet button and connect your Solana wallet (Phantom, Solflare, etc.)

  2. Enter Bet Amount: Input your desired bet amount in SOL

  3. Flip Coin: Click "FLIP COIN" button (the game randomly selects heads or tails)

  4. Wait for Result: The coin will flip and show the result

  5. Win or Lose:

    • Win: You receive 2x your bet amount (plus fee refund)
    • Lose: You lose your bet amount and fee
  6. Claim Rewards: Click the claim button to withdraw your winnings

Security Features

  • PDA (Program Derived Address): Vault uses PDAs for secure fund storage
  • Admin Controls: Only authorized admin can update settings
  • Balance Validation: Checks sufficient balance before allowing bets
  • Vault Validation: Ensures vault has enough funds for payouts
  • Random Generation: Uses blockchain timestamp and slot for randomness

Game Mechanics

Betting

  • Minimum bet: Defined in smart contract constants
  • Maximum bet: Limited by vault balance
  • Fee: 4% of bet amount (configurable by admin)
  • Win multiplier: 2x bet amount + fee refund

Randomness

The game uses blockchain data (timestamp + slot) to generate random results, ensuring fairness and transparency.

Rewards

  • Winners receive: bet_amount * 2 + fee
  • Losers lose: bet_amount + fee
  • Fee goes to loyalty wallet

Testing

Smart Contract Tests

cd sc
anchor test

Frontend Development

cd fe
npm run dev

Deployment

Deploy Smart Contract

cd sc

# Build
anchor build

# Deploy to devnet
anchor deploy --provider.cluster devnet

# Deploy to mainnet (be careful!)
anchor deploy --provider.cluster mainnet

Deploy Frontend

cd fe

# Build for production
npm run build

# Start production server
npm run start

Or deploy to platforms like:

  • Vercel: Connect your GitHub repo
  • Netlify: Use the Netlify plugin
  • Any static hosting: Upload the out folder after npm run build

Development

Smart Contract Development

cd sc

# Build
anchor build

# Test
anchor test

# View logs
solana logs

Frontend Development

cd fe

# Development server with hot reload
npm run dev

# Build
npm run build

# Lint
npm run lint

Important Addresses

  • Program ID: BX51pEVZAgWqxvgrPfX9yDiiU4WTKs4PZT34M5MjCRuc (Devnet)
  • Network: Solana Devnet (configurable)

Troubleshooting

Common Issues

  1. "Program ID mismatch"

    • Update PROGRAM_ID in fe/src/contexts/type.ts
    • Update IDL in fe/src/idl/coinflip.json
  2. "Insufficient funds"

    • Ensure your wallet has enough SOL for transaction fees
    • Check vault balance for rewards
  3. "Global state not initialized"

    • Run the initialization function (admin only)
    • Check that the global pool account exists
  4. Build errors

    • Ensure Rust and Anchor versions match
    • Run anchor clean and rebuild

πŸ“– Documentation

  • See INDEX.md for detailed project documentation
  • See fe/README.md for frontend-specific docs
  • See sc/README.md for smart contract docs

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

This project is licensed under the MIT License - see the LICENSE file for details.

Disclaimer

This is a gambling application. Please gamble responsibly. The developers are not responsible for any losses incurred while using this application.

Acknowledgments

  • Solana Foundation
  • Anchor Framework
  • Solana Wallet Adapter Team

Support

For issues and questions:

  • Open an issue on GitHub
  • Check existing documentation in INDEX.md

Built with me on Solana

About

solana coinflip game

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •