Skip to content

mevyou/oracle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Oracle Betting Backend

A comprehensive backend system for a betting oracle platform with MongoDB user profiles and real-time football data integration.

🌟 Features

⚽ Football Data Integration

  • Real-time match data from API-Football
  • Team search and match finding
  • Head-to-head analysis
  • Live match updates
  • Match predictions and statistics
  • Intelligent caching system

👤 User Profile Management

  • MongoDB-powered user profiles
  • Wallet-based authentication
  • Social login support (Google, Facebook, Twitter, Discord)
  • Contract integration for blockchain users
  • Profile search and discovery

🎮 Game Results System

  • Three-party game resolution (2 players + 1 arbitrator)
  • Dynamic evidence system
  • UUID-based tracking
  • Mock data for development

🚀 Quick Start

Prerequisites

  • Node.js 16+
  • MongoDB (local or Atlas)
  • API-Football account (free tier available)

Installation

# 1. Clone and install
npm install

# 2. Configure environment
cp env.template .env

# 3. Edit .env with your credentials
# - MongoDB connection string
# - Football API key

# 4. Generate Prisma client
npm run prisma:generate

# 5. Push database schema
npm run prisma:push

# 6. Start server
npm run dev

Server will start on http://localhost:3000

📡 API Endpoints

Football Data (/api/football)

# Find match between teams
GET /api/football/matches/find?team1=Bayern Munich&team2=Liverpool

# Search team
GET /api/football/teams/search?name=Barcelona

# Head-to-head
GET /api/football/matches/h2h?team1=Real Madrid&team2=Barcelona

# Live matches
GET /api/football/fixtures/live

# Team fixtures
GET /api/football/fixtures/team?name=Liverpool&next=5

User Profiles (/api/user)

# Get profile
GET /api/user/profile?address=0x1234...

# Create/update profile
POST /api/user/profile

# Check exists
GET /api/user/exists?address=0x1234...

# Social login
POST /api/user/social-login

Game Results (/api/results)

# Get all results
GET /api/results

# Get specific result
GET /api/results/:id

# Create result
POST /api/results

System (/)

# API info and all endpoints
GET /

# Health check
GET /health

📚 Documentation

Football API

  • 📖 Integration Guide: FOOTBALL_API_INTEGRATION.md
  • ⚡ Quick Start: FOOTBALL_QUICK_START.md
  • 📋 API Reference: FOOTBALL_API_REFERENCE.md
  • 💻 Code Examples: examples/football-usage-examples.ts
  • 🧪 Test Script: test-football-api.js

User Profiles

  • 🗄️ MongoDB Setup: MONGODB_SETUP.md
  • 🔄 Migration Guide: MIGRATION_GUIDE.md
  • 📖 API Reference: USER_API_QUICK_REFERENCE.md

Implementation

  • ✅ Summary: IMPLEMENTATION_COMPLETE.md
  • 📝 Implementation Details: IMPLEMENTATION_SUMMARY.md

🧪 Testing

Test Football API

node test-football-api.js

Manual Testing

# Test team search
curl "http://localhost:3000/api/football/teams/search?name=Liverpool"

# Test match finding
curl "http://localhost:3000/api/football/matches/find?team1=Real Madrid&team2=Barcelona"

# Test user profile
curl "http://localhost:3000/api/user/exists?address=0x1234567890abcdef"

# Test health
curl "http://localhost:3000/health"

🏗️ Architecture

┌─────────────┐
│   Frontend  │
└──────┬──────┘
       │
       │ HTTP/REST
       │
┌──────▼──────────────────────────────┐
│         Express.js Server           │
│  ┌──────────────────────────────┐  │
│  │  Routes                       │  │
│  │  - /api/football              │  │
│  │  - /api/user                  │  │
│  │  - /api/results               │  │
│  └──────────┬───────────────────┘  │
│             │                       │
│  ┌──────────▼───────────────────┐  │
│  │  Controllers                  │  │
│  └──────────┬───────────────────┘  │
│             │                       │
│  ┌──────────▼───────────────────┐  │
│  │  Services                     │  │
│  │  - FootballService            │  │
│  │  - UserService                │  │
│  │  - ContractService            │  │
│  └──────────┬───────────────────┘  │
│             │                       │
└─────────────┼───────────────────────┘
              │
       ┌──────┴──────┐
       │             │
┌──────▼─────┐ ┌────▼────────┐
│  MongoDB   │ │ API-Football│
│ (Profiles) │ │  (Matches)  │
└────────────┘ └─────────────┘

🔧 Tech Stack

  • Runtime: Node.js + TypeScript
  • Framework: Express.js
  • Database: MongoDB (via Prisma ORM)
  • External API: API-Football
  • Caching: In-memory cache with TTL
  • Validation: Input validation
  • Error Handling: Centralized error middleware

🎯 Key Features for Betting Oracle

1. Dynamic Match Finding

// Works with just team names!
GET /api/football/matches/find?team1=Bayern Munich&team2=Liverpool

// Or with specific date
GET /api/football/matches/find?team1=Bayern Munich&team2=Liverpool&date=2024-03-15

2. Automatic Winner Detection

const match = await footballService.findMatch({
  team1: 'Real Madrid',
  team2: 'Barcelona'
});

const winner = match.teams.home.winner 
  ? match.teams.home.name 
  : match.teams.away.winner 
    ? match.teams.away.name 
    : 'Draw';

3. Smart Caching

  • Reduces API costs by 70-90%
  • Automatic cache invalidation
  • Optimized TTL for each data type

4. Error Resilience

  • Graceful error handling
  • Helpful error messages
  • Fallback mechanisms

📦 Environment Variables

# Server
PORT=3000
NODE_ENV=development

# MongoDB
DATABASE_URL=mongodb://localhost:27017/oracle_db

# Football API (get from dashboard.api-football.com)
FOOTBALL_API_KEY=your_api_key_here
FOOTBALL_API_URL=https://v3.football.api-sports.io
FOOTBALL_API_HOST=v3.football.api-sports.io

🚢 Deployment

Build for Production

npm run build
npm start

Docker (Optional)

docker build -t oracle-backend .
docker run -p 3000:3000 oracle-backend

Environment Setup

  1. Set production environment variables
  2. Use MongoDB Atlas for database
  3. Configure CORS for your frontend domain
  4. Enable HTTPS
  5. Set up monitoring

📈 API Usage

Rate Limits (Football API)

  • Free: 100 requests/day
  • Basic: 3,000 requests/day
  • Pro: 15,000 requests/day

Caching Reduces Usage

With caching enabled:

  • 1,000 team searches → ~50 API calls
  • 500 match finds → ~100 API calls
  • Saves 80-90% of API requests

🛠️ Development

Available Scripts

npm run dev          # Start development server
npm run build        # Build for production
npm start            # Start production server
npm run lint         # Run linter
npm run prisma:generate  # Generate Prisma client
npm run prisma:push      # Push schema to database
npm run prisma:studio    # Open Prisma Studio GUI

Project Structure

src/
├── app.ts                 # Express app setup
├── server.ts              # Server entry point
├── config/
│   ├── config.ts          # Configuration
│   └── db.ts              # Database connection
├── controllers/
│   ├── football.controllers.ts
│   ├── user.controllers.ts
│   └── gameResults.controllers.ts
├── services/
│   ├── footballService.ts
│   ├── userService.ts
│   └── contractService.ts
├── routes/
│   ├── football.routes.ts
│   ├── user.routes.ts
│   └── gameResults.routes.ts
├── utils/
│   ├── footballApiClient.ts
│   ├── cache.ts
│   └── routeRegistry.ts
└── middlewares/
    └── errorHandler.ts

🔐 Security

Current Implementation

  • ✅ Input validation
  • ✅ Error handling
  • ✅ CORS enabled
  • ✅ Environment variables

Recommended Additions

  • JWT authentication
  • Rate limiting middleware
  • Request sanitization
  • HTTPS in production
  • API key rotation

🌐 CORS Configuration

Update CORS for production in src/app.ts:

app.use(cors({
  origin: ['https://yourdomain.com'],
  credentials: true
}));

📞 Support

📝 License

ISC

🎉 What's Next?

  1. Test the APIs - Run node test-football-api.js
  2. Integrate Frontend - Connect your betting UI
  3. Add More Sports - Basketball, Hockey, etc.
  4. Implement Betting Logic - Odds calculation, bet settlement
  5. Add Analytics - Track popular matches and bets
  6. Deploy - Launch to production

Built for betting oracles with real-time sports data ⚽🎲

Version 1.0.0

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •