A referral tracking system for Loot Survivor on Starknet. Track which players brought in new users who actually played the game.
- 🔗 Referral Link Capture: Automatically captures referral addresses from URL parameters
- 💾 LocalStorage Persistence: Saves referral data until wallet connection
- 🔐 Wallet Integration: Cartridge Controller (Starknet) wallet connection
- ✅ On-Chain Verification: Verifies actual gameplay via Starknet RPC
- 📊 Leaderboard: Real-time leaderboard showing top referrers
- 🎨 Dark Theme UI: Modern, clean interface with Tailwind CSS
- Frontend: Next.js 14 (App Router), Tailwind CSS, Lucide-react icons
- Backend/Database: Supabase (PostgreSQL)
- Web3: Cartridge Controller (Starknet)
- Verification: Starkscan API
npm installCreate a .env.local file in the root directory:
NEXT_PUBLIC_SUPABASE_URL=your_supabase_url
NEXT_PUBLIC_SUPABASE_ANON_KEY=your_supabase_anon_key
SUPABASE_SERVICE_ROLE_KEY=your_service_role_key
VERIFY_API_KEY=your_verify_endpoint_secret
NEXT_PUBLIC_LOOT_SURVIVOR_CONTRACT=0x0100000000000000000000000000000000000000000000000000000000000000
NEXT_PUBLIC_STARKNET_CHAIN=mainnetnpm run devOpen http://localhost:3000 in your browser.
- User visits with referral link:
https://yoursite.com?ref=0x... - Referral captured: The
refparameter is saved to localStorage - Wallet connection: When user connects their Cartridge Controller wallet
- Referral mapping created: POST request to
/api/referralscreates the mapping - On-chain verification: Server checks Starkscan API for actual gameplay
- Leaderboard update: Verified referrals appear on the leaderboard
The verification system checks if a referee address has interacted with the Loot Survivor contract:
- Fetches all referrals where
has_played = false - For each address, queries Starkscan API for transactions
- Updates
has_played = trueif transactions are found
You can trigger verification manually:
curl -X POST http://localhost:3000/api/verify -H "x-verify-key: $VERIFY_API_KEY"Or set up a cron job to run periodically:
# Example cron job (runs every hour)
0 * * * * curl -X POST https://your-domain.com/api/verify -H "x-verify-key: $VERIFY_API_KEY"For production, consider using:
- Vercel Cron Jobs
- GitHub Actions
- A dedicated cron service
Create a new referral mapping.
Request:
{
"referee_address": "0x...",
"referrer_address": "0x..."
}Response:
{
"success": true,
"data": { ... }
}Get leaderboard data.
Response:
{
"data": [
{
"rank": 1,
"referrer_address": "0x...",
"total_points": 10
}
]
}Trigger on-chain verification for all unverified referrals.
Response:
{
"message": "Verified 5 out of 10 referrals",
"verified": 5,
"total": 10
}Get verification statistics.
Response:
{
"total": 100,
"verified": 75,
"unverified": 25
}├── app/
│ ├── api/
│ │ ├── referrals/
│ │ │ └── route.ts # Referral CRUD endpoints
│ │ └── verify/
│ │ └── route.ts # On-chain verification
│ ├── globals.css # Global styles
│ ├── layout.tsx # Root layout
│ └── page.tsx # Main page
├── components/
│ ├── ReferralLeaderboard.tsx # Leaderboard component
│ ├── ReferralShare.tsx # Share referral link component
│ └── WalletConnection.tsx # Wallet connection component
├── hooks/
│ └── useReferral.ts # Referral capture hook
├── lib/
│ ├── referral.ts # Referral utilities
│ └── supabase.ts # Supabase client
├── supabase/
│ └── migrations/
│ └── 001_create_referrals_table.sql
└── package.json
The app expects Cartridge Controller to be available at window.cartridge.controller. Make sure users have the Cartridge extension installed.
The wallet connection component:
- Checks for Cartridge Controller on mount
- Polls connection status
- Automatically submits referral when wallet connects
- Handles connect/disconnect events
- API Protection: Consider adding authentication to
/api/verifyendpoint - Rate Limiting: Implement rate limiting on referral creation
- Input Validation: Address format validation is already implemented
- Self-Referral Prevention: The system prevents users from referring themselves
MIT