mirror of
https://github.com/laoxong/nofx.git
synced 2026-06-04 01:48:22 +08:00
f4ece051e7
* refactor: 简化交易动作,移除 update_stop_loss/update_take_profit/partial_close - 移除 Decision 结构体中的 NewStopLoss, NewTakeProfit, ClosePercentage 字段 - 删除 executeUpdateStopLossWithRecord, executeUpdateTakeProfitWithRecord, executePartialCloseWithRecord 函数 - 简化 logger 中的 partial_close 聚合逻辑 - 更新 AI prompt 和验证逻辑,只保留 6 个核心动作 - 清理相关测试代码 保留的交易动作: open_long, open_short, close_long, close_short, hold, wait * refactor: 移除 AI学习与反思 模块 - 删除前端 AILearning.tsx 组件和相关引用 - 删除后端 /performance API 接口 - 删除 logger 中 AnalyzePerformance、calculateSharpeRatio 等函数 - 删除 PerformanceAnalysis、TradeOutcome、SymbolPerformance 等结构体 - 删除 Context 中的 Performance 字段 - 移除 AI prompt 中夏普比率自我进化相关内容 - 清理 i18n 翻译文件中的相关条目 该模块基于磁盘存储计算,经常出错,做减法移除 * refactor: 将数据库操作统一迁移到 store 包 - 新增 store/ 包,统一管理所有数据库操作 - store.go: 主 Store 结构,懒加载各子模块 - user.go, ai_model.go, exchange.go, trader.go 等子模块 - 支持加密/解密函数注入 (SetCryptoFuncs) - 更新 main.go 使用 store.New() 替代 config.NewDatabase() - 更新 api/server.go 使用 *store.Store 替代 *config.Database - 更新 manager/trader_manager.go: - 新增 LoadTradersFromStore, LoadUserTradersFromStore 方法 - 删除旧版 LoadUserTraders, LoadTraderByID, loadSingleTrader 等方法 - 移除 nofx/config 依赖 - 删除 config/database.go 和 config/database_test.go - 更新 api/server_test.go 使用 store.Trader 类型 - 清理 logger/ 包中未使用的 telegram 相关代码 * refactor: unify encryption key management via .env - Remove redundant EncryptionManager and SecureStorage - Simplify CryptoService to load keys from environment variables only - RSA_PRIVATE_KEY: RSA private key for client-server encryption - DATA_ENCRYPTION_KEY: AES-256 key for database encryption - JWT_SECRET: JWT signing key for authentication - Update start.sh to auto-generate missing keys on first run - Remove secrets/ directory and file-based key storage - Delete obsolete encryption setup scripts - Update .env.example with all required keys * refactor: unify logger usage across mcp package - Add MCPLogger adapter in logger package to implement mcp.Logger interface - Update mcp/config.go to use global logger by default - Remove redundant defaultLogger from mcp/logger.go - Keep noopLogger for testing purposes * chore: remove leftover test RSA key file * chore: remove unused bootstrap package * refactor: unify logging to use logger package instead of fmt/log - Replace all fmt.Print/log.Print calls with logger package - Add auto-initialization in logger package init() for test compatibility - Update main.go to initialize logger at startup - Migrate all packages: api, backtest, config, decision, manager, market, store, trader * refactor: rename database file from config.db to data.db - Update main.go, start.sh, docker-compose.yml - Update migration script and documentation - Update .gitignore and translations * fix: add RSA_PRIVATE_KEY to docker-compose environment * fix: add registration_enabled to /api/config response * fix: Fix navigation between login and register pages Use window.location.href instead of react-router's navigate() to fix the issue where URL changes but the page doesn't reload due to App.tsx using custom route state management. * fix: Switch SQLite from WAL to DELETE mode for Docker compatibility WAL mode causes data sync issues with Docker bind mounts on macOS due to incompatible file locking mechanisms between the container and host. DELETE mode (traditional journaling) ensures data is written directly to the main database file. * refactor: Remove default user from database initialization The default user was a legacy placeholder that is no longer needed now that proper user registration is in place. * feat: Add order tracking system with centralized status sync - Add trader_orders table for tracking all order lifecycle - Implement GetOrderStatus interface for all exchanges (Binance, Bybit, Hyperliquid, Aster, Lighter) - Create OrderSyncManager for centralized order status polling - Add trading statistics (Sharpe ratio, win rate, profit factor) to AI context - Include recent completed orders in AI decision input - Remove per-order goroutine polling in favor of global sync manager * feat: Add TradingView K-line chart to dashboard - Create TradingViewChart component with exchange/symbol selectors - Support Binance, Bybit, OKX, Coinbase, Kraken, KuCoin exchanges - Add popular symbols quick selection - Support multiple timeframes (1m to 1W) - Add fullscreen mode - Integrate with Dashboard page below equity chart - Add i18n translations for zh/en * refactor: Replace separate charts with tabbed ChartTabs component - Create ChartTabs component with tab switching between equity curve and K-line - Add embedded mode support for EquityChart and TradingViewChart - User can now switch between account equity and market chart in same area * fix: Use ChartTabs in App.tsx and fix embedded mode in EquityChart - Replace EquityChart with ChartTabs in App.tsx (the actual dashboard renderer) - Fix EquityChart embedded mode for error and empty data states - Rename interval state to timeInterval to avoid shadowing window.setInterval - Add debug logging to ChartTabs component * feat: Add position tracking system for accurate trade history - Add trader_positions table to track complete open/close trades - Add PositionSyncManager to detect manual closes via polling - Record position on open, update on close with PnL calculation - Use positions table for trading stats and recent trades (replacing orders table) - Fix TradingView chart symbol format (add .P suffix for futures) - Fix DecisionCard wait/hold action color (gray instead of red) - Auto-append USDT suffix for custom symbol input * update ---------
280 lines
8.2 KiB
Go
280 lines
8.2 KiB
Go
package trader
|
||
|
||
import (
|
||
"context"
|
||
"crypto/ecdsa"
|
||
"encoding/json"
|
||
"fmt"
|
||
"io"
|
||
"nofx/logger"
|
||
"net/http"
|
||
"strings"
|
||
"sync"
|
||
"time"
|
||
|
||
lighterClient "github.com/elliottech/lighter-go/client"
|
||
lighterHTTP "github.com/elliottech/lighter-go/client/http"
|
||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||
"github.com/ethereum/go-ethereum/crypto"
|
||
)
|
||
|
||
// AccountInfo LIGHTER 賬戶信息
|
||
type AccountInfo struct {
|
||
AccountIndex int64 `json:"account_index"`
|
||
L1Address string `json:"l1_address"`
|
||
// 其他字段可以根據實際 API 響應添加
|
||
}
|
||
|
||
// LighterTraderV2 使用官方 lighter-go SDK 的新實現
|
||
type LighterTraderV2 struct {
|
||
ctx context.Context
|
||
privateKey *ecdsa.PrivateKey // L1 錢包私鑰(用於識別賬戶)
|
||
walletAddr string // Ethereum 錢包地址
|
||
|
||
client *http.Client
|
||
baseURL string
|
||
testnet bool
|
||
chainID uint32
|
||
|
||
// SDK 客戶端
|
||
httpClient lighterClient.MinimalHTTPClient
|
||
txClient *lighterClient.TxClient
|
||
|
||
// API Key 管理
|
||
apiKeyPrivateKey string // 40字節的 API Key 私鑰(用於簽名交易)
|
||
apiKeyIndex uint8 // API Key 索引(默認 0)
|
||
accountIndex int64 // 賬戶索引
|
||
|
||
// 認證令牌
|
||
authToken string
|
||
tokenExpiry time.Time
|
||
accountMutex sync.RWMutex
|
||
|
||
// 市場信息緩存
|
||
symbolPrecision map[string]SymbolPrecision
|
||
precisionMutex sync.RWMutex
|
||
|
||
// 市場索引緩存
|
||
marketIndexMap map[string]uint8 // symbol -> market_id
|
||
marketMutex sync.RWMutex
|
||
}
|
||
|
||
// NewLighterTraderV2 創建新的 LIGHTER 交易器(使用官方 SDK)
|
||
// 參數說明:
|
||
// - l1PrivateKeyHex: L1 錢包私鑰(32字節,標準以太坊私鑰)
|
||
// - walletAddr: 以太坊錢包地址(可選,會從私鑰自動派生)
|
||
// - apiKeyPrivateKeyHex: API Key 私鑰(40字節,用於簽名交易)如果為空則需要生成
|
||
// - testnet: 是否使用測試網
|
||
func NewLighterTraderV2(l1PrivateKeyHex, walletAddr, apiKeyPrivateKeyHex string, testnet bool) (*LighterTraderV2, error) {
|
||
// 1. 解析 L1 私鑰
|
||
l1PrivateKeyHex = strings.TrimPrefix(strings.ToLower(l1PrivateKeyHex), "0x")
|
||
l1PrivateKey, err := crypto.HexToECDSA(l1PrivateKeyHex)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("無效的 L1 私鑰: %w", err)
|
||
}
|
||
|
||
// 2. 如果沒有提供錢包地址,從私鑰派生
|
||
if walletAddr == "" {
|
||
walletAddr = crypto.PubkeyToAddress(*l1PrivateKey.Public().(*ecdsa.PublicKey)).Hex()
|
||
logger.Infof("✓ 從私鑰派生錢包地址: %s", walletAddr)
|
||
}
|
||
|
||
// 3. 確定 API URL 和 Chain ID
|
||
baseURL := "https://mainnet.zklighter.elliot.ai"
|
||
chainID := uint32(42766) // Mainnet Chain ID
|
||
if testnet {
|
||
baseURL = "https://testnet.zklighter.elliot.ai"
|
||
chainID = uint32(42069) // Testnet Chain ID
|
||
}
|
||
|
||
// 4. 創建 HTTP 客戶端
|
||
httpClient := lighterHTTP.NewClient(baseURL)
|
||
|
||
trader := &LighterTraderV2{
|
||
ctx: context.Background(),
|
||
privateKey: l1PrivateKey,
|
||
walletAddr: walletAddr,
|
||
client: &http.Client{Timeout: 30 * time.Second},
|
||
baseURL: baseURL,
|
||
testnet: testnet,
|
||
chainID: chainID,
|
||
httpClient: httpClient,
|
||
apiKeyPrivateKey: apiKeyPrivateKeyHex,
|
||
apiKeyIndex: 0, // 默認使用索引 0
|
||
symbolPrecision: make(map[string]SymbolPrecision),
|
||
marketIndexMap: make(map[string]uint8),
|
||
}
|
||
|
||
// 5. 初始化賬戶(獲取賬戶索引)
|
||
if err := trader.initializeAccount(); err != nil {
|
||
return nil, fmt.Errorf("初始化賬戶失敗: %w", err)
|
||
}
|
||
|
||
// 6. 如果沒有 API Key,提示用戶需要生成
|
||
if apiKeyPrivateKeyHex == "" {
|
||
logger.Infof("⚠️ 未提供 API Key 私鑰,請調用 GenerateAndRegisterAPIKey() 生成")
|
||
logger.Infof(" 或者從 LIGHTER 官網獲取現有的 API Key")
|
||
return trader, nil
|
||
}
|
||
|
||
// 7. 創建 TxClient(用於簽名交易)
|
||
txClient, err := lighterClient.NewTxClient(
|
||
httpClient,
|
||
apiKeyPrivateKeyHex,
|
||
trader.accountIndex,
|
||
trader.apiKeyIndex,
|
||
trader.chainID,
|
||
)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("創建 TxClient 失敗: %w", err)
|
||
}
|
||
|
||
trader.txClient = txClient
|
||
|
||
// 8. 驗證 API Key 是否正確
|
||
if err := trader.checkClient(); err != nil {
|
||
logger.Infof("⚠️ API Key 驗證失敗: %v", err)
|
||
logger.Infof(" 您可能需要重新生成 API Key 或檢查配置")
|
||
return trader, err
|
||
}
|
||
|
||
logger.Infof("✓ LIGHTER 交易器初始化成功 (account=%d, apiKey=%d, testnet=%v)",
|
||
trader.accountIndex, trader.apiKeyIndex, testnet)
|
||
|
||
return trader, nil
|
||
}
|
||
|
||
// initializeAccount 初始化賬戶信息(獲取賬戶索引)
|
||
func (t *LighterTraderV2) initializeAccount() error {
|
||
// 通過 L1 地址獲取賬戶信息
|
||
accountInfo, err := t.getAccountByL1Address()
|
||
if err != nil {
|
||
return fmt.Errorf("獲取賬戶信息失敗: %w", err)
|
||
}
|
||
|
||
t.accountMutex.Lock()
|
||
t.accountIndex = accountInfo.AccountIndex
|
||
t.accountMutex.Unlock()
|
||
|
||
logger.Infof("✓ 賬戶索引: %d", t.accountIndex)
|
||
return nil
|
||
}
|
||
|
||
// getAccountByL1Address 通過 L1 錢包地址獲取 LIGHTER 賬戶信息
|
||
func (t *LighterTraderV2) getAccountByL1Address() (*AccountInfo, error) {
|
||
endpoint := fmt.Sprintf("%s/api/v1/account?by=address&value=%s", t.baseURL, t.walletAddr)
|
||
|
||
req, err := http.NewRequest("GET", endpoint, nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
resp, err := t.client.Do(req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
defer resp.Body.Close()
|
||
|
||
body, err := io.ReadAll(resp.Body)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
if resp.StatusCode != http.StatusOK {
|
||
return nil, fmt.Errorf("獲取賬戶失敗 (status %d): %s", resp.StatusCode, string(body))
|
||
}
|
||
|
||
var accountInfo AccountInfo
|
||
if err := json.Unmarshal(body, &accountInfo); err != nil {
|
||
return nil, fmt.Errorf("解析賬戶響應失敗: %w", err)
|
||
}
|
||
|
||
return &accountInfo, nil
|
||
}
|
||
|
||
// checkClient 驗證 API Key 是否正確
|
||
func (t *LighterTraderV2) checkClient() error {
|
||
if t.txClient == nil {
|
||
return fmt.Errorf("TxClient 未初始化")
|
||
}
|
||
|
||
// 獲取服務器上註冊的 API Key 公鑰
|
||
publicKey, err := t.httpClient.GetApiKey(t.accountIndex, t.apiKeyIndex)
|
||
if err != nil {
|
||
return fmt.Errorf("獲取 API Key 失敗: %w", err)
|
||
}
|
||
|
||
// 獲取本地 API Key 的公鑰
|
||
pubKeyBytes := t.txClient.GetKeyManager().PubKeyBytes()
|
||
localPubKey := hexutil.Encode(pubKeyBytes[:])
|
||
localPubKey = strings.Replace(localPubKey, "0x", "", 1)
|
||
|
||
// 比對公鑰
|
||
if publicKey != localPubKey {
|
||
return fmt.Errorf("API Key 不匹配:本地=%s, 服務器=%s", localPubKey, publicKey)
|
||
}
|
||
|
||
logger.Infof("✓ API Key 驗證通過")
|
||
return nil
|
||
}
|
||
|
||
// GenerateAndRegisterAPIKey 生成新的 API Key 並註冊到 LIGHTER
|
||
// 注意:這需要 L1 私鑰簽名,所以必須在有 L1 私鑰的情況下調用
|
||
func (t *LighterTraderV2) GenerateAndRegisterAPIKey(seed string) (privateKey, publicKey string, err error) {
|
||
// 這個功能需要調用官方 SDK 的 GenerateAPIKey 函數
|
||
// 但這是在 sharedlib 中的 CGO 函數,無法直接在純 Go 代碼中調用
|
||
//
|
||
// 解決方案:
|
||
// 1. 讓用戶從 LIGHTER 官網生成 API Key
|
||
// 2. 或者我們可以實現一個簡單的 API Key 生成包裝器
|
||
|
||
return "", "", fmt.Errorf("GenerateAndRegisterAPIKey 功能待實現,請從 LIGHTER 官網生成 API Key")
|
||
}
|
||
|
||
// refreshAuthToken 刷新認證令牌(使用官方 SDK)
|
||
func (t *LighterTraderV2) refreshAuthToken() error {
|
||
if t.txClient == nil {
|
||
return fmt.Errorf("TxClient 未初始化,請先設置 API Key")
|
||
}
|
||
|
||
// 使用官方 SDK 生成認證令牌(有效期 7 小時)
|
||
deadline := time.Now().Add(7 * time.Hour)
|
||
authToken, err := t.txClient.GetAuthToken(deadline)
|
||
if err != nil {
|
||
return fmt.Errorf("生成認證令牌失敗: %w", err)
|
||
}
|
||
|
||
t.accountMutex.Lock()
|
||
t.authToken = authToken
|
||
t.tokenExpiry = deadline
|
||
t.accountMutex.Unlock()
|
||
|
||
logger.Infof("✓ 認證令牌已生成(有效期至: %s)", t.tokenExpiry.Format(time.RFC3339))
|
||
return nil
|
||
}
|
||
|
||
// ensureAuthToken 確保認證令牌有效
|
||
func (t *LighterTraderV2) ensureAuthToken() error {
|
||
t.accountMutex.RLock()
|
||
expired := time.Now().After(t.tokenExpiry.Add(-30 * time.Minute)) // 提前 30 分鐘刷新
|
||
t.accountMutex.RUnlock()
|
||
|
||
if expired {
|
||
logger.Info("🔄 認證令牌即將過期,刷新中...")
|
||
return t.refreshAuthToken()
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
// GetExchangeType 獲取交易所類型
|
||
func (t *LighterTraderV2) GetExchangeType() string {
|
||
return "lighter"
|
||
}
|
||
|
||
// Cleanup 清理資源
|
||
func (t *LighterTraderV2) Cleanup() error {
|
||
logger.Info("⏹ LIGHTER 交易器清理完成")
|
||
return nil
|
||
}
|