mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-09 22:10:57 +08:00
feat(hook): Add hook module to help decouple some specific logic (#784)
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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{
|
||||
|
||||
Reference in New Issue
Block a user