From a9a1cca576a197fd89f2c3d3a7e81151148a7ab9 Mon Sep 17 00:00:00 2001 From: shazbert Date: Wed, 7 Feb 2024 13:46:16 +1100 Subject: [PATCH 1/4] Shift wrapper function GetDefaultConfig to exchange.Base method definition, to ensure set defaults doesn't get called twice and to reduce code --- backtester/engine/setup.go | 10 +++--- cmd/config_builder/builder.go | 2 +- cmd/exchange_template/wrapper_file.tmpl | 19 ----------- engine/engine_test.go | 2 +- engine/event_manager_test.go | 3 +- engine/helpers.go | 2 +- engine/order_manager_test.go | 3 +- engine/withdraw_manager_test.go | 3 +- exchanges/alphapoint/alphapoint_wrapper.go | 5 --- exchanges/binance/binance_wrapper.go | 23 ------------- exchanges/binanceus/binanceus_wrapper.go | 22 ------------ exchanges/bitfinex/bitfinex_wrapper.go | 23 ------------- exchanges/bitflyer/bitflyer_wrapper.go | 23 ------------- exchanges/bithumb/bithumb_wrapper.go | 23 ------------- exchanges/bitmex/bitmex_wrapper.go | 23 ------------- exchanges/bitstamp/bitstamp_wrapper.go | 23 ------------- exchanges/btcmarkets/btcmarkets_wrapper.go | 23 ------------- exchanges/btse/btse_wrapper.go | 23 ------------- exchanges/bybit/bybit_wrapper.go | 21 ------------ exchanges/coinbasepro/coinbasepro_wrapper.go | 23 ------------- exchanges/coinut/coinut_wrapper.go | 23 ------------- exchanges/exchange.go | 35 ++++++++++++++++++++ exchanges/exmo/exmo_wrapper.go | 23 ------------- exchanges/gateio/gateio_wrapper.go | 22 ------------ exchanges/gemini/gemini_wrapper.go | 23 ------------- exchanges/hitbtc/hitbtc_wrapper.go | 23 ------------- exchanges/huobi/huobi_wrapper.go | 23 ------------- exchanges/interfaces.go | 3 +- exchanges/itbit/itbit_wrapper.go | 23 ------------- exchanges/kraken/kraken_wrapper.go | 23 ------------- exchanges/kucoin/kucoin_wrapper.go | 22 ------------ exchanges/lbank/lbank_wrapper.go | 23 ------------- exchanges/okcoin/okcoin_wrapper.go | 21 ------------ exchanges/okx/okx_wrapper.go | 22 ------------ exchanges/poloniex/poloniex_wrapper.go | 23 ------------- exchanges/sharedtestvalues/customex.go | 2 +- exchanges/yobit/yobit_wrapper.go | 23 ------------- gctscript/wrappers/gct/gctwrapper_test.go | 3 +- 38 files changed, 51 insertions(+), 608 deletions(-) diff --git a/backtester/engine/setup.go b/backtester/engine/setup.go index d0a3c3d0337..53ff097f1aa 100644 --- a/backtester/engine/setup.go +++ b/backtester/engine/setup.go @@ -145,14 +145,16 @@ func (bt *BackTest) SetupFromConfig(cfg *config.Config, templatePath, output str if err != nil { return err } - exch.SetDefaults() - exchBase := exch.GetBase() - exchBase.Verbose = cfg.DataSettings.VerboseExchangeRequests + var dc *gctconfig.Exchange - dc, err = exch.GetDefaultConfig(context.TODO()) + dc, err = exch.GetDefaultConfig(context.TODO(), exch) if err != nil { return err } + + exchBase := exch.GetBase() + exchBase.Verbose = cfg.DataSettings.VerboseExchangeRequests + err = exch.Setup(dc) if err != nil { return err diff --git a/cmd/config_builder/builder.go b/cmd/config_builder/builder.go index 2e764be09d3..4455494fdfa 100644 --- a/cmd/config_builder/builder.go +++ b/cmd/config_builder/builder.go @@ -36,7 +36,7 @@ func main() { cfgs := make([]config.Exchange, 0, len(exchanges)) for x := range exchanges { var cfg *config.Exchange - cfg, err = exchanges[x].GetDefaultConfig(context.Background()) + cfg, err = exchanges[x].GetDefaultConfig(context.Background(), exchanges[x]) if err != nil { log.Printf("Failed to get exchanges default config. Err: %s", err) continue diff --git a/cmd/exchange_template/wrapper_file.tmpl b/cmd/exchange_template/wrapper_file.tmpl index d57f96b5451..20dbff5ba03 100644 --- a/cmd/exchange_template/wrapper_file.tmpl +++ b/cmd/exchange_template/wrapper_file.tmpl @@ -26,25 +26,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/portfolio/withdraw" ) -// GetDefaultConfig returns a default exchange config -func ({{.Variable}} *{{.CapitalName}}) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) { - {{.Variable}}.SetDefaults() - exchCfg := new(config.Exchange) - exchCfg.Name = {{.Variable}}.Name - exchCfg.HTTPTimeout = exchange.DefaultHTTPTimeout - exchCfg.BaseCurrencies = {{.Variable}}.BaseCurrencies - - {{.Variable}}.SetupDefaults(exchCfg) - - if {{.Variable}}.Features.Supports.RESTCapabilities.AutoPairUpdates { - err := {{.Variable}}.UpdateTradablePairs(ctx, true) - if err != nil { - return nil, err - } - } - return exchCfg, nil -} - // SetDefaults sets the basic defaults for {{.CapitalName}} func ({{.Variable}} *{{.CapitalName}}) SetDefaults() { {{.Variable}}.Name = "{{.CapitalName}}" diff --git a/engine/engine_test.go b/engine/engine_test.go index 12715631721..1871f100b81 100644 --- a/engine/engine_test.go +++ b/engine/engine_test.go @@ -379,7 +379,7 @@ func TestGetDefaultConfigurations(t *testing.T) { t.Skipf("skipping %s unsupported", name) } - defaultCfg, err := exch.GetDefaultConfig(context.Background()) + defaultCfg, err := exch.GetDefaultConfig(context.Background(), exch) if err != nil { t.Fatal(err) } diff --git a/engine/event_manager_test.go b/engine/event_manager_test.go index 1e57beb6d5e..17a8feb04e9 100644 --- a/engine/event_manager_test.go +++ b/engine/event_manager_test.go @@ -281,8 +281,7 @@ func TestCheckEventCondition(t *testing.T) { if err != nil { t.Fatal(err) } - exch.SetDefaults() - conf, err := exch.GetDefaultConfig(context.Background()) + conf, err := exch.GetDefaultConfig(context.Background(), exch) if err != nil { t.Error(err) } diff --git a/engine/helpers.go b/engine/helpers.go index 1700b8cba65..64fef144b05 100644 --- a/engine/helpers.go +++ b/engine/helpers.go @@ -1057,7 +1057,7 @@ func NewExchangeByNameWithDefaults(ctx context.Context, name string) (exchange.I if err != nil { return nil, err } - defaultConfig, err := exch.GetDefaultConfig(ctx) + defaultConfig, err := exch.GetDefaultConfig(ctx, exch) if err != nil { return nil, err } diff --git a/engine/order_manager_test.go b/engine/order_manager_test.go index cbdc7659f32..899d6e14769 100644 --- a/engine/order_manager_test.go +++ b/engine/order_manager_test.go @@ -267,9 +267,8 @@ func OrdersSetup(t *testing.T) *OrderManager { if err != nil { t.Fatal(err) } - exch.SetDefaults() - cfg, err := exch.GetDefaultConfig(context.Background()) + cfg, err := exch.GetDefaultConfig(context.Background(), exch) if err != nil { t.Fatal(err) } diff --git a/engine/withdraw_manager_test.go b/engine/withdraw_manager_test.go index 836781e8b08..84562eb8fb7 100644 --- a/engine/withdraw_manager_test.go +++ b/engine/withdraw_manager_test.go @@ -24,8 +24,7 @@ func withdrawManagerTestHelper(t *testing.T) (*ExchangeManager, *portfolioManage t.Helper() em := NewExchangeManager() b := new(okx.Okx) - b.SetDefaults() - cfg, err := b.GetDefaultConfig(context.Background()) + cfg, err := b.GetDefaultConfig(context.Background(), b) if err != nil { t.Fatal(err) } diff --git a/exchanges/alphapoint/alphapoint_wrapper.go b/exchanges/alphapoint/alphapoint_wrapper.go index 36c212de598..3c9c5d654e8 100644 --- a/exchanges/alphapoint/alphapoint_wrapper.go +++ b/exchanges/alphapoint/alphapoint_wrapper.go @@ -26,11 +26,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/portfolio/withdraw" ) -// GetDefaultConfig returns a default exchange config for Alphapoint -func (a *Alphapoint) GetDefaultConfig(_ context.Context) (*config.Exchange, error) { - return nil, common.ErrFunctionNotSupported -} - // Bootstrap loads the exchange and performs initialisation tasks func (a *Alphapoint) Bootstrap(_ context.Context) (continueBootstrap bool, err error) { return false, common.ErrNotYetImplemented diff --git a/exchanges/binance/binance_wrapper.go b/exchanges/binance/binance_wrapper.go index d981fa4b2f3..96cca609ddd 100644 --- a/exchanges/binance/binance_wrapper.go +++ b/exchanges/binance/binance_wrapper.go @@ -36,29 +36,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/portfolio/withdraw" ) -// GetDefaultConfig returns a default exchange config -func (b *Binance) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) { - b.SetDefaults() - exchCfg, err := b.GetStandardConfig() - if err != nil { - return nil, err - } - - err = b.SetupDefaults(exchCfg) - if err != nil { - return nil, err - } - - if b.Features.Supports.RESTCapabilities.AutoPairUpdates { - err = b.UpdateTradablePairs(ctx, true) - if err != nil { - return nil, err - } - } - - return exchCfg, nil -} - // SetDefaults sets the basic defaults for Binance func (b *Binance) SetDefaults() { b.Name = "Binance" diff --git a/exchanges/binanceus/binanceus_wrapper.go b/exchanges/binanceus/binanceus_wrapper.go index c11ad1ad4f4..4e48ce46147 100644 --- a/exchanges/binanceus/binanceus_wrapper.go +++ b/exchanges/binanceus/binanceus_wrapper.go @@ -30,28 +30,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/portfolio/withdraw" ) -// GetDefaultConfig returns a default exchange config -func (bi *Binanceus) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) { - bi.SetDefaults() - exchCfg, err := bi.GetStandardConfig() - if err != nil { - return nil, err - } - - err = bi.SetupDefaults(exchCfg) - if err != nil { - return nil, err - } - - if bi.Features.Supports.RESTCapabilities.AutoPairUpdates { - err := bi.UpdateTradablePairs(ctx, true) - if err != nil { - return nil, err - } - } - return exchCfg, nil -} - // SetDefaults sets the basic defaults for Binanceus func (bi *Binanceus) SetDefaults() { bi.Name = "Binanceus" diff --git a/exchanges/bitfinex/bitfinex_wrapper.go b/exchanges/bitfinex/bitfinex_wrapper.go index de10d725518..ef1b07582a9 100644 --- a/exchanges/bitfinex/bitfinex_wrapper.go +++ b/exchanges/bitfinex/bitfinex_wrapper.go @@ -33,29 +33,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/portfolio/withdraw" ) -// GetDefaultConfig returns a default exchange config -func (b *Bitfinex) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) { - b.SetDefaults() - exchCfg, err := b.GetStandardConfig() - if err != nil { - return nil, err - } - - err = b.SetupDefaults(exchCfg) - if err != nil { - return nil, err - } - - if b.Features.Supports.RESTCapabilities.AutoPairUpdates { - err = b.UpdateTradablePairs(ctx, true) - if err != nil { - return nil, err - } - } - - return exchCfg, nil -} - // SetDefaults sets the basic defaults for bitfinex func (b *Bitfinex) SetDefaults() { b.Name = "Bitfinex" diff --git a/exchanges/bitflyer/bitflyer_wrapper.go b/exchanges/bitflyer/bitflyer_wrapper.go index a08a89cf0c4..4c38e450b2f 100644 --- a/exchanges/bitflyer/bitflyer_wrapper.go +++ b/exchanges/bitflyer/bitflyer_wrapper.go @@ -28,29 +28,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/portfolio/withdraw" ) -// GetDefaultConfig returns a default exchange config -func (b *Bitflyer) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) { - b.SetDefaults() - exchCfg, err := b.GetStandardConfig() - if err != nil { - return nil, err - } - - err = b.SetupDefaults(exchCfg) - if err != nil { - return nil, err - } - - if b.Features.Supports.RESTCapabilities.AutoPairUpdates { - err = b.UpdateTradablePairs(ctx, true) - if err != nil { - return nil, err - } - } - - return exchCfg, nil -} - // SetDefaults sets the basic defaults for Bitflyer func (b *Bitflyer) SetDefaults() { b.Name = "Bitflyer" diff --git a/exchanges/bithumb/bithumb_wrapper.go b/exchanges/bithumb/bithumb_wrapper.go index 5dbc314674d..0da0fd8c5e3 100644 --- a/exchanges/bithumb/bithumb_wrapper.go +++ b/exchanges/bithumb/bithumb_wrapper.go @@ -36,29 +36,6 @@ const wsRateLimitMillisecond = 1000 var errNotEnoughPairs = errors.New("at least one currency is required to fetch order history") -// GetDefaultConfig returns a default exchange config -func (b *Bithumb) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) { - b.SetDefaults() - exchCfg, err := b.GetStandardConfig() - if err != nil { - return nil, err - } - - err = b.SetupDefaults(exchCfg) - if err != nil { - return nil, err - } - - if b.Features.Supports.RESTCapabilities.AutoPairUpdates { - err = b.UpdateTradablePairs(ctx, true) - if err != nil { - return nil, err - } - } - - return exchCfg, nil -} - // SetDefaults sets the basic defaults for Bithumb func (b *Bithumb) SetDefaults() { b.Name = "Bithumb" diff --git a/exchanges/bitmex/bitmex_wrapper.go b/exchanges/bitmex/bitmex_wrapper.go index 151f1ef28e9..c97362db23b 100644 --- a/exchanges/bitmex/bitmex_wrapper.go +++ b/exchanges/bitmex/bitmex_wrapper.go @@ -34,29 +34,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/portfolio/withdraw" ) -// GetDefaultConfig returns a default exchange config -func (b *Bitmex) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) { - b.SetDefaults() - exchCfg, err := b.GetStandardConfig() - if err != nil { - return nil, err - } - - err = b.SetupDefaults(exchCfg) - if err != nil { - return nil, err - } - - if b.Features.Supports.RESTCapabilities.AutoPairUpdates { - err = b.UpdateTradablePairs(ctx, true) - if err != nil { - return nil, err - } - } - - return exchCfg, nil -} - // SetDefaults sets the basic defaults for Bitmex func (b *Bitmex) SetDefaults() { b.Name = "Bitmex" diff --git a/exchanges/bitstamp/bitstamp_wrapper.go b/exchanges/bitstamp/bitstamp_wrapper.go index 888fae0b395..0f71fac1d1a 100644 --- a/exchanges/bitstamp/bitstamp_wrapper.go +++ b/exchanges/bitstamp/bitstamp_wrapper.go @@ -30,29 +30,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/portfolio/withdraw" ) -// GetDefaultConfig returns a default exchange config -func (b *Bitstamp) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) { - b.SetDefaults() - exchCfg, err := b.GetStandardConfig() - if err != nil { - return nil, err - } - - err = b.SetupDefaults(exchCfg) - if err != nil { - return nil, err - } - - if b.Features.Supports.RESTCapabilities.AutoPairUpdates { - err = b.UpdateTradablePairs(ctx, true) - if err != nil { - return nil, err - } - } - - return exchCfg, nil -} - // SetDefaults sets default for Bitstamp func (b *Bitstamp) SetDefaults() { b.Name = "Bitstamp" diff --git a/exchanges/btcmarkets/btcmarkets_wrapper.go b/exchanges/btcmarkets/btcmarkets_wrapper.go index 17ee7277c0a..6001a9e6771 100644 --- a/exchanges/btcmarkets/btcmarkets_wrapper.go +++ b/exchanges/btcmarkets/btcmarkets_wrapper.go @@ -34,29 +34,6 @@ import ( var errFailedToConvertToCandle = errors.New("cannot convert time series data to kline.Candle, insufficient data") -// GetDefaultConfig returns a default exchange config -func (b *BTCMarkets) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) { - b.SetDefaults() - exchCfg, err := b.GetStandardConfig() - if err != nil { - return nil, err - } - - err = b.SetupDefaults(exchCfg) - if err != nil { - return nil, err - } - - if b.Features.Supports.RESTCapabilities.AutoPairUpdates { - err = b.UpdateTradablePairs(ctx, true) - if err != nil { - return nil, err - } - } - - return exchCfg, nil -} - // SetDefaults sets basic defaults func (b *BTCMarkets) SetDefaults() { b.Name = "BTC Markets" diff --git a/exchanges/btse/btse_wrapper.go b/exchanges/btse/btse_wrapper.go index f7ec3c8f702..ac070327ad3 100644 --- a/exchanges/btse/btse_wrapper.go +++ b/exchanges/btse/btse_wrapper.go @@ -33,29 +33,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/portfolio/withdraw" ) -// GetDefaultConfig returns a default exchange config -func (b *BTSE) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) { - b.SetDefaults() - exchCfg, err := b.GetStandardConfig() - if err != nil { - return nil, err - } - - err = b.SetupDefaults(exchCfg) - if err != nil { - return nil, err - } - - if b.Features.Supports.RESTCapabilities.AutoPairUpdates { - err = b.UpdateTradablePairs(ctx, true) - if err != nil { - return nil, err - } - } - - return exchCfg, nil -} - // SetDefaults sets the basic defaults for BTSE func (b *BTSE) SetDefaults() { b.Name = "BTSE" diff --git a/exchanges/bybit/bybit_wrapper.go b/exchanges/bybit/bybit_wrapper.go index 28d4f15041d..e2e17c66ff1 100644 --- a/exchanges/bybit/bybit_wrapper.go +++ b/exchanges/bybit/bybit_wrapper.go @@ -33,27 +33,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/portfolio/withdraw" ) -// GetDefaultConfig returns a default exchange config -func (by *Bybit) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) { - by.SetDefaults() - exchCfg, err := by.GetStandardConfig() - if err != nil { - return nil, err - } - err = by.SetupDefaults(exchCfg) - if err != nil { - return nil, err - } - - if by.Features.Supports.RESTCapabilities.AutoPairUpdates { - err := by.UpdateTradablePairs(ctx, false) - if err != nil { - return nil, err - } - } - return exchCfg, nil -} - // SetDefaults sets the basic defaults for Bybit func (by *Bybit) SetDefaults() { by.Name = "Bybit" diff --git a/exchanges/coinbasepro/coinbasepro_wrapper.go b/exchanges/coinbasepro/coinbasepro_wrapper.go index 47a20d9ed95..32ec7538ab3 100644 --- a/exchanges/coinbasepro/coinbasepro_wrapper.go +++ b/exchanges/coinbasepro/coinbasepro_wrapper.go @@ -29,29 +29,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/portfolio/withdraw" ) -// GetDefaultConfig returns a default exchange config -func (c *CoinbasePro) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) { - c.SetDefaults() - exchCfg, err := c.GetStandardConfig() - if err != nil { - return nil, err - } - - err = c.SetupDefaults(exchCfg) - if err != nil { - return nil, err - } - - if c.Features.Supports.RESTCapabilities.AutoPairUpdates { - err = c.UpdateTradablePairs(ctx, true) - if err != nil { - return nil, err - } - } - - return exchCfg, nil -} - // SetDefaults sets default values for the exchange func (c *CoinbasePro) SetDefaults() { c.Name = "CoinbasePro" diff --git a/exchanges/coinut/coinut_wrapper.go b/exchanges/coinut/coinut_wrapper.go index 503c2909461..19e95cc30f0 100644 --- a/exchanges/coinut/coinut_wrapper.go +++ b/exchanges/coinut/coinut_wrapper.go @@ -31,29 +31,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/portfolio/withdraw" ) -// GetDefaultConfig returns a default exchange config -func (c *COINUT) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) { - c.SetDefaults() - exchCfg, err := c.GetStandardConfig() - if err != nil { - return nil, err - } - - err = c.SetupDefaults(exchCfg) - if err != nil { - return nil, err - } - - if c.Features.Supports.RESTCapabilities.AutoPairUpdates { - err = c.UpdateTradablePairs(ctx, true) - if err != nil { - return nil, err - } - } - - return exchCfg, nil -} - // SetDefaults sets current default values func (c *COINUT) SetDefaults() { c.Name = "COINUT" diff --git a/exchanges/exchange.go b/exchanges/exchange.go index 25931e59983..4741aa0520c 100644 --- a/exchanges/exchange.go +++ b/exchanges/exchange.go @@ -62,6 +62,7 @@ var ( errSetDefaultsNotCalled = errors.New("set defaults not called") errExchangeIsNil = errors.New("exchange is nil") errBatchSizeZero = errors.New("batch size cannot be 0") + errExchangeMismatch = errors.New("exchange instance does not match base") ) // SetRequester sets the instance of the requester @@ -1943,3 +1944,37 @@ func (b *Base) Bootstrap(_ context.Context) (continueBootstrap bool, err error) func (b *Base) IsVerbose() bool { return b.Verbose } + +// GetDefaultConfig returns a default exchange config +func (b *Base) GetDefaultConfig(ctx context.Context, instance IBotExchange) (*config.Exchange, error) { + if instance == nil { + return nil, errExchangeIsNil + } + + if instance.GetBase() != b { + return nil, errExchangeMismatch + } + + if instance.GetName() == "" { + instance.SetDefaults() + } + + exchCfg, err := b.GetStandardConfig() + if err != nil { + return nil, err + } + + err = b.SetupDefaults(exchCfg) + if err != nil { + return nil, err + } + + if b.Features.Supports.RESTCapabilities.AutoPairUpdates { + err = instance.UpdateTradablePairs(ctx, true) + if err != nil { + return nil, err + } + } + + return exchCfg, nil +} diff --git a/exchanges/exmo/exmo_wrapper.go b/exchanges/exmo/exmo_wrapper.go index 0137cbbe2e7..c047fe1f6d0 100644 --- a/exchanges/exmo/exmo_wrapper.go +++ b/exchanges/exmo/exmo_wrapper.go @@ -29,29 +29,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/portfolio/withdraw" ) -// GetDefaultConfig returns a default exchange config -func (e *EXMO) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) { - e.SetDefaults() - exchCfg, err := e.GetStandardConfig() - if err != nil { - return nil, err - } - - err = e.SetupDefaults(exchCfg) - if err != nil { - return nil, err - } - - if e.Features.Supports.RESTCapabilities.AutoPairUpdates { - err = e.UpdateTradablePairs(ctx, true) - if err != nil { - return nil, err - } - } - - return exchCfg, nil -} - // SetDefaults sets the basic defaults for exmo func (e *EXMO) SetDefaults() { e.Name = "EXMO" diff --git a/exchanges/gateio/gateio_wrapper.go b/exchanges/gateio/gateio_wrapper.go index 924da1dcbc4..00d35970667 100644 --- a/exchanges/gateio/gateio_wrapper.go +++ b/exchanges/gateio/gateio_wrapper.go @@ -34,28 +34,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/types" ) -// GetDefaultConfig returns a default exchange config -func (g *Gateio) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) { - g.SetDefaults() - exchCfg, err := g.GetStandardConfig() - if err != nil { - return nil, err - } - - err = g.SetupDefaults(exchCfg) - if err != nil { - return nil, err - } - - if g.Features.Supports.RESTCapabilities.AutoPairUpdates { - err = g.UpdateTradablePairs(ctx, true) - if err != nil { - return nil, err - } - } - return exchCfg, nil -} - // SetDefaults sets default values for the exchange func (g *Gateio) SetDefaults() { g.Name = "GateIO" diff --git a/exchanges/gemini/gemini_wrapper.go b/exchanges/gemini/gemini_wrapper.go index fee75d6b1a2..7a98dca2339 100644 --- a/exchanges/gemini/gemini_wrapper.go +++ b/exchanges/gemini/gemini_wrapper.go @@ -31,29 +31,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/portfolio/withdraw" ) -// GetDefaultConfig returns a default exchange config -func (g *Gemini) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) { - g.SetDefaults() - exchCfg, err := g.GetStandardConfig() - if err != nil { - return nil, err - } - - err = g.SetupDefaults(exchCfg) - if err != nil { - return nil, err - } - - if g.Features.Supports.RESTCapabilities.AutoPairUpdates { - err := g.UpdateTradablePairs(ctx, true) - if err != nil { - return nil, err - } - } - - return exchCfg, nil -} - // SetDefaults sets package defaults for gemini exchange func (g *Gemini) SetDefaults() { g.Name = "Gemini" diff --git a/exchanges/hitbtc/hitbtc_wrapper.go b/exchanges/hitbtc/hitbtc_wrapper.go index 3b65fe649b5..2e8f797e5fa 100644 --- a/exchanges/hitbtc/hitbtc_wrapper.go +++ b/exchanges/hitbtc/hitbtc_wrapper.go @@ -31,29 +31,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/portfolio/withdraw" ) -// GetDefaultConfig returns a default exchange config -func (h *HitBTC) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) { - h.SetDefaults() - exchCfg, err := h.GetStandardConfig() - if err != nil { - return nil, err - } - - err = h.SetupDefaults(exchCfg) - if err != nil { - return nil, err - } - - if h.Features.Supports.RESTCapabilities.AutoPairUpdates { - err = h.UpdateTradablePairs(ctx, true) - if err != nil { - return nil, err - } - } - - return exchCfg, nil -} - // SetDefaults sets default settings for hitbtc func (h *HitBTC) SetDefaults() { h.Name = "HitBTC" diff --git a/exchanges/huobi/huobi_wrapper.go b/exchanges/huobi/huobi_wrapper.go index 62cada36bd0..9aeead89168 100644 --- a/exchanges/huobi/huobi_wrapper.go +++ b/exchanges/huobi/huobi_wrapper.go @@ -32,29 +32,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/portfolio/withdraw" ) -// GetDefaultConfig returns a default exchange config -func (h *HUOBI) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) { - h.SetDefaults() - exchCfg, err := h.GetStandardConfig() - if err != nil { - return nil, err - } - - err = h.SetupDefaults(exchCfg) - if err != nil { - return nil, err - } - - if h.Features.Supports.RESTCapabilities.AutoPairUpdates { - err = h.UpdateTradablePairs(ctx, true) - if err != nil { - return nil, err - } - } - - return exchCfg, nil -} - // SetDefaults sets default values for the exchange func (h *HUOBI) SetDefaults() { h.Name = "Huobi" diff --git a/exchanges/interfaces.go b/exchanges/interfaces.go index b7cb5b5c3f6..fcc8f3afb9e 100644 --- a/exchanges/interfaces.go +++ b/exchanges/interfaces.go @@ -63,7 +63,8 @@ type IBotExchange interface { SetHTTPClientUserAgent(ua string) error GetHTTPClientUserAgent() (string, error) SetClientProxyAddress(addr string) error - GetDefaultConfig(ctx context.Context) (*config.Exchange, error) + // GetDefaultConfig returns a default exchange config + GetDefaultConfig(ctx context.Context, instance IBotExchange) (*config.Exchange, error) GetBase() *Base GetHistoricCandles(ctx context.Context, pair currency.Pair, a asset.Item, interval kline.Interval, start, end time.Time) (*kline.Item, error) GetHistoricCandlesExtended(ctx context.Context, pair currency.Pair, a asset.Item, interval kline.Interval, start, end time.Time) (*kline.Item, error) diff --git a/exchanges/itbit/itbit_wrapper.go b/exchanges/itbit/itbit_wrapper.go index a585353ab7e..35548658cb3 100644 --- a/exchanges/itbit/itbit_wrapper.go +++ b/exchanges/itbit/itbit_wrapper.go @@ -28,29 +28,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/portfolio/withdraw" ) -// GetDefaultConfig returns a default exchange config -func (i *ItBit) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) { - i.SetDefaults() - exchCfg, err := i.GetStandardConfig() - if err != nil { - return nil, err - } - - err = i.SetupDefaults(exchCfg) - if err != nil { - return nil, err - } - - if i.Features.Supports.RESTCapabilities.AutoPairUpdates { - err = i.UpdateTradablePairs(ctx, true) - if err != nil { - return nil, err - } - } - - return exchCfg, nil -} - // SetDefaults sets the defaults for the exchange func (i *ItBit) SetDefaults() { i.Name = "ITBIT" diff --git a/exchanges/kraken/kraken_wrapper.go b/exchanges/kraken/kraken_wrapper.go index f2314180542..494fa85570a 100644 --- a/exchanges/kraken/kraken_wrapper.go +++ b/exchanges/kraken/kraken_wrapper.go @@ -34,29 +34,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/portfolio/withdraw" ) -// GetDefaultConfig returns a default exchange config -func (k *Kraken) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) { - k.SetDefaults() - exchCfg, err := k.GetStandardConfig() - if err != nil { - return nil, err - } - - err = k.SetupDefaults(exchCfg) - if err != nil { - return nil, err - } - - if k.Features.Supports.RESTCapabilities.AutoPairUpdates { - err = k.UpdateTradablePairs(ctx, true) - if err != nil { - return nil, err - } - } - - return exchCfg, nil -} - // SetDefaults sets current default settings func (k *Kraken) SetDefaults() { k.Name = "Kraken" diff --git a/exchanges/kucoin/kucoin_wrapper.go b/exchanges/kucoin/kucoin_wrapper.go index e00abb75f92..608882306fb 100644 --- a/exchanges/kucoin/kucoin_wrapper.go +++ b/exchanges/kucoin/kucoin_wrapper.go @@ -35,28 +35,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/portfolio/withdraw" ) -// GetDefaultConfig returns a default exchange config -func (ku *Kucoin) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) { - ku.SetDefaults() - exchCfg, err := ku.GetStandardConfig() - if err != nil { - return nil, err - } - - err = ku.SetupDefaults(exchCfg) - if err != nil { - return nil, err - } - - if ku.Features.Supports.RESTCapabilities.AutoPairUpdates { - err := ku.UpdateTradablePairs(ctx, true) - if err != nil { - return nil, err - } - } - return exchCfg, nil -} - // SetDefaults sets the basic defaults for Kucoin func (ku *Kucoin) SetDefaults() { ku.Name = "Kucoin" diff --git a/exchanges/lbank/lbank_wrapper.go b/exchanges/lbank/lbank_wrapper.go index 0e09c47201e..9b22dab8e01 100644 --- a/exchanges/lbank/lbank_wrapper.go +++ b/exchanges/lbank/lbank_wrapper.go @@ -28,29 +28,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/portfolio/withdraw" ) -// GetDefaultConfig returns a default exchange config -func (l *Lbank) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) { - l.SetDefaults() - exchCfg, err := l.GetStandardConfig() - if err != nil { - return nil, err - } - - err = l.SetupDefaults(exchCfg) - if err != nil { - return nil, err - } - - if l.Features.Supports.RESTCapabilities.AutoPairUpdates { - err = l.UpdateTradablePairs(ctx, true) - if err != nil { - return nil, err - } - } - - return exchCfg, nil -} - // SetDefaults sets the basic defaults for Lbank func (l *Lbank) SetDefaults() { l.Name = "Lbank" diff --git a/exchanges/okcoin/okcoin_wrapper.go b/exchanges/okcoin/okcoin_wrapper.go index 8519cdc1b11..c1c5fc0537a 100644 --- a/exchanges/okcoin/okcoin_wrapper.go +++ b/exchanges/okcoin/okcoin_wrapper.go @@ -28,27 +28,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/portfolio/withdraw" ) -// GetDefaultConfig returns a default exchange config -func (o *Okcoin) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) { - o.SetDefaults() - exchCfg, err := o.GetStandardConfig() - if err != nil { - return nil, err - } - - err = o.SetupDefaults(exchCfg) - if err != nil { - return nil, err - } - if o.Features.Supports.RESTCapabilities.AutoPairUpdates { - err = o.UpdateTradablePairs(ctx, true) - if err != nil { - return nil, err - } - } - return exchCfg, nil -} - // SetDefaults method assigns the default values for Okcoin func (o *Okcoin) SetDefaults() { o.SetErrorDefaults() diff --git a/exchanges/okx/okx_wrapper.go b/exchanges/okx/okx_wrapper.go index dbc49b73a12..5b0efb003b1 100644 --- a/exchanges/okx/okx_wrapper.go +++ b/exchanges/okx/okx_wrapper.go @@ -40,28 +40,6 @@ const ( okxWebsocketResponseMaxLimit = time.Second * 3 ) -// GetDefaultConfig returns a default exchange config -func (ok *Okx) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) { - ok.SetDefaults() - exchCfg, err := ok.GetStandardConfig() - if err != nil { - return nil, err - } - - err = ok.SetupDefaults(exchCfg) - if err != nil { - return nil, err - } - - if ok.Features.Supports.RESTCapabilities.AutoPairUpdates { - err = ok.UpdateTradablePairs(ctx, true) - if err != nil { - return nil, err - } - } - return exchCfg, nil -} - // SetDefaults sets the basic defaults for Okx func (ok *Okx) SetDefaults() { ok.Name = "Okx" diff --git a/exchanges/poloniex/poloniex_wrapper.go b/exchanges/poloniex/poloniex_wrapper.go index 97eb9293d56..49c209bd9fc 100644 --- a/exchanges/poloniex/poloniex_wrapper.go +++ b/exchanges/poloniex/poloniex_wrapper.go @@ -31,29 +31,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/portfolio/withdraw" ) -// GetDefaultConfig returns a default exchange config -func (p *Poloniex) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) { - p.SetDefaults() - exchCfg, err := p.GetStandardConfig() - if err != nil { - return nil, err - } - - err = p.SetupDefaults(exchCfg) - if err != nil { - return nil, err - } - - if p.Features.Supports.RESTCapabilities.AutoPairUpdates { - err = p.UpdateTradablePairs(ctx, true) - if err != nil { - return nil, err - } - } - - return exchCfg, nil -} - // SetDefaults sets default settings for poloniex func (p *Poloniex) SetDefaults() { p.Name = "Poloniex" diff --git a/exchanges/sharedtestvalues/customex.go b/exchanges/sharedtestvalues/customex.go index 11c105e9ec5..3e20b61b582 100644 --- a/exchanges/sharedtestvalues/customex.go +++ b/exchanges/sharedtestvalues/customex.go @@ -267,7 +267,7 @@ func (c *CustomEx) GetSubscriptions() ([]subscription.Subscription, error) { } // GetDefaultConfig is a mock method for CustomEx -func (c *CustomEx) GetDefaultConfig(_ context.Context) (*config.Exchange, error) { +func (c *CustomEx) GetDefaultConfig(context.Context, exchange.IBotExchange) (*config.Exchange, error) { return nil, nil } diff --git a/exchanges/yobit/yobit_wrapper.go b/exchanges/yobit/yobit_wrapper.go index 553cb81f8fd..dcc5b9f4c84 100644 --- a/exchanges/yobit/yobit_wrapper.go +++ b/exchanges/yobit/yobit_wrapper.go @@ -29,29 +29,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/portfolio/withdraw" ) -// GetDefaultConfig returns a default exchange config -func (y *Yobit) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) { - y.SetDefaults() - exchCfg, err := y.GetStandardConfig() - if err != nil { - return nil, err - } - - err = y.SetupDefaults(exchCfg) - if err != nil { - return nil, err - } - - if y.Features.Supports.RESTCapabilities.AutoPairUpdates { - err = y.UpdateTradablePairs(ctx, true) - if err != nil { - return nil, err - } - } - - return exchCfg, nil -} - // SetDefaults sets current default value for Yobit func (y *Yobit) SetDefaults() { y.Name = "Yobit" diff --git a/gctscript/wrappers/gct/gctwrapper_test.go b/gctscript/wrappers/gct/gctwrapper_test.go index 93c643e708e..c1964e38209 100644 --- a/gctscript/wrappers/gct/gctwrapper_test.go +++ b/gctscript/wrappers/gct/gctwrapper_test.go @@ -39,8 +39,7 @@ func TestMain(m *testing.M) { log.Print(err) os.Exit(1) } - exch.SetDefaults() - cfg, err := exch.GetDefaultConfig(context.Background()) + cfg, err := exch.GetDefaultConfig(context.Background(), exch) if err != nil { log.Fatal(err) } From 3b4382a1ed17ac8b7dce1f7ce10fda0985bee9a9 Mon Sep 17 00:00:00 2001 From: shazbert Date: Wed, 7 Feb 2024 13:49:02 +1100 Subject: [PATCH 2/4] rm alphapoint bootstrap method as is defined as exchange.Base method --- exchanges/alphapoint/alphapoint_wrapper.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/exchanges/alphapoint/alphapoint_wrapper.go b/exchanges/alphapoint/alphapoint_wrapper.go index 3c9c5d654e8..7d96ec2e006 100644 --- a/exchanges/alphapoint/alphapoint_wrapper.go +++ b/exchanges/alphapoint/alphapoint_wrapper.go @@ -26,11 +26,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/portfolio/withdraw" ) -// Bootstrap loads the exchange and performs initialisation tasks -func (a *Alphapoint) Bootstrap(_ context.Context) (continueBootstrap bool, err error) { - return false, common.ErrNotYetImplemented -} - // SetDefaults sets current default settings func (a *Alphapoint) SetDefaults() { a.Name = "Alphapoint" From abdcc5da7d6afc0a33eeca88710e93633b64d836 Mon Sep 17 00:00:00 2001 From: shazbert Date: Wed, 7 Feb 2024 16:22:55 +1100 Subject: [PATCH 3/4] add tests --- exchanges/exchange.go | 2 +- exchanges/exchange_test.go | 38 ++++++++++++++++++++++++-- exchanges/interfaces.go | 10 ++++++- exchanges/sharedtestvalues/customex.go | 2 +- 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/exchanges/exchange.go b/exchanges/exchange.go index 4741aa0520c..bd2cc147740 100644 --- a/exchanges/exchange.go +++ b/exchanges/exchange.go @@ -1946,7 +1946,7 @@ func (b *Base) IsVerbose() bool { } // GetDefaultConfig returns a default exchange config -func (b *Base) GetDefaultConfig(ctx context.Context, instance IBotExchange) (*config.Exchange, error) { +func (b *Base) GetDefaultConfig(ctx context.Context, instance LimitedScope) (*config.Exchange, error) { if instance == nil { return nil, errExchangeIsNil } diff --git a/exchanges/exchange_test.go b/exchanges/exchange_test.go index f048cc344fb..45e1a48784b 100644 --- a/exchanges/exchange_test.go +++ b/exchanges/exchange_test.go @@ -8,6 +8,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/thrasher-corp/gocryptotrader/common" "github.com/thrasher-corp/gocryptotrader/common/convert" "github.com/thrasher-corp/gocryptotrader/common/key" @@ -3224,9 +3225,7 @@ func TestGetOpenInterest(t *testing.T) { } // FakeBase is used to override functions -type FakeBase struct { - Base -} +type FakeBase struct{ Base } func (f *FakeBase) GetOpenInterest(context.Context, ...key.PairAsset) ([]futures.OpenInterest, error) { return []futures.OpenInterest{ @@ -3242,6 +3241,13 @@ func (f *FakeBase) GetOpenInterest(context.Context, ...key.PairAsset) ([]futures }, nil } +func (f *FakeBase) SetDefaults() { + f.Name = "test" + f.Requester, _ = request.New("test", common.NewHTTPClientWithTimeout(time.Second)) + f.Features.Supports.RESTCapabilities.AutoPairUpdates = true +} +func (f *FakeBase) UpdateTradablePairs(context.Context, bool) error { return nil } + func TestGetCachedOpenInterest(t *testing.T) { t.Parallel() var b FakeBase @@ -3330,3 +3336,29 @@ func TestParallelChanOp(t *testing.T) { assert.EventuallyWithT(t, f, 500*time.Millisecond, 50*time.Millisecond, "ParallelChanOp should complete within 500ms not 5*300ms") assert.Len(t, run, len(c), "Every channel was run to completion") } + +func TestGetDefaultConfig(t *testing.T) { + t.Parallel() + + exch := &FakeBase{} + + _, err := exch.GetDefaultConfig(context.Background(), nil) + assert.ErrorIs(t, err, errExchangeIsNil) + + _, err = exch.GetDefaultConfig(context.Background(), &FakeBase{}) + assert.ErrorIs(t, err, errExchangeMismatch) + + c, err := exch.GetDefaultConfig(context.Background(), exch) + require.NoError(t, err) + + assert.Equal(t, "test", c.Name) + cpy := exch.Requester + + // Test below demonstrates that the requester is not overwritten so that + // SetDefaults is not called twice. + c, err = exch.GetDefaultConfig(context.Background(), exch) + require.NoError(t, err) + + assert.Equal(t, "test", c.Name) + assert.Equal(t, cpy, exch.Requester) +} diff --git a/exchanges/interfaces.go b/exchanges/interfaces.go index fcc8f3afb9e..45160609d65 100644 --- a/exchanges/interfaces.go +++ b/exchanges/interfaces.go @@ -64,7 +64,7 @@ type IBotExchange interface { GetHTTPClientUserAgent() (string, error) SetClientProxyAddress(addr string) error // GetDefaultConfig returns a default exchange config - GetDefaultConfig(ctx context.Context, instance IBotExchange) (*config.Exchange, error) + GetDefaultConfig(ctx context.Context, instance LimitedScope) (*config.Exchange, error) GetBase() *Base GetHistoricCandles(ctx context.Context, pair currency.Pair, a asset.Item, interval kline.Interval, start, end time.Time) (*kline.Item, error) GetHistoricCandlesExtended(ctx context.Context, pair currency.Pair, a asset.Item, interval kline.Interval, start, end time.Time) (*kline.Item, error) @@ -189,3 +189,11 @@ type MarginManagement interface { futures.PNLCalculation GetFuturesContractDetails(ctx context.Context, item asset.Item) ([]futures.Contract, error) } + +// LimitedScope defines a subset of the exchange interface +type LimitedScope interface { + GetBase() *Base + GetName() string + SetDefaults() + UpdateTradablePairs(context.Context, bool) error +} diff --git a/exchanges/sharedtestvalues/customex.go b/exchanges/sharedtestvalues/customex.go index 3e20b61b582..9f561b4f6bf 100644 --- a/exchanges/sharedtestvalues/customex.go +++ b/exchanges/sharedtestvalues/customex.go @@ -267,7 +267,7 @@ func (c *CustomEx) GetSubscriptions() ([]subscription.Subscription, error) { } // GetDefaultConfig is a mock method for CustomEx -func (c *CustomEx) GetDefaultConfig(context.Context, exchange.IBotExchange) (*config.Exchange, error) { +func (c *CustomEx) GetDefaultConfig(context.Context, exchange.LimitedScope) (*config.Exchange, error) { return nil, nil } From 9a51c0be1523b8f8cc0d0426e4b232d71dae6117 Mon Sep 17 00:00:00 2001 From: Ryan O'Hara-Reid Date: Thu, 14 Mar 2024 15:42:01 +1100 Subject: [PATCH 4/4] glorious: make it a function and make it IBOTEXCHANGE --- backtester/engine/live.go | 4 +- backtester/engine/setup.go | 2 +- cmd/config_builder/builder.go | 2 +- engine/engine_test.go | 2 +- engine/event_manager_test.go | 3 +- engine/helpers.go | 2 +- engine/order_manager_test.go | 2 +- engine/withdraw_manager_test.go | 2 +- exchanges/exchange.go | 15 +- exchanges/exchange_test.go | 195 ++++++++++++++++++---- exchanges/interfaces.go | 10 -- exchanges/sharedtestvalues/customex.go | 5 - gctscript/wrappers/gct/gctwrapper_test.go | 3 +- 13 files changed, 182 insertions(+), 65 deletions(-) diff --git a/backtester/engine/live.go b/backtester/engine/live.go index 9c6347ea963..fa124bc2fdf 100644 --- a/backtester/engine/live.go +++ b/backtester/engine/live.go @@ -398,9 +398,7 @@ func (d *dataChecker) FetchLatestData() (bool, error) { if !d.hasUpdatedFunding { err = d.UpdateFunding(false) if err != nil { - if err != nil { - log.Errorln(common.LiveStrategy, err) - } + log.Errorln(common.LiveStrategy, err) } } diff --git a/backtester/engine/setup.go b/backtester/engine/setup.go index d6e1d4c1a84..14a96286655 100644 --- a/backtester/engine/setup.go +++ b/backtester/engine/setup.go @@ -147,7 +147,7 @@ func (bt *BackTest) SetupFromConfig(cfg *config.Config, templatePath, output str } var dc *gctconfig.Exchange - dc, err = exch.GetDefaultConfig(context.TODO(), exch) + dc, err = gctexchange.GetDefaultConfig(context.TODO(), exch) if err != nil { return err } diff --git a/cmd/config_builder/builder.go b/cmd/config_builder/builder.go index 4455494fdfa..31792b0d0c1 100644 --- a/cmd/config_builder/builder.go +++ b/cmd/config_builder/builder.go @@ -36,7 +36,7 @@ func main() { cfgs := make([]config.Exchange, 0, len(exchanges)) for x := range exchanges { var cfg *config.Exchange - cfg, err = exchanges[x].GetDefaultConfig(context.Background(), exchanges[x]) + cfg, err = exchange.GetDefaultConfig(context.Background(), exchanges[x]) if err != nil { log.Printf("Failed to get exchanges default config. Err: %s", err) continue diff --git a/engine/engine_test.go b/engine/engine_test.go index 8a949f1fec4..61ce3a1ba20 100644 --- a/engine/engine_test.go +++ b/engine/engine_test.go @@ -378,7 +378,7 @@ func TestGetDefaultConfigurations(t *testing.T) { t.Skipf("skipping %s unsupported", name) } - defaultCfg, err := exch.GetDefaultConfig(context.Background(), exch) + defaultCfg, err := exchange.GetDefaultConfig(context.Background(), exch) if err != nil { t.Fatal(err) } diff --git a/engine/event_manager_test.go b/engine/event_manager_test.go index 17a8feb04e9..70ec4703dc8 100644 --- a/engine/event_manager_test.go +++ b/engine/event_manager_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/thrasher-corp/gocryptotrader/currency" + exchange "github.com/thrasher-corp/gocryptotrader/exchanges" "github.com/thrasher-corp/gocryptotrader/exchanges/asset" "github.com/thrasher-corp/gocryptotrader/exchanges/ticker" ) @@ -281,7 +282,7 @@ func TestCheckEventCondition(t *testing.T) { if err != nil { t.Fatal(err) } - conf, err := exch.GetDefaultConfig(context.Background(), exch) + conf, err := exchange.GetDefaultConfig(context.Background(), exch) if err != nil { t.Error(err) } diff --git a/engine/helpers.go b/engine/helpers.go index 98d2e68185b..4dd95a15ad6 100644 --- a/engine/helpers.go +++ b/engine/helpers.go @@ -1054,7 +1054,7 @@ func NewExchangeByNameWithDefaults(ctx context.Context, name string) (exchange.I if err != nil { return nil, err } - defaultConfig, err := exch.GetDefaultConfig(ctx, exch) + defaultConfig, err := exchange.GetDefaultConfig(ctx, exch) if err != nil { return nil, err } diff --git a/engine/order_manager_test.go b/engine/order_manager_test.go index 320dd0dbebb..96ef85ecbff 100644 --- a/engine/order_manager_test.go +++ b/engine/order_manager_test.go @@ -268,7 +268,7 @@ func OrdersSetup(t *testing.T) *OrderManager { t.Fatal(err) } - cfg, err := exch.GetDefaultConfig(context.Background(), exch) + cfg, err := exchange.GetDefaultConfig(context.Background(), exch) if err != nil { t.Fatal(err) } diff --git a/engine/withdraw_manager_test.go b/engine/withdraw_manager_test.go index 84562eb8fb7..17ab390790c 100644 --- a/engine/withdraw_manager_test.go +++ b/engine/withdraw_manager_test.go @@ -24,7 +24,7 @@ func withdrawManagerTestHelper(t *testing.T) (*ExchangeManager, *portfolioManage t.Helper() em := NewExchangeManager() b := new(okx.Okx) - cfg, err := b.GetDefaultConfig(context.Background(), b) + cfg, err := exchange.GetDefaultConfig(context.Background(), b) if err != nil { t.Fatal(err) } diff --git a/exchanges/exchange.go b/exchanges/exchange.go index bd2cc147740..0b5b37987b1 100644 --- a/exchanges/exchange.go +++ b/exchanges/exchange.go @@ -62,7 +62,6 @@ var ( errSetDefaultsNotCalled = errors.New("set defaults not called") errExchangeIsNil = errors.New("exchange is nil") errBatchSizeZero = errors.New("batch size cannot be 0") - errExchangeMismatch = errors.New("exchange instance does not match base") ) // SetRequester sets the instance of the requester @@ -1946,18 +1945,16 @@ func (b *Base) IsVerbose() bool { } // GetDefaultConfig returns a default exchange config -func (b *Base) GetDefaultConfig(ctx context.Context, instance LimitedScope) (*config.Exchange, error) { - if instance == nil { +func GetDefaultConfig(ctx context.Context, exch IBotExchange) (*config.Exchange, error) { + if exch == nil { return nil, errExchangeIsNil } - if instance.GetBase() != b { - return nil, errExchangeMismatch + if exch.GetName() == "" { + exch.SetDefaults() } - if instance.GetName() == "" { - instance.SetDefaults() - } + b := exch.GetBase() exchCfg, err := b.GetStandardConfig() if err != nil { @@ -1970,7 +1967,7 @@ func (b *Base) GetDefaultConfig(ctx context.Context, instance LimitedScope) (*co } if b.Features.Supports.RESTCapabilities.AutoPairUpdates { - err = instance.UpdateTradablePairs(ctx, true) + err = exch.UpdateTradablePairs(ctx, true) if err != nil { return nil, err } diff --git a/exchanges/exchange_test.go b/exchanges/exchange_test.go index b2ca1e74e6a..98710939289 100644 --- a/exchanges/exchange_test.go +++ b/exchanges/exchange_test.go @@ -14,18 +14,24 @@ import ( "github.com/thrasher-corp/gocryptotrader/common/key" "github.com/thrasher-corp/gocryptotrader/config" "github.com/thrasher-corp/gocryptotrader/currency" + "github.com/thrasher-corp/gocryptotrader/exchanges/account" "github.com/thrasher-corp/gocryptotrader/exchanges/asset" "github.com/thrasher-corp/gocryptotrader/exchanges/collateral" + "github.com/thrasher-corp/gocryptotrader/exchanges/deposit" + "github.com/thrasher-corp/gocryptotrader/exchanges/fundingrate" "github.com/thrasher-corp/gocryptotrader/exchanges/futures" "github.com/thrasher-corp/gocryptotrader/exchanges/kline" "github.com/thrasher-corp/gocryptotrader/exchanges/margin" "github.com/thrasher-corp/gocryptotrader/exchanges/order" + "github.com/thrasher-corp/gocryptotrader/exchanges/orderbook" "github.com/thrasher-corp/gocryptotrader/exchanges/protocol" "github.com/thrasher-corp/gocryptotrader/exchanges/request" "github.com/thrasher-corp/gocryptotrader/exchanges/stream" "github.com/thrasher-corp/gocryptotrader/exchanges/subscription" "github.com/thrasher-corp/gocryptotrader/exchanges/ticker" + "github.com/thrasher-corp/gocryptotrader/exchanges/trade" "github.com/thrasher-corp/gocryptotrader/portfolio/banking" + "github.com/thrasher-corp/gocryptotrader/portfolio/withdraw" ) const ( @@ -3224,30 +3230,6 @@ func TestGetOpenInterest(t *testing.T) { } } -// FakeBase is used to override functions -type FakeBase struct{ Base } - -func (f *FakeBase) GetOpenInterest(context.Context, ...key.PairAsset) ([]futures.OpenInterest, error) { - return []futures.OpenInterest{ - { - Key: key.ExchangePairAsset{ - Exchange: f.Name, - Base: currency.BTC.Item, - Quote: currency.BONK.Item, - Asset: asset.Futures, - }, - OpenInterest: 1337, - }, - }, nil -} - -func (f *FakeBase) SetDefaults() { - f.Name = "test" - f.Requester, _ = request.New("test", common.NewHTTPClientWithTimeout(time.Second)) - f.Features.Supports.RESTCapabilities.AutoPairUpdates = true -} -func (f *FakeBase) UpdateTradablePairs(context.Context, bool) error { return nil } - func TestGetCachedOpenInterest(t *testing.T) { t.Parallel() var b FakeBase @@ -3342,13 +3324,10 @@ func TestGetDefaultConfig(t *testing.T) { exch := &FakeBase{} - _, err := exch.GetDefaultConfig(context.Background(), nil) + _, err := GetDefaultConfig(context.Background(), nil) assert.ErrorIs(t, err, errExchangeIsNil) - _, err = exch.GetDefaultConfig(context.Background(), &FakeBase{}) - assert.ErrorIs(t, err, errExchangeMismatch) - - c, err := exch.GetDefaultConfig(context.Background(), exch) + c, err := GetDefaultConfig(context.Background(), exch) require.NoError(t, err) assert.Equal(t, "test", c.Name) @@ -3356,9 +3335,165 @@ func TestGetDefaultConfig(t *testing.T) { // Test below demonstrates that the requester is not overwritten so that // SetDefaults is not called twice. - c, err = exch.GetDefaultConfig(context.Background(), exch) + c, err = GetDefaultConfig(context.Background(), exch) require.NoError(t, err) assert.Equal(t, "test", c.Name) assert.Equal(t, cpy, exch.Requester) } + +// FakeBase is used to override functions +type FakeBase struct{ Base } + +func (f *FakeBase) GetOpenInterest(context.Context, ...key.PairAsset) ([]futures.OpenInterest, error) { + return []futures.OpenInterest{ + { + Key: key.ExchangePairAsset{ + Exchange: f.Name, + Base: currency.BTC.Item, + Quote: currency.BONK.Item, + Asset: asset.Futures, + }, + OpenInterest: 1337, + }, + }, nil +} + +func (f *FakeBase) SetDefaults() { + f.Name = "test" + f.Requester, _ = request.New("test", common.NewHTTPClientWithTimeout(time.Second)) + f.Features.Supports.RESTCapabilities.AutoPairUpdates = true +} +func (f *FakeBase) UpdateTradablePairs(context.Context, bool) error { return nil } + +func (f *FakeBase) Setup(*config.Exchange) error { + return nil +} + +func (f *FakeBase) CancelAllOrders(context.Context, *order.Cancel) (order.CancelAllResponse, error) { + return order.CancelAllResponse{}, nil +} + +func (f *FakeBase) CancelBatchOrders(context.Context, []order.Cancel) (*order.CancelBatchResponse, error) { + return nil, nil +} + +func (f *FakeBase) CancelOrder(context.Context, *order.Cancel) error { + return nil +} + +func (f *FakeBase) FetchAccountInfo(context.Context, asset.Item) (account.Holdings, error) { + return account.Holdings{}, nil +} + +func (f *FakeBase) FetchOrderbook(context.Context, currency.Pair, asset.Item) (*orderbook.Base, error) { + return nil, nil +} + +func (f *FakeBase) FetchTicker(context.Context, currency.Pair, asset.Item) (*ticker.Price, error) { + return nil, nil +} + +func (f *FakeBase) FetchTradablePairs(context.Context, asset.Item) (currency.Pairs, error) { + return nil, nil +} + +func (f *FakeBase) GetAccountFundingHistory(context.Context) ([]FundingHistory, error) { + return nil, nil +} + +func (f *FakeBase) ValidateAPICredentials(context.Context, asset.Item) error { + return nil +} + +func (f *FakeBase) UpdateTickers(context.Context, asset.Item) error { + return nil +} + +func (f *FakeBase) UpdateTicker(context.Context, currency.Pair, asset.Item) (*ticker.Price, error) { + return nil, nil +} + +func (f *FakeBase) UpdateOrderbook(context.Context, currency.Pair, asset.Item) (*orderbook.Base, error) { + return nil, nil +} + +func (f *FakeBase) UpdateAccountInfo(context.Context, asset.Item) (account.Holdings, error) { + return account.Holdings{}, nil +} + +func (f *FakeBase) GetRecentTrades(context.Context, currency.Pair, asset.Item) ([]trade.Data, error) { + return nil, nil +} + +func (f *FakeBase) GetHistoricTrades(context.Context, currency.Pair, asset.Item, time.Time, time.Time) ([]trade.Data, error) { + return nil, nil +} + +func (f *FakeBase) GetServerTime(context.Context, asset.Item) (time.Time, error) { + return time.Now(), nil +} + +func (f *FakeBase) GetFeeByType(context.Context, *FeeBuilder) (float64, error) { + return 0.0, nil +} + +func (f *FakeBase) SubmitOrder(context.Context, *order.Submit) (*order.SubmitResponse, error) { + return nil, nil +} + +func (f *FakeBase) ModifyOrder(context.Context, *order.Modify) (*order.ModifyResponse, error) { + return nil, nil +} + +func (f *FakeBase) GetOrderInfo(context.Context, string, currency.Pair, asset.Item) (*order.Detail, error) { + return nil, nil +} + +func (f *FakeBase) GetDepositAddress(context.Context, currency.Code, string, string) (*deposit.Address, error) { + return nil, nil +} + +func (f *FakeBase) GetOrderHistory(context.Context, *order.MultiOrderRequest) (order.FilteredOrders, error) { + return nil, nil +} + +func (f *FakeBase) GetWithdrawalsHistory(context.Context, currency.Code, asset.Item) ([]WithdrawalHistory, error) { + return []WithdrawalHistory{}, nil +} + +func (f *FakeBase) GetActiveOrders(context.Context, *order.MultiOrderRequest) (order.FilteredOrders, error) { + return []order.Detail{}, nil +} + +func (f *FakeBase) WithdrawCryptocurrencyFunds(context.Context, *withdraw.Request) (*withdraw.ExchangeResponse, error) { + return nil, nil +} + +func (f *FakeBase) WithdrawFiatFunds(context.Context, *withdraw.Request) (*withdraw.ExchangeResponse, error) { + return nil, nil +} + +func (f *FakeBase) WithdrawFiatFundsToInternationalBank(context.Context, *withdraw.Request) (*withdraw.ExchangeResponse, error) { + return nil, nil +} + +func (f *FakeBase) GetHistoricCandles(context.Context, currency.Pair, asset.Item, kline.Interval, time.Time, time.Time) (*kline.Item, error) { + return &kline.Item{}, nil +} + +func (f *FakeBase) GetHistoricCandlesExtended(context.Context, currency.Pair, asset.Item, kline.Interval, time.Time, time.Time) (*kline.Item, error) { + return &kline.Item{}, nil +} + +func (f *FakeBase) UpdateOrderExecutionLimits(context.Context, asset.Item) error { + return nil +} + +func (f *FakeBase) GetLatestFundingRates(context.Context, *fundingrate.LatestRateRequest) ([]fundingrate.LatestRateResponse, error) { + return nil, nil +} + +func (f *FakeBase) GetFuturesContractDetails(context.Context, asset.Item) ([]futures.Contract, error) { + return nil, common.ErrFunctionNotSupported +} diff --git a/exchanges/interfaces.go b/exchanges/interfaces.go index 45160609d65..a9cba1abced 100644 --- a/exchanges/interfaces.go +++ b/exchanges/interfaces.go @@ -63,8 +63,6 @@ type IBotExchange interface { SetHTTPClientUserAgent(ua string) error GetHTTPClientUserAgent() (string, error) SetClientProxyAddress(addr string) error - // GetDefaultConfig returns a default exchange config - GetDefaultConfig(ctx context.Context, instance LimitedScope) (*config.Exchange, error) GetBase() *Base GetHistoricCandles(ctx context.Context, pair currency.Pair, a asset.Item, interval kline.Interval, start, end time.Time) (*kline.Item, error) GetHistoricCandlesExtended(ctx context.Context, pair currency.Pair, a asset.Item, interval kline.Interval, start, end time.Time) (*kline.Item, error) @@ -189,11 +187,3 @@ type MarginManagement interface { futures.PNLCalculation GetFuturesContractDetails(ctx context.Context, item asset.Item) ([]futures.Contract, error) } - -// LimitedScope defines a subset of the exchange interface -type LimitedScope interface { - GetBase() *Base - GetName() string - SetDefaults() - UpdateTradablePairs(context.Context, bool) error -} diff --git a/exchanges/sharedtestvalues/customex.go b/exchanges/sharedtestvalues/customex.go index 9f561b4f6bf..7876ba58b20 100644 --- a/exchanges/sharedtestvalues/customex.go +++ b/exchanges/sharedtestvalues/customex.go @@ -266,11 +266,6 @@ func (c *CustomEx) GetSubscriptions() ([]subscription.Subscription, error) { return nil, nil } -// GetDefaultConfig is a mock method for CustomEx -func (c *CustomEx) GetDefaultConfig(context.Context, exchange.LimitedScope) (*config.Exchange, error) { - return nil, nil -} - // GetBase is a mock method for CustomEx func (c *CustomEx) GetBase() *exchange.Base { return nil diff --git a/gctscript/wrappers/gct/gctwrapper_test.go b/gctscript/wrappers/gct/gctwrapper_test.go index c1964e38209..bb3c3633e3b 100644 --- a/gctscript/wrappers/gct/gctwrapper_test.go +++ b/gctscript/wrappers/gct/gctwrapper_test.go @@ -13,6 +13,7 @@ import ( "github.com/thrasher-corp/gocryptotrader/common" "github.com/thrasher-corp/gocryptotrader/config" "github.com/thrasher-corp/gocryptotrader/engine" + exchange "github.com/thrasher-corp/gocryptotrader/exchanges" "github.com/thrasher-corp/gocryptotrader/exchanges/asset" "github.com/thrasher-corp/gocryptotrader/gctscript/modules" "github.com/thrasher-corp/gocryptotrader/gctscript/modules/gct" @@ -39,7 +40,7 @@ func TestMain(m *testing.M) { log.Print(err) os.Exit(1) } - cfg, err := exch.GetDefaultConfig(context.Background(), exch) + cfg, err := exchange.GetDefaultConfig(context.Background(), exch) if err != nil { log.Fatal(err) }