From b79caceae64c8dbf67745c63a98c10acd07e4b24 Mon Sep 17 00:00:00 2001 From: WrBug Date: Sat, 14 Feb 2026 06:11:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20CLOB=20/time=20=E7=94=A8=20ResponseBody?= =?UTF-8?q?=20=E8=A7=A3=E6=9E=90=E7=BA=AF=E6=95=B0=E5=AD=97=E5=B9=B6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=20DOCKER=5FVERSION=20=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - PolymarketClobApi: getServerTime() 改为 Response,/time 返回纯时间戳非 JSON - PolymarketApiKeyService: 抽取 fetchServerTimeOrNull() 复用,解析 body 为 Long - deploy.sh: DOCKER_VERSION 支持环境变量,未设置时仍用分支名 Co-authored-by: Cursor --- .../polymarketbot/api/PolymarketClobApi.kt | 12 ++--- .../service/common/PolymarketApiKeyService.kt | 48 ++++++++----------- deploy.sh | 18 ++++--- 3 files changed, 34 insertions(+), 44 deletions(-) diff --git a/backend/src/main/kotlin/com/wrbug/polymarketbot/api/PolymarketClobApi.kt b/backend/src/main/kotlin/com/wrbug/polymarketbot/api/PolymarketClobApi.kt index bff12b2..e830e44 100644 --- a/backend/src/main/kotlin/com/wrbug/polymarketbot/api/PolymarketClobApi.kt +++ b/backend/src/main/kotlin/com/wrbug/polymarketbot/api/PolymarketClobApi.kt @@ -1,6 +1,7 @@ package com.wrbug.polymarketbot.api import com.google.gson.annotations.SerializedName +import okhttp3.ResponseBody import retrofit2.Response import retrofit2.http.* @@ -164,10 +165,10 @@ interface PolymarketClobApi { /** * 获取服务器时间 - * 端点: /time + * 端点: /time 返回纯数字(Unix 时间戳),非 JSON */ @GET("/time") - suspend fun getServerTime(): Response + suspend fun getServerTime(): Response } // 请求和响应数据类 @@ -363,13 +364,6 @@ data class ApiKeyResponse( val passphrase: String ) -/** - * 服务器时间响应 - */ -data class ServerTimeResponse( - val timestamp: Long -) - /** * 费率响应 * 文档: https://docs.polymarket.com/developers/market-makers/maker-rebates-program#1-fetch-the-fee-rate diff --git a/backend/src/main/kotlin/com/wrbug/polymarketbot/service/common/PolymarketApiKeyService.kt b/backend/src/main/kotlin/com/wrbug/polymarketbot/service/common/PolymarketApiKeyService.kt index 44ac94b..91112d0 100644 --- a/backend/src/main/kotlin/com/wrbug/polymarketbot/service/common/PolymarketApiKeyService.kt +++ b/backend/src/main/kotlin/com/wrbug/polymarketbot/service/common/PolymarketApiKeyService.kt @@ -98,6 +98,24 @@ class PolymarketApiKeyService( creds.passphrase.isNotBlank() } + /** + * 从 CLOB /time 获取服务器时间戳,失败时返回 null(调用方使用本地时间) + */ + private suspend fun fetchServerTimeOrNull(): Long? { + return try { + val timeApi = createUnauthenticatedApi() + val timeResponse = timeApi.getServerTime() + if (timeResponse.isSuccessful) { + timeResponse.body()?.string()?.trim()?.toLongOrNull() + } else { + null + } + } catch (e: Exception) { + logger.warn("获取服务器时间失败,使用本地时间", e) + null + } + } + /** * 创建新的 API Key */ @@ -107,20 +125,7 @@ class PolymarketApiKeyService( chainId: Long ): Result { return try { - // 获取服务器时间(可选,用于更准确的时间戳) - val serverTime = try { - val timeApi = createUnauthenticatedApi() - val timeResponse = timeApi.getServerTime() - if (timeResponse.isSuccessful && timeResponse.body() != null) { - timeResponse.body()!!.timestamp - } else { - null - } - } catch (e: Exception) { - logger.warn("获取服务器时间失败,使用本地时间", e) - null - } - + val serverTime = fetchServerTimeOrNull() // 创建带 L1 认证的 API 客户端 val api = createL1AuthenticatedApi(privateKey, walletAddress, chainId, serverTime) @@ -158,20 +163,7 @@ class PolymarketApiKeyService( chainId: Long ): Result { return try { - // 获取服务器时间(可选) - val serverTime = try { - val timeApi = createUnauthenticatedApi() - val timeResponse = timeApi.getServerTime() - if (timeResponse.isSuccessful && timeResponse.body() != null) { - timeResponse.body()!!.timestamp - } else { - null - } - } catch (e: Exception) { - logger.warn("获取服务器时间失败,使用本地时间", e) - null - } - + val serverTime = fetchServerTimeOrNull() // 创建带 L1 认证的 API 客户端 val api = createL1AuthenticatedApi(privateKey, walletAddress, chainId, serverTime) diff --git a/deploy.sh b/deploy.sh index e79eace..e20ced4 100755 --- a/deploy.sh +++ b/deploy.sh @@ -161,10 +161,12 @@ deploy() { # 注意:这里需要手动修改 docker-compose.yml,或者使用环境变量 warn "请确保 docker-compose.yml 中已配置使用 image: wrbug/polyhermes:latest" else - # 获取当前分支名作为版本号 - CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "dev") - # 如果分支名包含 /,替换为 -(Docker tag 不支持 /) - DOCKER_VERSION=$(echo "$CURRENT_BRANCH" | tr '/' '-') + # 版本号:优先使用环境变量 DOCKER_VERSION,未设置时使用当前分支名 + if [ -z "${DOCKER_VERSION}" ]; then + CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "dev") + DOCKER_VERSION=$(echo "$CURRENT_BRANCH" | tr '/' '-') + fi + export DOCKER_VERSION info "构建 Docker 镜像(本地构建,版本号: ${DOCKER_VERSION})..." @@ -216,9 +218,11 @@ main() { info "访问地址: http://localhost:${SERVER_PORT:-80}" echo "" if [ "$USE_DOCKER_HUB" != "true" ]; then - CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "dev") - DOCKER_VERSION=$(echo "$CURRENT_BRANCH" | tr '/' '-') - info "提示:本地构建的版本号为当前分支名: ${DOCKER_VERSION}" + if [ -z "${DOCKER_VERSION}" ]; then + CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "dev") + DOCKER_VERSION=$(echo "$CURRENT_BRANCH" | tr '/' '-') + fi + info "提示:本地构建的版本号: ${DOCKER_VERSION}(可通过环境变量 DOCKER_VERSION 指定)" info "生产环境推荐使用 Docker Hub 镜像:" info " ./deploy.sh --use-docker-hub" info " 或修改 docker-compose.yml 使用 image: wrbug/polyhermes:latest"