all: refactor exchange factory to return the minimal implementation

This commit is contained in:
c9s 2023-05-17 13:43:00 +08:00
parent b544d51772
commit 6bed2a31f6
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
4 changed files with 47 additions and 12 deletions

View File

@ -228,12 +228,16 @@ func (environ *Environment) ConfigureExchangeSessions(userConfig *Config) error
func (environ *Environment) AddExchangesByViperKeys() error {
for _, n := range types.SupportedExchanges {
if viper.IsSet(string(n) + "-api-key") {
ex, err := exchange.NewWithEnvVarPrefix(n, "")
exMinimal, err := exchange.NewWithEnvVarPrefix(n, "")
if err != nil {
return err
}
environ.AddExchange(n.String(), ex)
if ex, ok := exMinimal.(types.Exchange); ok {
environ.AddExchange(n.String(), ex)
} else {
log.Errorf("exchange %T does not implement types.Exchange", exMinimal)
}
}
}

View File

@ -703,21 +703,39 @@ func (session *ExchangeSession) FindPossibleSymbols() (symbols []string, err err
return symbols, nil
}
// newBasicPrivateExchange allocates a basic exchange instance with the user private credentials
func (session *ExchangeSession) newBasicPrivateExchange(exchangeName types.ExchangeName) (types.Exchange, error) {
var err error
var exMinimal types.ExchangeMinimal
if session.Key != "" && session.Secret != "" {
exMinimal, err = exchange2.New(exchangeName, session.Key, session.Secret, session.Passphrase)
} else {
exMinimal, err = exchange2.NewWithEnvVarPrefix(exchangeName, session.EnvVarPrefix)
}
if err != nil {
return nil, err
}
if ex, ok := exMinimal.(types.Exchange); ok {
return ex, nil
}
return nil, fmt.Errorf("exchange %T does not implement types.Exchange", exMinimal)
}
// InitExchange initialize the exchange instance and allocate memory for fields
// In this stage, the session var could be loaded from the JSON config, so the pointer fields are still nil
// The Init method will be called after this stage, environment.Init will call the session.Init method later.
func (session *ExchangeSession) InitExchange(name string, ex types.Exchange) error {
var err error
var exchangeName = session.ExchangeName
if ex == nil {
if session.PublicOnly {
ex, err = exchange2.NewPublic(exchangeName)
} else {
if session.Key != "" && session.Secret != "" {
ex, err = exchange2.New(exchangeName, session.Key, session.Secret, session.Passphrase)
} else {
ex, err = exchange2.NewWithEnvVarPrefix(exchangeName, session.EnvVarPrefix)
}
ex, err = session.newBasicPrivateExchange(exchangeName)
}
}

View File

@ -6,6 +6,7 @@ import (
"strings"
"github.com/c9s/bbgo/pkg/exchange/binance"
"github.com/c9s/bbgo/pkg/exchange/bitget"
"github.com/c9s/bbgo/pkg/exchange/kucoin"
"github.com/c9s/bbgo/pkg/exchange/max"
"github.com/c9s/bbgo/pkg/exchange/okex"
@ -13,10 +14,19 @@ import (
)
func NewPublic(exchangeName types.ExchangeName) (types.Exchange, error) {
return New(exchangeName, "", "", "")
exMinimal, err := New(exchangeName, "", "", "")
if err != nil {
return nil, err
}
if ex, ok := exMinimal.(types.Exchange); ok {
return ex, nil
}
return nil, fmt.Errorf("exchange %T does not implement types.Exchange", exMinimal)
}
func New(n types.ExchangeName, key, secret, passphrase string) (types.Exchange, error) {
func New(n types.ExchangeName, key, secret, passphrase string) (types.ExchangeMinimal, error) {
switch n {
case types.ExchangeBinance:
@ -31,8 +41,8 @@ func New(n types.ExchangeName, key, secret, passphrase string) (types.Exchange,
case types.ExchangeKucoin:
return kucoin.New(key, secret, passphrase), nil
// case types.ExchangeBitget:
// return bitget.New(key, secret, passphrase), nil
case types.ExchangeBitget:
return bitget.New(key, secret, passphrase), nil
default:
return nil, fmt.Errorf("unsupported exchange: %v", n)
@ -42,7 +52,7 @@ func New(n types.ExchangeName, key, secret, passphrase string) (types.Exchange,
// NewWithEnvVarPrefix allocate and initialize the exchange instance with the given environment variable prefix
// When the varPrefix is a empty string, the default exchange name will be used as the prefix
func NewWithEnvVarPrefix(n types.ExchangeName, varPrefix string) (types.Exchange, error) {
func NewWithEnvVarPrefix(n types.ExchangeName, varPrefix string) (types.ExchangeMinimal, error) {
if len(varPrefix) == 0 {
varPrefix = n.String()
}

View File

@ -81,6 +81,9 @@ type Exchange interface {
ExchangeTradeService
}
// ExchangeBasic is the new type for replacing the original Exchange interface
type ExchangeBasic = Exchange
// ExchangeOrderQueryService provides an interface for querying the order status via order ID or client order ID
//
//go:generate mockgen -destination=mocks/mock_exchange_order_query.go -package=mocks . ExchangeOrderQueryService