mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +00:00
Config: AssetEnabled upgrade (#1735)
* Config: Move assetEnabled upgrade to Version management * Assets: Do not error on asset not enabled, or disabled This became more messy with Disabling something that's defaulted to disabled. Taking an idealogical stance against erroring that what you want to have done is already done. * CurrencyManager: Set AssetEnabled when StorePairs(enabled) * RPCServer: Fix tests expecting StoreAssetPairFormat to enable the asset Also assertifies * Bitfinex: Fix tests for MarginFunding subs * GCTWrapper: Improve TestMain clarity * BTSE: Add futures to testconfig * Exchanges: Rename StoreAssetPairStore Previously we were calling it "Format", but accepting everything from the PairStore. We were also defaulting to turning the Asset on. Now callers need to get their AssetEnabled set as they want it, so there's no magic This change also moves responsibility for error wrapping outside to the caller. * Config: AssetEnabled upgrade should respect assetTypes Previously we ignored the field and just turned on everything. I think that was because we couldn't get at the old value. In either case, we have the option to do better, and respect the assetEnabled value * Config: Improve exchange config version upgrade error messages
This commit is contained in:
@@ -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),
|
||||
},
|
||||
@@ -1397,47 +1396,37 @@ func TestGetFormattedPairAndAssetType(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreAssetPairFormat(t *testing.T) {
|
||||
func TestSetAssetPairStore(t *testing.T) {
|
||||
b := Base{
|
||||
Config: &config.Exchange{Name: "kitties"},
|
||||
}
|
||||
|
||||
err := b.StoreAssetPairFormat(asset.Empty, currency.PairStore{})
|
||||
if err == nil {
|
||||
t.Error("error cannot be nil")
|
||||
}
|
||||
err := b.SetAssetPairStore(asset.Empty, currency.PairStore{})
|
||||
assert.ErrorIs(t, err, asset.ErrInvalidAsset)
|
||||
|
||||
err = b.StoreAssetPairFormat(asset.Spot, currency.PairStore{})
|
||||
if err == nil {
|
||||
t.Error("error cannot be nil")
|
||||
}
|
||||
err = b.SetAssetPairStore(asset.Spot, currency.PairStore{})
|
||||
assert.ErrorIs(t, err, currency.ErrPairFormatIsNil)
|
||||
|
||||
err = b.StoreAssetPairFormat(asset.Spot, currency.PairStore{
|
||||
RequestFormat: ¤cy.PairFormat{Uppercase: true}})
|
||||
if err == nil {
|
||||
t.Error("error cannot be nil")
|
||||
}
|
||||
err = b.SetAssetPairStore(asset.Spot, currency.PairStore{RequestFormat: ¤cy.PairFormat{Uppercase: true}})
|
||||
assert.ErrorIs(t, err, currency.ErrPairFormatIsNil)
|
||||
|
||||
err = b.StoreAssetPairFormat(asset.Spot, currency.PairStore{
|
||||
err = b.SetAssetPairStore(asset.Spot, currency.PairStore{
|
||||
RequestFormat: ¤cy.PairFormat{Uppercase: true},
|
||||
ConfigFormat: ¤cy.PairFormat{Uppercase: true}})
|
||||
if !errors.Is(err, errConfigPairFormatRequiresDelimiter) {
|
||||
t.Fatalf("received: '%v' but expected: '%v'", err, errConfigPairFormatRequiresDelimiter)
|
||||
}
|
||||
assert.ErrorIs(t, err, errConfigPairFormatRequiresDelimiter)
|
||||
|
||||
err = b.StoreAssetPairFormat(asset.Futures, currency.PairStore{
|
||||
err = b.SetAssetPairStore(asset.Futures, currency.PairStore{
|
||||
RequestFormat: ¤cy.PairFormat{Uppercase: true},
|
||||
ConfigFormat: ¤cy.PairFormat{Uppercase: true, Delimiter: currency.DashDelimiter}})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, b.CurrencyPairs.Pairs[asset.Futures].AssetEnabled, "SetAssetPairStore should not magically enable AssetTypes")
|
||||
|
||||
err = b.StoreAssetPairFormat(asset.Futures, currency.PairStore{
|
||||
err = b.SetAssetPairStore(asset.Futures, currency.PairStore{
|
||||
AssetEnabled: true,
|
||||
RequestFormat: ¤cy.PairFormat{Uppercase: true},
|
||||
ConfigFormat: ¤cy.PairFormat{Uppercase: true, Delimiter: currency.DashDelimiter}})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, b.CurrencyPairs.Pairs[asset.Futures].AssetEnabled, "AssetEnabled should be respected")
|
||||
}
|
||||
|
||||
func TestSetGlobalPairsManager(t *testing.T) {
|
||||
@@ -1518,7 +1507,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,
|
||||
},
|
||||
@@ -1797,7 +1786,7 @@ func TestFormatSymbol(t *testing.T) {
|
||||
Uppercase: true,
|
||||
},
|
||||
}
|
||||
err := b.StoreAssetPairFormat(asset.Spot, spotStore)
|
||||
err := b.SetAssetPairStore(asset.Spot, spotStore)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -1909,7 +1898,7 @@ func TestAssetWebsocketFunctionality(t *testing.T) {
|
||||
t.Fatalf("expected error: %v but received: %v", asset.ErrNotSupported, err)
|
||||
}
|
||||
|
||||
err = b.StoreAssetPairFormat(asset.Spot, currency.PairStore{
|
||||
err = b.SetAssetPairStore(asset.Spot, currency.PairStore{
|
||||
RequestFormat: ¤cy.PairFormat{
|
||||
Uppercase: true,
|
||||
},
|
||||
@@ -2122,14 +2111,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 +2254,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 +2278,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 +2305,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 +2431,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 +2443,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 +2562,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 +2635,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 +2671,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 +2730,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},
|
||||
})
|
||||
@@ -3130,3 +3119,20 @@ func TestGetTradingRequirements(t *testing.T) {
|
||||
requirements = (&Base{Features: Features{TradingRequirements: protocol.TradingRequirements{ClientOrderID: true}}}).GetTradingRequirements()
|
||||
require.NotEmpty(t, requirements)
|
||||
}
|
||||
|
||||
func TestSetConfigPairFormatFromExchange(t *testing.T) {
|
||||
t.Parallel()
|
||||
b := Base{Config: &config.Exchange{CurrencyPairs: ¤cy.PairsManager{}}}
|
||||
err := b.setConfigPairFormatFromExchange(asset.Spot)
|
||||
assert.ErrorIs(t, err, asset.ErrNotSupported, "setConfigPairFormatFromExchange should error correctly without pairs")
|
||||
err = b.CurrencyPairs.Store(asset.Spot, ¤cy.PairStore{
|
||||
Enabled: currency.Pairs{btcusdPair},
|
||||
ConfigFormat: ¤cy.PairFormat{Delimiter: "🐋"},
|
||||
RequestFormat: ¤cy.PairFormat{Delimiter: "🦥"},
|
||||
})
|
||||
require.NoError(t, err, "CurrencyPairs.Store must not error")
|
||||
err = b.setConfigPairFormatFromExchange(asset.Spot)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "🐋", b.Config.CurrencyPairs.Pairs[asset.Spot].ConfigFormat.Delimiter, "ConfigFormat should be correct and have a blow hole")
|
||||
assert.Equal(t, "🦥", b.Config.CurrencyPairs.Pairs[asset.Spot].RequestFormat.Delimiter, "RequestFormat should be correct and kinda lazy")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user