diff --git a/backtester/data/kline/api/api_test.go b/backtester/data/kline/api/api_test.go index 7990f1ba65c..356b1554441 100644 --- a/backtester/data/kline/api/api_test.go +++ b/backtester/data/kline/api/api_test.go @@ -7,7 +7,6 @@ import ( "time" "github.com/thrasher-corp/gocryptotrader/backtester/common" - "github.com/thrasher-corp/gocryptotrader/common/convert" "github.com/thrasher-corp/gocryptotrader/currency" "github.com/thrasher-corp/gocryptotrader/engine" "github.com/thrasher-corp/gocryptotrader/exchanges/asset" @@ -30,7 +29,7 @@ func TestLoadCandles(t *testing.T) { b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ Available: currency.Pairs{cp}, Enabled: currency.Pairs{cp}, - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Uppercase: true}, RequestFormat: ¤cy.PairFormat{Uppercase: true}} tt1 := time.Now().Add(-time.Minute).Round(gctkline.OneMin.Duration()) @@ -68,7 +67,7 @@ func TestLoadTrades(t *testing.T) { b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ Available: currency.Pairs{cp}, Enabled: currency.Pairs{cp}, - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Uppercase: true}, RequestFormat: ¤cy.PairFormat{Uppercase: true}} interval := gctkline.OneMin diff --git a/backtester/data/kline/live/live_test.go b/backtester/data/kline/live/live_test.go index 428e4fa5d43..becbef217ba 100644 --- a/backtester/data/kline/live/live_test.go +++ b/backtester/data/kline/live/live_test.go @@ -7,7 +7,6 @@ import ( "time" "github.com/thrasher-corp/gocryptotrader/backtester/common" - "github.com/thrasher-corp/gocryptotrader/common/convert" "github.com/thrasher-corp/gocryptotrader/currency" "github.com/thrasher-corp/gocryptotrader/engine" "github.com/thrasher-corp/gocryptotrader/exchanges/asset" @@ -33,7 +32,7 @@ func TestLoadCandles(t *testing.T) { b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ Available: currency.Pairs{cp}, Enabled: currency.Pairs{cp}, - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, RequestFormat: pFormat, ConfigFormat: pFormat, } @@ -68,7 +67,7 @@ func TestLoadTrades(t *testing.T) { b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ Available: currency.Pairs{cp}, Enabled: currency.Pairs{cp}, - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, RequestFormat: pFormat, ConfigFormat: pFormat, } diff --git a/backtester/engine/backtest_test.go b/backtester/engine/backtest_test.go index a19b6c82e80..b1188e3c5aa 100644 --- a/backtester/engine/backtest_test.go +++ b/backtester/engine/backtest_test.go @@ -35,7 +35,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/backtester/funding" "github.com/thrasher-corp/gocryptotrader/backtester/report" gctcommon "github.com/thrasher-corp/gocryptotrader/common" - "github.com/thrasher-corp/gocryptotrader/common/convert" "github.com/thrasher-corp/gocryptotrader/common/key" "github.com/thrasher-corp/gocryptotrader/currency" "github.com/thrasher-corp/gocryptotrader/database" @@ -182,7 +181,7 @@ func TestLoadDataAPI(t *testing.T) { b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ Available: currency.Pairs{cp}, Enabled: currency.Pairs{cp}, - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Uppercase: true}, RequestFormat: ¤cy.PairFormat{Uppercase: true}} @@ -236,7 +235,7 @@ func TestLoadDataCSV(t *testing.T) { b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ Available: currency.Pairs{cp}, Enabled: currency.Pairs{cp}, - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Uppercase: true}, RequestFormat: ¤cy.PairFormat{Uppercase: true}} _, err = bt.loadData(cfg, exch, cp, asset.Spot, false) @@ -301,7 +300,7 @@ func TestLoadDataDatabase(t *testing.T) { b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ Available: currency.Pairs{cp}, Enabled: currency.Pairs{cp}, - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Uppercase: true}, RequestFormat: ¤cy.PairFormat{Uppercase: true}} bt.databaseManager, err = engine.SetupDatabaseConnectionManager(&cfg.DataSettings.DatabaseData.Config) @@ -384,7 +383,7 @@ func TestLoadDataLive(t *testing.T) { b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ Available: currency.Pairs{cp}, Enabled: currency.Pairs{cp}, - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Uppercase: true}, RequestFormat: ¤cy.PairFormat{Uppercase: true}} _, err = bt.loadData(cfg, exch, cp, asset.Spot, false) diff --git a/backtester/engine/live_test.go b/backtester/engine/live_test.go index 5a3682cea58..3873e375609 100644 --- a/backtester/engine/live_test.go +++ b/backtester/engine/live_test.go @@ -15,7 +15,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/backtester/funding" "github.com/thrasher-corp/gocryptotrader/backtester/report" gctcommon "github.com/thrasher-corp/gocryptotrader/common" - "github.com/thrasher-corp/gocryptotrader/common/convert" "github.com/thrasher-corp/gocryptotrader/currency" "github.com/thrasher-corp/gocryptotrader/engine" "github.com/thrasher-corp/gocryptotrader/exchanges/asset" @@ -402,7 +401,7 @@ func TestLoadCandleData(t *testing.T) { eba := exch.CurrencyPairs.Pairs[asset.Spot] eba.Available = eba.Available.Add(cp) eba.Enabled = eba.Enabled.Add(cp) - eba.AssetEnabled = convert.BoolPtr(true) + eba.AssetEnabled = true l.exchange = exch l.dataType = common.DataCandle l.asset = asset.Spot diff --git a/backtester/engine/setup.go b/backtester/engine/setup.go index acbd3e06c17..65967682aa7 100644 --- a/backtester/engine/setup.go +++ b/backtester/engine/setup.go @@ -186,7 +186,7 @@ func (bt *BackTest) SetupFromConfig(cfg *config.Config, templatePath, output str if !ok { return fmt.Errorf("%v %v %w", cfg.CurrencySettings[i].ExchangeName, cfg.CurrencySettings[i].Asset, asset.ErrNotSupported) } - exchangeAsset.AssetEnabled = convert.BoolPtr(true) + exchangeAsset.AssetEnabled = true cp := currency.NewPair(cfg.CurrencySettings[i].Base, cfg.CurrencySettings[i].Quote).Format(*exchangeAsset.RequestFormat) exchangeAsset.Available = exchangeAsset.Available.Add(cp) exchangeAsset.Enabled = exchangeAsset.Enabled.Add(cp) diff --git a/backtester/eventhandlers/exchange/exchange_test.go b/backtester/eventhandlers/exchange/exchange_test.go index c4487b8d004..adbf15b8c67 100644 --- a/backtester/eventhandlers/exchange/exchange_test.go +++ b/backtester/eventhandlers/exchange/exchange_test.go @@ -16,7 +16,6 @@ import ( "github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/order" "github.com/thrasher-corp/gocryptotrader/backtester/funding" gctcommon "github.com/thrasher-corp/gocryptotrader/common" - "github.com/thrasher-corp/gocryptotrader/common/convert" gctconfig "github.com/thrasher-corp/gocryptotrader/config" "github.com/thrasher-corp/gocryptotrader/currency" "github.com/thrasher-corp/gocryptotrader/engine" @@ -381,7 +380,7 @@ func TestExecuteOrderBuySellSizeLimit(t *testing.T) { }, Pairs: map[asset.Item]*currency.PairStore{ asset.Spot: { - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, }, }, } diff --git a/backtester/funding/trackingcurrencies/trackingcurrencies_test.go b/backtester/funding/trackingcurrencies/trackingcurrencies_test.go index c2220424a1b..749057c434d 100644 --- a/backtester/funding/trackingcurrencies/trackingcurrencies_test.go +++ b/backtester/funding/trackingcurrencies/trackingcurrencies_test.go @@ -4,7 +4,6 @@ import ( "errors" "testing" - "github.com/thrasher-corp/gocryptotrader/common/convert" "github.com/thrasher-corp/gocryptotrader/currency" "github.com/thrasher-corp/gocryptotrader/engine" "github.com/thrasher-corp/gocryptotrader/exchanges/asset" @@ -55,7 +54,7 @@ func TestCreateUSDTrackingPairs(t *testing.T) { eba := exchB.CurrencyPairs.Pairs[a] eba.Available = eba.Available.Add(cp, cp2, cp3) eba.Enabled = eba.Enabled.Add(cp, cp2, cp3) - eba.AssetEnabled = convert.BoolPtr(true) + eba.AssetEnabled = true err = em.Add(exch) if !errors.Is(err, nil) { diff --git a/cmd/exchange_wrapper_standards/exchange_wrapper_standards_test.go b/cmd/exchange_wrapper_standards/exchange_wrapper_standards_test.go index c0c9fc9043f..26e1a4d079c 100644 --- a/cmd/exchange_wrapper_standards/exchange_wrapper_standards_test.go +++ b/cmd/exchange_wrapper_standards/exchange_wrapper_standards_test.go @@ -10,6 +10,7 @@ import ( "testing" "time" + "github.com/stretchr/testify/require" "github.com/thrasher-corp/gocryptotrader/common" "github.com/thrasher-corp/gocryptotrader/common/key" "github.com/thrasher-corp/gocryptotrader/config" @@ -100,14 +101,9 @@ func setupExchange(ctx context.Context, t *testing.T, name string, cfg *config.C b := exch.GetBase() assets := b.CurrencyPairs.GetAssetTypes(false) - if len(assets) == 0 { - t.Fatalf("Cannot setup %v, exchange has no assets", name) - } - for j := range assets { - err = b.CurrencyPairs.SetAssetEnabled(assets[j], true) - if err != nil && !errors.Is(err, currency.ErrAssetAlreadyEnabled) { - t.Fatalf("Cannot setup %v SetAssetEnabled %v", name, err) - } + require.NotEmpty(t, assets, "exchange %s must have assets", name) + for _, a := range assets { + require.NoErrorf(t, b.CurrencyPairs.SetAssetEnabled(a, true), "exchange %s SetAssetEnabled must not error for %s", name, a) } // Add +1 to len to verify that exchanges can handle requests with unset pairs and assets diff --git a/config/config.go b/config/config.go index 1754a73ef82..9a9110a90a3 100644 --- a/config/config.go +++ b/config/config.go @@ -904,16 +904,6 @@ func (c *Config) CheckExchangeConfigValues() error { continue } - for _, a := range assets { - if err := e.CurrencyPairs.IsAssetEnabled(a); errors.Is(err, currency.ErrAssetIsNil) { - // Checks if we have an old config without the ability to enable disable the entire asset - log.Warnf(log.ConfigMgr, "Exchange %s: upgrading config for asset type %s and setting enabled.\n", e.Name, a) - if err := e.CurrencyPairs.SetAssetEnabled(a, true); err != nil { - return err - } - } - } - if enabled := e.CurrencyPairs.GetAssetTypes(true); len(enabled) == 0 { // turn on an asset if all disabled log.Warnf(log.ConfigMgr, "%s assets disabled, turning on asset %s", e.Name, assets[0]) diff --git a/config/config_test.go b/config/config_test.go index b071f8d6c24..46404ff6c00 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -720,10 +720,10 @@ func TestCheckPairConsistency(t *testing.T) { t.Parallel() var c Config - err := c.CheckPairConsistency("asdf") - if !errors.Is(err, ErrExchangeNotFound) { - t.Fatalf("received: '%v' buy expected: '%v'", err, ErrExchangeNotFound) - } + p1 := currency.NewPairWithDelimiter("LTC", "USD", "_") + p2 := currency.NewPairWithDelimiter("BTC", "USD", "_") + + assert.ErrorIs(t, c.CheckPairConsistency("asdf"), ErrExchangeNotFound) c.Exchanges = append(c.Exchanges, Exchange{ @@ -731,18 +731,9 @@ func TestCheckPairConsistency(t *testing.T) { }, ) - // Test nil pair store - err = c.CheckPairConsistency(testFakeExchangeName) - if !errors.Is(err, errPairsManagerIsNil) { - t.Fatalf("received: '%v' buy expected: '%v'", err, errPairsManagerIsNil) - } - - enabled, err := currency.NewPairDelimiter("BTC_USD", "_") - if err != nil { - t.Fatal(err) - } + assert.ErrorIs(t, c.CheckPairConsistency(testFakeExchangeName), errPairsManagerIsNil) - c.Exchanges[0].CurrencyPairs = ¤cy.PairsManager{ + pm := ¤cy.PairsManager{ Pairs: map[asset.Item]*currency.PairStore{ asset.Spot: { RequestFormat: ¤cy.PairFormat{ @@ -754,123 +745,51 @@ func TestCheckPairConsistency(t *testing.T) { Delimiter: "_", }, Enabled: currency.Pairs{ - enabled, + p2, }, }, }, } + c.Exchanges[0].CurrencyPairs = pm - // Test for nil avail pairs - err = c.CheckPairConsistency(testFakeExchangeName) - if !errors.Is(err, nil) { - t.Fatalf("received: '%v' buy expected: '%v'", err, nil) - } - - p1, err := currency.NewPairDelimiter("LTC_USD", "_") - if err != nil { - t.Fatal(err) - } + assert.NoError(t, c.CheckPairConsistency(testFakeExchangeName), "Should not error on empty available pairs") + assert.Empty(t, pm.Pairs[asset.Spot].Enabled, "Unavailable pairs should be removed from enabled") // Test that enabled pair is not found in the available pairs - c.Exchanges[0].CurrencyPairs.Pairs[asset.Spot].Available = currency.Pairs{ - p1, - } + pm.Pairs[asset.Spot].Available = currency.Pairs{p1} // LTC_USD is only found in the available pairs list and should therefore // be added to the enabled pairs list due to the atLestOneEnabled code - err = c.CheckPairConsistency(testFakeExchangeName) - if err != nil { - t.Fatal(err) - } + assert.NoError(t, c.CheckPairConsistency(testFakeExchangeName), "Should not error when adding a pair from available to enabled") + require.Equal(t, 1, len(pm.Pairs[asset.Spot].Enabled), "One pair must be enabled") + assert.True(t, slices.Contains(pm.Pairs[asset.Spot].Enabled, p1), "Newly enabled pair should be in Enabled") - if len(c.Exchanges[0].CurrencyPairs.Pairs[asset.Spot].Enabled) != 1 { - t.Fatal("there should be at least one pair located in this list") - } + pm.Pairs[asset.Spot].Available = currency.Pairs{p1, p2} + assert.NoError(t, c.CheckPairConsistency(testFakeExchangeName), "Should not error with no changes to be made") - if !c.Exchanges[0].CurrencyPairs.Pairs[asset.Spot].Enabled[0].Equal(p1) { - t.Fatal("LTC_USD should be contained in the enabled pairs list") - } + pm.Pairs[asset.Spot].Enabled = nil + assert.NoError(t, c.CheckPairConsistency(testFakeExchangeName), "Should not error when adding a pair from available to enabled to fulfil atLeastOne") + assert.NotEmpty(t, pm.Pairs[asset.Spot].Enabled, "One pair must be enabled") - p2, err := currency.NewPairDelimiter("BTC_USD", "_") - if err != nil { - t.Fatal(err) - } + pm.Pairs[asset.Spot].Enabled = currency.Pairs{p1, p2} + assert.NoError(t, c.CheckPairConsistency(testFakeExchangeName), "CheckPairConsistency should not error with when removing an invalid pair") - // Add the BTC_USD pair and see result - c.Exchanges[0].CurrencyPairs.Pairs[asset.Spot].Available = currency.Pairs{ - p1, p2, - } + assert.NoError(t, c.CheckPairConsistency(testFakeExchangeName), "CheckPairConsistency should not error with consistent pairs") - if err := c.CheckPairConsistency(testFakeExchangeName); err != nil { - t.Fatal(err) - } + pm.Pairs[asset.Spot].AssetEnabled = true + pm.Pairs[asset.Spot].Enabled = currency.Pairs{} + assert.NoError(t, c.CheckPairConsistency(testFakeExchangeName), "CheckPairConsistency should not error with spot asset enabled but no pairs") - // Test that an empty enabled pair is populated with an available pair - c.Exchanges[0].CurrencyPairs.Pairs[asset.Spot].Enabled = nil - if err := c.CheckPairConsistency(testFakeExchangeName); err != nil { - t.Error("unexpected result", err) - } - - if len(c.Exchanges[0].CurrencyPairs.Pairs[asset.Spot].Enabled) != 1 { - t.Fatal("should be populated with at least one currency pair") - } - - // Test that an invalid enabled pair is removed from the list - c.Exchanges[0].CurrencyPairs.Pairs[asset.Spot].Enabled = currency.Pairs{ - p1, - p2, - } - if err := c.CheckPairConsistency(testFakeExchangeName); err != nil { - t.Error("unexpected result") - } + pm.Pairs[asset.Spot].AssetEnabled = true + pm.Pairs[asset.Spot].Enabled = currency.Pairs{currency.NewPair(currency.DASH, currency.USD)} + assert.NoError(t, c.CheckPairConsistency(testFakeExchangeName), "CheckPairConsistency should not error with spot asset enabled and enabled pairs") - // Test when no update is required as the available pairs and enabled pairs - // are consistent - if err := c.CheckPairConsistency(testFakeExchangeName); err != nil { - t.Error("unexpected result") - } - - c.Exchanges[0].CurrencyPairs.Pairs[asset.Spot].AssetEnabled = convert.BoolPtr(true) - c.Exchanges[0].CurrencyPairs.Pairs[asset.Spot].Enabled = currency.Pairs{} - - // Test no conflict and at least one on enabled asset type - if err := c.CheckPairConsistency(testFakeExchangeName); err != nil { - t.Error("unexpected result") - } + pm.Pairs[asset.Spot].AssetEnabled = false + pm.Pairs[asset.Spot].Enabled = currency.Pairs{} + assert.NoError(t, c.CheckPairConsistency(testFakeExchangeName), "CheckPairConsistency should not error with spot asset disabled and no enabled pairs") - c.Exchanges[0].CurrencyPairs.Pairs[asset.Spot].AssetEnabled = convert.BoolPtr(true) - c.Exchanges[0].CurrencyPairs.Pairs[asset.Spot].Enabled = currency.Pairs{currency.NewPair(currency.DASH, currency.USD)} - - // Test with conflict and at least one on enabled asset type - if err := c.CheckPairConsistency(testFakeExchangeName); err != nil { - t.Error("unexpected result") - } - - c.Exchanges[0].CurrencyPairs.Pairs[asset.Spot].AssetEnabled = convert.BoolPtr(false) - c.Exchanges[0].CurrencyPairs.Pairs[asset.Spot].Enabled = currency.Pairs{} - - // Test no conflict and at least one on disabled asset type - if err := c.CheckPairConsistency(testFakeExchangeName); err != nil { - t.Error("unexpected result") - } - - c.Exchanges[0].CurrencyPairs.Pairs[asset.Spot].Enabled = currency.Pairs{ - currency.NewPair(currency.DASH, currency.USD), - p1, - p2, - } - - // Test with conflict and at least one on disabled asset type - if err := c.CheckPairConsistency(testFakeExchangeName); err != nil { - t.Error("unexpected result") - } - - c.Exchanges[0].CurrencyPairs.Pairs[asset.Spot].AssetEnabled = nil - - // assetType enabled failure check - if err := c.CheckPairConsistency(testFakeExchangeName); err != nil { - t.Error("unexpected result") - } + pm.Pairs[asset.Spot].Enabled = currency.Pairs{currency.NewPair(currency.DASH, currency.USD), p1, p2} + assert.NoError(t, c.CheckPairConsistency(testFakeExchangeName), "CheckPairConsistency should not error with spot asset disabled but enabled pairs") } func TestSupportsPair(t *testing.T) { @@ -884,7 +803,7 @@ func TestSupportsPair(t *testing.T) { CurrencyPairs: ¤cy.PairsManager{ Pairs: map[asset.Item]*currency.PairStore{ asset.Spot: { - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Available: []currency.Pair{currency.NewPair(currency.BTC, currency.USD)}, ConfigFormat: fmt, RequestFormat: fmt, @@ -1538,7 +1457,7 @@ func TestCheckExchangeConfigValues(t *testing.T) { cfg.Exchanges = append(cfg.Exchanges, cpy[0]) cfg.Exchanges[0].CurrencyPairs.Pairs[asset.Spot].Enabled = nil - cfg.Exchanges[0].CurrencyPairs.Pairs[asset.Spot].AssetEnabled = convert.BoolPtr(false) + cfg.Exchanges[0].CurrencyPairs.Pairs[asset.Spot].AssetEnabled = false err = cfg.CheckExchangeConfigValues() require.NoError(t, err) @@ -1711,7 +1630,7 @@ func TestCheckConfig(t *testing.T) { LastUpdated: 0, Pairs: map[asset.Item]*currency.PairStore{ asset.Spot: { - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Available: currency.Pairs{cp1, cp2}, Enabled: currency.Pairs{cp1}, ConfigFormat: ¤cy.EMPTYFORMAT, diff --git a/config/versions/v3.go b/config/versions/v3.go new file mode 100644 index 00000000000..1ac33c3e061 --- /dev/null +++ b/config/versions/v3.go @@ -0,0 +1,44 @@ +package versions + +import ( + "bytes" + "context" + "fmt" + + "github.com/buger/jsonparser" + "github.com/thrasher-corp/gocryptotrader/common" +) + +// Version3 implements ExchangeVersion +type Version3 struct { +} + +func init() { + Manager.registerVersion(3, &Version3{}) +} + +// Exchanges returns all exchanges: "*" +func (v *Version3) Exchanges() []string { return []string{"*"} } + +// UpgradeExchange sets AssetEnabed: true for any exchange missing it +func (v *Version3) UpgradeExchange(ctx context.Context, e []byte) ([]byte, error) { + name, err := jsonparser.GetString(e, "name") + if err != nil { + return e, fmt.Errorf("%w `name`: %w", common.ErrGettingField, err) + } + cb := func(k []byte, v []byte, _ jsonparser.ValueType, _ int) error { + if _, err := jsonparser.GetBoolean(v, "assetEnabled"); err != nil { + fmt.Printf("Exchange %s: Setting asset %s enabled\n", name, k) + e, err = jsonparser.Set(e, []byte(`true`), "currencyPairs", "pairs", string(k), "assetEnabled") + return err + } + return nil + } + err = jsonparser.ObjectEach(bytes.Clone(e), cb, "currencyPairs", "pairs") + return e, err +} + +// DowngradeExchange doesn't do anything for this version, because it's a lossy downgrade to disable all assets +func (v *Version3) DowngradeExchange(ctx context.Context, e []byte) ([]byte, error) { + return e, nil +} diff --git a/currency/manager.go b/currency/manager.go index 8486aa78694..44404a45b8a 100644 --- a/currency/manager.go +++ b/currency/manager.go @@ -8,14 +8,11 @@ import ( "strings" "github.com/thrasher-corp/gocryptotrader/common" - "github.com/thrasher-corp/gocryptotrader/common/convert" "github.com/thrasher-corp/gocryptotrader/exchanges/asset" ) // Public errors var ( - ErrAssetAlreadyEnabled = errors.New("asset already enabled") - ErrAssetIsNil = errors.New("asset is nil") ErrAssetNotFound = errors.New("asset type not found in pair store") ErrPairAlreadyEnabled = errors.New("pair already enabled") ErrPairFormatIsNil = errors.New("pair format is nil") @@ -38,7 +35,7 @@ func (p *PairsManager) GetAssetTypes(enabled bool) asset.Items { defer p.mutex.RUnlock() assetTypes := make(asset.Items, 0, len(p.Pairs)) for k, ps := range p.Pairs { - if enabled && (ps.AssetEnabled == nil || !*ps.AssetEnabled) { + if enabled && !ps.AssetEnabled { continue } assetTypes = append(assetTypes, k) @@ -246,9 +243,7 @@ func (p *PairsManager) EnsureOnePairEnabled() (Pair, asset.Item, error) { p.mutex.Lock() defer p.mutex.Unlock() for _, v := range p.Pairs { - if v.AssetEnabled == nil || - !*v.AssetEnabled || - len(v.Available) == 0 { + if !v.AssetEnabled || len(v.Available) == 0 { continue } if len(v.Enabled) > 0 { @@ -256,9 +251,7 @@ func (p *PairsManager) EnsureOnePairEnabled() (Pair, asset.Item, error) { } } for k, v := range p.Pairs { - if v.AssetEnabled == nil || - !*v.AssetEnabled || - len(v.Available) == 0 { + if !v.AssetEnabled || len(v.Available) == 0 { continue } rp, err := v.Available.GetRandomPair() @@ -348,10 +341,7 @@ func (p *PairsManager) IsPairAvailable(pair Pair, a asset.Item) (bool, error) { if err != nil { return false, err } - if pairStore.AssetEnabled == nil { - return false, fmt.Errorf("%s %w", a, ErrAssetIsNil) - } - return *pairStore.AssetEnabled && pairStore.Available.Contains(pair, true), nil + return pairStore.AssetEnabled && pairStore.Available.Contains(pair, true), nil } // IsPairEnabled checks if a pair is enabled for an enabled asset type @@ -371,10 +361,7 @@ func (p *PairsManager) IsPairEnabled(pair Pair, a asset.Item) (bool, error) { if err != nil { return false, err } - if pairStore.AssetEnabled == nil { - return false, fmt.Errorf("%s %w", a, ErrAssetIsNil) - } - return *pairStore.AssetEnabled && pairStore.Enabled.Contains(pair, true), nil + return pairStore.AssetEnabled && pairStore.Enabled.Contains(pair, true), nil } // IsAssetEnabled checks to see if an asset is enabled @@ -391,11 +378,7 @@ func (p *PairsManager) IsAssetEnabled(a asset.Item) error { return err } - if pairStore.AssetEnabled == nil { - return fmt.Errorf("%s %w", a, ErrAssetIsNil) - } - - if !*pairStore.AssetEnabled { + if !pairStore.AssetEnabled { return fmt.Errorf("%s %w", a, asset.ErrNotEnabled) } return nil @@ -424,18 +407,8 @@ func (p *PairsManager) SetAssetEnabled(a asset.Item, enabled bool) error { return err } - if pairStore.AssetEnabled == nil { - pairStore.AssetEnabled = convert.BoolPtr(enabled) - return nil - } - - if !*pairStore.AssetEnabled && !enabled { - return errors.New("asset already disabled") - } else if *pairStore.AssetEnabled && enabled { - return ErrAssetAlreadyEnabled - } + pairStore.AssetEnabled = enabled - *pairStore.AssetEnabled = enabled return nil } @@ -547,13 +520,8 @@ func (ps *PairStore) clone() *PairStore { return nil } - var assetEnabled *bool - if ps.AssetEnabled != nil { - assetEnabled = convert.BoolPtr(*ps.AssetEnabled) - } - return &PairStore{ - AssetEnabled: assetEnabled, + AssetEnabled: ps.AssetEnabled, Enabled: slices.Clone(ps.Enabled), Available: slices.Clone(ps.Available), RequestFormat: ps.RequestFormat.clone(), diff --git a/currency/manager_test.go b/currency/manager_test.go index a7064846848..b3eb6aabf86 100644 --- a/currency/manager_test.go +++ b/currency/manager_test.go @@ -7,7 +7,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/thrasher-corp/gocryptotrader/common/convert" "github.com/thrasher-corp/gocryptotrader/exchanges/asset" ) @@ -24,7 +23,7 @@ func initTest(t *testing.T) *PairsManager { } spot := &PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Available: spotAvailable, Enabled: spotEnabled, RequestFormat: &PairFormat{Uppercase: true}, @@ -32,7 +31,7 @@ func initTest(t *testing.T) *PairsManager { } futures := &PairStore{ - AssetEnabled: convert.BoolPtr(false), + AssetEnabled: false, Available: spotAvailable, Enabled: spotEnabled, RequestFormat: &PairFormat{Uppercase: true}, @@ -486,7 +485,7 @@ func TestIsAssetEnabled_SetAssetEnabled(t *testing.T) { // Test asset type which doesn't exist p = initTest(t) - p.Pairs[asset.Spot].AssetEnabled = nil + p.Pairs[asset.Spot].AssetEnabled = false err = p.IsAssetEnabled(asset.Spot) if err == nil { @@ -528,7 +527,7 @@ func TestIsAssetEnabled_SetAssetEnabled(t *testing.T) { func TestFullStoreUnmarshalMarshal(t *testing.T) { t.Parallel() var um = make(FullStore) - um[asset.Spot] = &PairStore{AssetEnabled: convert.BoolPtr(true)} + um[asset.Spot] = &PairStore{AssetEnabled: true} data, err := json.Marshal(um) if err != nil { @@ -591,7 +590,7 @@ func TestIsPairAvailable(t *testing.T) { pm.Pairs[asset.PerpetualSwap] = &PairStore{} _, err = pm.IsPairAvailable(cp, asset.PerpetualSwap) - assert.ErrorIs(t, err, ErrAssetIsNil, "Should error when store AssetEnabled is nil") + require.NoError(t, err, "Must not error when store is empty") _, err = pm.IsPairAvailable(EMPTYPAIR, asset.PerpetualSwap) assert.ErrorIs(t, err, ErrCurrencyPairEmpty, "Should error when currency pair is empty") @@ -602,62 +601,35 @@ func TestIsPairEnabled(t *testing.T) { pm := initTest(t) cp := NewPairWithDelimiter("BTC", "USD", "-") enabled, err := pm.IsPairEnabled(cp, asset.Spot) - if !errors.Is(err, nil) { - t.Fatalf("received: '%v' but expected: '%v'", err, nil) - } - - if !enabled { - t.Fatal("expected pair to be enabled") - } + require.NoError(t, err) + assert.True(t, enabled, "IsPairEnabled should return true when pair is enabled") enabled, err = pm.IsPairEnabled(NewPair(SAFE, MOONRISE), asset.Spot) - if !errors.Is(err, nil) { - t.Fatalf("received: '%v' but expected: '%v'", err, nil) - } - - if enabled { - t.Fatal("expected pair to be disabled") - } + require.NoError(t, err) + assert.False(t, enabled, "IsPairEnabled should return false when pair does not exist") enabled, err = pm.IsPairEnabled(cp, asset.Futures) - if !errors.Is(err, nil) { - t.Fatalf("received: '%v' but expected: '%v'", err, nil) - } - - if enabled { - t.Fatal("expected pair to be disabled because asset type is not enabled") - } + require.NoError(t, err) + assert.False(t, enabled, "IsPairEnabled should return false when asset not enabled") cp = NewPairWithDelimiter("XRP", "DOGE", "-") enabled, err = pm.IsPairEnabled(cp, asset.Spot) - if !errors.Is(err, nil) { - t.Fatalf("received: '%v' but expected: '%v'", err, nil) - } - - if enabled { - t.Fatal("expected pair to be disabled because pair not found in enabled list") - } + require.NoError(t, err) + assert.False(t, enabled, "IsPairEnabled should return false when pair not in enabled list") _, err = pm.IsPairEnabled(cp, asset.PerpetualSwap) - if !errors.Is(err, ErrAssetNotFound) { - t.Fatalf("received: '%v' but expected: '%v'", err, ErrAssetNotFound) - } + assert.ErrorIs(t, err, ErrAssetNotFound) _, err = pm.IsPairEnabled(cp, asset.Item(1337)) - if !errors.Is(err, asset.ErrNotSupported) { - t.Fatalf("received: '%v' but expected: '%v'", err, asset.ErrNotSupported) - } + assert.ErrorIs(t, err, asset.ErrNotSupported) pm.Pairs[asset.PerpetualSwap] = &PairStore{} - _, err = pm.IsPairEnabled(cp, asset.PerpetualSwap) - if !errors.Is(err, ErrAssetIsNil) { - t.Fatalf("received: '%v' but expected: '%v'", err, ErrAssetIsNil) - } + enabled, err = pm.IsPairEnabled(cp, asset.PerpetualSwap) + require.NoError(t, err, "Must not error when store is empty") + assert.False(t, enabled, "Should return false when store is empty") _, err = pm.IsPairEnabled(EMPTYPAIR, asset.PerpetualSwap) - if !errors.Is(err, ErrCurrencyPairEmpty) { - t.Fatalf("received: '%v' but expected: '%v'", err, ErrCurrencyPairEmpty) - } + assert.ErrorIs(t, err, ErrCurrencyPairEmpty) } func TestEnsureOnePairEnabled(t *testing.T) { @@ -667,7 +639,7 @@ func TestEnsureOnePairEnabled(t *testing.T) { Pairs: map[asset.Item]*PairStore{ asset.Futures: {}, asset.Spot: { - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Available: []Pair{ p, }, @@ -700,13 +672,13 @@ func TestEnsureOnePairEnabled(t *testing.T) { pm = PairsManager{ Pairs: map[asset.Item]*PairStore{ asset.Futures: { - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Available: []Pair{ NewPair(BTC, USDC), }, }, asset.Spot: { - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Enabled: []Pair{ p, }, @@ -733,11 +705,11 @@ func TestEnsureOnePairEnabled(t *testing.T) { pm = PairsManager{ Pairs: map[asset.Item]*PairStore{ asset.Futures: { - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Available: []Pair{p}, }, asset.Options: { - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Available: []Pair{}, }, }, @@ -776,20 +748,20 @@ func TestLoad(t *testing.T) { RequestFormat: fmt2, Pairs: map[asset.Item]*PairStore{ asset.Futures: { - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Available: []Pair{p}, }, asset.Options: { - AssetEnabled: convert.BoolPtr(false), + AssetEnabled: false, Available: []Pair{}, }, }, } base.Load(&seed) - assert.True(t, *base.Pairs[asset.Futures].AssetEnabled, "Futures AssetEnabled should be true") + assert.True(t, base.Pairs[asset.Futures].AssetEnabled, "Futures AssetEnabled should be true") assert.True(t, base.Pairs[asset.Futures].Available.Contains(p, true), "Futures Available Pairs should contain BTCUSDT") - assert.False(t, *base.Pairs[asset.Options].AssetEnabled, "Options AssetEnabled should be false") + assert.False(t, base.Pairs[asset.Options].AssetEnabled, "Options AssetEnabled should be false") assert.Equal(t, tt, base.LastUpdated, "Last Updated should be correct") assert.Equal(t, fmt1.Uppercase, base.ConfigFormat.Uppercase, "ConfigFormat Uppercase should be correct") assert.Equal(t, fmt2.Delimiter, base.RequestFormat.Delimiter, "RequestFormat Delimiter should be correct") @@ -909,7 +881,7 @@ func TestIsAssetSupported(t *testing.T) { p := PairsManager{ Pairs: FullStore{ asset.Spot: { - AssetEnabled: convert.BoolPtr(false), + AssetEnabled: false, }, }, } diff --git a/currency/manager_types.go b/currency/manager_types.go index 80178378de2..e74dc7f5ada 100644 --- a/currency/manager_types.go +++ b/currency/manager_types.go @@ -24,7 +24,7 @@ type FullStore map[asset.Item]*PairStore // PairStore stores a currency pair store type PairStore struct { - AssetEnabled *bool `json:"assetEnabled"` + AssetEnabled bool `json:"assetEnabled"` Enabled Pairs `json:"enabled"` Available Pairs `json:"available"` RequestFormat *PairFormat `json:"requestFormat,omitempty"` diff --git a/engine/datahistory_manager_test.go b/engine/datahistory_manager_test.go index 7a955033b79..9851d0d61d3 100644 --- a/engine/datahistory_manager_test.go +++ b/engine/datahistory_manager_test.go @@ -11,7 +11,6 @@ import ( "github.com/gofrs/uuid" "github.com/thrasher-corp/gocryptotrader/common" - "github.com/thrasher-corp/gocryptotrader/common/convert" "github.com/thrasher-corp/gocryptotrader/config" "github.com/thrasher-corp/gocryptotrader/currency" "github.com/thrasher-corp/gocryptotrader/database" @@ -924,7 +923,7 @@ func createDHM(t *testing.T) (*DataHistoryManager, *datahistoryjob.DataHistoryJo b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ Available: currency.Pairs{cp, cp2}, Enabled: currency.Pairs{cp, cp2}, - AssetEnabled: convert.BoolPtr(true)} + AssetEnabled: true} err = em.Add(exch) if !errors.Is(err, nil) { t.Fatalf("received: '%v' but expected: '%v'", err, nil) @@ -940,7 +939,7 @@ func createDHM(t *testing.T) (*DataHistoryManager, *datahistoryjob.DataHistoryJo b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ Available: currency.Pairs{cp, cp2}, Enabled: currency.Pairs{cp, cp2}, - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Uppercase: true}, RequestFormat: ¤cy.PairFormat{Uppercase: true}, } diff --git a/engine/helpers_test.go b/engine/helpers_test.go index a935afe3046..14063fbaf9b 100644 --- a/engine/helpers_test.go +++ b/engine/helpers_test.go @@ -48,14 +48,14 @@ func CreateTestBot(tb testing.TB) *Engine { pairs1 := map[asset.Item]*currency.PairStore{ asset.Spot: { - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Available: currency.Pairs{cp1}, Enabled: currency.Pairs{cp1}, }, } pairs2 := map[asset.Item]*currency.PairStore{ asset.Spot: { - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Available: currency.Pairs{cp2}, Enabled: currency.Pairs{cp2}, }, @@ -374,7 +374,7 @@ func TestGetSpecificAvailablePairs(t *testing.T) { Name: testExchange, CurrencyPairs: ¤cy.PairsManager{Pairs: map[asset.Item]*currency.PairStore{ asset.Spot: { - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Enabled: currency.Pairs{currency.NewPair(currency.BTC, currency.USD), currency.NewPair(currency.BTC, c)}, Available: currency.Pairs{currency.NewPair(currency.BTC, currency.USD), currency.NewPair(currency.BTC, c)}, ConfigFormat: ¤cy.PairFormat{ @@ -719,7 +719,7 @@ func TestGetExchangeNamesByCurrency(t *testing.T) { Name: bf, CurrencyPairs: ¤cy.PairsManager{Pairs: map[asset.Item]*currency.PairStore{ asset.Spot: { - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Enabled: currency.Pairs{btcjpy}, Available: currency.Pairs{btcjpy}, ConfigFormat: ¤cy.PairFormat{ @@ -1044,7 +1044,7 @@ func (f fakeDepositExchange) GetDepositAddress(_ context.Context, _ currency.Cod func createDepositEngine(opts *fakeDepositExchangeOpts) *Engine { ps := currency.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Enabled: currency.Pairs{ currency.NewPair(currency.BTC, currency.USDT), currency.NewPair(currency.XRP, currency.USDT), diff --git a/engine/order_manager_test.go b/engine/order_manager_test.go index a0172fa2ff2..2131955d915 100644 --- a/engine/order_manager_test.go +++ b/engine/order_manager_test.go @@ -807,12 +807,12 @@ func TestProcessOrders(t *testing.T) { }, Pairs: map[asset.Item]*currency.PairStore{ asset.Spot: { - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Enabled: pairs, Available: pairs, }, asset.Futures: { - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Enabled: pairs, Available: pairs, }, @@ -831,12 +831,12 @@ func TestProcessOrders(t *testing.T) { }, Pairs: map[asset.Item]*currency.PairStore{ asset.Spot: { - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Enabled: pairs, Available: pairs, }, asset.Futures: { - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Enabled: pairs, Available: pairs, }, @@ -1522,14 +1522,14 @@ func TestGetOpenFuturesPosition(t *testing.T) { b.Enabled = true b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore) b.CurrencyPairs.Pairs[asset.Futures] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, RequestFormat: ¤cy.PairFormat{Delimiter: "-"}, ConfigFormat: ¤cy.PairFormat{Delimiter: "-"}, Available: currency.Pairs{cp}, Enabled: currency.Pairs{cp}, } b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Delimiter: "/"}, RequestFormat: ¤cy.PairFormat{Delimiter: "/"}, Available: currency.Pairs{cp}, @@ -1616,14 +1616,14 @@ func TestProcessFuturesPositions(t *testing.T) { b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore) b.CurrencyPairs.Pairs[asset.Futures] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, RequestFormat: ¤cy.PairFormat{Delimiter: "-"}, ConfigFormat: ¤cy.PairFormat{Delimiter: "-"}, Available: currency.Pairs{cp, cp2}, Enabled: currency.Pairs{cp, cp2}, } b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Delimiter: "/"}, RequestFormat: ¤cy.PairFormat{Delimiter: "/"}, Available: currency.Pairs{cp, cp2}, diff --git a/engine/rpcserver_test.go b/engine/rpcserver_test.go index c7d2575c955..60fe849aa1f 100644 --- a/engine/rpcserver_test.go +++ b/engine/rpcserver_test.go @@ -26,7 +26,6 @@ import ( "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" "github.com/thrasher-corp/gocryptotrader/config" "github.com/thrasher-corp/gocryptotrader/currency" @@ -485,7 +484,7 @@ func RPCTestSetup(t *testing.T) *Engine { b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ Available: currency.Pairs{cp}, Enabled: currency.Pairs{cp}, - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Uppercase: true}, RequestFormat: ¤cy.PairFormat{Uppercase: true}} err = em.Add(exch) @@ -504,7 +503,7 @@ func RPCTestSetup(t *testing.T) *Engine { b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ Available: currency.Pairs{cp}, Enabled: currency.Pairs{cp}, - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Uppercase: true}, RequestFormat: ¤cy.PairFormat{Uppercase: true}} err = em.Add(exch) @@ -1268,7 +1267,7 @@ func TestGetAccountInfo(t *testing.T) { b.Enabled = true b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore) b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, } fakeExchange := fExchange{ IBotExchange: exch, @@ -1296,7 +1295,7 @@ func TestUpdateAccountInfo(t *testing.T) { b.Enabled = true b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore) b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, } fakeExchange := fExchange{ IBotExchange: exch, @@ -1342,7 +1341,7 @@ func TestGetOrders(t *testing.T) { b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ Available: currency.Pairs{cp}, Enabled: currency.Pairs{cp}, - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Uppercase: true}, RequestFormat: ¤cy.PairFormat{Uppercase: true}} err = em.Add(exch) @@ -1452,7 +1451,7 @@ func TestGetOrder(t *testing.T) { b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ Available: currency.Pairs{cp}, Enabled: currency.Pairs{cp}, - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Uppercase: true}, RequestFormat: ¤cy.PairFormat{Uppercase: true}} err = em.Add(exch) @@ -1704,7 +1703,7 @@ func TestRPCServerUpsertDataHistoryJob(t *testing.T) { b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ Available: currency.Pairs{cp}, Enabled: currency.Pairs{cp}, - AssetEnabled: convert.BoolPtr(true)} + AssetEnabled: true} err = em.Add(exch) if !errors.Is(err, nil) { t.Fatalf("received: '%v' but expected: '%v'", err, nil) @@ -1991,7 +1990,7 @@ func TestGetManagedOrders(t *testing.T) { b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ Available: currency.Pairs{cp}, Enabled: currency.Pairs{cp}, - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Uppercase: true}, RequestFormat: ¤cy.PairFormat{Uppercase: true}} err = em.Add(exch) @@ -2294,7 +2293,7 @@ func TestCurrencyStateTradingPair(t *testing.T) { b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore) b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.EMPTYFORMAT, Available: currency.Pairs{cp}, Enabled: currency.Pairs{cp}, @@ -2340,14 +2339,14 @@ func TestGetFuturesPositionsOrders(t *testing.T) { b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore) b.CurrencyPairs.Pairs[asset.Futures] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, RequestFormat: ¤cy.PairFormat{Delimiter: "-"}, ConfigFormat: ¤cy.PairFormat{Delimiter: "-"}, Available: currency.Pairs{cp}, Enabled: currency.Pairs{cp}, } b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Delimiter: "/"}, RequestFormat: ¤cy.PairFormat{Delimiter: "/"}, Available: currency.Pairs{cp}, @@ -2422,13 +2421,13 @@ func TestGetCollateral(t *testing.T) { b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore) b.CurrencyPairs.Pairs[asset.Futures] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.EMPTYFORMAT, Available: currency.Pairs{cp}, Enabled: currency.Pairs{cp}, } b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.EMPTYFORMAT, Available: currency.Pairs{cp}, Enabled: currency.Pairs{cp}, @@ -2547,13 +2546,13 @@ func TestGetTechnicalAnalysis(t *testing.T) { b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore) b.CurrencyPairs.Pairs[asset.Futures] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{}, Available: currency.Pairs{cp}, Enabled: currency.Pairs{cp}, } b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{}, Available: currency.Pairs{cp}, Enabled: currency.Pairs{cp}, @@ -2819,7 +2818,7 @@ func TestGetMarginRatesHistory(t *testing.T) { b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore) b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{}, Available: currency.Pairs{cp}, Enabled: currency.Pairs{cp}, @@ -2962,7 +2961,7 @@ func TestGetFundingRates(t *testing.T) { b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore) err = b.CurrencyPairs.Store(asset.Futures, ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, RequestFormat: ¤cy.PairFormat{Delimiter: "-"}, ConfigFormat: ¤cy.PairFormat{Delimiter: "-"}, Available: currency.Pairs{cp}, @@ -2973,7 +2972,7 @@ func TestGetFundingRates(t *testing.T) { } err = b.CurrencyPairs.Store(asset.Spot, ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Delimiter: "/"}, RequestFormat: ¤cy.PairFormat{Delimiter: "/"}, Available: currency.Pairs{cp}, @@ -3070,7 +3069,7 @@ func TestGetLatestFundingRate(t *testing.T) { b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore) err = b.CurrencyPairs.Store(asset.Futures, ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, RequestFormat: ¤cy.PairFormat{Delimiter: "-"}, ConfigFormat: ¤cy.PairFormat{Delimiter: "-"}, Available: currency.Pairs{cp}, @@ -3080,7 +3079,7 @@ func TestGetLatestFundingRate(t *testing.T) { t.Fatal(err) } err = b.CurrencyPairs.Store(asset.Spot, ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Delimiter: "/"}, RequestFormat: ¤cy.PairFormat{Delimiter: "/"}, Available: currency.Pairs{cp}, @@ -3175,14 +3174,14 @@ func TestGetManagedPosition(t *testing.T) { b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore) b.CurrencyPairs.Pairs[asset.Futures] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, RequestFormat: ¤cy.PairFormat{Delimiter: "-"}, ConfigFormat: ¤cy.PairFormat{Delimiter: "-"}, Available: currency.Pairs{cp, cp2}, Enabled: currency.Pairs{cp, cp2}, } b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Delimiter: "/"}, RequestFormat: ¤cy.PairFormat{Delimiter: "/"}, Available: currency.Pairs{cp, cp2}, @@ -3318,14 +3317,14 @@ func TestGetAllManagedPositions(t *testing.T) { b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore) b.CurrencyPairs.Pairs[asset.Futures] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, RequestFormat: ¤cy.PairFormat{Delimiter: "-"}, ConfigFormat: ¤cy.PairFormat{Delimiter: "-"}, Available: currency.Pairs{cp, cp2}, Enabled: currency.Pairs{cp, cp2}, } b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Delimiter: "/"}, RequestFormat: ¤cy.PairFormat{Delimiter: "/"}, Available: currency.Pairs{cp, cp2}, @@ -3424,7 +3423,7 @@ func TestGetOrderbookMovement(t *testing.T) { b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore) b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Delimiter: "/"}, RequestFormat: ¤cy.PairFormat{Delimiter: "/"}, Available: currency.Pairs{cp}, @@ -3537,7 +3536,7 @@ func TestGetOrderbookAmountByNominal(t *testing.T) { b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore) b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Delimiter: "/"}, RequestFormat: ¤cy.PairFormat{Delimiter: "/"}, Available: currency.Pairs{cp}, @@ -3643,7 +3642,7 @@ func TestGetOrderbookAmountByImpact(t *testing.T) { b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore) b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Delimiter: "/"}, RequestFormat: ¤cy.PairFormat{Delimiter: "/"}, Available: currency.Pairs{cp}, @@ -3751,7 +3750,7 @@ func TestChangePositionMargin(t *testing.T) { b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore) b.CurrencyPairs.Pairs[asset.USDTMarginedFutures] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Delimiter: "/"}, RequestFormat: ¤cy.PairFormat{Delimiter: "/"}, Available: currency.Pairs{cp}, @@ -3814,7 +3813,7 @@ func TestSetLeverage(t *testing.T) { b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore) b.CurrencyPairs.Pairs[asset.USDTMarginedFutures] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Delimiter: "/"}, RequestFormat: ¤cy.PairFormat{Delimiter: "/"}, Available: currency.Pairs{cp}, @@ -3892,7 +3891,7 @@ func TestGetLeverage(t *testing.T) { b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore) b.CurrencyPairs.Pairs[asset.USDTMarginedFutures] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Delimiter: "/"}, RequestFormat: ¤cy.PairFormat{Delimiter: "/"}, Available: currency.Pairs{cp}, @@ -3972,7 +3971,7 @@ func TestSetMarginType(t *testing.T) { b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore) b.CurrencyPairs.Pairs[asset.USDTMarginedFutures] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Delimiter: "/"}, RequestFormat: ¤cy.PairFormat{Delimiter: "/"}, Available: currency.Pairs{cp}, @@ -4032,7 +4031,7 @@ func TestSetCollateralMode(t *testing.T) { b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore) b.CurrencyPairs.Pairs[asset.USDTMarginedFutures] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, ConfigFormat: ¤cy.PairFormat{Delimiter: "/"}, RequestFormat: ¤cy.PairFormat{Delimiter: "/"}, Available: currency.Pairs{cp}, @@ -4081,7 +4080,7 @@ func TestGetCollateralMode(t *testing.T) { b.Enabled = true b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore) b.CurrencyPairs.Pairs[asset.USDTMarginedFutures] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, } fakeExchange := fExchange{ @@ -4124,7 +4123,7 @@ func TestGetOpenInterest(t *testing.T) { b.Enabled = true b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore) b.CurrencyPairs.Pairs[asset.USDTMarginedFutures] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, } fakeExchange := fExchange{ @@ -4305,7 +4304,7 @@ func TestGetCurrencyTradeURL(t *testing.T) { b.Enabled = true b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore) err = b.CurrencyPairs.Store(asset.Spot, ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Enabled: []currency.Pair{currency.NewPair(currency.BTC, currency.USDT)}, Available: []currency.Pair{currency.NewPair(currency.BTC, currency.USDT)}, RequestFormat: ¤cy.PairFormat{Uppercase: true}, diff --git a/exchanges/account/account_test.go b/exchanges/account/account_test.go index a22fe0352ee..273548deb40 100644 --- a/exchanges/account/account_test.go +++ b/exchanges/account/account_test.go @@ -416,7 +416,7 @@ func TestUpdate(t *testing.T) { Exchange: "TeSt", Accounts: []SubAccount{ { - AssetType: 6969, + AssetType: asset.Empty, ID: "1337", Currencies: []Balance{ { diff --git a/exchanges/asset/asset.go b/exchanges/asset/asset.go index e229dffeab6..2a84988124c 100644 --- a/exchanges/asset/asset.go +++ b/exchanges/asset/asset.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "slices" "strings" ) @@ -20,10 +21,10 @@ type Item uint32 // Items stores a list of assets types type Items []Item -// Const vars for asset package +// Supported Assets const ( Empty Item = 0 - Spot Item = 1 << iota + Spot Item = 1 << (iota - 1) Margin CrossMargin MarginFunding @@ -41,13 +42,16 @@ const ( Options OptionCombo FutureCombo - LinearContract // Added to represent a USDT and USDC based linear derivatives(futures/perpetual) assets in Bybit V5 - All + LinearContract // A USDT or USDC base linear derivatives (futures/perpetual) + All // Must come immediately after all valid assets +) - optionsFlag = OptionCombo | Options - futuresFlag = PerpetualContract | PerpetualSwap | Futures | DeliveryFutures | UpsideProfitContract | DownsideProfitContract | CoinMarginedFutures | USDTMarginedFutures | USDCMarginedFutures | LinearContract | FutureCombo - supportedFlag = Spot | Margin | CrossMargin | MarginFunding | Index | Binary | PerpetualContract | PerpetualSwap | Futures | DeliveryFutures | UpsideProfitContract | DownsideProfitContract | CoinMarginedFutures | USDTMarginedFutures | USDCMarginedFutures | Options | LinearContract | OptionCombo | FutureCombo +const ( + optionsFlag = OptionCombo | Options + futuresFlag = PerpetualContract | PerpetualSwap | Futures | DeliveryFutures | UpsideProfitContract | DownsideProfitContract | CoinMarginedFutures | USDTMarginedFutures | USDCMarginedFutures | LinearContract | FutureCombo +) +const ( spot = "spot" margin = "margin" crossMargin = "cross_margin" // for Gateio exchange @@ -67,16 +71,17 @@ const ( options = "options" optionCombo = "option_combo" futureCombo = "future_combo" + linearContract = "linearcontract" all = "all" ) -var ( - supportedList = Items{Spot, Margin, CrossMargin, MarginFunding, Index, Binary, PerpetualContract, PerpetualSwap, Futures, DeliveryFutures, UpsideProfitContract, DownsideProfitContract, CoinMarginedFutures, USDTMarginedFutures, USDCMarginedFutures, Options, LinearContract, OptionCombo, FutureCombo} -) - // Supported returns a list of supported asset types func Supported() Items { - return supportedList + l := []Item{} + for i := Empty; 1<>size != 1; size++ { } + require.Equal(t, size, len(s), "Supported must return expected number of assets") + require.Equal(t, All>>1, s[len(s)-1], "Last item must be correct") +} - a = 0 - if a.String() != "" { - t.Fatal("TestString returned an unexpected result") +func TestString(t *testing.T) { + t.Parallel() + for a := Item(1); a <= All; a = a << 1 { + assert.NotEmptyf(t, a.String(), "%s.String should return non-empty") } + assert.Empty(t, Empty.String(), "Empty.String should return empty") } func TestStrings(t *testing.T) { @@ -56,17 +62,6 @@ func TestJoinToString(t *testing.T) { } } -func TestIsValid(t *testing.T) { - t.Parallel() - if Item(0).IsValid() { - t.Fatal("TestIsValid returned an unexpected result") - } - - if !Spot.IsValid() { - t.Fatal("TestIsValid returned an unexpected result") - } -} - func TestNew(t *testing.T) { t.Parallel() cases := []struct { @@ -90,8 +85,9 @@ func TestNew(t *testing.T) { {Input: "Options", Expected: Options}, {Input: "Option", Expected: Options}, {Input: "Future", Error: ErrNotSupported}, - {Input: "future_combo", Expected: FutureCombo}, {Input: "option_combo", Expected: OptionCombo}, + {Input: "future_combo", Expected: FutureCombo}, + {Input: "linearContract", Expected: LinearContract}, } for _, tt := range cases { @@ -108,90 +104,13 @@ func TestNew(t *testing.T) { } } -func TestSupported(t *testing.T) { - t.Parallel() - s := Supported() - if len(supportedList) != len(s) { - t.Fatal("TestSupported mismatched lengths") - } - for i := range supportedList { - if s[i] != supportedList[i] { - t.Fatal("TestSupported returned an unexpected result") - } - } -} - func TestIsFutures(t *testing.T) { t.Parallel() - type scenario struct { - item Item - isFutures bool + for _, a := range []Item{Spot, Margin, MarginFunding, Index, Binary} { + assert.Falsef(t, a.IsFutures(), "%s should return correctly for IsFutures") } - scenarios := []scenario{ - { - item: Spot, - isFutures: false, - }, - { - item: Margin, - isFutures: false, - }, - { - item: MarginFunding, - isFutures: false, - }, - { - item: Index, - isFutures: false, - }, - { - item: Binary, - isFutures: false, - }, - { - item: PerpetualContract, - isFutures: true, - }, - { - item: PerpetualSwap, - isFutures: true, - }, - { - item: Futures, - isFutures: true, - }, - { - item: UpsideProfitContract, - isFutures: true, - }, - { - item: DownsideProfitContract, - isFutures: true, - }, - { - item: CoinMarginedFutures, - isFutures: true, - }, - { - item: USDTMarginedFutures, - isFutures: true, - }, - { - item: USDCMarginedFutures, - isFutures: true, - }, { - item: FutureCombo, - isFutures: true, - }, - } - for _, s := range scenarios { - testScenario := s - t.Run(testScenario.item.String(), func(t *testing.T) { - t.Parallel() - if testScenario.item.IsFutures() != testScenario.isFutures { - t.Errorf("expected %v isFutures to be %v", testScenario.item, testScenario.isFutures) - } - }) + for _, a := range []Item{PerpetualContract, PerpetualSwap, Futures, UpsideProfitContract, DownsideProfitContract, CoinMarginedFutures, USDTMarginedFutures, USDCMarginedFutures, FutureCombo} { + assert.Truef(t, a.IsFutures(), "%s should return correctly for IsFutures") } } diff --git a/exchanges/exchange.go b/exchanges/exchange.go index 938923e2332..a1dbf9da148 100644 --- a/exchanges/exchange.go +++ b/exchanges/exchange.go @@ -15,7 +15,6 @@ import ( "unicode" "github.com/thrasher-corp/gocryptotrader/common" - "github.com/thrasher-corp/gocryptotrader/common/convert" "github.com/thrasher-corp/gocryptotrader/common/key" "github.com/thrasher-corp/gocryptotrader/config" "github.com/thrasher-corp/gocryptotrader/currency" @@ -321,11 +320,7 @@ func (b *Base) SetConfigPairs() error { enabledAsset = true } - err := b.CurrencyPairs.SetAssetEnabled(assetTypes[x], enabledAsset) - // Suppress error when assets are enabled by default and they are being - // enabled by config. A check for the inverse - // e.g. currency.ErrAssetAlreadyDisabled is not needed. - if err != nil && !errors.Is(err, currency.ErrAssetAlreadyEnabled) { + if err := b.CurrencyPairs.SetAssetEnabled(assetTypes[x], enabledAsset); err != nil { return err } @@ -335,12 +330,10 @@ func (b *Base) SetConfigPairs() error { } if b.Config.CurrencyPairs.UseGlobalFormat { - err = b.CurrencyPairs.StorePairs(assetTypes[x], cfgPS.Available, false) - if err != nil { + if err = b.CurrencyPairs.StorePairs(assetTypes[x], cfgPS.Available, false); err != nil { return err } - err = b.CurrencyPairs.StorePairs(assetTypes[x], cfgPS.Enabled, true) - if err != nil { + if err = b.CurrencyPairs.StorePairs(assetTypes[x], cfgPS.Enabled, true); err != nil { return err } continue @@ -351,24 +344,20 @@ func (b *Base) SetConfigPairs() error { } if exchPS.ConfigFormat != nil { - err = b.Config.CurrencyPairs.StoreFormat(assetTypes[x], exchPS.ConfigFormat, true) - if err != nil { + if err = b.Config.CurrencyPairs.StoreFormat(assetTypes[x], exchPS.ConfigFormat, true); err != nil { return err } } if exchPS.RequestFormat != nil { - err = b.Config.CurrencyPairs.StoreFormat(assetTypes[x], exchPS.RequestFormat, false) - if err != nil { + if err = b.Config.CurrencyPairs.StoreFormat(assetTypes[x], exchPS.RequestFormat, false); err != nil { return err } } - err = b.CurrencyPairs.StorePairs(assetTypes[x], cfgPS.Available, false) - if err != nil { + if err = b.CurrencyPairs.StorePairs(assetTypes[x], cfgPS.Available, false); err != nil { return err } - err = b.CurrencyPairs.StorePairs(assetTypes[x], cfgPS.Enabled, true) - if err != nil { + if err = b.CurrencyPairs.StorePairs(assetTypes[x], cfgPS.Enabled, true); err != nil { return err } } @@ -531,8 +520,7 @@ func (b *Base) IsEnabled() bool { // SetupDefaults sets the exchange settings based on the supplied config func (b *Base) SetupDefaults(exch *config.Exchange) error { - err := exch.Validate() - if err != nil { + if err := exch.Validate(); err != nil { return err } @@ -558,8 +546,7 @@ func (b *Base) SetupDefaults(exch *config.Exchange) error { exch.HTTPTimeout = DefaultHTTPTimeout } - err = b.SetHTTPClientTimeout(exch.HTTPTimeout) - if err != nil { + if err := b.SetHTTPClientTimeout(exch.HTTPTimeout); err != nil { return err } @@ -567,8 +554,7 @@ func (b *Base) SetupDefaults(exch *config.Exchange) error { exch.CurrencyPairs = &b.CurrencyPairs a := exch.CurrencyPairs.GetAssetTypes(false) for i := range a { - err = exch.CurrencyPairs.SetAssetEnabled(a[i], true) - if err != nil && !errors.Is(err, currency.ErrAssetAlreadyEnabled) { + if err := exch.CurrencyPairs.SetAssetEnabled(a[i], true); err != nil { return err } } @@ -576,18 +562,15 @@ func (b *Base) SetupDefaults(exch *config.Exchange) error { b.HTTPDebugging = exch.HTTPDebugging b.BypassConfigFormatUpgrades = exch.CurrencyPairs.BypassConfigFormatUpgrades - err = b.SetHTTPClientUserAgent(exch.HTTPUserAgent) - if err != nil { + if err := b.SetHTTPClientUserAgent(exch.HTTPUserAgent); err != nil { return err } - err = b.SetCurrencyPairFormat() - if err != nil { + if err := b.SetCurrencyPairFormat(); err != nil { return err } - err = b.SetConfigPairs() - if err != nil { + if err := b.SetConfigPairs(); err != nil { return err } @@ -597,27 +580,26 @@ func (b *Base) SetupDefaults(exch *config.Exchange) error { b.API.Endpoints = b.NewEndpoints() } - err = b.SetAPIURL() - if err != nil { + if err := b.SetAPIURL(); err != nil { return err } b.SetAPICredentialDefaults() - err = b.SetClientProxyAddress(exch.ProxyAddress) - if err != nil { + if err := b.SetClientProxyAddress(exch.ProxyAddress); err != nil { return err } + b.BaseCurrencies = exch.BaseCurrencies if exch.Orderbook.VerificationBypass { - log.Warnf(log.ExchangeSys, - "%s orderbook verification has been bypassed via config.", - b.Name) + log.Warnf(log.ExchangeSys, "%s orderbook verification has been bypassed via config.", b.Name) } + b.CanVerifyOrderbook = !exch.Orderbook.VerificationBypass b.States = currencystate.NewCurrencyStates() - return err + + return nil } // SetPairs sets the exchange currency pairs for either enabledPairs or @@ -1013,10 +995,6 @@ func (b *Base) StoreAssetPairFormat(a asset.Item, f currency.PairStore) error { b.Name) } - if f.AssetEnabled == nil { - f.AssetEnabled = convert.BoolPtr(true) - } - if f.RequestFormat == nil { return fmt.Errorf("%s cannot add to pairs manager, request pair format not provided", b.Name) @@ -1078,7 +1056,7 @@ func (b *Base) SetGlobalPairsManager(request, config *currency.PairFormat, asset return fmt.Errorf("%s cannot set pairs manager, asset is empty string", b.Name) } b.CurrencyPairs.Pairs[assets[i]] = new(currency.PairStore) - b.CurrencyPairs.Pairs[assets[i]].AssetEnabled = convert.BoolPtr(true) + b.CurrencyPairs.Pairs[assets[i]].AssetEnabled = true b.CurrencyPairs.Pairs[assets[i]].ConfigFormat = config b.CurrencyPairs.Pairs[assets[i]].RequestFormat = request } diff --git a/exchanges/exchange_test.go b/exchanges/exchange_test.go index a5db589d047..e555ca22098 100644 --- a/exchanges/exchange_test.go +++ b/exchanges/exchange_test.go @@ -10,7 +10,6 @@ import ( "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" "github.com/thrasher-corp/gocryptotrader/config" "github.com/thrasher-corp/gocryptotrader/currency" @@ -536,7 +535,7 @@ func TestLoadConfigPairs(t *testing.T) { }, Pairs: map[asset.Item]*currency.PairStore{ asset.Spot: { - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Enabled: pairs, Available: pairs, }, @@ -913,7 +912,7 @@ func TestSetPairs(t *testing.T) { }, Pairs: map[asset.Item]*currency.PairStore{ asset.Spot: { - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, }, }, }, @@ -973,7 +972,7 @@ func TestUpdatePairs(t *testing.T) { CurrencyPairs: currency.PairsManager{ Pairs: map[asset.Item]*currency.PairStore{ asset.Spot: { - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, }, }, ConfigFormat: ¤cy.PairFormat{Uppercase: true, Delimiter: currency.DashDelimiter}, @@ -1292,7 +1291,7 @@ func TestSupportsAsset(t *testing.T) { var b Base b.CurrencyPairs.Pairs = map[asset.Item]*currency.PairStore{ asset.Spot: { - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, }, } assert.True(t, b.SupportsAsset(asset.Spot), "Spot should be supported") @@ -1335,7 +1334,7 @@ func TestGetAssetType(t *testing.T) { } b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore) b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Enabled: currency.Pairs{ currency.NewPair(currency.BTC, currency.USD), }, @@ -1373,7 +1372,7 @@ func TestGetFormattedPairAndAssetType(t *testing.T) { b.CurrencyPairs.ConfigFormat = pFmt b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore) b.CurrencyPairs.Pairs[asset.Spot] = ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Enabled: currency.Pairs{ currency.NewPair(currency.BTC, currency.USD), }, @@ -1518,7 +1517,7 @@ func TestBase_ValidateKline(t *testing.T) { CurrencyPairs: currency.PairsManager{ Pairs: map[asset.Item]*currency.PairStore{ asset.Spot: { - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Enabled: pairs, Available: availablePairs, }, @@ -2122,14 +2121,14 @@ func TestGetPairAndAssetTypeRequestFormatted(t *testing.T) { CurrencyPairs: currency.PairsManager{ Pairs: map[asset.Item]*currency.PairStore{ asset.Spot: { - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Enabled: enabledPairs, Available: availablePairs, RequestFormat: ¤cy.PairFormat{Delimiter: "-", Uppercase: true}, ConfigFormat: ¤cy.EMPTYFORMAT, }, asset.PerpetualContract: { - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Enabled: enabledPairs, Available: availablePairs, RequestFormat: ¤cy.PairFormat{Delimiter: "_", Uppercase: true}, @@ -2265,7 +2264,7 @@ func TestGetKlineRequest(t *testing.T) { b.Features.Enabled.Kline.Intervals = kline.DeployExchangeIntervals(kline.IntervalCapacity{Interval: kline.OneDay, Capacity: 1439}) err = b.CurrencyPairs.Store(asset.Spot, ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Enabled: []currency.Pair{pair}, Available: []currency.Pair{pair}, }) @@ -2289,7 +2288,7 @@ func TestGetKlineRequest(t *testing.T) { assert.ErrorIs(t, err, currency.ErrPairFormatIsNil, "GetKlineRequest should return Format is Nil") err = b.CurrencyPairs.Store(asset.Spot, ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Enabled: []currency.Pair{pair}, Available: []currency.Pair{pair}, RequestFormat: ¤cy.PairFormat{Uppercase: true}, @@ -2316,7 +2315,7 @@ func TestGetKlineRequest(t *testing.T) { } err = b.CurrencyPairs.Store(asset.Futures, ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Enabled: []currency.Pair{pair}, Available: []currency.Pair{pair}, RequestFormat: ¤cy.PairFormat{Uppercase: true}, @@ -2442,7 +2441,7 @@ func TestGetKlineExtendedRequest(t *testing.T) { } err = b.CurrencyPairs.Store(asset.Spot, ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Enabled: []currency.Pair{pair}, Available: []currency.Pair{pair}, }) @@ -2454,7 +2453,7 @@ func TestGetKlineExtendedRequest(t *testing.T) { assert.ErrorIs(t, err, currency.ErrPairFormatIsNil, "GetKlineExtendedRequest should error correctly") err = b.CurrencyPairs.Store(asset.Spot, ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Enabled: []currency.Pair{pair}, Available: []currency.Pair{pair}, RequestFormat: ¤cy.PairFormat{Uppercase: true}, @@ -2573,7 +2572,7 @@ func TestEnsureOnePairEnabled(t *testing.T) { Pairs: map[asset.Item]*currency.PairStore{ asset.Futures: {}, asset.Spot: { - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Available: []currency.Pair{ currency.NewPair(currency.BTC, currency.USDT), }, @@ -2646,7 +2645,7 @@ func TestMatchSymbolWithAvailablePairs(t *testing.T) { b := Base{Name: "test"} whatIWant := currency.NewPair(currency.BTC, currency.USDT) err := b.CurrencyPairs.Store(asset.Spot, ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Available: []currency.Pair{whatIWant}}) if err != nil { t.Fatal(err) @@ -2682,7 +2681,7 @@ func TestMatchSymbolCheckEnabled(t *testing.T) { whatIWant := currency.NewPair(currency.BTC, currency.USDT) availButNoEnabled := currency.NewPair(currency.BTC, currency.AUD) err := b.CurrencyPairs.Store(asset.Spot, ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Available: []currency.Pair{whatIWant, availButNoEnabled}, Enabled: []currency.Pair{whatIWant}, }) @@ -2741,7 +2740,7 @@ func TestIsPairEnabled(t *testing.T) { whatIWant := currency.NewPair(currency.BTC, currency.USDT) availButNoEnabled := currency.NewPair(currency.BTC, currency.AUD) err := b.CurrencyPairs.Store(asset.Spot, ¤cy.PairStore{ - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, Available: []currency.Pair{whatIWant, availButNoEnabled}, Enabled: []currency.Pair{whatIWant}, }) diff --git a/exchanges/order/order_test.go b/exchanges/order/order_test.go index 04c0258d44a..db88086316f 100644 --- a/exchanges/order/order_test.go +++ b/exchanges/order/order_test.go @@ -22,7 +22,7 @@ import ( var errValidationCheckFailed = errors.New("validation check failed") -func TestSubmit_Validate(t *testing.T) { +func TestSubmitValidate(t *testing.T) { t.Parallel() testPair := currency.NewPair(currency.BTC, currency.LTC) tester := []struct { @@ -48,14 +48,10 @@ func TestSubmit_Validate(t *testing.T) { { ExpectedErr: ErrAssetNotSet, - Submit: &Submit{Exchange: "test", Pair: testPair}, - }, // valid pair but invalid asset - { - ExpectedErr: asset.ErrNotSupported, Submit: &Submit{ Exchange: "test", Pair: testPair, - AssetType: 255, + AssetType: asset.Empty, }, }, // valid pair but invalid asset { diff --git a/exchanges/orderbook/simulator/simulator_test.go b/exchanges/orderbook/simulator/simulator_test.go index 6ebb2c34d71..641acee9802 100644 --- a/exchanges/orderbook/simulator/simulator_test.go +++ b/exchanges/orderbook/simulator/simulator_test.go @@ -4,7 +4,6 @@ import ( "context" "testing" - "github.com/thrasher-corp/gocryptotrader/common/convert" "github.com/thrasher-corp/gocryptotrader/currency" "github.com/thrasher-corp/gocryptotrader/exchanges/asset" "github.com/thrasher-corp/gocryptotrader/exchanges/bitstamp" @@ -21,7 +20,7 @@ func TestSimulate(t *testing.T) { }, Pairs: map[asset.Item]*currency.PairStore{ asset.Spot: { - AssetEnabled: convert.BoolPtr(true), + AssetEnabled: true, }, }, } diff --git a/exchanges/sharedtestvalues/sharedtestvalues.go b/exchanges/sharedtestvalues/sharedtestvalues.go index 8f17fd82cc6..2ff0e37cf7a 100644 --- a/exchanges/sharedtestvalues/sharedtestvalues.go +++ b/exchanges/sharedtestvalues/sharedtestvalues.go @@ -2,7 +2,6 @@ package sharedtestvalues import ( "bytes" - "errors" "fmt" "os" "path/filepath" @@ -11,6 +10,7 @@ import ( "testing" "time" + "github.com/stretchr/testify/require" "github.com/thrasher-corp/gocryptotrader/currency" exchange "github.com/thrasher-corp/gocryptotrader/exchanges" "github.com/thrasher-corp/gocryptotrader/exchanges/asset" @@ -154,32 +154,26 @@ func SetupCurrencyPairsForExchangeAsset(t *testing.T, exch exchange.IBotExchange return } b := exch.GetBase() + err := b.CurrencyPairs.SetAssetEnabled(a, true) - if err != nil && !errors.Is(err, currency.ErrAssetAlreadyEnabled) { - t.Fatal(err) - } + require.NoError(t, err, "SetAssetEnabled msut not error") + availPairs, err := b.CurrencyPairs.GetPairs(a, false) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err, "GetPairs must not error") + apLen := len(availPairs) enabledPairs, err := b.CurrencyPairs.GetPairs(a, true) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err, "GetPairs must not error") + epLen := len(enabledPairs) availPairs = availPairs.Add(cp...) enabledPairs = enabledPairs.Add(cp...) if len(availPairs) != apLen { err = b.CurrencyPairs.StorePairs(a, availPairs, false) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err, "StorePairs must not error") } if len(enabledPairs) != epLen { err = b.CurrencyPairs.StorePairs(a, enabledPairs, true) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err, "StorePairs must not error") } }