Skip to content

Commit

Permalink
avoid parsing if EXCHANGE_SYMBOLS_MAP is not define. (#40)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 7, 2024
1 parent 48905d3 commit caec1f8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
26 changes: 14 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand All @@ -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")
}
Expand Down
14 changes: 14 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"fmt"
"os"
"testing"

Expand All @@ -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)
}

0 comments on commit caec1f8

Please sign in to comment.