Architecture

Technical overview of the Mystery Gift platform.

High-Level Components

┌─────────────────────────────────────────────────────────────┐
│                      Mystery Gift Platform                   │
├─────────────────────────────────────────────────────────────┤
│                                                               │
│  ┌──────────────┐    ┌──────────────┐    ┌──────────────┐  │
│  │ Livestream   │    │  Marketplace  │    │  TEE Services │  │
│  │   Server     │────│     API       │────│              │  │
│  │              │    │              │    │  • Random    │  │
│  │ • Raffles    │    │ • Packs      │    │  • Vault     │  │
│  │ • Twitch Bot  │    │ • Purchases  │    │  • Payments  │  │
│  └──────────────┘    └──────────────┘    └──────────────┘  │
│         │                    │                   │           │
│         └────────────────────┴───────────────────┘         │
│                            │                                 │
│                    ┌───────┴───────┐                          │
│                    │  D1 Database  │                          │
│                    │  + Cloudflare │                         │
│                    │    R2 Storage │                         │
│                    └───────────────┘                          │
│                           │                                   │
│                    ┌──────▼───────┐                          │
│                    │   Solana     │                          │
│                    │   + Base     │                          │
│                    │   RPC APIs   │                          │
│                    └──────────────┘                          │
└─────────────────────────────────────────────────────────────┘

Components

Livestream Server (apps/livestream/server)

24/7 daemon that orchestrates raffles.

Key Files:

  • core/agent.ts - Main initialization
  • core/scheduler.ts - Hourly raffle scheduling
  • raffle/manager.ts - Complete raffle cycle
  • twitch/chat-monitor.ts - Free entry system
  • social/twitter.ts - Twitter announcements

TEE Services

Secure enclave services running on Phala Cloud.

Randomness (services/verifiable-randomness-service)

  • Hardware-backed random generation
  • Remote attestation proofs
  • x402 payment support

Vault (services/verifiable-wallet-service)

  • Secure NFT custody
  • On-chain transfers
  • Solana + Base support

Marketplace API (apps/marketplace/api)

Cloudflare Worker backend.

Key Endpoints:

  • POST /api/v1/pack/:id/purchase - Buy packs
  • POST /api/v1/open/:purchaseId - Reveal cards
  • GET /api/v1/packs - Browse available packs

Miss AI (services/miss)

Conversational AI agent for platform assistance.

Tools:

  • Card lookups
  • Vault queries
  • Pack purchases
  • Raffle information

Data Flow

graph TD
    A[Token Holders] -->|Helius RPC| B[Livestream Server]
    C[Twitch Chat] -->|5-min window| B
    B -->|Random seed| D[TEE Randomness]
    D -->|Winner| B
    B -->|NFT transfer| E[TEE Vault]
    E -->|On-chain| F[Solana/Base]
    B -->|Announcement| G[Twitter/X]

Technology Stack

Component Technology
Backend Node.js 24+
Serverless Cloudflare Workers, Cloudflare D1
Database SQLite with Prisma ORM
Blockchain Solana, Base RPCs
Randomness Phala TEE (Intel TDX)
Streaming Twitch, WebSocket (Socket.io)
Frontend Next.js, React, Vite
CI/CD Turborepo

Next Steps