Skip to content

Commit

Permalink
Fix loading of custom currencies. Empty list if file not specified.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreibancioiu committed Aug 16, 2024
1 parent 14d5d7a commit 2f99eda
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
5 changes: 3 additions & 2 deletions cmd/rosetta/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"fmt"
"os"

"github.com/multiversx/mx-chain-rosetta/server/resources"
Expand All @@ -10,14 +11,14 @@ import (
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
4 changes: 2 additions & 2 deletions cmd/rosetta/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,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")
})
}
11 changes: 10 additions & 1 deletion cmd/rosetta/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/coinbase/rosetta-sdk-go/server"
"github.com/multiversx/mx-chain-rosetta/server/factory"
"github.com/multiversx/mx-chain-rosetta/server/resources"
"github.com/multiversx/mx-chain-rosetta/version"
"github.com/urfave/cli"
)
Expand Down Expand Up @@ -53,7 +54,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 Expand Up @@ -123,6 +124,14 @@ func startRosetta(ctx *cli.Context) error {
return nil
}

func decideCustomCurrencies(configFileCustomCurrencies string) ([]resources.Currency, error) {
if len(configFileCustomCurrencies) == 0 {
return make([]resources.Currency, 0), nil
}

return loadConfigOfCustomCurrencies(configFileCustomCurrencies)
}

func createHttpServer(port int, routers ...server.Router) (*http.Server, error) {
router := server.NewRouter(
routers...,
Expand Down

0 comments on commit 2f99eda

Please sign in to comment.