From 1f3302cbd296116cd6664d969ac102db9b6280d2 Mon Sep 17 00:00:00 2001 From: Eric Date: Tue, 30 Jul 2024 20:50:13 -0700 Subject: [PATCH 1/2] Refactor utils into pure functions --- providers/apis/dydx/api_handler.go | 2 +- providers/apis/dydx/parse.go | 66 ++++++++++++--------- providers/apis/dydx/parse_test.go | 24 ++------ providers/apis/dydx/research_api_handler.go | 2 +- 4 files changed, 46 insertions(+), 48 deletions(-) diff --git a/providers/apis/dydx/api_handler.go b/providers/apis/dydx/api_handler.go index 75777fc98..8e6afbe96 100644 --- a/providers/apis/dydx/api_handler.go +++ b/providers/apis/dydx/api_handler.go @@ -107,7 +107,7 @@ func (h *APIHandler) ParseResponse( } // Convert the dydx market params to a market map. - marketResp, err := h.ConvertMarketParamsToMarketMap(params) + marketResp, err := ConvertMarketParamsToMarketMap(params, h.logger) if err != nil { h.logger.Debug( "failed to convert dydx market params to market map", diff --git a/providers/apis/dydx/parse.go b/providers/apis/dydx/parse.go index b153652b0..a3b5b04d8 100644 --- a/providers/apis/dydx/parse.go +++ b/providers/apis/dydx/parse.go @@ -54,44 +54,51 @@ var ProviderMapping = map[string]string{ } // ConvertMarketParamsToMarketMap converts a dYdX market params response to a slinky market map response. -func (h *APIHandler) ConvertMarketParamsToMarketMap( +func ConvertMarketParamsToMarketMap( params dydxtypes.QueryAllMarketParamsResponse, + logger *zap.Logger, ) (mmtypes.MarketMapResponse, error) { marketMap := mmtypes.MarketMap{ Markets: make(map[string]mmtypes.Market), } for _, market := range params.MarketParams { - ticker, err := h.CreateTickerFromMarket(market) + ticker, err := CreateTickerFromMarket(market) if err != nil { - h.logger.Debug( - "failed to create ticker from market", - zap.String("market", market.Pair), - zap.Error(err), - ) + if logger != nil { + logger.Debug( + "failed to create ticker from market", + zap.String("market", market.Pair), + zap.Error(err), + ) + } return mmtypes.MarketMapResponse{}, fmt.Errorf("failed to create ticker from market: %w", err) } var exchangeConfigJSON dydxtypes.ExchangeConfigJson if err := json.Unmarshal([]byte(market.ExchangeConfigJson), &exchangeConfigJSON); err != nil { - h.logger.Debug( - "failed to unmarshal exchange json config", - zap.String("ticker", ticker.String()), - zap.Error(err), - ) + if logger != nil { + logger.Debug( + "failed to unmarshal exchange json config", + zap.String("ticker", ticker.String()), + zap.Error(err), + ) + } return mmtypes.MarketMapResponse{}, fmt.Errorf("failed to unmarshal exchange json config: %w", err) } // Convert the exchange config JSON to a set of paths and providers. - providers, err := h.ConvertExchangeConfigJSON(exchangeConfigJSON) + providers, err := ConvertExchangeConfigJSON(exchangeConfigJSON, logger) if err != nil { - h.logger.Debug( - "failed to convert exchange config json", - zap.String("ticker", ticker.String()), - zap.Error(err), - ) + if logger != nil { + logger.Debug( + "failed to convert exchange config json", + zap.String("ticker", ticker.String()), + zap.Error(err), + ) + } return mmtypes.MarketMapResponse{}, fmt.Errorf("failed to convert exchange config json: %w", err) } @@ -108,8 +115,8 @@ func (h *APIHandler) ConvertMarketParamsToMarketMap( } // CreateTickerFromMarket creates a ticker from a dYdX market. -func (h *APIHandler) CreateTickerFromMarket(market dydxtypes.MarketParam) (mmtypes.Ticker, error) { - cp, err := h.CreateCurrencyPairFromPair(market.Pair) +func CreateTickerFromMarket(market dydxtypes.MarketParam) (mmtypes.Ticker, error) { + cp, err := CreateCurrencyPairFromPair(market.Pair) if err != nil { return mmtypes.Ticker{}, err } @@ -125,7 +132,7 @@ func (h *APIHandler) CreateTickerFromMarket(market dydxtypes.MarketParam) (mmtyp } // CreateCurrencyPairFromPair creates a currency pair from a dYdX market. -func (h *APIHandler) CreateCurrencyPairFromPair(pair string) (slinkytypes.CurrencyPair, error) { +func CreateCurrencyPairFromPair(pair string) (slinkytypes.CurrencyPair, error) { split := strings.Split(pair, Delimiter) if len(split) != 2 { return slinkytypes.CurrencyPair{}, fmt.Errorf("expected pair (%s) to have 2 elements, got %d", pair, len(split)) @@ -142,8 +149,9 @@ func (h *APIHandler) CreateCurrencyPairFromPair(pair string) (slinkytypes.Curren // ConvertExchangeConfigJSON creates a set of paths and providers for a given ticker // from a dYdX market. These paths represent the different ways to convert a currency // pair using the dYdX market. -func (h *APIHandler) ConvertExchangeConfigJSON( +func ConvertExchangeConfigJSON( config dydxtypes.ExchangeConfigJson, + logger *zap.Logger, ) ([]mmtypes.ProviderConfig, error) { var ( providers = make([]mmtypes.ProviderConfig, 0, len(config.Exchanges)) @@ -161,11 +169,13 @@ func (h *APIHandler) ConvertExchangeConfigJSON( exchange, ok := ProviderMapping[cfg.ExchangeName] if !ok { // ignore unsupported exchanges - h.logger.Debug( - "skipping unsupported exchange", - zap.String("exchange", cfg.ExchangeName), - zap.String("ticker", cfg.Ticker), - ) + if logger != nil { + logger.Debug( + "skipping unsupported exchange", + zap.String("exchange", cfg.ExchangeName), + zap.String("ticker", cfg.Ticker), + ) + } continue } @@ -173,7 +183,7 @@ func (h *APIHandler) ConvertExchangeConfigJSON( // Determine if the exchange needs to have an normalizeByPair. var normalizeByPair *slinkytypes.CurrencyPair if len(cfg.AdjustByMarket) > 0 { - temp, err := h.CreateCurrencyPairFromPair(cfg.AdjustByMarket) + temp, err := CreateCurrencyPairFromPair(cfg.AdjustByMarket) if err != nil { return nil, fmt.Errorf( "failed to create normalize by pair for %s: %w", diff --git a/providers/apis/dydx/parse_test.go b/providers/apis/dydx/parse_test.go index 26535211a..229429ce9 100644 --- a/providers/apis/dydx/parse_test.go +++ b/providers/apis/dydx/parse_test.go @@ -68,10 +68,7 @@ func TestConvertMarketParamsToMarketMap(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - handler, err := dydx.NewAPIHandler(zap.NewNop(), dydx.DefaultAPIConfig) - require.NoError(t, err) - - resp, err := handler.ConvertMarketParamsToMarketMap(tc.params) + resp, err := dydx.ConvertMarketParamsToMarketMap(tc.params, zap.NewNop()) if tc.err { require.Error(t, err) } else { @@ -83,12 +80,9 @@ func TestConvertMarketParamsToMarketMap(t *testing.T) { } func TestCreateCurrencyPairFromMarket(t *testing.T) { - handler, err := dydx.NewAPIHandler(zap.NewNop(), dydx.DefaultAPIConfig) - require.NoError(t, err) - t.Run("good ticker", func(t *testing.T) { pair := "BTC-USD" - cp, err := handler.CreateCurrencyPairFromPair(pair) + cp, err := dydx.CreateCurrencyPairFromPair(pair) require.NoError(t, err) require.Equal(t, cp.Base, "BTC") require.Equal(t, cp.Quote, "USD") @@ -96,13 +90,13 @@ func TestCreateCurrencyPairFromMarket(t *testing.T) { t.Run("bad ticker", func(t *testing.T) { pair := "BTCUSD" - _, err := handler.CreateCurrencyPairFromPair(pair) + _, err := dydx.CreateCurrencyPairFromPair(pair) require.Error(t, err) }) t.Run("lower casing still corrects", func(t *testing.T) { pair := "btc-usd" - cp, err := handler.CreateCurrencyPairFromPair(pair) + cp, err := dydx.CreateCurrencyPairFromPair(pair) require.NoError(t, err) require.Equal(t, cp.Base, "BTC") require.Equal(t, cp.Quote, "USD") @@ -165,10 +159,7 @@ func TestCreateTickerFromMarket(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - handler, err := dydx.NewAPIHandler(zap.NewNop(), dydx.DefaultAPIConfig) - require.NoError(t, err) - - ticker, err := handler.CreateTickerFromMarket(tc.market) + ticker, err := dydx.CreateTickerFromMarket(tc.market) if tc.err { require.Error(t, err) } else { @@ -401,10 +392,7 @@ func TestConvertExchangeConfigJSON(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - handler, err := dydx.NewAPIHandler(zap.NewNop(), dydx.DefaultAPIConfig) - require.NoError(t, err) - - providers, err := handler.ConvertExchangeConfigJSON(tc.config) + providers, err := dydx.ConvertExchangeConfigJSON(tc.config, zap.NewNop()) if tc.expectedErr { require.Error(t, err) return diff --git a/providers/apis/dydx/research_api_handler.go b/providers/apis/dydx/research_api_handler.go index c08ac9e9b..e0918fa39 100644 --- a/providers/apis/dydx/research_api_handler.go +++ b/providers/apis/dydx/research_api_handler.go @@ -118,7 +118,7 @@ func (h *ResearchAPIHandler) ParseResponse( } // convert the response to a market-map - marketMap, err := h.ConvertMarketParamsToMarketMap(respMarketParams) + marketMap, err := ConvertMarketParamsToMarketMap(respMarketParams, h.logger) if err != nil { h.logger.Error("failed to convert QueryAllMarketsParamsResponse into MarketMap", zap.Error(err)) return types.NewMarketMapResponseWithErr( From 2c7418153c4011358af8897601f5f99af7de78db Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 1 Aug 2024 10:07:37 -0700 Subject: [PATCH 2/2] Remove logger --- providers/apis/dydx/api_handler.go | 2 +- providers/apis/dydx/parse.go | 45 ++------------------- providers/apis/dydx/parse_test.go | 6 +-- providers/apis/dydx/research_api_handler.go | 2 +- 4 files changed, 8 insertions(+), 47 deletions(-) diff --git a/providers/apis/dydx/api_handler.go b/providers/apis/dydx/api_handler.go index 8e6afbe96..f82512bd5 100644 --- a/providers/apis/dydx/api_handler.go +++ b/providers/apis/dydx/api_handler.go @@ -107,7 +107,7 @@ func (h *APIHandler) ParseResponse( } // Convert the dydx market params to a market map. - marketResp, err := ConvertMarketParamsToMarketMap(params, h.logger) + marketResp, err := ConvertMarketParamsToMarketMap(params) if err != nil { h.logger.Debug( "failed to convert dydx market params to market map", diff --git a/providers/apis/dydx/parse.go b/providers/apis/dydx/parse.go index a3b5b04d8..bc8d50dea 100644 --- a/providers/apis/dydx/parse.go +++ b/providers/apis/dydx/parse.go @@ -5,8 +5,6 @@ import ( "fmt" "strings" - "go.uber.org/zap" - "github.com/skip-mev/slinky/oracle/constants" slinkytypes "github.com/skip-mev/slinky/pkg/types" "github.com/skip-mev/slinky/providers/apis/bitstamp" @@ -56,7 +54,6 @@ var ProviderMapping = map[string]string{ // ConvertMarketParamsToMarketMap converts a dYdX market params response to a slinky market map response. func ConvertMarketParamsToMarketMap( params dydxtypes.QueryAllMarketParamsResponse, - logger *zap.Logger, ) (mmtypes.MarketMapResponse, error) { marketMap := mmtypes.MarketMap{ Markets: make(map[string]mmtypes.Market), @@ -65,42 +62,18 @@ func ConvertMarketParamsToMarketMap( for _, market := range params.MarketParams { ticker, err := CreateTickerFromMarket(market) if err != nil { - if logger != nil { - logger.Debug( - "failed to create ticker from market", - zap.String("market", market.Pair), - zap.Error(err), - ) - } - - return mmtypes.MarketMapResponse{}, fmt.Errorf("failed to create ticker from market: %w", err) + return mmtypes.MarketMapResponse{}, fmt.Errorf("failed to create ticker from market %s: %w", market.Pair, err) } var exchangeConfigJSON dydxtypes.ExchangeConfigJson if err := json.Unmarshal([]byte(market.ExchangeConfigJson), &exchangeConfigJSON); err != nil { - if logger != nil { - logger.Debug( - "failed to unmarshal exchange json config", - zap.String("ticker", ticker.String()), - zap.Error(err), - ) - } - - return mmtypes.MarketMapResponse{}, fmt.Errorf("failed to unmarshal exchange json config: %w", err) + return mmtypes.MarketMapResponse{}, fmt.Errorf("failed to unmarshal exchange json config for %s: %w", ticker.String(), err) } // Convert the exchange config JSON to a set of paths and providers. - providers, err := ConvertExchangeConfigJSON(exchangeConfigJSON, logger) + providers, err := ConvertExchangeConfigJSON(exchangeConfigJSON) if err != nil { - if logger != nil { - logger.Debug( - "failed to convert exchange config json", - zap.String("ticker", ticker.String()), - zap.Error(err), - ) - } - - return mmtypes.MarketMapResponse{}, fmt.Errorf("failed to convert exchange config json: %w", err) + return mmtypes.MarketMapResponse{}, fmt.Errorf("failed to convert exchange config json for %s: %w", ticker.String(), err) } marketMap.Markets[ticker.String()] = mmtypes.Market{ @@ -151,7 +124,6 @@ func CreateCurrencyPairFromPair(pair string) (slinkytypes.CurrencyPair, error) { // pair using the dYdX market. func ConvertExchangeConfigJSON( config dydxtypes.ExchangeConfigJson, - logger *zap.Logger, ) ([]mmtypes.ProviderConfig, error) { var ( providers = make([]mmtypes.ProviderConfig, 0, len(config.Exchanges)) @@ -168,15 +140,6 @@ func ConvertExchangeConfigJSON( // This means we have seen an exchange that slinky cannot support. exchange, ok := ProviderMapping[cfg.ExchangeName] if !ok { - // ignore unsupported exchanges - if logger != nil { - logger.Debug( - "skipping unsupported exchange", - zap.String("exchange", cfg.ExchangeName), - zap.String("ticker", cfg.Ticker), - ) - } - continue } diff --git a/providers/apis/dydx/parse_test.go b/providers/apis/dydx/parse_test.go index 229429ce9..282847861 100644 --- a/providers/apis/dydx/parse_test.go +++ b/providers/apis/dydx/parse_test.go @@ -5,8 +5,6 @@ import ( "github.com/stretchr/testify/require" - "go.uber.org/zap" - "github.com/skip-mev/slinky/oracle/constants" slinkytypes "github.com/skip-mev/slinky/pkg/types" "github.com/skip-mev/slinky/providers/apis/defi/raydium" @@ -68,7 +66,7 @@ func TestConvertMarketParamsToMarketMap(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - resp, err := dydx.ConvertMarketParamsToMarketMap(tc.params, zap.NewNop()) + resp, err := dydx.ConvertMarketParamsToMarketMap(tc.params) if tc.err { require.Error(t, err) } else { @@ -392,7 +390,7 @@ func TestConvertExchangeConfigJSON(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - providers, err := dydx.ConvertExchangeConfigJSON(tc.config, zap.NewNop()) + providers, err := dydx.ConvertExchangeConfigJSON(tc.config) if tc.expectedErr { require.Error(t, err) return diff --git a/providers/apis/dydx/research_api_handler.go b/providers/apis/dydx/research_api_handler.go index e0918fa39..5cb84dfb5 100644 --- a/providers/apis/dydx/research_api_handler.go +++ b/providers/apis/dydx/research_api_handler.go @@ -118,7 +118,7 @@ func (h *ResearchAPIHandler) ParseResponse( } // convert the response to a market-map - marketMap, err := ConvertMarketParamsToMarketMap(respMarketParams, h.logger) + marketMap, err := ConvertMarketParamsToMarketMap(respMarketParams) if err != nil { h.logger.Error("failed to convert QueryAllMarketsParamsResponse into MarketMap", zap.Error(err)) return types.NewMarketMapResponseWithErr(