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"