Token Snatcher (Whack-to-Earn)
A decentralized arcade game where players โsnatchโ tokens before they disappear and earn real on-chain rewards.
This project demonstrates:
Skill-based Web3 gaming
Secure score validation
On-chain reward distribution
Transparent leaderboard
NFT-powered multipliers
๐ Project Structure token-snatcher/ โ โโโ frontend/ โ Next.js game client โโโ backend/ โ NestJS API server โโโ contract/ โ Soroban smart contract โ โโโ README.md ๐ฅ FRONTEND โ Next.js ๐ Overview
The frontend is responsible for:
Rendering the game UI
Handling player interactions
Connecting wallet
Starting game sessions
Submitting scores
Displaying leaderboard
It does NOT control rewards. All rewards are validated on-chain.
๐ Tech Stack
Next.js
TypeScript
Phaser.js (game engine)
Stellar Wallet integration (Freighter compatible)
Tailwind (optional styling)
๐ Folder Structure frontend/ โ โโโ app/ โ โโโ page.tsx โ โโโ leaderboard/ โ โโโ ranked/ โ โโโ free/ โ โโโ components/ โ โโโ GameCanvas.tsx โ โโโ WalletButton.tsx โ โโโ ScoreBoard.tsx โ โโโ Leaderboard.tsx โ โโโ game/ โ โโโ scenes/ โ โ โโโ GameScene.ts โ โ โโโ ResultScene.ts โ โ โ โโโ spawnLogic.ts โ โโโ scoring.ts โ โโโ services/ โ โโโ contract.service.ts โ โโโ wallet.service.ts โ โโโ api.service.ts โ โโโ utils/ โ โโโ constants.ts โ โโโ package.json ๐ฎ Game Flow (Ranked Mode)
Player connects wallet
Player clicks โStart Ranked Gameโ
Frontend calls contract โ start_session()
Contract returns session ID + randomness seed
Game starts using that seed
Player finishes round
Frontend calls โ submit_score()
Smart contract validates score
Reward distributed automatically
โถ Installation cd frontend npm install npm run dev
App runs at:
http://localhost:3000 ๐ฅ BACKEND โ NestJS ๐ Overview
The backend is optional but recommended.
It handles:
Leaderboard caching
Tournament scheduling
Analytics
Bot detection
Listening to blockchain events
The backend does NOT control rewards.
๐ Tech Stack
NestJS
TypeScript
PostgreSQL
Stellar SDK
๐ Folder Structure backend/ โ โโโ src/ โ โโโ modules/ โ โ โโโ game/ โ โ โ โโโ game.controller.ts โ โ โ โโโ game.service.ts โ โ โ โ โ โโโ leaderboard/ โ โ โ โโโ leaderboard.controller.ts โ โ โ โโโ leaderboard.service.ts โ โ โ โ โ โโโ tournaments/ โ โ โ โโโ blockchain/ โ โ โโโ stellar.service.ts โ โ โโโ contract.listener.ts โ โ โ โโโ database/ โ โ โโโ entities/ โ โ โ โโโ main.ts โ โโโ package.json ๐ Backend Responsibilities 1๏ธโฃ Listen to Smart Contract Events
New session started
Score submitted
Reward paid
2๏ธโฃ Cache Leaderboard
Instead of querying blockchain every time.
3๏ธโฃ Anti-Bot Monitoring
Detect abnormal play frequency.
โถ Installation cd backend npm install npm run start:dev
Runs at:
http://localhost:4000 ๐ฆ SMART CONTRACT โ Stellar (Soroban) ๐ Overview
The smart contract is the core of the system.
It handles:
Game sessions
Random seed generation
Score validation
Reward distribution
Leaderboard updates
NFT multiplier logic
All financial logic lives here.
๐ Folder Structure contract/ โ โโโ src/ โ โโโ lib.rs โ โโโ game.rs โ โโโ rewards.rs โ โโโ leaderboard.rs โ โโโ storage.rs โ โโโ errors.rs โ โโโ tests/ โ โโโ test_game.rs โ โโโ test_rewards.rs โ โโโ test_leaderboard.rs โ โโโ Cargo.toml โโโ README.md ๐ Core Contract Functions start_session(player: Address) -> u64
submit_score(session_id: u64, score: u32)
claim_reward(session_id: u64)
get_leaderboard() -> Vec ๐ Anti-Cheat Mechanism
The contract:
Generates randomness seed
Stores start timestamp
Enforces time limit
Calculates max theoretical score
Rejects impossible values
Prevents duplicate submissions
Frontend scores are verified mathematically.
โถ Build Contract
cd contract
cargo build --target wasm32-unknown-unknown --release
๐ Deploy to Testnet
soroban contract deploy
--wasm target/wasm32-unknown-unknown/release/token_snatcher.wasm
--source your-identity
๐ How Everything Works Together
Player clicks token
โ
Frontend updates score
โ
Player submits score
โ
Contract verifies logic
โ
Reward distributed
โ
Backend caches event
โ
Leaderboard updates