mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2025-12-06 13:54:41 +08:00
* feat(trader): add LIGHTER DEX integration (initial implementation) Add pure Go implementation of LIGHTER DEX trader following NOFX architecture Features: - ✅ Account management with Ethereum wallet authentication - ✅ Order operations: market/limit orders, cancel, query - ✅ Position & balance queries - ✅ Zero-fee trading support (Standard accounts) - ✅ Up to 50x leverage for BTC/ETH Implementation: - Pure Go (no CGO dependencies) for easy deployment - Based on hyperliquid_trader.go architecture - Uses Ethereum ECDSA signatures (like Hyperliquid) - API base URL: https://mainnet.zklighter.elliot.ai Files: - lighter_trader.go: Core trader structure & auth - lighter_orders.go: Order management (create/cancel/query) - lighter_account.go: Balance & position queries Status: ⚠️ Partial implementation - ✅ Core structure complete - ⏸️ Auth token generation needs implementation - ⏸️ Transaction signing logic needs completion - ⏸️ Config integration pending Next steps: 1. Complete auth token generation 2. Add to config/exchange registry 3. Add frontend UI support 4. Create test suite 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: tinkle-community <tinklefund@gmail.com> * feat: Add LIGHTER DEX integration (快速整合階段) ## 🚀 新增功能 - ✅ 添加 LIGHTER DEX 作為第四個支持的交易所 (Binance, Hyperliquid, Aster, LIGHTER) - ✅ 完整的數據庫配置支持(ExchangeConfig 新增 LighterWalletAddr, LighterPrivateKey 字段) - ✅ 交易所註冊與初始化(initDefaultData 註冊 "lighter") - ✅ TraderManager 集成(配置傳遞邏輯完成) - ✅ AutoTrader 支持(NewAutoTrader 添加 "lighter" case) ## 📝 實現細節 ### 後端整合 1. **數據庫層** (config/database.go): - ExchangeConfig 添加 LIGHTER 字段 - 創建表時添加 lighter_wallet_addr, lighter_private_key 欄位 - ALTER TABLE 語句用於向後兼容 - UpdateExchange/CreateExchange/GetExchanges 支持 LIGHTER - migrateExchangesTable 支持 LIGHTER 字段 2. **API 層** (api/server.go, api/utils.go): - UpdateExchangeConfigRequest 添加 LIGHTER 字段 - SanitizeExchangeConfigForLog 添加脫敏處理 3. **Trader 層** (trader/): - lighter_trader.go: 核心結構、認證、初始化 - lighter_account.go: 餘額、持倉、市場價格查詢 - lighter_orders.go: 訂單管理(創建、取消、查詢) - lighter_trading.go: 交易功能實現(開多/空、平倉、止損/盈) - 實現完整 Trader interface (13個方法) 4. **Manager 層** (manager/trader_manager.go): - addTraderFromDB 添加 LIGHTER 配置設置 - AutoTraderConfig 添加 LIGHTER 字段 ### 實現的功能(快速整合階段) ✅ 基礎交易功能 (OpenLong, OpenShort, CloseLong, CloseShort) ✅ 餘額查詢 (GetBalance, GetAccountBalance) ✅ 持倉查詢 (GetPositions, GetPosition) ✅ 訂單管理 (CreateOrder, CancelOrder, CancelAllOrders) ✅ 止損/止盈 (SetStopLoss, SetTakeProfit, CancelStopLossOrders) ✅ 市場數據 (GetMarketPrice) ✅ 格式化工具 (FormatQuantity) ## ⚠️ TODO(完整實現階段) - [ ] 完整認證令牌生成邏輯 (refreshAuthToken) - [ ] 完整交易簽名邏輯(參考 Python SDK) - [ ] 從 API 獲取幣種精度 - [ ] 區分止損/止盈訂單類型 - [ ] 前端 UI 支持 - [ ] 完整測試套件 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: tinkle-community <tinklefund@gmail.com> * feat: 完整集成 LIGHTER DEX with SDK - 集成官方 lighter-go SDK (v0.0.0-20251104171447-78b9b55ebc48) - 集成 Poseidon2 Goldilocks 簽名庫 (poseidon_crypto v0.0.11) - 實現完整的 LighterTraderV2 使用官方 SDK - 實現 17 個 Trader 接口方法(賬戶、交易、訂單管理) - 支持雙密鑰系統(L1 錢包 + API Key) - V1/V2 自動切換機制(向後兼容) - 自動認證令牌管理(8小時有效期) - 添加完整集成文檔 LIGHTER_INTEGRATION.md 新增文件: - trader/lighter_trader_v2.go - V2 核心結構和初始化 - trader/lighter_trader_v2_account.go - 賬戶查詢方法 - trader/lighter_trader_v2_trading.go - 交易操作方法 - trader/lighter_trader_v2_orders.go - 訂單管理方法 - LIGHTER_INTEGRATION.md - 完整文檔 修改文件: - trader/auto_trader.go - 添加 LighterAPIKeyPrivateKey 配置 - config/database.go - 添加 API Key 字段支持 - go.mod, go.sum - 添加 SDK 依賴 🤖 Generated with Claude Code Co-Authored-By: tinkle-community <tinklefund@gmail.com> * feat(lighter): 實現完整 HTTP 調用與動態市場映射 ### 實現的功能 #### 1. submitOrder() - 真實訂單提交 - 使用 POST /api/v1/sendTx 提交已簽名訂單 - tx_type: 14 (CREATE_ORDER) - 價格保護機制 (price_protection) - 完整錯誤處理與響應解析 #### 2. GetActiveOrders() - 查詢活躍訂單 - GET /api/v1/accountActiveOrders - 使用認證令牌 (Authorization header) - 支持按市場索引過濾 #### 3. CancelOrder() - 真實取消訂單 - 使用 SDK 簽名 CancelOrderTxReq - POST /api/v1/sendTx with tx_type: 15 (CANCEL_ORDER) - 自動 nonce 管理 #### 4. getMarketIndex() - 動態市場映射 - 從 GET /api/v1/orderBooks 獲取市場列表 - 內存緩存 (marketIndexMap) 提高性能 - 回退到硬編碼映射(API 失敗時) - 線程安全 (sync.RWMutex) ### 技術實現 **數據結構**: - SendTxRequest/SendTxResponse - sendTx 請求響應 - MarketInfo - 市場信息緩存 **並發安全**: - marketMutex - 保護市場索引緩存 - 讀寫鎖優化性能 **錯誤處理**: - API 失敗回退機制 - 詳細日誌記錄 - HTTP 狀態碼驗證 ### 測試 ✅ 編譯通過 (CGO_ENABLED=1) ✅ 所有 Trader 接口方法實現完整 ✅ HTTP 調用格式符合 LIGHTER API 規範 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: tinkle-community <tinklefund@gmail.com> * feat(lighter): 數據庫遷移與前端類型支持 ### 數據庫變更 #### 新增欄位 - `exchanges.lighter_api_key_private_key` TEXT DEFAULT '' - 支持 LIGHTER V2 的 40 字節 API Key 私鑰 #### 遷移腳本 - 📄 `migrations/002_add_lighter_api_key.sql` - 包含完整的驗證和統計查詢 - 向後兼容現有配置(默認為空,使用 V1) #### Schema 更新 - `config/database.go`: - 更新 CREATE TABLE 語句 - 更新 exchanges_new 表結構 - 新增 ALTER TABLE 遷移命令 ### 前端類型更新 #### types.ts - 新增 `Exchange` 接口字段: - `lighterWalletAddr?: string` - L1 錢包地址 - `lighterPrivateKey?: string` - L1 私鑰 - `lighterApiKeyPrivateKey?: string` - API Key 私鑰(⭐新增) ### 技術細節 **數據庫兼容性**: - 使用 ALTER TABLE ADD COLUMN IF NOT EXISTS - 默認值為空字符串 - 不影響現有數據 **類型安全**: - TypeScript 可選字段 - 與後端 ExchangeConfig 結構對齊 ### 下一步 ⏳ **待完成**: 1. ExchangeConfigModal 組件更新 2. API 調用參數傳遞 3. V1/V2 狀態顯示 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: tinkle-community <tinklefund@gmail.com> * docs(lighter): 更新 LIGHTER_INTEGRATION.md 文檔狀態 * feat(lighter): 前端完整實現 - API Key 配置與 V1/V2 狀態 **英文**: - `lighterWalletAddress`, `lighterPrivateKey`, `lighterApiKeyPrivateKey` - `lighterWalletAddressDesc`, `lighterPrivateKeyDesc`, `lighterApiKeyPrivateKeyDesc` - `lighterApiKeyOptionalNote` - V1 模式提示 - `lighterV1Description`, `lighterV2Description` - 狀態說明 - `lighterPrivateKeyImported` - 導入成功提示 **中文(繁體)**: - 完整的中文翻譯對應 - 專業術語保留原文(L1、API Key、Poseidon2) **Exchange 接口**: - `lighterWalletAddr?: string` - `lighterPrivateKey?: string` - `lighterApiKeyPrivateKey?: string` **UpdateExchangeConfigRequest 接口**: - `lighter_wallet_addr?: string` - `lighter_private_key?: string` - `lighter_api_key_private_key?: string` **狀態管理**: - 添加 3 個 LIGHTER 狀態變量 - 更新 `secureInputTarget` 類型包含 'lighter' **表單字段**: - L1 錢包地址(必填,text input) - L1 私鑰(必填,password + 安全輸入) - API Key 私鑰(可選,password,40 字節) **V1/V2 狀態顯示**: - 動態背景顏色(V1: 橙色 #3F2E0F,V2: 綠色 #0F3F2E) - 圖標指示(V1: ⚠️,V2: ✅) - 狀態說明文字 **驗證邏輯**: - 必填字段:錢包地址 + L1 私鑰 - API Key 為可選字段 - 自動 V1/V2 檢測 **安全輸入**: - 支持通過 TwoStageKeyModal 安全導入私鑰 - 導入成功後顯示 toast 提示 **handleSaveExchange**: - 添加 3 個 LIGHTER 參數 - 更新交易所對象(新增/更新) - 構建 API 請求(snake_case 字段) **V1 模式(無 API Key)**: ``` ┌────────────────────────────────────────┐ │ ⚠️ LIGHTER V1 │ │ 基本模式 - 功能受限,僅用於測試框架 │ └────────────────────────────────────────┘ 背景: #3F2E0F (橙色調) 邊框: #F59E0B (橙色) ``` **V2 模式(有 API Key)**: ``` ┌────────────────────────────────────────┐ │ ✅ LIGHTER V2 │ │ 完整模式 - 支持 Poseidon2 簽名和真實交易 │ └────────────────────────────────────────┘ 背景: #0F3F2E (綠色調) 邊框: #10B981 (綠色) ``` 1. **類型安全** - 完整的 TypeScript 類型定義 - Props 接口正確對齊 - ✅ 無 LIGHTER 相關編譯錯誤 2. **用戶體驗** - 清晰的必填/可選字段區分 - 實時 V1/V2 狀態反饋 - 安全私鑰輸入支持 3. **向後兼容** - 不影響現有交易所配置 - 所有字段為可選(Optional) - API 請求格式統一 ✅ TypeScript 編譯通過(無 LIGHTER 錯誤) ✅ 類型定義完整且正確 ✅ 所有必需文件已更新 ✅ 與後端 API 格式對齊 Modified: - `web/src/i18n/translations.ts` - 中英文翻譯 - `web/src/types.ts` - 類型定義 - `web/src/components/traders/ExchangeConfigModal.tsx` - Modal 組件 - `web/src/hooks/useTraderActions.ts` - Actions hook 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: tinkle-community <tinklefund@gmail.com> * test(lighter): 添加 V1 測試套件與修復 SafeFloat64 缺失 - 新增 trader/helpers.go: 添加 SafeFloat64/SafeString/SafeInt 輔助函數 - 新增 trader/lighter_trader_test.go: LIGHTER V1 測試套件 - ✅ 測試通過 (7/10): - NewTrader 驗證 (無效私鑰, 有效私鑰格式) - FormatQuantity - GetExchangeType - InvalidQuantity 驗證 - InvalidLeverage 驗證 - HelperFunctions (SafeFloat64) - ⚠️ 待改進 (3/10): - GetBalance (需要調整 mock 響應格式) - GetPositions (需要調整 mock 響應格式) - GetMarketPrice (需要調整 mock 響應格式) - 修復 Bug: lighter_account.go 和 lighter_trader_v2_account.go 中未定義的 SafeFloat64 - 測試框架: httptest.Server mock LIGHTER API - 安全: 使用固定測試私鑰 (不含真實資金) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: tinkle-community <tinklefund@gmail.com> --------- Co-authored-by: the-dev-z <the-dev-z@users.noreply.github.com> Co-authored-by: tinkle-community <tinklefund@gmail.com>
652 lines
19 KiB
TypeScript
652 lines
19 KiB
TypeScript
import { api } from '../lib/api'
|
|
import type {
|
|
TraderInfo,
|
|
CreateTraderRequest,
|
|
TraderConfigData,
|
|
AIModel,
|
|
Exchange,
|
|
} from '../types'
|
|
import { t } from '../i18n/translations'
|
|
import { confirmToast } from '../lib/notify'
|
|
import { toast } from 'sonner'
|
|
import type { Language } from '../i18n/translations'
|
|
|
|
interface UseTraderActionsParams {
|
|
traders: TraderInfo[] | undefined
|
|
allModels: AIModel[]
|
|
allExchanges: Exchange[]
|
|
supportedModels: AIModel[]
|
|
supportedExchanges: Exchange[]
|
|
language: Language
|
|
mutateTraders: () => Promise<any>
|
|
setAllModels: (models: AIModel[]) => void
|
|
setAllExchanges: (exchanges: Exchange[]) => void
|
|
setUserSignalSource: (config: {
|
|
coinPoolUrl: string
|
|
oiTopUrl: string
|
|
}) => void
|
|
setShowCreateModal: (show: boolean) => void
|
|
setShowEditModal: (show: boolean) => void
|
|
setShowModelModal: (show: boolean) => void
|
|
setShowExchangeModal: (show: boolean) => void
|
|
setShowSignalSourceModal: (show: boolean) => void
|
|
setEditingModel: (modelId: string | null) => void
|
|
setEditingExchange: (exchangeId: string | null) => void
|
|
editingTrader: TraderConfigData | null
|
|
setEditingTrader: (trader: TraderConfigData | null) => void
|
|
}
|
|
|
|
export function useTraderActions({
|
|
traders,
|
|
allModels,
|
|
allExchanges,
|
|
supportedModels,
|
|
supportedExchanges,
|
|
language,
|
|
mutateTraders,
|
|
setAllModels,
|
|
setAllExchanges,
|
|
setUserSignalSource,
|
|
setShowCreateModal,
|
|
setShowEditModal,
|
|
setShowModelModal,
|
|
setShowExchangeModal,
|
|
setShowSignalSourceModal,
|
|
setEditingModel,
|
|
setEditingExchange,
|
|
editingTrader,
|
|
setEditingTrader,
|
|
}: UseTraderActionsParams) {
|
|
// 检查模型是否正在被运行中的交易员使用(用于UI禁用)
|
|
const isModelInUse = (modelId: string) => {
|
|
return traders?.some((t) => t.ai_model === modelId && t.is_running) || false
|
|
}
|
|
|
|
// 检查交易所是否正在被运行中的交易员使用(用于UI禁用)
|
|
const isExchangeInUse = (exchangeId: string) => {
|
|
return (
|
|
traders?.some((t) => t.exchange_id === exchangeId && t.is_running) ||
|
|
false
|
|
)
|
|
}
|
|
|
|
// 检查模型是否被任何交易员使用(包括停止状态的)
|
|
const isModelUsedByAnyTrader = (modelId: string) => {
|
|
return traders?.some((t) => t.ai_model === modelId) || false
|
|
}
|
|
|
|
// 检查交易所是否被任何交易员使用(包括停止状态的)
|
|
const isExchangeUsedByAnyTrader = (exchangeId: string) => {
|
|
return traders?.some((t) => t.exchange_id === exchangeId) || false
|
|
}
|
|
|
|
// 获取使用特定模型的交易员列表
|
|
const getTradersUsingModel = (modelId: string) => {
|
|
return traders?.filter((t) => t.ai_model === modelId) || []
|
|
}
|
|
|
|
// 获取使用特定交易所的交易员列表
|
|
const getTradersUsingExchange = (exchangeId: string) => {
|
|
return traders?.filter((t) => t.exchange_id === exchangeId) || []
|
|
}
|
|
|
|
const handleCreateTrader = async (data: CreateTraderRequest) => {
|
|
try {
|
|
const model = allModels?.find((m) => m.id === data.ai_model_id)
|
|
const exchange = allExchanges?.find((e) => e.id === data.exchange_id)
|
|
|
|
if (!model?.enabled) {
|
|
toast.error(t('modelNotConfigured', language))
|
|
return
|
|
}
|
|
|
|
if (!exchange?.enabled) {
|
|
toast.error(t('exchangeNotConfigured', language))
|
|
return
|
|
}
|
|
|
|
await toast.promise(api.createTrader(data), {
|
|
loading: '正在创建…',
|
|
success: '创建成功',
|
|
error: '创建失败',
|
|
})
|
|
setShowCreateModal(false)
|
|
// Immediately refresh traders list for better UX
|
|
await mutateTraders()
|
|
} catch (error) {
|
|
console.error('Failed to create trader:', error)
|
|
toast.error(t('createTraderFailed', language))
|
|
}
|
|
}
|
|
|
|
const handleEditTrader = async (traderId: string) => {
|
|
try {
|
|
const traderConfig = await api.getTraderConfig(traderId)
|
|
setEditingTrader(traderConfig)
|
|
setShowEditModal(true)
|
|
} catch (error) {
|
|
console.error('Failed to fetch trader config:', error)
|
|
toast.error(t('getTraderConfigFailed', language))
|
|
}
|
|
}
|
|
|
|
const handleSaveEditTrader = async (data: CreateTraderRequest) => {
|
|
if (!editingTrader || !editingTrader.trader_id) return
|
|
|
|
try {
|
|
const enabledModels = allModels?.filter((m) => m.enabled) || []
|
|
const enabledExchanges =
|
|
allExchanges?.filter((e) => {
|
|
if (!e.enabled) return false
|
|
|
|
// Aster 交易所需要特殊字段
|
|
if (e.id === 'aster') {
|
|
return (
|
|
e.asterUser &&
|
|
e.asterUser.trim() !== '' &&
|
|
e.asterSigner &&
|
|
e.asterSigner.trim() !== ''
|
|
)
|
|
}
|
|
|
|
// Hyperliquid 需要钱包地址
|
|
if (e.id === 'hyperliquid') {
|
|
return (
|
|
e.hyperliquidWalletAddr && e.hyperliquidWalletAddr.trim() !== ''
|
|
)
|
|
}
|
|
|
|
return true
|
|
}) || []
|
|
|
|
const model = enabledModels?.find((m) => m.id === data.ai_model_id)
|
|
const exchange = enabledExchanges?.find((e) => e.id === data.exchange_id)
|
|
|
|
if (!model) {
|
|
toast.error(t('modelConfigNotExist', language))
|
|
return
|
|
}
|
|
|
|
if (!exchange) {
|
|
toast.error(t('exchangeConfigNotExist', language))
|
|
return
|
|
}
|
|
|
|
const request = {
|
|
name: data.name,
|
|
ai_model_id: data.ai_model_id,
|
|
exchange_id: data.exchange_id,
|
|
initial_balance: data.initial_balance,
|
|
scan_interval_minutes: data.scan_interval_minutes,
|
|
btc_eth_leverage: data.btc_eth_leverage,
|
|
altcoin_leverage: data.altcoin_leverage,
|
|
trading_symbols: data.trading_symbols,
|
|
custom_prompt: data.custom_prompt,
|
|
override_base_prompt: data.override_base_prompt,
|
|
system_prompt_template: data.system_prompt_template,
|
|
is_cross_margin: data.is_cross_margin,
|
|
use_coin_pool: data.use_coin_pool,
|
|
use_oi_top: data.use_oi_top,
|
|
}
|
|
|
|
await toast.promise(api.updateTrader(editingTrader.trader_id, request), {
|
|
loading: '正在保存…',
|
|
success: '保存成功',
|
|
error: '保存失败',
|
|
})
|
|
setShowEditModal(false)
|
|
setEditingTrader(null)
|
|
// Immediately refresh traders list for better UX
|
|
await mutateTraders()
|
|
} catch (error) {
|
|
console.error('Failed to update trader:', error)
|
|
toast.error(t('updateTraderFailed', language))
|
|
}
|
|
}
|
|
|
|
const handleDeleteTrader = async (traderId: string) => {
|
|
{
|
|
const ok = await confirmToast(t('confirmDeleteTrader', language))
|
|
if (!ok) return
|
|
}
|
|
|
|
try {
|
|
await toast.promise(api.deleteTrader(traderId), {
|
|
loading: '正在删除…',
|
|
success: '删除成功',
|
|
error: '删除失败',
|
|
})
|
|
|
|
// Immediately refresh traders list for better UX
|
|
await mutateTraders()
|
|
} catch (error) {
|
|
console.error('Failed to delete trader:', error)
|
|
toast.error(t('deleteTraderFailed', language))
|
|
}
|
|
}
|
|
|
|
const handleToggleTrader = async (traderId: string, running: boolean) => {
|
|
try {
|
|
if (running) {
|
|
await toast.promise(api.stopTrader(traderId), {
|
|
loading: '正在停止…',
|
|
success: '已停止',
|
|
error: '停止失败',
|
|
})
|
|
} else {
|
|
await toast.promise(api.startTrader(traderId), {
|
|
loading: '正在启动…',
|
|
success: '已启动',
|
|
error: '启动失败',
|
|
})
|
|
}
|
|
|
|
// Immediately refresh traders list to update running status
|
|
await mutateTraders()
|
|
} catch (error) {
|
|
console.error('Failed to toggle trader:', error)
|
|
toast.error(t('operationFailed', language))
|
|
}
|
|
}
|
|
|
|
const handleModelClick = (modelId: string) => {
|
|
if (!isModelInUse(modelId)) {
|
|
setEditingModel(modelId)
|
|
setShowModelModal(true)
|
|
}
|
|
}
|
|
|
|
const handleExchangeClick = (exchangeId: string) => {
|
|
if (!isExchangeInUse(exchangeId)) {
|
|
setEditingExchange(exchangeId)
|
|
setShowExchangeModal(true)
|
|
}
|
|
}
|
|
|
|
// 通用删除配置处理函数
|
|
const handleDeleteConfig = async <T extends { id: string }>(config: {
|
|
id: string
|
|
type: 'model' | 'exchange'
|
|
checkInUse: (id: string) => boolean
|
|
getUsingTraders: (id: string) => any[]
|
|
cannotDeleteKey: string
|
|
confirmDeleteKey: string
|
|
allItems: T[] | undefined
|
|
clearFields: (item: T) => T
|
|
buildRequest: (items: T[]) => any
|
|
updateApi: (request: any) => Promise<void>
|
|
refreshApi: () => Promise<T[]>
|
|
setItems: (items: T[]) => void
|
|
closeModal: () => void
|
|
errorKey: string
|
|
}) => {
|
|
// 检查是否有交易员正在使用
|
|
if (config.checkInUse(config.id)) {
|
|
const usingTraders = config.getUsingTraders(config.id)
|
|
const traderNames = usingTraders.map((t) => t.trader_name).join(', ')
|
|
toast.error(
|
|
`${t(config.cannotDeleteKey, language)} · ${t('tradersUsing', language)}: ${traderNames} · ${t('pleaseDeleteTradersFirst', language)}`
|
|
)
|
|
return
|
|
}
|
|
|
|
{
|
|
const ok = await confirmToast(t(config.confirmDeleteKey, language))
|
|
if (!ok) return
|
|
}
|
|
|
|
try {
|
|
const updatedItems =
|
|
config.allItems?.map((item) =>
|
|
item.id === config.id ? config.clearFields(item) : item
|
|
) || []
|
|
|
|
const request = config.buildRequest(updatedItems)
|
|
await toast.promise(config.updateApi(request), {
|
|
loading: '正在更新配置…',
|
|
success: '配置已更新',
|
|
error: '更新配置失败',
|
|
})
|
|
|
|
// 重新获取用户配置以确保数据同步
|
|
const refreshedItems = await config.refreshApi()
|
|
config.setItems(refreshedItems)
|
|
|
|
config.closeModal()
|
|
} catch (error) {
|
|
console.error(`Failed to delete ${config.type} config:`, error)
|
|
toast.error(t(config.errorKey, language))
|
|
}
|
|
}
|
|
|
|
const handleDeleteModel = async (modelId: string) => {
|
|
await handleDeleteConfig({
|
|
id: modelId,
|
|
type: 'model',
|
|
checkInUse: isModelUsedByAnyTrader,
|
|
getUsingTraders: getTradersUsingModel,
|
|
cannotDeleteKey: 'cannotDeleteModelInUse',
|
|
confirmDeleteKey: 'confirmDeleteModel',
|
|
allItems: allModels,
|
|
clearFields: (m) => ({
|
|
...m,
|
|
apiKey: '',
|
|
customApiUrl: '',
|
|
customModelName: '',
|
|
enabled: false,
|
|
}),
|
|
buildRequest: (models) => ({
|
|
models: Object.fromEntries(
|
|
models.map((model) => [
|
|
model.provider,
|
|
{
|
|
enabled: model.enabled,
|
|
api_key: model.apiKey || '',
|
|
custom_api_url: model.customApiUrl || '',
|
|
custom_model_name: model.customModelName || '',
|
|
},
|
|
])
|
|
),
|
|
}),
|
|
updateApi: api.updateModelConfigs,
|
|
refreshApi: api.getModelConfigs,
|
|
setItems: (items) => {
|
|
// 使用函数式更新确保状态正确更新
|
|
setAllModels([...items])
|
|
},
|
|
closeModal: () => {
|
|
setShowModelModal(false)
|
|
setEditingModel(null)
|
|
},
|
|
errorKey: 'deleteConfigFailed',
|
|
})
|
|
}
|
|
|
|
const handleSaveModel = async (
|
|
modelId: string,
|
|
apiKey: string,
|
|
customApiUrl?: string,
|
|
customModelName?: string
|
|
) => {
|
|
try {
|
|
// 创建或更新用户的模型配置
|
|
const existingModel = allModels?.find((m) => m.id === modelId)
|
|
let updatedModels
|
|
|
|
// 找到要配置的模型(优先从已配置列表,其次从支持列表)
|
|
const modelToUpdate =
|
|
existingModel || supportedModels?.find((m) => m.id === modelId)
|
|
if (!modelToUpdate) {
|
|
toast.error(t('modelNotExist', language))
|
|
return
|
|
}
|
|
|
|
if (existingModel) {
|
|
// 更新现有配置
|
|
updatedModels =
|
|
allModels?.map((m) =>
|
|
m.id === modelId
|
|
? {
|
|
...m,
|
|
apiKey,
|
|
customApiUrl: customApiUrl || '',
|
|
customModelName: customModelName || '',
|
|
enabled: true,
|
|
}
|
|
: m
|
|
) || []
|
|
} else {
|
|
// 添加新配置
|
|
const newModel = {
|
|
...modelToUpdate,
|
|
apiKey,
|
|
customApiUrl: customApiUrl || '',
|
|
customModelName: customModelName || '',
|
|
enabled: true,
|
|
}
|
|
updatedModels = [...(allModels || []), newModel]
|
|
}
|
|
|
|
const request = {
|
|
models: Object.fromEntries(
|
|
updatedModels.map((model) => [
|
|
model.provider, // 使用 provider 而不是 id
|
|
{
|
|
enabled: model.enabled,
|
|
api_key: model.apiKey || '',
|
|
custom_api_url: model.customApiUrl || '',
|
|
custom_model_name: model.customModelName || '',
|
|
},
|
|
])
|
|
),
|
|
}
|
|
|
|
await toast.promise(api.updateModelConfigs(request), {
|
|
loading: '正在更新模型配置…',
|
|
success: '模型配置已更新',
|
|
error: '更新模型配置失败',
|
|
})
|
|
|
|
// 重新获取用户配置以确保数据同步
|
|
const refreshedModels = await api.getModelConfigs()
|
|
setAllModels(refreshedModels)
|
|
|
|
setShowModelModal(false)
|
|
setEditingModel(null)
|
|
} catch (error) {
|
|
console.error('Failed to save model config:', error)
|
|
toast.error(t('saveConfigFailed', language))
|
|
}
|
|
}
|
|
|
|
const handleDeleteExchange = async (exchangeId: string) => {
|
|
await handleDeleteConfig({
|
|
id: exchangeId,
|
|
type: 'exchange',
|
|
checkInUse: isExchangeUsedByAnyTrader,
|
|
getUsingTraders: getTradersUsingExchange,
|
|
cannotDeleteKey: 'cannotDeleteExchangeInUse',
|
|
confirmDeleteKey: 'confirmDeleteExchange',
|
|
allItems: allExchanges,
|
|
clearFields: (e) => ({
|
|
...e,
|
|
apiKey: '',
|
|
secretKey: '',
|
|
hyperliquidWalletAddr: '',
|
|
asterUser: '',
|
|
asterSigner: '',
|
|
asterPrivateKey: '',
|
|
enabled: false,
|
|
}),
|
|
buildRequest: (exchanges) => ({
|
|
exchanges: Object.fromEntries(
|
|
exchanges.map((exchange) => [
|
|
exchange.id,
|
|
{
|
|
enabled: exchange.enabled,
|
|
api_key: exchange.apiKey || '',
|
|
secret_key: exchange.secretKey || '',
|
|
testnet: exchange.testnet || false,
|
|
hyperliquid_wallet_addr: exchange.hyperliquidWalletAddr || '',
|
|
aster_user: exchange.asterUser || '',
|
|
aster_signer: exchange.asterSigner || '',
|
|
aster_private_key: exchange.asterPrivateKey || '',
|
|
},
|
|
])
|
|
),
|
|
}),
|
|
updateApi: api.updateExchangeConfigsEncrypted,
|
|
refreshApi: api.getExchangeConfigs,
|
|
setItems: (items) => {
|
|
// 使用函数式更新确保状态正确更新
|
|
setAllExchanges([...items])
|
|
},
|
|
closeModal: () => {
|
|
setShowExchangeModal(false)
|
|
setEditingExchange(null)
|
|
},
|
|
errorKey: 'deleteExchangeConfigFailed',
|
|
})
|
|
}
|
|
|
|
const handleSaveExchange = async (
|
|
exchangeId: string,
|
|
apiKey: string,
|
|
secretKey?: string,
|
|
testnet?: boolean,
|
|
hyperliquidWalletAddr?: string,
|
|
asterUser?: string,
|
|
asterSigner?: string,
|
|
asterPrivateKey?: string,
|
|
lighterWalletAddr?: string,
|
|
lighterPrivateKey?: string,
|
|
lighterApiKeyPrivateKey?: string
|
|
) => {
|
|
try {
|
|
// 找到要配置的交易所(从supportedExchanges中)
|
|
const exchangeToUpdate = supportedExchanges?.find(
|
|
(e) => e.id === exchangeId
|
|
)
|
|
if (!exchangeToUpdate) {
|
|
toast.error(t('exchangeNotExist', language))
|
|
return
|
|
}
|
|
|
|
// 创建或更新用户的交易所配置
|
|
const existingExchange = allExchanges?.find((e) => e.id === exchangeId)
|
|
let updatedExchanges
|
|
|
|
if (existingExchange) {
|
|
// 更新现有配置
|
|
updatedExchanges =
|
|
allExchanges?.map((e) =>
|
|
e.id === exchangeId
|
|
? {
|
|
...e,
|
|
apiKey,
|
|
secretKey,
|
|
testnet,
|
|
hyperliquidWalletAddr,
|
|
asterUser,
|
|
asterSigner,
|
|
asterPrivateKey,
|
|
lighterWalletAddr,
|
|
lighterPrivateKey,
|
|
lighterApiKeyPrivateKey,
|
|
enabled: true,
|
|
}
|
|
: e
|
|
) || []
|
|
} else {
|
|
// 添加新配置
|
|
const newExchange = {
|
|
...exchangeToUpdate,
|
|
apiKey,
|
|
secretKey,
|
|
testnet,
|
|
hyperliquidWalletAddr,
|
|
asterUser,
|
|
asterSigner,
|
|
asterPrivateKey,
|
|
lighterWalletAddr,
|
|
lighterPrivateKey,
|
|
lighterApiKeyPrivateKey,
|
|
enabled: true,
|
|
}
|
|
updatedExchanges = [...(allExchanges || []), newExchange]
|
|
}
|
|
|
|
const request = {
|
|
exchanges: Object.fromEntries(
|
|
updatedExchanges.map((exchange) => [
|
|
exchange.id,
|
|
{
|
|
enabled: exchange.enabled,
|
|
api_key: exchange.apiKey || '',
|
|
secret_key: exchange.secretKey || '',
|
|
testnet: exchange.testnet || false,
|
|
hyperliquid_wallet_addr: exchange.hyperliquidWalletAddr || '',
|
|
aster_user: exchange.asterUser || '',
|
|
aster_signer: exchange.asterSigner || '',
|
|
aster_private_key: exchange.asterPrivateKey || '',
|
|
lighter_wallet_addr: exchange.lighterWalletAddr || '',
|
|
lighter_private_key: exchange.lighterPrivateKey || '',
|
|
lighter_api_key_private_key: exchange.lighterApiKeyPrivateKey || '',
|
|
},
|
|
])
|
|
),
|
|
}
|
|
|
|
await toast.promise(api.updateExchangeConfigsEncrypted(request), {
|
|
loading: '正在更新交易所配置…',
|
|
success: '交易所配置已更新',
|
|
error: '更新交易所配置失败',
|
|
})
|
|
|
|
// 重新获取用户配置以确保数据同步
|
|
const refreshedExchanges = await api.getExchangeConfigs()
|
|
setAllExchanges(refreshedExchanges)
|
|
|
|
setShowExchangeModal(false)
|
|
setEditingExchange(null)
|
|
} catch (error) {
|
|
console.error('Failed to save exchange config:', error)
|
|
toast.error(t('saveConfigFailed', language))
|
|
}
|
|
}
|
|
|
|
const handleAddModel = () => {
|
|
setEditingModel(null)
|
|
setShowModelModal(true)
|
|
}
|
|
|
|
const handleAddExchange = () => {
|
|
setEditingExchange(null)
|
|
setShowExchangeModal(true)
|
|
}
|
|
|
|
const handleSaveSignalSource = async (
|
|
coinPoolUrl: string,
|
|
oiTopUrl: string
|
|
) => {
|
|
try {
|
|
await toast.promise(api.saveUserSignalSource(coinPoolUrl, oiTopUrl), {
|
|
loading: '正在保存…',
|
|
success: '保存成功',
|
|
error: '保存失败',
|
|
})
|
|
setUserSignalSource({ coinPoolUrl, oiTopUrl })
|
|
setShowSignalSourceModal(false)
|
|
} catch (error) {
|
|
console.error('Failed to save signal source:', error)
|
|
toast.error(t('saveSignalSourceFailed', language))
|
|
}
|
|
}
|
|
|
|
return {
|
|
// 辅助函数
|
|
isModelInUse,
|
|
isExchangeInUse,
|
|
isModelUsedByAnyTrader,
|
|
isExchangeUsedByAnyTrader,
|
|
getTradersUsingModel,
|
|
getTradersUsingExchange,
|
|
|
|
// 事件处理函数
|
|
handleCreateTrader,
|
|
handleEditTrader,
|
|
handleSaveEditTrader,
|
|
handleDeleteTrader,
|
|
handleToggleTrader,
|
|
handleAddModel,
|
|
handleAddExchange,
|
|
handleModelClick,
|
|
handleExchangeClick,
|
|
handleSaveModel,
|
|
handleDeleteModel,
|
|
handleSaveExchange,
|
|
handleDeleteExchange,
|
|
handleSaveSignalSource,
|
|
}
|
|
}
|