feat(hook): Add hook module to help decouple some specific logic (#784)

This commit is contained in:
Shui
2025-11-08 20:02:30 -05:00
committed by GitHub
parent 52b0bfe1d2
commit 631fb62d21
12 changed files with 466 additions and 35 deletions

View File

@@ -13,6 +13,7 @@ import (
"math/big"
"net/http"
"net/url"
"nofx/hook"
"sort"
"strconv"
"strings"
@@ -56,6 +57,18 @@ func NewAsterTrader(user, signer, privateKeyHex string) (*AsterTrader, error) {
if err != nil {
return nil, fmt.Errorf("解析私钥失败: %w", err)
}
client := &http.Client{
Timeout: 30 * time.Second, // 增加到30秒
Transport: &http.Transport{
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
IdleConnTimeout: 90 * time.Second,
},
}
res := hook.HookExec[hook.NewAsterTraderResult](hook.NEW_ASTER_TRADER, user, client)
if res != nil && res.Error() == nil {
client = res.GetResult()
}
return &AsterTrader{
ctx: context.Background(),
@@ -63,15 +76,8 @@ func NewAsterTrader(user, signer, privateKeyHex string) (*AsterTrader, error) {
signer: signer,
privateKey: privKey,
symbolPrecision: make(map[string]SymbolPrecision),
client: &http.Client{
Timeout: 30 * time.Second, // 增加到30秒
Transport: &http.Transport{
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
IdleConnTimeout: 90 * time.Second,
},
},
baseURL: "https://fapi.asterdex.com",
client: client,
baseURL: "https://fapi.asterdex.com",
}, nil
}

View File

@@ -175,7 +175,7 @@ func NewAutoTrader(config AutoTraderConfig, database interface{}, userID string)
switch config.Exchange {
case "binance":
log.Printf("🏦 [%s] 使用币安合约交易", config.Name)
trader = NewFuturesTrader(config.BinanceAPIKey, config.BinanceSecretKey)
trader = NewFuturesTrader(config.BinanceAPIKey, config.BinanceSecretKey, userID)
case "hyperliquid":
log.Printf("🏦 [%s] 使用Hyperliquid交易", config.Name)
trader, err = NewHyperliquidTrader(config.HyperliquidPrivateKey, config.HyperliquidWalletAddr, config.HyperliquidTestnet)

View File

@@ -6,6 +6,7 @@ import (
"encoding/hex"
"fmt"
"log"
"nofx/hook"
"strconv"
"strings"
"sync"
@@ -61,8 +62,14 @@ type FuturesTrader struct {
}
// NewFuturesTrader 创建合约交易器
func NewFuturesTrader(apiKey, secretKey string) *FuturesTrader {
func NewFuturesTrader(apiKey, secretKey string, userId string) *FuturesTrader {
client := futures.NewClient(apiKey, secretKey)
hookRes := hook.HookExec[hook.NewBinanceTraderResult](hook.NEW_BINANCE_TRADER, userId, client)
if hookRes != nil && hookRes.GetResult() != nil {
client = hookRes.GetResult()
}
// 同步时间,避免 Timestamp ahead 错误
syncBinanceServerTime(client)
trader := &FuturesTrader{