|
| 1 | +# Claude Code Project Guide |
| 2 | + |
| 3 | +## Project Summary |
| 4 | + |
| 5 | +Cube Worlds is a Telegram Mini App game built around NFTs on the TON blockchain. It consists of three main parts: a Telegram bot (Grammy), a backend API (Fastify), and a Vue 3 frontend (Vite). Data is stored in MongoDB via Typegoose. |
| 6 | + |
| 7 | +## Repository Layout |
| 8 | + |
| 9 | +``` |
| 10 | +src/ |
| 11 | + main.ts # Bot entrypoint: MongoDB connect, bot init, server start |
| 12 | + server.ts # Fastify HTTP server: webhooks, static files, API routes |
| 13 | + config.ts # Environment config via znv (lazy singleton proxy) |
| 14 | + logger.ts # Pino logger (silent in test mode) |
| 15 | + subscription.ts # TON transaction monitoring service |
| 16 | + bot/ |
| 17 | + index.ts # Bot creation, middleware chain, feature registration |
| 18 | + features/ # Bot command handlers (start, mint, dice, play, admin/*) |
| 19 | + helpers/ # Bot-specific utilities (keyboards, session, context) |
| 20 | + middlewares/ # Grammy middlewares (i18n, user attach, session) |
| 21 | + backend/ |
| 22 | + handlers/ # Fastify route handlers (auth, claim, nft, leaderboard, balances) |
| 23 | + *.test.ts # Backend tests (Node.js built-in test runner) |
| 24 | + common/ |
| 25 | + models/ # Typegoose models: User, Balance, Claim, CNFT, Transaction, Vote |
| 26 | + helpers/ # Shared utilities: ton.ts, ipfs.ts, generation.ts, telegram.ts |
| 27 | + frontend/ # Separate npm package (Vue 3 + Vite) |
| 28 | + src/ |
| 29 | + views/ # Vue components (Claim, Leaderboard, Exchange, Mining, Clicker, CNFT, FAQ) |
| 30 | + router/ # Vue Router config |
| 31 | + stores/ # Pinia stores (userStore) |
| 32 | + services/ # API service wrappers (auth, wallet) |
| 33 | +locales/ # i18n translation files (Fluent .ftl format) |
| 34 | +data/ # Static assets (NFT images, fonts) |
| 35 | +``` |
| 36 | + |
| 37 | +## Tech Stack |
| 38 | + |
| 39 | +- **Runtime:** Node.js 18+, TypeScript, ESM modules |
| 40 | +- **Bot:** Grammy 1.40 (Telegram Bot API) |
| 41 | +- **HTTP:** Fastify 5.7 |
| 42 | +- **Database:** MongoDB via Mongoose 8 + Typegoose 12 |
| 43 | +- **Frontend:** Vue 3.5, Vite 7, Pinia, Element Plus, Vue Router |
| 44 | +- **Blockchain:** TON (@ton/core, @ton/ton, TonConnect SDK, TonWeb) |
| 45 | +- **External APIs:** Pinata (IPFS), OpenAI, Stability AI, Telemetree |
| 46 | +- **Testing:** Node.js built-in test runner (`node --test`) |
| 47 | + |
| 48 | +## Key Commands |
| 49 | + |
| 50 | +```bash |
| 51 | +npm install && npm --prefix src/frontend install # Install all deps |
| 52 | +npm run dev # Backend watch mode (tsx) |
| 53 | +npm --prefix src/frontend run dev # Frontend dev server (port 5173) |
| 54 | +npm run build:all # Build backend (tsc) + frontend (vite) |
| 55 | +npm run lint # ESLint |
| 56 | +npm run format # Prettier |
| 57 | +npm run typecheck # TypeScript check |
| 58 | +npm run test:backend # Run backend tests |
| 59 | +``` |
| 60 | + |
| 61 | +## Conventions |
| 62 | + |
| 63 | +- **ESM only** — no CommonJS `require()`. Use `import` everywhere. |
| 64 | +- **Path alias:** `#root/*` maps to `./build/src/*` (backend). `@/*` maps to `./src/*` (frontend). |
| 65 | +- **Separate packages:** Frontend has its own `package.json` under `src/frontend/`. |
| 66 | +- **Don't touch `build/`** — it's generated output from `tsc`. |
| 67 | +- **Typegoose decorators** require `experimentalDecorators` and `emitDecoratorMetadata`. |
| 68 | +- **No semicolons**, single quotes, 2-space indent (Prettier config). |
| 69 | + |
| 70 | +## Data Models |
| 71 | + |
| 72 | +| Model | Purpose | Key Fields | |
| 73 | +|-------|---------|------------| |
| 74 | +| User | Telegram user profile + game state | id, wallet, votes, state, minted, referalId | |
| 75 | +| Balance | Balance change history | userId, amount (bigint), type (enum), createdAt | |
| 76 | +| Claim | Daily claim rewards with streaks | user (ref), streakDays, lastClaimDate, totalClaimed | |
| 77 | +| CNFT | Compressed NFT metadata | index, userId, wallet, votes, type, color, minted | |
| 78 | +| Transaction | TON transaction records | utime, lt, address, coins, hash, accepted | |
| 79 | +| Vote | Referral tracking | giver, receiver, quantity | |
| 80 | + |
| 81 | +## API Endpoints (all under `/api/`) |
| 82 | + |
| 83 | +| Method | Path | Purpose | |
| 84 | +|--------|------|---------| |
| 85 | +| POST | /api/auth/login | Authenticate via Telegram initData | |
| 86 | +| POST | /api/auth/set-wallet | Store user's TON wallet | |
| 87 | +| POST | /api/captcha | Bot captcha verification | |
| 88 | +| GET | /api/nft/:index | NFT metadata + image serving | |
| 89 | +| GET | /api/users/balance | User balance query | |
| 90 | +| GET | /api/users/leaderboard | Ranked leaderboard | |
| 91 | +| POST | /api/users/claim | Daily reward claim | |
| 92 | + |
| 93 | +## Bot Features |
| 94 | + |
| 95 | +Core commands: `/start`, `/mint`, `/dice`, `/balance`, `/whales`, `/webapp` |
| 96 | +Admin commands: `/stats`, `/queue`, `/play`, `/topup`, `/collection`, `/parameters` |
| 97 | + |
| 98 | +## Architecture Notes |
| 99 | + |
| 100 | +- Bot startup flow: `main.ts` → connect MongoDB → create bot → create server → start subscription → start bot |
| 101 | +- Auth uses Telegram's `initData` signature validation (7-day expiry) |
| 102 | +- Transaction monitoring polls TON blockchain for incoming wallet transactions |
| 103 | +- NFT minting pipeline: select image → AI generation (Stability) → IPFS upload (Pinata) → on-chain mint |
| 104 | +- Claim system: 60s cooldown, 10-day max streak, 100 base reward with multiplier |
| 105 | + |
| 106 | +## Current State & Known Issues |
| 107 | + |
| 108 | +- **TODOs in code:** |
| 109 | + - `src/bot/features/play.ts:41` — Story game lacks conversation persistence in DB |
| 110 | + - `src/common/helpers/telegram.ts:114` — User activity tracking logic incomplete |
| 111 | + - `src/bot/features/admin/queue.ts:244` — `sendNewPlaces` notification commented out |
| 112 | +- **Test coverage:** Auth, claim, wallet, balances, leaderboard handlers tested. NFT handler untested. |
| 113 | +- **Error handling:** NFT handler returns plain objects without proper HTTP status codes. |
| 114 | +- **Hidden routes:** `/cnft`, `/faq`, `/clicker` exist but are not shown in navigation menu. |
| 115 | + |
| 116 | +## When Making Changes |
| 117 | + |
| 118 | +- **Backend handler changes:** Check corresponding test file, run `npm run test:backend` |
| 119 | +- **Model changes:** Check all handlers and helpers that reference the model |
| 120 | +- **Frontend changes:** Stay within `src/frontend/` subtree |
| 121 | +- **Config changes:** Update both `src/config.ts` and `.env.example` |
| 122 | +- **Bot feature changes:** Check `src/bot/index.ts` for feature registration order |
| 123 | +- Always run `npm run lint` and `npm run typecheck` before considering work done |
0 commit comments