Compare commits

...

47 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 6e5ccdfe5c fix: 修复 walletType 字段问题和 WebSocket 内存泄漏,优化账户导入 UI
后端修复:
- 添加 walletType 字段到 Account 实体和数据库(V17 迁移)
- 修复 refreshProxyAddress 方法使用正确的 walletType
- 修复 WebSocketTicketService 内存泄漏(添加定时清理任务)

前端优化:
- 账户导入改为 Modal 弹窗,提升用户体验
- 默认钱包类型改为 Web3 Wallet(safe),并将其选项排在首位
- 更新多语言文本:将 MetaMask(浏览器钱包)改为 Web3钱包
- 修复 walletTypeHelp Tooltip 换行显示问题
2026-01-03 18:29:39 +08:00
WrBug 21da06d8b0 Merge pull request #12 from wry5560/feature/wallet-type-support
feat: 添加WebSocket票据认证和钱包类型支持
2026-01-03 17:57:27 +08:00
wry5560 5a83444b56 feat: 添加WebSocket票据认证和钱包类型支持
- 新增WebSocket票据服务,用于短期有效的WebSocket连接认证
- 支持Magic和Safe两种钱包类型,分别对应邮箱/OAuth登录和MetaMask用户
- 添加登录频率限制和安全防护
- 优化日志记录,屏蔽敏感信息
- 升级ethers.js到v6.16.0
- 新增多语言钱包类型说明
- 重构代理地址计算逻辑,支持CREATE2计算Magic代理地址
2026-01-03 12:27:59 +08:00
WrBug 591e678f73 Merge pull request #11 from WrBug/pre_release
Release v1.1.3: 添加 Telegram 通知功能
2026-01-02 05:13:45 +08:00
WrBug 1cfdcb88d3 feat: 优化 Telegram 通知功能
- 简化消息内容,仅保留标题、版本号等关键信息
- Docker 构建前发送构建中通知
- Docker 构建成功后添加部署文档链接
- PR 合并消息标题改为 'main 分支代码更新'
2026-01-02 05:12:24 +08:00
WrBug 2cc9cf82ba Merge pull request #10 from WrBug/pre_release
Release v1.1.3: 添加 Telegram 通知功能
2026-01-02 05:05:52 +08:00
WrBug 00f0898d98 fix: 修复 workflow YAML 语法错误,移除 heredoc 格式
- 将 Python 脚本的 heredoc 格式改为使用多个 echo 命令
- 避免 GitHub Actions YAML 解析器将 heredoc 误判为 YAML 语法
- 确保 workflow 文件符合 GitHub Actions 语法规范
2026-01-02 05:04:02 +08:00
WrBug 1de9b5e958 fix: PR 仅关闭时不发送 Telegram 通知
- 在发送通知步骤中添加 PR 合并状态检查
- 如果 PR 仅关闭(未合并),直接退出,不发送通知
- 确保只有合并到 main 分支的 PR 才会发送通知
2026-01-02 05:02:10 +08:00
WrBug c8e422a94b fix: 修复 workflow YAML 语法错误,移除 heredoc 格式
- 修复 docker-build.yml 和 telegram-notify.yml 中的 heredoc 格式问题
- 使用 $'\n' 格式构建多行字符串,避免 YAML 解析器误判
- 确保所有 workflow 文件符合 GitHub Actions YAML 语法规范
2026-01-02 04:58:14 +08:00
WrBug 60e9f9235d fix: 修复 workflow YAML 语法错误
- 修复 docker-build.yml 和 telegram-notify.yml 中的多行字符串格式问题
- 使用 heredoc 格式构建消息,避免 YAML 解析器误判
- 使用 $'\n' 格式追加内容,避免多行字符串导致 YAML 解析问题
2026-01-02 04:57:00 +08:00
WrBug 46baa416f4 fix: 修复 telegram-notify.yml YAML 语法错误
- 使用 heredoc 格式构建消息,避免 YAML 解析器误判
- 修复第 75 行的多行字符串格式问题
2026-01-02 04:52:50 +08:00
WrBug ecdb8af14a feat: 添加 Telegram 通知功能
- 添加 PR 合并到 main 分支时的 Telegram 通知
- 添加 Docker 镜像构建成功时的 Telegram 通知
- 支持 Markdown 转 HTML 格式
- 移除作者、仓库、变更统计、提交记录等冗余信息
2026-01-02 04:48:23 +08:00
WrBug d6027e48eb fix: 优化订单状态更新逻辑,避免误删订单和匹配明细
- 先检查 HTTP 状态码,非 200 的都跳过,不删除订单
- 只有当 HTTP 200 且响应体为 null 时,才表示订单不存在
- 保护已部分卖出的订单,如果已部分卖出则保留用于统计
- 避免因临时网络问题或 API 错误导致订单和 sell_match_detail 被误删
2026-01-02 04:13:10 +08:00
WrBug eb45013a93 Merge pull request #9 from WrBug/pre_release
fix: 修复前端编译错误(v1.1.2 后续修复)
2026-01-02 00:28:58 +08:00
WrBug f97bd5b9d9 docs: 更新 v1.1.2 发布说明,添加前端编译错误修复 2026-01-02 00:25:43 +08:00
WrBug df272156cf fix: 修复前端编译错误,移除不存在的 bestBid 属性引用 2026-01-02 00:24:31 +08:00
WrBug 502b4fb0b7 Merge pull request #8 from WrBug/pre_release
Release v1.1.2
2026-01-02 00:21:56 +08:00
WrBug 64f6a2b897 docs: 添加 v1.1.2 发布说明 2026-01-02 00:19:14 +08:00
WrBug f7f2411b9d 优化市场价格服务:移除降级查询逻辑,仅保留链上和订单簿查询
- 移除 CLOB Trades、Gamma Market Status、Gamma Market Price 查询逻辑
- 只保留链上 RPC 查询(市场结算结果)和 CLOB 订单簿查询
- 如果所有数据源都失败,抛出 IllegalStateException 异常
- 价格截位到 4 位小数(向下截断,不四舍五入)
- 移除未使用的导入和方法(MarketResponse, JsonUtils, OutcomeResult 等)
- 所有调用方都已正确处理异常,无需额外修改
2026-01-02 00:14:42 +08:00
WrBug d96eb3e00a fix: 恢复 V1 migration 文件,避免 checksum 不匹配
保持 V1__init_database.sql 的原有内容不变,避免破坏已有数据库的 migration checksum。
failed_trade 表的删除将通过 V16 migration 处理。
2026-01-01 22:33:23 +08:00
WrBug cc1a732984 refactor: 移除下单失败存储数据库的功能
- 删除 FailedTrade 实体类和 FailedTradeRepository
- 从 CopyOrderTrackingService 中移除失败交易存储逻辑
- 移除 recordFailedTrade 方法
- 移除检查失败交易的代码
- 创建 Flyway migration V16 删除 failed_trade 表
- 从 V1__init_database.sql 中移除 failed_trade 表创建语句

下单失败时仅记录日志,不再存储到数据库
2026-01-01 22:24:47 +08:00
WrBug ec06003157 feat: 自动使用当前分支名作为 Docker 版本号
- deploy.sh 自动获取当前 Git 分支名作为版本号
- 分支名中的 / 自动替换为 -(Docker tag 不支持 /)
- docker-compose.yml 启用 build args,从环境变量读取版本号
- 前端页面将显示当前分支名作为版本号
- 如果没有 Git 仓库或获取失败,使用默认值 'dev'
2026-01-01 22:10:21 +08:00
WrBug 3d05b13298 fix: 修复内存泄漏问题,缓存和复用 Retrofit/OkHttpClient 实例
问题:
- 每次调用 createClobApi、createGammaApi 等方法都创建新的 OkHttpClient 和 Retrofit 实例
- OkHttpClient 包含连接池、线程池等资源,导致内存不断增长
- 运行几小时后内存从 400MB 涨到 1GB+

解决方案:
- 为不需要认证的 API 创建共享的 OkHttpClient 实例
- Gamma API、Data API、GitHub API、不带认证的 CLOB API 使用单例客户端
- 带认证的 CLOB API 按钱包地址缓存(每个账户一个客户端)
- RPC API 按 RPC URL 缓存
- Builder Relayer API 按 relayerUrl 缓存
- 添加 @PreDestroy 方法清理缓存

效果:
- 大幅减少内存占用,避免内存泄漏
- 复用连接池和线程池,提高性能
- 内存占用将保持稳定,不再持续增长
2026-01-01 22:07:24 +08:00
WrBug fe2db11b75 其他代码修改
- CopyOrderTrackingRepository: 添加查询方法
- PolymarketClobService: 功能更新
- CopyOrderTrackingService: 逻辑优化
- OrderStatusUpdateService: 功能增强
- TelegramNotificationService: 通知优化
2026-01-01 12:03:05 +08:00
WrBug 5c18cbd95d 统一 Gson 使用,改为依赖注入方式
- 在 GsonConfig 中统一配置 Gson Bean(lenient 模式)
- 所有 Service 类通过构造函数注入 Gson 实例
- RetrofitFactory、BlockchainService、PolymarketApiKeyService 等统一使用注入的 Gson
- AccountService、PositionCheckService 添加 JsonUtils 注入
- OnChainWsUtils(object 单例)使用私有 Gson 实例(与 GsonConfig 配置一致)
- 移除所有 GsonConverterFactory.create() 无参调用,统一使用注入的 Gson
2026-01-01 12:01:17 +08:00
WrBug ad2fa4eef2 Merge pull request #7 from WrBug/pre_release
Release v1.1.1: 链上监听优化、市场查询优化和 Bug 修复
2025-12-28 05:13:18 +08:00
WrBug 8357546f3c docs: 更新 v1.1.1 release 文档,包含所有改动内容 2025-12-28 05:10:19 +08:00
WrBug a62141ea5e chore: 更新 RELEASE.md 为 v1.1.1 2025-12-28 05:04:45 +08:00
WrBug 9157203653 fix: 修复移动端 API 健康页面缺少数据显示
- 移动端添加 URL 地址显示
- 移动端添加状态文本显示(正常/异常/未配置)
- 移动端添加消息/状态信息显示
- 移动端和桌面端显示信息保持一致
2025-12-28 05:04:27 +08:00
WrBug 23fc20763a fix: 修复移动端 API 健康页面缺少数据显示
- 移动端添加 URL 地址显示
- 移动端添加状态文本显示(正常/异常/未配置)
- 移动端添加消息/状态信息显示
- 移动端和桌面端显示信息保持一致
2025-12-28 05:02:54 +08:00
WrBug e96a0b6279 feat: 优化 Telegram 推送消息格式
- 添加价格和数量截位处理:
  * 价格保留最多4位小数(截断,不四舍五入)
  * 数量保留最多2位小数(截断,不四舍五入)
- 优化账户信息显示格式:
  * 有账户名和钱包地址时显示:账户名(0x123...123)
  * 只有账户名时显示账户名
  * 只有钱包地址时显示脱敏后的地址
  * 都没有时显示未知账户
- 统一使用 buildAccountInfo 函数构建账户信息
- 应用到所有消息类型:订单成功、订单失败、订单被过滤、仓位赎回
2025-12-28 04:58:30 +08:00
WrBug 85d8619fe7 refactor: 统一发布说明文件,使用 RELEASE.md 替代版本化文件
- 创建 RELEASE.md,包含最新的 v1.1.0 发布说明
- 删除 RELEASE_v1.0.1.md 和 RELEASE_v1.1.0.md
- 仅保留最新的发布说明文件,简化维护
2025-12-28 04:53:47 +08:00
WrBug 89fb980da7 refactor: 移除 polygon.rpc.url 配置,使用 RpcNodeService 统一管理 RPC 节点
- 删除 application.properties 中的 polygon.rpc.url 配置
- 更新 ApiHealthCheckService 直接使用 RpcNodeService.getHttpUrl()
- 删除所有 Docker Compose 配置中的 POLYGON_RPC_URL 环境变量
- 删除所有部署脚本中的 POLYGON_RPC_URL 环境变量
- 更新所有文档,移除 POLYGON_RPC_URL 相关说明
- 删除 application.properties 中无用的 position.push 配置项
- 修正日志配置中的包名(polyhermes -> polymarketbot)

现在系统通过 RpcNodeService 从数据库读取 RPC 节点配置,用户可以通过系统设置页面管理 RPC 节点,不再需要环境变量配置。
2025-12-28 04:51:07 +08:00
WrBug 9f0b22fab5 feat: 优化市场状态查询和自动订单通知
- 使用 Retrofit 替代直接 HTTP 请求查询市场信息(通过 clob_token_ids)
- 实现链上查询市场结算结果(方案1):通过 ConditionalTokens 合约的 getCondition 方法实时获取市场结算状态
- 自动生成的订单(AUTO_、AUTO_FIFO_、AUTO_WS_)不发送 Telegram 通知
- 添加 ABI 编码解析工具方法(decodeUint256Array、decodeConditionResult)
- 优化 PositionCheckService:优先使用链上查询,失败时降级到 API 查询
2025-12-28 00:02:29 +08:00
WrBug 5e544cda07 fix: 修复固定金额模式卖出数量计算逻辑
- 删除卖出订单的 notificationSent 字段,使用 priceUpdated 作为共用字段
- 添加 leaderBuyQuantity 字段,在创建 CopyOrderTracking 时存储 Leader 买入数量
- 修复固定金额模式下卖出数量计算:优先使用存储的 leaderBuyQuantity,避免 API 查询失败
- 添加数据库迁移 V14 和 V15
- 优化 calculateSellQuantityForFixedMode 方法,支持新数据和旧数据兼容
2025-12-26 05:42:53 +08:00
WrBug b6d66aa81e docs: 添加 Docker 版本更新说明(中英文)
- 在部署文档中添加详细的 Docker 版本更新步骤(中文和英文)
- 在 README 中添加简要的更新说明和文档链接(中文和英文)
- 包含更新到最新版本和特定版本的方法
- 添加注意事项(备份数据库、服务中断等)
2025-12-26 02:30:06 +08:00
WrBug 62669719d9 feat: 实现卖出订单价格轮询更新和订单状态管理
- 添加 price_updated 字段到 sell_match_record 表,用于标记价格是否已更新
- 创建 OrderStatusUpdateService 定时任务服务,每5秒轮询一次:
  - 更新卖出订单的实际成交价(通过 orderId 查询订单详情)
  - 清理已删除账户的订单记录
- 修改 processSellTrade:下单完成后不再立即查询价格,直接保存,等待定时任务更新
- 添加 orderId 格式验证:非0x开头的直接标记为已更新,0x开头的等待定时任务更新
- 启用 Spring 定时任务功能(@EnableScheduling)
- 将 getActualExecutionPrice 方法改为 public,供定时任务调用
2025-12-26 02:20:48 +08:00
WrBug a6ca6cc3ff feat: 支持 RPC 节点启用/禁用功能
- 后端:更新 RpcNodeService,确保禁用的节点被忽略
  - getAllNodesWithDefault() 只返回启用的节点
  - checkAllNodesHealth() 只检查启用的节点
  - getAvailableNode() 已正确实现,只查询启用的节点

- 前端:添加启用/禁用开关功能
  - 在 RpcNodeSettings 页面添加启用列,使用 Switch 组件
  - 添加 handleToggleEnabled 函数处理启用/禁用切换
  - 添加多语言支持(中文简体、繁体、英文)
2025-12-26 01:57:22 +08:00
WrBug d1f351108f fix: 修复跟单卖出订单的 API 凭证解密和精度问题
- 修复 processSellTrade 中 API 凭证未解密的问题,与 processBuyTrade 保持一致
- 修复 SELL 订单的 makerAmount 和 takerAmount 精度问题:
  - makerAmount (shares) 最多 2 位小数(符合 API 要求)
  - takerAmount (USDC) 最多 4 位小数(符合 API 要求)
2025-12-26 01:50:03 +08:00
WrBug c4ac3a8fe0 feat: 实现链上 WebSocket 监听和并行监控策略
- 创建 OnChainWsService,支持通过 Polygon RPC eth_subscribe 实时监听链上交易
- 实现并行监控策略:链上 WS 和轮询同时运行,哪个数据先返回用哪个
- 支持通过 eth_unsubscribe 取消单个 Leader 的订阅
- 在 CopyOrderTrackingService 中添加 Mutex 保证线程安全
- 实现链上交易解析逻辑(USDC Transfer + ERC1155 Transfer)
- 实现 Gamma API 元数据补齐逻辑
- 优化 WebSocket 连接管理:只创建一个连接,没有跟单配置时自动取消
- 跟单配置生效/失效时及时更新 WebSocket 订阅
- 使用 Gson 替换所有 JSON 解析,JsonRpcResponse.result 使用 JsonElement 类型
- 添加相关文档:copy-trading-logic-summary.md 和 copy-trading-monitor-strategy.md
2025-12-26 01:36:00 +08:00
WrBug 1580b60d24 feat: 实现RPC节点管理功能
- 后端功能:
  - 添加RPC节点配置管理(增删改查、优先级调整)
  - 实现节点健康检查和自动故障转移
  - 修复Retrofit baseUrl处理问题(使用拦截器动态替换URL)
  - 添加RPC节点可用性验证(创建前验证)
  - 默认节点作为兜底,不返回给前端,始终排在最后
  - API健康检查使用动态获取的可用节点

- 前端功能:
  - 添加RPC节点设置页面
  - 支持添加、删除、检查节点
  - 支持调整节点优先级
  - 完整的多语言支持(中文简体/繁体、英文)
  - 添加菜单项多语言key

- 数据库:
  - 添加RPC节点配置表迁移脚本
2025-12-25 23:58:50 +08:00
WrBug 7bc059ec34 fix: 修复前端编译错误
- 移除 AccountImportForm 中未使用的 message 导入
- 移除 AccountImport 中 handleSuccess 未使用的 accountId 参数
2025-12-23 01:08:54 +08:00
WrBug b5908a9bbf Merge pull request #6 from WrBug/pre_release
feat: 添加最大仓位限制配置功能 (v1.0.3)
2025-12-23 01:02:07 +08:00
WrBug c20e0138df feat: 添加最大仓位限制配置功能
后端变更:
- 添加最大仓位金额(maxPositionValue)和最大仓位数量(maxPositionCount)配置字段
- 实现按市场检查仓位限制的过滤逻辑
- 添加数据库迁移脚本 V11__add_max_position_config.sql

前端变更:
- 重构账户导入和Leader添加为可复用组件(AccountImportForm, LeaderAddForm)
- 在CopyTradingAdd页面集成账户导入和Leader添加Modal
- 在CopyTradingAdd/CopyTradingEdit页面添加最大仓位限制配置项
- 补充所有多语言文件中的缺失键,确保zh-CN/zh-TW/en三种语言文件一致

功能说明:
- 支持设置单个市场的最大仓位金额限制(USDC)
- 支持设置单个市场的最大仓位数量限制
- 两个限制可以同时启用,下单前会检查当前市场仓位是否超过限制
2025-12-23 00:56:04 +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
WrBug 20acc72ed6 Merge pull request #4 from WrBug/pre_release
v1.0.2: 优化系统配置和统计功能
2025-12-11 03:20:50 +08:00
150 changed files with 21181 additions and 1672 deletions
+89
View File
@@ -36,6 +36,49 @@ jobs:
echo "Extracted version: $VERSION"
echo "Full tag: $TAG_NAME"
- name: Send Telegram notification (build started)
env:
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
run: |
# 检查必要的环境变量
if [ -z "$TELEGRAM_BOT_TOKEN" ] || [ -z "$TELEGRAM_CHAT_ID" ]; then
echo "⚠️ Telegram Bot Token 或 Chat ID 未配置,跳过通知"
exit 0
fi
# 获取构建信息
VERSION="${{ steps.extract_version.outputs.VERSION }}"
TAG="${{ steps.extract_version.outputs.TAG }}"
RELEASE_URL="${{ github.event.release.html_url }}"
# 构建消息内容(仅包含关键信息)
MESSAGE="🔨 <b>Docker 镜像构建中</b>"$'\n'$'\n'"📦 <b>版本:</b> ${VERSION}"$'\n'"🏷️ <b>Tag:</b> <code>${TAG}</code>"$'\n'"🔗 <a href=\"${RELEASE_URL}\">查看 Release</a>"
# 发送 Telegram 消息(使用 jq 转义 JSON
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-H "Content-Type: application/json" \
-d "$(jq -n \
--arg chat_id "$TELEGRAM_CHAT_ID" \
--arg text "$MESSAGE" \
'{chat_id: $chat_id, text: $text, parse_mode: "HTML", disable_web_page_preview: false}')" > /tmp/telegram_response.json
# 检查发送结果
if [ $? -eq 0 ]; then
RESPONSE=$(cat /tmp/telegram_response.json)
if echo "$RESPONSE" | grep -q '"ok":true'; then
echo "✅ Telegram 通知发送成功"
else
echo "❌ Telegram 通知发送失败: $RESPONSE"
# 通知失败不应该导致整个 job 失败
exit 0
fi
else
echo "❌ 发送 Telegram 消息时发生错误"
# 通知失败不应该导致整个 job 失败
exit 0
fi
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
@@ -65,3 +108,49 @@ jobs:
GITHUB_REPO_URL=https://github.com/WrBug/PolyHermes
cache-from: type=registry,ref=wrbug/polyhermes:latest
cache-to: type=inline
- name: Send Telegram notification
env:
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
run: |
# 检查必要的环境变量
if [ -z "$TELEGRAM_BOT_TOKEN" ] || [ -z "$TELEGRAM_CHAT_ID" ]; then
echo "⚠️ Telegram Bot Token 或 Chat ID 未配置,跳过通知"
exit 0
fi
# 获取构建信息
VERSION="${{ steps.extract_version.outputs.VERSION }}"
TAG="${{ steps.extract_version.outputs.TAG }}"
RELEASE_NAME="${{ github.event.release.name }}"
RELEASE_URL="${{ github.event.release.html_url }}"
REPO_NAME="${{ github.repository }}"
# 构建消息内容(仅包含关键信息)
DEPLOY_DOC_URL="https://github.com/WrBug/PolyHermes/blob/main/docs/zh/DEPLOYMENT.md"
MESSAGE="✅ <b>Docker 镜像构建成功</b>"$'\n'$'\n'"📦 <b>版本:</b> ${VERSION}"$'\n'"🏷️ <b>Tag:</b> <code>${TAG}</code>"$'\n'"🔗 <a href=\"${RELEASE_URL}\">查看 Release</a>"$'\n'"📚 <a href=\"${DEPLOY_DOC_URL}\">Docker 部署文档</a>"
# 发送 Telegram 消息(使用 jq 转义 JSON
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-H "Content-Type: application/json" \
-d "$(jq -n \
--arg chat_id "$TELEGRAM_CHAT_ID" \
--arg text "$MESSAGE" \
'{chat_id: $chat_id, text: $text, parse_mode: "HTML", disable_web_page_preview: false}')" > /tmp/telegram_response.json
# 检查发送结果
if [ $? -eq 0 ]; then
RESPONSE=$(cat /tmp/telegram_response.json)
if echo "$RESPONSE" | grep -q '"ok":true'; then
echo "✅ Telegram 通知发送成功"
else
echo "❌ Telegram 通知发送失败: $RESPONSE"
# 构建成功,通知失败不应该导致整个 job 失败
exit 0
fi
else
echo "❌ 发送 Telegram 消息时发生错误"
# 构建成功,通知失败不应该导致整个 job 失败
exit 0
fi
+103
View File
@@ -0,0 +1,103 @@
name: Telegram Notification on PR Merge
on:
pull_request:
types:
- closed # 当 PR 被关闭(合并或关闭)时触发
jobs:
notify:
runs-on: ubuntu-latest
# 只在 PR 被合并到 main 分支时执行
if: github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'main'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Get PR details
id: pr_details
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_NUMBER="${{ github.event.pull_request.number }}"
REPO="${{ github.repository }}"
# 获取 PR 详细信息
PR_RESPONSE=$(curl -s -H "Authorization: token ${GITHUB_TOKEN}" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${REPO}/pulls/${PR_NUMBER}")
# 获取 PR 变更的文件列表
FILES_RESPONSE=$(curl -s -H "Authorization: token ${GITHUB_TOKEN}" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${REPO}/pulls/${PR_NUMBER}/files")
# 提取 PR 描述(body),保留换行,限制长度
PR_BODY=$(echo "$PR_RESPONSE" | jq -r '.body // ""')
if [ ${#PR_BODY} -gt 500 ]; then
PR_BODY="${PR_BODY:0:500}..."
fi
# 保存到输出变量(使用 base64 编码避免特殊字符问题)
echo "pr_body<<EOF" >> $GITHUB_OUTPUT
echo "$PR_BODY" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Send Telegram notification
env:
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
run: |
# 检查 PR 是否被合并(而不是仅关闭)
PR_MERGED="${{ github.event.pull_request.merged }}"
if [ "$PR_MERGED" != "true" ]; then
echo "ℹ️ PR 仅关闭,未合并,跳过通知"
exit 0
fi
# 检查必要的环境变量
# 注意:TELEGRAM_CHAT_ID 可以是个人聊天 ID(正数)或群组 ID(负数,如 -1001234567890
if [ -z "$TELEGRAM_BOT_TOKEN" ] || [ -z "$TELEGRAM_CHAT_ID" ]; then
echo "⚠️ Telegram Bot Token 或 Chat ID 未配置,跳过通知"
exit 0
fi
# 获取 PR 基本信息
PR_NUMBER="${{ github.event.pull_request.number }}"
PR_TITLE="${{ github.event.pull_request.title }}"
PR_URL="${{ github.event.pull_request.html_url }}"
PR_MERGE_COMMIT="${{ github.event.pull_request.merge_commit_sha }}"
# 获取 PR 详细信息
PR_BODY="${{ steps.pr_details.outputs.pr_body }}"
# 转义 PR 标题中的 HTML 特殊字符
PR_TITLE_ESCAPED=$(echo "$PR_TITLE" | sed 's/&/\&amp;/g' | sed 's/</\&lt;/g' | sed 's/>/\&gt;/g')
# 构建消息内容(仅包含关键信息)
MESSAGE="🚀 <b>main 分支代码更新</b>"$'\n'$'\n'"📝 <b>PR #${PR_NUMBER}:</b> ${PR_TITLE_ESCAPED}"$'\n'"🔗 <a href=\"${PR_URL}\">查看 PR</a>"
# 发送 Telegram 消息(使用 jq 转义 JSON
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-H "Content-Type: application/json" \
-d "$(jq -n \
--arg chat_id "$TELEGRAM_CHAT_ID" \
--arg text "$MESSAGE" \
'{chat_id: $chat_id, text: $text, parse_mode: "HTML", disable_web_page_preview: false}')" > /tmp/telegram_response.json
# 检查发送结果
if [ $? -eq 0 ]; then
RESPONSE=$(cat /tmp/telegram_response.json)
if echo "$RESPONSE" | grep -q '"ok":true'; then
echo "✅ Telegram 通知发送成功"
else
echo "❌ Telegram 通知发送失败: $RESPONSE"
exit 1
fi
else
echo "❌ 发送 Telegram 消息时发生错误"
exit 1
fi
+1
View File
@@ -109,4 +109,5 @@ __pycache__/
# Submodules and external dependencies
clob-client/
builder-relayer-client/
landing-page/
-133
View File
@@ -1,133 +0,0 @@
# 🎉 v1.0.1 版本发布公告
## 📅 发布日期
2025年12月(具体日期待定)
## 🚀 主要更新
### 性能优化
#### 📊 订单簿请求优化
- 优化订单簿获取逻辑,仅在需要时请求,避免不必要的 API 调用
- 当未配置需要订单簿的过滤条件(价差、订单深度)时,跳过订单簿获取
- 订单簿只请求一次,在过滤检查中复用,提高性能
#### 💰 卖出价格计算优化
- 卖出改为市价卖出:优先使用订单簿的 bestBid(最高买入价)
- 如果 bestBid 获取失败,自动降级使用 Leader 的交易价格
- 卖出价格固定按 90% 计算,不再使用价格容忍度配置
- 提高卖出订单的成交率
#### ⚙️ 价格容忍度默认值
- 如果价格容忍度配置为 0,自动使用默认值 5%
- 确保买入订单能够正常应用价格调整,提高成交率
### 代码优化
#### 🎯 使用枚举优化过滤逻辑
- 新增 `FilterResult` 数据类和 `FilterStatus` 枚举
- 使用类型安全的枚举替代字符串判断,提高代码可维护性
- 移除无用字段 `isBuyOrder` 参数
- 优化 `checkFilters` 方法返回值,使用数据类封装结果
#### 📝 代码结构优化
- 减少 if-else 嵌套,使用 Kotlin 链式调用和空安全操作符
- 优化卖出价格计算逻辑,使用 `runCatching``?.let` 简化代码
- 提取公共方法 `calculateFallbackSellPrice`,提高代码复用性
### 功能改进
#### 🗑️ 移除最小订单簿深度功能
- 移除 `minOrderbookDepth`(最小订单簿深度)配置项
- 简化过滤逻辑,只保留 `minOrderDepth`(最小订单深度)
- 更新前端界面,移除相关配置项和提示文案
#### 📈 优化最小订单深度逻辑
- 修改 `minOrderDepth` 检查逻辑,检查所有方向(买盘+卖盘)的总深度
- 不再区分买卖方向,提供更全面的市场流动性评估
#### 🎯 市场结算判断优化
- 添加市场已关闭时的结算判断逻辑
- 如果市场已关闭且该 outcome 赢了(价格 >= 0.99),返回价格为 1
- 如果市场已关闭且该 outcome 输了(价格 <= 0.01),返回价格为 0
- 当没有仓位但有未完成订单时,正确判断市场结算状态并设置价格
### Bug 修复
- ✅ 修复盈亏和持仓价值计算逻辑未实现的问题
- ✅ 修复过滤检查中订单簿可能重复请求的问题
- ✅ 修复健康检查路径不一致问题(从 `/api/health` 改为 `/api/system/health`
## 📦 如何更新
### Docker 部署
```bash
# 拉取最新镜像
docker pull wrbug/polyhermes:v1.0.1
# 或使用 latest 标签
docker pull wrbug/polyhermes:latest
# 重启容器
docker-compose down
docker-compose up -d
```
### 源码部署
```bash
# 拉取最新代码
git fetch origin
git checkout v1.0.1
# 重新构建
cd backend
./gradlew bootJar
# 重启服务
# 根据您的部署方式重启服务
```
## 📚 文档更新
- 更新前端多语言文案,优化提示信息
- 移除最小订单簿深度相关的文档说明
## 🔗 相关链接
- **GitHub Release**: https://github.com/WrBug/PolyHermes/releases/tag/v1.0.1
- **完整更新日志**: https://github.com/WrBug/PolyHermes/compare/v1.0.0...v1.0.1
- **Docker Hub**: https://hub.docker.com/r/wrbug/polyhermes
## ⚠️ 重要提醒
**请务必使用官方 Docker 镜像源,避免财产损失!**
### ✅ 官方 Docker Hub 镜像
**官方镜像地址**`wrbug/polyhermes`
```bash
# ✅ 正确:使用官方镜像
docker pull wrbug/polyhermes:v1.0.1
# ❌ 错误:不要使用其他来源的镜像
# 任何非官方来源的镜像都可能包含恶意代码,导致您的私钥和资产被盗
```
### 🔗 官方渠道
请通过以下**唯一官方渠道**获取 PolyHermes
* **GitHub 仓库**https://github.com/WrBug/PolyHermes
* **Twitter**@polyhermes
* **Telegram 群组**:加入群组
---
**⭐ 如果这个项目对您有帮助,请给个 Star 支持一下!**
**💬 如有问题或建议,欢迎在 GitHub Issues 中反馈。**
+1028
View File
File diff suppressed because it is too large Load Diff
+19 -2
View File
@@ -228,6 +228,25 @@ docker-compose up -d
# image: wrbug/polyhermes:v1.0.0
```
**更新 Docker 版本**
```bash
# 1. 停止当前容器
docker-compose -f docker-compose.prod.yml down
# 2. 拉取最新镜像
docker pull wrbug/polyhermes:latest
# 3. 重新启动服务
docker-compose -f docker-compose.prod.yml up -d
# 或更新到特定版本(例如 v1.0.1)
# 修改 docker-compose.prod.yml 中的镜像标签为: image: wrbug/polyhermes:v1.0.1
# 然后执行: docker-compose -f docker-compose.prod.yml up -d
```
详细更新说明请参考:[部署文档 - 更新 Docker 版本](docs/zh/DEPLOYMENT.md#更新-docker-版本)
2. **本地构建部署(开发环境)**
```bash
@@ -251,7 +270,6 @@ DB_USERNAME=root
DB_PASSWORD=your_password_here
SPRING_PROFILES_ACTIVE=prod
SERVER_PORT=80
POLYGON_RPC_URL=https://polygon-rpc.com
JWT_SECRET=your-jwt-secret-key-change-in-production
ADMIN_RESET_PASSWORD_KEY=your-admin-reset-key-change-in-production
EOF
@@ -318,7 +336,6 @@ cd frontend
| `DB_USERNAME` | 数据库用户名 | `root` |
| `DB_PASSWORD` | 数据库密码 | - |
| `SERVER_PORT` | 后端服务端口 | `8000` |
| `POLYGON_RPC_URL` | Polygon RPC 地址 | `https://polygon-rpc.com` |
| `JWT_SECRET` | JWT 密钥 | - |
| `ADMIN_RESET_PASSWORD_KEY` | 管理员密码重置密钥 | - |
| `CRYPTO_SECRET_KEY` | 加密密钥(用于加密存储私钥和 API Key) | - |
+19 -2
View File
@@ -228,6 +228,25 @@ docker-compose up -d
# image: wrbug/polyhermes:v1.0.0
```
**Update Docker Version**:
```bash
# 1. Stop current containers
docker-compose -f docker-compose.prod.yml down
# 2. Pull latest image
docker pull wrbug/polyhermes:latest
# 3. Restart services
docker-compose -f docker-compose.prod.yml up -d
# Or update to specific version (e.g., v1.0.1)
# Modify image tag in docker-compose.prod.yml to: image: wrbug/polyhermes:v1.0.1
# Then run: docker-compose -f docker-compose.prod.yml up -d
```
For detailed update instructions, please refer to: [Deployment Guide - Update Docker Version](docs/en/DEPLOYMENT.md#update-docker-version)
2. **Local Build Deployment (Development Environment)**
```bash
@@ -251,7 +270,6 @@ DB_USERNAME=root
DB_PASSWORD=your_password_here
SPRING_PROFILES_ACTIVE=prod
SERVER_PORT=80
POLYGON_RPC_URL=https://polygon-rpc.com
JWT_SECRET=your-jwt-secret-key-change-in-production
ADMIN_RESET_PASSWORD_KEY=your-admin-reset-key-change-in-production
EOF
@@ -318,7 +336,6 @@ cd frontend
| `DB_USERNAME` | Database username | `root` |
| `DB_PASSWORD` | Database password | - |
| `SERVER_PORT` | Backend service port | `8000` |
| `POLYGON_RPC_URL` | Polygon RPC address | `https://polygon-rpc.com` |
| `JWT_SECRET` | JWT secret key | - |
| `ADMIN_RESET_PASSWORD_KEY` | Admin password reset key | - |
| `CRYPTO_SECRET_KEY` | Encryption key (for encrypting stored private keys and API Keys) | - |
+278
View File
@@ -0,0 +1,278 @@
# v1.1.2
## 🚀 主要功能
### 🐛 修复内存泄漏问题
- 修复 Retrofit/OkHttpClient 实例重复创建导致的内存泄漏问题
- 为不需要认证的 API 创建共享的 OkHttpClient 实例(Gamma API、Data API、GitHub API 等)
- 带认证的 CLOB API 按钱包地址缓存(每个账户一个客户端)
- RPC API 按 RPC URL 缓存,Builder Relayer API 按 relayerUrl 缓存
- 添加 `@PreDestroy` 方法清理缓存,确保资源正确释放
- **效果**:内存占用从运行几小时后从 400MB 涨到 1GB+ 变为保持稳定,大幅减少内存占用
### 📊 市场价格服务优化
- 移除降级查询逻辑,仅保留链上 RPC 查询和 CLOB 订单簿查询
- 移除 CLOB Trades、Gamma Market Status、Gamma Market Price 查询逻辑
- 如果所有数据源都失败,抛出明确的异常信息
- 价格截位到 4 位小数(向下截断,不四舍五入)
- 简化代码逻辑,提高查询效率和准确性
### 🔧 代码架构优化
- 统一 Gson 使用,改为依赖注入方式
-`GsonConfig` 中统一配置 Gson Beanlenient 模式)
- 所有 Service 类通过构造函数注入 Gson 实例
- 移除所有 `GsonConverterFactory.create()` 无参调用,统一使用注入的 Gson
- 提高代码一致性和可维护性
### 🗑️ 功能清理
- 移除下单失败存储数据库的功能
- 删除 `FailedTrade` 实体类和 `FailedTradeRepository`
-`CopyOrderTrackingService` 中移除失败交易存储逻辑
- 创建 Flyway migration V16 删除 `failed_trade`
- 下单失败时仅记录日志,不再存储到数据库,简化数据模型
### 🚀 部署优化
- 自动使用当前分支名作为 Docker 版本号
- 分支名中的 `/` 自动替换为 `-`Docker tag 不支持 `/
- `docker-compose.yml` 启用 build args,从环境变量读取版本号
- 前端页面将显示当前分支名作为版本号
- 如果没有 Git 仓库或获取失败,使用默认值 `dev`
## 🐛 Bug 修复
### 修复 Flyway Migration 问题
- 恢复 V1 migration 文件,避免 checksum 不匹配
- 保持 `V1__init_database.sql` 的原有内容不变
- `failed_trade` 表的删除通过 V16 migration 处理
- 确保已有数据库的 migration checksum 保持一致
### 修复前端编译错误
- 修复 `PositionList.tsx` 中引用不存在的 `bestBid` 属性导致的编译错误
- 使用 `currentPrice` 替代 `bestBid`,确保前端代码可以正常编译
## 📚 文档更新
- 新增智能资金分析文档(`docs/zh/smart-money-analysis.md`
- 详细说明智能资金分析功能的使用方法和策略
## 🔧 技术改进
- 优化 `RetrofitFactory`,实现客户端实例缓存和复用
- 优化 `CopyOrderTrackingService`,移除失败交易相关逻辑
- 优化 `OrderStatusUpdateService`,增强订单状态更新功能
- 优化 `TelegramNotificationService`,改进通知逻辑
- 优化 `PositionCheckService`,简化代码结构
- 优化 `PolymarketClobService`,改进 API 调用逻辑
## 📦 数据库变更
- 删除 `failed_trade` 表(Migration: V16
## 🔗 相关链接
- **GitHub Release**: https://github.com/WrBug/PolyHermes/releases/tag/v1.1.2
- **完整更新日志**: https://github.com/WrBug/PolyHermes/compare/v1.1.1...v1.1.2
- **Docker Hub**: https://hub.docker.com/r/wrbug/polyhermes
## 📊 统计信息
- **文件变更**: 29 个文件
- **代码变更**: +1597 行 / -678 行
- **主要提交**: 8 个提交
## ⚠️ 重要提醒
**请务必使用官方 Docker 镜像源,避免财产损失!**
### ✅ 官方 Docker Hub 镜像
**官方镜像地址**`wrbug/polyhermes`
```bash
# ✅ 正确:使用官方镜像
docker pull wrbug/polyhermes:v1.1.2
# ❌ 错误:不要使用其他来源的镜像
# 任何非官方来源的镜像都可能包含恶意代码,导致您的私钥和资产被盗
```
### 🔗 官方渠道
请通过以下**唯一官方渠道**获取 PolyHermes
* **GitHub 仓库**https://github.com/WrBug/PolyHermes
* **Twitter**@polyhermes
* **Telegram 群组**:加入群组
---
**⭐ 如果这个项目对您有帮助,请给个 Star 支持一下!**
---
# v1.1.1
## 🚀 主要功能
### 🔗 链上 WebSocket 监听优化
- 创建 `UnifiedOnChainWsService` 统一管理 WebSocket 连接,所有服务共享同一个连接
- 创建 `OnChainWsUtils` 工具类,提取公共的链上 WebSocket 相关功能
- 创建 `AccountOnChainMonitorService` 监听账户链上卖出和赎回事件
- 优化 `OnChainWsService`,复用公共代码,减少代码重复
- 支持通过链上 WebSocket 实时监听账户的卖出和赎回交易,自动更新订单状态
### 📊 市场状态查询优化
- 优化市场结算状态查询,优先使用链上查询 `ConditionalTokens.getCondition`
- 如果链上查询失败,自动降级到 Gamma API 查询
- 提供更实时和准确的市场结算结果
### 🔕 自动订单通知优化
- 自动生成的订单(AUTO_、AUTO_FIFO_、AUTO_WS_ 前缀)不再发送 Telegram 通知
- 优化 `OrderStatusUpdateService`,跳过自动生成订单的通知处理
- 减少不必要的通知,提升用户体验
## 🐛 Bug 修复
### 修复移动端 API 健康页面缺少数据显示
- 移动端添加 URL 地址显示
- 移动端添加状态文本显示(正常/异常/未配置)
- 移动端添加消息/状态信息显示
- 移动端和桌面端显示信息保持一致
## 🔧 功能优化
### 优化 Telegram 推送消息格式
- 添加价格和数量截位处理:
* 价格保留最多4位小数(截断,不四舍五入)
* 数量保留最多2位小数(截断,不四舍五入)
- 优化账户信息显示格式:
* 有账户名和钱包地址时显示:账户名(0x123...123)
* 只有账户名时显示账户名
* 只有钱包地址时显示脱敏后的地址
* 都没有时显示未知账户
### 配置优化
- 移除 `polygon.rpc.url` 配置,使用 RpcNodeService 统一管理 RPC 节点
- 删除无用的 `position.push.polling-interval``position.push.heartbeat-timeout` 配置项
- 修正日志配置中的包名(polyhermes -> polymarketbot
- 更新 `ApiHealthCheckService` 直接使用 `RpcNodeService.getHttpUrl()`
## 📚 文档更新
- 统一发布说明文件,使用 RELEASE.md 替代版本化文件(RELEASE_v1.0.1.md、RELEASE_v1.1.0.md
- 更新所有部署文档,移除 POLYGON_RPC_URL 相关说明
- 更新所有 Docker Compose 配置文件,移除 POLYGON_RPC_URL 环境变量
- 更新所有部署脚本,移除 POLYGON_RPC_URL 环境变量定义
## 🔧 技术改进
- 重构链上 WebSocket 服务,提取公共代码到 `OnChainWsUtils`
- 创建统一的 WebSocket 连接管理服务 `UnifiedOnChainWsService`
- 添加链上查询市场结算结果的功能(`BlockchainService.getCondition`
- 添加 ABI 编码/解码工具方法(`EthereumUtils.decodeConditionResult`
- 优化代码结构,减少代码重复,提高可维护性
---
# v1.1.0
## 🚀 主要功能
### 🔗 链上 WebSocket 实时监听
- 实现通过 Polygon RPC `eth_subscribe` 实时监听链上交易
- 支持监听 USDC Transfer 和 ERC1155 Transfer 事件
- 实现并行监控策略:链上 WebSocket 和轮询同时运行,哪个数据先返回用哪个
- 支持通过 `eth_unsubscribe` 取消单个 Leader 的订阅,无需重新连接
- 优化 WebSocket 连接管理:只创建一个连接,没有跟单配置时自动取消
- 跟单配置生效/失效时及时更新 WebSocket 订阅
- 使用 Gson 替换所有 JSON 解析,提高解析稳定性
- 添加 Mutex 保证线程安全,防止并发处理导致的数据重复
### 📊 RPC 节点管理
- 实现 RPC 节点管理功能,支持添加、编辑、删除自定义 RPC 节点
- 支持 RPC 节点启用/禁用功能,禁用的节点会被自动忽略
- 前端添加启用/禁用开关,支持实时切换节点状态
- 健康检查只检查启用的节点,提高检查效率
- 节点选择时自动过滤禁用的节点
### 💰 卖出订单价格轮询更新
- 添加 `price_updated` 字段到 `sell_match_record` 表,用于标记价格是否已更新
- 创建 `OrderStatusUpdateService` 定时任务服务,每 5 秒轮询一次:
- 更新卖出订单的实际成交价(通过 orderId 查询订单详情)
- 清理已删除账户的订单记录
- 支持加权平均价格计算,处理部分成交的订单
- 添加 orderId 格式验证:非 0x 开头的直接标记为已更新,0x 开头的等待定时任务更新
- 下单完成后不再立即查询价格,直接保存,等待定时任务更新
## 🐛 Bug 修复
### 修复跟单卖出订单的 API 凭证解密问题
- 修复 `processSellTrade` 中 API 凭证未解密的问题,与 `processBuyTrade` 保持一致
- 确保卖出订单能够正常使用 API 凭证进行认证
### 修复 SELL 订单精度问题
- 修复 SELL 订单的 `makerAmount``takerAmount` 精度问题:
- `makerAmount` (shares) 最多 2 位小数(符合 API 要求)
- `takerAmount` (USDC) 最多 4 位小数(符合 API 要求)
- 确保订单能够正常提交到 Polymarket API
## 📚 文档更新
- 添加 Docker 版本更新说明(中英文)
- 添加链上 WebSocket 监听策略文档
- 添加跟单逻辑总结文档
- 更新部署文档,包含详细的版本更新步骤
## 🔧 技术改进
- 使用 Gson 替换 ObjectMapper,提高 JSON 解析稳定性
- `JsonRpcResponse.result` 使用 `JsonElement` 类型,支持灵活的 JSON 结构
- 优化 WebSocket 连接管理,减少不必要的连接
- 添加线程安全机制,使用 Kotlin Coroutines Mutex
- 启用 Spring 定时任务功能(`@EnableScheduling`
## 📦 数据库变更
- 新增 `price_updated` 字段到 `sell_match_record` 表(Migration: V13
## 🔗 相关链接
- **GitHub Release**: https://github.com/WrBug/PolyHermes/releases/tag/v1.1.1
- **完整更新日志**: https://github.com/WrBug/PolyHermes/compare/v1.1.0...v1.1.1
- **Docker Hub**: https://hub.docker.com/r/wrbug/polyhermes
## 📊 统计信息
- **文件变更**: 32 个文件
- **代码变更**: +1872 行 / -1503 行
- **主要提交**: 7 个提交
## ⚠️ 重要提醒
**请务必使用官方 Docker 镜像源,避免财产损失!**
### ✅ 官方 Docker Hub 镜像
**官方镜像地址**`wrbug/polyhermes`
```bash
# ✅ 正确:使用官方镜像
docker pull wrbug/polyhermes:v1.1.1
# ❌ 错误:不要使用其他来源的镜像
# 任何非官方来源的镜像都可能包含恶意代码,导致您的私钥和资产被盗
```
### 🔗 官方渠道
请通过以下**唯一官方渠道**获取 PolyHermes
* **GitHub 仓库**https://github.com/WrBug/PolyHermes
* **Twitter**@polyhermes
* **Telegram 群组**:加入群组
---
**⭐ 如果这个项目对您有帮助,请给个 Star 支持一下!**
-55
View File
@@ -1,55 +0,0 @@
# v1.0.1
## 🚀 性能优化
### 📊 订单簿请求优化
* 优化订单簿获取逻辑,仅在需要时请求,避免不必要的 API 调用
* 当未配置需要订单簿的过滤条件(价差、订单深度)时,跳过订单簿获取
* 订单簿只请求一次,在过滤检查中复用,提高性能
### 💰 卖出价格计算优化
* 卖出改为市价卖出:优先使用订单簿的 bestBid(最高买入价)
* 如果 bestBid 获取失败,自动降级使用 Leader 的交易价格
* 卖出价格固定按 90% 计算,不再使用价格容忍度配置
* 提高卖出订单的成交率
### ⚙️ 价格容忍度默认值
* 如果价格容忍度配置为 0,自动使用默认值 5%
* 确保买入订单能够正常应用价格调整,提高成交率
## 🔧 代码优化
### 🎯 使用枚举优化过滤逻辑
* 新增 `FilterResult` 数据类和 `FilterStatus` 枚举
* 使用类型安全的枚举替代字符串判断,提高代码可维护性
* 移除无用字段 `isBuyOrder` 参数
* 优化 `checkFilters` 方法返回值,使用数据类封装结果
### 📝 代码结构优化
* 减少 if-else 嵌套,使用 Kotlin 链式调用和空安全操作符
* 优化卖出价格计算逻辑,使用 `runCatching``?.let` 简化代码
* 提取公共方法 `calculateFallbackSellPrice`,提高代码复用性
## 🗑️ 功能移除
### 移除最小订单簿深度功能
* 移除 `minOrderbookDepth`(最小订单簿深度)配置项
* 简化过滤逻辑,只保留 `minOrderDepth`(最小订单深度)
* 更新前端界面,移除相关配置项和提示文案
### 优化最小订单深度逻辑
* 修改 `minOrderDepth` 检查逻辑,检查所有方向(买盘+卖盘)的总深度
* 不再区分买卖方向,提供更全面的市场流动性评估
## 🐛 Bug 修复
* 修复盈亏和持仓价值计算逻辑未实现的问题
* 修复过滤检查中订单簿可能重复请求的问题
## 📚 文档更新
* 更新前端多语言文案,优化提示信息
* 移除最小订单簿深度相关的文档说明
**Full Changelog**: https://github.com/WrBug/polymarket-bot/compare/v1.0.0...v1.0.1
-3
View File
@@ -154,9 +154,6 @@ SPRING_PROFILES_ACTIVE=prod
# 服务器端口
SERVER_PORT=8000
# Polygon RPC
POLYGON_RPC_URL=https://polygon-rpc.com
# JWT 密钥(已自动生成随机值,生产环境建议修改)
JWT_SECRET=${JWT_SECRET}
-1
View File
@@ -14,7 +14,6 @@ services:
- DB_USERNAME=${DB_USERNAME:-root}
- DB_PASSWORD=${DB_PASSWORD:-}
- SERVER_PORT=8000
- POLYGON_RPC_URL=${POLYGON_RPC_URL:-https://polygon-rpc.com}
- JWT_SECRET=${JWT_SECRET:-change-me-in-production}
- ADMIN_RESET_PASSWORD_KEY=${ADMIN_RESET_PASSWORD_KEY:-change-me-in-production}
depends_on:
@@ -2,8 +2,10 @@ package com.wrbug.polymarketbot
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.scheduling.annotation.EnableScheduling
@SpringBootApplication
@EnableScheduling
class PolymarketBotApplication
fun main(args: Array<String>) {
@@ -1,5 +1,6 @@
package com.wrbug.polymarketbot.api
import com.google.gson.JsonElement
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.POST
@@ -29,10 +30,11 @@ data class JsonRpcRequest(
/**
* JSON-RPC 响应
* 使用 JsonElement 类型处理 result 字段,可以灵活处理字符串、对象、数组等类型
*/
data class JsonRpcResponse(
val jsonrpc: String? = null,
val result: String? = null,
val result: JsonElement? = null, // 使用 JsonElement 类型,可以处理任意 JSON 类型
val error: JsonRpcError? = null,
val id: Int? = null
)
@@ -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?
)
@@ -13,19 +13,56 @@ 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("include_tag") includeTag: Boolean? = null
@Query("clob_token_ids") clobTokenIds: List<String>? = 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>>
}
/**
* 体育元数据响应
* 文档: https://docs.polymarket.com/api-reference/sports/get-sports-metadata-information
*/
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 // 系列标识符
)
/**
* 市场响应(根据 Gamma API 文档)
*/
@@ -51,6 +88,23 @@ data class MarketResponse(
val liquidityNum: Double? = null,
val lastTradePrice: Double? = null,
val bestBid: Double? = null,
val bestAsk: Double? = null
val bestAsk: Double? = null,
// 以下字段可能存在于响应中,但不在标准文档中
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
)
@@ -0,0 +1,26 @@
package com.wrbug.polymarketbot.config
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
/**
* Gson 配置类
* 统一配置 Gson 实例,使用 lenient 模式允许解析格式不严格的 JSON
*/
@Configuration
class GsonConfig {
/**
* 创建 Gson Bean
* 使用 lenient 模式,允许解析格式不严格的 JSON
*/
@Bean
fun gson(): Gson {
return GsonBuilder()
.setLenient()
.create()
}
}
@@ -36,20 +36,19 @@ class JwtAuthenticationInterceptor(
handler: Any
): Boolean {
val path = request.requestURI
val method = request.method
// 只拦截POST请求
if (method != "POST") {
// 只拦截 /api/** 路径
if (!path.startsWith("/api/")) {
return true
}
// 排除不需要鉴权的路径
if (excludePaths.contains(path)) {
return true
}
// 只拦截 /api/** 路径
if (!path.startsWith("/api/")) {
// 允许 OPTIONS 请求(CORS 预检请求)
if (request.method == "OPTIONS") {
return true
}
@@ -1,5 +1,6 @@
package com.wrbug.polymarketbot.config
import com.google.gson.Gson
import com.wrbug.polymarketbot.api.PolymarketClobApi
import com.wrbug.polymarketbot.util.createClient
import org.springframework.beans.factory.annotation.Value
@@ -18,7 +19,9 @@ import retrofit2.converter.gson.GsonConverterFactory
* - 账户 API Key 在调用时动态设置,不在此处配置
*/
@Configuration
class RetrofitConfig {
class RetrofitConfig(
private val gson: Gson
) {
@Value("\${polymarket.clob.base-url}")
private lateinit var clobBaseUrl: String
@@ -37,7 +40,7 @@ class RetrofitConfig {
return Retrofit.Builder()
.baseUrl(clobBaseUrl)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(PolymarketClobApi::class.java)
}
@@ -21,10 +21,6 @@ class WebMvcConfig(
// 再注册JWT认证拦截器
registry.addInterceptor(jwtAuthenticationInterceptor)
.addPathPatterns("/api/**")
registry.addInterceptor(jwtAuthenticationInterceptor)
.addPathPatterns("/api/**")
registry.addInterceptor(jwtAuthenticationInterceptor)
.addPathPatterns("/api/**")
}
}
@@ -1,6 +1,7 @@
package com.wrbug.polymarketbot.config
import com.wrbug.polymarketbot.repository.UserRepository
import com.wrbug.polymarketbot.service.auth.WebSocketTicketService
import com.wrbug.polymarketbot.util.JwtUtils
import org.slf4j.LoggerFactory
import org.springframework.http.server.ServerHttpRequest
@@ -11,12 +12,13 @@ import org.springframework.web.socket.server.HandshakeInterceptor
/**
* WebSocket 握手拦截器
* 用于验证 JWT token
* 优先使用短期票据验证,其次使用 JWT token
*/
@Component
class WebSocketAuthInterceptor(
private val jwtUtils: JwtUtils,
private val userRepository: UserRepository
private val userRepository: UserRepository,
private val webSocketTicketService: WebSocketTicketService
) : HandshakeInterceptor {
private val logger = LoggerFactory.getLogger(WebSocketAuthInterceptor::class.java)
@@ -27,22 +29,36 @@ class WebSocketAuthInterceptor(
wsHandler: WebSocketHandler,
attributes: MutableMap<String, Any>
): Boolean {
// 从查询参数或请求头获取 token
val token = getTokenFromRequest(request)
if (token == null) {
logger.warn("WebSocket 连接缺少认证令牌: ${request.uri}")
// 优先使用票据验证(推荐方式,不暴露 JWT)
val ticket = getTicketFromRequest(request)
if (ticket != null) {
val username = webSocketTicketService.validateAndConsumeTicket(ticket)
if (username != null) {
attributes["username"] = username
logger.debug("WebSocket 连接票据认证成功: username=$username")
return true
}
logger.warn("WebSocket 连接票据验证失败(可能已过期或已使用)")
response.setStatusCode(org.springframework.http.HttpStatus.UNAUTHORIZED)
return false
}
// 兼容旧方式:使用 JWT token(不推荐,但保持向后兼容)
val token = getTokenFromRequest(request)
if (token == null) {
logger.warn("WebSocket 连接缺少认证令牌: ${request.uri.path}")
response.setStatusCode(org.springframework.http.HttpStatus.UNAUTHORIZED)
return false
}
// 验证 token
if (!jwtUtils.validateToken(token)) {
logger.warn("WebSocket 连接 token 验证失败: ${request.uri}")
logger.warn("WebSocket 连接 token 验证失败")
response.setStatusCode(org.springframework.http.HttpStatus.UNAUTHORIZED)
return false
}
// 验证tokenVersion(检查token是否因密码修改而失效)
val username = jwtUtils.getUsernameFromToken(token)
if (username != null) {
@@ -50,21 +66,21 @@ class WebSocketAuthInterceptor(
if (user != null) {
val tokenVersion = jwtUtils.getTokenVersionFromToken(token)
if (tokenVersion == null || tokenVersion != user.tokenVersion) {
logger.warn("WebSocket 连接 token 版本不匹配,token已失效: username=$username, tokenVersion=$tokenVersion, userTokenVersion=${user.tokenVersion}, uri=${request.uri}")
logger.warn("WebSocket 连接 token 版本不匹配,token已失效: username=$username")
response.setStatusCode(org.springframework.http.HttpStatus.UNAUTHORIZED)
return false
}
}
// 获取用户名并存入 attributes,供后续使用
attributes["username"] = username
logger.debug("WebSocket 连接认证成功: username=$username, uri=${request.uri}")
logger.debug("WebSocket 连接 JWT 认证成功: username=$username")
} else {
logger.warn("WebSocket 连接无法获取用户名: ${request.uri}")
logger.warn("WebSocket 连接无法获取用户名")
response.setStatusCode(org.springframework.http.HttpStatus.UNAUTHORIZED)
return false
}
return true
}
@@ -78,7 +94,22 @@ class WebSocketAuthInterceptor(
}
/**
* 从请求中获取 token
* 从请求中获取票据
*/
private fun getTicketFromRequest(request: ServerHttpRequest): String? {
val queryParams = request.uri.query ?: return null
val params = queryParams.split("&")
for (param in params) {
val parts = param.split("=", limit = 2)
if (parts.size == 2 && parts[0] == "ticket") {
return parts[1]
}
}
return null
}
/**
* 从请求中获取 token(兼容旧方式)
* 支持从查询参数 token 或请求头 Authorization 获取
*/
private fun getTokenFromRequest(request: ServerHttpRequest): String? {
@@ -93,13 +124,13 @@ class WebSocketAuthInterceptor(
}
}
}
// 从请求头获取
val authHeader = request.headers.getFirst("Authorization")
if (authHeader != null && authHeader.startsWith("Bearer ")) {
return authHeader.substring(7)
}
return null
}
}
@@ -2,6 +2,7 @@ package com.wrbug.polymarketbot.config
import com.wrbug.polymarketbot.websocket.PolymarketWebSocketHandler
import com.wrbug.polymarketbot.websocket.UnifiedWebSocketHandler
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Configuration
import org.springframework.web.socket.config.annotation.EnableWebSocket
import org.springframework.web.socket.config.annotation.WebSocketConfigurer
@@ -16,21 +17,46 @@ import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry
class WebSocketConfig(
private val polymarketWebSocketHandler: PolymarketWebSocketHandler,
private val unifiedWebSocketHandler: UnifiedWebSocketHandler,
private val webSocketAuthInterceptor: WebSocketAuthInterceptor
private val webSocketAuthInterceptor: WebSocketAuthInterceptor,
@Value("\${websocket.allowed-origins:}") private val allowedOriginsConfig: String
) : WebSocketConfigurer {
/**
* 获取允许的 WebSocket 来源
* 如果配置了 WEBSOCKET_ALLOWED_ORIGINS 环境变量,使用配置的域名
* 否则使用 setAllowedOriginPatterns 允许同源访问
*/
private fun getAllowedOrigins(): Array<String> {
return if (allowedOriginsConfig.isNotBlank()) {
allowedOriginsConfig.split(",").map { it.trim() }.toTypedArray()
} else {
emptyArray()
}
}
override fun registerWebSocketHandlers(registry: WebSocketHandlerRegistry) {
val origins = getAllowedOrigins()
// Polymarket RTDS 转发端点(转发外部 Polymarket 实时数据流)
// 注意:此端点不需要鉴权,因为它只是转发外部数据
registry.addHandler(polymarketWebSocketHandler, "/ws/polymarket")
.setAllowedOrigins("*") // 生产环境应该配置具体的域名
val polymarketHandler = registry.addHandler(polymarketWebSocketHandler, "/ws/polymarket")
if (origins.isNotEmpty()) {
polymarketHandler.setAllowedOrigins(*origins)
} else {
// 使用 setAllowedOriginPatterns 替代 setAllowedOrigins("*"),更安全
polymarketHandler.setAllowedOriginPatterns("*")
}
// 统一 WebSocket 端点(所有推送服务统一使用此路径,通过 channel 区分)
// 支持的频道:position(仓位推送)、order(订单推送,待实现)等
// 支持的频道:position(仓位推送)、order(订单推送)等
// 需要 JWT 鉴权
registry.addHandler(unifiedWebSocketHandler, "/ws")
.addInterceptors(webSocketAuthInterceptor) // 添加鉴权拦截器
.setAllowedOrigins("*") // 生产环境应该配置具体的域名
val unifiedHandler = registry.addHandler(unifiedWebSocketHandler, "/ws")
.addInterceptors(webSocketAuthInterceptor)
if (origins.isNotEmpty()) {
unifiedHandler.setAllowedOrigins(*origins)
} else {
unifiedHandler.setAllowedOriginPatterns("*")
}
}
}
@@ -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))
}
}
/**
* 删除账户
*/
@@ -3,6 +3,7 @@ package com.wrbug.polymarketbot.controller.auth
import com.wrbug.polymarketbot.dto.*
import com.wrbug.polymarketbot.enums.ErrorCode
import com.wrbug.polymarketbot.service.auth.AuthService
import com.wrbug.polymarketbot.service.auth.WebSocketTicketService
import jakarta.servlet.http.HttpServletRequest
import org.slf4j.LoggerFactory
import org.springframework.context.MessageSource
@@ -16,7 +17,8 @@ import org.springframework.web.bind.annotation.*
@RequestMapping("/api/auth")
class AuthController(
private val authService: AuthService,
private val messageSource: MessageSource
private val messageSource: MessageSource,
private val webSocketTicketService: WebSocketTicketService
) {
private val logger = LoggerFactory.getLogger(AuthController::class.java)
@@ -25,7 +27,10 @@ class AuthController(
* 登录接口
*/
@PostMapping("/login")
fun login(@RequestBody request: LoginRequest): ResponseEntity<ApiResponse<LoginResponse>> {
fun login(
@RequestBody request: LoginRequest,
httpRequest: HttpServletRequest
): ResponseEntity<ApiResponse<LoginResponse>> {
return try {
if (request.username.isBlank()) {
return ResponseEntity.ok(ApiResponse.error(ErrorCode.PARAM_EMPTY, "用户名不能为空", messageSource))
@@ -33,15 +38,19 @@ class AuthController(
if (request.password.isBlank()) {
return ResponseEntity.ok(ApiResponse.error(ErrorCode.PARAM_EMPTY, "密码不能为空", messageSource))
}
val result = authService.login(request.username, request.password)
val ipAddress = getClientIpAddress(httpRequest)
val result = authService.login(request.username, request.password, ipAddress)
result.fold(
onSuccess = { loginResponse ->
ResponseEntity.ok(ApiResponse.success(loginResponse))
},
onFailure = { e ->
logger.error("登录失败: ${e.message}", e)
when (e) {
is IllegalStateException -> {
// 限速或锁定错误
ResponseEntity.ok(ApiResponse.error(ErrorCode.AUTH_ERROR, e.message ?: "登录失败", messageSource))
}
is IllegalArgumentException -> {
if (e.message == ErrorCode.AUTH_USERNAME_OR_PASSWORD_ERROR.message) {
ResponseEntity.ok(ApiResponse.error(ErrorCode.AUTH_USERNAME_OR_PASSWORD_ERROR, messageSource = messageSource))
@@ -49,15 +58,36 @@ class AuthController(
ResponseEntity.ok(ApiResponse.error(ErrorCode.PARAM_ERROR, e.message, messageSource))
}
}
else -> ResponseEntity.ok(ApiResponse.error(ErrorCode.AUTH_ERROR, "登录失败: ${e.message}", messageSource))
else -> ResponseEntity.ok(ApiResponse.error(ErrorCode.AUTH_ERROR, "登录失败", messageSource))
}
}
)
} catch (e: Exception) {
logger.error("登录异常: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.AUTH_ERROR, "登录失败: ${e.message}", messageSource))
ResponseEntity.ok(ApiResponse.error(ErrorCode.AUTH_ERROR, "登录失败", messageSource))
}
}
/**
* 获取客户端IP地址
*/
private fun getClientIpAddress(request: HttpServletRequest): String {
var ip = request.getHeader("X-Forwarded-For")
if (ip.isNullOrBlank() || "unknown".equals(ip, ignoreCase = true)) {
ip = request.getHeader("X-Real-IP")
}
if (ip.isNullOrBlank() || "unknown".equals(ip, ignoreCase = true)) {
ip = request.getHeader("Proxy-Client-IP")
}
if (ip.isNullOrBlank() || "unknown".equals(ip, ignoreCase = true)) {
ip = request.remoteAddr
}
// 处理多个IP的情况
if (ip.contains(",")) {
ip = ip.split(",")[0].trim()
}
return ip
}
/**
* 重置密码接口
@@ -132,5 +162,27 @@ class AuthController(
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, "检查首次使用失败: ${e.message}", messageSource))
}
}
/**
* 获取 WebSocket 连接票据
* 返回一个短期有效(30秒)的一次性票据,用于 WebSocket 连接认证
* 避免在 WebSocket URL 中暴露 JWT
*/
@PostMapping("/ws-ticket")
fun getWebSocketTicket(httpRequest: HttpServletRequest): ResponseEntity<ApiResponse<WebSocketTicketResponse>> {
return try {
// 从请求属性中获取用户名(由 JWT 拦截器设置)
val username = httpRequest.getAttribute("username") as? String
if (username == null) {
return ResponseEntity.ok(ApiResponse.error(ErrorCode.AUTH_ERROR, "未认证", messageSource))
}
val ticket = webSocketTicketService.generateTicket(username)
ResponseEntity.ok(ApiResponse.success(WebSocketTicketResponse(ticket = ticket)))
} catch (e: Exception) {
logger.error("获取 WebSocket 票据异常: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, "获取票据失败", messageSource))
}
}
}
@@ -4,8 +4,10 @@ import com.wrbug.polymarketbot.api.LatestPriceResponse
import com.wrbug.polymarketbot.dto.*
import com.wrbug.polymarketbot.enums.ErrorCode
import com.wrbug.polymarketbot.service.accounts.AccountService
import com.wrbug.polymarketbot.service.common.MarketPriceService
import com.wrbug.polymarketbot.service.common.PolymarketClobService
import kotlinx.coroutines.runBlocking
import java.math.BigDecimal
import org.slf4j.LoggerFactory
import org.springframework.context.MessageSource
import org.springframework.http.ResponseEntity
@@ -20,14 +22,16 @@ import org.springframework.web.bind.annotation.*
class MarketController(
private val accountService: AccountService,
private val clobService: PolymarketClobService,
private val marketPriceService: MarketPriceService,
private val messageSource: MessageSource
) {
private val logger = LoggerFactory.getLogger(MarketController::class.java)
/**
* 获取市场价格(通过 Gamma API
* 使用 Gamma API 获取价格信息,因为 Gamma API 支持 condition_ids 参数
* 获取市场价格
* 使用 MarketPriceService 获取当前市场价格(支持多数据源降级)
* 返回当前价格,前端接收后自行填充到 bestBid 字段
*/
@PostMapping("/price")
fun getMarketPrice(@RequestBody request: MarketPriceRequest): ResponseEntity<ApiResponse<MarketPriceResponse>> {
@@ -36,16 +40,16 @@ class MarketController(
return ResponseEntity.ok(ApiResponse.error(ErrorCode.PARAM_MARKET_ID_EMPTY, messageSource = messageSource))
}
val result = runBlocking { accountService.getMarketPrice(request.marketId, request.outcomeIndex) }
result.fold(
onSuccess = { response ->
ResponseEntity.ok(ApiResponse.success(response))
},
onFailure = { e ->
logger.error("获取市场价格失败: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_MARKET_PRICE_FETCH_FAILED, e.message, messageSource))
}
val outcomeIndex = request.outcomeIndex ?: 0
val price = runBlocking {
marketPriceService.getCurrentMarketPrice(request.marketId, outcomeIndex)
}
val response = MarketPriceResponse(
marketId = request.marketId,
currentPrice = price.toString()
)
ResponseEntity.ok(ApiResponse.success(response))
} catch (e: Exception) {
logger.error("获取市场价格异常: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_MARKET_PRICE_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?
)
@@ -0,0 +1,366 @@
package com.wrbug.polymarketbot.controller.system
import com.wrbug.polymarketbot.dto.ApiResponse
import com.wrbug.polymarketbot.entity.RpcNodeConfig
import com.wrbug.polymarketbot.enums.ErrorCode
import com.wrbug.polymarketbot.service.system.AddRpcNodeRequest
import com.wrbug.polymarketbot.service.system.NodeCheckResult
import com.wrbug.polymarketbot.service.system.RpcNodeService
import com.wrbug.polymarketbot.service.system.UpdateRpcNodeRequest
import org.slf4j.LoggerFactory
import org.springframework.context.MessageSource
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
/**
* RPC 节点管理控制器
*/
@RestController
@RequestMapping("/api/system/rpc-nodes")
class RpcNodeController(
private val rpcNodeService: RpcNodeService,
private val messageSource: MessageSource
) {
private val logger = LoggerFactory.getLogger(RpcNodeController::class.java)
/**
* 获取所有节点列表
*/
@PostMapping("/list")
fun getAllNodes(@RequestBody request: Map<String, Any>?): ResponseEntity<ApiResponse<List<RpcNodeConfigDto>>> {
return try {
val nodes = rpcNodeService.getAllNodes()
val dtos = nodes.map { it.toDto() }
ResponseEntity.ok(ApiResponse.success(dtos))
} catch (e: Exception) {
logger.error("获取 RPC 节点列表失败: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(
ErrorCode.SERVER_ERROR,
customMsg = "获取节点列表失败:${e.message}",
messageSource = messageSource
))
}
}
/**
* 添加节点
*/
@PostMapping("/add")
fun addNode(@RequestBody request: AddRpcNodeRequest): ResponseEntity<ApiResponse<RpcNodeConfigDto>> {
return try {
if (request.providerType.isBlank()) {
return ResponseEntity.ok(ApiResponse.paramError("服务商类型不能为空"))
}
if (request.name.isBlank()) {
return ResponseEntity.ok(ApiResponse.paramError("节点名称不能为空"))
}
val result = rpcNodeService.addNode(request)
result.fold(
onSuccess = { node ->
ResponseEntity.ok(ApiResponse.success(node.toDto()))
},
onFailure = { e ->
logger.error("添加 RPC 节点失败: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(
ErrorCode.SERVER_ERROR,
customMsg = "添加节点失败:${e.message}",
messageSource = messageSource
))
}
)
} catch (e: Exception) {
logger.error("添加 RPC 节点异常: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(
ErrorCode.SERVER_ERROR,
customMsg = "添加节点失败:${e.message}",
messageSource = messageSource
))
}
}
/**
* 更新节点
*/
@PostMapping("/update")
fun updateNode(@RequestBody request: UpdateRpcNodeRequest): ResponseEntity<ApiResponse<RpcNodeConfigDto>> {
return try {
val result = rpcNodeService.updateNode(request)
result.fold(
onSuccess = { node ->
ResponseEntity.ok(ApiResponse.success(node.toDto()))
},
onFailure = { e ->
logger.error("更新 RPC 节点失败: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(
ErrorCode.SERVER_ERROR,
customMsg = "更新节点失败:${e.message}",
messageSource = messageSource
))
}
)
} catch (e: Exception) {
logger.error("更新 RPC 节点异常: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(
ErrorCode.SERVER_ERROR,
customMsg = "更新节点失败:${e.message}",
messageSource = messageSource
))
}
}
/**
* 删除节点
*/
@PostMapping("/delete")
fun deleteNode(@RequestBody request: DeleteRpcNodeRequest): ResponseEntity<ApiResponse<Unit>> {
return try {
val result = rpcNodeService.deleteNode(request.id)
result.fold(
onSuccess = {
ResponseEntity.ok(ApiResponse.success(Unit))
},
onFailure = { e ->
logger.error("删除 RPC 节点失败: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(
ErrorCode.SERVER_ERROR,
customMsg = "删除节点失败:${e.message}",
messageSource = messageSource
))
}
)
} catch (e: Exception) {
logger.error("删除 RPC 节点异常: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(
ErrorCode.SERVER_ERROR,
customMsg = "删除节点失败:${e.message}",
messageSource = messageSource
))
}
}
/**
* 更新节点优先级
*/
@PostMapping("/update-priority")
fun updatePriority(@RequestBody request: UpdatePriorityRequest): ResponseEntity<ApiResponse<Unit>> {
return try {
val result = rpcNodeService.updatePriority(request.id, request.priority)
result.fold(
onSuccess = {
ResponseEntity.ok(ApiResponse.success(Unit))
},
onFailure = { e ->
logger.error("更新节点优先级失败: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(
ErrorCode.SERVER_ERROR,
customMsg = "更新优先级失败:${e.message}",
messageSource = messageSource
))
}
)
} catch (e: Exception) {
logger.error("更新节点优先级异常: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(
ErrorCode.SERVER_ERROR,
customMsg = "更新优先级失败:${e.message}",
messageSource = messageSource
))
}
}
/**
* 检查节点健康状态
*/
@PostMapping("/check-health")
fun checkHealth(@RequestBody request: CheckHealthRequest): ResponseEntity<ApiResponse<Any>> {
return try {
if (request.id != null) {
// 检查单个节点
val result = rpcNodeService.checkNodeHealth(request.id)
result.fold(
onSuccess = { checkResult ->
ResponseEntity.ok(ApiResponse.success(checkResult.toDto()))
},
onFailure = { e ->
logger.error("检查节点健康状态失败: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(
ErrorCode.SERVER_ERROR,
customMsg = "检查节点失败:${e.message}",
messageSource = messageSource
))
}
)
} else {
// 批量检查所有节点
val result = rpcNodeService.checkAllNodesHealth()
result.fold(
onSuccess = { checkResults ->
val dtos = checkResults.mapValues { it.value.toDto() }
ResponseEntity.ok(ApiResponse.success(dtos))
},
onFailure = { e ->
logger.error("批量检查节点健康状态失败: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(
ErrorCode.SERVER_ERROR,
customMsg = "批量检查节点失败:${e.message}",
messageSource = messageSource
))
}
)
}
} catch (e: Exception) {
logger.error("检查节点健康状态异常: ${e.message}", e)
ResponseEntity.ok(ApiResponse.error(
ErrorCode.SERVER_ERROR,
customMsg = "检查节点失败:${e.message}",
messageSource = messageSource
))
}
}
/**
* 校验节点(添加前)
*/
@PostMapping("/validate")
fun validateNode(@RequestBody request: AddRpcNodeRequest): ResponseEntity<ApiResponse<ValidateNodeResponse>> {
return try {
if (request.providerType.isBlank()) {
return ResponseEntity.ok(ApiResponse.paramError("服务商类型不能为空"))
}
if (request.name.isBlank()) {
return ResponseEntity.ok(ApiResponse.paramError("节点名称不能为空"))
}
// 临时创建节点配置以进行验证(不保存到数据库)
// 这里直接复用 addNode 的部分逻辑,但只进行校验
val result = rpcNodeService.addNode(request)
result.fold(
onSuccess = { node ->
// 添加成功后立即删除(这只是为了校验)
rpcNodeService.deleteNode(node.id!!)
ResponseEntity.ok(ApiResponse.success(ValidateNodeResponse(
valid = true,
message = "节点可用",
responseTimeMs = node.responseTimeMs
)))
},
onFailure = { e ->
ResponseEntity.ok(ApiResponse.success(ValidateNodeResponse(
valid = false,
message = e.message ?: "节点验证失败",
responseTimeMs = null
)))
}
)
} catch (e: Exception) {
logger.error("验证节点异常: ${e.message}", e)
ResponseEntity.ok(ApiResponse.success(ValidateNodeResponse(
valid = false,
message = e.message ?: "节点验证失败",
responseTimeMs = null
)))
}
}
}
/**
* RPC 节点配置 DTO
*/
data class RpcNodeConfigDto(
val id: Long?,
val providerType: String,
val name: String,
val httpUrl: String,
val wsUrl: String?,
val apiKeyMasked: String?, // 脱敏后的 API Key
val enabled: Boolean,
val priority: Int,
val lastCheckTime: Long?,
val lastCheckStatus: String?,
val responseTimeMs: Int?,
val createdAt: Long,
val updatedAt: Long
)
/**
* 节点检查结果 DTO
*/
data class NodeCheckResultDto(
val status: String,
val message: String,
val checkTime: Long,
val responseTimeMs: Int?,
val blockNumber: String?
)
/**
* 验证节点响应
*/
data class ValidateNodeResponse(
val valid: Boolean,
val message: String,
val responseTimeMs: Int?
)
/**
* 删除节点请求
*/
data class DeleteRpcNodeRequest(
val id: Long
)
/**
* 更新优先级请求
*/
data class UpdatePriorityRequest(
val id: Long,
val priority: Int
)
/**
* 检查健康状态请求
*/
data class CheckHealthRequest(
val id: Long? = null // 如果为 null,则检查所有节点
)
/**
* 扩展函数:将 RpcNodeConfig 转换为 DTO
*/
private fun RpcNodeConfig.toDto(): RpcNodeConfigDto {
return RpcNodeConfigDto(
id = id,
providerType = providerType,
name = name,
httpUrl = httpUrl,
wsUrl = wsUrl,
apiKeyMasked = apiKey?.let { "***" }, // 脱敏显示
enabled = enabled,
priority = priority,
lastCheckTime = lastCheckTime,
lastCheckStatus = lastCheckStatus,
responseTimeMs = responseTimeMs,
createdAt = createdAt,
updatedAt = updatedAt
)
}
/**
* 扩展函数:将 NodeCheckResult 转换为 DTO
*/
private fun NodeCheckResult.toDto(): NodeCheckResultDto {
return NodeCheckResultDto(
status = status.name,
message = message,
checkTime = checkTime,
responseTimeMs = responseTimeMs,
blockNumber = blockNumber
)
}
@@ -7,7 +7,8 @@ data class AccountImportRequest(
val privateKey: String, // 私钥(前端加密后传输)
val walletAddress: String, // 钱包地址(前端从私钥推导,用于验证)
val accountName: String? = null,
val isEnabled: Boolean = true // 是否启用(用于订单推送等功能的开关)
val isEnabled: Boolean = true, // 是否启用(用于订单推送等功能的开关)
val walletType: String = "magic" // 钱包类型:magic(邮箱/OAuth登录)或 safeMetaMask浏览器钱包)
)
/**
@@ -72,6 +73,7 @@ data class AccountDto(
val proxyAddress: String, // Polymarket 代理钱包地址
val accountName: String?,
val isEnabled: Boolean, // 是否启用(用于订单推送等功能的开关)
val walletType: String = "magic", // 钱包类型:magic(邮箱/OAuth登录)或 safeMetaMask浏览器钱包)
val apiKeyConfigured: Boolean, // API Key 是否已配置(不返回实际 Key)
val apiSecretConfigured: Boolean, // API Secret 是否已配置
val apiPassphraseConfigured: Boolean, // API Passphrase 是否已配置
@@ -195,14 +197,11 @@ data class LatestPriceRequest(
)
/**
* 市场价格响应
* 市场当前价格响应
*/
data class MarketPriceResponse(
val marketId: String,
val lastPrice: String?, // 最新成交价
val bestBid: String?, // 最优买价(用于卖出参考)
val bestAsk: String?, // 最优卖价(用于买入参考)
val midpoint: String? // 中间价
val currentPrice: String // 当前价格(通过 MarketPriceService 获取,支持多数据源降级)
)
/**
@@ -14,3 +14,10 @@ data class CheckFirstUseResponse(
val isFirstUse: Boolean
)
/**
* WebSocket 票据响应
*/
data class WebSocketTicketResponse(
val ticket: String
)
@@ -34,6 +34,9 @@ data class CopyTradingCreateRequest(
val maxSpread: String? = null, // 最大价差(绝对价格),NULL表示不启用
val minPrice: String? = null, // 最低价格(可选),NULL表示不限制最低价
val maxPrice: String? = null, // 最高价格(可选),NULL表示不限制最高价
// 最大仓位配置
val maxPositionValue: String? = null, // 最大仓位金额(USDC),NULL表示不启用
val maxPositionCount: Int? = null, // 最大仓位数量,NULL表示不启用
// 新增配置字段
val configName: String? = null, // 配置名(可选)
val pushFailedOrders: Boolean? = null // 推送失败订单(可选)
@@ -65,6 +68,9 @@ data class CopyTradingUpdateRequest(
val maxSpread: String? = null,
val minPrice: String? = null, // 最低价格(可选),NULL表示不限制最低价
val maxPrice: String? = null, // 最高价格(可选),NULL表示不限制最高价
// 最大仓位配置
val maxPositionValue: String? = null, // 最大仓位金额(USDC),NULL表示不启用
val maxPositionCount: Int? = null, // 最大仓位数量,NULL表示不启用
// 新增配置字段
val configName: String? = null, // 配置名(可选,但提供时必须非空)
val pushFailedOrders: Boolean? = null // 推送失败订单(可选)
@@ -133,6 +139,9 @@ data class CopyTradingDto(
val maxSpread: String?,
val minPrice: String?, // 最低价格(可选),NULL表示不限制最低价
val maxPrice: String?, // 最高价格(可选),NULL表示不限制最高价
// 最大仓位配置
val maxPositionValue: String? = null, // 最大仓位金额(USDC),NULL表示不启用
val maxPositionCount: Int? = null, // 最大仓位数量,NULL表示不启用
// 新增配置字段
val configName: String? = null, // 配置名(可选)
val pushFailedOrders: Boolean = false, // 推送失败订单(默认关闭)
@@ -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
)
@@ -40,6 +40,9 @@ data class Account(
@Column(name = "is_enabled", nullable = false)
val isEnabled: Boolean = true, // 是否启用(用于订单推送等功能的开关)
@Column(name = "wallet_type", nullable = false, length = 20)
val walletType: String = "magic", // 钱包类型:magic(邮箱/OAuth登录)或 safeMetaMask浏览器钱包)
@Column(name = "created_at", nullable = false)
val createdAt: Long = System.currentTimeMillis(),
@@ -38,6 +38,9 @@ data class CopyOrderTracking(
@Column(name = "leader_buy_trade_id", nullable = false, length = 100)
val leaderBuyTradeId: String, // Leader 买入交易ID
@Column(name = "leader_buy_quantity", nullable = true, precision = 20, scale = 8)
val leaderBuyQuantity: BigDecimal? = null, // Leader 买入数量(用于固定金额模式计算卖出比例)
@Column(name = "quantity", nullable = false, precision = 20, scale = 8)
val quantity: BigDecimal, // 买入数量
@@ -53,6 +56,9 @@ data class CopyOrderTracking(
@Column(name = "status", nullable = false, length = 20)
var status: String = "filled", // filled, fully_matched, partially_matched
@Column(name = "notification_sent", nullable = false)
var notificationSent: Boolean = false, // 是否已发送通知(从订单详情获取实际数据后发送)
@Column(name = "created_at", nullable = false)
val createdAt: Long = System.currentTimeMillis(),
@@ -84,6 +84,13 @@ data class CopyTrading(
@Column(name = "max_price", precision = 20, scale = 8)
val maxPrice: BigDecimal? = null, // 最高价格(可选),NULL表示不限制最高价
// 最大仓位配置
@Column(name = "max_position_value", precision = 20, scale = 8)
val maxPositionValue: BigDecimal? = null, // 最大仓位金额(USDC),NULL表示不启用
@Column(name = "max_position_count")
val maxPositionCount: Int? = null, // 最大仓位数量,NULL表示不启用
// 新增配置字段
@Column(name = "config_name", length = 255)
val configName: String? = null, // 配置名(可选)
@@ -1,55 +0,0 @@
package com.wrbug.polymarketbot.entity
import jakarta.persistence.*
/**
* 失败交易实体
* 记录处理失败的交易信息
*/
@Entity
@Table(name = "failed_trade")
data class FailedTrade(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long? = null,
@Column(name = "leader_id", nullable = false)
val leaderId: Long,
@Column(name = "leader_trade_id", nullable = false, length = 100)
val leaderTradeId: String, // Leader 的交易ID
@Column(name = "trade_type", nullable = false, length = 10)
val tradeType: String, // BUY 或 SELL
@Column(name = "copy_trading_id", nullable = false)
val copyTradingId: Long,
@Column(name = "account_id", nullable = false)
val accountId: Long,
@Column(name = "market_id", nullable = false, length = 100)
val marketId: String,
@Column(name = "side", nullable = false, length = 10)
val side: String, // YES/NO
@Column(name = "price", nullable = false, length = 50)
val price: String, // 价格(字符串格式)
@Column(name = "size", nullable = false, length = 50)
val size: String, // 数量(字符串格式)
@Column(name = "error_message", columnDefinition = "TEXT")
val errorMessage: String? = null, // 错误信息
@Column(name = "retry_count", nullable = false)
val retryCount: Int = 0, // 重试次数
@Column(name = "failed_at", nullable = false)
val failedAt: Long = System.currentTimeMillis(),
@Column(name = "created_at", nullable = false)
val createdAt: 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,73 @@
package com.wrbug.polymarketbot.entity
import jakarta.persistence.*
/**
* Polygon RPC 节点配置实体
* 用于存储用户配置的 RPC 节点信息
*/
@Entity
@Table(name = "rpc_node_config")
data class RpcNodeConfig(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long? = null,
@Column(name = "provider_type", nullable = false, length = 50)
val providerType: String, // 服务商类型: ALCHEMY, INFURA, QUICKNODE, CHAINSTACK, GETBLOCK, CUSTOM, PUBLIC
@Column(name = "name", nullable = false, length = 100)
val name: String, // 节点名称
@Column(name = "http_url", nullable = false, length = 500)
val httpUrl: String, // HTTP RPC URL
@Column(name = "ws_url", length = 500)
val wsUrl: String? = null, // WebSocket URL (可选)
@Column(name = "api_key", length = 200)
val apiKey: String? = null, // API Key (加密存储)
@Column(name = "enabled", nullable = false)
var enabled: Boolean = true, // 是否启用
@Column(name = "priority", nullable = false)
var priority: Int = 0, // 优先级(数字越小优先级越高)
@Column(name = "last_check_time")
var lastCheckTime: Long? = null, // 最后检查时间(毫秒时间戳)
@Column(name = "last_check_status", length = 20)
var lastCheckStatus: String? = null, // 最后检查状态: HEALTHY, UNHEALTHY, UNKNOWN
@Column(name = "response_time_ms")
var responseTimeMs: Int? = null, // 最后一次响应时间(毫秒)
@Column(name = "created_at", nullable = false)
val createdAt: Long = System.currentTimeMillis(),
@Column(name = "updated_at", nullable = false)
var updatedAt: Long = System.currentTimeMillis()
)
/**
* RPC 节点健康状态枚举
*/
enum class NodeHealthStatus {
HEALTHY, // 健康
UNHEALTHY, // 不健康
UNKNOWN // 未知
}
/**
* RPC 节点服务商类型枚举
*/
enum class RpcProviderType {
ALCHEMY,
INFURA,
QUICKNODE,
CHAINSTACK,
GETBLOCK,
CUSTOM,
PUBLIC
}
@@ -41,6 +41,9 @@ data class SellMatchRecord(
@Column(name = "total_realized_pnl", nullable = false, precision = 20, scale = 8)
val totalRealizedPnl: BigDecimal, // 总已实现盈亏
@Column(name = "price_updated", nullable = false)
var priceUpdated: Boolean = false, // 共用字段:false 表示未处理(未查询订单详情,未发送通知),true 表示已处理(已查询订单详情,已发送通知)
@Column(name = "created_at", nullable = false)
val createdAt: 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 }
}
}
}
@@ -50,5 +50,16 @@ interface CopyOrderTrackingRepository : JpaRepository<CopyOrderTracking, Long> {
* 根据买入订单ID查询订单跟踪记录
*/
fun findByBuyOrderId(buyOrderId: String): List<CopyOrderTracking>
/**
* 查询未发送通知的买入订单(用于轮询更新)
*/
fun findByNotificationSentFalse(): List<CopyOrderTracking>
/**
* 查询指定时间之前创建的订单(用于检查30秒后未成交的订单)
*/
@Query("SELECT t FROM CopyOrderTracking t WHERE t.createdAt <= :beforeTime")
fun findByCreatedAtBefore(beforeTime: Long): List<CopyOrderTracking>
}
@@ -1,23 +0,0 @@
package com.wrbug.polymarketbot.repository
import com.wrbug.polymarketbot.entity.FailedTrade
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
/**
* 失败交易Repository
*/
@Repository
interface FailedTradeRepository : JpaRepository<FailedTrade, Long> {
/**
* 根据Leader ID和交易ID查询
*/
fun findByLeaderIdAndLeaderTradeId(leaderId: Long, leaderTradeId: String): FailedTrade?
/**
* 检查是否存在失败的交易
*/
fun existsByLeaderIdAndLeaderTradeId(leaderId: Long, leaderTradeId: String): Boolean
}
@@ -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>
}
@@ -0,0 +1,23 @@
package com.wrbug.polymarketbot.repository
import com.wrbug.polymarketbot.entity.RpcNodeConfig
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
@Repository
interface RpcNodeConfigRepository : JpaRepository<RpcNodeConfig, Long> {
/**
* 查询所有已启用的节点,按优先级排序(优先级数字越小越靠前)
*/
fun findAllByEnabledTrueOrderByPriorityAsc(): List<RpcNodeConfig>
/**
* 查询指定 ID 的已启用节点
*/
fun findByIdAndEnabledTrue(id: Long): RpcNodeConfig?
/**
* 查询所有节点,按优先级排序
*/
fun findAllByOrderByPriorityAsc(): List<RpcNodeConfig>
}
@@ -24,5 +24,11 @@ interface SellMatchRecordRepository : JpaRepository<SellMatchRecord, Long> {
* 根据Leader卖出交易ID查询记录
*/
fun findByLeaderSellTradeId(leaderSellTradeId: String): SellMatchRecord?
/**
* 查询所有价格未更新的卖出记录
* 注意priceUpdated 现在同时表示价格已更新和通知已发送共用字段
*/
fun findByPriceUpdatedFalse(): List<SellMatchRecord>
}
@@ -37,7 +37,8 @@ class AccountService(
private val orderSigningService: OrderSigningService,
private val cryptoUtils: CryptoUtils,
private val telegramNotificationService: TelegramNotificationService? = null, // 可选,避免循环依赖
private val relayClientService: RelayClientService
private val relayClientService: RelayClientService,
private val jsonUtils: JsonUtils
) {
private val logger = LoggerFactory.getLogger(AccountService::class.java)
@@ -98,8 +99,9 @@ class AccountService(
}
// 5. 获取代理地址(必须成功,否则导入失败)
// 根据用户选择的钱包类型计算代理地址
val proxyAddress = runBlocking {
val proxyResult = blockchainService.getProxyAddress(request.walletAddress)
val proxyResult = blockchainService.getProxyAddress(request.walletAddress, request.walletType)
if (proxyResult.isSuccess) {
val address = proxyResult.getOrNull()
if (address != null) {
@@ -149,6 +151,7 @@ class AccountService(
accountName = accountName,
isDefault = false, // 不再支持默认账户
isEnabled = request.isEnabled,
walletType = request.walletType, // 保存钱包类型
createdAt = System.currentTimeMillis(),
updatedAt = System.currentTimeMillis()
)
@@ -199,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)
}
}
/**
* 删除账户
*/
@@ -362,6 +444,7 @@ class AccountService(
proxyAddress = account.proxyAddress,
accountName = account.accountName,
isEnabled = account.isEnabled,
walletType = account.walletType,
apiKeyConfigured = account.apiKey != null,
apiSecretConfigured = account.apiSecret != null,
apiPassphraseConfigured = account.apiPassphrase != null,
@@ -927,6 +1010,8 @@ class AccountService(
marketId = request.marketId,
marketSlug = marketSlug,
side = request.side,
price = sellPrice, // 直接传递卖出价格
size = sellQuantity.toPlainString(), // 直接传递卖出数量
accountName = account.accountName,
walletAddress = account.walletAddress,
clobApi = clobApi,
@@ -1119,7 +1204,7 @@ class AccountService(
// 如果目标 outcome 不是第一个(index != 0),需要转换价格
// 对于二元市场:第二个 outcome 的价格 = 1 - 第一个 outcome 的价格
if (outcomeIndex != null && outcomeIndex > 0) {
val outcomes = JsonUtils.parseStringArray(market.outcomes)
val outcomes = jsonUtils.parseStringArray(market.outcomes)
// 只对二元市场进行价格转换
if (outcomes.size == 2) {
// 保存原始第一个 outcome 的价格
@@ -1151,13 +1236,13 @@ class AccountService(
null
}
// 优先使用 lastPrice(最近成交价),如果没有则使用 bestBid,最后使用 midpoint
val currentPrice = lastPrice ?: bestBid ?: midpoint ?: "0"
Result.success(
MarketPriceResponse(
marketId = marketId,
lastPrice = lastPrice,
bestBid = bestBid,
bestAsk = bestAsk,
midpoint = midpoint
currentPrice = currentPrice
)
)
} else {
@@ -21,8 +21,7 @@ import org.springframework.context.i18n.LocaleContextHolder
import com.wrbug.polymarketbot.service.system.SystemConfigService
import com.wrbug.polymarketbot.service.system.RelayClientService
import com.wrbug.polymarketbot.service.system.TelegramNotificationService
import com.wrbug.polymarketbot.util.RetrofitFactory
import com.wrbug.polymarketbot.util.JsonUtils
import com.wrbug.polymarketbot.service.common.MarketPriceService
import org.springframework.stereotype.Service
import java.math.BigDecimal
import java.util.concurrent.ConcurrentHashMap
@@ -45,7 +44,7 @@ class PositionCheckService(
private val telegramNotificationService: TelegramNotificationService?,
private val accountRepository: AccountRepository,
private val messageSource: MessageSource,
private val retrofitFactory: RetrofitFactory
private val marketPriceService: MarketPriceService
) {
private val logger = LoggerFactory.getLogger(PositionCheckService::class.java)
@@ -427,118 +426,10 @@ class PositionCheckService(
/**
* 获取当前市场最新价用于更新订单卖出价
* 优先使用 bestBid最优买价如果没有则使用 midpoint中间价
* 如果市场已关闭
* - outcome 赢了返回 1
* - outcome 输了返回 0
* 委托给 MarketPriceService 处理
*/
private suspend fun getCurrentMarketPrice(marketId: String, outcomeIndex: Int): BigDecimal {
return try {
// 先获取市场信息,检查市场是否已关闭
val gammaApi = retrofitFactory.createGammaApi()
val marketResponse = gammaApi.listMarkets(conditionIds = listOf(marketId))
if (marketResponse.isSuccessful && marketResponse.body() != null) {
val markets = marketResponse.body()!!
val market = markets.firstOrNull()
if (market != null && market.closed == true) {
// 市场已关闭,检查该 outcome 是赢了还是输了
val outcomeResult = checkOutcomeResult(market, outcomeIndex)
when (outcomeResult) {
OutcomeResult.WON -> {
logger.info("市场已关闭且该 outcome 赢了,返回价格为 1: marketId=$marketId, outcomeIndex=$outcomeIndex")
return BigDecimal.ONE
}
OutcomeResult.LOST -> {
logger.info("市场已关闭且该 outcome 输了,返回价格为 0: marketId=$marketId, outcomeIndex=$outcomeIndex")
return BigDecimal.ZERO
}
OutcomeResult.UNKNOWN -> {
// 无法判断,继续使用正常价格逻辑
}
}
}
}
// 如果市场未关闭或无法判断输赢,获取正常价格
val priceResult = accountService.getMarketPrice(marketId, outcomeIndex)
val marketPrice = priceResult.getOrNull()
if (marketPrice != null) {
// 优先使用 bestBid(最优买价,用于卖出参考),如果没有则使用 midpoint
val priceStr = marketPrice.bestBid ?: marketPrice.midpoint ?: marketPrice.lastPrice
priceStr?.toSafeBigDecimal() ?: BigDecimal.ZERO
} else {
BigDecimal.ZERO
}
} catch (e: Exception) {
logger.error("获取市场最新价失败: marketId=$marketId, outcomeIndex=$outcomeIndex, error=${e.message}", e)
BigDecimal.ZERO
}
}
/**
* Outcome 结果枚举
*/
private enum class OutcomeResult {
WON, // 赢了
LOST, // 输了
UNKNOWN // 无法判断
}
/**
* 检查该 outcome 的结果赢了输了或无法判断
* @param market 市场信息
* @param outcomeIndex outcome 索引
* @return OutcomeResult
*/
private fun checkOutcomeResult(market: com.wrbug.polymarketbot.api.MarketResponse, outcomeIndex: Int): OutcomeResult {
return try {
// 优先使用 outcomePrices(结算价格数组)
val outcomePrices = market.outcomePrices
if (outcomePrices != null && outcomePrices.isNotBlank()) {
val prices = JsonUtils.parseStringArray(outcomePrices)
if (outcomeIndex < prices.size) {
val price = prices[outcomeIndex].toSafeBigDecimal()
// 如果价格 >= 0.99,认为赢了
if (price >= BigDecimal("0.99")) {
return OutcomeResult.WON
}
// 如果价格 <= 0.01,认为输了
if (price <= BigDecimal("0.01")) {
return OutcomeResult.LOST
}
// 其他情况,无法判断
return OutcomeResult.UNKNOWN
}
}
// 如果没有 outcomePrices,使用 bestBid 和 bestAsk 判断
val bestBid = market.bestBid ?: 0.0
val bestAsk = market.bestAsk ?: 0.0
// 如果目标 outcome 不是第一个(index != 0),需要转换价格
val targetBid = if (outcomeIndex > 0) {
// 第二个 outcome 的 bestBid = 1 - 第一个 outcome 的 bestAsk
BigDecimal.ONE.subtract(BigDecimal.valueOf(bestAsk))
} else {
BigDecimal.valueOf(bestBid)
}
// 如果 bestBid >= 0.99,认为赢了
if (targetBid >= BigDecimal("0.99")) {
return OutcomeResult.WON
}
// 如果 bestBid <= 0.01,认为输了
if (targetBid <= BigDecimal("0.01")) {
return OutcomeResult.LOST
}
// 其他情况,无法判断
OutcomeResult.UNKNOWN
} catch (e: Exception) {
logger.warn("检查 outcome 结果失败: marketId=${market.conditionId}, outcomeIndex=$outcomeIndex, error=${e.message}", e)
OutcomeResult.UNKNOWN
}
return marketPriceService.getCurrentMarketPrice(marketId, outcomeIndex)
}
@@ -628,7 +519,8 @@ class PositionCheckService(
outcomeIndex = outcomeIndex,
totalMatchedQuantity = totalMatchedQuantity,
sellPrice = sellPrice,
totalRealizedPnl = totalRealizedPnl
totalRealizedPnl = totalRealizedPnl,
priceUpdated = true // 自动生成的订单,直接标记为已处理,不发送通知
)
val savedRecord = sellMatchRecordRepository.save(matchRecord)
@@ -742,7 +634,8 @@ class PositionCheckService(
outcomeIndex = outcomeIndex,
totalMatchedQuantity = totalMatchedQuantity,
sellPrice = sellPrice,
totalRealizedPnl = totalRealizedPnl
totalRealizedPnl = totalRealizedPnl,
priceUpdated = true // 自动生成的订单,直接标记为已处理,不发送通知
)
val savedRecord = sellMatchRecordRepository.save(matchRecord)
@@ -31,22 +31,44 @@ class AuthService(
private lateinit var resetPasswordKey: String
/**
* 登录
* 登录带IP限速保护
*/
fun login(username: String, password: String): Result<LoginResponse> {
fun login(username: String, password: String, ipAddress: String): Result<LoginResponse> {
return try {
// 检查登录频率限制
rateLimitService.checkLoginRateLimit(ipAddress).fold(
onSuccess = { },
onFailure = { e ->
return Result.failure(IllegalStateException(e.message ?: "登录频率限制"))
}
)
val user = userRepository.findByUsername(username)
?: return Result.failure(IllegalArgumentException(ErrorCode.AUTH_USERNAME_OR_PASSWORD_ERROR.message))
// 验证密码
if (!passwordEncoder.matches(password, user.password)) {
logger.warn("登录失败:密码错误,username=$username")
if (user == null) {
// 记录失败尝试
val lockoutMsg = rateLimitService.recordLoginFailure(ipAddress)
if (lockoutMsg != null) {
return Result.failure(IllegalStateException(lockoutMsg))
}
return Result.failure(IllegalArgumentException(ErrorCode.AUTH_USERNAME_OR_PASSWORD_ERROR.message))
}
// 验证密码
if (!passwordEncoder.matches(password, user.password)) {
// 记录失败尝试
val lockoutMsg = rateLimitService.recordLoginFailure(ipAddress)
if (lockoutMsg != null) {
return Result.failure(IllegalStateException(lockoutMsg))
}
return Result.failure(IllegalArgumentException(ErrorCode.AUTH_USERNAME_OR_PASSWORD_ERROR.message))
}
// 登录成功,清除失败记录
rateLimitService.clearLoginFailures(ipAddress)
// 生成JWT token(包含tokenVersion,用于使修改密码后的旧token失效)
val token = jwtUtils.generateToken(username, user.tokenVersion)
logger.info("用户登录成功:username=$username")
Result.success(LoginResponse(token = token))
} catch (e: Exception) {
@@ -0,0 +1,93 @@
package com.wrbug.polymarketbot.service.auth
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Service
import java.security.SecureRandom
import java.util.concurrent.ConcurrentHashMap
/**
* WebSocket 票据服务
* 用于生成短期有效的一次性票据避免在 WebSocket URL 中暴露 JWT
*/
@Service
class WebSocketTicketService {
companion object {
// 票据有效期(30秒)
private const val TICKET_VALIDITY_MS = 30_000L
// 票据长度(32字节 = 64个十六进制字符)
private const val TICKET_LENGTH = 32
}
private val secureRandom = SecureRandom()
// 存储票据:ticket -> TicketInfo
private val tickets = ConcurrentHashMap<String, TicketInfo>()
/**
* 票据信息
*/
data class TicketInfo(
val username: String,
val createdAt: Long,
val expiresAt: Long
)
/**
* 为用户生成 WebSocket 连接票据
* @param username 用户名
* @return 一次性票据
*/
fun generateTicket(username: String): String {
// 清理过期票据
cleanupExpiredTickets()
// 生成随机票据
val bytes = ByteArray(TICKET_LENGTH)
secureRandom.nextBytes(bytes)
val ticket = bytes.joinToString("") { "%02x".format(it) }
val now = System.currentTimeMillis()
tickets[ticket] = TicketInfo(
username = username,
createdAt = now,
expiresAt = now + TICKET_VALIDITY_MS
)
return ticket
}
/**
* 验证并消费票据一次性使用
* @param ticket 票据
* @return 用户名如果票据无效则返回 null
*/
fun validateAndConsumeTicket(ticket: String): String? {
val ticketInfo = tickets.remove(ticket) ?: return null
// 检查是否过期
if (System.currentTimeMillis() > ticketInfo.expiresAt) {
return null
}
return ticketInfo.username
}
/**
* 清理过期票据
*/
private fun cleanupExpiredTickets() {
val now = System.currentTimeMillis()
tickets.entries.removeIf { it.value.expiresAt < now }
}
/**
* 定时清理过期票据每分钟执行一次
* 防止过期票据长时间占用内存
*/
@Scheduled(fixedRate = 60_000) // 60秒 = 60000毫秒
fun scheduledCleanup() {
cleanupExpiredTickets()
}
}
@@ -1,5 +1,6 @@
package com.wrbug.polymarketbot.service.common
import com.google.gson.Gson
import com.wrbug.polymarketbot.api.EthereumRpcApi
import com.wrbug.polymarketbot.api.JsonRpcRequest
import com.wrbug.polymarketbot.api.JsonRpcResponse
@@ -12,6 +13,7 @@ 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
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
@@ -26,10 +28,10 @@ import java.math.BigInteger
class BlockchainService(
@Value("\${polymarket.data-api.base-url:https://data-api.polymarket.com}")
private val dataApiBaseUrl: String,
@Value("\${polygon.rpc.url:}")
private val polygonRpcUrl: String,
private val retrofitFactory: RetrofitFactory,
private val relayClientService: RelayClientService
private val relayClientService: RelayClientService,
private val rpcNodeService: RpcNodeService,
private val gson: Gson
) {
private val logger = LoggerFactory.getLogger(BlockchainService::class.java)
@@ -37,9 +39,16 @@ class BlockchainService(
// USDC 合约地址(Polygon 主网,Polymarket 使用 Polygon
private val usdcContractAddress = "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174"
// Polymarket 代理工厂合约地址(Polygon 主网)
// Polymarket Safe 代理工厂合约地址(Polygon 主网,用于 MetaMask 用户
// 合约地址: 0xaacFeEa03eb1561C4e67d661e40682Bd20E3541b
private val proxyFactoryContractAddress = "0xaacFeEa03eb1561C4e67d661e40682Bd20E3541b"
private val safeProxyFactoryAddress = "0xaacFeEa03eb1561C4e67d661e40682Bd20E3541b"
// Polymarket Magic 代理工厂合约地址(Polygon 主网,用于邮箱/OAuth 登录用户)
// 合约地址: 0xaB45c5A4B0c941a2F231C04C3f49182e1A254052
private val magicProxyFactoryAddress = "0xaB45c5A4B0c941a2F231C04C3f49182e1A254052"
// Magic Proxy 的 init code hash(用于 CREATE2 计算)
private val magicProxyInitCodeHash = "0xd21df8dc65880a8606f09fe0ce3df9b8869287ab0b058be05aa9e8af6330a00b"
// ConditionalTokens 合约地址(Polygon 主网)
private val conditionalTokensAddress = "0x4D97DCd97eC945f40cF65F87097ACe5EA0476045"
@@ -64,79 +73,174 @@ class BlockchainService(
Retrofit.Builder()
.baseUrl("$baseUrl/")
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(PolymarketDataApi::class.java)
}
private val polygonRpcApi: EthereumRpcApi? by lazy {
if (polygonRpcUrl.isBlank()) {
null
} else {
retrofitFactory.createEthereumRpcApi(polygonRpcUrl)
}
private val polygonRpcApi: EthereumRpcApi by lazy {
val rpcUrl = rpcNodeService.getHttpUrl()
retrofitFactory.createEthereumRpcApi(rpcUrl)
}
/**
* 获取 Polymarket 代理钱包地址
* 通过 RPC 调用代理工厂合约获取用户的代理钱包地址
* 根据指定的钱包类型返回对应的代理地址
*
* Polymarket 有两种代理钱包类型
* 1. Magic Proxy邮箱/OAuth 登录用户- 使用 CREATE2 计算地址
* 2. Safe ProxyMetaMask 钱包用户- 通过合约调用获取地址
*
* @param walletAddress 用户的钱包地址EOA
* @param walletType 钱包类型"magic"默认 "safe"
* @return 代理钱包地址
*/
suspend fun getProxyAddress(walletAddress: String, walletType: String = "magic"): Result<String> {
return try {
when (walletType.lowercase()) {
"safe" -> {
// Safe ProxyMetaMask 用户)
val safeProxyResult = getSafeProxyAddress(walletAddress)
if (safeProxyResult.isSuccess) {
val safeProxyAddress = safeProxyResult.getOrNull()!!
logger.debug("使用 Safe Proxy 地址: $safeProxyAddress")
Result.success(safeProxyAddress)
} else {
Result.failure(safeProxyResult.exceptionOrNull() ?: Exception("获取 Safe Proxy 地址失败"))
}
}
else -> {
// Magic Proxy(邮箱/OAuth 登录用户)- 默认
val magicProxyAddress = calculateMagicProxyAddress(walletAddress)
logger.debug("使用 Magic Proxy 地址: $magicProxyAddress")
Result.success(magicProxyAddress)
}
}
} catch (e: Exception) {
logger.error("获取代理地址失败: ${e.message}", e)
Result.failure(e)
}
}
/**
* 计算 Magic Proxy 地址使用 CREATE2
* 用于邮箱/OAuth 登录的用户
*
* CREATE2 地址计算公式
* address = keccak256(0xff ++ factory ++ salt ++ initCodeHash)[12:]
* salt = keccak256(eoaAddress)
*
* @param walletAddress 用户的钱包地址EOA
* @return Magic 代理钱包地址
*/
fun calculateMagicProxyAddress(walletAddress: String): String {
// 计算 salt = keccak256(eoaAddress)
val eoaBytes = EthereumUtils.hexToBytes(walletAddress.lowercase())
val salt = EthereumUtils.keccak256(eoaBytes)
// 计算 CREATE2 地址
// data = 0xff ++ factory ++ salt ++ initCodeHash
val prefix = byteArrayOf(0xff.toByte())
val factoryBytes = EthereumUtils.hexToBytes(magicProxyFactoryAddress)
val initCodeHashBytes = EthereumUtils.hexToBytes(magicProxyInitCodeHash)
val data = prefix + factoryBytes + salt + initCodeHashBytes
val hash = EthereumUtils.keccak256(data)
// 取后 20 字节作为地址
return "0x" + hash.copyOfRange(12, 32).joinToString("") { "%02x".format(it) }
}
/**
* 获取 Safe Proxy 地址
* 通过 RPC 调用 Safe 代理工厂合约获取用户的代理钱包地址
* 用于 MetaMask 钱包用户
*
* @param walletAddress 用户的钱包地址
* @return 代理钱包地址
*/
suspend fun getProxyAddress(walletAddress: String): Result<String> {
private suspend fun getSafeProxyAddress(walletAddress: String): Result<String> {
return try {
// 如果未配置 RPC URL,返回错误
if (polygonRpcUrl.isBlank()) {
logger.warn("未配置 Polygon RPC URL,无法获取代理地址")
return Result.failure(IllegalStateException("未配置 Polygon RPC URL,无法获取代理地址。请在配置文件中设置 polygon.rpc.url 环境变量"))
}
val rpcApi = polygonRpcApi ?: throw IllegalStateException("Polygon RPC URL 未配置")
val rpcApi = polygonRpcApi
// 计算函数选择器
val functionSelector = EthereumUtils.getFunctionSelector(computeProxyAddressFunctionSignature)
// 编码地址参数
val encodedAddress = EthereumUtils.encodeAddress(walletAddress)
// 构建调用数据
val data = functionSelector + encodedAddress
// 构建 JSON-RPC 请求
val rpcRequest = JsonRpcRequest(
method = "eth_call",
params = listOf(
mapOf(
"to" to proxyFactoryContractAddress,
"to" to safeProxyFactoryAddress,
"data" to data
),
"latest"
)
)
// 发送 RPC 请求
val response = rpcApi.call(rpcRequest)
if (!response.isSuccessful || response.body() == null) {
throw Exception("RPC 请求失败: ${response.code()} ${response.message()}")
return Result.failure(Exception("RPC 请求失败: ${response.code()} ${response.message()}"))
}
val rpcResponse = response.body()!!
// 检查错误
if (rpcResponse.error != null) {
throw Exception("RPC 错误: ${rpcResponse.error.message}")
return Result.failure(Exception("RPC 错误: ${rpcResponse.error.message}"))
}
val hexResult = rpcResponse.result ?: throw Exception("RPC 响应格式错误: result 为空")
// 使用 Gson 解析 resultJsonElement
val hexResult = rpcResponse.result?.asString
?: return Result.failure(Exception("RPC 响应格式错误: result 为空"))
// 解析代理地址
val proxyAddress = EthereumUtils.decodeAddress(hexResult)
Result.success(proxyAddress)
} catch (e: Exception) {
logger.error("获取代理地址失败: ${e.message}", e)
Result.failure(e)
}
}
/**
* 检查地址是否是合约
* @param address 地址
* @return 如果地址有代码是合约返回 true
*/
private suspend fun isContract(address: String): Boolean {
return try {
val rpcApi = polygonRpcApi
val rpcRequest = JsonRpcRequest(
method = "eth_getCode",
params = listOf(address, "latest")
)
val response = rpcApi.call(rpcRequest)
if (!response.isSuccessful || response.body() == null) {
return false
}
val rpcResponse = response.body()!!
if (rpcResponse.error != null) {
return false
}
val code = rpcResponse.result?.asString ?: "0x"
// 如果代码不是 "0x" 或 "0x0",则是合约
code != "0x" && code != "0x0"
} catch (e: Exception) {
logger.warn("检查合约地址失败: ${e.message}")
false
}
}
/**
* 查询账户 USDC 余额
@@ -147,12 +251,6 @@ class BlockchainService(
*/
suspend fun getUsdcBalance(walletAddress: String, proxyAddress: String): Result<String> {
return try {
// 如果未配置 RPC URL,返回错误
if (polygonRpcUrl.isBlank()) {
logger.warn("未配置 Polygon RPC URL,无法查询 USDC 余额")
return Result.failure(IllegalStateException("未配置 Polygon RPC URL,无法查询 USDC 余额。请在配置文件中设置 polygon.rpc.url 环境变量"))
}
// 检查代理地址是否为空
if (proxyAddress.isBlank()) {
logger.error("代理地址为空,无法查询余额")
@@ -173,7 +271,7 @@ class BlockchainService(
* 通过 RPC 查询 USDC 余额
*/
private suspend fun queryUsdcBalanceViaRpc(walletAddress: String): String {
val rpcApi = polygonRpcApi ?: throw IllegalStateException("Polygon RPC URL 未配置")
val rpcApi = polygonRpcApi
// 构建 ERC-20 balanceOf 函数调用
// function signature: balanceOf(address) -> bytes4(0x70a08231)
@@ -208,7 +306,9 @@ class BlockchainService(
throw Exception("RPC 错误: ${rpcResponse.error.message}")
}
val hexBalance = rpcResponse.result ?: throw Exception("RPC 响应格式错误: result 为空")
// 使用 Gson 解析 resultJsonElement
val hexBalance = rpcResponse.result?.asString
?: throw Exception("RPC 响应格式错误: result 为空")
// 将十六进制转换为 BigDecimalUSDC 有 6 位小数)
val balanceWei = BigInteger(hexBalance.removePrefix("0x"), 16)
@@ -264,13 +364,7 @@ class BlockchainService(
*/
suspend fun getTokenId(conditionId: String, outcomeIndex: Int): Result<String> {
return try {
// 如果未配置 RPC URL,返回错误
if (polygonRpcUrl.isBlank()) {
logger.warn("未配置 Polygon RPC URL,无法计算 tokenId")
return Result.failure(IllegalStateException("未配置 Polygon RPC URL,无法计算 tokenId"))
}
val rpcApi = polygonRpcApi ?: throw IllegalStateException("Polygon RPC URL 未配置")
val rpcApi = polygonRpcApi
// 验证 outcomeIndex
if (outcomeIndex < 0) {
@@ -309,7 +403,9 @@ class BlockchainService(
return Result.failure(Exception("调用 getCollectionId 失败: ${collectionIdResult.error}"))
}
val collectionId = collectionIdResult.result ?: return Result.failure(Exception("getCollectionId 返回结果为空"))
// 使用 Gson 解析 resultJsonElement
val collectionId = collectionIdResult.result?.asString
?: return Result.failure(Exception("getCollectionId 返回结果为空"))
// 2. 调用 getPositionId(collateralToken, collectionId)
val getPositionIdSelector = EthereumUtils.getFunctionSelector("getPositionId(address,bytes32)")
@@ -339,7 +435,9 @@ class BlockchainService(
return Result.failure(Exception("调用 getPositionId 失败: ${positionIdResult.error}"))
}
val tokenId = positionIdResult.result ?: return Result.failure(Exception("getPositionId 返回结果为空"))
// 使用 Gson 解析 resultJsonElement
val tokenId = positionIdResult.result?.asString
?: return Result.failure(Exception("getPositionId 返回结果为空"))
val tokenIdBigInt = EthereumUtils.decodeUint256(tokenId)
Result.success(tokenIdBigInt.toString())
@@ -449,7 +547,7 @@ class BlockchainService(
* 获取代理钱包的 nonce用于构建 Safe 交易
*/
private suspend fun getProxyNonce(proxyAddress: String): Result<BigInteger> {
val rpcApi = polygonRpcApi ?: throw IllegalStateException("Polygon RPC URL 未配置")
val rpcApi = polygonRpcApi
// Gnosis Safe 的 nonce 通过调用合约的 nonce() 函数获取
val nonceFunctionSelector = EthereumUtils.getFunctionSelector("nonce()")
@@ -475,7 +573,9 @@ class BlockchainService(
return Result.failure(Exception("获取 Proxy nonce 失败: ${rpcResponse.error.message}"))
}
val hexNonce = rpcResponse.result ?: return Result.failure(Exception("Proxy nonce 结果为空"))
// 使用 Gson 解析 resultJsonElement
val hexNonce = rpcResponse.result?.asString
?: return Result.failure(Exception("Proxy nonce 结果为空"))
val nonce = EthereumUtils.decodeUint256(hexNonce)
return Result.success(nonce)
}
@@ -484,7 +584,7 @@ class BlockchainService(
* 获取交易 nonce
*/
private suspend fun getTransactionCount(address: String): Result<BigInteger> {
val rpcApi = polygonRpcApi ?: throw IllegalStateException("Polygon RPC URL 未配置")
val rpcApi = polygonRpcApi
val rpcRequest = JsonRpcRequest(
method = "eth_getTransactionCount",
@@ -503,7 +603,9 @@ class BlockchainService(
return Result.failure(Exception("获取 nonce 失败: ${rpcResponse.error.message}"))
}
val hexNonce = rpcResponse.result ?: return Result.failure(Exception("nonce 结果为空"))
// 使用 Gson 解析 resultJsonElement
val hexNonce = rpcResponse.result?.asString
?: return Result.failure(Exception("nonce 结果为空"))
val nonce = EthereumUtils.decodeUint256(hexNonce)
return Result.success(nonce)
}
@@ -512,7 +614,7 @@ class BlockchainService(
* 获取 gas price
*/
private suspend fun getGasPrice(): Result<BigInteger> {
val rpcApi = polygonRpcApi ?: throw IllegalStateException("Polygon RPC URL 未配置")
val rpcApi = polygonRpcApi
val rpcRequest = JsonRpcRequest(
method = "eth_gasPrice",
@@ -529,7 +631,9 @@ class BlockchainService(
return Result.failure(Exception("获取 gas price 失败: ${rpcResponse.error.message}"))
}
val hexGasPrice = rpcResponse.result ?: return Result.failure(Exception("gas price 结果为空"))
// 使用 Gson 解析 resultJsonElement
val hexGasPrice = rpcResponse.result?.asString
?: return Result.failure(Exception("gas price 结果为空"))
val gasPrice = EthereumUtils.decodeUint256(hexGasPrice)
return Result.success(gasPrice)
}
@@ -603,10 +707,79 @@ class BlockchainService(
return Result.failure(Exception("发送交易失败: ${rpcResponse.error.message}"))
}
val txHash = rpcResponse.result ?: return Result.failure(Exception("交易哈希为空"))
// 使用 Gson 解析 resultJsonElement
val txHash = rpcResponse.result?.asString
?: return Result.failure(Exception("交易哈希为空"))
return Result.success(txHash)
}
/**
* 从链上查询市场条件Condition的结算结果
* 通过调用 ConditionalTokens 合约的 getCondition 函数获取 payouts
*
* @param conditionId 市场条件IDbytes32必须是 0x 开头的 66 位十六进制字符串
* @return Result<Pair<payoutDenominator, payouts>>
* - payoutDenominator: 支付分母通常为 1
* - payouts: 每个 outcome 的支付金额数组0 1
* - 如果 payouts[outcomeIndex] == 1表示该 outcome 赢了
* - 如果 payouts[outcomeIndex] == 0表示该 outcome 输了
* - 如果 payouts 为空表示市场尚未结算
*/
suspend fun getCondition(conditionId: String): Result<Pair<BigInteger, List<BigInteger>>> {
return try {
// 验证 conditionId 格式
if (conditionId.isBlank() || !conditionId.startsWith("0x") || conditionId.length != 66) {
return Result.failure(IllegalArgumentException("conditionId 格式错误,必须是 0x 开头的 66 位十六进制字符串"))
}
val rpcApi = polygonRpcApi
// 构建 getCondition(bytes32) 函数调用
// 函数签名: getCondition(bytes32)
val functionSelector = EthereumUtils.getFunctionSelector("getCondition(bytes32)")
val encodedConditionId = EthereumUtils.encodeBytes32(conditionId)
val data = functionSelector + encodedConditionId
// 构建 JSON-RPC 请求
val rpcRequest = JsonRpcRequest(
method = "eth_call",
params = listOf(
mapOf(
"to" to conditionalTokensAddress,
"data" to data
),
"latest"
)
)
// 发送 RPC 请求
val response = rpcApi.call(rpcRequest)
if (!response.isSuccessful || response.body() == null) {
return Result.failure(Exception("RPC 请求失败: ${response.code()} ${response.message()}"))
}
val rpcResponse = response.body()!!
// 检查错误
if (rpcResponse.error != null) {
return Result.failure(Exception("RPC 错误: ${rpcResponse.error.message}"))
}
// 使用 Gson 解析 resultJsonElement
val hexResult = rpcResponse.result?.asString
?: return Result.failure(Exception("RPC 响应格式错误: result 为空"))
// 解析 ABI 编码的返回结果
val (payoutDenominator, payouts) = EthereumUtils.decodeConditionResult(hexResult)
Result.success(Pair(payoutDenominator, payouts))
} catch (e: Exception) {
logger.error("查询市场条件失败: conditionId=$conditionId, ${e.message}", e)
Result.failure(e)
}
}
/**
* 查询交易详情用于调试和分析
* @param txHash 交易哈希
@@ -614,11 +787,7 @@ class BlockchainService(
*/
suspend fun getTransactionDetails(txHash: String): Result<String> {
return try {
if (polygonRpcUrl.isBlank()) {
return Result.failure(IllegalStateException("未配置 Polygon RPC URL"))
}
val rpcApi = polygonRpcApi ?: throw IllegalStateException("Polygon RPC URL 未配置")
val rpcApi = polygonRpcApi
// 查询交易
val txRequest = JsonRpcRequest(
@@ -636,7 +805,9 @@ class BlockchainService(
return Result.failure(Exception("查询交易失败: ${txRpcResponse.error.message}"))
}
val txResult = txRpcResponse.result ?: return Result.failure(Exception("交易结果为空"))
// 使用 Gson 解析 resultJsonElement
val txResult = txRpcResponse.result?.toString()
?: return Result.failure(Exception("交易结果为空"))
// 查询交易回执(包含内部调用和事件日志)
val receiptRequest = JsonRpcRequest(
@@ -653,7 +824,7 @@ class BlockchainService(
val receiptResult = if (receiptRpcResponse.error != null) {
"交易回执查询失败: ${receiptRpcResponse.error.message}"
} else {
receiptRpcResponse.result ?: "交易回执为空(可能还在打包中)"
receiptRpcResponse.result?.toString() ?: "交易回执为空(可能还在打包中)"
}
Result.success("交易信息:\n$txResult\n\n交易回执:\n$receiptResult")
@@ -0,0 +1,211 @@
package com.wrbug.polymarketbot.service.common
import com.wrbug.polymarketbot.api.PolymarketClobApi
import com.wrbug.polymarketbot.repository.AccountRepository
import com.wrbug.polymarketbot.util.CryptoUtils
import com.wrbug.polymarketbot.util.RetrofitFactory
import com.wrbug.polymarketbot.util.toSafeBigDecimal
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import java.math.BigDecimal
import java.math.BigInteger
/**
* 市场价格服务
* 统一封装从不同数据源获取市场价格的逻辑
* 数据源包括
* 1. 链上 RPC 查询市场结算结果
* 2. CLOB API订单簿价格
*/
@Service
class MarketPriceService(
private val blockchainService: BlockchainService,
private val retrofitFactory: RetrofitFactory,
private val accountRepository: AccountRepository,
private val cryptoUtils: CryptoUtils
) {
private val logger = LoggerFactory.getLogger(MarketPriceService::class.java)
/**
* 获取当前市场最新价
* 优先级
* 1. 链上查询市场结算结果如果已结算返回 1.0 0.0
* 2. CLOB API 查询订单簿价格最准确使用 bestBid
*
* 价格会被截位到 4 位小数向下截断不四舍五入用于显示和后续计算
*
* @param marketId 市场ID
* @param outcomeIndex 结果索引
* @return 市场价格已截位到 4 位小数
* @throws IllegalStateException 如果所有数据源都失败
*/
suspend fun getCurrentMarketPrice(marketId: String, outcomeIndex: Int): BigDecimal {
// 1. 优先从链上查询市场结算结果
val chainPrice = getPriceFromChainCondition(marketId, outcomeIndex)
if (chainPrice != null) {
// 截位到 4 位小数(向下截断,不四舍五入)
return chainPrice.setScale(4, java.math.RoundingMode.DOWN)
}
// 2. 从 CLOB API 查询订单簿价格(最准确)
val orderbookPrice = getPriceFromClobOrderbook(marketId, outcomeIndex)
if (orderbookPrice != null) {
// 截位到 4 位小数(向下截断,不四舍五入)
return orderbookPrice.setScale(4, java.math.RoundingMode.DOWN)
}
// 如果所有数据源都失败,抛出异常
val errorMsg = "无法获取市场价格: marketId=$marketId, outcomeIndex=$outcomeIndex (链上查询和订单簿查询均失败)"
logger.error(errorMsg)
throw IllegalStateException(errorMsg)
}
/**
* 从链上查询市场结算结果获取价格
* 如果市场已结算
* - payout > 0赢了 返回 1.0
* - payout == 0输了 返回 0.0
* 如果市场未结算或查询失败返回 null
*/
private suspend fun getPriceFromChainCondition(marketId: String, outcomeIndex: Int): BigDecimal? {
return try {
val chainResult = blockchainService.getCondition(marketId)
chainResult.fold(
onSuccess = { (_, payouts) ->
// 如果 payouts 不为空,说明市场已结算
if (payouts.isNotEmpty() && outcomeIndex < payouts.size) {
val payout = payouts[outcomeIndex]
when {
payout > BigInteger.ZERO -> {
logger.info("从链上查询到市场已结算,该 outcome 赢了: marketId=$marketId, outcomeIndex=$outcomeIndex, payout=$payout")
return BigDecimal.ONE
}
payout == BigInteger.ZERO -> {
logger.info("从链上查询到市场已结算,该 outcome 输了: marketId=$marketId, outcomeIndex=$outcomeIndex, payout=$payout")
return BigDecimal.ZERO
}
else -> {
logger.warn("从链上查询到异常的 payout 值: marketId=$marketId, outcomeIndex=$outcomeIndex, payout=$payout")
null
}
}
} else {
logger.debug("从链上查询到市场尚未结算: marketId=$marketId, payouts=${payouts.size}")
null
}
},
onFailure = { e ->
logger.debug("链上查询市场条件失败,降级到 API 查询: marketId=$marketId, error=${e.message}")
null
}
)
} catch (e: Exception) {
logger.debug("链上查询市场条件异常: marketId=$marketId, outcomeIndex=$outcomeIndex, error=${e.message}")
null
}
}
/**
* CLOB API 查询订单簿价格
* 获取订单簿的 bestBid bestAsk计算 midpoint = (bestBid + bestAsk) / 2
* 订单簿数据最准确反映当前市场真实价格
* 如果查询失败返回 null
*/
private suspend fun getPriceFromClobOrderbook(marketId: String, outcomeIndex: Int): BigDecimal? {
return try {
// 获取 tokenId(用于查询特定 outcome 的订单簿)
val tokenIdResult = blockchainService.getTokenId(marketId, outcomeIndex)
if (!tokenIdResult.isSuccess) {
return null
}
val tokenId = tokenIdResult.getOrNull() ?: return null
// 尝试使用带鉴权的 CLOB API,如果没有则使用不带鉴权的 API
val clobApi = try {
getAuthenticatedClobApi() ?: retrofitFactory.createClobApiWithoutAuth()
} catch (e: Exception) {
logger.debug("获取带鉴权的 CLOB API 失败,使用不带鉴权的 API: ${e.message}")
retrofitFactory.createClobApiWithoutAuth()
}
val orderbookResponse = clobApi.getOrderbook(tokenId = tokenId, market = null)
if (!orderbookResponse.isSuccessful || orderbookResponse.body() == null) {
return null
}
val orderbook = orderbookResponse.body()!!
// 获取 bestBid(最高买入价):从 bids 中找到价格最大的
// bids 表示买入订单列表,价格越高表示愿意出的价格越高
val bestBid = orderbook.bids
.mapNotNull { it.price.toSafeBigDecimal() }
.maxOrNull()
// 获取 bestAsk(最低卖出价):从 asks 中找到价格最小的
// asks 表示卖出订单列表,价格越低表示愿意卖的价格越低
val bestAsk = orderbook.asks
.mapNotNull { it.price.toSafeBigDecimal() }
.minOrNull()
// 由于主要用于卖出场景,优先使用 bestBid(最高买入价,卖给愿意买入的人)
// 如果没有 bestBid,则使用 midpoint 或 bestAsk
if (bestBid != null) {
logger.debug("从订单簿获取价格(bestBid: marketId=$marketId, outcomeIndex=$outcomeIndex, bestBid=$bestBid, bestAsk=$bestAsk")
return bestBid
} else if (bestAsk != null && bestAsk > BigDecimal.ZERO) {
// 如果没有 bestBid,使用 bestAsk 作为备选
logger.debug("从订单簿获取价格(bestAsk: marketId=$marketId, outcomeIndex=$outcomeIndex, bestAsk=$bestAsk")
return bestAsk
}
null
} catch (e: Exception) {
logger.debug("CLOB API 查询订单簿失败: marketId=$marketId, outcomeIndex=$outcomeIndex, error=${e.message}")
null
}
}
/**
* 获取带鉴权的 CLOB API 客户端
* 使用第一个有 API 凭证的账户
* 如果都没有返回 null
*/
private fun getAuthenticatedClobApi(): PolymarketClobApi? {
return try {
// 使用第一个有 API 凭证的账户
val account = accountRepository.findAllByOrderByCreatedAtAsc()
.firstOrNull { it.apiKey != null && it.apiSecret != null && it.apiPassphrase != null }
if (account == null || account.apiKey == null || account.apiSecret == null || account.apiPassphrase == null) {
return null
}
// 解密 API 凭证
val apiKey = account.apiKey
val apiSecret = try {
cryptoUtils.decrypt(account.apiSecret)
} catch (e: Exception) {
logger.debug("解密 API Secret 失败: ${e.message}")
return null
}
val apiPassphrase = try {
cryptoUtils.decrypt(account.apiPassphrase)
} catch (e: Exception) {
logger.debug("解密 API Passphrase 失败: ${e.message}")
return null
}
// 创建带鉴权的 CLOB API 客户端
retrofitFactory.createClobApi(apiKey, apiSecret, apiPassphrase, account.walletAddress)
} catch (e: Exception) {
logger.debug("获取带鉴权的 CLOB API 失败: ${e.message}")
null
}
}
}
@@ -1,5 +1,6 @@
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.util.PolymarketL1AuthInterceptor
@@ -19,7 +20,8 @@ import retrofit2.converter.gson.GsonConverterFactory
@Service
class PolymarketApiKeyService(
@Value("\${polymarket.clob.base-url}")
private val clobBaseUrl: String
private val clobBaseUrl: String,
private val gson: Gson
) {
private val logger = LoggerFactory.getLogger(PolymarketApiKeyService::class.java)
@@ -52,34 +54,36 @@ class PolymarketApiKeyService(
try {
// 先尝试获取现有的 API Keyderive
val deriveResult = deriveApiKey(privateKey, walletAddress, chainId)
val maskedAddress = "${walletAddress.take(6)}...${walletAddress.takeLast(4)}"
if (deriveResult.isSuccess) {
val creds = deriveResult.getOrNull()
if (creds != null && isApiCreds(creds)) {
logger.info("成功获取现有 API Key: ${walletAddress}")
logger.debug("成功获取现有 API Key: $maskedAddress")
return@runBlocking Result.success(creds)
}
}
// 如果获取失败或返回无效,尝试创建新的
logger.info("获取现有 API Key 失败,尝试创建新的: ${walletAddress}")
logger.debug("获取现有 API Key 失败,尝试创建新的: $maskedAddress")
val createResult = createApiKey(privateKey, walletAddress, chainId)
if (createResult.isSuccess) {
val creds = createResult.getOrNull()
if (creds != null && isApiCreds(creds)) {
logger.info("成功创建新 API Key: ${walletAddress}")
logger.debug("成功创建新 API Key: $maskedAddress")
return@runBlocking Result.success(creds)
}
}
// 两个都失败
val error = createResult.exceptionOrNull() ?: deriveResult.exceptionOrNull()
val errorMsg = error?.message ?: "未知错误"
logger.error("获取和创建 API Key 都失败: ${walletAddress}", error)
logger.error("获取和创建 API Key 都失败: $maskedAddress", error)
Result.failure(
IllegalStateException("无法获取或创建 API Key: $errorMsg")
)
} catch (e: Exception) {
logger.error("创建或获取 API Key 异常: ${walletAddress}", e)
val maskedAddress = "${walletAddress.take(6)}...${walletAddress.takeLast(4)}"
logger.error("创建或获取 API Key 异常: $maskedAddress", e)
Result.failure(e)
}
}
@@ -222,7 +226,7 @@ class PolymarketApiKeyService(
return Retrofit.Builder()
.baseUrl(clobBaseUrl)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(PolymarketClobApi::class.java)
}
@@ -236,7 +240,7 @@ class PolymarketApiKeyService(
return Retrofit.Builder()
.baseUrl(clobBaseUrl)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(PolymarketClobApi::class.java)
}
@@ -300,13 +300,23 @@ class PolymarketClobService(
)
val response = authenticatedClobApi.getOrder(orderId)
if (response.isSuccessful && response.body() != null) {
Result.success(response.body()!!)
if (response.isSuccessful) {
val body = response.body()
if (body != null) {
Result.success(body)
} else {
// 响应体为空,可能是订单不存在或已过期
logger.warn("获取订单详情失败: 响应体为空, orderId=$orderId, code=${response.code()}")
Result.failure(Exception("订单不存在或已过期: orderId=$orderId"))
}
} else {
Result.failure(Exception("获取订单详情失败: ${response.code()} ${response.message()}"))
// HTTP 状态码不是 2xx
val errorBody = response.errorBody()?.string()?.take(200) ?: "无错误详情"
logger.warn("获取订单详情失败: HTTP ${response.code()}, orderId=$orderId, errorBody=$errorBody")
Result.failure(Exception("获取订单详情失败: HTTP ${response.code()} ${response.message()}"))
}
} catch (e: Exception) {
logger.error("获取订单详情异常: ${e.message}", e)
logger.error("获取订单详情异常: orderId=$orderId, ${e.message}", e)
Result.failure(e)
}
}
@@ -3,50 +3,133 @@ package com.wrbug.polymarketbot.service.common
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.AtomicReference
/**
* 频率限制服务使用内存缓存全局限制
* 频率限制服务使用内存缓存
*/
@Service
class RateLimitService {
private val logger = LoggerFactory.getLogger(RateLimitService::class.java)
// 重置密码限速配置
@Value("\${rate-limit.reset-password.max-attempts:3}")
private var maxAttempts: Int = 3
private var resetPasswordMaxAttempts: Int = 3
@Value("\${rate-limit.reset-password.window-seconds:60}")
private var windowSeconds: Long = 60
private var resetPasswordWindowSeconds: Long = 60
// 登录限速配置
@Value("\${rate-limit.login.max-attempts:5}")
private var loginMaxAttempts: Int = 5
@Value("\${rate-limit.login.window-seconds:300}")
private var loginWindowSeconds: Long = 300 // 5分钟
@Value("\${rate-limit.login.lockout-seconds:900}")
private var loginLockoutSeconds: Long = 900 // 15分钟
// 全局尝试记录列表(时间戳),所有请求共享
private val resetPasswordAttempts = AtomicReference<MutableList<Long>>(mutableListOf())
// 登录失败尝试记录(IP -> 时间戳列表)
private val loginFailedAttempts = ConcurrentHashMap<String, MutableList<Long>>()
// 登录锁定记录(IP -> 锁定结束时间)
private val loginLockouts = ConcurrentHashMap<String, Long>()
/**
* 检查重置密码频率限制全局限制不按IP
* @return Result如果超过限制则返回失败
*/
fun checkResetPasswordRateLimit(): Result<Unit> {
val now = System.currentTimeMillis()
val windowStart = now - (windowSeconds * 1000)
val windowStart = now - (resetPasswordWindowSeconds * 1000)
// 获取当前尝试记录列表
val attempts = resetPasswordAttempts.get()
// 清理过期记录(超过时间窗口的记录)
val validAttempts = attempts.filter { it >= windowStart }.toMutableList()
// 检查是否超过限制
if (validAttempts.size >= maxAttempts) {
logger.warn("重置密码频率限制触发: attempts=${validAttempts.size}/$maxAttempts")
return Result.failure(IllegalStateException("频率限制:1分钟内最多尝试${maxAttempts}次,请稍后再试"))
if (validAttempts.size >= resetPasswordMaxAttempts) {
logger.warn("重置密码频率限制触发: attempts=${validAttempts.size}/$resetPasswordMaxAttempts")
return Result.failure(IllegalStateException("频率限制:1分钟内最多尝试${resetPasswordMaxAttempts}次,请稍后再试"))
}
// 记录本次尝试
validAttempts.add(now)
resetPasswordAttempts.set(validAttempts)
return Result.success(Unit)
}
/**
* 检查登录频率限制按IP限制
* @param ipAddress 客户端IP地址
* @return Result如果被锁定或超过限制则返回失败
*/
fun checkLoginRateLimit(ipAddress: String): Result<Unit> {
val now = System.currentTimeMillis()
// 检查是否被锁定
val lockoutEndTime = loginLockouts[ipAddress]
if (lockoutEndTime != null) {
if (now < lockoutEndTime) {
val remainingSeconds = (lockoutEndTime - now) / 1000
logger.warn("登录锁定中: ip=$ipAddress, remainingSeconds=$remainingSeconds")
return Result.failure(IllegalStateException("账户已被锁定,请${remainingSeconds}秒后再试"))
} else {
// 锁定已过期,清除锁定记录
loginLockouts.remove(ipAddress)
loginFailedAttempts.remove(ipAddress)
}
}
return Result.success(Unit)
}
/**
* 记录登录失败尝试
* @param ipAddress 客户端IP地址
* @return 如果触发锁定返回锁定信息否则返回 null
*/
fun recordLoginFailure(ipAddress: String): String? {
val now = System.currentTimeMillis()
val windowStart = now - (loginWindowSeconds * 1000)
// 获取或创建该IP的尝试记录
val attempts = loginFailedAttempts.computeIfAbsent(ipAddress) { mutableListOf() }
// 清理过期记录并添加新记录
synchronized(attempts) {
attempts.removeIf { it < windowStart }
attempts.add(now)
// 检查是否需要锁定
if (attempts.size >= loginMaxAttempts) {
val lockoutEndTime = now + (loginLockoutSeconds * 1000)
loginLockouts[ipAddress] = lockoutEndTime
logger.warn("登录锁定触发: ip=$ipAddress, attempts=${attempts.size}, lockoutSeconds=$loginLockoutSeconds")
return "登录失败次数过多,账户已被锁定${loginLockoutSeconds / 60}分钟"
}
}
val remainingAttempts = loginMaxAttempts - attempts.size
logger.warn("登录失败: ip=$ipAddress, attempts=${attempts.size}/$loginMaxAttempts, remainingAttempts=$remainingAttempts")
return null
}
/**
* 登录成功时清除失败记录
* @param ipAddress 客户端IP地址
*/
fun clearLoginFailures(ipAddress: String) {
loginFailedAttempts.remove(ipAddress)
loginLockouts.remove(ipAddress)
}
}
@@ -8,6 +8,7 @@ import com.wrbug.polymarketbot.util.multi
import com.wrbug.polymarketbot.util.toSafeBigDecimal
import org.slf4j.LoggerFactory
import com.wrbug.polymarketbot.service.common.PolymarketClobService
import com.wrbug.polymarketbot.service.accounts.AccountService
import org.springframework.stereotype.Service
import java.math.BigDecimal
@@ -16,7 +17,8 @@ import java.math.BigDecimal
*/
@Service
class CopyTradingFilterService(
private val clobService: PolymarketClobService
private val clobService: PolymarketClobService,
private val accountService: AccountService
) {
private val logger = LoggerFactory.getLogger(CopyTradingFilterService::class.java)
@@ -26,12 +28,16 @@ class CopyTradingFilterService(
* @param copyTrading 跟单配置
* @param tokenId token ID用于获取订单簿
* @param tradePrice Leader 交易价格用于价格区间检查
* @param copyOrderAmount 跟单金额USDC用于仓位检查如果为null则不进行仓位检查
* @param marketId 市场ID用于仓位检查按市场过滤仓位
* @return 过滤结果
*/
suspend fun checkFilters(
copyTrading: CopyTrading,
tokenId: String,
tradePrice: BigDecimal? = null // Leader 交易价格,用于价格区间检查
tradePrice: BigDecimal? = null, // Leader 交易价格,用于价格区间检查
copyOrderAmount: BigDecimal? = null, // 跟单金额(USDC),用于仓位检查
marketId: String? = null // 市场ID,用于仓位检查(按市场过滤仓位)
): FilterResult {
// 1. 价格区间检查(如果配置了价格区间)
if (tradePrice != null) {
@@ -76,6 +82,14 @@ class CopyTradingFilterService(
}
}
// 6. 仓位检查(如果配置了最大仓位限制且提供了跟单金额和市场ID)
if (copyOrderAmount != null && marketId != null) {
val positionCheck = checkPositionLimits(copyTrading, copyOrderAmount, marketId)
if (!positionCheck.isPassed) {
return positionCheck
}
}
return FilterResult.passed(orderbook)
}
@@ -184,5 +198,78 @@ class CopyTradingFilterService(
return FilterResult.passed()
}
/**
* 检查仓位限制按市场检查
* @param copyTrading 跟单配置
* @param copyOrderAmount 跟单金额USDC
* @param marketId 市场ID用于过滤该市场的仓位
* @return 过滤结果
*/
private suspend fun checkPositionLimits(
copyTrading: CopyTrading,
copyOrderAmount: BigDecimal,
marketId: String
): FilterResult {
// 如果未配置仓位限制,直接通过
if (copyTrading.maxPositionValue == null && copyTrading.maxPositionCount == null) {
return FilterResult.passed()
}
try {
// 获取账户的所有仓位信息
val positionsResult = accountService.getAllPositions()
if (positionsResult.isFailure) {
logger.warn("获取仓位信息失败,跳过仓位检查: accountId=${copyTrading.accountId}, marketId=$marketId, error=${positionsResult.exceptionOrNull()?.message}")
// 如果获取仓位失败,为了安全起见,不通过检查
return FilterResult.maxPositionValueFailed("获取仓位信息失败,无法进行仓位检查")
}
val positions = positionsResult.getOrNull() ?: return FilterResult.maxPositionValueFailed("仓位信息为空")
// 过滤出当前账户且该市场的仓位
val marketPositions = positions.currentPositions.filter {
it.accountId == copyTrading.accountId && it.marketId == marketId
}
// 检查最大仓位金额(如果配置了)
if (copyTrading.maxPositionValue != null) {
// 计算该市场的当前仓位总价值(累加该市场所有仓位的 currentValue
val currentPositionValue = marketPositions.sumOf { position ->
position.currentValue.toSafeBigDecimal()
}
// 检查:该市场的当前仓位 + 跟单金额 <= 最大仓位金额
val totalValueAfterOrder = currentPositionValue.add(copyOrderAmount)
if (totalValueAfterOrder.gt(copyTrading.maxPositionValue)) {
return FilterResult.maxPositionValueFailed(
"超过最大仓位金额限制: 当前该市场仓位=${currentPositionValue} USDC, 跟单金额=${copyOrderAmount} USDC, 总计=${totalValueAfterOrder} USDC > 最大限制=${copyTrading.maxPositionValue} USDC"
)
}
}
// 检查最大仓位数量(如果配置了)
if (copyTrading.maxPositionCount != null) {
// 计算该市场的当前仓位数量(该市场不同方向的仓位算不同仓位)
val currentPositionCount = marketPositions.size
// 检查:该市场的当前仓位数量 <= 最大仓位数量
// 注意:如果该市场已有仓位,跟单可能会增加新的仓位(不同方向)或增加现有仓位
// 为了简化,我们检查当前该市场的仓位数量是否已经达到或超过限制
if (currentPositionCount >= copyTrading.maxPositionCount) {
return FilterResult.maxPositionCountFailed(
"超过最大仓位数量限制: 当前该市场仓位数量=${currentPositionCount} >= 最大限制=${copyTrading.maxPositionCount}"
)
}
}
return FilterResult.passed()
} catch (e: Exception) {
logger.error("仓位检查异常: accountId=${copyTrading.accountId}, marketId=$marketId, error=${e.message}", e)
// 如果检查异常,为了安全起见,不通过检查
return FilterResult.maxPositionValueFailed("仓位检查异常: ${e.message}")
}
}
}
@@ -86,7 +86,9 @@ class CopyTradingService(
minOrderDepth = request.minOrderDepth?.toSafeBigDecimal() ?: template.minOrderDepth,
maxSpread = request.maxSpread?.toSafeBigDecimal() ?: template.maxSpread,
minPrice = request.minPrice?.toSafeBigDecimal() ?: template.minPrice,
maxPrice = request.maxPrice?.toSafeBigDecimal() ?: template.maxPrice
maxPrice = request.maxPrice?.toSafeBigDecimal() ?: template.maxPrice,
maxPositionValue = request.maxPositionValue?.toSafeBigDecimal(),
maxPositionCount = request.maxPositionCount
)
} else {
// 手动输入(所有字段必须提供)
@@ -112,7 +114,9 @@ class CopyTradingService(
minOrderDepth = request.minOrderDepth?.toSafeBigDecimal(),
maxSpread = request.maxSpread?.toSafeBigDecimal(),
minPrice = request.minPrice?.toSafeBigDecimal(),
maxPrice = request.maxPrice?.toSafeBigDecimal()
maxPrice = request.maxPrice?.toSafeBigDecimal(),
maxPositionValue = request.maxPositionValue?.toSafeBigDecimal(),
maxPositionCount = request.maxPositionCount
)
}
@@ -139,19 +143,22 @@ class CopyTradingService(
maxSpread = config.maxSpread,
minPrice = config.minPrice,
maxPrice = config.maxPrice,
maxPositionValue = config.maxPositionValue,
maxPositionCount = config.maxPositionCount,
configName = configName,
pushFailedOrders = request.pushFailedOrders ?: false
)
val saved = copyTradingRepository.save(copyTrading)
// 如果跟单已启用,重新启动监听(确保状态完全同步
// 如果跟单已启用,更新 Leader 监听和账户监听(增量更新,不重启所有监听
if (saved.enabled) {
kotlinx.coroutines.runBlocking {
try {
monitorService.restartMonitoring()
monitorService.updateLeaderMonitoring(saved.leaderId)
monitorService.updateAccountMonitoring(saved.accountId)
} catch (e: Exception) {
logger.error("重新启动跟单监听失败", e)
logger.error("更新监听失败", e)
}
}
}
@@ -204,6 +211,8 @@ class CopyTradingService(
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,
updatedAt = System.currentTimeMillis()
@@ -211,12 +220,13 @@ class CopyTradingService(
val saved = copyTradingRepository.save(updated)
// 重新启动监听(确保状态完全同步
// 更新 Leader 监听和账户监听(增量更新,根据 enabled 状态决定添加或移除
kotlinx.coroutines.runBlocking {
try {
monitorService.restartMonitoring()
monitorService.updateLeaderMonitoring(saved.leaderId)
monitorService.updateAccountMonitoring(saved.accountId)
} catch (e: Exception) {
logger.error("重新启动跟单监听失败", e)
logger.error("更新监听失败", e)
}
}
@@ -314,14 +324,17 @@ class CopyTradingService(
val copyTrading = copyTradingRepository.findById(copyTradingId).orElse(null)
?: return Result.failure(IllegalArgumentException("跟单配置不存在"))
val leaderId = copyTrading.leaderId
val accountId = copyTrading.accountId
copyTradingRepository.delete(copyTrading)
// 重新启动监听(确保状态完全同步
// 更新 Leader 监听和账户监听(检查是否还有其他启用的跟单配置
kotlinx.coroutines.runBlocking {
try {
monitorService.restartMonitoring()
monitorService.removeLeaderMonitoring(leaderId)
monitorService.updateAccountMonitoring(accountId)
} catch (e: Exception) {
logger.error("重新启动跟单监听失败", e)
logger.error("更新监听失败", e)
}
}
@@ -409,6 +422,8 @@ class CopyTradingService(
maxSpread = copyTrading.maxSpread?.toPlainString(),
minPrice = copyTrading.minPrice?.toPlainString(),
maxPrice = copyTrading.maxPrice?.toPlainString(),
maxPositionValue = copyTrading.maxPositionValue?.toPlainString(),
maxPositionCount = copyTrading.maxPositionCount,
configName = copyTrading.configName,
pushFailedOrders = copyTrading.pushFailedOrders,
createdAt = copyTrading.createdAt,
@@ -437,6 +452,8 @@ class CopyTradingService(
val minOrderDepth: BigDecimal?,
val maxSpread: BigDecimal?,
val minPrice: BigDecimal?,
val maxPrice: BigDecimal?
val maxPrice: BigDecimal?,
val maxPositionValue: BigDecimal?,
val maxPositionCount: Int?
)
}
@@ -17,7 +17,11 @@ enum class FilterStatus {
/** 失败:价差过大 */
FAILED_SPREAD,
/** 失败:订单深度不足 */
FAILED_ORDER_DEPTH
FAILED_ORDER_DEPTH,
/** 失败:超过最大仓位金额 */
FAILED_MAX_POSITION_VALUE,
/** 失败:超过最大仓位数量 */
FAILED_MAX_POSITION_COUNT
}
/**
@@ -73,6 +77,18 @@ data class FilterResult(
reason = reason,
orderbook = orderbook
)
/** 超过最大仓位金额 */
fun maxPositionValueFailed(reason: String) = FilterResult(
status = FilterStatus.FAILED_MAX_POSITION_VALUE,
reason = reason
)
/** 超过最大仓位数量 */
fun maxPositionCountFailed(reason: String) = FilterResult(
status = FilterStatus.FAILED_MAX_POSITION_COUNT,
reason = reason
)
}
}
@@ -0,0 +1,341 @@
package com.wrbug.polymarketbot.service.copytrading.monitor
import com.wrbug.polymarketbot.api.*
import com.wrbug.polymarketbot.entity.Account
import com.wrbug.polymarketbot.entity.CopyOrderTracking
import com.wrbug.polymarketbot.entity.SellMatchDetail
import com.wrbug.polymarketbot.entity.SellMatchRecord
import com.wrbug.polymarketbot.repository.AccountRepository
import com.wrbug.polymarketbot.repository.CopyOrderTrackingRepository
import com.wrbug.polymarketbot.repository.CopyTradingRepository
import com.wrbug.polymarketbot.repository.SellMatchDetailRepository
import com.wrbug.polymarketbot.repository.SellMatchRecordRepository
import com.wrbug.polymarketbot.util.RetrofitFactory
import com.wrbug.polymarketbot.util.multi
import com.wrbug.polymarketbot.util.toSafeBigDecimal
import jakarta.annotation.PreDestroy
import kotlinx.coroutines.*
import okhttp3.OkHttpClient
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import java.math.BigDecimal
import java.util.concurrent.ConcurrentHashMap
/**
* 跟单账户链上 WebSocket 监听服务
* 通过统一服务订阅跟单账户的卖出和赎回事件
* 用于更新订单状态不再依赖轮询
*/
@Service
class AccountOnChainMonitorService(
private val unifiedOnChainWsService: UnifiedOnChainWsService,
private val retrofitFactory: RetrofitFactory,
private val accountRepository: AccountRepository,
private val copyTradingRepository: CopyTradingRepository,
private val copyOrderTrackingRepository: CopyOrderTrackingRepository,
private val sellMatchRecordRepository: SellMatchRecordRepository,
private val sellMatchDetailRepository: SellMatchDetailRepository
) {
private val logger = LoggerFactory.getLogger(AccountOnChainMonitorService::class.java)
// 存储需要监听的账户:accountId -> Account
private val monitoredAccounts = ConcurrentHashMap<Long, Account>()
/**
* 启动链上 WebSocket 监听
* 通过统一服务订阅所有跟单账户
*/
fun start(accounts: List<Account>) {
// 如果没有账户,取消所有订阅
if (accounts.isEmpty()) {
logger.info("没有需要监听的跟单账户,取消所有订阅")
stop()
return
}
// 更新账户列表
monitoredAccounts.clear()
accounts.forEach { account ->
addAccount(account)
}
}
/**
* 添加账户监听
* 通过统一服务订阅该账户的地址
*/
fun addAccount(account: Account) {
if (account.id == null) {
logger.warn("账户 ID 为空,跳过: ${account.proxyAddress}")
return
}
val accountId = account.id!!
// 如果已经在监听列表中,不重复添加
if (monitoredAccounts.containsKey(accountId)) {
return
}
monitoredAccounts[accountId] = account
// 通过统一服务订阅
val subscriptionId = "ACCOUNT_$accountId"
unifiedOnChainWsService.subscribe(
subscriptionId = subscriptionId,
address = account.proxyAddress,
entityType = "ACCOUNT",
entityId = accountId,
callback = { txHash, httpClient, rpcApi ->
handleAccountTransaction(accountId, txHash, httpClient, rpcApi)
}
)
logger.info("已添加跟单账户进行链上监听: accountId=${accountId}, address=${account.proxyAddress}")
}
/**
* 处理账户的交易
*/
private suspend fun handleAccountTransaction(accountId: Long, txHash: String, httpClient: OkHttpClient, rpcApi: EthereumRpcApi) {
val account = monitoredAccounts[accountId] ?: return
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
// 获取区块号和时间戳
val blockNumber = receiptJson.get("blockNumber")?.asString
val blockTimestamp = if (blockNumber != null) {
OnChainWsUtils.getBlockTimestamp(blockNumber, rpcApi)
} else {
null
}
// 解析 receipt 中的 Transfer 日志
val logs = receiptJson.getAsJsonArray("logs") ?: return
val (erc20Transfers, erc1155Transfers) = OnChainWsUtils.parseReceiptTransfers(logs)
// 解析交易信息
val trade = OnChainWsUtils.parseTradeFromTransfers(
txHash = txHash,
timestamp = blockTimestamp,
walletAddress = account.proxyAddress,
erc20Transfers = erc20Transfers,
erc1155Transfers = erc1155Transfers,
retrofitFactory = retrofitFactory
)
if (trade != null && trade.side == "SELL") {
// 检测到卖出或赎回事件,更新订单状态
handleAccountSellOrRedeem(account, trade)
}
} catch (e: Exception) {
logger.error("处理账户交易失败: accountId=$accountId, txHash=$txHash, ${e.message}", e)
}
}
/**
* 处理账户的卖出或赎回事件
* 更新对应的订单状态
*/
private suspend fun handleAccountSellOrRedeem(account: Account, trade: TradeResponse) {
try {
// 获取该账户的所有启用的跟单配置
val copyTradings = copyTradingRepository.findByAccountId(account.id!!)
.filter { it.enabled }
if (copyTradings.isEmpty()) {
return
}
// 使用 trade 中已有的市场信息
val marketId = trade.market // conditionId
val outcomeIndex = trade.outcomeIndex ?: 0
// 计算卖出价格
val sellPrice = trade.price.toSafeBigDecimal()
// 为每个跟单配置更新订单状态
for (copyTrading in copyTradings) {
// 查找该跟单配置下所有未卖出的订单(remaining_quantity > 0
val unmatchedOrders = copyOrderTrackingRepository.findByCopyTradingId(copyTrading.id!!)
.filter {
it.remainingQuantity > BigDecimal.ZERO &&
it.marketId == marketId &&
it.outcomeIndex == outcomeIndex
}
.sortedBy { it.createdAt } // 按创建时间排序(FIFO
if (unmatchedOrders.isEmpty()) {
continue
}
// 卖出数量就是交易的 size
val soldQuantity = trade.size.toSafeBigDecimal()
// 更新订单状态为已卖出
updateOrdersAsSoldByFIFO(
unmatchedOrders,
soldQuantity,
sellPrice,
copyTrading.id!!,
marketId,
outcomeIndex
)
logger.info("跟单账户卖出/赎回事件处理完成: accountId=${account.id}, copyTradingId=${copyTrading.id}, txHash=${trade.id}, soldQuantity=$soldQuantity, sellPrice=$sellPrice")
}
} catch (e: Exception) {
logger.error("处理账户卖出/赎回事件失败: accountId=${account.id}, txHash=${trade.id}, error=${e.message}", e)
}
}
/**
* FIFO 顺序更新订单为已卖出
*/
private suspend fun updateOrdersAsSoldByFIFO(
orders: List<CopyOrderTracking>,
soldQuantity: BigDecimal,
sellPrice: BigDecimal,
copyTradingId: Long,
marketId: String,
outcomeIndex: Int
) {
var remainingSoldQuantity = soldQuantity
val matchDetails = mutableListOf<SellMatchDetail>()
var totalMatchedQuantity = BigDecimal.ZERO
var totalRealizedPnl = BigDecimal.ZERO
for (order in orders) {
if (remainingSoldQuantity <= BigDecimal.ZERO) {
break
}
val currentOrderRemaining = order.remainingQuantity.toSafeBigDecimal()
val matchedQty = minOf(currentOrderRemaining, remainingSoldQuantity)
if (matchedQty <= BigDecimal.ZERO) {
continue
}
// 计算盈亏
val buyPrice = order.price.toSafeBigDecimal()
val realizedPnl = sellPrice.subtract(buyPrice).multi(matchedQty)
// 创建匹配明细
val detail = SellMatchDetail(
matchRecordId = 0, // 稍后设置
trackingId = order.id!!,
buyOrderId = order.buyOrderId,
matchedQuantity = matchedQty,
buyPrice = buyPrice,
sellPrice = sellPrice,
realizedPnl = realizedPnl
)
matchDetails.add(detail)
totalMatchedQuantity = totalMatchedQuantity.add(matchedQty)
totalRealizedPnl = totalRealizedPnl.add(realizedPnl)
// 更新订单状态
order.matchedQuantity = order.matchedQuantity.add(matchedQty)
order.remainingQuantity = currentOrderRemaining.subtract(matchedQty)
order.status = if (order.remainingQuantity <= BigDecimal.ZERO) "fully_matched" else "partially_matched"
order.updatedAt = System.currentTimeMillis()
copyOrderTrackingRepository.save(order)
remainingSoldQuantity = remainingSoldQuantity.subtract(matchedQty)
}
// 如果有匹配的订单,创建卖出记录
if (totalMatchedQuantity > BigDecimal.ZERO && matchDetails.isNotEmpty()) {
val timestamp = System.currentTimeMillis()
val sellOrderId = "AUTO_WS_${timestamp}_${copyTradingId}" // 区分 WS 自动卖出
val leaderSellTradeId = "AUTO_WS_${timestamp}"
val matchRecord = SellMatchRecord(
copyTradingId = copyTradingId,
sellOrderId = sellOrderId,
leaderSellTradeId = leaderSellTradeId,
marketId = marketId,
side = outcomeIndex.toString(),
outcomeIndex = outcomeIndex,
totalMatchedQuantity = totalMatchedQuantity,
sellPrice = sellPrice,
totalRealizedPnl = totalRealizedPnl,
priceUpdated = true // WS 实时获取,直接标记为已更新
)
val savedRecord = sellMatchRecordRepository.save(matchRecord)
// 保存匹配明细
for (detail in matchDetails) {
val savedDetail = detail.copy(matchRecordId = savedRecord.id!!)
sellMatchDetailRepository.save(savedDetail)
}
logger.info("创建跟单账户链上自动卖出记录: copyTradingId=$copyTradingId, marketId=$marketId, totalMatched=$totalMatchedQuantity, totalPnl=$totalRealizedPnl")
}
}
/**
* 移除账户监听
* 取消该账户的订阅
*/
fun removeAccount(accountId: Long) {
monitoredAccounts.remove(accountId)
// 通过统一服务取消订阅
val subscriptionId = "ACCOUNT_$accountId"
unifiedOnChainWsService.unsubscribe(subscriptionId)
logger.info("已移除跟单账户的链上监听: accountId=$accountId")
}
/**
* 更新账户监听状态
*/
fun updateAccountMonitoring(accountId: Long) {
val account = accountRepository.findById(accountId).orElse(null)
if (account != null && account.isEnabled) {
addAccount(account)
} else {
removeAccount(accountId)
}
}
/**
* 停止监听
*/
fun stop() {
// 取消所有账户的订阅
val accountIds = monitoredAccounts.keys.toList()
for (accountId in accountIds) {
removeAccount(accountId)
}
monitoredAccounts.clear()
}
@PreDestroy
fun destroy() {
stop()
}
}
@@ -2,6 +2,7 @@ package com.wrbug.polymarketbot.service.copytrading.monitor
import com.wrbug.polymarketbot.entity.CopyTrading
import com.wrbug.polymarketbot.entity.Leader
import com.wrbug.polymarketbot.repository.AccountRepository
import com.wrbug.polymarketbot.repository.CopyTradingRepository
import com.wrbug.polymarketbot.repository.LeaderRepository
import jakarta.annotation.PostConstruct
@@ -12,14 +13,18 @@ import org.springframework.stereotype.Service
/**
* 跟单监听服务主服务
* 管理所有Leader的交易监听使用轮询方式
* 注意WebSocket 需要认证才能订阅其他用户的交易因此只使用轮询方式
* 管理所有Leader的交易监听
* 同时运行链上 WebSocket 监听和轮询监听并行处理
* 同时监听跟单账户的卖出/赎回事件通过链上 WebSocket
*/
@Service
class CopyTradingMonitorService(
private val copyTradingRepository: CopyTradingRepository,
private val leaderRepository: LeaderRepository,
private val pollingService: CopyTradingPollingService
private val accountRepository: AccountRepository,
private val pollingService: CopyTradingPollingService,
private val onChainWsService: OnChainWsService,
private val accountOnChainMonitorService: AccountOnChainMonitorService
) {
private val logger = LoggerFactory.getLogger(CopyTradingMonitorService::class.java)
@@ -46,12 +51,16 @@ class CopyTradingMonitorService(
@PreDestroy
fun destroy() {
scope.cancel()
// 只使用轮询,不使用WebSocket
// 停止轮询和链上 WS 监听
pollingService.stop()
onChainWsService.stop()
accountOnChainMonitorService.stop()
}
/**
* 启动监听
* 同时启动链上 WebSocket 监听和轮询监听并行运行
* 同时启动跟单账户的链上 WebSocket 监听用于检测卖出/赎回事件
*/
suspend fun startMonitoring() {
// 1. 获取所有启用的跟单关系
@@ -67,14 +76,26 @@ class CopyTradingMonitorService(
leaderRepository.findById(leaderId).orElse(null)
}
// 3. 获取所有需要监听的跟单账户(去重)
val accountIds = enabledCopyTradings.map { it.accountId }.distinct()
val accounts = accountIds.mapNotNull { accountId ->
accountRepository.findById(accountId).orElse(null)
}
// 3. 启动轮询监听(使用 /activity 接口,不需要认证
// 注意:WebSocket 需要认证才能订阅其他用户的交易,因此禁用WebSocket,只使用轮询
// 4. 同时启动链上 WebSocket 监听和轮询监听(并行运行
// 链上 WS 监听 Leader 的交易(实时,秒级延迟)
onChainWsService.start(leaders)
// 轮询监听 Leader 的交易(延迟,2秒间隔,作为备份)
pollingService.start(leaders)
// 5. 启动跟单账户的链上 WebSocket 监听(用于检测卖出/赎回事件)
accountOnChainMonitorService.start(accounts)
}
/**
* 添加Leader监听当创建新的跟单关系时调用
* 如果 Leader 已经在监听列表中不重复添加
*/
suspend fun addLeaderMonitoring(leaderId: Long) {
val leader = leaderRepository.findById(leaderId).orElse(null)
@@ -85,28 +106,82 @@ class CopyTradingMonitorService(
return
}
// 只使用轮询,不使用WebSocket(需要认证
// 同时添加到链上 WS 监听和轮询监听(如果不在列表中才添加
onChainWsService.addLeader(leader)
pollingService.addLeader(leader)
}
/**
* 移除Leader监听当删除跟单关系时调用
* 移除Leader监听当删除跟单关系或禁用时调用
* 检查该 Leader 是否还有其他启用的跟单配置
*/
suspend fun removeLeaderMonitoring(leaderId: Long) {
val copyTradings = copyTradingRepository.findByLeaderIdAndEnabledTrue(leaderId)
// 如果还有启用的跟单配置,不移除监听
if (copyTradings.isNotEmpty()) {
return
}
// 只使用轮询,不使用WebSocket
// 没有启用的跟单配置了,移除监听
onChainWsService.removeLeader(leaderId)
pollingService.removeLeader(leaderId)
}
/**
* 更新Leader监听当跟单配置状态改变时调用
* 根据当前状态决定添加或移除监听
*/
suspend fun updateLeaderMonitoring(leaderId: Long) {
val copyTradings = copyTradingRepository.findByLeaderIdAndEnabledTrue(leaderId)
val leader = leaderRepository.findById(leaderId).orElse(null)
?: return
if (copyTradings.isNotEmpty()) {
// 有启用的跟单配置,确保在监听列表中
onChainWsService.addLeader(leader)
pollingService.addLeader(leader)
// 更新账户监听(添加该配置关联的账户)
val accountIds = copyTradings.map { it.accountId }.distinct()
accountIds.forEach { accountId ->
val account = accountRepository.findById(accountId).orElse(null)
if (account != null) {
accountOnChainMonitorService.addAccount(account)
}
}
} else {
// 没有启用的跟单配置,移除监听
onChainWsService.removeLeader(leaderId)
pollingService.removeLeader(leaderId)
}
}
/**
* 更新账户监听当跟单配置状态改变时调用
* 根据当前状态决定添加或移除账户监听
*/
suspend fun updateAccountMonitoring(accountId: Long) {
val copyTradings = copyTradingRepository.findByAccountId(accountId)
.filter { it.enabled }
val account = accountRepository.findById(accountId).orElse(null)
?: return
if (copyTradings.isNotEmpty()) {
// 有启用的跟单配置,确保账户在监听列表中
accountOnChainMonitorService.addAccount(account)
} else {
// 没有启用的跟单配置,移除账户监听
accountOnChainMonitorService.removeAccount(accountId)
}
}
/**
* 重新启动监听当跟单关系状态改变时调用
* 注意这个方法会停止所有监听并重新启动建议使用 updateLeaderMonitoring 进行增量更新
*/
suspend fun restartMonitoring() {
// 只使用轮询,不使用WebSocket
// 停止所有监听
onChainWsService.stop()
pollingService.stop()
delay(1000) // 等待1秒
startMonitoring()
@@ -22,15 +22,14 @@ import java.util.concurrent.ConcurrentHashMap
@Service
class CopyTradingWebSocketService(
private val copyOrderTrackingService: CopyOrderTrackingService,
private val templateRepository: CopyTradingTemplateRepository
private val templateRepository: CopyTradingTemplateRepository,
private val gson: Gson
) {
private val logger = LoggerFactory.getLogger(CopyTradingWebSocketService::class.java)
@Value("\${polymarket.websocket.url:wss://ws-live-data.polymarket.com}")
private var websocketUrl: String = "wss://ws-live-data.polymarket.com"
private val gson = Gson()
private val scope = CoroutineScope(Dispatchers.Default + SupervisorJob())
// 存储每个Leader的WebSocket客户端:leaderId -> WebSocketClient
@@ -0,0 +1,177 @@
package com.wrbug.polymarketbot.service.copytrading.monitor
import com.wrbug.polymarketbot.api.*
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.RetrofitFactory
import jakarta.annotation.PreDestroy
import kotlinx.coroutines.*
import okhttp3.OkHttpClient
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import java.util.concurrent.ConcurrentHashMap
/**
* 链上 WebSocket 监听服务
* 通过统一服务订阅 Leader 的链上交易
*/
@Service
class OnChainWsService(
private val unifiedOnChainWsService: UnifiedOnChainWsService,
private val retrofitFactory: RetrofitFactory,
private val copyOrderTrackingService: CopyOrderTrackingService,
private val leaderRepository: LeaderRepository
) {
private val logger = LoggerFactory.getLogger(OnChainWsService::class.java)
// 存储需要监听的LeaderleaderId -> Leader
private val monitoredLeaders = ConcurrentHashMap<Long, Leader>()
/**
* 启动链上 WebSocket 监听
* 通过统一服务订阅所有 Leader
*/
fun start(leaders: List<Leader>) {
// 如果没有 Leader,取消所有订阅
if (leaders.isEmpty()) {
logger.info("没有需要监听的 Leader,取消所有订阅")
stop()
return
}
// 更新 Leader 列表
monitoredLeaders.clear()
leaders.forEach { leader ->
addLeader(leader)
}
}
/**
* 添加Leader监听
* 通过统一服务订阅该 Leader 的地址
*/
fun addLeader(leader: Leader) {
if (leader.id == null) {
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(
subscriptionId = subscriptionId,
address = leader.leaderAddress,
entityType = "LEADER",
entityId = leaderId,
callback = { txHash, httpClient, rpcApi ->
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) {
val leader = monitoredLeaders[leaderId] ?: return
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
// 获取区块号和时间戳
val blockNumber = receiptJson.get("blockNumber")?.asString
val blockTimestamp = if (blockNumber != null) {
OnChainWsUtils.getBlockTimestamp(blockNumber, rpcApi)
} else {
null
}
// 解析 receipt 中的 Transfer 日志
val logs = receiptJson.getAsJsonArray("logs") ?: return
val (erc20Transfers, erc1155Transfers) = OnChainWsUtils.parseReceiptTransfers(logs)
// 解析交易信息
val trade = OnChainWsUtils.parseTradeFromTransfers(
txHash = txHash,
timestamp = blockTimestamp,
walletAddress = leader.leaderAddress,
erc20Transfers = erc20Transfers,
erc1155Transfers = erc1155Transfers,
retrofitFactory = retrofitFactory
)
if (trade != null) {
// 调用 processTrade 处理交易
copyOrderTrackingService.processTrade(
leaderId = leaderId,
trade = trade,
source = "onchain-ws"
)
}
} 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")
}
/**
* 停止监听
*/
fun stop() {
// 取消所有 Leader 的订阅
val leaderIds = monitoredLeaders.keys.toList()
for (leaderId in leaderIds) {
removeLeader(leaderId)
}
monitoredLeaders.clear()
}
@PreDestroy
fun destroy() {
stop()
}
}
@@ -0,0 +1,377 @@
package com.wrbug.polymarketbot.service.copytrading.monitor
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonArray
import com.google.gson.reflect.TypeToken
import com.wrbug.polymarketbot.api.*
import com.wrbug.polymarketbot.service.system.RpcNodeService
import com.wrbug.polymarketbot.util.RetrofitFactory
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import org.slf4j.LoggerFactory
import java.math.BigInteger
/**
* 链上 WebSocket 工具类
* 提取公共的交易解析工具函数等逻辑
*/
object OnChainWsUtils {
private val logger = LoggerFactory.getLogger(OnChainWsUtils::class.java)
// 创建 Gson 实例(与 GsonConfig 中的配置一致,使用 lenient 模式)
private val gson: Gson = GsonBuilder()
.setLenient()
.create()
/**
* 解析 JSON 字符串数组
* @param jsonString JSON 字符串 "[\"Yes\", \"No\"]"
* @return 字符串列表如果解析失败返回空列表
*/
private fun parseStringArray(jsonString: String?): List<String> {
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()
}
}
// 合约地址
const val USDC_CONTRACT = "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174"
const val ERC1155_CONTRACT = "0x4d97dcd97ec945f40cf65f87097ace5ea0476045"
const val ERC20_TRANSFER_TOPIC = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
const val ERC1155_TRANSFER_SINGLE_TOPIC = "0xc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f62"
const val ERC1155_TRANSFER_BATCH_TOPIC = "0x4a39dc06d4c0dbc64b70af90fd698a233a518aa5d07e595d983b8c0526c8f7fb"
/**
* ERC20 Transfer 数据类
*/
data class Erc20Transfer(
val from: String,
val to: String,
val value: BigInteger
)
/**
* ERC1155 Transfer 数据类
*/
data class Erc1155Transfer(
val from: String,
val to: String,
val tokenId: BigInteger,
val value: BigInteger
)
/**
* 市场信息数据类
*/
data class MarketInfo(
val conditionId: String,
val outcomeIndex: Int?, // 可空,因为可能找不到对应的 tokenId
val outcome: String?
)
/**
* 解析 receipt 中的 Transfer 日志
*/
fun parseReceiptTransfers(logs: JsonArray): Pair<List<Erc20Transfer>, List<Erc1155Transfer>> {
val erc20 = mutableListOf<Erc20Transfer>()
val erc1155 = mutableListOf<Erc1155Transfer>()
for (logElement in logs) {
val log = logElement.asJsonObject
val address = log.get("address")?.asString?.lowercase() ?: continue
val topicsArray = log.getAsJsonArray("topics") ?: continue
val topics = topicsArray.mapNotNull { it.asString }
if (topics.isEmpty()) continue
val t0 = topics[0].lowercase()
val data = log.get("data")?.asString ?: "0x"
// USDC ERC20 Transfer
if (address == USDC_CONTRACT.lowercase() && t0 == ERC20_TRANSFER_TOPIC && topics.size >= 3) {
val from = topicToAddress(topics[1])
val to = topicToAddress(topics[2])
val value = hexToBigInt(data)
erc20.add(Erc20Transfer(from, to, value))
continue
}
// ERC1155 TransferSingle
if (t0 == ERC1155_TRANSFER_SINGLE_TOPIC && topics.size >= 4) {
val from = topicToAddress(topics[2])
val to = topicToAddress(topics[3])
val bytes = bytesFromHex(data)
if (bytes.size >= 64) {
val tokenId = sliceBigInt32(bytes, 0)
val value = sliceBigInt32(bytes, 32)
erc1155.add(Erc1155Transfer(from, to, tokenId, value))
}
continue
}
// ERC1155 TransferBatch
if (t0 == ERC1155_TRANSFER_BATCH_TOPIC && topics.size >= 4) {
val from = topicToAddress(topics[2])
val to = topicToAddress(topics[3])
val bytes = bytesFromHex(data)
if (bytes.size < 64) continue
val offIds = sliceBigInt32(bytes, 0).toInt()
val offVals = sliceBigInt32(bytes, 32).toInt()
if (offIds + 32 > bytes.size || offVals + 32 > bytes.size) continue
val nIds = sliceBigInt32(bytes, offIds).toInt()
val nVals = sliceBigInt32(bytes, offVals).toInt()
if (nIds != nVals) continue
val idsStart = offIds + 32
val valsStart = offVals + 32
for (i in 0 until nIds) {
val ib = idsStart + i * 32
val vb = valsStart + i * 32
if (ib + 32 > bytes.size || vb + 32 > bytes.size) break
val tokenId = sliceBigInt32(bytes, ib)
val value = sliceBigInt32(bytes, vb)
erc1155.add(Erc1155Transfer(from, to, tokenId, value))
}
}
}
return Pair(erc20, erc1155)
}
/**
* Transfer 日志解析交易信息
*/
suspend fun parseTradeFromTransfers(
txHash: String,
timestamp: Long?,
walletAddress: String,
erc20Transfers: List<Erc20Transfer>,
erc1155Transfers: List<Erc1155Transfer>,
retrofitFactory: RetrofitFactory
): TradeResponse? {
val wallet = walletAddress.lowercase()
// 计算 USDC 流入和流出
val usdcOut = erc20Transfers.filter { it.from.lowercase() == wallet }
.fold(BigInteger.ZERO) { acc, t -> acc + t.value }
val usdcIn = erc20Transfers.filter { it.to.lowercase() == wallet }
.fold(BigInteger.ZERO) { acc, t -> acc + t.value }
// 计算 ERC1155 流入和流出(按 tokenId 聚合)
val inById = mutableMapOf<BigInteger, BigInteger>()
val outById = mutableMapOf<BigInteger, BigInteger>()
for (t in erc1155Transfers) {
if (t.to.lowercase() == wallet) {
inById[t.tokenId] = (inById[t.tokenId] ?: BigInteger.ZERO) + t.value
}
if (t.from.lowercase() == wallet) {
outById[t.tokenId] = (outById[t.tokenId] ?: BigInteger.ZERO) + t.value
}
}
// 找到最大的流入和流出 tokenId
fun best(map: Map<BigInteger, BigInteger>): Pair<BigInteger?, BigInteger> =
map.entries.maxByOrNull { it.value }?.let { it.key to it.value } ?: (null to BigInteger.ZERO)
val (bestInId, bestInVal) = best(inById)
val (bestOutId, bestOutVal) = best(outById)
// 判断交易方向
var side: String? = null
var asset: BigInteger? = null
var sizeRaw = BigInteger.ZERO
var usdcRaw = BigInteger.ZERO
if (bestInId != null && bestInVal > BigInteger.ZERO && usdcOut > BigInteger.ZERO) {
// BUY: 收到 token,支付 USDC
side = "BUY"
asset = bestInId
sizeRaw = bestInVal
usdcRaw = usdcOut
} else if (bestOutId != null && bestOutVal > BigInteger.ZERO && usdcIn > BigInteger.ZERO) {
// SELL: 卖出 token,收到 USDC
side = "SELL"
asset = bestOutId
sizeRaw = bestOutVal
usdcRaw = usdcIn
} else {
// 无法判断交易方向
return null
}
// 计算价格和数量(USDC 有 6 位小数,shares 也有 6 位小数)
val usdcSize = usdcRaw.toBigDecimal().divide(BigInteger("1000000").toBigDecimal(), 8, java.math.RoundingMode.DOWN)
val size = sizeRaw.toBigDecimal().divide(BigInteger("1000000").toBigDecimal(), 8, java.math.RoundingMode.DOWN)
val price = if (size.signum() > 0) {
usdcSize.divide(size, 8, java.math.RoundingMode.DOWN)
} else {
return null
}
// 尝试通过 Gamma API 查询市场信息(通过 tokenId)
val marketInfo = fetchMarketByTokenId(asset.toString(), retrofitFactory)
// 创建 TradeResponse
return TradeResponse(
id = txHash,
market = marketInfo?.conditionId ?: "",
side = side,
price = price.toPlainString(),
size = size.toPlainString(),
timestamp = (timestamp ?: System.currentTimeMillis() / 1000).toString(),
user = walletAddress,
outcomeIndex = marketInfo?.outcomeIndex,
outcome = marketInfo?.outcome
)
}
/**
* 通过 Gamma API 查询市场信息通过 tokenId
* 使用 Retrofit 接口支持 clob_token_ids 参数
*/
suspend fun fetchMarketByTokenId(tokenId: String, retrofitFactory: RetrofitFactory): MarketInfo? {
return try {
val gammaApi = retrofitFactory.createGammaApi()
val marketsResponse = gammaApi.listMarkets(
conditionIds = null,
clobTokenIds = listOf(tokenId),
includeTag = null
)
if (!marketsResponse.isSuccessful || marketsResponse.body() == null) {
return null
}
val markets = marketsResponse.body()!!
val market = markets.firstOrNull()
if (market == null) {
return null
}
// 解析 clobTokenIds(可能是 JSON 字符串或数组)
val clobTokenIdsRaw = market.clobTokenIds ?: market.clob_token_ids
val clobTokenIds = when {
clobTokenIdsRaw == null -> null
else -> {
// 解析 JSON 字符串
parseStringArray(clobTokenIdsRaw)
}
}
// 解析 outcomes(可能是 JSON 字符串或数组)
val outcomes = parseStringArray(market.outcomes)
// 查找 tokenId 在 clobTokenIds 中的索引
val outcomeIndex = clobTokenIds?.indexOfFirst { token ->
token.equals(tokenId, ignoreCase = true)
}?.takeIf { it >= 0 }
// 获取 outcome 名称
val outcome = if (outcomeIndex != null && outcomes.isNotEmpty() && outcomeIndex < outcomes.size) {
outcomes[outcomeIndex]
} else {
null
}
val conditionId = market.conditionId ?: return null
MarketInfo(
conditionId = conditionId,
outcomeIndex = outcomeIndex,
outcome = outcome
)
} catch (e: Exception) {
logger.warn("查询市场信息失败: tokenId=$tokenId, error=${e.message}")
null
}
}
/**
* 获取区块时间戳
*/
suspend fun getBlockTimestamp(blockNumber: String, rpcApi: EthereumRpcApi): Long? {
return try {
val blockRequest = JsonRpcRequest(
method = "eth_getBlockByNumber",
params = listOf(blockNumber, false)
)
val blockResponse = rpcApi.call(blockRequest)
if (blockResponse.isSuccessful && blockResponse.body() != null) {
val blockRpcResponse = blockResponse.body()!!
if (blockRpcResponse.error == null && blockRpcResponse.result != null) {
val blockJson = blockRpcResponse.result.asJsonObject
val timestampHex = blockJson.get("timestamp")?.asString
if (timestampHex != null) {
BigInteger(timestampHex.removePrefix("0x"), 16).toLong() * 1000 // 转换为毫秒
} else {
null
}
} else {
null
}
} else {
null
}
} catch (e: Exception) {
logger.warn("获取区块时间戳失败: blockNumber=$blockNumber, error=${e.message}")
null
}
}
/**
* 工具函数地址转 topic32字节左对齐
*/
fun addressToTopic32(address: String): String {
val clean = address.removePrefix("0x").lowercase()
return "0x" + clean.padStart(64, '0')
}
/**
* 工具函数topic 转地址
*/
fun topicToAddress(topic: String): String {
val clean = topic.removePrefix("0x").lowercase()
return "0x" + clean.takeLast(40)
}
/**
* 工具函数十六进制转 BigInteger
*/
fun hexToBigInt(hex: String): BigInteger {
val clean = hex.removePrefix("0x")
return if (clean.isBlank()) BigInteger.ZERO else BigInteger(clean, 16)
}
/**
* 工具函数十六进制转字节数组
*/
fun bytesFromHex(hex: String): ByteArray {
val clean = hex.removePrefix("0x")
return ByteArray(clean.length / 2) { i ->
clean.substring(i * 2, i * 2 + 2).toInt(16).toByte()
}
}
/**
* 工具函数从字节数组切片 BigInteger32字节
*/
fun sliceBigInt32(bytes: ByteArray, offset: Int): BigInteger {
if (offset + 32 > bytes.size) return BigInteger.ZERO
val slice = bytes.sliceArray(offset until offset + 32)
return BigInteger(1, slice)
}
}
@@ -0,0 +1,565 @@
package com.wrbug.polymarketbot.service.copytrading.monitor
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.wrbug.polymarketbot.api.*
import com.wrbug.polymarketbot.service.system.RpcNodeService
import com.wrbug.polymarketbot.util.RetrofitFactory
import com.wrbug.polymarketbot.util.createClient
import com.wrbug.polymarketbot.util.getProxyConfig
import jakarta.annotation.PostConstruct
import jakarta.annotation.PreDestroy
import kotlinx.coroutines.*
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.WebSocket
import okhttp3.WebSocketListener
import okio.ByteString
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import java.util.concurrent.ConcurrentHashMap
/**
* 统一的链上 WebSocket 服务
* 管理唯一的 WebSocket 连接其他服务通过订阅的方式接收链上事件
*/
@Service
class UnifiedOnChainWsService(
private val rpcNodeService: RpcNodeService,
private val retrofitFactory: RetrofitFactory,
private val gson: Gson
) {
private val logger = LoggerFactory.getLogger(UnifiedOnChainWsService::class.java)
@Value("\${copy.trading.onchain.ws.reconnect.delay:3000}")
private var reconnectDelay: Long = 3000 // 重连延迟(毫秒),默认3秒
private val scope = CoroutineScope(Dispatchers.Default + SupervisorJob())
// 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>()
/**
* 订阅信息
*/
data class SubscriptionInfo(
val subscriptionId: String, // 订阅的唯一标识
val address: String, // 要监听的地址(Leader 地址或账户代理地址)
val entityType: String, // 实体类型:LEADER 或 ACCOUNT
val entityId: Long, // 实体 IDLeader ID 或 Account ID
val callback: suspend (String, OkHttpClient, EthereumRpcApi) -> Unit // 回调函数
)
/**
* 订阅地址监听
* @param subscriptionId 订阅的唯一标识建议格式"{entityType}_{entityId}"
* @param address 要监听的地址Leader 地址或账户代理地址
* @param entityType 实体类型LEADER ACCOUNT
* @param entityId 实体 IDLeader ID Account ID
* @param callback 回调函数当检测到该地址的交易时调用
* @return 是否订阅成功
*/
fun subscribe(
subscriptionId: String,
address: String,
entityType: String,
entityId: Long,
callback: suspend (String, OkHttpClient, EthereumRpcApi) -> Unit
): Boolean {
try {
// 如果已经订阅,先取消
if (subscriptions.containsKey(subscriptionId)) {
unsubscribe(subscriptionId)
}
// 创建订阅信息
val subscription = SubscriptionInfo(
subscriptionId = subscriptionId,
address = address.lowercase(),
entityType = entityType,
entityId = entityId,
callback = callback
)
subscriptions[subscriptionId] = subscription
// 如果已连接,立即订阅
if (isConnected) {
scope.launch {
subscribeAddress(subscription)
}
} else {
// 如果未连接,启动连接
startConnection()
}
logger.info("订阅地址监听: subscriptionId=$subscriptionId, address=$address, entityType=$entityType, entityId=$entityId")
return true
} catch (e: Exception) {
logger.error("订阅地址监听失败: subscriptionId=$subscriptionId, address=$address, error=${e.message}", e)
return false
}
}
/**
* 取消订阅
*/
fun unsubscribe(subscriptionId: String) {
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 }
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
}
// 如果已经连接,等待断开
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() {
connectionJob?.cancel()
connectionJob = null
// 关闭 WebSocket 连接
webSocket?.close(1000, "停止监听")
webSocket = null
isConnected = false
// 清空订阅信息
subscriptions.clear()
requestIdToSubscriptionId.clear()
rpcSubscriptionIdToSubscriptionId.clear()
}
@PostConstruct
fun init() {
// 服务启动时不自动连接,等待有订阅时再连接
logger.info("统一链上 WebSocket 服务已初始化")
}
@PreDestroy
fun destroy() {
stop()
scope.cancel()
}
}
@@ -100,8 +100,9 @@ class OrderSigningService {
return OrderAmounts(makerAmount.toString(), takerAmount.toString())
} else {
// SELL: makerAmount = size (shares), takerAmount = price * size (USDC)
// makerAmount 是 shares 数量,最多 4 位小数
// takerAmount 是 USDC 金额,需要使用原始价格计算(与SDK保持一致)
// 根据 Polymarket API 要求:
// - makerAmount (shares) 最多 2 位小数
// - takerAmount (USDC) 最多 4 位小数
val rawMakerAmt = roundDown(sizeDecimal, roundConfig.size)
// takerAmount = price * size,使用原始价格计算(不使用舍入后的价格)
@@ -109,16 +110,15 @@ class OrderSigningService {
// 例如:0.9596 * 16.09 = 15.439964,而不是 0.96 * 16.09 = 15.4464
val rawTakerAmt = rawMakerAmt.multiply(priceDecimal)
// 确保 makerAmount 精度(shares,最多 4 位小数)
val finalMakerAmt = roundDown(rawMakerAmt, TAKER_AMOUNT_DECIMALS)
// 确保 makerAmount 精度(shares,最多 2 位小数,符合 API 要求
val finalMakerAmt = roundDown(rawMakerAmt, MAKER_AMOUNT_DECIMALS)
// takerAmount 不进行舍入,直接使用精确计算结果转换为 wei
// parseUnits 会将 BigDecimal 转换为 wei(6 位小数),自动处理精度
// 使用原始价格计算可以确保与SDK的结果一致
// 确保 takerAmount 精度(USDC,最多 4 位小数,符合 API 要求)
val finalTakerAmt = roundDown(rawTakerAmt, TAKER_AMOUNT_DECIMALS)
// 转换为 wei6 位小数)
val makerAmount = parseUnits(finalMakerAmt, COLLATERAL_TOKEN_DECIMALS)
val takerAmount = parseUnits(rawTakerAmt, COLLATERAL_TOKEN_DECIMALS)
val takerAmount = parseUnits(finalTakerAmt, COLLATERAL_TOKEN_DECIMALS)
return OrderAmounts(makerAmount.toString(), takerAmount.toString())
}
@@ -172,25 +172,15 @@ class OrderSigningService {
// 5. 确保 maker 地址也是小写格式
val makerAddressLower = makerAddress.lowercase()
// 打印签名前的订单参数
logger.info("========== 订单签名前参数 ==========")
logger.info("订单方向: $side")
logger.info("价格: $price")
logger.info("数量: $size")
logger.info("Token ID: $tokenId")
logger.info("Maker 地址: $makerAddressLower")
logger.info("Signer 地址: $signerAddress")
logger.info("Taker 地址: $taker")
logger.info("Maker Amount (wei): ${amounts.makerAmount}")
logger.info("Taker Amount (wei): ${amounts.takerAmount}")
logger.info("Salt: $salt")
logger.info("Expiration: $expiration")
logger.info("Nonce: $nonce")
logger.info("Fee Rate BPS: $feeRateBps")
logger.info("Signature Type: $signatureType")
logger.info("Exchange Contract: $EXCHANGE_CONTRACT")
logger.info("Chain ID: $CHAIN_ID")
logger.info("====================================")
// 打印签名前的订单参数DEBUG 级别,避免敏感信息泄露)
logger.debug("========== 订单签名前参数 ==========")
logger.debug("订单方向: $side, 价格: $price, 数量: $size")
logger.debug("Token ID: $tokenId")
logger.debug("Maker: ${makerAddressLower.take(10)}...${makerAddressLower.takeLast(6)}")
logger.debug("Signer: ${signerAddress.take(10)}...${signerAddress.takeLast(6)}")
logger.debug("Amounts - Maker: ${amounts.makerAmount}, Taker: ${amounts.takerAmount}")
logger.debug("Salt: $salt, Expiration: $expiration, Nonce: $nonce, FeeRateBPS: $feeRateBps")
logger.debug("Signature Type: $signatureType, Chain ID: $CHAIN_ID")
// 6. 构建订单数据并签名
val signature = signOrder(
@@ -8,10 +8,13 @@ import com.wrbug.polymarketbot.repository.*
import com.wrbug.polymarketbot.util.RetrofitFactory
import com.wrbug.polymarketbot.util.*
import kotlinx.coroutines.*
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import org.slf4j.LoggerFactory
import org.springframework.dao.DataIntegrityViolationException
import org.springframework.dao.DuplicateKeyException
import java.sql.SQLException
import java.util.concurrent.ConcurrentHashMap
import com.wrbug.polymarketbot.service.copytrading.configs.CopyTradingFilterService
import com.wrbug.polymarketbot.service.copytrading.configs.FilterStatus
import com.wrbug.polymarketbot.service.copytrading.orders.OrderSigningService
@@ -34,7 +37,6 @@ open class CopyOrderTrackingService(
private val sellMatchRecordRepository: SellMatchRecordRepository,
private val sellMatchDetailRepository: SellMatchDetailRepository,
private val processedTradeRepository: ProcessedTradeRepository,
private val failedTradeRepository: FailedTradeRepository,
private val filteredOrderRepository: FilteredOrderRepository,
private val copyTradingRepository: CopyTradingRepository,
private val accountRepository: AccountRepository,
@@ -53,11 +55,22 @@ 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
*/
private fun getMutex(leaderId: Long, tradeId: String): Mutex {
val key = "${leaderId}_${tradeId}"
return tradeMutexMap.getOrPut(key) { Mutex() }
}
/**
* 解密账户私钥
@@ -102,24 +115,23 @@ open class CopyOrderTrackingService(
/**
* 处理交易事件WebSocket 或轮询
* 根据交易方向调用相应的处理方法
* 使用 Mutex 保证线程安全单实例部署
*/
@Transactional
suspend fun processTrade(leaderId: Long, trade: TradeResponse, source: String): Result<Unit> {
return try {
// 获取该交易的 Mutex(按交易ID锁定,不同交易可以并行处理)
val mutex = getMutex(leaderId, trade.id)
return mutex.withLock {
try {
// 1. 检查是否已处理(去重,包括失败状态)
val existingProcessed = processedTradeRepository.findByLeaderIdAndLeaderTradeId(leaderId, trade.id)
if (existingProcessed != null) {
if (existingProcessed.status == "FAILED") {
return Result.success(Unit)
return@withLock Result.success(Unit)
}
return Result.success(Unit)
}
// 检查是否已记录为失败交易
val failedTrade = failedTradeRepository.findByLeaderIdAndLeaderTradeId(leaderId, trade.id)
if (failedTrade != null) {
return Result.success(Unit)
return@withLock Result.success(Unit)
}
// 2. 处理交易逻辑
@@ -137,11 +149,11 @@ open class CopyOrderTrackingService(
"处理交易失败: leaderId=$leaderId, tradeId=${trade.id}, side=${trade.side}",
result.exceptionOrNull()
)
return result
return@withLock result
}
// 3. 标记为已处理(成功状态)
// 注意:并发情况下可能多个请求同时处理同一笔交易,需要处理唯一约束冲突
// 由于使用了 Mutex,这里理论上不会出现并发冲突,但保留异常处理作为兜底
try {
val processed = ProcessedTrade(
leaderId = leaderId,
@@ -153,18 +165,16 @@ open class CopyOrderTrackingService(
)
processedTradeRepository.save(processed)
} catch (e: Exception) {
// 检查是否是唯一键冲突异常(可能是 DataIntegrityViolationException、DuplicateKeyException 或 SQLException
// 检查是否是唯一键冲突异常(理论上不会发生,但保留作为兜底
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 Result.success(Unit)
return@withLock Result.success(Unit)
}
logger.debug("交易已处理(并发检测): leaderId=$leaderId, tradeId=${trade.id}, status=${existing.status}")
return Result.success(Unit)
return@withLock Result.success(Unit)
} else {
// 如果检查不到,可能是事务隔离级别问题,等待一下再查询
delay(100)
@@ -172,15 +182,13 @@ open class CopyOrderTrackingService(
processedTradeRepository.findByLeaderIdAndLeaderTradeId(leaderId, trade.id)
if (existingAfterDelay != null) {
logger.debug("延迟查询到记录(并发检测): leaderId=$leaderId, tradeId=${trade.id}, status=${existingAfterDelay.status}")
return Result.success(Unit)
return@withLock Result.success(Unit)
}
// 如果还是查询不到,记录警告但不抛出异常(可能是其他约束冲突)
logger.warn(
"保存ProcessedTrade时发生唯一约束冲突,但查询不到记录: leaderId=$leaderId, tradeId=${trade.id}",
e
)
// 不抛出异常,避免影响其他交易的处理
return Result.success(Unit)
return@withLock Result.success(Unit)
}
} else {
// 其他类型的异常,重新抛出
@@ -192,6 +200,7 @@ open class CopyOrderTrackingService(
} catch (e: Exception) {
logger.error("处理交易异常: leaderId=$leaderId, tradeId=${trade.id}", e)
Result.failure(e)
}
}
}
@@ -241,11 +250,30 @@ open class CopyOrderTrackingService(
}
val tokenId = tokenIdResult.getOrNull() ?: continue
// 先计算跟单金额(用于仓位检查)
// 注意:这里先计算金额,即使后续被过滤也会记录
val tradePrice = trade.price.toSafeBigDecimal()
val buyQuantity = try {
calculateBuyQuantity(trade, copyTrading)
} catch (e: Exception) {
logger.warn("计算买入数量失败: ${e.message}", e)
continue
}
// 计算跟单金额(USDC)= 买入数量 × 价格
val copyOrderAmount = buyQuantity.multi(tradePrice)
// 过滤条件检查(在计算订单参数之前)
// 传入 Leader 交易价格,用于价格区间检查
// 传入跟单金额和市场ID,用于仓位检查(按市场检查仓位)
// 订单簿只请求一次,返回给后续逻辑使用
val tradePrice = trade.price.toSafeBigDecimal()
val filterResult = filterService.checkFilters(copyTrading, tokenId, tradePrice = tradePrice)
val filterResult = filterService.checkFilters(
copyTrading,
tokenId,
tradePrice = tradePrice,
copyOrderAmount = copyOrderAmount,
marketId = trade.market
)
val orderbook = filterResult.orderbook // 获取订单簿(如果需要)
if (!filterResult.isPassed) {
logger.warn("过滤条件检查失败,跳过创建订单: copyTradingId=${copyTrading.id}, reason=${filterResult.reason}")
@@ -338,9 +366,8 @@ open class CopyOrderTrackingService(
continue
}
// 计算买入数量
val buyQuantity = calculateBuyQuantity(trade, copyTrading)
// 买入数量已在过滤检查前计算,这里直接使用
// 如果数量为0或负数,跳过
if (buyQuantity.lte(BigDecimal.ZERO)) {
logger.warn("计算出的买入数量为0或负数,跳过: copyTradingId=${copyTrading.id}, tradeId=${trade.id}")
continue
@@ -457,27 +484,6 @@ open class CopyOrderTrackingService(
if (createOrderResult.isFailure) {
// 提取错误信息(只保留 code 和 errorBody
val exception = createOrderResult.exceptionOrNull()
val errorMsg = buildFullErrorMessage(
exception,
"BUY",
buyPrice.toString(),
finalBuyQuantity.toString(),
trade.id
)
// 记录失败交易到数据库
// retryCount = MAX_RETRY_ATTEMPTS - 1,表示已重试的次数
recordFailedTrade(
leaderId = leaderId,
trade = trade,
copyTradingId = copyTrading.id!!,
accountId = copyTrading.accountId,
side = "BUY",
price = buyPrice.toString(),
size = finalBuyQuantity.toString(),
errorMessage = errorMsg,
retryCount = MAX_RETRY_ATTEMPTS - 1 // 已重试次数
)
// 发送订单失败通知(异步,不阻塞,仅在 pushFailedOrders 为 true 时发送)
if (copyTrading.pushFailedOrders) {
@@ -533,8 +539,15 @@ open class CopyOrderTrackingService(
}
val realOrderId = createOrderResult.getOrNull() ?: continue
// 验证 orderId 格式(必须以 0x 开头的 16 进制)
if (!isValidOrderId(realOrderId)) {
logger.warn("买入订单ID格式无效,跳过保存: orderId=$realOrderId")
continue
}
// 创建买入订单跟踪记录(使用真实订单ID,使用outcomeIndex
// 先使用下单时的价格和数量作为临时值,等待轮询任务获取实际数据后再发送通知
val tracking = CopyOrderTracking(
copyTradingId = copyTrading.id,
accountId = copyTrading.accountId,
@@ -544,94 +557,17 @@ open class CopyOrderTrackingService(
outcomeIndex = trade.outcomeIndex, // 新增字段
buyOrderId = realOrderId, // 使用真实订单ID
leaderBuyTradeId = trade.id,
quantity = finalBuyQuantity, // 使用最终数量(可能已调整
price = buyPrice,
leaderBuyQuantity = trade.size.toSafeBigDecimal(), // 存储 Leader 买入数量(用于固定金额模式计算卖出比例
quantity = finalBuyQuantity, // 使用最终数量(可能已调整),临时值
price = buyPrice, // 使用下单价格,临时值
remainingQuantity = finalBuyQuantity,
status = "filled"
status = "filled",
notificationSent = false // 标记为未发送通知,等待轮询任务获取实际数据后发送
)
copyOrderTrackingRepository.save(tracking)
// 发送订单成功通知(异步,不阻塞)
notificationScope.launch {
try {
// 获取市场信息(标题和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
// 重新创建 CLOB API 客户端用于查询订单详情
val apiSecret = try {
decryptApiSecret(account)
} catch (e: Exception) {
logger.warn("解密 API Secret 失败: ${e.message}", e)
null
}
val apiPassphrase = try {
decryptApiPassphrase(account)
} catch (e: Exception) {
logger.warn("解密 API Passphrase 失败: ${e.message}", e)
null
}
val clobApiForQuery = if (apiSecret != null && apiPassphrase != null) {
retrofitFactory.createClobApi(
account.apiKey,
apiSecret,
apiPassphrase,
account.walletAddress
)
} else {
null
}
// 获取当前语言设置(从 LocaleContextHolder
val locale = try {
org.springframework.context.i18n.LocaleContextHolder.getLocale()
} catch (e: Exception) {
java.util.Locale("zh", "CN") // 默认简体中文
}
// 获取 Leader 和跟单配置信息
val leader = leaderRepository.findById(copyTrading.leaderId).orElse(null)
val leaderName = leader?.leaderName
val configName = copyTrading.configName
telegramNotificationService?.sendOrderSuccessNotification(
orderId = realOrderId,
marketTitle = marketTitle,
marketId = trade.market,
marketSlug = marketSlug,
side = "BUY",
accountName = account.accountName,
walletAddress = account.walletAddress,
clobApi = clobApiForQuery,
apiKey = account.apiKey,
apiSecret = apiSecret,
apiPassphrase = apiPassphrase,
walletAddressForApi = account.walletAddress,
locale = locale,
leaderName = leaderName,
configName = configName
)
} catch (e: Exception) {
logger.warn("发送订单成功通知失败: ${e.message}", e)
}
}
logger.info("买入订单已保存,等待轮询任务获取实际数据后发送通知: orderId=$realOrderId, copyTradingId=${copyTrading.id}")
} catch (e: Exception) {
logger.error("处理买入交易失败: copyTradingId=${copyTrading.id}, tradeId=${trade.id}", e)
// 继续处理下一个跟单关系
@@ -705,9 +641,113 @@ open class CopyOrderTrackingService(
}
}
/**
* 计算固定金额模式下的卖出数量
* 根据未匹配订单的实际买入比例计算
*/
private suspend fun calculateSellQuantityForFixedMode(
unmatchedOrders: List<CopyOrderTracking>,
leaderSellQuantity: BigDecimal,
copyTrading: CopyTrading
): BigDecimal {
if (unmatchedOrders.isEmpty()) {
return BigDecimal.ZERO
}
// 获取 Leader 信息(用于查询 Leader 买入交易)
val leader = leaderRepository.findById(copyTrading.leaderId).orElse(null)
?: run {
logger.warn("Leader 不存在,使用默认比例: leaderId=${copyTrading.leaderId}")
return leaderSellQuantity.multi(copyTrading.copyRatio)
}
// 创建不需要认证的 CLOB API 客户端(用于查询公开的交易数据)
// 注意:Polymarket CLOB API 的 /data/trades 接口是公开的,不需要认证
val clobApi = retrofitFactory.createClobApiWithoutAuth()
// 计算总比例:sum(跟单买入数量) / sum(Leader 买入数量)
// 优先使用存储的 leaderBuyQuantity,如果不存在则尝试查询 API(兼容旧数据)
var totalCopyQuantity = BigDecimal.ZERO
var totalLeaderQuantity = BigDecimal.ZERO
var successCount = 0
var failCount = 0
logger.debug("开始计算固定金额模式卖出数量: copyTradingId=${copyTrading.id}, unmatchedOrdersCount=${unmatchedOrders.size}, leaderSellQuantity=$leaderSellQuantity")
for (order in unmatchedOrders) {
val copyQty = order.quantity.toSafeBigDecimal()
var leaderQty: BigDecimal? = null
// 优先使用存储的 leaderBuyQuantity
if (order.leaderBuyQuantity != null) {
leaderQty = order.leaderBuyQuantity.toSafeBigDecimal()
logger.debug("使用存储的 Leader 买入数量: copyOrderId=${order.buyOrderId}, copyQty=$copyQty, leaderQty=$leaderQty")
successCount++
} else {
// 兼容旧数据:如果 leaderBuyQuantity 为空,尝试查询 API
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()) {
val leaderBuyTrade = tradesData.firstOrNull()
if (leaderBuyTrade != null) {
leaderQty = leaderBuyTrade.size.toSafeBigDecimal()
logger.debug("从 API 查询到 Leader 买入数量: leaderBuyTradeId=${order.leaderBuyTradeId}, leaderQty=$leaderQty")
successCount++
} else {
logger.warn("未找到 Leader 买入交易: leaderBuyTradeId=${order.leaderBuyTradeId}")
failCount++
}
} else {
logger.warn("Leader 买入交易数据为空: leaderBuyTradeId=${order.leaderBuyTradeId}")
failCount++
}
} else {
logger.warn("查询 Leader 买入交易失败: leaderBuyTradeId=${order.leaderBuyTradeId}, code=${tradesResponse.code()}")
failCount++
}
} catch (e: Exception) {
logger.warn("查询 Leader 买入交易异常: leaderBuyTradeId=${order.leaderBuyTradeId}, error=${e.message}")
failCount++
}
}
// 如果成功获取到 Leader 买入数量,累加
if (leaderQty != null && leaderQty.gt(BigDecimal.ZERO)) {
totalCopyQuantity = totalCopyQuantity.add(copyQty)
totalLeaderQuantity = totalLeaderQuantity.add(leaderQty)
} else {
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)
}
// 计算实际比例:跟单买入数量 / 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
}
/**
* 卖出订单匹配
* 统一按比例计算不区分RATIO或FIXED模式
* 根据 copyMode 计算卖出数量
* - RATIO 模式使用配置的 copyRatio
* - FIXED 模式根据实际买入比例计算
* 实际创建卖出订单并记录匹配关系
* 注意此方法在 @Transactional 方法中被调用会自动继承事务
*/
@@ -733,10 +773,7 @@ open class CopyOrderTrackingService(
return
}
// 2. 计算需要匹配的数量(统一按比例计算
val needMatch = leaderSellTrade.size.toSafeBigDecimal().multi(copyTrading.copyRatio)
// 3. 查找未匹配的买入订单(FIFO顺序)
// 2. 查找未匹配的买入订单(FIFO顺序
// 直接使用outcomeIndex匹配,而不是转换为YES/NO
if (leaderSellTrade.outcomeIndex == null) {
logger.warn("卖出交易缺少outcomeIndex,无法匹配: tradeId=${leaderSellTrade.id}, market=${leaderSellTrade.market}")
@@ -754,6 +791,28 @@ open class CopyOrderTrackingService(
return
}
// 3. 计算需要匹配的数量
// 对于 FIXED 模式,需要根据实际买入比例计算;对于 RATIO 模式,使用配置的 copyRatio
val needMatch = when (copyTrading.copyMode) {
"FIXED" -> {
// 固定金额模式:根据未匹配订单的实际比例计算
// 需要查询每个订单对应的 Leader 买入交易,计算实际比例
calculateSellQuantityForFixedMode(
unmatchedOrders = unmatchedOrders,
leaderSellQuantity = leaderSellTrade.size.toSafeBigDecimal(),
copyTrading = copyTrading
)
}
"RATIO" -> {
// 比例模式:直接使用配置的 copyRatio
leaderSellTrade.size.toSafeBigDecimal().multi(copyTrading.copyRatio)
}
else -> {
logger.warn("不支持的 copyMode: ${copyTrading.copyMode},使用默认比例模式")
leaderSellTrade.size.toSafeBigDecimal().multi(copyTrading.copyRatio)
}
}
// 4. 获取tokenId(直接使用outcomeIndex,支持多元市场)
val tokenIdResult = blockchainService.getTokenId(leaderSellTrade.market, leaderSellTrade.outcomeIndex)
if (tokenIdResult.isFailure) {
@@ -814,9 +873,24 @@ open class CopyOrderTrackingService(
return
}
// 7. 解密私钥(在方法开始时解密一次,后续复用)
// 7. 解密 API 凭证
val apiSecret = try {
decryptApiSecret(account)
} catch (e: Exception) {
logger.warn("解密 API 凭证失败,跳过创建卖出订单: accountId=${account.id}, error=${e.message}")
return
}
val apiPassphrase = try {
decryptApiPassphrase(account)
} catch (e: Exception) {
logger.warn("解密 API 凭证失败,跳过创建卖出订单: accountId=${account.id}, error=${e.message}")
return
}
// 8. 解密私钥(在方法开始时解密一次,后续复用)
val decryptedPrivateKey = decryptPrivateKey(account)
// 8. 创建并签名卖出订单
// 9. 创建并签名卖出订单
val signedOrder = try {
orderSigningService.createAndSignOrder(
privateKey = decryptedPrivateKey,
@@ -835,7 +909,7 @@ open class CopyOrderTrackingService(
return
}
// 9. 构建订单请求
// 10. 构建订单请求
// 跟单订单使用 FAK (Fill-And-Kill),允许部分成交,未成交部分立即取消
// 这样可以快速响应 Leader 的交易,避免订单长期挂单导致价格不匹配
val orderRequest = NewOrderRequest(
@@ -845,15 +919,15 @@ open class CopyOrderTrackingService(
deferExec = false
)
// 10. 创建带认证的CLOB API客户端
// 11. 创建带认证的CLOB API客户端(使用解密后的凭证)
val clobApi = retrofitFactory.createClobApi(
account.apiKey,
account.apiSecret,
account.apiPassphrase,
apiSecret,
apiPassphrase,
account.walletAddress
)
// 11. 调用API创建卖出订单(带重试机制,重试时会重新生成salt并重新签名)
// 12. 调用API创建卖出订单(带重试机制,重试时会重新生成salt并重新签名)
val createOrderResult = createOrderWithRetry(
clobApi = clobApi,
@@ -869,32 +943,27 @@ open class CopyOrderTrackingService(
)
if (createOrderResult.isFailure) {
// 创建订单失败,记录到失败表
// 创建订单失败,记录错误日志
val exception = createOrderResult.exceptionOrNull()
val errorMsg = buildFullErrorMessage(
exception,
"SELL",
sellPrice.toString(),
totalMatched.toString(),
leaderSellTrade.id
)
recordFailedTrade(
leaderId = copyTrading.leaderId,
trade = leaderSellTrade,
copyTradingId = copyTrading.id!!,
accountId = copyTrading.accountId,
side = "SELL", // 订单方向是SELL
price = sellPrice.toString(),
size = totalMatched.toString(),
errorMessage = errorMsg,
retryCount = 1 // 已重试一次
)
logger.error("创建卖出订单失败: copyTradingId=${copyTrading.id}, tradeId=${leaderSellTrade.id}, error=${exception?.message}")
return
}
val realSellOrderId = createOrderResult.getOrNull() ?: return
// 12. 更新买入订单跟踪状态
// 12. 下单时直接使用下单价格保存,等待定时任务更新实际成交价
// priceUpdated 统一由定时任务更新,下单时统一设置为 false(非0x开头的除外)
val priceUpdated = !realSellOrderId.startsWith("0x", ignoreCase = true)
if (priceUpdated) {
logger.debug("卖出订单ID非0x开头,标记为已更新: orderId=$realSellOrderId")
} else {
logger.debug("卖出订单ID为0x开头,等待定时任务更新价格: orderId=$realSellOrderId")
}
// 使用下单价格,等待定时任务更新实际成交价
val actualSellPrice = sellPrice
// 13. 更新买入订单跟踪状态
for (order in unmatchedOrders) {
val detail = matchDetails.find { it.trackingId == order.id }
if (detail != null) {
@@ -907,8 +976,17 @@ open class CopyOrderTrackingService(
}
}
// 13. 创建卖出匹配记录(使用真实订单ID,使用outcomeIndex
val totalRealizedPnl = matchDetails.sumOf { it.realizedPnl.toSafeBigDecimal() }
// 14. 重新计算盈亏(使用实际成交价
val updatedMatchDetails = matchDetails.map { detail ->
val updatedRealizedPnl = actualSellPrice.subtract(detail.buyPrice).multi(detail.matchedQuantity)
detail.copy(
sellPrice = actualSellPrice,
realizedPnl = updatedRealizedPnl
)
}
// 15. 创建卖出匹配记录(使用真实订单ID和实际成交价)
val totalRealizedPnl = updatedMatchDetails.sumOf { it.realizedPnl.toSafeBigDecimal() }
val matchRecord = SellMatchRecord(
copyTradingId = copyTrading.id,
@@ -918,17 +996,20 @@ open class CopyOrderTrackingService(
side = leaderSellTrade.outcomeIndex.toString(), // 使用outcomeIndex作为side(兼容旧数据)
outcomeIndex = leaderSellTrade.outcomeIndex, // 新增字段
totalMatchedQuantity = totalMatched,
sellPrice = sellPrice,
totalRealizedPnl = totalRealizedPnl
sellPrice = actualSellPrice, // 使用实际成交价(如果查询失败则为下单价格)
totalRealizedPnl = totalRealizedPnl,
priceUpdated = priceUpdated // 共用字段:false 表示未处理(未查询订单详情,未发送通知),true 表示已处理(已查询订单详情,已发送通知)
)
val savedRecord = sellMatchRecordRepository.save(matchRecord)
// 14. 保存匹配明细
for (detail in matchDetails) {
// 16. 保存匹配明细(使用实际成交价)
for (detail in updatedMatchDetails) {
val savedDetail = detail.copy(matchRecordId = savedRecord.id!!)
sellMatchDetailRepository.save(savedDetail)
}
logger.info("卖出订单已保存,等待轮询任务获取实际数据后发送通知: orderId=$realSellOrderId, copyTradingId=${copyTrading.id}")
}
@@ -1122,98 +1203,6 @@ open class CopyOrderTrackingService(
return "code=$code, errorBody=$errorBody"
}
/**
* 记录失败交易到数据库
* 注意此方法在 @Transactional 方法中被调用会自动继承事务
*/
private suspend fun recordFailedTrade(
leaderId: Long,
trade: TradeResponse,
copyTradingId: Long,
accountId: Long,
side: String,
price: String,
size: String,
errorMessage: String,
retryCount: Int
) {
try {
// 确保错误信息不超过数据库字段限制(TEXT类型通常支持65535字符)
val maxErrorMessageLength = 50000 // 保留一些余量
val finalErrorMessage = if (errorMessage.length > maxErrorMessageLength) {
errorMessage.substring(0, maxErrorMessageLength) + "... (截断)"
} else {
errorMessage
}
val failedTrade = FailedTrade(
leaderId = leaderId,
leaderTradeId = trade.id,
tradeType = trade.side.uppercase(),
copyTradingId = copyTradingId,
accountId = accountId,
marketId = trade.market,
side = side,
price = price,
size = size,
errorMessage = finalErrorMessage,
retryCount = retryCount,
failedAt = System.currentTimeMillis()
)
failedTradeRepository.save(failedTrade)
// 记录日志,确认已保存到数据库
logger.info("失败交易已保存到数据库: leaderId=$leaderId, tradeId=${trade.id}, errorMessageLength=${finalErrorMessage.length}")
// 标记为已处理(失败状态),避免重复处理
// 注意:并发情况下可能多个请求同时处理同一笔交易,需要处理唯一约束冲突
try {
val processed = ProcessedTrade(
leaderId = leaderId,
leaderTradeId = trade.id,
tradeType = trade.side.uppercase(),
source = "polling",
status = "FAILED",
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 == "SUCCESS") {
logger.warn("交易已成功处理,但尝试记录为失败(并发冲突): leaderId=$leaderId, tradeId=${trade.id}")
} else {
logger.debug("交易已标记为失败(并发检测): leaderId=$leaderId, tradeId=${trade.id}")
}
} else {
// 如果查询不到,等待一下再查询(可能是事务隔离级别问题)
delay(100)
val existingAfterDelay =
processedTradeRepository.findByLeaderIdAndLeaderTradeId(leaderId, trade.id)
if (existingAfterDelay != null) {
logger.debug("延迟查询到记录(并发检测): leaderId=$leaderId, tradeId=${trade.id}, status=${existingAfterDelay.status}")
} else {
logger.warn(
"保存ProcessedTrade失败记录时发生唯一约束冲突,但查询不到记录: leaderId=$leaderId, tradeId=${trade.id}",
e
)
}
}
} else {
// 其他类型的异常,记录但不抛出(避免影响其他交易的处理)
logger.warn("保存ProcessedTrade失败记录时发生异常: leaderId=$leaderId, tradeId=${trade.id}", e)
}
}
logger.warn("已记录失败交易: leaderId=$leaderId, tradeId=${trade.id}, error=$errorMessage")
} catch (e: Exception) {
logger.error("记录失败交易异常: leaderId=$leaderId, tradeId=${trade.id}", e)
}
}
/**
* 更新订单状态
@@ -1328,6 +1317,119 @@ open class CopyOrderTrackingService(
FilterStatus.FAILED_ORDERBOOK_EMPTY -> "ORDERBOOK_EMPTY"
FilterStatus.FAILED_SPREAD -> "SPREAD"
FilterStatus.FAILED_ORDER_DEPTH -> "ORDER_DEPTH"
FilterStatus.FAILED_MAX_POSITION_VALUE -> "MAX_POSITION_VALUE"
FilterStatus.FAILED_MAX_POSITION_COUNT -> "MAX_POSITION_COUNT"
}
}
/**
* 验证订单ID格式
* 订单ID必须以 0x 开头且是有效的 16 进制字符串
*
* @param orderId 订单ID
* @return 如果格式有效返回 true否则返回 false
*/
private fun isValidOrderId(orderId: String): Boolean {
if (!orderId.startsWith("0x", ignoreCase = true)) {
return false
}
// 验证是否为有效的 16 进制字符串(去除 0x 前缀后)
val hexPart = orderId.substring(2)
if (hexPart.isEmpty()) {
return false
}
// 检查是否只包含 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 如果查询失败使用此价格作为默认值
* @return 实际成交价加权平均如果查询失败则返回 fallbackPrice
*/
suspend fun getActualExecutionPrice(
orderId: String,
clobApi: PolymarketClobApi,
fallbackPrice: BigDecimal
): BigDecimal {
return try {
// 1. 查询订单详情
val orderResponse = clobApi.getOrder(orderId)
if (!orderResponse.isSuccessful) {
val errorBody = orderResponse.errorBody()?.string()?.take(200) ?: "无错误详情"
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}")
return fallbackPrice
}
// 3. 如果订单已成交,通过 associateTrades 获取交易记录
val associateTrades = order.associateTrades
if (associateTrades.isNullOrEmpty()) {
logger.debug("订单无关联交易记录,使用下单价格: orderId=$orderId")
return fallbackPrice
}
// 4. 查询所有关联的交易记录
val trades = mutableListOf<TradeResponse>()
for (tradeId in associateTrades) {
try {
val tradesResponse = clobApi.getTrades(id = tradeId)
if (tradesResponse.isSuccessful && tradesResponse.body() != null) {
val tradesData = tradesResponse.body()!!.data
trades.addAll(tradesData)
}
} catch (e: Exception) {
logger.warn("查询交易记录失败: tradeId=$tradeId, error=${e.message}")
}
}
if (trades.isEmpty()) {
logger.debug("未找到交易记录,使用下单价格: orderId=$orderId")
return fallbackPrice
}
// 5. 计算加权平均成交价
// 加权平均 = Σ(price * size) / Σ(size)
var totalAmount = BigDecimal.ZERO
var totalSize = BigDecimal.ZERO
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)
}
}
if (totalSize > BigDecimal.ZERO) {
val weightedAveragePrice = totalAmount.divide(totalSize, 8, java.math.RoundingMode.HALF_UP)
logger.info("计算实际成交价成功: orderId=$orderId, 加权平均价=$weightedAveragePrice, 下单价格=$fallbackPrice, 交易笔数=${trades.size}")
return weightedAveragePrice
} else {
logger.warn("交易记录数量为0,使用下单价格: orderId=$orderId")
return fallbackPrice
}
} catch (e: Exception) {
logger.error("获取实际成交价异常: orderId=$orderId, error=${e.message}", e)
return fallbackPrice
}
}
@@ -341,9 +341,9 @@ class CopyTradingStatisticsService(
// 传递 outcomeIndex 参数,确保获取对应 outcome 的价格
val result = accountService.getMarketPrice(marketId, outcomeIndex)
result.onSuccess { response ->
// 使用中间价,如果没有则使用最后价格
val price = response.midpoint ?: response.lastPrice
if (price != null) {
// 使用当前价格
val price = response.currentPrice
if (price.isNotBlank() && price != "0") {
// 使用 "marketId:outcomeIndex" 作为 key
val key = "$marketId:$outcomeIndex"
prices[key] = price
@@ -0,0 +1,887 @@
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.system.TelegramNotificationService
import com.wrbug.polymarketbot.util.RetrofitFactory
import com.wrbug.polymarketbot.util.CryptoUtils
import com.wrbug.polymarketbot.util.toSafeBigDecimal
import com.wrbug.polymarketbot.util.multi
import kotlinx.coroutines.*
import org.slf4j.LoggerFactory
import org.springframework.boot.context.event.ApplicationReadyEvent
import org.springframework.context.event.EventListener
import org.springframework.context.i18n.LocaleContextHolder
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.math.BigDecimal
/**
* 订单状态更新服务
* 定时轮询更新卖出订单的实际成交价并更新买入订单的实际数据并发送通知
*/
@Service
class OrderStatusUpdateService(
private val sellMatchRecordRepository: SellMatchRecordRepository,
private val sellMatchDetailRepository: SellMatchDetailRepository,
private val copyTradingRepository: CopyTradingRepository,
private val accountRepository: AccountRepository,
private val copyOrderTrackingRepository: CopyOrderTrackingRepository,
private val leaderRepository: LeaderRepository,
private val retrofitFactory: RetrofitFactory,
private val cryptoUtils: CryptoUtils,
private val trackingService: CopyOrderTrackingService,
private val telegramNotificationService: TelegramNotificationService?
) {
private val logger = LoggerFactory.getLogger(OrderStatusUpdateService::class.java)
private val updateScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
@EventListener(ApplicationReadyEvent::class)
fun onApplicationReady() {
logger.info("订单状态更新服务已启动,将每5秒轮询一次")
}
/**
* 定时更新订单状态
* 每5秒执行一次
*/
@Scheduled(fixedDelay = 5000)
fun updateOrderStatus() {
updateScope.launch {
try {
// 1. 清理已删除账户的订单
cleanupDeletedAccountOrders()
// 2. 检查30秒前创建的订单,如果未成交则删除
checkAndDeleteUnfilledOrders()
// 3. 更新卖出订单的实际成交价并发送通知(priceUpdated 共用字段)
updatePendingSellOrderPrices()
// 4. 更新买入订单的实际数据并发送通知
updatePendingBuyOrders()
} catch (e: Exception) {
logger.error("订单状态更新异常: ${e.message}", e)
}
}
}
/**
* 验证订单ID格式
* 订单ID必须以 0x 开头且是有效的 16 进制字符串
*
* @param orderId 订单ID
* @return 如果格式有效返回 true否则返回 false
*/
private fun isValidOrderId(orderId: String): Boolean {
if (!orderId.startsWith("0x", ignoreCase = true)) {
return false
}
// 验证是否为有效的 16 进制字符串(去除 0x 前缀后)
val hexPart = orderId.substring(2)
if (hexPart.isEmpty()) {
return false
}
// 检查是否只包含 0-9, a-f, A-F
return hexPart.all { it in '0'..'9' || it in 'a'..'f' || it in 'A'..'F' }
}
/**
* 清理已删除账户的订单
*/
@Transactional
private suspend fun cleanupDeletedAccountOrders() {
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后还没有成交则删除
*/
@Transactional
private suspend fun checkAndDeleteUnfilledOrders() {
try {
// 计算30秒前的时间戳
val thirtySecondsAgo = System.currentTimeMillis() - 30000
// 查询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 {
// 获取账户
val account = accountRepository.findById(accountId).orElse(null)
if (account == null) {
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!!)
} catch (e: Exception) {
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!!,
apiSecret,
apiPassphrase,
account.walletAddress
)
// 检查每个订单
for (order in orders) {
try {
// 查询订单详情
val orderResponse = clobApi.getOrder(order.buyOrderId)
// 先检查 HTTP 状态码,非 200 的都跳过
if (orderResponse.code() != 200) {
// HTTP 非 200,记录日志并跳过,等待下次轮询
// 不删除订单,因为可能是临时网络问题或 API 错误
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"),表示订单不存在
// 检查订单是否已部分卖出,如果已部分卖出则保留订单用于统计
val hasMatchedDetails = sellMatchDetailRepository.findByTrackingId(order.id!!).isNotEmpty()
if (hasMatchedDetails || order.matchedQuantity > BigDecimal.ZERO) {
logger.debug("订单不存在但已部分卖出,保留订单用于统计: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}, matchedQuantity=${order.matchedQuantity}")
continue
}
// 订单不存在且未部分卖出,删除本地订单
logger.info("订单不存在(HTTP 200 但响应体为空),删除本地订单: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}")
try {
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)
}
continue
}
// 检查订单是否成交
// 如果订单状态不是 FILLED 且已成交数量为0,说明未成交,删除
val sizeMatched = orderDetail.sizeMatched?.toSafeBigDecimal() ?: BigDecimal.ZERO
if (orderDetail.status != "FILLED" && sizeMatched <= BigDecimal.ZERO) {
logger.info("订单30秒后仍未成交,删除本地订单: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}, status=${orderDetail.status}, sizeMatched=$sizeMatched")
try {
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)
}
} else {
logger.debug("订单已成交或部分成交,保留: orderId=${order.buyOrderId}, status=${orderDetail.status}, sizeMatched=$sizeMatched")
}
} catch (e: Exception) {
logger.error("检查订单失败: orderId=${order.buyOrderId}, error=${e.message}", e)
}
}
} catch (e: Exception) {
logger.error("检查账户订单失败: accountId=$accountId, error=${e.message}", e)
}
}
} catch (e: Exception) {
logger.error("检查未成交订单异常: ${e.message}", e)
}
}
/**
* 更新待更新的卖出订单价格
* 注意priceUpdated 现在同时表示价格已更新和通知已发送共用字段
*/
@Transactional
private suspend fun updatePendingSellOrderPrices() {
try {
// 查询所有价格未更新的卖出记录(priceUpdated = false 表示未处理)
val pendingRecords = sellMatchRecordRepository.findByPriceUpdatedFalse()
if (pendingRecords.isEmpty()) {
return
}
logger.debug("找到 ${pendingRecords.size} 条待更新价格的卖出订单")
for (record in pendingRecords) {
try {
// 获取跟单关系
val copyTrading = copyTradingRepository.findById(record.copyTradingId).orElse(null)
if (copyTrading == null) {
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!!)
} catch (e: Exception) {
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!!,
apiSecret,
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)
if (!isAutoOrder) {
// 非自动订单,发送通知(使用临时数据)
sendSellOrderNotification(
record = record,
useTemporaryData = true,
account = account,
copyTrading = copyTrading,
clobApi = clobApi,
apiSecret = apiSecret,
apiPassphrase = apiPassphrase
)
} else {
logger.debug("自动生成的订单,跳过发送通知: orderId=${record.sellOrderId}")
}
// 标记为已处理(priceUpdated = true 同时表示价格已更新和通知已发送)
val updatedRecord = SellMatchRecord(
id = record.id,
copyTradingId = record.copyTradingId,
sellOrderId = record.sellOrderId,
leaderSellTradeId = record.leaderSellTradeId,
marketId = record.marketId,
side = record.side,
outcomeIndex = record.outcomeIndex,
totalMatchedQuantity = record.totalMatchedQuantity,
sellPrice = record.sellPrice,
totalRealizedPnl = record.totalRealizedPnl,
priceUpdated = true, // 标记为已处理(价格已更新和通知已发送)
createdAt = record.createdAt
)
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)
if (isAutoOrder) {
logger.debug("自动生成的订单,跳过发送通知并直接标记为已处理: orderId=${record.sellOrderId}")
// 直接标记为已处理,不发送通知
val updatedRecord = SellMatchRecord(
id = record.id,
copyTradingId = record.copyTradingId,
sellOrderId = record.sellOrderId,
leaderSellTradeId = record.leaderSellTradeId,
marketId = record.marketId,
side = record.side,
outcomeIndex = record.outcomeIndex,
totalMatchedQuantity = record.totalMatchedQuantity,
sellPrice = record.sellPrice,
totalRealizedPnl = record.totalRealizedPnl,
priceUpdated = true, // 标记为已处理
createdAt = record.createdAt
)
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)
// 更新明细的卖出价格和盈亏
// 注意:SellMatchDetail 的字段都是 val,需要创建新对象
val updatedDetail = SellMatchDetail(
id = detail.id,
matchRecordId = detail.matchRecordId,
trackingId = detail.trackingId,
buyOrderId = detail.buyOrderId,
matchedQuantity = detail.matchedQuantity,
buyPrice = detail.buyPrice,
sellPrice = actualSellPrice, // 更新卖出价格
realizedPnl = updatedRealizedPnl, // 更新盈亏
createdAt = detail.createdAt
)
sellMatchDetailRepository.save(updatedDetail)
totalRealizedPnl = totalRealizedPnl.add(updatedRealizedPnl)
}
// 发送通知(使用实际价格)
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,
copyTradingId = record.copyTradingId,
sellOrderId = record.sellOrderId,
leaderSellTradeId = record.leaderSellTradeId,
marketId = record.marketId,
side = record.side,
outcomeIndex = record.outcomeIndex,
totalMatchedQuantity = record.totalMatchedQuantity,
sellPrice = actualSellPrice, // 更新卖出价格
totalRealizedPnl = totalRealizedPnl, // 更新总盈亏
priceUpdated = true, // 标记为已处理(价格已更新和通知已发送)
createdAt = record.createdAt
)
sellMatchRecordRepository.save(updatedRecord)
logger.info("更新卖出订单价格成功并已发送通知: orderId=${record.sellOrderId}, 原价格=${record.sellPrice}, 新价格=$actualSellPrice")
} else {
// 价格相同,但已经查询过,发送通知并标记为已处理
sendSellOrderNotification(
record = record,
actualPrice = actualSellPrice.toString(),
actualSize = record.totalMatchedQuantity.toString(),
account = account,
copyTrading = copyTrading,
clobApi = clobApi,
apiSecret = apiSecret,
apiPassphrase = apiPassphrase
)
val updatedRecord = SellMatchRecord(
id = record.id,
copyTradingId = record.copyTradingId,
sellOrderId = record.sellOrderId,
leaderSellTradeId = record.leaderSellTradeId,
marketId = record.marketId,
side = record.side,
outcomeIndex = record.outcomeIndex,
totalMatchedQuantity = record.totalMatchedQuantity,
sellPrice = record.sellPrice,
totalRealizedPnl = record.totalRealizedPnl,
priceUpdated = true, // 标记为已处理(价格已更新和通知已发送)
createdAt = record.createdAt
)
sellMatchRecordRepository.save(updatedRecord)
logger.debug("卖出订单价格无需更新但已发送通知: orderId=${record.sellOrderId}, price=$actualSellPrice")
}
} catch (e: Exception) {
logger.warn("更新卖出订单价格失败: orderId=${record.sellOrderId}, error=${e.message}", e)
// 继续处理下一条记录
}
}
} catch (e: Exception) {
logger.error("更新待更新卖出订单价格异常: ${e.message}", e)
}
}
/**
* 更新待发送通知的买入订单
* 查询订单详情获取实际价格和数量然后发送通知并更新数据库
*/
@Transactional
private suspend fun updatePendingBuyOrders() {
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,直接标记为已发送,使用临时数据发送通知
val updatedOrder = CopyOrderTracking(
id = order.id,
copyTradingId = order.copyTradingId,
accountId = order.accountId,
leaderId = order.leaderId,
marketId = order.marketId,
side = order.side,
outcomeIndex = order.outcomeIndex,
buyOrderId = order.buyOrderId,
leaderBuyTradeId = order.leaderBuyTradeId,
quantity = order.quantity,
price = order.price,
matchedQuantity = order.matchedQuantity,
remainingQuantity = order.remainingQuantity,
status = order.status,
notificationSent = true, // 标记为已发送通知
createdAt = order.createdAt,
updatedAt = System.currentTimeMillis()
)
copyOrderTrackingRepository.save(updatedOrder)
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!!)
} catch (e: Exception) {
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!!,
apiSecret,
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"),表示订单不存在
// 检查订单是否已部分卖出,如果已部分卖出则保留订单用于统计
val hasMatchedDetails = sellMatchDetailRepository.findByTrackingId(order.id!!).isNotEmpty()
if (hasMatchedDetails || order.matchedQuantity > BigDecimal.ZERO) {
logger.debug("订单不存在但已部分卖出,保留订单用于统计: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}, matchedQuantity=${order.matchedQuantity}")
continue
}
// 订单不存在且未部分卖出,删除本地订单
logger.info("订单不存在(HTTP 200 但响应体为空),删除本地订单: orderId=${order.buyOrderId}, copyOrderTrackingId=${order.id}")
try {
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)
}
continue
}
// 获取实际价格和数量
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
// 创建更新后的订单对象
val updatedOrder = CopyOrderTracking(
id = order.id,
copyTradingId = order.copyTradingId,
accountId = order.accountId,
leaderId = order.leaderId,
marketId = order.marketId,
side = order.side,
outcomeIndex = order.outcomeIndex,
buyOrderId = order.buyOrderId,
leaderBuyTradeId = order.leaderBuyTradeId,
quantity = actualSize, // 使用实际数量
price = actualPrice, // 使用实际价格
matchedQuantity = order.matchedQuantity,
remainingQuantity = order.remainingQuantity,
status = order.status,
notificationSent = true, // 标记为已发送通知
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,
actualPrice = actualPrice.toString(),
actualSize = actualSize.toString(),
actualOutcome = actualOutcome,
account = account,
copyTrading = copyTrading,
clobApi = clobApi,
apiSecret = apiSecret,
apiPassphrase = apiPassphrase
)
} catch (e: Exception) {
logger.warn("更新买入订单失败: orderId=${order.buyOrderId}, error=${e.message}", e)
// 继续处理下一条记录
}
}
} catch (e: Exception) {
logger.error("更新待发送通知买入订单异常: ${e.message}", e)
}
}
/**
* 发送买入订单通知
*/
private suspend fun sendBuyOrderNotification(
order: CopyOrderTracking,
useTemporaryData: Boolean = false,
actualPrice: String? = null,
actualSize: String? = null,
actualOutcome: String? = null,
account: Account? = null,
copyTrading: CopyTrading? = null,
clobApi: PolymarketClobApi? = null,
apiSecret: String? = null,
apiPassphrase: String? = null
) {
if (telegramNotificationService == null) {
return
}
try {
// 获取跟单关系和账户信息(如果未提供)
val finalCopyTrading = copyTrading ?: copyTradingRepository.findById(order.copyTradingId).orElse(null)
if (finalCopyTrading == null) {
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 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
}
// 发送通知
telegramNotificationService.sendOrderSuccessNotification(
orderId = order.buyOrderId,
marketTitle = marketTitle,
marketId = order.marketId,
marketSlug = marketSlug,
side = "BUY",
price = actualPrice ?: order.price.toString(), // 使用实际价格或临时价格
size = actualSize ?: order.quantity.toString(), // 使用实际数量或临时数量
outcome = actualOutcome, // 使用实际 outcome
accountName = finalAccount.accountName,
walletAddress = finalAccount.walletAddress,
clobApi = finalClobApi,
apiKey = finalAccount.apiKey,
apiSecret = apiSecret,
apiPassphrase = apiPassphrase,
walletAddressForApi = finalAccount.walletAddress,
locale = locale,
leaderName = leaderName,
configName = configName
)
logger.info("买入订单通知已发送: orderId=${order.buyOrderId}, copyTradingId=${order.copyTradingId}")
} catch (e: Exception) {
logger.warn("发送买入订单通知失败: orderId=${order.buyOrderId}, error=${e.message}", e)
}
}
/**
* 发送卖出订单通知
*/
private suspend fun sendSellOrderNotification(
record: SellMatchRecord,
useTemporaryData: Boolean = false,
actualPrice: String? = null,
actualSize: String? = null,
actualOutcome: String? = null,
account: Account? = null,
copyTrading: CopyTrading? = null,
clobApi: PolymarketClobApi? = null,
apiSecret: String? = null,
apiPassphrase: String? = null
) {
if (telegramNotificationService == null) {
return
}
try {
// 获取跟单关系和账户信息(如果未提供)
val finalCopyTrading = copyTrading ?: copyTradingRepository.findById(record.copyTradingId).orElse(null)
if (finalCopyTrading == null) {
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 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
}
// 发送通知
telegramNotificationService.sendOrderSuccessNotification(
orderId = record.sellOrderId,
marketTitle = marketTitle,
marketId = record.marketId,
marketSlug = marketSlug,
side = "SELL",
price = actualPrice ?: record.sellPrice.toString(), // 使用实际价格或临时价格
size = actualSize ?: record.totalMatchedQuantity.toString(), // 使用实际数量或临时数量
outcome = actualOutcome, // 使用实际 outcome
accountName = finalAccount.accountName,
walletAddress = finalAccount.walletAddress,
clobApi = finalClobApi,
apiKey = finalAccount.apiKey,
apiSecret = apiSecret,
apiPassphrase = apiPassphrase,
walletAddressForApi = finalAccount.walletAddress,
locale = locale,
leaderName = leaderName,
configName = configName
)
logger.info("卖出订单通知已发送: orderId=${record.sellOrderId}, copyTradingId=${record.copyTradingId}")
} catch (e: Exception) {
logger.warn("发送卖出订单通知失败: orderId=${record.sellOrderId}, error=${e.message}", e)
}
}
}
@@ -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
)
}
}
@@ -28,12 +28,11 @@ class ApiHealthCheckService(
private val dataApiBaseUrl: String,
@Value("\${polymarket.gamma.base-url}")
private val gammaBaseUrl: String,
@Value("\${polygon.rpc.url:}")
private val polygonRpcUrl: String,
@Value("\${polymarket.rtds.ws-url}")
private val polymarketWsUrl: String,
@Value("\${polymarket.builder.relayer-url:}")
private val builderRelayerUrl: String
private val builderRelayerUrl: String,
private val rpcNodeService: RpcNodeService
) : ApplicationContextAware {
private var applicationContext: ApplicationContext? = null
@@ -186,19 +185,12 @@ class ApiHealthCheckService(
/**
* 检查 Polygon RPC
* 使用动态获取的可用节点RpcNodeService 总是返回一个有效的 URL包括默认节点
*/
private suspend fun checkPolygonRpc(): ApiHealthCheckDto = withContext(Dispatchers.IO) {
if (polygonRpcUrl.isBlank()) {
return@withContext ApiHealthCheckDto(
name = "Polygon RPC",
url = "未配置",
status = "skipped",
message = "未配置 Polygon RPC URL"
)
}
val url = polygonRpcUrl
checkJsonRpcApi("Polygon RPC", url)
// 使用 RpcNodeService 获取可用节点(总是返回有效值,包括默认节点)
val rpcUrl = rpcNodeService.getHttpUrl()
checkJsonRpcApi("Polygon RPC", rpcUrl)
}
/**
@@ -24,12 +24,11 @@ import java.math.BigInteger
*/
@Service
class RelayClientService(
@Value("\${polygon.rpc.url:}")
private val polygonRpcUrl: String,
@Value("\${polymarket.builder.relayer-url:}")
private val builderRelayerUrl: String,
private val retrofitFactory: RetrofitFactory,
private val systemConfigService: SystemConfigService
private val systemConfigService: SystemConfigService,
private val rpcNodeService: RpcNodeService
) {
private val logger = LoggerFactory.getLogger(RelayClientService::class.java)
@@ -43,12 +42,9 @@ class RelayClientService(
// 空集合ID
private val EMPTY_SET = "0x0000000000000000000000000000000000000000000000000000000000000000"
private val polygonRpcApi: EthereumRpcApi? by lazy {
if (polygonRpcUrl.isBlank()) {
null
} else {
retrofitFactory.createEthereumRpcApi(polygonRpcUrl)
}
private val polygonRpcApi: EthereumRpcApi by lazy {
val rpcUrl = rpcNodeService.getHttpUrl()
retrofitFactory.createEthereumRpcApi(rpcUrl)
}
/**
@@ -260,7 +256,7 @@ class RelayClientService(
builderSecret: String,
builderPassphrase: String
): Result<String> {
val rpcApi = polygonRpcApi ?: throw IllegalStateException("Polygon RPC URL 未配置")
val rpcApi = polygonRpcApi
val relayerApi = retrofitFactory.createBuilderRelayerApi(
relayerUrl = builderRelayerUrl,
apiKey = builderApiKey,
@@ -337,15 +333,10 @@ class RelayClientService(
// 打包签名(参考 builder-relayer-client/src/utils/index.ts 的 splitAndPackSig
val packedSignature = splitAndPackSig(safeSignature)
// 调试日志
// 调试日志(地址已遮蔽)
logger.debug("=== Builder Relayer 签名调试 ===")
logger.debug("Safe Address: $proxyAddress")
logger.debug("From Address: $fromAddress")
logger.debug("To: ${safeTx.to}")
logger.debug("Data: $redeemCallData")
logger.debug("Nonce: $proxyNonce")
logger.debug("Packed Signature: $packedSignature")
logger.debug("Signature Length: ${packedSignature.length} (expected: 132 with 0x)")
logger.debug("Safe: ${proxyAddress.take(10)}..., From: ${fromAddress.take(10)}..., Nonce: $proxyNonce")
logger.debug("Signature Length: ${packedSignature.length}")
// 构建 TransactionRequest(参考 builder-relayer-client/src/builder/safe.ts
// 注意:根据 TypeScript 实现,data 和 signature 都应该带 0x 前缀
@@ -368,13 +359,7 @@ class RelayClientService(
metadata = "Redeem positions via Builder Relayer"
)
logger.debug("Request Type: ${request.type}")
logger.debug("Request From: ${request.from}")
logger.debug("Request To: ${request.to}")
logger.debug("Request ProxyWallet: ${request.proxyWallet}")
logger.debug("Request Data Length: ${request.data.length}")
logger.debug("Request Signature Length: ${request.signature.length}")
logger.debug("Request Nonce: ${request.nonce}")
logger.debug("Request: type=${request.type}, dataLen=${request.data.length}, sigLen=${request.signature.length}, nonce=${request.nonce}")
// 调用 Builder Relayer API(认证头通过拦截器添加)
val response = relayerApi.submitTransaction(request)
@@ -461,13 +446,7 @@ class RelayClientService(
safeTx: SafeTransaction
): Result<String> {
return try {
// 如果未配置 RPC URL,返回错误
if (polygonRpcUrl.isBlank()) {
logger.warn("未配置 Polygon RPC URL,无法执行交易")
return Result.failure(IllegalStateException("未配置 Polygon RPC URL,无法执行交易。请配置 polygon.rpc.url 或启用 Builder RelayerGasless"))
}
val rpcApi = polygonRpcApi ?: throw IllegalStateException("Polygon RPC URL 未配置")
val rpcApi = polygonRpcApi
// 从私钥推导实际签名地址(交易真正的 from 地址)
val cleanPrivateKey = privateKey.removePrefix("0x")
@@ -665,7 +644,7 @@ class RelayClientService(
}
val hexNonce = rpcResponse.result ?: return Result.failure(Exception("Proxy nonce 结果为空"))
val nonce = EthereumUtils.decodeUint256(hexNonce)
val nonce = EthereumUtils.decodeUint256(hexNonce.asString)
return Result.success(nonce)
}
@@ -689,7 +668,7 @@ class RelayClientService(
}
val hexNonce = rpcResponse.result ?: return Result.failure(Exception("nonce 结果为空"))
val nonce = EthereumUtils.decodeUint256(hexNonce)
val nonce = EthereumUtils.decodeUint256(hexNonce.asString)
return Result.success(nonce)
}
@@ -713,7 +692,7 @@ class RelayClientService(
}
val hexGasPrice = rpcResponse.result ?: return Result.failure(Exception("gas price 结果为空"))
val gasPrice = EthereumUtils.decodeUint256(hexGasPrice)
val gasPrice = EthereumUtils.decodeUint256(hexGasPrice.asString)
return Result.success(gasPrice)
}
@@ -784,7 +763,7 @@ class RelayClientService(
}
val txHash = rpcResponse.result ?: return Result.failure(Exception("交易哈希为空"))
return Result.success(txHash)
return Result.success(txHash.asString)
}
/**
@@ -0,0 +1,513 @@
package com.wrbug.polymarketbot.service.system
import com.wrbug.polymarketbot.api.EthereumRpcApi
import com.wrbug.polymarketbot.api.JsonRpcRequest
import com.wrbug.polymarketbot.entity.NodeHealthStatus
import com.wrbug.polymarketbot.entity.RpcNodeConfig
import com.wrbug.polymarketbot.entity.RpcProviderType
import com.wrbug.polymarketbot.repository.RpcNodeConfigRepository
import com.wrbug.polymarketbot.util.CryptoUtils
import com.wrbug.polymarketbot.util.RetrofitFactory
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
/**
* RPC 节点管理服务
* 负责管理用户配置的 Polygon RPC 节点
*/
@Service
class RpcNodeService(
private val rpcNodeConfigRepository: RpcNodeConfigRepository,
private val cryptoUtils: CryptoUtils,
private val retrofitFactory: RetrofitFactory
) {
private val logger = LoggerFactory.getLogger(RpcNodeService::class.java)
companion object {
// 默认公共节点
private const val DEFAULT_RPC_URL = "https://polygon.publicnode.com"
private const val DEFAULT_WS_URL = "wss://polygon.publicnode.com"
// 主流服务商 URL 模板
private val PROVIDER_HTTP_TEMPLATES = mapOf(
RpcProviderType.ALCHEMY to "https://polygon-mainnet.g.alchemy.com/v2/{apiKey}",
RpcProviderType.INFURA to "https://polygon-mainnet.infura.io/v3/{apiKey}",
RpcProviderType.QUICKNODE to "https://your-endpoint.quiknode.pro/{apiKey}/",
RpcProviderType.CHAINSTACK to "https://polygon-mainnet.core.chainstack.com/{apiKey}",
RpcProviderType.GETBLOCK to "https://go.getblock.io/{apiKey}/"
)
private val PROVIDER_WS_TEMPLATES = mapOf(
RpcProviderType.ALCHEMY to "wss://polygon-mainnet.g.alchemy.com/v2/{apiKey}",
RpcProviderType.INFURA to "wss://polygon-mainnet.infura.io/ws/v3/{apiKey}",
RpcProviderType.QUICKNODE to "wss://your-endpoint.quiknode.pro/{apiKey}/",
RpcProviderType.CHAINSTACK to "wss://ws-polygon-mainnet.core.chainstack.com/{apiKey}",
RpcProviderType.GETBLOCK to "wss://go.getblock.io/{apiKey}/"
)
}
/**
* 获取所有节点配置不包含默认节点
* 默认节点作为兜底不应该返回给前端
*/
fun getAllNodes(): List<RpcNodeConfig> {
val allNodes = rpcNodeConfigRepository.findAllByOrderByPriorityAsc()
// 过滤掉默认节点,只返回用户配置的节点
return allNodes.filterNot { isDefaultNode(it) }
}
/**
* 获取所有节点配置包含默认节点用于内部使用
* 只返回启用的节点禁用的节点会被忽略
* 默认节点始终排在最后
*/
fun getAllNodesWithDefault(): List<RpcNodeConfig> {
// 只查询启用的节点
val allNodes = rpcNodeConfigRepository.findAllByEnabledTrueOrderByPriorityAsc()
// 分离默认节点和用户配置的节点
val (defaultNodes, userNodes) = allNodes.partition { isDefaultNode(it) }
// 返回用户配置的节点,默认节点排在最后(如果存在)
return userNodes + defaultNodes
}
/**
* 判断是否是默认节点
*/
private fun isDefaultNode(node: RpcNodeConfig): Boolean {
return node.httpUrl == DEFAULT_RPC_URL ||
node.httpUrl == DEFAULT_RPC_URL.removeSuffix("/") ||
(node.providerType == RpcProviderType.PUBLIC.name &&
(node.httpUrl.contains("polygon.publicnode.com") ||
node.httpUrl.contains("publicnode.com")))
}
/**
* 获取第一个可用的节点
* 按优先级顺序遍历所有启用的节点找到第一个真正可用的节点
* 如果所有节点都不可用返回默认节点
* @return 可用节点的配置,如果没有可用节点则返回失败
*/
fun getAvailableNode(): Result<RpcNodeConfig> {
return try {
val nodes = rpcNodeConfigRepository.findAllByEnabledTrueOrderByPriorityAsc()
.filterNot { isDefaultNode(it) } // 排除默认节点
if (nodes.isEmpty()) {
logger.warn("没有配置任何 RPC 节点,使用默认节点: $DEFAULT_RPC_URL")
return Result.failure(IllegalStateException("没有配置任何 RPC 节点"))
}
// 优先使用最近检查状态为 HEALTHY 的节点
val healthyNodes = nodes.filter {
it.lastCheckStatus == NodeHealthStatus.HEALTHY.name
}
// 先尝试使用健康的节点(按优先级排序)
for (node in healthyNodes) {
try {
// 快速验证节点是否仍然可用(使用较短的超时时间)
val checkResult = validateNode(node.httpUrl, node.wsUrl).getOrNull()
if (checkResult != null && checkResult.status == NodeHealthStatus.HEALTHY) {
logger.debug("使用健康的 RPC 节点: ${node.name} (${node.httpUrl})")
return Result.success(node)
}
} catch (e: Exception) {
logger.debug("节点 ${node.name} 验证失败,尝试下一个节点: ${e.message}")
// 继续尝试下一个节点
}
}
// 如果没有健康的节点,尝试验证所有节点(按优先级)
for (node in nodes) {
try {
val checkResult = validateNode(node.httpUrl, node.wsUrl).getOrNull()
if (checkResult != null && checkResult.status == NodeHealthStatus.HEALTHY) {
logger.info("找到可用的 RPC 节点: ${node.name} (${node.httpUrl})")
return Result.success(node)
}
} catch (e: Exception) {
logger.debug("节点 ${node.name} 验证失败,尝试下一个节点: ${e.message}")
// 继续尝试下一个节点
}
}
// 所有节点都不可用,返回失败
logger.warn("所有 RPC 节点都不可用,将使用默认节点: $DEFAULT_RPC_URL")
Result.failure(IllegalStateException("所有 RPC 节点都不可用"))
} catch (e: Exception) {
logger.error("获取可用节点失败: ${e.message}", e)
Result.failure(e)
}
}
/**
* 获取节点的 HTTP URL (如果没有配置,使用默认节点)
*/
fun getHttpUrl(): String {
val nodeResult = getAvailableNode()
return if (nodeResult.isSuccess) {
nodeResult.getOrNull()?.httpUrl ?: DEFAULT_RPC_URL
} else {
logger.warn("没有可用的用户配置节点,使用默认节点")
DEFAULT_RPC_URL
}
}
/**
* 获取节点的 WebSocket URL (如果没有配置,使用默认节点)
*/
fun getWsUrl(): String {
val nodeResult = getAvailableNode()
return if (nodeResult.isSuccess) {
nodeResult.getOrNull()?.wsUrl ?: DEFAULT_WS_URL
} else {
logger.warn("没有可用的用户配置节点,使用默认 WS 节点")
DEFAULT_WS_URL
}
}
/**
* 添加节点
*/
@Transactional
fun addNode(request: AddRpcNodeRequest): Result<RpcNodeConfig> {
return try {
// 1. 验证请求
val providerType = try {
RpcProviderType.valueOf(request.providerType.uppercase())
} catch (e: IllegalArgumentException) {
return Result.failure(IllegalArgumentException("不支持的服务商类型: ${request.providerType}"))
}
// 2. 构建 HTTP 和 WS URL
val (httpUrl, wsUrl) = if (providerType == RpcProviderType.CUSTOM) {
// 自定义节点,使用用户提供的 URL
if (request.httpUrl.isNullOrBlank()) {
return Result.failure(IllegalArgumentException("自定义节点必须提供 HTTP URL"))
}
Pair(request.httpUrl, request.wsUrl)
} else {
// 主流服务商,使用模板生成 URL
if (request.apiKey.isNullOrBlank()) {
return Result.failure(IllegalArgumentException("${request.providerType} 节点必须提供 API Key"))
}
val httpTemplate = PROVIDER_HTTP_TEMPLATES[providerType]
?: return Result.failure(IllegalArgumentException("未找到 ${request.providerType} 的 HTTP URL 模板"))
val wsTemplate = PROVIDER_WS_TEMPLATES[providerType]
Pair(
httpTemplate.replace("{apiKey}", request.apiKey),
wsTemplate?.replace("{apiKey}", request.apiKey)
)
}
// 3. 校验节点可用性
val validationResult = validateNode(httpUrl, wsUrl)
if (validationResult.isFailure) {
return Result.failure(validationResult.exceptionOrNull() ?: Exception("节点验证失败"))
}
val checkResult = validationResult.getOrNull()!!
// 检查节点是否健康,如果不健康则不允许添加
if (checkResult.status != NodeHealthStatus.HEALTHY) {
return Result.failure(IllegalArgumentException("节点不可用: ${checkResult.message}"))
}
// 4. 加密 API Key (如果有)
val encryptedApiKey = request.apiKey?.let { cryptoUtils.encrypt(it) }
// 5. 获取当前最大优先级
val maxPriority = rpcNodeConfigRepository.findAllByOrderByPriorityAsc()
.maxOfOrNull { it.priority } ?: 0
// 6. 创建节点配置
val node = RpcNodeConfig(
providerType = providerType.name,
name = request.name,
httpUrl = httpUrl,
wsUrl = wsUrl,
apiKey = encryptedApiKey,
enabled = true,
priority = maxPriority + 1, // 新节点放到最后
lastCheckTime = checkResult.checkTime,
lastCheckStatus = checkResult.status.name,
responseTimeMs = checkResult.responseTimeMs
)
val savedNode = rpcNodeConfigRepository.save(node)
logger.info("成功添加 RPC 节点: ${savedNode.name} (${savedNode.httpUrl})")
Result.success(savedNode)
} catch (e: Exception) {
logger.error("添加节点失败: ${e.message}", e)
Result.failure(e)
}
}
/**
* 更新节点
* 默认节点不允许更新作为兜底不应该返回给前端
*/
@Transactional
fun updateNode(request: UpdateRpcNodeRequest): Result<RpcNodeConfig> {
return try {
val node = rpcNodeConfigRepository.findById(request.id).orElse(null)
?: return Result.failure(IllegalArgumentException("节点不存在: ${request.id}"))
// 如果是默认节点,不允许更新
if (isDefaultNode(node)) {
return Result.failure(IllegalArgumentException("默认节点不允许更新"))
}
// 更新字段
val updatedNode = node.copy(
name = request.name ?: node.name,
enabled = request.enabled ?: node.enabled,
priority = request.priority ?: node.priority,
updatedAt = System.currentTimeMillis()
)
val savedNode = rpcNodeConfigRepository.save(updatedNode)
logger.info("成功更新 RPC 节点: ${savedNode.name}")
Result.success(savedNode)
} catch (e: Exception) {
logger.error("更新节点失败: ${e.message}", e)
Result.failure(e)
}
}
/**
* 删除节点
* 默认节点不允许删除作为兜底不应该返回给前端
*/
@Transactional
fun deleteNode(id: Long): Result<Unit> {
return try {
val node = rpcNodeConfigRepository.findById(id).orElse(null)
?: return Result.failure(IllegalArgumentException("节点不存在: $id"))
// 如果是默认节点,不允许删除
if (isDefaultNode(node)) {
return Result.failure(IllegalArgumentException("默认节点不允许删除"))
}
rpcNodeConfigRepository.delete(node)
logger.info("成功删除 RPC 节点: ${node.name}")
Result.success(Unit)
} catch (e: Exception) {
logger.error("删除节点失败: ${e.message}", e)
Result.failure(e)
}
}
/**
* 更新节点优先级
* 默认节点不允许更新优先级作为兜底始终排在最后
*/
@Transactional
fun updatePriority(id: Long, priority: Int): Result<Unit> {
return try {
val node = rpcNodeConfigRepository.findById(id).orElse(null)
?: return Result.failure(IllegalArgumentException("节点不存在: $id"))
// 如果是默认节点,不允许更新优先级
if (isDefaultNode(node)) {
return Result.failure(IllegalArgumentException("默认节点不允许更新优先级"))
}
val updatedNode = node.copy(
priority = priority,
updatedAt = System.currentTimeMillis()
)
rpcNodeConfigRepository.save(updatedNode)
logger.info("成功更新节点优先级: ${node.name} -> $priority")
Result.success(Unit)
} catch (e: Exception) {
logger.error("更新节点优先级失败: ${e.message}", e)
Result.failure(e)
}
}
/**
* 检查单个节点健康状态
* 默认节点不应该被检查作为兜底不应该返回给前端
*/
@Transactional
fun checkNodeHealth(nodeId: Long): Result<NodeCheckResult> {
return try {
val node = rpcNodeConfigRepository.findById(nodeId).orElse(null)
?: return Result.failure(IllegalArgumentException("节点不存在: $nodeId"))
// 如果是默认节点,不允许检查
if (isDefaultNode(node)) {
return Result.failure(IllegalArgumentException("默认节点不允许检查"))
}
val checkResult = validateNode(node.httpUrl, node.wsUrl).getOrThrow()
// 更新节点健康状态
val updatedNode = node.copy(
lastCheckTime = checkResult.checkTime,
lastCheckStatus = checkResult.status.name,
responseTimeMs = checkResult.responseTimeMs,
updatedAt = System.currentTimeMillis()
)
rpcNodeConfigRepository.save(updatedNode)
logger.info("检查节点健康状态: ${node.name} -> ${checkResult.status}")
Result.success(checkResult)
} catch (e: Exception) {
logger.error("检查节点健康状态失败: ${e.message}", e)
Result.failure(e)
}
}
/**
* 批量检查所有节点健康状态不包含默认节点和禁用的节点
* 默认节点作为兜底不应该返回给前端
* 禁用的节点不应该被检查
*/
@Transactional
fun checkAllNodesHealth(): Result<Map<Long, NodeCheckResult>> {
return try {
// 只查询启用的节点,过滤掉默认节点和禁用的节点
val allNodes = rpcNodeConfigRepository.findAllByEnabledTrueOrderByPriorityAsc()
// 过滤掉默认节点,只检查用户配置的启用节点
val nodes = allNodes.filterNot { isDefaultNode(it) }
val results = mutableMapOf<Long, NodeCheckResult>()
for (node in nodes) {
try {
val checkResult = validateNode(node.httpUrl, node.wsUrl).getOrNull()
if (checkResult != null) {
results[node.id!!] = checkResult
// 更新节点状态
val updatedNode = node.copy(
lastCheckTime = checkResult.checkTime,
lastCheckStatus = checkResult.status.name,
responseTimeMs = checkResult.responseTimeMs,
updatedAt = System.currentTimeMillis()
)
rpcNodeConfigRepository.save(updatedNode)
}
} catch (e: Exception) {
logger.error("检查节点 ${node.name} 失败: ${e.message}", e)
}
}
Result.success(results)
} catch (e: Exception) {
logger.error("批量检查节点健康状态失败: ${e.message}", e)
Result.failure(e)
}
}
/**
* 校验节点可用性
* 调用 eth_blockNumber 验证节点是否可用
*/
private fun validateNode(httpUrl: String, wsUrl: String?): Result<NodeCheckResult> {
return try {
logger.debug("开始验证节点: $httpUrl")
// 创建临时 RPC API
val rpcApi = retrofitFactory.createEthereumRpcApi(httpUrl)
// 调用 eth_blockNumber
val startTime = System.currentTimeMillis()
val rpcRequest = JsonRpcRequest(
method = "eth_blockNumber",
params = emptyList()
)
val response = kotlinx.coroutines.runBlocking { rpcApi.call(rpcRequest) }
val responseTime = (System.currentTimeMillis() - startTime).toInt()
if (!response.isSuccessful || response.body() == null) {
logger.warn("节点验证失败: HTTP ${response.code()}")
return Result.success(NodeCheckResult(
status = NodeHealthStatus.UNHEALTHY,
message = "HTTP 请求失败: ${response.code()}",
checkTime = System.currentTimeMillis(),
responseTimeMs = responseTime
))
}
val rpcResponse = response.body()!!
if (rpcResponse.error != null) {
logger.warn("节点验证失败: RPC 错误 ${rpcResponse.error.message}")
return Result.success(NodeCheckResult(
status = NodeHealthStatus.UNHEALTHY,
message = "RPC 错误: ${rpcResponse.error.message}",
checkTime = System.currentTimeMillis(),
responseTimeMs = responseTime
))
}
val blockNumber = rpcResponse.result?.asString
if (blockNumber.isNullOrBlank()) {
return Result.success(NodeCheckResult(
status = NodeHealthStatus.UNHEALTHY,
message = "区块号为空",
checkTime = System.currentTimeMillis(),
responseTimeMs = responseTime
))
}
logger.info("节点验证成功: $httpUrl, 区块号: $blockNumber, 响应时间: ${responseTime}ms")
Result.success(NodeCheckResult(
status = NodeHealthStatus.HEALTHY,
message = "节点可用, 当前区块: $blockNumber",
checkTime = System.currentTimeMillis(),
responseTimeMs = responseTime,
blockNumber = blockNumber
))
} catch (e: Exception) {
logger.error("验证节点失败: ${e.message}", e)
Result.success(NodeCheckResult(
status = NodeHealthStatus.UNHEALTHY,
message = "验证失败: ${e.message}",
checkTime = System.currentTimeMillis(),
responseTimeMs = null
))
}
}
}
/**
* 添加节点请求
*/
data class AddRpcNodeRequest(
val providerType: String, // ALCHEMY, INFURA, QUICKNODE, CHAINSTACK, GETBLOCK, CUSTOM, PUBLIC
val name: String,
val apiKey: String? = null, // 主流服务商需要
val httpUrl: String? = null, // CUSTOM 需要
val wsUrl: String? = null
)
/**
* 更新节点请求
*/
data class UpdateRpcNodeRequest(
val id: Long,
val name: String? = null,
val enabled: Boolean? = null,
val priority: Int? = null
)
/**
* 节点检查结果
*/
data class NodeCheckResult(
val status: NodeHealthStatus,
val message: String,
val checkTime: Long,
val responseTimeMs: Int?,
val blockNumber: String? = null
)
@@ -25,6 +25,19 @@ class SystemConfigService(
const val CONFIG_KEY_BUILDER_SECRET = "builder.secret"
const val CONFIG_KEY_BUILDER_PASSPHRASE = "builder.passphrase"
const val CONFIG_KEY_AUTO_REDEEM = "auto_redeem"
/**
* 遮蔽敏感信息仅显示前4位和后4位
* 例如abcd1234...wxyz5678
*/
fun maskSensitiveValue(value: String?): String? {
if (value == null) return null
return when {
value.length <= 8 -> "****" // 太短则完全遮蔽
value.length <= 16 -> "${value.take(2)}...${value.takeLast(2)}"
else -> "${value.take(4)}...${value.takeLast(4)}"
}
}
}
/**
@@ -36,26 +49,26 @@ class SystemConfigService(
val builderPassphrase = getConfigValue(CONFIG_KEY_BUILDER_PASSPHRASE)
val autoRedeem = isAutoRedeemEnabled()
// 获取完整的 API Key(用于前端展示
val builderApiKeyDisplay = builderApiKey?.let {
// 获取遮蔽后的显示值(仅显示部分字符,用于前端确认配置
val builderApiKeyDisplay = builderApiKey?.let {
try {
cryptoUtils.decrypt(it)
maskSensitiveValue(cryptoUtils.decrypt(it))
} catch (e: Exception) {
null
}
}
val builderSecretDisplay = builderSecret?.let {
try {
cryptoUtils.decrypt(it)
maskSensitiveValue(cryptoUtils.decrypt(it))
} catch (e: Exception) {
null
}
}
val builderPassphraseDisplay = builderPassphrase?.let {
try {
cryptoUtils.decrypt(it)
maskSensitiveValue(cryptoUtils.decrypt(it))
} catch (e: Exception) {
null
}
@@ -65,6 +65,9 @@ class TelegramNotificationService(
marketId: String? = null,
marketSlug: String? = null,
side: String,
price: String? = null, // 订单价格(可选,如果提供则直接使用)
size: String? = null, // 订单数量(可选,如果提供则直接使用)
outcome: String? = null, // 市场方向(可选,如果提供则直接使用)
accountName: String? = null,
walletAddress: String? = null,
clobApi: PolymarketClobApi? = null,
@@ -84,35 +87,49 @@ class TelegramNotificationService(
java.util.Locale("zh", "CN") // 默认简体中文
}
// 尝试从订单详情获取实际价格和数量
var actualPrice: String? = null
var actualSize: String? = null
// 优先使用传入的价格和数量,如果没有提供则尝试从订单详情获取
var actualPrice: String? = price
var actualSize: String? = size
var actualSide: String = side
var actualOutcome: String? = null // 市场方向(outcome
var actualOutcome: String? = outcome
if (orderId != null && clobApi != null && apiKey != null && apiSecret != null && apiPassphrase != null && walletAddressForApi != null) {
// 如果价格或数量未提供,尝试从订单详情获取
if ((actualPrice == null || actualSize == null) && orderId != null && clobApi != null && apiKey != null && apiSecret != null && apiPassphrase != null && walletAddressForApi != null) {
try {
val orderResponse = clobApi.getOrder(orderId)
if (orderResponse.isSuccessful && orderResponse.body() != null) {
val order = orderResponse.body()!!
actualPrice = order.price
actualSize = order.originalSize // 使用 originalSize 作为订单数量
actualSide = order.side // 使用订单详情中的 side
actualOutcome = order.outcome // 使用订单详情中的 outcome(市场方向)
if (orderResponse.isSuccessful) {
val order = orderResponse.body()
if (order != null) {
if (actualPrice == null) {
actualPrice = order.price
}
if (actualSize == null) {
actualSize = order.originalSize // 使用 originalSize 作为订单数量
}
actualSide = order.side // 使用订单详情中的 side
if (actualOutcome == null) {
actualOutcome = order.outcome // 使用订单详情中的 outcome(市场方向)
}
} else {
logger.debug("查询订单详情失败: 响应体为空, orderId=$orderId")
}
} else {
val errorBody = orderResponse.errorBody()?.string()?.take(200) ?: "无错误详情"
logger.debug("查询订单详情失败: orderId=$orderId, code=${orderResponse.code()}, errorBody=$errorBody")
}
} catch (e: Exception) {
logger.warn("查询订单详情失败,使用默认值: ${e.message}", e)
logger.warn("查询订单详情失败: orderId=$orderId, ${e.message}", e)
}
}
// 如果没有获取到实际值,使用默认值(这种情况不应该发生,但为了兼容性保留)
val price = actualPrice ?: "0"
val size = actualSize ?: "0"
// 如果仍然没有获取到实际值,使用默认值(这种情况不应该发生,但为了兼容性保留)
val finalPrice = actualPrice ?: "0"
val finalSize = actualSize ?: "0"
// 计算订单金额 = price × sizeUSDC
val amount = try {
val priceDecimal = price.toSafeBigDecimal()
val sizeDecimal = size.toSafeBigDecimal()
val priceDecimal = finalPrice.toSafeBigDecimal()
val sizeDecimal = finalSize.toSafeBigDecimal()
priceDecimal.multiply(sizeDecimal).toString()
} catch (e: Exception) {
logger.warn("计算订单金额失败: ${e.message}", e)
@@ -126,8 +143,8 @@ class TelegramNotificationService(
marketSlug = marketSlug,
side = actualSide,
outcome = actualOutcome,
price = price,
size = size,
price = finalPrice,
size = finalSize,
amount = amount,
accountName = accountName,
walletAddress = walletAddress,
@@ -296,18 +313,8 @@ class TelegramNotificationService(
else -> filterType
}
// 优先使用账户名称,如果没有账户名称才显示钱包地址
val accountInfo: String = when {
!accountName.isNullOrBlank() -> {
accountName!!
}
!walletAddress.isNullOrBlank() -> {
maskAddress(walletAddress!!)
}
else -> {
unknownAccount
}
}
// 构建账户信息(格式:账户名(钱包地址)
val accountInfo = buildAccountInfo(accountName, walletAddress, unknownAccount)
val time = DateUtils.formatDateTime()
@@ -359,13 +366,17 @@ class TelegramNotificationService(
""
}
// 格式化价格和数量
val priceDisplay = formatPrice(price)
val sizeDisplay = formatQuantity(size)
return """🚫 <b>$orderFiltered</b>
📊 <b>$orderInfo</b>
$marketLabel: $marketDisplay$outcomeDisplay
$sideLabel: <b>$sideDisplay</b>
$priceLabel: <code>$price</code>
$quantityLabel: <code>$size</code> shares
$priceLabel: <code>$priceDisplay</code>
$quantityLabel: <code>$sizeDisplay</code> shares
$amountLabel: <code>$amountDisplay</code> USDC
$accountLabel: $escapedAccountInfo
@@ -570,6 +581,68 @@ class TelegramNotificationService(
}
}
/**
* 格式化价格显示保留最多4位小数截断不四舍五入
*/
private fun formatPrice(price: String): String {
return try {
val priceDecimal = price.toSafeBigDecimal()
val formatted = if (priceDecimal.scale() > 4) {
priceDecimal.setScale(4, java.math.RoundingMode.DOWN).stripTrailingZeros()
} else {
priceDecimal.stripTrailingZeros()
}
formatted.toPlainString()
} catch (e: Exception) {
price
}
}
/**
* 格式化数量显示保留最多2位小数截断不四舍五入
*/
private fun formatQuantity(quantity: String): String {
return try {
val quantityDecimal = quantity.toSafeBigDecimal()
val formatted = if (quantityDecimal.scale() > 2) {
quantityDecimal.setScale(2, java.math.RoundingMode.DOWN).stripTrailingZeros()
} else {
quantityDecimal.stripTrailingZeros()
}
formatted.toPlainString()
} catch (e: Exception) {
quantity
}
}
/**
* 构建账户信息显示格式账户名(钱包地址)
*/
private fun buildAccountInfo(
accountName: String?,
walletAddress: String?,
unknownAccount: String
): String {
return when {
!accountName.isNullOrBlank() && !walletAddress.isNullOrBlank() -> {
// 有账户名和钱包地址:账户名(钱包地址)
"${accountName}(${maskAddress(walletAddress)})"
}
!accountName.isNullOrBlank() -> {
// 只有账户名
accountName
}
!walletAddress.isNullOrBlank() -> {
// 只有钱包地址
maskAddress(walletAddress)
}
else -> {
// 都没有
unknownAccount
}
}
}
/**
* 构建订单成功消息
*/
@@ -613,18 +686,8 @@ class TelegramNotificationService(
else -> side
}
// 优先使用账户名称,如果没有账户名称才显示钱包地址
val accountInfo: String = when {
!accountName.isNullOrBlank() -> {
accountName!!
}
!walletAddress.isNullOrBlank() -> {
maskAddress(walletAddress!!)
}
else -> {
unknownAccount
}
}
// 构建账户信息(格式:账户名(钱包地址)
val accountInfo = buildAccountInfo(accountName, walletAddress, unknownAccount)
// 构建跟单信息(如果有)
val copyTradingInfo = mutableListOf<String>()
@@ -694,14 +757,18 @@ class TelegramNotificationService(
""
}
// 格式化价格和数量
val priceDisplay = formatPrice(price)
val sizeDisplay = formatQuantity(size)
return """✅ <b>$orderCreatedSuccess</b>
📊 <b>$orderInfo</b>
$orderIdLabel: <code>${orderId ?: unknown}</code>
$marketLabel: $marketDisplay$outcomeDisplay
$sideLabel: <b>$sideDisplay</b>
$priceLabel: <code>$price</code>
$quantityLabel: <code>$size</code> shares
$priceLabel: <code>$priceDisplay</code>
$quantityLabel: <code>$sizeDisplay</code> shares
$amountLabel: <code>$amountDisplay</code> USDC
$accountLabel: $escapedAccountInfo$escapedCopyTradingInfo
@@ -748,18 +815,8 @@ class TelegramNotificationService(
else -> side
}
// 优先使用账户名称,如果没有账户名称才显示钱包地址
val accountInfo: String = when {
!accountName.isNullOrBlank() -> {
accountName!!
}
!walletAddress.isNullOrBlank() -> {
maskAddress(walletAddress!!)
}
else -> {
unknownAccount
}
}
// 构建账户信息(格式:账户名(钱包地址)
val accountInfo = buildAccountInfo(accountName, walletAddress, unknownAccount)
val time = DateUtils.formatDateTime()
@@ -818,13 +875,17 @@ class TelegramNotificationService(
""
}
// 格式化价格和数量
val priceDisplay = formatPrice(price)
val sizeDisplay = formatQuantity(size)
return """❌ <b>$orderCreatedFailed</b>
📊 <b>$orderInfo</b>
$marketLabel: $marketDisplay$outcomeDisplay
$sideLabel: <b>$sideDisplay</b>
$priceLabel: <code>$price</code>
$quantityLabel: <code>$size</code> shares
$priceLabel: <code>$priceDisplay</code>
$quantityLabel: <code>$sizeDisplay</code> shares
$amountLabel: <code>$amountDisplay</code> USDC
$accountLabel: $escapedAccountInfo
@@ -889,18 +950,8 @@ class TelegramNotificationService(
val timeLabel = messageSource.getMessage("notification.order.time", null, "时间", locale)
val unknownAccount: String = messageSource.getMessage("notification.order.unknown_account", null, "未知账户", locale) ?: "未知账户"
// 优先使用账户名称,如果没有账户名称才显示钱包地址
val accountInfo: String = when {
!accountName.isNullOrBlank() -> {
accountName!!
}
!walletAddress.isNullOrBlank() -> {
maskAddress(walletAddress!!)
}
else -> {
unknownAccount
}
}
// 构建账户信息(格式:账户名(钱包地址)
val accountInfo = buildAccountInfo(accountName, walletAddress, unknownAccount)
val time = DateUtils.formatDateTime()
@@ -923,12 +974,7 @@ class TelegramNotificationService(
// 构建仓位列表
val positionsText = positions.joinToString("\n") { position ->
val quantityDisplay = try {
val quantityDecimal = position.quantity.toSafeBigDecimal()
quantityDecimal.stripTrailingZeros().toPlainString()
} catch (e: Exception) {
position.quantity
}
val quantityDisplay = formatQuantity(position.quantity)
val valueDisplay = try {
val valueDecimal = position.value.toSafeBigDecimal()
val formatted = if (valueDecimal.scale() > 4) {
@@ -19,7 +19,7 @@ object EthereumUtils {
* @return 函数选择器例如 "0x12345678"
*/
fun getFunctionSelector(functionSignature: String): String {
val hash = keccak256(functionSignature.toByteArray())
val hash = keccak256Hex(functionSignature.toByteArray())
return "0x" + hash.substring(0, 8)
}
@@ -77,16 +77,109 @@ object EthereumUtils {
return BigInteger(cleanHex, 16)
}
/**
* ABI 编码的响应中解析 uint256 数组
* ABI 编码格式
* - offset (32 bytes): 数组数据的位置偏移量
* - length (32 bytes): 数组长度
* - data: 每个元素 32 字节
* @param hexResult 十六进制结果完整的 ABI 编码响应
* @param offset 数组数据的偏移位置字节数 offset 位置开始读取
* @return BigInteger 数组
*/
fun decodeUint256Array(hexResult: String, offset: Int = 0): List<BigInteger> {
val cleanHex = hexResult.removePrefix("0x")
if (cleanHex.length < (offset + 1) * 64) {
return emptyList()
}
// 从 offset 位置开始读取
val startPos = offset * 64 // 每个 uint256 是 64 个十六进制字符
val lengthHex = cleanHex.substring(startPos, startPos + 64)
val length = BigInteger(lengthHex, 16).toInt()
if (length <= 0 || length > 100) { // 防止异常数据
return emptyList()
}
val result = mutableListOf<BigInteger>()
for (i in 0 until length) {
val elementStart = startPos + 64 + (i * 64) // 跳过长度字段
if (elementStart + 64 > cleanHex.length) {
break
}
val elementHex = cleanHex.substring(elementStart, elementStart + 64)
result.add(BigInteger(elementHex, 16))
}
return result
}
/**
* ABI 编码的元组响应中解析数据
* 用于解析 getCondition 返回的 (uint256 payoutDenominator, uint256[] payouts)
* @param hexResult 十六进制结果
* @return Pair<payoutDenominator, payouts>
*/
fun decodeConditionResult(hexResult: String): Pair<BigInteger, List<BigInteger>> {
val cleanHex = hexResult.removePrefix("0x")
// 第一个 32 字节:payoutDenominator
val payoutDenominatorHex = cleanHex.substring(0, 64)
val payoutDenominator = BigInteger(payoutDenominatorHex, 16)
// 第二个 32 字节:payouts 数组的偏移量(通常是 0x40 = 64 字节)
val offsetHex = cleanHex.substring(64, 128)
val offset = BigInteger(offsetHex, 16).toInt() / 32 // 转换为 32 字节单位
// 从 offset 位置解析数组
val payouts = decodeUint256Array(hexResult, offset)
return Pair(payoutDenominator, payouts)
}
/**
* 将十六进制字符串转换为字节数组
* @param hex 十六进制字符串带或不带 0x 前缀
* @return 字节数组
*/
fun hexToBytes(hex: String): ByteArray {
val cleanHex = hex.removePrefix("0x")
return ByteArray(cleanHex.length / 2) { i ->
cleanHex.substring(i * 2, i * 2 + 2).toInt(16).toByte()
}
}
/**
* 将字节数组转换为十六进制字符串
* @param bytes 字节数组
* @return 十六进制字符串 0x 前缀
*/
fun bytesToHex(bytes: ByteArray): String {
return "0x" + bytes.joinToString("") { "%02x".format(it) }
}
/**
* 计算 Keccak-256 哈希Ethereum 标准
* 使用 BouncyCastle 库实现真正的 Keccak-256
* @param data 输入数据
* @return 32 字节的哈希值
*/
private fun keccak256(data: ByteArray): String {
fun keccak256(data: ByteArray): ByteArray {
val digest = KeccakDigest(256)
digest.update(data, 0, data.size)
val hash = ByteArray(digest.digestSize)
digest.doFinal(hash, 0)
return hash.joinToString("") { "%02x".format(it) }
return hash
}
/**
* 计算 Keccak-256 哈希并返回十六进制字符串
* @param data 输入数据
* @return 十六进制哈希字符串
*/
fun keccak256Hex(data: ByteArray): String {
return keccak256(data).joinToString("") { "%02x".format(it) }
}
}
@@ -2,14 +2,16 @@ package com.wrbug.polymarketbot.util
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import org.springframework.stereotype.Component
/**
* JSON 工具类
* 用于解析 JSON 字符串
*/
object JsonUtils {
private val gson = Gson()
@Component
class JsonUtils(
private val gson: Gson
) {
/**
* 解析 JSON 字符串数组
@@ -28,5 +30,29 @@ object JsonUtils {
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
}
}
}
@@ -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
}
}
}
@@ -1,37 +1,139 @@
package com.wrbug.polymarketbot.util
import com.google.gson.Gson
import com.google.gson.GsonBuilder
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 okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.Interceptor
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
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
import java.io.IOException
import java.util.concurrent.ConcurrentHashMap
import jakarta.annotation.PreDestroy
/**
* Retrofit 客户端工厂
* 用于创建带认证的 Polymarket CLOB API 客户端和 Ethereum RPC API 客户端
*
* 注意为了避免内存泄漏本类会缓存和复用客户端实例
*/
@Component
class RetrofitFactory(
@Value("\${polymarket.clob.base-url}")
private val clobBaseUrl: String,
@Value("\${polymarket.gamma.base-url}")
private val gammaBaseUrl: String
private val gammaBaseUrl: String,
private val gson: Gson
) {
private val logger = LoggerFactory.getLogger(RetrofitFactory::class.java)
// 共享的 OkHttpClient(用于不需要认证的 API)
private val sharedOkHttpClient: OkHttpClient by lazy {
createClient().build()
}
// 共享的 OkHttpClient(用于需要跟随重定向的 API)
private val sharedOkHttpClientWithRedirect: OkHttpClient by lazy {
createClient()
.followRedirects(true)
.followSslRedirects(true)
.build()
}
// 缓存 Gamma API 客户端(单例)
private val gammaApi: PolymarketGammaApi by lazy {
val baseUrl = if (gammaBaseUrl.endsWith("/")) {
gammaBaseUrl.dropLast(1)
} else {
gammaBaseUrl
}
Retrofit.Builder()
.baseUrl("$baseUrl/")
.client(sharedOkHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(PolymarketGammaApi::class.java)
}
// 缓存 Data API 客户端(单例)
private val dataApi: PolymarketDataApi by lazy {
val baseUrl = "https://data-api.polymarket.com"
Retrofit.Builder()
.baseUrl("$baseUrl/")
.client(sharedOkHttpClientWithRedirect)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(PolymarketDataApi::class.java)
}
// 缓存 GitHub API 客户端(单例)
private val githubApi: GitHubApi by lazy {
val baseUrl = "https://api.github.com"
// 添加拦截器,设置 Accept 头以获取 reactions 数据
val githubInterceptor = object : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request().newBuilder()
.header("Accept", "application/vnd.github+json")
.build()
return chain.proceed(request)
}
}
val okHttpClient = createClient()
.addInterceptor(githubInterceptor)
.build()
Retrofit.Builder()
.baseUrl("$baseUrl/")
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(GitHubApi::class.java)
}
// 缓存不带认证的 CLOB API 客户端(单例)
private val clobApiWithoutAuth: PolymarketClobApi by lazy {
Retrofit.Builder()
.baseUrl(clobBaseUrl)
.client(sharedOkHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(PolymarketClobApi::class.java)
}
// 缓存带认证的 CLOB API 客户端:walletAddress -> PolymarketClobApi
// 注意:每个账户使用不同的 API Key,需要不同的客户端
private val clobApiCache = ConcurrentHashMap<String, PolymarketClobApi>()
// 缓存 RPC API 客户端:rpcUrl -> EthereumRpcApi
private val rpcApiCache = ConcurrentHashMap<String, EthereumRpcApi>()
// 缓存 Builder Relayer API 客户端:relayerUrl -> BuilderRelayerApi
private val builderRelayerApiCache = ConcurrentHashMap<String, BuilderRelayerApi>()
/**
* 创建带认证的 Polymarket CLOB API 客户端
* 按钱包地址缓存避免重复创建
* @param apiKey API Key
* @param apiSecret API Secret
* @param apiPassphrase API Passphrase
@@ -44,129 +146,169 @@ class RetrofitFactory(
apiPassphrase: String,
walletAddress: String
): PolymarketClobApi {
val authInterceptor = PolymarketAuthInterceptor(apiKey, apiSecret, apiPassphrase, walletAddress)
// 添加响应日志拦截器,用于调试 JSON 解析错误
val responseLoggingInterceptor = ResponseLoggingInterceptor()
val okHttpClient = createClient()
.addInterceptor(authInterceptor)
.addInterceptor(responseLoggingInterceptor)
.build()
// 创建 lenient 模式的 Gson,允许解析格式不严格的 JSON
val gson = GsonBuilder()
.setLenient()
.create()
return Retrofit.Builder()
.baseUrl(clobBaseUrl)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(PolymarketClobApi::class.java)
// 使用钱包地址作为缓存键(每个账户使用不同的 API Key)
return clobApiCache.computeIfAbsent(walletAddress) {
val authInterceptor = PolymarketAuthInterceptor(apiKey, apiSecret, apiPassphrase, walletAddress)
// 添加响应日志拦截器,用于调试 JSON 解析错误
val responseLoggingInterceptor = ResponseLoggingInterceptor()
val okHttpClient = createClient()
.addInterceptor(authInterceptor)
.addInterceptor(responseLoggingInterceptor)
.build()
Retrofit.Builder()
.baseUrl(clobBaseUrl)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(PolymarketClobApi::class.java)
}
}
/**
* 创建不带认证的 Polymarket CLOB API 客户端
* 用于不需要认证的查询接口
* @return PolymarketClobApi 客户端
* @return PolymarketClobApi 客户端单例
*/
fun createClobApiWithoutAuth(): PolymarketClobApi {
// 添加响应日志拦截器,用于调试 JSON 解析错误
val responseLoggingInterceptor = ResponseLoggingInterceptor()
val okHttpClient = createClient()
.addInterceptor(responseLoggingInterceptor)
.build()
// 创建 lenient 模式的 Gson,允许解析格式不严格的 JSON
val gson = GsonBuilder()
.setLenient()
.create()
return Retrofit.Builder()
.baseUrl(clobBaseUrl)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(PolymarketClobApi::class.java)
return clobApiWithoutAuth
}
/**
* 创建 Ethereum RPC API 客户端
* 使用固定的 baseUrl通过拦截器动态替换为实际的 RPC URL
* 如果 RPC 不可用将抛出异常
* RPC URL 缓存避免重复创建
* @param rpcUrl RPC 节点 URL
* @return EthereumRpcApi 客户端
* @throws IllegalArgumentException 如果 RPC URL 无效或不可用
*/
fun createEthereumRpcApi(rpcUrl: String): EthereumRpcApi {
val okHttpClient = createClient().build()
// 确保实际的 RPC URL 以 / 结尾
val actualRpcUrl = if (rpcUrl.endsWith("/")) {
rpcUrl
} else {
"$rpcUrl/"
}
// 创建 lenient 模式的 Gson
val gson = GsonBuilder()
.setLenient()
.create()
return Retrofit.Builder()
.baseUrl(rpcUrl)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(EthereumRpcApi::class.java)
// 使用 RPC URL 作为缓存键
return rpcApiCache.computeIfAbsent(actualRpcUrl) {
// 验证 RPC 是否可用(仅在新创建时验证)
validateRpcAvailability(actualRpcUrl)
// 使用固定的 baseUrlRetrofit 要求 baseUrl 必须以 / 结尾)
val fixedBaseUrl = "https://polyrpc.polyhermes/"
// 创建 URL 替换拦截器
val urlReplaceInterceptor = RpcUrlReplaceInterceptor(fixedBaseUrl, actualRpcUrl)
val okHttpClient = createClient()
.addInterceptor(urlReplaceInterceptor)
.build()
Retrofit.Builder()
.baseUrl(fixedBaseUrl)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(EthereumRpcApi::class.java)
}
}
/**
* 验证 RPC 节点是否可用
* 通过发送一个简单的 eth_blockNumber 请求来验证
* @param rpcUrl RPC 节点 URL
* @throws IllegalArgumentException 如果 RPC 不可用
*/
private fun validateRpcAvailability(rpcUrl: String) {
try {
// 解析 URL
val httpUrl = rpcUrl.toHttpUrlOrNull()
?: throw IllegalArgumentException("无效的 RPC URL: $rpcUrl")
// 创建 JSON-RPC 请求体
val jsonRpcRequest = """
{
"jsonrpc": "2.0",
"method": "eth_blockNumber",
"params": [],
"id": 1
}
""".trimIndent()
val mediaType = "application/json; charset=utf-8".toMediaType()
val requestBody = jsonRpcRequest.toRequestBody(mediaType)
// 创建请求
val request = Request.Builder()
.url(httpUrl)
.post(requestBody)
.header("Content-Type", "application/json")
.build()
// 创建临时客户端用于验证(使用较短的超时时间)
val testClient = createClient()
.connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.SECONDS)
.writeTimeout(5, TimeUnit.SECONDS)
.build()
// 发送请求
val response = testClient.newCall(request).execute()
if (!response.isSuccessful) {
throw IllegalArgumentException("RPC 节点不可用: HTTP ${response.code} ${response.message}")
}
val responseBody = response.body?.string()
if (responseBody.isNullOrBlank()) {
throw IllegalArgumentException("RPC 节点响应为空")
}
// 检查响应是否包含错误
if (responseBody.contains("\"error\"")) {
throw IllegalArgumentException("RPC 节点返回错误: $responseBody")
}
// 检查响应是否包含 result
if (!responseBody.contains("\"result\"")) {
throw IllegalArgumentException("RPC 节点响应格式错误: $responseBody")
}
logger.debug("RPC 节点验证成功: $rpcUrl")
} catch (e: IllegalArgumentException) {
logger.error("RPC 节点验证失败: $rpcUrl - ${e.message}")
throw e
} catch (e: Exception) {
logger.error("RPC 节点验证失败: $rpcUrl - ${e.message}", e)
throw IllegalArgumentException("RPC 节点不可用: ${e.message}", e)
}
}
/**
* 创建 Polymarket Gamma API 客户端
* Gamma API 是公开 API不需要认证
* @return PolymarketGammaApi 客户端
* @return PolymarketGammaApi 客户端单例
*/
fun createGammaApi(): PolymarketGammaApi {
val baseUrl = if (gammaBaseUrl.endsWith("/")) {
gammaBaseUrl.dropLast(1)
} else {
gammaBaseUrl
}
val okHttpClient = createClient().build()
// 创建 lenient 模式的 Gson
val gson = GsonBuilder()
.setLenient()
.create()
return Retrofit.Builder()
.baseUrl("$baseUrl/")
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(PolymarketGammaApi::class.java)
return gammaApi
}
/**
* 创建 Polymarket Data API 客户端
* Data API 是公开 API不需要认证
* @return PolymarketDataApi 客户端
* @return PolymarketDataApi 客户端单例
*/
fun createDataApi(): PolymarketDataApi {
val baseUrl = "https://data-api.polymarket.com"
val okHttpClient = createClient()
.followRedirects(true)
.followSslRedirects(true)
.build()
// 创建 lenient 模式的 Gson
val gson = GsonBuilder()
.setLenient()
.create()
return Retrofit.Builder()
.baseUrl("$baseUrl/")
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(PolymarketDataApi::class.java)
return dataApi
}
/**
* 创建 Builder Relayer API 客户端
* relayerUrl 缓存避免重复创建
* @param relayerUrl Builder Relayer URL
* @param apiKey Builder API Key
* @param secret Builder Secret
@@ -185,45 +327,120 @@ class RetrofitFactory(
relayerUrl
}
// 添加 Builder 认证拦截器
val builderAuthInterceptor = BuilderAuthInterceptor(apiKey, secret, passphrase)
val okHttpClient = createClient()
.addInterceptor(builderAuthInterceptor)
.build()
val gson = GsonBuilder()
.setLenient()
.create()
return Retrofit.Builder()
.baseUrl("$baseUrl/")
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(BuilderRelayerApi::class.java)
// 使用 baseUrl 作为缓存键(注意:如果 API Key 变化,需要清理缓存)
return builderRelayerApiCache.computeIfAbsent(baseUrl) {
// 添加 Builder 认证拦截器
val builderAuthInterceptor = BuilderAuthInterceptor(apiKey, secret, passphrase)
val okHttpClient = createClient()
.addInterceptor(builderAuthInterceptor)
.build()
Retrofit.Builder()
.baseUrl("$baseUrl/")
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(BuilderRelayerApi::class.java)
}
}
/**
* 创建 GitHub API 客户端
* GitHub API 是公开 API不需要认证但建议使用 token 提高速率限制
* 添加 Accept 头以获取 reactions 数据
* @return GitHubApi 客户端
* @return GitHubApi 客户端单例
*/
fun createGitHubApi(): GitHubApi {
val baseUrl = "https://api.github.com"
return githubApi
}
/**
* 清理缓存用于测试或配置变更时
*/
@PreDestroy
fun destroy() {
logger.info("清理 RetrofitFactory 缓存")
clobApiCache.clear()
rpcApiCache.clear()
builderRelayerApiCache.clear()
}
/**
* 清理指定钱包地址的 CLOB API 缓存
* 用于 API Key 变更时
*/
fun clearClobApiCache(walletAddress: String) {
clobApiCache.remove(walletAddress)
logger.debug("已清理 CLOB API 缓存: $walletAddress")
}
/**
* 清理指定 RPC URL RPC API 缓存
* 用于 RPC 节点变更时
*/
fun clearRpcApiCache(rpcUrl: String) {
val actualRpcUrl = if (rpcUrl.endsWith("/")) rpcUrl else "$rpcUrl/"
rpcApiCache.remove(actualRpcUrl)
logger.debug("已清理 RPC API 缓存: $actualRpcUrl")
}
}
/**
* RPC URL 替换拦截器
* 用于将固定的 baseUrl 替换为实际的 RPC URL
*/
class RpcUrlReplaceInterceptor(
private val fixedBaseUrl: String,
private val actualRpcUrl: String
) : Interceptor {
private val logger = LoggerFactory.getLogger(RpcUrlReplaceInterceptor::class.java)
@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest = chain.request()
val originalUrl = originalRequest.url
// 添加拦截器,设置 Accept 头以获取 reactions 数据
val githubInterceptor = object : Interceptor {
// 将固定 baseUrl 替换为实际的 RPC URL
val originalUrlString = originalUrl.toString()
val newUrlString = originalUrlString.replace(fixedBaseUrl, actualRpcUrl)
// 使用 HttpUrl 解析新 URL,确保格式正确
val newUrl = newUrlString.toHttpUrlOrNull()
?: throw IllegalArgumentException("无效的 RPC URL: $newUrlString")
logger.debug("RPC URL 替换: $originalUrlString -> $newUrlString")
val newRequest = originalRequest.newBuilder()
.url(newUrl)
.build()
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("Accept", "application/vnd.github+json")
.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(githubInterceptor)
.addInterceptor(nbaStatsInterceptor)
.build()
val gson = GsonBuilder()
@@ -231,11 +448,11 @@ class RetrofitFactory(
.create()
return Retrofit.Builder()
.baseUrl("$baseUrl/")
.baseUrl(baseUrl)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(GitHubApi::class.java)
.create(NbaStatsApi::class.java)
}
}
@@ -259,22 +476,35 @@ class ResponseLoggingInterceptor : Interceptor {
val responseBody = response.peekBody(2048)
val responseBodyString = responseBody.string()
// 检查是否是有效的 JSON
val isJson = responseBodyString.trim().startsWith("{") ||
responseBodyString.trim().startsWith("[")
// 检查响应体是否为空
val isEmpty = responseBodyString.isBlank()
if (!isJson || !response.isSuccessful) {
// 检查是否是有效的 JSON
val trimmedBody = responseBodyString.trim()
val isJson = !isEmpty && (
trimmedBody.startsWith("{") ||
trimmedBody.startsWith("[")
)
// 如果响应体为空或不是 JSON,记录警告
if (isEmpty || !isJson) {
val bodyPreview = if (isEmpty) {
"(空响应体)"
} else {
trimmedBody.take(500)
}
logger.warn(
"API 响应异常: method=${request.method}, url=${request.url}, " +
"code=${response.code}, isJson=$isJson, " +
"responseBody=${responseBodyString.take(500)}"
"code=${response.code}, isJson=$isJson, isEmpty=$isEmpty, " +
"responseBody=$bodyPreview"
)
}
} catch (e: Exception) {
// 如果读取响应体失败,记录异常但不影响响应
logger.debug("读取响应体失败: ${e.message}")
}
}
return response
}
}
@@ -40,23 +40,12 @@ 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
# Polygon RPC 配置(用于查询链上余额)
# 可选:如果未配置,将无acc法查询 USDC 余额,但仍可通过 Subgraph API 查询持仓
# 示例:https://polygon-rpc.com 或 https://polygon-mainnet.infura.io/v3/YOUR_PROJECT_ID
polygon.rpc.url=${POLYGON_RPC_URL:https://polygon-rpc.com}
# Builder Relayer 配置(用于 Gasless 交易)
# 从 polymarket.com/settings?tab=builder 获取 Builder API 凭证
# Builder API Key、Secret、Passphrase 现在通过系统设置页面配置,存储在数据库中
# 如果未配置,将使用手动发送交易的方式(需要用户支付 gas)
polymarket.builder.relayer-url=${POLYMARKET_BUILDER_RELAYER_URL:https://relayer-v2.polymarket.com/}
# 仓位推送配置
# 轮询间隔(毫秒),默认3秒
position.push.polling-interval=${POSITION_PUSH_POLLING_INTERVAL:3000}
# 心跳超时时间(毫秒),默认60秒,超过此时间未收到心跳则清理连接
position.push.heartbeat-timeout=${POSITION_PUSH_HEARTBEAT_TIMEOUT:60000}
# 跟单轮询配置
# 轮询间隔(毫秒),默认2秒
copy.trading.polling.interval=${COPY_TRADING_POLLING_INTERVAL:2000}
@@ -0,0 +1,9 @@
-- ============================================
-- V11: 添加最大仓位配置字段
-- 在 copy_trading 表中添加最大仓位金额和最大仓位数量配置
-- ============================================
ALTER TABLE copy_trading
ADD COLUMN max_position_value DECIMAL(20, 8) NULL COMMENT '最大仓位金额(USDC),NULL表示不启用' AFTER max_price,
ADD COLUMN max_position_count INT NULL COMMENT '最大仓位数量,NULL表示不启用' AFTER max_position_value;
@@ -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,41 @@
-- 创建 RPC 节点配置表
CREATE TABLE rpc_node_config (
id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '主键',
provider_type VARCHAR(50) NOT NULL COMMENT '服务商类型: ALCHEMY, INFURA, QUICKNODE, CHAINSTACK, GETBLOCK, CUSTOM, PUBLIC',
name VARCHAR(100) NOT NULL COMMENT '节点名称',
http_url VARCHAR(500) NOT NULL COMMENT 'HTTP RPC URL',
ws_url VARCHAR(500) COMMENT 'WebSocket URL (可选)',
api_key VARCHAR(200) COMMENT 'API Key (加密存储)',
enabled BOOLEAN DEFAULT TRUE COMMENT '是否启用',
priority INT DEFAULT 0 COMMENT '优先级(数字越小优先级越高)',
last_check_time BIGINT COMMENT '最后检查时间(毫秒时间戳)',
last_check_status VARCHAR(20) COMMENT '最后检查状态: HEALTHY, UNHEALTHY, UNKNOWN',
response_time_ms INT COMMENT '最后一次响应时间(毫秒)',
created_at BIGINT NOT NULL COMMENT '创建时间(毫秒时间戳)',
updated_at BIGINT NOT NULL COMMENT '更新时间(毫秒时间戳)',
INDEX idx_enabled_priority (enabled, priority),
INDEX idx_last_check_status (last_check_status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Polygon RPC 节点配置表';
-- 插入默认公共节点 (PublicNode)
INSERT INTO rpc_node_config (
provider_type,
name,
http_url,
ws_url,
enabled,
priority,
created_at,
updated_at,
last_check_status
) VALUES (
'PUBLIC',
'PublicNode (Default)',
'https://polygon.publicnode.com',
'wss://polygon.publicnode.com',
TRUE,
999,
UNIX_TIMESTAMP() * 1000,
UNIX_TIMESTAMP() * 1000,
'UNKNOWN'
);
@@ -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;
@@ -0,0 +1,8 @@
-- 添加 price_updated 字段到 sell_match_record 表
-- 用于标记卖出价格是否已从订单详情中更新
ALTER TABLE sell_match_record
ADD COLUMN price_updated BOOLEAN DEFAULT FALSE COMMENT '价格是否已更新(从订单详情获取实际成交价)';
-- 为已存在的记录设置默认值
UPDATE sell_match_record SET price_updated = TRUE WHERE price_updated IS NULL;
@@ -0,0 +1,8 @@
-- 添加 notification_sent 字段到 copy_order_tracking 表
-- 用于标记买入订单是否已发送通知
ALTER TABLE copy_order_tracking
ADD COLUMN notification_sent BOOLEAN DEFAULT FALSE COMMENT '是否已发送通知(从订单详情获取实际数据后发送)';
-- 为已存在的记录设置默认值(已存在的订单视为已发送通知)
UPDATE copy_order_tracking SET notification_sent = TRUE WHERE notification_sent IS NULL;
@@ -0,0 +1,7 @@
-- 添加 Leader 买入数量字段,用于固定金额模式计算卖出比例
ALTER TABLE copy_order_tracking
ADD COLUMN leader_buy_quantity DECIMAL(20, 8) DEFAULT NULL COMMENT 'Leader 买入数量(用于固定金额模式计算卖出比例)';
-- 对于已有数据,如果无法从 API 查询,设置为 NULL(不影响现有功能)
-- 新创建的记录会在创建时自动填充此字段
@@ -0,0 +1,8 @@
-- ============================================
-- V16: 删除失败交易记录表
-- 移除下单失败存储到数据库的功能
-- ============================================
-- 删除失败交易记录表
DROP TABLE IF EXISTS failed_trade;
@@ -0,0 +1,35 @@
-- ============================================
-- 添加 wallet_type 字段到 wallet_accounts 表
-- 用于区分 Magic 和 Safe 两种钱包类型
-- ============================================
-- 使用存储过程检查并添加字段(如果不存在)
DELIMITER $$
CREATE PROCEDURE IF NOT EXISTS add_wallet_type_if_not_exists()
BEGIN
DECLARE column_exists INT DEFAULT 0;
SELECT COUNT(*) INTO column_exists
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'wallet_accounts'
AND COLUMN_NAME = 'wallet_type';
IF column_exists = 0 THEN
ALTER TABLE wallet_accounts
ADD COLUMN wallet_type VARCHAR(20) NOT NULL DEFAULT 'magic' COMMENT '钱包类型:magic(邮箱/OAuth登录)或 safeMetaMask浏览器钱包)' AFTER is_enabled;
END IF;
END$$
DELIMITER ;
-- 执行存储过程
CALL add_wallet_type_if_not_exists();
-- 删除存储过程
DROP PROCEDURE IF EXISTS add_wallet_type_if_not_exists;
-- 为现有账户设置默认walletType为magic(如果值为NULL
UPDATE wallet_accounts SET wallet_type = 'magic' WHERE wallet_type IS NULL OR wallet_type = '';

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