Files
PolyHermes/backend/IMPLEMENTATION.md
T
WrBug 4f7fef145f Initial commit: Polymarket copy trading bot
- Backend: Spring Boot + Kotlin implementation
  - Account management with private key import
  - Leader management
  - Copy trading configuration
  - Order synchronization
  - Balance and position queries via Polymarket API
  - Ethereum RPC integration for USDC balance
  - Proxy address calculation

- Frontend: React + TypeScript
  - Account management UI
  - Mobile responsive design
  - Account import with private key/mnemonic support
  - Balance display and account details modal

- Database: MySQL with Flyway migrations
- API Integration: Polymarket CLOB API, Data API, Ethereum RPC
2025-11-21 04:32:08 +08:00

240 lines
6.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 后端实现总结
## 已完成的工作
### 1. 项目基础结构 ✅
- ✅ 创建了完整的后端项目目录结构
- ✅ 配置了 `application.properties` 配置文件
- ✅ 创建了 Spring Boot 主应用类
- ✅ 配置了 Gradle 构建文件
### 2. 工具类 ✅
`/Users/wrbug/hype-quant/quant/src/main/kotlin/com/hypequant/util` 拷贝并适配了以下工具类:
-`SafeConvertExt.kt` - 安全类型转换扩展函数
-`MathExt.kt` - BigDecimal 数学运算扩展函数
-`SystemExt.kt` - 系统环境变量工具
-`OkHttpExt.kt` - OkHttp 客户端扩展函数
-`CategoryValidator.kt` - 分类验证工具类(新增)
### 3. Polymarket API 封装 ✅
#### 3.1 CLOB API ✅
- ✅ 定义了 `PolymarketClobApi` 接口
- ✅ 实现了 `PolymarketClobService` 服务封装
- ✅ 支持的功能:
- 获取订单簿
- 获取价格信息
- 获取中间价
- 创建订单
- 获取活跃订单
- 取消订单
- 获取交易记录
#### 3.2 Gamma API ✅
- ✅ 定义了 `PolymarketGammaApi` 接口
- ✅ 实现了 `PolymarketGammaService` 服务封装
- ✅ 支持的功能:
- 获取市场列表(带分类验证)
- 获取市场详情
- 搜索市场(带分类验证)
- 获取事件列表(带分类验证)
- 获取体育市场
- 获取加密货币市场
### 4. WebSocket 转发服务 ✅
- ✅ 实现了 `PolymarketWebSocketHandler` - WebSocket 处理器
- ✅ 实现了 `PolymarketWebSocketClient` - Polymarket RTDS 客户端
- ✅ 配置了 `WebSocketConfig` - WebSocket 配置
- ✅ 支持双向消息转发:
- 前端 → 后端 → Polymarket RTDS
- Polymarket RTDS → 后端 → 前端
### 5. 统一响应格式 ✅
- ✅ 创建了 `ApiResponse` 统一响应格式
- ✅ 提供了便捷的响应创建方法:
- `success()` - 成功响应
- `error()` - 错误响应
- `paramError()` - 参数错误
- `authError()` - 认证错误
- `notFound()` - 资源不存在
- `businessError()` - 业务逻辑错误
- `serverError()` - 服务器错误
### 6. Controller 实现 ✅
- ✅ 实现了 `MarketController` - 市场相关接口
- ✅ 提供的接口:
- `POST /api/markets/list` - 获取市场列表
- `POST /api/markets/detail` - 获取市场详情
- `POST /api/markets/search` - 搜索市场
- `POST /api/markets/sports` - 获取体育市场
- `POST /api/markets/crypto` - 获取加密货币市场
### 7. 配置类 ✅
-`RetrofitConfig` - Retrofit 和 API 客户端配置
-`WebSocketConfig` - WebSocket 配置
## 项目结构
```
backend/
├── src/main/kotlin/com/wrbug/polymarketbot/
│ ├── api/ # API 接口定义
│ │ ├── PolymarketClobApi.kt
│ │ └── PolymarketGammaApi.kt
│ ├── config/ # 配置类
│ │ ├── RetrofitConfig.kt
│ │ └── WebSocketConfig.kt
│ ├── controller/ # 控制器
│ │ └── MarketController.kt
│ ├── dto/ # 数据传输对象
│ │ └── ApiResponse.kt
│ ├── service/ # 服务层
│ │ ├── PolymarketClobService.kt
│ │ └── PolymarketGammaService.kt
│ ├── util/ # 工具类
│ │ ├── SafeConvertExt.kt
│ │ ├── MathExt.kt
│ │ ├── SystemExt.kt
│ │ ├── OkHttpExt.kt
│ │ └── CategoryValidator.kt
│ ├── websocket/ # WebSocket 处理
│ │ ├── PolymarketWebSocketHandler.kt
│ │ └── PolymarketWebSocketClient.kt
│ └── PolymarketBotApplication.kt # 主应用类
├── src/main/resources/
│ └── application.properties # 配置文件
├── build.gradle.kts # Gradle 构建配置
└── settings.gradle.kts # Gradle 设置
```
## 关键特性
### 1. 分类验证
所有涉及分类的接口都会验证分类参数,仅支持 `sports``crypto`
```kotlin
// 自动验证分类
CategoryValidator.validate(category)
```
### 2. 统一错误处理
所有接口统一返回 `ApiResponse` 格式,包含:
- `code`: 错误码
- `data`: 响应数据
- `msg`: 错误消息
### 3. WebSocket 转发
前端通过 `ws://localhost:8000/ws/polymarket` 连接,后端自动转发到 Polymarket RTDS。
### 4. 异步支持
所有 API 调用使用 Kotlin Coroutines 的 `suspend` 函数,Controller 层使用 `runBlocking` 调用。
## 依赖说明
主要依赖:
- Spring Boot 3.2.0
- Kotlin 1.9.20
- Retrofit 2.9.0
- OkHttp 4.12.0
- Java-WebSocket 1.5.4
- MySQL Connector 8.2.0
## 配置说明
### 环境变量
- `DB_USERNAME`: 数据库用户名
- `DB_PASSWORD`: 数据库密码
- `SERVER_PORT`: 服务器端口(默认 8000
- `POLYMARKET_API_KEY`: Polymarket API Key(可选)
### application.properties
```properties
# Polymarket API 配置
polymarket.clob.base-url=https://clob.polymarket.com
polymarket.gamma.base-url=https://gamma-api.polymarket.com
polymarket.rtds.ws-url=wss://ws-live-data.polymarket.com
polymarket.api-key=${POLYMARKET_API_KEY:}
```
## 使用示例
### 1. 获取市场列表
```bash
curl -X POST http://localhost:8000/api/markets/list \
-H "Content-Type: application/json" \
-d '{
"category": "sports",
"active": true,
"limit": 20
}'
```
### 2. WebSocket 连接
```javascript
const ws = new WebSocket('ws://localhost:8000/ws/polymarket');
ws.onopen = () => {
// 订阅市场数据
ws.send(JSON.stringify({
type: 'subscribe',
channel: 'market',
market: 'market_id'
}));
};
ws.onmessage = (event) => {
console.log('收到消息:', event.data);
};
```
## 下一步工作
1. **数据库实体和 Repository**
- 创建 Market、Order、Trade 等实体类
- 实现对应的 Repository 接口
- 创建 Flyway 迁移脚本
2. **数据同步服务**
- 实现市场数据同步任务
- 实现价格更新任务
- 实现订单状态同步
3. **订单管理 Controller**
- 实现订单创建接口
- 实现订单查询接口
- 实现订单取消接口
4. **测试**
- 单元测试
- 集成测试
- API 测试
5. **文档**
- API 文档
- 部署文档
## 注意事项
1. **分类限制**: 严格限制只支持 `sports``crypto` 两个分类
2. **WebSocket**: 生产环境需要配置正确的 CORS 策略
3. **API Key**: 某些接口需要配置 Polymarket API Key
4. **错误处理**: 所有异常都会被捕获并返回统一格式的错误响应