This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
LNp2pBot is a Telegram bot that facilitates peer-to-peer Lightning Network trading. Users can create buy/sell orders for Bitcoin using Lightning Network hold invoices to ensure trustless transactions.
npm start # Compile TypeScript and start bot
npm run dev # Compile and start with nodemon for development
npm run prestart # Compile TypeScript only
npm run predev # Compile TypeScript onlynpm run lint # ESLint code validation
npm run format # Format code with Prettiernpm test # Run all tests with Mocha
npm run pretest # Compile tests only- app.ts: Main entry point, handles MongoDB connection and bot initialization
- bot/: Bot logic and command handlers
- start.ts: Bot initialization, command registration, and scheduled jobs
- modules/: Feature-specific modules (community, orders, dispute, etc.)
- middleware/: Authentication, validation, and context enhancement
- models/: MongoDB schemas (User, Order, Community, Dispute, etc.)
- ln/: Lightning Network integration (hold invoices, payments, subscriptions)
- jobs/: Scheduled background tasks for order management and payments
- util/: Shared utilities and helpers
The bot uses custom context types that extend Telegraf's base context:
MainContext: Adds i18n, user, and admin propertiesCommunityContext: Extends MainContext for community-specific features
Each feature module follows this pattern:
commands.ts: Command handlersactions.ts: Action button handlersmessages.ts: Message templatesscenes.ts: Multi-step conversation flowsindex.ts: Module configuration and exports
The bot uses Lightning Network hold invoices for trustless escrow:
- Seller creates order
- Buyer takes order, seller pays hold invoice
- Funds are held until both parties confirm fiat exchange
- Hold invoice is settled or canceled based on dispute resolution
Background jobs handle critical functions:
- Pending payment retries
- Order expiration and cleanup
- Community earnings calculation
- Node health monitoring
- User: Telegram users with trading history and preferences
- Order: Trading orders with status tracking and dispute handling
- Community: Telegram groups with custom fee structures
- Dispute: Conflict resolution with solver assignment
- PendingPayment: Failed payment retry queue
Copy .env-sample to .env and configure:
BOT_TOKEN: Telegram bot API tokenLND_*: Lightning Network node connection detailsDB_*orMONGO_URI: MongoDB connectionCHANNEL: Public order announcement channelADMIN_CHANNEL: Admin notifications
Tests are in TypeScript and use Mocha with Chai assertions. Test compilation uses a separate tsconfig.test.json that includes the tests directory.
npm test # Run all tests
npm run pretest # Compile tests only
export NODE_ENV=test && mocha --exit 'dist/tests/**/*.spec.js' # Run specific test patternTwo distinct trading patterns:
Sell Orders (Seller has Bitcoin):
- Seller creates order → Published to channel
- Buyer takes order → Seller pays hold invoice (funds locked)
- Buyer sends fiat → Seller confirms receipt
- Hold invoice settled → Buyer receives Bitcoin
Buy Orders (Buyer wants Bitcoin):
- Buyer creates order → Published to channel
- Seller takes order → Seller pays hold invoice (funds locked)
- Buyer provides invoice → Seller sends fiat
- Buyer confirms fiat receipt → Hold invoice settled → Buyer receives Bitcoin
- MainContext: Base context with i18n, user, admin properties
- CommunityContext: Extends MainContext with wizard state for multi-step flows
- Middleware chain: User validation → Admin checking → Context enhancement → Command routing
Critical background processes with specific timing:
- Pending payments: Every 5 minutes (configurable via
PENDING_PAYMENT_WINDOW) - Order cancellation: Every 20 seconds
- Order deletion: Every hour at 25 minutes past
- Community earnings: Every 10 minutes
- Node health: Every minute
- Solver availability: Daily at midnight
- 10 supported languages via YAML files in
locales/ - User-specific language preferences stored in database
- Dynamic language switching with
@grammyjs/i18n - Message templates with interpolation support
Orders follow specific state transitions:
- PENDING → ACTIVE → FIAT_SENT → COMPLETED
- Dispute states: DISPUTE, CANCELED_BY_ADMIN
- Failed states: EXPIRED, CANCELED
- Custom fee structures per Telegram group
- Solver assignment and dispute resolution
- Automated earnings calculation and distribution
- Ban management (global and community-level)
- Winston logger with configurable levels and timeout monitoring
- Global unhandled rejection handlers in app.ts
- Try-catch blocks throughout with proper error context
- Graceful shutdown handling (SIGINT/SIGTERM)
- Strict mode enabled for better type safety
- Separate test configuration (tsconfig.test.json)
- Comprehensive interface definitions for all models
- Custom type extensions for Telegraf contexts
- telegraf: Telegram bot framework (4.8.0)
- mongoose: MongoDB ODM (6.13.6)
- lightning: LND node integration (10.25.0)
- node-schedule: Cron job scheduling
- @grammyjs/i18n: Internationalization
- winston: Logging with timeout monitoring
- canvas: QR code generation with random backgrounds