Skip to content

rbuddy101/jack-ai

Repository files navigation

Made for an overnight hackathon for ETH Argentina 2025

๐Ÿƒ Autonomous AI Blackjack Agent

An autonomous AI agent that plays blackjack on the Base blockchain, powered by Coinbase AgentKit and OpenAI. The agent makes strategic decisions, executes on-chain transactions, and automatically claims winningsโ€”all while streaming real-time gameplay updates to a Next.js dashboard.

โœจ Features

  • ๐Ÿค– Autonomous Gameplay: AI agent makes strategic hit/stand decisions using GPT-4
  • ๐Ÿ“Š Real-time Dashboard: Live card display and game state visualization via Server-Sent Events (SSE)
  • ๐ŸŽฒ Provably Fair: Uses Chainlink VRF (Verifiable Random Function) for cryptographically secure card dealing
  • ๐Ÿ’ฐ Automatic Claiming: Detects completed games and claims winnings automatically
  • ๐Ÿ”„ Smart State Management: 11-state game loop handles VRF callbacks and trading periods
  • โ›“๏ธ Base Network: Deployed on Base blockchain for low-cost transactions
  • ๐Ÿ“ˆ Statistics Tracking: Monitors wins, losses, pushes, busts, and win streaks

๐ŸŽฎ How It Works

Hybrid Architecture

The application uses a dual-path architecture for optimal performance:

  1. Direct RPC Path - Efficient contract reads and claiming

    • Game status checks (getGameStatus(), getPlayerStats())
    • Direct claiming of winnings bypasses AI for speed
    • VRF state polling and trading period detection
  2. AI Agent Path - Strategic gameplay decisions

    • AI analyzes game state and chooses hit/stand
    • Executes actions through custom blackjack action provider
    • Natural language conversation interface with blockchain execution

Game Flow

1. Check for existing game โ†’ Resume if active
2. Start new game with ETH bet โ†’ Wait for VRF initial deal
3. AI evaluates hand โ†’ Make hit/stand decision
4. Execute action โ†’ Wait for VRF card dealing
5. Repeat until game complete
6. Automatically claim winnings
7. Stop (single-game mode)

VRF Integration

The blackjack contract uses Chainlink VRF for provably fair randomness:

  • All card dealing is cryptographically secure and verifiable on-chain
  • VRF callbacks typically complete within 2-30 seconds
  • Application polls contract every 2 seconds for state changes

๐Ÿš€ Quick Start

Prerequisites

Installation

  1. Clone the repository

    git clone <your-repo-url>
    cd onchain-agent
  2. Install dependencies

    npm install
  3. Configure environment variables

    cp .env.example .env

    Edit .env and add your API keys:

    # Required: OpenAI API Key
    OPENAI_API_KEY=sk-...
    
    # Required: Coinbase Developer Platform credentials
    CDP_API_KEY_ID=...
    CDP_API_KEY_SECRET=...
    
    # Optional: Will be auto-generated if not provided
    CDP_WALLET_SECRET=...
    AI_WALLET=0x...
    
    # Contract configuration
    BLACKJACK_CONTRACT_ADDRESS=0x1234... # Provided in .env.example
    NETWORK_ID=base-sepolia
    
    # Betting configuration
    BET_AMOUNT=700000000000000 # 0.0007 ETH in wei
    NEXT_PUBLIC_BET_AMOUNT=0.0007
  4. Fund your wallet

    The application will generate a wallet on first run and save it to wallet_data.txt. Fund this address with Sepolia ETH using a Base Sepolia faucet.

  5. Start the development server

    npm run dev
  6. Open the dashboard

    Navigate to http://localhost:3000 to see the autonomous player dashboard.

๐ŸŽฏ Usage

Autonomous Mode

The primary interface is the autonomous player dashboard:

  1. Start Autonomous Play - Click to begin a single game
  2. Watch Live - Real-time card display shows dealer and player hands
  3. View Statistics - Track wins, losses, and performance metrics
  4. Action Log - See detailed event history

The agent will:

  • Start a new game (or resume/claim existing)
  • Make strategic decisions based on card values
  • Execute transactions on-chain
  • Wait for VRF callbacks
  • Claim winnings automatically
  • Stop after one game (click Start again for next game)

Chat Interface (Optional)

You can also interact with the agent via natural language:

You: "Start a blackjack game with 0.001 ETH"
Agent: [Executes startGame transaction and waits for VRF]

You: "What's the game status?"
Agent: "Your hand: 15 (7โ™  8โ™ฅ), Dealer: 10โ™ฆ. You can hit or stand."

You: "Hit me"
Agent: [Executes hit transaction, waits for VRF card]

๐Ÿ“ Project Structure

onchain-agent/
โ”œโ”€โ”€ app/
โ”‚   โ”œโ”€โ”€ api/
โ”‚   โ”‚   โ”œโ”€โ”€ agent/              # AI agent endpoints
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ action-providers/
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ blackjack/  # Custom blackjack actions
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ prepare-agentkit.ts  # AgentKit initialization
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ create-agent.ts      # OpenAI agent setup
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ route.ts             # Chat API endpoint
โ”‚   โ”‚   โ””โ”€โ”€ autonomous/
โ”‚   โ”‚       โ”œโ”€โ”€ route.ts         # Control autonomous player
โ”‚   โ”‚       โ””โ”€โ”€ stream/
โ”‚   โ”‚           โ””โ”€โ”€ route.ts     # SSE stream endpoint
โ”‚   โ”œโ”€โ”€ hooks/
โ”‚   โ”‚   โ””โ”€โ”€ useAutonomousPlayer.ts  # SSE client hook
โ”‚   โ””โ”€โ”€ page.tsx                 # Main dashboard page
โ”œโ”€โ”€ components/
โ”‚   โ”œโ”€โ”€ GameDashboard.tsx        # Main dashboard component
โ”‚   โ”œโ”€โ”€ CardDisplay.tsx          # Real-time card visualization
โ”‚   โ”œโ”€โ”€ StateMachine.tsx         # State machine diagram
โ”‚   โ”œโ”€โ”€ GameStats.tsx            # Statistics display
โ”‚   โ””โ”€โ”€ ActionLog.tsx            # Event history
โ”œโ”€โ”€ lib/
โ”‚   โ”œโ”€โ”€ game-loop.ts             # 11-state game loop
โ”‚   โ”œโ”€โ”€ autonomous-player.ts     # Player manager singleton
โ”‚   โ””โ”€โ”€ rpc-client.ts            # Direct RPC contract interface
โ”œโ”€โ”€ Blackjack.sol                # Smart contract source (reference)
โ”œโ”€โ”€ Blackjackabi.json            # Contract ABI
โ””โ”€โ”€ CLAUDE.md                    # Detailed technical documentation

๐Ÿ› ๏ธ Development

Available Scripts

npm run dev      # Start development server
npm run build    # Production build
npm start        # Run production server
npm run lint     # Run ESLint

Key Configuration Files

  • app/api/agent/prepare-agentkit.ts - Configure AgentKit, wallet provider, and action providers
  • app/api/agent/create-agent.ts - Configure OpenAI model and system prompts
  • lib/game-loop.ts - Adjust game loop behavior and state transitions
  • .env - Environment variables for API keys and contract addresses

Custom Action Provider

The blackjack action provider (app/api/agent/action-providers/blackjack/) implements:

  • blackjack_start_game - Start game with ETH bet
  • blackjack_hit - Draw another card
  • blackjack_stand - End player's turn
  • blackjack_claim_winnings - Claim winnings from completed game
  • blackjack_get_game_status - Get full game state
  • blackjack_get_player_stats - Get lifetime statistics

All actions automatically handle VRF polling and wait for callbacks to complete.

๐Ÿ”— Smart Contract

The blackjack game is powered by a smart contract deployed on Base:

  • Network: Base Sepolia (testnet) / Base Mainnet (production)
  • Contract: See BLACKJACK_CONTRACT_ADDRESS in .env.example
  • Source: Blackjack.sol (included for reference)
  • Features:
    • Chainlink VRF for provably fair randomness
    • Prediction market trading period (60 seconds)
    • Single game per address constraint
    • Automatic payout calculation

๐Ÿ“– Documentation

  • CLAUDE.md - Comprehensive technical documentation
    • Architecture deep-dive
    • State machine details
    • API reference
    • Development workflows

๐Ÿค Contributing

Contributions are welcome! This project demonstrates:

  • Custom AgentKit action providers
  • On-chain gaming with VRF
  • Real-time web3 dashboards
  • Autonomous agent architectures

Feel free to:

  • Add new game strategies
  • Improve the AI decision-making
  • Enhance the dashboard UI
  • Add analytics and insights

๐Ÿ“„ License

MIT License - see LICENSE file for details

๐Ÿ”— Resources


Built with โค๏ธ using Coinbase AgentKit

About

Built overnight at ETH Argentina 2025, this project is a fully autonomous AI blackjack agent that strategizes with GPT-4, executes and settles games on the Base blockchain, and streams real-time gameplay through a live Next.js dashboard.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors