A React-based racing game where players earn ERC-20 tokens based on their racing performance. Built with Next.js and integrated with thirdweb API for authentication and token distribution.
- Real-time Racing Game: Smooth 2D racing with obstacle avoidance
- Performance-based Rewards: Earn tokens based on race completion time and performance
- Anti-cheat System: Server-side verification before token distribution
- thirdweb Integration: Secure authentication and smart contract interactions
- Modern UI: Beautiful, responsive design with Tailwind CSS
- Achievement System: Track progress and unlock milestones
- Node.js 18+
- npm or yarn
- thirdweb account and API keys
-
Clone the repository
git clone <your-repo-url> cd crypto-racer
-
Install dependencies
npm install
-
Set up environment variables Create a
.env.local
file in the root directory:# Thirdweb API Configuration THIRDWEB_SECRET_KEY=your_secret_key_here TOKEN_CONTRACT_ADDRESS=0x...your_token_contract_address CHAIN_ID=84532 ADMIN_ADDRESS=0x...your_admin_wallet_address # Game Configuration GAME_REWARD_BASE=100 GAME_REWARD_MAX=500 GAME_VERIFICATION_WINDOW=86400000
-
Run the development server
npm run dev
-
Open your browser Navigate to http://localhost:3000
- Login: Enter your email to authenticate with thirdweb
- Start Racing: Press SPACE to begin a race
- Control: Use A/D or Arrow Keys to move left/right
- Objective: Avoid red obstacles and reach 1000 points
- Earn Tokens: Complete races to earn ERC-20 tokens
- Claim Rewards: Use the reward system to claim your tokens
- RacingGame: Main game logic with collision detection and scoring
- AuthComponent: thirdweb authentication integration
- RewardSystem: Token claiming and distribution
- GameStats: Performance tracking and achievements
/api/claim-rewards
: Server-side verification and token distribution- Anti-cheat Protection: Validates game performance before rewards
- thirdweb Integration: Secure smart contract interactions
- Scoring System: Points based on survival time and obstacle avoidance
- Difficulty Scaling: Obstacles move faster as score increases
- Performance Tracking: Records best times and total races
- Reward Calculation: Tokens based on time, performance, and race count
- Server-side Verification: All game completions verified before rewards
- Timestamp Validation: Prevents replay attacks
- Performance Validation: Ensures reasonable game statistics
- Rate Limiting: Built-in protection against abuse
- Base Reward: 100 tokens per completed race
- Time Bonus: Up to 50 tokens for faster completion
- Performance Bonus: 10 tokens per race completed
- Maximum Reward: Capped at 500 tokens per session
- Game completion data sent to server
- Server verifies performance and calculates rewards
- thirdweb API called to mint/transfer tokens
- Transaction hash returned to user
THIRDWEB_SECRET_KEY
: Your thirdweb API secret keyTOKEN_CONTRACT_ADDRESS
: ERC-20 token contract addressCHAIN_ID
: Target blockchain network IDADMIN_ADDRESS
: Admin wallet for token distribution
GAME_REWARD_BASE
: Base tokens per raceGAME_REWARD_MAX
: Maximum tokens per sessionGAME_VERIFICATION_WINDOW
: Time window for valid sessions
crypto-racer/
โโโ app/ # Next.js app directory
โ โโโ api/ # API routes
โ โโโ components/ # React components
โ โโโ globals.css # Global styles
โ โโโ layout.tsx # Root layout
โ โโโ page.tsx # Main page
โโโ components/ # Shared components
โโโ package.json # Dependencies
โโโ tailwind.config.ts # Tailwind configuration
โโโ tsconfig.json # TypeScript configuration
npm run dev
: Start development servernpm run build
: Build for productionnpm run start
: Start production servernpm run lint
: Run ESLint
Modify obstacle spawn rates and speeds in RacingGame.tsx
:
// Obstacle spawn rate
if (Math.random() < 0.02) { ... }
// Speed increase
setObstacleSpeed(prev => Math.min(prev + 0.5, 8))
Adjust token calculations in RewardSystem.tsx
:
const baseReward = 100
const timeBonus = Math.max(0, 50 - Math.floor(gameStats.bestTime / 1000))
const performanceBonus = Math.floor(gameStats.totalRaces * 10)
Customize colors and animations in tailwind.config.ts
:
colors: {
'race-red': '#e74c3c',
'race-blue': '#3498db',
'race-green': '#2ecc71',
}
- Connect your GitHub repository
- Set environment variables in Vercel dashboard
- Deploy automatically on push
- Build the project:
npm run build
- Set environment variables
- Deploy the
out
directory
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- Issues: Create a GitHub issue
- Documentation: Check the thirdweb API docs
- Community: Join our Discord server
- Multiplayer racing
- NFT car skins
- Leaderboards
- Tournament system
- Mobile optimization
- More game modes
Built with โค๏ธ using Next.js, React, and thirdweb