diff --git a/cmd/rosetta/config.go b/cmd/rosetta/config.go index 9f3e3274..b5346326 100644 --- a/cmd/rosetta/config.go +++ b/cmd/rosetta/config.go @@ -2,22 +2,31 @@ package main import ( "encoding/json" + "fmt" "os" "github.com/multiversx/mx-chain-rosetta/server/resources" ) +func decideCustomCurrencies(configFileCustomCurrencies string) ([]resources.Currency, error) { + if len(configFileCustomCurrencies) == 0 { + return make([]resources.Currency, 0), nil + } + + return loadConfigOfCustomCurrencies(configFileCustomCurrencies) +} + func loadConfigOfCustomCurrencies(configFile string) ([]resources.Currency, error) { fileContent, err := os.ReadFile(configFile) if err != nil { - return nil, err + return nil, fmt.Errorf("error when reading custom currencies config file: %w", err) } var customCurrencies []resources.Currency err = json.Unmarshal(fileContent, &customCurrencies) if err != nil { - return nil, err + return nil, fmt.Errorf("error when loading custom currencies from file: %w", err) } return customCurrencies, nil diff --git a/cmd/rosetta/config_test.go b/cmd/rosetta/config_test.go index 3e407f73..6d6c7978 100644 --- a/cmd/rosetta/config_test.go +++ b/cmd/rosetta/config_test.go @@ -7,6 +7,20 @@ import ( "github.com/stretchr/testify/require" ) +func TestDecideCustomCurrencies(t *testing.T) { + t.Run("with success (file provided)", func(t *testing.T) { + customCurrencies, err := decideCustomCurrencies("testdata/custom-currencies.json") + require.NoError(t, err) + require.Len(t, customCurrencies, 2) + }) + + t.Run("with success (file not provided)", func(t *testing.T) { + customCurrencies, err := decideCustomCurrencies("") + require.NoError(t, err) + require.Empty(t, customCurrencies) + }) +} + func TestLoadConfigOfCustomCurrencies(t *testing.T) { t.Run("with success", func(t *testing.T) { customCurrencies, err := loadConfigOfCustomCurrencies("testdata/custom-currencies.json") @@ -26,11 +40,11 @@ func TestLoadConfigOfCustomCurrencies(t *testing.T) { t.Run("with error (missing file)", func(t *testing.T) { _, err := loadConfigOfCustomCurrencies("testdata/missing-file.json") - require.Error(t, err) + require.ErrorContains(t, err, "error when reading custom currencies config file") }) t.Run("with error (invalid file)", func(t *testing.T) { _, err := loadConfigOfCustomCurrencies("testdata/custom-currencies-bad.json") - require.Error(t, err) + require.ErrorContains(t, err, "error when loading custom currencies from file") }) } diff --git a/cmd/rosetta/main.go b/cmd/rosetta/main.go index 904a6658..fc51576c 100644 --- a/cmd/rosetta/main.go +++ b/cmd/rosetta/main.go @@ -53,7 +53,7 @@ func startRosetta(ctx *cli.Context) error { return err } - customCurrencies, err := loadConfigOfCustomCurrencies(cliFlags.configFileCustomCurrencies) + customCurrencies, err := decideCustomCurrencies(cliFlags.configFileCustomCurrencies) if err != nil { return err }