1、暂存

This commit is contained in:
2025-10-14 19:58:59 +08:00
parent 556a32cb7c
commit 643eab3496
60 changed files with 5244 additions and 657 deletions

View File

@ -19,11 +19,13 @@ const (
PCRegisterMobile = "_PCRegisterMobile_%v" // 用户注册时手机key
PCResetPwdMobile = "_PCResetPwdMobile_%v" // 用户重置密码时手机key
SpotSymbolTicker = "_SpotSymbolTicker_" // 现货交易对行情
FutSymbolTicker = "_FutSymbolTicker_" // 合约交易对行情
PreOrderScriptList = "_ProOrderScriptList_" // 脚本执行list
PreSpotOrderList = "_PreSpotOrderList_:%s" // 待触发的现货订单集合{交易所类型 exchange_type}
PreFutOrderList = "_PreFutOrderList_:%s" // 待触发的订单集合 {交易所类型 exchange_type}
// SpotSymbolTicker = "_SpotSymbolTicker_" // 现货交易对行情
// FutSymbolTicker = "_FutSymbolTicker_" // 合约交易对行情
PreOrderScriptList = "_ProOrderScriptList_" // 脚本执行list
// 待触发的现货订单集合{交易所类型 exchange_type}
PreSpotOrderList = "_PreSpotOrderList_:%s"
// 待触发的订单集合 {交易所类型 exchange_type}
PreFutOrderList = "_PreFutOrderList_:%s"
//策略现货订单集合 {交易所类型 exchange_type}
StrategySpotOrderList = "strategy_spot_order_list:%s"

View File

@ -0,0 +1,107 @@
package factory
import (
"fmt"
"go-admin/common/global"
"go-admin/common/interfaces"
"strings"
)
// ExchangeFactory 交易所工厂
type ExchangeFactory struct {
clients map[string]interfaces.ExchangeInterface
}
// NewExchangeFactory 创建交易所工厂实例
func NewExchangeFactory() *ExchangeFactory {
return &ExchangeFactory{
clients: make(map[string]interfaces.ExchangeInterface),
}
}
// GetExchange 获取指定交易所的客户端
func (f *ExchangeFactory) GetExchange(exchangeType string) (interfaces.ExchangeInterface, error) {
exchangeType = strings.ToLower(strings.TrimSpace(exchangeType))
// 检查是否已经创建了该交易所的客户端
if client, exists := f.clients[exchangeType]; exists {
return client, nil
}
// 根据交易所类型创建对应的客户端
var client interfaces.ExchangeInterface
var err error
switch exchangeType {
case global.EXCHANGE_BINANCE:
client, err = f.createBinanceClient()
case global.EXCHANGE_BITGET:
client, err = f.createBitgetClient()
case global.EXCHANGE_OKEX:
return nil, fmt.Errorf("OKEx integration not implemented yet")
case global.EXCHANGE_GATE:
return nil, fmt.Errorf("Gate.io integration not implemented yet")
case global.EXCHANGE_COINBASE:
return nil, fmt.Errorf("Coinbase integration not implemented yet")
case global.EXCHANGE_BITFINEX:
return nil, fmt.Errorf("Bitfinex integration not implemented yet")
case global.EXCHANGE_BITMEX:
return nil, fmt.Errorf("BitMEX integration not implemented yet")
default:
return nil, fmt.Errorf("unsupported exchange type: %s", exchangeType)
}
if err != nil {
return nil, fmt.Errorf("failed to create %s client: %v", exchangeType, err)
}
// 缓存客户端实例
f.clients[exchangeType] = client
return client, nil
}
// createBinanceClient 创建Binance客户端包装器
func (f *ExchangeFactory) createBinanceClient() (interfaces.ExchangeInterface, error) {
// Binance适配器尚未完全实现暴时返回错误
return nil, fmt.Errorf("Binance adapter not fully implemented yet")
}
// createBitgetClient 创建Bitget客户端
func (f *ExchangeFactory) createBitgetClient() (interfaces.ExchangeInterface, error) {
// 使用默认空参数创建Bitget客户端公开接口不需要认证
return nil, fmt.Errorf("Bitget adapter not fully implemented yet")
}
// GetSupportedExchanges 获取支持的交易所列表
func (f *ExchangeFactory) GetSupportedExchanges() []string {
return []string{
// global.EXCHANGE_BINANCE, // Binance适配器尚未完全实现
global.EXCHANGE_BITGET,
// 其他交易所可以在未来添加
}
}
// RegisterExchange 注册自定义交易所客户端
func (f *ExchangeFactory) RegisterExchange(exchangeType string, client interfaces.ExchangeInterface) {
f.clients[strings.ToLower(exchangeType)] = client
}
// ClearCache 清理缓存的客户端
func (f *ExchangeFactory) ClearCache() {
for _, client := range f.clients {
if closer, ok := client.(interface{ Close() error }); ok {
closer.Close()
}
}
f.clients = make(map[string]interfaces.ExchangeInterface)
}
// 全局工厂实例
var DefaultExchangeFactory = NewExchangeFactory()
// GetExchangeClient 获取交易所客户端的便捷函数
func GetExchangeClient(exchangeType string) (interfaces.ExchangeInterface, error) {
return DefaultExchangeFactory.GetExchange(exchangeType)
}

View File

@ -3,6 +3,7 @@ package global
//交易所类型字典
const (
EXCHANGE_BINANCE = "binance"
EXCHANGE_BITGET = "bitget"
EXCHANGE_OKEX = "okex"
EXCHANGE_GATE = "gate"
EXCHANGE_COINBASE = "coinbase"

View File

@ -17,8 +17,8 @@ const (
K_SPOT = "k_spot"
//代币-配置
COIN_DETAIL = "c_spot_detail"
//代币-现货24h涨跌幅
COIN_PRICE_CHANGE = "c_spot_priceChange"
//代币-现货24h涨跌幅 {exchange}
COIN_PRICE_CHANGE = "c_spot_priceChange:%s"
//代币-现货热门排序
COIN_HOT_SORT = "c_spot_hot_sort"
//代币-现货新币排序

View File

@ -0,0 +1,255 @@
package helper
import (
"crypto"
"crypto/hmac"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"net/http"
"sort"
"strconv"
"strings"
"time"
)
// BitgetClient Bitget HTTP客户端
type BitgetClient struct {
APIKey string
APISecret string
Passphrase string
HTTPClient *http.Client
BaseURL string
SignType string
}
// NewBitgetClient 创建新的Bitget客户端
func NewBitgetClient(apiKey, apiSecret, passphrase string, proxyUrl string) (*BitgetClient, error) {
var proxyType, proxyAddr string
if proxyUrl != "" {
if strings.HasPrefix(proxyUrl, "http://") {
proxyType = "http"
proxyAddr = proxyUrl
} else if strings.HasPrefix(proxyUrl, "https://") {
proxyType = "https"
proxyAddr = proxyUrl
} else if strings.HasPrefix(proxyUrl, "socks5://") {
proxyType = "socks5"
proxyAddr = proxyUrl
} else {
// 默认为http代理
proxyType = "http"
proxyAddr = "http://" + proxyUrl
}
}
// 创建HTTP客户端
client := &http.Client{
Transport: &http.Transport{
MaxIdleConns: 1000,
IdleConnTimeout: 10 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
// 设置代理
if proxyAddr != "" {
err := CreateHtppProxy(proxyType, proxyAddr, client)
if err != nil {
return nil, err
}
}
return &BitgetClient{
APIKey: apiKey,
APISecret: apiSecret,
Passphrase: passphrase,
HTTPClient: client,
BaseURL: "https://api.bitget.com",
SignType: "SHA256",
}, nil
}
// DoGet 执行GET请求
func (bc *BitgetClient) DoGet(uri string, params map[string]string) ([]byte, error) {
timestamp := bc.getTimestamp()
body := bc.buildGetParams(params)
sign := bc.sign("GET", uri, body, timestamp)
requestURL := bc.BaseURL + uri + body
request, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
return nil, err
}
bc.setHeaders(request, timestamp, sign)
response, err := bc.HTTPClient.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
return ioutil.ReadAll(response.Body)
}
// DoPost 执行POST请求
func (bc *BitgetClient) DoPost(uri string, params interface{}) ([]byte, error) {
timestamp := bc.getTimestamp()
var body string
if params != nil {
bodyBytes, err := json.Marshal(params)
if err != nil {
return nil, err
}
body = string(bodyBytes)
}
sign := bc.sign("POST", uri, body, timestamp)
requestURL := bc.BaseURL + uri
request, err := http.NewRequest("POST", requestURL, strings.NewReader(body))
if err != nil {
return nil, err
}
bc.setHeaders(request, timestamp, sign)
response, err := bc.HTTPClient.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
return ioutil.ReadAll(response.Body)
}
// getTimestamp 获取时间戳
func (bc *BitgetClient) getTimestamp() string {
return strconv.FormatInt(time.Now().Unix()*1000, 10)
}
// setHeaders 设置请求头
func (bc *BitgetClient) setHeaders(request *http.Request, timestamp, sign string) {
request.Header.Add("Content-Type", "application/json")
request.Header.Add("ACCESS-KEY", bc.APIKey)
request.Header.Add("ACCESS-SIGN", sign)
request.Header.Add("ACCESS-TIMESTAMP", timestamp)
request.Header.Add("ACCESS-PASSPHRASE", bc.Passphrase)
}
// sign 生成签名
func (bc *BitgetClient) sign(method, requestPath, body, timestamp string) string {
var payload strings.Builder
payload.WriteString(timestamp)
payload.WriteString(method)
payload.WriteString(requestPath)
if body != "" && body != "?" {
payload.WriteString(body)
}
if bc.SignType == "RSA" {
return bc.signByRSA(payload.String())
}
return bc.signByHMAC(payload.String())
}
// signByHMAC HMAC签名
func (bc *BitgetClient) signByHMAC(payload string) string {
hash := hmac.New(sha256.New, []byte(bc.APISecret))
hash.Write([]byte(payload))
return base64.StdEncoding.EncodeToString(hash.Sum(nil))
}
// signByRSA RSA签名
func (bc *BitgetClient) signByRSA(payload string) string {
sign, _ := bc.rsaSign([]byte(payload), []byte(bc.APISecret), crypto.SHA256)
return base64.StdEncoding.EncodeToString(sign)
}
// rsaSign RSA签名实现
func (bc *BitgetClient) rsaSign(src []byte, priKey []byte, hash crypto.Hash) ([]byte, error) {
block, _ := pem.Decode(priKey)
if block == nil {
return nil, errors.New("key is invalid format")
}
var pkixPrivateKey interface{}
var err error
if block.Type == "RSA PRIVATE KEY" {
pkixPrivateKey, err = x509.ParsePKCS1PrivateKey(block.Bytes)
} else if block.Type == "PRIVATE KEY" {
pkixPrivateKey, err = x509.ParsePKCS8PrivateKey(block.Bytes)
}
if err != nil {
return nil, err
}
h := hash.New()
_, err = h.Write(src)
if err != nil {
return nil, err
}
bytes := h.Sum(nil)
sign, err := rsa.SignPKCS1v15(rand.Reader, pkixPrivateKey.(*rsa.PrivateKey), hash, bytes)
if err != nil {
return nil, err
}
return sign, nil
}
// buildGetParams 构建GET请求参数
func (bc *BitgetClient) buildGetParams(params map[string]string) string {
if len(params) == 0 {
return ""
}
return "?" + bc.sortParams(params)
}
// sortParams 参数排序
func (bc *BitgetClient) sortParams(params map[string]string) string {
keys := make([]string, 0, len(params))
for k := range params {
keys = append(keys, k)
}
sort.Strings(keys)
sorted := make([]string, 0, len(params))
for _, k := range keys {
sorted = append(sorted, k+"="+params[k])
}
return strings.Join(sorted, "&")
}
// SendSpotRequestAuth 发送现货认证请求
func (bc *BitgetClient) SendSpotRequestAuth(endpoint, method string, params map[string]interface{}) ([]byte, *http.Response, error) {
if method == "GET" {
paramStr := make(map[string]string)
for k, v := range params {
paramStr[k] = fmt.Sprintf("%v", v)
}
resp, err := bc.DoGet(endpoint, paramStr)
return resp, nil, err
} else {
resp, err := bc.DoPost(endpoint, params)
return resp, nil, err
}
}
// SendFuturesRequestAuth 发送合约认证请求
func (bc *BitgetClient) SendFuturesRequestAuth(endpoint, method string, params map[string]interface{}) ([]byte, *http.Response, error) {
return bc.SendSpotRequestAuth(endpoint, method, params)
}

View File

@ -0,0 +1,170 @@
package interfaces
import (
"go-admin/models"
"github.com/shopspring/decimal"
)
// ExchangeInterface 交易所通用接口抽象层
type ExchangeInterface interface {
// 基础信息接口
GetExchangeName() string
GetExchangeInfo() (ExchangeInfoResponse, error)
// 现货市场接口
SpotInterface
// 合约接口(如果支持)
FuturesInterface
// WebSocket接口
WebSocketInterface
}
// SpotInterface 现货交易接口
type SpotInterface interface {
// 市场数据
GetSpotTicker24h(symbol string) (models.Ticker24, error)
GetSpotTickers() ([]models.TradeSet, error)
// 订单管理
PlaceSpotOrder(params SpotOrderParams) (SpotOrderResponse, error)
CancelSpotOrder(symbol, orderID string) error
GetSpotOrder(symbol, orderID string) (SpotOrderResponse, error)
GetSpotOrders(symbol string) ([]SpotOrderResponse, error)
}
// FuturesInterface 合约交易接口
type FuturesInterface interface {
// 市场数据
GetFuturesTicker24h(symbol string) (models.Ticker24, error)
GetFuturesTickers() ([]models.TradeSet, error)
// 订单管理
PlaceFuturesOrder(params FuturesOrderParams) (FuturesOrderResponse, error)
CancelFuturesOrder(symbol, orderID string) error
GetFuturesOrder(symbol, orderID string) (FuturesOrderResponse, error)
}
// WebSocketInterface WebSocket接口
type WebSocketInterface interface {
SubscribeSpotTicker(symbols []string, callback func(models.Ticker24, string, string)) error
SubscribeFuturesTicker(symbols []string, callback func(models.Ticker24, string, string)) error
SubscribeSpotDepth(symbols []string, callback func(models.DepthBin, string, string)) error
SubscribeSpotTrades(symbols []string, callback func(models.NewDealPush, string, string)) error
SubscribeKline(symbols []string, interval string, callback func(models.Kline, int, string, string)) error
Close() error
}
// 通用数据结构定义
type ExchangeInfoResponse struct {
Symbols []SymbolInfo `json:"symbols"`
}
type SymbolInfo struct {
Symbol string `json:"symbol"`
BaseAsset string `json:"baseAsset"`
QuoteAsset string `json:"quoteAsset"`
Status string `json:"status"`
MinQty string `json:"minQty"`
MaxQty string `json:"maxQty"`
StepSize string `json:"stepSize"`
MinPrice string `json:"minPrice"`
MaxPrice string `json:"maxPrice"`
TickSize string `json:"tickSize"`
MinNotional string `json:"minNotional"`
}
// 现货订单参数
type SpotOrderParams struct {
Symbol string `json:"symbol"`
Side string `json:"side"` // BUY/SELL
Type string `json:"type"` // MARKET/LIMIT
Quantity decimal.Decimal `json:"quantity"`
Price decimal.Decimal `json:"price,omitempty"`
TimeInForce string `json:"timeInForce,omitempty"` // GTC/IOC/FOK
NewClientOrderId string `json:"newClientOrderId,omitempty"`
}
// 合约订单参数
type FuturesOrderParams struct {
Symbol string `json:"symbol"`
Side string `json:"side"` // BUY/SELL
Type string `json:"type"` // MARKET/LIMIT
Quantity decimal.Decimal `json:"quantity"`
Price decimal.Decimal `json:"price,omitempty"`
TimeInForce string `json:"timeInForce,omitempty"`
PositionSide string `json:"positionSide,omitempty"` // LONG/SHORT/BOTH
NewClientOrderId string `json:"newClientOrderId,omitempty"`
}
// 响应数据结构
type SpotAccountResponse struct {
Balances []SpotBalanceResponse `json:"balances"`
}
type SpotBalanceResponse struct {
Asset string `json:"asset"`
Free string `json:"free"`
Locked string `json:"locked"`
}
type SpotOrderResponse struct {
Symbol string `json:"symbol"`
OrderId int64 `json:"orderId"`
ClientOrderId string `json:"clientOrderId"`
Price string `json:"price"`
OrigQty string `json:"origQty"`
ExecutedQty string `json:"executedQty"`
Status string `json:"status"`
TimeInForce string `json:"timeInForce"`
Type string `json:"type"`
Side string `json:"side"`
Time int64 `json:"time"`
UpdateTime int64 `json:"updateTime"`
}
type FuturesAccountResponse struct {
TotalWalletBalance string `json:"totalWalletBalance"`
Assets []FuturesBalanceResponse `json:"assets"`
Positions []FuturesPositionResponse `json:"positions"`
}
type FuturesBalanceResponse struct {
Asset string `json:"asset"`
WalletBalance string `json:"walletBalance"`
UnrealizedProfit string `json:"unrealizedProfit"`
MarginBalance string `json:"marginBalance"`
MaintMargin string `json:"maintMargin"`
InitialMargin string `json:"initialMargin"`
PositionInitialMargin string `json:"positionInitialMargin"`
OpenOrderInitialMargin string `json:"openOrderInitialMargin"`
}
type FuturesPositionResponse struct {
Symbol string `json:"symbol"`
PositionAmt string `json:"positionAmt"`
EntryPrice string `json:"entryPrice"`
MarkPrice string `json:"markPrice"`
UnrealizedProfit string `json:"unrealizedProfit"`
LiquidationPrice string `json:"liquidationPrice"`
Leverage string `json:"leverage"`
PositionSide string `json:"positionSide"`
}
type FuturesOrderResponse struct {
Symbol string `json:"symbol"`
OrderId int64 `json:"orderId"`
ClientOrderId string `json:"clientOrderId"`
Price string `json:"price"`
OrigQty string `json:"origQty"`
ExecutedQty string `json:"executedQty"`
Status string `json:"status"`
TimeInForce string `json:"timeInForce"`
Type string `json:"type"`
Side string `json:"side"`
PositionSide string `json:"positionSide"`
ReduceOnly bool `json:"reduceOnly"`
Time int64 `json:"time"`
UpdateTime int64 `json:"updateTime"`
}