- 删除 application.properties 中的 polygon.rpc.url 配置 - 更新 ApiHealthCheckService 直接使用 RpcNodeService.getHttpUrl() - 删除所有 Docker Compose 配置中的 POLYGON_RPC_URL 环境变量 - 删除所有部署脚本中的 POLYGON_RPC_URL 环境变量 - 更新所有文档,移除 POLYGON_RPC_URL 相关说明 - 删除 application.properties 中无用的 position.push 配置项 - 修正日志配置中的包名(polyhermes -> polymarketbot) 现在系统通过 RpcNodeService 从数据库读取 RPC 节点配置,用户可以通过系统设置页面管理 RPC 节点,不再需要环境变量配置。
15 KiB
PolyHermes Development Guide
📖 中文版本: 开发文档(中文)
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
- Development Environment Setup
- Code Standards
- API Documentation
- Database Design
- Frontend Development Guide
- Backend Development Guide
- 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
- Clone Repository
git clone https://github.com/WrBug/PolyHermes.git
cd PolyHermes
- Configure Database
Create MySQL database:
CREATE DATABASE polyhermes CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
- Configure Environment Variables
Edit backend/src/main/resources/application.properties or use environment variables:
# 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}
- Start Backend Service
cd backend
./gradlew bootRun
Backend service will start at http://localhost:8000.
Frontend Development Environment
- Install Dependencies
cd frontend
npm install
- Configure Environment Variables (Optional)
Create .env file:
VITE_API_URL=http://localhost:8000
VITE_WS_URL=ws://localhost:8000
- Start Development Server
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
Core Standards:
- Follow Kotlin coding standards
- Controller methods must not use
suspend - Entity ID fields use
Long? = null - All time fields use
Longtimestamps (milliseconds) - Use
BigDecimalfor numerical calculations - Use
ErrorCodeenum 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
Core Standards:
- Use TypeScript type definitions
- Use functional components and Hooks
- Do not use
anytype - Must use internationalization (i18n) for all text display
- Must use
formatUSDCfunction to format USDC amounts - Must support mobile and desktop
- Do not add TODO comments in code
Commit Standards
Follow Conventional Commits standards:
feat: New featurefix: Bug fixdocs: Documentation updatestyle: Code style adjustmentrefactor: Code refactoringtest: Test relatedchore: Build/tool related
Examples:
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:
{
"code": 0,
"data": {},
"msg": ""
}
code: Response code, 0 means success, non-0 means failuredata: Response data, can be any typemsg: Response message, usually empty on success, contains error message on failure
Error Code Standards
0: Success1001-1999: Parameter error2001-2999: Authentication/permission error3001-3999: Resource not found4001-4999: Business logic error5001-5999: Server internal error
Main API Interfaces
Account Management
POST /api/accounts/list- Get account listPOST /api/accounts/import- Import account (via private key)POST /api/accounts/detail- Get account detailsPOST /api/accounts/edit- Edit accountPOST /api/accounts/delete- Delete accountPOST /api/accounts/balance- Get account balance
Leader Management
POST /api/leaders/list- Get Leader listPOST /api/leaders/add- Add LeaderPOST /api/leaders/edit- Edit LeaderPOST /api/leaders/delete- Delete Leader
Copy Trading Templates
POST /api/templates/list- Get template listPOST /api/templates/add- Add templatePOST /api/templates/edit- Edit templatePOST /api/templates/delete- Delete template
Copy Trading Configuration
POST /api/copy-trading/list- Get copy trading configuration listPOST /api/copy-trading/add- Add copy trading configurationPOST /api/copy-trading/edit- Edit copy trading configurationPOST /api/copy-trading/delete- Delete copy trading configurationPOST /api/copy-trading/enable- Enable copy tradingPOST /api/copy-trading/disable- Disable copy trading
Order Management
POST /api/copy-trading/orders/buy- Get buy order listPOST /api/copy-trading/orders/sell- Get sell order listPOST /api/copy-trading/orders/matched- Get matched order list
Statistical Analysis
POST /api/statistics/global- Get global statisticsPOST /api/statistics/leader- Get Leader statisticsPOST /api/statistics/category- Get category statisticsPOST /api/copy-trading/statistics- Get copy trading relationship statistics
Position Management
POST /api/positions/list- Get position listPOST /api/positions/sell- Sell positionPOST /api/positions/redeem- Redeem position
System Management
POST /api/system-settings/proxy- Configure proxyPOST /api/system-settings/api-health- Get API health statusPOST /api/users/list- Get user listPOST /api/users/add- Add userPOST /api/users/edit- Edit userPOST /api/users/delete- Delete user
For detailed API interface documentation, please refer to: Copy Trading System Requirements
🗄️ Database Design
Main Data Tables
accounts- Account tableleaders- Leader tabletemplates- Copy trading template tablecopy_trading- Copy trading configuration tablecopy_orders- Copy trading order tablepositions- Position tableusers- User tablesystem_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:
- Add translations in
src/locales/{locale}/common.json - Use
useTranslationHook in components:
import { useTranslation } from 'react-i18next'
const MyComponent: React.FC = () => {
const { t } = useTranslation()
return <div>{t('key')}</div>
}
Mobile Adaptation
- Use
react-responsiveto detect device type - Breakpoint settings: Mobile < 768px, Desktop >= 768px
- Use responsive layouts and components
Utility Functions
USDC Amount Formatting:
import { formatUSDC } from '../utils'
const balance = formatUSDC('1.23456') // "1.2345"
Ethereum Address Validation:
import { isValidWalletAddress } from '../utils'
if (isValidWalletAddress(address)) {
// Address is valid
}
⚙️ Backend Development Guide
Project Structure
backend/src/main/kotlin/com/wrbug/polymarketbot/
├── api/ # API interface definitions (Retrofit)
│ ├── PolymarketClobApi.kt
│ ├── PolymarketGammaApi.kt
│ └── GitHubApi.kt
├── controller/ # REST controllers
├── service/ # Business logic services
├── entity/ # Database entities
├── repository/ # Data access layer
├── dto/ # Data Transfer Objects
├── util/ # Utility classes
│ ├── CryptoUtils.kt # Encryption utilities
│ ├── RetrofitFactory.kt # Retrofit factory
│ └── ...
└── websocket/ # WebSocket handling
Creating New API Interface
- Define Retrofit Interface (in
api/directory):
interface MyApi {
@POST("/endpoint")
suspend fun myMethod(@Body request: MyRequest): Response<MyResponse>
}
- Create Service (in
service/directory):
@Service
class MyService(
private val myApi: MyApi
) {
suspend fun doSomething(): Result<MyResponse> {
// Business logic
}
}
- Create Controller (in
controller/directory):
@RestController
@RequestMapping("/api/my")
class MyController(
private val myService: MyService,
private val messageSource: MessageSource
) {
@PostMapping("/list")
fun list(@RequestBody request: MyListRequest): ResponseEntity<ApiResponse<MyListResponse>> {
return try {
val data = runBlocking { myService.getList(request) }
ResponseEntity.ok(ApiResponse.success(data))
} catch (e: Exception) {
logger.error("Failed to get list", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, messageSource = messageSource))
}
}
}
Database Operations
Using Spring Data JPA:
@Repository
interface MyRepository : JpaRepository<MyEntity, Long> {
fun findByCode(code: String): MyEntity?
fun findByCategory(category: String): List<MyEntity>
}
Encrypted Storage
Use CryptoUtils to encrypt sensitive data:
@Autowired
private lateinit var cryptoUtils: CryptoUtils
// Encrypt
val encrypted = cryptoUtils.encrypt("sensitive-data")
// Decrypt
val decrypted = cryptoUtils.decrypt(encrypted)
🔧 FAQ
Q1: How to add a new page?
- Create page component in
frontend/src/pages/ - Add route in
frontend/src/App.tsx - Add menu item in
frontend/src/components/Layout.tsx(if needed)
Q2: How to add a new API interface?
- Create Controller in
backend/src/main/kotlin/.../controller/ - Create Service in
backend/src/main/kotlin/.../service/ - Add API call method in
frontend/src/services/api.ts
Q3: How to add a database table?
- Create Entity class (in
entity/directory) - Create Repository interface (in
repository/directory) - Create Flyway migration script (in
resources/db/migration/)
Q4: How to test WebSocket?
Use browser console or WebSocket client tool to connect to ws://localhost:8000/ws
Q5: How to debug backend code?
- Use IDE's debugging feature (IntelliJ IDEA, VS Code, etc.)
- Add logs in code:
logger.debug("Debug info") - View log output:
./gradlew bootRunor view log files
📚 Related Documentation
- Deployment Documentation / English - Detailed deployment guide
- Version Management Documentation / English - Version number management and auto-build
- Copy Trading System Requirements - Backend API interface documentation
- Frontend Requirements - Frontend feature documentation
🤝 Contributing
Contributions are welcome! Please follow these steps:
- Fork this repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Follow code standards
- Commit your changes (
git commit -m 'feat: Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Happy Coding! 🚀