From caec1f8fc9bca4a1edbdf77be610bcf6b0715cc5 Mon Sep 17 00:00:00 2001 From: Mozquito <6951742+Tocard@users.noreply.github.com> Date: Fri, 7 Jun 2024 22:06:32 +0200 Subject: [PATCH] avoid parsing if EXCHANGE_SYMBOLS_MAP is not define. (#40) * avoid parsing if EXCHANGE_SYMBOLS_MAP is not define. Will instead use defaultExchangeSymbolsMap * Apply suggestions from code review Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --------- Co-authored-by: Kevin Yang <5478483+k-yang@users.noreply.github.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- config/config.go | 26 ++++++++++++++------------ config/config_test.go | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/config/config.go b/config/config.go index 1bf6841..b8968d4 100644 --- a/config/config.go +++ b/config/config.go @@ -84,18 +84,20 @@ func Get() (*Config, error) { conf.WebsocketEndpoint = os.Getenv("WEBSOCKET_ENDPOINT") conf.FeederMnemonic = os.Getenv("FEEDER_MNEMONIC") conf.EnableTLS = os.Getenv("ENABLE_TLS") == "true" - overrideExchangeSymbolsMapJson := os.Getenv("EXCHANGE_SYMBOLS_MAP") - overrideExchangeSymbolsMap := map[string]map[string]string{} - err := json.Unmarshal([]byte(overrideExchangeSymbolsMapJson), &overrideExchangeSymbolsMap) - if err != nil { - return nil, fmt.Errorf("failed to parse EXCHANGE_SYMBOLS_MAP: invalid json") - } - conf.ExchangesToPairToSymbolMap = defaultExchangeSymbolsMap - for exchange, symbolMap := range overrideExchangeSymbolsMap { - conf.ExchangesToPairToSymbolMap[exchange] = map[asset.Pair]types.Symbol{} - for nibiAssetPair, tickerSymbol := range symbolMap { - conf.ExchangesToPairToSymbolMap[exchange][asset.MustNewPair(nibiAssetPair)] = types.Symbol(tickerSymbol) + + overrideExchangeSymbolsMapJson := os.Getenv("EXCHANGE_SYMBOLS_MAP") + if overrideExchangeSymbolsMapJson != "" { + overrideExchangeSymbolsMap := map[string]map[string]string{} + err := json.Unmarshal([]byte(overrideExchangeSymbolsMapJson), &overrideExchangeSymbolsMap) + if err != nil { + return nil, fmt.Errorf("failed to parse EXCHANGE_SYMBOLS_MAP: %w", err) + } + for exchange, symbolMap := range overrideExchangeSymbolsMap { + conf.ExchangesToPairToSymbolMap[exchange] = map[asset.Pair]types.Symbol{} + for nibiAssetPair, tickerSymbol := range symbolMap { + conf.ExchangesToPairToSymbolMap[exchange][asset.MustNewPair(nibiAssetPair)] = types.Symbol(tickerSymbol) + } } } @@ -104,7 +106,7 @@ func Get() (*Config, error) { datasourceConfigMap := map[string]json.RawMessage{} if datasourceConfigMapJson != "" { - err = json.Unmarshal([]byte(datasourceConfigMapJson), &datasourceConfigMap) + err := json.Unmarshal([]byte(datasourceConfigMapJson), &datasourceConfigMap) if err != nil { return nil, fmt.Errorf("failed to parse DATASOURCE_CONFIG_MAP: invalid json") } diff --git a/config/config_test.go b/config/config_test.go index 6b74eb7..d7afc1d 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1,6 +1,7 @@ package config import ( + "fmt" "os" "testing" @@ -23,3 +24,16 @@ func TestConfig_Get(t *testing.T) { _, err := Get() require.NoError(t, err) } + +func TestConfig_Without_EXCHANGE_SYMBOLS_MAP(t *testing.T) { + os.Unsetenv("EXCHANGE_SYMBOLS_MAP") + os.Setenv("CHAIN_ID", "nibiru-localnet-0") + os.Setenv("GRPC_ENDPOINT", "localhost:9090") + os.Setenv("WEBSOCKET_ENDPOINT", "ws://localhost:26657/websocket") + os.Setenv("FEEDER_MNEMONIC", "earth wash broom grow recall fitness") + app.SetPrefixes(app.AccountAddressPrefix) + os.Setenv("VALIDATOR_ADDRESS", "nibivaloper1d7zygazerfwx4l362tnpcp0ramzm97xvv9ryxr") + cfg, err := Get() + fmt.Println(cfg) + require.NoError(t, err) +}