Files
exchange_go/common/factory/exchange_factory.go

108 lines
3.4 KiB
Go
Raw Normal View History

2025-10-14 19:58:59 +08:00
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)
}