Compare commits

..

2 Commits

Author SHA1 Message Date
WrBug 10b374b4d9 Merge branch 'pre_release' into feature_nba_quant
# Conflicts:
#	backend/src/main/kotlin/com/wrbug/polymarketbot/api/PolymarketGammaApi.kt
#	frontend/src/components/Layout.tsx
#	frontend/src/types/index.ts
2026-01-03 19:53:03 +08:00
WrBug e9e1111df9 feat: 实现 NBA 量化交易系统
- 后端实现:
  - 实现 NBA 比赛数据服务,从 Polymarket API 获取数据
  - 实现数据库存储和增量拉取逻辑(优先从 DB 获取,数据不足时增量拉取)
  - 使用 sports_market_types 参数直接筛选 moneyline 类型
  - 实现分页拉取逻辑(基于 gameStartTime 和 createdAt)
  - 移除 nba_markets 相关的外键约束(V12 迁移)
  - 修复数据拉取逻辑:超过 3 天的数据不拉取

- 前端实现:
  - 实现策略创建/编辑/列表页面
  - 实现交易信号展示页面和统计页面
  - 修复重复请求问题(使用 useCallback 包装 fetchGames)
  - 支持选择单场比赛进行配置
  - 使用西8区时间格式化显示

- 数据库:
  - 创建 NBA 量化交易相关表(V11 迁移)
  - 移除外键约束(V12 迁移)

- 文档:
  - 添加产品需求文档、技术方案、算法文档等
2025-12-13 08:05:08 +08:00
145 changed files with 15830 additions and 8614 deletions
+595 -172
View File
@@ -5,62 +5,391 @@ path: backend/**
# 后端开发规范
## 核心原则
- **禁止**在代码中添加 TODO/FIXME/XXX 注释
- **禁止**返回 mock 数据或硬编码的假数据
- **禁止**在 API 调用失败时返回默认值作为 fallback
- 所有功能必须完整实现,确保代码可以正常运行
## 代码完成规范
### TODO 处理规则
- **禁止**在代码中添加 TODO 注释
- **必须**根据 TODO 的内容直接完成代码实现
- 如果遇到暂时无法完全实现的功能,应该:
1. 实现一个可用的基础版本
2. 添加清晰的注释说明当前实现的限制和后续改进方向
3. 确保代码可以正常编译和运行
- **禁止**使用 `// TODO: 实现XXX` 这样的注释
- **禁止**使用 `// FIXME:` 或 `// XXX:` 这样的注释
- 如果某个功能需要依赖外部资源(如 API、库等),应该:
1. 先实现一个占位或模拟实现
2. 在注释中说明依赖关系和实现方式
3. 确保代码逻辑完整,不会因为未实现的功能而崩溃
### API 调用实现规则
- **必须**查找相关的 API 文档或接口定义
- **必须**根据 API 文档完成代码实现
- **禁止**在 API 调用处添加 TODO 注释
- **禁止**直接返回 mock 数据或硬编码的假数据
- **必须**调用真实的 API 或查询真实的数据库
- 如果 API 文档不完整,应该:
1. 查找项目中已有的类似 API 调用作为参考
2. 查看 API 接口定义(如 `PolymarketClobApi.kt`
3. 查看 API 文档(如 `docs/polymarket-api-reference.md`
4. 实现一个可用的版本,包含错误处理
- 如果 API 调用失败,应该:
1. 返回明确的错误信息
2. 记录错误日志
3. 返回错误响应,而不是返回 mock 数据
### 代码完成示例
```kotlin
// ❌ 错误:添加 TODO 注释
fun getAccountBalance(accountId: Long?): Result<AccountBalanceResponse> {
// TODO: 调用 Polymarket API 查询余额
return Result.success(AccountBalanceResponse(usdcBalance = "0"))
}
// ✅ 正确:查找 API 文档并完成实现
// 1. 查找 API 接口定义:PolymarketClobApi.getActiveOrders()
// 2. 查找 API 文档:docs/polymarket-api-reference.md
// 3. 实现完整的 API 调用逻辑,调用真实的 API
fun getAccountBalance(accountId: Long?): Result<AccountBalanceResponse> {
return try {
val account = getAccount(accountId) ?: return Result.failure(IllegalArgumentException("账户不存在"))
// 如果账户没有配置 API 凭证,返回错误而不是 mock 数据
if (account.apiKey == null || account.apiSecret == null || account.apiPassphrase == null) {
return Result.failure(IllegalStateException("账户未配置 API 凭证,无法查询余额"))
}
// 解密 API 凭证并创建认证客户端
val apiKey = cryptoUtils.decrypt(account.apiKey!!)
val apiSecret = cryptoUtils.decrypt(account.apiSecret!!)
val apiPassphrase = cryptoUtils.decrypt(account.apiPassphrase!!)
val clobApi = retrofitFactory.createClobApi(apiKey, apiSecret, apiPassphrase)
// 调用真实的 API 查询余额
val response = clobApi.getActiveOrders(limit = 100)
if (response.isSuccessful && response.body() != null) {
// 根据 API 响应处理余额数据(从真实响应中解析)
val orders = response.body()!!
// 实际应该调用余额查询接口或从链上查询
// 这里只是示例,实际应该调用真实的余额查询 API
val balance = queryRealBalanceFromApi(clobApi, account.walletAddress)
Result.success(AccountBalanceResponse(usdcBalance = balance))
} else {
logger.error("查询余额失败: ${response.code()} ${response.message()}")
Result.failure(Exception("查询余额失败: ${response.code()} ${response.message()}"))
}
} catch (e: Exception) {
logger.error("查询账户余额失败", e)
Result.failure(e)
}
}
```
## 需求文档引用
### 跟单系统需求
- **需求文档**: `docs/copy-trading-requirements.md`
- **说明**: 所有跟单系统相关的功能实现必须严格按照需求文档执行
- **核心功能**:
- 账户管理(通过私钥导入,支持多账户)
- Leader 管理(被跟单者管理)
- 订单同步与执行(监控 Leader 交易并自动复制)
- 跟单配置管理(全局配置和单个 Leader 配置)
- 风险控制(每日亏损限制、订单数限制等)
- 跟单记录与统计
**重要提示**: 在实现跟单系统相关功能时,请先查阅 `docs/copy-trading-requirements.md` 了解详细需求,包括:
- 数据模型设计(Account、Leader、CopyOrder 等)
- API 接口设计(请求/响应格式)
- 业务规则和验证逻辑
- 安全要求(私钥加密存储、API Key 管理等)
## 项目范围
- **平台**: 仅支持 Polymarket
### 平台支持
- **仅支持**: Polymarket 平台
- **不支持**: 其他预测市场平台(如 Kalshi 等)
### 分类支持
- **仅支持**:
- `sports`: 体育相关市场
- `crypto`: 加密货币相关市场
- **不支持**: 其他分类(如 politics、entertainment 等)
### 分类验证
- 所有涉及分类的接口、实体、服务必须验证分类参数
- 分类参数只能是 `sports` 或 `crypto`
- 无效分类应返回明确的错误提示
## 包名规范
- **包名**: `com.wrbug.polymarketbot`
- 所有代码必须使用此包名
## 实体类规范
### ID字段规范
- **必须**使用 `Long? = null` 作为 `@GeneratedValue` 的 id 字段
- **禁止**使用 `Long = 0` 或其他默认值
```kotlin
// ✅ 正确
@Entity
@Table(name = "example_table")
data class ExampleEntity(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long? = null,
// ...
)
// ❌ 错误
@Entity
data class ExampleEntity(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0, // 禁止使用
// ...
)
```
## 配置文件规范
### 配置文件格式
- **必须**使用 `application.properties` 格式
- **禁止**使用 `application.yml` 格式
- 配置文件位置: `src/main/resources/application.properties`
### 配置示例
```properties
# 应用配置
spring.application.name=polymarket-bot-backend
# 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/polymarket_bot?useSSL=false&serverTimezone=UTC&characterEncoding=utf8mb4
spring.datasource.username=${DB_USERNAME:root}
spring.datasource.password=${DB_PASSWORD:password}
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# HikariCP 连接池配置
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.minimum-idle=2
spring.datasource.hikari.connection-timeout=30000
# JPA 配置
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.show-sql=false
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
# Flyway 配置
spring.flyway.enabled=true
spring.flyway.locations=classpath:db/migration
spring.flyway.baseline-on-migrate=true
# 服务器配置
server.port=${SERVER_PORT:8000}
# 日志配置
logging.level.root=INFO
logging.level.com.wrbug.polymarketbot=DEBUG
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n
```
### 环境变量引用
- 使用 `${ENV_VAR:default}` 格式引用环境变量
- 支持多环境配置: `application-dev.properties`, `application-prod.properties`
## 代码规范
### Controller规范
- Controller 方法**禁止**使用 `suspend`
- 如需调用 suspend 方法,使用 `runBlocking`(最小化使用)
- 只作用于 suspend 方法调用
```kotlin
// ✅ 正确
@RestController
class ExampleController(
private val exampleService: ExampleService
) {
@PostMapping("/example")
fun getExample(): ResponseEntity<ApiResponse<ExampleDto>> {
val data = runBlocking { exampleService.getData() }
return ResponseEntity.ok(ApiResponse.success(data))
}
}
// ❌ 错误
@RestController
class ExampleController {
@PostMapping("/example")
suspend fun getExample(): ResponseEntity<ApiResponse<ExampleDto>> { // 禁止使用suspend
// ...
}
}
```
### Service规范
- Service 层可以使用 `suspend` 方法
- 使用 `@Transactional` 管理事务
- 使用构造函数注入依赖
```kotlin
@Service
class ExampleService(
private val exampleRepository: ExampleRepository
) {
suspend fun getAllData(): List<ExampleEntity> {
return exampleRepository.findAll()
}
@Transactional
fun saveData(entity: ExampleEntity): ExampleEntity {
return exampleRepository.save(entity)
}
}
```
### Repository规范
- Repository 接口继承 `JpaRepository`
- 使用 Spring Data JPA 方法命名规范
```kotlin
@Repository
interface ExampleRepository : JpaRepository<ExampleEntity, Long> {
fun findByCode(code: String): ExampleEntity?
fun findByCategoryAndStatus(category: String, status: String): List<ExampleEntity>
fun findByCategory(category: String): List<ExampleEntity> // category: sports 或 crypto
}
```
### Entity规范
- 使用 `@Entity` 和 `@Table` 注解
- ID字段使用 `Long? = null`
- 时间字段使用 `Long` 时间戳(毫秒)
- 数值字段使用 `BigDecimal`,使用 `String` 存储
```kotlin
@Entity
@Table(name = "example_table")
data class ExampleEntity(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long? = null, // ✅ 正确:使用可空类型
val id: Long? = null,
@Column(name = "created_at", nullable = false)
val createdAt: Long = System.currentTimeMillis(), // ✅ 使用 Long 时间戳
@Column(name = "code", unique = true, nullable = false, length = 100)
val code: String = "",
@Column(name = "category", nullable = false, length = 20)
val category: String = "", // sports 或 crypto
@Column(name = "amount", nullable = false, precision = 20, scale = 8)
val amount: BigDecimal = BigDecimal.ZERO // ✅ 使用 BigDecimal
val amount: BigDecimal = BigDecimal.ZERO,
@Column(name = "status", nullable = false, length = 20)
val status: String = "", // active, inactive
@Column(name = "created_at", nullable = false)
val createdAt: Long = System.currentTimeMillis(),
@Column(name = "updated_at", nullable = false)
var updatedAt: Long = System.currentTimeMillis()
)
```
**规则**:
- ID 字段必须使用 `Long? = null`
- 时间字段使用 `Long` 时间戳(毫秒)
- 数值字段使用 `BigDecimal`
- **禁止**使用 `LocalDateTime` 或 `Double`
## 数值计算规范
## 配置文件规范
- **必须**使用 `application.properties` 格式(禁止 `application.yml`
- 使用 `${ENV_VAR:default}` 引用环境变量
### BigDecimal使用
- 所有数值计算必须使用 `BigDecimal`
- 使用 `String` 存储数值
- 使用扩展函数进行安全转换和比较
## 代码规范
```kotlin
// 使用扩展函数
val amount = "0.5".toSafeBigDecimal()
val total = amount.add("0.4".toSafeBigDecimal())
### Controller
- **禁止**使用 `suspend`
- 使用 `runBlocking` 调用 suspend 方法
- 统一使用 `@PostMapping`
// 比较
if (total.lt(BigDecimal.ONE)) {
// 业务逻辑
}
```
### Service
- 可以使用 `suspend` 方法
- 使用 `@Transactional` 管理事务
- 使用构造函数注入依赖
## 时间字段规范
### Repository
- 继承 `JpaRepository<Entity, Long>`
- 使用 Spring Data JPA 方法命名规范
### 时间戳使用
- 所有时间字段使用 `Long` 类型存储毫秒级时间戳
- **禁止**使用 `LocalDateTime` 或其他时间类型
## API 接口规范
```kotlin
// ✅ 正确
@Column(name = "created_at", nullable = false)
val createdAt: Long = System.currentTimeMillis()
### 统一响应格式
// ❌ 错误
@Column(name = "created_at")
val createdAt: LocalDateTime = LocalDateTime.now() // 禁止使用
```
## HTTP客户端规范
### Retrofit使用
- 使用 Retrofit 定义 API 接口
- 使用 OkHttp 作为底层 HTTP 客户端
- 使用拦截器处理认证
```kotlin
// Polymarket CLOB API接口定义(跟单系统需要)
interface PolymarketClobApi {
@POST("/orders")
suspend fun createOrder(@Body order: CreateOrderRequest): Response<OrderResponse>
@GET("/orders/active")
suspend fun getActiveOrders(
@Query("market") market: String?,
@Query("limit") limit: Int?,
@Query("offset") offset: Int?
): Response<List<OrderResponse>>
@DELETE("/orders/{orderId}")
suspend fun cancelOrder(@Path("orderId") orderId: String): Response<CancelOrderResponse>
@GET("/trades")
suspend fun getTrades(
@Query("market") market: String?,
@Query("user") user: String?,
@Query("limit") limit: Int?,
@Query("offset") offset: Int?
): Response<List<TradeResponse>>
}
// Retrofit配置
@Configuration
class RetrofitConfig {
@Bean
fun polymarketClobApi(): PolymarketClobApi {
val okHttpClient = OkHttpClient.Builder()
.addInterceptor(AuthInterceptor())
.build()
return Retrofit.Builder()
.baseUrl("https://clob.polymarket.com")
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(PolymarketClobApi::class.java)
}
}
```
## API接口规范
### 请求规范
- **所有接口统一使用POST方法**,包括查询类接口
- 请求头: `Content-Type: application/json`
- 请求体: JSON格式
### 响应规范
- **统一响应格式**:
```json
{
"code": 0,
@@ -69,7 +398,77 @@ data class ExampleEntity(
}
```
### 错误码范围
- **响应字段说明**:
- `code`: 响应码,0表示成功,非0表示失败
- `data`: 响应数据,可以是任意类型(对象、数组、字符串、数字等)
- `msg`: 响应消息,成功时通常为空,失败时包含错误提示
### 响应示例
**成功响应**:
```json
{
"code": 0,
"data": {
"id": "123",
"name": "example"
},
"msg": ""
}
```
**失败响应**:
```json
{
"code": 1001,
"data": null,
"msg": "参数错误:参数不能为空"
}
```
### Controller实现示例
```kotlin
@RestController
@RequestMapping("/api/example")
class ExampleController(
private val exampleService: ExampleService
) {
private val logger = LoggerFactory.getLogger(ExampleController::class.java)
@PostMapping("/list")
fun getList(@RequestBody request: ExampleListRequest): ResponseEntity<ApiResponse<ExampleListResponse>> {
return try {
val data = runBlocking { exampleService.getList(request) }
val response = ExampleListResponse(
list = data,
total = data.size.toLong(),
page = request.page ?: 1,
limit = request.limit ?: 20
)
ResponseEntity.ok(ApiResponse.success(response))
} catch (e: Exception) {
logger.error("Failed to get list", e)
ResponseEntity.ok(ApiResponse.serverError("获取列表失败:${e.message}"))
}
}
}
// 统一响应格式
data class ApiResponse<T>(
val code: Int,
val data: T?,
val msg: String
) {
companion object {
fun <T> success(data: T): ApiResponse<T> = ApiResponse(0, data, "")
fun <T> paramError(msg: String): ApiResponse<T> = ApiResponse(1001, null, msg)
fun <T> serverError(msg: String): ApiResponse<T> = ApiResponse(5001, null, msg)
}
}
```
### 错误码规范
- `0`: 成功
- `1001-1999`: 参数错误
- `2001-2999`: 认证/权限错误
@@ -77,131 +476,7 @@ data class ExampleEntity(
- `4001-4999`: 业务逻辑错误
- `5001-5999`: 服务器内部错误
### Controller 示例
```kotlin
@RestController
@RequestMapping("/api/example")
class ExampleController(
private val exampleService: ExampleService,
private val messageSource: MessageSource
) {
@PostMapping("/list")
fun getList(@RequestBody request: ExampleListRequest): ResponseEntity<ApiResponse<ExampleListResponse>> {
return try {
val data = runBlocking { exampleService.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))
}
}
}
```
## 数值计算
```kotlin
val amount = "0.5".toSafeBigDecimal()
val total = amount.add("0.4".toSafeBigDecimal())
if (total.lt(BigDecimal.ONE)) {
// 业务逻辑
}
```
## JSON 解析规范
### 使用扩展函数(推荐)
- **优先**使用扩展函数 `fromJson<T>()` 和 `toJson()`,禁止直接使用 `Gson`
- 扩展函数提供了类型安全的解析,使用方便
```kotlin
// ✅ 正确:使用扩展函数(推荐)
val json = "{\"name\":\"test\"}"
val obj = json.fromJson<MyDataClass>()
val jsonStr = obj.toJson()
// ✅ 也可以:使用 JsonUtils 类(兼容旧代码)
val obj = jsonUtils.fromJson<MyDataClass>(json)
// ❌ 错误:直接使用 Gson
val obj = gson.fromJson(json, MyDataClass::class.java)
```
### 扩展函数列表
- `String?.fromJson<T>()` - 解析 JSON 字符串为对象(支持泛型)
- `JsonElement?.fromJson<T>()` - 解析 JsonElement 为对象(支持泛型)
- `Any?.toJson()` - 将对象转换为 JSON 字符串
- `String?.parseStringArray()` - 解析 JSON 字符串数组
### JsonUtils 类
- `JsonUtils` 主要用于初始化全局 Gson 实例,供扩展函数使用
- 保留 `parseStringArray()` 方法用于兼容旧代码
- 不推荐直接使用 `JsonUtils` 的方法,优先使用扩展函数
### Data Class 规范
- **所有 data class 字段必须提供默认值**
- 可空字段使用 `? = null`
- 非空字段提供合适的默认值(空字符串、空集合、默认数值等)
```kotlin
// ✅ 正确:所有字段都有默认值
data class MyDto(
val name: String = "",
val age: Int = 0,
val tags: List<String> = emptyList(),
val optional: String? = null
)
// ❌ 错误:缺少默认值
data class MyDto(
val name: String, // 缺少默认值
val age: Int = 0
)
```
## Side 判断规范
**禁止**使用 "YES"/"NO" 字符串判断 side
```kotlin
// ❌ 错误
if (side != null && side.uppercase() == "NO") { }
// ✅ 正确
if (outcomeIndex != null && outcomeIndex == 1) { }
```
## 多语言规范
- **禁止**硬编码中文或英文错误消息
- **必须**使用 `ErrorCode` 枚举和 `MessageSource`
```kotlin
// ❌ 错误
return ResponseEntity.ok(ApiResponse.paramError("配置ID不能为空"))
// ✅ 正确
return ResponseEntity.ok(ApiResponse.error(ErrorCode.PARAM_EMPTY, messageSource))
```
### 多语言支持范围
1. **API 响应消息**: 使用 `ErrorCode` + `MessageSource`
2. **日志消息**: 可使用中文或英文
3. **代码注释**: 建议使用中文
4. **数据库字段**: 使用英文(snake_case
## HTTP 客户端
- 使用 Retrofit + OkHttp
- 使用拦截器处理认证
## 跟单系统需求
参考文档: `docs/copy-trading-requirements.md`
核心功能:
- 账户管理(私钥导入,多账户)
- Leader 管理
- 订单同步与执行
- 跟单配置管理
- 风险控制
- 跟单记录与统计
详细错误码定义参见需求文档
## 禁止事项
@@ -209,25 +484,173 @@ return ResponseEntity.ok(ApiResponse.error(ErrorCode.PARAM_EMPTY, messageSource)
- ❌ 禁止使用 `!!` 除非有明确原因
- ❌ 禁止忽略异常
- ❌ 禁止硬编码配置值
- ❌ 禁止提交敏感信息到 Git
- ❌ 禁止提交敏感信息到Git
- ❌ Controller 方法禁止使用 `suspend`
- ❌ 实体类ID禁止使用 `Long = 0`
- ❌ **禁止直接返回 mock 数据或硬编码的假数据**
- ❌ **禁止在 API 调用失败时返回 mock 数据作为默认值**
- ❌ **所有返回的数据必须来自真实的 API 调用或数据库查询**
### 配置
- ❌ 禁止使用 `application.yml`
- ❌ 禁止在代码中硬编码配置值
### 类型
- ❌ 禁止使用 `Double` 进行数值计算
- ❌ 禁止使用 `LocalDateTime` 存储时间
- ❌ 禁止实体类 ID 使用非空默认值
- ❌ 禁止直接使用 `Gson`(必须使用 `JsonUtils`
- ❌ 禁止 data class 字段缺少默认值
- ❌ 禁止实体类ID使用非空默认值
### API 接口
- ❌ 禁止使用 GET/PUT/DELETE(统一使用 POST
### API接口
- ❌ 禁止使用GETPUTDELETE等方法(统一使用POST
- ❌ 禁止返回不符合统一格式的响应
- ❌ 禁止在响应中直接返回 Map 类型
- ❌ 禁止在响应中直接返回Map类型(使用data class
### Side 判断
- ❌ 禁止使用 "YES"/"NO" 字符串判断 side
- ✅ 必须使用 `outcomeIndex` 判断
### Side 判断规范
- ❌ **禁止使用 "YES""NO" 字符串判断 side**
- ✅ **必须使用 `outcomeIndex` 判断方向**0 = 第一个 outcome1 = 第二个 outcome,以此类推)
- ✅ 如果必须使用 side 字符串,应该从市场的 outcomes 数组中获取,而不是硬编码 "YES"/"NO"
- ✅ 对于二元市场的价格转换,应该通过 `outcomeIndex` 判断是否为第二个 outcomeindex = 1),而不是判断 side 是否为 "NO"
### 数据源
-禁止直接返回 mock 数据
- ❌ 禁止在 API 调用失败时返回 mock 数据
- ✅ 所有数据必须来自真实 API 或数据库查询
```kotlin
//错误:使用字符串比较判断 side
if (side != null && side.uppercase() == "NO") {
// 转换价格
}
// ❌ 错误:硬编码 "YES"/"NO" 判断
when (side.uppercase()) {
"YES" -> // ...
"NO" -> // ...
}
// ✅ 正确:使用 outcomeIndex 判断
if (outcomeIndex != null && outcomeIndex == 1) {
// 第二个 outcome(在二元市场中通常是 NO),转换价格
}
// ✅ 正确:从市场 outcomes 获取 side 信息
val outcomes = JsonUtils.parseStringArray(market.outcomes)
val targetOutcomeIndex = outcomes.indexOfFirst { it.equals(side, ignoreCase = true) }
if (targetOutcomeIndex >= 0) {
// 使用 targetOutcomeIndex 进行判断
}
```
## 多语言使用规范
### 错误消息和响应文本
- **禁止**在代码中硬编码中文或英文错误消息
- **必须**使用 `ErrorCode` 枚举定义错误码和消息
- **必须**使用 `ApiResponse.error(ErrorCode, messageSource)` 或 `MessageUtils.getMessage()` 获取国际化消息
- **禁止**直接使用 `ApiResponse.paramError("硬编码消息")` 或 `ApiResponse.serverError("硬编码消息")`
- 错误消息的默认语言使用中文(在 `ErrorCode` 枚举中定义),通过 `MessageSource` 支持多语言
### 使用 ErrorCode 和 MessageSource
项目已经实现了国际化支持,必须使用以下方式:
```kotlin
// ❌ 错误:硬编码错误消息
return ResponseEntity.ok(ApiResponse.paramError("配置ID不能为空"))
return ResponseEntity.ok(ApiResponse.serverError("获取配置列表失败:${e.message}"))
// ✅ 正确:使用 ErrorCode 枚举
return ResponseEntity.ok(ApiResponse.error(ErrorCode.PARAM_EMPTY, messageSource = messageSource))
// ✅ 正确:使用 ErrorCode 和自定义消息(如果需要动态消息)
return ResponseEntity.ok(ApiResponse.error(
ErrorCode.PARAM_ERROR,
customMsg = "配置ID不能为空",
messageSource = messageSource
))
// ✅ 正确:使用 MessageUtils
@Autowired
private lateinit var messageUtils: MessageUtils
return ResponseEntity.ok(ApiResponse.error(
ErrorCode.PARAM_EMPTY,
messageSource = messageSource
))
```
### 添加新的错误码
如果需要添加新的错误码,必须在 `ErrorCode` 枚举中定义:
```kotlin
enum class ErrorCode(
val code: Int,
val message: String, // 默认消息(中文)
val messageKey: String // 国际化消息键
) {
// 新错误码示例
NOTIFICATION_CONFIG_NOT_FOUND(3009, "通知配置不存在", "error.notification_config_not_found"),
NOTIFICATION_CONFIG_INVALID(4009, "通知配置无效", "error.notification_config_invalid"),
}
```
然后在语言资源文件中添加对应的翻译:
- `src/main/resources/messages_zh_CN.properties`
- `src/main/resources/messages_zh_TW.properties`
- `src/main/resources/messages_en.properties`
### 日志消息规范
- **日志消息可以使用中文或英文**,便于开发调试
- **禁止**在日志中硬编码用户可见的错误消息(应该使用 ErrorCode)
- 日志消息应该清晰、简洁,便于排查问题
- 日志中的业务数据(如账户名、订单ID等)可以使用原始值
### 代码注释规范
- **代码注释可以使用中文或英文**
- **业务逻辑注释**建议使用中文,便于团队理解
- **API 文档注释**(KDoc)建议使用中文,但也可以使用英文
- **类和方法注释**应该清晰说明功能和参数
### 数据库字段和配置
- **数据库字段名**使用英文(snake_case
- **配置项名称**使用英文(kebab-case 或 dot.notation
- **枚举值**使用英文(UPPER_SNAKE_CASE
- **常量定义**使用英文(UPPER_SNAKE_CASE
### 多语言支持策略
1. **API 响应消息**:使用 `ErrorCode` 枚举,通过 `MessageSource` 支持多语言
2. **错误码**:使用 `ErrorCode` 枚举,包含 `code`、`message`(默认中文)和 `messageKey`(国际化键)
3. **日志消息**:可以使用中文或英文,便于开发调试
4. **代码注释**:可以使用中文或英文,建议使用中文
5. **业务数据**:根据实际需求,可以包含多语言内容(如市场标题、描述等)
### Controller 中使用多语言示例
```kotlin
@RestController
class NotificationController(
private val notificationConfigService: NotificationConfigService,
private val messageSource: MessageSource // 注入 MessageSource
) {
@PostMapping("/configs/list")
fun list(@RequestBody request: NotificationConfigListRequest): ResponseEntity<ApiResponse<List<NotificationConfigDto>>> {
return try {
// ... 业务逻辑
ResponseEntity.ok(ApiResponse.success(configs))
} catch (e: Exception) {
logger.error("获取配置列表失败: ${e.message}", e)
// ✅ 正确:使用 ErrorCode 和 MessageSource
ResponseEntity.ok(ApiResponse.error(
ErrorCode.SERVER_ERROR,
messageSource = messageSource
))
}
}
@PostMapping("/configs/detail")
fun detail(@RequestBody request: NotificationConfigDetailRequest): ResponseEntity<ApiResponse<NotificationConfigDto>> {
if (request.id == null) {
// ✅ 正确:使用 ErrorCode
return ResponseEntity.ok(ApiResponse.error(
ErrorCode.PARAM_EMPTY,
messageSource = messageSource
))
}
// ... 业务逻辑
}
}
```
+232 -68
View File
@@ -5,41 +5,107 @@ path: frontend/**
# 前端开发规范
## 核心原则
- **禁止**在代码中添加 TODO/FIXME/XXX 注释
- **禁止**使用 `any` 类型
- **禁止**硬编码中文或英文文本
- **必须**支持移动端和桌面端
## 代码完成规范
### TODO 处理规则
- **禁止**在代码中添加 TODO 注释
- **必须**根据 TODO 的内容直接完成代码实现
- 如果遇到暂时无法完全实现的功能,应该:
1. 实现一个可用的基础版本
2. 添加清晰的注释说明当前实现的限制和后续改进方向
3. 确保代码可以正常编译和运行
- **禁止**使用 `// TODO: 实现XXX` 这样的注释
- **禁止**使用 `// FIXME:` 或 `// XXX:` 这样的注释
- 如果某个功能需要依赖外部资源(如 API、库等),应该:
1. 先实现一个占位或模拟实现
2. 在注释中说明依赖关系和实现方式
3. 确保代码逻辑完整,不会因为未实现的功能而崩溃
### API 调用实现规则
- **必须**查找相关的 API 文档或接口定义
- **必须**根据 API 文档完成代码实现
- **禁止**在 API 调用处添加 TODO 注释
- 如果 API 文档不完整,应该:
1. 查找项目中已有的类似 API 调用作为参考
2. 查看 API 服务定义(如 `services/api.ts`
3. 查看 API 文档(如 `docs/copy-trading-requirements.md`
4. 实现一个可用的版本,包含错误处理
### 代码完成示例
```typescript
// ❌ 错误:添加 TODO 注释
const fetchAccountBalance = async (accountId: number) => {
// TODO: 调用 API 查询余额
return { balance: '0' }
}
// ✅ 正确:查找 API 定义并完成实现
// 1. 查找 API 服务定义:services/api.ts 中的 accounts.balance
// 2. 查找 API 文档:docs/copy-trading-requirements.md
// 3. 实现完整的 API 调用逻辑
const fetchAccountBalance = async (accountId: number) => {
try {
// 根据 API 服务定义调用接口
const response = await apiService.accounts.balance({ accountId })
// 根据 API 响应格式处理数据
if (response.data.code === 0 && response.data.data) {
return response.data.data
} else {
// API 调用失败时返回默认值
console.warn('查询余额失败,返回默认值:', response.data.msg)
return { balance: '0' }
}
} catch (error) {
console.error('查询余额异常:', error)
// 异常时返回默认值,确保代码可以正常运行
return { balance: '0' }
}
}
```
## 技术栈
- **框架**: React + TypeScript
- **UI 库**: Ant Design(移动端使用 Ant Design Mobile
- **HTTP 客户端**: axios
- **状态管理**: Zustand
- **国际化**: react-i18next
- **UI库**: Ant Design 或 Material-UI(推荐 Ant Design Mobile 用于移动端
- **HTTP客户端**: axios
- **状态管理**: Zustand 或 Redux
- **响应式设计**: 必须支持移动端和桌面端
## 移动端适配
## 移动端适配要求
### 响应式断点
### 响应式设计
- **必须支持移动端和桌面端**
- 使用响应式布局(Responsive Design
- 移动端优先(Mobile First)设计原则
- 支持触摸操作和手势
### 断点设置
- **移动端**: < 768px
- **平板**: 768px - 1024px
- **桌面端**: > 1024px
### 适配要求
- 使用响应式布局(Mobile First
- 导航栏:移动端抽屉菜单,桌面端顶部导航
- 列表:移动端卡片布局,桌面端表格布局
- 按钮:移动端不小于 44x44px
- 支持触摸操作和手势
### UI 组件适配
- 使用 Ant Design 的响应式组件
- 移动端使用 Ant Design Mobile(如果使用 Ant Design
- 表格使用虚拟滚动或分页(移动端性能优化)
- 表单使用移动端友好的输入组件
### 布局适配
- 导航栏:移动端使用抽屉菜单,桌面端使用顶部导航
- 列表:移动端使用卡片布局,桌面端使用表格布局
- 按钮:移动端按钮尺寸不小于 44x44px(触摸友好)
- 间距:移动端使用更大的间距,提高可点击区域
### 性能优化
- 图片懒加载
- 代码分割(Code Splitting
- 移动端减少动画效果
- 使用 CSS 媒体查询优化样式
## 代码规范
### 组件
### 组件规范
- 使用函数式组件
- 使用 TypeScript 类型定义
- 组件文件使用 PascalCase 命名
@@ -56,18 +122,24 @@ export const MarketCard: React.FC<MarketProps> = ({ marketId, platform }) => {
};
// ❌ 错误
export const marketCard = (props: any) => { }
export const marketCard = (props: any) => { // 禁止使用any
// ...
};
```
### API 调用
- 使用 axios
### API调用规范
- 使用 axios 进行 HTTP 请求
- 统一错误处理
- 使用 TypeScript 定义响应类型
```typescript
// API服务
import axios from 'axios';
interface Market {
id: string;
marketId: string;
platform: string;
title: string;
}
@@ -75,16 +147,24 @@ export const marketService = {
getMarkets: async (): Promise<Market[]> => {
const response = await axios.get<Market[]>('/api/markets');
return response.data;
},
getMarketById: async (id: string): Promise<Market> => {
const response = await axios.get<Market>(`/api/markets/${id}`);
return response.data;
}
};
```
### 状态管理
- 全局状态使用 Zustand
### 状态管理规范
- 使用 Zustand 或 Redux 管理全局状态
- 本地状态使用 `useState`
- 复杂状态使用 `useReducer`
```typescript
// Zustand Store示例
import { create } from 'zustand';
interface MarketStore {
markets: Market[];
setMarkets: (markets: Market[]) => void;
@@ -96,64 +176,80 @@ export const useMarketStore = create<MarketStore>((set) => ({
}));
```
### USDC 金额格式化
**必须**使用 `formatUSDC` 函数(从 `../utils` 导入)
### USDC 金额格式化规范
- **必须**使用 `formatUSDC` 函数格式化所有 USDC 金额显示
- **禁止**直接使用 `toFixed()` 或 `parseFloat().toFixed()` 格式化 USDC
- **禁止**硬编码小数位数格式化 USDC
- 所有 USDC 金额显示必须统一使用 `formatUSDC` 函数
#### formatUSDC 函数说明
- **位置**: `src/utils/index.ts`
- **功能**: 格式化 USDC 金额,最多显示 4 位小数,自动去除尾随零(截断,不四舍五入)
- **参数**: `value: string | number | undefined | null`
- **返回值**: 格式化后的字符串,如果值为空或无效则返回 `'-'`
#### 使用示例
```typescript
// ✅ 正确:使用 formatUSDC 格式化 USDC 金额
import { formatUSDC } from '../utils'
// ✅ 正确
const BalanceDisplay: React.FC<{ balance: string }> = ({ balance }) => {
return <span>{formatUSDC(balance)} USDC</span>
}
// ✅ 正确:在表格列中使用
const columns = [
{
title: '余额',
dataIndex: 'balance',
render: (balance: string) => `${formatUSDC(balance)} USDC`
}
]
// ✅ 正确:在统计组件中使用
<Statistic
title="总盈亏"
value={formatUSDC(stats?.totalPnl || '0')}
suffix="USDC"
/>
// ❌ 错误
const balance = parseFloat(value).toFixed(4)
// ❌ 错误:直接使用 toFixed
const balance = parseFloat(value).toFixed(4) // 禁止
// ❌ 错误:硬编码格式化
const balance = `${parseFloat(value).toFixed(2)} USDC` // 禁止
// ❌ 错误:使用自定义格式化函数
const formatBalance = (value: string) => parseFloat(value).toFixed(4) // 禁止
```
**格式化规则**:
- 最多显示 4 位小数(截断,不四舍五入)
- 自动去除尾随零
- 空值返回 `'-'`
#### 格式化规则
- **最多显示 4 位小数**:如果金额超过 4 位小数,截断到 4 位(不四舍五入)
- **自动去除尾随零**:去除不必要的尾随零和小数点
- **示例**
- `formatUSDC(1.23)` => `"1.23"`
- `formatUSDC(1.23456)` => `"1.2345"`(截断,不四舍五入)
- `formatUSDC(1.2)` => `"1.2"`
- `formatUSDC(1)` => `"1"`
- `formatUSDC(1.23459)` => `"1.2345"`(截断,不四舍五入)
- `formatUSDC(null)` => `"-"`
- `formatUSDC(undefined)` => `"-"`
## 多语言规范
### 使用方式
**必须**使用 `useTranslation` Hook
#### 工具函数统一导出
- 所有工具函数统一从 `src/utils/index.ts` 导出
- **必须**从 `../utils` 导入工具函数,而不是从具体文件导入
- `ethers.ts` 中的函数也会在 `index.ts` 中统一导出
```typescript
// ✅ 正确
import { useTranslation } from 'react-i18next'
// ✅ 正确:从 utils 统一导入
import { formatUSDC, getAddressFromPrivateKey, isValidWalletAddress } from '../utils'
const MyComponent: React.FC = () => {
const { t } = useTranslation()
return (
<div>
<h1>{t('page.title')}</h1>
<Button>{t('common.save')}</Button>
</div>
)
}
// ❌ 错误:硬编码文本
<h1>页面标题</h1>
<Button>保存</Button>
// ❌ 错误:从具体文件导入
import { formatUSDC } from '../utils/index'
import { getAddressFromPrivateKey } from '../utils/ethers'
```
### 语言文件
- 位置: `src/locales/{locale}/common.json`
- 支持: `zh-CN`、`zh-TW`、`en`
- 键名: 点号分隔的层级结构(如 `notificationSettings.title`
### 添加新翻译
1. 在 `src/locales/zh-CN/common.json` 添加中文
2. 在 `src/locales/zh-TW/common.json` 添加繁体
3. 在 `src/locales/en/common.json` 添加英文
4. 在组件中使用 `t('key')`
## 移动端适配示例
### 响应式布局
@@ -171,6 +267,29 @@ const MyComponent: React.FC = () => {
};
```
### 移动端导航
```typescript
import { Drawer } from 'antd';
const MobileNav: React.FC = () => {
const [open, setOpen] = useState(false);
return (
<>
<Button onClick={() => setOpen(true)}>菜单</Button>
<Drawer
title="导航"
placement="left"
onClose={() => setOpen(false)}
open={open}
>
{/* 导航内容 */}
</Drawer>
</>
);
};
```
### 响应式表格
```typescript
import { Table } from 'antd';
@@ -197,18 +316,63 @@ const ResponsiveTable: React.FC = () => {
### 代码质量
- ❌ 禁止使用 `any` 类型
- ❌ 禁止忽略错误处理
- ❌ 禁止硬编码 API 地址
- ❌ 禁止硬编码API地址
- ❌ 禁止在组件中直接使用 `fetch`
- ❌ 禁止忽略移动端适配
### 类型安全
- ❌ 禁止使用 `any`
- ❌ 禁止忽略 TypeScript 类型检查
- ❌ 禁止使用 `@ts-ignore` 除非有明确原因
### 移动端适配
- ❌ 禁止固定宽度布局
- ❌ 禁止使用过小的触摸目标(< 44x44px
- ❌ 禁止使用桌面端专用交互(如 hover
- ❌ 禁止忽略移动端性能优化
- ❌ 禁止使用桌面端专用的交互方式(如 hover)
### 多语言
- ❌ 禁止硬编码中文或英文文本
- ❌ 禁止绕过 `useTranslation` 直接显示文本
### 多语言使用规范
- **必须**使用多语言(i18n)进行所有文本显示
- **禁止**在组件中硬编码中文或英文文本
- **必须**使用 `useTranslation` Hook 获取翻译函数
- **必须**在语言文件中定义所有显示的文本
- **必须**支持至少中文(简体/繁体)和英文
#### 多语言使用示例
```typescript
// ✅ 正确:使用多语言
import { useTranslation } from 'react-i18next'
const MyComponent: React.FC = () => {
const { t } = useTranslation()
return (
<div>
<h1>{t('page.title')}</h1>
<Button>{t('common.save')}</Button>
</div>
)
}
// ❌ 错误:硬编码文本
const MyComponent: React.FC = () => {
return (
<div>
<h1>页面标题</h1> {/* 禁止硬编码 */}
<Button>保存</Button> {/* 禁止硬编码 */}
</div>
)
}
```
#### 语言文件结构
- 语言文件位置:`src/locales/{locale}/common.json`
- 支持的语言:`zh-CN`(简体中文)、`zh-TW`(繁体中文)、`en`(英文)
- 键名使用点号分隔的层级结构,如:`notificationSettings.title`
#### 添加新翻译的步骤
1. 在 `src/locales/zh-CN/common.json` 中添加中文翻译
2. 在 `src/locales/zh-TW/common.json` 中添加繁体中文翻译
3. 在 `src/locales/en/common.json` 中添加英文翻译
4. 在组件中使用 `t('key')` 获取翻译
+2 -2
View File
@@ -47,9 +47,9 @@ FROM eclipse-temurin:17-jre-jammy
WORKDIR /app
# 安装 Nginx 和必要的工具(包含时区数据)
# 安装 Nginx 和必要的工具
RUN apt-get update && \
apt-get install -y nginx curl tzdata && \
apt-get install -y nginx curl && \
rm -rf /var/lib/apt/lists/* && \
rm -rf /etc/nginx/sites-enabled/default
+1028
View File
File diff suppressed because it is too large Load Diff
-2
View File
@@ -2,7 +2,6 @@
[![GitHub](https://img.shields.io/badge/GitHub-WrBug%2FPolyHermes-blue?logo=github)](https://github.com/WrBug/PolyHermes)
[![Twitter](https://img.shields.io/badge/Twitter-@polyhermes-blue?logo=twitter)](https://x.com/polyhermes)
[![Docker](https://img.shields.io/docker/v/wrbug/polyhermes?label=Docker&logo=docker)](https://hub.docker.com/r/wrbug/polyhermes)
> 🌐 **Language**: [English](README_EN.md) | 中文
@@ -433,7 +432,6 @@ cd frontend
- [GitHub 仓库](https://github.com/WrBug/PolyHermes)
- [Twitter](https://x.com/polyhermes)
- [Telegram 群组](https://t.me/polyhermes)
- [Polymarket 官网](https://polymarket.com)
- [Polymarket API 文档](https://docs.polymarket.com)
-2
View File
@@ -2,7 +2,6 @@
[![GitHub](https://img.shields.io/badge/GitHub-WrBug%2FPolyHermes-blue?logo=github)](https://github.com/WrBug/PolyHermes)
[![Twitter](https://img.shields.io/badge/Twitter-@polyhermes-blue?logo=twitter)](https://x.com/polyhermes)
[![Docker](https://img.shields.io/docker/v/wrbug/polyhermes?label=Docker&logo=docker)](https://hub.docker.com/r/wrbug/polyhermes)
> 🌐 **Language**: English | [中文](README.md)
@@ -433,7 +432,6 @@ This project is licensed under the MIT License. See the [LICENSE](LICENSE) file
- [GitHub Repository](https://github.com/WrBug/PolyHermes)
- [Twitter](https://x.com/polyhermes)
- [Telegram Group](https://t.me/polyhermes)
- [Polymarket Official Website](https://polymarket.com)
- [Polymarket API Documentation](https://docs.polymarket.com)
+2 -695
View File
@@ -1,696 +1,3 @@
# v1.1.10
## 🚀 主要功能
### 📢 推送已过滤订单功能
- **新增推送已过滤订单功能(pushFilteredOrders**,默认关闭
- 支持在模板和跟单配置中配置是否推送被过滤的订单通知
- 开启后,当订单因过滤条件(价格区间、订单深度、价差、仓位限制等)被过滤时,会发送 Telegram 通知
- 帮助用户了解哪些订单被过滤以及过滤原因
- **数据库迁移**
- 添加 `push_filtered_orders` 字段到 `copy_trading_templates`
- 添加 `push_filtered_orders` 字段到 `copy_trading`
- 迁移脚本:`V24__add_push_filtered_orders_to_templates.sql`
- **后端实现**
- 在实体类(`CopyTradingTemplate``CopyTrading`)中添加 `pushFilteredOrders` 字段
- 在 DTO 中添加字段支持(创建、更新、查询)
- 在 Service 中处理字段的创建、更新和传递
- 在发送过滤订单通知时检查 `pushFilteredOrders` 字段,只有为 `true` 时才发送
- **前端实现**
- 在模板新增、编辑、推送页面添加"推送已过滤订单"开关
- 在跟单配置新增、编辑页面添加"推送已过滤订单"开关
- 从模板创建跟单配置时,自动填充 `pushFilteredOrders` 字段
- 支持多语言(中文、繁体中文、英文)
### 🔍 优化订单列表筛选功能
- **支持按市场标题搜索**
- 买入订单列表:添加市场标题筛选,移除方向筛选
- 卖出订单列表:添加市场标题筛选,将方向筛选改为状态筛选
- 已成交订单列表:添加市场标题筛选和市场列显示
- **改进分组体验**
- 记录用户的分组偏好到 localStorage,跨会话持久化
- 所有搜索输入框添加 0.5 秒防抖优化,提升性能
- **后端优化**
- `MarketGroupedOrdersRequest` 添加 `marketId``marketTitle` 字段
- 买入/卖出订单分组接口支持市场 ID 模糊匹配和市场标题关键字筛选
### ⚡ 优化跟单关系统计性能
- **移除未实现盈亏计算**
- 移除未实现盈亏和持仓价值的计算逻辑
- 总盈亏现在仅包含已实现盈亏,计算更准确
- 简化盈亏百分比计算,仅基于已实现盈亏
- **性能提升**
- 从 1+N 次网络请求减少到 0 次
- 删除不再使用的方法和依赖注入
- 统计查询速度显著提升
### 🐳 Docker 容器时区配置
- **支持通过 .env 自定义时区**:
-`docker-compose.yml``docker-compose.prod.yml` 中添加时区环境变量配置
- 支持通过 `TZ` 环境变量自定义容器时区
- 默认使用系统时区
## 🐛 Bug 修复
### 修复跟单配置更新时清空可选字段无法保存的问题
- **问题**:修改跟单配置时,清空价格区间、最大仓位数量、截止时间等可选字段后,无法保存到数据库
- **修复**
- 修复价格区间(`minPrice`/`maxPrice`)清空后无法保存的问题
- 修复最大仓位数量(`maxPositionCount`)清空后无法保存的问题
- 修复市场截止时间(`maxMarketEndDate`)清空后无法保存的问题
- 修复其他可选字段(`minOrderDepth`/`maxSpread`/`maxPositionValue`)清空后无法保存的问题
- **实现方案**
- 前端:清空字段时传空字符串或 `-1` 标记,让后端识别为清空操作
- 后端:处理空字符串和 `-1` 标记,正确设置为 `null` 以清空字段
### 修复新建和编辑页面的截止时间输入框交互问题
- **问题**:删除截止时间输入框内容后,失去焦点会自动填充 1
- **修复**
-`min``1` 改为 `0`,允许空值
- 优化 `onChange` 处理,当值为 `0``null``undefined` 时设置为 `undefined`(清空)
- 添加 `onBlur` 处理,确保失去焦点时如果值为空或 `0`,设置为 `undefined`
- 修改 `parser`,空值时返回空字符串而不是 `0`
### 修复订单通知重复发送和时间显示问题
- **修复并发导致的重复通知问题**:
-`OrderStatusUpdateService` 中实现双重检查机制
- 先保存订单标记为已发送,再重新查询数据库检查
- 防止定时任务并发时重复发送同一订单的通知
- 同时修复买入订单和卖出订单的通知逻辑
- **修复 Telegram 通知时间显示**
- `TelegramNotificationService.sendOrderSuccessNotification` 添加 `orderTime` 参数
- 使用订单的 `createdAt` 时间戳作为通知显示时间
- 而不是使用当前通知发送时间
- 更准确反映订单的实际创建时间
### 修复新建跟单配置时 pushFilteredOrders 字段未生效的问题
- **问题**:新建跟单配置时,即使设置了 `pushFilteredOrders: true`,也没有生效
- **修复**
- 修复手动输入模式下 `pushFilteredOrders` 被硬编码为 `false` 的问题
- 修复从模板填充表单时未加载 `pushFilteredOrders` 的问题
- 添加 `CopyTradingTemplate` 接口的 `pushFilteredOrders` 字段定义
## 📝 技术细节
### 数据库变更
- **迁移脚本**`V24__add_push_filtered_orders_to_templates.sql`
- **变更内容**
- `copy_trading_templates.push_filtered_orders`: BOOLEAN NOT NULL DEFAULT FALSE
- `copy_trading.push_filtered_orders`: BOOLEAN NOT NULL DEFAULT FALSE
- **自动执行**:升级时会自动执行迁移脚本
### API 变更
- **无新增接口**
- **无移除接口**
- **请求/响应变更**
- `CopyTradingCreateRequest` 添加 `pushFilteredOrders` 字段
- `CopyTradingUpdateRequest` 添加 `pushFilteredOrders` 字段
- `TemplateCreateRequest` 添加 `pushFilteredOrders` 字段
- `TemplateUpdateRequest` 添加 `pushFilteredOrders` 字段
- `MarketGroupedOrdersRequest` 添加 `marketId``marketTitle` 字段
### 前端变更
- **新增字段**
- `CopyTradingTemplate` 接口添加 `pushFilteredOrders` 字段
- **组件更新**
- 模板新增、编辑、推送页面添加"推送已过滤订单"开关
- 跟单配置新增、编辑页面添加"推送已过滤订单"开关
- 优化截止时间输入框交互逻辑
- **多语言支持**
- 添加中文、繁体中文、英文翻译
## 📊 变更统计
- **提交数量**6 个提交
- **文件变更**30 个文件
- **代码变更**+651 行 / -555 行(净增加 96 行)
### 详细文件变更
**后端变更**
- `CopyTrading.kt` - 添加 `pushFilteredOrders` 字段(+3 行)
- `CopyTradingTemplate.kt` - 添加 `pushFilteredOrders` 字段(+3 行)
- `CopyTradingDto.kt` - 添加 `pushFilteredOrders` 字段支持(+3 行)
- `CopyTradingTemplateDto.kt` - 添加 `pushFilteredOrders` 字段支持(+10 行)
- `CopyTradingService.kt` - 处理 `pushFilteredOrders` 字段和清空字段逻辑(+93 行)
- `CopyTradingTemplateService.kt` - 处理 `pushFilteredOrders` 字段(+8 行)
- `CopyOrderTrackingService.kt` - 检查 `pushFilteredOrders` 字段发送通知(+42 行)
- `CopyTradingStatisticsService.kt` - 优化统计性能(-328 行)
- `OrderStatusUpdateService.kt` - 修复重复通知问题(+142 行)
- `TelegramNotificationService.kt` - 添加订单时间参数(+17 行)
- `V24__add_push_filtered_orders_to_templates.sql` - 数据库迁移脚本(+13 行)
**前端变更**
- `AddModal.tsx` - 添加 `pushFilteredOrders` 字段和优化截止时间输入框(+40 行)
- `EditModal.tsx` - 添加 `pushFilteredOrders` 字段和优化截止时间输入框(+59 行)
- `TemplateAdd.tsx` - 添加 `pushFilteredOrders` 字段(+15 行)
- `TemplateEdit.tsx` - 添加 `pushFilteredOrders` 字段(+15 行)
- `TemplateList.tsx` - 添加 `pushFilteredOrders` 字段(+13 行)
- `BuyOrdersTab.tsx` - 优化筛选功能(+94 行)
- `SellOrdersTab.tsx` - 优化筛选功能(+77 行)
- `MatchedOrdersTab.tsx` - 优化筛选功能(+26 行)
- `types/index.ts` - 添加 `pushFilteredOrders` 字段定义(+6 行)
- `locales/*/common.json` - 添加多语言翻译(+31 行)
**配置文件变更**
- `docker-compose.yml` - 添加时区配置(+5 行)
- `docker-compose.prod.yml` - 添加时区配置(+5 行)
- `Dockerfile` - 优化构建配置(+4 行)
## 🔄 主要提交
```
9c303e0 feat: 添加推送已过滤订单功能并修复相关问题
cb8e469 fix: 修复跟单配置更新时清空可选字段无法保存的问题
279806d feat: 优化订单列表筛选功能,支持按市场标题搜索并改进分组体验
90fa487 refactor: 移除未实现盈亏计算以优化跟单关系统计性能
b58bb26 feat: 添加 Docker 容器时区配置,支持通过 .env 自定义
6af76c4 fix: 修复订单通知重复发送和时间显示问题
```
## 🎯 升级建议
1. **数据库迁移**:本次版本包含数据库迁移脚本,升级时会自动执行
- 自动添加 `push_filtered_orders` 字段到模板表和跟单配置表
- 现有数据不受影响,新字段默认值为 `false`
2. **配置更新**
- 可选:在 `.env` 文件中添加 `TZ` 环境变量自定义容器时区
- 无需其他配置变更
3. **兼容性**
- 完全向后兼容,不影响现有功能
- API 变更都是新增字段,不影响现有调用
## 📦 Docker 镜像
Docker 镜像会自动构建并推送到 Docker Hub:
- `wrbug/polyhermes:v1.1.10`
- `wrbug/polyhermes:latest`(如果这是最新版本)
## 🔗 相关链接
- [GitHub Release](https://github.com/WrBug/PolyHermes/releases/tag/v1.1.10)
- [完整更新日志](https://github.com/WrBug/PolyHermes/compare/v1.1.9...v1.1.10)
---
**发布日期**2026-01-12
---
# v1.1.9
## 🐛 Bug 修复
### 修复盈亏统计计算错误问题
- **问题**:在仓位检查时,刚创建的订单可能因仓位更新延迟被误判为已卖出,导致盈亏统计计算错误
- **修复**
- 优化自动卖出订单筛选逻辑,在 SQL 层直接过滤创建时间超过阈值的订单
- 新增 `findUnmatchedBuyOrdersByOutcomeIndexOlderThan` Repository 方法,提高查询效率
- 统一使用 2 分钟阈值保护刚创建的订单,避免误判
- 两个场景都使用 SQL 过滤:
* 场景1:仓位不存在时,延迟检测使用 SQL 过滤
* 场景2:仓位部分存在时,FIFO 匹配使用 SQL 过滤
- **优化效果**
- SQL 层面直接过滤,减少数据传输,提高查询效率
- 代码更简洁,逻辑更清晰
- 刚创建的订单(< 2 分钟)不会被误判为已卖出
- 确保盈亏统计计算的准确性
## 📝 技术细节
- **Repository 变更**
- 新增 `findUnmatchedBuyOrdersByOutcomeIndexOlderThan` 方法
- 在 SQL 查询中添加 `createdAt < :thresholdTime` 条件过滤
- **Service 变更**
- `PositionCheckService` 中两个场景都使用 SQL 过滤替代内存过滤
- 统一使用 2 分钟(120000 毫秒)作为时间阈值
## 📊 变更统计
- **提交数量**1 个提交
- **文件变更**2 个文件
- **代码变更**+57 行 / -19 行(净增加 38 行)
### 详细文件变更
**后端变更**
- `CopyOrderTrackingRepository.kt` - 新增 SQL 查询方法(+18 行)
- `PositionCheckService.kt` - 优化订单筛选逻辑(+39 行 / -19 行)
## 🔄 主要提交
```
6ad4024 fix: 优化自动卖出订单筛选逻辑,避免刚创建的订单被误判
```
## 🎯 升级建议
1. **数据库迁移**:无需数据库迁移,可直接升级
2. **配置更新**:无需配置变更
3. **兼容性**:完全向后兼容,不影响现有功能
## 📦 Docker 镜像
Docker 镜像会自动构建并推送到 Docker Hub:
- `wrbug/polyhermes:v1.1.9`
- `wrbug/polyhermes:latest`(如果这是最新版本)
## 🔗 相关链接
- [GitHub Release](https://github.com/WrBug/PolyHermes/releases/tag/v1.1.9)
---
# v1.1.8
## 🚀 主要功能
### ⚡ Polymarket Activity WebSocket 双重监听方案
- **新增 PolymarketActivityWsService**:通过 Activity WebSocket 实时监听 Leader 交易
- 延迟 < 100ms,适合快速跟单场景
- 订阅全局 activity 交易流,客户端过滤 Leader 地址
- 支持动态添加/移除 Leader 监听
- 地址筛选忽略大小写,提高匹配准确性
- **双重监听机制**
- Activity WebSocket(低延迟,< 100ms):作为主要监听方式
- On-Chain WebSocket(高可靠性,~2-3s):作为兜底监听方式
- 自动去重,避免重复处理同一笔交易
- **协议支持**
- 支持 `connection_id` 字段
- 优先使用消息中的 `outcomeIndex` 字段,fallback 到从 `outcome` 解析
- **参考文档**
- [双重监听方案文档](docs/zh/copy-trading-dual-monitoring-plan.md)
- [Activity WebSocket API 文档](docs/zh/polymarket-activity-websocket-api.md)
### 🔧 支持同一用户创建多个相同 Leader 的跟单配置
- **移除唯一约束**:允许同一用户创建多个跟单同一个 Leader 的配置
- **应用场景**:支持不同参数(比例、过滤条件等)的多配置跟单
- **数据库迁移**`V23__remove_unique_constraint_from_copy_trading.sql`
### 🎯 市场截止时间筛选功能
- **新增市场截止时间过滤**:支持设置跟单配置的最大市场截止时间
- **自动过滤过期市场**:系统会自动跳过超过设定截止时间的市场
- **数据库字段**`max_market_end_date`(时间戳,单位:秒)
- **数据库迁移**`V22__add_max_market_end_date_to_copy_trading.sql`
### 🔍 关键字过滤功能
- **新增关键字过滤**:支持在黑名单或白名单模式下过滤市场标题关键字
- **过滤模式**
- `DISABLED`:禁用关键字过滤
- `BLACKLIST`:黑名单模式(包含关键字的市场会被过滤)
- `WHITELIST`:白名单模式(只允许包含关键字的市场)
- **多关键字支持**:支持多个关键字,以 JSON 数组格式存储
- **数据库字段**`keyword_filter_mode``keywords`JSON 数组)
- **数据库迁移**`V20__add_keyword_filter.sql`
### 📊 订单列表功能重构
- **按市场分组显示**:订单列表按市场分组,便于查看和管理
- **市场信息展示**
- 显示市场标题和 slug
- 支持跳转到 Polymarket 市场页面
- 显示市场基本信息(标题、slug、截止时间等)
- **订单 ID 复制功能**:一键复制订单 ID,方便查询和调试
- **UI/UX 优化**
- 优化订单列表展示样式
- 改进移动端适配
### 📈 市场信息管理和缓存优化
- **新增 MarketService**:统一管理市场信息查询和缓存
- 使用 LRU 缓存提高查询性能
- 支持从 Gamma API 和 CLOB API 查询市场信息
- 自动缓存市场信息,减少 API 调用
- **新增 Market 实体和表**:持久化存储市场信息
- 存储市场标题、slug、eventSlug、endDate 等基本信息
- 支持通过 marketId 快速查询市场信息
- **数据库迁移**
- `V19__create_markets_table.sql`:创建市场信息表
- `V21__add_event_slug_to_markets.sql`:添加 eventSlug 字段
- **市场信息轮询服务**:定期更新市场信息,保持数据新鲜度
## 🐛 Bug 修复
### 前端 TypeScript 类型错误修复
- **修复 InputNumber parser 类型错误**`parser` 函数应返回 `number` 而不是 `string`
- **修复文件**
- `src/pages/CopyTradingOrders/AddModal.tsx`
- `src/pages/CopyTradingOrders/EditModal.tsx`
## 📝 文档更新
- **新增双重监听方案文档**:详细说明 Activity WebSocket 和 On-Chain WebSocket 的双重监听机制
- **新增 Activity WebSocket API 文档**:详细的 API 格式和消息结构说明
- **更新开发规范**JSON 解析规范和 Data Class 规范
## 📊 统计信息
- **61 个文件被修改**
- **+5125 行新增代码**
- **-1942 行删除代码**
- **净增加 3183 行代码**
## 🔄 主要提交
```
d376a82 feat: 添加市场信息管理和订单ID复制功能
2af2c0e feat: 订单列表按市场分组并支持跳转到Polymarket
9ed5190 feat: 添加关键字过滤功能并优化市场 slug 处理
0327eaf feat: 添加市场截止时间筛选功能
a16b6fc feat: 支持同一用户创建多个相同 leader 的跟单配置
19508dc feat: 实现 Polymarket Activity WebSocket 双重监听方案
```
## 🎯 升级建议
1. **数据库迁移**:确保执行所有数据库迁移脚本(V19-V23)
2. **配置更新**:新版本的 `application.properties` 中添加了 Activity WebSocket URL 配置
3. **环境变量**:如果使用 Docker 部署,建议更新 `docker-compose.yml` 中的配置
## 📦 Docker 镜像
Docker 镜像会自动构建并推送到 Docker Hub:
- `wrbug/polyhermes:v1.1.8`
- `wrbug/polyhermes:latest`(如果这是最新版本)
## 🔗 相关链接
- [GitHub Release](https://github.com/WrBug/PolyHermes/releases/tag/v1.1.8)
- [双重监听方案文档](docs/zh/copy-trading-dual-monitoring-plan.md)
- [Activity WebSocket API 文档](docs/zh/polymarket-activity-websocket-api.md)
---
# v1.1.7
## 🚀 主要功能
### 💰 Polymarket Maker Rebates Program 费率支持
- **新增费率查询 API 接口** (`getFeeRate`)
- 支持动态查询 Maker Rebates Program 费率
- 修正 API 返回字段名:使用 `base_fee` 而非 `fee_rate_bps`(与 TypeScript clob-client 一致)
- **动态费率获取**
- 在所有订单创建处动态获取费率:
* 跟单买入订单 (`processBuyTrade`)
* 跟单卖出订单 (`matchSellOrder`)
* 账户卖出订单 (`sellPosition`)
- 费率获取失败时降级到默认值 "0",确保系统可用性
- 添加详细的日志记录,便于监控和调试
- **参考文档**: https://docs.polymarket.com/developers/market-makers/maker-rebates-program
### 🔧 Docker 部署优化
- **日志级别环境变量支持**
-`application.properties` 中支持通过 `LOG_LEVEL_ROOT``LOG_LEVEL_APP` 环境变量配置日志级别
-`docker-compose.yml``docker-compose.prod.yml` 中添加日志级别环境变量配置
-`deploy.sh``.env` 模板中添加日志级别配置说明
- 支持通过环境变量动态配置日志级别,无需修改配置文件
- 默认值:`root=INFO`, `app=DEBUG`
## 🐛 Bug 修复
### 修复市场条件查询的 RPC 调用错误
- **问题**:使用错误的函数签名 `conditions(bytes32)` 导致 RPC 调用失败(execution reverted
- **修复**
- 将错误的 `conditions(bytes32)` 函数调用改为正确的 `getOutcomeSlotCount(bytes32)``payoutDenominator(bytes32)` 函数调用
- 修复 `BlockchainService.getCondition` 方法,使用正确的 ConditionalTokens 合约函数签名
- 改进 `MarketPriceService` 的错误处理:当链上查询出现 RPC 错误时,降级到 CLOB API 或 Gamma API 查询,而不是直接抛出异常,提高容错性
### 修复 RPC 错误时误创建自动卖出记录的问题
- **问题**:当链上查询市场条件出现 RPC 错误(execution reverted)时,系统会误判为市场已卖出,创建错误的自动卖出记录
- **修复**
- 修改 `getPriceFromChainCondition` 返回 `Pair<BigDecimal?, Boolean>`,第二个值表示是否发生 RPC 错误
-`getCurrentMarketPrice` 中检测到 RPC 错误时抛出异常,`PositionCheckService` 会捕获并跳过该市场的处理
- 避免在市场不存在或尚未创建时误判为已卖出
## 📝 文档更新
### 更新 Telegram 群链接
- 将所有 Telegram 群链接统一更新为 `t.me/polyhermes`
- 更新了以下文件:
- `frontend/src/components/Layout.tsx` - 桌面端和移动端导航链接
- `RELEASE.md` - 相关链接
- `README.md``README_EN.md` - 相关链接部分
### 添加 Docker 版本徽章
- 在 README 和 README_EN.md 中添加动态 Docker 版本徽章
- 使用 shields.io 自动显示 Docker Hub 上 `wrbug/polyhermes` 镜像的最新版本
- 版本信息自动更新,无需手动维护
## 📊 变更统计
- **提交数量**5 个提交
- **文件变更**16 个文件
- **代码变更**+205 行 / -886 行(净减少 681 行)
### 详细文件变更
**后端变更**
- `PolymarketClobApi.kt` - 添加费率查询接口(+25 行)
- `AccountService.kt` - 在订单创建处添加动态费率获取(+11 行)
- `BlockchainService.kt` - 修复市场条件查询的 RPC 调用错误(+84 行)
- `MarketPriceService.kt` - 改进错误处理,支持降级到其他数据源(+36 行)
- `PolymarketClobService.kt` - 添加费率查询服务(+32 行)
- `CopyOrderTrackingService.kt` - 在跟单订单创建处添加费率获取(+34 行)
- `PositionCheckService.kt` - 修复 RPC 错误处理逻辑(+2 行)
- `application.properties` - 添加日志级别环境变量支持(+6 行)
**前端变更**
- `Layout.tsx` - 更新 Telegram 群链接(+4 行)
**配置文件变更**
- `docker-compose.yml` - 添加日志级别环境变量(+4 行)
- `docker-compose.prod.yml` - 添加日志级别环境变量(+4 行)
- `deploy.sh` - 添加日志级别配置说明(+5 行)
**文档变更**
- `README.md` - 更新 Telegram 链接,添加 Docker 版本徽章(+2 行)
- `README_EN.md` - 更新 Telegram 链接,添加 Docker 版本徽章(+2 行)
- `RELEASE.md` - 更新 Telegram 链接(+4 行)
- `docs/zh/smart-money-analysis.md` - 删除文档(-836 行)
## 🔧 技术细节
### API 变更
- **新增接口**
- `POST /api/clob/fee-rate` - 获取 Maker Rebates Program 费率(内部使用)
- **无移除接口**
### 环境变量变更
- **新增环境变量**
- `LOG_LEVEL_ROOT` - Root 日志级别(默认:INFO
- `LOG_LEVEL_APP` - 应用日志级别(默认:DEBUG)
### 合约调用修复
- **修复的函数调用**
-`conditions(bytes32)` 改为 `getOutcomeSlotCount(bytes32)``payoutDenominator(bytes32)`
- 使用正确的 ConditionalTokens 合约函数签名
- 参考:https://polygonscan.com/address/0x4d97dcd97ec945f40cf65f87097ace5ea0476045#code
## 📝 升级说明
### 数据库升级
- **无需数据库迁移**:本次更新不涉及数据库结构变更
### 配置更新
- **可选配置**:新增日志级别环境变量,如不配置将使用默认值
- `LOG_LEVEL_ROOT=INFO`(默认)
- `LOG_LEVEL_APP=DEBUG`(默认)
### Docker 部署
- **推荐更新**:使用 Docker Hub 镜像部署的用户,建议更新到最新版本
```bash
docker pull wrbug/polyhermes:latest
docker-compose -f docker-compose.prod.yml up -d
```
## 🔗 相关链接
- **GitHub 仓库**https://github.com/WrBug/PolyHermes
- **Twitter**@polyhermes
- **Telegram 群组**https://t.me/polyhermes
---
**发布日期**2026-01-07
---
# v1.1.5
## 🔧 功能优化与改进
### 前端优化
#### 优化 InputNumber 输入框格式化
- 优化数值输入框的格式化逻辑,修正正则表达式以正确处理整数显示
- 更新所有相关 InputNumber 组件的 formatter 函数,确保显示准确性
- 影响的组件:CopyTradingAdd、CopyTradingEdit、EditModal、TemplateAdd、TemplateEdit、TemplateList
- 影响范围:跟单配置、模板配置中的所有数值输入框
#### 优化数字显示格式
- 添加 `formatNumber` 工具函数,自动去除小数尾随零(如 100.00 → 100
- 统一所有数值输入框的显示格式,提升用户体验
### 后端优化
#### 优化按比例跟单金额计算逻辑
- 优化按比例计算的订单金额处理,使用向上取整确保满足最小限制要求
- 对订单金额进行向上取整处理(保留 2 位小数精度)
- 自动调整订单数量以满足最小限制要求
- 使用 `RoundingMode.CEILING` 确保金额满足最小限制
- 影响范围:按比例跟单的订单创建逻辑
- 技术细节:
- 扩展 `BigDecimal.div()` 扩展函数,支持指定精度和舍入模式
- 在 `CopyOrderTrackingService` 中优化金额计算和验证逻辑
#### 增强 copyRatio 精度支持
- 将 copyRatio 字段精度从 DECIMAL(10,2) 增加到 DECIMAL(20,8)
- 支持更精确的跟单比例设置(最小 0.01%,最大 10000%
- 影响的实体:CopyTrading、CopyTradingTemplate
- 数据库迁移:新增 V18 迁移脚本,自动升级数据库字段精度
## 🔧 功能优化
### 移除刷新代理钱包接口
- **移除接口**
- `POST /api/accounts/refresh-proxy` - 刷新单个账户的代理地址
- `POST /api/accounts/refresh-all-proxies` - 刷新所有账户的代理地址
- **原因**:代理地址应在账户导入时自动计算,无需手动刷新
- **影响范围**AccountController、AccountService
- **向后兼容性**:这些接口已不再使用,移除不影响现有功能
### 前端跟单比例配置优化
- **最小比例**:从 10% 降低到 0.01%,支持更灵活的跟单比例设置
- **最大比例**:增加到 10000%,满足大比例跟单需求
- **显示格式**:比例模式显示为百分比(如 "100%" 而不是 "1x"
- **输入验证**:增强输入验证,确保比例在合理范围内
## 📊 变更统计
- **提交数量**3 个提交
- **文件变更**15 个文件
- **代码变更**+575 行 / -194 行(净增加 381 行)
### 详细文件变更
**后端变更**
- `AccountController.kt` - 移除刷新代理钱包接口(-59 行)
- `AccountService.kt` - 移除刷新代理钱包方法(-79 行)
- `CopyTrading.kt` - 增加 copyRatio 精度
- `CopyTradingTemplate.kt` - 增加 copyRatio 精度
- `CopyOrderTrackingService.kt` - 优化按比例跟单金额计算逻辑(+45 行)
- `MathExt.kt` - 扩展 div 函数支持精度和舍入模式(+20 行)
- `V18__increase_copy_ratio_precision.sql` - 数据库迁移脚本(+14 行)
**前端变更**
- `CopyTradingAdd.tsx` - 优化 formatter、优化比例配置(+106 行)
- `CopyTradingEdit.tsx` - 优化 formatter、优化比例配置(+106 行)
- `CopyTradingList.tsx` - 优化比例显示格式
- `CopyTradingOrders/EditModal.tsx` - 优化 formatter、优化比例配置(+106 行)
- `TemplateAdd.tsx` - 优化 formatter、优化比例配置(+65 行)
- `TemplateEdit.tsx` - 优化 formatter、优化比例配置(+65 行)
- `TemplateList.tsx` - 优化 formatter、优化比例配置(+65 行)
- `utils/index.ts` - 添加 formatNumber 工具函数(+31 行)
## 🔧 技术细节
### 数据库变更
- **迁移脚本**`V18__increase_copy_ratio_precision.sql`
- **变更内容**
- `copy_trading.copy_ratio`: DECIMAL(10,2) → DECIMAL(20,8)
- `copy_trading_templates.copy_ratio`: DECIMAL(10,2) → DECIMAL(20,8)
- **自动执行**:升级时会自动执行迁移脚本
### API 变更
- **移除接口**
- `POST /api/accounts/refresh-proxy`
- `POST /api/accounts/refresh-all-proxies`
- **无新增接口**
### 前端变更
- **工具函数**:新增 `formatNumber()` 函数,用于格式化数字显示
- **组件更新**:所有数值输入框统一使用新的 formatter 函数
- **显示优化**:跟单模式的比例显示为百分比格式
## 📝 升级说明
### 数据库升级
本次版本包含数据库迁移脚本,升级时会自动执行:
- 自动增加 `copy_ratio` 字段的精度
- 现有数据不受影响,精度升级是向后兼容的
### 配置变更
无需额外配置变更。
### 兼容性
- **向后兼容**:所有变更都是向后兼容的
- **API 兼容**:移除的接口不影响现有功能(这些接口已不再使用)
- **数据兼容**:数据库字段精度升级不会影响现有数据
## 🎯 主要改进
1. **优化输入框格式化**:优化数值输入框的显示逻辑
2. **优化跟单金额计算**:确保按比例跟单的金额满足最小限制要求
3. **提升精度支持**:支持更精确的跟单比例设置(0.01% - 10000%
4. **代码清理**:移除不再使用的刷新代理钱包接口
## 🔗 相关链接
- [GitHub Tag](https://github.com/WrBug/PolyHermes/releases/tag/v1.1.5)
- [变更日志](https://github.com/WrBug/PolyHermes/compare/v1.1.4...v1.1.5)
## 🙏 致谢
感谢所有贡献者和测试用户的反馈与支持!
---
# v1.1.2
## 🚀 主要功能
@@ -795,7 +102,7 @@ docker pull wrbug/polyhermes:v1.1.2
* **GitHub 仓库**https://github.com/WrBug/PolyHermes
* **Twitter**@polyhermes
* **Telegram 群组**https://t.me/polyhermes
* **Telegram 群组**加入群组
---
@@ -962,7 +269,7 @@ docker pull wrbug/polyhermes:v1.1.1
* **GitHub 仓库**https://github.com/WrBug/PolyHermes
* **Twitter**@polyhermes
* **Telegram 群组**https://t.me/polyhermes
* **Telegram 群组**加入群组
---
+1 -1
View File
@@ -130,7 +130,7 @@ export PROXY_PORT=8888
- 代理配置错误
**排查步骤**:
1. 检查 Polymarket RTDS WebSocket URL(现在使用代码常量 `PolymarketConstants.RTDS_WS_URL`
1. 检查 `polymarket.rtds.ws-url` 配置是否正确
2. 检查网络连接
3. 查看详细错误日志
-3
View File
@@ -68,9 +68,6 @@ dependencies {
// Logging
implementation("org.slf4j:slf4j-api")
// Caffeine Cache (LRU)
implementation("com.github.ben-manes.caffeine:caffeine:3.1.8")
// Test
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test")
-5
View File
@@ -9,7 +9,6 @@ services:
ports:
- "${SERVER_PORT:-8000}:8000"
environment:
- TZ=${TZ:-Asia/Shanghai}
- SPRING_PROFILES_ACTIVE=${SPRING_PROFILES_ACTIVE:-prod}
- DB_URL=${DB_URL:-jdbc:mysql://mysql:3306/polyhermes?useSSL=false&serverTimezone=UTC&characterEncoding=utf8&allowPublicKeyRetrieval=true}
- DB_USERNAME=${DB_USERNAME:-root}
@@ -17,8 +16,6 @@ services:
- SERVER_PORT=8000
- JWT_SECRET=${JWT_SECRET:-change-me-in-production}
- ADMIN_RESET_PASSWORD_KEY=${ADMIN_RESET_PASSWORD_KEY:-change-me-in-production}
volumes:
- /etc/localtime:/etc/localtime:ro
depends_on:
mysql:
condition: service_healthy
@@ -32,14 +29,12 @@ services:
ports:
- "${MYSQL_PORT:-3306}:3306"
environment:
- TZ=${TZ:-Asia/Shanghai}
- MYSQL_ROOT_PASSWORD=${DB_PASSWORD:-rootpassword}
- MYSQL_DATABASE=polyhermes
- MYSQL_CHARACTER_SET_SERVER=utf8mb4
- MYSQL_COLLATION_SERVER=utf8mb4_unicode_ci
volumes:
- mysql-data:/var/lib/mysql
- /etc/localtime:/etc/localtime:ro
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p${DB_PASSWORD:-rootpassword}"]
interval: 10s
@@ -0,0 +1,102 @@
package com.wrbug.polymarketbot.api
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Query
/**
* NBA Stats API 接口
* Base URL: https://stats.nba.com/stats/
*
* 注意:NBA Stats API 需要设置正确的请求头:
* - User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
* - Referer: https://www.nba.com/
* - Accept: application/json
*/
interface NbaStatsApi {
/**
* 获取赛程和比分
* @param GameDate 比赛日期,格式:YYYY-MM-DD,不传则获取今天的比赛
* @param LeagueID 联盟ID,默认:00 (NBA)
* @param DayOffset 日期偏移,默认:0
* @return ScoreboardResponse
*/
@GET("Scoreboard")
suspend fun getScoreboard(
@Query("GameDate") gameDate: String? = null,
@Query("LeagueID") leagueId: String = "00",
@Query("DayOffset") dayOffset: Int = 0
): Response<ScoreboardResponse>
}
/**
* NBA Stats API Scoreboard 响应
*/
data class ScoreboardResponse(
val resultSets: List<ResultSet>
)
/**
* Result Set
*/
data class ResultSet(
val name: String,
val headers: List<String>,
val rowSet: List<List<Any?>>
)
/**
* Game Header (从 Scoreboard 的 resultSets[0] 获取)
* Headers: ["GAME_DATE_EST", "GAME_SEQUENCE", "GAME_ID", "GAME_STATUS_ID", "GAME_STATUS_TEXT",
* "GAMECODE", "HOME_TEAM_ID", "VISITOR_TEAM_ID", "SEASON", "LIVE_PERIOD",
* "LIVE_PC_TIME", "NATL_TV_BROADCASTER_ABBREV", "LIVE_PERIOD_TIME_BCAST", "WH_STATUS"]
*/
data class GameHeader(
val gameDateEst: String,
val gameSequence: Int,
val gameId: String,
val gameStatusId: Int,
val gameStatusText: String,
val gameCode: String,
val homeTeamId: Int,
val visitorTeamId: Int,
val season: String,
val livePeriod: Int?,
val livePcTime: String?,
val natlTvBroadcasterAbbrev: String?,
val livePeriodTimeBcast: String?,
val whStatus: Int?
)
/**
* Line Score (从 Scoreboard 的 resultSets[1] 获取)
* Headers: ["GAME_DATE_EST", "GAME_SEQUENCE", "GAME_ID", "TEAM_ID", "TEAM_ABBREVIATION",
* "TEAM_NAME", "PTS_QTR1", "PTS_QTR2", "PTS_QTR3", "PTS_QTR4", "PTS_OT1",
* "PTS_OT2", "PTS_OT3", "PTS_OT4", "PTS", "FG_PCT", "FT_PCT", "FG3_PCT",
* "AST", "REB", "TOV"]
*/
data class LineScore(
val gameDateEst: String,
val gameSequence: Int,
val gameId: String,
val teamId: Int,
val teamAbbreviation: String,
val teamName: String,
val ptsQtr1: Int?,
val ptsQtr2: Int?,
val ptsQtr3: Int?,
val ptsQtr4: Int?,
val ptsOt1: Int?,
val ptsOt2: Int?,
val ptsOt3: Int?,
val ptsOt4: Int?,
val pts: Int,
val fgPct: Double?,
val ftPct: Double?,
val fg3Pct: Double?,
val ast: Int?,
val reb: Int?,
val tov: Int?
)
@@ -149,19 +149,6 @@ interface PolymarketClobApi {
@GET("/auth/derive-api-key")
suspend fun deriveApiKey(): Response<ApiKeyResponse>
/**
* 获取费率
* 文档: https://docs.polymarket.com/developers/market-makers/maker-rebates-program#1-fetch-the-fee-rate
* 端点: GET /fee-rate
*
* @param tokenId Token ID
* @return 费率响应
*/
@GET("/fee-rate")
suspend fun getFeeRate(
@Query("token_id") tokenId: String
): Response<FeeRateResponse>
/**
* 获取服务器时间
* 端点: /time
@@ -370,18 +357,6 @@ data class ServerTimeResponse(
val timestamp: Long
)
/**
* 费率响应
* 文档: https://docs.polymarket.com/developers/market-makers/maker-rebates-program#1-fetch-the-fee-rate
*
* 注意:根据 TypeScript clob-client 源码,API 返回的字段名是 base_fee,而不是文档中的 fee_rate_bps
* 参考: clob-client/src/client.ts:312
*/
data class FeeRateResponse(
@SerializedName("base_fee")
val baseFee: Int // 费率基点(0 表示无费率,1000 表示 10%)
)
/**
* 最新价响应(从订单表获取)
*/
@@ -13,36 +13,54 @@ import retrofit2.http.Query
interface PolymarketGammaApi {
/**
* 根据 condition ID 列表获取市场信息
* 获取体育元数据信息
* 文档: https://docs.polymarket.com/api-reference/sports/get-sports-metadata-information
* @return 体育元数据数组
*/
@GET("/sports")
suspend fun getSports(): Response<List<SportsMetadataResponse>>
/**
* 根据条件获取市场信息
* 文档: https://docs.polymarket.com/api-reference/markets/list-markets
* @param conditionIds condition ID 数组(16 进制字符串,如 "0x..."
* @param clobTokenIds CLOB token ID 数组(用于通过 tokenId 查询市场)
* @param includeTag 是否包含标签信息
* @param tags 标签 ID 数组,用于过滤市场(如 NBA 的 tag ID)
* @param active 是否只返回活跃的市场
* @param closed 是否包含已关闭的市场
* @param archived 是否包含已归档的市场
* @param limit 返回的市场数量限制
* @param startDateMin 最小开始日期(ISO 8601 格式,UTC 时区,如 "2025-12-01T00:00:00Z"
* @param sportsMarketTypes 体育市场类型数组(如 ["moneyline"] 用于筛选 moneyline 类型)
* @return 市场信息数组
*/
@GET("/markets")
suspend fun listMarkets(
@Query("condition_ids") conditionIds: List<String>? = null,
@Query("clob_token_ids") clobTokenIds: List<String>? = null,
@Query("include_tag") includeTag: Boolean? = null
@Query("include_tag") includeTag: Boolean? = null,
@Query("tags") tags: List<String>? = null,
@Query("active") active: Boolean? = null,
@Query("closed") closed: Boolean? = null,
@Query("archived") archived: Boolean? = null,
@Query("limit") limit: Int? = null,
@Query("start_date_min") startDateMin: String? = null,
@Query("sports_market_types") sportsMarketTypes: List<String>? = null,
): Response<List<MarketResponse>>
}
/**
* 事件响应(从 MarketResponse.events 解析)
* 体育元数据响应
* 文档: https://docs.polymarket.com/api-reference/sports/get-sports-metadata-information
*/
data class EventResponse(
val id: String? = null,
val ticker: String? = null,
val slug: String,
val title: String,
val category: String? = null,
val active: Boolean? = null,
val closed: Boolean? = null,
val archived: Boolean? = null,
val startDate: String? = null,
val endDate: String? = null,
val createdAt: String? = null
data class SportsMetadataResponse(
val sport: String? = null, // 体育标识符或缩写(如 "NBA"
val image: String? = null, // 体育 logo 或图片 URL
val resolution: String? = null, // 官方决议源 URL
val ordering: String? = null, // 显示顺序(通常是 "home" 或 "away"
val tags: String? = null, // 逗号分隔的标签 ID 列表
val series: String? = null // 系列标识符
)
/**
@@ -71,9 +89,22 @@ data class MarketResponse(
val lastTradePrice: Double? = null,
val bestBid: Double? = null,
val bestAsk: Double? = null,
val events: List<EventResponse>? = null, // 事件列表(从 events[0] 获取 slug
// 以下字段可能存在于响应中,但不在标准文档中
val clobTokenIds: String? = null, // CLOB token IDs(可能是 JSON 字符串或数组)
val clob_token_ids: String? = null // 下划线格式(兼容不同 API 版本)
val tags: List<MarketTag>? = null, // 市场标签列表
val sportsMarketType: String? = null, // 市场类型:moneyline, spread 等
val gameStartTime: String? = null, // 比赛开始时间(格式:2025-12-13 00:00:00+00
val createdAt: String? = null, // 市场创建时间(ISO 8601 格式)
val resolutionSource: String? = null // 决议源 URL(如 "https://www.nba.com/"
)
/**
* 市场标签
*/
data class MarketTag(
val id: String? = null,
val label: String? = null,
val slug: String? = null
)
@@ -2,8 +2,8 @@ package com.wrbug.polymarketbot.config
import com.google.gson.Gson
import com.wrbug.polymarketbot.api.PolymarketClobApi
import com.wrbug.polymarketbot.constants.PolymarketConstants
import com.wrbug.polymarketbot.util.createClient
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import retrofit2.Retrofit
@@ -23,6 +23,9 @@ class RetrofitConfig(
private val gson: Gson
) {
@Value("\${polymarket.clob.base-url}")
private lateinit var clobBaseUrl: String
/**
* 创建 CLOB API 客户端
* 用于跟单系统的订单操作和交易查询
@@ -35,7 +38,7 @@ class RetrofitConfig(
val okHttpClient = createClient().build()
return Retrofit.Builder()
.baseUrl(PolymarketConstants.CLOB_BASE_URL)
.baseUrl(clobBaseUrl)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
@@ -1,48 +0,0 @@
package com.wrbug.polymarketbot.constants
/**
* Polymarket API 常量
* 集中管理所有 Polymarket API 的 URL 配置
*/
object PolymarketConstants {
/**
* Polymarket CLOB API 基础 URL
*/
const val CLOB_BASE_URL = "https://clob.polymarket.com"
/**
* Polymarket RTDS WebSocket URL
* 用于订单推送服务
*/
const val RTDS_WS_URL = "wss://ws-subscriptions-clob.polymarket.com"
/**
* Polymarket User Channel WebSocket URL
* 用于跟单服务(订阅 Leader 交易)
*/
const val USER_WS_URL = "wss://ws-live-data.polymarket.com"
/**
* Polymarket Activity WebSocket URL
* 用于 Activity 全局交易流监听
*/
const val ACTIVITY_WS_URL = "wss://ws-live-data.polymarket.com"
/**
* Polymarket Data API 基础 URL
*/
const val DATA_API_BASE_URL = "https://data-api.polymarket.com"
/**
* Polymarket Gamma API 基础 URL
*/
const val GAMMA_BASE_URL = "https://gamma-api.polymarket.com"
/**
* Builder Relayer API URL
* 用于 Gasless 交易
*/
const val BUILDER_RELAYER_URL = "https://relayer-v2.polymarket.com/"
}
@@ -95,6 +95,65 @@ class AccountController(
}
}
/**
* 刷新账户的代理地址
* 使用最新的代理地址计算逻辑(支持 Magic 和 Safe 两种类型)
*/
@PostMapping("/refresh-proxy")
fun refreshProxyAddress(@RequestBody request: AccountDetailRequest): ResponseEntity<ApiResponse<AccountDto>> {
return try {
if (request.accountId == null || request.accountId <= 0) {
return ResponseEntity.ok(ApiResponse.error(ErrorCode.PARAM_ACCOUNT_ID_INVALID, messageSource = messageSource))
}
val result = accountService.refreshProxyAddress(request.accountId)
result.fold(
onSuccess = { account ->
ResponseEntity.ok(ApiResponse.success(account))
},
onFailure = { e ->
logger.error("刷新代理地址失败: ${e.message}", e)
when (e) {
is IllegalArgumentException -> ResponseEntity.ok(
ApiResponse.error(
ErrorCode.PARAM_ERROR,
e.message,
messageSource
)
)
else -> ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, e.message, messageSource))
}
}
)
} catch (e: Exception) {
logger.error("刷新代理地址异常: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, e.message, messageSource))
}
}
/**
* 批量刷新所有账户的代理地址
*/
@PostMapping("/refresh-all-proxies")
fun refreshAllProxyAddresses(): ResponseEntity<ApiResponse<List<AccountDto>>> {
return try {
val result = accountService.refreshAllProxyAddresses()
result.fold(
onSuccess = { accounts ->
ResponseEntity.ok(ApiResponse.success(accounts))
},
onFailure = { e ->
logger.error("批量刷新代理地址失败: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, e.message, messageSource))
}
)
} catch (e: Exception) {
logger.error("批量刷新代理地址异常: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, e.message, messageSource))
}
}
/**
* 删除账户
*/
@@ -200,51 +200,5 @@ class CopyOrderTrackingController(
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ORDER_TRACKING_LIST_FETCH_FAILED, e.message, messageSource))
}
}
/**
* 查询按市场分组的订单列表(买入/卖出)
* POST /api/copy-trading/orders/grouped-by-market
*/
@PostMapping("/grouped-by-market")
fun getOrderListGroupedByMarket(@RequestBody request: MarketGroupedOrdersRequest): ResponseEntity<ApiResponse<MarketGroupedOrdersResponse>> {
return try {
if (request.copyTradingId <= 0) {
return ResponseEntity.ok(ApiResponse.error(ErrorCode.PARAM_COPY_TRADING_ID_INVALID, messageSource = messageSource))
}
if (request.type.isBlank()) {
return ResponseEntity.ok(ApiResponse.error(ErrorCode.PARAM_EMPTY, "订单类型不能为空", messageSource))
}
val validTypes = listOf("buy", "sell")
if (!validTypes.contains(request.type.lowercase())) {
return ResponseEntity.ok(ApiResponse.error(ErrorCode.PARAM_ERROR, "订单类型必须是 buy 或 sell", messageSource))
}
val result: Result<MarketGroupedOrdersResponse> = runBlocking {
when (request.type.lowercase()) {
"buy" -> statisticsService.getBuyOrderListGroupedByMarket(request)
"sell" -> statisticsService.getSellOrderListGroupedByMarket(request)
else -> Result.failure(IllegalArgumentException("不支持的订单类型: ${request.type}"))
}
}
result.fold(
onSuccess = { response: MarketGroupedOrdersResponse ->
ResponseEntity.ok(ApiResponse.success(response))
},
onFailure = { e: Throwable ->
logger.error("查询按市场分组的订单列表失败: copyTradingId=${request.copyTradingId}, type=${request.type}", e)
when (e) {
is IllegalArgumentException -> ResponseEntity.ok(ApiResponse.error(ErrorCode.PARAM_ERROR, e.message, messageSource))
else -> ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ORDER_TRACKING_LIST_FETCH_FAILED, e.message, messageSource))
}
}
)
} catch (e: Exception) {
logger.error("查询按市场分组的订单列表异常: copyTradingId=${request.copyTradingId}, type=${request.type}", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ORDER_TRACKING_LIST_FETCH_FAILED, e.message, messageSource))
}
}
}
@@ -0,0 +1,69 @@
package com.wrbug.polymarketbot.controller.nba
import com.wrbug.polymarketbot.dto.*
import com.wrbug.polymarketbot.enums.ErrorCode
import com.wrbug.polymarketbot.service.nba.NbaGameService
import kotlinx.coroutines.runBlocking
import org.slf4j.LoggerFactory
import org.springframework.context.MessageSource
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
/**
* NBA 比赛控制器
*/
@RestController
@RequestMapping("/api/nba/games")
class NbaGameController(
private val nbaGameService: NbaGameService,
private val messageSource: MessageSource
) {
private val logger = LoggerFactory.getLogger(NbaGameController::class.java)
/**
* 获取 NBA 比赛列表
*/
@PostMapping("/list")
fun getNbaGames(@RequestBody request: NbaGameListRequest): ResponseEntity<ApiResponse<NbaGameListResponse>> {
return try {
val result = runBlocking {
nbaGameService.getNbaGames(request)
}
result.fold(
onSuccess = { ResponseEntity.ok(ApiResponse.success(it)) },
onFailure = { e ->
logger.error("获取 NBA 比赛列表失败: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, e.message, messageSource))
}
)
} catch (e: Exception) {
logger.error("获取 NBA 比赛列表异常: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, e.message, messageSource))
}
}
/**
* 获取 7 天内的所有球队(用于策略配置)
*/
@PostMapping("/teams")
fun getTeamsInNext7Days(): ResponseEntity<ApiResponse<List<String>>> {
return try {
val result = runBlocking {
nbaGameService.getTeamsInNext7Days()
}
result.fold(
onSuccess = { ResponseEntity.ok(ApiResponse.success(it)) },
onFailure = { e ->
logger.error("获取球队列表失败: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, e.message, messageSource))
}
)
} catch (e: Exception) {
logger.error("获取球队列表异常: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, e.message, messageSource))
}
}
}
@@ -0,0 +1,70 @@
package com.wrbug.polymarketbot.controller.nba
import com.wrbug.polymarketbot.dto.*
import com.wrbug.polymarketbot.enums.ErrorCode
import com.wrbug.polymarketbot.service.nba.NbaMarketService
import kotlinx.coroutines.runBlocking
import org.slf4j.LoggerFactory
import org.springframework.context.MessageSource
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
/**
* NBA 市场控制器
*/
@RestController
@RequestMapping("/api/nba/markets")
class NbaMarketController(
private val nbaMarketService: NbaMarketService,
private val messageSource: MessageSource
) {
private val logger = LoggerFactory.getLogger(NbaMarketController::class.java)
/**
* 获取 NBA 市场列表
*/
@PostMapping("/list")
fun getNbaMarkets(@RequestBody request: NbaMarketListRequest): ResponseEntity<ApiResponse<NbaMarketListResponse>> {
return try {
val result = runBlocking {
nbaMarketService.getNbaMarkets(request)
}
result.fold(
onSuccess = { ResponseEntity.ok(ApiResponse.success(it)) },
onFailure = { e ->
logger.error("获取 NBA 市场列表失败: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, e.message, messageSource))
}
)
} catch (e: Exception) {
logger.error("获取 NBA 市场列表异常: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, e.message, messageSource))
}
}
/**
* 从 NBA 市场中获取球队列表(用于策略配置)
* 从市场名称中解析出所有唯一的球队名称
*/
@PostMapping("/teams")
fun getTeamsFromMarkets(): ResponseEntity<ApiResponse<List<String>>> {
return try {
val result = runBlocking {
nbaMarketService.getTeamsFromMarkets(active = true)
}
result.fold(
onSuccess = { ResponseEntity.ok(ApiResponse.success(it)) },
onFailure = { e ->
logger.error("获取球队列表失败: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, e.message, messageSource))
}
)
} catch (e: Exception) {
logger.error("获取球队列表异常: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, e.message, messageSource))
}
}
}
@@ -0,0 +1,166 @@
package com.wrbug.polymarketbot.controller.nba
import com.wrbug.polymarketbot.dto.*
import com.wrbug.polymarketbot.enums.ErrorCode
import com.wrbug.polymarketbot.service.nba.NbaQuantitativeStrategyService
import kotlinx.coroutines.runBlocking
import org.slf4j.LoggerFactory
import org.springframework.context.MessageSource
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
/**
* NBA 量化策略控制器
*/
@RestController
@RequestMapping("/api/nba/strategies")
class NbaQuantitativeStrategyController(
private val strategyService: NbaQuantitativeStrategyService,
private val messageSource: MessageSource
) {
private val logger = LoggerFactory.getLogger(NbaQuantitativeStrategyController::class.java)
/**
* 创建策略
*/
@PostMapping("/create")
fun createStrategy(@RequestBody request: NbaQuantitativeStrategyCreateRequest): ResponseEntity<ApiResponse<NbaQuantitativeStrategyDto>> {
return try {
if (request.strategyName.isBlank()) {
return ResponseEntity.ok(ApiResponse.error(ErrorCode.PARAM_EMPTY, customMsg = "策略名称不能为空", messageSource = messageSource))
}
if (request.accountId <= 0) {
return ResponseEntity.ok(ApiResponse.error(ErrorCode.PARAM_ERROR, customMsg = "账户ID无效", messageSource = messageSource))
}
val result = runBlocking { strategyService.createStrategy(request) }
result.fold(
onSuccess = { strategy ->
ResponseEntity.ok(ApiResponse.success(strategy))
},
onFailure = { e ->
logger.error("创建策略失败: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, e.message, messageSource))
}
)
} catch (e: Exception) {
logger.error("创建策略异常: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, e.message, messageSource))
}
}
/**
* 更新策略
*/
@PostMapping("/update")
fun updateStrategy(@RequestBody request: NbaQuantitativeStrategyUpdateRequest): ResponseEntity<ApiResponse<NbaQuantitativeStrategyDto>> {
return try {
if (request.id <= 0) {
return ResponseEntity.ok(ApiResponse.error(ErrorCode.PARAM_ERROR, customMsg = "策略ID无效", messageSource = messageSource))
}
val result = runBlocking { strategyService.updateStrategy(request) }
result.fold(
onSuccess = { strategy ->
ResponseEntity.ok(ApiResponse.success(strategy))
},
onFailure = { e ->
logger.error("更新策略失败: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, e.message, messageSource))
}
)
} catch (e: Exception) {
logger.error("更新策略异常: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, e.message, messageSource))
}
}
/**
* 获取策略列表
*/
@PostMapping("/list")
fun getStrategyList(@RequestBody request: NbaQuantitativeStrategyListRequest): ResponseEntity<ApiResponse<NbaQuantitativeStrategyListResponse>> {
return try {
val result = runBlocking { strategyService.getStrategyList(request) }
result.fold(
onSuccess = { response ->
ResponseEntity.ok(ApiResponse.success(response))
},
onFailure = { e ->
logger.error("获取策略列表失败: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, e.message, messageSource))
}
)
} catch (e: Exception) {
logger.error("获取策略列表异常: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, e.message, messageSource))
}
}
/**
* 获取策略详情
*/
@PostMapping("/detail")
fun getStrategyDetail(@RequestBody request: NbaQuantitativeStrategyDetailRequest): ResponseEntity<ApiResponse<NbaQuantitativeStrategyDto>> {
return try {
if (request.id == null || request.id <= 0) {
return ResponseEntity.ok(ApiResponse.error(ErrorCode.PARAM_EMPTY, customMsg = "策略ID不能为空", messageSource = messageSource))
}
val result = runBlocking { strategyService.getStrategyDetail(request.id) }
result.fold(
onSuccess = { strategy ->
ResponseEntity.ok(ApiResponse.success(strategy))
},
onFailure = { e ->
logger.error("获取策略详情失败: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, e.message, messageSource))
}
)
} catch (e: Exception) {
logger.error("获取策略详情异常: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, e.message, messageSource))
}
}
/**
* 删除策略
*/
@PostMapping("/delete")
fun deleteStrategy(@RequestBody request: NbaQuantitativeStrategyDeleteRequest): ResponseEntity<ApiResponse<Unit>> {
return try {
if (request.id == null || request.id <= 0) {
return ResponseEntity.ok(ApiResponse.error(ErrorCode.PARAM_EMPTY, customMsg = "策略ID不能为空", messageSource = messageSource))
}
val result = runBlocking { strategyService.deleteStrategy(request.id) }
result.fold(
onSuccess = {
ResponseEntity.ok(ApiResponse.success(Unit))
},
onFailure = { e ->
logger.error("删除策略失败: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, e.message, messageSource))
}
)
} catch (e: Exception) {
logger.error("删除策略异常: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, e.message, messageSource))
}
}
}
/**
* 策略详情请求
*/
data class NbaQuantitativeStrategyDetailRequest(
val id: Long?
)
/**
* 策略删除请求
*/
data class NbaQuantitativeStrategyDeleteRequest(
val id: Long?
)
@@ -125,8 +125,7 @@ data class AccountPositionDto(
val proxyAddress: String,
val marketId: String,
val marketTitle: String?,
val marketSlug: String?, // 显示用的 slug
val eventSlug: String? = null, // 跳转用的 slug(从 events[0].slug 获取)
val marketSlug: String?,
val marketIcon: String?, // 市场图标 URL
val side: String, // 结果名称(如 "YES", "NO", "Pakistan" 等)
val outcomeIndex: Int? = null, // 结果索引(0, 1, 2...),用于计算 tokenId
@@ -1,64 +0,0 @@
package com.wrbug.polymarketbot.dto
import com.google.gson.annotations.SerializedName
/**
* Activity WebSocket 交易消息 DTO
* 根据 Polymarket RTDS Activity WebSocket API 格式定义
*/
data class ActivityTradeMessage(
val topic: String = "", // "activity"
val type: String = "", // "trades"
val timestamp: Long? = null, // 消息时间戳(可选)
@SerializedName("connection_id")
val connectionId: String? = null, // 连接 ID(可选,由服务器返回)
val payload: ActivityTradePayload = ActivityTradePayload() // 交易数据
)
/**
* Activity Trade Payload
*/
data class ActivityTradePayload(
val asset: String = "", // Token ID (用于下单)
@SerializedName("conditionId")
val conditionId: String = "", // Market condition ID
@SerializedName("eventSlug")
val eventSlug: String? = null, // 事件 slug
val slug: String? = null, // 市场 slug
val outcome: String? = null, // 结果方向 (Yes/No/Up/Down)
@SerializedName("outcomeIndex")
val outcomeIndex: Int? = null, // 结果索引 (0=Yes/Up, 1=No/Down) - 优先使用此字段
val side: String = "", // 交易方向 (BUY/SELL)
// price 和 size 可能是数字或字符串,使用 Any 类型,后续转换为 String
val price: Any? = null, // 交易价格
val size: Any? = null, // 交易数量 (shares)
val timestamp: Any? = null, // Unix 时间戳(可能是秒或毫秒,可能是数字或字符串)
@SerializedName("transactionHash")
val transactionHash: String? = null, // 交易哈希
val trader: ActivityTrader? = null, // 交易者信息对象(优先)
@SerializedName("proxyWallet")
val proxyWallet: String? = null, // 交易者地址(fallback,如果 trader 不存在)
val name: String? = null // 交易者名称(fallback,如果 trader 不存在)
)
/**
* Activity Trader 信息
*/
data class ActivityTrader(
val name: String? = null, // 交易者用户名(可选)
val address: String? = null // 交易者钱包地址 ⭐ 关键字段
)
@@ -37,14 +37,9 @@ data class CopyTradingCreateRequest(
// 最大仓位配置
val maxPositionValue: String? = null, // 最大仓位金额(USDC),NULL表示不启用
val maxPositionCount: Int? = null, // 最大仓位数量,NULL表示不启用
// 关键字过滤配置
val keywordFilterMode: String? = null, // 关键字过滤模式:DISABLED(不启用)、WHITELIST(白名单)、BLACKLIST(黑名单)
val keywords: List<String>? = null, // 关键字列表,当keywordFilterMode为DISABLED时为null
// 新增配置字段
val configName: String? = null, // 配置名(可选)
val pushFailedOrders: Boolean? = null, // 推送失败订单(可选)
val pushFilteredOrders: Boolean? = null, // 推送已过滤订单(可选)
val maxMarketEndDate: Long? = null // 市场截止时间限制(毫秒时间戳),仅跟单截止时间小于此时间的订单,NULL表示不启用
val pushFailedOrders: Boolean? = null // 推送失败订单(可选)
)
/**
@@ -76,14 +71,9 @@ data class CopyTradingUpdateRequest(
// 最大仓位配置
val maxPositionValue: String? = null, // 最大仓位金额(USDC),NULL表示不启用
val maxPositionCount: Int? = null, // 最大仓位数量,NULL表示不启用
// 关键字过滤配置
val keywordFilterMode: String? = null, // 关键字过滤模式:DISABLED(不启用)、WHITELIST(白名单)、BLACKLIST(黑名单)
val keywords: List<String>? = null, // 关键字列表,当keywordFilterMode为DISABLED时为null
// 新增配置字段
val configName: String? = null, // 配置名(可选,但提供时必须非空)
val pushFailedOrders: Boolean? = null, // 推送失败订单(可选)
val pushFilteredOrders: Boolean? = null, // 推送已过滤订单(可选)
val maxMarketEndDate: Long? = null // 市场截止时间限制(毫秒时间戳),仅跟单截止时间小于此时间的订单,NULL表示不启用
val pushFailedOrders: Boolean? = null // 推送失败订单(可选)
)
/**
@@ -152,14 +142,9 @@ data class CopyTradingDto(
// 最大仓位配置
val maxPositionValue: String? = null, // 最大仓位金额(USDC),NULL表示不启用
val maxPositionCount: Int? = null, // 最大仓位数量,NULL表示不启用
// 关键字过滤配置
val keywordFilterMode: String? = null, // 关键字过滤模式:DISABLED(不启用)、WHITELIST(白名单)、BLACKLIST(黑名单)
val keywords: List<String>? = null, // 关键字列表,当keywordFilterMode为DISABLED时为null
// 新增配置字段
val configName: String? = null, // 配置名(可选)
val pushFailedOrders: Boolean = false, // 推送失败订单(默认关闭)
val pushFilteredOrders: Boolean = false, // 推送已过滤订单(默认关闭)
val maxMarketEndDate: Long? = null, // 市场截止时间限制(毫秒时间戳),仅跟单截止时间小于此时间的订单,NULL表示不启用
val createdAt: Long,
val updatedAt: Long
)
@@ -40,10 +40,6 @@ data class BuyOrderInfo(
val orderId: String,
val leaderTradeId: String,
val marketId: String,
val marketTitle: String? = null, // 市场名称
val marketSlug: String? = null, // 市场 slug(用于显示)
val eventSlug: String? = null, // 跳转用的 slug(从 events[0].slug 获取)
val marketCategory: String? = null, // 市场分类(sports, crypto 等)
val side: String,
val quantity: String,
val price: String,
@@ -61,10 +57,6 @@ data class SellOrderInfo(
val orderId: String,
val leaderTradeId: String,
val marketId: String,
val marketTitle: String? = null, // 市场名称
val marketSlug: String? = null, // 市场 slug(用于显示)
val eventSlug: String? = null, // 跳转用的 slug(从 events[0].slug 获取)
val marketCategory: String? = null, // 市场分类(sports, crypto 等)
val side: String,
val quantity: String,
val price: String,
@@ -79,11 +71,6 @@ data class SellOrderInfo(
data class MatchedOrderInfo(
val sellOrderId: String,
val buyOrderId: String,
val marketId: String? = null, // 市场ID(从买入订单获取)
val marketTitle: String? = null, // 市场名称
val marketSlug: String? = null, // 市场 slug(用于显示)
val eventSlug: String? = null, // 跳转用的 slug(从 events[0].slug 获取)
val marketCategory: String? = null, // 市场分类(sports, crypto 等)
val matchedQuantity: String,
val buyPrice: String,
val sellPrice: String,
@@ -110,60 +97,12 @@ data class OrderTrackingRequest(
val page: Int? = 1,
val limit: Int? = 20,
val marketId: String? = null,
val marketTitle: String? = null, // 市场标题关键字筛选
val side: String? = null,
val status: String? = null,
val sellOrderId: String? = null,
val buyOrderId: String? = null
)
/**
* 按市场分组的订单查询请求
*/
data class MarketGroupedOrdersRequest(
val copyTradingId: Long,
val type: String, // buy, sell, matched
val page: Int? = 1,
val limit: Int? = 20,
val marketId: String? = null,
val marketTitle: String? = null
)
/**
* 单个市场的订单统计信息
*/
data class MarketOrderStats(
val count: Long,
val totalAmount: String, // 总金额
val totalPnl: String?, // 总盈亏(买入订单未实现盈亏,此字段为空)
val fullyMatched: Boolean, // 是否全部成交
val fullyMatchedCount: Long, // 完全成交的订单数
val partiallyMatchedCount: Long, // 部分成交的订单数
val filledCount: Long // 未成交的订单数
)
/**
* 单个市场分组的响应数据
*/
data class MarketOrderGroup(
val marketId: String,
val marketTitle: String?,
val marketSlug: String?, // 显示用的 slug
val eventSlug: String? = null, // 跳转用的 slug(从 events[0].slug 获取)
val marketCategory: String?,
val stats: MarketOrderStats,
val orders: List<Any> // BuyOrderInfo, SellOrderInfo 或 MatchedOrderInfo 的列表
)
/**
* 按市场分组的订单列表响应
*/
data class MarketGroupedOrdersResponse(
val list: List<MarketOrderGroup>,
val total: Long, // 市场总数
val page: Int,
val limit: Int
)
/**
* 统计查询请求
*/
@@ -23,8 +23,7 @@ data class TemplateCreateRequest(
val minOrderDepth: String? = null, // 最小订单深度(USDC金额),NULL表示不启用
val maxSpread: String? = null, // 最大价差(绝对价格),NULL表示不启用
val minPrice: String? = null, // 最低价格(可选),NULL表示不限制最低价
val maxPrice: String? = null, // 最高价格(可选),NULL表示不限制最高价
val pushFilteredOrders: Boolean? = null // 推送已过滤订单(默认关闭)
val maxPrice: String? = null // 最高价格(可选),NULL表示不限制最高价
)
/**
@@ -51,8 +50,7 @@ data class TemplateUpdateRequest(
val minOrderDepth: String? = null, // 最小订单深度(USDC金额),NULL表示不启用
val maxSpread: String? = null, // 最大价差(绝对价格),NULL表示不启用
val minPrice: String? = null, // 最低价格(可选),NULL表示不限制最低价
val maxPrice: String? = null, // 最高价格(可选),NULL表示不限制最高价
val pushFilteredOrders: Boolean? = null // 推送已过滤订单(默认关闭)
val maxPrice: String? = null // 最高价格(可选),NULL表示不限制最高价
)
/**
@@ -86,8 +84,7 @@ data class TemplateCopyRequest(
val minOrderDepth: String? = null, // 最小订单深度(USDC金额),NULL表示不启用
val maxSpread: String? = null, // 最大价差(绝对价格),NULL表示不启用
val minPrice: String? = null, // 最低价格(可选),NULL表示不限制最低价
val maxPrice: String? = null, // 最高价格(可选),NULL表示不限制最高价
val pushFilteredOrders: Boolean? = null // 推送已过滤订单(默认关闭)
val maxPrice: String? = null // 最高价格(可选),NULL表示不限制最高价
)
/**
@@ -122,7 +119,6 @@ data class TemplateDto(
val maxSpread: String?,
val minPrice: String?, // 最低价格(可选),NULL表示不限制最低价
val maxPrice: String?, // 最高价格(可选),NULL表示不限制最高价
val pushFilteredOrders: Boolean, // 推送已过滤订单(默认关闭)
val createdAt: Long,
val updatedAt: Long
)
@@ -25,8 +25,7 @@ data class MarketDto(
val liquidityNum: Double?,
val bestBid: Double?,
val bestAsk: Double?,
val lastTradePrice: Double?,
val events: List<MarketDto>? = null // 事件列表(从 events[0] 获取 slug
val lastTradePrice: Double?
)
/**
@@ -38,9 +37,9 @@ data class OutcomeDto(
)
/**
* 事件 DTO(用于其他 API 调用)
* 事件 DTO
*/
data class EventListDto(
data class EventDto(
val id: String,
val title: String,
val category: String,
@@ -56,7 +55,7 @@ data class SeriesDto(
val id: String,
val title: String,
val category: String,
val events: List<EventListDto>?,
val events: List<EventDto>?,
val createdAt: Long? // 时间戳(毫秒)
)
@@ -71,3 +70,4 @@ data class CommentDto(
val createdAt: Long, // 时间戳(毫秒)
val user: String?
)
@@ -0,0 +1,40 @@
package com.wrbug.polymarketbot.dto
import java.time.LocalDate
/**
* NBA 比赛 DTO
*/
data class NbaGameDto(
val id: Long?,
val nbaGameId: String?,
val homeTeam: String,
val awayTeam: String,
val gameDate: LocalDate,
val gameTime: Long?,
val gameStatus: String,
val homeScore: Int,
val awayScore: Int,
val period: Int,
val timeRemaining: String?,
val polymarketMarketId: String?
)
/**
* NBA 比赛列表响应
*/
data class NbaGameListResponse(
val list: List<NbaGameDto>,
val total: Long
)
/**
* NBA 比赛列表请求
* 前端传递时间戳(毫秒),后端转换为西8区时间
*/
data class NbaGameListRequest(
val startTimestamp: Long? = null, // 开始时间戳(毫秒)
val endTimestamp: Long? = null, // 结束时间戳(毫秒)
val gameStatus: String? = null
)
@@ -0,0 +1,45 @@
package com.wrbug.polymarketbot.dto
/**
* NBA 市场 DTO
*/
data class NbaMarketDto(
val id: String? = null,
val question: String? = null,
val conditionId: String? = null,
val slug: String? = null,
val description: String? = null,
val category: String? = null,
val active: Boolean? = null,
val closed: Boolean? = null,
val archived: Boolean? = null,
val volume: String? = null,
val liquidity: String? = null,
val endDate: String? = null,
val startDate: String? = null,
val outcomes: String? = null,
val outcomePrices: String? = null,
val volumeNum: Double? = null,
val liquidityNum: Double? = null,
val lastTradePrice: Double? = null,
val bestBid: Double? = null,
val bestAsk: Double? = null
)
/**
* NBA 市场列表响应
*/
data class NbaMarketListResponse(
val list: List<NbaMarketDto>,
val total: Long
)
/**
* NBA 市场列表请求
*/
data class NbaMarketListRequest(
val active: Boolean? = true,
val closed: Boolean? = false,
val archived: Boolean? = false
)
@@ -0,0 +1,213 @@
package com.wrbug.polymarketbot.dto
import java.math.BigDecimal
import java.time.LocalDate
/**
* NBA 量化策略 DTO
*/
data class NbaQuantitativeStrategyDto(
val id: Long?,
val strategyName: String,
val strategyDescription: String?,
val accountId: Long,
val accountName: String?,
val enabled: Boolean,
val filterTeams: List<String>?,
val filterDateFrom: LocalDate?,
val filterDateTo: LocalDate?,
val filterGameImportance: String?,
val minWinProbabilityDiff: BigDecimal,
val minWinProbability: BigDecimal?,
val maxWinProbability: BigDecimal?,
val minTradeValue: BigDecimal,
val minRemainingTime: Int?,
val maxRemainingTime: Int?,
val minScoreDiff: Int?,
val maxScoreDiff: Int?,
val buyAmountStrategy: String,
val fixedBuyAmount: BigDecimal?,
val buyRatio: BigDecimal?,
val baseBuyAmount: BigDecimal?,
val buyTiming: String,
val delayBuySeconds: Int,
val buyDirection: String,
val enableSell: Boolean,
val takeProfitThreshold: BigDecimal?,
val stopLossThreshold: BigDecimal?,
val probabilityReversalThreshold: BigDecimal?,
val sellRatio: BigDecimal,
val sellTiming: String,
val delaySellSeconds: Int,
val priceStrategy: String,
val fixedPrice: BigDecimal?,
val priceOffset: BigDecimal,
val maxPosition: BigDecimal,
val minPosition: BigDecimal,
val maxGamePosition: BigDecimal?,
val maxDailyLoss: BigDecimal?,
val maxDailyOrders: Int?,
val maxDailyProfit: BigDecimal?,
val priceTolerance: BigDecimal,
val minProbabilityThreshold: BigDecimal?,
val maxProbabilityThreshold: BigDecimal?,
val baseStrengthWeight: BigDecimal,
val recentFormWeight: BigDecimal,
val lineupIntegrityWeight: BigDecimal,
val starStatusWeight: BigDecimal,
val environmentWeight: BigDecimal,
val matchupAdvantageWeight: BigDecimal,
val scoreDiffWeight: BigDecimal,
val momentumWeight: BigDecimal,
val dataUpdateFrequency: Int,
val analysisFrequency: Int,
val pushFailedOrders: Boolean,
val pushFrequency: String,
val batchPushInterval: Int,
val createdAt: Long,
val updatedAt: Long
)
/**
* NBA 量化策略创建请求
*/
data class NbaQuantitativeStrategyCreateRequest(
val strategyName: String,
val strategyDescription: String? = null,
val accountId: Long,
val enabled: Boolean = true,
val filterTeams: List<String>? = null,
val filterDateFrom: LocalDate? = null,
val filterDateTo: LocalDate? = null,
val filterGameImportance: String? = null,
val minWinProbabilityDiff: BigDecimal? = null,
val minWinProbability: BigDecimal? = null,
val maxWinProbability: BigDecimal? = null,
val minTradeValue: BigDecimal? = null,
val minRemainingTime: Int? = null,
val maxRemainingTime: Int? = null,
val minScoreDiff: Int? = null,
val maxScoreDiff: Int? = null,
val buyAmountStrategy: String? = null,
val fixedBuyAmount: BigDecimal? = null,
val buyRatio: BigDecimal? = null,
val baseBuyAmount: BigDecimal? = null,
val buyTiming: String? = null,
val delayBuySeconds: Int? = null,
val buyDirection: String? = null,
val enableSell: Boolean? = null,
val takeProfitThreshold: BigDecimal? = null,
val stopLossThreshold: BigDecimal? = null,
val probabilityReversalThreshold: BigDecimal? = null,
val sellRatio: BigDecimal? = null,
val sellTiming: String? = null,
val delaySellSeconds: Int? = null,
val priceStrategy: String? = null,
val fixedPrice: BigDecimal? = null,
val priceOffset: BigDecimal? = null,
val maxPosition: BigDecimal? = null,
val minPosition: BigDecimal? = null,
val maxGamePosition: BigDecimal? = null,
val maxDailyLoss: BigDecimal? = null,
val maxDailyOrders: Int? = null,
val maxDailyProfit: BigDecimal? = null,
val priceTolerance: BigDecimal? = null,
val minProbabilityThreshold: BigDecimal? = null,
val maxProbabilityThreshold: BigDecimal? = null,
val baseStrengthWeight: BigDecimal? = null,
val recentFormWeight: BigDecimal? = null,
val lineupIntegrityWeight: BigDecimal? = null,
val starStatusWeight: BigDecimal? = null,
val environmentWeight: BigDecimal? = null,
val matchupAdvantageWeight: BigDecimal? = null,
val scoreDiffWeight: BigDecimal? = null,
val momentumWeight: BigDecimal? = null,
val dataUpdateFrequency: Int? = null,
val analysisFrequency: Int? = null,
val pushFailedOrders: Boolean? = null,
val pushFrequency: String? = null,
val batchPushInterval: Int? = null
)
/**
* NBA 量化策略更新请求
*/
data class NbaQuantitativeStrategyUpdateRequest(
val id: Long,
val strategyName: String? = null,
val strategyDescription: String? = null,
val enabled: Boolean? = null,
val filterTeams: List<String>? = null,
val filterDateFrom: LocalDate? = null,
val filterDateTo: LocalDate? = null,
val filterGameImportance: String? = null,
val minWinProbabilityDiff: BigDecimal? = null,
val minWinProbability: BigDecimal? = null,
val maxWinProbability: BigDecimal? = null,
val minTradeValue: BigDecimal? = null,
val minRemainingTime: Int? = null,
val maxRemainingTime: Int? = null,
val minScoreDiff: Int? = null,
val maxScoreDiff: Int? = null,
val buyAmountStrategy: String? = null,
val fixedBuyAmount: BigDecimal? = null,
val buyRatio: BigDecimal? = null,
val baseBuyAmount: BigDecimal? = null,
val buyTiming: String? = null,
val delayBuySeconds: Int? = null,
val buyDirection: String? = null,
val enableSell: Boolean? = null,
val takeProfitThreshold: BigDecimal? = null,
val stopLossThreshold: BigDecimal? = null,
val probabilityReversalThreshold: BigDecimal? = null,
val sellRatio: BigDecimal? = null,
val sellTiming: String? = null,
val delaySellSeconds: Int? = null,
val priceStrategy: String? = null,
val fixedPrice: BigDecimal? = null,
val priceOffset: BigDecimal? = null,
val maxPosition: BigDecimal? = null,
val minPosition: BigDecimal? = null,
val maxGamePosition: BigDecimal? = null,
val maxDailyLoss: BigDecimal? = null,
val maxDailyOrders: Int? = null,
val maxDailyProfit: BigDecimal? = null,
val priceTolerance: BigDecimal? = null,
val minProbabilityThreshold: BigDecimal? = null,
val maxProbabilityThreshold: BigDecimal? = null,
val baseStrengthWeight: BigDecimal? = null,
val recentFormWeight: BigDecimal? = null,
val lineupIntegrityWeight: BigDecimal? = null,
val starStatusWeight: BigDecimal? = null,
val environmentWeight: BigDecimal? = null,
val matchupAdvantageWeight: BigDecimal? = null,
val scoreDiffWeight: BigDecimal? = null,
val momentumWeight: BigDecimal? = null,
val dataUpdateFrequency: Int? = null,
val analysisFrequency: Int? = null,
val pushFailedOrders: Boolean? = null,
val pushFrequency: String? = null,
val batchPushInterval: Int? = null
)
/**
* NBA 量化策略列表请求
*/
data class NbaQuantitativeStrategyListRequest(
val accountId: Long? = null,
val enabled: Boolean? = null,
val strategyName: String? = null,
val page: Int? = 1,
val limit: Int? = 20
)
/**
* NBA 量化策略列表响应
*/
data class NbaQuantitativeStrategyListResponse(
val list: List<NbaQuantitativeStrategyDto>,
val total: Long,
val page: Int,
val limit: Int
)
@@ -58,10 +58,7 @@ data class CopyOrderTracking(
@Column(name = "notification_sent", nullable = false)
var notificationSent: Boolean = false, // 是否已发送通知(从订单详情获取实际数据后发送)
@Column(name = "source", nullable = false, length = 20)
val source: String, // 订单来源:activity-wsPolymarket WebSocket)、onchain-wsOnChain WebSocket
@Column(name = "created_at", nullable = false)
val createdAt: Long = System.currentTimeMillis(),
@@ -8,7 +8,12 @@ import com.wrbug.polymarketbot.util.toSafeBigDecimal
* 跟单配置实体(独立配置,不再绑定模板)
*/
@Entity
@Table(name = "copy_trading")
@Table(
name = "copy_trading",
uniqueConstraints = [
UniqueConstraint(columnNames = ["account_id", "leader_id"])
]
)
data class CopyTrading(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@@ -27,7 +32,7 @@ data class CopyTrading(
@Column(name = "copy_mode", nullable = false, length = 10)
val copyMode: String = "RATIO", // "RATIO" 或 "FIXED"
@Column(name = "copy_ratio", nullable = false, precision = 20, scale = 8)
@Column(name = "copy_ratio", nullable = false, precision = 10, scale = 2)
val copyRatio: BigDecimal = BigDecimal.ONE, // 仅在 copyMode="RATIO" 时生效
@Column(name = "fixed_amount", precision = 20, scale = 8)
@@ -86,13 +91,6 @@ data class CopyTrading(
@Column(name = "max_position_count")
val maxPositionCount: Int? = null, // 最大仓位数量,NULL表示不启用
// 关键字过滤配置
@Column(name = "keyword_filter_mode", nullable = false, length = 20)
val keywordFilterMode: String = "DISABLED", // 关键字过滤模式:DISABLED(不启用)、WHITELIST(白名单)、BLACKLIST(黑名单)
@Column(name = "keywords", columnDefinition = "JSON")
val keywords: String? = null, // 关键字列表(JSON数组),例如:["NBA", "足球", "NBA总决赛"],当keywordFilterMode为DISABLED时为NULL
// 新增配置字段
@Column(name = "config_name", length = 255)
val configName: String? = null, // 配置名(可选)
@@ -100,12 +98,6 @@ data class CopyTrading(
@Column(name = "push_failed_orders", nullable = false)
val pushFailedOrders: Boolean = false, // 推送失败订单(默认关闭)
@Column(name = "push_filtered_orders", nullable = false)
val pushFilteredOrders: Boolean = false, // 推送已过滤订单(默认关闭)
@Column(name = "max_market_end_date")
val maxMarketEndDate: Long? = null, // 市场截止时间限制(毫秒时间戳),仅跟单截止时间小于此时间的订单,NULL表示不启用
@Column(name = "created_at", nullable = false)
val createdAt: Long = System.currentTimeMillis(),
@@ -20,7 +20,7 @@ data class CopyTradingTemplate(
@Column(name = "copy_mode", nullable = false, length = 10)
val copyMode: String = "RATIO", // "RATIO" 或 "FIXED"
@Column(name = "copy_ratio", nullable = false, precision = 20, scale = 8)
@Column(name = "copy_ratio", nullable = false, precision = 10, scale = 2)
val copyRatio: BigDecimal = BigDecimal.ONE, // 仅在 copyMode="RATIO" 时生效
@Column(name = "fixed_amount", precision = 20, scale = 8)
@@ -72,9 +72,6 @@ data class CopyTradingTemplate(
@Column(name = "max_price", precision = 20, scale = 8)
val maxPrice: BigDecimal? = null, // 最高价格(可选),NULL表示不限制最高价
@Column(name = "push_filtered_orders", nullable = false)
val pushFilteredOrders: Boolean = false, // 推送已过滤订单(默认关闭)
@Column(name = "created_at", nullable = false)
val createdAt: Long = System.currentTimeMillis(),
@@ -1,60 +0,0 @@
package com.wrbug.polymarketbot.entity
import jakarta.persistence.*
/**
* 市场信息实体
* 用于缓存市场的基本信息(名称、slug等)
*/
@Entity
@Table(name = "markets", indexes = [
Index(name = "idx_market_id", columnList = "market_id", unique = true)
])
data class Market(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long? = null,
@Column(name = "market_id", unique = true, nullable = false, length = 100)
val marketId: String, // 市场IDcondition ID
@Column(name = "title", nullable = false, length = 500)
val title: String, // 市场名称(question
@Column(name = "slug", length = 200)
val slug: String? = null, // 市场slug(用于显示)
@Column(name = "event_slug", length = 200)
val eventSlug: String? = null, // 跳转用的 slug(从 events[0].slug 获取)
@Column(name = "category", length = 50)
val category: String? = null, // 市场分类
@Column(name = "icon", length = 500)
val icon: String? = null, // 市场图标URL
@Column(name = "image", length = 500)
val image: String? = null, // 市场图片URL
@Column(name = "description", columnDefinition = "TEXT")
val description: String? = null, // 市场描述
@Column(name = "active", nullable = false)
val active: Boolean = true, // 是否活跃
@Column(name = "closed", nullable = false)
val closed: Boolean = false, // 是否已关闭
@Column(name = "archived", nullable = false)
val archived: Boolean = false, // 是否已归档
@Column(name = "end_date")
val endDate: Long? = null, // 市场截止时间(毫秒时间戳)
@Column(name = "created_at", nullable = false)
val createdAt: Long = System.currentTimeMillis(),
@Column(name = "updated_at", nullable = false)
var updatedAt: Long = System.currentTimeMillis()
)
@@ -0,0 +1,55 @@
package com.wrbug.polymarketbot.entity
import jakarta.persistence.*
import java.time.LocalDate
/**
* NBA 比赛实体
*/
@Entity
@Table(name = "nba_games")
data class NbaGame(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long? = null,
@Column(name = "nba_game_id", unique = true, length = 100)
val nbaGameId: String? = null,
@Column(name = "home_team", nullable = false, length = 100)
val homeTeam: String,
@Column(name = "away_team", nullable = false, length = 100)
val awayTeam: String,
@Column(name = "game_date", nullable = false)
val gameDate: LocalDate,
@Column(name = "game_time")
val gameTime: Long? = null,
@Column(name = "game_status", length = 50)
val gameStatus: String = "scheduled",
@Column(name = "home_score")
val homeScore: Int = 0,
@Column(name = "away_score")
val awayScore: Int = 0,
@Column(name = "period")
val period: Int = 0,
@Column(name = "time_remaining", length = 50)
val timeRemaining: String? = null,
@Column(name = "polymarket_market_id", length = 100)
val polymarketMarketId: String? = null,
@Column(name = "created_at", nullable = false)
val createdAt: Long = System.currentTimeMillis(),
@Column(name = "updated_at", nullable = false)
var updatedAt: Long = System.currentTimeMillis()
)
@@ -0,0 +1,63 @@
package com.wrbug.polymarketbot.entity
import jakarta.persistence.*
/**
* NBA 市场实体(Polymarket 市场信息)
*/
@Entity
@Table(name = "nba_markets")
data class NbaMarket(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long? = null,
@Column(name = "polymarket_market_id", unique = true, nullable = false, length = 100)
val polymarketMarketId: String,
@Column(name = "condition_id", unique = true, nullable = false, length = 100)
val conditionId: String,
@Column(name = "market_slug", length = 255)
val marketSlug: String? = null,
@Column(name = "market_question", columnDefinition = "TEXT")
val marketQuestion: String? = null,
@Column(name = "market_description", columnDefinition = "TEXT")
val marketDescription: String? = null,
@Column(name = "category", length = 50)
val category: String = "sports",
@Column(name = "active")
val active: Boolean = true,
@Column(name = "closed")
val closed: Boolean = false,
@Column(name = "archived")
val archived: Boolean = false,
@Column(name = "volume", length = 50)
val volume: String? = null,
@Column(name = "liquidity", length = 50)
val liquidity: String? = null,
@Column(name = "outcomes", columnDefinition = "TEXT")
val outcomes: String? = null,
@Column(name = "end_date", length = 50)
val endDate: String? = null,
@Column(name = "start_date", length = 50)
val startDate: String? = null,
@Column(name = "created_at", nullable = false)
val createdAt: Long = System.currentTimeMillis(),
@Column(name = "updated_at", nullable = false)
var updatedAt: Long = System.currentTimeMillis()
)
@@ -0,0 +1,196 @@
package com.wrbug.polymarketbot.entity
import jakarta.persistence.*
import java.math.BigDecimal
import java.time.LocalDate
/**
* NBA 量化策略配置实体
*/
@Entity
@Table(name = "nba_quantitative_strategies")
data class NbaQuantitativeStrategy(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long? = null,
@Column(name = "strategy_name", nullable = false, length = 100)
val strategyName: String,
@Column(name = "strategy_description", columnDefinition = "TEXT")
val strategyDescription: String? = null,
@Column(name = "account_id", nullable = false)
val accountId: Long,
@Column(name = "enabled")
val enabled: Boolean = true,
// 比赛筛选参数
@Column(name = "filter_teams", columnDefinition = "TEXT")
val filterTeams: String? = null,
@Column(name = "filter_date_from")
val filterDateFrom: LocalDate? = null,
@Column(name = "filter_date_to")
val filterDateTo: LocalDate? = null,
@Column(name = "filter_game_importance", length = 50)
val filterGameImportance: String? = null,
// 触发条件参数
@Column(name = "min_win_probability_diff", precision = 5, scale = 4)
val minWinProbabilityDiff: BigDecimal = BigDecimal("0.1"),
@Column(name = "min_win_probability", precision = 5, scale = 4)
val minWinProbability: BigDecimal? = null,
@Column(name = "max_win_probability", precision = 5, scale = 4)
val maxWinProbability: BigDecimal? = null,
@Column(name = "min_trade_value", precision = 5, scale = 4)
val minTradeValue: BigDecimal = BigDecimal("0.05"),
@Column(name = "min_remaining_time")
val minRemainingTime: Int? = null,
@Column(name = "max_remaining_time")
val maxRemainingTime: Int? = null,
@Column(name = "min_score_diff")
val minScoreDiff: Int? = null,
@Column(name = "max_score_diff")
val maxScoreDiff: Int? = null,
// 买入规则参数
@Column(name = "buy_amount_strategy", length = 20)
val buyAmountStrategy: String = "FIXED",
@Column(name = "fixed_buy_amount", precision = 20, scale = 8)
val fixedBuyAmount: BigDecimal? = null,
@Column(name = "buy_ratio", precision = 5, scale = 4)
val buyRatio: BigDecimal? = null,
@Column(name = "base_buy_amount", precision = 20, scale = 8)
val baseBuyAmount: BigDecimal? = null,
@Column(name = "buy_timing", length = 20)
val buyTiming: String = "IMMEDIATE",
@Column(name = "delay_buy_seconds")
val delayBuySeconds: Int = 0,
@Column(name = "buy_direction", length = 10)
val buyDirection: String = "AUTO",
// 卖出规则参数
@Column(name = "enable_sell")
val enableSell: Boolean = true,
@Column(name = "take_profit_threshold", precision = 5, scale = 4)
val takeProfitThreshold: BigDecimal? = null,
@Column(name = "stop_loss_threshold", precision = 5, scale = 4)
val stopLossThreshold: BigDecimal? = null,
@Column(name = "probability_reversal_threshold", precision = 5, scale = 4)
val probabilityReversalThreshold: BigDecimal? = null,
@Column(name = "sell_ratio", precision = 5, scale = 4)
val sellRatio: BigDecimal = BigDecimal("1.0"),
@Column(name = "sell_timing", length = 20)
val sellTiming: String = "IMMEDIATE",
@Column(name = "delay_sell_seconds")
val delaySellSeconds: Int = 0,
// 价格策略参数
@Column(name = "price_strategy", length = 20)
val priceStrategy: String = "MARKET",
@Column(name = "fixed_price", precision = 5, scale = 4)
val fixedPrice: BigDecimal? = null,
@Column(name = "price_offset", precision = 5, scale = 4)
val priceOffset: BigDecimal = BigDecimal.ZERO,
// 风险控制参数
@Column(name = "max_position", precision = 20, scale = 8)
val maxPosition: BigDecimal = BigDecimal("50"),
@Column(name = "min_position", precision = 20, scale = 8)
val minPosition: BigDecimal = BigDecimal("5"),
@Column(name = "max_game_position", precision = 20, scale = 8)
val maxGamePosition: BigDecimal? = null,
@Column(name = "max_daily_loss", precision = 20, scale = 8)
val maxDailyLoss: BigDecimal? = null,
@Column(name = "max_daily_orders")
val maxDailyOrders: Int? = null,
@Column(name = "max_daily_profit", precision = 20, scale = 8)
val maxDailyProfit: BigDecimal? = null,
@Column(name = "price_tolerance", precision = 5, scale = 4)
val priceTolerance: BigDecimal = BigDecimal("0.05"),
@Column(name = "min_probability_threshold", precision = 5, scale = 4)
val minProbabilityThreshold: BigDecimal? = null,
@Column(name = "max_probability_threshold", precision = 5, scale = 4)
val maxProbabilityThreshold: BigDecimal? = null,
// 算法权重参数
@Column(name = "base_strength_weight", precision = 5, scale = 4)
val baseStrengthWeight: BigDecimal = BigDecimal("0.3"),
@Column(name = "recent_form_weight", precision = 5, scale = 4)
val recentFormWeight: BigDecimal = BigDecimal("0.25"),
@Column(name = "lineup_integrity_weight", precision = 5, scale = 4)
val lineupIntegrityWeight: BigDecimal = BigDecimal("0.2"),
@Column(name = "star_status_weight", precision = 5, scale = 4)
val starStatusWeight: BigDecimal = BigDecimal("0.15"),
@Column(name = "environment_weight", precision = 5, scale = 4)
val environmentWeight: BigDecimal = BigDecimal("0.1"),
@Column(name = "matchup_advantage_weight", precision = 5, scale = 4)
val matchupAdvantageWeight: BigDecimal = BigDecimal("0.2"),
@Column(name = "score_diff_weight", precision = 5, scale = 4)
val scoreDiffWeight: BigDecimal = BigDecimal("0.3"),
@Column(name = "momentum_weight", precision = 5, scale = 4)
val momentumWeight: BigDecimal = BigDecimal("0.2"),
// 系统配置参数
@Column(name = "data_update_frequency")
val dataUpdateFrequency: Int = 30,
@Column(name = "analysis_frequency")
val analysisFrequency: Int = 30,
@Column(name = "push_failed_orders")
val pushFailedOrders: Boolean = false,
@Column(name = "push_frequency", length = 20)
val pushFrequency: String = "REALTIME",
@Column(name = "batch_push_interval")
val batchPushInterval: Int = 1,
@Column(name = "created_at", nullable = false)
val createdAt: Long = System.currentTimeMillis(),
@Column(name = "updated_at", nullable = false)
var updatedAt: Long = System.currentTimeMillis()
)
@@ -0,0 +1,50 @@
package com.wrbug.polymarketbot.entity
import jakarta.persistence.*
import java.math.BigDecimal
import java.time.LocalDate
/**
* NBA 策略执行统计实体
*/
@Entity
@Table(name = "nba_strategy_statistics")
data class NbaStrategyStatistics(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long? = null,
@Column(name = "strategy_id", nullable = false)
val strategyId: Long,
@Column(name = "stat_date", nullable = false)
val statDate: LocalDate,
@Column(name = "total_signals")
val totalSignals: Int = 0,
@Column(name = "buy_signals")
val buySignals: Int = 0,
@Column(name = "sell_signals")
val sellSignals: Int = 0,
@Column(name = "success_signals")
val successSignals: Int = 0,
@Column(name = "failed_signals")
val failedSignals: Int = 0,
@Column(name = "total_profit", precision = 20, scale = 8)
val totalProfit: BigDecimal = BigDecimal.ZERO,
@Column(name = "total_volume", precision = 20, scale = 8)
val totalVolume: BigDecimal = BigDecimal.ZERO,
@Column(name = "created_at", nullable = false)
val createdAt: Long = System.currentTimeMillis(),
@Column(name = "updated_at", nullable = false)
var updatedAt: Long = System.currentTimeMillis()
)
@@ -0,0 +1,64 @@
package com.wrbug.polymarketbot.entity
import jakarta.persistence.*
import java.math.BigDecimal
/**
* NBA 交易信号实体
*/
@Entity
@Table(name = "nba_trading_signals")
data class NbaTradingSignal(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long? = null,
@Column(name = "strategy_id", nullable = false)
val strategyId: Long,
@Column(name = "game_id")
val gameId: Long? = null,
@Column(name = "market_id")
val marketId: Long? = null,
@Column(name = "signal_type", nullable = false, length = 10)
val signalType: String,
@Column(name = "direction", nullable = false, length = 10)
val direction: String,
@Column(name = "price", nullable = false, precision = 5, scale = 4)
val price: BigDecimal,
@Column(name = "quantity", nullable = false, precision = 20, scale = 8)
val quantity: BigDecimal,
@Column(name = "total_amount", nullable = false, precision = 20, scale = 8)
val totalAmount: BigDecimal,
@Column(name = "reason", columnDefinition = "TEXT")
val reason: String? = null,
@Column(name = "win_probability", precision = 5, scale = 4)
val winProbability: BigDecimal? = null,
@Column(name = "trade_value", precision = 5, scale = 4)
val tradeValue: BigDecimal? = null,
@Column(name = "signal_status", length = 20)
val signalStatus: String = "GENERATED",
@Column(name = "execution_result", columnDefinition = "TEXT")
val executionResult: String? = null,
@Column(name = "error_message", columnDefinition = "TEXT")
val errorMessage: String? = null,
@Column(name = "created_at", nullable = false)
val createdAt: Long = System.currentTimeMillis(),
@Column(name = "updated_at", nullable = false)
var updatedAt: Long = System.currentTimeMillis()
)
@@ -0,0 +1,66 @@
package com.wrbug.polymarketbot.enums
/**
* Polymarket 体育项目 Tag ID 枚举
* 用于标识不同体育项目在 Polymarket 中的 tag ID
*/
enum class SportsTagId(val tagId: String, val displayName: String) {
/**
* 美国职业篮球联赛
*/
NBA("745", "NBA"),
/**
* 美国职业棒球大联盟
*/
MLB("100381", "MLB"),
/**
* 美国国家橄榄球联盟
*/
NFL("450", "NFL"),
/**
* 美国大学橄榄球
*/
CFB("100351", "CFB"),
/**
* 美国国家冰球联盟
*/
NHL("899", "NHL"),
/**
* 游戏/电子竞技
*/
GAMES("100639", "GAMES"),
/**
* 美国大学篮球
*/
CBB("101178", "CBB");
companion object {
/**
* 根据 tag ID 查找枚举
*/
fun fromTagId(tagId: String): SportsTagId? {
return values().find { it.tagId == tagId }
}
/**
* 根据显示名称查找枚举
*/
fun fromDisplayName(displayName: String): SportsTagId? {
return values().find { it.displayName.equals(displayName, ignoreCase = true) }
}
/**
* 获取所有 tag IDs 列表
*/
fun getAllTagIds(): List<String> {
return values().map { it.tagId }
}
}
}
@@ -61,46 +61,5 @@ interface CopyOrderTrackingRepository : JpaRepository<CopyOrderTracking, Long> {
*/
@Query("SELECT t FROM CopyOrderTracking t WHERE t.createdAt <= :beforeTime")
fun findByCreatedAtBefore(beforeTime: Long): List<CopyOrderTracking>
/**
* 查询指定时间之前创建且状态不为指定状态的订单
*/
fun findByCreatedAtBeforeAndStatusNot(beforeTime: Long, status: String): List<CopyOrderTracking>
/**
* 查询指定跟单配置下的活跃仓位数量
* 活跃仓位定义为 remainingQuantity > 0 的不同 (marketId, outcomeIndex) 组合
*/
@Query("SELECT COUNT(DISTINCT CONCAT(t.marketId, '_', COALESCE(t.outcomeIndex, -1))) FROM CopyOrderTracking t WHERE t.copyTradingId = :copyTradingId AND t.remainingQuantity > 0")
fun countActivePositions(copyTradingId: Long): Int
/**
* 检查指定市场是否存在活跃仓位
*/
fun existsByCopyTradingIdAndMarketIdAndRemainingQuantityGreaterThan(copyTradingId: Long, marketId: String, remainingQuantity: BigDecimal): Boolean
/**
* 计算指定跟单配置和市场下的当前持仓总价值 (成本价计算)
*/
@Query("SELECT SUM(t.remainingQuantity * t.price) FROM CopyOrderTracking t WHERE t.copyTradingId = :copyTradingId AND t.marketId = :marketId AND t.remainingQuantity > 0")
fun sumCurrentPositionValueByMarket(copyTradingId: Long, marketId: String): BigDecimal?
/**
* 查询指定跟单配置下,创建时间超过指定时间点的未匹配订单(FIFO顺序)
* 用于避免刚创建的订单被误判为已卖出
*
* @param copyTradingId 跟单配置ID
* @param marketId 市场ID
* @param outcomeIndex 结果索引
* @param thresholdTime 时间阈值(毫秒时间戳),只查询创建时间小于该值的订单
* @return 未匹配订单列表(按创建时间升序排列)
*/
@Query("SELECT t FROM CopyOrderTracking t WHERE t.copyTradingId = :copyTradingId AND t.marketId = :marketId AND t.outcomeIndex = :outcomeIndex AND t.remainingQuantity > 0 AND t.createdAt < :thresholdTime ORDER BY t.createdAt ASC")
fun findUnmatchedBuyOrdersByOutcomeIndexOlderThan(
copyTradingId: Long,
marketId: String,
outcomeIndex: Int,
thresholdTime: Long
): List<CopyOrderTracking>
}
@@ -21,12 +21,12 @@ interface CopyTradingRepository : JpaRepository<CopyTrading, Long> {
fun findByLeaderId(leaderId: Long): List<CopyTrading>
/**
* 根据账户ID和Leader ID查找跟单列表
* 根据账户ID和Leader ID查找跟单
*/
fun findByAccountIdAndLeaderId(
accountId: Long,
leaderId: Long
): List<CopyTrading>
): CopyTrading?
/**
* 查找所有启用的跟单
@@ -1,12 +0,0 @@
package com.wrbug.polymarketbot.repository
import com.wrbug.polymarketbot.entity.Market
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
@Repository
interface MarketRepository : JpaRepository<Market, Long> {
fun findByMarketId(marketId: String): Market?
fun findByMarketIdIn(marketIds: List<String>): List<Market>
}
@@ -0,0 +1,27 @@
package com.wrbug.polymarketbot.repository
import com.wrbug.polymarketbot.entity.NbaGame
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
import java.time.LocalDate
@Repository
interface NbaGameRepository : JpaRepository<NbaGame, Long> {
fun findByNbaGameId(nbaGameId: String): NbaGame?
fun findByGameDate(gameDate: LocalDate): List<NbaGame>
fun findByGameDateBetween(startDate: LocalDate, endDate: LocalDate): List<NbaGame>
fun findByGameStatus(gameStatus: String): List<NbaGame>
fun findByHomeTeamAndAwayTeamAndGameDate(homeTeam: String, awayTeam: String, gameDate: LocalDate): NbaGame?
fun findByPolymarketMarketId(polymarketMarketId: String): NbaGame?
/**
* 查询最新的比赛(按创建时间倒序)
*/
fun findFirstByOrderByCreatedAtDesc(): NbaGame?
/**
* 根据创建时间查询比赛
*/
fun findByCreatedAtGreaterThan(createdAt: Long): List<NbaGame>
}
@@ -0,0 +1,15 @@
package com.wrbug.polymarketbot.repository
import com.wrbug.polymarketbot.entity.NbaMarket
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
@Repository
interface NbaMarketRepository : JpaRepository<NbaMarket, Long> {
fun findByConditionId(conditionId: String): NbaMarket?
fun findByPolymarketMarketId(polymarketMarketId: String): NbaMarket?
fun findByActiveAndClosed(active: Boolean, closed: Boolean): List<NbaMarket>
fun findByCategory(category: String): List<NbaMarket>
fun findByActive(active: Boolean): List<NbaMarket>
}
@@ -0,0 +1,14 @@
package com.wrbug.polymarketbot.repository
import com.wrbug.polymarketbot.entity.NbaQuantitativeStrategy
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
@Repository
interface NbaQuantitativeStrategyRepository : JpaRepository<NbaQuantitativeStrategy, Long> {
fun findByAccountId(accountId: Long): List<NbaQuantitativeStrategy>
fun findByAccountIdAndEnabled(accountId: Long, enabled: Boolean): List<NbaQuantitativeStrategy>
fun findByEnabled(enabled: Boolean): List<NbaQuantitativeStrategy>
fun findByStrategyName(strategyName: String): NbaQuantitativeStrategy?
}
@@ -0,0 +1,14 @@
package com.wrbug.polymarketbot.repository
import com.wrbug.polymarketbot.entity.NbaStrategyStatistics
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
import java.time.LocalDate
@Repository
interface NbaStrategyStatisticsRepository : JpaRepository<NbaStrategyStatistics, Long> {
fun findByStrategyId(strategyId: Long): List<NbaStrategyStatistics>
fun findByStrategyIdAndStatDate(strategyId: Long, statDate: LocalDate): NbaStrategyStatistics?
fun findByStrategyIdAndStatDateBetween(strategyId: Long, startDate: LocalDate, endDate: LocalDate): List<NbaStrategyStatistics>
}
@@ -0,0 +1,18 @@
package com.wrbug.polymarketbot.repository
import com.wrbug.polymarketbot.entity.NbaTradingSignal
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
import java.time.Instant
@Repository
interface NbaTradingSignalRepository : JpaRepository<NbaTradingSignal, Long> {
fun findByStrategyId(strategyId: Long): List<NbaTradingSignal>
fun findByGameId(gameId: Long): List<NbaTradingSignal>
fun findByMarketId(marketId: Long): List<NbaTradingSignal>
fun findBySignalType(signalType: String): List<NbaTradingSignal>
fun findBySignalStatus(signalStatus: String): List<NbaTradingSignal>
fun findByStrategyIdAndSignalType(strategyId: Long, signalType: String): List<NbaTradingSignal>
fun findByStrategyIdAndCreatedAtBetween(strategyId: Long, startTime: Long, endTime: Long): List<NbaTradingSignal>
}
@@ -8,10 +8,8 @@ import com.wrbug.polymarketbot.util.RetrofitFactory
import com.wrbug.polymarketbot.util.toSafeBigDecimal
import com.wrbug.polymarketbot.util.eq
import com.wrbug.polymarketbot.util.JsonUtils
import com.wrbug.polymarketbot.util.getEventSlug
import com.wrbug.polymarketbot.service.common.PolymarketClobService
import com.wrbug.polymarketbot.service.common.BlockchainService
import com.wrbug.polymarketbot.service.common.MarketService
import com.wrbug.polymarketbot.service.common.PolymarketApiKeyService
import com.wrbug.polymarketbot.service.copytrading.orders.OrderPushService
import com.wrbug.polymarketbot.service.copytrading.orders.OrderSigningService
@@ -38,7 +36,6 @@ class AccountService(
private val orderPushService: OrderPushService,
private val orderSigningService: OrderSigningService,
private val cryptoUtils: CryptoUtils,
private val marketService: MarketService, // 市场信息服务
private val telegramNotificationService: TelegramNotificationService? = null, // 可选,避免循环依赖
private val relayClientService: RelayClientService,
private val jsonUtils: JsonUtils
@@ -205,6 +202,85 @@ class AccountService(
}
}
/**
* 刷新账户的代理地址
* 使用最新的代理地址计算逻辑(支持 Magic 和 Safe 两种类型)
*/
@Transactional
fun refreshProxyAddress(accountId: Long): Result<AccountDto> {
return try {
val account = accountRepository.findById(accountId)
.orElse(null) ?: return Result.failure(IllegalArgumentException("账户不存在"))
// 重新获取代理地址(使用保存的钱包类型)
val proxyAddress = runBlocking {
val proxyResult = blockchainService.getProxyAddress(account.walletAddress, account.walletType)
if (proxyResult.isSuccess) {
proxyResult.getOrNull()
?: throw IllegalStateException("获取代理地址返回空值")
} else {
val error = proxyResult.exceptionOrNull()
throw IllegalStateException("获取代理地址失败: ${error?.message}")
}
}
// 更新账户
val updated = account.copy(
proxyAddress = proxyAddress,
updatedAt = System.currentTimeMillis()
)
val saved = accountRepository.save(updated)
logger.info("刷新代理地址成功: accountId=${accountId}, oldProxy=${account.proxyAddress}, newProxy=${proxyAddress}")
Result.success(toDto(saved))
} catch (e: Exception) {
logger.error("刷新代理地址失败: accountId=${accountId}", e)
Result.failure(e)
}
}
/**
* 刷新所有账户的代理地址
*/
@Transactional
fun refreshAllProxyAddresses(): Result<List<AccountDto>> {
return try {
val accounts = accountRepository.findAll()
val updatedAccounts = mutableListOf<AccountDto>()
accounts.forEach { account ->
try {
val proxyAddress = runBlocking {
val proxyResult = blockchainService.getProxyAddress(account.walletAddress, account.walletType)
if (proxyResult.isSuccess) {
proxyResult.getOrNull()
} else {
null
}
}
if (proxyAddress != null && proxyAddress != account.proxyAddress) {
val updated = account.copy(
proxyAddress = proxyAddress,
updatedAt = System.currentTimeMillis()
)
val saved = accountRepository.save(updated)
logger.info("刷新代理地址成功: accountId=${account.id}, oldProxy=${account.proxyAddress}, newProxy=${proxyAddress}")
updatedAccounts.add(toDto(saved))
}
} catch (e: Exception) {
logger.warn("刷新账户 ${account.id} 代理地址失败: ${e.message}")
}
}
logger.info("批量刷新代理地址完成: 更新了 ${updatedAccounts.size} 个账户")
Result.success(updatedAccounts)
} catch (e: Exception) {
logger.error("批量刷新代理地址失败", e)
Result.failure(e)
}
}
/**
* 删除账户
*/
@@ -231,12 +307,11 @@ class AccountService(
/**
* 查询账户列表
* 列表接口只返回基本信息,不查询统计信息(统计信息只在详情接口中查询)
*/
fun getAccountList(): Result<AccountListResponse> {
return try {
val accounts = accountRepository.findAllByOrderByCreatedAtAsc()
val accountDtos = accounts.map { toBasicDto(it) }
val accountDtos = accounts.map { toDto(it) }
Result.success(
AccountListResponse(
@@ -357,30 +432,7 @@ class AccountService(
}
/**
* 转换为基础 DTO(列表使用,不包含统计信息)
* 列表接口只返回基本信息,不查询统计信息,以提高性能
*/
private fun toBasicDto(account: Account): AccountDto {
return AccountDto(
id = account.id!!,
walletAddress = account.walletAddress,
proxyAddress = account.proxyAddress,
accountName = account.accountName,
isEnabled = account.isEnabled,
walletType = account.walletType,
apiKeyConfigured = account.apiKey != null,
apiSecretConfigured = account.apiSecret != null,
apiPassphraseConfigured = account.apiPassphrase != null,
totalOrders = null,
totalPnl = null,
activeOrders = null,
completedOrders = null,
positionCount = null
)
}
/**
* 转换为完整 DTO(详情使用,包含交易统计数据)
* 转换为 DTO
* 包含交易统计数据(总订单数、总盈亏、活跃订单数、已完成订单数、持仓数量)
*/
private fun toDto(account: Account): AccountDto {
@@ -659,8 +711,7 @@ class AccountService(
proxyAddress = account.proxyAddress,
marketId = pos.conditionId ?: "",
marketTitle = pos.title ?: "",
marketSlug = pos.slug ?: "", // 显示用的 slug
eventSlug = pos.eventSlug, // 跳转用的 slug(从 events[0].slug 获取)
marketSlug = pos.slug ?: "",
marketIcon = pos.icon, // 市场图标
side = pos.outcome ?: "",
outcomeIndex = pos.outcomeIndex, // 添加 outcomeIndex
@@ -867,15 +918,6 @@ class AccountService(
// 7. 解密私钥
val decryptedPrivateKey = decryptPrivateKey(account)
// 获取费率(根据 Polymarket Maker Rebates Program 要求)
val feeRateResult = clobService.getFeeRate(tokenId)
val feeRateBps = if (feeRateResult.isSuccess) {
feeRateResult.getOrNull()?.toString() ?: "0"
} else {
logger.warn("获取费率失败,使用默认值 0: tokenId=$tokenId, error=${feeRateResult.exceptionOrNull()?.message}")
"0"
}
// 11. 创建并签名订单(使用计算后的卖出数量)
val signedOrder = try {
orderSigningService.createAndSignOrder(
@@ -887,7 +929,7 @@ class AccountService(
size = sellQuantity.toPlainString(), // 使用计算后的卖出数量
signatureType = 2, // Browser Wallet(与正确订单数据一致)
nonce = "0",
feeRateBps = feeRateBps, // 使用动态获取的费率
feeRateBps = "0",
expiration = expiration
)
} catch (e: Exception) {
@@ -937,20 +979,31 @@ class AccountService(
notificationScope.launch {
try {
// 获取市场信息(标题和slug
val market = marketService.getMarket(request.marketId)
val marketTitle = market?.title ?: request.marketId
val marketSlug = market?.eventSlug // 跳转用的 slug
val marketInfo = withContext(Dispatchers.IO) {
try {
val gammaApi = retrofitFactory.createGammaApi()
val marketResponse = gammaApi.listMarkets(conditionIds = listOf(request.marketId))
if (marketResponse.isSuccessful && marketResponse.body() != null) {
marketResponse.body()!!.firstOrNull()
} else {
null
}
} catch (e: Exception) {
logger.warn("获取市场信息失败: ${e.message}", e)
null
}
}
val marketTitle = marketInfo?.question ?: request.marketId
val marketSlug = marketInfo?.slug
// 获取当前语言设置(从 LocaleContextHolder
val locale = try {
org.springframework.context.i18n.LocaleContextHolder.getLocale()
} catch (e: Exception) {
java.util.Locale("zh", "CN") // 默认简体中文
}
// 使用当前时间作为订单创建时间
val orderTime = System.currentTimeMillis()
telegramNotificationService?.sendOrderSuccessNotification(
orderId = orderId,
marketTitle = marketTitle,
@@ -966,8 +1019,7 @@ class AccountService(
apiSecret = try { cryptoUtils.decrypt(account.apiSecret!!) } catch (e: Exception) { null },
apiPassphrase = try { cryptoUtils.decrypt(account.apiPassphrase!!) } catch (e: Exception) { null },
walletAddressForApi = account.walletAddress,
locale = locale,
orderTime = orderTime // 使用订单创建时间
locale = locale
)
} catch (e: Exception) {
logger.warn("发送订单成功通知失败: ${e.message}", e)
@@ -995,17 +1047,31 @@ class AccountService(
notificationScope.launch {
try {
// 获取市场信息(标题和slug
val market = marketService.getMarket(request.marketId)
val marketTitle = market?.title ?: request.marketId
val marketSlug = market?.eventSlug // 跳转用的 slug
val marketInfo = withContext(Dispatchers.IO) {
try {
val gammaApi = retrofitFactory.createGammaApi()
val marketResponse = gammaApi.listMarkets(conditionIds = listOf(request.marketId))
if (marketResponse.isSuccessful && marketResponse.body() != null) {
marketResponse.body()!!.firstOrNull()
} else {
null
}
} catch (e: Exception) {
logger.warn("获取市场信息失败: ${e.message}", e)
null
}
}
val marketTitle = marketInfo?.question ?: request.marketId
val marketSlug = marketInfo?.slug
// 获取当前语言设置(从 LocaleContextHolder
val locale = try {
org.springframework.context.i18n.LocaleContextHolder.getLocale()
} catch (e: Exception) {
java.util.Locale("zh", "CN") // 默认简体中文
}
telegramNotificationService?.sendOrderFailureNotification(
marketTitle = marketTitle,
marketId = request.marketId,
@@ -1039,20 +1105,34 @@ class AccountService(
notificationScope.launch {
try {
// 获取市场信息(标题和slug
val market = marketService.getMarket(request.marketId)
val marketTitle = market?.title ?: request.marketId
val marketSlug = market?.eventSlug // 跳转用的 slug
val marketInfo = withContext(Dispatchers.IO) {
try {
val gammaApi = retrofitFactory.createGammaApi()
val marketResponse = gammaApi.listMarkets(conditionIds = listOf(request.marketId))
if (marketResponse.isSuccessful && marketResponse.body() != null) {
marketResponse.body()!!.firstOrNull()
} else {
null
}
} catch (e: Exception) {
logger.warn("获取市场信息失败: ${e.message}", e)
null
}
}
val marketTitle = marketInfo?.question ?: request.marketId
val marketSlug = marketInfo?.slug
// 获取当前语言设置(从 LocaleContextHolder
val locale = try {
org.springframework.context.i18n.LocaleContextHolder.getLocale()
} catch (e: Exception) {
java.util.Locale("zh", "CN") // 默认简体中文
}
// 只传递后端返回的 msg,不传递完整堆栈
val errorMsg = orderResponse.body()?.errorMsg ?: "创建订单失败"
telegramNotificationService?.sendOrderFailureNotification(
marketTitle = marketTitle,
marketId = request.marketId,
@@ -62,19 +62,6 @@ class PositionCheckService(
// 记录已发送提示的配置(避免重复推送)
private val notifiedConfigs = ConcurrentHashMap<Long, Long>() // accountId/copyTradingId -> lastNotificationTime
// 待检查的仓位记录(延迟检测机制)
// key: "accountId_marketId_outcomeIndex_copyTradingId"
// value: PendingPositionCheck(包含订单列表和首次检测时间)
private data class PendingPositionCheck(
val accountId: Long,
val marketId: String,
val outcomeIndex: Int,
val copyTradingId: Long,
val orders: List<CopyOrderTracking>,
val firstDetectedTime: Long // 首次检测到仓位不存在的时间
)
private val pendingPositionChecks = ConcurrentHashMap<String, PendingPositionCheck>()
// 同步锁,确保订阅任务的启动和停止是线程安全的
private val lock = Any()
@@ -86,7 +73,6 @@ class PositionCheckService(
logger.info("PositionCheckService 初始化,订阅仓位轮训事件")
startSubscription()
startCacheCleanup()
startPendingPositionCheckTask()
}
/**
@@ -146,121 +132,6 @@ class PositionCheckService(
}
}
/**
* 启动待检查仓位的定期检查任务
* 每30秒检查一次如果超过3分钟且确实不存在则标记为已卖出
*/
private fun startPendingPositionCheckTask() {
scope.launch(Dispatchers.IO) {
while (isActive) {
try {
delay(30000) // 每30秒检查一次
checkPendingPositions()
} catch (e: Exception) {
logger.error("检查待检查仓位异常: ${e.message}", e)
}
}
}
}
/**
* 检查待检查的仓位
* 如果超过3分钟且确实不存在则标记为已卖出
* 如果存在则删除记录
*/
private suspend fun checkPendingPositions() {
if (pendingPositionChecks.isEmpty()) {
return
}
try {
// 获取最新的仓位数据
val result = accountService.getAllPositions()
if (result.isFailure) {
logger.warn("获取仓位数据失败,跳过待检查仓位验证: ${result.exceptionOrNull()?.message}")
return
}
val positionListResponse = result.getOrNull() ?: return
val currentPositions = positionListResponse.currentPositions
// 按账户和市场分组当前仓位
val positionsByAccountAndMarket = currentPositions.groupBy {
"${it.accountId}_${it.marketId}_${it.outcomeIndex ?: 0}"
}
val now = System.currentTimeMillis()
val checkDelay = 180000L // 3分钟 = 180000毫秒
val toRemove = mutableListOf<String>()
val toMarkAsSold = mutableListOf<PendingPositionCheck>()
// 遍历所有待检查的仓位
for ((key, pendingCheck) in pendingPositionChecks) {
// 先过滤出仍然有效的订单(remainingQuantity > 0
val validOrders = pendingCheck.orders.filter { order ->
// 重新从数据库查询订单状态,确保数据是最新的
val currentOrder = copyOrderTrackingRepository.findById(order.id!!).orElse(null)
currentOrder != null && currentOrder.remainingQuantity > BigDecimal.ZERO
}
// 如果没有有效订单了,删除记录
if (validOrders.isEmpty()) {
toRemove.add(key)
logger.info("待检查仓位的订单已全部处理,删除记录: marketId=${pendingCheck.marketId}, outcomeIndex=${pendingCheck.outcomeIndex}, accountId=${pendingCheck.accountId}, copyTradingId=${pendingCheck.copyTradingId}")
continue
}
val positionKey = "${pendingCheck.accountId}_${pendingCheck.marketId}_${pendingCheck.outcomeIndex}"
val position = positionsByAccountAndMarket[positionKey]?.firstOrNull()
if (position != null) {
// 仓位存在,删除记录
toRemove.add(key)
logger.info("待检查仓位已恢复,删除记录: marketId=${pendingCheck.marketId}, outcomeIndex=${pendingCheck.outcomeIndex}, accountId=${pendingCheck.accountId}, copyTradingId=${pendingCheck.copyTradingId}, elapsedTime=${now - pendingCheck.firstDetectedTime}ms")
} else {
// 仓位不存在,检查是否超过3分钟
val elapsedTime = now - pendingCheck.firstDetectedTime
if (elapsedTime >= checkDelay) {
// 超过3分钟且确实不存在,标记为已卖出(使用有效订单)
toMarkAsSold.add(pendingCheck.copy(orders = validOrders))
toRemove.add(key)
logger.info("待检查仓位超过3分钟仍不存在,标记为已卖出: marketId=${pendingCheck.marketId}, outcomeIndex=${pendingCheck.outcomeIndex}, accountId=${pendingCheck.accountId}, copyTradingId=${pendingCheck.copyTradingId}, elapsedTime=${elapsedTime}ms, validOrderCount=${validOrders.size}, originalOrderCount=${pendingCheck.orders.size}")
} else {
// 未超过3分钟,更新订单列表(移除已处理的订单)
if (validOrders.size < pendingCheck.orders.size) {
pendingPositionChecks[key] = pendingCheck.copy(orders = validOrders)
logger.debug("更新待检查仓位记录,移除已处理的订单: marketId=${pendingCheck.marketId}, outcomeIndex=${pendingCheck.outcomeIndex}, validOrderCount=${validOrders.size}, originalOrderCount=${pendingCheck.orders.size}")
}
logger.debug("待检查仓位仍不存在,继续等待: marketId=${pendingCheck.marketId}, outcomeIndex=${pendingCheck.outcomeIndex}, accountId=${pendingCheck.accountId}, copyTradingId=${pendingCheck.copyTradingId}, elapsedTime=${elapsedTime}ms, remainingTime=${checkDelay - elapsedTime}ms")
}
}
}
// 删除已恢复或已处理的记录
toRemove.forEach { key ->
pendingPositionChecks.remove(key)
}
// 标记为已卖出
for (pendingCheck in toMarkAsSold) {
try {
val currentPrice = getCurrentMarketPrice(pendingCheck.marketId, pendingCheck.outcomeIndex)
updateOrdersAsSold(
pendingCheck.orders,
currentPrice,
pendingCheck.copyTradingId,
pendingCheck.marketId,
pendingCheck.outcomeIndex
)
} catch (e: Exception) {
logger.error("标记待检查仓位为已卖出失败: marketId=${pendingCheck.marketId}, outcomeIndex=${pendingCheck.outcomeIndex}, error=${e.message}", e)
}
}
} catch (e: Exception) {
logger.error("检查待检查仓位异常: ${e.message}", e)
}
}
/**
* 清理过期的缓存条目超过2小时的记录
*/
@@ -292,16 +163,8 @@ class PositionCheckService(
notifiedConfigs.remove(key)
}
// 清理过期的待检查仓位记录(超过1小时的记录,正常情况下应该在3分钟内处理完)
val expiredPendingChecks = pendingPositionChecks.entries.filter { (_, check) ->
(now - check.firstDetectedTime) > 3600000 // 1小时
}
expiredPendingChecks.forEach { (key, _) ->
pendingPositionChecks.remove(key)
}
if (expiredPositions.isNotEmpty() || expiredProcessed.isNotEmpty() || expiredConfigs.isNotEmpty() || expiredPendingChecks.isNotEmpty()) {
logger.debug("清理过期缓存: positions=${expiredPositions.size}, processed=${expiredProcessed.size}, configs=${expiredConfigs.size}, pendingChecks=${expiredPendingChecks.size}")
if (expiredPositions.isNotEmpty() || expiredProcessed.isNotEmpty() || expiredConfigs.isNotEmpty()) {
logger.debug("清理过期缓存: positions=${expiredPositions.size}, processed=${expiredProcessed.size}, configs=${expiredConfigs.size}")
}
}
@@ -508,100 +371,51 @@ class PositionCheckService(
val position = positionsByAccountAndMarket[positionKey]?.firstOrNull()
if (position == null) {
// 仓位不存在,使用延迟检测机制
// 先查询创建时间超过2分钟的未匹配订单(SQL层过滤,避免刚创建的订单被误判)
// 仓位不存在,检查订单创建时间
// 只有当订单创建时间超过2分钟时,才认为仓位被出售了
// 这样可以避免刚创建的订单因为API延迟而被误判为已卖出
val now = System.currentTimeMillis()
val thresholdTime = now - 120000 // 2分钟 = 120000毫秒
val ordersToCheck = copyOrderTrackingRepository.findUnmatchedBuyOrdersByOutcomeIndexOlderThan(
copyTradingId = copyTrading.id!!,
marketId = marketId,
outcomeIndex = outcomeIndex,
thresholdTime = thresholdTime
)
if (ordersToCheck.isNotEmpty()) {
// 有订单创建时间超过2分钟,记录到待检查列表
val checkKey = "${copyTrading.accountId}_${marketId}_${outcomeIndex}_${copyTrading.id}"
// 如果已经存在记录,更新订单列表(可能订单状态有变化)
val existingCheck = pendingPositionChecks[checkKey]
if (existingCheck == null) {
// 首次检测到,记录
pendingPositionChecks[checkKey] = PendingPositionCheck(
accountId = copyTrading.accountId,
marketId = marketId,
outcomeIndex = outcomeIndex,
copyTradingId = copyTrading.id!!,
orders = ordersToCheck,
firstDetectedTime = now
)
logger.info("首次检测到仓位不存在,记录待检查: marketId=$marketId, outcomeIndex=$outcomeIndex, accountId=${copyTrading.accountId}, copyTradingId=${copyTrading.id}, orderCount=${ordersToCheck.size}, positionKey=$positionKey")
} else {
// 已存在记录,更新订单列表(可能订单状态有变化)
pendingPositionChecks[checkKey] = existingCheck.copy(orders = ordersToCheck)
logger.debug("更新待检查仓位记录: marketId=$marketId, outcomeIndex=$outcomeIndex, accountId=${copyTrading.accountId}, copyTradingId=${copyTrading.id}, orderCount=${ordersToCheck.size}, elapsedTime=${now - existingCheck.firstDetectedTime}ms")
}
val ordersToMarkAsSold = orders.filter { order ->
val orderAge = now - order.createdAt
orderAge > 120000 // 2分钟 = 120000毫秒
}
if (ordersToMarkAsSold.isNotEmpty()) {
// 有订单创建时间超过2分钟,认为仓位已被出售
val currentPrice = getCurrentMarketPrice(marketId, outcomeIndex)
updateOrdersAsSold(ordersToMarkAsSold, currentPrice, copyTrading.id, marketId, outcomeIndex)
logger.debug("仓位不存在且订单创建时间超过2分钟,标记为已卖出: marketId=$marketId, outcomeIndex=$outcomeIndex, orderCount=${ordersToMarkAsSold.size}")
} else {
// 订单创建时间不足2分钟,可能是刚创建的订单,暂时不处理
logger.debug("仓位不存在但无符合条件的订单创建时间不足2分钟,暂不标记为已卖出: marketId=$marketId, outcomeIndex=$outcomeIndex, orderCount=${orders.size}, thresholdTime=$thresholdTime, positionKey=$positionKey")
logger.debug("仓位不存在但订单创建时间不足2分钟,暂不标记为已卖出: marketId=$marketId, outcomeIndex=$outcomeIndex, orderCount=${orders.size}, oldestOrderAge=${orders.minOfOrNull { now - it.createdAt }?.let { "${it}ms" } ?: "N/A"}")
}
} else {
// 有仓位,先检查是否有对应的待检查记录,如果有则删除(仓位已恢复)
val checkKey = "${copyTrading.accountId}_${marketId}_${outcomeIndex}_${copyTrading.id}"
val pendingCheck = pendingPositionChecks.remove(checkKey)
if (pendingCheck != null) {
logger.info("待检查仓位已恢复,删除待检查记录: marketId=$marketId, outcomeIndex=$outcomeIndex, accountId=${copyTrading.accountId}, copyTradingId=${copyTrading.id}, elapsedTime=${System.currentTimeMillis() - pendingCheck.firstDetectedTime}ms")
}
// 有仓位,按订单下单顺序(FIFO)更新状态
// 先查询创建时间超过2分钟的未匹配订单(SQL层过滤,避免刚创建的订单被误判)
val now = System.currentTimeMillis()
val thresholdTime = now - 120000 // 2分钟 = 120000毫秒
val validOrders = copyOrderTrackingRepository.findUnmatchedBuyOrdersByOutcomeIndexOlderThan(
copyTradingId = copyTrading.id!!,
marketId = marketId,
outcomeIndex = outcomeIndex,
thresholdTime = thresholdTime
)
// 如果没有符合条件的订单,跳过处理
if (validOrders.isEmpty()) {
logger.debug("仓位存在但无符合条件的订单(创建时间不足2分钟),暂不进行FIFO匹配: marketId=$marketId, outcomeIndex=$outcomeIndex, thresholdTime=$thresholdTime")
continue
}
// 计算逻辑:
// 1. 总订单数量 = 所有符合条件的未卖出订单的剩余数量总和
// 1. 总订单数量 = 所有未卖出订单的剩余数量总和
// 2. 已成交数量 = 总订单数量 - 仓位数量(因为还有仓位,说明部分订单已卖出)
// 3. 如果已成交数量 = 0,说明订单还没有卖出,不修改订单状态
// 4. 如果已成交数量 > 0,按FIFO顺序匹配订单
val positionQuantity = position.quantity.toSafeBigDecimal()
// 计算总订单数量(只计算符合条件的订单)
val totalOrderQuantity = validOrders.fold(BigDecimal.ZERO) { sum, order ->
// 计算总订单数量
val totalOrderQuantity = orders.fold(BigDecimal.ZERO) { sum, order ->
sum.add(order.remainingQuantity.toSafeBigDecimal())
}
// 计算已成交数量
val soldQuantity = totalOrderQuantity.subtract(positionQuantity)
// 如果已成交数量 <= 0,说明订单还没有卖出,不修改订单状态
if (soldQuantity <= BigDecimal.ZERO) {
logger.debug("仓位数量 >= 订单数量总和,订单尚未卖出: marketId=$marketId, outcomeIndex=$outcomeIndex, positionQuantity=$positionQuantity, totalOrderQuantity=$totalOrderQuantity")
continue
}
// 如果已成交数量 > 0,按FIFO顺序匹配订单(只匹配符合条件的订单)
try {
// 如果已成交数量 > 0,按FIFO顺序匹配订单
val currentPrice = getCurrentMarketPrice(marketId, outcomeIndex)
updateOrdersAsSoldByFIFO(validOrders, soldQuantity, currentPrice,
updateOrdersAsSoldByFIFO(orders, soldQuantity, currentPrice,
copyTrading.id, marketId, outcomeIndex)
} catch (e: Exception) {
logger.warn("无法获取市场价格,跳过FIFO匹配: marketId=$marketId, outcomeIndex=$outcomeIndex, error=${e.message}")
// 无法获取价格时,跳过该市场的处理,等待下次检查时再试
continue
}
}
}
}
@@ -21,29 +21,29 @@ import java.util.concurrent.CopyOnWriteArrayList
class PositionPollingService(
private val accountService: AccountService
) {
private val logger = LoggerFactory.getLogger(PositionPollingService::class.java)
@Value("\${position.polling.interval:2000}")
private var pollingInterval: Long = 2000 // 轮训间隔(毫秒),默认2秒
// 订阅者列表(支持多个订阅者)
private val subscribers = CopyOnWriteArrayList<(PositionListResponse) -> Unit>()
// 最新仓位数据(用于丢弃机制)
@Volatile
private var latestPositions: PositionListResponse? = null
// 协程作用域和任务
private val scope = CoroutineScope(Dispatchers.Default + SupervisorJob())
private var pollingJob: Job? = null
// 事件分发协程(使用专门的线程,避免阻塞轮训)
private val eventDispatcherScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
// 同步锁,确保轮询任务的启动和停止是线程安全的
private val lock = Any()
/**
* 初始化服务后端启动时直接启动轮训
*/
@@ -52,7 +52,7 @@ class PositionPollingService(
logger.info("PositionPollingService 初始化,启动仓位轮训任务,轮训间隔: ${pollingInterval}ms")
startPolling()
}
/**
* 清理资源
*/
@@ -66,7 +66,7 @@ class PositionPollingService(
scope.cancel()
eventDispatcherScope.cancel()
}
/**
* 订阅仓位事件
* @param callback 回调函数接收最新的仓位数据
@@ -78,7 +78,7 @@ class PositionPollingService(
latestPositions?.let { callback(it) }
}
}
/**
* 取消订阅仓位事件
*/
@@ -87,7 +87,7 @@ class PositionPollingService(
subscribers.remove(callback)
}
}
/**
* 启动轮训任务
*/
@@ -95,7 +95,7 @@ class PositionPollingService(
synchronized(lock) {
// 如果已经有轮训任务在运行,先取消
pollingJob?.cancel()
// 启动新的轮训任务
pollingJob = scope.launch {
while (isActive) {
@@ -109,7 +109,7 @@ class PositionPollingService(
}
}
}
/**
* 轮训仓位数据并发布事件
* 使用专门的线程分发事件避免阻塞轮训
@@ -123,7 +123,7 @@ class PositionPollingService(
if (positions != null) {
// 更新最新数据(丢弃旧数据,只保留最新的)
latestPositions = positions
// 在专门的线程中分发事件,避免阻塞轮训
eventDispatcherScope.launch {
try {
@@ -131,7 +131,7 @@ class PositionPollingService(
val currentSubscribers = synchronized(lock) {
subscribers.toList() // 复制列表,避免并发修改
}
currentSubscribers.forEach { callback ->
try {
callback(positions)
@@ -139,6 +139,8 @@ class PositionPollingService(
logger.error("通知订阅者失败: ${e.message}", e)
}
}
logger.debug("发布仓位数据事件: currentPositions=${positions.currentPositions.size}, historyPositions=${positions.historyPositions.size}, subscribers=${currentSubscribers.size}")
} catch (e: Exception) {
logger.error("分发仓位数据事件失败: ${e.message}", e)
}
@@ -7,11 +7,11 @@ import com.wrbug.polymarketbot.api.JsonRpcResponse
import com.wrbug.polymarketbot.api.PolymarketDataApi
import com.wrbug.polymarketbot.api.PositionResponse
import com.wrbug.polymarketbot.api.ValueResponse
import com.wrbug.polymarketbot.constants.PolymarketConstants
import com.wrbug.polymarketbot.util.EthereumUtils
import com.wrbug.polymarketbot.util.RetrofitFactory
import com.wrbug.polymarketbot.util.createClient
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import com.wrbug.polymarketbot.service.system.RelayClientService
import com.wrbug.polymarketbot.service.system.RpcNodeService
import org.springframework.stereotype.Service
@@ -26,6 +26,8 @@ import java.math.BigInteger
*/
@Service
class BlockchainService(
@Value("\${polymarket.data-api.base-url:https://data-api.polymarket.com}")
private val dataApiBaseUrl: String,
private val retrofitFactory: RetrofitFactory,
private val relayClientService: RelayClientService,
private val rpcNodeService: RpcNodeService,
@@ -59,10 +61,10 @@ class BlockchainService(
private val computeProxyAddressFunctionSignature = "computeProxyAddress(address)"
private val dataApi: PolymarketDataApi by lazy {
val baseUrl = if (PolymarketConstants.DATA_API_BASE_URL.endsWith("/")) {
PolymarketConstants.DATA_API_BASE_URL.dropLast(1)
val baseUrl = if (dataApiBaseUrl.endsWith("/")) {
dataApiBaseUrl.dropLast(1)
} else {
PolymarketConstants.DATA_API_BASE_URL
dataApiBaseUrl
}
val okHttpClient = createClient()
.followRedirects(true)
@@ -713,7 +715,7 @@ class BlockchainService(
/**
* 从链上查询市场条件Condition的结算结果
* 通过调用 ConditionalTokens 合约的 conditions mapping payoutNumerators mapping
* 通过调用 ConditionalTokens 合约的 getCondition 函数获取 payouts
*
* @param conditionId 市场条件IDbytes32必须是 0x 开头的 66 位十六进制字符串
* @return Result<Pair<payoutDenominator, payouts>>
@@ -732,131 +734,44 @@ class BlockchainService(
val rpcApi = polygonRpcApi
// 1. 调用 getOutcomeSlotCount(bytes32) 获取结果槽位数量
// 函数签名: getOutcomeSlotCount(bytes32) returns (uint)
val getOutcomeSlotCountSelector = EthereumUtils.getFunctionSelector("getOutcomeSlotCount(bytes32)")
// 构建 getCondition(bytes32) 函数调用
// 函数签名: getCondition(bytes32)
val functionSelector = EthereumUtils.getFunctionSelector("getCondition(bytes32)")
val encodedConditionId = EthereumUtils.encodeBytes32(conditionId)
val outcomeSlotCountData = getOutcomeSlotCountSelector + encodedConditionId
val data = functionSelector + encodedConditionId
val outcomeSlotCountRequest = JsonRpcRequest(
// 构建 JSON-RPC 请求
val rpcRequest = JsonRpcRequest(
method = "eth_call",
params = listOf(
mapOf(
"to" to conditionalTokensAddress,
"data" to outcomeSlotCountData
"data" to data
),
"latest"
)
)
val outcomeSlotCountResponse = rpcApi.call(outcomeSlotCountRequest)
// 发送 RPC 请求
val response = rpcApi.call(rpcRequest)
if (!outcomeSlotCountResponse.isSuccessful || outcomeSlotCountResponse.body() == null) {
return Result.failure(Exception("RPC 请求失败 (getOutcomeSlotCount): ${outcomeSlotCountResponse.code()} ${outcomeSlotCountResponse.message()}"))
if (!response.isSuccessful || response.body() == null) {
return Result.failure(Exception("RPC 请求失败: ${response.code()} ${response.message()}"))
}
val outcomeSlotCountRpcResponse = outcomeSlotCountResponse.body()!!
val rpcResponse = response.body()!!
if (outcomeSlotCountRpcResponse.error != null) {
val errorMsg = "RPC 错误 (code=${outcomeSlotCountRpcResponse.error.code}): ${outcomeSlotCountRpcResponse.error.message}, data=${outcomeSlotCountRpcResponse.error.data}"
logger.warn("查询市场条件(getOutcomeSlotCount)出现RPC错误: conditionId=$conditionId, $errorMsg")
logger.debug("RPC 请求详情: to=$conditionalTokensAddress, data=$outcomeSlotCountData")
return Result.failure(Exception(errorMsg))
// 检查错误
if (rpcResponse.error != null) {
return Result.failure(Exception("RPC 错误: ${rpcResponse.error.message}"))
}
val outcomeSlotCountHex = outcomeSlotCountRpcResponse.result?.asString
// 使用 Gson 解析 resultJsonElement
val hexResult = rpcResponse.result?.asString
?: return Result.failure(Exception("RPC 响应格式错误: result 为空"))
val outcomeSlotCount = EthereumUtils.decodeUint256(outcomeSlotCountHex).toInt()
// 如果 outcomeSlotCount 为 0,说明市场尚未创建或不存在
if (outcomeSlotCount <= 0) {
logger.debug("市场尚未创建或不存在: conditionId=$conditionId, outcomeSlotCount=$outcomeSlotCount")
return Result.success(Pair(BigInteger.ZERO, emptyList()))
}
// 2. 调用 payoutDenominator(bytes32) 获取分母
// 函数签名: payoutDenominator(bytes32) returns (uint)
val payoutDenominatorSelector = EthereumUtils.getFunctionSelector("payoutDenominator(bytes32)")
val payoutDenominatorData = payoutDenominatorSelector + encodedConditionId
val payoutDenominatorRequest = JsonRpcRequest(
method = "eth_call",
params = listOf(
mapOf(
"to" to conditionalTokensAddress,
"data" to payoutDenominatorData
),
"latest"
)
)
val payoutDenominatorResponse = rpcApi.call(payoutDenominatorRequest)
if (!payoutDenominatorResponse.isSuccessful || payoutDenominatorResponse.body() == null) {
return Result.failure(Exception("RPC 请求失败 (payoutDenominator): ${payoutDenominatorResponse.code()} ${payoutDenominatorResponse.message()}"))
}
val payoutDenominatorRpcResponse = payoutDenominatorResponse.body()!!
if (payoutDenominatorRpcResponse.error != null) {
val errorMsg = "RPC 错误 (code=${payoutDenominatorRpcResponse.error.code}): ${payoutDenominatorRpcResponse.error.message}, data=${payoutDenominatorRpcResponse.error.data}"
logger.warn("查询市场条件(payoutDenominator)出现RPC错误: conditionId=$conditionId, $errorMsg")
logger.debug("RPC 请求详情: to=$conditionalTokensAddress, data=$payoutDenominatorData")
return Result.failure(Exception(errorMsg))
}
val payoutDenominatorHex = payoutDenominatorRpcResponse.result?.asString
?: return Result.failure(Exception("RPC 响应格式错误: result 为空"))
val payoutDenominator = EthereumUtils.decodeUint256(payoutDenominatorHex)
// 如果 outcomeSlotCount 为 0,说明市场尚未创建或不存在
if (outcomeSlotCount <= 0) {
logger.debug("市场尚未创建或不存在: conditionId=$conditionId, outcomeSlotCount=$outcomeSlotCount")
return Result.success(Pair(BigInteger.ZERO, emptyList()))
}
// 如果 payoutDenominator 为 0,说明市场尚未结算
if (payoutDenominator == BigInteger.ZERO) {
logger.debug("市场尚未结算: conditionId=$conditionId, payoutDenominator=$payoutDenominator")
return Result.success(Pair(BigInteger.ZERO, emptyList()))
}
// 2. 查询每个 outcome 的 payoutNumerators
val payouts = mutableListOf<BigInteger>()
for (i in 0 until outcomeSlotCount) {
val payoutNumeratorsFunctionSelector = EthereumUtils.getFunctionSelector("payoutNumerators(bytes32,uint256)")
val encodedIndex = EthereumUtils.encodeUint256(BigInteger.valueOf(i.toLong()))
val payoutNumeratorsData = payoutNumeratorsFunctionSelector + encodedConditionId + encodedIndex
val payoutRequest = JsonRpcRequest(
method = "eth_call",
params = listOf(
mapOf(
"to" to conditionalTokensAddress,
"data" to payoutNumeratorsData
),
"latest"
)
)
val payoutResponse = rpcApi.call(payoutRequest)
if (!payoutResponse.isSuccessful || payoutResponse.body() == null) {
logger.warn("查询 payoutNumerators 失败: index=$i")
continue
}
val payoutRpcResponse = payoutResponse.body()!!
if (payoutRpcResponse.error != null) {
logger.warn("查询 payoutNumerators 错误: index=$i, error=${payoutRpcResponse.error.message}")
continue
}
val payoutHex = payoutRpcResponse.result?.asString ?: "0x0"
val payout = EthereumUtils.decodeUint256(payoutHex)
payouts.add(payout)
}
// 解析 ABI 编码的返回结果
val (payoutDenominator, payouts) = EthereumUtils.decodeConditionResult(hexResult)
Result.success(Pair(payoutDenominator, payouts))
} catch (e: Exception) {
@@ -1,173 +0,0 @@
package com.wrbug.polymarketbot.service.common
import com.wrbug.polymarketbot.repository.CopyOrderTrackingRepository
import jakarta.annotation.PostConstruct
import jakarta.annotation.PreDestroy
import kotlinx.coroutines.*
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
/**
* 市场信息轮询服务
* 定期检查买入订单的市场信息是否缺失如果缺失则从API获取并更新
*/
@Service
class MarketPollingService(
private val copyOrderTrackingRepository: CopyOrderTrackingRepository,
private val marketService: MarketService
) {
private val logger = LoggerFactory.getLogger(MarketPollingService::class.java)
@Value("\${market.polling.interval:30000}")
private var pollingInterval: Long = 30000 // 轮询间隔(毫秒),默认30秒
@Value("\${market.polling.batch.size:50}")
private var batchSize: Int = 50 // 批量处理大小,每次最多处理50个市场
// 协程作用域和任务
private val scope = CoroutineScope(Dispatchers.Default + SupervisorJob())
private var pollingJob: Job? = null
// 同步锁,确保轮询任务的启动和停止是线程安全的
private val lock = Any()
/**
* 初始化服务后端启动时直接启动轮询
*/
@PostConstruct
fun init() {
logger.info("MarketPollingService 初始化,启动市场信息轮询任务,轮询间隔: ${pollingInterval}ms (${pollingInterval / 1000 / 60}分钟)")
startPolling()
}
/**
* 清理资源
*/
@PreDestroy
fun destroy() {
synchronized(lock) {
pollingJob?.cancel()
pollingJob = null
}
scope.cancel()
}
/**
* 启动轮询任务
*/
private fun startPolling() {
synchronized(lock) {
// 如果已经有轮询任务在运行,先取消
pollingJob?.cancel()
// 启动新的轮询任务
pollingJob = scope.launch(Dispatchers.IO) {
// 启动时立即执行一次
try {
checkAndUpdateMissingMarkets()
} catch (e: Exception) {
logger.error("初始检查市场信息失败: ${e.message}", e)
}
// 然后按间隔定期执行
while (isActive) {
try {
delay(pollingInterval)
checkAndUpdateMissingMarkets()
} catch (e: Exception) {
logger.error("轮询市场信息失败: ${e.message}", e)
}
}
}
}
}
/**
* 检查并更新缺失的市场信息
*/
private suspend fun checkAndUpdateMissingMarkets() {
try {
logger.debug("开始检查缺失的市场信息...")
// 1. 获取所有买入订单的市场ID(去重)
val allOrders = copyOrderTrackingRepository.findAll()
val marketIds = allOrders.map { it.marketId }.distinct()
if (marketIds.isEmpty()) {
logger.debug("没有找到任何订单,跳过市场信息检查")
return
}
logger.debug("找到 ${marketIds.size} 个不同的市场ID")
// 2. 检查哪些市场信息在数据库中缺失
val existingMarkets = marketService.marketRepository.findByMarketIdIn(marketIds)
val existingMarketIds = existingMarkets.map { it.marketId }.toSet()
val missingMarketIds = marketIds.filter { it !in existingMarketIds }
// 过滤掉空字符串和无效的市场ID
val validMissingMarketIds = missingMarketIds.filter {
it.isNotBlank() && it.startsWith("0x")
}
if (validMissingMarketIds.isEmpty()) {
logger.debug("所有市场信息都已存在,无需更新")
return
}
logger.info("发现 ${validMissingMarketIds.size} 个缺失的市场信息,开始批量更新...")
// 3. 批量从API获取缺失的市场信息(分批处理,避免一次性请求过多)
val batches = validMissingMarketIds.chunked(batchSize)
var successCount = 0
var failCount = 0
for ((index, batch) in batches.withIndex()) {
try {
logger.debug("处理第 ${index + 1}/${batches.size} 批,包含 ${batch.size} 个市场: ${batch.take(3).joinToString(", ")}${if (batch.size > 3) "..." else ""}")
// 使用 MarketService 的批量获取方法
// 这个方法会尝试从API获取并保存到数据库
val markets = marketService.getMarkets(batch)
val batchSuccessCount = markets.size
val batchFailCount = batch.size - batchSuccessCount
successCount += batchSuccessCount
failCount += batchFailCount
if (batchFailCount > 0) {
logger.warn("${index + 1} 批中有 ${batchFailCount} 个市场信息获取失败")
}
// 避免请求过于频繁,每批之间稍作延迟
if (index < batches.size - 1) {
delay(1000) // 延迟1秒,避免API限流
}
} catch (e: Exception) {
logger.error("批量获取市场信息失败: batch=${batch.take(5).joinToString(", ")}..., error=${e.message}", e)
failCount += batch.size
}
}
logger.info("市场信息更新完成: 成功=${successCount}, 失败=${failCount}, 总计=${validMissingMarketIds.size}")
} catch (e: Exception) {
logger.error("检查并更新市场信息异常: ${e.message}", e)
}
}
/**
* 手动触发检查用于测试或手动刷新
*/
fun triggerCheck() {
scope.launch(Dispatchers.IO) {
try {
checkAndUpdateMissingMarkets()
} catch (e: Exception) {
logger.error("手动触发检查市场信息失败: ${e.message}", e)
}
}
}
}
@@ -31,8 +31,7 @@ class MarketPriceService(
* 获取当前市场最新价
* 优先级
* 1. 链上查询市场结算结果如果已结算返回 1.0 0.0
* 2. CLOB API 查询订单簿价格最准确优先使用使用 bestBid
* 3. Gamma Market API 查询市场价格快速作为备选
* 2. CLOB API 查询订单簿价格最准确使用 bestBid
*
* 价格会被截位到 4 位小数向下截断不四舍五入用于显示和后续计算
*
@@ -43,35 +42,21 @@ class MarketPriceService(
*/
suspend fun getCurrentMarketPrice(marketId: String, outcomeIndex: Int): BigDecimal {
// 1. 优先从链上查询市场结算结果
val (chainPrice, hasRpcError) = getPriceFromChainCondition(marketId, outcomeIndex)
val chainPrice = getPriceFromChainCondition(marketId, outcomeIndex)
if (chainPrice != null) {
// 截位到 4 位小数(向下截断,不四舍五入)
return chainPrice.setScale(4, java.math.RoundingMode.DOWN)
}
// 如果链上查询出现 RPC 错误(execution reverted),说明市场可能不存在或尚未创建
// 在这种情况下,降级到其他数据源(CLOB API 或 Gamma API),而不是直接抛出异常
// 因为 marketId 可能在 API 中存在,但在链上尚未创建
if (hasRpcError) {
logger.debug("链上查询市场条件出现 RPC 错误(execution reverted),降级到 API 查询: marketId=$marketId, outcomeIndex=$outcomeIndex")
}
// 2. 从 CLOB API 查询订单簿价格(最准确,优先使用)
// 2. 从 CLOB API 查询订单簿价格(最准确)
val orderbookPrice = getPriceFromClobOrderbook(marketId, outcomeIndex)
if (orderbookPrice != null) {
// 截位到 4 位小数(向下截断,不四舍五入)
return orderbookPrice.setScale(4, java.math.RoundingMode.DOWN)
}
// 3. 从 Gamma Market API 查询市场价格(作为备选)
val marketPrice = getPriceFromGammaMarket(marketId, outcomeIndex)
if (marketPrice != null) {
// 截位到 4 位小数(向下截断,不四舍五入)
return marketPrice.setScale(4, java.math.RoundingMode.DOWN)
}
// 如果所有数据源都失败,抛出异常
val errorMsg = "无法获取市场价格: marketId=$marketId, outcomeIndex=$outcomeIndex (链上查询订单簿查询和 Market API 均失败)"
val errorMsg = "无法获取市场价格: marketId=$marketId, outcomeIndex=$outcomeIndex (链上查询订单簿查询均失败)"
logger.error(errorMsg)
throw IllegalStateException(errorMsg)
}
@@ -82,10 +67,8 @@ class MarketPriceService(
* - payout > 0赢了 返回 1.0
* - payout == 0输了 返回 0.0
* 如果市场未结算或查询失败返回 null
*
* @return Pair<BigDecimal?, Boolean> 第一个值是价格如果已结算第二个值表示是否发生了 RPC 错误execution reverted
*/
private suspend fun getPriceFromChainCondition(marketId: String, outcomeIndex: Int): Pair<BigDecimal?, Boolean> {
private suspend fun getPriceFromChainCondition(marketId: String, outcomeIndex: Int): BigDecimal? {
return try {
val chainResult = blockchainService.getCondition(marketId)
chainResult.fold(
@@ -96,95 +79,29 @@ class MarketPriceService(
when {
payout > BigInteger.ZERO -> {
logger.info("从链上查询到市场已结算,该 outcome 赢了: marketId=$marketId, outcomeIndex=$outcomeIndex, payout=$payout")
return Pair(BigDecimal.ONE, false)
return BigDecimal.ONE
}
payout == BigInteger.ZERO -> {
logger.info("从链上查询到市场已结算,该 outcome 输了: marketId=$marketId, outcomeIndex=$outcomeIndex, payout=$payout")
return Pair(BigDecimal.ZERO, false)
return BigDecimal.ZERO
}
else -> {
logger.warn("从链上查询到异常的 payout 值: marketId=$marketId, outcomeIndex=$outcomeIndex, payout=$payout")
Pair(null, false)
null
}
}
} else {
logger.debug("从链上查询到市场尚未结算: marketId=$marketId, payouts=${payouts.size}")
Pair(null, false)
null
}
},
onFailure = { e ->
// 检查是否是 execution reverted 错误
val isRpcError = e.message?.contains("execution reverted", ignoreCase = true) == true
if (isRpcError) {
logger.warn("链上查询市场条件出现 RPC 错误(execution reverted),可能市场不存在或尚未创建: marketId=$marketId, error=${e.message}")
} else {
logger.debug("链上查询市场条件失败,降级到 API 查询: marketId=$marketId, error=${e.message}")
}
Pair(null, isRpcError)
null
}
)
} catch (e: Exception) {
val isRpcError = e.message?.contains("execution reverted", ignoreCase = true) == true
if (isRpcError) {
logger.warn("链上查询市场条件异常(execution reverted: marketId=$marketId, outcomeIndex=$outcomeIndex, error=${e.message}")
} else {
logger.debug("链上查询市场条件异常: marketId=$marketId, outcomeIndex=$outcomeIndex, error=${e.message}")
}
Pair(null, isRpcError)
}
}
/**
* Gamma Market API 获取价格
* 使用 outcomePrices 字段格式通常为 JSON 字符串 "[\"0.5\", \"0.5\"]"
* 如果查询失败或 outcomePrices 为空返回 null
*/
private suspend fun getPriceFromGammaMarket(marketId: String, outcomeIndex: Int): BigDecimal? {
return try {
val gammaApi = retrofitFactory.createGammaApi()
val marketResponse = gammaApi.listMarkets(conditionIds = listOf(marketId))
if (!marketResponse.isSuccessful || marketResponse.body() == null) {
logger.debug("Gamma Market API 查询失败: marketId=$marketId, code=${marketResponse.code()}")
return null
}
val markets = marketResponse.body()!!
if (markets.isEmpty()) {
logger.debug("Gamma Market API 未找到市场: marketId=$marketId")
return null
}
val market = markets.first()
// 尝试从 outcomePrices 字段获取价格
val outcomePricesStr = market.outcomePrices
if (outcomePricesStr.isNullOrBlank()) {
logger.debug("Market outcomePrices 为空: marketId=$marketId")
return null
}
// 解析 outcomePrices(通常是 JSON 数组字符串)
val outcomePrices = try {
// 移除首尾的方括号和引号,按逗号分割
val cleanStr = outcomePricesStr.trim().removeSurrounding("[", "]")
cleanStr.split(",").map {
it.trim().removeSurrounding("\"").toSafeBigDecimal()
}
} catch (e: Exception) {
logger.warn("解析 outcomePrices 失败: marketId=$marketId, outcomePrices=$outcomePricesStr, error=${e.message}")
null
}
if (outcomePrices != null && outcomeIndex < outcomePrices.size) {
val price = outcomePrices[outcomeIndex]
logger.debug("从 Gamma Market API 获取价格: marketId=$marketId, outcomeIndex=$outcomeIndex, price=$price")
return price
}
null
} catch (e: Exception) {
logger.debug("Gamma Market API 查询异常: marketId=$marketId, outcomeIndex=$outcomeIndex, error=${e.message}")
null
}
}
@@ -1,234 +0,0 @@
package com.wrbug.polymarketbot.service.common
import com.github.benmanes.caffeine.cache.Cache
import com.github.benmanes.caffeine.cache.Caffeine
import com.wrbug.polymarketbot.api.MarketResponse
import com.wrbug.polymarketbot.api.PolymarketGammaApi
import com.wrbug.polymarketbot.entity.Market
import com.wrbug.polymarketbot.repository.MarketRepository
import com.wrbug.polymarketbot.util.RetrofitFactory
import com.wrbug.polymarketbot.util.getEventSlug
import kotlinx.coroutines.runBlocking
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import java.time.Instant
import java.time.format.DateTimeFormatter
/**
* 市场信息服务
* 负责缓存和管理市场信息
*/
@Service
class MarketService(
val marketRepository: MarketRepository, // 改为 public,供 MarketPollingService 使用
private val retrofitFactory: RetrofitFactory
) {
private val logger = LoggerFactory.getLogger(MarketService::class.java)
// LRU 缓存(避免频繁查询数据库),最多缓存 200 条记录
private val marketCache: Cache<String, Market> = Caffeine.newBuilder()
.maximumSize(200) // 最多缓存 200 条记录
.build()
/**
* 根据市场ID获取市场信息
* 优先从缓存获取如果不存在则从数据库查询如果数据库也没有则从API获取并保存
*/
fun getMarket(marketId: String): Market? {
// 1. 从缓存获取
marketCache.getIfPresent(marketId)?.let { return it }
// 2. 从数据库查询
val market = marketRepository.findByMarketId(marketId)
if (market != null) {
marketCache.put(marketId, market)
return market
}
// 3. 从API获取(异步,不阻塞)
runBlocking {
try {
fetchAndSaveMarket(marketId)
} catch (e: Exception) {
logger.warn("获取市场信息失败: marketId=$marketId, error=${e.message}")
}
}
// 再次从数据库查询(API可能已经保存)
return marketRepository.findByMarketId(marketId)?.also {
marketCache.put(marketId, it)
}
}
/**
* 批量获取市场信息
*/
fun getMarkets(marketIds: List<String>): Map<String, Market> {
val result = mutableMapOf<String, Market>()
val missingIds = mutableListOf<String>()
// 1. 从缓存和数据库获取
for (marketId in marketIds) {
val market = getMarket(marketId)
if (market != null) {
result[marketId] = market
} else {
missingIds.add(marketId)
}
}
// 2. 批量从API获取缺失的市场信息
if (missingIds.isNotEmpty()) {
runBlocking {
try {
fetchAndSaveMarkets(missingIds)
} catch (e: Exception) {
logger.warn("批量获取市场信息失败: marketIds=$missingIds, error=${e.message}")
}
}
// 再次从数据库查询
val savedMarkets = marketRepository.findByMarketIdIn(missingIds)
for (market in savedMarkets) {
result[market.marketId] = market
marketCache.put(market.marketId, market)
}
}
return result
}
/**
* 从API获取市场信息并保存到数据库
*/
private suspend fun fetchAndSaveMarket(marketId: String): Market? {
return try {
val gammaApi = retrofitFactory.createGammaApi()
val response = gammaApi.listMarkets(conditionIds = listOf(marketId))
if (response.isSuccessful && response.body() != null) {
val markets = response.body()!!
if (markets.isNotEmpty()) {
val marketResponse = markets.first()
saveMarketFromResponse(marketId, marketResponse)
} else {
null
}
} else {
null
}
} catch (e: Exception) {
logger.error("从API获取市场信息失败: marketId=$marketId, error=${e.message}", e)
null
}
}
/**
* 批量从API获取市场信息并保存到数据库
*/
private suspend fun fetchAndSaveMarkets(marketIds: List<String>) {
if (marketIds.isEmpty()) return
try {
val gammaApi = retrofitFactory.createGammaApi()
val response = gammaApi.listMarkets(conditionIds = marketIds)
if (response.isSuccessful && response.body() != null) {
val markets = response.body()!!
val marketMap = markets.associateBy { it.conditionId ?: "" }
for (marketId in marketIds) {
val marketResponse = marketMap[marketId]
if (marketResponse != null) {
saveMarketFromResponse(marketId, marketResponse)
}
}
}
} catch (e: Exception) {
logger.error("批量从API获取市场信息失败: marketIds=$marketIds, error=${e.message}", e)
}
}
/**
* 从API响应保存市场信息到数据库
*/
private fun saveMarketFromResponse(marketId: String, marketResponse: MarketResponse): Market? {
return try {
val existingMarket = marketRepository.findByMarketId(marketId)
// 保存原来的 slug(用于显示)
val slug = marketResponse.slug
// 保存跳转用的 slug(从 events[0].slug 获取)
val eventSlug = marketResponse.getEventSlug()
val market = if (existingMarket != null) {
// 更新现有市场信息
existingMarket.copy(
title = marketResponse.question ?: existingMarket.title,
slug = slug ?: existingMarket.slug,
eventSlug = eventSlug ?: existingMarket.eventSlug,
category = marketResponse.category ?: existingMarket.category,
icon = marketResponse.icon ?: existingMarket.icon,
image = marketResponse.image ?: existingMarket.image,
description = marketResponse.description ?: existingMarket.description,
active = marketResponse.active ?: existingMarket.active,
closed = marketResponse.closed ?: existingMarket.closed,
archived = marketResponse.archived ?: existingMarket.archived,
endDate = parseEndDate(marketResponse.endDate),
updatedAt = System.currentTimeMillis()
)
} else {
// 创建新市场信息
Market(
marketId = marketId,
title = marketResponse.question ?: marketId,
slug = slug,
eventSlug = eventSlug,
category = marketResponse.category,
icon = marketResponse.icon,
image = marketResponse.image,
description = marketResponse.description,
active = marketResponse.active ?: true,
closed = marketResponse.closed ?: false,
archived = marketResponse.archived ?: false,
endDate = parseEndDate(marketResponse.endDate),
createdAt = System.currentTimeMillis(),
updatedAt = System.currentTimeMillis()
)
}
val savedMarket = marketRepository.save(market)
marketCache.put(marketId, savedMarket)
savedMarket
} catch (e: Exception) {
logger.error("保存市场信息失败: marketId=$marketId, error=${e.message}", e)
null
}
}
/**
* 清除缓存用于测试或手动刷新
*/
fun clearCache() {
marketCache.invalidateAll()
}
/**
* 解析市场截止时间ISO 8601 格式
*/
private fun parseEndDate(endDate: String?): Long? {
if (endDate.isNullOrBlank()) {
return null
}
return try {
// ISO 8601 格式,例如:2025-03-15T12:00:00Z
Instant.parse(endDate).toEpochMilli()
} catch (e: Exception) {
logger.warn("解析市场截止时间失败: endDate=$endDate, error=${e.message}")
null
}
}
}
@@ -3,12 +3,12 @@ package com.wrbug.polymarketbot.service.common
import com.google.gson.Gson
import com.wrbug.polymarketbot.api.ApiKeyResponse
import com.wrbug.polymarketbot.api.PolymarketClobApi
import com.wrbug.polymarketbot.constants.PolymarketConstants
import com.wrbug.polymarketbot.util.PolymarketL1AuthInterceptor
import com.wrbug.polymarketbot.util.RetrofitFactory
import com.wrbug.polymarketbot.util.createClient
import kotlinx.coroutines.runBlocking
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
@@ -19,6 +19,8 @@ import retrofit2.converter.gson.GsonConverterFactory
*/
@Service
class PolymarketApiKeyService(
@Value("\${polymarket.clob.base-url}")
private val clobBaseUrl: String,
private val gson: Gson
) {
@@ -222,7 +224,7 @@ class PolymarketApiKeyService(
.build()
return Retrofit.Builder()
.baseUrl(PolymarketConstants.CLOB_BASE_URL)
.baseUrl(clobBaseUrl)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
@@ -236,7 +238,7 @@ class PolymarketApiKeyService(
val okHttpClient = createClient().build()
return Retrofit.Builder()
.baseUrl(PolymarketConstants.CLOB_BASE_URL)
.baseUrl(clobBaseUrl)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
@@ -401,37 +401,5 @@ class PolymarketClobService(
Result.failure(e)
}
}
/**
* 获取费率
* 文档: https://docs.polymarket.com/developers/market-makers/maker-rebates-program#1-fetch-the-fee-rate
*
* 注意根据 TypeScript clob-client 源码API 返回的字段名是 base_fee而不是文档中的 fee_rate_bps
* 参考: clob-client/src/client.ts:312
*
* @param tokenId Token ID
* @return 费率基点0 表示无费率1000 表示 10%
*/
suspend fun getFeeRate(tokenId: String): Result<Int> {
return try {
val response = clobApi.getFeeRate(tokenId)
if (response.isSuccessful && response.body() != null) {
val baseFee = response.body()!!.baseFee
logger.debug("获取费率成功: tokenId=$tokenId, baseFee=$baseFee")
Result.success(baseFee)
} else {
val errorBody = try {
response.errorBody()?.string()
} catch (e: Exception) {
null
}
logger.error("获取费率失败: tokenId=$tokenId, code=${response.code()}, errorBody=$errorBody")
Result.failure(Exception("获取费率失败: ${response.code()} ${response.message()}"))
}
} catch (e: Exception) {
logger.error("获取费率异常: tokenId=$tokenId, error=${e.message}", e)
Result.failure(e)
}
}
}
@@ -6,11 +6,9 @@ import com.wrbug.polymarketbot.util.gt
import com.wrbug.polymarketbot.util.lt
import com.wrbug.polymarketbot.util.multi
import com.wrbug.polymarketbot.util.toSafeBigDecimal
import com.wrbug.polymarketbot.util.JsonUtils
import org.slf4j.LoggerFactory
import com.wrbug.polymarketbot.service.common.PolymarketClobService
import com.wrbug.polymarketbot.service.accounts.AccountService
import com.wrbug.polymarketbot.repository.CopyOrderTrackingRepository
import org.springframework.stereotype.Service
import java.math.BigDecimal
@@ -20,9 +18,7 @@ import java.math.BigDecimal
@Service
class CopyTradingFilterService(
private val clobService: PolymarketClobService,
private val accountService: AccountService,
private val copyOrderTrackingRepository: CopyOrderTrackingRepository,
private val jsonUtils: JsonUtils
private val accountService: AccountService
) {
private val logger = LoggerFactory.getLogger(CopyTradingFilterService::class.java)
@@ -34,8 +30,6 @@ class CopyTradingFilterService(
* @param tradePrice Leader 交易价格用于价格区间检查
* @param copyOrderAmount 跟单金额USDC用于仓位检查如果为null则不进行仓位检查
* @param marketId 市场ID用于仓位检查按市场过滤仓位
* @param marketTitle 市场标题用于关键字过滤
* @param marketEndDate 市场截止时间用于市场截止时间检查
* @return 过滤结果
*/
suspend fun checkFilters(
@@ -43,27 +37,9 @@ class CopyTradingFilterService(
tokenId: String,
tradePrice: BigDecimal? = null, // Leader 交易价格,用于价格区间检查
copyOrderAmount: BigDecimal? = null, // 跟单金额(USDC),用于仓位检查
marketId: String? = null, // 市场ID,用于仓位检查(按市场过滤仓位)
marketTitle: String? = null, // 市场标题,用于关键字过滤
marketEndDate: Long? = null // 市场截止时间,用于市场截止时间检查
marketId: String? = null // 市场ID,用于仓位检查(按市场过滤仓位)
): FilterResult {
// 1. 关键字过滤检查(如果配置了关键字过滤
if (copyTrading.keywordFilterMode != null && copyTrading.keywordFilterMode != "DISABLED") {
val keywordCheck = checkKeywordFilter(copyTrading, marketTitle)
if (!keywordCheck.isPassed) {
return keywordCheck
}
}
// 1.5. 市场截止时间检查(如果配置了市场截止时间限制)
if (copyTrading.maxMarketEndDate != null) {
val marketEndDateCheck = checkMarketEndDate(copyTrading, marketEndDate)
if (!marketEndDateCheck.isPassed) {
return marketEndDateCheck
}
}
// 2. 价格区间检查(如果配置了价格区间)
// 1. 价格区间检查(如果配置了价格区间
if (tradePrice != null) {
val priceRangeCheck = checkPriceRange(copyTrading, tradePrice)
if (!priceRangeCheck.isPassed) {
@@ -71,24 +47,16 @@ class CopyTradingFilterService(
}
}
// 3. 检查是否需要获取订单簿或需要执行仓位检查
// 只有在配置了需要订单簿的过滤条件时才获取订单簿
// 2. 检查是否需要获取订单簿
// 只有在配置了需要订单簿的过滤条件时才获取
val needOrderbook = copyTrading.maxSpread != null || copyTrading.minOrderDepth != null
// 3.5. 如果不需要订单簿,则跳过订单簿相关的检查,但仍然需要检查仓位限制
if (!needOrderbook) {
// 仓位检查(如果配置了最大仓位限制且提供了跟单金额和市场ID)
if (copyOrderAmount != null && marketId != null) {
val positionCheck = checkPositionLimits(copyTrading, copyOrderAmount, marketId)
if (!positionCheck.isPassed) {
return positionCheck
}
}
// 通过所有检查
// 不需要订单簿,直接通过
return FilterResult.passed()
}
// 4. 获取订单簿(仅在需要时,只请求一次)
// 3. 获取订单簿(仅在需要时,只请求一次)
val orderbookResult = clobService.getOrderbookByTokenId(tokenId)
if (!orderbookResult.isSuccess) {
val error = orderbookResult.exceptionOrNull()
@@ -98,7 +66,7 @@ class CopyTradingFilterService(
val orderbook = orderbookResult.getOrNull()
?: return FilterResult.orderbookEmpty()
// 5. 买一卖一价差过滤(如果配置了)
// 4. 买一卖一价差过滤(如果配置了)
if (copyTrading.maxSpread != null) {
val spreadCheck = checkSpread(copyTrading, orderbook)
if (!spreadCheck.isPassed) {
@@ -106,7 +74,7 @@ class CopyTradingFilterService(
}
}
// 6. 订单深度过滤(如果配置了,检查所有方向)
// 5. 订单深度过滤(如果配置了,检查所有方向)
if (copyTrading.minOrderDepth != null) {
val depthCheck = checkOrderDepth(copyTrading, orderbook)
if (!depthCheck.isPassed) {
@@ -114,7 +82,7 @@ class CopyTradingFilterService(
}
}
// 7. 仓位检查(如果配置了最大仓位限制且提供了跟单金额和市场ID)
// 6. 仓位检查(如果配置了最大仓位限制且提供了跟单金额和市场ID)
if (copyOrderAmount != null && marketId != null) {
val positionCheck = checkPositionLimits(copyTrading, copyOrderAmount, marketId)
if (!positionCheck.isPassed) {
@@ -125,65 +93,6 @@ class CopyTradingFilterService(
return FilterResult.passed(orderbook)
}
/**
* 检查关键字过滤
* @param copyTrading 跟单配置
* @param marketTitle 市场标题
* @return 过滤结果
*/
private fun checkKeywordFilter(
copyTrading: CopyTrading,
marketTitle: String?
): FilterResult {
// 如果未启用关键字过滤,直接通过
if (copyTrading.keywordFilterMode == null || copyTrading.keywordFilterMode == "DISABLED") {
return FilterResult.passed()
}
// 如果没有市场标题,无法进行关键字过滤,为了安全起见,不通过
if (marketTitle.isNullOrBlank()) {
return FilterResult.keywordFilterFailed("市场标题为空,无法进行关键字过滤")
}
// 解析关键字列表
val keywords = jsonUtils.parseStringArray(copyTrading.keywords)
if (keywords.isEmpty()) {
// 如果关键字列表为空,白名单模式不通过,黑名单模式通过
return if (copyTrading.keywordFilterMode == "WHITELIST") {
FilterResult.keywordFilterFailed("白名单模式但关键字列表为空")
} else {
FilterResult.passed()
}
}
// 将市场标题转换为小写,用于不区分大小写的匹配
val titleLower = marketTitle.lowercase()
// 检查市场标题是否包含任意关键字
val containsKeyword = keywords.any { keyword ->
titleLower.contains(keyword.lowercase())
}
// 根据过滤模式决定是否通过
return when (copyTrading.keywordFilterMode) {
"WHITELIST" -> {
if (containsKeyword) {
FilterResult.passed()
} else {
FilterResult.keywordFilterFailed("白名单模式:市场标题不包含任何关键字。市场标题:$marketTitle,关键字列表:${keywords.joinToString(", ")}")
}
}
"BLACKLIST" -> {
if (containsKeyword) {
FilterResult.keywordFilterFailed("黑名单模式:市场标题包含关键字。市场标题:$marketTitle,匹配的关键字:${keywords.filter { titleLower.contains(it.lowercase()) }.joinToString(", ")}")
} else {
FilterResult.passed()
}
}
else -> FilterResult.passed()
}
}
/**
* 检查价格区间
* @param copyTrading 跟单配置
@@ -325,46 +234,32 @@ class CopyTradingFilterService(
// 检查最大仓位金额(如果配置了)
if (copyTrading.maxPositionValue != null) {
// 比较数据库成本价(本地订单记录)和外部持仓市值(可能来自其他终端的操作),取最大值
val dbValue = copyOrderTrackingRepository.sumCurrentPositionValueByMarket(copyTrading.id!!, marketId) ?: BigDecimal.ZERO
val extValue = marketPositions.sumOf { it.currentValue.toSafeBigDecimal() }
val currentPositionValue = dbValue.max(extValue)
// 计算该市场的当前仓位总价值(累加该市场所有仓位的 currentValue
val currentPositionValue = marketPositions.sumOf { position ->
position.currentValue.toSafeBigDecimal()
}
// 检查:该市场的当前仓位 + 跟单金额 <= 最大仓位金额
val totalValueAfterOrder = currentPositionValue.add(copyOrderAmount)
if (totalValueAfterOrder.gt(copyTrading.maxPositionValue)) {
return FilterResult.maxPositionValueFailed(
"超过最大仓位金额限制: 当前该市场仓位(取最大值)=${currentPositionValue} USDC (DB=${dbValue}, Ext=${extValue}), 跟单金额=${copyOrderAmount} USDC, 总计=${totalValueAfterOrder} USDC > 最大限制=${copyTrading.maxPositionValue} USDC"
"超过最大仓位金额限制: 当前该市场仓位=${currentPositionValue} USDC, 跟单金额=${copyOrderAmount} USDC, 总计=${totalValueAfterOrder} USDC > 最大限制=${copyTrading.maxPositionValue} USDC"
)
}
}
// 检查最大仓位数量(如果配置了)
if (copyTrading.maxPositionCount != null) {
// 使用数据库中的订单记录计算活跃仓位数量(解决延迟问题
val dbCount = copyOrderTrackingRepository.countActivePositions(copyTrading.id!!)
// 计算该市场的当前仓位数量(该市场不同方向的仓位算不同仓位
val currentPositionCount = marketPositions.size
// 计算外部持仓中的唯一市场数量(防止遗漏非本项目创建的仓位)
val extCount = positions.currentPositions
.filter { it.accountId == copyTrading.accountId }
.map { it.marketId }
.distinct()
.size
val currentPositionCount = maxOf(dbCount, extCount)
// 检查:如果当前没有该市场的活跃仓位,且总仓位数量已达到限制,则不允许开新仓
// 判断当前市场是否已有活跃仓位(数据库或外部持仓)
val hasDbPosition = copyOrderTrackingRepository.existsByCopyTradingIdAndMarketIdAndRemainingQuantityGreaterThan(
copyTrading.id, marketId, BigDecimal.ZERO
)
val hasExtPosition = marketPositions.isNotEmpty()
val hasCurrentMarketPosition = hasDbPosition || hasExtPosition
if (!hasCurrentMarketPosition && currentPositionCount >= copyTrading.maxPositionCount) {
// 检查:该市场的当前仓位数量 <= 最大仓位数量
// 注意:如果该市场已有仓位,跟单可能会增加新的仓位(不同方向)或增加现有仓位
// 为了简化,我们检查当前该市场的仓位数量是否已经达到或超过限制
if (currentPositionCount >= copyTrading.maxPositionCount) {
return FilterResult.maxPositionCountFailed(
"超过最大仓位数量限制: 当前活跃仓位总数(取最大值)=${currentPositionCount} (DB=${dbCount}, Ext=${extCount}) >= 最大限制=${copyTrading.maxPositionCount}"
"超过最大仓位数量限制: 当前该市场仓位数量=${currentPositionCount} >= 最大限制=${copyTrading.maxPositionCount}"
)
}
}
@@ -376,38 +271,5 @@ class CopyTradingFilterService(
return FilterResult.maxPositionValueFailed("仓位检查异常: ${e.message}")
}
}
/**
* 检查市场截止时间
* @param copyTrading 跟单配置
* @param marketEndDate 市场截止时间毫秒时间戳
* @return 过滤结果
*/
private fun checkMarketEndDate(
copyTrading: CopyTrading,
marketEndDate: Long?
): FilterResult {
// 如果未配置市场截止时间限制,直接通过
if (copyTrading.maxMarketEndDate == null) {
return FilterResult.passed()
}
// 如果没有市场截止时间,无法检查,为了安全起见,不通过
if (marketEndDate == null) {
return FilterResult.marketEndDateFailed("市场缺少截止时间信息,无法进行市场截止时间检查")
}
// 检查:市场截止时间 - 当前时间 <= 最大限制时间
val currentTime = System.currentTimeMillis()
val remainingTime = marketEndDate - currentTime
if (remainingTime > copyTrading.maxMarketEndDate) {
return FilterResult.marketEndDateFailed(
"市场截止时间超出限制: 剩余时间=${remainingTime}ms (${remainingTime / (1000 * 60 * 60)}小时) > 最大限制=${copyTrading.maxMarketEndDate}ms (${copyTrading.maxMarketEndDate / (1000 * 60 * 60)}小时)"
)
}
return FilterResult.passed()
}
}
@@ -9,9 +9,6 @@ import com.wrbug.polymarketbot.repository.CopyTradingRepository
import com.wrbug.polymarketbot.repository.CopyTradingTemplateRepository
import com.wrbug.polymarketbot.repository.LeaderRepository
import com.wrbug.polymarketbot.service.copytrading.monitor.CopyTradingMonitorService
import com.google.gson.Gson
import com.wrbug.polymarketbot.util.IllegalBigDecimal
import com.wrbug.polymarketbot.util.JsonUtils
import com.wrbug.polymarketbot.util.toSafeBigDecimal
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
@@ -27,9 +24,7 @@ class CopyTradingService(
private val accountRepository: AccountRepository,
private val templateRepository: CopyTradingTemplateRepository,
private val leaderRepository: LeaderRepository,
private val monitorService: CopyTradingMonitorService,
private val jsonUtils: JsonUtils,
private val gson: Gson
private val monitorService: CopyTradingMonitorService
) {
private val logger = LoggerFactory.getLogger(CopyTradingService::class.java)
@@ -51,7 +46,16 @@ class CopyTradingService(
val leader = leaderRepository.findById(request.leaderId).orElse(null)
?: return Result.failure(IllegalArgumentException("Leader 不存在"))
// 3. 验证配置名(强校验:不能为空字符串
// 3. 检查是否已存在相同的跟单关系(accountId + leaderId
val existing = copyTradingRepository.findByAccountIdAndLeaderId(
request.accountId,
request.leaderId
)
if (existing != null) {
return Result.failure(IllegalArgumentException("该跟单关系已存在"))
}
// 4. 验证配置名(强校验:不能为空字符串)
val configName = request.configName?.trim()
if (configName.isNullOrBlank()) {
return Result.failure(IllegalArgumentException("配置名不能为空"))
@@ -84,11 +88,7 @@ class CopyTradingService(
minPrice = request.minPrice?.toSafeBigDecimal() ?: template.minPrice,
maxPrice = request.maxPrice?.toSafeBigDecimal() ?: template.maxPrice,
maxPositionValue = request.maxPositionValue?.toSafeBigDecimal(),
maxPositionCount = request.maxPositionCount,
keywordFilterMode = request.keywordFilterMode ?: "DISABLED",
keywords = convertKeywordsToJson(request.keywords),
maxMarketEndDate = request.maxMarketEndDate,
pushFilteredOrders = request.pushFilteredOrders ?: template.pushFilteredOrders
maxPositionCount = request.maxPositionCount
)
} else {
// 手动输入(所有字段必须提供)
@@ -116,11 +116,7 @@ class CopyTradingService(
minPrice = request.minPrice?.toSafeBigDecimal(),
maxPrice = request.maxPrice?.toSafeBigDecimal(),
maxPositionValue = request.maxPositionValue?.toSafeBigDecimal(),
maxPositionCount = request.maxPositionCount,
keywordFilterMode = request.keywordFilterMode ?: "DISABLED",
keywords = convertKeywordsToJson(request.keywords),
maxMarketEndDate = request.maxMarketEndDate,
pushFilteredOrders = request.pushFilteredOrders ?: false // 手动输入时使用请求中的值,默认为 false
maxPositionCount = request.maxPositionCount
)
}
@@ -149,12 +145,8 @@ class CopyTradingService(
maxPrice = config.maxPrice,
maxPositionValue = config.maxPositionValue,
maxPositionCount = config.maxPositionCount,
keywordFilterMode = config.keywordFilterMode,
keywords = config.keywords,
configName = configName,
pushFailedOrders = request.pushFailedOrders ?: false,
maxMarketEndDate = config.maxMarketEndDate,
pushFilteredOrders = config.pushFilteredOrders
pushFailedOrders = request.pushFailedOrders ?: false
)
val saved = copyTradingRepository.save(copyTrading)
@@ -215,88 +207,14 @@ class CopyTradingService(
websocketReconnectInterval = request.websocketReconnectInterval ?: copyTrading.websocketReconnectInterval,
websocketMaxRetries = request.websocketMaxRetries ?: copyTrading.websocketMaxRetries,
supportSell = request.supportSell ?: copyTrading.supportSell,
// 处理可选字段:空字符串表示要清空(设置为 null),null 表示不更新,转换失败保留旧值
minOrderDepth = if (request.minOrderDepth != null) {
if (request.minOrderDepth.isBlank()) {
null
} else {
val converted = request.minOrderDepth.toSafeBigDecimal()
if (converted == IllegalBigDecimal) copyTrading.minOrderDepth else converted
}
} else {
copyTrading.minOrderDepth
},
maxSpread = if (request.maxSpread != null) {
if (request.maxSpread.isBlank()) {
null
} else {
val converted = request.maxSpread.toSafeBigDecimal()
if (converted == IllegalBigDecimal) copyTrading.maxSpread else converted
}
} else {
copyTrading.maxSpread
},
minPrice = if (request.minPrice != null) {
if (request.minPrice.isBlank()) {
null
} else {
val converted = request.minPrice.toSafeBigDecimal()
if (converted == IllegalBigDecimal) copyTrading.minPrice else converted
}
} else {
copyTrading.minPrice
},
maxPrice = if (request.maxPrice != null) {
if (request.maxPrice.isBlank()) {
null
} else {
val converted = request.maxPrice.toSafeBigDecimal()
if (converted == IllegalBigDecimal) copyTrading.maxPrice else converted
}
} else {
copyTrading.maxPrice
},
maxPositionValue = if (request.maxPositionValue != null) {
if (request.maxPositionValue.isBlank()) {
null
} else {
val converted = request.maxPositionValue.toSafeBigDecimal()
if (converted == IllegalBigDecimal) copyTrading.maxPositionValue else converted
}
} else {
copyTrading.maxPositionValue
},
// 处理 maxPositionCount-1 表示要清空(设置为 null),null 表示不更新
maxPositionCount = if (request.maxPositionCount != null) {
if (request.maxPositionCount == -1) {
null
} else {
request.maxPositionCount
}
} else {
copyTrading.maxPositionCount
},
keywordFilterMode = request.keywordFilterMode ?: copyTrading.keywordFilterMode,
keywords = if (request.keywords != null) {
convertKeywordsToJson(request.keywords)
} else if (request.keywordFilterMode != null && request.keywordFilterMode == "DISABLED") {
null
} else {
copyTrading.keywords
},
minOrderDepth = request.minOrderDepth?.toSafeBigDecimal() ?: copyTrading.minOrderDepth,
maxSpread = request.maxSpread?.toSafeBigDecimal() ?: copyTrading.maxSpread,
minPrice = request.minPrice?.toSafeBigDecimal() ?: copyTrading.minPrice,
maxPrice = request.maxPrice?.toSafeBigDecimal() ?: copyTrading.maxPrice,
maxPositionValue = request.maxPositionValue?.toSafeBigDecimal() ?: copyTrading.maxPositionValue,
maxPositionCount = request.maxPositionCount ?: copyTrading.maxPositionCount,
configName = configName,
pushFailedOrders = request.pushFailedOrders ?: copyTrading.pushFailedOrders,
pushFilteredOrders = request.pushFilteredOrders ?: copyTrading.pushFilteredOrders,
// 处理 maxMarketEndDate-1 表示要清空(设置为 null),null 表示不更新
maxMarketEndDate = if (request.maxMarketEndDate != null) {
if (request.maxMarketEndDate == -1L) {
null
} else {
request.maxMarketEndDate
}
} else {
copyTrading.maxMarketEndDate
},
updatedAt = System.currentTimeMillis()
)
@@ -346,10 +264,11 @@ class CopyTradingService(
return try {
val copyTradings = when {
request.accountId != null && request.leaderId != null -> {
copyTradingRepository.findByAccountIdAndLeaderId(
val found = copyTradingRepository.findByAccountIdAndLeaderId(
request.accountId,
request.leaderId
)
if (found != null) listOf(found) else emptyList()
}
request.accountId != null -> {
copyTradingRepository.findByAccountId(request.accountId)
@@ -505,47 +424,13 @@ class CopyTradingService(
maxPrice = copyTrading.maxPrice?.toPlainString(),
maxPositionValue = copyTrading.maxPositionValue?.toPlainString(),
maxPositionCount = copyTrading.maxPositionCount,
keywordFilterMode = copyTrading.keywordFilterMode,
keywords = convertJsonToKeywords(copyTrading.keywords),
configName = copyTrading.configName,
pushFailedOrders = copyTrading.pushFailedOrders,
pushFilteredOrders = copyTrading.pushFilteredOrders,
maxMarketEndDate = copyTrading.maxMarketEndDate,
createdAt = copyTrading.createdAt,
updatedAt = copyTrading.updatedAt
)
}
/**
* 将关键字列表转换为 JSON 字符串
*/
private fun convertKeywordsToJson(keywords: List<String>?): String? {
if (keywords == null || keywords.isEmpty()) {
return null
}
return try {
gson.toJson(keywords)
} catch (e: Exception) {
logger.error("转换关键字列表为 JSON 失败", e)
null
}
}
/**
* JSON 字符串转换为关键字列表
*/
private fun convertJsonToKeywords(jsonString: String?): List<String>? {
if (jsonString.isNullOrBlank()) {
return null
}
return try {
jsonUtils.parseStringArray(jsonString)
} catch (e: Exception) {
logger.error("解析关键字 JSON 失败", e)
null
}
}
/**
* 内部配置类用于构建 CopyTrading 实体
*/
@@ -569,10 +454,6 @@ class CopyTradingService(
val minPrice: BigDecimal?,
val maxPrice: BigDecimal?,
val maxPositionValue: BigDecimal?,
val maxPositionCount: Int?,
val keywordFilterMode: String,
val keywords: String?, // JSON 字符串
val maxMarketEndDate: Long?, // 市场截止时间限制(毫秒时间戳)
val pushFilteredOrders: Boolean // 推送已过滤订单(默认关闭)
val maxPositionCount: Int?
)
}
@@ -21,11 +21,7 @@ enum class FilterStatus {
/** 失败:超过最大仓位金额 */
FAILED_MAX_POSITION_VALUE,
/** 失败:超过最大仓位数量 */
FAILED_MAX_POSITION_COUNT,
/** 失败:关键字过滤 */
FAILED_KEYWORD_FILTER,
/** 失败:市场截止时间超出限制 */
FAILED_MARKET_END_DATE
FAILED_MAX_POSITION_COUNT
}
/**
@@ -93,18 +89,6 @@ data class FilterResult(
status = FilterStatus.FAILED_MAX_POSITION_COUNT,
reason = reason
)
/** 关键字过滤失败 */
fun keywordFilterFailed(reason: String) = FilterResult(
status = FilterStatus.FAILED_KEYWORD_FILTER,
reason = reason
)
/** 市场截止时间超出限制 */
fun marketEndDateFailed(reason: String) = FilterResult(
status = FilterStatus.FAILED_MARKET_END_DATE,
reason = reason
)
}
}
@@ -114,10 +114,10 @@ class AccountOnChainMonitorService(
}
val receiptRpcResponse = receiptResponse.body()!!
if (receiptRpcResponse.error != null || receiptRpcResponse.result == null || receiptRpcResponse.result.isJsonNull) {
if (receiptRpcResponse.error != null || receiptRpcResponse.result == null) {
return
}
// 使用 Gson 解析 receipt JSON
val receiptJson = receiptRpcResponse.result.asJsonObject
@@ -14,9 +14,7 @@ import org.springframework.stereotype.Service
/**
* 跟单监听服务主服务
* 管理所有Leader的交易监听
* 使用双重监听机制
* 1. Activity WebSocket - 低延迟< 100ms优先使用
* 2. OnChain WebSocket - 高可靠性~2-3s作为兜底
* 同时运行链上 WebSocket 监听和轮询监听并行处理
* 同时监听跟单账户的卖出/赎回事件通过链上 WebSocket
*/
@Service
@@ -24,7 +22,7 @@ class CopyTradingMonitorService(
private val copyTradingRepository: CopyTradingRepository,
private val leaderRepository: LeaderRepository,
private val accountRepository: AccountRepository,
private val activityWsService: PolymarketActivityWsService,
private val pollingService: CopyTradingPollingService,
private val onChainWsService: OnChainWsService,
private val accountOnChainMonitorService: AccountOnChainMonitorService
) {
@@ -53,17 +51,15 @@ class CopyTradingMonitorService(
@PreDestroy
fun destroy() {
scope.cancel()
// 停止所有监听
activityWsService.stop()
// 停止轮询和链上 WS 监听
pollingService.stop()
onChainWsService.stop()
accountOnChainMonitorService.stop()
}
/**
* 启动监听
* 启动双重监听机制
* 1. Activity WebSocket - 低延迟< 100ms优先使用
* 2. OnChain WebSocket - 高可靠性~2-3s作为兜底
* 同时启动链上 WebSocket 监听和轮询监听并行运行
* 同时启动跟单账户的链上 WebSocket 监听用于检测卖出/赎回事件
*/
suspend fun startMonitoring() {
@@ -86,13 +82,14 @@ class CopyTradingMonitorService(
accountRepository.findById(accountId).orElse(null)
}
// 4. 启动 Activity WebSocket 监听(优先,低延迟
activityWsService.start(leaders)
// 5. 启动链上 WebSocket 监听(兜底,高可靠性)
// 4. 同时启动链上 WebSocket 监听和轮询监听(并行运行
// 链上 WS 监听 Leader 的交易(实时,秒级延迟)
onChainWsService.start(leaders)
// 6. 启动跟单账户的链上 WebSocket 监听(用于检测卖出/赎回事件
// 轮询监听 Leader 的交易(延迟,2秒间隔,作为备份
pollingService.start(leaders)
// 5. 启动跟单账户的链上 WebSocket 监听(用于检测卖出/赎回事件)
accountOnChainMonitorService.start(accounts)
}
@@ -109,9 +106,9 @@ class CopyTradingMonitorService(
return
}
// 同时添加到两种监听
activityWsService.addLeader(leader)
// 同时添加到链上 WS 监听和轮询监听(如果不在列表中才添加)
onChainWsService.addLeader(leader)
pollingService.addLeader(leader)
}
/**
@@ -125,9 +122,9 @@ class CopyTradingMonitorService(
return
}
// 没有启用的跟单配置了,同时从两种监听移除
activityWsService.removeLeader(leaderId)
// 没有启用的跟单配置了,移除监听
onChainWsService.removeLeader(leaderId)
pollingService.removeLeader(leaderId)
}
/**
@@ -141,8 +138,8 @@ class CopyTradingMonitorService(
if (copyTradings.isNotEmpty()) {
// 有启用的跟单配置,确保在监听列表中
activityWsService.addLeader(leader)
onChainWsService.addLeader(leader)
pollingService.addLeader(leader)
// 更新账户监听(添加该配置关联的账户)
val accountIds = copyTradings.map { it.accountId }.distinct()
@@ -153,9 +150,9 @@ class CopyTradingMonitorService(
}
}
} else {
// 没有启用的跟单配置,同时从两种监听移除
activityWsService.removeLeader(leaderId)
// 没有启用的跟单配置,移除监听
onChainWsService.removeLeader(leaderId)
pollingService.removeLeader(leaderId)
}
}
@@ -184,8 +181,8 @@ class CopyTradingMonitorService(
*/
suspend fun restartMonitoring() {
// 停止所有监听
activityWsService.stop()
onChainWsService.stop()
pollingService.stop()
delay(1000) // 等待1秒
startMonitoring()
}
@@ -0,0 +1,279 @@
package com.wrbug.polymarketbot.service.copytrading.monitor
import com.wrbug.polymarketbot.api.TradeResponse
import com.wrbug.polymarketbot.api.UserActivityResponse
import com.wrbug.polymarketbot.entity.Leader
import com.wrbug.polymarketbot.repository.CopyTradingTemplateRepository
import com.wrbug.polymarketbot.util.RetrofitFactory
import jakarta.annotation.PreDestroy
import kotlinx.coroutines.*
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import com.wrbug.polymarketbot.service.copytrading.statistics.CopyOrderTrackingService
import org.springframework.stereotype.Service
import retrofit2.Response
import java.util.concurrent.ConcurrentHashMap
/**
* 跟单轮询监听服务
* 通过定期轮询 Polymarket Data API /activity 接口获取Leader的交易记录
* 使用 /activity 接口可以查询用户的链上活动包括交易
*/
@Service
class CopyTradingPollingService(
private val copyOrderTrackingService: CopyOrderTrackingService,
private val retrofitFactory: RetrofitFactory,
private val templateRepository: CopyTradingTemplateRepository
) {
private val logger = LoggerFactory.getLogger(CopyTradingPollingService::class.java)
@Value("\${copy.trading.polling.interval:2000}")
private var pollingInterval: Long = 2000 // 轮询间隔(毫秒),默认2秒
@Value("\${copy.trading.polling.enabled:true}")
private var pollingEnabled: Boolean = true // 是否启用轮询
private val scope = CoroutineScope(Dispatchers.Default + SupervisorJob())
// 存储需要监听的LeaderleaderId -> Leader
private val monitoredLeaders = ConcurrentHashMap<Long, Leader>()
// 存储每个Leader已缓存的交易ID集合:leaderId -> Set<tradeId>
private val cachedTradeIds = ConcurrentHashMap<Long, MutableSet<String>>()
// 存储每个Leader是否首次轮询:leaderId -> isFirstPoll
private val isFirstPoll = ConcurrentHashMap<Long, Boolean>()
// 轮询任务
private var pollingJob: Job? = null
/**
* 启动轮询监听
*/
fun start(leaders: List<Leader>) {
if (!pollingEnabled) {
return
}
leaders.forEach { leader ->
addLeader(leader)
}
// 启动轮询任务
startPolling()
}
/**
* 添加Leader监听
*/
fun addLeader(leader: Leader) {
if (leader.id == null) {
logger.warn("Leader ID为空,跳过: ${leader.leaderAddress}")
return
}
val leaderId = leader.id
monitoredLeaders[leaderId] = leader
// 初始化缓存的交易ID集合
cachedTradeIds[leaderId] = mutableSetOf()
// 首次轮询标志,用于缓存数据而不处理
isFirstPoll[leaderId] = true
// 如果轮询任务没有运行,启动它
startPolling()
}
/**
* 移除Leader监听
*/
fun removeLeader(leaderId: Long) {
monitoredLeaders.remove(leaderId)
cachedTradeIds.remove(leaderId)
isFirstPoll.remove(leaderId)
// 如果没有需要监听的Leader了,停止轮询任务
if (monitoredLeaders.isEmpty()) {
stopPolling()
}
}
/**
* 停止所有监听
*/
fun stop() {
stopPolling()
monitoredLeaders.clear()
cachedTradeIds.clear()
isFirstPoll.clear()
}
/**
* 启动轮询任务
*/
private fun startPolling() {
if (pollingJob != null && pollingJob!!.isActive) {
return
}
if (monitoredLeaders.isEmpty()) {
return
}
pollingJob = scope.launch {
while (isActive) {
try {
// 轮询所有Leader的交易
pollAllLeaders()
// 等待下一次轮询
delay(pollingInterval)
} catch (e: Exception) {
logger.error("轮询任务异常", e)
delay(pollingInterval) // 异常后继续等待
}
}
}
}
/**
* 停止轮询任务
*/
private fun stopPolling() {
pollingJob?.cancel()
pollingJob = null
}
/**
* 轮询所有Leader的交易
*/
private suspend fun pollAllLeaders() {
val leaders = monitoredLeaders.values.toList()
if (leaders.isEmpty()) {
return
}
// 并发轮询所有Leader(限制并发数)
leaders.chunked(10).forEach { chunk ->
chunk.forEach { leader ->
try {
pollLeaderTrades(leader)
} catch (e: Exception) {
logger.error("轮询Leader交易失败: leaderId=${leader.id}, address=${leader.leaderAddress}", e)
}
}
// 每个chunk之间稍作延迟,避免API限流
delay(100)
}
}
/**
* 轮询单个Leader的交易
* 使用 Polymarket Data API /activity 接口
* 通过 diff 分析增量数据不使用 start 字段
*/
private suspend fun pollLeaderTrades(leader: Leader) {
if (leader.id == null) {
return
}
val leaderId = leader.id
val leaderAddress = leader.leaderAddress
try {
val firstPoll = isFirstPoll[leaderId] == true
val cachedIds = cachedTradeIds[leaderId] ?: mutableSetOf()
// 创建 Data API 客户端(不需要认证)
val dataApi = retrofitFactory.createDataApi()
// 查询用户活动(只查询交易类型,不使用 start 字段)
// 查询最近的数据(limit=100),通过 diff 找出增量
val response: Response<List<UserActivityResponse>> = dataApi.getUserActivity(
user = leaderAddress,
limit = 100, // 每次最多查询100条
offset = 0,
type = listOf("TRADE"), // 只查询交易类型
start = null, // 不使用 start 字段
sortBy = "TIMESTAMP",
sortDirection = "DESC" // 按时间戳降序,最新的在前
)
if (!response.isSuccessful || response.body() == null) {
logger.warn("获取Leader活动失败: leaderId=$leaderId, address=$leaderAddress, code=${response.code()}, message=${response.message()}")
return
}
val activities = response.body()!!
// 将 UserActivityResponse 转换为 TradeResponse
val allTrades = activities.mapNotNull { activity ->
// 只处理交易类型
if (activity.type != "TRADE" || activity.side == null || activity.price == null || activity.size == null) {
return@mapNotNull null
}
// 转换为 TradeResponse
TradeResponse(
id = activity.transactionHash ?: "${activity.timestamp}_${activity.conditionId}",
market = activity.conditionId,
side = activity.side, // BUY 或 SELL
price = activity.price.toString(),
size = activity.size.toString(),
timestamp = activity.timestamp.toString(), // 时间戳(秒)
user = activity.proxyWallet,
outcomeIndex = activity.outcomeIndex, // 结果索引(0=YES, 1=NO
outcome = activity.outcome // 结果名称
)
}
if (firstPoll) {
// 首次轮询:缓存所有查询到的交易ID,不处理
val tradeIds = allTrades.map { it.id }.toSet()
cachedIds.addAll(tradeIds)
cachedTradeIds[leaderId] = cachedIds
// 标记首次轮询完成
isFirstPoll[leaderId] = false
} else {
// 后续轮询:通过 diff 找出新增的交易
val newTradeIds = allTrades.map { it.id }.toSet()
val incrementalTradeIds = newTradeIds - cachedIds
if (incrementalTradeIds.isNotEmpty()) {
// 找出新增的交易
val incrementalTrades = allTrades.filter { it.id in incrementalTradeIds }
// 处理新增的交易
incrementalTrades.forEach { trade ->
try {
// 检查是否已处理(去重由processTrade内部处理)
copyOrderTrackingService.processTrade(leaderId, trade, "polling")
} catch (e: Exception) {
logger.error("处理交易失败: leaderId=$leaderId, tradeId=${trade.id}", e)
}
}
// 更新缓存:添加新增的交易ID
cachedIds.addAll(incrementalTradeIds)
cachedTradeIds[leaderId] = cachedIds
} else {
}
// 限制缓存大小,避免内存溢出(只保留最近1000条)
if (cachedIds.size > 1000) {
// 保留最新的1000条(由于查询是按时间戳降序,保留前1000条即可)
val sortedTradeIds = allTrades.map { it.id }.take(1000).toSet()
cachedTradeIds[leaderId] = sortedTradeIds.toMutableSet()
}
}
} catch (e: Exception) {
logger.error("轮询Leader交易异常: leaderId=$leaderId, address=$leaderAddress", e)
}
}
}
@@ -9,8 +9,8 @@ import com.wrbug.polymarketbot.repository.CopyTradingTemplateRepository
import com.wrbug.polymarketbot.websocket.PolymarketWebSocketClient
import jakarta.annotation.PreDestroy
import kotlinx.coroutines.*
import com.wrbug.polymarketbot.constants.PolymarketConstants
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import com.wrbug.polymarketbot.service.copytrading.statistics.CopyOrderTrackingService
import org.springframework.stereotype.Service
import java.util.concurrent.ConcurrentHashMap
@@ -28,7 +28,8 @@ class CopyTradingWebSocketService(
private val logger = LoggerFactory.getLogger(CopyTradingWebSocketService::class.java)
private val websocketUrl: String = PolymarketConstants.USER_WS_URL
@Value("\${polymarket.websocket.url:wss://ws-live-data.polymarket.com}")
private var websocketUrl: String = "wss://ws-live-data.polymarket.com"
private val scope = CoroutineScope(Dispatchers.Default + SupervisorJob())
// 存储每个Leader的WebSocket客户端:leaderId -> WebSocketClient
@@ -213,7 +214,7 @@ class CopyTradingWebSocketService(
// 处理交易
scope.launch {
try {
copyOrderTrackingService.processTrade(leaderId, trade, "activity-ws")
copyOrderTrackingService.processTrade(leaderId, trade, "websocket")
} catch (e: Exception) {
logger.error("处理交易失败: leaderId=$leaderId, tradeId=${trade.id}", e)
}
@@ -1,8 +1,5 @@
package com.wrbug.polymarketbot.service.copytrading.monitor
import com.github.benmanes.caffeine.cache.Cache
import com.github.benmanes.caffeine.cache.Caffeine
import com.google.gson.JsonNull
import com.wrbug.polymarketbot.api.*
import com.wrbug.polymarketbot.entity.Leader
import com.wrbug.polymarketbot.repository.LeaderRepository
@@ -14,7 +11,6 @@ import okhttp3.OkHttpClient
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.TimeUnit
/**
* 链上 WebSocket 监听服务
@@ -27,17 +23,12 @@ class OnChainWsService(
private val copyOrderTrackingService: CopyOrderTrackingService,
private val leaderRepository: LeaderRepository
) {
private val logger = LoggerFactory.getLogger(OnChainWsService::class.java)
// 存储需要监听的LeaderleaderId -> Leader
private val monitoredLeaders = ConcurrentHashMap<Long, Leader>()
// 存储已处理的交易哈希,用于去重(LRU 缓存,保留最近 100 条)
private val processedTxHashes: Cache<String, Long> = Caffeine.newBuilder()
.maximumSize(100)
.build()
/**
* 启动链上 WebSocket 监听
* 通过统一服务订阅所有 Leader
@@ -49,14 +40,14 @@ class OnChainWsService(
stop()
return
}
// 更新 Leader 列表
monitoredLeaders.clear()
leaders.forEach { leader ->
addLeader(leader)
}
}
/**
* 添加Leader监听
* 通过统一服务订阅该 Leader 的地址
@@ -66,17 +57,17 @@ class OnChainWsService(
logger.warn("Leader ID为空,跳过: ${leader.leaderAddress}")
return
}
val leaderId = leader.id!!
// 如果已经在监听列表中,不重复添加
if (monitoredLeaders.containsKey(leaderId)) {
logger.debug("Leader 已在监听列表中: ${leader.leaderName} (${leader.leaderAddress})")
return
}
monitoredLeaders[leaderId] = leader
// 通过统一服务订阅
val subscriptionId = "LEADER_$leaderId"
unifiedOnChainWsService.subscribe(
@@ -88,53 +79,36 @@ class OnChainWsService(
handleLeaderTransaction(leaderId, txHash, httpClient, rpcApi)
}
)
logger.info("添加 Leader 监听: ${leader.leaderName} (${leader.leaderAddress})")
}
/**
* 处理 Leader 的交易
*/
private suspend fun handleLeaderTransaction(
leaderId: Long,
txHash: String,
httpClient: OkHttpClient,
rpcApi: EthereumRpcApi
) {
private suspend fun handleLeaderTransaction(leaderId: Long, txHash: String, httpClient: OkHttpClient, rpcApi: EthereumRpcApi) {
val leader = monitoredLeaders[leaderId] ?: return
// 根据 txHash 去重(使用原子操作避免竞态条件)
val currentTime = System.currentTimeMillis()
val existingTimestamp = processedTxHashes.asMap().putIfAbsent(txHash, currentTime)
if (existingTimestamp != null) {
logger.debug("交易已处理过,跳过: leaderId=$leaderId, txHash=$txHash, firstProcessedAt=$existingTimestamp")
return
}
logger.debug("开始处理 Leader 交易: leaderId=$leaderId, txHash=$txHash, leaderAddress=${leader.leaderAddress}")
try {
// 获取交易 receipt
val receiptRequest = JsonRpcRequest(
method = "eth_getTransactionReceipt",
params = listOf(txHash)
)
val receiptResponse = rpcApi.call(receiptRequest)
if (!receiptResponse.isSuccessful || receiptResponse.body() == null) {
logger.warn("获取交易 receipt 失败: leaderId=$leaderId, txHash=$txHash, code=${receiptResponse.code()}")
return
}
val receiptRpcResponse = receiptResponse.body()!!
if (receiptRpcResponse.error != null || receiptRpcResponse.result == null || receiptRpcResponse.result is JsonNull) {
logger.warn("交易 receipt 错误: leaderId=$leaderId, txHash=$txHash, error=${receiptRpcResponse.error}")
if (receiptRpcResponse.error != null || receiptRpcResponse.result == null) {
return
}
// 使用 Gson 解析 receipt JSON
val receiptJson = receiptRpcResponse.result.asJsonObject
// 获取区块号和时间戳
val blockNumber = receiptJson.get("blockNumber")?.asString
val blockTimestamp = if (blockNumber != null) {
@@ -142,15 +116,11 @@ class OnChainWsService(
} else {
null
}
// 解析 receipt 中的 Transfer 日志
val logs = receiptJson.getAsJsonArray("logs") ?: run {
logger.warn("交易 receipt 中没有日志: leaderId=$leaderId, txHash=$txHash")
return
}
val logs = receiptJson.getAsJsonArray("logs") ?: return
val (erc20Transfers, erc1155Transfers) = OnChainWsUtils.parseReceiptTransfers(logs)
logger.debug("解析交易日志: leaderId=$leaderId, txHash=$txHash, erc20Transfers=${erc20Transfers.size}, erc1155Transfers=${erc1155Transfers.size}")
// 解析交易信息
val trade = OnChainWsUtils.parseTradeFromTransfers(
txHash = txHash,
@@ -160,37 +130,34 @@ class OnChainWsService(
erc1155Transfers = erc1155Transfers,
retrofitFactory = retrofitFactory
)
if (trade != null) {
logger.info("成功解析交易: leaderId=$leaderId, txHash=$txHash, side=${trade.side}, market=${trade.market}, size=${trade.size}")
// 调用 processTrade 处理交易
copyOrderTrackingService.processTrade(
leaderId = leaderId,
trade = trade,
source = "onchain-ws"
)
} else {
logger.warn("无法解析交易(返回 null: leaderId=$leaderId, txHash=$txHash, erc20Transfers=${erc20Transfers.size}, erc1155Transfers=${erc1155Transfers.size}")
}
} catch (e: Exception) {
logger.error("处理 Leader 交易失败: leaderId=$leaderId, txHash=$txHash, ${e.message}", e)
}
}
/**
* 移除Leader监听
* 取消该 Leader 的订阅
*/
fun removeLeader(leaderId: Long) {
monitoredLeaders.remove(leaderId)
// 通过统一服务取消订阅
val subscriptionId = "LEADER_$leaderId"
unifiedOnChainWsService.unsubscribe(subscriptionId)
logger.info("移除 Leader 监听: leaderId=$leaderId")
}
/**
* 停止监听
*/
@@ -202,7 +169,7 @@ class OnChainWsService(
}
monitoredLeaders.clear()
}
@PreDestroy
fun destroy() {
stop()
@@ -207,7 +207,6 @@ object OnChainWsUtils {
usdcRaw = usdcIn
} else {
// 无法判断交易方向
logger.debug("无法判断交易方向: txHash=$txHash, bestInId=$bestInId, bestInVal=$bestInVal, bestOutId=$bestOutId, bestOutVal=$bestOutVal, usdcOut=$usdcOut, usdcIn=$usdcIn")
return null
}
@@ -1,576 +0,0 @@
package com.wrbug.polymarketbot.service.copytrading.monitor
import com.github.benmanes.caffeine.cache.Cache
import com.github.benmanes.caffeine.cache.Caffeine
import com.wrbug.polymarketbot.api.TradeResponse
import com.wrbug.polymarketbot.dto.ActivityTradeMessage
import com.wrbug.polymarketbot.dto.ActivityTradePayload
import com.wrbug.polymarketbot.entity.Leader
import com.wrbug.polymarketbot.repository.LeaderRepository
import com.wrbug.polymarketbot.service.copytrading.statistics.CopyOrderTrackingService
import com.wrbug.polymarketbot.util.fromJson
import com.wrbug.polymarketbot.constants.PolymarketConstants
import com.wrbug.polymarketbot.websocket.PolymarketWebSocketClient
import jakarta.annotation.PreDestroy
import kotlinx.coroutines.*
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import java.math.BigDecimal
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.TimeUnit
/**
* Polymarket Activity WebSocket 监听服务
* 通过订阅全局 activity 交易流trades + orders_matched客户端过滤 Leader 地址实现实时交易检测
* 延迟 < 100ms适合快速跟单场景
*/
@Service
class PolymarketActivityWsService(
private val copyOrderTrackingService: CopyOrderTrackingService,
private val leaderRepository: LeaderRepository
) {
private val logger = LoggerFactory.getLogger(PolymarketActivityWsService::class.java)
private val websocketUrl: String = PolymarketConstants.ACTIVITY_WS_URL
private val scope = CoroutineScope(Dispatchers.Default + SupervisorJob())
// 单例 WebSocket 客户端
private var wsClient: PolymarketWebSocketClient? = null
// 要监听的 Leader 地址集合(小写地址 -> leaderId
private val monitoredAddresses = ConcurrentHashMap<String, Long>()
// 存储已处理的交易哈希,用于去重(LRU 缓存,保留最近 100 条)
// 因为同时订阅 trades 和 orders_matched,同一个交易可能被推送两次
private val processedTxHashes: Cache<String, Long> = Caffeine.newBuilder()
.maximumSize(100)
.expireAfterWrite(10, TimeUnit.MINUTES)
.build()
// 是否已订阅
@Volatile
private var isSubscribed = false
// 最后一次收到 activity 消息的时间(毫秒时间戳)
@Volatile
private var lastActivityTime: Long = 0
// Activity 消息超时检测任务
private var activityTimeoutJob: Job? = null
// 性能统计
private var totalMessagesProcessed = 0L
private var addressMatchMessages = 0L
private var jsonParseMessages = 0L
private var duplicateTxHashMessages = 0L
/**
* 启动监听
*/
fun start(leaders: List<Leader>) {
monitoredAddresses.clear()
leaders.forEach { leader ->
val leaderId = leader.id
if (leaderId != null) {
monitoredAddresses[leader.leaderAddress.lowercase()] = leaderId
}
}
if (monitoredAddresses.isEmpty()) {
logger.info("没有需要监听的 Leader,停止 Activity WebSocket")
stop()
return
}
logger.info("启动 Activity WebSocket 监听(trades + orders_matched),监控 ${monitoredAddresses.size} 个 Leader 地址")
connectAndSubscribe()
}
/**
* 添加 Leader
*/
fun addLeader(leader: Leader) {
if (leader.id == null) {
logger.warn("Leader ID 为空,跳过: ${leader.leaderAddress}")
return
}
val address = leader.leaderAddress.lowercase()
val existingLeaderId = monitoredAddresses[address]
if (existingLeaderId != null && existingLeaderId == leader.id) {
logger.debug("Leader 已在监听列表中: ${leader.leaderName} (${address})")
return
}
val leaderId = leader.id
if (leaderId != null) {
monitoredAddresses[address] = leaderId
logger.info("添加 Leader 到 Activity WS 监听: ${leader.leaderName} (${address})")
// 如果 WebSocket 未连接,连接
val client = wsClient
if (client == null || !client.isConnected()) {
connectAndSubscribe()
}
}
}
/**
* 移除 Leader
*/
fun removeLeader(leaderId: Long) {
val addressToRemove = monitoredAddresses.entries
.find { it.value == leaderId }?.key
if (addressToRemove != null) {
monitoredAddresses.remove(addressToRemove)
logger.info("从 Activity WS 监听移除 Leader: leaderId=$leaderId, address=$addressToRemove")
}
// 如果没有 Leader 了,停止监听
if (monitoredAddresses.isEmpty()) {
logger.info("没有 Leader 需要监听了,停止 Activity WebSocket")
stop()
}
}
/**
* 连接并订阅
*/
private fun connectAndSubscribe() {
val existingClient = wsClient
if (existingClient != null && existingClient.isConnected()) {
// 如果已连接但未订阅,发送订阅
if (!isSubscribed) {
subscribeAllActivity()
}
return
}
logger.info("连接 Activity WebSocket: $websocketUrl")
val newClient = PolymarketWebSocketClient(
url = websocketUrl,
sessionId = "copy-trading-activity",
onMessage = { message -> handleMessage(message) },
onOpen = {
logger.info("Activity WebSocket 连接成功")
subscribeAllActivity()
},
onReconnect = {
logger.info("Activity WebSocket 重连成功,重新订阅")
subscribeAllActivity()
}
)
wsClient = newClient
scope.launch {
try {
newClient.connect()
} catch (e: Exception) {
logger.error("连接 Activity WebSocket 失败", e)
}
}
}
/**
* 订阅全局 activity
* 根据 @polymarket/real-time-data-client 的协议格式
* 使用 "action": "subscribe" 而不是 "type": "subscribe"
* 同时订阅 trades orders_matched 两种类型
*/
private fun subscribeAllActivity() {
val client = wsClient
if (client == null || !client.isConnected()) {
logger.warn("WebSocket 未连接,无法订阅")
return
}
try {
// 根据 real-time-data-client 的协议格式
// 订阅消息应包含 "action": "subscribe" 和 "subscriptions" 数组
// 同时订阅 trades 和 orders_matched 两种类型
val subscribeMessage = """
{
"action": "subscribe",
"subscriptions": [
{
"topic": "activity",
"type": "trades"
},
{
"topic": "activity",
"type": "orders_matched"
}
]
}
""".trimIndent()
client.sendMessage(subscribeMessage)
isSubscribed = true
// 重置最后一次收到 activity 消息的时间
lastActivityTime = System.currentTimeMillis()
// 启动 Activity 消息超时检测
// startActivityTimeoutCheck()
logger.info("Activity WebSocket 订阅成功(全局交易流: trades + orders_matched")
} catch (e: Exception) {
logger.error("订阅 Activity WebSocket 失败", e)
isSubscribed = false
}
}
/**
* 启动 Activity 消息超时检测
* 每30秒检查一次如果超过30秒没有收到activity消息则重连
*/
private fun startActivityTimeoutCheck() {
// 先停止之前的检测任务
stopActivityTimeoutCheck()
activityTimeoutJob = scope.launch {
while (isActive && isSubscribed) {
delay(30000) // 每30秒检查一次
// 如果已经取消订阅,停止检测
if (!isSubscribed) {
break
}
// 如果 lastActivityTime 为 0,说明还没有收到过消息,跳过本次检测
if (lastActivityTime == 0L) {
continue
}
val currentTime = System.currentTimeMillis()
val timeSinceLastActivity = currentTime - lastActivityTime
// 如果超过30秒没有收到activity消息,触发重连
if (timeSinceLastActivity >= 30000) {
logger.warn("超过30秒未收到 Activity 消息,触发重连。距离上次消息: ${timeSinceLastActivity}ms")
// 关闭当前连接并重连
wsClient?.closeConnection()
wsClient = null
isSubscribed = false
// 重新连接
connectAndSubscribe()
break // 重连后会重新启动检测任务
}
}
}
}
/**
* 停止 Activity 消息超时检测
*/
private fun stopActivityTimeoutCheck() {
activityTimeoutJob?.cancel()
activityTimeoutJob = null
}
/**
* 检查消息是否包含监听的 Leader 地址
* 快速过滤避免不必要的 JSON 解析
* 只需要检查 "proxyWallet":"0x..." "trader":{"address":"0x..."} 格式
*/
private fun containsMonitoredAddress(message: String): Boolean {
// 快速检查:如果消息很短,不可能包含地址
if (message.length < 50) {
return false
}
// 遍历所有监听的地址
for ((address, leaderId) in monitoredAddresses) {
// 检查 proxyWallet:格式为 "proxyWallet":"0x..."
if (message.contains("\"proxyWallet\":\"$address\"", ignoreCase = true)) {
addressMatchMessages++
return true
}
// 检查 trader.address:格式为 "trader":{"address":"0x..."}
if (message.contains("\"trader\"", ignoreCase = true) &&
message.contains("\"address\":\"$address\"", ignoreCase = true)
) {
addressMatchMessages++
return true
}
}
return false
}
/**
* 处理消息
*/
private fun handleMessage(message: String) {
try {
totalMessagesProcessed++
// 处理 PONG 响应
if (message.trim() == "PONG" || message.trim() == "pong") {
return
}
// 快速预检查:检查是否包含监听地址
// 绝大部分消息会在这一步被过滤掉,避免不必要的 JSON 解析
if (!containsMonitoredAddress(message)) {
return
}
logger.info("发现leader交易:${message}")
// 使用扩展函数解析消息(只对包含监听地址的消息)
val tradeMessage = message.fromJson<ActivityTradeMessage>() ?: run {
// 不是有效的 JSON 或格式不匹配,跳过
logger.warn("无法解析为 ActivityTradeMessage: ${message.take(200)}")
return
}
jsonParseMessages++
// 检查是否是 activity 消息(trades 或 orders_matched
if (tradeMessage.topic != "activity" ||
(tradeMessage.type != "trades" && tradeMessage.type != "orders_matched")) {
return
}
// 更新最后一次收到 activity 消息的时间(即使不是我们监听的 Leader 的交易)
lastActivityTime = System.currentTimeMillis()
val payload = tradeMessage.payload
// 根据 txHash 去重(使用原子操作避免竞态条件)
val txHash = payload.transactionHash
if (txHash != null && txHash.isNotBlank()) {
val currentTime = System.currentTimeMillis()
val existingTimestamp = processedTxHashes.asMap().putIfAbsent(txHash, currentTime)
if (existingTimestamp != null) {
duplicateTxHashMessages++
logger.debug("交易已处理过,跳过: txHash=$txHash, firstProcessedAt=$existingTimestamp, type=${tradeMessage.type}")
return
}
}
// 提取交易者地址
val traderAddress = extractTraderAddress(payload) ?: run {
// 没有交易者地址,跳过
logger.warn("Activity Trade 消息中没有交易者地址: trader=${payload.trader}, proxyWallet=${payload.proxyWallet}, asset=${payload.asset}")
return
}
// 二次验证:确认地址匹配
val normalizedAddress = traderAddress.lowercase()
val leaderId = monitoredAddresses[normalizedAddress] ?: run {
return
}
// 解析交易数据
val trade = parseActivityTrade(payload, leaderId)
if (trade != null) {
logger.info("✅ 检测到 Leader 交易: leaderId=$leaderId, address=$traderAddress, side=${trade.side}, market=${trade.market}, size=${trade.size}")
// 异步处理交易(避免阻塞消息处理)
scope.launch {
try {
copyOrderTrackingService.processTrade(
leaderId = leaderId,
trade = trade,
source = "activity-ws"
)
} catch (e: Exception) {
logger.error("处理 Activity WS 交易失败: leaderId=$leaderId, tradeId=${trade.id}", e)
}
}
} else {
logger.warn("解析交易数据失败: leaderId=$leaderId, address=$traderAddress, asset=${payload.asset}, side=${payload.side}")
}
} catch (e: Exception) {
logger.error("处理 Activity WebSocket 消息失败: ${e.message}", e)
}
}
/**
* 提取交易者地址
* 优先检查 trader.addressfallback proxyWallet
*/
private fun extractTraderAddress(payload: ActivityTradePayload): String? {
// 优先检查 trader.address
val address = payload.trader?.address
?: payload.proxyWallet
return address
}
/**
* 解析 Activity Trade TradeResponse
*/
private fun parseActivityTrade(payload: ActivityTradePayload, leaderId: Long): TradeResponse? {
return try {
// 提取必需字段并验证
val asset = payload.asset
val conditionId = payload.conditionId
val sideRaw = payload.side
if (asset.isBlank() || conditionId.isBlank() || sideRaw.isBlank()) {
logger.warn("Activity Trade 消息缺少必需字段: asset=$asset, conditionId=$conditionId, side=$sideRaw")
return null
}
val side = sideRaw.uppercase()
// 验证 side 必须是 BUY 或 SELL
if (side != "BUY" && side != "SELL") {
logger.warn("Activity Trade 消息 side 字段无效: side=$side")
return null
}
// price 和 size 可能是数字或字符串,统一转换为字符串
val price = convertToString(payload.price) ?: run {
logger.warn("Activity Trade 消息 price 字段无效: ${payload.price}")
return null
}
val size = convertToString(payload.size) ?: run {
logger.warn("Activity Trade 消息 size 字段无效: ${payload.size}")
return null
}
// 时间戳处理:可能是秒或毫秒,可能是数字或字符串
val timestamp = when {
payload.timestamp == null -> System.currentTimeMillis().toString()
payload.timestamp is Number -> {
val ts = (payload.timestamp as Number).toLong()
// 如果时间戳小于 1e12(秒级),转换为毫秒
if (ts < 1e12) (ts * 1000).toString() else ts.toString()
}
payload.timestamp is String -> {
val tsStr = payload.timestamp as String
val ts = tsStr.toLongOrNull() ?: System.currentTimeMillis()
if (ts < 1e12) (ts * 1000).toString() else tsStr
}
else -> System.currentTimeMillis().toString()
}
// 解析 outcome 和 outcomeIndex
// 优先使用消息中的 outcomeIndex,如果没有则从 outcome 字符串解析
val outcome = payload.outcome
val outcomeIndex = payload.outcomeIndex
?: parseOutcomeIndex(outcome)
// 使用 transactionHash 作为 trade ID,如果没有则生成 fallback ID
val tradeId = payload.transactionHash ?: "${leaderId}_${System.currentTimeMillis()}_${asset.take(10)}"
TradeResponse(
id = tradeId,
market = conditionId,
side = side,
price = price,
size = size,
timestamp = timestamp,
user = null, // Activity WS 中不需要
outcomeIndex = outcomeIndex,
outcome = outcome
)
} catch (e: Exception) {
logger.error("解析 Activity Trade 失败: ${e.message}", e)
null
}
}
/**
* Any 类型的值转换为 String
* 支持 NumberStringBigDecimal 等类型
*/
private fun convertToString(value: Any?): String? {
if (value == null) return null
return when (value) {
is String -> value
is Number -> {
// 如果是数字,转换为 BigDecimal 再转为字符串(保持精度)
try {
BigDecimal(value.toString()).toPlainString()
} catch (e: Exception) {
value.toString()
}
}
is BigDecimal -> value.toPlainString()
else -> value.toString()
}
}
/**
* 解析 outcome outcomeIndex
*/
private fun parseOutcomeIndex(outcome: String?): Int? {
return when (outcome?.uppercase()) {
"YES", "UP", "TRUE" -> 0
"NO", "DOWN", "FALSE" -> 1
else -> null
}
}
/**
* 停止监听
*/
fun stop() {
logger.info("停止 Activity WebSocket 监听")
stopActivityTimeoutCheck()
wsClient?.closeConnection()
wsClient = null
isSubscribed = false
monitoredAddresses.clear()
processedTxHashes.invalidateAll() // 清空去重缓存
lastActivityTime = 0
}
/**
* 获取连接状态
*/
fun isConnected(): Boolean {
return wsClient?.isConnected() ?: false
}
/**
* 获取监听的 Leader 数量
*/
fun getMonitoredCount(): Int {
return monitoredAddresses.size
}
/**
* 获取性能统计信息
*/
fun getPerformanceStats(): Map<String, Any> {
val jsonParseRate = if (totalMessagesProcessed > 0) {
(jsonParseMessages.toDouble() / totalMessagesProcessed * 100).toInt()
} else {
0
}
return mapOf(
"totalMessages" to totalMessagesProcessed,
"addressMatches" to addressMatchMessages,
"jsonParses" to jsonParseMessages,
"duplicateTxHashes" to duplicateTxHashMessages,
"jsonParseRate" to "$jsonParseRate%",
"filteringEfficiency" to if (totalMessagesProcessed > 0) {
((1.0 - jsonParseMessages.toDouble() / totalMessagesProcessed) * 100).toInt()
} else {
0
}
)
}
@PreDestroy
fun destroy() {
logger.info("Activity WS 性能统计: ${getPerformanceStats()}")
stop()
scope.cancel()
}
}
@@ -1,7 +1,6 @@
package com.wrbug.polymarketbot.service.copytrading.monitor
import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import com.wrbug.polymarketbot.api.*
import com.wrbug.polymarketbot.service.system.RpcNodeService
@@ -20,7 +19,6 @@ import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicInteger
/**
* 统一的链上 WebSocket 服务
@@ -40,8 +38,27 @@ class UnifiedOnChainWsService(
private val scope = CoroutineScope(Dispatchers.Default + SupervisorJob())
// 存储所有地址的连接:address -> AddressWsConnection
private val addressConnections = ConcurrentHashMap<String, AddressWsConnection>()
// WebSocket 连接(唯一)
private var webSocket: WebSocket? = null
@Volatile
private var isConnected = false
// 订阅ID计数器(用于请求 ID
private var requestIdCounter = 0
// 连接任务(确保只有一个连接任务在运行)
private var connectionJob: Job? = null
// 存储所有订阅:subscriptionId -> 订阅信息
private val subscriptions = ConcurrentHashMap<String, SubscriptionInfo>()
// 存储请求 ID 到订阅 ID 的映射:requestId -> subscriptionId
// 用于在收到订阅响应时,将 subscription ID 关联到对应的订阅
private val requestIdToSubscriptionId = ConcurrentHashMap<Int, String>()
// 存储 RPC subscriptionId 到订阅 ID 的映射:rpcSubscriptionId -> subscriptionId
// 用于在收到日志通知时,知道是哪个订阅
private val rpcSubscriptionIdToSubscriptionId = ConcurrentHashMap<String, String>()
/**
* 订阅信息
@@ -71,24 +88,31 @@ class UnifiedOnChainWsService(
callback: suspend (String, OkHttpClient, EthereumRpcApi) -> Unit
): Boolean {
try {
val lowerAddress = address.lowercase()
// 找到或创建该地址的连接
val connection = addressConnections.computeIfAbsent(lowerAddress) {
AddressWsConnection(it).apply { start() }
// 如果已经订阅,先取消
if (subscriptions.containsKey(subscriptionId)) {
unsubscribe(subscriptionId)
}
// 创建订阅信息
val subscription = SubscriptionInfo(
subscriptionId = subscriptionId,
address = lowerAddress,
address = address.lowercase(),
entityType = entityType,
entityId = entityId,
callback = callback
)
// 添加订阅
connection.addSubscription(subscription)
subscriptions[subscriptionId] = subscription
// 如果已连接,立即订阅
if (isConnected) {
scope.launch {
subscribeAddress(subscription)
}
} else {
// 如果未连接,启动连接
startConnection()
}
logger.info("订阅地址监听: subscriptionId=$subscriptionId, address=$address, entityType=$entityType, entityId=$entityId")
return true
@@ -102,345 +126,440 @@ class UnifiedOnChainWsService(
* 取消订阅
*/
fun unsubscribe(subscriptionId: String) {
// 遍历所有连接找到含有该订阅的连接
for (connection in addressConnections.values) {
if (connection.hasSubscription(subscriptionId)) {
connection.removeSubscription(subscriptionId)
val subscription = subscriptions.remove(subscriptionId)
if (subscription != null && isConnected) {
// 取消该订阅的所有 RPC 订阅
scope.launch {
// 查找该订阅的所有 RPC subscriptionId
val rpcSubscriptionIds = rpcSubscriptionIdToSubscriptionId.entries
.filter { it.value == subscriptionId }
.map { it.key }
// 如果该连接没有订阅了,停止并移除
if (connection.isSubscriptionsEmpty()) {
connection.stop()
addressConnections.remove(connection.address)
logger.info("连接已无订阅,关闭连接: address=${connection.address}")
for (rpcSubId in rpcSubscriptionIds) {
unsubscribeRpc(rpcSubId)
rpcSubscriptionIdToSubscriptionId.remove(rpcSubId)
}
}
logger.info("取消订阅: subscriptionId=$subscriptionId")
}
// 如果没有订阅了,停止连接
if (subscriptions.isEmpty()) {
stop()
}
}
/**
* 启动连接如果还没有连接
*/
private fun startConnection() {
// 如果没有订阅,不启动连接
if (subscriptions.isEmpty()) {
return
}
// 如果连接任务已经在运行,不重复启动
if (connectionJob != null && connectionJob!!.isActive) {
return
}
// 启动连接任务
connectionJob = scope.launch {
startConnectionLoop()
}
}
/**
* 启动连接循环
*/
private suspend fun startConnectionLoop() {
while (scope.isActive) {
try {
// 如果没有订阅,停止连接
if (subscriptions.isEmpty()) {
logger.info("没有订阅,停止连接")
stop()
break
}
logger.info("取消订阅: subscriptionId=$subscriptionId")
return
// 如果已经连接,等待断开
if (isConnected && webSocket != null) {
waitForDisconnect()
continue
}
// 获取可用的 RPC 节点
val wsUrl = rpcNodeService.getWsUrl()
val httpUrl = rpcNodeService.getHttpUrl()
if (wsUrl.isBlank() || httpUrl.isBlank()) {
logger.warn("没有可用的 RPC 节点,等待重试...")
delay(reconnectDelay)
continue
}
logger.info("连接链上 WebSocket: $wsUrl (${subscriptions.size} 个订阅)")
// 创建 HTTP 客户端(用于 RPC 调用)
val httpClient = createHttpClient()
// 创建 RPC API 客户端
val rpcApi = retrofitFactory.createEthereumRpcApi(httpUrl)
// 连接 WebSocket
connectWebSocket(wsUrl, httpClient, rpcApi)
// 等待连接建立
waitForConnect()
// 如果连接成功,订阅所有地址
if (isConnected) {
logger.info("WebSocket 连接已建立,开始订阅")
for (subscription in subscriptions.values) {
subscribeAddress(subscription)
}
// 等待连接断开
waitForDisconnect()
}
// 连接断开后,如果没有订阅了,不再重连
if (subscriptions.isEmpty()) {
logger.info("没有订阅,停止重连")
break
}
// 等待后重连
logger.info("WebSocket 连接断开,等待 ${reconnectDelay}ms 后重连")
delay(reconnectDelay)
} catch (e: Exception) {
logger.error("连接异常: ${e.message}", e)
delay(reconnectDelay)
}
}
}
/**
* 停止所有服务
* 创建 HTTP 客户端
*/
private fun createHttpClient(): OkHttpClient {
val proxy = getProxyConfig()
val builder = createClient()
if (proxy != null) {
builder.proxy(proxy)
}
return builder.build()
}
/**
* 连接 WebSocket
*/
private fun connectWebSocket(wsUrl: String, httpClient: OkHttpClient, rpcApi: EthereumRpcApi) {
// 先关闭旧连接
webSocket?.close(1000, "重新连接")
webSocket = null
isConnected = false
val request = Request.Builder()
.url(wsUrl)
.build()
webSocket = httpClient.newWebSocket(request, object : WebSocketListener() {
override fun onOpen(webSocket: WebSocket, response: okhttp3.Response) {
isConnected = true
logger.info("链上 WebSocket 连接成功")
}
override fun onMessage(webSocket: WebSocket, text: String) {
scope.launch {
handleMessage(text, httpClient, rpcApi)
}
}
override fun onMessage(webSocket: WebSocket, bytes: ByteString) {
scope.launch {
handleMessage(bytes.utf8(), httpClient, rpcApi)
}
}
override fun onClosing(webSocket: WebSocket, code: Int, reason: String) {
isConnected = false
logger.warn("链上 WebSocket 连接关闭: code=$code, reason=$reason")
}
override fun onClosed(webSocket: WebSocket, code: Int, reason: String) {
isConnected = false
logger.warn("链上 WebSocket 连接已关闭: code=$code, reason=$reason")
}
override fun onFailure(webSocket: WebSocket, t: Throwable, response: okhttp3.Response?) {
logger.error("链上 WebSocket 连接失败: ${t.message}", t)
isConnected = false
}
})
}
/**
* 等待连接建立
*/
private suspend fun waitForConnect() {
var waited = 0L
val timeout = 15000L // 15秒超时
while (!isConnected && waited < timeout) {
delay(100)
waited += 100
}
if (!isConnected) {
logger.warn("WebSocket 连接超时,等待重连")
}
}
/**
* 等待连接断开
*/
private suspend fun waitForDisconnect() {
while (isConnected && scope.isActive) {
delay(1000)
}
}
/**
* 订阅地址为每个地址订阅 6 个事件
*/
private suspend fun subscribeAddress(subscription: SubscriptionInfo) {
if (webSocket == null || !isConnected) {
return
}
val address = subscription.address
val walletTopic = OnChainWsUtils.addressToTopic32(address)
val subscriptionId = subscription.subscriptionId
try {
// 订阅 USDC Transfer (from wallet)
subscribeLogs(OnChainWsUtils.USDC_CONTRACT, listOf(OnChainWsUtils.ERC20_TRANSFER_TOPIC, walletTopic), subscriptionId)
// 订阅 USDC Transfer (to wallet)
subscribeLogs(OnChainWsUtils.USDC_CONTRACT, listOf(OnChainWsUtils.ERC20_TRANSFER_TOPIC, null, walletTopic), subscriptionId)
// 订阅 ERC1155 TransferSingle (from wallet)
subscribeLogs(OnChainWsUtils.ERC1155_CONTRACT, listOf(OnChainWsUtils.ERC1155_TRANSFER_SINGLE_TOPIC, null, walletTopic), subscriptionId)
// 订阅 ERC1155 TransferSingle (to wallet)
subscribeLogs(OnChainWsUtils.ERC1155_CONTRACT, listOf(OnChainWsUtils.ERC1155_TRANSFER_SINGLE_TOPIC, null, null, walletTopic), subscriptionId)
// 订阅 ERC1155 TransferBatch (from wallet)
subscribeLogs(OnChainWsUtils.ERC1155_CONTRACT, listOf(OnChainWsUtils.ERC1155_TRANSFER_BATCH_TOPIC, null, walletTopic), subscriptionId)
// 订阅 ERC1155 TransferBatch (to wallet)
subscribeLogs(OnChainWsUtils.ERC1155_CONTRACT, listOf(OnChainWsUtils.ERC1155_TRANSFER_BATCH_TOPIC, null, null, walletTopic), subscriptionId)
logger.debug("已订阅地址: subscriptionId=$subscriptionId, address=$address")
} catch (e: Exception) {
logger.error("订阅地址失败: subscriptionId=$subscriptionId, address=$address, error=${e.message}", e)
}
}
/**
* 订阅日志
*/
private fun subscribeLogs(address: String, topics: List<String?>, subscriptionId: String) {
val ws = webSocket ?: return
val params = mapOf(
"address" to address.lowercase(),
"topics" to topics.filterNotNull()
)
val requestId = ++requestIdCounter
requestIdToSubscriptionId[requestId] = subscriptionId
val request = mapOf(
"jsonrpc" to "2.0",
"id" to requestId,
"method" to "eth_subscribe",
"params" to listOf("logs", params)
)
val message = gson.toJson(request)
ws.send(message)
}
/**
* 取消 RPC 订阅
*/
private fun unsubscribeRpc(rpcSubscriptionId: String) {
val ws = webSocket ?: return
val requestId = ++requestIdCounter
val request = mapOf(
"jsonrpc" to "2.0",
"id" to requestId,
"method" to "eth_unsubscribe",
"params" to listOf(rpcSubscriptionId)
)
val message = gson.toJson(request)
ws.send(message)
}
/**
* 处理 WebSocket 消息
*/
private suspend fun handleMessage(text: String, httpClient: OkHttpClient, rpcApi: EthereumRpcApi) {
try {
val message = gson.fromJson(text, JsonObject::class.java)
// 处理订阅响应
if (message.has("result") && message.has("id")) {
val requestId = message.get("id")?.asInt
val rpcSubscriptionId = message.get("result")?.asString
if (requestId != null && rpcSubscriptionId != null) {
val subscriptionId = requestIdToSubscriptionId.remove(requestId)
if (subscriptionId != null) {
// 保存 RPC subscriptionId 到订阅的映射
rpcSubscriptionIdToSubscriptionId[rpcSubscriptionId] = subscriptionId
logger.debug("订阅成功: subscriptionId=$subscriptionId, rpcSubscriptionId=$rpcSubscriptionId")
}
}
return
}
// 处理日志通知
if (message.has("params")) {
val params = message.getAsJsonObject("params")
val subscriptionIdParam = params.get("subscription")?.asString
val result = params.getAsJsonObject("result")
if (result != null) {
val txHash = result.get("transactionHash")?.asString
if (txHash != null && subscriptionIdParam != null) {
// 根据 RPC subscriptionId 找到对应的订阅
val subscriptionId = rpcSubscriptionIdToSubscriptionId[subscriptionIdParam]
if (subscriptionId != null) {
// 处理交易,分发给对应的订阅者
processTransactionForSubscription(txHash, subscriptionId, httpClient, rpcApi)
} else {
// 如果没有找到订阅,可能是新订阅还未建立映射,尝试处理所有订阅
processTransaction(txHash, httpClient, rpcApi)
}
}
}
}
} catch (e: Exception) {
logger.error("处理 WebSocket 消息失败: ${e.message}", e)
}
}
/**
* 处理交易为特定订阅
* 直接调用订阅的回调
*/
private suspend fun processTransactionForSubscription(
txHash: String,
subscriptionId: String,
httpClient: OkHttpClient,
rpcApi: EthereumRpcApi
) {
val subscription = subscriptions[subscriptionId] ?: return
try {
subscription.callback(txHash, httpClient, rpcApi)
} catch (e: Exception) {
logger.error("调用订阅回调失败: subscriptionId=$subscriptionId, txHash=$txHash, error=${e.message}", e)
}
}
/**
* 处理交易为所有订阅用于兼容
* 解析交易中的 Transfer 事件分发给所有订阅者
*/
private suspend fun processTransaction(txHash: String, httpClient: OkHttpClient, rpcApi: EthereumRpcApi) {
try {
// 获取交易 receipt
val receiptRequest = JsonRpcRequest(
method = "eth_getTransactionReceipt",
params = listOf(txHash)
)
val receiptResponse = rpcApi.call(receiptRequest)
if (!receiptResponse.isSuccessful || receiptResponse.body() == null) {
return
}
val receiptRpcResponse = receiptResponse.body()!!
if (receiptRpcResponse.error != null || receiptRpcResponse.result == null) {
return
}
// 使用 Gson 解析 receipt JSON
val receiptJson = receiptRpcResponse.result.asJsonObject
// 解析 receipt 中的 Transfer 日志
val logs = receiptJson.getAsJsonArray("logs") ?: return
val (erc20Transfers, erc1155Transfers) = OnChainWsUtils.parseReceiptTransfers(logs)
// 为每个订阅检查是否匹配,如果匹配则调用回调
for (subscription in subscriptions.values) {
val address = subscription.address
// 检查该地址是否参与了交易(通过检查 Transfer 日志)
val isInvolved = erc20Transfers.any {
it.from.lowercase() == address || it.to.lowercase() == address
} || erc1155Transfers.any {
it.from.lowercase() == address || it.to.lowercase() == address
}
if (isInvolved) {
// 该地址参与了交易,调用回调
try {
subscription.callback(txHash, httpClient, rpcApi)
} catch (e: Exception) {
logger.error("调用订阅回调失败: subscriptionId=${subscription.subscriptionId}, txHash=$txHash, error=${e.message}", e)
}
}
}
} catch (e: Exception) {
logger.error("处理交易失败: txHash=$txHash, ${e.message}", e)
}
}
/**
* 停止连接
*/
fun stop() {
for (connection in addressConnections.values) {
connection.stop()
}
addressConnections.clear()
connectionJob?.cancel()
connectionJob = null
// 关闭 WebSocket 连接
webSocket?.close(1000, "停止监听")
webSocket = null
isConnected = false
// 清空订阅信息
subscriptions.clear()
requestIdToSubscriptionId.clear()
rpcSubscriptionIdToSubscriptionId.clear()
}
/**
* 获取连接状态
* @return Map<address, isConnected>
*/
fun getConnectionStatuses(): Map<String, Boolean> {
return addressConnections.mapValues { (_, connection) -> connection.isConnected() }
}
@PostConstruct
fun init() {
logger.info("统一链上 WebSocket 服务已初始化 (独立连接模式)")
// 服务启动时不自动连接,等待有订阅时再连接
logger.info("统一链上 WebSocket 服务已初始化")
}
@PreDestroy
fun destroy() {
stop()
scope.cancel()
}
/**
* 单个地址的 WebSocket 连接管理
*/
inner class AddressWsConnection(val address: String) {
private var webSocket: WebSocket? = null
@Volatile
private var isConnected = false
// 订阅ID计数器(用于请求 ID
private var requestIdCounter = AtomicInteger(0)
// 连接任务
private var connectionJob: Job? = null
// 该连接下的所有订阅:subscriptionId -> SubscriptionInfo
// 理论上一个地址可能被多个业务订阅(如:既是被跟单者又是普通监控),虽然业务上通常只有一个
private val subscriptions = ConcurrentHashMap<String, SubscriptionInfo>()
// 存储请求 ID 到订阅 ID 的映射:requestId -> subscriptionId
private val requestIdToSubscriptionId = ConcurrentHashMap<Int, String>()
// 存储 RPC subscriptionId 到订阅 ID 的映射:rpcSubscriptionId -> subscriptionId
private val rpcSubscriptionIdToSubscriptionId = ConcurrentHashMap<String, String>()
fun start() {
if (connectionJob != null && connectionJob!!.isActive) return
connectionJob = scope.launch {
startConnectionLoop()
}
}
fun stop() {
connectionJob?.cancel()
connectionJob = null
webSocket?.close(1000, "停止监听")
webSocket = null
isConnected = false
subscriptions.clear()
requestIdToSubscriptionId.clear()
rpcSubscriptionIdToSubscriptionId.clear()
}
fun addSubscription(subscription: SubscriptionInfo) {
// 如果已经存在,先移除旧的
removeSubscription(subscription.subscriptionId)
subscriptions[subscription.subscriptionId] = subscription
// 如果已经连接,立即发送链上订阅请求
if (isConnected) {
scope.launch {
subscribeAddressOnChain(subscription)
}
}
}
fun removeSubscription(subscriptionId: String) {
subscriptions.remove(subscriptionId)
// 不需要显式发送 eth_unsubscribe,因为连接是 per-address 的,
// 只要只要连接还在,就保持该地址相关的所有 logs 订阅。
// 只有当所有 subscription 都移除了,连接才会关闭。
}
fun hasSubscription(subscriptionId: String): Boolean {
return subscriptions.containsKey(subscriptionId)
}
fun isSubscriptionsEmpty(): Boolean {
return subscriptions.isEmpty()
}
fun isConnected(): Boolean {
return isConnected
}
private suspend fun startConnectionLoop() {
while (scope.isActive) {
try {
if (subscriptions.isEmpty()) {
// 如果启动循环时还没订阅(不太可能,通常是先 addSubscription 再 start,或者是 start 后 addSubscription
// 或者订阅被清空了,外部应当掉 stop,但这里作为防守
delay(1000)
continue
}
if (isConnected && webSocket != null) {
waitForDisconnect()
continue
}
// 获取可用的 RPC 节点
val wsUrl = rpcNodeService.getWsUrl()
val httpUrl = rpcNodeService.getHttpUrl()
logger.info("[$address] 连接链上 WebSocket: $wsUrl")
val httpClient = createHttpClient()
val rpcApi = retrofitFactory.createEthereumRpcApi(httpUrl)
connectWebSocket(wsUrl, httpClient, rpcApi)
waitForConnect()
if (isConnected) {
logger.info("[$address] WebSocket 连接已建立,开始注册订阅")
// 重新为所有订阅注册链上监听
for (subscription in subscriptions.values) {
subscribeAddressOnChain(subscription)
}
waitForDisconnect()
}
logger.info("[$address] WebSocket 连接断开,等待 ${reconnectDelay}ms 后重连")
delay(reconnectDelay)
} catch (e: Exception) {
logger.error("[$address] 连接异常: ${e.message}", e)
delay(reconnectDelay)
}
}
}
private fun connectWebSocket(wsUrl: String, httpClient: OkHttpClient, rpcApi: EthereumRpcApi) {
webSocket?.close(1000, "重新连接")
webSocket = null
isConnected = false
val request = Request.Builder().url(wsUrl).build()
webSocket = httpClient.newWebSocket(request, object : WebSocketListener() {
override fun onOpen(webSocket: WebSocket, response: okhttp3.Response) {
isConnected = true
logger.info("[$address] 链上 WebSocket 连接成功")
}
override fun onMessage(webSocket: WebSocket, text: String) {
scope.launch { handleMessage(text, httpClient, rpcApi) }
}
override fun onMessage(webSocket: WebSocket, bytes: ByteString) {
scope.launch { handleMessage(bytes.utf8(), httpClient, rpcApi) }
}
override fun onClosing(webSocket: WebSocket, code: Int, reason: String) {
isConnected = false
logger.warn("[$address] 链上 WebSocket 连接关闭: code=$code, reason=$reason")
}
override fun onClosed(webSocket: WebSocket, code: Int, reason: String) {
isConnected = false
logger.warn("[$address] 链上 WebSocket 连接已关闭: code=$code, reason=$reason")
}
override fun onFailure(webSocket: WebSocket, t: Throwable, response: okhttp3.Response?) {
logger.error("[$address] 链上 WebSocket 连接失败: ${t.message}", t)
isConnected = false
}
})
}
private suspend fun subscribeAddressOnChain(subscription: SubscriptionInfo) {
if (webSocket == null || !isConnected) return
val walletTopic = OnChainWsUtils.addressToTopic32(address)
val subId = subscription.subscriptionId
try {
// 订阅该地址相关的所有事件
// USDC Transfer (from/to)
subscribeLogs(OnChainWsUtils.USDC_CONTRACT, listOf(OnChainWsUtils.ERC20_TRANSFER_TOPIC, walletTopic), subId)
subscribeLogs(OnChainWsUtils.USDC_CONTRACT, listOf(OnChainWsUtils.ERC20_TRANSFER_TOPIC, null, walletTopic), subId)
// ERC1155 TransferSingle (from/to)
subscribeLogs(OnChainWsUtils.ERC1155_CONTRACT, listOf(OnChainWsUtils.ERC1155_TRANSFER_SINGLE_TOPIC, null, walletTopic), subId)
subscribeLogs(OnChainWsUtils.ERC1155_CONTRACT, listOf(OnChainWsUtils.ERC1155_TRANSFER_SINGLE_TOPIC, null, null, walletTopic), subId)
// ERC1155 TransferBatch (from/to)
subscribeLogs(OnChainWsUtils.ERC1155_CONTRACT, listOf(OnChainWsUtils.ERC1155_TRANSFER_BATCH_TOPIC, null, walletTopic), subId)
subscribeLogs(OnChainWsUtils.ERC1155_CONTRACT, listOf(OnChainWsUtils.ERC1155_TRANSFER_BATCH_TOPIC, null, null, walletTopic), subId)
logger.debug("[$address] 已发送链上订阅请求: subscriptionId=$subId")
} catch (e: Exception) {
logger.error("[$address] 发送链上订阅请求失败: error=${e.message}", e)
}
}
private fun subscribeLogs(contractAddress: String, topics: List<String?>, subscriptionId: String) {
val ws = webSocket ?: return
val topicsArray = gson.toJsonTree(topics).asJsonArray
val logParams = JsonObject()
logParams.addProperty("address", contractAddress.lowercase())
logParams.add("topics", topicsArray)
val requestId = requestIdCounter.incrementAndGet()
requestIdToSubscriptionId[requestId] = subscriptionId
val request = JsonObject()
request.addProperty("jsonrpc", "2.0")
request.addProperty("id", requestId)
request.addProperty("method", "eth_subscribe")
val paramsArray = JsonArray()
paramsArray.add("logs")
paramsArray.add(logParams)
request.add("params", paramsArray)
ws.send(gson.toJson(request))
}
private suspend fun handleMessage(text: String, httpClient: OkHttpClient, rpcApi: EthereumRpcApi) {
try {
val message = gson.fromJson(text, JsonObject::class.java)
// 1. 处理订阅响应 (eth_subscribe response)
if (message.has("result") && message.has("id")) {
val requestId = message.get("id")?.asInt
val rpcSubscriptionId = message.get("result")?.asString
if (requestId != null && rpcSubscriptionId != null) {
val subId = requestIdToSubscriptionId.remove(requestId)
if (subId != null) {
rpcSubscriptionIdToSubscriptionId[rpcSubscriptionId] = subId
logger.debug("[$address] 链上订阅成功: mapped connection rpcSubId=$rpcSubscriptionId to localSubId=$subId")
}
}
return
}
// 2. 处理日志通知 (eth_subscription)
val method = message.get("method")?.asString
if (method == "eth_subscription") {
val params = message.getAsJsonObject("params") ?: return
val rpcSubParam = params.get("subscription")?.asString
val result = params.getAsJsonObject("result") ?: return
val txHash = result.get("transactionHash")?.asString
if (txHash != null && rpcSubParam != null) {
// 找到触发此通知的本地订阅 ID
// 因为我们在这个连接里只订阅了 this.address,所以理论上所有通知都跟这个 address 有关
// 但我们需要找到对应的 callback
val localSubId = rpcSubscriptionIdToSubscriptionId[rpcSubParam]
if (localSubId != null) {
val subscription = subscriptions[localSubId]
if (subscription != null) {
logger.info("[$address] 收到交易通知: txHash=$txHash, subId=$localSubId")
runCatching {
subscription.callback(txHash, httpClient, rpcApi)
}.onFailure { e ->
logger.error("[$address] 回调执行失败: ${e.message}", e)
}
}
} else {
// 找不到具体是哪个订阅请求触发的(可能是重启后之前的订阅残留?或者映射丢失?)
// 在单地址单连接模式下,只要是这个 connection 收到的,肯定是关于这个 address 的
// 我们可以尝试通知所有订阅者(通常一个地址只有一个订阅者,除非此地址既是Leader又是User)
logger.warn("[$address] 未找到映射的订阅ID: rpcSubId=$rpcSubParam. 广播给所有订阅者.")
subscriptions.values.forEach { sub ->
runCatching {
sub.callback(txHash, httpClient, rpcApi)
}.onFailure { e ->
logger.error("[$address] 广播回调执行失败: ${e.message}", e)
}
}
}
}
}
} catch (e: Exception) {
logger.error("[$address] 处理消息失败: ${e.message}", e)
}
}
private suspend fun waitForConnect() {
var waited = 0L
val timeout = 15000L
while (!isConnected && waited < timeout) {
delay(100)
waited += 100
}
if (!isConnected) logger.warn("[$address] WebSocket 连接超时")
}
private suspend fun waitForDisconnect() {
while (isConnected && scope.isActive) {
delay(1000)
}
}
private fun createHttpClient(): OkHttpClient {
val proxy = getProxyConfig()
val builder = createClient()
if (proxy != null) builder.proxy(proxy)
return builder.build()
}
}
}
@@ -1,6 +1,7 @@
package com.wrbug.polymarketbot.service.copytrading.orders
import com.fasterxml.jackson.databind.ObjectMapper
import com.wrbug.polymarketbot.api.MarketResponse
import com.wrbug.polymarketbot.dto.OrderDetailDto
import com.wrbug.polymarketbot.dto.OrderMessageDto
import com.wrbug.polymarketbot.dto.OrderPushMessage
@@ -18,8 +19,6 @@ import com.wrbug.polymarketbot.util.CryptoUtils
import com.wrbug.polymarketbot.repository.CopyOrderTrackingRepository
import com.wrbug.polymarketbot.repository.CopyTradingRepository
import com.wrbug.polymarketbot.repository.LeaderRepository
import com.wrbug.polymarketbot.constants.PolymarketConstants
import com.wrbug.polymarketbot.service.common.MarketService
import org.springframework.stereotype.Service
import java.util.concurrent.ConcurrentHashMap
@@ -36,13 +35,13 @@ class OrderPushService(
private val cryptoUtils: CryptoUtils,
private val copyOrderTrackingRepository: CopyOrderTrackingRepository? = null, // 可选,避免循环依赖
private val copyTradingRepository: CopyTradingRepository? = null, // 可选,避免循环依赖
private val leaderRepository: LeaderRepository? = null, // 可选,避免循环依赖
private val marketService: MarketService // 市场信息服务
private val leaderRepository: LeaderRepository? = null // 可选,避免循环依赖
) {
private val logger = LoggerFactory.getLogger(OrderPushService::class.java)
private val polymarketWsUrl: String = PolymarketConstants.RTDS_WS_URL
@Value("\${polymarket.rtds.ws-url}")
private lateinit var polymarketWsUrl: String
// 存储账户 ID 和对应的 WebSocket 连接
private val accountConnections = ConcurrentHashMap<Long, PolymarketWebSocketClient>()
@@ -423,8 +422,8 @@ class OrderPushService(
result.fold(
onSuccess = { openOrder ->
// 获取市场信息(使用 MarketService,优先从数据库/缓存获取
val market = marketService.getMarket(conditionId ?: openOrder.market)
// 获取市场信息(通过 Gamma API
val marketInfo = fetchMarketInfo(conditionId ?: openOrder.market)
// 转换为 DTO
// 注意:createdAt 是 unix timestamp (Long),需要转换为字符串
@@ -437,9 +436,9 @@ class OrderPushService(
filled = openOrder.sizeMatched, // 使用 size_matched
status = openOrder.status,
createdAt = openOrder.createdAt.toString(), // unix timestamp 转换为字符串
marketName = market?.title,
marketSlug = market?.slug, // 显示用的 slug
marketIcon = market?.icon
marketName = marketInfo?.question,
marketSlug = marketInfo?.slug,
marketIcon = marketInfo?.icon
)
},
onFailure = { e ->
@@ -453,6 +452,40 @@ class OrderPushService(
}
}
/**
* 获取市场信息通过 Gamma API
* 文档: https://docs.polymarket.com/api-reference/markets/list-markets
*
* 使用 /markets 接口通过 condition_ids 查询参数获取市场信息
* 订单返回的 market 字段是 16 进制的 condition ID "0x..."
*/
private suspend fun fetchMarketInfo(conditionId: String): MarketResponse? {
return try {
// 创建 Gamma API 客户端(公开 API,不需要认证)
val gammaApi = retrofitFactory.createGammaApi()
// 调用 Gamma API 获取市场信息
// 使用 /markets 接口,通过 condition_ids 查询参数
val response = gammaApi.listMarkets(
conditionIds = listOf(conditionId),
includeTag = null
)
if (response.isSuccessful && response.body() != null) {
val markets = response.body()!!
if (markets.isNotEmpty()) {
val market = markets.first()
return market
} else {
return null
}
} else {
null
}
} catch (e: Exception) {
null
}
}
/**
* 订阅账户的订单推送保留用于向后兼容
*/
@@ -19,14 +19,12 @@ import com.wrbug.polymarketbot.service.copytrading.configs.CopyTradingFilterServ
import com.wrbug.polymarketbot.service.copytrading.configs.FilterStatus
import com.wrbug.polymarketbot.service.copytrading.orders.OrderSigningService
import com.wrbug.polymarketbot.service.common.BlockchainService
import com.wrbug.polymarketbot.service.common.MarketService
import com.wrbug.polymarketbot.service.common.PolymarketClobService
import com.wrbug.polymarketbot.service.system.TelegramNotificationService
import com.wrbug.polymarketbot.util.CryptoUtils
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.math.BigDecimal
import kotlin.math.max
/**
* 订单跟踪服务
@@ -49,7 +47,6 @@ open class CopyOrderTrackingService(
private val clobService: PolymarketClobService,
private val retrofitFactory: RetrofitFactory,
private val cryptoUtils: CryptoUtils,
private val marketService: MarketService, // 市场信息服务
private val telegramNotificationService: TelegramNotificationService? = null // 可选,避免循环依赖
) {
@@ -57,16 +54,16 @@ open class CopyOrderTrackingService(
// 协程作用域(用于异步发送通知)
private val notificationScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
// 使用 Mutex 保证线程安全(按交易ID锁定)
private val tradeMutexMap = ConcurrentHashMap<String, Mutex>()
// 订单创建重试配置
companion object {
private const val MAX_RETRY_ATTEMPTS = 2 // 最多重试次数(首次 + 1次重试)
private const val RETRY_DELAY_MS = 3000L // 重试前等待时间(毫秒,3秒)
}
/**
* 获取或创建 Mutex按交易ID
*/
@@ -124,86 +121,85 @@ open class CopyOrderTrackingService(
suspend fun processTrade(leaderId: Long, trade: TradeResponse, source: String): Result<Unit> {
// 获取该交易的 Mutex(按交易ID锁定,不同交易可以并行处理)
val mutex = getMutex(leaderId, trade.id)
logger.debug("processTrade: ${trade.id}, $source")
return mutex.withLock {
try {
// 1. 检查是否已处理(去重,包括失败状态)
val existingProcessed = processedTradeRepository.findByLeaderIdAndLeaderTradeId(leaderId, trade.id)
// 1. 检查是否已处理(去重,包括失败状态)
val existingProcessed = processedTradeRepository.findByLeaderIdAndLeaderTradeId(leaderId, trade.id)
if (existingProcessed != null) {
logger.debug("processTrade: 重复 ${trade.id}, $source")
if (existingProcessed.status == "FAILED") {
if (existingProcessed != null) {
if (existingProcessed.status == "FAILED") {
return@withLock Result.success(Unit)
}
}
return@withLock Result.success(Unit)
}
}
// 2. 处理交易逻辑
val result = when (trade.side.uppercase()) {
"BUY" -> processBuyTrade(leaderId, trade, source)
"SELL" -> processSellTrade(leaderId, trade)
else -> {
logger.warn("未知的交易方向: ${trade.side}")
Result.failure(IllegalArgumentException("未知的交易方向: ${trade.side}"))
}
// 2. 处理交易逻辑
val result = when (trade.side.uppercase()) {
"BUY" -> processBuyTrade(leaderId, trade)
"SELL" -> processSellTrade(leaderId, trade)
else -> {
logger.warn("未知的交易方向: ${trade.side}")
Result.failure(IllegalArgumentException("未知的交易方向: ${trade.side}"))
}
}
if (result.isFailure) {
logger.error(
"处理交易失败: leaderId=$leaderId, tradeId=${trade.id}, side=${trade.side}",
result.exceptionOrNull()
)
if (result.isFailure) {
logger.error(
"处理交易失败: leaderId=$leaderId, tradeId=${trade.id}, side=${trade.side}",
result.exceptionOrNull()
)
return@withLock result
}
}
// 3. 标记为已处理(成功状态)
// 3. 标记为已处理(成功状态)
// 由于使用了 Mutex,这里理论上不会出现并发冲突,但保留异常处理作为兜底
try {
val processed = ProcessedTrade(
leaderId = leaderId,
leaderTradeId = trade.id,
tradeType = trade.side.uppercase(),
source = source,
status = "SUCCESS",
processedAt = System.currentTimeMillis()
)
processedTradeRepository.save(processed)
} catch (e: Exception) {
// 检查是否是唯一键冲突异常(理论上不会发生,但保留作为兜底)
if (isUniqueConstraintViolation(e)) {
val existing = processedTradeRepository.findByLeaderIdAndLeaderTradeId(leaderId, trade.id)
if (existing != null) {
if (existing.status == "FAILED") {
logger.debug("交易已标记为失败,跳过处理: leaderId=$leaderId, tradeId=${trade.id}")
return@withLock Result.success(Unit)
}
logger.debug("交易已处理(并发检测): leaderId=$leaderId, tradeId=${trade.id}, status=${existing.status}")
return@withLock Result.success(Unit)
} else {
// 如果检查不到,可能是事务隔离级别问题,等待一下再查询
delay(100)
val existingAfterDelay =
processedTradeRepository.findByLeaderIdAndLeaderTradeId(leaderId, trade.id)
if (existingAfterDelay != null) {
logger.debug("延迟查询到记录(并发检测): leaderId=$leaderId, tradeId=${trade.id}, status=${existingAfterDelay.status}")
return@withLock Result.success(Unit)
}
logger.warn(
"保存ProcessedTrade时发生唯一约束冲突,但查询不到记录: leaderId=$leaderId, tradeId=${trade.id}",
e
)
return@withLock Result.success(Unit)
}
} else {
// 其他类型的异常,重新抛出
throw e
}
}
Result.success(Unit)
try {
val processed = ProcessedTrade(
leaderId = leaderId,
leaderTradeId = trade.id,
tradeType = trade.side.uppercase(),
source = source,
status = "SUCCESS",
processedAt = System.currentTimeMillis()
)
processedTradeRepository.save(processed)
} catch (e: Exception) {
logger.error("处理交易异常: leaderId=$leaderId, tradeId=${trade.id}", e)
Result.failure(e)
// 检查是否是唯一键冲突异常(理论上不会发生,但保留作为兜底)
if (isUniqueConstraintViolation(e)) {
val existing = processedTradeRepository.findByLeaderIdAndLeaderTradeId(leaderId, trade.id)
if (existing != null) {
if (existing.status == "FAILED") {
logger.debug("交易已标记为失败,跳过处理: leaderId=$leaderId, tradeId=${trade.id}")
return@withLock Result.success(Unit)
}
logger.debug("交易已处理(并发检测): leaderId=$leaderId, tradeId=${trade.id}, status=${existing.status}")
return@withLock Result.success(Unit)
} else {
// 如果检查不到,可能是事务隔离级别问题,等待一下再查询
delay(100)
val existingAfterDelay =
processedTradeRepository.findByLeaderIdAndLeaderTradeId(leaderId, trade.id)
if (existingAfterDelay != null) {
logger.debug("延迟查询到记录(并发检测): leaderId=$leaderId, tradeId=${trade.id}, status=${existingAfterDelay.status}")
return@withLock Result.success(Unit)
}
logger.warn(
"保存ProcessedTrade时发生唯一约束冲突,但查询不到记录: leaderId=$leaderId, tradeId=${trade.id}",
e
)
return@withLock Result.success(Unit)
}
} else {
// 其他类型的异常,重新抛出
throw e
}
}
Result.success(Unit)
} catch (e: Exception) {
logger.error("处理交易异常: leaderId=$leaderId, tradeId=${trade.id}", e)
Result.failure(e)
}
}
}
@@ -213,7 +209,7 @@ open class CopyOrderTrackingService(
* 创建跟单买入订单并记录到跟踪表
*/
@Transactional
suspend fun processBuyTrade(leaderId: Long, trade: TradeResponse, source: String): Result<Unit> {
suspend fun processBuyTrade(leaderId: Long, trade: TradeResponse): Result<Unit> {
return try {
// 1. 查找所有启用且支持该Leader的跟单关系
val copyTradings = copyTradingRepository.findByLeaderIdAndEnabledTrue(leaderId)
@@ -257,46 +253,26 @@ open class CopyOrderTrackingService(
// 先计算跟单金额(用于仓位检查)
// 注意:这里先计算金额,即使后续被过滤也会记录
val tradePrice = trade.price.toSafeBigDecimal()
var buyQuantity = try {
val buyQuantity = try {
calculateBuyQuantity(trade, copyTrading)
} catch (e: Exception) {
logger.warn("计算买入数量失败: ${e.message}", e)
continue
}
// 计算跟单金额(USDC)= 买入数量 × 价格
val copyOrderAmount = buyQuantity.multi(tradePrice)
// 如果启用了关键字过滤或市场截止时间过滤,需要先获取市场信息
var marketTitle: String? = null
var marketEndDate: Long? = null
val needMarketInfo =
copyTrading.keywordFilterMode != "DISABLED" || copyTrading.maxMarketEndDate != null
if (needMarketInfo) {
try {
val market = marketService.getMarket(trade.market)
marketTitle = market?.title
marketEndDate = market?.endDate
} catch (e: Exception) {
logger.warn("获取市场信息失败(关键字过滤/市场截止时间检查需要): ${e.message}", e)
}
}
// 过滤条件检查(在计算订单参数之前)
// 传入 Leader 交易价格,用于价格区间检查
// 传入跟单金额和市场ID,用于仓位检查(按市场检查仓位)
// 传入市场标题,用于关键字过滤
// 传入市场截止时间,用于市场截止时间检查
// 订单簿只请求一次,返回给后续逻辑使用
val filterResult = filterService.checkFilters(
copyTrading,
tokenId,
copyTrading,
tokenId,
tradePrice = tradePrice,
copyOrderAmount = copyOrderAmount,
marketId = trade.market,
marketTitle = marketTitle,
marketEndDate = marketEndDate
marketId = trade.market
)
val orderbook = filterResult.orderbook // 获取订单簿(如果需要)
if (!filterResult.isPassed) {
@@ -306,9 +282,23 @@ open class CopyOrderTrackingService(
notificationScope.launch {
try {
// 获取市场信息(标题和slug
val market = marketService.getMarket(trade.market)
val marketTitle = market?.title ?: trade.market
val marketSlug = market?.slug // 显示用的 slug
val marketInfo = withContext(Dispatchers.IO) {
try {
val gammaApi = retrofitFactory.createGammaApi()
val marketResponse = gammaApi.listMarkets(conditionIds = listOf(trade.market))
if (marketResponse.isSuccessful && marketResponse.body() != null) {
marketResponse.body()!!.firstOrNull()
} else {
null
}
} catch (e: Exception) {
logger.warn("获取市场信息失败: ${e.message}", e)
null
}
}
val marketTitle = marketInfo?.question ?: trade.market
val marketSlug = marketInfo?.slug
// 从过滤结果中提取 filterType
val filterType = extractFilterType(filterResult.status, filterResult.reason)
@@ -347,29 +337,27 @@ open class CopyOrderTrackingService(
logger.error("保存被过滤订单失败: ${e.message}", e)
}
// 发送 Telegram 通知(仅在 pushFilteredOrders 为 true 时发送)
if (copyTrading.pushFilteredOrders) {
val locale = try {
org.springframework.context.i18n.LocaleContextHolder.getLocale()
} catch (e: Exception) {
java.util.Locale("zh", "CN") // 默认简体中文
}
telegramNotificationService?.sendOrderFilteredNotification(
marketTitle = marketTitle,
marketId = trade.market,
marketSlug = marketSlug,
side = "BUY",
outcome = trade.outcome,
price = trade.price,
size = trade.size,
filterReason = filterResult.reason,
filterType = filterType,
accountName = account.accountName,
walletAddress = account.walletAddress,
locale = locale
)
// 发送 Telegram 通知
val locale = try {
org.springframework.context.i18n.LocaleContextHolder.getLocale()
} catch (e: Exception) {
java.util.Locale("zh", "CN") // 默认简体中文
}
telegramNotificationService?.sendOrderFilteredNotification(
marketTitle = marketTitle,
marketId = trade.market,
marketSlug = marketSlug,
side = "BUY",
outcome = trade.outcome,
price = trade.price,
size = trade.size,
filterReason = filterResult.reason,
filterType = filterType,
accountName = account.accountName,
walletAddress = account.walletAddress,
locale = locale
)
} catch (e: Exception) {
logger.error("处理被过滤订单通知失败: ${e.message}", e)
}
@@ -381,72 +369,22 @@ open class CopyOrderTrackingService(
// 买入数量已在过滤检查前计算,这里直接使用
// 如果数量为0或负数,跳过
if (buyQuantity.lte(BigDecimal.ZERO)) {
logger.warn("计算得到的买入数量为0,跳过跟单: copyTradingId=${copyTrading.id}, tradeId=${trade.id}")
logger.warn("计算的买入数量为0或负数,跳过: copyTradingId=${copyTrading.id}, tradeId=${trade.id}")
continue
}
if (buyQuantity.lt(BigDecimal.ONE)) {
logger.warn("计算得到的买入数量小于1,自动调整为1 (Polymarket 最小下单数量): copyTradingId=${copyTrading.id}, tradeId=${trade.id}, originalQuantity=$buyQuantity")
buyQuantity = BigDecimal.ONE
}
// 验证订单数量限制(仅比例模式)
var finalBuyQuantity = buyQuantity
if (copyTrading.copyMode == "RATIO") {
val tradePrice = trade.price.toSafeBigDecimal()
val rawOrderAmount = buyQuantity.multi(tradePrice)
// 对按比例计算的金额进行向上取整处理(确保满足最小限制)
// 向上取整到 2 位小数(USDC 精度)
val roundedOrderAmount = rawOrderAmount.setScale(2, java.math.RoundingMode.CEILING)
// 如果原始金额或向上取整后的金额小于最小限制,调整 buyQuantity 以满足最小限制
// 这样可以避免精度问题导致订单被错误地跳过
if (roundedOrderAmount.lt(copyTrading.minOrderSize)) {
logger.debug("订单金额(向上取整后)低于最小限制,调整数量以满足最小限制: copyTradingId=${copyTrading.id}, rawAmount=$rawOrderAmount, roundedAmount=$roundedOrderAmount, min=${copyTrading.minOrderSize}")
// 计算满足最小限制所需的数量(向上取整)
val minQuantity =
copyTrading.minOrderSize.div(tradePrice, 8, java.math.RoundingMode.CEILING)
if (minQuantity.lte(BigDecimal.ZERO)) {
logger.warn("计算出的最小数量为0或负数,跳过: copyTradingId=${copyTrading.id}")
continue
}
// 使用调整后的数量
finalBuyQuantity = minQuantity
logger.debug(
"已调整数量以满足最小限制: copyTradingId=${copyTrading.id}, originalQuantity=$buyQuantity, adjustedQuantity=$finalBuyQuantity, adjustedAmount=${
finalBuyQuantity.multi(
tradePrice
)
}"
)
} else if (rawOrderAmount.lt(copyTrading.minOrderSize)) {
// 原始金额小于最小限制,但向上取整后满足,调整数量以满足最小限制
logger.debug("订单金额(精度处理后)低于最小限制,调整数量以满足最小限制: copyTradingId=${copyTrading.id}, rawAmount=$rawOrderAmount, min=${copyTrading.minOrderSize}")
// 计算满足最小限制所需的数量(向上取整)
val minQuantity =
copyTrading.minOrderSize.div(tradePrice, 8, java.math.RoundingMode.CEILING)
if (minQuantity.lte(BigDecimal.ZERO)) {
logger.warn("计算出的最小数量为0或负数,跳过: copyTradingId=${copyTrading.id}")
continue
}
// 使用调整后的数量
finalBuyQuantity = minQuantity
logger.debug(
"已调整数量以满足最小限制: copyTradingId=${copyTrading.id}, originalQuantity=$buyQuantity, adjustedQuantity=$finalBuyQuantity, adjustedAmount=${
finalBuyQuantity.multi(
tradePrice
)
}"
)
val orderAmount = buyQuantity.multi(trade.price.toSafeBigDecimal())
if (orderAmount.lt(copyTrading.minOrderSize)) {
logger.warn("订单金额低于最小限制,跳过: copyTradingId=${copyTrading.id}, amount=$orderAmount, min=${copyTrading.minOrderSize}")
continue
}
// 检查最大限制(使用调整后的数量)
val finalOrderAmount = finalBuyQuantity.multi(tradePrice)
if (finalOrderAmount.gt(copyTrading.maxOrderSize)) {
logger.warn("订单金额超过最大限制,调整数量: copyTradingId=${copyTrading.id}, amount=$finalOrderAmount, max=${copyTrading.maxOrderSize}")
if (orderAmount.gt(copyTrading.maxOrderSize)) {
logger.warn("订单金额超过最大限制,调整数量: copyTradingId=${copyTrading.id}, amount=$orderAmount, max=${copyTrading.maxOrderSize}")
// 调整数量到最大值
val adjustedQuantity =
copyTrading.maxOrderSize.div(tradePrice, 8, java.math.RoundingMode.DOWN)
val adjustedQuantity = copyTrading.maxOrderSize.div(trade.price.toSafeBigDecimal())
if (adjustedQuantity.lte(BigDecimal.ZERO)) {
logger.warn("调整后的数量为0或负数,跳过: copyTradingId=${copyTrading.id}")
continue
@@ -471,7 +409,7 @@ open class CopyOrderTrackingService(
// 计算价格(应用价格容忍度)
val buyPrice = calculateAdjustedPrice(trade.price.toSafeBigDecimal(), copyTrading, isBuy = true)
logger.debug("计算价格结果:$buyPrice")
// 在创建订单前,检查订单簿中是否有可匹配的订单(避免 FAK 订单失败)
// 如果过滤检查时已经获取了订单簿,直接使用;否则重新获取
val orderbookForCheck = orderbook ?: run {
@@ -482,21 +420,21 @@ open class CopyOrderTrackingService(
null
}
}
// 检查是否有可匹配的卖单(asks)
if (orderbookForCheck != null) {
val bestAsk = orderbookForCheck.asks
.mapNotNull { it.price.toSafeBigDecimal() }
.minOrNull()
if (bestAsk == null) {
logger.warn("订单簿中没有卖单,跳过创建订单: copyTradingId=${copyTrading.id}, tradeId=${trade.id}")
continue
}
// 如果调整后的买入价格低于最佳卖单价格,无法匹配
if (buyPrice.lt(bestAsk)) {
logger.info("调整后的买入价格 ($buyPrice) 低于最佳卖单价格 ($bestAsk),无法匹配,跳过创建订单: copyTradingId=${copyTrading.id}, tradeId=${trade.id}, leaderPrice=${trade.price}, tolerance=${copyTrading.priceTolerance}")
logger.warn("调整后的买入价格 ($buyPrice) 低于最佳卖单价格 ($bestAsk),无法匹配,跳过创建订单: copyTradingId=${copyTrading.id}, tradeId=${trade.id}, leaderPrice=${trade.price}, tolerance=${copyTrading.priceTolerance}")
continue
}
}
@@ -526,17 +464,6 @@ open class CopyOrderTrackingService(
// 解密私钥
val decryptedPrivateKey = decryptPrivateKey(account)
// 获取费率(根据 Polymarket Maker Rebates Program 要求)
val feeRateResult = clobService.getFeeRate(tokenId)
val feeRateBps = if (feeRateResult.isSuccess) {
feeRateResult.getOrNull()?.toString() ?: "0"
} else {
logger.warn("获取费率失败,使用默认值 0: tokenId=$tokenId, error=${feeRateResult.exceptionOrNull()?.message}")
"0"
}
logger.info("准备创建买入订单: copyTradingId=${copyTrading.id}, tradeId=${trade.id}, leaderPrice=${trade.price}, tolerance=${copyTrading.priceTolerance}, calculatedPrice=$buyPrice, quantity=$finalBuyQuantity, baseFee=$feeRateBps")
// 调用API创建订单(带重试机制)
// 重试策略:最多重试 MAX_RETRY_ATTEMPTS 次,每次重试前等待 RETRY_DELAY_MS 毫秒
// 每次重试都会重新生成salt并重新签名,确保签名唯一性
@@ -550,24 +477,37 @@ open class CopyOrderTrackingService(
size = finalBuyQuantity.toString(),
owner = account.apiKey,
copyTradingId = copyTrading.id!!,
tradeId = trade.id,
feeRateBps = feeRateBps
tradeId = trade.id
)
// 处理订单创建失败
if (createOrderResult.isFailure) {
// 提取错误信息(只保留 code 和 errorBody
val exception = createOrderResult.exceptionOrNull()
logger.error("创建买入订单失败: copyTradingId=${copyTrading.id}, tradeId=${trade.id}, leaderPrice=${trade.price}, myPrice=$buyPrice, error=${exception?.message}")
// 发送订单失败通知(异步,不阻塞,仅在 pushFailedOrders 为 true 时发送)
if (copyTrading.pushFailedOrders) {
notificationScope.launch {
try {
// 获取市场信息(标题和slug
val market = marketService.getMarket(trade.market)
val marketTitle = market?.title ?: trade.market
val marketSlug = market?.eventSlug // 跳转用的 slug
val marketInfo = withContext(Dispatchers.IO) {
try {
val gammaApi = retrofitFactory.createGammaApi()
val marketResponse =
gammaApi.listMarkets(conditionIds = listOf(trade.market))
if (marketResponse.isSuccessful && marketResponse.body() != null) {
marketResponse.body()!!.firstOrNull()
} else {
null
}
} catch (e: Exception) {
logger.warn("获取市场信息失败: ${e.message}", e)
null
}
}
val marketTitle = marketInfo?.question ?: trade.market
val marketSlug = marketInfo?.slug
// 获取当前语言设置(从 LocaleContextHolder
val locale = try {
@@ -599,7 +539,7 @@ open class CopyOrderTrackingService(
}
val realOrderId = createOrderResult.getOrNull() ?: continue
// 验证 orderId 格式(必须以 0x 开头的 16 进制)
if (!isValidOrderId(realOrderId)) {
logger.warn("买入订单ID格式无效,跳过保存: orderId=$realOrderId")
@@ -622,8 +562,7 @@ open class CopyOrderTrackingService(
price = buyPrice, // 使用下单价格,临时值
remainingQuantity = finalBuyQuantity,
status = "filled",
notificationSent = false, // 标记为未发送通知,等待轮询任务获取实际数据后发送
source = source // 订单来源
notificationSent = false // 标记为未发送通知,等待轮询任务获取实际数据后发送
)
copyOrderTrackingRepository.save(tracking)
@@ -686,8 +625,8 @@ open class CopyOrderTrackingService(
private fun calculateBuyQuantity(trade: TradeResponse, copyTrading: CopyTrading): BigDecimal {
return when (copyTrading.copyMode) {
"RATIO" -> {
// 比例模式:Leader 数量 × (比例 / 100)
trade.size.toSafeBigDecimal().multi(copyTrading.copyRatio.div(100))
// 比例模式:Leader 数量 × 比例
trade.size.toSafeBigDecimal().multi(copyTrading.copyRatio)
}
"FIXED" -> {
@@ -719,7 +658,7 @@ open class CopyOrderTrackingService(
val leader = leaderRepository.findById(copyTrading.leaderId).orElse(null)
?: run {
logger.warn("Leader 不存在,使用默认比例: leaderId=${copyTrading.leaderId}")
return leaderSellQuantity.multi(copyTrading.copyRatio.div(100))
return leaderSellQuantity.multi(copyTrading.copyRatio)
}
// 创建不需要认证的 CLOB API 客户端(用于查询公开的交易数据)
@@ -738,7 +677,7 @@ open class CopyOrderTrackingService(
for (order in unmatchedOrders) {
val copyQty = order.quantity.toSafeBigDecimal()
var leaderQty: BigDecimal? = null
// 优先使用存储的 leaderBuyQuantity
if (order.leaderBuyQuantity != null) {
leaderQty = order.leaderBuyQuantity.toSafeBigDecimal()
@@ -749,7 +688,7 @@ open class CopyOrderTrackingService(
logger.debug("Leader 买入数量未存储,尝试查询 API: leaderBuyTradeId=${order.leaderBuyTradeId}, copyOrderId=${order.buyOrderId}")
try {
val tradesResponse = clobApi.getTrades(id = order.leaderBuyTradeId)
if (tradesResponse.isSuccessful && tradesResponse.body() != null) {
val tradesData = tradesResponse.body()!!.data
if (tradesData.isNotEmpty()) {
@@ -775,7 +714,7 @@ open class CopyOrderTrackingService(
failCount++
}
}
// 如果成功获取到 Leader 买入数量,累加
if (leaderQty != null && leaderQty.gt(BigDecimal.ZERO)) {
totalCopyQuantity = totalCopyQuantity.add(copyQty)
@@ -784,23 +723,23 @@ open class CopyOrderTrackingService(
logger.warn("无法获取 Leader 买入数量,跳过该订单: copyOrderId=${order.buyOrderId}, leaderBuyTradeId=${order.leaderBuyTradeId}")
}
}
logger.info("固定金额模式计算结果汇总: copyTradingId=${copyTrading.id}, successCount=$successCount, failCount=$failCount, totalCopyQuantity=$totalCopyQuantity, totalLeaderQuantity=$totalLeaderQuantity")
// 如果无法计算总比例(查询失败),使用默认比例
if (totalLeaderQuantity.lte(BigDecimal.ZERO)) {
logger.warn("无法计算总比例(Leader 买入数量为 0),使用默认比例: copyTradingId=${copyTrading.id}")
return leaderSellQuantity.multi(copyTrading.copyRatio.div(100))
return leaderSellQuantity.multi(copyTrading.copyRatio)
}
// 计算实际比例:跟单买入数量 / Leader 买入数量
val actualRatio = totalCopyQuantity.div(totalLeaderQuantity)
// 计算需要卖出的数量:Leader 卖出数量 × 实际比例
val needMatch = leaderSellQuantity.multi(actualRatio)
logger.debug("固定金额模式卖出数量计算: copyTradingId=${copyTrading.id}, leaderSellQuantity=$leaderSellQuantity, totalCopyQuantity=$totalCopyQuantity, totalLeaderQuantity=$totalLeaderQuantity, actualRatio=$actualRatio, needMatch=$needMatch")
return needMatch
}
@@ -864,26 +803,16 @@ open class CopyOrderTrackingService(
copyTrading = copyTrading
)
}
"RATIO" -> {
// 比例模式:直接使用配置的 copyRatio (需要除以100)
leaderSellTrade.size.toSafeBigDecimal().multi(copyTrading.copyRatio.div(100))
// 比例模式:直接使用配置的 copyRatio
leaderSellTrade.size.toSafeBigDecimal().multi(copyTrading.copyRatio)
}
else -> {
logger.warn("不支持的 copyMode: ${copyTrading.copyMode},使用默认比例模式")
leaderSellTrade.size.toSafeBigDecimal().multi(copyTrading.copyRatio.div(100))
leaderSellTrade.size.toSafeBigDecimal().multi(copyTrading.copyRatio)
}
}
// 如果需要卖出的数量小于1(但大于0),自动调整为1(Polymarket 最小下单数量)
// 注意:如果实际持有数量不足1,后续的 totalMatched 检查会拦截
var finalNeedMatch = needMatch
if (finalNeedMatch.gt(BigDecimal.ZERO) && finalNeedMatch.lt(BigDecimal.ONE)) {
logger.warn("计算得到的卖出数量小于1,自动调整为1: copyTradingId=${copyTrading.id}, original=$needMatch")
finalNeedMatch = BigDecimal.ONE
}
// 4. 获取tokenId(直接使用outcomeIndex,支持多元市场)
val tokenIdResult = blockchainService.getTokenId(leaderSellTrade.market, leaderSellTrade.outcomeIndex)
if (tokenIdResult.isFailure) {
@@ -907,7 +836,7 @@ open class CopyOrderTrackingService(
// 6. 按FIFO顺序匹配,计算实际可以卖出的数量
// 使用计算出的实际卖出价格(而不是 Leader 价格)来创建匹配明细
var totalMatched = BigDecimal.ZERO
var remaining = finalNeedMatch
var remaining = needMatch
val matchDetails = mutableListOf<SellMatchDetail>()
for (order in unmatchedOrders) {
@@ -944,11 +873,6 @@ open class CopyOrderTrackingService(
return
}
if (totalMatched.lt(BigDecimal.ONE)) {
logger.warn("卖出数量小于1,跳过卖出 (Polymarket 最小下单数量为 1): copyTradingId=${copyTrading.id}, tradeId=${leaderSellTrade.id}, quantity=$totalMatched")
return
}
// 7. 解密 API 凭证
val apiSecret = try {
decryptApiSecret(account)
@@ -965,16 +889,7 @@ open class CopyOrderTrackingService(
// 8. 解密私钥(在方法开始时解密一次,后续复用)
val decryptedPrivateKey = decryptPrivateKey(account)
// 获取费率(根据 Polymarket Maker Rebates Program 要求)
val feeRateResult = clobService.getFeeRate(tokenId)
val feeRateBps = if (feeRateResult.isSuccess) {
feeRateResult.getOrNull()?.toString() ?: "0"
} else {
logger.warn("获取费率失败,使用默认值 0: tokenId=$tokenId, error=${feeRateResult.exceptionOrNull()?.message}")
"0"
}
// 9. 创建并签名卖出订单
val signedOrder = try {
orderSigningService.createAndSignOrder(
@@ -986,7 +901,7 @@ open class CopyOrderTrackingService(
size = totalMatched.toString(),
signatureType = 2, // Browser Wallet
nonce = "0",
feeRateBps = feeRateBps, // 使用动态获取的费率
feeRateBps = "0",
expiration = "0"
)
} catch (e: Exception) {
@@ -1024,8 +939,7 @@ open class CopyOrderTrackingService(
size = totalMatched.toString(),
owner = account.apiKey,
copyTradingId = copyTrading.id,
tradeId = leaderSellTrade.id,
feeRateBps = feeRateBps
tradeId = leaderSellTrade.id
)
if (createOrderResult.isFailure) {
@@ -1045,7 +959,7 @@ open class CopyOrderTrackingService(
} else {
logger.debug("卖出订单ID为0x开头,等待定时任务更新价格: orderId=$realSellOrderId")
}
// 使用下单价格,等待定时任务更新实际成交价
val actualSellPrice = sellPrice
@@ -1094,19 +1008,19 @@ open class CopyOrderTrackingService(
val savedDetail = detail.copy(matchRecordId = savedRecord.id!!)
sellMatchDetailRepository.save(savedDetail)
}
logger.info("卖出订单已保存,等待轮询任务获取实际数据后发送通知: orderId=$realSellOrderId, copyTradingId=${copyTrading.id}")
}
/**
* 创建订单带重试机制
*
*
* 重试策略
* - 最多重试 MAX_RETRY_ATTEMPTS 首次尝试 + 重试
* - 每次重试前等待 RETRY_DELAY_MS 毫秒
* - 每次重试都重新生成salt并重新签名确保签名唯一性
*
*
* @param clobApi CLOB API 客户端
* @param privateKey 私钥用于签名
* @param makerAddress 代理钱包地址
@@ -1117,7 +1031,6 @@ open class CopyOrderTrackingService(
* @param owner API Key用于owner字段
* @param copyTradingId 跟单配置ID用于日志
* @param tradeId Leader 交易ID用于日志
* @param feeRateBps 费率基点从API动态获取
* @return 成功返回订单ID失败返回异常
*/
private suspend fun createOrderWithRetry(
@@ -1130,8 +1043,7 @@ open class CopyOrderTrackingService(
size: String,
owner: String,
copyTradingId: Long,
tradeId: String,
feeRateBps: String
tradeId: String
): Result<String> {
var lastError: Exception? = null
@@ -1148,7 +1060,7 @@ open class CopyOrderTrackingService(
size = size,
signatureType = 2, // Browser Wallet
nonce = "0",
feeRateBps = feeRateBps, // 使用动态获取的费率
feeRateBps = "0",
expiration = "0"
)
@@ -1174,10 +1086,10 @@ open class CopyOrderTrackingService(
}
val errorMsg = "code=${orderResponse.code()}, errorBody=${errorBody ?: "null"}"
lastError = Exception(errorMsg)
// 记录错误日志
logger.error("创建订单失败 (尝试 $attempt/$MAX_RETRY_ATTEMPTS): copyTradingId=$copyTradingId, tradeId=$tradeId, $errorMsg")
// 如果不是最后一次尝试,等待后重试
if (attempt < MAX_RETRY_ATTEMPTS) {
delay(RETRY_DELAY_MS)
@@ -1191,10 +1103,10 @@ open class CopyOrderTrackingService(
if (!response.success || response.orderId == null) {
val errorMsg = "errorMsg=${response.errorMsg}"
lastError = Exception(errorMsg)
// 记录错误日志
logger.error("创建订单失败 (尝试 $attempt/$MAX_RETRY_ATTEMPTS): copyTradingId=$copyTradingId, tradeId=$tradeId, $errorMsg")
// 如果不是最后一次尝试,等待后重试
if (attempt < MAX_RETRY_ATTEMPTS) {
delay(RETRY_DELAY_MS)
@@ -1206,17 +1118,14 @@ open class CopyOrderTrackingService(
// 创建订单成功
logger.info("创建订单成功: copyTradingId=$copyTradingId, tradeId=$tradeId, orderId=${response.orderId}, attempt=$attempt")
return Result.success(response.orderId)
} catch (e: Exception) {
val errorMsg = "error=${e.message}"
lastError = Exception(errorMsg, e)
// 记录错误日志(包含堆栈)
logger.error(
"创建订单异常 (尝试 $attempt/$MAX_RETRY_ATTEMPTS): copyTradingId=$copyTradingId, tradeId=$tradeId, $errorMsg",
e
)
logger.error("创建订单异常 (尝试 $attempt/$MAX_RETRY_ATTEMPTS): copyTradingId=$copyTradingId, tradeId=$tradeId, $errorMsg", e)
// 如果不是最后一次尝试,等待后重试
if (attempt < MAX_RETRY_ATTEMPTS) {
delay(RETRY_DELAY_MS)
@@ -1228,10 +1137,7 @@ open class CopyOrderTrackingService(
// 所有重试都失败
val finalError = lastError ?: Exception("error=未知错误")
logger.error(
"创建订单失败(所有重试都失败): copyTradingId=$copyTradingId, tradeId=$tradeId, side=$side, price=$price, size=$size",
finalError
)
logger.error("创建订单失败(所有重试都失败): copyTradingId=$copyTradingId, tradeId=$tradeId, side=$side, price=$price, size=$size", finalError)
return Result.failure(finalError)
}
@@ -1366,7 +1272,7 @@ open class CopyOrderTrackingService(
// 计算价格调整范围(百分比)
val tolerancePercent = tolerance.div(100)
val adjustment = originalPrice.multi(tolerancePercent).max(0.01.toSafeBigDecimal())
val adjustment = originalPrice.multi(tolerancePercent)
return if (isBuy) {
// 买入:可以稍微加价以确保成交(在原价格基础上加容忍度)
@@ -1413,15 +1319,13 @@ open class CopyOrderTrackingService(
FilterStatus.FAILED_ORDER_DEPTH -> "ORDER_DEPTH"
FilterStatus.FAILED_MAX_POSITION_VALUE -> "MAX_POSITION_VALUE"
FilterStatus.FAILED_MAX_POSITION_COUNT -> "MAX_POSITION_COUNT"
FilterStatus.FAILED_KEYWORD_FILTER -> "KEYWORD_FILTER"
FilterStatus.FAILED_MARKET_END_DATE -> "MARKET_END_DATE"
}
}
/**
* 验证订单ID格式
* 订单ID必须以 0x 开头且是有效的 16 进制字符串
*
*
* @param orderId 订单ID
* @return 如果格式有效返回 true否则返回 false
*/
@@ -1437,11 +1341,11 @@ open class CopyOrderTrackingService(
// 检查是否只包含 0-9, a-f, A-F
return hexPart.all { it in '0'..'9' || it in 'a'..'f' || it in 'A'..'F' }
}
/**
* 获取订单的实际成交价
* 通过查询订单详情和关联的交易记录计算加权平均成交价
*
*
* @param orderId 订单ID
* @param clobApi CLOB API 客户端已认证
* @param fallbackPrice 如果查询失败使用此价格作为默认值
@@ -1460,14 +1364,14 @@ open class CopyOrderTrackingService(
logger.warn("查询订单详情失败: orderId=$orderId, code=${orderResponse.code()}, errorBody=$errorBody")
return fallbackPrice
}
val order = orderResponse.body()
if (order == null) {
// 响应体为空,可能是订单不存在或已过期
logger.warn("查询订单详情失败: 响应体为空, orderId=$orderId, code=${orderResponse.code()}")
return fallbackPrice
}
// 2. 如果订单未成交,使用下单价格
if (order.status != "FILLED" && order.sizeMatched.toSafeBigDecimal() <= BigDecimal.ZERO) {
logger.debug("订单未成交,使用下单价格: orderId=$orderId, status=${order.status}")
@@ -1508,7 +1412,7 @@ open class CopyOrderTrackingService(
for (trade in trades) {
val tradePrice = trade.price.toSafeBigDecimal()
val tradeSize = trade.size.toSafeBigDecimal()
if (tradeSize > BigDecimal.ZERO) {
totalAmount = totalAmount.add(tradePrice.multiply(tradeSize))
totalSize = totalSize.add(tradeSize)
@@ -31,7 +31,8 @@ class CopyTradingStatisticsService(
private val sellMatchDetailRepository: SellMatchDetailRepository,
private val accountRepository: AccountRepository,
private val leaderRepository: LeaderRepository,
private val marketService: com.wrbug.polymarketbot.service.common.MarketService
private val accountService: AccountService,
private val blockchainService: BlockchainService
) {
private val logger = LoggerFactory.getLogger(CopyTradingStatisticsService::class.java)
@@ -61,12 +62,19 @@ class CopyTradingStatisticsService(
// 6. 计算统计信息
val statistics = calculateStatistics(buyOrders, sellRecords, matchDetails)
// 7. 不再计算未实现盈亏和持仓价值(优化性能
// 未实现盈亏计算需要查询链上持仓和市场价格,性能开销大
val unrealizedPnl = "0"
val positionValue = "0"
// 7. 获取链上实际持仓(用于准确计算未实现盈亏,考虑手动卖出的情况
val actualPositions = getActualPositions(account)
// 8. 构建响应(总盈亏 = 已实现盈亏)
// 8. 获取当前市场价格(用于计算未实现盈亏)
val currentPrice = getCurrentMarketPrice(buyOrders)
// 9. 计算未实现盈亏(使用链上实际持仓,而不是 remainingQuantity
val unrealizedPnl = calculateUnrealizedPnl(buyOrders, currentPrice, actualPositions)
// 10. 计算持仓价值(使用链上实际持仓和当前价格)
val positionValue = calculatePositionValue(buyOrders, currentPrice, actualPositions)
// 11. 构建响应
val response = CopyTradingStatisticsResponse(
copyTradingId = copyTradingId,
accountId = copyTrading.accountId,
@@ -85,8 +93,8 @@ class CopyTradingStatisticsService(
currentPositionValue = positionValue,
totalRealizedPnl = statistics.totalRealizedPnl,
totalUnrealizedPnl = unrealizedPnl,
totalPnl = statistics.totalRealizedPnl,
totalPnlPercent = calculatePnlPercentOnlyRealized(statistics.totalBuyAmount, statistics.totalRealizedPnl)
totalPnl = (statistics.totalRealizedPnl.toSafeBigDecimal().add(unrealizedPnl.toSafeBigDecimal())).toString(),
totalPnlPercent = calculatePnlPercent(statistics.totalBuyAmount, statistics.totalRealizedPnl, unrealizedPnl)
)
Result.success(response)
@@ -134,21 +142,12 @@ class CopyTradingStatisticsService(
private fun getBuyOrderList(request: OrderTrackingRequest): Pair<List<BuyOrderInfo>, Long> {
var orders = copyOrderTrackingRepository.findByCopyTradingId(request.copyTradingId)
// 批量获取市场信息(用于筛选)
val allMarketIds = orders.map { it.marketId }.distinct()
val markets = marketService.getMarkets(allMarketIds)
// 筛选
if (!request.marketId.isNullOrBlank()) {
// marketId 支持模糊匹配
orders = orders.filter { it.marketId.contains(request.marketId!!, ignoreCase = true) }
orders = orders.filter { it.marketId == request.marketId }
}
if (!request.marketTitle.isNullOrBlank()) {
// marketTitle 关键字筛选
orders = orders.filter { order ->
val market = markets[order.marketId]
market?.title?.contains(request.marketTitle!!, ignoreCase = true) == true
}
if (!request.side.isNullOrBlank()) {
orders = orders.filter { it.side == request.side }
}
if (!request.status.isNullOrBlank()) {
orders = orders.filter { it.status == request.status }
@@ -169,15 +168,10 @@ class CopyTradingStatisticsService(
// 转换为DTO
val list = pagedOrders.map { order ->
val amount = order.quantity.toSafeBigDecimal().multi(order.price)
val market = markets[order.marketId]
BuyOrderInfo(
orderId = order.buyOrderId,
leaderTradeId = order.leaderBuyTradeId,
marketId = order.marketId,
marketTitle = market?.title,
marketSlug = market?.slug, // 显示用的 slug
eventSlug = market?.eventSlug, // 跳转用的 slug(从数据库读取)
marketCategory = market?.category,
side = order.side,
quantity = order.quantity.toString(),
price = order.price.toString(),
@@ -198,21 +192,12 @@ class CopyTradingStatisticsService(
private fun getSellOrderList(request: OrderTrackingRequest): Pair<List<SellOrderInfo>, Long> {
var records = sellMatchRecordRepository.findByCopyTradingId(request.copyTradingId)
// 批量获取市场信息(用于筛选)
val allMarketIds = records.map { it.marketId }.distinct()
val markets = marketService.getMarkets(allMarketIds)
// 筛选
if (!request.marketId.isNullOrBlank()) {
// marketId 支持模糊匹配
records = records.filter { it.marketId.contains(request.marketId!!, ignoreCase = true) }
records = records.filter { it.marketId == request.marketId }
}
if (!request.marketTitle.isNullOrBlank()) {
// marketTitle 关键字筛选
records = records.filter { record ->
val market = markets[record.marketId]
market?.title?.contains(request.marketTitle!!, ignoreCase = true) == true
}
if (!request.side.isNullOrBlank()) {
records = records.filter { it.side == request.side }
}
val total = records.size.toLong()
@@ -230,15 +215,10 @@ class CopyTradingStatisticsService(
// 转换为DTO
val list = pagedRecords.map { record ->
val amount = record.totalMatchedQuantity.toSafeBigDecimal().multi(record.sellPrice)
val market = markets[record.marketId]
SellOrderInfo(
orderId = record.sellOrderId,
leaderTradeId = record.leaderSellTradeId,
marketId = record.marketId,
marketTitle = market?.title,
marketSlug = market?.slug, // 显示用的 slug
eventSlug = market?.eventSlug, // 跳转用的 slug(从数据库读取)
marketCategory = market?.category,
side = record.side,
quantity = record.totalMatchedQuantity.toString(),
price = record.sellPrice.toString(),
@@ -257,14 +237,6 @@ class CopyTradingStatisticsService(
private fun getMatchedOrderList(request: OrderTrackingRequest): Pair<List<MatchedOrderInfo>, Long> {
val matchDetails = sellMatchDetailRepository.findByCopyTradingId(request.copyTradingId)
// 获取所有相关的卖出记录(用于筛选)
val matchRecordIds = matchDetails.map { it.matchRecordId }.distinct()
val matchRecords = matchRecordIds.mapNotNull { id ->
sellMatchRecordRepository.findById(id).orElse(null)
}
val marketIds = matchRecords.map { it.marketId }.distinct()
val markets = marketService.getMarkets(marketIds)
// 筛选
var filtered = matchDetails
if (!request.sellOrderId.isNullOrBlank()) {
@@ -278,21 +250,6 @@ class CopyTradingStatisticsService(
if (!request.buyOrderId.isNullOrBlank()) {
filtered = filtered.filter { it.buyOrderId == request.buyOrderId }
}
if (!request.marketId.isNullOrBlank()) {
// marketId 支持模糊匹配
filtered = filtered.filter { detail ->
val matchRecord = matchRecords.find { it.id == detail.matchRecordId }
matchRecord?.marketId?.contains(request.marketId!!, ignoreCase = true) == true
}
}
if (!request.marketTitle.isNullOrBlank()) {
// marketTitle 关键字筛选
filtered = filtered.filter { detail ->
val matchRecord = matchRecords.find { it.id == detail.matchRecordId }
val market = matchRecord?.let { markets[it.marketId] }
market?.title?.contains(request.marketTitle!!, ignoreCase = true) == true
}
}
val total = filtered.size.toLong()
@@ -306,26 +263,11 @@ class CopyTradingStatisticsService(
val end = minOf(start + limit, filtered.size)
val pagedDetails = if (start < filtered.size) filtered.subList(start, end) else emptyList()
// 获取匹配记录以获取市场ID
val pagedMatchRecordIds = pagedDetails.map { it.matchRecordId }.distinct()
val pagedMatchRecords = pagedMatchRecordIds.mapNotNull { id ->
sellMatchRecordRepository.findById(id).orElse(null)
}
val pagedMarketIds = pagedMatchRecords.map { it.marketId }.distinct()
val pagedMarkets = marketService.getMarkets(pagedMarketIds)
// 转换为DTO
val list = pagedDetails.map { detail ->
val matchRecord = pagedMatchRecords.find { it.id == detail.matchRecordId }
val market = matchRecord?.let { pagedMarkets[it.marketId] }
MatchedOrderInfo(
sellOrderId = matchRecord?.sellOrderId ?: "",
sellOrderId = sellMatchRecordRepository.findById(detail.matchRecordId).orElse(null)?.sellOrderId ?: "",
buyOrderId = detail.buyOrderId,
marketId = matchRecord?.marketId,
marketTitle = market?.title,
marketSlug = market?.slug, // 显示用的 slug
eventSlug = market?.eventSlug, // 跳转用的 slug(从数据库读取)
marketCategory = market?.category,
matchedQuantity = detail.matchedQuantity.toString(),
buyPrice = detail.buyPrice.toString(),
sellPrice = detail.sellPrice.toString(),
@@ -382,16 +324,200 @@ class CopyTradingStatisticsService(
}
/**
* 计算盈亏百分比仅基于已实现盈亏
* 获取当前市场价格
* (marketId, outcomeIndex) 组合获取价格支持多元市场
*/
private fun calculatePnlPercentOnlyRealized(
private suspend fun getCurrentMarketPrice(buyOrders: List<CopyOrderTracking>): Map<String, String> {
val prices = mutableMapOf<String, String>()
// 获取所有不同的 (marketId, outcomeIndex) 组合
val marketOutcomePairs = buyOrders
.filter { it.outcomeIndex != null }
.map { Pair(it.marketId, it.outcomeIndex!!) }
.distinct()
for ((marketId, outcomeIndex) in marketOutcomePairs) {
try {
// 传递 outcomeIndex 参数,确保获取对应 outcome 的价格
val result = accountService.getMarketPrice(marketId, outcomeIndex)
result.onSuccess { response ->
// 使用当前价格
val price = response.currentPrice
if (price.isNotBlank() && price != "0") {
// 使用 "marketId:outcomeIndex" 作为 key
val key = "$marketId:$outcomeIndex"
prices[key] = price
}
}
} catch (e: Exception) {
logger.warn("获取市场价格失败: marketId=$marketId, outcomeIndex=$outcomeIndex", e)
}
}
return prices
}
/**
* 获取链上实际持仓
* (marketId, outcomeIndex) 组合返回实际持仓数量
*/
private suspend fun getActualPositions(account: Account?): Map<String, BigDecimal> {
val positions = mutableMapOf<String, BigDecimal>()
if (account == null || account.proxyAddress.isBlank()) {
return positions
}
try {
val positionsResult = blockchainService.getPositions(account.proxyAddress)
if (positionsResult.isSuccess) {
val positionList = positionsResult.getOrNull() ?: emptyList()
for (pos in positionList) {
// 只处理有 conditionId 和 outcomeIndex 的仓位
if (pos.conditionId != null && pos.outcomeIndex != null && pos.size != null) {
val key = "${pos.conditionId}:${pos.outcomeIndex}"
val size = pos.size.toSafeBigDecimal()
// 如果 size > 0,表示有持仓;如果 size < 0,表示做空(取绝对值)
positions[key] = size.abs()
}
}
}
} catch (e: Exception) {
logger.warn("获取链上持仓失败: accountId=${account.id}, error=${e.message}", e)
}
return positions
}
/**
* 计算未实现盈亏
* 使用链上实际持仓数量而不是 remainingQuantity考虑手动卖出的情况
* 按市场聚合订单计算加权平均买入价格避免重复计算
*/
private fun calculateUnrealizedPnl(
buyOrders: List<CopyOrderTracking>,
currentPrices: Map<String, String>,
actualPositions: Map<String, BigDecimal>
): String {
var totalUnrealizedPnl = BigDecimal.ZERO
// 按市场聚合订单,计算加权平均买入价格
val marketAggregates = mutableMapOf<String, Pair<BigDecimal, BigDecimal>>() // key -> (总持仓, 总成本)
for (order in buyOrders) {
// 如果没有 outcomeIndex,跳过(无法确定价格和持仓)
if (order.outcomeIndex == null) {
logger.warn("订单缺少 outcomeIndex,跳过未实现盈亏计算: orderId=${order.buyOrderId}, marketId=${order.marketId}")
continue
}
// 使用 "marketId:outcomeIndex" 作为 key
val key = "${order.marketId}:${order.outcomeIndex}"
// 获取订单的持仓数量(使用 remainingQuantity,因为这是该订单的持仓)
val orderQty = order.remainingQuantity.toSafeBigDecimal()
// 如果订单持仓 <= 0,跳过
if (orderQty.lte(BigDecimal.ZERO)) continue
val buyPrice = order.price.toSafeBigDecimal()
val orderCost = orderQty.multi(buyPrice)
// 聚合同一市场的订单
val existing = marketAggregates[key]
if (existing != null) {
val totalQty = existing.first.add(orderQty)
val totalCost = existing.second.add(orderCost)
marketAggregates[key] = Pair(totalQty, totalCost)
} else {
marketAggregates[key] = Pair(orderQty, orderCost)
}
}
// 计算每个市场的未实现盈亏
for ((key, aggregate) in marketAggregates) {
val (totalQty, totalCost) = aggregate
// 获取链上实际持仓数量(如果存在),否则使用聚合的持仓数量
val actualQty = actualPositions[key] ?: totalQty
// 如果实际持仓 <= 0,说明已全部卖出(包括手动卖出),跳过未实现盈亏计算
if (actualQty.lte(BigDecimal.ZERO)) continue
// 获取当前市场价格
val currentPrice = currentPrices[key]?.toSafeBigDecimal()
?: continue // 如果没有当前价格,跳过
// 计算加权平均买入价格
val avgBuyPrice = if (totalQty.gt(BigDecimal.ZERO)) {
totalCost.div(totalQty)
} else {
continue
}
// 使用实际持仓数量和加权平均买入价格计算未实现盈亏
val unrealizedPnl = currentPrice.subtract(avgBuyPrice).multi(actualQty)
totalUnrealizedPnl = totalUnrealizedPnl.add(unrealizedPnl)
}
return totalUnrealizedPnl.toString()
}
/**
* 计算持仓价值
* 使用链上实际持仓数量和当前市场价格计算
* 按市场聚合避免重复计算
*/
private fun calculatePositionValue(
buyOrders: List<CopyOrderTracking>,
currentPrices: Map<String, String>,
actualPositions: Map<String, BigDecimal>
): String {
var totalPositionValue = BigDecimal.ZERO
// 按市场聚合,获取所有不同的市场
val marketKeys = buyOrders
.filter { it.outcomeIndex != null }
.map { "${it.marketId}:${it.outcomeIndex}" }
.distinct()
for (key in marketKeys) {
// 获取链上实际持仓数量(如果存在)
val actualQty = actualPositions[key]
// 如果没有链上持仓,计算该市场的总持仓(所有订单的 remainingQuantity 之和)
val totalQty = actualQty ?: buyOrders
.filter { it.outcomeIndex != null && "${it.marketId}:${it.outcomeIndex}" == key }
.sumOf { it.remainingQuantity.toSafeBigDecimal() }
// 如果持仓 <= 0,跳过
if (totalQty.lte(BigDecimal.ZERO)) continue
// 获取当前市场价格
val currentPrice = currentPrices[key]?.toSafeBigDecimal()
?: continue // 如果没有当前价格,跳过
// 计算持仓价值:持仓数量 × 当前价格
val positionValue = totalQty.multi(currentPrice)
totalPositionValue = totalPositionValue.add(positionValue)
}
return totalPositionValue.toString()
}
/**
* 计算盈亏百分比
*/
private fun calculatePnlPercent(
totalBuyAmount: String,
totalRealizedPnl: String
totalRealizedPnl: String,
totalUnrealizedPnl: String
): String {
val buyAmount = totalBuyAmount.toSafeBigDecimal()
if (buyAmount.lte(BigDecimal.ZERO)) return "0"
val percent = totalRealizedPnl.toSafeBigDecimal().div(buyAmount).multi(100)
val totalPnl = totalRealizedPnl.toSafeBigDecimal().add(totalUnrealizedPnl.toSafeBigDecimal())
val percent = totalPnl.div(buyAmount).multi(100)
return percent.setScale(2, RoundingMode.HALF_UP).toString()
}
@@ -570,258 +696,5 @@ class CopyTradingStatisticsService(
val currentPositionQuantity: String,
val totalRealizedPnl: String
)
/**
* 获取按市场分组的买入订单列表
*/
fun getBuyOrderListGroupedByMarket(request: MarketGroupedOrdersRequest): Result<MarketGroupedOrdersResponse> {
return try {
// 1. 验证跟单关系
copyTradingRepository.findById(request.copyTradingId).orElse(null)
?: return Result.failure(IllegalArgumentException("跟单关系不存在: ${request.copyTradingId}"))
// 2. 获取所有买入订单
var orders = copyOrderTrackingRepository.findByCopyTradingId(request.copyTradingId)
// 3. 批量获取市场信息(用于筛选)
val allMarketIds = orders.map { it.marketId }.distinct()
val markets = marketService.getMarkets(allMarketIds)
// 4. 筛选
if (!request.marketId.isNullOrBlank()) {
// marketId 支持模糊匹配
orders = orders.filter { it.marketId.contains(request.marketId!!, ignoreCase = true) }
}
if (!request.marketTitle.isNullOrBlank()) {
// marketTitle 关键字筛选
orders = orders.filter { order ->
val market = markets[order.marketId]
market?.title?.contains(request.marketTitle!!, ignoreCase = true) == true
}
}
// 5. 按市场ID分组
val groups = mutableMapOf<String, MutableList<CopyOrderTracking>>()
orders.forEach { order ->
val marketId = order.marketId
if (!groups.containsKey(marketId)) {
groups[marketId] = mutableListOf()
}
groups[marketId]!!.add(order)
}
// 4. 转换为分组数据并计算统计信息
val marketIds = groups.keys.toList()
val list = marketIds.map { marketId ->
val marketOrders = groups[marketId] ?: mutableListOf()
// 计算统计信息
val count = marketOrders.size.toLong()
val totalAmount = marketOrders.sumOf { order ->
order.quantity.toSafeBigDecimal().multi(order.price)
}
// 计算订单状态统计
val fullyMatchedCount = marketOrders.count { it.status == "fully_matched" }
val partiallyMatchedCount = marketOrders.count { it.status == "partially_matched" }
val filledCount = marketOrders.count { it.status == "filled" }
val fullyMatched = fullyMatchedCount == marketOrders.size
val stats = MarketOrderStats(
count = count,
totalAmount = totalAmount.toString(),
totalPnl = null, // 买入订单没有已实现盈亏
fullyMatched = fullyMatched,
fullyMatchedCount = fullyMatchedCount.toLong(),
partiallyMatchedCount = partiallyMatchedCount.toLong(),
filledCount = filledCount.toLong()
)
// 排序(按创建时间倒序)
marketOrders.sortByDescending { it.createdAt }
// 转换为 DTO
val orderDtos = marketOrders.map { order ->
val amount = order.quantity.toSafeBigDecimal().multi(order.price)
val market = markets[order.marketId]
BuyOrderInfo(
orderId = order.buyOrderId,
leaderTradeId = order.leaderBuyTradeId,
marketId = order.marketId,
marketTitle = market?.title,
marketSlug = market?.slug, // 显示用的 slug
eventSlug = market?.eventSlug, // 跳转用的 slug(从数据库读取)
marketCategory = market?.category,
side = order.side,
quantity = order.quantity.toString(),
price = order.price.toString(),
amount = amount.toString(),
matchedQuantity = order.matchedQuantity.toString(),
remainingQuantity = order.remainingQuantity.toString(),
status = order.status,
createdAt = order.createdAt
)
}
MarketOrderGroup(
marketId = marketId,
marketTitle = markets[marketId]?.title,
marketSlug = markets[marketId]?.slug, // 显示用的 slug
eventSlug = markets[marketId]?.eventSlug, // 跳转用的 slug(从数据库读取)
marketCategory = markets[marketId]?.category,
stats = stats,
orders = orderDtos as List<Any>
)
}.sortedByDescending { group ->
// 找出该市场最近的买入订单时间
group.orders.mapNotNull { order ->
when (order) {
is BuyOrderInfo -> order.createdAt
else -> null
}
}.maxOrNull() ?: 0L
}
// 5. 分页
val page = (request.page ?: 1)
val limit = request.limit ?: 20
val total = list.size.toLong()
val start = (page - 1) * limit
val end = minOf(start + limit, list.size)
val pagedList = if (start < list.size) list.subList(start, end) else emptyList()
val response = MarketGroupedOrdersResponse(
list = pagedList,
total = total,
page = page,
limit = limit
)
Result.success(response)
} catch (e: Exception) {
logger.error("获取按市场分组的卖出订单列表失败: copyTradingId=${request.copyTradingId}", e)
Result.failure(e)
}
}
/**
* 获取按市场分组的卖出订单列表
*/
fun getSellOrderListGroupedByMarket(request: MarketGroupedOrdersRequest): Result<MarketGroupedOrdersResponse> {
return try {
// 1. 验证跟单关系
copyTradingRepository.findById(request.copyTradingId).orElse(null)
?: return Result.failure(IllegalArgumentException("跟单关系不存在: ${request.copyTradingId}"))
// 2. 获取所有卖出记录
var sellRecords = sellMatchRecordRepository.findByCopyTradingId(request.copyTradingId)
// 3. 批量获取市场信息(用于筛选)
val allMarketIds = sellRecords.map { it.marketId }.distinct()
val markets = marketService.getMarkets(allMarketIds)
// 4. 筛选
if (!request.marketId.isNullOrBlank()) {
// marketId 支持模糊匹配
sellRecords = sellRecords.filter { it.marketId.contains(request.marketId!!, ignoreCase = true) }
}
if (!request.marketTitle.isNullOrBlank()) {
// marketTitle 关键字筛选
sellRecords = sellRecords.filter { record ->
val market = markets[record.marketId]
market?.title?.contains(request.marketTitle!!, ignoreCase = true) == true
}
}
// 5. 按市场ID分组
val groups = mutableMapOf<String, MutableList<SellMatchRecord>>()
sellRecords.forEach { record ->
val marketId = record.marketId
if (!groups.containsKey(marketId)) {
groups[marketId] = mutableListOf()
}
groups[marketId]!!.add(record)
}
// 4. 转换为分组数据并计算统计信息
val marketIds = groups.keys.toList()
val list = marketIds.map { marketId ->
val marketRecords = groups[marketId] ?: mutableListOf()
// 计算统计信息
val count = marketRecords.size.toLong()
val totalAmount = marketRecords.sumOf { record ->
record.totalMatchedQuantity.toSafeBigDecimal().multi(record.sellPrice)
}
val totalPnl = marketRecords.sumOf { it.totalRealizedPnl.toSafeBigDecimal() }
val stats = MarketOrderStats(
count = count,
totalAmount = totalAmount.toString(),
totalPnl = totalPnl.toString(),
fullyMatched = true, // 卖出订单都是已成交的
fullyMatchedCount = count, // 所有订单都是已成交的
partiallyMatchedCount = 0L,
filledCount = 0L
)
// 排序(按创建时间倒序)
marketRecords.sortByDescending { it.createdAt }
// 转换为 DTO
val orderDtos = marketRecords.map { record ->
val amount = record.totalMatchedQuantity.toSafeBigDecimal().multi(record.sellPrice)
val market = markets[record.marketId]
SellOrderInfo(
orderId = record.sellOrderId,
leaderTradeId = record.leaderSellTradeId,
marketId = record.marketId,
marketTitle = market?.title,
marketSlug = market?.slug, // 显示用的 slug
eventSlug = market?.eventSlug, // 跳转用的 slug(从数据库读取)
marketCategory = market?.category,
side = record.side,
quantity = record.totalMatchedQuantity.toString(),
price = record.sellPrice.toString(),
amount = amount.toString(),
realizedPnl = record.totalRealizedPnl.toString(),
createdAt = record.createdAt
)
}
MarketOrderGroup(
marketId = marketId,
marketTitle = markets[marketId]?.title,
marketSlug = markets[marketId]?.slug, // 显示用的 slug
eventSlug = markets[marketId]?.eventSlug, // 跳转用的 slug(从数据库读取)
marketCategory = markets[marketId]?.category,
stats = stats,
orders = orderDtos as List<Any>
)
}.sortedByDescending { it.stats.count }
// 5. 分页
val page = (request.page ?: 1)
val limit = request.limit ?: 20
val total = list.size.toLong()
val start = (page - 1) * limit
val end = minOf(start + limit, list.size)
val pagedList = if (start < list.size) list.subList(start, end) else emptyList()
val response = MarketGroupedOrdersResponse(
list = pagedList,
total = total,
page = page,
limit = limit
)
Result.success(response)
} catch (e: Exception) {
logger.error("获取按市场分组的卖出订单列表失败: copyTradingId=${request.copyTradingId}", e)
Result.failure(e)
}
}
}
@@ -3,7 +3,6 @@ package com.wrbug.polymarketbot.service.copytrading.statistics
import com.wrbug.polymarketbot.api.PolymarketClobApi
import com.wrbug.polymarketbot.entity.*
import com.wrbug.polymarketbot.repository.*
import com.wrbug.polymarketbot.service.common.MarketService
import com.wrbug.polymarketbot.service.system.TelegramNotificationService
import com.wrbug.polymarketbot.util.RetrofitFactory
import com.wrbug.polymarketbot.util.CryptoUtils
@@ -18,7 +17,6 @@ import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.math.BigDecimal
import java.util.concurrent.ConcurrentHashMap
/**
* 订单状态更新服务
@@ -35,28 +33,18 @@ class OrderStatusUpdateService(
private val retrofitFactory: RetrofitFactory,
private val cryptoUtils: CryptoUtils,
private val trackingService: CopyOrderTrackingService,
private val marketService: MarketService, // 市场信息服务
private val telegramNotificationService: TelegramNotificationService?
) {
private val logger = LoggerFactory.getLogger(OrderStatusUpdateService::class.java)
private val updateScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
// 缓存首次检测到订单详情为 null 的时间戳(订单ID -> 首次检测时间)
private val orderNullDetectionTime = ConcurrentHashMap<String, Long>()
// 订单详情为 null 的重试时间窗口(1分钟)
private val ORDER_NULL_RETRY_WINDOW_MS = 60000L
// 订单详情为 null 但已部分卖出的清理时间窗口(1小时)
private val PARTIAL_SOLD_CLEANUP_WINDOW_MS = 3600000L
@EventListener(ApplicationReadyEvent::class)
fun onApplicationReady() {
logger.info("订单状态更新服务已启动,将每5秒轮询一次")
}
/**
* 定时更新订单状态
* 每5秒执行一次
@@ -67,13 +55,13 @@ class OrderStatusUpdateService(
try {
// 1. 清理已删除账户的订单
cleanupDeletedAccountOrders()
// 2. 检查30秒前创建的订单,如果未成交则删除
checkAndDeleteUnfilledOrders()
// 3. 更新卖出订单的实际成交价并发送通知(priceUpdated 共用字段)
updatePendingSellOrderPrices()
// 4. 更新买入订单的实际数据并发送通知
updatePendingBuyOrders()
} catch (e: Exception) {
@@ -81,11 +69,11 @@ class OrderStatusUpdateService(
}
}
}
/**
* 验证订单ID格式
* 订单ID必须以 0x 开头且是有效的 16 进制字符串
*
*
* @param orderId 订单ID
* @return 如果格式有效返回 true否则返回 false
*/
@@ -101,7 +89,7 @@ class OrderStatusUpdateService(
// 检查是否只包含 0-9, a-f, A-F
return hexPart.all { it in '0'..'9' || it in 'a'..'f' || it in 'A'..'F' }
}
/**
* 清理已删除账户的订单
*/
@@ -110,41 +98,41 @@ class OrderStatusUpdateService(
try {
// 查询所有卖出记录
val allRecords = sellMatchRecordRepository.findAll()
// 查询所有有效的账户ID
val validAccountIds = accountRepository.findAll().mapNotNull { it.id }.toSet()
// 查询所有有效的跟单关系
val validCopyTradingIds = copyTradingRepository.findAll()
.filter { it.accountId in validAccountIds }
.mapNotNull { it.id }
.toSet()
// 找出需要删除的记录(关联的跟单关系已不存在或账户已删除)
val recordsToDelete = allRecords.filter { record ->
val copyTrading = copyTradingRepository.findById(record.copyTradingId).orElse(null)
copyTrading == null || copyTrading.accountId !in validAccountIds
}
if (recordsToDelete.isNotEmpty()) {
logger.info("清理已删除账户的订单: ${recordsToDelete.size} 条记录")
// 删除匹配明细
for (record in recordsToDelete) {
val details = sellMatchDetailRepository.findByMatchRecordId(record.id!!)
sellMatchDetailRepository.deleteAll(details)
}
// 删除卖出记录
sellMatchRecordRepository.deleteAll(recordsToDelete)
logger.info("已清理 ${recordsToDelete.size} 条已删除账户的订单记录")
}
} catch (e: Exception) {
logger.error("清理已删除账户订单异常: ${e.message}", e)
}
}
/**
* 检查30秒前创建的订单如果未成交则删除
* 首次检测但加入缓存中30s后还没有成交则删除
@@ -154,24 +142,19 @@ class OrderStatusUpdateService(
try {
// 计算30秒前的时间戳
val thirtySecondsAgo = System.currentTimeMillis() - 30000
// 查询30秒前创建的订单,并过滤掉已经完全匹配的订单
// 已经完全匹配的订单(status = "fully_matched")不需要再检查
// 使用数据库查询过滤,避免加载过多数据
val ordersToCheck = copyOrderTrackingRepository.findByCreatedAtBeforeAndStatusNot(
thirtySecondsAgo,
"fully_matched"
)
// 查询30秒前创建的订单
val ordersToCheck = copyOrderTrackingRepository.findByCreatedAtBefore(thirtySecondsAgo)
if (ordersToCheck.isEmpty()) {
return
}
logger.debug("检查 ${ordersToCheck.size} 个30秒前创建的订单是否成交")
// 按账户分组,避免重复创建 API 客户端
val ordersByAccount = ordersToCheck.groupBy { it.accountId }
for ((accountId, orders) in ordersByAccount) {
try {
// 获取账户
@@ -180,13 +163,13 @@ class OrderStatusUpdateService(
logger.warn("账户不存在,跳过检查: accountId=$accountId")
continue
}
// 检查账户是否配置了 API 凭证
if (account.apiKey == null || account.apiSecret == null || account.apiPassphrase == null) {
logger.debug("账户未配置 API 凭证,跳过检查: accountId=${account.id}")
continue
}
// 解密 API 凭证
val apiSecret = try {
cryptoUtils.decrypt(account.apiSecret!!)
@@ -194,14 +177,14 @@ class OrderStatusUpdateService(
logger.warn("解密 API Secret 失败: accountId=${account.id}, error=${e.message}")
continue
}
val apiPassphrase = try {
cryptoUtils.decrypt(account.apiPassphrase!!)
} catch (e: Exception) {
logger.warn("解密 API Passphrase 失败: accountId=${account.id}, error=${e.message}")
continue
}
// 创建带认证的 CLOB API 客户端
val clobApi = retrofitFactory.createClobApi(
account.apiKey!!,
@@ -209,13 +192,13 @@ class OrderStatusUpdateService(
apiPassphrase,
account.walletAddress
)
// 检查每个订单
for (order in orders) {
try {
// 查询订单详情
val orderResponse = clobApi.getOrder(order.buyOrderId)
// 先检查 HTTP 状态码,非 200 的都跳过
if (orderResponse.code() != 200) {
// HTTP 非 200,记录日志并跳过,等待下次轮询
@@ -224,72 +207,30 @@ class OrderStatusUpdateService(
logger.debug("订单查询失败(HTTP非200),等待下次轮询: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}, code=${orderResponse.code()}, errorBody=$errorBody")
continue
}
// HTTP 200,检查响应体
// 响应体也可能返回字符串 "null"Gson 解析时会返回 null
val orderDetail = orderResponse.body()
if (orderDetail == null) {
// HTTP 200 且响应体为 null(或字符串 "null"),可能是网络异常或 API 暂时不可用
// 使用兜底逻辑:首次检测不删除,1分钟后仍为 null 才删除
val firstDetectionTime =
orderNullDetectionTime.getOrPut(order.buyOrderId) { System.currentTimeMillis() }
val currentTime = System.currentTimeMillis()
// HTTP 200 且响应体为 null(或字符串 "null"),表示订单不存在
// 检查订单是否已部分卖出,如果已部分卖出则保留订单用于统计
val hasMatchedDetails =
sellMatchDetailRepository.findByTrackingId(order.id!!).isNotEmpty()
val hasMatchedDetails = sellMatchDetailRepository.findByTrackingId(order.id!!).isNotEmpty()
if (hasMatchedDetails || order.matchedQuantity > BigDecimal.ZERO) {
// 检查是否超过清理时间窗口(1小时)
val orderAge = currentTime - order.createdAt
if (orderAge >= PARTIAL_SOLD_CLEANUP_WINDOW_MS) {
logger.warn("订单详情为 null 且已部分卖出,但超过清理时间窗口,删除本地订单: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}, matchedQuantity=${order.matchedQuantity}, 已等待=${orderAge / 1000}s")
try {
copyOrderTrackingRepository.deleteById(order.id!!)
logger.info("已删除本地订单(超时清理): orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}")
// 清除缓存
orderNullDetectionTime.remove(order.buyOrderId)
} catch (e: Exception) {
logger.error(
"删除本地订单失败: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}, error=${e.message}",
e
)
}
continue
} else {
logger.debug("订单详情为 null 但已部分卖出,保留订单用于统计: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}, matchedQuantity=${order.matchedQuantity}, 已等待=${orderAge / 1000}s")
// 清除缓存,下次重新检测
orderNullDetectionTime.remove(order.buyOrderId)
continue
}
}
// 检查是否超过重试时间窗口
if (currentTime - firstDetectionTime < ORDER_NULL_RETRY_WINDOW_MS) {
// 未超过重试窗口,记录日志并等待下次轮询
val elapsedSeconds = ((currentTime - firstDetectionTime) / 1000).toInt()
logger.debug("订单详情为 null(可能是网络异常),等待重试: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}, 已等待=${elapsedSeconds}s, 重试窗口=${ORDER_NULL_RETRY_WINDOW_MS / 1000}s")
logger.debug("订单不存在但已部分卖出,保留订单用于统计: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}, matchedQuantity=${order.matchedQuantity}")
continue
}
// 超过重试窗口,删除本地订单
logger.warn("订单详情为 null 超过重试窗口,删除本地订单: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}, 已等待=$((currentTime - firstDetectionTime) / 1000}s")
// 订单不存在且未部分卖出,删除本地订单
logger.info("订单不存在(HTTP 200 但响应体为空),删除本地订单: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}")
try {
copyOrderTrackingRepository.deleteById(order.id!!)
logger.info("已删除本地订单: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}")
// 清除缓存
orderNullDetectionTime.remove(order.buyOrderId)
} catch (e: Exception) {
logger.error(
"删除本地订单失败: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}, error=${e.message}",
e
)
logger.error("删除本地订单失败: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}, error=${e.message}", e)
}
continue
}
// 订单详情不为 null,清除缓存
orderNullDetectionTime.remove(order.buyOrderId)
// 检查订单是否成交
// 如果订单状态不是 FILLED 且已成交数量为0,说明未成交,删除
val sizeMatched = orderDetail.sizeMatched?.toSafeBigDecimal() ?: BigDecimal.ZERO
@@ -299,11 +240,10 @@ class OrderStatusUpdateService(
copyOrderTrackingRepository.deleteById(order.id!!)
logger.info("已删除未成交订单: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}")
} catch (e: Exception) {
logger.error(
"删除未成交订单失败: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}, error=${e.message}",
e
)
logger.error("删除未成交订单失败: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}, error=${e.message}", e)
}
} else {
logger.debug("订单已成交或部分成交,保留: orderId=${order.buyOrderId}, status=${orderDetail.status}, sizeMatched=$sizeMatched")
}
} catch (e: Exception) {
logger.error("检查订单失败: orderId=${order.buyOrderId}, error=${e.message}", e)
@@ -317,7 +257,7 @@ class OrderStatusUpdateService(
logger.error("检查未成交订单异常: ${e.message}", e)
}
}
/**
* 更新待更新的卖出订单价格
* 注意priceUpdated 现在同时表示价格已更新和通知已发送共用字段
@@ -327,13 +267,13 @@ class OrderStatusUpdateService(
try {
// 查询所有价格未更新的卖出记录(priceUpdated = false 表示未处理)
val pendingRecords = sellMatchRecordRepository.findByPriceUpdatedFalse()
if (pendingRecords.isEmpty()) {
return
}
logger.debug("找到 ${pendingRecords.size} 条待更新价格的卖出订单")
for (record in pendingRecords) {
try {
// 获取跟单关系
@@ -342,20 +282,20 @@ class OrderStatusUpdateService(
logger.warn("跟单关系不存在,跳过更新: copyTradingId=${record.copyTradingId}")
continue
}
// 获取账户
val account = accountRepository.findById(copyTrading.accountId).orElse(null)
if (account == null) {
logger.warn("账户不存在,跳过更新: accountId=${copyTrading.accountId}")
continue
}
// 检查账户是否配置了 API 凭证
if (account.apiKey == null || account.apiSecret == null || account.apiPassphrase == null) {
logger.debug("账户未配置 API 凭证,跳过更新: accountId=${account.id}")
continue
}
// 解密 API 凭证
val apiSecret = try {
cryptoUtils.decrypt(account.apiSecret!!)
@@ -363,14 +303,14 @@ class OrderStatusUpdateService(
logger.warn("解密 API Secret 失败: accountId=${account.id}, error=${e.message}")
continue
}
val apiPassphrase = try {
cryptoUtils.decrypt(account.apiPassphrase!!)
} catch (e: Exception) {
logger.warn("解密 API Passphrase 失败: accountId=${account.id}, error=${e.message}")
continue
}
// 创建带认证的 CLOB API 客户端
val clobApi = retrofitFactory.createClobApi(
account.apiKey!!,
@@ -378,16 +318,16 @@ class OrderStatusUpdateService(
apiPassphrase,
account.walletAddress
)
// 如果 orderId 不是 0x 开头,直接标记为已处理(priceUpdated = true 表示已处理,包括价格更新和通知发送)
if (!record.sellOrderId.startsWith("0x", ignoreCase = true)) {
logger.debug("卖出订单ID非0x开头,直接标记为已处理: orderId=${record.sellOrderId}")
// 检查是否为自动生成的订单(AUTO_ 或 AUTO_FIFO_ 开头),如果是则不发送通知
val isAutoOrder = record.sellOrderId.startsWith("AUTO_", ignoreCase = true) ||
record.sellOrderId.startsWith("AUTO_FIFO_", ignoreCase = true) ||
record.sellOrderId.startsWith("AUTO_WS_", ignoreCase = true)
val isAutoOrder = record.sellOrderId.startsWith("AUTO_", ignoreCase = true) ||
record.sellOrderId.startsWith("AUTO_FIFO_", ignoreCase = true) ||
record.sellOrderId.startsWith("AUTO_WS_", ignoreCase = true)
if (!isAutoOrder) {
// 非自动订单,发送通知(使用临时数据)
sendSellOrderNotification(
@@ -397,13 +337,12 @@ class OrderStatusUpdateService(
copyTrading = copyTrading,
clobApi = clobApi,
apiSecret = apiSecret,
apiPassphrase = apiPassphrase,
orderCreatedAt = record.createdAt
apiPassphrase = apiPassphrase
)
} else {
logger.debug("自动生成的订单,跳过发送通知: orderId=${record.sellOrderId}")
}
// 标记为已处理(priceUpdated = true 同时表示价格已更新和通知已发送)
val updatedRecord = SellMatchRecord(
id = record.id,
@@ -422,12 +361,12 @@ class OrderStatusUpdateService(
sellMatchRecordRepository.save(updatedRecord)
continue
}
// 检查是否为自动生成的订单(AUTO_ 或 AUTO_FIFO_ 开头),如果是则跳过发送通知
val isAutoOrder = record.sellOrderId.startsWith("AUTO_", ignoreCase = true) ||
record.sellOrderId.startsWith("AUTO_FIFO_", ignoreCase = true) ||
record.sellOrderId.startsWith("AUTO_WS_", ignoreCase = true)
val isAutoOrder = record.sellOrderId.startsWith("AUTO_", ignoreCase = true) ||
record.sellOrderId.startsWith("AUTO_FIFO_", ignoreCase = true) ||
record.sellOrderId.startsWith("AUTO_WS_", ignoreCase = true)
if (isAutoOrder) {
logger.debug("自动生成的订单,跳过发送通知并直接标记为已处理: orderId=${record.sellOrderId}")
// 直接标记为已处理,不发送通知
@@ -448,24 +387,23 @@ class OrderStatusUpdateService(
sellMatchRecordRepository.save(updatedRecord)
continue
}
// 查询订单详情,获取实际成交价
val actualSellPrice = trackingService.getActualExecutionPrice(
orderId = record.sellOrderId,
clobApi = clobApi,
fallbackPrice = record.sellPrice
)
// 如果价格已更新(与当前价格不同),更新数据库
if (actualSellPrice != record.sellPrice) {
// 重新计算盈亏
val details = sellMatchDetailRepository.findByMatchRecordId(record.id!!)
var totalRealizedPnl = BigDecimal.ZERO
for (detail in details) {
val updatedRealizedPnl =
actualSellPrice.subtract(detail.buyPrice).multi(detail.matchedQuantity)
val updatedRealizedPnl = actualSellPrice.subtract(detail.buyPrice).multi(detail.matchedQuantity)
// 更新明细的卖出价格和盈亏
// 注意:SellMatchDetail 的字段都是 val,需要创建新对象
val updatedDetail = SellMatchDetail(
@@ -480,11 +418,23 @@ class OrderStatusUpdateService(
createdAt = detail.createdAt
)
sellMatchDetailRepository.save(updatedDetail)
totalRealizedPnl = totalRealizedPnl.add(updatedRealizedPnl)
}
// 先更新卖出记录,标记 priceUpdated = true(在发送通知之前更新
// 发送通知(使用实际价格
sendSellOrderNotification(
record = record,
actualPrice = actualSellPrice.toString(),
actualSize = record.totalMatchedQuantity.toString(),
account = account,
copyTrading = copyTrading,
clobApi = clobApi,
apiSecret = apiSecret,
apiPassphrase = apiPassphrase
)
// 更新卖出记录
// 注意:SellMatchRecord 的字段都是 val,需要创建新对象
val updatedRecord = SellMatchRecord(
id = record.id,
@@ -501,24 +451,21 @@ class OrderStatusUpdateService(
createdAt = record.createdAt
)
sellMatchRecordRepository.save(updatedRecord)
logger.info("更新卖出订单价格成功: orderId=${record.sellOrderId}, 原价格=${record.sellPrice}, 新价格=$actualSellPrice")
// 发送通知(使用实际价格)
logger.info("更新卖出订单价格成功并已发送通知: orderId=${record.sellOrderId}, 原价格=${record.sellPrice}, 新价格=$actualSellPrice")
} else {
// 价格相同,但已经查询过,发送通知并标记为已处理
sendSellOrderNotification(
record = updatedRecord,
record = record,
actualPrice = actualSellPrice.toString(),
actualSize = record.totalMatchedQuantity.toString(),
account = account,
copyTrading = copyTrading,
clobApi = clobApi,
apiSecret = apiSecret,
apiPassphrase = apiPassphrase,
orderCreatedAt = record.createdAt
apiPassphrase = apiPassphrase
)
logger.info("卖出订单通知已发送: orderId=${record.sellOrderId}")
} else {
// 价格相同,但已经查询过,先标记为已处理再发送通知
val updatedRecord = SellMatchRecord(
id = record.id,
copyTradingId = record.copyTradingId,
@@ -534,22 +481,7 @@ class OrderStatusUpdateService(
createdAt = record.createdAt
)
sellMatchRecordRepository.save(updatedRecord)
logger.debug("卖出订单价格无需更新: orderId=${record.sellOrderId}, price=$actualSellPrice")
// 发送通知
sendSellOrderNotification(
record = updatedRecord,
actualPrice = actualSellPrice.toString(),
actualSize = record.totalMatchedQuantity.toString(),
account = account,
copyTrading = copyTrading,
clobApi = clobApi,
apiSecret = apiSecret,
apiPassphrase = apiPassphrase,
orderCreatedAt = record.createdAt
)
logger.info("卖出订单通知已发送: orderId=${record.sellOrderId}")
logger.debug("卖出订单价格无需更新但已发送通知: orderId=${record.sellOrderId}, price=$actualSellPrice")
}
} catch (e: Exception) {
logger.warn("更新卖出订单价格失败: orderId=${record.sellOrderId}, error=${e.message}", e)
@@ -560,7 +492,7 @@ class OrderStatusUpdateService(
logger.error("更新待更新卖出订单价格异常: ${e.message}", e)
}
}
/**
* 更新待发送通知的买入订单
* 查询订单详情获取实际价格和数量然后发送通知并更新数据库
@@ -570,19 +502,19 @@ class OrderStatusUpdateService(
try {
// 查询所有未发送通知的买入订单
val pendingOrders = copyOrderTrackingRepository.findByNotificationSentFalse()
if (pendingOrders.isEmpty()) {
return
}
logger.debug("找到 ${pendingOrders.size} 条待发送通知的买入订单")
for (order in pendingOrders) {
try {
// 验证 orderId 格式(必须以 0x 开头的 16 进制)
if (!isValidOrderId(order.buyOrderId)) {
logger.warn("买入订单ID格式无效,直接标记为已发送通知: orderId=${order.buyOrderId}")
// 对于非 0x 开头的订单ID标记为已发送,使用临时数据发送通知
// 对于非 0x 开头的订单ID直接标记为已发送,使用临时数据发送通知
val updatedOrder = CopyOrderTracking(
id = order.id,
copyTradingId = order.copyTradingId,
@@ -599,40 +531,34 @@ class OrderStatusUpdateService(
remainingQuantity = order.remainingQuantity,
status = order.status,
notificationSent = true, // 标记为已发送通知
source = order.source, // 保留原始订单来源
createdAt = order.createdAt,
updatedAt = System.currentTimeMillis()
)
copyOrderTrackingRepository.save(updatedOrder)
sendBuyOrderNotification(
updatedOrder,
useTemporaryData = true,
orderCreatedAt = order.createdAt
)
sendBuyOrderNotification(updatedOrder, useTemporaryData = true)
continue
}
// 获取跟单关系
val copyTrading = copyTradingRepository.findById(order.copyTradingId).orElse(null)
if (copyTrading == null) {
logger.warn("跟单关系不存在,跳过更新: copyTradingId=${order.copyTradingId}")
continue
}
// 获取账户
val account = accountRepository.findById(order.accountId).orElse(null)
if (account == null) {
logger.warn("账户不存在,跳过更新: accountId=${order.accountId}")
continue
}
// 检查账户是否配置了 API 凭证
if (account.apiKey == null || account.apiSecret == null || account.apiPassphrase == null) {
logger.debug("账户未配置 API 凭证,跳过更新: accountId=${account.id}")
continue
}
// 解密 API 凭证
val apiSecret = try {
cryptoUtils.decrypt(account.apiSecret!!)
@@ -640,14 +566,14 @@ class OrderStatusUpdateService(
logger.warn("解密 API Secret 失败: accountId=${account.id}, error=${e.message}")
continue
}
val apiPassphrase = try {
cryptoUtils.decrypt(account.apiPassphrase!!)
} catch (e: Exception) {
logger.warn("解密 API Passphrase 失败: accountId=${account.id}, error=${e.message}")
continue
}
// 创建带认证的 CLOB API 客户端
val clobApi = retrofitFactory.createClobApi(
account.apiKey!!,
@@ -655,91 +581,49 @@ class OrderStatusUpdateService(
apiPassphrase,
account.walletAddress
)
// 查询订单详情
val orderResponse = clobApi.getOrder(order.buyOrderId)
// 先检查 HTTP 状态码,非 200 的都跳过
if (orderResponse.code() != 200) {
val errorBody = orderResponse.errorBody()?.string()?.take(200) ?: "无错误详情"
logger.debug("查询订单详情失败(HTTP非200),等待下次轮询: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}, code=${orderResponse.code()}, errorBody=$errorBody")
continue
}
// HTTP 200,检查响应体
// 响应体也可能返回字符串 "null"Gson 解析时会返回 null
val orderDetail = orderResponse.body()
if (orderDetail == null) {
// HTTP 200 且响应体为 null(或字符串 "null"),可能是网络异常或 API 暂时不可用
// 使用兜底逻辑:首次检测不删除,1分钟后仍为 null 才删除
val firstDetectionTime =
orderNullDetectionTime.getOrPut(order.buyOrderId) { System.currentTimeMillis() }
val currentTime = System.currentTimeMillis()
// HTTP 200 且响应体为 null(或字符串 "null"),表示订单不存在
// 检查订单是否已部分卖出,如果已部分卖出则保留订单用于统计
val hasMatchedDetails = sellMatchDetailRepository.findByTrackingId(order.id!!).isNotEmpty()
if (hasMatchedDetails || order.matchedQuantity > BigDecimal.ZERO) {
// 检查是否超过清理时间窗口(1小时)
val orderAge = currentTime - order.createdAt
if (orderAge >= PARTIAL_SOLD_CLEANUP_WINDOW_MS) {
logger.warn("订单详情为 null 且已部分卖出,但超过清理时间窗口,删除本地订单: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}, matchedQuantity=${order.matchedQuantity}, 已等待=${orderAge / 1000}s")
try {
copyOrderTrackingRepository.deleteById(order.id!!)
logger.info("已删除本地订单(超时清理): orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}")
// 清除缓存
orderNullDetectionTime.remove(order.buyOrderId)
} catch (e: Exception) {
logger.error(
"删除本地订单失败: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}, error=${e.message}",
e
)
}
continue
} else {
logger.debug("订单详情为 null 但已部分卖出,保留订单用于统计: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}, matchedQuantity=${order.matchedQuantity}, 已等待=${orderAge / 1000}s")
// 清除缓存,下次重新检测
orderNullDetectionTime.remove(order.buyOrderId)
continue
}
}
// 检查是否超过重试时间窗口
if (currentTime - firstDetectionTime < ORDER_NULL_RETRY_WINDOW_MS) {
// 未超过重试窗口,记录日志并等待下次轮询
val elapsedSeconds = ((currentTime - firstDetectionTime) / 1000).toInt()
logger.debug("订单详情为 null(可能是网络异常),等待重试: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}, 已等待=${elapsedSeconds}s, 重试窗口=${ORDER_NULL_RETRY_WINDOW_MS / 1000}s")
logger.debug("订单不存在但已部分卖出,保留订单用于统计: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}, matchedQuantity=${order.matchedQuantity}")
continue
}
// 超过重试窗口,删除本地订单
logger.warn("订单详情为 null 超过重试窗口,删除本地订单: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}, 已等待=$((currentTime - firstDetectionTime) / 1000)s")
// 订单不存在且未部分卖出,删除本地订单
logger.info("订单不存在(HTTP 200 但响应体为空),删除本地订单: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}")
try {
copyOrderTrackingRepository.deleteById(order.id!!)
logger.info("已删除本地订单: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}")
// 清除缓存
orderNullDetectionTime.remove(order.buyOrderId)
} catch (e: Exception) {
logger.error(
"删除本地订单失败: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}, error=${e.message}",
e
)
logger.error("删除本地订单失败: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}, error=${e.message}", e)
}
continue
}
// 订单详情不为 null,清除缓存
orderNullDetectionTime.remove(order.buyOrderId)
// 获取实际价格和数量
val actualPrice = orderDetail.price?.toSafeBigDecimal() ?: order.price
val actualSize = orderDetail.originalSize?.toSafeBigDecimal() ?: order.quantity
val actualOutcome = orderDetail.outcome
// 更新订单数据(如果实际数据与临时数据不同)
val needUpdate = actualPrice != order.price || actualSize != order.quantity
// 先保存更新后的订单,标记 notificationSent = true
// 这样可以防止其他并发任务重复发送通知
// 创建更新后的订单对象
val updatedOrder = CopyOrderTracking(
id = order.id,
copyTradingId = order.copyTradingId,
@@ -756,20 +640,19 @@ class OrderStatusUpdateService(
remainingQuantity = order.remainingQuantity,
status = order.status,
notificationSent = true, // 标记为已发送通知
source = order.source, // 保留原始订单来源
createdAt = order.createdAt,
updatedAt = System.currentTimeMillis()
)
// 保存更新后的订单(在发送通知之前保存)
// 保存更新后的订单
copyOrderTrackingRepository.save(updatedOrder)
if (needUpdate) {
logger.info("更新买入订单数据成功: orderId=${order.buyOrderId}, 原价格=${order.price}, 新价格=$actualPrice, 原数量=${order.quantity}, 新数量=$actualSize")
} else {
logger.debug("买入订单数据无需更新: orderId=${order.buyOrderId}")
}
// 发送通知(使用实际数据)
sendBuyOrderNotification(
order = updatedOrder,
@@ -780,8 +663,7 @@ class OrderStatusUpdateService(
copyTrading = copyTrading,
clobApi = clobApi,
apiSecret = apiSecret,
apiPassphrase = apiPassphrase,
orderCreatedAt = order.createdAt
apiPassphrase = apiPassphrase
)
} catch (e: Exception) {
logger.warn("更新买入订单失败: orderId=${order.buyOrderId}, error=${e.message}", e)
@@ -792,7 +674,7 @@ class OrderStatusUpdateService(
logger.error("更新待发送通知买入订单异常: ${e.message}", e)
}
}
/**
* 发送买入订单通知
*/
@@ -806,13 +688,12 @@ class OrderStatusUpdateService(
copyTrading: CopyTrading? = null,
clobApi: PolymarketClobApi? = null,
apiSecret: String? = null,
apiPassphrase: String? = null,
orderCreatedAt: Long? = null // 订单创建时间(毫秒时间戳)
apiPassphrase: String? = null
) {
if (telegramNotificationService == null) {
return
}
try {
// 获取跟单关系和账户信息(如果未提供)
val finalCopyTrading = copyTrading ?: copyTradingRepository.findById(order.copyTradingId).orElse(null)
@@ -820,48 +701,62 @@ class OrderStatusUpdateService(
logger.warn("跟单关系不存在,跳过发送通知: copyTradingId=${order.copyTradingId}")
return
}
val finalAccount = account ?: accountRepository.findById(order.accountId).orElse(null)
if (finalAccount == null) {
logger.warn("账户不存在,跳过发送通知: accountId=${order.accountId}")
return
}
// 获取市场信息
val market = marketService.getMarket(order.marketId)
val marketTitle = market?.title ?: order.marketId
val marketInfo = withContext(Dispatchers.IO) {
try {
val gammaApi = retrofitFactory.createGammaApi()
val marketResponse = gammaApi.listMarkets(conditionIds = listOf(order.marketId))
if (marketResponse.isSuccessful && marketResponse.body() != null) {
marketResponse.body()!!.firstOrNull()
} else {
null
}
} catch (e: Exception) {
logger.warn("获取市场信息失败: ${e.message}", e)
null
}
}
val marketTitle = marketInfo?.question ?: order.marketId
val marketSlug = marketInfo?.slug
// 获取 Leader 和跟单配置信息
val leader = leaderRepository.findById(order.leaderId).orElse(null)
val leaderName = leader?.leaderName
val configName = finalCopyTrading.configName
// 获取当前语言设置
val locale = try {
LocaleContextHolder.getLocale()
} catch (e: Exception) {
java.util.Locale("zh", "CN") // 默认简体中文
}
// 创建 CLOB API 客户端(如果未提供)
val finalClobApi =
clobApi ?: if (finalAccount.apiKey != null && apiSecret != null && apiPassphrase != null) {
retrofitFactory.createClobApi(
finalAccount.apiKey!!,
apiSecret,
apiPassphrase,
finalAccount.walletAddress
)
} else {
null
}
val finalClobApi = clobApi ?: if (finalAccount.apiKey != null && apiSecret != null && apiPassphrase != null) {
retrofitFactory.createClobApi(
finalAccount.apiKey!!,
apiSecret,
apiPassphrase,
finalAccount.walletAddress
)
} else {
null
}
// 发送通知
telegramNotificationService.sendOrderSuccessNotification(
orderId = order.buyOrderId,
marketTitle = marketTitle,
marketId = order.marketId,
marketSlug = market?.eventSlug, // 跳转用的 slug
marketSlug = marketSlug,
side = "BUY",
price = actualPrice ?: order.price.toString(), // 使用实际价格或临时价格
size = actualSize ?: order.quantity.toString(), // 使用实际数量或临时数量
@@ -875,17 +770,16 @@ class OrderStatusUpdateService(
walletAddressForApi = finalAccount.walletAddress,
locale = locale,
leaderName = leaderName,
configName = configName,
orderTime = orderCreatedAt // 使用订单创建时间
configName = configName
)
logger.info("买入订单通知已发送: orderId=${order.buyOrderId}, copyTradingId=${order.copyTradingId}")
} catch (e: Exception) {
logger.warn("发送买入订单通知失败: orderId=${order.buyOrderId}, error=${e.message}", e)
}
}
/**
* 发送卖出订单通知
*/
@@ -899,13 +793,12 @@ class OrderStatusUpdateService(
copyTrading: CopyTrading? = null,
clobApi: PolymarketClobApi? = null,
apiSecret: String? = null,
apiPassphrase: String? = null,
orderCreatedAt: Long? = null // 订单创建时间(毫秒时间戳)
apiPassphrase: String? = null
) {
if (telegramNotificationService == null) {
return
}
try {
// 获取跟单关系和账户信息(如果未提供)
val finalCopyTrading = copyTrading ?: copyTradingRepository.findById(record.copyTradingId).orElse(null)
@@ -913,48 +806,62 @@ class OrderStatusUpdateService(
logger.warn("跟单关系不存在,跳过发送通知: copyTradingId=${record.copyTradingId}")
return
}
val finalAccount = account ?: accountRepository.findById(finalCopyTrading.accountId).orElse(null)
if (finalAccount == null) {
logger.warn("账户不存在,跳过发送通知: accountId=${finalCopyTrading.accountId}")
return
}
// 获取市场信息
val market = marketService.getMarket(record.marketId)
val marketTitle = market?.title ?: record.marketId
val marketInfo = withContext(Dispatchers.IO) {
try {
val gammaApi = retrofitFactory.createGammaApi()
val marketResponse = gammaApi.listMarkets(conditionIds = listOf(record.marketId))
if (marketResponse.isSuccessful && marketResponse.body() != null) {
marketResponse.body()!!.firstOrNull()
} else {
null
}
} catch (e: Exception) {
logger.warn("获取市场信息失败: ${e.message}", e)
null
}
}
val marketTitle = marketInfo?.question ?: record.marketId
val marketSlug = marketInfo?.slug
// 获取 Leader 和跟单配置信息
val leader = leaderRepository.findById(finalCopyTrading.leaderId).orElse(null)
val leaderName = leader?.leaderName
val configName = finalCopyTrading.configName
// 获取当前语言设置
val locale = try {
LocaleContextHolder.getLocale()
} catch (e: Exception) {
java.util.Locale("zh", "CN") // 默认简体中文
}
// 创建 CLOB API 客户端(如果未提供)
val finalClobApi =
clobApi ?: if (finalAccount.apiKey != null && apiSecret != null && apiPassphrase != null) {
retrofitFactory.createClobApi(
finalAccount.apiKey!!,
apiSecret,
apiPassphrase,
finalAccount.walletAddress
)
} else {
null
}
val finalClobApi = clobApi ?: if (finalAccount.apiKey != null && apiSecret != null && apiPassphrase != null) {
retrofitFactory.createClobApi(
finalAccount.apiKey!!,
apiSecret,
apiPassphrase,
finalAccount.walletAddress
)
} else {
null
}
// 发送通知
telegramNotificationService.sendOrderSuccessNotification(
orderId = record.sellOrderId,
marketTitle = marketTitle,
marketId = record.marketId,
marketSlug = market?.eventSlug, // 跳转用的 slug
marketSlug = marketSlug,
side = "SELL",
price = actualPrice ?: record.sellPrice.toString(), // 使用实际价格或临时价格
size = actualSize ?: record.totalMatchedQuantity.toString(), // 使用实际数量或临时数量
@@ -968,10 +875,9 @@ class OrderStatusUpdateService(
walletAddressForApi = finalAccount.walletAddress,
locale = locale,
leaderName = leaderName,
configName = configName,
orderTime = orderCreatedAt // 使用订单创建时间
configName = configName
)
logger.info("卖出订单通知已发送: orderId=${record.sellOrderId}, copyTradingId=${record.copyTradingId}")
} catch (e: Exception) {
logger.warn("发送卖出订单通知失败: orderId=${record.sellOrderId}, error=${e.message}", e)
@@ -62,8 +62,7 @@ class CopyTradingTemplateService(
minOrderDepth = request.minOrderDepth?.toSafeBigDecimal(),
maxSpread = request.maxSpread?.toSafeBigDecimal(),
minPrice = request.minPrice?.toSafeBigDecimal(),
maxPrice = request.maxPrice?.toSafeBigDecimal(),
pushFilteredOrders = request.pushFilteredOrders ?: false
maxPrice = request.maxPrice?.toSafeBigDecimal()
)
val saved = templateRepository.save(template)
@@ -122,7 +121,6 @@ class CopyTradingTemplateService(
maxSpread = request.maxSpread?.toSafeBigDecimal() ?: template.maxSpread,
minPrice = request.minPrice?.toSafeBigDecimal() ?: template.minPrice,
maxPrice = request.maxPrice?.toSafeBigDecimal() ?: template.maxPrice,
pushFilteredOrders = request.pushFilteredOrders ?: template.pushFilteredOrders,
updatedAt = System.currentTimeMillis()
)
@@ -188,8 +186,7 @@ class CopyTradingTemplateService(
minOrderDepth = request.minOrderDepth?.toSafeBigDecimal() ?: sourceTemplate.minOrderDepth,
maxSpread = request.maxSpread?.toSafeBigDecimal() ?: sourceTemplate.maxSpread,
minPrice = request.minPrice?.toSafeBigDecimal() ?: sourceTemplate.minPrice,
maxPrice = request.maxPrice?.toSafeBigDecimal() ?: sourceTemplate.maxPrice,
pushFilteredOrders = request.pushFilteredOrders ?: sourceTemplate.pushFilteredOrders
maxPrice = request.maxPrice?.toSafeBigDecimal() ?: sourceTemplate.maxPrice
)
val saved = templateRepository.save(newTemplate)
@@ -263,7 +260,6 @@ class CopyTradingTemplateService(
maxSpread = template.maxSpread?.toPlainString(),
minPrice = template.minPrice?.toPlainString(),
maxPrice = template.maxPrice?.toPlainString(),
pushFilteredOrders = template.pushFilteredOrders,
createdAt = template.createdAt,
updatedAt = template.updatedAt
)
@@ -0,0 +1,579 @@
package com.wrbug.polymarketbot.service.nba
import com.wrbug.polymarketbot.api.PolymarketGammaApi
import com.wrbug.polymarketbot.dto.NbaGameDto
import com.wrbug.polymarketbot.dto.NbaGameListRequest
import com.wrbug.polymarketbot.dto.NbaGameListResponse
import com.wrbug.polymarketbot.entity.NbaGame
import com.wrbug.polymarketbot.enums.SportsTagId
import com.wrbug.polymarketbot.repository.NbaGameRepository
import com.wrbug.polymarketbot.util.RetrofitFactory
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.Instant
import java.time.LocalDate
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
/**
* NBA 比赛服务
* 从数据库和 Polymarket API 获取比赛数据
* 优先从数据库获取如果数据不足则增量拉取 API 数据
*/
@Service
class NbaGameService(
private val retrofitFactory: RetrofitFactory,
private val nbaGameRepository: NbaGameRepository
) {
private val logger = LoggerFactory.getLogger(NbaGameService::class.java)
/**
* 获取 NBA 比赛列表
* 优先从数据库获取如果数据不足则增量拉取 API 数据
* 前端传递时间戳后端转换为西8区时间用于过滤
*/
suspend fun getNbaGames(request: NbaGameListRequest): Result<NbaGameListResponse> {
return try {
// 将时间戳转换为西8区(PST/PDT)的日期范围
val pstZone = ZoneId.of("America/Los_Angeles")
val startTimestamp = request.startTimestamp ?: ZonedDateTime.now(pstZone).toInstant().toEpochMilli()
val endTimestamp = request.endTimestamp ?: ZonedDateTime.now(pstZone).plusDays(7).toInstant().toEpochMilli()
val startDate = Instant.ofEpochMilli(startTimestamp).atZone(pstZone).toLocalDate()
val endDate = Instant.ofEpochMilli(endTimestamp).atZone(pstZone).toLocalDate()
// 1. 先从数据库获取数据
val dbGames = nbaGameRepository.findByGameDateBetween(startDate, endDate)
logger.info("从数据库获取到 ${dbGames.size} 个比赛(日期范围:$startDate$endDate")
// 2. 检查是否需要增量拉取
val needFetch = shouldFetchFromApi(dbGames)
if (needFetch) {
logger.info("数据库数据不足,开始增量拉取 API 数据")
// 3. 获取数据库最新的 createdAt,用于增量拉取
val latestGame = nbaGameRepository.findFirstByOrderByCreatedAtDesc()
val incrementalStartDateMin = latestGame?.createdAt?.let {
// 将数据库的 createdAt(时间戳)转换为 UTC ISO 8601 格式
Instant.ofEpochMilli(it)
.atZone(java.time.ZoneOffset.UTC)
.format(DateTimeFormatter.ISO_INSTANT)
} ?: run {
// 如果没有数据库数据,使用一周前的时间
Instant.now()
.minusSeconds(7 * 24 * 60 * 60)
.atZone(java.time.ZoneOffset.UTC)
.format(DateTimeFormatter.ISO_INSTANT)
}
logger.info("使用增量拉取起始时间: $incrementalStartDateMin")
// 4. 增量拉取 API 数据
val apiGames = fetchGamesFromApi(startDate, endDate, incrementalStartDateMin)
// 5. 保存新数据到数据库
if (apiGames.isNotEmpty()) {
saveGamesToDatabase(apiGames)
}
// 6. 合并数据库数据和 API 数据
val allGames = (dbGames + apiGames.map { dtoToEntity(it) }).distinctBy {
"${it.homeTeam}_${it.awayTeam}_${it.gameDate}"
}
// 转换为 DTO
val gameDtos = allGames.map { entityToDto(it) }
// 根据状态过滤
val filteredGames = if (request.gameStatus != null) {
gameDtos.filter { it.gameStatus == request.gameStatus }
} else {
gameDtos
}
Result.success(
NbaGameListResponse(
list = filteredGames,
total = filteredGames.size.toLong()
)
)
} else {
// 数据库数据充足,直接返回
val gameDtos = dbGames.map { entityToDto(it) }
// 根据状态过滤
val filteredGames = if (request.gameStatus != null) {
gameDtos.filter { it.gameStatus == request.gameStatus }
} else {
gameDtos
}
Result.success(
NbaGameListResponse(
list = filteredGames,
total = filteredGames.size.toLong()
)
)
}
} catch (e: Exception) {
logger.error("获取 NBA 比赛列表失败: ${e.message}", e)
Result.failure(e)
}
}
/**
* 判断是否需要从 API 拉取数据
* 逻辑
* 1. 如果数据库没有数据需要拉取
* 2. 如果数据库最新数据的 gameTime 在未来 3 天内0-3 不需要拉取
* 3. 如果数据库最新数据的 gameTime 超过 3 >3 不需要拉取数据太远
* 4. 如果数据库最新数据的 gameTime 已经过去<0需要拉取数据过期
*/
private fun shouldFetchFromApi(dbGames: List<NbaGame>): Boolean {
if (dbGames.isEmpty()) {
logger.info("数据库没有数据,需要从 API 拉取")
return true
}
// 检查最新数据的 gameTime(未来最远的比赛)
val latestGame = dbGames.maxByOrNull { it.gameTime ?: 0L }
if (latestGame?.gameTime == null) {
logger.info("数据库最新数据没有 gameTime,需要从 API 拉取")
return true
}
// 计算最新数据的 gameTime 距离现在的时间(以天为单位)
val now = Instant.now().toEpochMilli()
val gameTime = latestGame.gameTime
val daysDiff = (gameTime - now) / (24 * 60 * 60 * 1000)
// 如果数据已经过去(daysDiff < 0),需要拉取
if (daysDiff < 0) {
logger.info("数据库最新数据的 gameTime 已经过去(${daysDiff} 天前),需要从 API 拉取")
return true
}
// 如果数据在未来 3 天内(0 <= daysDiff <= 3),不需要拉取
if (daysDiff >= 0 && daysDiff <= 3) {
logger.info("数据库数据充足(最新数据 ${daysDiff} 天后,在未来 3 天内),无需从 API 拉取")
return false
}
// 如果数据超过 3 天(daysDiff > 3),不需要拉取(数据太远)
logger.info("数据库最新数据的 gameTime 超过 3 天(${daysDiff} 天后),数据太远,不需要从 API 拉取")
return false
}
/**
* API 拉取比赛数据
*/
private suspend fun fetchGamesFromApi(
startDate: LocalDate,
endDate: LocalDate,
startDateMin: String
): List<NbaGameDto> {
// 从 Polymarket API 获取 NBA 市场(分页拉取)
val gammaApi = retrofitFactory.createGammaApi()
val nbaTagId = SportsTagId.NBA.tagId
// 计算未来3天的时间点(UTC
val threeDaysLater = Instant.now()
.plusSeconds(3 * 24 * 60 * 60) // 加上3天(秒数)
val allMarkets = mutableListOf<com.wrbug.polymarketbot.api.MarketResponse>()
var hasMore = true
var pageCount = 0
var currentStartDateMin = startDateMin
while (hasMore) {
pageCount++
logger.debug("分页拉取第 $pageCount 页,start_date_min: $currentStartDateMin")
val response = gammaApi.listMarkets(
conditionIds = null,
includeTag = true,
tags = listOf(nbaTagId),
active = true, // 只获取活跃的市场
closed = false,
archived = false,
limit = 500, // 使用 500 作为 limit
startDateMin = currentStartDateMin,
sportsMarketTypes = listOf("moneyline") // 直接通过 API 筛选 moneyline 类型
)
if (!response.isSuccessful || response.body() == null) {
logger.error("获取 NBA 市场失败: ${response.code()} ${response.message()}")
break
}
val markets = response.body()!!
logger.info("$pageCount 页获取到 ${markets.size} 个市场")
if (markets.isEmpty()) {
// 没有更多数据了
hasMore = false
break
}
// 先记录最后一项的 createdAt(用于下一次分页)
val lastMarket = markets.last()
val lastCreatedAt = lastMarket.createdAt
if (lastCreatedAt == null) {
// 如果最后一个元素没有 createdAt,停止分页
hasMore = false
logger.warn("数组最后一个元素缺少 createdAt,停止分页")
break
}
// 移除非 NBA 项(根据 resolutionSource 判断)
val nbaMarkets = markets.filter { market ->
!market.resolutionSource.isNullOrBlank() &&
market.resolutionSource!!.lowercase().contains("nba")
}
logger.info("$pageCount 页过滤后剩余 ${nbaMarkets.size} 个 NBA 市场")
// 添加到总列表(只添加 NBA 市场)
allMarkets.addAll(nbaMarkets)
// 从后往前遍历,找到第一个有 gameStartTime 字段的数据(在 NBA 市场中查找)
var foundGameStartTime: String? = null
for (i in nbaMarkets.size - 1 downTo 0) {
val market = nbaMarkets[i]
if (!market.gameStartTime.isNullOrBlank()) {
foundGameStartTime = market.gameStartTime
logger.debug("从后往前找到第 ${i + 1} 个有 gameStartTime 的 NBA 市场: $foundGameStartTime")
break
}
}
if (foundGameStartTime == null) {
// 如果整页都没有 gameStartTime,使用 createdAt 继续分页
currentStartDateMin = lastCreatedAt
logger.debug("本页没有找到 gameStartTime,使用最后一个元素的 createdAt 继续分页")
continue
}
// 解析 gameStartTime(格式:2025-12-13 00:00:00+00
val gameStartDate = try {
// 尝试解析格式 "2025-12-13 00:00:00+00"
val dateTimeStr = foundGameStartTime.replace(" ", "T")
// 如果时区是 +00,转换为 Z
val normalizedStr = if (dateTimeStr.endsWith("+00")) {
dateTimeStr.replace("+00", "Z")
} else if (dateTimeStr.endsWith("-00")) {
dateTimeStr.replace("-00", "Z")
} else {
dateTimeStr
}
val instant = Instant.parse(normalizedStr)
// 转换为日期(以天为单位,不考虑时间)
instant.atZone(java.time.ZoneOffset.UTC).toLocalDate()
} catch (e: Exception) {
logger.warn("解析 gameStartTime 失败: $foundGameStartTime, error: ${e.message}")
null
}
if (gameStartDate == null) {
// 无法解析 gameStartTime,使用 createdAt 继续分页
currentStartDateMin = lastCreatedAt
logger.debug("无法解析 gameStartTime,使用最后一个元素的 createdAt 继续分页")
continue
}
// 计算未来 3 天的日期(以天为单位,不考虑时间)
val threeDaysLaterDate = Instant.now()
.plusSeconds(3 * 24 * 60 * 60) // 加上3天(秒数)
.atZone(java.time.ZoneOffset.UTC)
.toLocalDate()
// 判断 gameStartDate 是否在未来 3 天以内(包括第 3 天)
val daysBetween = java.time.temporal.ChronoUnit.DAYS.between(
Instant.now().atZone(java.time.ZoneOffset.UTC).toLocalDate(),
gameStartDate
)
if (daysBetween <= 3 && daysBetween >= 0) {
// 如果在 3 天内(包括第 3 天),使用数组最后一个元素的 createdAt 继续分页
currentStartDateMin = lastCreatedAt
logger.info("找到的 gameStartTime ($foundGameStartTime, 日期: $gameStartDate) 在未来 ${daysBetween} 天内,继续分页")
} else {
// 如果不在 3 天内,停止分页
hasMore = false
logger.info("找到的 gameStartTime ($foundGameStartTime, 日期: $gameStartDate) 不在未来 3 天内(相差 ${daysBetween} 天),停止分页")
}
}
logger.info("分页拉取完成,共获取 ${allMarkets.size} 个 NBA moneyline 市场(${pageCount} 页)")
// 注意:allMarkets 已经通过 API 的 sports_market_types 参数过滤了 moneyline 类型
// 并且已经过滤了非 NBA 项(根据 resolutionSource),这里直接使用即可
// 将市场转换为比赛数据
val games = allMarkets.mapNotNull { market ->
convertMarketToGame(market, startDate, endDate)
}
// 去重:相同的主队、客队和日期只保留一个
val uniqueGames = games.groupBy { "${it.homeTeam}_${it.awayTeam}_${it.gameDate}" }
.map { it.value.first() }
return uniqueGames
}
/**
* 保存比赛数据到数据库
*/
@Transactional
private fun saveGamesToDatabase(games: List<NbaGameDto>) {
if (games.isEmpty()) {
return
}
var savedCount = 0
var updatedCount = 0
games.forEach { dto ->
try {
// 尝试根据 nbaGameId 或 polymarketMarketId 查找现有记录
val existing = dto.nbaGameId?.let {
nbaGameRepository.findByNbaGameId(it)
} ?: dto.polymarketMarketId?.let {
nbaGameRepository.findByPolymarketMarketId(it)
}
if (existing != null) {
// 更新现有记录(data class 的 copy 方法)
val updated = NbaGame(
id = existing.id,
nbaGameId = existing.nbaGameId,
homeTeam = dto.homeTeam,
awayTeam = dto.awayTeam,
gameDate = dto.gameDate,
gameTime = dto.gameTime,
gameStatus = dto.gameStatus,
homeScore = dto.homeScore,
awayScore = dto.awayScore,
period = dto.period,
timeRemaining = dto.timeRemaining,
polymarketMarketId = dto.polymarketMarketId,
createdAt = existing.createdAt,
updatedAt = System.currentTimeMillis()
)
nbaGameRepository.save(updated)
updatedCount++
} else {
// 创建新记录
val entity = dtoToEntity(dto)
nbaGameRepository.save(entity)
savedCount++
}
} catch (e: Exception) {
logger.error("保存比赛数据失败: ${dto.nbaGameId}, error: ${e.message}", e)
}
}
logger.info("保存比赛数据完成:新增 $savedCount 条,更新 $updatedCount")
}
/**
* DTO 转实体
*/
private fun dtoToEntity(dto: NbaGameDto): NbaGame {
return NbaGame(
id = null,
nbaGameId = dto.nbaGameId,
homeTeam = dto.homeTeam,
awayTeam = dto.awayTeam,
gameDate = dto.gameDate,
gameTime = dto.gameTime,
gameStatus = dto.gameStatus,
homeScore = dto.homeScore,
awayScore = dto.awayScore,
period = dto.period,
timeRemaining = dto.timeRemaining,
polymarketMarketId = dto.polymarketMarketId,
createdAt = System.currentTimeMillis(),
updatedAt = System.currentTimeMillis()
)
}
/**
* 实体转 DTO
*/
private fun entityToDto(entity: NbaGame): NbaGameDto {
return NbaGameDto(
id = entity.id,
nbaGameId = entity.nbaGameId,
homeTeam = entity.homeTeam,
awayTeam = entity.awayTeam,
gameDate = entity.gameDate,
gameTime = entity.gameTime,
gameStatus = entity.gameStatus,
homeScore = entity.homeScore,
awayScore = entity.awayScore,
period = entity.period,
timeRemaining = entity.timeRemaining,
polymarketMarketId = entity.polymarketMarketId
)
}
/**
* 获取 7 天内的所有球队去重
*/
suspend fun getTeamsInNext7Days(): Result<List<String>> {
return try {
// 使用当前西8区时间计算7天范围
val pstZone = ZoneId.of("America/Los_Angeles")
val now = ZonedDateTime.now(pstZone)
val startTimestamp = now.toInstant().toEpochMilli()
val endTimestamp = now.plusDays(7).toInstant().toEpochMilli()
val gamesResult = getNbaGames(
NbaGameListRequest(
startTimestamp = startTimestamp,
endTimestamp = endTimestamp
)
)
gamesResult.fold(
onSuccess = { response ->
val teams = mutableSetOf<String>()
response.list.forEach { game ->
teams.add(game.homeTeam)
teams.add(game.awayTeam)
}
Result.success(teams.sorted())
},
onFailure = { exception -> Result.failure(exception) }
)
} catch (e: Exception) {
logger.error("获取球队列表失败: ${e.message}", e)
Result.failure(e)
}
}
/**
* Polymarket 市场转换为比赛数据
*/
private fun convertMarketToGame(
market: com.wrbug.polymarketbot.api.MarketResponse,
startDate: LocalDate,
endDate: LocalDate
): NbaGameDto? {
if (market.question.isNullOrBlank()) {
return null
}
// 解析市场名称,提取球队和日期信息
val parsed = NbaMarketNameParser.parse(market.question)
if (parsed.homeTeam == null || parsed.awayTeam == null) {
// 无法解析出两个球队,跳过
return null
}
// 确定比赛日期
val gameDate = parsed.gameDate ?: run {
// 如果没有解析出日期,尝试从 startDate 或 endDate 中提取
parseDateFromMarketDates(market.startDate, market.endDate) ?: return null
}
// 检查日期是否在请求范围内
if (gameDate.isBefore(startDate) || gameDate.isAfter(endDate)) {
return null
}
// 解析比赛时间(从 endDate 或 startDate 中提取,转换为西8区时间戳)
val gameTime = parseGameTimeFromMarket(market.startDate, market.endDate, gameDate)
// 确定比赛状态
val gameStatus = when {
market.closed == true -> "finished"
market.archived == true -> "finished"
market.active == true -> "scheduled"
else -> "scheduled"
}
return NbaGameDto(
id = null,
nbaGameId = market.conditionId ?: market.id, // 使用 conditionId 或 id 作为 gameId
homeTeam = parsed.homeTeam,
awayTeam = parsed.awayTeam,
gameDate = gameDate,
gameTime = gameTime, // 西8区时间戳(毫秒)
gameStatus = gameStatus,
homeScore = 0, // Polymarket 不提供比分
awayScore = 0,
period = 0,
timeRemaining = null,
polymarketMarketId = market.id
)
}
/**
* 从市场的 startDate endDate 中解析日期
*/
private fun parseDateFromMarketDates(startDate: String?, endDate: String?): LocalDate? {
val dateStr = endDate ?: startDate ?: return null
return try {
// 尝试解析 ISO 8601 格式
if (dateStr.contains("T")) {
val instant = Instant.parse(dateStr)
val pstZone = ZoneId.of("America/Los_Angeles")
instant.atZone(pstZone).toLocalDate()
} else {
// 尝试解析日期字符串
LocalDate.parse(dateStr, DateTimeFormatter.ISO_DATE)
}
} catch (e: Exception) {
logger.debug("解析市场日期失败: $dateStr, error: ${e.message}")
null
}
}
/**
* 从市场的日期时间中解析比赛时间转换为西8区时间戳
*/
private fun parseGameTimeFromMarket(
startDate: String?,
endDate: String?,
gameDate: LocalDate
): Long? {
val dateTimeStr = endDate ?: startDate ?: return null
return try {
val pstZone = ZoneId.of("America/Los_Angeles")
// 尝试解析 ISO 8601 格式
val instant = if (dateTimeStr.contains("T")) {
Instant.parse(dateTimeStr)
} else {
// 如果没有时间部分,使用默认时间(晚上8点)
val defaultTime = gameDate.atTime(20, 0)
defaultTime.atZone(pstZone).toInstant()
}
// 转换为西8区时间戳
instant.atZone(pstZone).toInstant().toEpochMilli()
} catch (e: Exception) {
logger.debug("解析比赛时间失败: $dateTimeStr, error: ${e.message}")
// 解析失败时,使用默认时间(晚上8点 PST)
try {
val defaultTime = gameDate.atTime(20, 0)
val pstZone = ZoneId.of("America/Los_Angeles")
defaultTime.atZone(pstZone).toInstant().toEpochMilli()
} catch (e2: Exception) {
null
}
}
}
}
@@ -0,0 +1,314 @@
package com.wrbug.polymarketbot.service.nba
import org.slf4j.LoggerFactory
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.regex.Pattern
/**
* NBA 市场名称解析器
* Polymarket 市场名称中提取球队和日期信息
*/
object NbaMarketNameParser {
private val logger = LoggerFactory.getLogger(NbaMarketNameParser::class.java)
// NBA 球队名称映射(支持多种格式)
private val teamNameMapping = mapOf(
// 完整名称
"atlanta hawks" to "Atlanta Hawks",
"boston celtics" to "Boston Celtics",
"brooklyn nets" to "Brooklyn Nets",
"charlotte hornets" to "Charlotte Hornets",
"chicago bulls" to "Chicago Bulls",
"cleveland cavaliers" to "Cleveland Cavaliers",
"dallas mavericks" to "Dallas Mavericks",
"denver nuggets" to "Denver Nuggets",
"detroit pistons" to "Detroit Pistons",
"golden state warriors" to "Golden State Warriors",
"houston rockets" to "Houston Rockets",
"indiana pacers" to "Indiana Pacers",
"la clippers" to "LA Clippers",
"los angeles lakers" to "Los Angeles Lakers",
"memphis grizzlies" to "Memphis Grizzlies",
"miami heat" to "Miami Heat",
"milwaukee bucks" to "Milwaukee Bucks",
"minnesota timberwolves" to "Minnesota Timberwolves",
"new orleans pelicans" to "New Orleans Pelicans",
"new york knicks" to "New York Knicks",
"oklahoma city thunder" to "Oklahoma City Thunder",
"orlando magic" to "Orlando Magic",
"philadelphia 76ers" to "Philadelphia 76ers",
"phoenix suns" to "Phoenix Suns",
"portland trail blazers" to "Portland Trail Blazers",
"sacramento kings" to "Sacramento Kings",
"san antonio spurs" to "San Antonio Spurs",
"toronto raptors" to "Toronto Raptors",
"utah jazz" to "Utah Jazz",
"washington wizards" to "Washington Wizards",
// 常见缩写和别名
"hawks" to "Atlanta Hawks",
"celtics" to "Boston Celtics",
"nets" to "Brooklyn Nets",
"hornets" to "Charlotte Hornets",
"bulls" to "Chicago Bulls",
"cavaliers" to "Cleveland Cavaliers",
"cavs" to "Cleveland Cavaliers",
"mavericks" to "Dallas Mavericks",
"mavs" to "Dallas Mavericks",
"nuggets" to "Denver Nuggets",
"pistons" to "Detroit Pistons",
"warriors" to "Golden State Warriors",
"rockets" to "Houston Rockets",
"pacers" to "Indiana Pacers",
"clippers" to "LA Clippers",
"lakers" to "Los Angeles Lakers",
"grizzlies" to "Memphis Grizzlies",
"heat" to "Miami Heat",
"bucks" to "Milwaukee Bucks",
"timberwolves" to "Minnesota Timberwolves",
"wolves" to "Minnesota Timberwolves",
"pelicans" to "New Orleans Pelicans",
"knicks" to "New York Knicks",
"thunder" to "Oklahoma City Thunder",
"magic" to "Orlando Magic",
"76ers" to "Philadelphia 76ers",
"sixers" to "Philadelphia 76ers",
"suns" to "Phoenix Suns",
"trail blazers" to "Portland Trail Blazers",
"blazers" to "Portland Trail Blazers",
"kings" to "Sacramento Kings",
"spurs" to "San Antonio Spurs",
"raptors" to "Toronto Raptors",
"jazz" to "Utah Jazz",
"wizards" to "Washington Wizards",
"wiz" to "Washington Wizards"
)
/**
* 解析结果
*/
data class ParsedMarketInfo(
val homeTeam: String?,
val awayTeam: String?,
val gameDate: LocalDate?,
val confidence: Double // 置信度 0.0-1.0
)
/**
* 解析市场名称
* @param marketName 市场名称
* @return 解析结果
*/
fun parse(marketName: String?): ParsedMarketInfo {
if (marketName.isNullOrBlank()) {
return ParsedMarketInfo(null, null, null, 0.0)
}
val normalized = marketName.lowercase()
var homeTeam: String? = null
var awayTeam: String? = null
var gameDate: LocalDate? = null
var confidence = 0.0
// 尝试提取球队名称
val teams = extractTeams(normalized)
if (teams.size >= 2) {
// 通常第一个是客队,第二个是主队
awayTeam = teams[0]
homeTeam = teams[1]
confidence += 0.5
} else if (teams.size == 1) {
// 只有一个球队,无法确定主客场
awayTeam = teams[0]
confidence += 0.2
}
// 尝试提取日期
val date = extractDate(normalized)
if (date != null) {
gameDate = date
confidence += 0.3
}
return ParsedMarketInfo(homeTeam, awayTeam, gameDate, confidence.coerceAtMost(1.0))
}
/**
* 提取球队名称
*/
private fun extractTeams(text: String): List<String> {
val teams = mutableListOf<String>()
// 常见的球队名称模式
val patterns = listOf(
// "Team1 vs Team2" 或 "Team1 @ Team2"
Pattern.compile("(\\w+(?:\\s+\\w+)*?)\\s+(?:vs|@|v\\.?|versus)\\s+(\\w+(?:\\s+\\w+)*?)", Pattern.CASE_INSENSITIVE),
// "Will Team1 beat Team2"
Pattern.compile("will\\s+(\\w+(?:\\s+\\w+)*?)\\s+beat\\s+(\\w+(?:\\s+\\w+)*?)", Pattern.CASE_INSENSITIVE),
// "Team1 win" 或 "Team1 wins"
Pattern.compile("(\\w+(?:\\s+\\w+)*?)\\s+win", Pattern.CASE_INSENSITIVE)
)
for (pattern in patterns) {
val matcher = pattern.matcher(text)
if (matcher.find()) {
val team1 = normalizeTeamName(matcher.group(1) ?: "")
val team2 = if (matcher.groupCount() >= 2) {
normalizeTeamName(matcher.group(2) ?: "")
} else null
if (team1 != null) {
teams.add(team1)
}
if (team2 != null) {
teams.add(team2)
}
if (teams.size >= 2) {
break
}
}
}
// 如果模式匹配失败,尝试直接查找球队名称
if (teams.isEmpty()) {
for ((key, value) in teamNameMapping) {
if (text.contains(key, ignoreCase = true)) {
if (!teams.contains(value)) {
teams.add(value)
}
}
}
}
return teams.distinct()
}
/**
* 标准化球队名称
*/
private fun normalizeTeamName(name: String): String? {
val normalized = name.trim().lowercase()
return teamNameMapping[normalized] ?: teamNameMapping.entries.firstOrNull {
normalized.contains(it.key, ignoreCase = true)
}?.value
}
/**
* 提取日期
*/
private fun extractDate(text: String): LocalDate? {
// 尝试多种日期格式
try {
// 格式1: "Dec 15, 2024" 或 "December 15, 2024"
val pattern1 = Pattern.compile("(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[a-z]*\\s+(\\d{1,2}),?\\s+(\\d{4})", Pattern.CASE_INSENSITIVE)
val matcher1 = pattern1.matcher(text)
if (matcher1.find()) {
val monthStr = matcher1.group(1)?.lowercase() ?: return null
val day = matcher1.group(2)?.toIntOrNull() ?: return null
val year = matcher1.group(3)?.toIntOrNull() ?: return null
val monthMap = mapOf(
"jan" to 1, "january" to 1,
"feb" to 2, "february" to 2,
"mar" to 3, "march" to 3,
"apr" to 4, "april" to 4,
"may" to 5,
"jun" to 6, "june" to 6,
"jul" to 7, "july" to 7,
"aug" to 8, "august" to 8,
"sep" to 9, "september" to 9,
"oct" to 10, "october" to 10,
"nov" to 11, "november" to 11,
"dec" to 12, "december" to 12
)
val month = monthMap.entries.firstOrNull { monthStr.startsWith(it.key) }?.value
if (month != null) {
return try {
LocalDate.of(year, month, day)
} catch (e: Exception) {
null
}
}
}
// 格式2: "2024-12-15"
val pattern2 = Pattern.compile("(\\d{4})[-/](\\d{1,2})[-/](\\d{1,2})")
val matcher2 = pattern2.matcher(text)
if (matcher2.find()) {
val year = matcher2.group(1)?.toIntOrNull() ?: return null
val month = matcher2.group(2)?.toIntOrNull() ?: return null
val day = matcher2.group(3)?.toIntOrNull() ?: return null
return try {
LocalDate.of(year, month, day)
} catch (e: Exception) {
null
}
}
// 格式3: "12/15/2024" 或 "12/15/24"
val pattern3 = Pattern.compile("(\\d{1,2})/(\\d{1,2})/(\\d{2,4})")
val matcher3 = pattern3.matcher(text)
if (matcher3.find()) {
val month = matcher3.group(1)?.toIntOrNull() ?: return null
val day = matcher3.group(2)?.toIntOrNull() ?: return null
val yearStr = matcher3.group(3) ?: return null
val year = if (yearStr.length == 2) {
// 两位年份,假设是 2000-2099
val y = yearStr.toIntOrNull() ?: return null
if (y < 50) 2000 + y else 1900 + y
} else {
yearStr.toIntOrNull() ?: return null
}
return try {
LocalDate.of(year, month, day)
} catch (e: Exception) {
null
}
}
} catch (e: Exception) {
logger.debug("解析日期失败: ${e.message}")
}
return null
}
/**
* 匹配比赛和市场
* @param homeTeam 主队名称
* @param awayTeam 客队名称
* @param gameDate 比赛日期
* @param parsedMarket 解析的市场信息
* @return 是否匹配
*/
fun matchGame(
homeTeam: String,
awayTeam: String,
gameDate: LocalDate,
parsedMarket: ParsedMarketInfo
): Boolean {
// 检查日期是否匹配(允许1天误差)
val dateMatch = parsedMarket.gameDate?.let { marketDate ->
val daysDiff = kotlin.math.abs(java.time.temporal.ChronoUnit.DAYS.between(gameDate, marketDate))
daysDiff <= 1
} ?: false
if (!dateMatch && parsedMarket.gameDate != null) {
return false
}
// 检查球队是否匹配
val homeMatch = parsedMarket.homeTeam?.let {
normalizeTeamName(it)?.equals(normalizeTeamName(homeTeam), ignoreCase = true)
} ?: false
val awayMatch = parsedMarket.awayTeam?.let {
normalizeTeamName(it)?.equals(normalizeTeamName(awayTeam), ignoreCase = true)
} ?: false
// 如果两个球队都匹配,或者至少一个匹配且日期匹配
return (homeMatch && awayMatch) || ((homeMatch || awayMatch) && dateMatch)
}
}
@@ -0,0 +1,165 @@
package com.wrbug.polymarketbot.service.nba
import com.wrbug.polymarketbot.api.MarketResponse
import com.wrbug.polymarketbot.api.PolymarketGammaApi
import com.wrbug.polymarketbot.dto.NbaMarketDto
import com.wrbug.polymarketbot.dto.NbaMarketListRequest
import com.wrbug.polymarketbot.dto.NbaMarketListResponse
import com.wrbug.polymarketbot.enums.SportsTagId
import com.wrbug.polymarketbot.util.RetrofitFactory
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
/**
* NBA 市场服务
* 用于从 Polymarket 获取 NBA 相关的市场信息
*/
@Service
class NbaMarketService(
private val retrofitFactory: RetrofitFactory
) {
private val logger = LoggerFactory.getLogger(NbaMarketService::class.java)
/**
* 获取 NBA tag ID 列表
* 直接使用枚举中定义的已知 tag ID无需调用 API
*/
suspend fun getNbaTagIds(): Result<List<String>> {
// 直接使用枚举中定义的 NBA tag ID
val nbaTagId = SportsTagId.NBA.tagId
logger.debug("使用枚举中的 NBA tag ID: $nbaTagId")
return Result.success(listOf(nbaTagId))
}
/**
* 获取 NBA 市场列表
* 使用 NBA tag IDs 过滤市场
*
* @param request 请求参数
* @return NBA 市场列表响应
*/
suspend fun getNbaMarkets(request: NbaMarketListRequest): Result<NbaMarketListResponse> {
return try {
// 先获取 NBA 的 tag IDs
val tagIdsResult = getNbaTagIds()
if (tagIdsResult.isFailure) {
return Result.failure(tagIdsResult.exceptionOrNull() ?: Exception("无法获取 NBA tag IDs"))
}
val tagIds = tagIdsResult.getOrNull() ?: return Result.failure(IllegalStateException("NBA tag IDs 为空"))
if (tagIds.isEmpty()) {
logger.warn("NBA tag IDs 为空,无法过滤市场")
return Result.success(NbaMarketListResponse(
list = emptyList(),
total = 0L
))
}
// 调用 /markets 接口,使用 tag IDs 过滤
val gammaApi = retrofitFactory.createGammaApi()
val response = gammaApi.listMarkets(
conditionIds = null,
includeTag = true,
tags = tagIds,
active = request.active,
closed = request.closed,
archived = request.archived
)
if (response.isSuccessful && response.body() != null) {
val markets = response.body()!!
logger.info("获取到 ${markets.size} 个 NBA 市场")
// 转换为 DTO
val marketDtos = markets.map { market ->
NbaMarketDto(
id = market.id,
question = market.question,
conditionId = market.conditionId,
slug = market.slug,
description = market.description,
category = market.category,
active = market.active,
closed = market.closed,
archived = market.archived,
volume = market.volume,
liquidity = market.liquidity,
endDate = market.endDate,
startDate = market.startDate,
outcomes = market.outcomes,
outcomePrices = market.outcomePrices,
volumeNum = market.volumeNum,
liquidityNum = market.liquidityNum,
lastTradePrice = market.lastTradePrice,
bestBid = market.bestBid,
bestAsk = market.bestAsk
)
}
Result.success(NbaMarketListResponse(
list = marketDtos,
total = marketDtos.size.toLong()
))
} else {
logger.error("获取 NBA 市场失败: ${response.code()} ${response.message()}")
val errorBody = response.errorBody()?.string()
logger.error("错误响应体: $errorBody")
Result.failure(Exception("获取 NBA 市场失败: ${response.code()} ${response.message()}"))
}
} catch (e: Exception) {
logger.error("获取 NBA 市场异常: ${e.message}", e)
Result.failure(e)
}
}
/**
* NBA 市场中提取球队列表
* 解析市场名称提取所有唯一的球队名称
*
* @param active 是否只从活跃市场提取默认 true
* @return 球队名称列表去重排序
*/
suspend fun getTeamsFromMarkets(active: Boolean = true): Result<List<String>> {
return try {
// 获取 NBA 市场列表
val marketsResult = getNbaMarkets(
NbaMarketListRequest(
active = active,
closed = false,
archived = false
)
)
if (marketsResult.isFailure) {
return Result.failure(marketsResult.exceptionOrNull() ?: Exception("无法获取 NBA 市场"))
}
val markets = marketsResult.getOrNull()?.list ?: return Result.success(emptyList())
// 使用市场名称解析器提取球队
val teams = mutableSetOf<String>()
markets.forEach { market ->
if (!market.question.isNullOrBlank()) {
val parsed = NbaMarketNameParser.parse(market.question)
if (parsed != null) {
// 提取主队和客队
parsed.homeTeam?.let { teams.add(it) }
parsed.awayTeam?.let { teams.add(it) }
}
}
}
// 排序并返回
val sortedTeams = teams.sorted()
logger.info("${markets.size} 个市场中提取到 ${sortedTeams.size} 个球队")
Result.success(sortedTeams)
} catch (e: Exception) {
logger.error("从市场提取球队列表失败: ${e.message}", e)
Result.failure(e)
}
}
}
@@ -0,0 +1,346 @@
package com.wrbug.polymarketbot.service.nba
import com.wrbug.polymarketbot.dto.*
import com.wrbug.polymarketbot.entity.NbaQuantitativeStrategy
import com.wrbug.polymarketbot.repository.AccountRepository
import com.wrbug.polymarketbot.repository.NbaQuantitativeStrategyRepository
import com.wrbug.polymarketbot.util.JsonUtils
import com.wrbug.polymarketbot.util.toSafeBigDecimal
import org.slf4j.LoggerFactory
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Sort
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.math.BigDecimal
/**
* NBA 量化策略服务
*/
@Service
class NbaQuantitativeStrategyService(
private val strategyRepository: NbaQuantitativeStrategyRepository,
private val accountRepository: AccountRepository
) {
private val logger = LoggerFactory.getLogger(NbaQuantitativeStrategyService::class.java)
/**
* 创建策略
*/
@Transactional
suspend fun createStrategy(request: NbaQuantitativeStrategyCreateRequest): Result<NbaQuantitativeStrategyDto> {
return try {
// 验证账户是否存在
val account = accountRepository.findById(request.accountId).orElse(null)
if (account == null) {
return Result.failure(IllegalArgumentException("账户不存在"))
}
// 验证策略名称是否重复
val existing = strategyRepository.findByStrategyName(request.strategyName)
if (existing != null) {
return Result.failure(IllegalArgumentException("策略名称已存在"))
}
// 创建策略实体
val strategy = NbaQuantitativeStrategy(
strategyName = request.strategyName,
strategyDescription = request.strategyDescription,
accountId = request.accountId,
enabled = request.enabled,
filterTeams = request.filterTeams?.let { JsonUtils.toJson(it) },
filterDateFrom = request.filterDateFrom,
filterDateTo = request.filterDateTo,
filterGameImportance = request.filterGameImportance,
minWinProbabilityDiff = request.minWinProbabilityDiff ?: BigDecimal("0.1"),
minWinProbability = request.minWinProbability,
maxWinProbability = request.maxWinProbability,
minTradeValue = request.minTradeValue ?: BigDecimal("0.05"),
minRemainingTime = request.minRemainingTime,
maxRemainingTime = request.maxRemainingTime,
minScoreDiff = request.minScoreDiff,
maxScoreDiff = request.maxScoreDiff,
buyAmountStrategy = request.buyAmountStrategy ?: "FIXED",
fixedBuyAmount = request.fixedBuyAmount,
buyRatio = request.buyRatio,
baseBuyAmount = request.baseBuyAmount,
buyTiming = request.buyTiming ?: "IMMEDIATE",
delayBuySeconds = request.delayBuySeconds ?: 0,
buyDirection = request.buyDirection ?: "AUTO",
enableSell = request.enableSell ?: true,
takeProfitThreshold = request.takeProfitThreshold,
stopLossThreshold = request.stopLossThreshold,
probabilityReversalThreshold = request.probabilityReversalThreshold,
sellRatio = request.sellRatio ?: BigDecimal("1.0"),
sellTiming = request.sellTiming ?: "IMMEDIATE",
delaySellSeconds = request.delaySellSeconds ?: 0,
priceStrategy = request.priceStrategy ?: "MARKET",
fixedPrice = request.fixedPrice,
priceOffset = request.priceOffset ?: BigDecimal.ZERO,
maxPosition = request.maxPosition ?: BigDecimal("50"),
minPosition = request.minPosition ?: BigDecimal("5"),
maxGamePosition = request.maxGamePosition,
maxDailyLoss = request.maxDailyLoss,
maxDailyOrders = request.maxDailyOrders,
maxDailyProfit = request.maxDailyProfit,
priceTolerance = request.priceTolerance ?: BigDecimal("0.05"),
minProbabilityThreshold = request.minProbabilityThreshold,
maxProbabilityThreshold = request.maxProbabilityThreshold,
baseStrengthWeight = request.baseStrengthWeight ?: BigDecimal("0.3"),
recentFormWeight = request.recentFormWeight ?: BigDecimal("0.25"),
lineupIntegrityWeight = request.lineupIntegrityWeight ?: BigDecimal("0.2"),
starStatusWeight = request.starStatusWeight ?: BigDecimal("0.15"),
environmentWeight = request.environmentWeight ?: BigDecimal("0.1"),
matchupAdvantageWeight = request.matchupAdvantageWeight ?: BigDecimal("0.2"),
scoreDiffWeight = request.scoreDiffWeight ?: BigDecimal("0.3"),
momentumWeight = request.momentumWeight ?: BigDecimal("0.2"),
dataUpdateFrequency = request.dataUpdateFrequency ?: 30,
analysisFrequency = request.analysisFrequency ?: 30,
pushFailedOrders = request.pushFailedOrders ?: false,
pushFrequency = request.pushFrequency ?: "REALTIME",
batchPushInterval = request.batchPushInterval ?: 1
)
val saved = strategyRepository.save(strategy)
Result.success(toDto(saved))
} catch (e: Exception) {
logger.error("创建策略失败: ${e.message}", e)
Result.failure(e)
}
}
/**
* 更新策略
*/
@Transactional
suspend fun updateStrategy(request: NbaQuantitativeStrategyUpdateRequest): Result<NbaQuantitativeStrategyDto> {
return try {
val strategy = strategyRepository.findById(request.id).orElse(null)
if (strategy == null) {
return Result.failure(IllegalArgumentException("策略不存在"))
}
// 更新字段(只更新提供的字段)
val updated = strategy.copy(
strategyName = request.strategyName ?: strategy.strategyName,
strategyDescription = request.strategyDescription ?: strategy.strategyDescription,
enabled = request.enabled ?: strategy.enabled,
filterTeams = request.filterTeams?.let { JsonUtils.toJson(it) } ?: strategy.filterTeams,
filterDateFrom = request.filterDateFrom ?: strategy.filterDateFrom,
filterDateTo = request.filterDateTo ?: strategy.filterDateTo,
filterGameImportance = request.filterGameImportance ?: strategy.filterGameImportance,
minWinProbabilityDiff = request.minWinProbabilityDiff ?: strategy.minWinProbabilityDiff,
minWinProbability = request.minWinProbability ?: strategy.minWinProbability,
maxWinProbability = request.maxWinProbability ?: strategy.maxWinProbability,
minTradeValue = request.minTradeValue ?: strategy.minTradeValue,
minRemainingTime = request.minRemainingTime ?: strategy.minRemainingTime,
maxRemainingTime = request.maxRemainingTime ?: strategy.maxRemainingTime,
minScoreDiff = request.minScoreDiff ?: strategy.minScoreDiff,
maxScoreDiff = request.maxScoreDiff ?: strategy.maxScoreDiff,
buyAmountStrategy = request.buyAmountStrategy ?: strategy.buyAmountStrategy,
fixedBuyAmount = request.fixedBuyAmount ?: strategy.fixedBuyAmount,
buyRatio = request.buyRatio ?: strategy.buyRatio,
baseBuyAmount = request.baseBuyAmount ?: strategy.baseBuyAmount,
buyTiming = request.buyTiming ?: strategy.buyTiming,
delayBuySeconds = request.delayBuySeconds ?: strategy.delayBuySeconds,
buyDirection = request.buyDirection ?: strategy.buyDirection,
enableSell = request.enableSell ?: strategy.enableSell,
takeProfitThreshold = request.takeProfitThreshold ?: strategy.takeProfitThreshold,
stopLossThreshold = request.stopLossThreshold ?: strategy.stopLossThreshold,
probabilityReversalThreshold = request.probabilityReversalThreshold ?: strategy.probabilityReversalThreshold,
sellRatio = request.sellRatio ?: strategy.sellRatio,
sellTiming = request.sellTiming ?: strategy.sellTiming,
delaySellSeconds = request.delaySellSeconds ?: strategy.delaySellSeconds,
priceStrategy = request.priceStrategy ?: strategy.priceStrategy,
fixedPrice = request.fixedPrice ?: strategy.fixedPrice,
priceOffset = request.priceOffset ?: strategy.priceOffset,
maxPosition = request.maxPosition ?: strategy.maxPosition,
minPosition = request.minPosition ?: strategy.minPosition,
maxGamePosition = request.maxGamePosition ?: strategy.maxGamePosition,
maxDailyLoss = request.maxDailyLoss ?: strategy.maxDailyLoss,
maxDailyOrders = request.maxDailyOrders ?: strategy.maxDailyOrders,
maxDailyProfit = request.maxDailyProfit ?: strategy.maxDailyProfit,
priceTolerance = request.priceTolerance ?: strategy.priceTolerance,
minProbabilityThreshold = request.minProbabilityThreshold ?: strategy.minProbabilityThreshold,
maxProbabilityThreshold = request.maxProbabilityThreshold ?: strategy.maxProbabilityThreshold,
baseStrengthWeight = request.baseStrengthWeight ?: strategy.baseStrengthWeight,
recentFormWeight = request.recentFormWeight ?: strategy.recentFormWeight,
lineupIntegrityWeight = request.lineupIntegrityWeight ?: strategy.lineupIntegrityWeight,
starStatusWeight = request.starStatusWeight ?: strategy.starStatusWeight,
environmentWeight = request.environmentWeight ?: strategy.environmentWeight,
matchupAdvantageWeight = request.matchupAdvantageWeight ?: strategy.matchupAdvantageWeight,
scoreDiffWeight = request.scoreDiffWeight ?: strategy.scoreDiffWeight,
momentumWeight = request.momentumWeight ?: strategy.momentumWeight,
dataUpdateFrequency = request.dataUpdateFrequency ?: strategy.dataUpdateFrequency,
analysisFrequency = request.analysisFrequency ?: strategy.analysisFrequency,
pushFailedOrders = request.pushFailedOrders ?: strategy.pushFailedOrders,
pushFrequency = request.pushFrequency ?: strategy.pushFrequency,
batchPushInterval = request.batchPushInterval ?: strategy.batchPushInterval,
updatedAt = System.currentTimeMillis()
)
val saved = strategyRepository.save(updated)
Result.success(toDto(saved))
} catch (e: Exception) {
logger.error("更新策略失败: ${e.message}", e)
Result.failure(e)
}
}
/**
* 获取策略列表
*/
suspend fun getStrategyList(request: NbaQuantitativeStrategyListRequest): Result<NbaQuantitativeStrategyListResponse> {
return try {
val page = request.page ?: 1
val limit = request.limit ?: 20
val pageable = PageRequest.of(page - 1, limit, Sort.by(Sort.Direction.DESC, "createdAt"))
val strategies = when {
request.accountId != null && request.enabled != null -> {
strategyRepository.findByAccountIdAndEnabled(request.accountId, request.enabled)
}
request.accountId != null -> {
strategyRepository.findByAccountId(request.accountId)
}
request.enabled != null -> {
strategyRepository.findByEnabled(request.enabled)
}
else -> {
strategyRepository.findAll(pageable).content
}
}
// 过滤策略名称(如果提供)
val filtered = if (request.strategyName != null) {
strategies.filter { it.strategyName.contains(request.strategyName, ignoreCase = true) }
} else {
strategies
}
val total = filtered.size.toLong()
val dtoList = filtered.map { toDto(it) }
Result.success(
NbaQuantitativeStrategyListResponse(
list = dtoList,
total = total,
page = page,
limit = limit
)
)
} catch (e: Exception) {
logger.error("获取策略列表失败: ${e.message}", e)
Result.failure(e)
}
}
/**
* 获取策略详情
*/
suspend fun getStrategyDetail(id: Long): Result<NbaQuantitativeStrategyDto> {
return try {
val strategy = strategyRepository.findById(id).orElse(null)
if (strategy == null) {
return Result.failure(IllegalArgumentException("策略不存在"))
}
Result.success(toDto(strategy))
} catch (e: Exception) {
logger.error("获取策略详情失败: ${e.message}", e)
Result.failure(e)
}
}
/**
* 删除策略
*/
@Transactional
suspend fun deleteStrategy(id: Long): Result<Unit> {
return try {
val strategy = strategyRepository.findById(id).orElse(null)
if (strategy == null) {
return Result.failure(IllegalArgumentException("策略不存在"))
}
strategyRepository.delete(strategy)
Result.success(Unit)
} catch (e: Exception) {
logger.error("删除策略失败: ${e.message}", e)
Result.failure(e)
}
}
/**
* 获取启用的策略列表
*/
suspend fun getEnabledStrategies(): List<NbaQuantitativeStrategy> {
return strategyRepository.findByEnabled(true)
}
/**
* 转换为 DTO
*/
private fun toDto(strategy: NbaQuantitativeStrategy): NbaQuantitativeStrategyDto {
val account = accountRepository.findById(strategy.accountId).orElse(null)
return NbaQuantitativeStrategyDto(
id = strategy.id,
strategyName = strategy.strategyName,
strategyDescription = strategy.strategyDescription,
accountId = strategy.accountId,
accountName = account?.accountName,
enabled = strategy.enabled,
filterTeams = strategy.filterTeams?.let { JsonUtils.parseStringList(it) },
filterDateFrom = strategy.filterDateFrom,
filterDateTo = strategy.filterDateTo,
filterGameImportance = strategy.filterGameImportance,
minWinProbabilityDiff = strategy.minWinProbabilityDiff,
minWinProbability = strategy.minWinProbability,
maxWinProbability = strategy.maxWinProbability,
minTradeValue = strategy.minTradeValue,
minRemainingTime = strategy.minRemainingTime,
maxRemainingTime = strategy.maxRemainingTime,
minScoreDiff = strategy.minScoreDiff,
maxScoreDiff = strategy.maxScoreDiff,
buyAmountStrategy = strategy.buyAmountStrategy,
fixedBuyAmount = strategy.fixedBuyAmount,
buyRatio = strategy.buyRatio,
baseBuyAmount = strategy.baseBuyAmount,
buyTiming = strategy.buyTiming,
delayBuySeconds = strategy.delayBuySeconds,
buyDirection = strategy.buyDirection,
enableSell = strategy.enableSell,
takeProfitThreshold = strategy.takeProfitThreshold,
stopLossThreshold = strategy.stopLossThreshold,
probabilityReversalThreshold = strategy.probabilityReversalThreshold,
sellRatio = strategy.sellRatio,
sellTiming = strategy.sellTiming,
delaySellSeconds = strategy.delaySellSeconds,
priceStrategy = strategy.priceStrategy,
fixedPrice = strategy.fixedPrice,
priceOffset = strategy.priceOffset,
maxPosition = strategy.maxPosition,
minPosition = strategy.minPosition,
maxGamePosition = strategy.maxGamePosition,
maxDailyLoss = strategy.maxDailyLoss,
maxDailyOrders = strategy.maxDailyOrders,
maxDailyProfit = strategy.maxDailyProfit,
priceTolerance = strategy.priceTolerance,
minProbabilityThreshold = strategy.minProbabilityThreshold,
maxProbabilityThreshold = strategy.maxProbabilityThreshold,
baseStrengthWeight = strategy.baseStrengthWeight,
recentFormWeight = strategy.recentFormWeight,
lineupIntegrityWeight = strategy.lineupIntegrityWeight,
starStatusWeight = strategy.starStatusWeight,
environmentWeight = strategy.environmentWeight,
matchupAdvantageWeight = strategy.matchupAdvantageWeight,
scoreDiffWeight = strategy.scoreDiffWeight,
momentumWeight = strategy.momentumWeight,
dataUpdateFrequency = strategy.dataUpdateFrequency,
analysisFrequency = strategy.analysisFrequency,
pushFailedOrders = strategy.pushFailedOrders,
pushFrequency = strategy.pushFrequency,
batchPushInterval = strategy.batchPushInterval,
createdAt = strategy.createdAt,
updatedAt = strategy.updatedAt
)
}
}
@@ -1,6 +1,5 @@
package com.wrbug.polymarketbot.service.system
import com.wrbug.polymarketbot.constants.PolymarketConstants
import com.wrbug.polymarketbot.dto.ApiHealthCheckDto
import com.wrbug.polymarketbot.dto.ApiHealthCheckResponse
import com.wrbug.polymarketbot.util.createClient
@@ -12,9 +11,9 @@ import org.slf4j.LoggerFactory
import org.springframework.beans.BeansException
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware
import org.springframework.beans.factory.annotation.Value
import com.wrbug.polymarketbot.service.copytrading.orders.OrderPushService
import com.wrbug.polymarketbot.service.copytrading.monitor.PolymarketActivityWsService
import com.wrbug.polymarketbot.service.copytrading.monitor.UnifiedOnChainWsService
import com.wrbug.polymarketbot.service.copytrading.monitor.CopyTradingWebSocketService
import org.springframework.stereotype.Service
import java.util.concurrent.TimeUnit
@@ -23,6 +22,16 @@ import java.util.concurrent.TimeUnit
*/
@Service
class ApiHealthCheckService(
@Value("\${polymarket.clob.base-url}")
private val clobBaseUrl: String,
@Value("\${polymarket.data-api.base-url}")
private val dataApiBaseUrl: String,
@Value("\${polymarket.gamma.base-url}")
private val gammaBaseUrl: String,
@Value("\${polymarket.rtds.ws-url}")
private val polymarketWsUrl: String,
@Value("\${polymarket.builder.relayer-url:}")
private val builderRelayerUrl: String,
private val rpcNodeService: RpcNodeService
) : ApplicationContextAware {
@@ -43,6 +52,17 @@ class ApiHealthCheckService(
}
}
/**
* 获取跟单 WebSocket 服务通过 ApplicationContext 避免循环依赖
*/
private fun getCopyTradingWebSocketService(): CopyTradingWebSocketService? {
return try {
applicationContext?.getBean(CopyTradingWebSocketService::class.java)
} catch (e: BeansException) {
null
}
}
/**
* 获取 RelayClientService通过 ApplicationContext 避免循环依赖
*/
@@ -54,28 +74,6 @@ class ApiHealthCheckService(
}
}
/**
* 获取 PolymarketActivityWsService通过 ApplicationContext 避免循环依赖
*/
private fun getPolymarketActivityWsService(): PolymarketActivityWsService? {
return try {
applicationContext?.getBean(PolymarketActivityWsService::class.java)
} catch (e: BeansException) {
null
}
}
/**
* 获取 UnifiedOnChainWsService通过 ApplicationContext 避免循环依赖
*/
private fun getUnifiedOnChainWsService(): UnifiedOnChainWsService? {
return try {
applicationContext?.getBean(UnifiedOnChainWsService::class.java)
} catch (e: BeansException) {
null
}
}
private val logger = LoggerFactory.getLogger(ApiHealthCheckService::class.java)
/**
@@ -91,9 +89,7 @@ class ApiHealthCheckService(
async { checkDataApi() },
async { checkGammaApi() },
async { checkPolygonRpc() },
async { checkPolymarketRtdsWebSocket() },
async { checkPolymarketActivityWebSocket() },
async { checkUnifiedOnChainWebSocket() },
async { checkPolymarketWebSocket() },
async { checkBuilderRelayerApi() },
async { checkGitHubApi() }
)
@@ -110,7 +106,7 @@ class ApiHealthCheckService(
* 检查 Polymarket CLOB API
*/
private suspend fun checkClobApi(): ApiHealthCheckDto = withContext(Dispatchers.IO) {
val url = "${PolymarketConstants.CLOB_BASE_URL}/"
val url = "$clobBaseUrl/"
checkApi("Polymarket CLOB API", url)
}
@@ -118,7 +114,7 @@ class ApiHealthCheckService(
* 检查 Polymarket Data API
*/
private suspend fun checkDataApi(): ApiHealthCheckDto = withContext(Dispatchers.IO) {
val url = "${PolymarketConstants.DATA_API_BASE_URL}/"
val url = "$dataApiBaseUrl/"
checkApi("Polymarket Data API", url)
}
@@ -135,7 +131,7 @@ class ApiHealthCheckService(
.build()
// 使用 /markets 接口检查(不传参数,返回空列表或少量市场数据)
val url = "${PolymarketConstants.GAMMA_BASE_URL}/markets"
val url = "$gammaBaseUrl/markets"
val request = Request.Builder()
.url(url)
.get()
@@ -180,7 +176,7 @@ class ApiHealthCheckService(
logger.warn("检查 Polymarket Gamma API 失败", e)
ApiHealthCheckDto(
name = "Polymarket Gamma API",
url = "${PolymarketConstants.GAMMA_BASE_URL}/markets",
url = "$gammaBaseUrl/markets",
status = "error",
message = e.message ?: "连接失败"
)
@@ -198,143 +194,69 @@ class ApiHealthCheckService(
}
/**
* 检查 Polymarket RTDS WebSocket 连接状态
* 用于订单推送服务
* 检查 Polymarket WebSocket 连接状态
* 不显示延时只显示连接状态
*/
private suspend fun checkPolymarketRtdsWebSocket(): ApiHealthCheckDto = withContext(Dispatchers.Default) {
private suspend fun checkPolymarketWebSocket(): ApiHealthCheckDto = withContext(Dispatchers.Default) {
try {
// 检查订单推送服务的连接状态
val orderPushService = getOrderPushService()
val statuses = orderPushService?.getConnectionStatuses() ?: emptyMap()
val total = statuses.size
val connected = statuses.values.count { it }
val orderPushStatuses = orderPushService?.getConnectionStatuses() ?: emptyMap()
val orderPushConnected = orderPushStatuses.values.any { it }
val orderPushTotal = orderPushStatuses.size
val orderPushConnectedCount = orderPushStatuses.values.count { it }
if (total == 0) {
// 检查跟单 WebSocket 服务的连接状态
val copyTradingWebSocketService = getCopyTradingWebSocketService()
val copyTradingStatuses = copyTradingWebSocketService?.getConnectionStatuses() ?: emptyMap()
val copyTradingConnected = copyTradingStatuses.values.any { it }
val copyTradingTotal = copyTradingStatuses.size
val copyTradingConnectedCount = copyTradingStatuses.values.count { it }
// 计算总体状态
val totalConnections = orderPushTotal + copyTradingTotal
val connectedConnections = orderPushConnectedCount + copyTradingConnectedCount
val url = polymarketWsUrl
val hasAnyConnection = orderPushConnected || copyTradingConnected
if (totalConnections == 0) {
// 没有配置任何 WebSocket 连接
ApiHealthCheckDto(
name = "Polymarket RTDS WebSocket",
url = PolymarketConstants.RTDS_WS_URL,
name = "Polymarket WebSocket",
url = url,
status = "skipped",
message = "未配置账户连接"
message = "未配置 WebSocket 连接"
)
} else if (connected > 0) {
val message = if (connected == total) {
"所有账户连接正常 ($connected/$total)"
} else if (hasAnyConnection) {
// 至少有一个连接是活跃的
val message = if (connectedConnections == totalConnections) {
"所有连接正常 ($connectedConnections/$totalConnections)"
} else {
"部分账户连接正常 ($connected/$total)"
"部分连接正常 ($connectedConnections/$totalConnections)"
}
ApiHealthCheckDto(
name = "Polymarket RTDS WebSocket",
url = PolymarketConstants.RTDS_WS_URL,
name = "Polymarket WebSocket",
url = url,
status = "success",
message = message
// 不设置 responseTimeWebSocket 不显示延时
)
} else {
// 所有连接都断开
ApiHealthCheckDto(
name = "Polymarket RTDS WebSocket",
url = PolymarketConstants.RTDS_WS_URL,
name = "Polymarket WebSocket",
url = url,
status = "error",
message = "所有账户连接断开 (0/$total)"
message = "所有连接断开 ($connectedConnections/$totalConnections)"
// 不设置 responseTimeWebSocket 不显示延时
)
}
} catch (e: Exception) {
logger.warn("检查 Polymarket RTDS WebSocket 状态失败", e)
logger.warn("检查 Polymarket WebSocket 状态失败", e)
ApiHealthCheckDto(
name = "Polymarket RTDS WebSocket",
url = PolymarketConstants.RTDS_WS_URL,
status = "error",
message = "检查失败:${e.message}"
)
}
}
/**
* 检查 Polymarket Activity WebSocket 连接状态
* 用于 Activity 全局交易流监听
*/
private suspend fun checkPolymarketActivityWebSocket(): ApiHealthCheckDto = withContext(Dispatchers.Default) {
try {
val activityWsService = getPolymarketActivityWsService()
val isConnected = activityWsService?.isConnected() ?: false
if (isConnected) {
ApiHealthCheckDto(
name = "Polymarket Activity WebSocket",
url = PolymarketConstants.ACTIVITY_WS_URL,
status = "success",
message = "连接正常"
)
} else {
ApiHealthCheckDto(
name = "Polymarket Activity WebSocket",
url = PolymarketConstants.ACTIVITY_WS_URL,
status = "error",
message = "连接断开"
)
}
} catch (e: Exception) {
logger.warn("检查 Polymarket Activity WebSocket 状态失败", e)
ApiHealthCheckDto(
name = "Polymarket Activity WebSocket",
url = PolymarketConstants.ACTIVITY_WS_URL,
status = "error",
message = "检查失败:${e.message}"
)
}
}
/**
* 检查统一链上 WebSocket 连接状态
* 用于监听链上事件
*/
private suspend fun checkUnifiedOnChainWebSocket(): ApiHealthCheckDto = withContext(Dispatchers.Default) {
try {
val unifiedOnChainWsService = getUnifiedOnChainWsService()
if (unifiedOnChainWsService == null) {
return@withContext ApiHealthCheckDto(
name = "链上 WebSocket",
url = rpcNodeService.getWsUrl(),
status = "error",
message = "服务未初始化"
)
}
// 检查连接状态
val statuses = unifiedOnChainWsService.getConnectionStatuses()
val total = statuses.size
val connected = statuses.values.count { it }
if (total == 0) {
ApiHealthCheckDto(
name = "链上 WebSocket",
url = rpcNodeService.getWsUrl(),
status = "skipped",
message = "未配置地址监听"
)
} else if (connected > 0) {
val message = if (connected == total) {
"所有地址连接正常 ($connected/$total)"
} else {
"部分地址连接正常 ($connected/$total)"
}
ApiHealthCheckDto(
name = "链上 WebSocket",
url = rpcNodeService.getWsUrl(),
status = "success",
message = message
)
} else {
ApiHealthCheckDto(
name = "链上 WebSocket",
url = rpcNodeService.getWsUrl(),
status = "error",
message = "所有地址连接断开 (0/$total)"
)
}
} catch (e: Exception) {
logger.warn("检查链上 WebSocket 状态失败", e)
ApiHealthCheckDto(
name = "链上 WebSocket",
url = rpcNodeService.getWsUrl(),
name = "Polymarket WebSocket",
url = polymarketWsUrl,
status = "error",
message = "检查失败:${e.message}"
)
@@ -468,10 +390,19 @@ class ApiHealthCheckService(
private suspend fun checkBuilderRelayerApi(): ApiHealthCheckDto = withContext(Dispatchers.IO) {
val relayClientService = getRelayClientService()
if (builderRelayerUrl.isBlank()) {
return@withContext ApiHealthCheckDto(
name = "Builder Relayer API",
url = "未配置",
status = "skipped",
message = "未配置 Builder Relayer URL"
)
}
if (relayClientService == null) {
return@withContext ApiHealthCheckDto(
name = "Builder Relayer API",
url = PolymarketConstants.BUILDER_RELAYER_URL,
url = builderRelayerUrl,
status = "error",
message = "服务未初始化"
)
@@ -480,7 +411,7 @@ class ApiHealthCheckService(
if (!relayClientService.isBuilderApiKeyConfigured()) {
return@withContext ApiHealthCheckDto(
name = "Builder Relayer API",
url = PolymarketConstants.BUILDER_RELAYER_URL,
url = builderRelayerUrl,
status = "skipped",
message = "Builder API Key 未配置"
)
@@ -492,7 +423,7 @@ class ApiHealthCheckService(
onSuccess = { responseTime ->
ApiHealthCheckDto(
name = "Builder Relayer API",
url = PolymarketConstants.BUILDER_RELAYER_URL,
url = builderRelayerUrl,
status = "success",
message = "连接成功",
responseTime = responseTime
@@ -501,7 +432,7 @@ class ApiHealthCheckService(
onFailure = { e ->
ApiHealthCheckDto(
name = "Builder Relayer API",
url = PolymarketConstants.BUILDER_RELAYER_URL,
url = builderRelayerUrl,
status = "error",
message = e.message ?: "连接失败"
)
@@ -511,7 +442,7 @@ class ApiHealthCheckService(
logger.warn("检查 Builder Relayer API 失败", e)
ApiHealthCheckDto(
name = "Builder Relayer API",
url = PolymarketConstants.BUILDER_RELAYER_URL,
url = builderRelayerUrl,
status = "error",
message = e.message ?: "连接失败"
)
@@ -1,48 +0,0 @@
package com.wrbug.polymarketbot.service.system
import com.wrbug.polymarketbot.repository.ProcessedTradeRepository
import org.slf4j.LoggerFactory
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
/**
* 已处理交易清理服务
* 定期清理过期的去重记录
*/
@Service
class ProcessedTradeCleanupService(
private val processedTradeRepository: ProcessedTradeRepository
) {
companion object {
private val logger = LoggerFactory.getLogger(ProcessedTradeCleanupService::class.java)
// 保留时间:1小时(3600000毫秒)
// 说明:重复订单通常10秒后就不会再出现,保留10分钟是为了安全起见
private const val RETENTION_MS = 600_000L
// 定时清理间隔:10分钟(600000毫秒)
private const val CLEANUP_INTERVAL_MS = 600_000L
}
/**
* 定时清理过期记录
* 每10分钟执行一次
*/
@Scheduled(fixedDelay = CLEANUP_INTERVAL_MS)
@Transactional
fun cleanupExpiredProcessedTrades() {
try {
val expireTime = System.currentTimeMillis() - RETENTION_MS
val deletedCount = processedTradeRepository.deleteByProcessedAtBefore(expireTime)
if (deletedCount > 0) {
logger.info("清理过期已处理交易记录: deletedCount=$deletedCount, expireTime=$expireTime")
}
} catch (e: Exception) {
logger.error("清理过期已处理交易记录失败", e)
}
}
}
@@ -3,11 +3,11 @@ package com.wrbug.polymarketbot.service.system
import com.wrbug.polymarketbot.api.BuilderRelayerApi
import com.wrbug.polymarketbot.api.EthereumRpcApi
import com.wrbug.polymarketbot.api.JsonRpcRequest
import com.wrbug.polymarketbot.constants.PolymarketConstants
import com.wrbug.polymarketbot.util.EthereumUtils
import com.wrbug.polymarketbot.util.RetrofitFactory
import com.wrbug.polymarketbot.util.createClient
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import java.math.BigInteger
@@ -24,6 +24,8 @@ import java.math.BigInteger
*/
@Service
class RelayClientService(
@Value("\${polymarket.builder.relayer-url:}")
private val builderRelayerUrl: String,
private val retrofitFactory: RetrofitFactory,
private val systemConfigService: SystemConfigService,
private val rpcNodeService: RpcNodeService
@@ -52,10 +54,10 @@ class RelayClientService(
val builderApiKey = systemConfigService.getBuilderApiKey()
val builderSecret = systemConfigService.getBuilderSecret()
val builderPassphrase = systemConfigService.getBuilderPassphrase()
if (isBuilderRelayerEnabled(builderApiKey, builderSecret, builderPassphrase)) {
return retrofitFactory.createBuilderRelayerApi(
relayerUrl = PolymarketConstants.BUILDER_RELAYER_URL,
relayerUrl = builderRelayerUrl,
apiKey = builderApiKey!!,
secret = builderSecret!!,
passphrase = builderPassphrase!!
@@ -72,19 +74,19 @@ class RelayClientService(
builderSecret: String?,
builderPassphrase: String?
): Boolean {
return PolymarketConstants.BUILDER_RELAYER_URL.isNotBlank() &&
return builderRelayerUrl.isNotBlank() &&
builderApiKey != null && builderApiKey.isNotBlank() &&
builderSecret != null && builderSecret.isNotBlank() &&
builderPassphrase != null && builderPassphrase.isNotBlank()
}
/**
* 检查 Builder API Key 是否已配置
*/
fun isBuilderApiKeyConfigured(): Boolean {
return systemConfigService.isBuilderApiKeyConfigured()
}
/**
* 检查 Builder Relayer API 健康状态用于 API 健康检查
*/
@@ -93,24 +95,24 @@ class RelayClientService(
val builderApiKey = systemConfigService.getBuilderApiKey()
val builderSecret = systemConfigService.getBuilderSecret()
val builderPassphrase = systemConfigService.getBuilderPassphrase()
if (builderApiKey == null || builderSecret == null || builderPassphrase == null) {
return Result.failure(IllegalStateException("Builder API Key 未配置"))
}
val relayerApi = retrofitFactory.createBuilderRelayerApi(
relayerUrl = PolymarketConstants.BUILDER_RELAYER_URL,
relayerUrl = builderRelayerUrl,
apiKey = builderApiKey,
secret = builderSecret,
passphrase = builderPassphrase
)
// 使用一个测试地址来检查 API 是否可用(使用一个已知的地址,如零地址)
val testAddress = "0x0000000000000000000000000000000000000000"
val startTime = System.currentTimeMillis()
val response = relayerApi.getDeployed(testAddress)
val responseTime = System.currentTimeMillis() - startTime
if (response.isSuccessful) {
Result.success(responseTime)
} else {
@@ -226,18 +228,11 @@ class RelayClientService(
val builderApiKey = systemConfigService.getBuilderApiKey()
val builderSecret = systemConfigService.getBuilderSecret()
val builderPassphrase = systemConfigService.getBuilderPassphrase()
// 优先使用 Builder RelayerGasless
if (isBuilderRelayerEnabled(builderApiKey, builderSecret, builderPassphrase)) {
logger.info("使用 Builder Relayer 执行 Gasless 交易")
return executeViaBuilderRelayer(
privateKey,
proxyAddress,
safeTx,
builderApiKey!!,
builderSecret!!,
builderPassphrase!!
)
return executeViaBuilderRelayer(privateKey, proxyAddress, safeTx, builderApiKey!!, builderSecret!!, builderPassphrase!!)
}
// 回退到手动发送交易(需要用户支付 gas)
@@ -261,8 +256,9 @@ class RelayClientService(
builderSecret: String,
builderPassphrase: String
): Result<String> {
val rpcApi = polygonRpcApi
val relayerApi = retrofitFactory.createBuilderRelayerApi(
relayerUrl = PolymarketConstants.BUILDER_RELAYER_URL,
relayerUrl = builderRelayerUrl,
apiKey = builderApiKey,
secret = builderSecret,
passphrase = builderPassphrase
@@ -324,19 +320,19 @@ class RelayClientService(
val messageWithPrefix = ByteArray(prefix.size + safeTxStructuredHash.size)
System.arraycopy(prefix, 0, messageWithPrefix, 0, prefix.size)
System.arraycopy(safeTxStructuredHash, 0, messageWithPrefix, prefix.size, safeTxStructuredHash.size)
// 对带前缀的消息进行 keccak256 哈希
val keccak256 = org.bouncycastle.crypto.digests.KeccakDigest(256)
keccak256.update(messageWithPrefix, 0, messageWithPrefix.size)
val hashWithPrefix = ByteArray(keccak256.digestSize)
keccak256.doFinal(hashWithPrefix, 0)
val ecKeyPair = org.web3j.crypto.ECKeyPair.create(privateKeyBigInt)
val safeSignature = org.web3j.crypto.Sign.signMessage(hashWithPrefix, ecKeyPair, false)
// 打包签名(参考 builder-relayer-client/src/utils/index.ts 的 splitAndPackSig
val packedSignature = splitAndPackSig(safeSignature)
// 调试日志(地址已遮蔽)
logger.debug("=== Builder Relayer 签名调试 ===")
logger.debug("Safe: ${proxyAddress.take(10)}..., From: ${fromAddress.take(10)}..., Nonce: $proxyNonce")
@@ -362,7 +358,7 @@ class RelayClientService(
),
metadata = "Redeem positions via Builder Relayer"
)
logger.debug("Request: type=${request.type}, dataLen=${request.data.length}, sigLen=${request.signature.length}, nonce=${request.nonce}")
// 调用 Builder Relayer API(认证头通过拦截器添加)
@@ -376,7 +372,7 @@ class RelayClientService(
val relayerResponse = response.body()!!
val txHash = relayerResponse.transactionHash ?: relayerResponse.hash
?: return Result.failure(Exception("Builder Relayer 返回的交易哈希为空"))
?: return Result.failure(Exception("Builder Relayer 返回的交易哈希为空"))
logger.info("Builder Relayer 执行成功: transactionID=${relayerResponse.transactionID}, txHash=$txHash")
return Result.success(txHash)
@@ -385,14 +381,14 @@ class RelayClientService(
/**
* 打包签名参考 builder-relayer-client/src/utils/index.ts splitAndPackSig
* 将签名打包成 Gnosis Safe 接受的格式encodePacked(["uint256", "uint256", "uint8"], [r, s, v])
*
*
* TypeScript 实现流程
* 1. 从签名字符串中提取 v最后 2 个字符
* 2. 调整 v 0,1 -> +31; 27,28 -> +4
* 3. 修改签名字符串替换最后 2 个字符
* 4. 从修改后的签名字符串中提取 r, s, v作为十进制字符串
* 5. 使用 encodePacked 打包uint256(BigInt(r)) + uint256(BigInt(s)) + uint8(parseInt(v))
*
*
* 关键encodePacked 会将 BigInt 编码为 32 字节64 个十六进制字符uint8 编码为 1 字节2 个十六进制字符
*/
private fun splitAndPackSig(signature: org.web3j.crypto.Sign.SignatureData): String {
@@ -407,37 +403,37 @@ class RelayClientService(
}
val originalVHex = String.format("%02x", originalV)
val sigString = "0x$rHex$sHex$originalVHex" // 130 个十六进制字符(65 字节)
// 2. 从签名字符串中提取 v(最后 2 个字符,作为十六进制)
val sigV = sigString.substring(sigString.length - 2).toInt(16)
// 3. 调整 v 值(参考 TypeScript 实现)
val adjustedV = when (sigV) {
0, 1 -> sigV + 31
27, 28 -> sigV + 4
else -> throw IllegalArgumentException("Invalid signature v value: $sigV")
}
// 4. 修改签名字符串(替换最后 2 个字符)
val modifiedSigString = sigString.substring(0, sigString.length - 2) + String.format("%02x", adjustedV)
// 5. 从修改后的签名字符串中提取 r, s, v(作为十六进制字符串)
// modifiedSigString 格式:0x + r(64) + s(64) + v(2) = 132 个字符
val rHexStr = modifiedSigString.substring(2, 66) // 64 个字符(十六进制)
val sHexStr = modifiedSigString.substring(66, 130) // 64 个字符(十六进制)
val vHexStr = modifiedSigString.substring(130, 132) // 2 个字符(十六进制)
// 6. 转换为 BigInteger 和 Int(模拟 TypeScript 的 BigInt 和 parseInt
val rBigInt = BigInteger(rHexStr, 16)
val sBigInt = BigInteger(sHexStr, 16)
val vInt = vHexStr.toInt(16)
// 7. 使用 encodePacked 打包:uint256(r) + uint256(s) + uint8(v)
// encodePacked 会将 BigInt 编码为 32 字节(64 个十六进制字符),uint8 编码为 1 字节(2 个十六进制字符)
val rEncoded = EthereumUtils.encodeUint256(rBigInt) // 64 个十六进制字符
val sEncoded = EthereumUtils.encodeUint256(sBigInt) // 64 个十六进制字符
val vEncoded = String.format("%02x", vInt) // 2 个十六进制字符
return "0x$rEncoded$sEncoded$vEncoded"
}
@@ -508,13 +504,13 @@ class RelayClientService(
val messageWithPrefix = ByteArray(prefix.size + safeTxStructuredHash.size)
System.arraycopy(prefix, 0, messageWithPrefix, 0, prefix.size)
System.arraycopy(safeTxStructuredHash, 0, messageWithPrefix, prefix.size, safeTxStructuredHash.size)
// 对带前缀的消息进行 keccak256 哈希
val keccak256 = org.bouncycastle.crypto.digests.KeccakDigest(256)
keccak256.update(messageWithPrefix, 0, messageWithPrefix.size)
val hashWithPrefix = ByteArray(keccak256.digestSize)
keccak256.doFinal(hashWithPrefix, 0)
val ecKeyPair = org.web3j.crypto.ECKeyPair.create(privateKeyBigInt)
val safeSignature = org.web3j.crypto.Sign.signMessage(hashWithPrefix, ecKeyPair, false)
@@ -576,8 +572,7 @@ class RelayClientService(
redeemCallData: String,
safeSignatureHex: String
): String {
val execFunctionSelector =
EthereumUtils.getFunctionSelector("execTransaction(address,uint256,bytes,uint8,uint256,uint256,uint256,address,address,bytes)")
val execFunctionSelector = EthereumUtils.getFunctionSelector("execTransaction(address,uint256,bytes,uint8,uint256,uint256,uint256,address,address,bytes)")
val encodedTo = EthereumUtils.encodeAddress(safeTx.to)
val encodedValue = EthereumUtils.encodeUint256(BigInteger.ZERO)
@@ -605,20 +600,20 @@ class RelayClientService(
val encodedSignatures = safeSignatureHex
return "0x" + execFunctionSelector.removePrefix("0x") +
encodedTo +
encodedValue +
encodedDataOffset +
encodedDataLength +
encodedData +
encodedOperation +
encodedSafeTxGas +
encodedBaseGas +
encodedGasPrice +
encodedGasToken +
encodedRefundReceiver +
encodedSignaturesOffset +
encodedSignaturesLength +
encodedSignatures
encodedTo +
encodedValue +
encodedDataOffset +
encodedDataLength +
encodedData +
encodedOperation +
encodedSafeTxGas +
encodedBaseGas +
encodedGasPrice +
encodedGasToken +
encodedRefundReceiver +
encodedSignaturesOffset +
encodedSignaturesLength +
encodedSignatures
}
/**
@@ -59,26 +59,6 @@ class TelegramNotificationService(
* @param walletAddressForApi 钱包地址可选用于查询订单详情
* @param locale 语言设置可选如果提供则使用否则使用 LocaleContextHolder 获取
*/
// 已发送通知的订单ID缓存(key: orderId, value: timestamp
private val sentOrderIds = java.util.concurrent.ConcurrentHashMap<String, Long>()
/**
* 发送订单成功通知
* @param orderId 订单ID用于查询订单详情获取实际价格和数量
* @param marketTitle 市场标题
* @param marketId 市场IDconditionId用于生成链接
* @param marketSlug 市场slug用于生成链接
* @param side 订单方向BUY/SELL用于多语言显示
* @param accountName 账户名称
* @param walletAddress 钱包地址
* @param clobApi CLOB API 客户端可选如果提供则查询订单详情获取实际价格和数量
* @param apiKey API Key可选用于查询订单详情
* @param apiSecret API Secret可选用于查询订单详情
* @param apiPassphrase API Passphrase可选用于查询订单详情
* @param walletAddressForApi 钱包地址可选用于查询订单详情
* @param locale 语言设置可选如果提供则使用否则使用 LocaleContextHolder 获取
* @param orderTime 订单时间可选如果提供则使用订单创建时间否则使用当前通知时间
*/
suspend fun sendOrderSuccessNotification(
orderId: String?,
marketTitle: String,
@@ -97,29 +77,8 @@ class TelegramNotificationService(
walletAddressForApi: String? = null,
locale: java.util.Locale? = null,
leaderName: String? = null, // Leader 名称(备注)
configName: String? = null, // 跟单配置名
orderTime: Long? = null // 订单创建时间(毫秒时间戳),用于通知中的时间显示
configName: String? = null // 跟单配置名
) {
// 1. 如果提供了 orderId,检查是否已发送过通知(去重)
if (orderId != null) {
val lastSentTime = sentOrderIds[orderId]
if (lastSentTime != null) {
// 如果5分钟内已发送过,跳过
if (System.currentTimeMillis() - lastSentTime < 5 * 60 * 1000) {
logger.info("订单通知已发送过(5分钟内),跳过: orderId=$orderId")
return
}
}
// 记录发送时间
sentOrderIds[orderId] = System.currentTimeMillis()
// 简单的清理逻辑:如果缓存过大,清理过期的
if (sentOrderIds.size > 1000) {
val expiryTime = System.currentTimeMillis() - 5 * 60 * 1000
sentOrderIds.entries.removeIf { it.value < expiryTime }
}
}
// 获取语言设置(优先使用传入的 locale,否则从 LocaleContextHolder 获取)
val currentLocale = locale ?: try {
LocaleContextHolder.getLocale()
@@ -191,8 +150,7 @@ class TelegramNotificationService(
walletAddress = walletAddress,
locale = currentLocale,
leaderName = leaderName,
configName = configName,
orderTime = orderTime
configName = configName
)
sendMessage(message)
}
@@ -702,8 +660,7 @@ class TelegramNotificationService(
walletAddress: String?,
locale: java.util.Locale,
leaderName: String? = null, // Leader 名称(备注)
configName: String? = null, // 跟单配置名
orderTime: Long? = null // 订单创建时间(毫秒时间戳)
configName: String? = null // 跟单配置名
): String {
// 获取多语言文本
@@ -729,13 +686,6 @@ class TelegramNotificationService(
else -> side
}
// 获取图标
val icon = when (side.uppercase()) {
"BUY" -> "🚀"
"SELL" -> "💰"
else -> "📣"
}
// 构建账户信息(格式:账户名(钱包地址))
val accountInfo = buildAccountInfo(accountName, walletAddress, unknownAccount)
@@ -753,12 +703,7 @@ class TelegramNotificationService(
""
}
// 使用订单时间(如果提供),否则使用当前通知时间
val time = if (orderTime != null) {
DateUtils.formatDateTime(orderTime)
} else {
DateUtils.formatDateTime()
}
val time = DateUtils.formatDateTime()
// 转义 HTML 特殊字符
val escapedMarketTitle = marketTitle.replace("<", "&lt;").replace(">", "&gt;")
@@ -816,7 +761,7 @@ class TelegramNotificationService(
val priceDisplay = formatPrice(price)
val sizeDisplay = formatQuantity(size)
return """$icon <b>$orderCreatedSuccess</b>
return """ <b>$orderCreatedSuccess</b>
📊 <b>$orderInfo</b>
$orderIdLabel: <code>${orderId ?: unknown}</code>
@@ -1044,7 +989,7 @@ class TelegramNotificationService(
"${position.marketId.substring(0, 8)}... (${position.side}): $quantityDisplay shares = $valueDisplay USDC"
}
return """💸 <b>$redeemSuccess</b>
return """ <b>$redeemSuccess</b>
📊 <b>$redeemInfo</b>
$accountLabel: $escapedAccountInfo
@@ -1,154 +1,58 @@
package com.wrbug.polymarketbot.util
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonElement
import com.google.gson.reflect.TypeToken
import jakarta.annotation.PostConstruct
import org.springframework.stereotype.Component
/**
* JSON 工具类
* 用于初始化全局 Gson 实例供扩展函数使用
*
* 使用 Spring 注入的 Gson Bean已在 GsonConfig 中配置为 lenient 模式
* 初始化后设置全局 gson 实例供扩展函数使用
* 用于解析 JSON 字符串
*/
@Component
class JsonUtils(
private val injectedGson: Gson
private val gson: Gson
) {
@PostConstruct
fun init() {
// 设置全局 Gson 实例,供扩展函数使用
_gson = injectedGson
}
/**
* 解析 JSON 字符串数组
* @param jsonString JSON 字符串 "[\"Yes\", \"No\"]"
* @return 字符串列表如果解析失败返回空列表
*/
fun parseStringArray(jsonString: String?): List<String> {
return jsonString?.parseStringArray() ?: emptyList()
}
}
/**
* 全局 Gson 实例用于扩展函数
* JsonUtils 初始化时设置
*/
@Volatile
private var _gson: Gson? = null
/**
* 获取全局 Gson 实例
* 如果尚未初始化创建默认实例
*/
val gson: Gson
get() = _gson ?: GsonBuilder().setLenient().create().also { _gson = it }
// ============================================================================
// 扩展函数:JSON 序列化和反序列化
// ============================================================================
/**
* 将对象转换为 JSON 字符串
*
* @return JSON 字符串如果对象为 null 则返回空字符串
*
* @example
* ```kotlin
* val obj = MyDataClass(name = "test", value = 123)
* val json = obj.toJson() // {"name":"test","value":123}
* ```
*/
fun Any?.toJson(): String {
return if (this == null) {
""
} else {
try {
gson.toJson(this)
if (jsonString.isNullOrBlank()) {
return emptyList()
}
return try {
val listType = object : TypeToken<List<String>>() {}.type
gson.fromJson<List<String>>(jsonString, listType) ?: emptyList()
} catch (e: Exception) {
""
emptyList()
}
}
/**
* 解析 JSON 字符串列表parseStringArray 的别名
*/
fun parseStringList(jsonString: String?): List<String> {
return parseStringArray(jsonString)
}
/**
* 将对象转换为 JSON 字符串
* @param obj 要转换的对象
* @return JSON 字符串
*/
fun toJson(obj: Any?): String? {
if (obj == null) {
return null
}
return try {
gson.toJson(obj)
} catch (e: Exception) {
null
}
}
}
/**
* JSON 字符串解析为对象支持泛型
*
* @return 解析后的对象如果解析失败返回 null
*
* @example
* ```kotlin
* val json = "{\"name\":\"test\",\"value\":123}"
* val obj = json.fromJson<MyDataClass>() // MyDataClass(name="test", value=123)
*
* // 对于泛型类型,自动使用 TypeToken
* val json = "[\"a\", \"b\", \"c\"]"
* val list = json.fromJson<List<String>>() // ["a", "b", "c"]
* ```
*/
inline fun <reified T> String?.fromJson(): T? {
if (this.isNullOrBlank()) {
return null
}
return try {
val typeToken = object : TypeToken<T>() {}
gson.fromJson(this, typeToken.type)
} catch (e: Exception) {
null
}
}
/**
* JsonElement 解析为对象支持泛型
*
* @return 解析后的对象如果解析失败返回 null
*
* @example
* ```kotlin
* val jsonElement: JsonElement = ...
* val obj = jsonElement.fromJson<MyDataClass>()
* ```
*/
inline fun <reified T> JsonElement?.fromJson(): T? {
if (this == null) {
return null
}
return try {
val typeToken = object : TypeToken<T>() {}
gson.fromJson(this, typeToken.type)
} catch (e: Exception) {
null
}
}
/**
* 解析 JSON 字符串数组
*
* @return 字符串列表如果解析失败返回空列表
*
* @example
* ```kotlin
* val json = "[\"Yes\", \"No\"]"
* val list = json.parseStringArray() // ["Yes", "No"]
* ```
*/
fun String?.parseStringArray(): List<String> {
if (this.isNullOrBlank()) {
return emptyList()
}
return try {
val listType = object : TypeToken<List<String>>() {}.type
gson.fromJson<List<String>>(this, listType) ?: emptyList()
} catch (e: Exception) {
emptyList()
}
}
@@ -1,20 +0,0 @@
package com.wrbug.polymarketbot.util
import com.wrbug.polymarketbot.api.MarketResponse
/**
* MarketResponse 扩展函数
* events[0].slug 获取 slug用于网页跳转
*/
fun MarketResponse?.getEventSlug(): String? {
return this?.events?.firstOrNull()?.slug ?: this?.slug
}
/**
* MarketResponse 扩展函数
* 获取显示用的 slug使用原来的 slug 字段
*/
fun MarketResponse?.getDisplaySlug(): String? {
return this?.slug
}
@@ -26,23 +26,21 @@ fun BigDecimal.multi(value: Any): BigDecimal {
return BigDecimal.ZERO
}
/**
* BigDecimal除法扩展函数带精度和舍入模式
* BigDecimal除法扩展函数
* 安全地将BigDecimal与任意数值类型相除
* @param value 除数支持BigDecimalBigInteger类型或可转换为BigDecimal的字符串
* @param scale 精度小数位数
* @param roundingMode 舍入模式
* @return 除法结果如果转换失败返回IllegalBigDecimal
* @return 除法结果精度为18位小数使用四舍五入模式如果转换失败返回IllegalBigDecimal
*/
fun BigDecimal.div(value: Any, scale: Int = 18, roundingMode: RoundingMode = RoundingMode.HALF_UP): BigDecimal {
fun BigDecimal.div(value: Any): BigDecimal {
kotlin.runCatching {
val divisor = when (value) {
is BigDecimal -> value
is BigInteger -> value.toBigDecimal()
else -> BigDecimal(value.toString())
if (value is BigDecimal) {
return divide(value, 18, RoundingMode.HALF_UP).stripTrailingZeros()
}
return divide(divisor, scale, roundingMode)
if (value is BigInteger) {
return divide(value.toSafeBigDecimal(), 18, RoundingMode.HALF_UP).stripTrailingZeros()
}
return divide(BigDecimal(value.toString()), 18, RoundingMode.HALF_UP).stripTrailingZeros()
}
return IllegalBigDecimal
}
@@ -0,0 +1,64 @@
package com.wrbug.polymarketbot.util
import com.wrbug.polymarketbot.api.NbaStatsApi
import org.slf4j.LoggerFactory
import java.time.LocalDate
import java.time.format.DateTimeFormatter
/**
* NBA API 验证工具
* 用于验证 API 调用是否正确
*/
object NbaApiValidator {
private val logger = LoggerFactory.getLogger(NbaApiValidator::class.java)
/**
* 验证 API 调用
*/
suspend fun validateApi(nbaStatsApi: NbaStatsApi): Boolean {
return try {
val today = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
logger.info("验证 NBA Stats API,日期: $today")
val response = nbaStatsApi.getScoreboard(gameDate = today)
logger.info("API 响应状态码: ${response.code()}")
logger.info("API 响应消息: ${response.message()}")
if (response.isSuccessful && response.body() != null) {
val scoreboard = response.body()!!
logger.info("ResultSets 数量: ${scoreboard.resultSets.size}")
scoreboard.resultSets.forEachIndexed { index, resultSet ->
logger.info("ResultSet[$index]: name=${resultSet.name}, headers=${resultSet.headers.size}, rows=${resultSet.rowSet.size}")
if (resultSet.headers.isNotEmpty()) {
logger.info(" Headers: ${resultSet.headers.take(10)}")
}
if (resultSet.rowSet.isNotEmpty()) {
val firstRow = resultSet.rowSet.first()
logger.info(" First row size: ${firstRow.size}")
logger.info(" First row (first 5): ${firstRow.take(5)}")
}
}
// 检查是否有 GameHeader 和 LineScore
val hasGameHeader = scoreboard.resultSets.any { it.name == "GameHeader" }
val hasLineScore = scoreboard.resultSets.any { it.name == "LineScore" }
logger.info("包含 GameHeader: $hasGameHeader")
logger.info("包含 LineScore: $hasLineScore")
hasGameHeader && hasLineScore
} else {
logger.error("API 调用失败")
val errorBody = response.errorBody()?.string()
logger.error("错误响应体: $errorBody")
false
}
} catch (e: Exception) {
logger.error("验证 API 异常: ${e.message}", e)
false
}
}
}
@@ -4,10 +4,10 @@ import com.google.gson.Gson
import com.wrbug.polymarketbot.api.BuilderRelayerApi
import com.wrbug.polymarketbot.api.EthereumRpcApi
import com.wrbug.polymarketbot.api.GitHubApi
import com.wrbug.polymarketbot.api.NbaStatsApi
import com.wrbug.polymarketbot.api.PolymarketClobApi
import com.wrbug.polymarketbot.api.PolymarketDataApi
import com.wrbug.polymarketbot.api.PolymarketGammaApi
import com.wrbug.polymarketbot.constants.PolymarketConstants
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.Interceptor
@@ -19,6 +19,7 @@ import okhttp3.Response
import okio.Buffer
import java.util.concurrent.TimeUnit
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
@@ -34,6 +35,10 @@ import jakarta.annotation.PreDestroy
*/
@Component
class RetrofitFactory(
@Value("\${polymarket.clob.base-url}")
private val clobBaseUrl: String,
@Value("\${polymarket.gamma.base-url}")
private val gammaBaseUrl: String,
private val gson: Gson
) {
@@ -54,10 +59,10 @@ class RetrofitFactory(
// 缓存 Gamma API 客户端(单例)
private val gammaApi: PolymarketGammaApi by lazy {
val baseUrl = if (PolymarketConstants.GAMMA_BASE_URL.endsWith("/")) {
PolymarketConstants.GAMMA_BASE_URL.dropLast(1)
val baseUrl = if (gammaBaseUrl.endsWith("/")) {
gammaBaseUrl.dropLast(1)
} else {
PolymarketConstants.GAMMA_BASE_URL
gammaBaseUrl
}
Retrofit.Builder()
@@ -70,7 +75,7 @@ class RetrofitFactory(
// 缓存 Data API 客户端(单例)
private val dataApi: PolymarketDataApi by lazy {
val baseUrl = PolymarketConstants.DATA_API_BASE_URL
val baseUrl = "https://data-api.polymarket.com"
Retrofit.Builder()
.baseUrl("$baseUrl/")
@@ -109,7 +114,7 @@ class RetrofitFactory(
// 缓存不带认证的 CLOB API 客户端(单例)
private val clobApiWithoutAuth: PolymarketClobApi by lazy {
Retrofit.Builder()
.baseUrl(PolymarketConstants.CLOB_BASE_URL)
.baseUrl(clobBaseUrl)
.client(sharedOkHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
@@ -154,7 +159,7 @@ class RetrofitFactory(
.build()
Retrofit.Builder()
.baseUrl(PolymarketConstants.CLOB_BASE_URL)
.baseUrl(clobBaseUrl)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
@@ -411,6 +416,44 @@ class RpcUrlReplaceInterceptor(
return chain.proceed(newRequest)
}
/**
* 创建 NBA Stats API 客户端
* NBA Stats API 是公开 API但需要设置正确的请求头
* @return NbaStatsApi 客户端
*/
fun createNbaStatsApi(): NbaStatsApi {
val baseUrl = "https://stats.nba.com/stats/"
// 添加拦截器,设置 NBA Stats API 需要的请求头
val nbaStatsInterceptor = object : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request().newBuilder()
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
.header("Referer", "https://www.nba.com/")
.header("Accept", "application/json")
.header("Accept-Language", "en-US,en;q=0.9")
.header("Origin", "https://www.nba.com")
.build()
return chain.proceed(request)
}
}
val okHttpClient = createClient()
.addInterceptor(nbaStatsInterceptor)
.build()
val gson = GsonBuilder()
.setLenient()
.create()
return Retrofit.Builder()
.baseUrl(baseUrl)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(NbaStatsApi::class.java)
}
}
/**
@@ -139,8 +139,7 @@ class PolymarketWebSocketClient(
/**
* 启动 PING 保活机制
* 根据 @polymarket/real-time-data-client发送小写 "ping"
* 10 秒发送一次
* 根据官方文档 10 秒发送一次 "PING"
*/
private fun startPing() {
stopPing() // 先停止之前的 PING 任务
@@ -150,7 +149,7 @@ class PolymarketWebSocketClient(
delay(10000) // 10 秒
if (isConnected) {
try {
sendMessage("ping") // 使用小写,与官方客户端保持一致
sendMessage("PING")
} catch (e: Exception) {
logger.warn("发送 PING 失败: $sessionId, ${e.message}")
break
@@ -1,7 +1,7 @@
package com.wrbug.polymarketbot.websocket
import com.wrbug.polymarketbot.constants.PolymarketConstants
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component
import org.springframework.web.socket.*
import java.util.concurrent.ConcurrentHashMap
@@ -15,7 +15,8 @@ class PolymarketWebSocketHandler : WebSocketHandler {
private val logger = LoggerFactory.getLogger(PolymarketWebSocketHandler::class.java)
private val polymarketWsUrl: String = PolymarketConstants.RTDS_WS_URL
@Value("\${polymarket.rtds.ws-url}")
private lateinit var polymarketWsUrl: String
// 存储客户端会话和对应的 Polymarket 连接的映射
private val clientSessions = ConcurrentHashMap<String, WebSocketSession>()
@@ -29,20 +29,22 @@ server.port=${SERVER_PORT:8000}
# 默认使用 dev 环境,可通过 --spring.profiles.active=prod 切换
spring.profiles.active=${SPRING_PROFILES_ACTIVE:dev}
# 日志配置(支持通过环境变量配置)
logging.level.root=${LOG_LEVEL_ROOT:WARN}
logging.level.com.wrbug.polymarketbot=${LOG_LEVEL_APP:INFO}
# 日志配置
logging.level.root=INFO
logging.level.com.wrbug.polyhermes=DEBUG
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n
# Polymarket API 配置
# 注意:Polymarket API URL 现在使用代码常量(PolymarketConstants),不再从配置文件读取
# 如需修改,请修改 com.wrbug.polymarketbot.constants.PolymarketConstants 类
polymarket.clob.base-url=https://clob.polymarket.com
polymarket.rtds.ws-url=wss://ws-subscriptions-clob.polymarket.com
polymarket.data-api.base-url=https://data-api.polymarket.com
polymarket.gamma.base-url=https://gamma-api.polymarket.com
# Builder Relayer 配置(用于 Gasless 交易)
# 从 polymarket.com/settings?tab=builder 获取 Builder API 凭证
# Builder API Key、Secret、Passphrase 现在通过系统设置页面配置,存储在数据库中
# 如果未配置,将使用手动发送交易的方式(需要用户支付 gas)
# 注意:Builder Relayer URL 现在使用代码常量(PolymarketConstants.BUILDER_RELAYER_URL),不再从配置文件读取
polymarket.builder.relayer-url=${POLYMARKET_BUILDER_RELAYER_URL:https://relayer-v2.polymarket.com/}
# 跟单轮询配置
# 轮询间隔(毫秒),默认2秒
@@ -50,10 +52,6 @@ copy.trading.polling.interval=${COPY_TRADING_POLLING_INTERVAL:2000}
# 是否启用轮询,默认true
copy.trading.polling.enabled=${COPY_TRADING_POLLING_ENABLED:true}
# 跟单监听配置
# 链上 WebSocket 重连延迟(毫秒),默认3秒
copy.trading.onchain.ws.reconnect.delay=${COPY_TRADING_ONCHAIN_WS_RECONNECT_DELAY:3000}
# WebSocket 配置
websocket.heartbeat-timeout=${WEBSOCKET_HEARTBEAT_TIMEOUT:60000}
@@ -0,0 +1,184 @@
-- NBA 量化交易系统数据库表
-- 1. NBA 市场表(Polymarket 市场信息)
CREATE TABLE IF NOT EXISTS nba_markets (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
polymarket_market_id VARCHAR(100) UNIQUE NOT NULL COMMENT 'Polymarket 市场 ID',
condition_id VARCHAR(100) UNIQUE NOT NULL COMMENT 'Condition ID',
market_slug VARCHAR(255) COMMENT '市场 slug',
market_question TEXT COMMENT '市场名称/问题',
market_description TEXT COMMENT '市场描述',
category VARCHAR(50) DEFAULT 'sports' COMMENT '分类',
active BOOLEAN DEFAULT true COMMENT '是否活跃',
closed BOOLEAN DEFAULT false COMMENT '是否已关闭',
archived BOOLEAN DEFAULT false COMMENT '是否已归档',
volume VARCHAR(50) COMMENT '交易量',
liquidity VARCHAR(50) COMMENT '流动性',
outcomes TEXT COMMENT '结果选项(JSON',
end_date VARCHAR(50) COMMENT '结束日期',
start_date VARCHAR(50) COMMENT '开始日期',
created_at BIGINT NOT NULL,
updated_at BIGINT NOT NULL,
INDEX idx_condition_id (condition_id),
INDEX idx_active (active),
INDEX idx_closed (closed),
INDEX idx_category (category)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='NBA市场表(Polymarket市场)';
-- 2. NBA 比赛表(NBA 比赛信息)
CREATE TABLE IF NOT EXISTS nba_games (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
nba_game_id VARCHAR(100) UNIQUE COMMENT 'NBA 比赛 ID(来自 NBA API',
home_team VARCHAR(100) NOT NULL COMMENT '主队名称',
away_team VARCHAR(100) NOT NULL COMMENT '客队名称',
game_date DATE NOT NULL COMMENT '比赛日期',
game_time BIGINT COMMENT '比赛时间(时间戳,毫秒)',
game_status VARCHAR(50) DEFAULT 'scheduled' COMMENT '比赛状态:scheduled/active/finished',
home_score INT DEFAULT 0 COMMENT '主队得分',
away_score INT DEFAULT 0 COMMENT '客队得分',
period INT DEFAULT 0 COMMENT '当前节次',
time_remaining VARCHAR(50) COMMENT '剩余时间',
polymarket_market_id VARCHAR(100) COMMENT '关联的 Polymarket 市场 ID',
created_at BIGINT NOT NULL,
updated_at BIGINT NOT NULL,
INDEX idx_game_date (game_date),
INDEX idx_game_status (game_status),
INDEX idx_home_team (home_team),
INDEX idx_away_team (away_team),
INDEX idx_polymarket_market_id (polymarket_market_id),
FOREIGN KEY (polymarket_market_id) REFERENCES nba_markets(polymarket_market_id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='NBA比赛表';
-- 3. 量化策略配置表
CREATE TABLE IF NOT EXISTS nba_quantitative_strategies (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
strategy_name VARCHAR(100) NOT NULL COMMENT '策略名称',
strategy_description TEXT COMMENT '策略描述',
account_id BIGINT NOT NULL COMMENT '关联账户 ID',
enabled BOOLEAN DEFAULT true COMMENT '是否启用',
-- 比赛筛选参数
filter_teams TEXT COMMENT '关注的球队列表(JSON',
filter_date_from DATE COMMENT '日期范围开始',
filter_date_to DATE COMMENT '日期范围结束',
filter_game_importance VARCHAR(50) COMMENT '比赛重要性:all/regular/playoff/key',
-- 触发条件参数
min_win_probability_diff DECIMAL(5, 4) DEFAULT 0.1000 COMMENT '最小获胜概率差异',
min_win_probability DECIMAL(5, 4) COMMENT '最小获胜概率',
max_win_probability DECIMAL(5, 4) COMMENT '最大获胜概率',
min_trade_value DECIMAL(5, 4) DEFAULT 0.0500 COMMENT '最小交易价值',
min_remaining_time INT COMMENT '最小剩余时间(分钟)',
max_remaining_time INT COMMENT '最大剩余时间(分钟)',
min_score_diff INT COMMENT '最小分差',
max_score_diff INT COMMENT '最大分差',
-- 买入规则参数
buy_amount_strategy VARCHAR(20) DEFAULT 'FIXED' COMMENT '买入金额策略:FIXED/RATIO/DYNAMIC',
fixed_buy_amount DECIMAL(20, 8) COMMENT '固定买入金额(USDC',
buy_ratio DECIMAL(5, 4) COMMENT '买入比例(0-1',
base_buy_amount DECIMAL(20, 8) COMMENT '基础买入金额(USDC',
buy_timing VARCHAR(20) DEFAULT 'IMMEDIATE' COMMENT '买入时机:IMMEDIATE/DELAYED',
delay_buy_seconds INT DEFAULT 0 COMMENT '延迟买入时间(秒)',
buy_direction VARCHAR(10) DEFAULT 'AUTO' COMMENT '买入方向:AUTO/YES/NO',
-- 卖出规则参数
enable_sell BOOLEAN DEFAULT true COMMENT '是否启用卖出',
take_profit_threshold DECIMAL(5, 4) COMMENT '止盈阈值(0-1',
stop_loss_threshold DECIMAL(5, 4) COMMENT '止损阈值(-1-0',
probability_reversal_threshold DECIMAL(5, 4) COMMENT '概率反转阈值(0-1',
sell_ratio DECIMAL(5, 4) DEFAULT 1.0000 COMMENT '卖出比例(0-1',
sell_timing VARCHAR(20) DEFAULT 'IMMEDIATE' COMMENT '卖出时机:IMMEDIATE/DELAYED',
delay_sell_seconds INT DEFAULT 0 COMMENT '延迟卖出时间(秒)',
-- 价格策略参数
price_strategy VARCHAR(20) DEFAULT 'MARKET' COMMENT '价格策略:FIXED/MARKET/DYNAMIC',
fixed_price DECIMAL(5, 4) COMMENT '固定价格(0-1',
price_offset DECIMAL(5, 4) DEFAULT 0.0000 COMMENT '价格偏移(-0.1-0.1',
-- 风险控制参数
max_position DECIMAL(20, 8) DEFAULT 50.00000000 COMMENT '最大持仓(USDC',
min_position DECIMAL(20, 8) DEFAULT 5.00000000 COMMENT '最小持仓(USDC',
max_game_position DECIMAL(20, 8) COMMENT '单场比赛最大持仓(USDC',
max_daily_loss DECIMAL(20, 8) COMMENT '每日亏损限制(USDC',
max_daily_orders INT COMMENT '每日订单限制',
max_daily_profit DECIMAL(20, 8) COMMENT '每日盈利目标(USDC',
price_tolerance DECIMAL(5, 4) DEFAULT 0.0500 COMMENT '价格容忍度(0-1',
min_probability_threshold DECIMAL(5, 4) COMMENT '最小概率阈值(0.5-1.0',
max_probability_threshold DECIMAL(5, 4) COMMENT '最大概率阈值(0.0-0.5',
-- 算法权重参数(高级)
base_strength_weight DECIMAL(5, 4) DEFAULT 0.3000 COMMENT '基础实力权重',
recent_form_weight DECIMAL(5, 4) DEFAULT 0.2500 COMMENT '近期状态权重',
lineup_integrity_weight DECIMAL(5, 4) DEFAULT 0.2000 COMMENT '阵容完整度权重',
star_status_weight DECIMAL(5, 4) DEFAULT 0.1500 COMMENT '球星状态权重',
environment_weight DECIMAL(5, 4) DEFAULT 0.1000 COMMENT '环境因素权重',
matchup_advantage_weight DECIMAL(5, 4) DEFAULT 0.2000 COMMENT '对位优势权重',
score_diff_weight DECIMAL(5, 4) DEFAULT 0.3000 COMMENT '分差调整权重',
momentum_weight DECIMAL(5, 4) DEFAULT 0.2000 COMMENT '势头调整权重',
-- 系统配置参数
data_update_frequency INT DEFAULT 30 COMMENT '数据更新频率(秒)',
analysis_frequency INT DEFAULT 30 COMMENT '分析频率(秒)',
push_failed_orders BOOLEAN DEFAULT false COMMENT '是否推送失败订单',
push_frequency VARCHAR(20) DEFAULT 'REALTIME' COMMENT '推送频率:REALTIME/BATCH',
batch_push_interval INT DEFAULT 1 COMMENT '批量推送间隔(秒)',
created_at BIGINT NOT NULL,
updated_at BIGINT NOT NULL,
INDEX idx_account_id (account_id),
INDEX idx_enabled (enabled),
INDEX idx_strategy_name (strategy_name),
FOREIGN KEY (account_id) REFERENCES wallet_accounts(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='NBA量化策略配置表';
-- 4. 交易信号表
CREATE TABLE IF NOT EXISTS nba_trading_signals (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
strategy_id BIGINT NOT NULL COMMENT '策略 ID',
game_id BIGINT COMMENT '比赛 ID',
market_id BIGINT COMMENT '市场 ID',
signal_type VARCHAR(10) NOT NULL COMMENT '信号类型:BUY/SELL',
direction VARCHAR(10) NOT NULL COMMENT '方向:YES/NO',
price DECIMAL(5, 4) NOT NULL COMMENT '价格(0-1',
quantity DECIMAL(20, 8) NOT NULL COMMENT '数量',
total_amount DECIMAL(20, 8) NOT NULL COMMENT '总金额(USDC',
reason TEXT COMMENT '触发原因',
win_probability DECIMAL(5, 4) COMMENT '获胜概率',
trade_value DECIMAL(5, 4) COMMENT '交易价值',
signal_status VARCHAR(20) DEFAULT 'GENERATED' COMMENT '信号状态:GENERATED/EXECUTING/SUCCESS/FAILED',
execution_result TEXT COMMENT '执行结果',
error_message TEXT COMMENT '错误信息',
created_at BIGINT NOT NULL,
updated_at BIGINT NOT NULL,
INDEX idx_strategy_id (strategy_id),
INDEX idx_game_id (game_id),
INDEX idx_market_id (market_id),
INDEX idx_signal_type (signal_type),
INDEX idx_signal_status (signal_status),
INDEX idx_created_at (created_at),
FOREIGN KEY (strategy_id) REFERENCES nba_quantitative_strategies(id) ON DELETE CASCADE,
FOREIGN KEY (game_id) REFERENCES nba_games(id) ON DELETE SET NULL,
FOREIGN KEY (market_id) REFERENCES nba_markets(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='NBA交易信号表';
-- 5. 策略执行统计表
CREATE TABLE IF NOT EXISTS nba_strategy_statistics (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
strategy_id BIGINT NOT NULL COMMENT '策略 ID',
stat_date DATE NOT NULL COMMENT '统计日期',
total_signals INT DEFAULT 0 COMMENT '总信号数',
buy_signals INT DEFAULT 0 COMMENT '买入信号数',
sell_signals INT DEFAULT 0 COMMENT '卖出信号数',
success_signals INT DEFAULT 0 COMMENT '成功信号数',
failed_signals INT DEFAULT 0 COMMENT '失败信号数',
total_profit DECIMAL(20, 8) DEFAULT 0.00000000 COMMENT '总盈亏(USDC',
total_volume DECIMAL(20, 8) DEFAULT 0.00000000 COMMENT '总交易量(USDC',
created_at BIGINT NOT NULL,
updated_at BIGINT NOT NULL,
UNIQUE KEY uk_strategy_date (strategy_id, stat_date),
INDEX idx_strategy_id (strategy_id),
INDEX idx_stat_date (stat_date),
FOREIGN KEY (strategy_id) REFERENCES nba_quantitative_strategies(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='NBA策略执行统计表';
@@ -0,0 +1,37 @@
-- 移除 NBA 市场相关的外键约束
-- 由于不再在数据库中存储市场信息,需要移除这些外键约束
-- 1. 移除 nba_games 表的外键约束
SET @fk_name = (SELECT CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'nba_games'
AND COLUMN_NAME = 'polymarket_market_id'
AND REFERENCED_TABLE_NAME = 'nba_markets'
LIMIT 1);
SET @sql = IF(@fk_name IS NOT NULL,
CONCAT('ALTER TABLE nba_games DROP FOREIGN KEY ', @fk_name),
'SELECT "Foreign key constraint not found"');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- 2. 移除 nba_trading_signals 表的外键约束(如果存在)
SET @fk_name = (SELECT CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'nba_trading_signals'
AND COLUMN_NAME = 'market_id'
AND REFERENCED_TABLE_NAME = 'nba_markets'
LIMIT 1);
SET @sql = IF(@fk_name IS NOT NULL,
CONCAT('ALTER TABLE nba_trading_signals DROP FOREIGN KEY ', @fk_name),
'SELECT "Foreign key constraint not found"');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
@@ -1,14 +0,0 @@
-- ============================================
-- V18: 增加 copy_ratio 字段的精度,支持更小的小数值
-- ============================================
-- 修改 copy_trading 表的 copy_ratio 字段精度
-- 从 DECIMAL(10, 2) 改为 DECIMAL(20, 8),支持更小的跟单比例值(如 0.001)
ALTER TABLE copy_trading
MODIFY COLUMN copy_ratio DECIMAL(20, 8) NOT NULL DEFAULT 1.00000000 COMMENT '跟单比例(仅在copyMode=RATIO时生效)';
-- 修改 copy_trading_templates 表的 copy_ratio 字段精度
-- 从 DECIMAL(10, 2) 改为 DECIMAL(20, 8),支持更小的跟单比例值(如 0.001)
ALTER TABLE copy_trading_templates
MODIFY COLUMN copy_ratio DECIMAL(20, 8) NOT NULL DEFAULT 1.00000000 COMMENT '跟单比例(仅在copyMode=RATIO时生效)';
@@ -1,22 +0,0 @@
-- ============================================
-- V19: 创建市场信息表
-- ============================================
-- 创建 markets 表
CREATE TABLE IF NOT EXISTS markets (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
market_id VARCHAR(100) NOT NULL UNIQUE COMMENT '市场IDcondition ID',
title VARCHAR(500) NOT NULL COMMENT '市场名称(question',
slug VARCHAR(200) COMMENT '市场slug',
category VARCHAR(50) COMMENT '市场分类',
icon VARCHAR(500) COMMENT '市场图标URL',
image VARCHAR(500) COMMENT '市场图片URL',
description TEXT COMMENT '市场描述',
active BOOLEAN NOT NULL DEFAULT TRUE COMMENT '是否活跃',
closed BOOLEAN NOT NULL DEFAULT FALSE COMMENT '是否已关闭',
archived BOOLEAN NOT NULL DEFAULT FALSE COMMENT '是否已归档',
created_at BIGINT NOT NULL COMMENT '创建时间(时间戳,毫秒)',
updated_at BIGINT NOT NULL COMMENT '更新时间(时间戳,毫秒)',
INDEX idx_market_id (market_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='市场信息表';
@@ -1,19 +0,0 @@
-- ============================================
-- V20: 添加关键字过滤字段
-- 在 copy_trading 表中添加关键字过滤配置
-- 支持白名单和黑名单两种模式
-- ============================================
-- 添加关键字过滤模式字段
-- DISABLED: 不启用关键字过滤(默认)
-- WHITELIST: 白名单模式,只跟单包含关键字的市场
-- BLACKLIST: 黑名单模式,不跟单包含关键字的市场
ALTER TABLE copy_trading
ADD COLUMN keyword_filter_mode VARCHAR(20) NOT NULL DEFAULT 'DISABLED' COMMENT '关键字过滤模式(DISABLED/WHITELIST/BLACKLIST' AFTER max_position_count;
-- 添加关键字列表字段(JSON 数组)
-- 当 keyword_filter_mode 为 DISABLED 时,此字段为 NULL
-- 当 keyword_filter_mode 为 WHITELIST 或 BLACKLIST 时,此字段存储关键字数组,例如:["NBA", "足球", "NBA总决赛"]
ALTER TABLE copy_trading
ADD COLUMN keywords JSON NULL COMMENT '关键字列表(JSON数组),仅在WHITELIST或BLACKLIST模式下使用' AFTER keyword_filter_mode;
@@ -1,8 +0,0 @@
-- ============================================
-- V21: 添加 event_slug 字段到 markets 表
-- 用于存储跳转用的 slug(从 events[0].slug 获取)
-- ============================================
ALTER TABLE markets
ADD COLUMN event_slug VARCHAR(200) NULL COMMENT '跳转用的 slug(从 events[0].slug 获取,用于构建 URL' AFTER slug;
@@ -1,20 +0,0 @@
-- ============================================
-- V22: 添加市场截止时间筛选功能
-- 1. 为 copy_trading 表添加 max_market_end_date 字段
-- 2. 为 markets 表添加 end_date 字段并清空已有数据
-- ============================================
-- 1. 添加市场截止时间筛选字段到 copy_trading 表
-- 仅跟单截止时间小于设置时间的订单
-- 存储毫秒时间戳,NULL 表示不启用此筛选
ALTER TABLE copy_trading
ADD COLUMN max_market_end_date BIGINT NULL COMMENT '市场截止时间限制(毫秒时间戳),仅跟单截止时间小于此时间的订单,NULL表示不启用';
-- 2. 为 markets 表添加 end_date 字段
-- 删除已有数据,让系统重新从 API 获取,确保所有数据的 end_date 都正确填充
DELETE FROM markets;
-- 添加市场截止时间字段
ALTER TABLE markets
ADD COLUMN end_date BIGINT NULL COMMENT '市场截止时间(毫秒时间戳),NULL表示未设置' AFTER archived;
@@ -1,19 +0,0 @@
-- ============================================
-- V23: 移除跟单关系的唯一约束
-- 允许同一用户创建多个相同 leader 的跟单配置
-- ============================================
-- 删除唯一约束 uk_account_leader
SET @uk_name = (SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'copy_trading'
AND CONSTRAINT_TYPE = 'UNIQUE'
AND CONSTRAINT_NAME = 'uk_account_leader'
LIMIT 1);
SET @sql = IF(@uk_name IS NOT NULL,
CONCAT('ALTER TABLE copy_trading DROP INDEX ', @uk_name),
'SELECT 1');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
@@ -1,13 +0,0 @@
-- ============================================
-- V24: 添加推送已过滤订单字段到模板表和跟单配置表
-- 用于配置是否推送被过滤的订单通知,默认关闭
-- ============================================
-- 添加推送已过滤订单字段到模板表
ALTER TABLE copy_trading_templates
ADD COLUMN push_filtered_orders BOOLEAN NOT NULL DEFAULT FALSE COMMENT '推送已过滤订单(默认关闭)';
-- 添加推送已过滤订单字段到跟单配置表
ALTER TABLE copy_trading
ADD COLUMN push_filtered_orders BOOLEAN NOT NULL DEFAULT FALSE COMMENT '推送已过滤订单(默认关闭)';
@@ -1,12 +0,0 @@
-- ============================================
-- V25: 添加订单来源字段到跟单订单跟踪表
-- 用于记录订单是从哪个数据源接收到的(activity-ws 或 onchain-ws
-- ============================================
-- 添加订单来源字段
ALTER TABLE copy_order_tracking
ADD COLUMN source VARCHAR(20) NOT NULL DEFAULT 'unknown' COMMENT '订单来源:activity-wsPolymarket WebSocket)、onchain-wsOnChain WebSocket';
-- 对于已有数据,设置为默认值 unknown(不影响现有功能)
-- 新创建的记录会在创建时自动填充此字段
@@ -0,0 +1,59 @@
package com.wrbug.polymarketbot.service.nba
import com.wrbug.polymarketbot.util.RetrofitFactory
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
/**
* NBA 比赛服务测试
* 用于验证 API 调用是否正确
*/
@SpringBootTest
class NbaGameServiceTest {
@Autowired
private lateinit var retrofitFactory: RetrofitFactory
@Test
fun testNbaStatsApi() {
runBlocking {
try {
val nbaStatsApi = retrofitFactory.createNbaStatsApi()
// 测试获取今天的比赛
val today = java.time.LocalDate.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd"))
println("测试日期: $today")
val response = nbaStatsApi.getScoreboard(gameDate = today)
println("响应状态码: ${response.code()}")
println("响应消息: ${response.message()}")
if (response.isSuccessful && response.body() != null) {
val scoreboard = response.body()!!
println("ResultSets 数量: ${scoreboard.resultSets.size}")
scoreboard.resultSets.forEachIndexed { index, resultSet ->
println("ResultSet[$index]: name=${resultSet.name}, headers=${resultSet.headers.size}, rows=${resultSet.rowSet.size}")
if (resultSet.headers.isNotEmpty()) {
println(" Headers: ${resultSet.headers.take(5)}...")
}
if (resultSet.rowSet.isNotEmpty()) {
println(" First row size: ${resultSet.rowSet.first().size}")
println(" First row: ${resultSet.rowSet.first().take(5)}...")
}
}
} else {
println("API 调用失败")
println("错误响应体: ${response.errorBody()?.string()}")
}
} catch (e: Exception) {
println("测试异常: ${e.message}")
e.printStackTrace()
}
}
}
}
-5
View File
@@ -76,11 +76,6 @@ JWT_SECRET=${JWT_SECRET}
# 管理员密码重置密钥(已自动生成随机值,生产环境建议修改)
ADMIN_RESET_PASSWORD_KEY=${ADMIN_RESET_KEY}
# 日志级别配置(可选,默认值:root=WARN, app=INFO
# 可选值:TRACE, DEBUG, INFO, WARN, ERROR, OFF
# LOG_LEVEL_ROOT=WARN
# LOG_LEVEL_APP=INFO
EOF
info ".env 文件已创建,已自动生成随机密码和密钥"
warn "生产环境建议修改以下参数:"

Some files were not shown because too many files have changed in this diff Show More