An open API service indexing awesome lists of open source software.

https://github.com/lf1up/slot_machine

Solana VRF Slot Machine
https://github.com/lf1up/slot_machine

blockchain cryptocurrency igaming smart-contracts solana web3

Last synced: about 1 month ago
JSON representation

Solana VRF Slot Machine

Awesome Lists containing this project

README

          

# ๐ŸŽฐ Solana Slot Machine with Multi-Tier Security

A **production-ready, cryptographically secure** slot machine built on Solana using Anchor framework, featuring **three levels of security** including Switchboard VRF readiness for verifiable randomness.

## ๐Ÿ” Security Architecture

### โœ… **Level 1: Switchboard VRF Ready** (Ultimate Security)
- **Framework implemented** - ready for VRF callback integration
- **Cryptographic proofs** will ensure randomness is genuine and verifiable on-chain
- **Impossible to manipulate** - even validators cannot predict outcomes

### โœ… **Level 2: Commit-Reveal Scheme** (High Security - Current Default)
- **Two-phase protection** against MEV attacks and front-running
- **SHA-256 cryptographic hashing** with multiple entropy sources
- **Minimum delay enforcement** (2 seconds for testing, configurable for production)
- **Anti-replay protection** - each commitment can only be revealed once

### โœ… **Level 3: Legacy Spin** (Basic Security - Deprecated)
- **Single transaction** implementation for compatibility
- **Marked as deprecated** in code with security warnings
- **Not recommended** for production use

## ๐Ÿš€ Current Implementation Status

### Fully Implemented โœ…
- **Commit-Reveal security scheme** with cryptographic verification
- **Multi-entropy randomness** combining player secrets and network state
- **Treasury management** with PDA-based authorization
- **Comprehensive test suite** covering all security scenarios
- **Modern UI** with real-time wallet integration and animations

### Framework Ready ๐Ÿ”ง
- **Switchboard VRF integration** (instruction handlers implemented, callback integration pending)
- **Randomness client** account structure and management
- **VRF request/consume flow** (ready for Switchboard callback implementation)

### Deprecated โš ๏ธ
- **Legacy spin function** (kept for backward compatibility only)

## ๐ŸŽฎ How to Play (Current Implementation)

### Recommended: Commit-Reveal Method
```typescript
// 1. Generate commitment
const secretValue = crypto.randomInt(1000000);
const salt = crypto.randomInt(1000000);
const nonce = new BN(Date.now());

// 2. Create commitment hash
const hasher = createHash('sha256');
hasher.update(Buffer.from(secretValue.toString()));
hasher.update(Buffer.from(salt.toString()));
hasher.update(playerPublicKey.toBuffer());
const commitmentHash = hasher.digest();

// 3. Submit commitment
await program.methods
.commit(Array.from(commitmentHash), betAmount, nonce)
.accounts({
gameConfig,
treasury,
commitment: commitmentPDA,
player: playerPublicKey,
systemProgram: SystemProgram.programId,
})
.signers([playerKeypair])
.rpc();

// 4. Wait minimum delay (2 seconds for testing)
await new Promise(resolve => setTimeout(resolve, 2000));

// 5. Reveal and spin
await program.methods
.revealAndSpin(new BN(secretValue), new BN(salt))
.accounts({
gameConfig,
treasury,
commitment: commitmentPDA,
player: playerPublicKey,
systemProgram: SystemProgram.programId,
})
.signers([playerKeypair])
.rpc();
```

### Future: Switchboard VRF (When Fully Integrated)
```typescript
// 1. Create commitment (same as above)
await program.methods.commit(commitmentHash, betAmount, nonce);

// 2. Request VRF randomness
await program.methods.requestRandomness()
.accounts({
player: playerPublicKey,
authority: authorityPublicKey,
commitment: commitmentPDA,
randomnessClient: randomnessClientPDA,
});

// 3. Switchboard VRF callback provides verifiable randomness
// (This will be called automatically by Switchboard)
await program.methods.consumeRandomness(secretValue, salt);
```

## ๐Ÿ† Payout Structure

| Outcome | Probability | Multiplier | Description |
|---------|-------------|------------|-------------|
| **JACKPOT!** | 0.5% | 25x | Ultimate win! |
| **Big Win!** | 1.5% | 10x | Fantastic! |
| **Great!** | 3% | 6x | Excellent! |
| **Nice!** | 5% | 3x | Good win! |
| **Win!** | 10% | 2x | Double up! |
| **Break Even** | 15% | 1x | Get your bet back |
| **Try Again!** | 65% | 0x | Better luck next time |

**Expected Return: ~89%** (11% house edge - typical for casino games)

## ๐Ÿ—๏ธ Architecture

### Account Structures
```rust
pub struct Commitment {
pub player: Pubkey, // Player who made the commitment
pub commitment_hash: [u8; 32], // SHA-256 hash of secret + salt + player
pub bet_amount: u64, // Locked bet amount
pub timestamp: i64, // Commitment creation time
pub revealed: bool, // Prevents double reveals
pub bump: u8, // PDA bump seed
pub nonce: u64, // Unique nonce for PDA derivation
pub randomness_requested: bool, // Switchboard VRF request status
}

pub struct RandomnessClient {
pub authority: Pubkey, // Admin authority
pub bump: u8, // PDA bump seed
pub use_switchboard: bool, // Enable/disable Switchboard VRF
}
```

### Current Instruction Set
1. **initialize()** - Set up game configuration and treasury
2. **init_randomness_client()** - Initialize Switchboard VRF client
3. **commit()** - Create cryptographic commitment
4. **request_randomness()** - Request VRF (framework ready)
5. **consume_randomness()** - Process VRF callback (framework ready)
6. **reveal_and_spin()** - Secure commit-reveal without VRF
7. **spin()** - Legacy function (deprecated)

## ๐Ÿ”ง Development Setup

### Prerequisites
- **Rust** (latest stable)
- **Solana CLI** (v1.16+)
- **Anchor Framework** (v0.31.1)
- **Node.js** (v18+)

### Installation
```bash
# Clone repository
git clone
cd slot_machine

# Install dependencies
npm install

# Build program
anchor build

# Run tests
anchor test

# Start UI (demo mode)
npm run dev
```

## ๐Ÿงช Current Test Coverage

Our test suite validates the implemented security features:

```
โœ… Initialization and setup
โœ… Commitment creation and validation
โœ… Randomness client initialization
โœ… VRF request framework (ready for callback integration)
โœ… Secure commit-reveal flow
โœ… Double reveal protection
โœ… Insufficient delay protection
โœ… Invalid commitment protection
โœ… Player authorization verification
```

### Security Features Tested
- โœ… **Anti-replay attacks** (prevents double reveals)
- โœ… **Timing attack prevention** (minimum delay enforcement)
- โœ… **Invalid secret protection** (cryptographic verification)
- โœ… **MEV attack mitigation** (commit-reveal scheme)
- โœ… **Randomness quality** (multiple entropy sources)

## ๐ŸŒŸ Current Features

### Implemented Security Features
1. **๐ŸŽฏ Commit-Reveal Protection**: Advanced MEV and timing attack prevention
2. **โšก Cryptographic Verification**: SHA-256 hash validation
3. **๐Ÿ›ก๏ธ Anti-Replay Defense**: Single-use commitment system
4. **๐Ÿ”„ Multi-Entropy Randomness**: Player secrets + network state
5. **๐Ÿงช Comprehensive Testing**: Edge cases and security scenarios covered

### UI Features (Demo Implementation)
- **๐Ÿ”— Wallet Integration**: Connect with Phantom and other Solana wallets
- **๐ŸŽฐ Animated Slot Machine**: CSS animations with emoji reels
- **๐Ÿ”’ Commit-Reveal Flow**: Demonstrates secure gaming mechanism
- **๐Ÿ’Ž Visual Feedback**: Real-time game state and result display
- **๐Ÿ“ฑ Responsive Design**: Works on desktop and mobile

## ๐Ÿš€ Production Deployment Guide

### For Mainnet Deployment:

1. **Update Configuration**:
```rust
// In lib.rs - increase delay for production
const MIN_DELAY_SECONDS: i64 = 30; // 30 seconds for mainnet
```

2. **Security Audit**: Professional security review recommended

3. **Treasury Setup**: Multi-signature authority for fund management

4. **Switchboard Integration**: Complete VRF callback implementation

5. **Monitoring**: Set up automated monitoring for unusual patterns

## ๐Ÿ”ฎ Next Steps

### High Priority
- [ ] **Complete Switchboard VRF callback integration**
- [ ] **Professional security audit**
- [ ] **Production treasury management**

### Medium Priority
- [ ] **Rate limiting implementation**
- [ ] **Advanced monitoring and alerting**
- [ ] **Gas optimization**

### Future Enhancements
- [ ] **Multi-game support**
- [ ] **Progressive jackpots**
- [ ] **NFT integration**

## โš ๏ธ Current Limitations

1. **Switchboard VRF**: Framework ready, callback integration pending
2. **UI Demo Mode**: Simulated transactions, not connected to actual program
3. **Single Game Type**: Only slot machine implemented
4. **Testing Configuration**: 2-second delay (should be 30+ seconds for production)

## ๐Ÿ“„ License

MIT License - see LICENSE file for details.

## ๐Ÿค Contributing

1. Fork the repository
2. Create feature branch
3. Implement changes with tests
4. Submit pull request

## โš ๏ธ Disclaimer

This software is for educational and research purposes. Always ensure compliance with local gambling laws and conduct professional security audits before production deployment.

---

**๐ŸŽฐ Current Status: High-security commit-reveal implementation ready, with Switchboard VRF framework prepared for ultimate security.**