Skip to content

Commit

Permalink
Merge pull request #92 from multiversx/fix-config-custom-currencies
Browse files Browse the repository at this point in the history
Fix loading of custom currencies. Empty list if file not specified.
  • Loading branch information
andreibancioiu authored Aug 16, 2024
2 parents 14d5d7a + 3786f54 commit 728b59c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
13 changes: 11 additions & 2 deletions cmd/rosetta/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions cmd/rosetta/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
})
}
2 changes: 1 addition & 1 deletion cmd/rosetta/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 728b59c

Please sign in to comment.