fix: CLOB /time 用 ResponseBody 解析纯数字并支持 DOCKER_VERSION 环境变量

- PolymarketClobApi: getServerTime() 改为 Response<ResponseBody>,/time 返回纯时间戳非 JSON
- PolymarketApiKeyService: 抽取 fetchServerTimeOrNull() 复用,解析 body 为 Long
- deploy.sh: DOCKER_VERSION 支持环境变量,未设置时仍用分支名

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
WrBug
2026-02-14 06:11:22 +08:00
parent ed749fb606
commit b79caceae6
3 changed files with 34 additions and 44 deletions
@@ -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<ServerTimeResponse>
suspend fun getServerTime(): Response<ResponseBody>
}
// 请求和响应数据类
@@ -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
@@ -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<ApiKeyCreds> {
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<ApiKeyCreds> {
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)
+11 -7
View File
@@ -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"