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:
@@ -1,6 +1,7 @@
|
|||||||
package com.wrbug.polymarketbot.api
|
package com.wrbug.polymarketbot.api
|
||||||
|
|
||||||
import com.google.gson.annotations.SerializedName
|
import com.google.gson.annotations.SerializedName
|
||||||
|
import okhttp3.ResponseBody
|
||||||
import retrofit2.Response
|
import retrofit2.Response
|
||||||
import retrofit2.http.*
|
import retrofit2.http.*
|
||||||
|
|
||||||
@@ -164,10 +165,10 @@ interface PolymarketClobApi {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取服务器时间
|
* 获取服务器时间
|
||||||
* 端点: /time
|
* 端点: /time 返回纯数字(Unix 时间戳),非 JSON
|
||||||
*/
|
*/
|
||||||
@GET("/time")
|
@GET("/time")
|
||||||
suspend fun getServerTime(): Response<ServerTimeResponse>
|
suspend fun getServerTime(): Response<ResponseBody>
|
||||||
}
|
}
|
||||||
|
|
||||||
// 请求和响应数据类
|
// 请求和响应数据类
|
||||||
@@ -363,13 +364,6 @@ data class ApiKeyResponse(
|
|||||||
val passphrase: String
|
val passphrase: String
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
|
||||||
* 服务器时间响应
|
|
||||||
*/
|
|
||||||
data class ServerTimeResponse(
|
|
||||||
val timestamp: Long
|
|
||||||
)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 费率响应
|
* 费率响应
|
||||||
* 文档: https://docs.polymarket.com/developers/market-makers/maker-rebates-program#1-fetch-the-fee-rate
|
* 文档: https://docs.polymarket.com/developers/market-makers/maker-rebates-program#1-fetch-the-fee-rate
|
||||||
|
|||||||
+20
-28
@@ -98,6 +98,24 @@ class PolymarketApiKeyService(
|
|||||||
creds.passphrase.isNotBlank()
|
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
|
* 创建新的 API Key
|
||||||
*/
|
*/
|
||||||
@@ -107,20 +125,7 @@ class PolymarketApiKeyService(
|
|||||||
chainId: Long
|
chainId: Long
|
||||||
): Result<ApiKeyCreds> {
|
): Result<ApiKeyCreds> {
|
||||||
return try {
|
return try {
|
||||||
// 获取服务器时间(可选,用于更准确的时间戳)
|
val serverTime = fetchServerTimeOrNull()
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建带 L1 认证的 API 客户端
|
// 创建带 L1 认证的 API 客户端
|
||||||
val api = createL1AuthenticatedApi(privateKey, walletAddress, chainId, serverTime)
|
val api = createL1AuthenticatedApi(privateKey, walletAddress, chainId, serverTime)
|
||||||
|
|
||||||
@@ -158,20 +163,7 @@ class PolymarketApiKeyService(
|
|||||||
chainId: Long
|
chainId: Long
|
||||||
): Result<ApiKeyCreds> {
|
): Result<ApiKeyCreds> {
|
||||||
return try {
|
return try {
|
||||||
// 获取服务器时间(可选)
|
val serverTime = fetchServerTimeOrNull()
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建带 L1 认证的 API 客户端
|
// 创建带 L1 认证的 API 客户端
|
||||||
val api = createL1AuthenticatedApi(privateKey, walletAddress, chainId, serverTime)
|
val api = createL1AuthenticatedApi(privateKey, walletAddress, chainId, serverTime)
|
||||||
|
|
||||||
|
|||||||
@@ -161,10 +161,12 @@ deploy() {
|
|||||||
# 注意:这里需要手动修改 docker-compose.yml,或者使用环境变量
|
# 注意:这里需要手动修改 docker-compose.yml,或者使用环境变量
|
||||||
warn "请确保 docker-compose.yml 中已配置使用 image: wrbug/polyhermes:latest"
|
warn "请确保 docker-compose.yml 中已配置使用 image: wrbug/polyhermes:latest"
|
||||||
else
|
else
|
||||||
# 获取当前分支名作为版本号
|
# 版本号:优先使用环境变量 DOCKER_VERSION,未设置时使用当前分支名
|
||||||
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "dev")
|
if [ -z "${DOCKER_VERSION}" ]; then
|
||||||
# 如果分支名包含 /,替换为 -(Docker tag 不支持 /)
|
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "dev")
|
||||||
DOCKER_VERSION=$(echo "$CURRENT_BRANCH" | tr '/' '-')
|
DOCKER_VERSION=$(echo "$CURRENT_BRANCH" | tr '/' '-')
|
||||||
|
fi
|
||||||
|
export DOCKER_VERSION
|
||||||
|
|
||||||
info "构建 Docker 镜像(本地构建,版本号: ${DOCKER_VERSION})..."
|
info "构建 Docker 镜像(本地构建,版本号: ${DOCKER_VERSION})..."
|
||||||
|
|
||||||
@@ -216,9 +218,11 @@ main() {
|
|||||||
info "访问地址: http://localhost:${SERVER_PORT:-80}"
|
info "访问地址: http://localhost:${SERVER_PORT:-80}"
|
||||||
echo ""
|
echo ""
|
||||||
if [ "$USE_DOCKER_HUB" != "true" ]; then
|
if [ "$USE_DOCKER_HUB" != "true" ]; then
|
||||||
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "dev")
|
if [ -z "${DOCKER_VERSION}" ]; then
|
||||||
DOCKER_VERSION=$(echo "$CURRENT_BRANCH" | tr '/' '-')
|
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "dev")
|
||||||
info "提示:本地构建的版本号为当前分支名: ${DOCKER_VERSION}"
|
DOCKER_VERSION=$(echo "$CURRENT_BRANCH" | tr '/' '-')
|
||||||
|
fi
|
||||||
|
info "提示:本地构建的版本号: ${DOCKER_VERSION}(可通过环境变量 DOCKER_VERSION 指定)"
|
||||||
info "生产环境推荐使用 Docker Hub 镜像:"
|
info "生产环境推荐使用 Docker Hub 镜像:"
|
||||||
info " ./deploy.sh --use-docker-hub"
|
info " ./deploy.sh --use-docker-hub"
|
||||||
info " 或修改 docker-compose.yml 使用 image: wrbug/polyhermes:latest"
|
info " 或修改 docker-compose.yml 使用 image: wrbug/polyhermes:latest"
|
||||||
|
|||||||
Reference in New Issue
Block a user