Skip to content

A trustless peer-to-peer betting DApp on the Polygon blockchain where players wager MATIC on real-time Tic-Tac-Toe games using cryptographic settlement proofs.

Notifications You must be signed in to change notification settings

88448844/polygon-p2p-tictactoe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

7 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŽฎ Polygon P2P Tic-Tac-Toe

                  โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•— โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—    โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—  โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—    โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•— 
                  โ•šโ•โ•โ–ˆโ–ˆโ•”โ•โ•โ•โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•”โ•โ•โ•โ•โ•    โ•šโ•โ•โ–ˆโ–ˆโ•”โ•โ•โ•โ–ˆโ–ˆโ•”โ•โ•โ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•”โ•โ•โ•โ•โ•    โ•šโ•โ•โ–ˆโ–ˆโ•”โ•โ•โ•โ–ˆโ–ˆโ•”โ•โ•โ•โ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•”โ•โ•โ•โ•โ•
                     โ–ˆโ–ˆโ•‘   โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘            โ–ˆโ–ˆโ•‘   โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘            โ–ˆโ–ˆโ•‘   โ–ˆโ–ˆโ•‘   โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—  
                     โ–ˆโ–ˆโ•‘   โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘            โ–ˆโ–ˆโ•‘   โ–ˆโ–ˆโ•”โ•โ•โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘            โ–ˆโ–ˆโ•‘   โ–ˆโ–ˆโ•‘   โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•”โ•โ•โ•  
                     โ–ˆโ–ˆโ•‘   โ–ˆโ–ˆโ•‘โ•šโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—       โ–ˆโ–ˆโ•‘   โ–ˆโ–ˆโ•‘  โ–ˆโ–ˆโ•‘โ•šโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—       โ–ˆโ–ˆโ•‘   โ•šโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•”โ•โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—
                     โ•šโ•โ•   โ•šโ•โ• โ•šโ•โ•โ•โ•โ•โ•       โ•šโ•โ•   โ•šโ•โ•  โ•šโ•โ• โ•šโ•โ•โ•โ•โ•โ•       โ•šโ•โ•    โ•šโ•โ•โ•โ•โ•โ• โ•šโ•โ•โ•โ•โ•โ•โ•

                                           โ•”โ•โ•โ•โ•โ•โ•โ•โ•ฆโ•โ•โ•โ•โ•โ•โ•โ•ฆโ•โ•โ•โ•โ•โ•โ•โ•—
                                           โ•‘       โ•‘       โ•‘       โ•‘
                                           โ•‘   X   โ•‘   O   โ•‘   X   โ•‘
                                           โ•‘       โ•‘       โ•‘       โ•‘
                                           โ• โ•โ•โ•โ•โ•โ•โ•โ•ฌโ•โ•โ•โ•โ•โ•โ•โ•ฌโ•โ•โ•โ•โ•โ•โ•โ•ฃ
                                           โ•‘       โ•‘       โ•‘       โ•‘
                                           โ•‘   O   โ•‘   X   โ•‘   O   โ•‘
                                           โ•‘       โ•‘       โ•‘       โ•‘
                                           โ• โ•โ•โ•โ•โ•โ•โ•โ•ฌโ•โ•โ•โ•โ•โ•โ•โ•ฌโ•โ•โ•โ•โ•โ•โ•โ•ฃ
                                           โ•‘       โ•‘       โ•‘       โ•‘
                                           โ•‘   X   โ•‘   O   โ•‘   X   โ•‘
                                           โ•‘       โ•‘       โ•‘       โ•‘
                                           โ•šโ•โ•โ•โ•โ•โ•โ•โ•ฉโ•โ•โ•โ•โ•โ•โ•โ•ฉโ•โ•โ•โ•โ•โ•โ•โ•

๐Ÿš€ The First Truly Decentralized P2P Betting Game ๐Ÿš€

Live Demo Smart Contract License

Bet MATIC. Play Tic-Tac-Toe. Winner Takes All.

๐ŸŽฎ Play Now โ€ข ๐Ÿ“– Docs โ€ข ๐Ÿ› Report Bug โ€ข โœจ Request Feature


๐ŸŽฏ What Is This?

A peer-to-peer betting platform where two players can wager MATIC on a Tic-Tac-Toe game. The smart contract acts as trustless escrow, holding funds until there's a winner. No house edge. No intermediaries. Just you, your opponent, and the blockchain.

๐ŸŒŸ Why It's Special

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  ๐Ÿ’ฐ Bet Real Money     ๐ŸŽฎ Play Classic Game     ๐Ÿ† Winner Gets 95% 
  ๐Ÿ” Trustless Escrow   โšก Instant Payouts      ๐ŸŽ‰ Epic Celebrations
  ๐ŸŒ Low Fees (5%)      ๐Ÿ’Ž Polygon Network      โšก Fast Settlement  
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  • ๐ŸŽฒ No Draws: If there's a draw, the board auto-resets until someone wins
  • โšก Real-Time: Live updates via polling - see your opponent's moves instantly
  • ๐Ÿ’ธ Fair Play: Smart contract ensures winner gets 1.9x their bet (5% hosting fee)
  • ๐ŸŽจ Beautiful UI: Minimalistic dark theme with confetti celebrations
  • ๐Ÿ“ฑ Mobile Ready: Play on any device with MetaMask

๐Ÿš€ Quick Start

1๏ธโƒฃ Prerequisites

2๏ธโƒฃ Play in 60 Seconds

1. ๐ŸŒ Open: https://p2p-tic-tac-toe-1063244275551.us-central1.run.app
2. ๐ŸฆŠ Connect MetaMask (auto-connects on load)
3. ๐Ÿ’ฐ Create game OR join with Match ID
4. ๐ŸŽฎ Play Tic-Tac-Toe
5. ๐Ÿ† Winner claims prize!

3๏ธโƒฃ How to Create a Game

// Player A
1. Enter bet amount (e.g., 0.1 MATIC โ‰ˆ $0.10)
2. Click "Create Game"
3. Sign MetaMask transaction
4. Share your 6-digit Match ID with opponent

4๏ธโƒฃ How to Join a Game

// Player B  
1. Enter the 6-digit Match ID
2. Enter same bet amount
3. Click "Join Game"
4. Sign MetaMask transaction
5. Game starts!

๐Ÿ’ก Features

- โœ… Auto wallet connection - โœ… Minimalistic dark theme - โœ… Fixed 3x3 grid (100px cells) - โœ… Copy-to-clipboard Match ID - โœ… Smooth animations

๐ŸŽฎ Gameplay

  • โœ… Classic Tic-Tac-Toe rules
  • โœ… Real-time move updates
  • โœ… Auto-restart on draw
  • โœ… 3-second confetti celebration
  • โœ… Toast notifications for all actions

๐Ÿ” Security

  • โœ… Smart contract escrow
  • โœ… Cryptographic winner verification
  • โœ… Backend signature authority
  • โœ… Cannot cheat or steal funds
  • โœ… Trustless settlement

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   MetaMask  โ”‚โ—„โ”€โ”€โ”€โ”€โ”€โ–บโ”‚   Frontend   โ”‚โ—„โ”€โ”€โ”€โ”€โ”€โ–บโ”‚     Backend     โ”‚
โ”‚  (Polygon)  โ”‚       โ”‚  (React)     โ”‚       โ”‚   (Node.js)     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ”‚                                               โ”‚
       โ”‚         Deposits & Claims                     โ”‚ Auto-signs
       โ”‚              โ†“                                 โ”‚ winners
       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                      โ†“
           โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
           โ”‚  UlaniWager Smart    โ”‚
           โ”‚  Contract (Polygon)  โ”‚
           โ”‚  Holds Funds Safely  โ”‚
           โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Tech Stack

Layer Technology
Frontend React, Vite, ethers.js, canvas-confetti
Backend Node.js, Express 4, ethers.js
Blockchain Polygon Mainnet
Smart Contract Solidity (UlaniWager)
Deployment Google Cloud Run
Wallet MetaMask

๐Ÿ” How It Works

The Smart Contract (Escrow)

// Player A creates game
createWager(matchId) payable
โ†’ Locks Player A's MATIC

// Player B joins
joinWager(matchId) payable  
โ†’ Locks Player B's MATIC (must match A's bet)

// Winner claims prize
settle(matchId, winner, signature)
โ†’ Verifies backend signature
โ†’ Pays 95% of total pot to winner (5% hosting fee)
โ†’ Winner receives 1.9x their original bet

The Backend (Referee)

// Tracks game state
- Board position
- Whose turn
- Win/draw detection

// Signs winners
signature = sign(chainId + matchId + winnerAddress)
โ†’ Cryptographic proof of win
โ†’ Only backend can create valid signatures

Security Model

Q: How do we prevent cheating?

A: Signature verification!

// Backend signs winner
const signature = backendWallet.sign(
  keccak256(chainId, matchId, winnerAddress)
);

// Contract verifies signature
address signer = ecrecover(signature);
require(signer == TRUSTED_BACKEND_ADDRESS);
// โœ… Only pays if signature is valid

๐Ÿ” Players can't forge signatures โ†’ Need backend's private key
๐Ÿ” Backend only signs actual winners โ†’ Verified game state
๐Ÿ” Contract mathematically verifies โ†’ Uses ecrecover


๐Ÿ“Š Game Flow

graph LR
    A[Player A Creates] -->|Deposit MATIC| B[Get Match ID]
    B --> C[Player B Joins]
    C -->|Deposit MATIC| D[Play Tic-Tac-Toe]
    D -->|Win| E[Winner Claims]
    D -->|Draw| F[Board Resets]
    F --> D
    E --> G[Get 2x Bet]
Loading

๐Ÿ› ๏ธ Local Development

Prerequisites

  • Node.js 18+
  • MetaMask with Polygon Mainnet
  • Git

Clone & Install

git clone https://github.com/88448844/polygon-p2p-tictactoe.git
cd polygon-p2p-tictactoe

# Install backend dependencies
cd backend
npm install

# Install frontend dependencies
cd ../frontend
npm install

Environment Setup

Backend (backend/.env):

PRIVATE_KEY=your_backend_wallet_private_key_without_0x
NODE_ENV=development

โš ๏ธ IMPORTANT: Never commit .env files! They're in .gitignore.

Frontend (frontend/.env):

VITE_API_URL=http://localhost:3000

Run Locally

# Terminal 1: Start backend
cd backend
node server.js

# Terminal 2: Start frontend
cd frontend
npm run dev

Open http://localhost:5173 and connect MetaMask to Polygon Mainnet!


๐Ÿš€ Deployment

Deploy to Google Cloud Run

# Setup
gcloud auth login
gcloud config set project YOUR_PROJECT_ID

# Create secret for private key
echo -n "your_private_key" | gcloud secrets create BETTING_PRIVATE_KEY --data-file=-

# Deploy
gcloud run deploy p2p-tic-tac-toe \
  --source . \
  --region us-central1 \
  --allow-unauthenticated \
  --set-env-vars "NODE_ENV=production" \
  --set-secrets "PRIVATE_KEY=BETTING_PRIVATE_KEY:latest"

See SPEC.md for detailed deployment guide.


๐Ÿ“– Documentation


๐Ÿค Contributing

Contributions are welcome! Here's how:

  1. Fork the repo
  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.


๐ŸŽ‰ Acknowledgments

  • Polygon Network - For fast & cheap transactions
  • MetaMask - For seamless wallet integration
  • React & Vite - For amazing DX
  • ethers.js - For blockchain interaction
  • Google Cloud Run - For serverless hosting

๐ŸŒŸ Star this repo if you like it! ๐ŸŒŸ

Made with โค๏ธ for the Polygon community

๐ŸŽฎ Play Now โ€ข ๐Ÿ“– Read Docs โ€ข ๐Ÿ› Report Issues

                                         โ•”โ•โ•โ•โ•โ•โ•โ•โ•ฆโ•โ•โ•โ•โ•โ•โ•โ•ฆโ•โ•โ•โ•โ•โ•โ•โ•—
                                         โ•‘       โ•‘       โ•‘       โ•‘
                                         โ•‘   X   โ•‘   O   โ•‘   X   โ•‘
                                         โ•‘       โ•‘       โ•‘       โ•‘
                                         โ• โ•โ•โ•โ•โ•โ•โ•โ•ฌโ•โ•โ•โ•โ•โ•โ•โ•ฌโ•โ•โ•โ•โ•โ•โ•โ•ฃ
                                         โ•‘       โ•‘       โ•‘       โ•‘
                                         โ•‘   O   โ•‘   X   โ•‘   O   โ•‘
                                         โ•‘       โ•‘       โ•‘       โ•‘
                                         โ• โ•โ•โ•โ•โ•โ•โ•โ•ฌโ•โ•โ•โ•โ•โ•โ•โ•ฌโ•โ•โ•โ•โ•โ•โ•โ•ฃ
                                         โ•‘       โ•‘       โ•‘       โ•‘
                                         โ•‘   X   โ•‘   O   โ•‘   X   โ•‘
                                         โ•‘       โ•‘       โ•‘       โ•‘
                                         โ•šโ•โ•โ•โ•โ•โ•โ•โ•ฉโ•โ•โ•โ•โ•โ•โ•โ•ฉโ•โ•โ•โ•โ•โ•โ•โ•

๐ŸŽฏ No draws. Only winners. ๐Ÿ†

About

A trustless peer-to-peer betting DApp on the Polygon blockchain where players wager MATIC on real-time Tic-Tac-Toe games using cryptographic settlement proofs.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published