# PolyHermes Development Guide > π **δΈζηζ¬**: [εΌεζζ‘£οΌδΈζοΌ](../zh/DEVELOPMENT.md) This document describes the development guide for the PolyHermes project, including project structure, development environment setup, code standards, API interfaces, etc. ## π Table of Contents - [Project Structure](#project-structure) - [Development Environment Setup](#development-environment-setup) - [Code Standards](#code-standards) - [API Documentation](#api-documentation) - [Database Design](#database-design) - [Frontend Development Guide](#frontend-development-guide) - [Backend Development Guide](#backend-development-guide) - [FAQ](#faq) ## π¦ Project Structure ``` polyhermes/ βββ backend/ # Backend service β βββ src/main/kotlin/ β β βββ com/wrbug/polymarketbot/ β β βββ api/ # API interface definitions (Retrofit) β β βββ config/ # Configuration classes β β βββ controller/ # REST controllers β β βββ dto/ # Data Transfer Objects β β βββ entity/ # Database entities β β βββ repository/ # Data access layer β β βββ service/ # Business logic services β β βββ util/ # Utility classes β β βββ websocket/ # WebSocket handling β βββ src/main/resources/ β βββ application.properties β βββ db/migration/ # Flyway database migration scripts βββ frontend/ # Frontend application β βββ src/ β β βββ components/ # Common components β β βββ pages/ # Page components β β βββ services/ # API services β β βββ store/ # State management (Zustand) β β βββ types/ # TypeScript type definitions β β βββ utils/ # Utility functions β β βββ hooks/ # React Hooks β β βββ locales/ # Internationalization resources β β βββ styles/ # Style files β βββ public/ # Static resources βββ docs/ # Documentation β βββ zh/ # Chinese documentation β β βββ DEPLOYMENT.md # Deployment documentation β β βββ VERSION_MANAGEMENT.md # Version management documentation β β βββ ... β βββ en/ # English documentation β β βββ DEPLOYMENT.md # Deployment documentation β β βββ VERSION_MANAGEMENT.md # Version management documentation β β βββ ... β βββ copy-trading-requirements.md # Copy trading system requirements βββ .github/workflows/ # GitHub Actions workflows βββ README.md # Project description ``` ## π οΈ Development Environment Setup ### Prerequisites - **JDK**: 17+ - **Node.js**: 18+ - **MySQL**: 8.0+ - **Gradle**: 7.5+ (or use Gradle Wrapper) - **Docker**: 20.10+ (optional, for containerized deployment) ### Backend Development Environment 1. **Clone Repository** ```bash git clone https://github.com/WrBug/PolyHermes.git cd PolyHermes ``` 2. **Configure Database** Create MySQL database: ```sql CREATE DATABASE polyhermes CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; ``` 3. **Configure Environment Variables** Edit `backend/src/main/resources/application.properties` or use environment variables: ```properties # Database configuration spring.datasource.url=jdbc:mysql://localhost:3306/polyhermes?useSSL=false&serverTimezone=UTC&characterEncoding=utf8mb4 spring.datasource.username=${DB_USERNAME:root} spring.datasource.password=${DB_PASSWORD:password} # Server port server.port=${SERVER_PORT:8000} # JWT secret jwt.secret=${JWT_SECRET:change-me-in-production} # Encryption key (for encrypting stored private keys and API Keys) crypto.secret.key=${CRYPTO_SECRET_KEY:change-me-in-production} ``` 4. **Start Backend Service** ```bash cd backend ./gradlew bootRun ``` Backend service will start at `http://localhost:8000`. ### Frontend Development Environment 1. **Install Dependencies** ```bash cd frontend npm install ``` 2. **Configure Environment Variables (Optional)** Create `.env` file: ```env VITE_API_URL=http://localhost:8000 VITE_WS_URL=ws://localhost:8000 ``` 3. **Start Development Server** ```bash npm run dev ``` Frontend application will start at `http://localhost:3000`. ## π Code Standards ### Backend Development Standards For detailed standards, please refer to: [Backend Development Standards](.cursor/rules/backend.mdc) **Core Standards**: - Follow Kotlin coding standards - Controller methods **must not** use `suspend` - Entity ID fields use `Long? = null` - All time fields use `Long` timestamps (milliseconds) - Use `BigDecimal` for numerical calculations - Use `ErrorCode` enum to define error codes and messages - **Do not** add TODO comments in code - **Do not** directly return mock data ### Frontend Development Standards For detailed standards, please refer to: [Frontend Development Standards](.cursor/rules/frontend.mdc) **Core Standards**: - Use TypeScript type definitions - Use functional components and Hooks - **Do not** use `any` type - **Must** use internationalization (i18n) for all text display - **Must** use `formatUSDC` function to format USDC amounts - **Must** support mobile and desktop - **Do not** add TODO comments in code ### Commit Standards Follow [Conventional Commits](https://www.conventionalcommits.org/) standards: - `feat`: New feature - `fix`: Bug fix - `docs`: Documentation update - `style`: Code style adjustment - `refactor`: Code refactoring - `test`: Test related - `chore`: Build/tool related Examples: ```bash git commit -m "feat: Add version number display feature" git commit -m "fix: Fix order status update issue" ``` ## π‘ API Documentation ### Unified Response Format All API interfaces use POST method uniformly, response format as follows: ```json { "code": 0, "data": {}, "msg": "" } ``` - `code`: Response code, 0 means success, non-0 means failure - `data`: Response data, can be any type - `msg`: Response message, usually empty on success, contains error message on failure ### Error Code Standards - `0`: Success - `1001-1999`: Parameter error - `2001-2999`: Authentication/permission error - `3001-3999`: Resource not found - `4001-4999`: Business logic error - `5001-5999`: Server internal error ### Main API Interfaces #### Account Management - `POST /api/accounts/list` - Get account list - `POST /api/accounts/import` - Import account (via private key) - `POST /api/accounts/detail` - Get account details - `POST /api/accounts/edit` - Edit account - `POST /api/accounts/delete` - Delete account - `POST /api/accounts/balance` - Get account balance #### Leader Management - `POST /api/leaders/list` - Get Leader list - `POST /api/leaders/add` - Add Leader - `POST /api/leaders/edit` - Edit Leader - `POST /api/leaders/delete` - Delete Leader #### Copy Trading Templates - `POST /api/templates/list` - Get template list - `POST /api/templates/add` - Add template - `POST /api/templates/edit` - Edit template - `POST /api/templates/delete` - Delete template #### Copy Trading Configuration - `POST /api/copy-trading/list` - Get copy trading configuration list - `POST /api/copy-trading/add` - Add copy trading configuration - `POST /api/copy-trading/edit` - Edit copy trading configuration - `POST /api/copy-trading/delete` - Delete copy trading configuration - `POST /api/copy-trading/enable` - Enable copy trading - `POST /api/copy-trading/disable` - Disable copy trading #### Order Management - `POST /api/copy-trading/orders/buy` - Get buy order list - `POST /api/copy-trading/orders/sell` - Get sell order list - `POST /api/copy-trading/orders/matched` - Get matched order list #### Statistical Analysis - `POST /api/statistics/global` - Get global statistics - `POST /api/statistics/leader` - Get Leader statistics - `POST /api/statistics/category` - Get category statistics - `POST /api/copy-trading/statistics` - Get copy trading relationship statistics #### Position Management - `POST /api/positions/list` - Get position list - `POST /api/positions/sell` - Sell position - `POST /api/positions/redeem` - Redeem position #### System Management - `POST /api/system-settings/proxy` - Configure proxy - `POST /api/system-settings/api-health` - Get API health status - `POST /api/users/list` - Get user list - `POST /api/users/add` - Add user - `POST /api/users/edit` - Edit user - `POST /api/users/delete` - Delete user For detailed API interface documentation, please refer to: [Copy Trading System Requirements](../zh/copy-trading-requirements.md) ## ποΈ Database Design ### Main Data Tables - `accounts` - Account table - `leaders` - Leader table - `templates` - Copy trading template table - `copy_trading` - Copy trading configuration table - `copy_orders` - Copy trading order table - `positions` - Position table - `users` - User table - `system_settings` - System settings table Database migration scripts are located at `backend/src/main/resources/db/migration/`, managed using Flyway. ## π¨ Frontend Development Guide ### Project Structure ``` frontend/src/ βββ components/ # Common components β βββ Layout.tsx # Layout component (supports mobile) β βββ Logo.tsx # Logo component βββ pages/ # Page components β βββ AccountList.tsx β βββ LeaderList.tsx β βββ CopyTradingList.tsx β βββ ... βββ services/ # API services β βββ api.ts # API service definitions β βββ websocket.ts # WebSocket service βββ store/ # State management (Zustand) βββ types/ # TypeScript type definitions βββ utils/ # Utility functions β βββ index.ts # Unified export β βββ ethers.ts # Ethereum related utilities β βββ auth.ts # Authentication related utilities β βββ version.ts # Version number utilities βββ hooks/ # React Hooks βββ locales/ # Internationalization resources β βββ zh-CN/ β βββ zh-TW/ β βββ en/ βββ styles/ # Style files ``` ### Internationalization Support The project supports multiple languages (Simplified Chinese, Traditional Chinese, English), using `react-i18next`. **Adding New Translations**: 1. Add translations in `src/locales/{locale}/common.json` 2. Use `useTranslation` Hook in components: ```typescript import { useTranslation } from 'react-i18next' const MyComponent: React.FC = () => { const { t } = useTranslation() return