Skip to content

[P0 嚴重] 創建交易員時初始餘額被固定為 1000 #787

@the-dev-z

Description

@the-dev-z

[P0 嚴重] 創建交易員時初始餘額被固定為 1000

[P0 Critical] Initial Balance Locked at 1000 When Creating Trader


📋 問題描述 | Bug Description

中文
無論用戶在創建交易員表單中填入任何初始餘額(如 100, 500, 2000),創建後實際餘額始終為 1000 USDT,導致無法自定義起始資金。

English:
Regardless of what initial balance users enter in the trader creation form (e.g., 100, 500, 2000), the actual balance after creation is always 1000 USDT, preventing custom starting capital.

嚴重性 | Severity: 🔴 P0 - 阻塞正常使用 | Blocks Normal Usage


🔄 復現步驟 | Reproduction Steps

  1. 打開 http://localhost:3000/traders

    • Open http://localhost:3000/traders
  2. 點擊 「+ 創建交易員」按鈕

    • Click "+ Create Trader" button
  3. 填寫表單 | Fill form:

    • 交易員名稱 | Trader Name: Test-HYP
    • 選擇交易所 | Select Exchange: HYPERLIQUID
    • 初始餘額 | Initial Balance: 輸入 100(而非預設的 1000)| Enter 100 (not default 1000)
    • 其他字段按正常填寫 | Fill other fields normally
  4. 點擊「創建」| Click "Create"

  5. 查看新創建的交易員卡片 | Check newly created trader card


✅ 期望行為 | Expected Behavior

  • 用戶輸入初始餘額 100 → 實際餘額應為 100 USDT

    • User enters 100 → Actual balance should be 100 USDT
  • Total Equity 應顯示接近 100(如 99.xx)

    • Total Equity should display close to 100 (e.g., 99.xx)
  • P&L 計算基準應為用戶輸入的 100

    • P&L calculation base should be user-entered 100

❌ 實際行為 | Actual Behavior

  • 用戶輸入 100,但 Total Equity 顯示 99.83 USDT(接近 1000 的數值)

    • User enters 100, but Total Equity shows 99.83 USDT (close to 1000)
  • P&L 計算仍基於 1000,而非用戶輸入的 100

    • P&L still calculated based on 1000, not user-entered 100
  • 修改餘額後也不生效(見相關 Issue)

    • Editing balance doesn't work either (see related issue)

🔬 技術根因(推測)| Root Cause (Estimated)

可能原因 1 | Possible Cause 1: 前端未正確傳遞參數 | Frontend not passing parameter

位置 | Location: web/src/components/AITradersPage.tsx 創建表單提交邏輯 | Create form submission logic

// ❌ 可能遺漏了 initialBalance
// ❌ May have missed initialBalance
const createTrader = async (data) => {
    await fetch('/api/traders', {
        method: 'POST',
        body: JSON.stringify({
            name: data.name,
            exchange: data.exchange,
            // initialBalance: data.initialBalance,  ← 可能遺漏 | May be missing
        })
    })
}

可能原因 2 | Possible Cause 2: 後端有 hardcoded 邏輯 | Backend hardcoded logic

位置 | Location: api/server.go handleCreateTrader 函數 | function

// ❌ 可能存在類似邏輯 | May have similar logic
trader := &Trader{
    Name:           req.Name,
    Exchange:       req.Exchange,
    InitialBalance: 1000,  // ← hardcoded!
}

可能原因 3 | Possible Cause 3: 資料庫預設值 | Database default value

位置 | Location: database/schema.sql 或 ORM model

資料庫 traders 表的 initial_balance 欄位有預設值 1000

  • Database traders table initial_balance field has default value 1000

💡 建議修復方案 | Suggested Fix

Step 1: 確認前端是否傳遞參數 | Confirm frontend passes parameter

// web/src/components/AITradersPage.tsx
const handleCreateTrader = async (formData) => {
    const payload = {
        name: formData.name,
        exchange: formData.exchange,
        initialBalance: parseFloat(formData.initialBalance), // ✅ 確保傳遞 | Ensure passed
        // ... 其他字段 | other fields
    }

    console.log('Creating trader with payload:', payload) // 🔍 Debug

    const res = await fetch('/api/traders', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(payload)
    })
}

Step 2: 確認後端接收並使用參數 | Confirm backend receives and uses parameter

// api/server.go - handleCreateTrader
type CreateTraderRequest struct {
    Name           string  `json:"name"`
    Exchange       string  `json:"exchange"`
    InitialBalance float64 `json:"initialBalance"` // ✅ 確保接收 | Ensure received
}

func (s *Server) handleCreateTrader(c *gin.Context) {
    var req CreateTraderRequest
    if err := c.BindJSON(&req); err != nil {
        return
    }

    log.Printf("📝 創建交易員 - 初始餘額: %.2f | Creating trader - Initial balance: %.2f", req.InitialBalance)

    trader := &Trader{
        Name:           req.Name,
        Exchange:       req.Exchange,
        InitialBalance: req.InitialBalance, // ✅ 使用用戶輸入值 | Use user input
    }

    s.database.CreateTrader(trader)
}

Step 3: 檢查資料庫寫入 | Check database write

-- 檢查最近創建的交易員 | Check recently created traders
SELECT id, name, initial_balance, created_at
FROM traders
ORDER BY created_at DESC
LIMIT 5;

🧪 測試建議 | Test Recommendations

  • 邊界值測試 1 | Boundary Test 1: 初始餘額設為 0.01 | Set initial balance to 0.01
  • 邊界值測試 2 | Boundary Test 2: 初始餘額設為 1000000 | Set to 1000000
  • 正常值測試 | Normal Test: 設為 100, 500, 2000,確認創建後餘額正確 | Confirm balance correct after creation
  • 前端日誌檢查 | Frontend Log: 打開 DevTools Console,查看 POST 請求的 payload | Check POST request payload
  • 後端日誌檢查 | Backend Log: 查看 docker logs nofx-trading,確認接收到的參數 | Confirm received parameters

🔗 相關 Issue | Related Issues


📊 環境資訊 | Environment

  • 版本 | Version: 基於 NoFxAiOS/nofx dev 分支 | Based on NoFxAiOS/nofx dev branch
  • 測試交易所 | Test Exchange: HYPERLIQUID
  • 測試日期 | Test Date: 2025-11-09

優先級建議 | Priority: 🔴 P0 - 阻塞正常使用,建議優先修復 | Blocks normal usage, priority fix recommended

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions