|
| 1 | +# Anchor Rock Paper Scissor |
| 2 | + |
| 3 | +A confidential Rock Paper Scissor game built on Solana using Anchor and MagicBlock's Ephemeral Rollups SDK. This example demonstrates how to implement a two-player game with hidden choices that remain private during gameplay until the winner is revealed. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This project showcases: |
| 8 | +- **Solana Smart Contract**: Built with Anchor framework |
| 9 | +- **Confidentiality**: Player choices are hidden using MagicBlock's Ephemeral Rollups |
| 10 | +- **On-chain Game Logic**: Automated winner determination with transparent results |
| 11 | +- **Permission System**: Fine-grained access control via the ephemeral rollups SDK |
| 12 | + |
| 13 | +## Versioning |
| 14 | + |
| 15 | +The following software packages may be required, other versions may also be compatible: |
| 16 | + |
| 17 | +| Software | Version | Installation Guide | |
| 18 | +|----------|---------|-------------------| |
| 19 | +| Solana | 2.3.13 | [Install Solana](https://docs.solana.com/cli/install-solana-cli-tools) | |
| 20 | +| Rust | 1.85.0 | [Install Rust](https://www.rust-lang.org/tools/install) | |
| 21 | +| Anchor | 0.32.1 | [Install Anchor](https://www.anchor-lang.com/docs/installation) | |
| 22 | +| Node | 24.10.0 | [Install Node](https://nodejs.org/) | |
| 23 | + |
| 24 | +## Prerequisites |
| 25 | + |
| 26 | +- **Rust**: 1.85.0 |
| 27 | +- **Node.js**: 24.10.0 |
| 28 | +- **Solana CLI**: 2.3.13 |
| 29 | +- **Anchor CLI**: 0.32.1 |
| 30 | +- **Yarn**: Package manager (or npm) |
| 31 | + |
| 32 | +### Installation |
| 33 | + |
| 34 | +1. Install Rust: |
| 35 | +```bash |
| 36 | +curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh |
| 37 | +``` |
| 38 | + |
| 39 | +2. Install Solana CLI: |
| 40 | +```bash |
| 41 | +sh -c "$(curl -sSfL https://release.solana.com/v1.18.0/install)" |
| 42 | +``` |
| 43 | + |
| 44 | +3. Install Anchor: |
| 45 | +```bash |
| 46 | +cargo install --git https://github.com/coral-xyz/anchor avm --locked --force |
| 47 | +avm install latest |
| 48 | +avm use latest |
| 49 | +``` |
| 50 | + |
| 51 | +4. Configure Solana (optional, for devnet): |
| 52 | +```bash |
| 53 | +solana config set --url devnet |
| 54 | +``` |
| 55 | + |
| 56 | +## Project Structure |
| 57 | + |
| 58 | +``` |
| 59 | +anchor-rock-paper-scissor/ |
| 60 | +├── programs/ |
| 61 | +│ └── anchor-rock-paper-scissor/ |
| 62 | +│ ├── src/ |
| 63 | +│ │ └── lib.rs # Program logic |
| 64 | +│ └── Cargo.toml |
| 65 | +├── tests/ |
| 66 | +│ └── anchor-rock-paper-scissor.ts # Test suite |
| 67 | +├── Anchor.toml # Anchor configuration |
| 68 | +├── Cargo.toml # Workspace configuration |
| 69 | +└── package.json # Node dependencies |
| 70 | +``` |
| 71 | + |
| 72 | +## Build |
| 73 | + |
| 74 | +Build the Solana program: |
| 75 | + |
| 76 | +```bash |
| 77 | +anchor build |
| 78 | +``` |
| 79 | + |
| 80 | +This will: |
| 81 | +- Compile the Rust program |
| 82 | +- Generate TypeScript IDL (Interface Definition Language) |
| 83 | +- Output artifacts to the `target/` directory |
| 84 | + |
| 85 | +## Deployment |
| 86 | + |
| 87 | +### Deploy to Devnet |
| 88 | + |
| 89 | +1. Set your wallet (if not already configured): |
| 90 | +```bash |
| 91 | +solana config set --keypair ~/.config/solana/id.json |
| 92 | +``` |
| 93 | + |
| 94 | +2. Update `Anchor.toml` with your program ID (after first deployment) |
| 95 | + |
| 96 | +3. Deploy: |
| 97 | +```bash |
| 98 | +anchor deploy --provider.cluster devnet |
| 99 | +``` |
| 100 | + |
| 101 | +The deployment will output your program ID. Update `Anchor.toml` and redeploy with the correct ID. |
| 102 | + |
| 103 | +### Deploy to Localnet |
| 104 | + |
| 105 | +Start a local Solana validator: |
| 106 | +```bash |
| 107 | +solana-test-validator |
| 108 | +``` |
| 109 | + |
| 110 | +In another terminal, deploy: |
| 111 | +```bash |
| 112 | +anchor deploy --provider.cluster localnet |
| 113 | +``` |
| 114 | + |
| 115 | +## Testing |
| 116 | + |
| 117 | +### Install Dependencies |
| 118 | + |
| 119 | +```bash |
| 120 | +yarn install |
| 121 | +``` |
| 122 | + |
| 123 | +### Run Tests |
| 124 | + |
| 125 | +Run the full test suite: |
| 126 | + |
| 127 | +```bash |
| 128 | +yarn test |
| 129 | +``` |
| 130 | + |
| 131 | +The test suite includes: |
| 132 | +1. **Airdrop SOL** - Fund test players |
| 133 | +2. **Create Game** - Player 1 initiates a game |
| 134 | +3. **Join Game** - Player 2 joins the game |
| 135 | +4. **Make Choices** - Both players privately make their choices |
| 136 | +5. **Verify Privacy** - Confirm choices remain hidden from opponent |
| 137 | +6. **Reveal Winner** - Determine and announce the game winner |
| 138 | + |
| 139 | +### Custom Test Endpoint |
| 140 | + |
| 141 | +To test against a custom ephemeral rollup endpoint: |
| 142 | + |
| 143 | +```bash |
| 144 | +EPHEMERAL_PROVIDER_ENDPOINT=http://your-endpoint:port \ |
| 145 | +EPHEMERAL_WS_ENDPOINT=ws://your-endpoint:port \ |
| 146 | +yarn test |
| 147 | +``` |
| 148 | + |
| 149 | +## Usage |
| 150 | + |
| 151 | +### Game Flow |
| 152 | + |
| 153 | +1. **Player 1 creates a game** with a unique game ID |
| 154 | +2. **Player 2 joins** the same game |
| 155 | +3. **Both players make hidden choices** (Rock, Paper, or Scissors) |
| 156 | +4. **Choices are encrypted** in the ephemeral rollup |
| 157 | +5. **Winner is revealed** - game logic determines the winner |
| 158 | +6. **Results are finalized** on-chain |
| 159 | + |
| 160 | +### Example Test Output |
| 161 | + |
| 162 | +``` |
| 163 | +Program ID: <program-address> |
| 164 | +Game ID (u64): 1706309545000 |
| 165 | +... |
| 166 | +✅ Game Created: <tx-hash> |
| 167 | +✅ Player 2 joined game <id>: <tx-hash> |
| 168 | +✅ Player 1 chose {"rock":{}}: <tx-hash> |
| 169 | +✅ Player 2 chose {"paper":{}}: <tx-hash> |
| 170 | +✅ Reveal Winner TX Sent: <tx-hash> |
| 171 | +🎲 Game Result Account Data: {winner: 2, player1Choice: {...}, player2Choice: {...}} |
| 172 | +``` |
| 173 | + |
| 174 | +## Key Concepts |
| 175 | + |
| 176 | +### Ephemeral Rollups |
| 177 | + |
| 178 | +Player choices are processed in MagicBlock's Ephemeral Rollups, which provides: |
| 179 | +- **TEE Execution**: Encrypted execution environment |
| 180 | +- **Privacy**: Other players cannot see choices until revealed |
| 181 | +- **Finality**: Results are committed to Solana mainnet |
| 182 | + |
| 183 | +### Program Accounts |
| 184 | + |
| 185 | +- **Game Account**: Stores game state and result |
| 186 | +- **PlayerChoice Account**: Stores a player's encrypted choice (PDAs) |
| 187 | +- **Permission Account**: Controls access to encrypted data |
| 188 | + |
| 189 | +### Anchor Instructions |
| 190 | + |
| 191 | +- `create_game` - Initialize a new game |
| 192 | +- `join_game` - Add the second player |
| 193 | +- `make_choice` - Submit encrypted choice |
| 194 | +- `create_permission` - Setup access control |
| 195 | +- `delegate_pda` - Delegate PDA to TEE validator |
| 196 | +- `reveal_winner` - Compute and store result |
| 197 | + |
| 198 | +## Environment Variables |
| 199 | + |
| 200 | +| Variable | Default | Description | |
| 201 | +|----------|---------|-------------| |
| 202 | +| `EPHEMERAL_PROVIDER_ENDPOINT` | `https://tee.magicblock.app` | Ephemeral rollup RPC endpoint | |
| 203 | +| `EPHEMERAL_WS_ENDPOINT` | `wss://tee.magicblock.app` | WebSocket endpoint for subscriptions | |
| 204 | + |
| 205 | +## Troubleshooting |
| 206 | + |
| 207 | +### Build Errors |
| 208 | + |
| 209 | +**Error: "anchor-lang not found"** |
| 210 | +- Run: `cargo update` |
| 211 | +- Ensure Rust is up to date: `rustup update` |
| 212 | + |
| 213 | +### Deployment Issues |
| 214 | + |
| 215 | +**Error: "Account does not have enough SOL"** |
| 216 | +- Airdrop SOL: `solana airdrop 10 <your-address> --url devnet` |
| 217 | + |
| 218 | +### Test Failures |
| 219 | + |
| 220 | +**Tests timeout or fail to connect** |
| 221 | +- Verify the ephemeral endpoint is reachable |
| 222 | +- Check your internet connection |
| 223 | +- Ensure the Solana cluster is available |
| 224 | + |
| 225 | +**Permission denied errors** |
| 226 | +- Ensure wallet has sufficient SOL for transaction fees |
| 227 | +- Check permission setup in test (game and choice PDAs) |
| 228 | + |
| 229 | +## Development |
| 230 | + |
| 231 | +### Modify Program Logic |
| 232 | + |
| 233 | +Edit `programs/anchor-rock-paper-scissor/src/lib.rs`: |
| 234 | + |
| 235 | +1. Update instruction handlers |
| 236 | +2. Run `anchor build` to compile |
| 237 | +3. Run `yarn test` to validate changes |
| 238 | + |
| 239 | +### Generate New Types |
| 240 | + |
| 241 | +After program changes: |
| 242 | +```bash |
| 243 | +anchor build --skip-lint |
| 244 | +``` |
| 245 | + |
| 246 | +This regenerates TypeScript types in `target/types/`. |
| 247 | + |
| 248 | +## References |
| 249 | + |
| 250 | +- [Anchor Documentation](https://www.anchor-lang.com/) |
| 251 | +- [Solana Documentation](https://docs.solana.com/) |
| 252 | +- [MagicBlock Ephemeral Rollups SDK](https://github.com/magicblock-labs/ephemeral-rollups-sdk) |
| 253 | +- [Solana Web3.js](https://github.com/solana-labs/solana-web3.js) |
| 254 | + |
| 255 | +## License |
| 256 | + |
| 257 | +MIT |
| 258 | + |
| 259 | +## Support |
| 260 | + |
| 261 | +For issues or questions: |
| 262 | +1. Check existing GitHub issues |
| 263 | +2. Review test logs for error details |
| 264 | +3. Ensure all prerequisites are installed |
| 265 | +4. Verify network connectivity to Solana endpoints |
0 commit comments