Skip to content

arif-aygun/base-genius

ย 
ย 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

92 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿง  Base Genius

Weekly Quiz Mini-App for Base & Farcaster

Test your knowledge. Mint your achievement. Engage with the community.

Live Demo Next.js TypeScript Base


๐Ÿ“– Introduction

Base Genius is a lightweight, mobile-first mini-app designed for the Base blockchain and Farcaster ecosystem.

๐ŸŽฏ What it does:

  • Runs a weekly 5-question quiz testing users on recent Base/Farcaster news and community events
  • Optimized for mini-app frames and social wallet flows
  • Perfect scorers can mint a collectible on-chain badge NFT as proof of knowledge and engagement

๐Ÿ’ก Perfect for:

  • Community growth and engagement
  • Low-friction educational experiences
  • Weekly content updates with minimal overhead

โœจ Features

Feature Description
๐ŸŽฒ Randomized Questions 5 questions randomly selected from a pool of 50+
๐Ÿ”’ Anti-Cheat Server-side validation, answers never sent to client
๐Ÿ† NFT Rewards Mint on-chain badges for perfect scores
๐Ÿ“ฑ Mobile-First Optimized for Farcaster frames and social wallets
๐Ÿค– AI-Assisted Weekly question generation using Gemini AI
โšก Fast & Light Next.js 16 with TypeScript and Tailwind CSS

๐Ÿ—๏ธ Tech Stack

Framework    โ†’ Next.js 16 (App Router)
Language     โ†’ TypeScript
Styling      โ†’ Tailwind CSS
Blockchain   โ†’ Base (Ethereum L2)
Web3 Tools   โ†’ OnchainKit (wallet & minting)
Data Layer   โ†’ JSON-based API routes

Note

All question validation happens server-side to prevent cheating. Correct answers are never sent to the client.


๐Ÿ“ Repository Structure

๐Ÿ“‚ app/ โ€” Next.js App Router
app/
โ”œโ”€โ”€ ๐Ÿ“„ page.tsx                      # Main game UI & state machine
โ”œโ”€โ”€ ๐Ÿ“„ layout.tsx                    # Root layout
โ”œโ”€โ”€ ๐Ÿ“„ globals.css                   # Global styles
โ”‚
โ”œโ”€โ”€ ๐Ÿ“‚ api/                          # Server-side API routes
โ”‚   โ”œโ”€โ”€ questions/route.ts           # GET: 5 randomized questions
โ”‚   โ”œโ”€โ”€ submit-answers/route.ts      # POST: Validate answers
โ”‚   โ””โ”€โ”€ auth/route.ts                # Auth helper
โ”‚
โ”œโ”€โ”€ ๐Ÿ“‚ components/                   # React components
โ”‚   โ”œโ”€โ”€ QuizCard.tsx
โ”‚   โ”œโ”€โ”€ ResultsCard.tsx
โ”‚   โ”œโ”€โ”€ MintBadgeButton.tsx
โ”‚   โ””โ”€โ”€ ConnectWallet.tsx
โ”‚
โ”œโ”€โ”€ ๐Ÿ“‚ data/
โ”‚   โ””โ”€โ”€ quiz-questions.json          # 50+ questions with metadata
โ”‚
โ””โ”€โ”€ ๐Ÿ“‚ types/
    โ””โ”€โ”€ quiz.ts                      # TypeScript type definitions
โš™๏ธ contracts/ โ€” Smart Contracts
contracts/
โ”œโ”€โ”€ BaseGeniusBadge.sol              # Solidity NFT contract
โ””โ”€โ”€ BaseGeniusBadgeABI.ts            # ABI for app integration
๐Ÿ”ง scripts/ โ€” Helper Scripts
scripts/
โ”œโ”€โ”€ generate_weekly_questions.py     # ๐Ÿค– AI question generator
โ”œโ”€โ”€ generate-signer-wallet.ts
โ””โ”€โ”€ check-contract-signer.ts
๐Ÿ–ผ๏ธ public/metadata/ โ€” NFT Metadata
public/metadata/
โ”œโ”€โ”€ week-50.json
โ”œโ”€โ”€ week-51.json
โ””โ”€โ”€ ...

๐Ÿ”„ Data Flow & API Contract

GET /api/questions

Purpose: Fetch 5 randomized quiz questions

// Response
{
  questions: Array<{
    id: string;
    question: string;
    options: string[];
    // โš ๏ธ correctAnswer NOT included (anti-cheat)
  }>
}

Important

Query params support excluding recently served question IDs to prevent repetition.

POST /api/submit-answers

Purpose: Validate user answers and return detailed results

// Request Body
{
  answers: Array<{
    questionId: string;
    selectedIndex: number;
  }>
}

// Response
{
  score: number;
  results: Array<{
    questionId: string;
    correct: boolean;
    explanation: string;
  }>
}

๐Ÿ† Minting Flow

graph LR
    A[Submit Answers] --> B{Score = 5/5?}
    B -->|Yes| C[Show Mint Button]
    B -->|No| D[Show Results]
    C --> E[Connect Wallet]
    E --> F[Mint NFT Badge]
    F --> G[Display Success]
Loading

๐Ÿš€ Getting Started

Prerequisites

Tool Version Required
Node.js v18+ โœ… Yes
npm/pnpm Latest โœ… Yes
Ethereum Wallet - ๐Ÿ”ง Optional (for testing mints)

Installation

# 1๏ธโƒฃ Clone the repository
git clone https://github.com/yourusername/base-genius.git
cd base-genius

# 2๏ธโƒฃ Install dependencies
npm install

# 3๏ธโƒฃ Set up environment variables
cp .env.example .env
# Edit .env with your API keys

# 4๏ธโƒฃ Run development server
npm run dev

๐ŸŒ Open http://localhost:3000 in your browser

Production Build

npm run build
npm start

Tip

For Farcaster mini-app frame compatibility, the app uses useMiniKit() and setFrameReady() hooks.


๐Ÿ” Environment & Secrets

Create a .env file from the example template:

cp .env.example .env

Required API Keys:

Variable Purpose Where to Get
GEMINI_API_KEY AI question generation Google AI Studio
NEYNAR_API_KEY Farcaster data fetching Neynar Dashboard

Warning

Never commit .env files to version control. These keys should remain secret.

๐Ÿ“š More details: See docs/ai-setup-guide.md for complete setup instructions.


๐Ÿค– Weekly Question Generation

The app includes an AI-powered script to generate fresh quiz questions weekly.

Setup Python Environment

# Create virtual environment
python3 -m venv .venv

# Activate (Linux/Mac)
source .venv/bin/activate

# Activate (Windows)
.venv\Scripts\activate

# Install dependencies
pip install -r scripts/requirements.txt

Generate Questions

python3 scripts/generate_weekly_questions.py

What it does:

  • โœ๏ธ Generates new questions using Gemini AI
  • ๐Ÿ“ Updates data/quiz-questions.json
  • ๐Ÿ–ผ๏ธ Creates metadata files in public/metadata/
  • ๐Ÿ” Validates question format and structure

Tip

Set up a GitHub Action to run this weekly for automated content updates!


๐ŸŽจ NFT Minting System

Requirements for Minting

Requirement Description
๐ŸŽฏ Perfect Score User must answer all 5 questions correctly
๐Ÿ‘› Connected Wallet Must connect wallet via OnchainKit
โ›“๏ธ Base Network NFT mints on Base blockchain

Minting Components

lib/nftService.ts          โ†’ Core minting logic
components/MintBadgeButton.tsx  โ†’ UI component
contracts/BaseGeniusBadge.sol   โ†’ Smart contract
public/metadata/week-*.json     โ†’ NFT metadata

Setup Instructions

๐Ÿ“– Complete guide: NFT_SETUP_INSTRUCTIONS.md

Covers:

  • Contract deployment
  • Configuration
  • Metadata setup
  • Testing

โœ… Testing & Quality

Type Checking

npm run build  # Catches TypeScript errors

Recommended Tests

Area Test Type Priority
๐Ÿ“ Question validation Unit tests High
๐Ÿ”€ Shuffling logic Unit tests High
๐Ÿ”Œ API routes Integration Medium
๐ŸŽจ UI components Component tests Low

Note

Test suites not included by default. Add your preferred testing framework (Jest, Vitest, etc.).


๐Ÿ’ก Developer Tips & Conventions

State Management

  • ๐ŸŽฎ Game State: Uses useState for simple state machine (welcome โ†’ loading โ†’ quiz โ†’ results)
  • ๐Ÿšซ No Redux/Zustand: Keep it lightweight for mini-app performance

Styling Guidelines

  • ๐ŸŽจ Tailwind CSS: Utility-first approach
  • ๐Ÿ“ฑ Mobile-First: Designed for Farcaster frames
  • โœจ Consistency: Use design tokens for colors and spacing

Security Best Practices

  • ๐Ÿ”’ Server-Side Validation: All answer checking happens on the server
  • ๐ŸŽฒ Fisher-Yates Shuffle: Random question selection
  • ๐Ÿšซ No Client-Side Answers: Correct answers never sent to client

API Conventions

Important

When changing API routes, always update types/quiz.ts to maintain type safety.


๐Ÿ“š Key Files Reference

Quick reference to the most important files in the codebase:

File Purpose Priority
app/page.tsx Main game UI & state transitions ๐Ÿ”ด Critical
app/api/questions/route.ts Question randomization & serving ๐Ÿ”ด Critical
app/api/submit-answers/route.ts Server-side answer validation ๐Ÿ”ด Critical
data/quiz-questions.json Question pool (50+ questions) ๐Ÿ”ด Critical
lib/nftService.ts Minting & Web3 integration ๐ŸŸก Important
contracts/BaseGeniusBadge.sol NFT smart contract ๐ŸŸก Important
components/QuizCard.tsx Quiz UI component ๐ŸŸข Reference
components/MintBadgeButton.tsx Minting UI component ๐ŸŸข Reference

๐Ÿ”ง Troubleshooting

โŒ Questions not showing

Check:

  1. โœ… Verify data/quiz-questions.json is valid JSON
  2. โœ… Confirm API route is accessible at /api/questions
  3. โœ… Check browser console for network errors
  4. โœ… Ensure at least 5 questions exist in the pool

Quick test:

curl http://localhost:3000/api/questions
โŒ Minting fails

Check:

  1. โœ… Contract address configured in minikit.config.ts
  2. โœ… Signer wallet properly configured
  3. โœ… User connected to Base network
  4. โœ… User has sufficient ETH for gas fees

Debug command:

npx ts-node scripts/check-contract-signer.ts
โŒ TypeScript errors

Solutions:

# Clear cache and reinstall
rm -rf .next node_modules
npm install

# Run type check
npm run build

๐Ÿš€ Next Steps & Roadmap

Recommended Improvements

  • Testing Suite

    • Add Jest/Vitest for unit tests
    • Test API validation flow
    • Test question shuffling logic
  • CI/CD Pipeline

    • GitHub Actions for npm run build
    • ESLint and TypeScript checks on PRs
    • Automated deployment previews
  • Automated Content

    • Weekly cron job for question generation
    • Auto-commit generated questions
    • Scheduled metadata updates
  • Analytics

    • Track quiz completion rates
    • Monitor minting success rates
    • User engagement metrics

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is open source and available under the MIT License.


Built with โค๏ธ for the Base & Farcaster community

๐ŸŒ Live Demo โ€ข ๐Ÿ“– Docs

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 76.8%
  • Python 9.4%
  • Solidity 7.4%
  • CSS 5.8%
  • JavaScript 0.6%