Backend: - Add configurable draw cycle settings (minutes/hourly/daily/weekly/custom) - Add CYCLE_TYPE, CYCLE_INTERVAL_*, CYCLE_DAILY_TIME, CYCLE_WEEKLY_* env vars - Add SALES_CLOSE_BEFORE_DRAW_MINUTES and CYCLES_TO_GENERATE_AHEAD - Fix SQLite parameter issue in scheduler - Fix TypeScript error in webhooks controller Frontend: - Add 'Save This Link' section with copy button on ticket status page - Improve draw animation to show immediately when draw starts - Show 'Waiting for next round...' instead of 'Drawing Now!' after draw - Hide Buy Tickets button when waiting for next round - Skip draw animation if no tickets were sold - Keep winner screen open longer (15s) for next cycle to load - Auto-refresh to next lottery cycle after draw Telegram Bot: - Various improvements and fixes
Lightning Lottery Backend API
Bitcoin Lightning Network powered lottery system with automatic payouts to Lightning Addresses.
Features
- Public Ticket Purchasing: Anonymous ticket purchases via Lightning invoices
- Automated Draws: Scheduled draws using cryptographically secure RNG
- Instant Payouts: Automatic payouts to Lightning Addresses
- Nostr Authentication: Optional user accounts via Nostr signatures
- Multiple Cycle Types: Hourly, daily, weekly, and monthly jackpots
- Admin Interface: Manual draw triggers and payout management
Tech Stack
- Runtime: Node.js with TypeScript
- Framework: Express.js
- Database: PostgreSQL
- Lightning: LNbits integration
- Scheduler: node-cron
Setup
Prerequisites
- Node.js 18+
- PostgreSQL 14+
- LNbits instance
Installation
- Install dependencies:
npm install
- Configure environment variables:
cp .env.example .env
# Edit .env with your configuration
- Setup database:
# Create database
createdb lightning_lotto
# Run schema
psql lightning_lotto < src/database/schema.sql
Development
npm run dev
Production
npm run build
npm start
Scheduler (Optional Separate Process)
npm run scheduler
API Endpoints
Public Endpoints
GET /jackpot/next- Get next upcoming cyclePOST /jackpot/buy- Create ticket purchaseGET /tickets/:id- Get ticket status
User Endpoints (Nostr Auth)
POST /auth/nostr- Authenticate with NostrGET /me- Get user profilePATCH /me/lightning-address- Update Lightning AddressGET /me/tickets- Get user's ticketsGET /me/wins- Get user's wins
Admin Endpoints
GET /admin/cycles- List all cyclesPOST /admin/cycles/:id/run-draw- Manually run drawGET /admin/payouts- List payoutsPOST /admin/payouts/:id/retry- Retry failed payout
Webhook Endpoints
POST /webhooks/lnbits/payment- LNbits payment notification
Configuration
See .env.example for all configuration options.
Required Environment Variables
DATABASE_URL- PostgreSQL connection stringLNBITS_API_BASE_URL- LNbits API URLLNBITS_ADMIN_KEY- LNbits admin/invoice keyJWT_SECRET- Secret for JWT signingADMIN_API_KEY- Admin endpoint authentication
Database Schema
The system uses 7 main tables:
lotteries- Lottery configurationjackpot_cycles- Draw cyclesticket_purchases- Purchase recordstickets- Individual ticketspayouts- Winner payoutsusers- Nostr user accountsdraw_logs- Audit trail
Security
- JWT tokens for user authentication
- Admin API key for admin endpoints
- Webhook signature verification
- Rate limiting on all endpoints
- Idempotent payment processing
- Transaction-based ticket issuance
Scheduler Tasks
The scheduler runs three main tasks:
- Cycle Generator: Creates future cycles (every 5 minutes)
- Draw Executor: Triggers draws at scheduled times (every minute)
- Payout Retry: Retries failed payouts with exponential backoff (every 10 minutes)
License
MIT