Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e5acad5091 | |||
| 1688fa9633 | |||
| 1967d97c31 | |||
| 79b154515d | |||
| 9b4d8fc001 | |||
| cb1f43871a | |||
| a2be5b7f52 | |||
| 708d6ddb41 |
@@ -1,183 +0,0 @@
|
||||
---
|
||||
name: 🤖 Bug Report for AI Fix / AI Bug 报告
|
||||
description: Bug 报告模板
|
||||
title: '[Bug] / '
|
||||
assignees: []
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
value: |
|
||||
## Bug Report Template 🐛
|
||||
|
||||
本模板用于报告 PolyHermes 项目的 bug。
|
||||
|
||||
⚠️ **Important / 重要提示**:
|
||||
- AI fixes will be on the `fix_issues_by_ai` branch / AI 修复将在 `fix_issues_by_ai` 分支上进行
|
||||
- All AI fixes require human review before merging / 所有 AI 修复需要人工审核后才能合并
|
||||
- Security vulnerabilities, database migrations, major changes are not recommended for AI auto-fix
|
||||
/ 涉及安全漏洞、数据库迁移、重大变更等问题不建议使用 AI 自动修复
|
||||
|
||||
---
|
||||
|
||||
本模板用于报告 PolyHermes 项目的 bug。
|
||||
|
||||
This template is for reporting bugs in the PolyHermes project.
|
||||
|
||||
- type: textarea
|
||||
id: description
|
||||
attributes:
|
||||
label: 📝 Bug Description / Bug 描述
|
||||
description: Clearly and concisely describe the bug / 清晰简洁地描述这个 bug
|
||||
placeholder: Describe the bug you encountered / 描述你遇到的问题...
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: dropdown
|
||||
id: type
|
||||
attributes:
|
||||
label: 🎯 Bug Type / 问题类型
|
||||
description: Select the type of bug / 选择问题类型
|
||||
options:
|
||||
- Frontend bug (UI/UX/interaction) / 前端 bug (UI/UX/交互问题)
|
||||
- Backend bug (API/logic/data) / 后端 bug (API/逻辑/数据处理)
|
||||
- Database issue / 数据库问题
|
||||
- Performance issue / 性能问题
|
||||
- Configuration/Deployment / 配置/部署问题
|
||||
- Documentation / 文档问题
|
||||
- Other / 其他
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: dropdown
|
||||
id: scope
|
||||
attributes:
|
||||
label: 📍 Affected Scope / 影响范围
|
||||
description: Select the scope of impact / 选择问题影响范围
|
||||
options:
|
||||
- Specific page/function only / 仅影响特定页面/功能
|
||||
- Entire system / 影响整个系统
|
||||
- Specific user role / 影响特定用户角色
|
||||
- Only in specific environment / 仅在特定环境下重现
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: textarea
|
||||
id: steps
|
||||
attributes:
|
||||
label: 🔍 Steps to Reproduce / 复现步骤
|
||||
description: Provide clear, detailed steps to reproduce the bug / 提供清晰、详细的步骤来重现这个 bug
|
||||
placeholder: |
|
||||
1. Visit page: `...` / 访问页面:`...`
|
||||
2. Click button: `...` / 点击按钮:`...`
|
||||
3. Input data: `...` / 输入数据:`...`
|
||||
4. Submit form: `...` / 提交表单:`...`
|
||||
5. Observe error: `...` / 观察到错误:`...`
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: dropdown
|
||||
id: frequency
|
||||
attributes:
|
||||
label: Reproduction Frequency / 复现频率
|
||||
options:
|
||||
- Always reproducible (100%) / 总是能复现 (100%)
|
||||
- Frequently reproducible (50%+) / 经常能复现 (50%+)
|
||||
- Occasionally reproducible (<50%) / 偶尔能复现 (<50%)
|
||||
- Hard to reproduce / 很难复现
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: textarea
|
||||
id: expected
|
||||
attributes:
|
||||
label: 💻 Expected Behavior / 预期行为
|
||||
description: Describe what you expected to happen / 描述你期望发生什么
|
||||
placeholder: What should happen / 应该发生什么...
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: textarea
|
||||
id: actual
|
||||
attributes:
|
||||
label: ❌ Actual Behavior / 实际行为
|
||||
description: Describe what actually happened / 描述实际发生了什么
|
||||
placeholder: What actually happened / 实际发生了什么...
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: textarea
|
||||
id: screenshots
|
||||
attributes:
|
||||
label: 📸 Screenshots / Recordings / 截图/录屏
|
||||
description: If applicable, add screenshots or recordings to illustrate the problem (drag and drop files here)
|
||||
/ 如果适用,添加截图或录屏来说明问题(可以拖拽文件到这里)
|
||||
placeholder: Add screenshots or recordings / 添加截图或录屏...
|
||||
|
||||
- type: textarea
|
||||
id: environment
|
||||
attributes:
|
||||
label: 🌐 Environment / 环境
|
||||
description: Provide relevant environment information / 提供相关环境信息
|
||||
value: |
|
||||
**Browser (for frontend issues) / 浏览器(前端问题):**
|
||||
- Browser: ______ / 浏览器:______
|
||||
- Browser version: ______ / 浏览器版本:______
|
||||
- Operating System: ______ / 操作系统:______
|
||||
|
||||
**Backend Environment (for backend issues) / 后端环境(后端问题):**
|
||||
- Node.js version: ______ / Node.js 版本:______
|
||||
- Database version: ______ / 数据库版本:______
|
||||
- Docker version (if used): ______ / Docker 版本(如果使用):______
|
||||
- Other relevant dependencies: ______ / 其他相关依赖版本:______
|
||||
validations:
|
||||
required: false
|
||||
|
||||
- type: textarea
|
||||
id: related-files
|
||||
attributes:
|
||||
label: 📁 Related Files / Code / 相关文件/代码
|
||||
description: Provide relevant file paths or code snippets / 提供可能涉及的文件路径或相关代码片段
|
||||
placeholder: |
|
||||
Possibly related files / 可能涉及的文件:
|
||||
- frontend/src/components/...
|
||||
- backend/src/main/kotlin/...
|
||||
|
||||
Error logs / 错误日志:
|
||||
```
|
||||
Paste error logs here / 粘贴错误日志
|
||||
```
|
||||
validations:
|
||||
required: false
|
||||
|
||||
- type: textarea
|
||||
id: suggestions
|
||||
attributes:
|
||||
label: 🎯 Fix Suggestions (Optional) / 修复建议(可选)
|
||||
description: If you have fix ideas, describe them briefly / 如果你有修复思路,可以在这里简单描述
|
||||
placeholder: |
|
||||
Suggest adding ZZZ check in the YYY method of file XXX
|
||||
/ 建议在 XXX 文件的 YYY 方法中,添加 ZZZ 检查
|
||||
...
|
||||
|
||||
- type: dropdown
|
||||
id: priority
|
||||
attributes:
|
||||
label: 🚨 Priority / 优先级
|
||||
options:
|
||||
- 🔴 High - Blocking core functionality, affects user experience / 高 - 阻塞核心功能,影响用户体验
|
||||
- 🟡 Medium - Limited functionality but not blocking / 中 - 功能受限但不阻塞
|
||||
- 🟢 Low - Minor issue, doesn't affect usage / 低 - 小问题,不影响使用
|
||||
validations:
|
||||
required: true
|
||||
|
||||
- type: textarea
|
||||
id: additional
|
||||
attributes:
|
||||
label: 📝 Additional Information / 补充说明
|
||||
description: Any other information that helps AI understand and fix the issue / 任何其他有助于 AI 理解和修复问题的信息
|
||||
placeholder: |
|
||||
- Was this bug introduced recently? / 这个 bug 是最近引入的吗?
|
||||
- Is it related to a specific PR or commit? / 是否与某个特定的 PR 或 commit 相关?
|
||||
- Does it only occur with specific datasets or users? / 是否只在特定数据集或特定用户情况下出现?
|
||||
/ ...
|
||||
Any other context / 其他任何上下文信息...
|
||||
@@ -1,127 +0,0 @@
|
||||
# PR 合并后自动关闭关联的 Issue
|
||||
# 当 PR 从 ai_fix/N_xxx 分支合并到 main 时,关闭 #N 对应的 Issue(若 PR 描述中未含 Closes #N 则通过分支名解析)
|
||||
# 关闭后发送 Telegram 通知(复用 TELEGRAM_BOT_TOKEN / TELEGRAM_CHAT_ID)
|
||||
|
||||
name: Close issue on PR merge
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [closed]
|
||||
branches: [main]
|
||||
|
||||
jobs:
|
||||
close-issue:
|
||||
if: github.event.pull_request.merged == true
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
issues: write
|
||||
pull-requests: read
|
||||
|
||||
steps:
|
||||
- name: Get PR info
|
||||
id: pr
|
||||
run: |
|
||||
# 从 PR body 查找 Closes #N / Fixes #N(若已有则 GitHub 已自动关 issue,本 step 仅做解析)
|
||||
BODY="${{ github.event.pull_request.body }}"
|
||||
HEAD_REF="${{ github.event.pull_request.head.ref }}"
|
||||
|
||||
# 优先从 PR 描述解析(无匹配时 grep 会 exit 1,需 || true 避免 set -e 导致脚本退出)
|
||||
ISSUE_NUM=$(echo "$BODY" | grep -oE '(Closes|Fixes|Resolves) #([0-9]+)' | head -1 | grep -oE '[0-9]+' || true)
|
||||
if [ -z "$ISSUE_NUM" ]; then
|
||||
# 从分支名解析 ai_fix/N_xxx
|
||||
ISSUE_NUM=$(echo "$HEAD_REF" | sed -n 's|^ai_fix/\([0-9]*\)_.*|\1|p')
|
||||
fi
|
||||
|
||||
if [ -z "$ISSUE_NUM" ]; then
|
||||
echo "ISSUE_NUMBER=" >> $GITHUB_OUTPUT
|
||||
echo "skip=true" >> $GITHUB_OUTPUT
|
||||
echo "未从 PR 描述或分支名解析到 Issue 编号,跳过关闭"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "ISSUE_NUMBER=$ISSUE_NUM" >> $GITHUB_OUTPUT
|
||||
echo "skip=false" >> $GITHUB_OUTPUT
|
||||
echo "解析到 Issue #$ISSUE_NUM"
|
||||
|
||||
- name: Get issue details
|
||||
if: steps.pr.outputs.skip != 'true'
|
||||
id: issue
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
ISSUE_NUM="${{ steps.pr.outputs.ISSUE_NUMBER }}"
|
||||
# 获取 issue 标题与 URL(用于 TG 消息)
|
||||
JSON=$(gh issue view "$ISSUE_NUM" --json title,url 2>/dev/null || echo '{"title":"","url":""}')
|
||||
TITLE=$(echo "$JSON" | jq -r '.title')
|
||||
ISSUE_URL=$(echo "$JSON" | jq -r '.url')
|
||||
echo "title<<EOF" >> $GITHUB_OUTPUT
|
||||
echo "$TITLE" >> $GITHUB_OUTPUT
|
||||
echo "EOF" >> $GITHUB_OUTPUT
|
||||
echo "url=$ISSUE_URL" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Close issue
|
||||
if: steps.pr.outputs.skip != 'true'
|
||||
uses: actions/github-script@v7
|
||||
with:
|
||||
script: |
|
||||
const issueNumber = parseInt('${{ steps.pr.outputs.ISSUE_NUMBER }}', 10);
|
||||
if (!issueNumber || isNaN(issueNumber)) {
|
||||
console.log('No valid issue number, skip');
|
||||
return;
|
||||
}
|
||||
const { data: issue } = await github.rest.issues.get({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
issue_number: issueNumber
|
||||
});
|
||||
if (issue.state === 'open') {
|
||||
await github.rest.issues.update({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
issue_number: issueNumber,
|
||||
state: 'closed'
|
||||
});
|
||||
console.log(`Issue #${issueNumber} closed.`);
|
||||
} else {
|
||||
console.log(`Issue #${issueNumber} already closed.`);
|
||||
}
|
||||
|
||||
- name: Send Telegram notification
|
||||
if: steps.pr.outputs.skip != 'true'
|
||||
env:
|
||||
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
|
||||
TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
|
||||
run: |
|
||||
# 与 docker-build 一致:未配置则跳过
|
||||
if [ -z "$TELEGRAM_BOT_TOKEN" ] || [ -z "$TELEGRAM_CHAT_ID" ]; then
|
||||
echo "⚠️ Telegram Bot Token 或 Chat ID 未配置,跳过通知"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
ISSUE_NUM="${{ steps.pr.outputs.ISSUE_NUMBER }}"
|
||||
ISSUE_TITLE="${{ steps.issue.outputs.title }}"
|
||||
ISSUE_URL="${{ steps.issue.outputs.url }}"
|
||||
PR_URL="${{ github.event.pull_request.html_url }}"
|
||||
|
||||
# 与 docker-build 相同的 HTML 消息格式
|
||||
MESSAGE="✅ <b>AI 修复的 Issue 已关闭</b>"$'\n'$'\n'"🔢 <b>Issue:</b> #${ISSUE_NUM} ${ISSUE_TITLE}"$'\n'"📎 <a href=\"${ISSUE_URL}\">查看 Issue</a>"$'\n'"🔗 <a href=\"${PR_URL}\">查看 PR</a>"
|
||||
|
||||
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 0
|
||||
fi
|
||||
else
|
||||
echo "❌ 发送 Telegram 消息时发生错误"
|
||||
exit 0
|
||||
fi
|
||||
+42
@@ -13,10 +13,13 @@ import com.wrbug.polymarketbot.dto.CryptoTailMarketOptionDto
|
||||
import com.wrbug.polymarketbot.dto.CryptoTailAutoMinSpreadResponse
|
||||
import com.wrbug.polymarketbot.dto.CryptoTailMonitorInitRequest
|
||||
import com.wrbug.polymarketbot.dto.CryptoTailMonitorInitResponse
|
||||
import com.wrbug.polymarketbot.dto.CryptoTailManualOrderRequest
|
||||
import com.wrbug.polymarketbot.dto.CryptoTailManualOrderResponse
|
||||
import com.wrbug.polymarketbot.enums.ErrorCode
|
||||
import com.wrbug.polymarketbot.service.binance.BinanceKlineAutoSpreadService
|
||||
import com.wrbug.polymarketbot.service.cryptotail.CryptoTailStrategyService
|
||||
import com.wrbug.polymarketbot.service.cryptotail.CryptoTailMonitorService
|
||||
import com.wrbug.polymarketbot.service.cryptotail.CryptoTailStrategyExecutionService
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.context.MessageSource
|
||||
import org.springframework.http.ResponseEntity
|
||||
@@ -24,12 +27,14 @@ import org.springframework.web.bind.annotation.PostMapping
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import kotlinx.coroutines.runBlocking
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/crypto-tail-strategy")
|
||||
class CryptoTailStrategyController(
|
||||
private val cryptoTailStrategyService: CryptoTailStrategyService,
|
||||
private val cryptoTailMonitorService: CryptoTailMonitorService,
|
||||
private val cryptoTailStrategyExecutionService: CryptoTailStrategyExecutionService,
|
||||
private val binanceKlineAutoSpreadService: BinanceKlineAutoSpreadService,
|
||||
private val messageSource: MessageSource
|
||||
) {
|
||||
@@ -216,4 +221,41 @@ class CryptoTailStrategyController(
|
||||
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, e.message, messageSource))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 手动下单
|
||||
* 用户主动触发下单,不检查任何条件,仅检查当前周期是否已下单
|
||||
*/
|
||||
@PostMapping("/manual-order")
|
||||
fun manualOrder(@RequestBody request: CryptoTailManualOrderRequest): ResponseEntity<ApiResponse<CryptoTailManualOrderResponse>> {
|
||||
return runBlocking {
|
||||
try {
|
||||
if (request.strategyId <= 0) {
|
||||
return@runBlocking ResponseEntity.ok(
|
||||
ApiResponse.error(ErrorCode.CRYPTO_TAIL_STRATEGY_NOT_FOUND, messageSource = messageSource)
|
||||
)
|
||||
}
|
||||
val result = cryptoTailStrategyExecutionService.manualOrder(request)
|
||||
result.fold(
|
||||
onSuccess = { ResponseEntity.ok(ApiResponse.success(it)) },
|
||||
onFailure = { e ->
|
||||
logger.error("手动下单失败: ${e.message}", e)
|
||||
val code = when (e.message) {
|
||||
"策略不存在" -> ErrorCode.CRYPTO_TAIL_STRATEGY_NOT_FOUND
|
||||
"当前周期已下单" -> ErrorCode.PARAM_ERROR
|
||||
"价格必须在 0~1 之间" -> ErrorCode.PARAM_ERROR
|
||||
"数量不能少于 1" -> ErrorCode.PARAM_ERROR
|
||||
"总金额不能少于 1 USDC" -> ErrorCode.PARAM_ERROR
|
||||
"总金额超过策略配置的投入金额" -> ErrorCode.PARAM_ERROR
|
||||
else -> ErrorCode.SERVER_ERROR
|
||||
}
|
||||
ResponseEntity.ok(ApiResponse.error(code, e.message, messageSource))
|
||||
}
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
logger.error("手动下单异常: ${e.message}", e)
|
||||
ResponseEntity.ok(ApiResponse.error(ErrorCode.SERVER_ERROR, e.message, messageSource))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.wrbug.polymarketbot.dto
|
||||
|
||||
/**
|
||||
* 加密价差策略手动下单请求
|
||||
*/
|
||||
data class CryptoTailManualOrderRequest(
|
||||
/** 策略ID */
|
||||
val strategyId: Long = 0L,
|
||||
/** 当前周期开始时间 (Unix 秒) */
|
||||
val periodStartUnix: Long = 0L,
|
||||
/** 下单方向: UP or DOWN */
|
||||
val direction: String = "UP",
|
||||
/** 下单价格 */
|
||||
val price: String = "0",
|
||||
/** 下单数量 */
|
||||
val size: String = "1",
|
||||
/** 市场标题(用于记录) */
|
||||
val marketTitle: String = "",
|
||||
/** Token IDs */
|
||||
val tokenIds: List<String> = emptyList()
|
||||
)
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.wrbug.polymarketbot.dto
|
||||
|
||||
/**
|
||||
* 加密价差策略手动下单响应
|
||||
*/
|
||||
data class CryptoTailManualOrderResponse(
|
||||
/** 是否成功 */
|
||||
val success: Boolean = false,
|
||||
/** 订单ID */
|
||||
val orderId: String? = null,
|
||||
/** 提示消息 */
|
||||
val message: String = "",
|
||||
/** 下单详情 */
|
||||
val orderDetails: ManualOrderDetails? = null
|
||||
)
|
||||
|
||||
/**
|
||||
* 手动下单详情
|
||||
*/
|
||||
data class ManualOrderDetails(
|
||||
/** 策略ID */
|
||||
val strategyId: Long = 0L,
|
||||
/** 方向 */
|
||||
val direction: String = "",
|
||||
/** 下单价格 */
|
||||
val price: String = "",
|
||||
/** 下单数量 */
|
||||
val size: String = "",
|
||||
/** 总金额 */
|
||||
val totalAmount: String = ""
|
||||
)
|
||||
@@ -5,7 +5,9 @@ package com.wrbug.polymarketbot.dto
|
||||
*/
|
||||
data class CryptoTailMonitorInitRequest(
|
||||
/** 策略ID */
|
||||
val strategyId: Long = 0L
|
||||
val strategyId: Long = 0L,
|
||||
/** 指定周期开始时间 (Unix 秒),不传则用服务器当前周期 */
|
||||
val periodStartUnix: Long? = null
|
||||
)
|
||||
|
||||
/**
|
||||
@@ -55,7 +57,11 @@ data class CryptoTailMonitorInitResponse(
|
||||
/** 当前时间 (毫秒时间戳) */
|
||||
val currentTimestamp: Long = System.currentTimeMillis(),
|
||||
/** 是否启用 */
|
||||
val enabled: Boolean = true
|
||||
val enabled: Boolean = true,
|
||||
/** 投入金额模式: FIXED or RATIO */
|
||||
val amountMode: String? = null,
|
||||
/** 投入金额数值 */
|
||||
val amountValue: String? = null
|
||||
)
|
||||
|
||||
/**
|
||||
|
||||
@@ -56,6 +56,9 @@ data class CryptoTailStrategyTrigger(
|
||||
@Column(name = "fail_reason", length = 500)
|
||||
val failReason: String? = null,
|
||||
|
||||
@Column(name = "trigger_type", nullable = false, length = 20)
|
||||
val triggerType: String = "AUTO",
|
||||
|
||||
@Column(name = "created_at", nullable = false)
|
||||
val createdAt: Long = System.currentTimeMillis(),
|
||||
|
||||
|
||||
+4
-21
@@ -408,25 +408,10 @@ class BacktestExecutionService(
|
||||
val cost = actualSellQuantity.multiply(position.avgPrice)
|
||||
val profitLoss = netAmount.subtract(cost)
|
||||
|
||||
// Bug #39 Fix: correctly reduce position quantity after sell
|
||||
// 更新余额和持仓
|
||||
currentBalance += netAmount
|
||||
val remainingQuantity = position.quantity - actualSellQuantity
|
||||
val remainingLeaderBuyQuantity = if (position.leaderBuyQuantity != null && position.leaderBuyQuantity > BigDecimal.ZERO) {
|
||||
val totalQty = position.quantity
|
||||
val leaderReduction = actualSellQuantity.divide(
|
||||
totalQty, 8, java.math.RoundingMode.DOWN
|
||||
)
|
||||
(position.leaderBuyQuantity - leaderReduction).coerceAtLeast(BigDecimal.ZERO)
|
||||
} else {
|
||||
position.leaderBuyQuantity
|
||||
}
|
||||
if (remainingQuantity <= BigDecimal.ZERO) {
|
||||
if (position.quantity <= BigDecimal.ZERO) {
|
||||
positions.remove(positionKey)
|
||||
} else {
|
||||
positions[positionKey] = position.copy(
|
||||
quantity = remainingQuantity,
|
||||
leaderBuyQuantity = remainingLeaderBuyQuantity
|
||||
)
|
||||
}
|
||||
|
||||
// 记录交易到当前页列表
|
||||
@@ -647,8 +632,7 @@ class BacktestExecutionService(
|
||||
val settlementPrice = avgPrice
|
||||
|
||||
val settlementValue = quantity.multiply(settlementPrice)
|
||||
// Bug #39 Fix: profitLoss for closed settlement at avgPrice should be ~0
|
||||
val profitLoss = settlementValue.subtract(quantity.multiply(avgPrice))
|
||||
val profitLoss = settlementValue.negate()
|
||||
|
||||
balance += settlementValue
|
||||
|
||||
@@ -718,8 +702,7 @@ class BacktestExecutionService(
|
||||
if (balance > peakBalance) {
|
||||
peakBalance = balance
|
||||
}
|
||||
// Bug #39 Fix: use current balance, not runningBalance from previous iteration
|
||||
val drawdown = peakBalance - balance
|
||||
val drawdown = peakBalance - runningBalance
|
||||
if (drawdown > maxDrawdown) {
|
||||
maxDrawdown = drawdown
|
||||
}
|
||||
|
||||
+6
-1
@@ -91,7 +91,12 @@ class BinanceKlineService {
|
||||
}
|
||||
}.toSet()
|
||||
val wsKeysNeeded = parsed.map { (_, symbol, interval) -> "$symbol-$interval" }.toSet()
|
||||
if (normalized == requiredMarketPrefixes.get()) return
|
||||
|
||||
// 检查是否有需要的 WebSocket 连接缺失(可能因网络问题断开)
|
||||
val hasMissingConnection = wsKeysNeeded.any { it !in connectedWebSockets.keys }
|
||||
|
||||
// 只有当集合相同且所有需要的连接都存在时才跳过
|
||||
if (normalized == requiredMarketPrefixes.get() && !hasMissingConnection) return
|
||||
requiredMarketPrefixes.set(normalized)
|
||||
synchronized(subscriptionLock) {
|
||||
connectedWebSockets.keys.toList().forEach { wsKey ->
|
||||
|
||||
+5
-2
@@ -147,7 +147,8 @@ class CryptoTailMonitorService(
|
||||
|
||||
val account = accountRepository.findById(strategy.accountId).orElse(null)
|
||||
val nowSeconds = System.currentTimeMillis() / 1000
|
||||
val periodStartUnix = (nowSeconds / strategy.intervalSeconds) * strategy.intervalSeconds
|
||||
val periodStartUnix = request.periodStartUnix
|
||||
?: ((nowSeconds / strategy.intervalSeconds) * strategy.intervalSeconds)
|
||||
|
||||
// 获取市场信息
|
||||
val slug = "${strategy.marketSlugPrefix}-$periodStartUnix"
|
||||
@@ -207,7 +208,9 @@ class CryptoTailMonitorService(
|
||||
tokenIdUp = tokenIds.getOrNull(0),
|
||||
tokenIdDown = tokenIds.getOrNull(1),
|
||||
currentTimestamp = System.currentTimeMillis(),
|
||||
enabled = strategy.enabled
|
||||
enabled = strategy.enabled,
|
||||
amountMode = strategy.amountMode,
|
||||
amountValue = strategy.amountValue.toPlainString()
|
||||
)
|
||||
|
||||
Result.success(response)
|
||||
|
||||
+206
-51
@@ -3,6 +3,9 @@ package com.wrbug.polymarketbot.service.cryptotail
|
||||
import com.wrbug.polymarketbot.api.GammaEventBySlugResponse
|
||||
import com.wrbug.polymarketbot.api.NewOrderRequest
|
||||
import com.wrbug.polymarketbot.api.PolymarketClobApi
|
||||
import com.wrbug.polymarketbot.dto.CryptoTailManualOrderRequest
|
||||
import com.wrbug.polymarketbot.dto.CryptoTailManualOrderResponse
|
||||
import com.wrbug.polymarketbot.dto.ManualOrderDetails
|
||||
import com.wrbug.polymarketbot.entity.Account
|
||||
import com.wrbug.polymarketbot.entity.CryptoTailStrategy
|
||||
import com.wrbug.polymarketbot.entity.CryptoTailStrategyTrigger
|
||||
@@ -43,9 +46,6 @@ private const val SPREAD_MAX_PRICE_ADJUSTMENT = "0.02"
|
||||
/** 数量小数位数,与 OrderSigningService 的 roundConfig.size 一致 */
|
||||
private const val SIZE_DECIMAL_SCALE = 2
|
||||
|
||||
/** 单笔下单最小 USDC 金额(平台限制),RATIO 模式计算值低于此值时按此值下单 */
|
||||
private val MIN_ORDER_USDC = BigDecimal("1")
|
||||
|
||||
/**
|
||||
* 周期内预置上下文:账户、解密凭证、费率、签名类型、CLOB 客户端;不含预签订单。
|
||||
* 触发时 FIXED/RATIO 均按 outcomeIndex 计算 size 并签名提交。
|
||||
@@ -154,7 +154,7 @@ class CryptoTailStrategyExecutionService(
|
||||
}
|
||||
val signatureType = orderSigningService.getSignatureTypeForWalletType(account.walletType)
|
||||
|
||||
if (strategy.amountMode.uppercase() != "RATIO" && strategy.amountValue < MIN_ORDER_USDC) return null
|
||||
if (strategy.amountMode.uppercase() != "RATIO" && strategy.amountValue < BigDecimal("1")) return null
|
||||
|
||||
val ctx = PeriodContext(
|
||||
strategy = strategy,
|
||||
@@ -337,36 +337,29 @@ class CryptoTailStrategyExecutionService(
|
||||
val ctx = getOrInvalidatePeriodContext(strategy, periodStartUnix)
|
||||
|
||||
if (ctx != null) {
|
||||
var availableBalanceForRatio = BigDecimal.ZERO
|
||||
var amountUsdc = when (strategy.amountMode.uppercase()) {
|
||||
val amountUsdc = when (strategy.amountMode.uppercase()) {
|
||||
"RATIO" -> {
|
||||
val balanceResult = accountService.getAccountBalance(ctx.account.id)
|
||||
val availableBalance =
|
||||
balanceResult.getOrNull()?.availableBalance?.toSafeBigDecimal() ?: BigDecimal.ZERO
|
||||
availableBalanceForRatio = availableBalance
|
||||
availableBalance.multiply(strategy.amountValue).divide(BigDecimal("100"), 18, RoundingMode.DOWN)
|
||||
}
|
||||
|
||||
else -> strategy.amountValue
|
||||
}
|
||||
if (amountUsdc < MIN_ORDER_USDC) {
|
||||
val amountMode = strategy.amountMode.uppercase()
|
||||
if (amountMode == "RATIO" && availableBalanceForRatio >= MIN_ORDER_USDC) {
|
||||
amountUsdc = MIN_ORDER_USDC
|
||||
} else {
|
||||
saveTriggerRecord(
|
||||
strategy,
|
||||
periodStartUnix,
|
||||
marketTitle,
|
||||
outcomeIndex,
|
||||
triggerPrice,
|
||||
amountUsdc,
|
||||
null,
|
||||
"fail",
|
||||
"投入金额不足"
|
||||
)
|
||||
return
|
||||
}
|
||||
if (amountUsdc < BigDecimal("1")) {
|
||||
saveTriggerRecord(
|
||||
strategy,
|
||||
periodStartUnix,
|
||||
marketTitle,
|
||||
outcomeIndex,
|
||||
triggerPrice,
|
||||
amountUsdc,
|
||||
null,
|
||||
"fail",
|
||||
"投入金额不足"
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
val tokenId = tokenIds.getOrNull(outcomeIndex) ?: run {
|
||||
@@ -421,7 +414,8 @@ class CryptoTailStrategyExecutionService(
|
||||
outcomeIndex,
|
||||
triggerPrice,
|
||||
amountUsdc,
|
||||
orderRequest
|
||||
orderRequest,
|
||||
triggerType = "AUTO"
|
||||
)
|
||||
return
|
||||
}
|
||||
@@ -437,7 +431,8 @@ class CryptoTailStrategyExecutionService(
|
||||
outcomeIndex: Int,
|
||||
triggerPrice: BigDecimal,
|
||||
amountUsdc: BigDecimal,
|
||||
orderRequest: NewOrderRequest
|
||||
orderRequest: NewOrderRequest,
|
||||
triggerType: String = "AUTO"
|
||||
) {
|
||||
var failReason: String? = null
|
||||
try {
|
||||
@@ -454,9 +449,10 @@ class CryptoTailStrategyExecutionService(
|
||||
amountUsdc,
|
||||
body.orderId,
|
||||
"success",
|
||||
null
|
||||
null,
|
||||
triggerType = triggerType
|
||||
)
|
||||
logger.info("加密价差策略下单成功: strategyId=${strategy.id}, periodStartUnix=$periodStartUnix, outcomeIndex=$outcomeIndex, orderId=${body.orderId}")
|
||||
logger.info("加密价差策略下单成功: strategyId=${strategy.id}, periodStartUnix=$periodStartUnix, outcomeIndex=$outcomeIndex, orderId=${body.orderId}, triggerType=$triggerType")
|
||||
return
|
||||
}
|
||||
failReason = body.errorMsg ?: "unknown"
|
||||
@@ -477,7 +473,8 @@ class CryptoTailStrategyExecutionService(
|
||||
amountUsdc,
|
||||
null,
|
||||
"fail",
|
||||
failReason
|
||||
failReason,
|
||||
triggerType = triggerType
|
||||
)
|
||||
logger.error("加密价差策略下单失败: strategyId=${strategy.id}, periodStartUnix=$periodStartUnix, reason=$failReason")
|
||||
}
|
||||
@@ -524,28 +521,23 @@ class CryptoTailStrategyExecutionService(
|
||||
|
||||
val balanceResult = accountService.getAccountBalance(account.id)
|
||||
val availableBalance = balanceResult.getOrNull()?.availableBalance?.toSafeBigDecimal() ?: BigDecimal.ZERO
|
||||
var amountUsdc = when (strategy.amountMode.uppercase()) {
|
||||
val amountUsdc = when (strategy.amountMode.uppercase()) {
|
||||
"RATIO" -> availableBalance.multiply(strategy.amountValue).divide(BigDecimal("100"), 18, RoundingMode.DOWN)
|
||||
else -> strategy.amountValue
|
||||
}
|
||||
if (amountUsdc < MIN_ORDER_USDC) {
|
||||
val amountMode = strategy.amountMode.uppercase()
|
||||
if (amountMode == "RATIO" && availableBalance >= MIN_ORDER_USDC) {
|
||||
amountUsdc = MIN_ORDER_USDC
|
||||
} else {
|
||||
saveTriggerRecord(
|
||||
strategy,
|
||||
periodStartUnix,
|
||||
marketTitle,
|
||||
outcomeIndex,
|
||||
triggerPrice,
|
||||
amountUsdc,
|
||||
null,
|
||||
"fail",
|
||||
"投入金额不足"
|
||||
)
|
||||
return
|
||||
}
|
||||
if (amountUsdc < BigDecimal("1")) {
|
||||
saveTriggerRecord(
|
||||
strategy,
|
||||
periodStartUnix,
|
||||
marketTitle,
|
||||
outcomeIndex,
|
||||
triggerPrice,
|
||||
amountUsdc,
|
||||
null,
|
||||
"fail",
|
||||
"投入金额不足"
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
val tokenId = tokenIds.getOrNull(outcomeIndex) ?: run {
|
||||
@@ -665,7 +657,8 @@ class CryptoTailStrategyExecutionService(
|
||||
amountUsdc: BigDecimal,
|
||||
orderId: String?,
|
||||
status: String,
|
||||
failReason: String?
|
||||
failReason: String?,
|
||||
triggerType: String = "AUTO"
|
||||
) {
|
||||
val record = CryptoTailStrategyTrigger(
|
||||
strategyId = strategy.id!!,
|
||||
@@ -676,11 +669,173 @@ class CryptoTailStrategyExecutionService(
|
||||
amountUsdc = amountUsdc,
|
||||
orderId = orderId,
|
||||
status = status,
|
||||
failReason = failReason
|
||||
failReason = failReason,
|
||||
triggerType = triggerType
|
||||
)
|
||||
triggerRepository.save(record)
|
||||
}
|
||||
|
||||
/**
|
||||
* 手动下单:用户主动触发下单,不检查任何条件,仅检查当前周期是否已下单
|
||||
*/
|
||||
suspend fun manualOrder(request: CryptoTailManualOrderRequest): Result<CryptoTailManualOrderResponse> {
|
||||
return try {
|
||||
val strategy = strategyRepository.findById(request.strategyId).orElse(null)
|
||||
?: return Result.failure(IllegalArgumentException("策略不存在"))
|
||||
|
||||
val outcomeIndex = if (request.direction.uppercase() == "UP") 0 else 1
|
||||
|
||||
if (outcomeIndex < 0 || outcomeIndex >= request.tokenIds.size) {
|
||||
return Result.failure(IllegalArgumentException("outcomeIndex 越界"))
|
||||
}
|
||||
|
||||
val price = request.price.toSafeBigDecimal()
|
||||
if (price <= BigDecimal.ZERO || price > BigDecimal.ONE) {
|
||||
return Result.failure(IllegalArgumentException("价格必须在 0~1 之间"))
|
||||
}
|
||||
val priceRounded = price.setScale(4, RoundingMode.UP)
|
||||
|
||||
val size = request.size.toSafeBigDecimal()
|
||||
if (size < BigDecimal.ONE) {
|
||||
return Result.failure(IllegalArgumentException("数量不能少于 1"))
|
||||
}
|
||||
|
||||
val amountUsdc = priceRounded.multi(size).setScale(2, RoundingMode.HALF_UP)
|
||||
if (amountUsdc < BigDecimal.ONE) {
|
||||
return Result.failure(IllegalArgumentException("总金额不能少于 1 USDC"))
|
||||
}
|
||||
|
||||
val mutex = getTriggerMutex(strategy.id!!, request.periodStartUnix)
|
||||
mutex.withLock {
|
||||
if (triggerRepository.findByStrategyIdAndPeriodStartUnix(
|
||||
strategy.id!!,
|
||||
request.periodStartUnix
|
||||
) != null
|
||||
) {
|
||||
return@withLock Result.failure(IllegalArgumentException("当前周期已下单"))
|
||||
}
|
||||
|
||||
var ctx = getOrInvalidatePeriodContext(strategy, request.periodStartUnix)
|
||||
if (ctx == null) {
|
||||
ctx = ensurePeriodContext(
|
||||
strategy,
|
||||
request.periodStartUnix,
|
||||
request.tokenIds,
|
||||
request.marketTitle.ifBlank { null }
|
||||
)
|
||||
}
|
||||
if (ctx != null) {
|
||||
val tokenId = request.tokenIds.getOrNull(outcomeIndex)
|
||||
?: return@withLock Result.failure(IllegalArgumentException("tokenIds 越界"))
|
||||
|
||||
val priceStr = priceRounded.toPlainString()
|
||||
val sizeStr = size.toPlainString()
|
||||
val feeRateBps = ctx.feeRateByTokenId[tokenId] ?: "0"
|
||||
|
||||
val signedOrder = orderSigningService.createAndSignOrder(
|
||||
privateKey = ctx.decryptedPrivateKey,
|
||||
makerAddress = ctx.account.proxyAddress,
|
||||
tokenId = tokenId,
|
||||
side = "BUY",
|
||||
price = priceStr,
|
||||
size = sizeStr,
|
||||
signatureType = ctx.signatureType,
|
||||
nonce = "0",
|
||||
feeRateBps = feeRateBps,
|
||||
expiration = "0"
|
||||
)
|
||||
|
||||
val orderRequest = NewOrderRequest(
|
||||
order = signedOrder,
|
||||
owner = ctx.account.apiKey!!,
|
||||
orderType = "FAK",
|
||||
deferExec = false
|
||||
)
|
||||
|
||||
val orderResult = submitOrderForManualOrder(
|
||||
ctx.clobApi,
|
||||
strategy,
|
||||
request.periodStartUnix,
|
||||
request.marketTitle,
|
||||
outcomeIndex,
|
||||
priceRounded,
|
||||
amountUsdc,
|
||||
orderRequest
|
||||
)
|
||||
|
||||
orderResult.fold(
|
||||
onSuccess = { orderId ->
|
||||
Result.success(
|
||||
CryptoTailManualOrderResponse(
|
||||
success = true,
|
||||
orderId = orderId,
|
||||
message = "下单成功",
|
||||
orderDetails = ManualOrderDetails(
|
||||
strategyId = strategy.id!!,
|
||||
direction = request.direction,
|
||||
price = priceStr,
|
||||
size = sizeStr,
|
||||
totalAmount = amountUsdc.toPlainString()
|
||||
)
|
||||
)
|
||||
)
|
||||
},
|
||||
onFailure = { e ->
|
||||
Result.failure(e)
|
||||
}
|
||||
)
|
||||
} else {
|
||||
Result.failure(IllegalArgumentException("账户未配置或凭证不足"))
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
logger.error("手动下单异常: strategyId=${request.strategyId}, ${e.message}", e)
|
||||
Result.failure(e)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun submitOrderForManualOrder(
|
||||
clobApi: PolymarketClobApi,
|
||||
strategy: CryptoTailStrategy,
|
||||
periodStartUnix: Long,
|
||||
marketTitle: String?,
|
||||
outcomeIndex: Int,
|
||||
price: BigDecimal,
|
||||
amountUsdc: BigDecimal,
|
||||
orderRequest: NewOrderRequest
|
||||
): Result<String> {
|
||||
return try {
|
||||
val response = clobApi.createOrder(orderRequest)
|
||||
if (response.isSuccessful && response.body() != null) {
|
||||
val body = response.body()!!
|
||||
if (body.success && body.orderId != null) {
|
||||
saveTriggerRecord(
|
||||
strategy,
|
||||
periodStartUnix,
|
||||
marketTitle,
|
||||
outcomeIndex,
|
||||
price,
|
||||
amountUsdc,
|
||||
body.orderId,
|
||||
"success",
|
||||
null,
|
||||
triggerType = "MANUAL"
|
||||
)
|
||||
logger.info("手动下单成功: strategyId=${strategy.id}, periodStartUnix=$periodStartUnix, outcomeIndex=$outcomeIndex, orderId=${body.orderId}")
|
||||
Result.success(body.orderId)
|
||||
} else {
|
||||
Result.failure(Exception(body.errorMsg ?: "unknown"))
|
||||
}
|
||||
} else {
|
||||
val errorBody = response.errorBody()?.string().orEmpty()
|
||||
Result.failure(Exception(errorBody.ifEmpty { "请求失败" }))
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
logger.error("手动下单异常: strategyId=${strategy.id}, periodStartUnix=$periodStartUnix", e)
|
||||
Result.failure(e)
|
||||
}
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
fun destroy() {
|
||||
// 清理所有周期上下文缓存,避免敏感信息(明文私钥、API Secret)在内存中保留
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
-- 添加触发类型字段到加密价差策略触发记录表
|
||||
-- AUTO: 自动下单触发
|
||||
-- MANUAL: 手动下单触发
|
||||
ALTER TABLE crypto_tail_strategy_trigger
|
||||
ADD COLUMN trigger_type VARCHAR(20) DEFAULT 'AUTO' COMMENT '触发类型:AUTO(自动)或 MANUAL(手动)';
|
||||
@@ -1,4 +1,209 @@
|
||||
# PolyHermes 一键部署脚本使用说明
|
||||
# PolyHermes One-Click Deployment Script / PolyHermes 一键部署脚本使用说明
|
||||
|
||||
[English](#english) | [中文](#中文)
|
||||
|
||||
---
|
||||
|
||||
<a name="english"></a>
|
||||
## English
|
||||
|
||||
## ✨ Core Features
|
||||
|
||||
- **Run from any directory** - No need to download source code
|
||||
- **Online images only** - Pull official images from Docker Hub
|
||||
- **Auto-download config** - Download the latest `docker-compose.prod.yml` from GitHub
|
||||
- **Interactive configuration** - User-friendly Q&A style configuration wizard
|
||||
- **Auto-generate secrets** - All sensitive configurations will auto-generate secure random values on Enter
|
||||
|
||||
## 🚀 Quick Start
|
||||
|
||||
### One-Click Installation (Recommended)
|
||||
|
||||
**Using curl (Recommended):**
|
||||
```bash
|
||||
mkdir -p ~/polyhermes && cd ~/polyhermes && curl -fsSL https://raw.githubusercontent.com/WrBug/PolyHermes/main/deploy-interactive.sh -o deploy.sh && chmod +x deploy.sh && ./deploy.sh
|
||||
```
|
||||
|
||||
**Using wget:**
|
||||
```bash
|
||||
mkdir -p ~/polyhermes && cd ~/polyhermes && wget -O deploy.sh https://raw.githubusercontent.com/WrBug/PolyHermes/main/deploy-interactive.sh && chmod +x deploy.sh && ./deploy.sh
|
||||
```
|
||||
|
||||
This command will automatically:
|
||||
- 📁 Create a dedicated working directory `~/polyhermes`
|
||||
- 📥 Download the deployment script
|
||||
- ✅ Check Docker environment
|
||||
- ⚙️ Configure all parameters interactively (press Enter for defaults)
|
||||
- 🔐 Auto-generate secure random secrets
|
||||
- 🚀 Download latest images and deploy
|
||||
|
||||
**Or run directly via pipe (without saving file):**
|
||||
```bash
|
||||
# curl method
|
||||
mkdir -p ~/polyhermes && cd ~/polyhermes && curl -fsSL https://raw.githubusercontent.com/WrBug/PolyHermes/main/deploy-interactive.sh | bash
|
||||
|
||||
# wget method
|
||||
mkdir -p ~/polyhermes && cd ~/polyhermes && wget -qO- https://raw.githubusercontent.com/WrBug/PolyHermes/main/deploy-interactive.sh | bash
|
||||
```
|
||||
|
||||
### Method 1: Download and Run Script Directly
|
||||
|
||||
```bash
|
||||
# Download script
|
||||
curl -O https://raw.githubusercontent.com/WrBug/PolyHermes/main/deploy-interactive.sh
|
||||
|
||||
# Add execute permission
|
||||
chmod +x deploy-interactive.sh
|
||||
|
||||
# Run
|
||||
./deploy-interactive.sh
|
||||
```
|
||||
|
||||
### Method 2: Run in Project Directory
|
||||
|
||||
```bash
|
||||
git clone https://github.com/WrBug/PolyHermes.git
|
||||
cd PolyHermes
|
||||
./deploy-interactive.sh
|
||||
```
|
||||
|
||||
## 📝 Usage Flow
|
||||
|
||||
After running the script, you will be guided through the following steps:
|
||||
|
||||
```
|
||||
Step 1: Environment Check → Check Docker/Docker Compose
|
||||
Step 2: Configuration → Interactive input (press Enter for defaults)
|
||||
Step 3: Get Deploy Config → Download docker-compose.prod.yml from GitHub
|
||||
Step 4: Generate Env File → Auto-generate .env
|
||||
Step 5: Pull Docker Images → Pull latest images from Docker Hub
|
||||
Step 6: Deploy Services → Start containers
|
||||
Step 7: Health Check → Verify services are running properly
|
||||
```
|
||||
|
||||
## ⚡ Simplest Usage
|
||||
|
||||
**Press Enter for all configuration items to use default values**, the script will automatically:
|
||||
- Use port 80 (application) and 3307 (MySQL)
|
||||
- Generate 32-character database password
|
||||
- Generate 128-character JWT secret
|
||||
- Generate 64-character admin reset key
|
||||
- Generate 64-character encryption key
|
||||
- Configure reasonable log levels
|
||||
|
||||
### Interactive Example
|
||||
|
||||
The script will prompt you for configuration one by one, **press Enter to skip and use default values**:
|
||||
|
||||
```
|
||||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||
Step 2: Configuration
|
||||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||
|
||||
💡 All configurations are optional, press Enter to use default or auto-generated values
|
||||
|
||||
⚠ Secret config: Press Enter to auto-generate secure random secrets
|
||||
⚠ Other config: Press Enter to use default values in parentheses
|
||||
|
||||
【Basic Configuration】
|
||||
Will configure: Server port, MySQL port, Timezone
|
||||
➤ Server port [Default: 80]: ⏎
|
||||
➤ MySQL port (external access) [Default: 3307]: ⏎
|
||||
➤ Timezone [Default: Asia/Shanghai]: ⏎
|
||||
|
||||
【Database Configuration】
|
||||
Will configure: Database username, Database password
|
||||
➤ Database username [Default: root]: ⏎
|
||||
➤ Database password [Enter to auto-generate]: ⏎
|
||||
[✓] Database password auto-generated (32 characters)
|
||||
|
||||
【Security Configuration】
|
||||
Will configure: JWT secret, Admin password reset key, Data encryption key
|
||||
➤ JWT secret [Enter to auto-generate]: ⏎
|
||||
[✓] JWT secret auto-generated (128 characters)
|
||||
➤ Admin password reset key [Enter to auto-generate]: ⏎
|
||||
[✓] Admin reset key auto-generated (64 characters)
|
||||
➤ Encryption key (for API Key encryption) [Enter to auto-generate]: ⏎
|
||||
[✓] Encryption key auto-generated (64 characters)
|
||||
|
||||
【Log Configuration】
|
||||
Will configure: Root log level, Application log level
|
||||
Available levels: TRACE, DEBUG, INFO, WARN, ERROR, OFF
|
||||
➤ Root log level (third-party libs) [Default: WARN]: ⏎
|
||||
➤ Application log level [Default: INFO]: ⏎
|
||||
|
||||
【Other Configuration】
|
||||
Will configure: Runtime environment, Auto-update policy, GitHub repo
|
||||
➤ Spring Profile [Default: prod]: ⏎
|
||||
➤ Allow prerelease updates (true/false) [Default: false]: ⏎
|
||||
➤ GitHub repository [Default: WrBug/PolyHermes]: ⏎
|
||||
```
|
||||
|
||||
## 🔧 Files Generated by Script
|
||||
|
||||
After running, the script will generate in the current directory:
|
||||
|
||||
1. **docker-compose.prod.yml** - Docker Compose config downloaded from GitHub (always latest)
|
||||
2. **.env** - Environment variables file auto-generated based on your configuration
|
||||
|
||||
These two files contain all the configuration needed to run PolyHermes.
|
||||
|
||||
## 🌐 Post-Deployment Management
|
||||
|
||||
### Quick Update (Recommended)
|
||||
|
||||
If you already have configuration files, running the script again will detect and ask:
|
||||
|
||||
```bash
|
||||
./deploy-interactive.sh
|
||||
```
|
||||
|
||||
```
|
||||
【Existing Configuration Detected】
|
||||
Found existing .env configuration file
|
||||
|
||||
Use existing configuration to update images directly? [Y/n]: ⏎
|
||||
```
|
||||
|
||||
- **Press Enter or input Y**: Use existing config, pull latest images and update
|
||||
- **Input N**: Reconfigure (existing config will be backed up)
|
||||
|
||||
### Manual Management Commands
|
||||
|
||||
```bash
|
||||
# View service status
|
||||
docker compose -f docker-compose.prod.yml ps
|
||||
|
||||
# View logs
|
||||
docker compose -f docker-compose.prod.yml logs -f
|
||||
|
||||
# Restart services
|
||||
docker compose -f docker-compose.prod.yml restart
|
||||
|
||||
# Stop services
|
||||
docker compose -f docker-compose.prod.yml down
|
||||
|
||||
# Update to latest version
|
||||
docker pull wrbug/polyhermes:latest
|
||||
docker compose -f docker-compose.prod.yml up -d
|
||||
```
|
||||
|
||||
## 🔐 Security Recommendations
|
||||
|
||||
- **Protect .env file**: Contains sensitive information, never commit to version control
|
||||
- **Backup database regularly**: Data is stored in Docker volume `mysql-data`
|
||||
- **Configure HTTPS for production**: Recommend using Nginx or Caddy as reverse proxy
|
||||
|
||||
## 📞 Support
|
||||
|
||||
- [GitHub Repository](https://github.com/WrBug/PolyHermes)
|
||||
- [Issue Feedback](https://github.com/WrBug/PolyHermes/issues)
|
||||
- [Full Deployment Documentation](docs/zh/DEPLOYMENT_GUIDE.md)
|
||||
|
||||
---
|
||||
|
||||
<a name="中文"></a>
|
||||
## 中文
|
||||
|
||||
## ✨ 核心特性
|
||||
|
||||
|
||||
+175
-179
@@ -1,18 +1,19 @@
|
||||
#!/bin/bash
|
||||
|
||||
# ========================================
|
||||
# PolyHermes Interactive Deploy Script
|
||||
# PolyHermes 交互式一键部署脚本
|
||||
# ========================================
|
||||
# 功能:
|
||||
# - 交互式配置环境变量
|
||||
# - 自动生成安全密钥
|
||||
# - 使用 Docker Hub 线上镜像部署
|
||||
# - 支持配置预检和回滚
|
||||
# Features / 功能:
|
||||
# - Interactive env config / 交互式配置环境变量
|
||||
# - Auto-generate secrets / 自动生成安全密钥
|
||||
# - Deploy via Docker Hub images / 使用 Docker Hub 线上镜像部署
|
||||
# - Config check and rollback / 支持配置预检和回滚
|
||||
# ========================================
|
||||
|
||||
set -e
|
||||
|
||||
# 颜色输出
|
||||
# Colors / 颜色输出
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
@@ -20,7 +21,14 @@ BLUE='\033[0;34m'
|
||||
CYAN='\033[0;36m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# 打印函数
|
||||
# Language: LANG=zh* → prompts in Chinese only; else show "中文 / English"
|
||||
# 语言:LANG 为 zh* 时仅中文,否则显示「中文 / English」
|
||||
USE_ZH_ONLY=false
|
||||
case "${LANG:-}" in
|
||||
zh*) USE_ZH_ONLY=true ;;
|
||||
esac
|
||||
|
||||
# Print functions / 打印函数
|
||||
info() {
|
||||
echo -e "${GREEN}[✓]${NC} $1"
|
||||
}
|
||||
@@ -37,6 +45,17 @@ title() {
|
||||
echo -e "${CYAN}${1}${NC}"
|
||||
}
|
||||
|
||||
# Bilingual: 中文 / English (or Chinese only when LANG=zh*)
|
||||
bilingual() {
|
||||
local zh="$1"
|
||||
local en="$2"
|
||||
if [ "$USE_ZH_ONLY" = true ]; then
|
||||
echo "$zh"
|
||||
else
|
||||
echo "$zh / $en"
|
||||
fi
|
||||
}
|
||||
|
||||
# 生成随机密钥
|
||||
generate_secret() {
|
||||
local length=${1:-32}
|
||||
@@ -47,54 +66,57 @@ generate_secret() {
|
||||
fi
|
||||
}
|
||||
|
||||
# 生成随机端口号(10000-60000之间)
|
||||
# 生成随机端口号(10000-60000之间)/ Generate random port (10000-60000)
|
||||
generate_random_port() {
|
||||
echo $((10000 + RANDOM % 50001))
|
||||
}
|
||||
|
||||
# 读取用户输入(支持默认值)
|
||||
# 读取用户输入(支持默认值)/ Read user input (with default)
|
||||
read_input() {
|
||||
local prompt="$1"
|
||||
local default="$2"
|
||||
local is_secret="$3"
|
||||
local value=""
|
||||
|
||||
# 构建提示信息(不使用颜色,因为 read -p 可能不支持)
|
||||
local prompt_text=""
|
||||
if [ -n "$default" ]; then
|
||||
if [ "$is_secret" = "secret" ]; then
|
||||
prompt_text="${prompt} [回车自动生成]: "
|
||||
if [ "$USE_ZH_ONLY" = true ]; then
|
||||
prompt_text="${prompt} [回车自动生成]: "
|
||||
else
|
||||
prompt_text="${prompt} [Enter to auto-generate]: "
|
||||
fi
|
||||
else
|
||||
prompt_text="${prompt} [默认: ${default}]: "
|
||||
if [ "$USE_ZH_ONLY" = true ]; then
|
||||
prompt_text="${prompt} [默认: ${default}]: "
|
||||
else
|
||||
prompt_text="${prompt} [Default: ${default}]: "
|
||||
fi
|
||||
fi
|
||||
else
|
||||
prompt_text="${prompt}: "
|
||||
fi
|
||||
|
||||
# 使用 read -p 确保提示正确显示
|
||||
read -r -p "$prompt_text" value
|
||||
|
||||
# 如果用户没有输入,使用默认值
|
||||
if [ -z "$value" ]; then
|
||||
if [ "$is_secret" = "secret" ] && [ -z "$default" ]; then
|
||||
# 自动生成密钥
|
||||
case "$prompt" in
|
||||
*JWT*)
|
||||
*JWT*|*jwt*)
|
||||
value=$(generate_secret 64)
|
||||
# 输出到 stderr,避免被捕获到返回值中
|
||||
info "已自动生成 JWT 密钥(128字符)" >&2
|
||||
info "$(bilingual "已自动生成 JWT 密钥(128字符)" "JWT secret auto-generated (128 chars)")" >&2
|
||||
;;
|
||||
*管理员*|*ADMIN*)
|
||||
*管理员*|*ADMIN*|*admin*|*reset*|*Reset*)
|
||||
value=$(generate_secret 32)
|
||||
info "已自动生成管理员重置密钥(64字符)" >&2
|
||||
info "$(bilingual "已自动生成管理员重置密钥(64字符)" "Admin reset key auto-generated (64 chars)")" >&2
|
||||
;;
|
||||
*加密*|*CRYPTO*)
|
||||
*加密*|*CRYPTO*|*crypto*|*Encryption*)
|
||||
value=$(generate_secret 32)
|
||||
info "已自动生成加密密钥(64字符)" >&2
|
||||
info "$(bilingual "已自动生成加密密钥(64字符)" "Encryption key auto-generated (64 chars)")" >&2
|
||||
;;
|
||||
*数据库密码*|*DB_PASSWORD*)
|
||||
*数据库密码*|*DB_PASSWORD*|*database*|*Database*)
|
||||
value=$(generate_secret 16)
|
||||
info "已自动生成数据库密码(32字符)" >&2
|
||||
info "$(bilingual "已自动生成数据库密码(32字符)" "Database password auto-generated (32 chars)")" >&2
|
||||
;;
|
||||
*)
|
||||
value="$default"
|
||||
@@ -108,126 +130,115 @@ read_input() {
|
||||
echo "$value"
|
||||
}
|
||||
|
||||
# 检查 Docker 环境
|
||||
# 检查 Docker 环境 / Check Docker environment
|
||||
check_docker() {
|
||||
title "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||||
title " 步骤 1: 环境检查"
|
||||
title " $(bilingual "步骤 1: 环境检查" "Step 1: Environment Check")"
|
||||
title "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||||
|
||||
# 检查 Docker
|
||||
if ! command -v docker &> /dev/null; then
|
||||
error "Docker 未安装"
|
||||
error "$(bilingual "Docker 未安装" "Docker is not installed")"
|
||||
echo ""
|
||||
info "请先安装 Docker:"
|
||||
info "$(bilingual "请先安装 Docker:" "Please install Docker first:")"
|
||||
info " macOS: brew install docker"
|
||||
info " Ubuntu/Debian: apt-get install docker.io"
|
||||
info " CentOS/RHEL: yum install docker"
|
||||
exit 1
|
||||
fi
|
||||
info "Docker 已安装: $(docker --version | head -1)"
|
||||
info "$(bilingual "Docker 已安装" "Docker installed"): $(docker --version | head -1)"
|
||||
|
||||
# 检查 Docker Compose
|
||||
if docker compose version &> /dev/null 2>&1; then
|
||||
info "Docker Compose 已安装: $(docker compose version)"
|
||||
info "$(bilingual "Docker Compose 已安装" "Docker Compose installed"): $(docker compose version)"
|
||||
elif command -v docker-compose &> /dev/null; then
|
||||
info "Docker Compose 已安装: $(docker-compose --version)"
|
||||
info "$(bilingual "Docker Compose 已安装" "Docker Compose installed"): $(docker-compose --version)"
|
||||
else
|
||||
error "Docker Compose 未安装"
|
||||
error "$(bilingual "Docker Compose 未安装" "Docker Compose is not installed")"
|
||||
echo ""
|
||||
info "请先安装 Docker Compose:"
|
||||
info "$(bilingual "请先安装 Docker Compose:" "Please install Docker Compose:")"
|
||||
info " https://docs.docker.com/compose/install/"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 检查 Docker 守护进程
|
||||
if ! docker info &> /dev/null; then
|
||||
error "Docker 守护进程未运行"
|
||||
info "请启动 Docker 服务:"
|
||||
info " macOS: 打开 Docker Desktop"
|
||||
error "$(bilingual "Docker 守护进程未运行" "Docker daemon is not running")"
|
||||
info "$(bilingual "请启动 Docker 服务:" "Please start Docker:")"
|
||||
info " $(bilingual "macOS: 打开 Docker Desktop" "macOS: Open Docker Desktop")"
|
||||
info " Linux: systemctl start docker"
|
||||
exit 1
|
||||
fi
|
||||
info "Docker 守护进程运行正常"
|
||||
info "$(bilingual "Docker 守护进程运行正常" "Docker daemon is running")"
|
||||
|
||||
echo ""
|
||||
}
|
||||
|
||||
# 交互式配置收集
|
||||
# 交互式配置收集 / Interactive configuration
|
||||
collect_configuration() {
|
||||
title "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||||
title " 步骤 2: 配置收集"
|
||||
title " $(bilingual "步骤 2: 配置收集" "Step 2: Configuration")"
|
||||
title "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||||
echo ""
|
||||
info "💡 所有配置项均为可选,直接按回车即可使用默认值或自动生成"
|
||||
info "$(bilingual "💡 所有配置项均为可选,直接按回车即可使用默认值或自动生成" "💡 All options are optional, press Enter for default or auto-generated values")"
|
||||
echo ""
|
||||
warn "密钥配置:回车将自动生成安全的随机密钥"
|
||||
warn "其他配置:回车将使用括号中的默认值"
|
||||
warn "$(bilingual "密钥配置:回车将自动生成安全的随机密钥" "Secrets: Enter to auto-generate secure random keys")"
|
||||
warn "$(bilingual "其他配置:回车将使用括号中的默认值" "Other: Enter to use default value in brackets")"
|
||||
echo ""
|
||||
|
||||
# 基础配置
|
||||
title "【基础配置】"
|
||||
echo -e "${CYAN}将配置:服务器端口、MySQL端口、时区${NC}"
|
||||
# 生成随机端口作为默认值
|
||||
title "$(bilingual "【基础配置】" "【Basic】")"
|
||||
echo -e "${CYAN}$(bilingual "将配置:服务器端口、MySQL端口、时区" "Server port, MySQL port, Timezone")${NC}"
|
||||
DEFAULT_PORT=$(generate_random_port)
|
||||
SERVER_PORT=$(read_input "➤ 服务器端口" "$DEFAULT_PORT")
|
||||
MYSQL_PORT=$(read_input "➤ MySQL 端口(外部访问)" "3307")
|
||||
TZ=$(read_input "➤ 时区" "Asia/Shanghai")
|
||||
SERVER_PORT=$(read_input "$(bilingual "➤ 服务器端口" "➤ Server port")" "$DEFAULT_PORT")
|
||||
MYSQL_PORT=$(read_input "$(bilingual "➤ MySQL 端口(外部访问)" "➤ MySQL port (external)")" "3307")
|
||||
TZ=$(read_input "$(bilingual "➤ 时区" "➤ Timezone")" "Asia/Shanghai")
|
||||
echo ""
|
||||
|
||||
# 数据库配置
|
||||
title "【数据库配置】"
|
||||
echo -e "${CYAN}将配置:数据库用户名、数据库密码${NC}"
|
||||
echo -e "${YELLOW}💡 提示:密码留空将自动生成 32 字符的安全随机密码${NC}"
|
||||
DB_USERNAME=$(read_input "➤ 数据库用户名" "root")
|
||||
DB_PASSWORD=$(read_input "➤ 数据库密码" "" "secret")
|
||||
title "$(bilingual "【数据库配置】" "【Database】")"
|
||||
echo -e "${CYAN}$(bilingual "将配置:数据库用户名、数据库密码" "Database username, password")${NC}"
|
||||
echo -e "${YELLOW}$(bilingual "💡 提示:密码留空将自动生成 32 字符的安全随机密码" "💡 Leave password empty to auto-generate 32-char password")${NC}"
|
||||
DB_USERNAME=$(read_input "$(bilingual "➤ 数据库用户名" "➤ Database username")" "root")
|
||||
DB_PASSWORD=$(read_input "$(bilingual "➤ 数据库密码" "➤ Database password")" "" "secret")
|
||||
echo ""
|
||||
|
||||
# 安全配置
|
||||
title "【安全配置】"
|
||||
echo -e "${CYAN}将配置:JWT密钥、管理员密码重置密钥、数据加密密钥${NC}"
|
||||
echo -e "${YELLOW}💡 提示:留空将自动生成高强度随机密钥(推荐)${NC}"
|
||||
JWT_SECRET=$(read_input "➤ JWT 密钥" "" "secret")
|
||||
ADMIN_RESET_PASSWORD_KEY=$(read_input "➤ 管理员密码重置密钥" "" "secret")
|
||||
CRYPTO_SECRET_KEY=$(read_input "➤ 加密密钥(用于加密 API Key)" "" "secret")
|
||||
title "$(bilingual "【安全配置】" "【Security】")"
|
||||
echo -e "${CYAN}$(bilingual "将配置:JWT密钥、管理员密码重置密钥、数据加密密钥" "JWT secret, Admin reset key, Encryption key")${NC}"
|
||||
echo -e "${YELLOW}$(bilingual "💡 提示:留空将自动生成高强度随机密钥(推荐)" "💡 Leave empty to auto-generate strong keys (recommended)")${NC}"
|
||||
JWT_SECRET=$(read_input "$(bilingual "➤ JWT 密钥" "➤ JWT secret")" "" "secret")
|
||||
ADMIN_RESET_PASSWORD_KEY=$(read_input "$(bilingual "➤ 管理员密码重置密钥" "➤ Admin password reset key")" "" "secret")
|
||||
CRYPTO_SECRET_KEY=$(read_input "$(bilingual "➤ 加密密钥(用于加密 API Key)" "➤ Encryption key (for API Key)")" "" "secret")
|
||||
echo ""
|
||||
|
||||
# 日志配置
|
||||
title "【日志配置】"
|
||||
echo -e "${CYAN}将配置:Root日志级别、应用日志级别${NC}"
|
||||
echo -e "${YELLOW}可选级别: TRACE, DEBUG, INFO, WARN, ERROR, OFF${NC}"
|
||||
LOG_LEVEL_ROOT=$(read_input "➤ Root 日志级别(第三方库)" "WARN")
|
||||
LOG_LEVEL_APP=$(read_input "➤ 应用日志级别" "INFO")
|
||||
title "$(bilingual "【日志配置】" "【Logging】")"
|
||||
echo -e "${CYAN}$(bilingual "将配置:Root日志级别、应用日志级别" "Root log level, App log level")${NC}"
|
||||
echo -e "${YELLOW}$(bilingual "可选级别: TRACE, DEBUG, INFO, WARN, ERROR, OFF" "Levels: TRACE, DEBUG, INFO, WARN, ERROR, OFF")${NC}"
|
||||
LOG_LEVEL_ROOT=$(read_input "$(bilingual "➤ Root 日志级别(第三方库)" "➤ Root log level (3rd party)")" "WARN")
|
||||
LOG_LEVEL_APP=$(read_input "$(bilingual "➤ 应用日志级别" "➤ App log level")" "INFO")
|
||||
echo ""
|
||||
|
||||
# 自动设置不需要用户输入的配置
|
||||
SPRING_PROFILES_ACTIVE="prod"
|
||||
ALLOW_PRERELEASE="false"
|
||||
GITHUB_REPO="WrBug/PolyHermes"
|
||||
}
|
||||
|
||||
# 下载 docker-compose.prod.yml(如果不存在)
|
||||
# 下载 docker-compose.prod.yml(如果不存在)/ Download docker-compose.prod.yml if missing
|
||||
download_docker_compose_file() {
|
||||
title "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||||
title " 步骤 3: 获取部署配置"
|
||||
title " $(bilingual "步骤 3: 获取部署配置" "Step 3: Get Deploy Config")"
|
||||
title "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||||
|
||||
if [ -f "docker-compose.prod.yml" ]; then
|
||||
info "检测到现有 docker-compose.prod.yml,跳过下载"
|
||||
info "$(bilingual "检测到现有 docker-compose.prod.yml,跳过下载" "Existing docker-compose.prod.yml found, skip download")"
|
||||
echo ""
|
||||
return 0
|
||||
fi
|
||||
|
||||
info "正在从 GitHub 下载 docker-compose.prod.yml..."
|
||||
info "$(bilingual "正在从 GitHub 下载 docker-compose.prod.yml..." "Downloading docker-compose.prod.yml from GitHub...")"
|
||||
|
||||
# GitHub raw 文件链接
|
||||
local compose_url="https://raw.githubusercontent.com/WrBug/PolyHermes/main/docker-compose.prod.yml"
|
||||
|
||||
# 尝试下载
|
||||
if curl -fsSL "$compose_url" -o docker-compose.prod.yml; then
|
||||
info "docker-compose.prod.yml 下载成功"
|
||||
info "$(bilingual "docker-compose.prod.yml 下载成功" "docker-compose.prod.yml downloaded")"
|
||||
else
|
||||
error "docker-compose.prod.yml 下载失败"
|
||||
warn "请检查网络连接或手动下载:"
|
||||
error "$(bilingual "docker-compose.prod.yml 下载失败" "Failed to download docker-compose.prod.yml")"
|
||||
warn "$(bilingual "请检查网络连接或手动下载:" "Check network or download manually:")"
|
||||
warn " $compose_url"
|
||||
exit 1
|
||||
fi
|
||||
@@ -235,28 +246,26 @@ download_docker_compose_file() {
|
||||
echo ""
|
||||
}
|
||||
|
||||
# 生成 .env 文件
|
||||
# 生成 .env 文件 / Generate .env file
|
||||
generate_env_file() {
|
||||
title "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||||
title " 步骤 4: 生成环境变量文件"
|
||||
title " $(bilingual "步骤 4: 生成环境变量文件" "Step 4: Generate .env")"
|
||||
title "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||||
|
||||
# 备份现有 .env 文件
|
||||
if [ -f ".env" ]; then
|
||||
BACKUP_FILE=".env.backup.$(date +%Y%m%d_%H%M%S)"
|
||||
cp .env "$BACKUP_FILE"
|
||||
warn "已备份现有配置文件到: $BACKUP_FILE"
|
||||
warn "$(bilingual "已备份现有配置文件到" "Backed up existing config to"): $BACKUP_FILE"
|
||||
fi
|
||||
|
||||
# 生成新的 .env 文件
|
||||
cat > .env <<EOF
|
||||
# ========================================
|
||||
# PolyHermes 生产环境配置
|
||||
# 生成时间: $(date '+%Y-%m-%d %H:%M:%S')
|
||||
# PolyHermes Production Config / 生产环境配置
|
||||
# Generated / 生成时间: $(date '+%Y-%m-%d %H:%M:%S')
|
||||
# ========================================
|
||||
|
||||
# ============================================
|
||||
# 基础配置
|
||||
# Basic / 基础配置
|
||||
# ============================================
|
||||
TZ=${TZ}
|
||||
SPRING_PROFILES_ACTIVE=${SPRING_PROFILES_ACTIVE}
|
||||
@@ -264,112 +273,107 @@ SERVER_PORT=${SERVER_PORT}
|
||||
MYSQL_PORT=${MYSQL_PORT}
|
||||
|
||||
# ============================================
|
||||
# 数据库配置
|
||||
# Database / 数据库配置
|
||||
# ============================================
|
||||
DB_URL=jdbc:mysql://mysql:3306/polyhermes?useSSL=false&serverTimezone=UTC&characterEncoding=utf8&allowPublicKeyRetrieval=true
|
||||
DB_USERNAME=${DB_USERNAME}
|
||||
DB_PASSWORD=${DB_PASSWORD}
|
||||
|
||||
# ============================================
|
||||
# 安全配置(请妥善保管)
|
||||
# Security (keep safe) / 安全配置(请妥善保管)
|
||||
# ============================================
|
||||
JWT_SECRET=${JWT_SECRET}
|
||||
ADMIN_RESET_PASSWORD_KEY=${ADMIN_RESET_PASSWORD_KEY}
|
||||
CRYPTO_SECRET_KEY=${CRYPTO_SECRET_KEY}
|
||||
|
||||
# ============================================
|
||||
# 日志配置
|
||||
# Logging / 日志配置
|
||||
# ============================================
|
||||
LOG_LEVEL_ROOT=${LOG_LEVEL_ROOT}
|
||||
LOG_LEVEL_APP=${LOG_LEVEL_APP}
|
||||
|
||||
# ============================================
|
||||
# 其他配置
|
||||
# Other / 其他配置
|
||||
# ============================================
|
||||
ALLOW_PRERELEASE=${ALLOW_PRERELEASE}
|
||||
GITHUB_REPO=${GITHUB_REPO}
|
||||
EOF
|
||||
|
||||
info "配置文件已生成: .env"
|
||||
info "$(bilingual "配置文件已生成" "Config file generated"): .env"
|
||||
echo ""
|
||||
|
||||
# 显示配置摘要
|
||||
title "【配置摘要】"
|
||||
echo " 服务器端口: ${SERVER_PORT}"
|
||||
echo " MySQL 端口: ${MYSQL_PORT}"
|
||||
echo " 时区: ${TZ}"
|
||||
echo " 数据库用户: ${DB_USERNAME}"
|
||||
echo " 数据库密码: ${DB_PASSWORD:0:8}... (已隐藏)"
|
||||
echo " JWT 密钥: ${JWT_SECRET:0:16}... (已隐藏)"
|
||||
echo " 管理员重置密钥: ${ADMIN_RESET_PASSWORD_KEY:0:16}... (已隐藏)"
|
||||
echo " 加密密钥: ${CRYPTO_SECRET_KEY:0:16}... (已隐藏)"
|
||||
echo " 日志级别: Root=${LOG_LEVEL_ROOT}, App=${LOG_LEVEL_APP}"
|
||||
title "$(bilingual "【配置摘要】" "【Config Summary】")"
|
||||
echo " $(bilingual "服务器端口" "Server port"): ${SERVER_PORT}"
|
||||
echo " $(bilingual "MySQL 端口" "MySQL port"): ${MYSQL_PORT}"
|
||||
echo " $(bilingual "时区" "Timezone"): ${TZ}"
|
||||
echo " $(bilingual "数据库用户" "DB user"): ${DB_USERNAME}"
|
||||
echo " $(bilingual "数据库密码" "DB password"): ${DB_PASSWORD:0:8}... $(bilingual "(已隐藏)" "(hidden)")"
|
||||
echo " $(bilingual "JWT 密钥" "JWT secret"): ${JWT_SECRET:0:16}... $(bilingual "(已隐藏)" "(hidden)")"
|
||||
echo " $(bilingual "管理员重置密钥" "Admin reset key"): ${ADMIN_RESET_PASSWORD_KEY:0:16}... $(bilingual "(已隐藏)" "(hidden)")"
|
||||
echo " $(bilingual "加密密钥" "Encryption key"): ${CRYPTO_SECRET_KEY:0:16}... $(bilingual "(已隐藏)" "(hidden)")"
|
||||
echo " $(bilingual "日志级别" "Log level"): Root=${LOG_LEVEL_ROOT}, App=${LOG_LEVEL_APP}"
|
||||
echo ""
|
||||
}
|
||||
|
||||
# 拉取镜像
|
||||
# 拉取镜像 / Pull images
|
||||
pull_images() {
|
||||
title "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||||
title " 步骤 5: 拉取 Docker 镜像"
|
||||
title " $(bilingual "步骤 5: 拉取 Docker 镜像" "Step 5: Pull Docker Images")"
|
||||
title "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||||
|
||||
info "正在从 Docker Hub 拉取最新镜像..."
|
||||
info "$(bilingual "正在从 Docker Hub 拉取最新镜像..." "Pulling latest images from Docker Hub...")"
|
||||
|
||||
# 拉取应用镜像
|
||||
if docker pull wrbug/polyhermes:latest; then
|
||||
info "应用镜像拉取成功: wrbug/polyhermes:latest"
|
||||
info "$(bilingual "应用镜像拉取成功" "App image pulled"): wrbug/polyhermes:latest"
|
||||
else
|
||||
error "应用镜像拉取失败"
|
||||
warn "可能的原因:"
|
||||
warn " 1. 网络连接问题"
|
||||
warn " 2. Docker Hub 服务异常"
|
||||
warn " 3. 镜像不存在"
|
||||
error "$(bilingual "应用镜像拉取失败" "Failed to pull app image")"
|
||||
warn "$(bilingual "可能的原因:" "Possible reasons:")"
|
||||
warn " 1. $(bilingual "网络连接问题" "Network issue")"
|
||||
warn " 2. $(bilingual "Docker Hub 服务异常" "Docker Hub unavailable")"
|
||||
warn " 3. $(bilingual "镜像不存在" "Image not found")"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 拉取 MySQL 镜像
|
||||
if docker pull mysql:8.2; then
|
||||
info "MySQL 镜像拉取成功: mysql:8.2"
|
||||
info "$(bilingual "MySQL 镜像拉取成功" "MySQL image pulled"): mysql:8.2"
|
||||
else
|
||||
warn "MySQL 镜像拉取失败,将在启动时自动下载"
|
||||
warn "$(bilingual "MySQL 镜像拉取失败,将在启动时自动下载" "MySQL pull failed, will download on start")"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
}
|
||||
|
||||
# 部署服务
|
||||
# 部署服务 / Deploy services
|
||||
deploy_services() {
|
||||
title "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||||
title " 步骤 6: 部署服务"
|
||||
title " $(bilingual "步骤 6: 部署服务" "Step 6: Deploy Services")"
|
||||
title "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||||
|
||||
# 停止现有服务
|
||||
if docker compose -f docker-compose.prod.yml ps -q 2>/dev/null | grep -q .; then
|
||||
warn "检测到正在运行的服务,正在停止..."
|
||||
warn "$(bilingual "检测到正在运行的服务,正在停止..." "Stopping existing services...")"
|
||||
docker compose -f docker-compose.prod.yml down
|
||||
info "已停止现有服务"
|
||||
info "$(bilingual "已停止现有服务" "Stopped existing services")"
|
||||
fi
|
||||
|
||||
# 启动服务
|
||||
info "正在启动服务..."
|
||||
info "$(bilingual "正在启动服务..." "Starting services...")"
|
||||
if docker compose -f docker-compose.prod.yml up -d; then
|
||||
info "服务启动成功"
|
||||
info "$(bilingual "服务启动成功" "Services started")"
|
||||
else
|
||||
error "服务启动失败"
|
||||
error "请检查日志: docker compose -f docker-compose.prod.yml logs"
|
||||
error "$(bilingual "服务启动失败" "Failed to start services")"
|
||||
error "$(bilingual "请检查日志" "Check logs"): docker compose -f docker-compose.prod.yml logs"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo ""
|
||||
}
|
||||
|
||||
# 健康检查
|
||||
# 健康检查 / Health check
|
||||
health_check() {
|
||||
title "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||||
title " 步骤 7: 健康检查"
|
||||
title " $(bilingual "步骤 7: 健康检查" "Step 7: Health Check")"
|
||||
title "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||||
|
||||
info "等待服务启动(最多等待 60 秒)..."
|
||||
info "$(bilingual "等待服务启动(最多等待 60 秒)..." "Waiting for services (up to 60s)...")"
|
||||
|
||||
local max_attempts=12
|
||||
local attempt=0
|
||||
@@ -377,13 +381,11 @@ health_check() {
|
||||
while [ $attempt -lt $max_attempts ]; do
|
||||
attempt=$((attempt + 1))
|
||||
|
||||
# 检查容器状态
|
||||
if docker compose -f docker-compose.prod.yml ps | grep -q "Up"; then
|
||||
info "容器运行正常"
|
||||
info "$(bilingual "容器运行正常" "Containers are up")"
|
||||
|
||||
# 检查应用是否响应
|
||||
if curl -s -o /dev/null -w "%{http_code}" http://localhost:${SERVER_PORT} | grep -q "200\|302\|401"; then
|
||||
info "应用响应正常"
|
||||
info "$(bilingual "应用响应正常" "App is responding")"
|
||||
echo ""
|
||||
return 0
|
||||
fi
|
||||
@@ -394,79 +396,76 @@ health_check() {
|
||||
done
|
||||
|
||||
echo ""
|
||||
warn "健康检查超时,请手动检查服务状态"
|
||||
warn "查看日志: docker compose -f docker-compose.prod.yml logs -f"
|
||||
warn "$(bilingual "健康检查超时,请手动检查服务状态" "Health check timeout, please check services manually")"
|
||||
warn "$(bilingual "查看日志" "View logs"): docker compose -f docker-compose.prod.yml logs -f"
|
||||
echo ""
|
||||
}
|
||||
|
||||
# 显示部署信息
|
||||
# 显示部署信息 / Show deployment info
|
||||
show_deployment_info() {
|
||||
title "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||||
title " 部署完成!"
|
||||
title " $(bilingual "部署完成!" "Deployment Complete!")"
|
||||
title "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||||
echo ""
|
||||
|
||||
info "访问地址: ${GREEN}http://localhost:${SERVER_PORT}${NC}"
|
||||
info "$(bilingual "访问地址" "Access URL"): ${GREEN}http://localhost:${SERVER_PORT}${NC}"
|
||||
echo ""
|
||||
|
||||
title "【常用命令】"
|
||||
echo -e " 查看服务状态: ${CYAN}docker compose -f docker-compose.prod.yml ps${NC}"
|
||||
echo -e " 查看日志: ${CYAN}docker compose -f docker-compose.prod.yml logs -f${NC}"
|
||||
echo -e " 停止服务: ${CYAN}docker compose -f docker-compose.prod.yml down${NC}"
|
||||
echo -e " 重启服务: ${CYAN}docker compose -f docker-compose.prod.yml restart${NC}"
|
||||
echo -e " 更新镜像: ${CYAN}docker pull wrbug/polyhermes:latest && docker compose -f docker-compose.prod.yml up -d${NC}"
|
||||
title "$(bilingual "【常用命令】" "【Common Commands】")"
|
||||
echo -e " $(bilingual "查看服务状态" "Status"): ${CYAN}docker compose -f docker-compose.prod.yml ps${NC}"
|
||||
echo -e " $(bilingual "查看日志" "Logs"): ${CYAN}docker compose -f docker-compose.prod.yml logs -f${NC}"
|
||||
echo -e " $(bilingual "停止服务" "Stop"): ${CYAN}docker compose -f docker-compose.prod.yml down${NC}"
|
||||
echo -e " $(bilingual "重启服务" "Restart"): ${CYAN}docker compose -f docker-compose.prod.yml restart${NC}"
|
||||
echo -e " $(bilingual "更新镜像" "Update"): ${CYAN}docker pull wrbug/polyhermes:latest && docker compose -f docker-compose.prod.yml up -d${NC}"
|
||||
echo ""
|
||||
|
||||
title "【数据库连接信息】"
|
||||
echo -e " 主机: ${CYAN}localhost${NC}"
|
||||
echo -e " 端口: ${CYAN}${MYSQL_PORT}${NC}"
|
||||
echo -e " 数据库: ${CYAN}polyhermes${NC}"
|
||||
echo -e " 用户名: ${CYAN}${DB_USERNAME}${NC}"
|
||||
echo -e " 密码: ${CYAN}${DB_PASSWORD}${NC}"
|
||||
title "$(bilingual "【数据库连接信息】" "【Database Connection】")"
|
||||
echo -e " $(bilingual "主机" "Host"): ${CYAN}localhost${NC}"
|
||||
echo -e " $(bilingual "端口" "Port"): ${CYAN}${MYSQL_PORT}${NC}"
|
||||
echo -e " $(bilingual "数据库" "Database"): ${CYAN}polyhermes${NC}"
|
||||
echo -e " $(bilingual "用户名" "Username"): ${CYAN}${DB_USERNAME}${NC}"
|
||||
echo -e " $(bilingual "密码" "Password"): ${CYAN}${DB_PASSWORD}${NC}"
|
||||
echo ""
|
||||
|
||||
title "【管理员重置密钥】"
|
||||
echo -e " 重置密钥: ${CYAN}${ADMIN_RESET_PASSWORD_KEY}${NC}"
|
||||
echo -e " ${YELLOW}💡 此密钥用于重置管理员密码,请妥善保管${NC}"
|
||||
title "$(bilingual "【管理员重置密钥】" "【Admin Reset Key】")"
|
||||
echo -e " $(bilingual "重置密钥" "Reset key"): ${CYAN}${ADMIN_RESET_PASSWORD_KEY}${NC}"
|
||||
echo -e " ${YELLOW}$(bilingual "💡 此密钥用于重置管理员密码,请妥善保管" "💡 Keep this key safe; it is used to reset admin password")${NC}"
|
||||
echo ""
|
||||
|
||||
warn "重要提示:"
|
||||
warn " 1. 请妥善保管 .env 文件,勿提交到版本控制系统"
|
||||
warn " 2. 定期备份数据库数据(位于 Docker volume: polyhermes_mysql-data)"
|
||||
warn " 3. 生产环境建议配置反向代理(如 Nginx)并启用 HTTPS"
|
||||
warn "$(bilingual "重要提示:" "Important:")"
|
||||
warn " 1. $(bilingual "请妥善保管 .env 文件,勿提交到版本控制系统" "Keep .env secure; do not commit to version control")"
|
||||
warn " 2. $(bilingual "定期备份数据库数据(位于 Docker volume: polyhermes_mysql-data)" "Back up DB regularly (Docker volume: polyhermes_mysql-data)")"
|
||||
warn " 3. $(bilingual "生产环境建议配置反向代理(如 Nginx)并启用 HTTPS" "Use a reverse proxy (e.g. Nginx) and HTTPS in production")"
|
||||
echo ""
|
||||
}
|
||||
|
||||
# 主函数
|
||||
# 主函数 / Main
|
||||
main() {
|
||||
clear
|
||||
|
||||
echo ""
|
||||
title "========================================="
|
||||
title " PolyHermes 交互式一键部署脚本 "
|
||||
title " $(bilingual "PolyHermes 交互式一键部署脚本" "PolyHermes Interactive Deploy") "
|
||||
title "========================================="
|
||||
echo ""
|
||||
|
||||
# 执行部署流程
|
||||
check_docker
|
||||
|
||||
# 检查是否已存在 .env 文件
|
||||
if [ -f ".env" ]; then
|
||||
echo ""
|
||||
title "【检测到现有配置】"
|
||||
info "发现已存在的 .env 配置文件"
|
||||
title "$(bilingual "【检测到现有配置】" "【Existing Config Found】")"
|
||||
info "$(bilingual "发现已存在的 .env 配置文件" "Found existing .env file")"
|
||||
echo ""
|
||||
echo -ne "${YELLOW}是否使用现有配置直接更新镜像?[Y/n]: ${NC}"
|
||||
echo -ne "${YELLOW}$(bilingual "是否使用现有配置直接更新镜像?[Y/n]" "Use existing config to update images? [Y/n]"): ${NC}"
|
||||
read -r use_existing
|
||||
use_existing=${use_existing:-Y}
|
||||
|
||||
if [[ "$use_existing" =~ ^[Yy]$ ]]; then
|
||||
info "将使用现有配置,跳过配置步骤"
|
||||
info "$(bilingual "将使用现有配置,跳过配置步骤" "Using existing config, skipping configuration")"
|
||||
echo ""
|
||||
# 从现有 .env 文件读取必要的变量
|
||||
source .env 2>/dev/null || true
|
||||
else
|
||||
warn "将重新配置,现有配置将被备份"
|
||||
warn "$(bilingual "将重新配置,现有配置将被备份" "Will reconfigure; existing config will be backed up")"
|
||||
echo ""
|
||||
collect_configuration
|
||||
fi
|
||||
@@ -476,21 +475,18 @@ main() {
|
||||
|
||||
download_docker_compose_file
|
||||
|
||||
# 只有在重新配置时才生成新的 .env 文件
|
||||
if [[ ! "$use_existing" =~ ^[Yy]$ ]] || [ ! -f ".env" ]; then
|
||||
generate_env_file
|
||||
fi
|
||||
|
||||
# 确认部署
|
||||
echo ""
|
||||
title "【确认部署】"
|
||||
echo -ne "${YELLOW}是否开始部署?[Y/n](回车默认为是): ${NC}"
|
||||
title "$(bilingual "【确认部署】" "【Confirm Deploy】")"
|
||||
echo -ne "${YELLOW}$(bilingual "是否开始部署?[Y/n](回车默认为是)" "Start deployment? [Y/n] (Enter = Yes)"): ${NC}"
|
||||
read -r confirm
|
||||
|
||||
# 默认为 Y,只有明确输入 n/N 才取消
|
||||
confirm=${confirm:-Y}
|
||||
if [[ "$confirm" =~ ^[Nn]$ ]]; then
|
||||
warn "部署已取消"
|
||||
warn "$(bilingual "部署已取消" "Deployment cancelled")"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
@@ -500,11 +496,11 @@ main() {
|
||||
health_check
|
||||
show_deployment_info
|
||||
|
||||
info "部署流程已完成!"
|
||||
info "$(bilingual "部署流程已完成!" "Deployment finished!")"
|
||||
}
|
||||
|
||||
# 捕获 Ctrl+C
|
||||
trap 'echo ""; warn "部署已中断"; exit 1' INT
|
||||
# 捕获 Ctrl+C / Handle Ctrl+C
|
||||
trap 'echo ""; warn "$(bilingual "部署已中断" "Deployment interrupted")"; exit 1' INT
|
||||
|
||||
# 运行主函数
|
||||
# 运行主函数 / Run main
|
||||
main "$@"
|
||||
|
||||
@@ -1644,6 +1644,48 @@
|
||||
"switchToLatest": "Switch to Latest Period",
|
||||
"periodEnded": "Current period has ended",
|
||||
"newPeriodAvailable": "New period has started"
|
||||
},
|
||||
"manualOrder": {
|
||||
"title": "Manual Order",
|
||||
"buttonUp": "Buy Up",
|
||||
"buttonDown": "Buy Down",
|
||||
"confirmTitle": "Manual Order Confirmation",
|
||||
"marketTitle": "Market Title",
|
||||
"direction": "Direction",
|
||||
"directionUp": "Up",
|
||||
"directionDown": "Down",
|
||||
"orderPrice": "Order Price",
|
||||
"orderSize": "Order Size",
|
||||
"totalAmount": "Total Amount",
|
||||
"account": "Account",
|
||||
"cancel": "Cancel",
|
||||
"confirm": "Confirm Order",
|
||||
"orderUnit": "USDC",
|
||||
"sizeUnit": "shares",
|
||||
"statusNotOrdered": "Not Ordered",
|
||||
"statusOrdered": "This period has been ordered",
|
||||
"statusOrderedAuto": "This period has been auto-ordered",
|
||||
"statusOrderedManual": "This period has been manually ordered",
|
||||
"errorInsufficientBalance": "Insufficient account balance, available: {balance} USDC",
|
||||
"errorPriceOutOfRange": "Price must be between 0 and 1",
|
||||
"errorMinSize": "Size cannot be less than 1 share",
|
||||
"errorExceedsBalance": "Total amount exceeds available balance",
|
||||
"errorPriceRequired": "Please enter order price",
|
||||
"errorSizeRequired": "Please enter order size",
|
||||
"success": "Manual order successful",
|
||||
"failed": "Manual order failed: {{reason}}",
|
||||
"errorSigning": "Signing failed: {reason}",
|
||||
"errorNetwork": "Network error, please try again later",
|
||||
"priceNotLoaded": "Price data not loaded",
|
||||
"insufficientBalance": "Insufficient balance, available amount less than 1 USDC",
|
||||
"fetchBalanceFailed": "Failed to fetch account balance",
|
||||
"availableBalance": "Available Balance",
|
||||
"fetchLatestPrice": "Latest Price",
|
||||
"priceUpdated": "Price updated",
|
||||
"maxSize": "Max",
|
||||
"invalidPriceOrBalance": "Invalid price or balance",
|
||||
"insufficientBalanceForMax": "Insufficient balance for max size",
|
||||
"maxSizeUpdated": "Updated to max size"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1644,6 +1644,48 @@
|
||||
"switchToLatest": "切换到最新周期",
|
||||
"periodEnded": "当前周期已结束",
|
||||
"newPeriodAvailable": "新周期已开始"
|
||||
},
|
||||
"manualOrder": {
|
||||
"title": "手动下单",
|
||||
"buttonUp": "买入 Up",
|
||||
"buttonDown": "买入 Down",
|
||||
"confirmTitle": "手动下单确认",
|
||||
"marketTitle": "市场标题",
|
||||
"direction": "方向",
|
||||
"directionUp": "Up",
|
||||
"directionDown": "Down",
|
||||
"orderPrice": "下单价格",
|
||||
"orderSize": "下单数量",
|
||||
"totalAmount": "总金额",
|
||||
"account": "账户",
|
||||
"cancel": "取消",
|
||||
"confirm": "确认下单",
|
||||
"orderUnit": "USDC",
|
||||
"sizeUnit": "张",
|
||||
"statusNotOrdered": "未下单",
|
||||
"statusOrdered": "本周期已下单",
|
||||
"statusOrderedAuto": "本周期已自动下单",
|
||||
"statusOrderedManual": "本周期已手动下单",
|
||||
"errorInsufficientBalance": "账户余额不足,可用余额:{{balance}} USDC",
|
||||
"errorPriceOutOfRange": "价格必须在 0~1 之间",
|
||||
"errorMinSize": "数量不能少于 1 张",
|
||||
"errorExceedsBalance": "总金额超过可用余额",
|
||||
"errorPriceRequired": "请输入下单价格",
|
||||
"errorSizeRequired": "请输入下单数量",
|
||||
"success": "手动下单成功",
|
||||
"failed": "手动下单失败:{{reason}}",
|
||||
"errorSigning": "签名失败:{{reason}}",
|
||||
"errorNetwork": "网络错误,请稍后重试",
|
||||
"priceNotLoaded": "价格数据未加载",
|
||||
"insufficientBalance": "余额不足,可用金额少于 1 USDC",
|
||||
"fetchBalanceFailed": "获取账户余额失败",
|
||||
"availableBalance": "可用余额",
|
||||
"fetchLatestPrice": "获取最新价",
|
||||
"priceUpdated": "价格已更新",
|
||||
"maxSize": "最大",
|
||||
"invalidPriceOrBalance": "价格或余额无效",
|
||||
"insufficientBalanceForMax": "余额不足以购买最大数量",
|
||||
"maxSizeUpdated": "已更新为最大数量"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1644,6 +1644,48 @@
|
||||
"switchToLatest": "切換到最新週期",
|
||||
"periodEnded": "當前週期已結束",
|
||||
"newPeriodAvailable": "新週期已開始"
|
||||
},
|
||||
"manualOrder": {
|
||||
"title": "手動下單",
|
||||
"buttonUp": "買入 Up",
|
||||
"buttonDown": "買入 Down",
|
||||
"confirmTitle": "手動下單確認",
|
||||
"marketTitle": "市場標題",
|
||||
"direction": "方向",
|
||||
"directionUp": "Up",
|
||||
"directionDown": "Down",
|
||||
"orderPrice": "下單價格",
|
||||
"orderSize": "下單數量",
|
||||
"totalAmount": "總金額",
|
||||
"account": "賬戶",
|
||||
"cancel": "取消",
|
||||
"confirm": "確認下單",
|
||||
"orderUnit": "USDC",
|
||||
"sizeUnit": "張",
|
||||
"statusNotOrdered": "未下單",
|
||||
"statusOrdered": "本週期已下單",
|
||||
"statusOrderedAuto": "本週期已自動下單",
|
||||
"statusOrderedManual": "本週期已手動下單",
|
||||
"errorInsufficientBalance": "賬戶餘額不足,可用餘額:{balance} USDC",
|
||||
"errorPriceOutOfRange": "價格必須在 0~1 之間",
|
||||
"errorMinSize": "數量不能少於 1 張",
|
||||
"errorExceedsBalance": "總金額超過可用餘額",
|
||||
"errorPriceRequired": "請輸入下單價格",
|
||||
"errorSizeRequired": "請輸入下單數量",
|
||||
"success": "手動下單成功",
|
||||
"failed": "手動下單失敗:{{reason}}",
|
||||
"errorSigning": "簽名失敗:{reason}",
|
||||
"errorNetwork": "網絡錯誤,請稍後重試",
|
||||
"priceNotLoaded": "價格數據未加載",
|
||||
"insufficientBalance": "餘額不足,可用金額少於 1 USDC",
|
||||
"fetchBalanceFailed": "獲取賬戶餘額失敗",
|
||||
"availableBalance": "可用餘額",
|
||||
"fetchLatestPrice": "獲取最新價",
|
||||
"priceUpdated": "價格已更新",
|
||||
"maxSize": "最大",
|
||||
"invalidPriceOrBalance": "價格或餘額無效",
|
||||
"insufficientBalanceForMax": "餘額不足以購買最大數量",
|
||||
"maxSizeUpdated": "已更新為最大數量"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,8 +10,6 @@ interface BacktestChartProps {
|
||||
}[]
|
||||
}
|
||||
|
||||
// Bug #39 Note: This chart currently displays cash balance (balanceAfter), not total equity.
|
||||
// A true equity curve (cash + position value) would require an equityAfter field in the trade records.
|
||||
const BacktestChart: React.FC<BacktestChartProps> = ({ trades }) => {
|
||||
const { t } = useTranslation()
|
||||
const chartRef = useRef<HTMLDivElement>(null)
|
||||
|
||||
@@ -12,9 +12,12 @@ import {
|
||||
Alert,
|
||||
Radio,
|
||||
Button,
|
||||
Tooltip
|
||||
Tooltip,
|
||||
Modal,
|
||||
InputNumber,
|
||||
message
|
||||
} from 'antd'
|
||||
import { ClockCircleOutlined, SyncOutlined, InfoCircleOutlined } from '@ant-design/icons'
|
||||
import { ClockCircleOutlined, SyncOutlined, InfoCircleOutlined, ShoppingCartOutlined } from '@ant-design/icons'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { useMediaQuery } from 'react-responsive'
|
||||
import * as echarts from 'echarts'
|
||||
@@ -61,6 +64,10 @@ const CryptoTailMonitor: React.FC = () => {
|
||||
const marketChartRef = useRef<HTMLDivElement>(null)
|
||||
const marketChartInstance = useRef<echarts.ECharts | null>(null)
|
||||
const lastPeriodStartRef = useRef<number | null>(null)
|
||||
const selectedStrategyIdRef = useRef<number | null>(null)
|
||||
useEffect(() => {
|
||||
selectedStrategyIdRef.current = selectedStrategyId
|
||||
}, [selectedStrategyId])
|
||||
|
||||
// localStorage key for period switch mode
|
||||
const PERIOD_SWITCH_MODE_KEY = 'cryptoTailMonitor_periodSwitchMode'
|
||||
@@ -84,6 +91,26 @@ const CryptoTailMonitor: React.FC = () => {
|
||||
// 标记当前是否在查看旧周期(手动模式下)
|
||||
const [isViewingOldPeriod, setIsViewingOldPeriod] = useState<boolean>(false)
|
||||
|
||||
// 手动下单状态
|
||||
const [manualOrderModal, setManualOrderModal] = useState<{
|
||||
visible: boolean
|
||||
direction: 'UP' | 'DOWN'
|
||||
price: string
|
||||
size: string
|
||||
totalAmount: string
|
||||
bestBid: string
|
||||
availableBalance: string
|
||||
}>({
|
||||
visible: false,
|
||||
direction: 'UP',
|
||||
price: '',
|
||||
size: '',
|
||||
totalAmount: '',
|
||||
bestBid: '',
|
||||
availableBalance: ''
|
||||
})
|
||||
const [ordering, setOrdering] = useState(false)
|
||||
|
||||
// 获取策略列表
|
||||
useEffect(() => {
|
||||
const fetchStrategies = async () => {
|
||||
@@ -127,7 +154,7 @@ const CryptoTailMonitor: React.FC = () => {
|
||||
setPendingPeriodData(null)
|
||||
setIsViewingOldPeriod(false)
|
||||
try {
|
||||
const res = await apiService.cryptoTailStrategy.monitorInit(selectedStrategyId)
|
||||
const res = await apiService.cryptoTailStrategy.monitorInit({ strategyId: selectedStrategyId })
|
||||
if (res.data.code === 0 && res.data.data) {
|
||||
setInitData(res.data.data)
|
||||
} else {
|
||||
@@ -172,11 +199,23 @@ const CryptoTailMonitor: React.FC = () => {
|
||||
const lastPeriod = lastPeriodStartRef.current
|
||||
|
||||
if (pushPeriod != null && pushPeriod !== lastPeriod) {
|
||||
// 新周期到来
|
||||
// 新周期到来:重新拉取 init(含 tokenIds),再更新状态
|
||||
lastPeriodStartRef.current = pushPeriod
|
||||
|
||||
const marketTitle = (data as { marketTitle?: string }).marketTitle
|
||||
const applyFreshInit = (fresh: CryptoTailMonitorInitResponse) => {
|
||||
if (selectedStrategyIdRef.current !== fresh.strategyId) return
|
||||
const merged: CryptoTailMonitorInitResponse = {
|
||||
...fresh,
|
||||
periodStartUnix: pushPeriod,
|
||||
marketTitle: marketTitle ?? fresh.marketTitle ?? ''
|
||||
}
|
||||
if (periodSwitchMode === 'manual' && lastPeriod != null) {
|
||||
setPendingPeriodData(p => p ? { ...p, initData: merged } : null)
|
||||
} else {
|
||||
setInitData(merged)
|
||||
}
|
||||
}
|
||||
if (periodSwitchMode === 'manual' && lastPeriod != null) {
|
||||
// 手动模式:保存新周期数据到 pending,保留当前显示
|
||||
setPendingPeriodData({
|
||||
periodStartUnix: pushPeriod,
|
||||
priceHistory: [newPoint],
|
||||
@@ -184,26 +223,35 @@ const CryptoTailMonitor: React.FC = () => {
|
||||
pushData: data
|
||||
})
|
||||
setIsViewingOldPeriod(true)
|
||||
// 更新 pending 数据的 initData
|
||||
setInitData(prev => {
|
||||
if (prev) {
|
||||
setPendingPeriodData(p => p ? { ...p, initData: { ...prev, periodStartUnix: pushPeriod, marketTitle: (data as { marketTitle?: string }).marketTitle ?? prev.marketTitle } } : null)
|
||||
apiService.cryptoTailStrategy.monitorInit({ strategyId: selectedStrategyId!, periodStartUnix: pushPeriod }).then(res => {
|
||||
if (res.data?.code === 0 && res.data?.data) applyFreshInit(res.data.data)
|
||||
else {
|
||||
setInitData(prev => {
|
||||
if (prev) setPendingPeriodData(p => p ? { ...p, initData: { ...prev, periodStartUnix: pushPeriod, marketTitle: marketTitle ?? prev.marketTitle ?? '' } } : null)
|
||||
return prev ?? null
|
||||
})
|
||||
}
|
||||
return prev
|
||||
}).catch(() => {
|
||||
setInitData(prev => {
|
||||
if (prev) setPendingPeriodData(p => p ? { ...p, initData: { ...prev, periodStartUnix: pushPeriod, marketTitle: marketTitle ?? prev.marketTitle ?? '' } } : null)
|
||||
return prev ?? null
|
||||
})
|
||||
})
|
||||
} else {
|
||||
// 自动模式或首次推送:直接切换
|
||||
if (lastPeriod != null) {
|
||||
setHasSwitchedPeriod(true)
|
||||
}
|
||||
if (lastPeriod != null) setHasSwitchedPeriod(true)
|
||||
setFirstDataTime(newPoint.time)
|
||||
setInitData(prev => prev ? { ...prev, periodStartUnix: pushPeriod, marketTitle: (data as { marketTitle?: string }).marketTitle ?? prev.marketTitle } : null)
|
||||
setPriceHistory([newPoint])
|
||||
setPushData(data)
|
||||
setIsViewingOldPeriod(false)
|
||||
setPendingPeriodData(null)
|
||||
return
|
||||
apiService.cryptoTailStrategy.monitorInit({ strategyId: selectedStrategyId!, periodStartUnix: pushPeriod }).then(res => {
|
||||
if (res.data?.code === 0 && res.data?.data) applyFreshInit(res.data.data)
|
||||
else setInitData(prev => prev ? { ...prev, periodStartUnix: pushPeriod, marketTitle: marketTitle ?? prev.marketTitle ?? '' } : null)
|
||||
}).catch(() => {
|
||||
setInitData(prev => prev ? { ...prev, periodStartUnix: pushPeriod, marketTitle: marketTitle ?? prev.marketTitle ?? '' } : null)
|
||||
})
|
||||
}
|
||||
return
|
||||
} else {
|
||||
// 同周期:追加数据
|
||||
if (periodSwitchMode === 'manual' && isViewingOldPeriod && pendingPeriodData) {
|
||||
@@ -709,6 +757,186 @@ const CryptoTailMonitor: React.FC = () => {
|
||||
const spreadBelowThreshold = currentSpread != null && currentSpread !== '' && minSpreadLineNum.length > 0 &&
|
||||
parseFloat(currentSpread) < Math.min(...minSpreadLineNum)
|
||||
|
||||
// 手动下单:打开弹窗
|
||||
const handleOpenManualOrderModal = async (direction: 'UP' | 'DOWN') => {
|
||||
if (!pushData) {
|
||||
message.warning(t('cryptoTailMonitor.manualOrder.priceNotLoaded'))
|
||||
return
|
||||
}
|
||||
const bestBid = direction === 'UP' ? pushData.currentPriceUp : pushData.currentPriceDown
|
||||
if (!bestBid) {
|
||||
message.warning(t('cryptoTailMonitor.manualOrder.priceNotLoaded'))
|
||||
return
|
||||
}
|
||||
// 计算默认价格:最优 bid × 1.1,限制在 0~0.99 之间
|
||||
const rawPrice = parseFloat(bestBid) * 1.1
|
||||
const defaultPrice = Math.min(0.99, Math.max(0, rawPrice))
|
||||
|
||||
// 获取账户余额
|
||||
let availableBalance = '0'
|
||||
if (initData?.accountId) {
|
||||
try {
|
||||
const balanceRes = await apiService.accounts.balance({ accountId: initData.accountId })
|
||||
if (balanceRes.data.code === 0 && balanceRes.data.data?.availableBalance) {
|
||||
availableBalance = balanceRes.data.data.availableBalance
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('获取账户余额失败:', e)
|
||||
}
|
||||
}
|
||||
|
||||
// 使用策略配置的金额
|
||||
let defaultAmountUsdc = 10
|
||||
if (initData?.amountMode === 'FIXED' && initData?.amountValue) {
|
||||
defaultAmountUsdc = parseFloat(initData.amountValue)
|
||||
} else if (initData?.amountMode === 'RATIO' && initData?.amountValue) {
|
||||
// RATIO 模式:按比例计算
|
||||
const balanceNum = parseFloat(availableBalance)
|
||||
const ratio = parseFloat(initData.amountValue || '10')
|
||||
defaultAmountUsdc = balanceNum * ratio / 100
|
||||
// 至少保留 1 USDC
|
||||
if (defaultAmountUsdc < 1) {
|
||||
message.warning(t('cryptoTailMonitor.manualOrder.insufficientBalance'))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 计算默认数量(保留2位小数,用于手动下单)
|
||||
let defaultSize = (defaultAmountUsdc / defaultPrice).toFixed(2)
|
||||
// 确保至少 1 张
|
||||
if (parseFloat(defaultSize) < 1) {
|
||||
defaultSize = '1.00'
|
||||
}
|
||||
|
||||
// 重新计算总金额(基于实际数量)
|
||||
const defaultTotalAmount = (defaultPrice * parseFloat(defaultSize)).toFixed(2)
|
||||
|
||||
setManualOrderModal({
|
||||
visible: true,
|
||||
direction,
|
||||
price: defaultPrice.toFixed(4),
|
||||
size: defaultSize,
|
||||
totalAmount: defaultTotalAmount,
|
||||
bestBid,
|
||||
availableBalance
|
||||
})
|
||||
}
|
||||
|
||||
// 获取最新价
|
||||
const handleFetchLatestPrice = async () => {
|
||||
if (!pushData) {
|
||||
message.warning(t('cryptoTailMonitor.manualOrder.priceNotLoaded'))
|
||||
return
|
||||
}
|
||||
const latestPrice = manualOrderModal.direction === 'UP'
|
||||
? pushData.currentPriceUp
|
||||
: pushData.currentPriceDown
|
||||
if (!latestPrice) {
|
||||
message.warning(t('cryptoTailMonitor.manualOrder.priceNotLoaded'))
|
||||
return
|
||||
}
|
||||
const price = Math.min(0.99, parseFloat(latestPrice))
|
||||
const size = parseFloat(manualOrderModal.size)
|
||||
const totalAmount = (price * size).toFixed(2)
|
||||
setManualOrderModal({
|
||||
...manualOrderModal,
|
||||
price: price.toFixed(4),
|
||||
totalAmount
|
||||
})
|
||||
message.success(t('cryptoTailMonitor.manualOrder.priceUpdated'))
|
||||
}
|
||||
|
||||
const handleCloseManualOrderModal = () => {
|
||||
setManualOrderModal({
|
||||
visible: false,
|
||||
direction: 'UP',
|
||||
price: '',
|
||||
size: '',
|
||||
totalAmount: '',
|
||||
bestBid: '',
|
||||
availableBalance: ''
|
||||
})
|
||||
}
|
||||
|
||||
const handlePriceChange = (value: number | null) => {
|
||||
if (value === null) return
|
||||
const clamped = Math.min(0.99, Math.max(0, value))
|
||||
const price = clamped.toFixed(4)
|
||||
const size = parseFloat(manualOrderModal.size)
|
||||
const totalAmount = (clamped * size).toFixed(2)
|
||||
setManualOrderModal({ ...manualOrderModal, price, totalAmount })
|
||||
}
|
||||
|
||||
const handleSizeChange = (value: number | null) => {
|
||||
if (value === null) return
|
||||
const size = value.toFixed(2)
|
||||
const priceRaw = parseFloat(manualOrderModal.price)
|
||||
const price = Math.min(0.99, Math.max(0, priceRaw))
|
||||
const totalAmount = (price * value).toFixed(2)
|
||||
setManualOrderModal({ ...manualOrderModal, size, totalAmount, price: price.toFixed(4) })
|
||||
}
|
||||
|
||||
// 计算最大数量(截位处理)
|
||||
const handleMaxSize = () => {
|
||||
const price = parseFloat(manualOrderModal.price)
|
||||
const balance = parseFloat(manualOrderModal.availableBalance)
|
||||
|
||||
if (price <= 0 || balance <= 0) {
|
||||
message.warning(t('cryptoTailMonitor.manualOrder.invalidPriceOrBalance'))
|
||||
return
|
||||
}
|
||||
|
||||
// 最大数量 = 余额 / 价格,保留2位小数
|
||||
let maxSize = Math.floor((balance / price) * 100) / 100
|
||||
|
||||
// 确保至少 1 张
|
||||
if (maxSize < 1) {
|
||||
message.warning(t('cryptoTailMonitor.manualOrder.insufficientBalanceForMax'))
|
||||
return
|
||||
}
|
||||
|
||||
const totalAmount = (price * maxSize).toFixed(2)
|
||||
setManualOrderModal({
|
||||
...manualOrderModal,
|
||||
size: maxSize.toFixed(2),
|
||||
totalAmount
|
||||
})
|
||||
message.success(t('cryptoTailMonitor.manualOrder.maxSizeUpdated'))
|
||||
}
|
||||
|
||||
const handleManualOrder = async () => {
|
||||
if (!initData || !pushData) return
|
||||
try {
|
||||
setOrdering(true)
|
||||
const tokenIds: string[] = []
|
||||
if (initData.tokenIdUp) tokenIds.push(initData.tokenIdUp)
|
||||
if (initData.tokenIdDown) tokenIds.push(initData.tokenIdDown)
|
||||
const request = {
|
||||
strategyId: initData.strategyId,
|
||||
periodStartUnix: pushData.periodStartUnix,
|
||||
direction: manualOrderModal.direction,
|
||||
price: Math.min(0.99, Math.max(0, parseFloat(manualOrderModal.price) || 0)).toFixed(4),
|
||||
size: manualOrderModal.size,
|
||||
marketTitle: pushData.marketTitle || initData.marketTitle,
|
||||
tokenIds
|
||||
}
|
||||
const res = await apiService.cryptoTailStrategy.manualOrder(request)
|
||||
if (res.data.code === 0 && res.data.data?.success) {
|
||||
message.success(t('cryptoTailMonitor.manualOrder.success'))
|
||||
handleCloseManualOrderModal()
|
||||
} else {
|
||||
const reason = res.data.msg?.trim() || 'unknown'
|
||||
message.error(t('cryptoTailMonitor.manualOrder.failed', { reason }))
|
||||
}
|
||||
} catch (error: unknown) {
|
||||
const err = error as { response?: { data?: { msg?: string } }; message?: string }
|
||||
const reason = err?.response?.data?.msg?.trim() ?? err?.message?.trim() ?? 'unknown'
|
||||
message.error(t('cryptoTailMonitor.manualOrder.failed', { reason }))
|
||||
} finally {
|
||||
setOrdering(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div style={{ padding: isMobile ? 12 : 24 }}>
|
||||
<Title level={2} style={{ marginBottom: 16, fontSize: isMobile ? 20 : 24 }}>
|
||||
@@ -717,18 +945,22 @@ const CryptoTailMonitor: React.FC = () => {
|
||||
|
||||
{/* 顶部控制区 */}
|
||||
<Card style={{ marginBottom: 16 }}>
|
||||
<Space wrap size="middle">
|
||||
<Space>
|
||||
<Space direction={isMobile ? 'vertical' : 'horizontal'} size="middle" style={{ width: '100%' }}>
|
||||
<Space direction={isMobile ? 'vertical' : 'horizontal'} size="small" style={{ width: isMobile ? '100%' : 'auto' }}>
|
||||
<Text strong>{t('cryptoTailMonitor.selectStrategy')}</Text>
|
||||
<Select
|
||||
style={{ minWidth: isMobile ? 200 : 300 }}
|
||||
style={{ minWidth: isMobile ? '100%' : 300, width: isMobile ? '100%' : 'auto' }}
|
||||
loading={strategiesLoading}
|
||||
value={selectedStrategyId}
|
||||
onChange={(id) => setSelectedStrategyId(id)}
|
||||
placeholder={t('cryptoTailMonitor.selectStrategyPlaceholder')}
|
||||
popupMatchSelectWidth={false}
|
||||
dropdownStyle={{ minWidth: isMobile ? 280 : 'auto', wordWrap: 'break-word', whiteSpace: 'normal' }}
|
||||
optionLabelProp="label"
|
||||
options={strategies.map(s => ({
|
||||
label: `${s.name || s.marketSlugPrefix} (${s.intervalSeconds === 300 ? '5m' : '15m'})`,
|
||||
value: s.id
|
||||
value: s.id,
|
||||
style: { whiteSpace: 'normal', wordWrap: 'break-word' }
|
||||
}))}
|
||||
/>
|
||||
</Space>
|
||||
@@ -914,29 +1146,63 @@ const CryptoTailMonitor: React.FC = () => {
|
||||
/>
|
||||
</Card>
|
||||
|
||||
{/* 手动下单 */}
|
||||
{!isMobile ? (
|
||||
<Card title={t('cryptoTailMonitor.manualOrder.title')} style={{ marginTop: 16 }}>
|
||||
<Row gutter={16}>
|
||||
<Col span={12}>
|
||||
<Button
|
||||
type="primary"
|
||||
icon={<ShoppingCartOutlined />}
|
||||
disabled={!pushData || pushData.triggered || pushData.periodEnded}
|
||||
onClick={() => handleOpenManualOrderModal('UP')}
|
||||
loading={ordering}
|
||||
block
|
||||
style={{ backgroundColor: '#1890ff', borderColor: '#1890ff' }}
|
||||
>
|
||||
{t('cryptoTailMonitor.manualOrder.buttonUp')}
|
||||
</Button>
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
<Button
|
||||
type="primary"
|
||||
icon={<ShoppingCartOutlined />}
|
||||
disabled={!pushData || pushData.triggered || pushData.periodEnded}
|
||||
onClick={() => handleOpenManualOrderModal('DOWN')}
|
||||
loading={ordering}
|
||||
block
|
||||
style={{ backgroundColor: '#fa8c16', borderColor: '#fa8c16' }}
|
||||
>
|
||||
{t('cryptoTailMonitor.manualOrder.buttonDown')}
|
||||
</Button>
|
||||
</Col>
|
||||
</Row>
|
||||
</Card>
|
||||
) : null}
|
||||
|
||||
{/* 策略信息 */}
|
||||
<Card title={t('cryptoTailMonitor.strategyInfo.title')} style={{ marginTop: 16 }}>
|
||||
<Row gutter={[16, 8]}>
|
||||
<Col span={12}>
|
||||
<Row gutter={[16, isMobile ? 12 : 8]}>
|
||||
<Col xs={24} sm={24} md={12}>
|
||||
<Text type="secondary">{t('cryptoTailMonitor.strategyInfo.market')}: </Text>
|
||||
<Text>{pushData?.marketTitle ?? initData.marketTitle}</Text>
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
<Col xs={24} sm={24} md={12}>
|
||||
<Text type="secondary">{t('cryptoTailMonitor.strategyInfo.interval')}: </Text>
|
||||
<Text>{initData.intervalSeconds === 300 ? '5m' : '15m'}</Text>
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
<Col xs={24} sm={24} md={12}>
|
||||
<Text type="secondary">{t('cryptoTailMonitor.strategyInfo.account')}: </Text>
|
||||
<Text>{initData.accountName || `#${initData.accountId}`}</Text>
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
<Col xs={24} sm={24} md={12}>
|
||||
<Text type="secondary">{t('cryptoTailMonitor.strategyInfo.spreadMode')}: </Text>
|
||||
<Text>{initData.minSpreadMode}</Text>
|
||||
{initData.minSpreadMode === 'FIXED' && initData.minSpreadValue && (
|
||||
<Text> ({formatNumber(initData.minSpreadValue, 4)})</Text>
|
||||
)}
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
<Col xs={24} sm={24} md={12}>
|
||||
<Text type="secondary">{t('cryptoTailMonitor.strategyInfo.spreadDirection')}: </Text>
|
||||
<Text>{(initData.spreadDirection ?? 'MIN') === 'MAX' ? t('cryptoTailMonitor.stat.configuredSpreadMax') : t('cryptoTailMonitor.stat.configuredSpreadMin')}</Text>
|
||||
</Col>
|
||||
@@ -944,6 +1210,162 @@ const CryptoTailMonitor: React.FC = () => {
|
||||
</Card>
|
||||
</>
|
||||
)}
|
||||
|
||||
{/* 手动下单确认弹窗 */}
|
||||
<Modal
|
||||
title={t('cryptoTailMonitor.manualOrder.confirmTitle')}
|
||||
open={manualOrderModal.visible}
|
||||
onCancel={handleCloseManualOrderModal}
|
||||
footer={[
|
||||
<Button key="cancel" onClick={handleCloseManualOrderModal}>
|
||||
{t('cryptoTailMonitor.manualOrder.cancel')}
|
||||
</Button>,
|
||||
<Button
|
||||
key="confirm"
|
||||
type="primary"
|
||||
onClick={handleManualOrder}
|
||||
loading={ordering}
|
||||
style={
|
||||
manualOrderModal.direction === 'UP'
|
||||
? { backgroundColor: '#1890ff', borderColor: '#1890ff' }
|
||||
: { backgroundColor: '#fa8c16', borderColor: '#fa8c16' }
|
||||
}
|
||||
>
|
||||
{t('cryptoTailMonitor.manualOrder.confirm')}
|
||||
</Button>
|
||||
]}
|
||||
width={isMobile ? '100%' : 480}
|
||||
style={isMobile ? { maxWidth: '100%', top: 0, paddingBottom: 0 } : undefined}
|
||||
>
|
||||
{initData && (
|
||||
<Space direction="vertical" style={{ width: '100%' }} size={16}>
|
||||
<Row gutter={[12, 8]}>
|
||||
<Col span={24}>
|
||||
<Text type="secondary" style={{ display: 'block', marginBottom: 4 }}>
|
||||
{t('cryptoTailMonitor.manualOrder.marketTitle')}
|
||||
</Text>
|
||||
<Text>{pushData?.marketTitle ?? initData.marketTitle}</Text>
|
||||
</Col>
|
||||
<Col span={24}>
|
||||
<Text type="secondary" style={{ display: 'block', marginBottom: 4 }}>
|
||||
{t('cryptoTailMonitor.manualOrder.direction')}
|
||||
</Text>
|
||||
<Text
|
||||
strong
|
||||
style={{
|
||||
color: manualOrderModal.direction === 'UP' ? '#1890ff' : '#fa8c16'
|
||||
}}
|
||||
>
|
||||
{manualOrderModal.direction === 'UP'
|
||||
? t('cryptoTailMonitor.manualOrder.directionUp')
|
||||
: t('cryptoTailMonitor.manualOrder.directionDown')}
|
||||
</Text>
|
||||
</Col>
|
||||
<Col span={24}>
|
||||
<Text type="secondary" style={{ display: 'block', marginBottom: 4 }}>
|
||||
{t('cryptoTailMonitor.manualOrder.availableBalance')}
|
||||
</Text>
|
||||
<Text strong style={{ fontSize: 16, color: '#52c41a' }}>
|
||||
{manualOrderModal.availableBalance ? formatNumber(manualOrderModal.availableBalance, 2) : '-'} {t('cryptoTailMonitor.manualOrder.orderUnit')}
|
||||
</Text>
|
||||
</Col>
|
||||
<Col span={24}>
|
||||
<Text type="secondary" style={{ display: 'block', marginBottom: 4 }}>
|
||||
{t('cryptoTailMonitor.manualOrder.orderPrice')} ({t('cryptoTailMonitor.manualOrder.orderUnit')})
|
||||
</Text>
|
||||
<Space.Compact style={{ width: '100%' }}>
|
||||
<InputNumber
|
||||
style={{ width: '100%' }}
|
||||
value={manualOrderModal.price ? parseFloat(manualOrderModal.price) : undefined}
|
||||
onChange={handlePriceChange}
|
||||
min={0}
|
||||
max={1}
|
||||
step={0.0001}
|
||||
precision={4}
|
||||
placeholder="0.0000"
|
||||
/>
|
||||
<Button onClick={handleFetchLatestPrice} icon={<SyncOutlined />}>
|
||||
{t('cryptoTailMonitor.manualOrder.fetchLatestPrice')}
|
||||
</Button>
|
||||
</Space.Compact>
|
||||
</Col>
|
||||
<Col span={24}>
|
||||
<Text type="secondary" style={{ display: 'block', marginBottom: 4 }}>
|
||||
{t('cryptoTailMonitor.manualOrder.orderSize')} ({t('cryptoTailMonitor.manualOrder.sizeUnit')})
|
||||
</Text>
|
||||
<Space.Compact style={{ width: '100%' }}>
|
||||
<InputNumber
|
||||
style={{ width: '100%' }}
|
||||
value={manualOrderModal.size ? parseFloat(manualOrderModal.size) : undefined}
|
||||
onChange={handleSizeChange}
|
||||
min={1}
|
||||
precision={2}
|
||||
placeholder="1"
|
||||
/>
|
||||
<Button onClick={handleMaxSize} type="primary" ghost>
|
||||
{t('cryptoTailMonitor.manualOrder.maxSize')}
|
||||
</Button>
|
||||
</Space.Compact>
|
||||
</Col>
|
||||
<Col span={24}>
|
||||
<Text type="secondary" style={{ display: 'block', marginBottom: 4 }}>
|
||||
{t('cryptoTailMonitor.manualOrder.totalAmount')}
|
||||
</Text>
|
||||
<Text strong style={{ fontSize: 16 }}>
|
||||
{manualOrderModal.totalAmount} {t('cryptoTailMonitor.manualOrder.orderUnit')}
|
||||
</Text>
|
||||
</Col>
|
||||
<Col span={24}>
|
||||
<Text type="secondary" style={{ display: 'block', marginBottom: 4 }}>
|
||||
{t('cryptoTailMonitor.manualOrder.account')}
|
||||
</Text>
|
||||
<Text>{initData.accountName || `#${initData.accountId}`}</Text>
|
||||
</Col>
|
||||
</Row>
|
||||
</Space>
|
||||
)}
|
||||
</Modal>
|
||||
|
||||
{/* 移动端底部悬浮按钮 */}
|
||||
{isMobile && (
|
||||
<div
|
||||
style={{
|
||||
position: 'fixed',
|
||||
bottom: 0,
|
||||
left: 0,
|
||||
right: 0,
|
||||
zIndex: 1000,
|
||||
padding: '12px 16px',
|
||||
background: 'rgba(255, 255, 255, 0.95)',
|
||||
backdropFilter: 'blur(10px)',
|
||||
borderTop: '1px solid #f0f0f0',
|
||||
boxShadow: '0 -2px 8px rgba(0, 0, 0, 0.06)'
|
||||
}}
|
||||
>
|
||||
<div style={{ display: 'flex', width: '100%', gap: 0 }}>
|
||||
<Button
|
||||
type="primary"
|
||||
icon={<ShoppingCartOutlined />}
|
||||
disabled={!pushData || pushData.triggered || pushData.periodEnded}
|
||||
onClick={() => handleOpenManualOrderModal('UP')}
|
||||
loading={ordering}
|
||||
style={{ flex: 1, backgroundColor: '#1890ff', borderColor: '#1890ff', height: 44, borderRadius: '6px 0 0 6px' }}
|
||||
>
|
||||
{t('cryptoTailMonitor.manualOrder.buttonUp')}
|
||||
</Button>
|
||||
<Button
|
||||
type="primary"
|
||||
icon={<ShoppingCartOutlined />}
|
||||
disabled={!pushData || pushData.triggered || pushData.periodEnded}
|
||||
onClick={() => handleOpenManualOrderModal('DOWN')}
|
||||
loading={ordering}
|
||||
style={{ flex: 1, backgroundColor: '#fa8c16', borderColor: '#fa8c16', height: 44, borderRadius: '0 6px 6px 0' }}
|
||||
>
|
||||
{t('cryptoTailMonitor.manualOrder.buttonDown')}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -498,10 +498,20 @@ export const apiService = {
|
||||
apiClient.post<ApiResponse<import('../types').CryptoTailMarketOptionDto[]>>('/crypto-tail-strategy/market-options', {}),
|
||||
autoMinSpread: (data: { intervalSeconds: number }) =>
|
||||
apiClient.post<ApiResponse<import('../types').CryptoTailAutoMinSpreadResponse>>('/crypto-tail-strategy/auto-min-spread', data),
|
||||
monitorInit: (strategyId: number) =>
|
||||
apiClient.post<ApiResponse<import('../types').CryptoTailMonitorInitResponse>>('/crypto-tail-strategy/monitor/init', { strategyId })
|
||||
monitorInit: (data: { strategyId: number; periodStartUnix?: number }) =>
|
||||
apiClient.post<ApiResponse<import('../types').CryptoTailMonitorInitResponse>>('/crypto-tail-strategy/monitor/init', data),
|
||||
manualOrder: (data: {
|
||||
strategyId: number
|
||||
periodStartUnix: number
|
||||
direction: 'UP' | 'DOWN'
|
||||
price: string
|
||||
size: string
|
||||
marketTitle: string
|
||||
tokenIds: string[]
|
||||
}) =>
|
||||
apiClient.post<ApiResponse<import('../types').CryptoTailManualOrderResponse>>('/crypto-tail-strategy/manual-order', data)
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* 订单管理 API
|
||||
*/
|
||||
|
||||
@@ -1156,6 +1156,10 @@ export interface CryptoTailMonitorInitResponse {
|
||||
currentTimestamp: number
|
||||
/** 是否启用 */
|
||||
enabled: boolean
|
||||
/** 投入金额模式: FIXED or RATIO */
|
||||
amountMode?: string
|
||||
/** 投入金额数值 */
|
||||
amountValue?: string
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1203,3 +1207,27 @@ export interface CryptoTailMonitorPushData {
|
||||
/** 周期是否已结束 */
|
||||
periodEnded: boolean
|
||||
}
|
||||
|
||||
export interface CryptoTailManualOrderResponse {
|
||||
/** 是否成功 */
|
||||
success: boolean
|
||||
/** 订单ID */
|
||||
orderId?: string
|
||||
/** 提示消息 */
|
||||
message: string
|
||||
/** 下单详情 */
|
||||
orderDetails?: ManualOrderDetails
|
||||
}
|
||||
|
||||
export interface ManualOrderDetails {
|
||||
/** 策略ID */
|
||||
strategyId: number
|
||||
/** 方向 */
|
||||
direction: string
|
||||
/** 下单价格 */
|
||||
price: string
|
||||
/** 下单数量 */
|
||||
size: string
|
||||
/** 总金额 */
|
||||
totalAmount: string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user