mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-18 09:54:35 +08:00
refactor: standardize code comments
This commit is contained in:
@@ -7,29 +7,29 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// AccountBalance 账户余额信息
|
||||
// AccountBalance Account balance information
|
||||
type AccountBalance struct {
|
||||
TotalEquity float64 `json:"total_equity"` // 总权益
|
||||
AvailableBalance float64 `json:"available_balance"` // 可用余额
|
||||
MarginUsed float64 `json:"margin_used"` // 已用保证金
|
||||
UnrealizedPnL float64 `json:"unrealized_pnl"` // 未实现盈亏
|
||||
MaintenanceMargin float64 `json:"maintenance_margin"` // 维持保证金
|
||||
TotalEquity float64 `json:"total_equity"` // Total equity
|
||||
AvailableBalance float64 `json:"available_balance"` // Available balance
|
||||
MarginUsed float64 `json:"margin_used"` // Used margin
|
||||
UnrealizedPnL float64 `json:"unrealized_pnl"` // Unrealized PnL
|
||||
MaintenanceMargin float64 `json:"maintenance_margin"` // Maintenance margin
|
||||
}
|
||||
|
||||
// Position 持仓信息
|
||||
// Position Position information
|
||||
type Position struct {
|
||||
Symbol string `json:"symbol"` // 交易对
|
||||
Side string `json:"side"` // "long" 或 "short"
|
||||
Size float64 `json:"size"` // 持仓大小
|
||||
EntryPrice float64 `json:"entry_price"` // 开仓均价
|
||||
MarkPrice float64 `json:"mark_price"` // 标记价格
|
||||
LiquidationPrice float64 `json:"liquidation_price"` // 强平价格
|
||||
UnrealizedPnL float64 `json:"unrealized_pnl"` // 未实现盈亏
|
||||
Leverage float64 `json:"leverage"` // 杠杆倍数
|
||||
MarginUsed float64 `json:"margin_used"` // 已用保证金
|
||||
Symbol string `json:"symbol"` // Trading pair
|
||||
Side string `json:"side"` // "long" or "short"
|
||||
Size float64 `json:"size"` // Position size
|
||||
EntryPrice float64 `json:"entry_price"` // Average entry price
|
||||
MarkPrice float64 `json:"mark_price"` // Mark price
|
||||
LiquidationPrice float64 `json:"liquidation_price"` // Liquidation price
|
||||
UnrealizedPnL float64 `json:"unrealized_pnl"` // Unrealized PnL
|
||||
Leverage float64 `json:"leverage"` // Leverage multiplier
|
||||
MarginUsed float64 `json:"margin_used"` // Used margin
|
||||
}
|
||||
|
||||
// GetBalance 获取账户余额(实现 Trader 接口)
|
||||
// GetBalance Get account balance (implements Trader interface)
|
||||
func (t *LighterTrader) GetBalance() (map[string]interface{}, error) {
|
||||
balance, err := t.GetAccountBalance()
|
||||
if err != nil {
|
||||
@@ -45,10 +45,10 @@ func (t *LighterTrader) GetBalance() (map[string]interface{}, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetAccountBalance 获取账户详细余额信息
|
||||
// GetAccountBalance Get detailed account balance information
|
||||
func (t *LighterTrader) GetAccountBalance() (*AccountBalance, error) {
|
||||
if err := t.ensureAuthToken(); err != nil {
|
||||
return nil, fmt.Errorf("认证令牌无效: %w", err)
|
||||
return nil, fmt.Errorf("invalid auth token: %w", err)
|
||||
}
|
||||
|
||||
t.accountMutex.RLock()
|
||||
@@ -62,7 +62,7 @@ func (t *LighterTrader) GetAccountBalance() (*AccountBalance, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 添加认证头
|
||||
// Add auth header
|
||||
t.accountMutex.RLock()
|
||||
req.Header.Set("Authorization", t.authToken)
|
||||
t.accountMutex.RUnlock()
|
||||
@@ -79,21 +79,21 @@ func (t *LighterTrader) GetAccountBalance() (*AccountBalance, error) {
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("获取余额失败 (status %d): %s", resp.StatusCode, string(body))
|
||||
return nil, fmt.Errorf("failed to get balance (status %d): %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
var balance AccountBalance
|
||||
if err := json.Unmarshal(body, &balance); err != nil {
|
||||
return nil, fmt.Errorf("解析余额响应失败: %w", err)
|
||||
return nil, fmt.Errorf("failed to parse balance response: %w", err)
|
||||
}
|
||||
|
||||
return &balance, nil
|
||||
}
|
||||
|
||||
// GetPositionsRaw 获取所有持仓(返回原始类型)
|
||||
// GetPositionsRaw Get all positions (returns raw type)
|
||||
func (t *LighterTrader) GetPositionsRaw(symbol string) ([]Position, error) {
|
||||
if err := t.ensureAuthToken(); err != nil {
|
||||
return nil, fmt.Errorf("认证令牌无效: %w", err)
|
||||
return nil, fmt.Errorf("invalid auth token: %w", err)
|
||||
}
|
||||
|
||||
t.accountMutex.RLock()
|
||||
@@ -110,7 +110,7 @@ func (t *LighterTrader) GetPositionsRaw(symbol string) ([]Position, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 添加认证头
|
||||
// Add auth header
|
||||
t.accountMutex.RLock()
|
||||
req.Header.Set("Authorization", t.authToken)
|
||||
t.accountMutex.RUnlock()
|
||||
@@ -127,18 +127,18 @@ func (t *LighterTrader) GetPositionsRaw(symbol string) ([]Position, error) {
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("获取持仓失败 (status %d): %s", resp.StatusCode, string(body))
|
||||
return nil, fmt.Errorf("failed to get positions (status %d): %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
var positions []Position
|
||||
if err := json.Unmarshal(body, &positions); err != nil {
|
||||
return nil, fmt.Errorf("解析持仓响应失败: %w", err)
|
||||
return nil, fmt.Errorf("failed to parse positions response: %w", err)
|
||||
}
|
||||
|
||||
return positions, nil
|
||||
}
|
||||
|
||||
// GetPositions 获取所有持仓(实现 Trader 接口)
|
||||
// GetPositions Get all positions (implements Trader interface)
|
||||
func (t *LighterTrader) GetPositions() ([]map[string]interface{}, error) {
|
||||
positions, err := t.GetPositionsRaw("")
|
||||
if err != nil {
|
||||
@@ -163,25 +163,25 @@ func (t *LighterTrader) GetPositions() ([]map[string]interface{}, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetPosition 获取指定币种的持仓
|
||||
// GetPosition Get position for specified symbol
|
||||
func (t *LighterTrader) GetPosition(symbol string) (*Position, error) {
|
||||
positions, err := t.GetPositionsRaw(symbol)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 找到指定币种的持仓
|
||||
// Find position for specified symbol
|
||||
for _, pos := range positions {
|
||||
if pos.Symbol == symbol && pos.Size > 0 {
|
||||
return &pos, nil
|
||||
}
|
||||
}
|
||||
|
||||
// 无持仓
|
||||
// No position
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// GetMarketPrice 获取市场价格
|
||||
// GetMarketPrice Get market price
|
||||
func (t *LighterTrader) GetMarketPrice(symbol string) (float64, error) {
|
||||
endpoint := fmt.Sprintf("%s/api/v1/market/ticker?symbol=%s", t.baseURL, symbol)
|
||||
|
||||
@@ -202,24 +202,24 @@ func (t *LighterTrader) GetMarketPrice(symbol string) (float64, error) {
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return 0, fmt.Errorf("获取市场价格失败 (status %d): %s", resp.StatusCode, string(body))
|
||||
return 0, fmt.Errorf("failed to get market price (status %d): %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
var ticker map[string]interface{}
|
||||
if err := json.Unmarshal(body, &ticker); err != nil {
|
||||
return 0, fmt.Errorf("解析价格响应失败: %w", err)
|
||||
return 0, fmt.Errorf("failed to parse price response: %w", err)
|
||||
}
|
||||
|
||||
// 提取最新价格
|
||||
// Extract latest price
|
||||
price, err := SafeFloat64(ticker, "last_price")
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("无法获取价格: %w", err)
|
||||
return 0, fmt.Errorf("failed to get price: %w", err)
|
||||
}
|
||||
|
||||
return price, nil
|
||||
}
|
||||
|
||||
// GetAccountInfo 获取账户完整信息(用于AutoTrader)
|
||||
// GetAccountInfo Get complete account information (for AutoTrader)
|
||||
func (t *LighterTrader) GetAccountInfo() (map[string]interface{}, error) {
|
||||
balance, err := t.GetAccountBalance()
|
||||
if err != nil {
|
||||
@@ -231,7 +231,7 @@ func (t *LighterTrader) GetAccountInfo() (map[string]interface{}, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 构建返回信息
|
||||
// Build return information
|
||||
info := map[string]interface{}{
|
||||
"total_equity": balance.TotalEquity,
|
||||
"available_balance": balance.AvailableBalance,
|
||||
@@ -245,27 +245,27 @@ func (t *LighterTrader) GetAccountInfo() (map[string]interface{}, error) {
|
||||
return info, nil
|
||||
}
|
||||
|
||||
// SetLeverage 设置杠杆倍数
|
||||
// SetLeverage Set leverage multiplier
|
||||
func (t *LighterTrader) SetLeverage(symbol string, leverage int) error {
|
||||
if err := t.ensureAuthToken(); err != nil {
|
||||
return fmt.Errorf("认证令牌无效: %w", err)
|
||||
return fmt.Errorf("invalid auth token: %w", err)
|
||||
}
|
||||
|
||||
// TODO: 实现设置杠杆的API调用
|
||||
// LIGHTER可能需要签名交易来设置杠杆
|
||||
// TODO: Implement set leverage API call
|
||||
// LIGHTER may require signed transaction to set leverage
|
||||
|
||||
return fmt.Errorf("SetLeverage未实现")
|
||||
return fmt.Errorf("SetLeverage not implemented")
|
||||
}
|
||||
|
||||
// GetMaxLeverage 获取最大杠杆倍数
|
||||
// GetMaxLeverage Get maximum leverage multiplier
|
||||
func (t *LighterTrader) GetMaxLeverage(symbol string) (int, error) {
|
||||
// LIGHTER支持BTC/ETH最高50x杠杆
|
||||
// TODO: 从API获取实际限制
|
||||
// LIGHTER supports up to 50x leverage for BTC/ETH
|
||||
// TODO: Get actual limits from API
|
||||
|
||||
if symbol == "BTC-PERP" || symbol == "ETH-PERP" {
|
||||
return 50, nil
|
||||
}
|
||||
|
||||
// 其他币种默认20x
|
||||
// Default 20x for other symbols
|
||||
return 20, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user